Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
refactor(openai-codex): remove the wasm admission gate that never fires
Jack Rudenkoc30375e2b24d
diff --git a/ai-docs/architecture/README.md b/ai-docs/architecture/README.mdindex 3ecf8859..960b4411 100644--- a/ai-docs/architecture/README.md+++ b/ai-docs/architecture/README.md@@ -28,7 +28,7 @@ three earlier write-ups this material cites were lost.| [`keychain.md`](keychain.md) | The macOS Keychain backend: the 17ms-vs-28ms measurement that split presence from values, the two `security` behaviours that are traps (hex reads, a "secure" write that stores nothing and exits 0), and why no new `CredentialSource` member | touch `providers/keychain.ts`, `keychain-source.ts`, or the Providers tab's key write/delete || [`wire.md`](wire.md) | Every request from the connection's catalog `wire` and the route registry: the four-step pipeline, Jack's decisions where they diverge from the backend spec (clamp, borrow-and-drop, try in turn), the first-run refusal that stuck for a session, `+` doubling as the chain separator, name gates one layer down, custom endpoints' declared wire, the advisor and discovery on registry hosts | touch `connection-*.ts`, `wire-*.ts`, `declared-wire.ts`, `ComposedHandler` step 5w, an adapter's limit or reasoning fields, the advisor's request, or `+`/`#` model-spec markers || [`adapters.md`](adapters.md) | Layers 1–3 (FormatConverter / ModelTranslator / ProviderTransport), stream-parser selection, errors that ride an HTTP 200, and the `upstream_status` remap with its downstream readers | add a provider, model translator, or stream parser; or change error classification and retry |-| [`code-mode.md`](code-mode.md) | `--code-mode` for `openai-codex`: one freeform `exec` tool running JavaScript in a QuickJS sandbox, nested calls relayed as `tool_use` blocks; the decision and why its gates run in that order, the lite and classic shapes and the lite → classic fallback, the request-scoped binding and the account/session keys, step 5r resume and its tombstones, the input builder (turn records, classic rounds transliterated), the store byte ceilings, and the sandbox measurements (handle ownership, wasm growing to 2 GiB under a 32 MiB heap limit, hence the 256 MiB ceiling per module and 512 MiB per process) | touch `code-mode/`, step 5d or 5r, the Responses leg parser's exec handling, the turn driver's code-mode hooks, or the sandbox limits |+| [`code-mode.md`](code-mode.md) | `--code-mode` for `openai-codex`: one freeform `exec` tool running JavaScript in a QuickJS sandbox, nested calls relayed as `tool_use` blocks; the decision and why its gates run in that order, the lite and classic shapes and the lite → classic fallback, the request-scoped binding and the account/session keys, step 5r resume and its tombstones, the input builder (turn records, classic rounds transliterated), the store byte ceilings, and the sandbox measurements (handle ownership, wasm growing to 2 GiB under a 32 MiB heap limit, hence the 256 MiB ceiling per module, retire-and-replay past it) | touch `code-mode/`, step 5d or 5r, the Responses leg parser's exec handling, the turn driver's code-mode hooks, or the sandbox limits || [`network-recovery.md`](network-recovery.md) | The two-tier connection hold: why the deadline is DERIVED from `API_TIMEOUT_MS` (measured 359.607 s, a default and not a watchdog), why the tier-2 status is 503 and never 429, why the banner is magmux's `overlay` and never a pane of ours (closing a pane wedged Claude Code), why its lease is renewed on its own tick and not by ladder events, the five sites where a network failure wore an auth status code, and why a pane child gets no watchdog | touch the retry ladder, `recovery/`, the tier-2 status, the recovery banner, or any `x-claudish-recovery` reader || [`behavior-layer.md`](behavior-layer.md) | Layer 4, the harness-convention SUPERVISOR: the rule engine, the four hooks, the 115-call plan-mode measurement it was built on, and opt-in telemetry | write a `behavior/` rule, or change harness-conformance handling || [`advisor.md`](advisor.md) | `--advisor` for any main model: why it is independent of `--monitor` yet keeps four of its launch bits, the decorator around the ROUTED handler, id capture by tool name (the `toolu_` prefix that made it a no-op on every foreign provider), the tee and retained session-keyed state, the ten stub paths and advice origin, Claude Code's advisor gates, metered panel billing, and the inherited-placeholder 401 | touch `--advisor`, `advisor-decorator.ts`, `native-handler-advisor.ts`, `advisor-startup.ts`, monitor-gated launch code, or native auth in `claude-runner.ts` |diff --git a/ai-docs/architecture/code-mode.md b/ai-docs/architecture/code-mode.mdindex 3421ad6b..375d6408 100644--- a/ai-docs/architecture/code-mode.md+++ b/ai-docs/architecture/code-mode.md@@ -316,10 +316,12 @@ limit (32 × 32 MiB) is deliberately NOT sized under the ceiling: the heap limitlinear 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). Theceiling 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 summed-over every module still alive exceeds `PROCESS_WASM_CEILING_BYTES = 512 MiB`, so the per-module-ceiling is a per-process one too. 256 MiB, not the 1 GiB first shipped: linear memory never-shrinks, so whatever one runaway script reaches stays resident in the proxy.+over 256 MiB is dropped for reload when its last runtime ends. The per-module ceiling is a+per-process one with no separate gate: a retired module never outlives the slice that retired it+(retire evicts every suspended runtime; the running one releases the module when its slice ends;+measured: the old lease holds 0 live runtimes right after), so only the current module is ever+alive when a new script is admitted. 256 MiB, not the 1 GiB first shipped: linear memory never+shrinks, so whatever one runaway script reaches stays resident until the module is dropped.**Settlement after every slice.** The top-level promise is read FIRST: fulfilled, or rejected withthe `exit()` sentinel → completed, and every outstanding call is DISCARDED (in claudish an unawaited@@ -331,7 +333,7 @@ promise that can never settle`). `blocked([])` is unrepresentable.|---|---|---|| QuickJS heap per runtime | 32 MiB | `failed` (out of memory) || CPU per script, cumulative across slices | 2 s | `failed` (`interrupted: exceeded the 2 s CPU budget`) |-| wasm linear memory per module / summed over live modules | 256 MiB / 512 MiB | slice stopped, module retired, its suspended scripts lost / new script refused |+| wasm linear memory per module (the only module alive at admission) | 256 MiB | a slice that grew it past by more than its heap limit: stopped; any slice ending over it: module retired, its suspended scripts lost and replayed || source | 256 KB | `failed` before running || calls per batch / per script | 32 / 256 | the excess call rejects with a string, so `Promise.allSettled` still finishes || one call input / one batch | 2 MiB / 8 MiB | that call rejects |diff --git a/packages/cli/src/code-mode/sandbox/quickjs-loader.ts b/packages/cli/src/code-mode/sandbox/quickjs-loader.tsindex 751b6043..6a2757ab 100644--- a/packages/cli/src/code-mode/sandbox/quickjs-loader.ts+++ b/packages/cli/src/code-mode/sandbox/quickjs-loader.ts@@ -44,12 +44,6 @@ export const RECYCLE_WASM_BYTES = 256 * 1024 * 1024;* scripts need kilobytes.*/export const WASM_CEILING_BYTES = 256 * 1024 * 1024;-/**- * Wasm memory summed over EVERY module still alive in the process (the- * current one plus retired ones a running script still holds). A new runtime- * is refused while the sum is over this, so retired modules can never pile up.- */-export const PROCESS_WASM_CEILING_BYTES = 2 * WASM_CEILING_BYTES;/** A runtime the lease tracks for the live cap and LRU eviction. */export interface LeasedRuntime {@@ -72,7 +66,6 @@ export class QuickJSModuleLease {constructor(module: QuickJSWASMModule) {this.module = module;- liveLeases.add(this);}/** True once the module failed internally; no new runtime may be created in it. */@@ -91,19 +84,16 @@ export class QuickJSModuleLease {/*** Register a new runtime. At the cap, the least recently active suspended- * runtime is evicted first. Returns why the runtime is refused — the wasm- * memory of every live module is over `PROCESS_WASM_CEILING_BYTES`, or no- * slot can be freed (every live runtime is running) — in which case the- * caller must not create one; undefined when admitted.+ * runtime is evicted first. Returns why the runtime is refused (no slot can+ * be freed: every live runtime is running), in which case the caller must+ * not create one; undefined when admitted.+ *+ * No process-wide memory gate: a retired module never outlives the slice+ * that retired it (`retire` evicts every suspended runtime, the running one+ * releases it when its slice ends), so by the next `admit` only the current+ * module is alive, and `WASM_CEILING_BYTES` already bounds it.*/admit(runtime: LeasedRuntime): string | undefined {- const processBytes = processWasmBytes();- if (processBytes > PROCESS_WASM_CEILING_BYTES) {- log(- `[CodeMode] sandbox: ${processBytes} bytes of wasm memory across ${liveLeases.size} module(s); new script refused`- );- return "claudish: the sandbox is over its memory limit while other scripts finish; try again";- }while (this.live.size >= MAX_LIVE_RUNTIMES) {let oldest: LeasedRuntime | undefined;for (const candidate of this.live) {@@ -134,7 +124,6 @@ export class QuickJSModuleLease {dropCurrent(this);log("[CodeMode] sandbox: idle module over 256 MiB of wasm memory; dropped for reload");}- if (this.live.size === 0 && !this.usable) liveLeases.delete(this);}/**@@ -160,7 +149,6 @@ export class QuickJSModuleLease {return;}}- if (this.live.size === 0) liveLeases.delete(this);}/**@@ -181,7 +169,6 @@ export class QuickJSModuleLease {}}dropCurrent(this);- liveLeases.delete(this);}/** Current wasm linear memory size; 0 when the library does not expose it. */@@ -197,18 +184,6 @@ export class QuickJSModuleLease {const SLOTS_BUSY = "claudish: every sandbox slot is running a script; try again";-/**- * Every lease still alive: the current one, plus a retired one until its last- * runtime ends. Their wasm memory is summed for admission.- */-const liveLeases = new Set<QuickJSModuleLease>();--function processWasmBytes(): number {- let bytes = 0;- for (const lease of liveLeases) bytes += lease.wasmBytes();- return bytes;-}-let current: QuickJSModuleLease | undefined;let loading: Promise<QuickJSModuleLease | undefined> | undefined;/** Set when the packages cannot be imported at all: no retry in this process. */@@ -277,7 +252,6 @@ export function resetQuickJSLoaderForTests(): void {loading = undefined;unavailable = undefined;wasmBytesUnreadableLogged = false;- liveLeases.clear();}/** Whether a sandbox can run in this process (loads the module on first call). */