Skip to content
Merged
Prev Previous commit
Next Next commit
fix(process): leave percentage heap overrides unestablished
--max-old-space-size-percentage takes precedence over the absolute flag
in either order. Node computes that MiB from startup memory, which a
later read does not recover, so the effective heap stays unknown instead
of the 128 MiB request or an absolute flag.

Co-authored-by: morluto <morluto@users.noreply.github.com>
  • Loading branch information
cursoragent and morluto committed Oct 21, 2026
commit 4e4b70ecf75e6abe00653dabd413e3fe8c9ce1d4
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.
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.

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
5 changes: 4 additions & 1 deletion docs/native-investigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ cancellation stops matching while leaving the Hopper API 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. Literal mode retains Unicode casefold matching.
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.
Literal mode retains Unicode casefold matching.

## Bounded static traces

Expand Down
68 changes: 68 additions & 0 deletions src/process/RegexMatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,70 @@ it.each([
nodeOptions: '"--max-old-space-size=256',
heap: effective(80, true),
},
{
name: "leaves a percentage flag unestablished instead of the 128 MiB request",
execArgv: ["--max-old-space-size-percentage=50"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
{
name: "lets a percentage flag win over a later absolute flag",
execArgv: ["--max-old-space-size-percentage=50", "--max-old-space-size=64"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
{
name: "lets a percentage flag win over an earlier absolute flag",
execArgv: ["--max-old-space-size=64", "--max-old-space-size-percentage=50"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
{
name: "lets a NODE_OPTIONS percentage win over an execArgv absolute flag",
execArgv: ["--max-old-space-size=64"],
nodeOptions: "--max-old-space-size-percentage=50",
heap: { kind: "unknown" } as const,
},
{
name: "lets an execArgv percentage win over a NODE_OPTIONS absolute flag",
execArgv: ["--max-old-space-size-percentage=50"],
nodeOptions: "--max-old-space-size=64",
heap: { kind: "unknown" } as const,
},
{
name: "recognizes an underscore percentage flag",
execArgv: ["--max_old_space_size_percentage=50"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
{
name: "recognizes a percentage flag split from its value",
execArgv: [
"--max-old-space-size=64",
"--max-old-space-size-percentage",
"50",
],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
{
name: "does not let a later absolute flag replace an invalid percentage",
execArgv: ["--max-old-space-size-percentage=0", "--max-old-space-size=64"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
{
name: "leaves a fractional percentage unestablished",
execArgv: ["--max-old-space-size-percentage=50.5"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
{
name: "does not treat a final zero absolute flag as 128 MiB after a percentage",
execArgv: ["--max-old-space-size-percentage=50", "--max-old-space-size=0"],
nodeOptions: undefined,
heap: { kind: "unknown" } as const,
},
] as const)(
"resolveRegexWorkerHeap $name",
({ execArgv, nodeOptions, heap }) => {
Expand All @@ -111,8 +175,12 @@ it("describes the effective heap and keeps the deadline fixed", () => {
);
expect(projected.remediation.action).not.toContain("no runtime override");
expect(projected.remediation.action).toContain("--max-old-space-size");
expect(projected.remediation.action).toContain(
"--max-old-space-size-percentage",
);
if (heap.kind === "unknown") {
expect(limitation).toContain("was not established");
expect(limitation).toContain("--max-old-space-size-percentage");
expect(projected.message).toContain("was not established");
expect(projected.details).toMatchObject({
reported_limits: { requested_worker_heap_mib: 128 },
Expand Down
24 changes: 21 additions & 3 deletions src/process/RegexMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@ const WORKER_HEAP_MIB = 128;
* `--max-old-space-size` on the process that starts REA, including
* `NODE_OPTIONS`, overrides it. A zero or empty value leaves this request
* in effect. `NODE_OPTIONS` is applied before `process.execArgv`, and the
* last flag wins. That environment value is not copied into `execArgv`.
* last absolute flag wins. That environment value is not copied into `execArgv`.
*
* `--max-old-space-size-percentage` is applied after both sources and takes
* precedence over the absolute flag in either order. Node turns it into a MiB
* from constrained or total memory while parsing options. That base can change
* before user code reads it, and some Node versions also cap total memory at
* 4 GiB when pointer compression is on. A later read is not the value Node
* applied, so a percentage flag leaves the effective heap unestablished
* instead of the 128 MiB request or an absolute flag.
*/
const OLD_SPACE_FLAG = /^--max[-_]old[-_]space[-_]size(?:=(.*))?$/u;
const OLD_SPACE_PERCENT_FLAG =
/^--max[-_]old[-_]space[-_]size[-_]percentage(?:=.*)?$/u;

/** Old-generation heap the regex worker will actually hit. */
export type RegexWorkerHeapLimit =
Expand Down Expand Up @@ -94,7 +104,15 @@ export const resolveRegexWorkerHeap = (
nodeOptions === undefined ? [] : nodeOptionsArgv(nodeOptions);
let limit: RegexWorkerHeapLimit =
fromEnvironment === undefined ? { kind: "unknown" } : requestedHeap;
let percentage = false;
for (const token of [...(fromEnvironment ?? []), ...execArgv]) {
if (OLD_SPACE_PERCENT_FLAG.test(token)) {
percentage = true;
limit = { kind: "unknown" };
continue;
}
// A percentage flag wins over every absolute flag, including a later one.
if (percentage) continue;
const parsed = parsedOldSpaceMiB(token);
if (parsed === "absent") continue;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
if (parsed === "unrecognized") {
Expand All @@ -117,7 +135,7 @@ const launchRegexWorkerHeap = resolveRegexWorkerHeap(

const heapBound = (heap: RegexWorkerHeapLimit): string => {
if (heap.kind === "unknown")
return "The worker requests 128 MiB. A launch --max-old-space-size is set and the effective old-generation heap was not established.";
return "The worker requests 128 MiB. A launch --max-old-space-size or --max-old-space-size-percentage is set and the effective old-generation heap was not established.";
const override =
"A positive Node --max-old-space-size, including NODE_OPTIONS, overrides the 128 MiB worker request";
return heap.overridden
Expand Down Expand Up @@ -173,7 +191,7 @@ export const regexWorkerHeapError = (
: { requested_worker_heap_mib: WORKER_HEAP_MIB },
{
cause,
remediationAction: `${heapBound(heap)} Restart REA after changing --max-old-space-size. Simplify the pattern or use literal mode.`,
remediationAction: `${heapBound(heap)} Restart REA after changing --max-old-space-size. A launch --max-old-space-size-percentage takes precedence over that flag in either order. Simplify the pattern or use literal mode.`,
},
);
const replySchema = z.discriminatedUnion("status", [
Expand Down
40 changes: 40 additions & 0 deletions tests/boundary/process/regexMatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,46 @@ it("names the fixed regex deadline and the effective worker heap", () => {
}
});

const relativeHeapGap = (left: number, right: number): number =>
Math.abs(left - right) / Math.max(left, right);

it("lets --max-old-space-size-percentage override an absolute heap in either order", () => {
const percentage = "--max-old-space-size-percentage=50";
const absolute = "--max-old-space-size=64";
const alone = probeWorkerHeap([percentage], undefined);
const percentageThenAbsolute = probeWorkerHeap(
[percentage, absolute],
undefined,
);
const absoluteThenPercentage = probeWorkerHeap(
[absolute, percentage],
undefined,
);
const onlyAbsolute = probeWorkerHeap([absolute], undefined);
for (const observed of [
alone,
percentageThenAbsolute,
absoluteThenPercentage,
onlyAbsolute,
])
expect(observed.resourceOld).toBe(128);
expect(
relativeHeapGap(alone.heap, percentageThenAbsolute.heap),
).toBeLessThan(0.05);
expect(
relativeHeapGap(alone.heap, absoluteThenPercentage.heap),
).toBeLessThan(0.05);
expect(relativeHeapGap(alone.heap, onlyAbsolute.heap)).toBeGreaterThan(0.25);
for (const execArgv of [
[percentage],
[percentage, absolute],
[absolute, percentage],
])
expect(resolveRegexWorkerHeap(execArgv, 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