Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(openai-codex): store a lost script's tombstone after a restart
Jack Rudenkoed786b6f7c9d
diff --git a/ai-docs/architecture/code-mode.md b/ai-docs/architecture/code-mode.mdindex 4028c4f2..a4a73840 100644--- a/ai-docs/architecture/code-mode.md+++ b/ai-docs/architecture/code-mode.md@@ -174,7 +174,11 @@ for one record serialise. Guard: `openai-codex`, `oauth` arm, an account key, a`tool_choice` absent or `auto`. A request with a named tool, `any` or `none` never resumes ascript: consuming a batch there would run the script's next tools outside the turn that owns it;the next ordinary request resumes it. It reads the stores only if a code-mode request already-created them.+created them, with one exception: when none exist (the first request after a restart) but the last+message answers nested ids, 5r creates them (setting not `off`), so the lost script's tombstone is+stored BEFORE 5d builds the input. Without it the builder rendered an unstored `Script failed` on+that request and `Script completed` ("output unavailable after a claudish restart") on the next,+for the same script.| Situation (the last user message answers nested ids) | Behaviour ||---|---|diff --git a/packages/cli/src/code-mode/resume.ts b/packages/cli/src/code-mode/resume.tsindex 6be5f328..a9cab340 100644--- a/packages/cli/src/code-mode/resume.ts+++ b/packages/cli/src/code-mode/resume.ts@@ -79,6 +79,24 @@ export type ResumeOutcome =export interface ResumeOptions {/** The resolved code-mode setting; `off` turns matching suspended records lost. */setting?: CodeModeSetting;+ /**+ * Creates the process's stores when none exist yet but the last message+ * answers nested ids (the first request after a restart), so the lost-script+ * tombstone is stored before 5d builds the input. Default: never create.+ */+ createStores?: () => CodeModeStores;+}++/** True when the last message is a user message carrying a tool_result for a nested id. */+function lastMessageAnswersNestedIds(messages: ClaudeMessage[]): boolean {+ const last = messages[messages.length - 1];+ if (last?.role !== "user") return false;+ return blocksOf(last).some(+ (b) =>+ b.type === "tool_result" &&+ typeof b.tool_use_id === "string" &&+ decodeNestedId(b.tool_use_id) !== undefined+ );}interface RequestView {@@ -502,13 +520,25 @@ export function resumeFromToolResults(opts: ResumeOptions = {}): ResumeOutcome {try {- if (!stores) return { kind: "none" };if (binding?.auth?.arm !== "oauth") return { kind: "none" };const accountKey = accountKeyFromBinding(binding);const sessionKey = sessionKeyFor(extractSessionId(req), accountKey);if (!accountKey || !sessionKey) return { kind: "none" };if (!isAutoToolChoice(toolChoice)) return { kind: "none" };const messages = Array.isArray(req.messages) ? (req.messages as ClaudeMessage[]) : [];+ let active = stores;+ if (+ !active &&+ opts.setting !== "off" &&+ opts.createStores &&+ lastMessageAnswersNestedIds(messages)+ ) {+ // After a restart: no store, but history answers a script claudish no+ // longer has. Without its tombstone the builder rendered `Script failed`+ // now and `Script completed` on the next request for the same script.+ active = opts.createStores();+ }+ if (!active) return { kind: "none" };const resume = new Resume({messages,@@ -518,7 +548,7 @@ export function resumeFromToolResults(results: indexToolResults(messages),tools: Array.isArray(req.tools) ? (req.tools as ClaudeToolLike[]) : [],},- stores+ active);if (opts.setting === "off") {resume.loseAnswered();diff --git a/packages/cli/src/handlers/composed-handler.ts b/packages/cli/src/handlers/composed-handler.tsindex dd31ea76..b1a27d80 100644--- a/packages/cli/src/handlers/composed-handler.ts+++ b/packages/cli/src/handlers/composed-handler.ts@@ -1440,15 +1440,20 @@ export class ComposedHandler implements ModelHandler {// Synchronous from record lookup to state transition. A script that// blocked again is answered here with its next batch — no upstream call,// no further auth, no stats record (nothing was billed) — through the same- // exit as step 8. Anything else continues; a store that was never created- // (no code-mode request in this process) is never created here.+ // exit as step 8. Anything else continues. A store that was never created+ // (no code-mode request in this process) is created here only when the+ // last message answers nested ids — the first request after a restart —+ // so the lost script's tombstone exists before 5d builds the input.if (this.provider.name === "openai-codex") {const resumed = resumeFromToolResults(claudeRequest,binding,peekCodeModeStores(),claudeRequest?.tool_choice,- { setting: this.options.codeMode ?? DEFAULT_CODE_MODE_SETTING }+ {+ setting: this.options.codeMode ?? DEFAULT_CODE_MODE_SETTING,+ createStores: getCodeModeStores,+ });if (resumed.kind === "reply") {const sse = createSyntheticToolUseReply(resumed.blocks, {