Skip to content
Prev Previous commit
Next Next commit
fix(javascript): keep Bun OOM and checkpoint heap controls distinct
Bun heap-exhaustion exits no longer claim the ignored max_heap_mb V8
old-space limit. Checkpoint decode names the parent heap and Node
NODE_OPTIONS, the same control as the parent result budget.

Refs #5910

Co-authored-by: morluto <morluto@users.noreply.github.com>
  • Loading branch information
cursoragent and morluto committed Oct 21, 2026
commit 21b55f9817716136892658856c3ef649ab4aa7ab
18 changes: 12 additions & 6 deletions docs/javascript-artifact-reconstruction.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,16 +635,22 @@ still independently checks actual bytes, record overhead, digests and source
identities before adopting results.

A source that exhausts the analyzer heap does not terminate the CLI/MCP
process. The failure names `max_heap_mb` (CLI `--max-heap-mb`), the effective
mebibytes, the inclusive range 128 to 16384, and the 1024 MiB default. A
changed value applies on the next analysis, which starts a new worker. It does
not change a worker that is already running and does not require restarting
the REA server. Raising `max_heap_mb` does not raise the parent result budget,
process. On Node, the failure names `max_heap_mb` (CLI `--max-heap-mb`), the
effective mebibytes, the inclusive range 128 to 16384, and the 1024 MiB
default. A changed value applies on the next analysis, which starts a new
worker. It does not change a worker that is already running and does not
require restarting the REA server. On Bun, the same heap-exhaustion diagnostic
is not attributed to that V8 old-space limit, because Bun ignores the flag.
Raising `max_heap_mb` does not raise the parent result budget,
the transfer record ceiling, the 2 GiB transfer file maximum, the CLI/MCP
process heap, or fixed parser limits. The parent result budget follows that
process heap. On Node, set `NODE_OPTIONS` (for example `--max-old-space-size`)
in the environment that starts the CLI or MCP server, then restart that
process, to raise the parent heap and the result budget. Completed static facts are retained, other files can still be analyzed,
process, to raise the parent heap and the result budget. A completed static
checkpoint that does not fit the parent's remaining heap names that parent
process heap. Increasing `max_heap_mb` does not raise it. On Node, the same
`NODE_OPTIONS` restart can raise the parent heap and admit the checkpoint.
Completed static facts are retained, other files can still be analyzed,
and unexamined semantic relationships remain explicit unknowns. The parent
also checks transfer expansion and accumulated results against its remaining
heap before accepting semantic facts. A resource-limited result has partial
Expand Down
46 changes: 46 additions & 0 deletions src/domain/javascript/javascriptAnalysisResourceControls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { AnalysisResourceConstraintError } from "../analysisErrorCore.js";
import { projectAnalysisError } from "../analysisErrorProjection.js";
import {
JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS,
JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION,
JAVASCRIPT_PARENT_RESULT_BUDGET_REMEDIATION,
JAVASCRIPT_SEMANTIC_ITEM_MAXIMUM,
JAVASCRIPT_WORKER_HEAP_MIB_MAXIMUM,
JAVASCRIPT_WORKER_HEAP_MIB_MINIMUM,
javaScriptAnalysisResourceControlsSchema,
javaScriptWorkerHeapExhaustionRecovery,
javaScriptSemanticCeilingCoverage,
javaScriptWorkerHeapRemediation,
} from "./javascriptAnalysisResourceControls.js";
Expand Down Expand Up @@ -96,6 +98,50 @@ it("names the parent heap control for the parent result budget", () => {
);
});

it("names the parent heap control for checkpoint decode", () => {
expect(JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION).toContain(
"parent process heap",
);
expect(JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION).toContain(
"NODE_OPTIONS",
);
expect(JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION).toContain(
"--max-old-space-size",
);
expect(JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION).toContain(
"admit this checkpoint",
);
expect(JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION).toContain(
"Increasing max_heap_mb does not raise it",
);
});

