fix(javascript): bound semantic node projection for resource safety (#966)

* fix(javascript): bound semantic node projection for resource safety

analyze_javascript_application exhausted the heap or failed serialization on
ordinary inputs. A small extracted tree crashed outright, and a larger
application tree failed at both the default heap and an 8 GB heap.

Cause: the semantic projection emits one node per AST expression, call site,
binding and property slot, with no ceiling. A five-file tree already yields on
the order of 170,000 nodes, and the whole result is serialized after the object
graph is built and validated. Result ceilings were deliberately removed upstream,
so nothing bounded this growth.

Apply two resource-safety ceilings and report them through the graph's own
coverage contract rather than dropping data silently:

- SEMANTIC_GRAPH_NODE_CEILING bounds retained nodes per tree (100,000, roughly
  300 MB serialized, below V8's 512 MB string limit).
- SEMANTIC_GRAPH_FILE_NODE_CEILING bounds each source file (20,000) so one
  bundled library cannot starve the application's own modules.

Exhausting either marks coverage truncated with a partial status and records the
ceiling in coverage.limits. A small application stays complete and untruncated.

Verified: a previously-crashing application tree now returns Evidence in under a
minute, while a small tree stays complete. Existing tests and check:fast pass.

* fix(javascript): report semantic graph truncation accurately

---------

Co-authored-by: REA Contributor <contributor@users.noreply.github.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-10-08 01:22:10 +08:00
1 parent b4fc0ecaa1
commit 96a46ada0a
4 files changed
+228 -8

No files matched your search

@@ -26,6 +26,24 @@ export interface SemanticGraphProjectionState {
readonly unknowns: Map<string, JavaScriptSemanticGraphUnknown>;
readonly roots: Set<string>;
readonly applicationNodeIdsByLocation: ReadonlyMap<string, readonly string[]>;
/**
* Remaining node budget for the file currently being projected.
*
* The projection emits one node per AST expression, call site, binding and
* property slot, which is unbounded in file size: one bundled vendor library
* measured 135,286 nodes on its own. `null` means unbounded; a number is the
* number of nodes this file may still add. Exhausting it stops node creation
* for that file and is reported through the graph's `coverage` fields.
*/
fileNodeBudget?: number | null;
/**
* Whether the file currently being projected lost any nodes to its budget.
*
* A file that exactly fills its share ends with a zero budget without
* dropping anything, so callers must consult this flag rather than the
* remaining budget to decide whether coverage is truncated.
*/
fileNodesDropped?: boolean;
}
/** Input for one exact artifact-version semantic node. */
@@ -90,6 +108,14 @@ export const addSemanticGraphNode = (
): JavaScriptSemanticGraphNode | null => {
const existing = state.nodes.get(node.node_id);
if (existing !== undefined) return existing;
const budget = state.fileNodeBudget;
if (typeof budget === "number") {
if (budget <= 0) {
state.fileNodesDropped = true;
return null;
}
state.fileNodeBudget = budget - 1;
}
state.nodes.set(node.node_id, node);
return node;
};