Skip to content
Merged
Prev Previous commit
Next Next commit
fix(process): leave --max-heap-size old generation unestablished
A positive --max-heap-size or --max_heap_size sets the total heap, not the
old generation, and can stay below the 128 MiB worker request when an
old-space flag is also present. The effective old generation stays
unestablished while that value is in effect. Zero or empty clears it.

Co-authored-by: morluto <morluto@users.noreply.github.com>
  • Loading branch information
cursoragent and morluto committed Oct 21, 2026
commit a8e194acb6581d9a40834174b65928282598cd90
2 changes: 1 addition & 1 deletion docs/kuna-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ The background export belongs to the session. `close_binary`, or the end of a CL

The assembly label index is scanned once per export reader. Cancelling a request stops its wait without interrupting other readers; closing the session aborts and drains the shared scan before releasing its files.

Regex procedure and string search uses ECMAScript Unicode matching in a cancellable worker with a five-second matching deadline and a worker old-generation heap. Literal matching remains the default. Cancellation or a matching deadline leaves the session available. This release has no runtime override for that deadline. The worker requests 128 MiB. A positive Node.js `--max-old-space-size` on the process that starts REA, including `NODE_OPTIONS`, overrides that request and sets the effective heap. `--max-old-space-size-percentage` takes precedence over that flag in either order. REA does not turn the percentage into a MiB, so the effective heap stays unestablished instead of the 128 MiB request or the absolute flag. The reported `NODE_OPTIONS` value is the process initial environment when that block can be read. A later edit to the environment is not the value Node applied, and an unreadable initial value leaves the effective heap unestablished. `--env-file` or `--env-file-if-exists` can supply `NODE_OPTIONS` when the initial environment has none. That file is not part of the initial environment block, so the effective heap stays unestablished instead of the 128 MiB request. An initial `NODE_OPTIONS` is still used, because Node does not let the file replace it.
Regex procedure and string search uses ECMAScript Unicode matching in a cancellable worker with a five-second matching deadline and a worker old-generation heap. Literal matching remains the default. Cancellation or a matching deadline leaves the session available. This release has no runtime override for that deadline. The worker requests 128 MiB. A positive Node.js `--max-old-space-size` on the process that starts REA, including `NODE_OPTIONS`, overrides that request and sets the effective heap. `--max-old-space-size-percentage` takes precedence over that flag in either order. REA does not turn the percentage into a MiB, so the effective heap stays unestablished instead of the 128 MiB request or the absolute flag. The reported `NODE_OPTIONS` value is the process initial environment when that block can be read. A later edit to the environment is not the value Node applied, and an unreadable initial value leaves the effective heap unestablished. `--env-file` or `--env-file-if-exists` can supply `NODE_OPTIONS` when the initial environment has none. That file is not part of the initial environment block, so the effective heap stays unestablished instead of the 128 MiB request. An initial `NODE_OPTIONS` is still used, because Node does not let the file replace it. A positive `--max-heap-size`, including the underscore form and launch `NODE_OPTIONS` on Node versions that allow it, sets the total heap rather than the old generation and can be lower than the 128 MiB request. The effective old generation stays unestablished while that value is in effect. A zero or empty value clears it.

Single-function subprocess output and each cached source facet share a 16 MiB buffered-text capacity. Cached spans are checked before allocation; larger text remains in the local export and the error identifies the file and range. `analyze_function` initially returns the complete dossier, which can still be large; later retained views do not remove that first transfer. Prefer the individual text tools for a focused question.

Expand Down
6 changes: 5 additions & 1 deletion docs/native-investigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ unestablished. `--env-file` or `--env-file-if-exists` can supply `NODE_OPTIONS`
when the initial environment has none. That file is not part of the initial
environment block, so the effective heap stays unestablished instead of the
128 MiB request. An initial `NODE_OPTIONS` is still used, because Node does
not let the file replace it.
not let the file replace it. A positive `--max-heap-size`, including the
underscore form and launch `NODE_OPTIONS` on Node versions that allow it,
sets the total heap rather than the old generation and can be lower than the
128 MiB request. The effective old generation stays unestablished while that
value is in effect. A zero or empty value clears it.
Literal mode retains Unicode casefold matching.

