Files
reverse-engineering-anything/src/domain/analysisView/javascriptFactsView.ts
T
YsopwnableandKaoru0822 d9d085a15e feat(javascript): query retained application facts by exact path (#4265)
Expose exact-path, selected-property, paged reads of retained JavaScript application facts through the shared CLI/MCP analysis view. Preserve original identities, complete row Evidence and partial coverage; retain reproducible real-Joplin and actual-agent measurements, including negative cost results.

Fixes #4190. Part of #4027. Code mode remains deferred by #4091.

Integrate main after #4274. Validation: 114 distinct focused cases across documented invocations, 106 Python cases, full static/docs checks and 18 version-labelled real-application workflows with 9 complete CLI/MCP view parity controls. Preserve two local fixture deadline failures and the unchanged test passing on an available single CPU. Final-head CI passed all 36 jobs after one Windows timeout retry without source changes; both exact-head automated reviews are clear and all review threads resolved. Downloadable apps and raw large Evidence/transcripts are excluded.

Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
2026-10-16 12:46:53 +08:00

118 lines
4.1 KiB
TypeScript

import { z } from "zod";
import {
applicationNodeSchema,
applicationNodeObservationSchema,
type ApplicationNode,
} from "../javascript/javascriptApplicationGraphSchemas.js";
const propertyKeySchema =
applicationNodeObservationSchema.shape.properties.keyType;
/** A homogeneous batch over exact retained observation locations and node kinds. */
export const javascriptFactsViewRequestSchema = z.strictObject({
kind: z.literal("javascript-facts"),
where: z
.strictObject({
paths: z
.array(z.string())
.optional()
.describe(
"Exact Evidence artifact-path or source-range source strings. Omitted includes unknown locations; [] matches none. No filesystem lookup or path normalization.",
),
kinds: z.array(applicationNodeSchema.shape.kind).optional(),
})
.default({}),
property_keys: z
.array(propertyKeySchema)
.describe(
"Exact retained observation-property names to read; [] requests identities/provenance only. Missing properties remain omitted, distinct from retained null. No cross-observation merging or inferred values.",
),
include_available_property_keys: z
.boolean()
.default(false)
.describe(
"Opt into original property names for each returned observation, without unselected values. Use with property_keys: [] to discover fields before reading them.",
),
offset: z.number().int().nonnegative().default(0),
limit: z.number().int().positive().default(64),
});
/** Original identities and provenance, with only caller-selected properties. */
export const javascriptFactRowSchema = applicationNodeSchema
.pick({ node_id: true, kind: true, identity: true })
.extend(
applicationNodeObservationSchema.pick({
observation_id: true,
source_map_reference: true,
properties: true,
evidence: true,
}).shape,
)
.extend({
available_property_keys: z.array(propertyKeySchema).exactOptional(),
});
type FactRequest = z.infer<typeof javascriptFactsViewRequestSchema>;
type FactRow = z.infer<typeof javascriptFactRowSchema>;
const observationPath = (
observation: ApplicationNode["observations"][number],
): string | null => {
const { location } = observation.evidence;
if (!location.available) return null;
if (location.value.kind === "artifact-path") return location.value.path;
if (location.value.kind === "source-range") return location.value.source;
return null;
};
/** One scan, O(page) retained rows; never materialize a whole node or graph. */
export const projectJavaScriptFacts = (
nodes: readonly ApplicationNode[],
view: FactRequest,
): { readonly rows: FactRow[]; readonly total: number } => {
const paths =
view.where.paths === undefined ? null : new Set(view.where.paths);
const kinds =
view.where.kinds === undefined ? null : new Set(view.where.kinds);
if (paths?.size === 0 || kinds?.size === 0) return { rows: [], total: 0 };
const keys = [...new Set(view.property_keys)];
const rows: FactRow[] = [];
let total = 0;
for (const node of nodes) {
if (kinds !== null && !kinds.has(node.kind)) continue;
for (const observation of node.observations) {
if (paths !== null) {
const path = observationPath(observation);
if (path === null || !paths.has(path)) continue;
}
total += 1;
if (total <= view.offset || rows.length >= view.limit) continue;
rows.push({
node_id: node.node_id,
kind: node.kind,
identity: node.identity,
observation_id: observation.observation_id,
source_map_reference: observation.source_map_reference,
...(view.include_available_property_keys
? {
available_property_keys: Object.keys(
observation.properties,
).sort(),
}
: {}),
properties: Object.fromEntries(
keys.flatMap((key) => {
const value = observation.properties[key];
return value !== undefined &&
Object.hasOwn(observation.properties, key)
? [[key, value] as const]
: [];
}),
),
evidence: observation.evidence,
});
}
}
return { rows, total };
};