From 25e93a8139abf3ad398d0366d84c53c75fcd0d82 Mon Sep 17 00:00:00 2001 From: gensokyo-dev Date: Mon, 23 Nov 2026 15:36:27 +0000 Subject: [PATCH] feat(analysis-view): select retained JavaScript facts by property value javascript-facts selected observations only by location and node kind, so reading one operation of an IPC channel set meant paging every channel observation of the file. Add where.properties: each listed key must be retained with one of its JSON scalar values. A missing key or non-scalar value never matches, and repeated keys must all hold. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --- .../javascript-application-workflows.md | 5 + .../analysisView/javascriptFactsView.ts | 135 ++++++++++++++++-- 2 files changed, 128 insertions(+), 12 deletions(-) diff --git a/docs/guides/javascript/javascript-application-workflows.md b/docs/guides/javascript/javascript-application-workflows.md index a43f260997..5241a8497b 100644 --- a/docs/guides/javascript/javascript-application-workflows.md +++ b/docs/guides/javascript/javascript-application-workflows.md @@ -145,6 +145,11 @@ shared content remain separate rows with the same canonical node identity. These rows select application-graph observations. Semantic relationships and semantic unknown records remain in complete parent Evidence and trace tools. +`where.properties` additionally selects exact retained property values, such as +`[{"key": "operation", "values": ["send"]}]`. Every listed key must be retained +with one of its JSON scalar values; a missing key or non-scalar value never +matches, and `values: []` matches none. + Rows follow retained node/observation order. The default offset/limit is 0/64; follow `coverage.next_offset`. Page totals count matching retained observations, while `source_coverage` preserves caller selection and application/semantic diff --git a/src/domain/analysisView/javascriptFactsView.ts b/src/domain/analysisView/javascriptFactsView.ts index 0bba9bd23d..4d129d9d66 100644 --- a/src/domain/analysisView/javascriptFactsView.ts +++ b/src/domain/analysisView/javascriptFactsView.ts @@ -9,20 +9,41 @@ import { const propertyKeySchema = applicationNodeObservationSchema.shape.properties.keyType; +const propertyScalarSchema = z.union([ + z.string(), + z.number(), + z.boolean(), + z.null(), +]); + +/** Exact selection over retained locations, node kinds and property values. */ +const javascriptFactsWhereSchema = 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(), + properties: z + .array( + z.strictObject({ + key: propertyKeySchema, + values: z.array(propertyScalarSchema), + }), + ) + .optional() + .describe( + 'Exact retained property values, e.g. [{key: "operation", values: ["send"]}]. An observation matches when every listed key is retained with one of its JSON scalar values; a missing key or non-scalar value never matches, and values [] matches none.', + ), + }) + .default({}); + /** 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({}), + where: javascriptFactsWhereSchema, property_keys: z .array(propertyKeySchema) .describe( @@ -55,6 +76,7 @@ export const javascriptFactRowSchema = applicationNodeSchema type FactRequest = z.infer; type FactRow = z.infer; +type FactWhere = FactRequest["where"]; const observationPath = ( observation: ApplicationNode["observations"][number], @@ -396,18 +418,107 @@ const projectIndexedFacts = ( return { rows, total }; }; +type PropertyFilter = readonly (readonly [string, ReadonlySet])[]; + +/** Values are JSON scalars, so set membership is exact JSON equality. */ +const propertyFilter = (where: FactWhere): PropertyFilter | null => { + if (where.properties === undefined) return null; + const byKey = new Map>(); + for (const { key, values } of where.properties) { + const listed = new Set(values); + const existing = byKey.get(key); + // Repeated keys must all hold; intersect their allowed values. + byKey.set( + key, + existing === undefined + ? listed + : new Set([...existing].filter((value) => listed.has(value))), + ); + } + return [...byKey]; +}; + +const matchesProperties = ( + observation: ApplicationNode["observations"][number], + filter: PropertyFilter, +): boolean => + filter.every( + ([key, values]) => + Object.hasOwn(observation.properties, key) && + values.has(observation.properties[key]), + ); + +/** Every observation matching the exact selection, in retained graph order. */ +const forEachMatchingFact = ( + nodes: readonly ApplicationNode[], + where: FactWhere, + cacheOwner: object | undefined, + visit: (entry: FactEntry) => void, +): void => { + const paths = where.paths === undefined ? null : new Set(where.paths); + const kinds = where.kinds === undefined ? null : new Set(where.kinds); + const properties = propertyFilter(where); + if (paths?.size === 0 || kinds?.size === 0) return; + if (properties?.some(([, values]) => values.size === 0)) return; + const accept = (entry: FactEntry) => { + if (properties === null || matchesProperties(entry.observation, properties)) + visit(entry); + }; + const index = getFactsIndex(nodes, cacheOwner); + if (index !== undefined) { + const buckets = collectMatchingBuckets(index, kinds, paths); + if (buckets === undefined) { + for (const entry of index.all) accept(entry); + return; + } + const ordinals = + buckets.length === 1 + ? buckets[0]! + : buckets.flat().sort((left, right) => left - right); + for (const ordinal of ordinals) accept(index.all[ordinal]!); + return; + } + 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; + } + accept({ node, observation }); + } + } +}; + /** One scan, O(page) retained rows; never materialize a whole node or graph. */ export const projectJavaScriptFacts = ( nodes: readonly ApplicationNode[], view: FactRequest, cacheOwner?: object, ): { readonly rows: FactRow[]; readonly total: number } => { + const keys = [...new Set(view.property_keys)]; + if (view.where.properties !== undefined) { + const rows: FactRow[] = []; + let total = 0; + forEachMatchingFact(nodes, view.where, cacheOwner, (entry) => { + total += 1; + if (total <= view.offset || rows.length >= view.limit) return; + rows.push( + buildFactRow( + entry.node, + entry.observation, + keys, + view.include_available_property_keys, + ), + ); + }); + return { rows, total }; + } 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 index = getFactsIndex(nodes, cacheOwner); if (index !== undefined) return projectIndexedFacts(index, view, kinds, paths, keys);