## Bounded static traces
Expand Down
62 changes: 62 additions & 0 deletions src/process/RegexMatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,60 @@ it.each([
nodeOptions: "",
heap: effective(128, false),
},
{
name: "leaves a positive --max-heap-size unestablished instead of the 128 MiB request",
execArgv: ["--max-heap-size=64"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
{
name: "recognizes an underscore --max-heap-size",
execArgv: ["--max_heap_size=+064"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
{
name: "leaves a NODE_OPTIONS --max-heap-size unestablished",
execArgv: [],
nodeOptions: "--max-heap-size=64",
heap: { kind: "unknown" } as const,
},
{
name: "does not let an old-space flag hide a positive --max-heap-size",
execArgv: ["--max-heap-size=64", "--max-old-space-size=256"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
{
name: "does not let a later --max-heap-size hide an old-space flag",
execArgv: ["--max-old-space-size=64"],
nodeOptions: "--max_heap_size=256",
heap: { kind: "unknown" } as const,
},
{
name: "lets a final zero --max-heap-size clear the total-heap cap",
execArgv: ["--max-heap-size=64", "--max-heap-size=0"],
nodeOptions: undefined,
heap: effective(128, false),
},
{
name: "keeps an old-space flag when a final --max-heap-size is zero",
execArgv: ["--max-old-space-size=256", "--max-heap-size="],
nodeOptions: undefined,
heap: effective(256, true),
},
{
name: "leaves an unparsed --max-heap-size unestablished",
execArgv: ["--max-heap-size=64.5", "--max-old-space-size=80"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
{
name: "does not let a later zero clear an unparsed --max-heap-size",
execArgv: ["--max-heap-size=64.5", "--max-heap-size=0"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
] as const)(
"resolveRegexWorkerHeap $name",
({ execArgv, nodeOptions, heap }) => {
Expand All @@ -220,6 +274,12 @@ it("does not treat a rewritten NODE_OPTIONS value as the launch heap", () => {
kind: "unrecoverable",
}),
).toEqual({ kind: "unknown" });
expect(
resolveLaunchRegexWorkerHeap([], {
kind: "recovered",
value: "--max_heap_size=64",
}),
).toEqual({ kind: "unknown" });
if (launch.kind === "recovered") {
expect(launch.value).not.toBe(rewritten);
expect(
Expand Down Expand Up @@ -257,6 +317,8 @@ it("describes the effective heap and keeps the deadline fixed", () => {
if (heap.kind === "unknown") {
expect(limitation).toContain("was not established");
expect(limitation).toContain("--max-old-space-size-percentage");
expect(limitation).toContain("--max-heap-size");
expect(projected.remediation.action).toContain("--max-heap-size");
expect(projected.message).toContain("was not established");
expect(projected.details).toMatchObject({
reported_limits: { requested_worker_heap_mib: 128 },
Expand Down
31 changes: 26 additions & 5 deletions src/process/RegexMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ const WORKER_HEAP_MIB = 128;
* are not copied into `process.execArgv`. The launch string is the
* `NODE_OPTIONS` entry in the process initial environment when that block can
* be read. Otherwise the effective heap stays unestablished.
*
* `--max-heap-size` is the total heap, not the old generation. A positive
* value can be lower than the 128 MiB request and still changes the result
* when an old-space flag is also present. The old generation stays
* unestablished while that value is in effect. Zero or empty clears it.
* Node 24 and later also accept the flag in `NODE_OPTIONS`.
*/
const OLD_SPACE_FLAG = /^--max[-_]old[-_]space[-_]size(?:=(.*))?$/u;
const OLD_SPACE_PERCENT_FLAG =
/^--max[-_]old[-_]space[-_]size[-_]percentage(?:=.*)?$/u;
const MAX_HEAP_FLAG = /^--max[-_]heap[-_]size(?:=(.*))?$/u;
/**
* Node applies `NODE_OPTIONS` from `--env-file` / `--env-file-if-exists` only
* when the variable was absent. The file is not in `/proc/self/environ`, and a
Expand Down Expand Up @@ -92,10 +99,11 @@ const nodeOptionsArgv = (
return inString ? undefined : argv;
};

const parsedOldSpaceMiB = (
const parsedFlagMiB = (
token: string,
flag: RegExp,
): number | "absent" | "unrecognized" => {
const match = OLD_SPACE_FLAG.exec(token);
const match = flag.exec(token);
if (match === null) return "absent";
const raw = match[1];
if (raw === undefined) return "unrecognized";
Expand Down Expand Up @@ -127,6 +135,7 @@ export const resolveRegexWorkerHeap = (
let limit: RegexWorkerHeapLimit =
fromEnvironment === undefined ? { kind: "unknown" } : requestedHeap;
let percentage = false;
let maxHeap: "clear" | "positive" | "unrecognized" = "clear";
for (const token of tokens) {
if (OLD_SPACE_PERCENT_FLAG.test(token)) {
percentage = true;
Expand All @@ -135,7 +144,17 @@ export const resolveRegexWorkerHeap = (
}
// A percentage flag wins over every absolute flag, including a later one.
if (percentage) continue;
const parsed = parsedOldSpaceMiB(token);
const totalHeap = parsedFlagMiB(token, MAX_HEAP_FLAG);
if (totalHeap === "unrecognized") {
maxHeap = "unrecognized";
continue;
}
if (totalHeap !== "absent") {
if (maxHeap !== "unrecognized")
maxHeap = totalHeap > 0 ? "positive" : "clear";
continue;
}
const parsed = parsedFlagMiB(token, OLD_SPACE_FLAG);
if (parsed === "absent") continue;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
if (parsed === "unrecognized") {
limit = { kind: "unknown" };
Expand All @@ -146,7 +165,9 @@ export const resolveRegexWorkerHeap = (
? { kind: "effective", mib: parsed, overridden: true }
: requestedHeap;
}
return limit;
// A positive total-heap cap is not an old-generation size. Zero clears it
// and leaves the old-space result. An unparsed value does not.
return maxHeap === "clear" ? limit : { kind: "unknown" };
};

/** Provenance of the `NODE_OPTIONS` value Node applied before user code ran. */
Expand Down Expand Up @@ -218,7 +239,7 @@ const launchRegexWorkerHeap = resolveLaunchRegexWorkerHeap(

const heapBound = (heap: RegexWorkerHeapLimit): string => {
if (heap.kind === "unknown")
return "The worker requests 128 MiB. The effective old-generation heap was not established from the launch flags. --max-old-space-size, --max-old-space-size-percentage, --env-file, and --env-file-if-exists can change that request.";
return "The worker requests 128 MiB. The effective old-generation heap was not established from the launch flags. --max-old-space-size, --max-old-space-size-percentage, --max-heap-size, --env-file, and --env-file-if-exists can change that request.";
const override =
"A positive Node --max-old-space-size, including NODE_OPTIONS, overrides the 128 MiB worker request";
return heap.overridden
Expand Down
23 changes: 23 additions & 0 deletions tests/boundary/process/regexMatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,29 @@ it("does not report the 128 MiB request for NODE_OPTIONS supplied by an env file
}
});

it("does not report the 128 MiB request when --max-heap-size lowers the worker heap", () => {
const baseline = probeWorkerHeap([], undefined);
const heap64 = probeWorkerHeap(["--max-heap-size=64"], undefined);
const underscore = probeWorkerHeap(["--max_heap_size=64"], undefined);
expect(baseline.resourceOld).toBe(128);
expect(heap64.resourceOld).toBe(128);
expect(underscore.resourceOld).toBe(128);
expect(heap64.heap).toBeLessThan(baseline.heap);
expect(underscore.heap).toBe(heap64.heap);
expect(resolveRegexWorkerHeap(["--max-heap-size=64"], undefined)).toEqual({
kind: "unknown",
});
expect(resolveRegexWorkerHeap([], "--max-heap-size=64")).toEqual({
kind: "unknown",
});
expect(
resolveRegexWorkerHeap(
["--max-heap-size=64", "--max-old-space-size=256"],
undefined,
),
).toEqual({ kind: "unknown" });
});

it("lets a launch --max-old-space-size override the 128 MiB worker request", () => {
const baseline = probeWorkerHeap([], undefined);
const smaller = probeWorkerHeap(["--max-old-space-size=64"], undefined);
Expand Down
Loading