Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(browser): name the operand that stopped web-bundle admission
Host heap pressure no longer claims the fixed result budget is exhausted.
A Windows Bun allocation before the commit limit still names that limit.

Co-authored-by: morluto <morluto@users.noreply.github.com>
  • Loading branch information
cursoragent and morluto committed Oct 21, 2026
commit e96ace766584ddb3854af1fa9f54d8d3ecd3b1be
11 changes: 8 additions & 3 deletions docs/browser-observation.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,10 @@ and result byte budgets have no runtime override.
One web-bundle capture/analysis is admitted per REA process at a time. Completed
script results share an 8 MiB serialized transfer budget, with host heap
reservation checked before decoding and validating another result.
Exhausted result capacity stops further script launches and returns a typed
When the host reservation is what reaches zero, the failure names that heap
headroom and the remaining fixed budget. When the 8 MiB allowance itself is
exhausted, the failure names that fixed budget and says it has no runtime
override. Either stop ends further script launches and returns a typed
resource failure with completed facts and explicit unstarted-script unknowns.
Worker control frames are limited to 64 KiB. Cleanup must confirm child termination
before releasing its private files; failed cleanup retains its exact owner and
Expand All @@ -419,8 +422,10 @@ facts, including cancellation during final cleanup.
The worker heap setting is not a process-family RSS limit. Node enforces V8
old-space flags; Bun ignores them. Windows Bun instead uses an owned process
commit limit with a 512 MiB runtime reserve; other Bun hosts have no enforced
memory limit. These bounds do not establish an aggregate host-memory limit
across other REA operations or external processes.
memory limit. An allocation failure before that Windows limit is reported as
reached still names the configured limit and says it was not reached. These
bounds do not establish an aggregate host-memory limit across other REA
operations or external processes.

