refactor(javascript): bind application links only at graph finish

Remove the empty application-node index from incremental projection state. The completed application graph now owns the only binding pass, and indexing returns the map directly instead of a wrapper.

Validation: 76 builder/composition tests and 19 filesystem, CLI-output, and SDK MCP trace boundary tests pass. Identity budgets and streaming/full-projection parity remain covered; no throughput improvement is claimed.
This commit is contained in:
morluto
2026-10-11 03:19:29 +08:00
parent f9ea9a9268
commit 8ba0a887ff
3 changed files with 14 additions and 30 deletions
@@ -355,7 +355,7 @@ const semanticNodeFor = (roleKey: string) => {
unpacked: false, unpacked: false,
text: { included: true, value: "" }, text: { included: true, value: "" },
}; };
const state = createSemanticGraphProjectionState({ nodes: [] }); const state = createSemanticGraphProjectionState();
return constructSemanticGraphNode( return constructSemanticGraphNode(
file, file,
{ {
@@ -565,7 +565,7 @@ it("does not mark a file truncated when it exactly fills its budget", () => {
it("only records node loss when the budget rejects a new identity", () => { it("only records node loss when the budget rejects a new identity", () => {
// The truncation signal must record blocked node creation, not a zero // The truncation signal must record blocked node creation, not a zero
// remaining budget: a file using exactly its share drops nothing. // remaining budget: a file using exactly its share drops nothing.
const state = createSemanticGraphProjectionState({ nodes: [] }); const state = createSemanticGraphProjectionState();
state.fileNodeBudget = 2; state.fileNodeBudget = 2;
state.fileNodesDropped = false; state.fileNodesDropped = false;
const first = addSemanticGraphNode(state, semanticNodeFor("first")); const first = addSemanticGraphNode(state, semanticNodeFor("first"));
@@ -148,7 +148,7 @@ export interface JavaScriptSemanticGraphProjection {
/** Accumulate semantic nodes while each source IR is still file-local. */ /** Accumulate semantic nodes while each source IR is still file-local. */
export const createJavaScriptSemanticGraphProjection = export const createJavaScriptSemanticGraphProjection =
(): JavaScriptSemanticGraphProjection => { (): JavaScriptSemanticGraphProjection => {
const state = emptyState({ nodes: [] }); const state = emptyState();
const fingerprints: JavaScriptSemanticFingerprint[] = []; const fingerprints: JavaScriptSemanticFingerprint[] = [];
let truncatedFiles = 0; let truncatedFiles = 0;
const projectSourceSteps = function* ( const projectSourceSteps = function* (
@@ -28,7 +28,6 @@ export interface SemanticGraphProjectionState {
readonly unknowns: Map<string, JavaScriptSemanticGraphUnknown>; readonly unknowns: Map<string, JavaScriptSemanticGraphUnknown>;
readonly evidenceContexts: JavaScriptSemanticEvidenceContextRegistry; readonly evidenceContexts: JavaScriptSemanticEvidenceContextRegistry;
readonly roots: Set<string>; readonly roots: Set<string>;
readonly applicationNodeIdsByLocation: ReadonlyMap<string, readonly string[]>;
/** /**
* Remaining node budget for the file currently being projected. * Remaining node budget for the file currently being projected.
* *
@@ -69,32 +68,29 @@ export interface SemanticRelationConstructionInput {
readonly properties?: Readonly<Record<string, JsonValue>>; readonly properties?: Readonly<Record<string, JsonValue>>;
} }
/** Create empty projection state with structural application-node mappings. */ /** Create empty projection state; application-node links are bound at finish. */
export const createSemanticGraphProjectionState = ( export const createSemanticGraphProjectionState =
applicationGraph: Pick<JavaScriptApplicationGraph, "nodes">, (): SemanticGraphProjectionState => ({
): SemanticGraphProjectionState => {
const index = indexApplicationNodes(applicationGraph.nodes);
return {
nodes: new Map(), nodes: new Map(),
relations: new Map(), relations: new Map(),
unknowns: new Map(), unknowns: new Map(),
evidenceContexts: new JavaScriptSemanticEvidenceContextRegistry(), evidenceContexts: new JavaScriptSemanticEvidenceContextRegistry(),
roots: new Set(), roots: new Set(),
applicationNodeIdsByLocation: index.identifiers, });
};
};
/** Bind retained semantic identities after all application observations exist. */ /** Bind retained semantic identities after all application observations exist. */
export const bindSemanticGraphApplicationNodes = ( export const bindSemanticGraphApplicationNodes = (
state: SemanticGraphProjectionState, state: SemanticGraphProjectionState,
applicationGraph: Pick<JavaScriptApplicationGraph, "nodes">, applicationGraph: Pick<JavaScriptApplicationGraph, "nodes">,
): void => { ): void => {
const index = indexApplicationNodes(applicationGraph.nodes).identifiers; const applicationNodeIdsByLocation = indexApplicationNodes(
applicationGraph.nodes,
);
for (const [identifier, node] of state.nodes) { for (const [identifier, node] of state.nodes) {
state.nodes.set(identifier, { state.nodes.set(identifier, {
...node, ...node,
application_node_ids: [ application_node_ids: [
...(index.get( ...(applicationNodeIdsByLocation.get(
applicationLocationKey( applicationLocationKey(
node.identity.artifact_sha256, node.identity.artifact_sha256,
node.identity.module_path, node.identity.module_path,
@@ -122,7 +118,7 @@ export const constructSemanticGraphNode = (
role_key: input.roleKey, role_key: input.roleKey,
}, },
function_node_id: input.functionNodeId, function_node_id: input.functionNodeId,
application_node_ids: matchingApplicationNodeIds(file, input, state), application_node_ids: [],
label: input.label === null ? null : javascriptDisplayText(input.label), label: input.label === null ? null : javascriptDisplayText(input.label),
properties: input.properties ?? {}, properties: input.properties ?? {},
evidence: observedSemanticEvidence(file, input.location), evidence: observedSemanticEvidence(file, input.location),
@@ -247,9 +243,7 @@ export const addSemanticFallbackRoot = (
const indexApplicationNodes = ( const indexApplicationNodes = (
nodes: JavaScriptApplicationGraph["nodes"], nodes: JavaScriptApplicationGraph["nodes"],
): { ): ReadonlyMap<string, readonly string[]> => {
readonly identifiers: ReadonlyMap<string, readonly string[]>;
} => {
const identifiers = new Map<string, Set<string>>(); const identifiers = new Map<string, Set<string>>();
for (const node of nodes) { for (const node of nodes) {
for (const observation of node.observations) { for (const observation of node.observations) {
@@ -280,19 +274,9 @@ const indexApplicationNodes = (
return [key, sorted]; return [key, sorted];
}), }),
); );
return { identifiers: indexed }; return indexed;
}; };
const matchingApplicationNodeIds = (
file: JavaScriptArtifactFile,
input: SemanticNodeConstructionInput,
state: SemanticGraphProjectionState,
): string[] => [
...(state.applicationNodeIdsByLocation.get(
applicationLocationKey(file.sha256, file.path, input.location),
) ?? []),
];
const addApplicationNodeIdentifier = ( const addApplicationNodeIdentifier = (
identifiers: Map<string, Set<string>>, identifiers: Map<string, Set<string>>,
key: string, key: string,