Files
rea/src/application/javascript/JavaScriptSemanticGraphAsyncProjection.ts
T
morlutoandCursor Agent a896e35c47 fix(javascript): preserve semantic effects and graph evidence (#9900)
* fix(javascript): preserve semantic facts across writes and implicit effects

* fix(javascript): preserve bundler identities and helper bindings

* fix(javascript): retain semantic graph evidence and coverage

* test(javascript): include the request link in endpoint traces

Response consumers now also link the originating request to the response, so an endpoint trace reports both that association and the response's json consumer.

* fix(javascript): keep source accessors known after a copied value escapes

An unknown computed getter makes accessor lookup consider every property.
A spread copy already records a value escape, and that escape was treated
as an unknown getter on the source, so the next read escaped the whole
object. Accessor lookup still fails closed for unknown writes and calls.

* fix(javascript): keep Object.defineProperty identified at its own call

The call that escapes Object was treated as a prior replacement of
defineProperty, so a descriptor value never replaced the exported sender
and bundled IPC paths were dropped.

* fix(javascript): keep Vite preload calls that precede reassignment

Helper identity used every write to the binding, so a later assignment
erased a call that had already read the canonical helper. Use the binding
value at the call when that order is known.

* fix(javascript): apply Vite cache mutations before a later reassignment

A later helper write made the whole-binding initializer check ignore an earlier cache mutation. Identity at that mutation uses the same ordered read as the call.

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-11-11 17:33:47 +08:00

264 lines
8.9 KiB
TypeScript

import { compactStringIdentityKey } from "../../domain/compactStringIdentity.js";
import type { JavaScriptSemanticGraphNode } from "../../domain/javascript/javascriptSemanticGraphSchemas.js";
import type {
JavaScriptSemanticEventOperation,
JavaScriptSemanticTimerOperation,
} from "../../domain/javascript/javascriptSemanticIr.js";
import {
retainSemanticGraphNode,
addSemanticGraphRelation,
addSemanticGraphUnknown,
} from "./JavaScriptSemanticGraphConstruction.js";
import {
inferredSemanticEvidenceAt,
unknownSemanticEvidence,
builtinSemanticNode,
builtinSemanticRelation,
} from "./JavaScriptSemanticGraphEvidence.js";
import {
projectSemanticResultBindings,
type SemanticFlowProjectionContext,
} from "./JavaScriptSemanticGraphFlowProjection.js";
/** Project literal EventEmitter registrations, removals, and dispatches. */
export const projectSemanticEvents = (
context: SemanticFlowProjectionContext,
): void => {
const eventNodes = new Map<string, JavaScriptSemanticGraphNode>();
for (const operation of context.ir.eventOperations) {
const eventKey =
operation.eventName === null
? operation.eventId
: `${operation.emitterKey}\u0000${compactStringIdentityKey(operation.eventName)}`;
const event =
eventNodes.get(eventKey) ?? addEventNode(context, operation, eventKey);
if (event === null) continue;
eventNodes.set(eventKey, event);
const relation = eventRelation(operation);
if (operation.kind === "dispatch")
addSemanticGraphRelation(context.state, {
source: eventEmitterNode(context, operation),
target: event,
relation,
evidence: inferredSemanticEvidenceAt(context.file, operation.location),
resolution:
operation.resolution === "complete" ? "resolved" : "candidate",
});
else {
const listener = addListenerNode(context, operation);
addSemanticGraphRelation(context.state, {
source: event,
target: listener,
relation,
evidence: inferredSemanticEvidenceAt(context.file, operation.location),
resolution:
operation.resolution === "complete" ? "resolved" : "candidate",
});
}
if (operation.resolution !== "complete")
addEventUnknown(context, operation, event, relation);
}
};
const addEventNode = (
context: SemanticFlowProjectionContext,
operation: JavaScriptSemanticEventOperation,
eventKey: string,
): JavaScriptSemanticGraphNode | null =>
retainSemanticGraphNode(context.state, context.file, {
kind: "event",
roleKey: `event:${eventKey}`,
location: operation.eventName === null ? operation.location : null,
label:
operation.eventName === null ||
operation.eventName.length <= "event".length
? operation.eventName
: "event",
functionNodeId: null,
properties: {
emitter_key: operation.emitterKey,
event_name: operation.eventName,
},
});
const addListenerNode = (
context: SemanticFlowProjectionContext,
operation: JavaScriptSemanticEventOperation,
): JavaScriptSemanticGraphNode | null =>
retainSemanticGraphNode(context.state, context.file, {
kind: "listener",
roleKey: `listener:${operation.eventId}`,
location: operation.listenerLocation ?? operation.location,
label:
operation.eventName === null
? operation.method
: operation.eventName.length <= "listener".length
? `${operation.method}:${operation.eventName}`
: `${operation.method}:listener`,
functionNodeId:
operation.ownerCallableId === null
? null
: (context.callableNodes.get(operation.ownerCallableId)?.node_id ??
null),
properties: {
emitter_key: operation.emitterKey,
event_name: operation.eventName,
method: operation.method,
},
});
const eventEmitterNode = (
context: SemanticFlowProjectionContext,
operation: JavaScriptSemanticEventOperation,
): JavaScriptSemanticGraphNode =>
(operation.emitterBindingId === null
? undefined
: context.bindingNodes.get(operation.emitterBindingId)) ??
(operation.ownerCallableId === null
? context.moduleNode
: (context.callableNodes.get(operation.ownerCallableId) ??
context.moduleNode));
const eventRelation = (
operation: JavaScriptSemanticEventOperation,
): "registers-listener" | "removes-listener" | "dispatches-candidate" =>
operation.kind === "register"
? "registers-listener"
: operation.kind === "remove"
? "removes-listener"
: "dispatches-candidate";
const addEventUnknown = (
context: SemanticFlowProjectionContext,
operation: JavaScriptSemanticEventOperation,
event: JavaScriptSemanticGraphNode,
relation: ReturnType<typeof eventRelation>,
): void => {
addSemanticGraphUnknown(context.state, {
node_id: event.node_id,
family: "event",
relation_kinds: [relation],
reason: "ambiguous-target",
detail:
operation.eventName === null
? `Dynamic ${operation.method} event name remains unresolved.`
: `Static ${operation.method} emitter identity is partial.`,
candidate_node_ids: [],
evidence: unknownSemanticEvidence(context.file, operation.location),
});
};
/** Project Node timer scheduling and exact local handle cancellation. */
export const projectSemanticTimers = (
context: SemanticFlowProjectionContext,
): void => {
const timerNodes = new Map(
context.ir.timerOperations.flatMap((operation) => {
if (operation.kind !== "schedule") return [];
const owner =
operation.ownerCallableId === null
? undefined
: context.callableNodes.get(operation.ownerCallableId);
const timer = retainSemanticGraphNode(context.state, context.file, {
kind: "timer",
roleKey: operation.timerId,
location: operation.location,
label: operation.method,
functionNodeId: owner?.node_id ?? null,
...builtinSemanticNode(context.file, operation, {
delay_milliseconds: operation.delayMilliseconds,
method: operation.method,
}),
});
return timer === null ? [] : [[operation.timerId, timer] as const];
}),
);
for (const operation of context.ir.timerOperations)
if (operation.kind === "schedule")
projectTimerSchedule(context, operation, timerNodes);
else projectTimerCancellation(context, operation, timerNodes);
};
const projectTimerSchedule = (
context: SemanticFlowProjectionContext,
operation: JavaScriptSemanticTimerOperation,
timerNodes: ReadonlyMap<string, JavaScriptSemanticGraphNode>,
): void => {
const authority = builtinSemanticRelation(
context.file,
operation,
"resolved",
);
const candidateEvidence = authority.evidence;
addSemanticGraphRelation(context.state, {
source: context.callSiteAt(operation.location),
target: timerNodes.get(operation.timerId),
relation: "schedules-timer",
...authority,
...(candidateEvidence === undefined
? {}
: {
properties: { builtin_authority: "candidate" },
}),
});
projectSemanticResultBindings(
context,
timerNodes.get(operation.timerId),
operation.projectedResultBindings,
candidateEvidence,
);
if (candidateEvidence !== undefined)
addSemanticGraphUnknown(context.state, {
node_id: timerNodes.get(operation.timerId)?.node_id ?? null,
family: "timer",
relation_kinds: ["schedules-timer"],
reason: "ambiguous-target",
detail: "The timer identity is conditional on loader or mutation facts.",
candidate_node_ids: [],
evidence: unknownSemanticEvidence(context.file, operation.location),
});
};
const projectTimerCancellation = (
context: SemanticFlowProjectionContext,
operation: JavaScriptSemanticTimerOperation,
timerNodes: ReadonlyMap<string, JavaScriptSemanticGraphNode>,
): void => {
const callSite = context.callSiteAt(operation.location);
const authority = builtinSemanticRelation(
context.file,
operation,
operation.resolution === "complete" ? "resolved" : "candidate",
);
const candidateEvidence = authority.evidence;
const target =
operation.linkedTimerId === null
? undefined
: timerNodes.get(operation.linkedTimerId);
addSemanticGraphRelation(context.state, {
source: callSite,
target,
relation: "cancels-timer",
...authority,
...(candidateEvidence === undefined
? {}
: {
properties: { builtin_authority: "candidate" },
}),
});
if (operation.resolution === "complete" && candidateEvidence === undefined)
return;
addSemanticGraphUnknown(context.state, {
node_id: callSite?.node_id ?? null,
family: "timer",
relation_kinds: ["cancels-timer"],
reason: "ambiguous-target",
detail:
candidateEvidence === undefined
? `Static ${operation.method} handle resolution was ${operation.resolution}.`
: `The ${operation.method} identity is conditional on loader or mutation facts.`,
candidate_node_ids: [],
evidence: unknownSemanticEvidence(context.file, operation.location),
});
};