it("attributes V8 heap exhaustion to max_heap_mb only when that flag is enforced", () => {
const node = javaScriptWorkerHeapExhaustionRecovery({
heapFlagsEnforced: true,
heapMb: 1024,
path: "app.js",
phase: "static",
});
expect(node.reason).toContain(
"exhausted its 1024 MiB V8 old-space heap (max_heap_mb)",
);
expect(node.remediationAction).toContain("max_heap_mb");
expect(node.remediationAction).toContain("--max-heap-mb");
const bun = javaScriptWorkerHeapExhaustionRecovery({
heapFlagsEnforced: false,
heapMb: 1024,
path: "app.js",
phase: "static",
});
expect(bun.reason).toContain("Bun ignores the V8 old-space flag");
expect(bun.reason).not.toContain(
"exhausted its 1024 MiB V8 old-space heap (max_heap_mb)",
);
expect(bun.remediationAction).toContain("Bun ignores V8 heap flags");
expect(bun.remediationAction).not.toContain("Increase max_heap_mb");
});

it("names the four semantic arguments on ceiling coverage", () => {
const nodes = javaScriptSemanticCeilingCoverage("nodes", 50, 30);
const unknowns = javaScriptSemanticCeilingCoverage("unknowns", 3, 2);
Expand Down
31 changes: 29 additions & 2 deletions src/domain/javascript/javascriptAnalysisResourceControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,36 @@ export const javaScriptWorkerHeapRemediation = (heapMb: number): string => {
return `${adjustment} The effective worker heap for this analysis is ${String(heapMb)} MiB. A changed max_heap_mb applies on the next analysis, which starts a new worker. It does not change a worker that is already running, and it does not require restarting the REA server. ${HEAP_DOES_NOT_RAISE} On Bun, V8 heap flags are ignored: Windows limits the analyzer's committed memory to max_heap_mb plus a 512 MiB reserve, and other Bun hosts enforce no analyzer heap limit.`;
};

const PARENT_HEAP_NODE_OPTIONS =
"On Node, set NODE_OPTIONS (for example --max-old-space-size) in the environment that starts the CLI or MCP server, then restart that process,";
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

/** Parent admission follows the CLI/MCP heap, not the isolated worker heap argument. */
export const JAVASCRIPT_PARENT_RESULT_BUDGET_REMEDIATION =
"This limit belongs to the parent result budget. That budget follows the parent V8 heap limit. Increasing max_heap_mb does not raise it. On Node, set NODE_OPTIONS (for example --max-old-space-size) in the environment that starts the CLI or MCP server, then restart that process, to raise the parent heap and this budget. Use the retained static facts or analyze fewer source files.";
export const JAVASCRIPT_PARENT_RESULT_BUDGET_REMEDIATION = `This limit belongs to the parent result budget. That budget follows the parent V8 heap limit. Increasing max_heap_mb does not raise it. ${PARENT_HEAP_NODE_OPTIONS} to raise the parent heap and this budget. Use the retained static facts or analyze fewer source files.`;

/** Checkpoint decode uses the parent heap, not the isolated worker heap argument. */
export const JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION = `This limit belongs to the parent process heap. Increasing max_heap_mb does not raise it. max_heap_mb changes only the isolated analyzer, on the next analysis, by starting a new worker. ${PARENT_HEAP_NODE_OPTIONS} to raise the parent heap and admit this checkpoint.`;

/**
* Heap-exhaustion exit text. Node exhausted the configured V8 old-space.
* Bun ignores that flag, so the same diagnostic must not name it as the cause.
*/
export const javaScriptWorkerHeapExhaustionRecovery = (input: {
readonly heapFlagsEnforced: boolean;
readonly heapMb: number;
readonly path: string;
readonly phase: string;
}): { readonly reason: string; readonly remediationAction: string } => {
if (input.heapFlagsEnforced)
return {
reason: `Owned JavaScript worker exhausted its ${String(input.heapMb)} MiB V8 old-space heap (max_heap_mb) while analyzing ${input.path} during ${input.phase}`,
remediationAction: javaScriptWorkerHeapRemediation(input.heapMb),
};
return {
reason: `Owned JavaScript worker ran out of memory while analyzing ${input.path} during ${input.phase}. Bun ignores the V8 old-space flag, so this exit is not the configured max_heap_mb limit of ${String(input.heapMb)} MiB.`,
remediationAction:
"Bun ignores V8 heap flags, so raising max_heap_mb does not change this exit. Windows limits the analyzer's committed memory to max_heap_mb plus a 512 MiB reserve and reports that separately as a process memory limit. Other Bun hosts enforce no analyzer heap limit. Reduce the analysis instead of increasing max_heap_mb.",
};
};

const semanticControl = {
nodes: {
Expand Down
20 changes: 13 additions & 7 deletions src/javascript/analysis/JavaScriptAnalysisWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ import {
javaScriptTransferRetentionBytes,
} from "./JavaScriptAnalysisTransfer.js";
import {
JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION,
JAVASCRIPT_PARENT_RESULT_BUDGET_REMEDIATION,
JAVASCRIPT_WORKER_HEAP_MIB_MAXIMUM,
JAVASCRIPT_WORKER_HEAP_MIB_MINIMUM,
javaScriptWorkerHeapExhaustionRecovery,
javaScriptWorkerHeapRemediation,
} from "../../domain/javascript/javascriptAnalysisResourceControls.js";
import { javaScriptWorkerStaticAnalysisSchema } from "./JavaScriptAnalysisWorkerSchemas.js";
Expand Down Expand Up @@ -695,8 +697,7 @@ export class JavaScriptAnalysisWorker {
"The parent's remaining heap cannot safely decode the completed JavaScript static checkpoint",
{ ...this.#limits(), checkpoint_bytes: bytes },
{
remediationAction:
"This limit belongs to the parent process heap. Increasing max_heap_mb does not raise it. max_heap_mb changes only the isolated analyzer, on the next analysis, by starting a new worker.",
remediationAction: JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION,
},
);
const text = await readFile(path, "utf8");
Expand Down Expand Up @@ -1006,19 +1007,24 @@ export class JavaScriptAnalysisWorker {
path: string,
failure: Extract<WorkerPrimaryFailure, { kind: "exit" }>,
): AnalysisError {
if (failure.heapExhaustion)
if (failure.heapExhaustion) {
const recovery = javaScriptWorkerHeapExhaustionRecovery({
heapFlagsEnforced: WORKER_HEAP_FLAGS_ENFORCED,
heapMb: this.limits.heapMb,
path,
phase: this.#phase,
});
return new AnalysisResourceConstraintError(
OPERATION,
"memory",
`Owned JavaScript worker exhausted its ${String(this.limits.heapMb)} MiB V8 old-space heap (max_heap_mb) while analyzing ${path} during ${this.#phase}`,
recovery.reason,
this.#limits(),
{
capturedOutput: this.#captured(),
remediationAction: javaScriptWorkerHeapRemediation(
this.limits.heapMb,
),
remediationAction: recovery.remediationAction,
},
);
}
return new ProviderAdapterError(PROVIDER, OPERATION, {
capturedOutput: this.#captured(),
diagnostics: {
Expand Down
30 changes: 25 additions & 5 deletions tests/boundary/javascript/workerOrderAudit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { expect, it } from "vitest";
import {
AnalysisCancelledError,
AnalysisOutputError,
AnalysisResourceConstraintError,
} from "../../../src/domain/analysisErrorCore.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { javaScriptRuntime } from "../../../src/domain/runtimeVersion.js";
import { JavaScriptAnalysisWorker } from "../../../src/javascript/analysis/JavaScriptAnalysisWorker.js";
import {
javaScriptWorkerMessageSchema,
Expand Down Expand Up @@ -777,11 +780,28 @@ it("retains the owned worker's V8 heap exhaustion classification", async () => {
try {
const result = await worker.analyze(file, { nodes: 100, unknowns: 100 });
expect(result.ok).toBe(false);
if (!result.ok)
expect(result.error.error).toMatchObject({
_tag: "AnalysisResourceConstraintError",
resource: "memory",
});
if (!result.ok) {
expect(result.error.error).toBeInstanceOf(
AnalysisResourceConstraintError,
);
const projected = projectAnalysisError(result.error.error);
if (javaScriptRuntime(process.versions).id === "node") {
expect(projected.message).toContain(
"128 MiB V8 old-space heap (max_heap_mb)",
);
expect(projected.remediation.action).toContain("--max-heap-mb");
} else {
expect(projected.message).toContain(
"Bun ignores the V8 old-space flag",
);
expect(projected.message).not.toContain(
"128 MiB V8 old-space heap (max_heap_mb)",
);
expect(projected.remediation.action).not.toContain(
"Increase max_heap_mb",
);
}
}
} finally {
await worker.close();
}
Expand Down
Loading