Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(openai-codex): move an evicted script's record to lost at eviction

Jack Rudenko3abb67a84e0d
diff --git a/packages/cli/src/code-mode/relay.ts b/packages/cli/src/code-mode/relay.ts
index 62045c13..e267cfea 100644
--- a/packages/cli/src/code-mode/relay.ts
+++ b/packages/cli/src/code-mode/relay.ts
@@ -219,6 +219,7 @@ class CodeModeTurn implements CodeModeTurnHooks {
limits: this.ctx.limits,
seed: record.originalCallId,
module: lease,
+ onEvicted: () => this.scripts.loseIfRuntimeGone(record),
});
const step: ScriptStep = runtime.start(record.source);
log(`[CodeMode] exec ${record.originalCallId}: ${describeStep(step)}`);
diff --git a/packages/cli/src/code-mode/replay.ts b/packages/cli/src/code-mode/replay.ts
index 378d95a7..4bfd00f9 100644
--- a/packages/cli/src/code-mode/replay.ts
+++ b/packages/cli/src/code-mode/replay.ts
@@ -119,6 +119,7 @@ export function replayLostScript(input: ReplayInput): ReplayResult {
allTools: rendered.allTools,
seed: record.originalCallId,
module: lease,
+ onEvicted: () => scripts.loseIfRuntimeGone(record),
});
let step = runtime.start(record.source);
const hashes: string[] = [];
diff --git a/packages/cli/src/code-mode/sandbox/script-runtime.ts b/packages/cli/src/code-mode/sandbox/script-runtime.ts
index f4640d39..93ad0025 100644
--- a/packages/cli/src/code-mode/sandbox/script-runtime.ts
+++ b/packages/cli/src/code-mode/sandbox/script-runtime.ts
@@ -42,6 +42,7 @@ import type {
QuickJSHandle,
QuickJSRuntime,
} from "quickjs-emscripten-core";
+import { log } from "../../logger.js";
import type { ExecToolMeta } from "../exec-tool.js";
import {
type ExecOutput,
@@ -321,7 +322,16 @@ class QuickJSScriptRuntime implements ScriptRuntime, LeasedRuntime {
evict(): void {
this.wasLost = true;
this.dispose();
- this.onEvicted?.();
+ this.notifyEvicted();
+ }
+
+ /** The owner's callback; its failure is the owner's, never the module's. */
+ private notifyEvicted(): void {
+ try {
+ this.onEvicted?.();
+ } catch (err) {
+ log(`[CodeMode] sandbox: the eviction callback threw: ${err}`);
+ }
}
/** LeasedRuntime: the module is poisoned; drop handles without touching wasm. */
@@ -332,7 +342,7 @@ class QuickJSScriptRuntime implements ScriptRuntime, LeasedRuntime {
this.topHandle = this.cutHandle = this.exitHandle = undefined;
this.ctx = undefined;
this.rt = undefined;
- this.onEvicted?.();
+ this.notifyEvicted();
}
// ─── context setup ──────────────────────────────────────────────────
diff --git a/packages/cli/src/code-mode/script-store.ts b/packages/cli/src/code-mode/script-store.ts
index a7aabe88..b7857663 100644
--- a/packages/cli/src/code-mode/script-store.ts
+++ b/packages/cli/src/code-mode/script-store.ts
@@ -262,10 +262,20 @@ export class ScriptStore {
const record = this.records.get(recordKey(accountKey, execCallId));
if (!record) return undefined;
record.lastTouched = this.now();
+ this.loseIfRuntimeGone(record);
+ return record;
+ }
+
+ /**
+ * A suspended record whose runtime is gone (evicted for the live cap, a
+ * retired or poisoned module) moves to `lost` NOW, so it leaves the
+ * suspended set and enters the settled caps. The runtime's `onEvicted`
+ * calls this; `get()` and the reaper catch any it missed.
+ */
+ loseIfRuntimeGone(record: ExecRecord): void {
if (record.state === "suspended" && (!record.runtime || !record.runtime.alive)) {
this.transition(record, "evict");
}
- return record;
}
/** The record without touching it or checking its runtime. */
@@ -379,10 +389,11 @@ export class ScriptStore {
// ─── caps and reaping ───────────────────────────────────────────────
- /** Expire idle suspended records and enforce the caps. */
+ /** Lose suspended records whose runtime is gone, expire idle ones, enforce the caps. */
reap(): void {
const now = this.now();
for (const record of [...this.records.values()]) {
+ this.loseIfRuntimeGone(record);
if (record.state === "suspended" && now - record.lastTouched > this.limits.suspendedTtlMs) {
this.transition(record, "expire");
}