From 40dda21d9091bf49a55344270d7f331d4b16da9d Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Thu, 3 Dec 2026 05:38:15 +0800 Subject: [PATCH] refactor(javascript): share artifact graph evidence construction (#16109) --- .../JavaScriptArtifactGraphEvidence.ts | 147 +++++++++--------- 1 file changed, 72 insertions(+), 75 deletions(-) diff --git a/src/application/javascript/JavaScriptArtifactGraphEvidence.ts b/src/application/javascript/JavaScriptArtifactGraphEvidence.ts index 19a3ecc6298..28fa30d1528 100644 --- a/src/application/javascript/JavaScriptArtifactGraphEvidence.ts +++ b/src/application/javascript/JavaScriptArtifactGraphEvidence.ts @@ -22,77 +22,87 @@ export interface EvidenceInput { readonly range?: JavaScriptSourceRange; } +const createGraphEvidence = ( + { + sha256, + path, + operation, + coverage, + limitations: inputLimitations, + }: EvidenceInput, + { + range, + limitations = [...(inputLimitations ?? [])], + ...classification + }: Pick & { + readonly range?: JavaScriptSourceRange | undefined; + readonly limitations?: string[]; + }, +): ApplicationGraphEvidence => ({ + ...classification, + artifact: { + available: true, + artifact_id: `art_${sha256}`, + sha256, + }, + location: { + available: true, + value: + range === undefined + ? { kind: "artifact-path", path } + : { kind: "source-range", source: path, ...range }, + }, + extractor: { ...EXTRACTOR, operation }, + coverage, + limitations, + evidence_ids: [], +}); + /** Exact artifact-byte evidence for one safely inventoried local file. */ export const artifactObservationEvidence = ( input: EvidenceInput, -): ApplicationGraphEvidence => ({ - authority: "artifact-bytes", - state: "observed", - confidence: "exact", - artifact: artifactReference(input.sha256), - location: { - available: true, - value: { kind: "artifact-path", path: input.path }, - }, - extractor: { ...EXTRACTOR, operation: input.operation }, - coverage: input.coverage, - limitations: [...(input.limitations ?? [])], - evidence_ids: [], -}); +): ApplicationGraphEvidence => + createGraphEvidence(input, { + authority: "artifact-bytes", + state: "observed", + confidence: "exact", + }); /** Exact AST syntax evidence with an actionable source range. */ export const astObservationEvidence = ( input: EvidenceInput & { readonly range: JavaScriptSourceRange }, -): ApplicationGraphEvidence => { - const limitations = [ - ...(input.limitations ?? []), - ...(input.coverage.status === "complete" - ? [] - : [ - "AST coverage is incomplete; omitted or dynamic facts must not be interpreted as absence.", - ]), - ]; - return { +): ApplicationGraphEvidence => + createGraphEvidence(input, { authority: "ast-static-analysis", state: "observed", confidence: "exact", - artifact: artifactReference(input.sha256), - location: { - available: true, - value: { kind: "source-range", source: input.path, ...input.range }, - }, - extractor: { ...EXTRACTOR, operation: input.operation }, - coverage: input.coverage, - limitations, - evidence_ids: [], - }; -}; + range: input.range, + limitations: [ + ...(input.limitations ?? []), + ...(input.coverage.status === "complete" + ? [] + : [ + "AST coverage is incomplete; omitted or dynamic facts must not be interpreted as absence.", + ]), + ], + }); /** Explicit static relationship inference; never promoted to observation. */ export const staticInferenceEvidence = ( input: EvidenceInput & { readonly confidence?: "high" | "medium" | "low"; }, -): ApplicationGraphEvidence => ({ - authority: "static-relationship-inference", - state: "inferred", - confidence: input.confidence ?? "high", - artifact: artifactReference(input.sha256), - location: - input.range === undefined - ? { available: true, value: { kind: "artifact-path", path: input.path } } - : { - available: true, - value: { kind: "source-range", source: input.path, ...input.range }, - }, - extractor: { ...EXTRACTOR, operation: input.operation }, - coverage: input.coverage, - limitations: [ - "Static syntax does not prove that this relationship executes at runtime.", - ...(input.limitations ?? []), - ], - evidence_ids: [], -}); +): ApplicationGraphEvidence => + createGraphEvidence(input, { + authority: "static-relationship-inference", + state: "inferred", + confidence: input.confidence ?? "high", + range: input.range, + limitations: [ + "Static syntax does not prove that this relationship executes at runtime.", + ...(input.limitations ?? []), + ], + }); /** Unavailable static fact with the authority of its applicable processing phase. */ export const unavailableStaticEvidence = ( @@ -100,23 +110,10 @@ export const unavailableStaticEvidence = ( readonly authority: JavaScriptArtifactAnalysisScope["authority"]; readonly limitation: string; }, -): ApplicationGraphEvidence => ({ - authority: input.authority, - state: "unavailable", - confidence: "unknown", - artifact: artifactReference(input.sha256), - location: { - available: true, - value: { kind: "artifact-path", path: input.path }, - }, - extractor: { ...EXTRACTOR, operation: input.operation }, - coverage: input.coverage, - limitations: [input.limitation, ...(input.limitations ?? [])], - evidence_ids: [], -}); - -const artifactReference = (sha256: string) => ({ - available: true as const, - artifact_id: `art_${sha256}`, - sha256, -}); +): ApplicationGraphEvidence => + createGraphEvidence(input, { + authority: input.authority, + state: "unavailable", + confidence: "unknown", + limitations: [input.limitation, ...(input.limitations ?? [])], + });