Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(openai-codex): stop failing an innocent script at the wasm ceiling
Jack Rudenko7d95ca206297
diff --git a/ai-docs/architecture/code-mode.md b/ai-docs/architecture/code-mode.mdindex ec31b087..3421ad6b 100644--- a/ai-docs/architecture/code-mode.md+++ b/ai-docs/architecture/code-mode.md@@ -297,12 +297,26 @@ a fresh module.2048 MiB wasm maximum in 3.8 s before QuickJS reported out of memory; `a.push({i})` reached143 MiB. Linear memory never shrinks. So the interrupt handler also checks the module's`HEAPU8.byteLength` (private, guarded; a module where it reads 0 logs once that the ceilings are-off): a slice that grows it past `WASM_CEILING_BYTES = 256 MiB` is stopped (`out of memory: the-script grew the sandbox's memory past claudish's limit`) and the module is RETIRED: no new script-is admitted, and every SUSPENDED runtime in it is evicted at once (its record goes `lost`; replay-re-runs it in the next module), because a suspended runtime holds the whole module and may wait on-Claude Code for hours. An idle module over 256 MiB is dropped for reload when its last runtime-ends. Admission is also gated process-wide: a new runtime is refused while the wasm memory summed+off) against `WASM_CEILING_BYTES = 256 MiB`, two ways:++- a slice past the ceiling that grew the module by MORE than its own heap limit is stopped (`out of+ memory: the script grew the sandbox's memory past claudish's limit`) — the runaway signature: the+ string-push loop grew it 282 MiB in one slice;+- after EVERY slice, a module over the ceiling is retired with no script blamed. Scripts that each+ stay inside their heap limit still add up, and result delivery grows memory from the host,+ outside any interrupt check (measured: 31 suspended scripts each holding 8 × 1 MiB results, the+ per-script result cap, took the module to 298 MiB with no slice stopped). Blaming the slice that+ happened to cross failed an innocent script (measured: a script allocating 56 MiB within a 64 MiB+ heap, beside 26 holders at 248 MiB, failed "out of memory"; now it completes).++Retiring admits no new script and evicts every SUSPENDED runtime in the module at once (its record+goes `lost`; replay re-runs it in the next module, which 5r loads first: `needs-module`), because a+suspended runtime holds the whole module and may wait on Claude Code for hours. Live cap × heap+limit (32 × 32 MiB) is deliberately NOT sized under the ceiling: the heap limit does not bound+linear memory (one runaway alone reached 298 MiB under it), so no product of the two is a bound,+and lowering either would refuse legitimate scripts (the 8 MiB result budget needs the heap). The+ceiling plus retire-and-replay is the bound; what it costs a holder is one replay. An idle module+over 256 MiB is dropped for reload when its last runtime ends. Admission is also gated process-wide: a new runtime is refused while the wasm memory summedover every module still alive exceeds `PROCESS_WASM_CEILING_BYTES = 512 MiB`, so the per-moduleceiling is a per-process one too. 256 MiB, not the 1 GiB first shipped: linear memory nevershrinks, so whatever one runaway script reaches stays resident in the proxy.diff --git a/packages/cli/src/code-mode/sandbox/script-runtime.ts b/packages/cli/src/code-mode/sandbox/script-runtime.tsindex 67017b7b..20019d37 100644--- a/packages/cli/src/code-mode/sandbox/script-runtime.ts+++ b/packages/cli/src/code-mode/sandbox/script-runtime.ts@@ -365,9 +365,13 @@ class QuickJSScriptRuntime implements ScriptRuntime, LeasedRuntime {return true;}// The runtime's memory limit does not bound the module's linear memory- // (see WASM_CEILING_BYTES); a slice that grows it past the ceiling stops.+ // (see WASM_CEILING_BYTES). A slice past the ceiling is stopped only when+ // IT grew the module by more than its own heap limit — the runaway+ // signature (measured: one string-push loop grew it 282 MiB in one+ // slice). Memory other scripts hold is not this script's fault: that+ // case retires the module after the slice instead (`slice()`).const wasm = this.lease.wasmBytes();- if (wasm > WASM_CEILING_BYTES && wasm > this.sliceWasmBytes) {+ if (wasm > WASM_CEILING_BYTES && wasm - this.sliceWasmBytes > this.limits.memoryBytes) {this.wasmExceeded = true;return true;}@@ -554,9 +558,23 @@ class QuickJSScriptRuntime implements ScriptRuntime, LeasedRuntime {this.sliceStart = -1;this.running = false;this.lastActive = Date.now();+ this.retireIfOverCeiling();}}+ /**+ * After every slice: a module whose scripts TOGETHER hold more than the+ * ceiling is retired, with no script blamed. Result delivery grows memory+ * from the host, outside any interrupt check (measured: 31 scripts each+ * holding 8 × 1 MiB results took the module to 298 MiB with no slice+ * stopped). Retiring evicts every suspended runtime, this one included once+ * its slice is over; each replays in a fresh module when its results come.+ */+ private retireIfOverCeiling(): void {+ if (!this.lease.usable || this.lease.wasmBytes() <= WASM_CEILING_BYTES) return;+ this.lease.retire("the scripts in it hold more memory than the ceiling");+ }+private settleTop(): ScriptStep {if (this.interrupted || this.wasmExceeded) return this.cpuExceeded();const ctx = this.requireCtx();