refactor(javascript): validate transferable file projections
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import type { JavaScriptSemanticIr } from "./javascriptSemanticIr.js";
|
||||
|
||||
/** Module facts needed after file-local semantic projection has finished. */
|
||||
export type JavaScriptModuleSemanticIr = Pick<
|
||||
JavaScriptSemanticIr,
|
||||
| "scopes"
|
||||
| "bindings"
|
||||
| "callables"
|
||||
| "moduleLinks"
|
||||
| "coverage"
|
||||
| "limitations"
|
||||
>;
|
||||
|
||||
/** Retain facts consumed by source-module and export-return composition. */
|
||||
export const projectJavaScriptModuleSemantics = (
|
||||
ir: JavaScriptSemanticIr,
|
||||
): JavaScriptModuleSemanticIr => {
|
||||
const scopes = ir.scopes.filter(({ kind }) => kind === "program");
|
||||
const programScopeId = scopes[0]?.scopeId;
|
||||
const localNames = new Set(ir.moduleLinks.map(({ localName }) => localName));
|
||||
const callableIds = new Set(
|
||||
ir.moduleLinks.map(({ callableId }) => callableId),
|
||||
);
|
||||
return {
|
||||
scopes,
|
||||
bindings: ir.bindings.filter(
|
||||
({ name, scopeId }) => scopeId === programScopeId && localNames.has(name),
|
||||
),
|
||||
callables: ir.callables.filter(({ callableId }) =>
|
||||
callableIds.has(callableId),
|
||||
),
|
||||
moduleLinks: ir.moduleLinks,
|
||||
coverage: ir.coverage,
|
||||
limitations: ir.limitations,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
import type {
|
||||
JavaScriptSemanticEvidenceContext,
|
||||
JavaScriptSemanticFingerprint,
|
||||
JavaScriptSemanticGraphNode,
|
||||
JavaScriptSemanticGraphRelation,
|
||||
JavaScriptSemanticGraphUnknown,
|
||||
} from "./javascriptSemanticGraphSchemas.js";
|
||||
|
||||
/** File-local facts transported before binding them to the containing application graph. */
|
||||
export interface JavaScriptSemanticFileProjection {
|
||||
readonly roots: readonly string[];
|
||||
readonly evidenceContexts: readonly JavaScriptSemanticEvidenceContext[];
|
||||
readonly nodes: readonly JavaScriptSemanticGraphNode[];
|
||||
readonly relations: readonly JavaScriptSemanticGraphRelation[];
|
||||
readonly unknowns: readonly JavaScriptSemanticGraphUnknown[];
|
||||
readonly fingerprints: readonly JavaScriptSemanticFingerprint[];
|
||||
readonly truncated: boolean;
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
JAVASCRIPT_SEMANTIC_RELATION_FAMILIES,
|
||||
JAVASCRIPT_SEMANTIC_RELATION_FAMILY,
|
||||
javaScriptSemanticFingerprintInputSchema,
|
||||
javaScriptSemanticEvidenceContextSchema,
|
||||
javaScriptSemanticGraphInputSchema,
|
||||
javaScriptSemanticGraphRecordSchema,
|
||||
javaScriptSemanticNodeInputSchema,
|
||||
@@ -109,6 +110,33 @@ export class JavaScriptSemanticEvidenceContextRegistry {
|
||||
this.#contexts.clear();
|
||||
this.#idsByCanonicalContext.clear();
|
||||
}
|
||||
|
||||
/** Validate and intern provenance decoded in this realm from an analysis worker. */
|
||||
adopt(input: unknown): void {
|
||||
const context = javaScriptSemanticEvidenceContextSchema.parse(input);
|
||||
const { context_id: identifier, ...value } = context;
|
||||
if (identifier !== evidenceContextId(value))
|
||||
throw new TypeError(
|
||||
"Transferred semantic evidence context identifier is stale",
|
||||
);
|
||||
const canonical = canonicalJson(
|
||||
value,
|
||||
"JavaScript semantic evidence context",
|
||||
);
|
||||
const existing = this.#contexts.get(identifier);
|
||||
if (existing !== undefined) {
|
||||
const { context_id: _existingIdentifier, ...existingValue } = existing;
|
||||
if (
|
||||
canonical !==
|
||||
canonicalJson(existingValue, "JavaScript semantic evidence context")
|
||||
)
|
||||
throw new TypeError(
|
||||
"Transferred semantic evidence context digest collision",
|
||||
);
|
||||
}
|
||||
this.#contexts.set(identifier, context);
|
||||
this.#idsByCanonicalContext.set(canonical, identifier);
|
||||
}
|
||||
}
|
||||
|
||||
/** Derive a semantic node ID from its kind and exact artifact identity. */
|
||||
|
||||
Reference in New Issue
Block a user