Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(openai-codex): never hand a forked session the owner's next batch
Jack Rudenkoa59220a7148f
diff --git a/ai-docs/architecture/code-mode.md b/ai-docs/architecture/code-mode.mdindex 8d979212..ebab5735 100644--- a/ai-docs/architecture/code-mode.md+++ b/ai-docs/architecture/code-mode.md@@ -184,7 +184,7 @@ for the same script.|---|---|| suspended, owned by this session, expected batch | results resolve or reject by seq (`is_error` rejects with a plain string, as Codex); missing → "no tool_result returned"; blocked again → **synthetic reply**; done → output stored, the upstream turn continues and the input builder renders it || `[Request interrupted by user` beside the results | terminated: output so far, the results, "interrupted by the user"; continue. A LOST record is replayed only up to the answered round and terminated there (never fed its results), so Esc yields no further tool call |-| consumed batch, same `batchHash` (a duplicate request) | the memoised step: the same synthetic reply, or the stored completion |+| consumed batch, same `batchHash` (a duplicate request) | the memoised step: the same synthetic reply, or the stored completion. The reply goes to the OWNER only: a fork asking while the owner's next batch is pending gets the forked-script `Script failed` below, never that batch (its tools would run twice) || consumed batch, different `batchHash` (a rewind that re-ran tools) | lost path, tombstone under the new completion key || suspended, owned by ANOTHER session (a fork) | untouched; the fork gets `Script failed` "this script belongs to the session this one was forked from; claudish cannot run it twice"; the owner's later run output wins every lookup || record lost, source kept, owned by this session, the request answers its LAST round | **replay** (`replay.ts`): the source re-runs in a fresh runtime and each batch is answered from history, provided batch k is exactly the k-th round (same ids, names and canonical inputs); then as the first row. `Math.random` is seeded by the call id; a script that branches on `Date` diverges |diff --git a/packages/cli/src/code-mode/resume.ts b/packages/cli/src/code-mode/resume.tsindex 16205478..cd300466 100644--- a/packages/cli/src/code-mode/resume.ts+++ b/packages/cli/src/code-mode/resume.ts@@ -274,11 +274,7 @@ class Resume {const hash = batchHash(batchEntries(batch.ids, results));if (batch.batchHash !== undefined) {- // Different results for a batch already consumed (a rewind that re-ran- // the tools): not resumable; a tombstone under the new completion key.- if (batch.batchHash !== hash)- return this.tombstone(execKey, record, LOST_SCRIPT_NOTE, results);- return this.replayDuplicate(record, k);+ return this.answerConsumed(execKey, record, k, batch.batchHash === hash, results);}if (record.state !== "suspended") return { kind: "none" };// A fork answering the original's pending batch: never resumed and never@@ -298,6 +294,25 @@ class Resume {}}+ /** Results for batch `k`, which the script already consumed. */+ private answerConsumed(+ execKey: string,+ record: ExecRecord,+ k: number,+ sameResults: boolean,+ results: ReadonlyMap<string, ClaudeContentBlock>+ ): ResumeOutcome {+ // Different results (a rewind that re-ran the tools): not resumable; a+ // tombstone under the new completion key.+ if (!sameResults) return this.tombstone(execKey, record, LOST_SCRIPT_NOTE, results);+ // Another session (a fork) may read a completed output, never be handed+ // the owner's next batch: its tools would run twice, once per session.+ if (record.sessionKey !== this.view.sessionKey && record.batches[k + 1]?.blocks) {+ return this.tombstone(execKey, record, FORKED_SCRIPT_NOTE, results);+ }+ return this.replayDuplicate(record, k);+ }+/*** The round the LAST message answers: the last round carrying one of its* tool_result ids. Never searched over the whole request's results, which