Resource-tree inventories distinguish a valid empty `resources` array from a
missing or malformed collection. Invalid resource entries are omitted with
Expand Down
62 changes: 40 additions & 22 deletions src/browser/WebBundleAnalysisWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
webBundleHeapExhaustedReason,
webBundleHeapExhaustedRemediation,
webBundleMemoryExhaustion,
webBundleResultAdmissionFailure,
webBundleScriptTimeoutRecovery,
} from "../domain/webBundleAnalysis.js";
import {
Expand Down Expand Up @@ -294,30 +295,12 @@ export class WebBundleAnalysisWorker {
try {
if (signal?.aborted)
throw new AnalysisCancelledError(OPERATION, { cause: signal.reason });
const resultBudget = Math.min(
WEB_BUNDLE_WORKER_LIMITS.resultBytes - this.#retainedResultBytes,
Math.max(
0,
Math.floor(
(getHeapStatistics().heap_size_limit -
process.memoryUsage().heapUsed -
64 * MIB) /
RESULT_EXPANSION,
),
),
);
if (resultBudget <= 0) {
const admitted = this.#admittedResultBudget();
if (!admitted.ok) {
continuation = "result_capacity_exhausted";
throw new AnalysisResourceConstraintError(
OPERATION,
"memory",
webBundleFixedByteBudget(
WEB_BUNDLE_WORKER_LIMITS.resultBytes,
"result budget",
),
{ result_bytes: WEB_BUNDLE_WORKER_LIMITS.resultBytes },
);
throw admitted.error;
}
const resultBudget = admitted.bytes;
await prepareProcessOwnershipInspection(signal);
deadline = new ProviderStartupDeadline(timeoutMs, signal);
this.#activeDeadline = deadline;
Expand Down Expand Up @@ -504,6 +487,41 @@ export class WebBundleAnalysisWorker {
return result.ok ? result : failed(result.error);
}

#admittedResultBudget():
| { readonly ok: true; readonly bytes: number }
| { readonly ok: false; readonly error: AnalysisResourceConstraintError } {
const fixedRemaining =
WEB_BUNDLE_WORKER_LIMITS.resultBytes - this.#retainedResultBytes;
const hostCapacity = Math.max(
0,
Math.floor(
(getHeapStatistics().heap_size_limit -
process.memoryUsage().heapUsed -
64 * MIB) /
RESULT_EXPANSION,
),
);
const bytes = Math.min(fixedRemaining, hostCapacity);
if (bytes > 0) return { ok: true, bytes };
const failure = webBundleResultAdmissionFailure({
fixedBudgetBytes: WEB_BUNDLE_WORKER_LIMITS.resultBytes,
fixedRemainingBytes: fixedRemaining,
hostCapacityBytes: hostCapacity,
});
return {
ok: false,
error: new AnalysisResourceConstraintError(
OPERATION,
"memory",
failure.reason,
failure.reportedLimits,
failure.kind === "host-capacity"
? { remediationAction: failure.remediationAction }
: undefined,
),
};
}

async #writeSource(
path: string,
script: IncludedScript,
Expand Down
9 changes: 8 additions & 1 deletion src/browser/WebBundleAnalysisWorkerCapacity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ it("refuses before spawning or reading source when no host result capacity remai
const result = await worker.analyze(script, 10_000);
expect(result.ok).toBe(false);
if (result.ok) throw new Error("Expected result-capacity refusal");
expect(result.error.error).toBeInstanceOf(AnalysisResourceConstraintError);
const refusal = result.error.error;
expect(refusal).toBeInstanceOf(AnalysisResourceConstraintError);
if (!(refusal instanceof AnalysisResourceConstraintError))
throw new Error("Expected a result-capacity refusal");
expect(refusal.message).toContain("host result capacity 0 bytes");
expect(refusal.message).toContain("still has 8388608 bytes");
expect(refusal.message).not.toContain("has no further room");
expect(refusal.remediationAction).toContain("NODE_OPTIONS");
expect(result.error.continuation).toBe("result_capacity_exhausted");
expect(result.error.completed).toBeNull();
expect(sourceReads).toBe(0);
Expand Down
64 changes: 60 additions & 4 deletions src/domain/webBundleAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ export type WebBundleMemoryExhaustion =
readonly heapMb: number;
readonly limitBytes: number;
}
| { readonly kind: "bun-allocation"; readonly heapMb: number };
| {
readonly kind: "bun-allocation";
readonly heapMb: number;
readonly processMemoryLimitBytes: number | null;
};

/** Map host facts onto the exhausted resource. The worker supplies those facts. */
export const webBundleMemoryExhaustion = (input: {
Expand All @@ -111,7 +115,11 @@ export const webBundleMemoryExhaustion = (input: {
heapMb: input.heapMb,
limitBytes: input.processMemoryLimitBytes,
};
return { kind: "bun-allocation", heapMb: input.heapMb };
return {
kind: "bun-allocation",
heapMb: input.heapMb,
processMemoryLimitBytes: input.processMemoryLimitBytes,
};
};

const webBundleHeapRange = (): string =>
Expand All @@ -136,7 +144,9 @@ export const webBundleHeapExhaustedReason = (
case "process-commit":
return `Owned web analyzer reached its ${String(exhaustion.limitBytes)}-byte process commit limit. That limit is max_heap_mb ${String(exhaustion.heapMb)} MiB plus a 512 MiB Bun runtime reserve (${range}). Bun ignores the V8 old-space flag, so this is not V8 old-space exhaustion.`;
case "bun-allocation":
return `Owned web analyzer ran out of memory. Bun ignores the V8 old-space flag, so this exit is not the configured max_heap_mb of ${String(exhaustion.heapMb)} MiB (${range}). This host enforces no analyzer process commit limit.`;
return exhaustion.processMemoryLimitBytes === null
? `Owned web analyzer ran out of memory. Bun ignores the V8 old-space flag, so this exit is not the configured max_heap_mb of ${String(exhaustion.heapMb)} MiB (${range}). This host enforces no analyzer process commit limit.`
: `Owned web analyzer ran out of memory before its ${String(exhaustion.processMemoryLimitBytes)}-byte process commit limit was reported as reached. That configured limit is max_heap_mb ${String(exhaustion.heapMb)} MiB plus a 512 MiB Bun runtime reserve (${range}). Bun ignores the V8 old-space flag, so this exit is not V8 old-space exhaustion.`;
}
};

Expand All @@ -149,7 +159,9 @@ export const webBundleHeapExhaustedRemediation = (
case "process-commit":
return `${webBundleHeapIncrease(exhaustion.heapMb)} That raises the Windows process commit limit (max_heap_mb plus the fixed 512 MiB Bun runtime reserve), not a V8 old-space heap. ${webBundleHeapDoesNotRaise}`;
case "bun-allocation":
return `Bun ignores V8 heap flags, so raising max_heap_mb does not change this exit. A reached Windows process commit limit is reported separately. Other Bun hosts enforce no analyzer heap limit. Reduce the analysis instead of increasing max_heap_mb. ${webBundleHeapDoesNotRaise}`;
return exhaustion.processMemoryLimitBytes === null
? `Bun ignores V8 heap flags, so raising max_heap_mb does not change this exit. A reached Windows process commit limit is reported separately. Other Bun hosts enforce no analyzer heap limit. Reduce the analysis instead of increasing max_heap_mb. ${webBundleHeapDoesNotRaise}`
: `The configured ${String(exhaustion.processMemoryLimitBytes)}-byte process commit limit was not reported as reached, so raising max_heap_mb does not change this exit. A reached Windows process commit limit is reported separately. Reduce the analysis instead of increasing max_heap_mb. ${webBundleHeapDoesNotRaise}`;
}
};

Expand All @@ -162,6 +174,50 @@ export const webBundleFixedByteBudget = (
"max_heap_mb",
);

/**
* Result admission failed because `min(fixedRemaining, hostCapacity)` is not
* positive. The fixed allowance wins when it has no room left. Host pressure
* is named only while that allowance still has room.
*/
export const webBundleResultAdmissionFailure = (input: {
readonly fixedBudgetBytes: number;
readonly fixedRemainingBytes: number;
readonly hostCapacityBytes: number;
}):
| {
readonly kind: "fixed-budget";
readonly reason: string;
readonly reportedLimits: { readonly result_bytes: number };
}
| {
readonly kind: "host-capacity";
readonly reason: string;
readonly remediationAction: string;
readonly reportedLimits: {
readonly result_bytes: number;
readonly result_budget_remaining_bytes: number;
readonly host_result_capacity_bytes: number;
};
} => {
if (input.fixedRemainingBytes <= 0)
return {
kind: "fixed-budget",
reason: webBundleFixedByteBudget(input.fixedBudgetBytes, "result budget"),
reportedLimits: { result_bytes: input.fixedBudgetBytes },
};
return {
kind: "host-capacity",
reason: `The CLI/MCP host heap has no remaining reserved capacity for another web-bundle result (host result capacity ${String(input.hostCapacityBytes)} bytes). The fixed ${String(input.fixedBudgetBytes)}-byte result budget still has ${String(input.fixedRemainingBytes)} bytes available. This is host memory pressure, not exhaustion of that fixed allowance.`,
remediationAction:
"Retry when the CLI/MCP process has more heap headroom. On Node, set NODE_OPTIONS (for example --max-old-space-size=<MiB>) in the environment that starts the CLI or MCP server, then restart that process, to raise this host heap. Raising max_heap_mb changes the isolated analyzer, not this host admission. The fixed result budget is separate and was not exhausted.",
reportedLimits: {
result_bytes: input.fixedBudgetBytes,
result_budget_remaining_bytes: input.fixedRemainingBytes,
host_result_capacity_bytes: input.hostCapacityBytes,
},
};
};

const sourceLocationSchema = z.object({
script_key: z.string(),
line: z.number().int().min(1).nullable(),
Expand Down
Loading
Loading