Skip to content

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

feat(openai-codex): relay exec tool calls as Claude Code tool_use blocks

Jack Rudenko786010c24155
diff --git a/packages/cli/src/code-mode/relay.ts b/packages/cli/src/code-mode/relay.ts
new file mode 100644
index 00000000..62045c13
--- /dev/null
+++ b/packages/cli/src/code-mode/relay.ts
@@ -0,0 +1,278 @@
+/**
+ * The code-mode side of a Responses turn: what happens when the model's exec
+ * call reaches claudish (architecture §5.11). The turn driver
+ * (`responses-turn.ts`) reaches this only through `CodeModeTurnHooks`, so a
+ * classic provider never loads it.
+ *
+ * onExecCall: an ExecRecord is created (keyed `(accountKey, exec key)`, owned
+ * by this session) and the session is pinned to code mode. The script runs
+ * in a fresh QuickJS runtime:
+ * blocked on N nested calls → the runtime is kept on the suspended record,
+ * the batch is recorded, and the driver relays N tool_use blocks
+ * `toolu_cx_<hex>_<seq>` (Claude Code's own tool names);
+ * finished without blocking → its output is stored and the driver
+ * continues the turn with the exec call + output appended.
+ * An input cut by `response.incomplete` is never run: `Script failed`, so
+ * the model can retry smaller.
+ * onTurnEnd: the message's Responses items go to the turn records.
+ * onTurnAbandoned: Claude Code never saw the batch; the script is terminated.
+ *
+ * Script errors are data (`Script failed`, returned to the model). A store or
+ * sandbox failure is the lost path, never a throw out of the handler.
+ */
+
+import { type CachedReasoningItem, rememberReasoningForCall } from "../adapters/reasoning-cache.js";
+import { log } from "../logger.js";
+import { EXEC_TOOL_NAME, type ExecToolMeta } from "./exec-tool.js";
+import { completionKey } from "./hashes.js";
+import { encodeNestedId, execKeyFor } from "./ids.js";
+import {
+ type ExecOutput,
+ clampExecOutputTokens,
+ formatExecOutput,
+ parseExecPragma,
+} from "./output.js";
+import { type QuickJSModuleLease, loadQuickJSModule } from "./sandbox/quickjs-loader.js";
+import {
+ type NestedCall,
+ type SandboxLimits,
+ type ScriptStep,
+ createScriptRuntime,
+ describeStep,
+} from "./sandbox/script-runtime.js";
+import type { ExecRecord, ScriptStore } from "./script-store.js";
+import type { CodeModeStores } from "./stores.js";
+import type { ToolNameTable } from "./tool-names.js";
+import { turnAnchorFor } from "./turn-records.js";
+import type {
+ ClaudeMessage,
+ CodeModeTurnHooks,
+ ExecTurnStep,
+ NestedToolUse,
+ ResponsesItem,
+} from "./types.js";
+
+/** The failure text of an exec whose input `response.incomplete` cut. */
+export const TRUNCATED_EXEC_ERROR =
+ "exec input was truncated by max_output_tokens; it was not run. Send a shorter script.";
+
+/** The failure text when the sandbox cannot run a script at all. */
+export const SANDBOX_UNAVAILABLE_ERROR =
+ "claudish's script sandbox is unavailable in this process; the script was not run";
+
+export interface CodeModeTurnContext {
+ stores: CodeModeStores;
+ accountKey: string;
+ sessionKey: string;
+ /** The shape this request was sent in, read at exec time (the lite fallback may change it). */
+ shape: () => "lite" | "classic";
+ /** The exec tool's identifier table (`renderExecTool`). */
+ names: ToolNameTable;
+ allTools: readonly ExecToolMeta[];
+ /** Output budget of an exec with no pragma (discovery's truncation limit). */
+ defaultMaxOutputTokens: number;
+ /** The request's last message: what a text-only message answered (its anchor). */
+ previousMessage: ClaudeMessage | undefined;
+ /** Test seam: the sandbox module. Default `loadQuickJSModule`. */
+ loadModule?: () => Promise<QuickJSModuleLease | undefined>;
+ /** Test seam: sandbox limits. */
+ limits?: Partial<SandboxLimits>;
+}
+
+/** The tool_use blocks for a blocked step's calls. */
+export function nestedBlocksFor(
+ execKey: string,
+ calls: [NestedCall, ...NestedCall[]]
+): [NestedToolUse, ...NestedToolUse[]] {
+ const [first, ...rest] = calls.map((call) => ({
+ id: encodeNestedId(execKey, call.seq),
+ name: call.name,
+ input: call.input,
+ }));
+ return [first, ...rest];
+}
+
+/** The two items that carry a finished exec back to the model. */
+export function execItems(
+ callId: string,
+ source: string,
+ output: ExecOutput,
+ maxOutputTokens: number
+): ResponsesItem[] {
+ return [
+ { type: "custom_tool_call", call_id: callId, name: EXEC_TOOL_NAME, input: source },
+ {
+ type: "custom_tool_call_output",
+ call_id: callId,
+ output: formatExecOutput(output, { maxOutputTokens }),
+ },
+ ];
+}
+
+/**
+ * Settle a record on a `done` step: `finish` or `fail`, and store the output
+ * under `key` as a `run` output.
+ */
+export function settleDone(
+ scripts: ScriptStore,
+ record: ExecRecord,
+ output: ExecOutput,
+ key: string,
+ sessionKey: string
+): void {
+ scripts.storeOutput(record, key, output, "run", sessionKey);
+ if (record.state === "running") {
+ scripts.transition(record, output.status === "completed" ? "finish" : "fail");
+ }
+}
+
+/**
+ * Keep a blocked step: the runtime stays on the suspended record and the
+ * batch is recorded (with its blocks, for a duplicate request's replay).
+ */
+export function suspendBlocked(
+ scripts: ScriptStore,
+ record: ExecRecord,
+ blocks: NestedToolUse[]
+): void {
+ scripts.transition(record, "block");
+ record.batches.push({ ids: blocks.map((b) => b.id), blocks });
+}
+
+function reasoningOf(items: ResponsesItem[]): ResponsesItem[] {
+ return items.filter((item) => item.type === "reasoning");
+}
+
+class CodeModeTurn implements CodeModeTurnHooks {
+ constructor(private readonly ctx: CodeModeTurnContext) {}
+
+ private get scripts(): ScriptStore {
+ return this.ctx.stores.scripts;
+ }
+
+ async onExecCall(call: {
+ callId: string;
+ input: string;
+ precedingItems: ResponsesItem[];
+ truncated: boolean;
+ }): Promise<ExecTurnStep> {
+ const { callId, input } = call;
+ const pragma = parseExecPragma(input);
+ const maxOutputTokens = clampExecOutputTokens(
+ pragma.maxOutputTokens ?? this.ctx.defaultMaxOutputTokens
+ );
+ const failed = (error: string): ExecTurnStep => ({
+ kind: "continue",
+ appendItems: execItems(
+ callId,
+ input,
+ { status: "failed", wallMs: 0, parts: [], error },
+ maxOutputTokens
+ ),
+ });
+
+ let record: ExecRecord;
+ try {
+ const reasoning = reasoningOf(call.precedingItems);
+ record = this.scripts.create({
+ accountKey: this.ctx.accountKey,
+ sessionKey: this.ctx.sessionKey,
+ execCallId: execKeyFor(callId),
+ originalCallId: callId,
+ source: input,
+ shape: this.ctx.shape(),
+ reasoningItems: reasoning.length > 0 ? reasoning : undefined,
+ maxOutputTokens,
+ });
+ // This session relayed an exec: a later discovery failure cannot flip it back.
+ this.scripts.pin(this.ctx.sessionKey);
+ } catch (err) {
+ log(`[CodeMode] exec ${callId}: the script store failed: ${err}`);
+ return failed(`claudish could not keep this script's state: ${err}`);
+ }
+
+ try {
+ return await this.run(record, call.truncated);
+ } catch (err) {
+ log(`[CodeMode] exec ${callId}: claudish failed running it: ${err}`);
+ try {
+ if (record.state === "running" || record.state === "suspended") {
+ this.scripts.transition(record, record.state === "running" ? "fail" : "evict");
+ }
+ } catch {
+ // The record is lost either way.
+ }
+ return failed("claudish's sandbox failed internally; this script's state is lost");
+ }
+ }
+
+ /** Run the record's script (never when its input was truncated). */
+ private async run(record: ExecRecord, truncated: boolean): Promise<ExecTurnStep> {
+ const notRun = (error: string): ExecTurnStep =>
+ this.finished(record, { status: "failed", wallMs: 0, parts: [], error });
+ if (truncated) return notRun(TRUNCATED_EXEC_ERROR);
+ const lease = await (this.ctx.loadModule ?? loadQuickJSModule)();
+ if (!lease) return notRun(SANDBOX_UNAVAILABLE_ERROR);
+ const runtime = createScriptRuntime({
+ tools: this.ctx.names,
+ allTools: this.ctx.allTools,
+ limits: this.ctx.limits,
+ seed: record.originalCallId,
+ module: lease,
+ });
+ const step: ScriptStep = runtime.start(record.source);
+ log(`[CodeMode] exec ${record.originalCallId}: ${describeStep(step)}`);
+ if (step.kind === "done") return this.finished(record, step.output);
+ this.scripts.attachRuntime(record, runtime);
+ const blocks = nestedBlocksFor(record.execCallId, step.calls);
+ suspendBlocked(this.scripts, record, blocks);
+ // A later non-code-mode render replays the exec's reasoning once, before
+ // the first nested call (the A3 `_p0` rule).
+ if (record.reasoningItems) {
+ rememberReasoningForCall(
+ blocks[0].id,
+ record.reasoningItems as unknown as CachedReasoningItem[]
+ );
+ }
+ return { kind: "emit-tool-uses", blocks };
+ }
+
+ /** A script that finished without blocking: store its output, continue the turn. */
+ private finished(record: ExecRecord, output: ExecOutput): ExecTurnStep {
+ settleDone(this.scripts, record, output, completionKey([]), this.ctx.sessionKey);
+ return {
+ kind: "continue",
+ appendItems: execItems(
+ record.originalCallId,
+ record.source,
+ output,
+ record.maxOutputTokens ?? this.ctx.defaultMaxOutputTokens
+ ),
+ };
+ }
+
+ onTurnEnd(turn: { anchor?: string; items: ResponsesItem[]; texts: string[] }): void {
+ if (turn.items.length === 0) return;
+ const anchor =
+ turn.anchor ??
+ turnAnchorFor(
+ this.ctx.sessionKey,
+ { role: "assistant", content: turn.texts.map((text) => ({ type: "text", text })) },
+ this.ctx.previousMessage
+ );
+ this.ctx.stores.turns.put(this.ctx.sessionKey, anchor, turn.items);
+ }
+
+ onTurnAbandoned(callId: string): void {
+ const record = this.scripts.peek(this.ctx.accountKey, execKeyFor(callId));
+ if (record?.state === "suspended") {
+ log(`[CodeMode] exec ${callId}: the client left before the batch arrived; terminated`);
+ this.scripts.transition(record, "abandon");
+ }
+ }
+}
+
+/** The hooks for one code-mode Responses turn. */
+export function createCodeModeTurnHooks(ctx: CodeModeTurnContext): CodeModeTurnHooks {
+ return new CodeModeTurn(ctx);
+}
diff --git a/packages/cli/src/code-mode/request.ts b/packages/cli/src/code-mode/request.ts
index 2bf1da60..64bae227 100644
--- a/packages/cli/src/code-mode/request.ts
+++ b/packages/cli/src/code-mode/request.ts
@@ -37,6 +37,7 @@ import type { AuthBinding } from "../providers/transport/types.js";
import {
DEFAULT_EXEC_MAX_OUTPUT_TOKENS,
type ExecToolDefinition,
+ type ExecToolMeta,
renderExecTool,
} from "./exec-tool.js";
import { accountKeyFromBinding, sessionKeyFor, uuidV5 } from "./ids.js";
@@ -50,6 +51,7 @@ import {
} from "./policy.js";
import { isSandboxAvailable } from "./sandbox/quickjs-loader.js";
import { getCodeModeStores } from "./stores.js";
+import type { ToolNameTable } from "./tool-names.js";
import type { ResponsesItem } from "./types.js";
/** The header that marks a lite request (sent over HTTP; Codex's WebSocket uses client_metadata). */
@@ -224,6 +226,14 @@ export interface PreparedCodeMode {
prePayload?: any;
/** What the payload was built from; the lite → classic fallback rebuilds from it. */
context?: CodeModePayloadContext;
+ /** What the turn's exec calls run with (`createCodeModeTurnHooks`). */
+ turn?: {
+ accountKey: string;
+ sessionKey: string;
+ names: ToolNameTable;
+ allTools: ExecToolMeta[];
+ defaultMaxOutputTokens: number;
+ };
}
/** Does a 400 body name the lite header or `reasoning.context` (the lite shape's own rejections)? */
@@ -337,7 +347,19 @@ async function prepare(input: PrepareCodeModeInput): Promise<PreparedCodeMode> {
modelInfo,
};
const request = applyCodeModeToPayload(structuredClone(payload), context);
- return { decision, request, prePayload, context };
+ return {
+ decision,
+ request,
+ prePayload,
+ context,
+ turn: {
+ accountKey,
+ sessionKey,
+ names: rendered.names,
+ allTools: rendered.allTools,
+ defaultMaxOutputTokens,
+ },
+ };
} catch (err) {
log(`[CodeMode] request build failed; sending this request non-code-mode: ${err}`);
return { decision };
diff --git a/packages/cli/src/code-mode/script-store.ts b/packages/cli/src/code-mode/script-store.ts
index 28e63400..a7aabe88 100644
--- a/packages/cli/src/code-mode/script-store.ts
+++ b/packages/cli/src/code-mode/script-store.ts
@@ -38,7 +38,7 @@
import { log } from "../logger.js";
import type { ExecOutput } from "./output.js";
import type { ScriptRuntime } from "./sandbox/script-runtime.js";
-import type { ResponsesItem } from "./types.js";
+import type { NestedToolUse, ResponsesItem } from "./types.js";
export type ExecState = "running" | "suspended" | "completed" | "failed" | "terminated" | "lost";
@@ -94,6 +94,12 @@ export interface StoredOutput {
export interface ExecBatch {
ids: string[];
batchHash?: string;
+ /**
+ * The tool_use blocks as relayed, kept only while the record is live, so a
+ * duplicate request can replay the same reply (dropped when it settles: the
+ * inputs are up to 8 MiB a batch and are not in the byte cap).
+ */
+ blocks?: NestedToolUse[];
}
export interface ExecRecord {
@@ -299,6 +305,7 @@ export class ScriptStore {
record.state = to;
record.lastTouched = this.now();
const nowLive = isLive(to);
+ if (!nowLive) for (const batch of record.batches) batch.blocks = undefined;
if (wasLive && !nowLive) this.settledBytes += record.bytes;
else if (!wasLive && nowLive) this.settledBytes -= record.bytes;
if (!nowLive) this.enforceCaps();
diff --git a/packages/cli/src/code-mode/types.ts b/packages/cli/src/code-mode/types.ts
index 30669435..bf1c1d3e 100644
--- a/packages/cli/src/code-mode/types.ts
+++ b/packages/cli/src/code-mode/types.ts
@@ -30,3 +30,49 @@ export interface ClaudeMessage {
role: string;
content: string | ClaudeContentBlock[];
}
+
+/** A nested tool call relayed to Claude Code as one tool_use block. */
+export interface NestedToolUse {
+ /** `toolu_cx_<hex>_<seq>` (`encodeNestedId`). */
+ id: string;
+ /** Claude Code's own tool name. */
+ name: string;
+ input: Record<string, unknown>;
+}
+
+/**
+ * What the turn driver does after an exec call: relay the script's batch as
+ * tool_use blocks (the message ends `tool_use`), or continue the turn with
+ * another upstream leg whose input carries the exec items.
+ */
+export type ExecTurnStep =
+ | { kind: "emit-tool-uses"; blocks: [NestedToolUse, ...NestedToolUse[]] }
+ | { kind: "continue"; appendItems: ResponsesItem[] };
+
+/**
+ * How the Responses turn driver reaches code mode. The driver never imports
+ * code mode itself, so classic providers never load QuickJS.
+ */
+export interface CodeModeTurnHooks {
+ /**
+ * An exec call ended a leg. `precedingItems` are that leg's assistant text
+ * and reasoning items in order; `truncated` means `response.incomplete` cut
+ * the exec input, which is then never run. Never rejects: a store or sandbox
+ * failure becomes a `Script failed` continuation.
+ */
+ onExecCall(call: {
+ callId: string;
+ input: string;
+ precedingItems: ResponsesItem[];
+ truncated: boolean;
+ }): ExecTurnStep | Promise<ExecTurnStep>;
+ /**
+ * The message ended: store its Responses items (they end before the final
+ * exec call when it ended on nested calls). `anchor` is the message's first
+ * tool_use id; undefined when it has none, and `texts` (its text blocks, in
+ * order) then derive it.
+ */
+ onTurnEnd(record: { anchor?: string; items: ResponsesItem[]; texts: string[] }): void;
+ /** The client went away before the `message_stop` of a relayed batch. */
+ onTurnAbandoned(callId: string): void;
+}
diff --git a/packages/cli/src/handlers/composed-handler.ts b/packages/cli/src/handlers/composed-handler.ts
index 912a170d..193cd5f8 100644
--- a/packages/cli/src/handlers/composed-handler.ts
+++ b/packages/cli/src/handlers/composed-handler.ts
@@ -39,13 +39,16 @@ import type { BehaviorEngine, BehaviorSession } from "../behavior/index.js";
import { getBehaviorEngine } from "../behavior/index.js";
import { accountKeyFromBinding } from "../code-mode/ids.js";
import { type CodeModeSetting, DEFAULT_CODE_MODE_SETTING } from "../code-mode/policy.js";
+import { createCodeModeTurnHooks } from "../code-mode/relay.js";
import {
type CodeModePayloadContext,
+ type PreparedCodeMode,
isLiteRejection,
prepareCodeModeRequest,
rebuildAsClassic,
withoutLiteHeader,
} from "../code-mode/request.js";
+import { getCodeModeStores } from "../code-mode/stores.js";
import {
getLogLevel,
log,
@@ -120,6 +123,7 @@ import { createDevinConnectStream } from "./shared/stream-parsers/devin-connect.
import { createGeminiSseStream } from "./shared/stream-parsers/gemini-sse.js";
import { createOllamaJsonlStream } from "./shared/stream-parsers/ollama-jsonl.js";
import {
+ type CodeModeTurnHooks,
type ResponsesRetryOptions,
createResponsesStreamHandler,
resolveStallPolicy,
@@ -155,6 +159,8 @@ type LegBinding = AuthBinding & LegAuthBinding;
interface ResponsesTurnWiring {
retry: ResponsesRetryOptions;
onTurnSummary: (s: { legs: number; legRetries: number }) => void;
+ /** Code mode for this turn (`openai-codex`, decided at 5d). */
+ codeMode?: CodeModeTurnHooks;
}
/**
@@ -1444,6 +1450,7 @@ export class ComposedHandler implements ModelHandler {
let codeModeShape: "lite" | "classic" | undefined;
let codeModePrePayload: any;
let codeModeContext: CodeModePayloadContext | undefined;
+ let codeModeTurn: PreparedCodeMode["turn"];
if (this.provider.name === "openai-codex") {
const prepared = await prepareCodeModeRequest({
providerName: this.provider.name,
@@ -1463,6 +1470,7 @@ export class ComposedHandler implements ModelHandler {
codeModeShape = prepared.request.shape;
codeModePrePayload = prepared.prePayload;
codeModeContext = prepared.context;
+ codeModeTurn = prepared.turn;
wireHeaders = [...wireHeaders, ...prepared.request.headers];
// 5a'. --model-params again, only for a code-mode payload: the user's
// explicit fields still win over what 5d wrote (idempotent deep merge).
@@ -2399,10 +2407,29 @@ export class ComposedHandler implements ModelHandler {
arm: binding?.auth?.arm,
}),
firstHeadersAt: responseHeadersAt,
+ // A code-mode continuation leg appends to the body that was accepted.
+ primaryBody: () => requestPayload,
},
onTurnSummary: (s) => {
legRetries = s.legRetries;
},
+ // Code mode (decided at 5d): exec calls run in the sandbox and their
+ // batches are relayed as tool_use blocks; `codeModeShape` is read at
+ // exec time because the lite → classic fallback may have changed it.
+ codeMode: codeModeTurn
+ ? createCodeModeTurnHooks({
+ stores: getCodeModeStores(),
+ accountKey: codeModeTurn.accountKey,
+ sessionKey: codeModeTurn.sessionKey,
+ shape: () => codeModeShape ?? "classic",
+ names: codeModeTurn.names,
+ allTools: codeModeTurn.allTools,
+ defaultMaxOutputTokens: codeModeTurn.defaultMaxOutputTokens,
+ previousMessage: Array.isArray(claudeRequest?.messages)
+ ? claudeRequest.messages[claudeRequest.messages.length - 1]
+ : undefined,
+ })
+ : undefined,
};
}
@@ -2829,6 +2856,7 @@ export class ComposedHandler implements ModelHandler {
onExtraBilledInput: (input, detail) =>
this.tokenTracker.addBilledInputTokens(input, detail),
onTurnSummary: responsesTurn?.onTurnSummary,
+ codeMode: responsesTurn?.codeMode,
});
case "anthropic-sse":
diff --git a/packages/cli/src/handlers/shared/stream-parsers/openai-responses-sse.ts b/packages/cli/src/handlers/shared/stream-parsers/openai-responses-sse.ts
index a37a4aeb..4be4909b 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/openai-responses-sse.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/openai-responses-sse.ts
@@ -11,12 +11,14 @@
*/
import type { Context } from "hono";
+import type { CodeModeTurnHooks } from "../../../code-mode/types.js";
import { wrapAnthropicError } from "../anthropic-error.js";
import type { UsageCacheDetail } from "../token-tracker.js";
import { ClaudeSseSink } from "./responses/claude-sse-sink.js";
import { type ResponsesRetryOptions, runResponsesTurn } from "./responses/responses-turn.js";
export type { ResponsesRetryOptions } from "./responses/responses-turn.js";
+export type { CodeModeTurnHooks, ExecTurnStep } from "../../../code-mode/types.js";
export { resolveStallPolicy, type StallPolicy } from "./responses/stall-guard.js";
export function createResponsesStreamHandler(
@@ -97,6 +99,11 @@ export function createResponsesStreamHandler(
* values of `toolNameMap`.
*/
declaredToolNames?: string[];
+ /**
+ * Code mode for this turn (`openai-codex`): exec calls are run through these
+ * hooks (responses/responses-turn.ts). Absent ⇒ exec calls are ignored.
+ */
+ codeMode?: CodeModeTurnHooks;
}
): Response {
if (!response.body) {
@@ -130,6 +137,7 @@ export function createResponsesStreamHandler(
retry: opts.retry,
onExtraBilledInput: opts.onExtraBilledInput,
onTurnSummary: opts.onTurnSummary,
+ codeMode: opts.codeMode,
});
},
// Downstream went away (client disconnect, abort, or timeout). The
diff --git a/packages/cli/src/handlers/shared/stream-parsers/responses/claude-sse-sink.ts b/packages/cli/src/handlers/shared/stream-parsers/responses/claude-sse-sink.ts
index 451d9e43..b6c6ef1e 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/responses/claude-sse-sink.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/responses/claude-sse-sink.ts
@@ -41,6 +41,9 @@ export class ClaudeSseSink {
private lastActivity = Date.now();
private readonly toolBlocks = new Map<number, ToolBlockState>();
private readonly abort = new AbortController();
+ private firstToolId: string | undefined;
+ /** Text blocks of the message by index, in order (code mode anchors a text-only turn by them). */
+ private readonly texts = new Map<number, string>();
constructor(
private readonly controller: ReadableStreamDefaultController<Uint8Array>,
@@ -120,6 +123,7 @@ export class ClaudeSseSink {
}
startBlock(index: number, contentBlock: Record<string, unknown>): void {
+ if (contentBlock.type === "text") this.texts.set(index, "");
this.send("content_block_start", {
type: "content_block_start",
index,
@@ -128,9 +132,23 @@ export class ClaudeSseSink {
}
delta(index: number, delta: Record<string, unknown>): void {
+ if (delta.type === "text_delta" && typeof delta.text === "string") {
+ const text = this.texts.get(index);
+ if (text !== undefined) this.texts.set(index, text + delta.text);
+ }
this.send("content_block_delta", { type: "content_block_delta", index, delta });
}
+ /** The id of the message's first tool_use block, if any. */
+ get firstToolUseId(): string | undefined {
+ return this.firstToolId;
+ }
+
+ /** The message's text blocks so far, in block order. */
+ textBlocks(): string[] {
+ return [...this.texts.entries()].sort((a, b) => a[0] - b[0]).map(([, text]) => text);
+ }
+
stopBlock(index: number): void {
this.send("content_block_stop", { type: "content_block_stop", index });
}
@@ -138,6 +156,7 @@ export class ClaudeSseSink {
/** A tool_use `content_block_start`; the observer fires here and nowhere else. */
startToolBlock(index: number, id: string, name: string): void {
this.toolBlocks.set(index, { name, argsComplete: false, stopped: false });
+ this.firstToolId ??= id;
this.opts.onToolCallObserved?.(name);
this.startBlock(index, { type: "tool_use", id, name, input: {} });
}
@@ -154,6 +173,15 @@ export class ClaudeSseSink {
this.stopBlock(index);
}
+ /** One whole tool_use block: start, its complete arguments as one delta, stop. */
+ emitToolUse(id: string, name: string, argsJson: string): void {
+ const index = this.nextIndex();
+ this.startToolBlock(index, id, name);
+ this.delta(index, { type: "input_json_delta", partial_json: argsJson });
+ this.markToolArgsComplete(index);
+ this.stopToolBlock(index);
+ }
+
/** `message_delta` + `message_stop`. */
endMessage(stopReason: string, usage: { input_tokens: number; output_tokens: number }): void {
this.send("message_delta", {
diff --git a/packages/cli/src/handlers/shared/stream-parsers/responses/responses-leg.ts b/packages/cli/src/handlers/shared/stream-parsers/responses/responses-leg.ts
index db317e3c..af70a609 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/responses/responses-leg.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/responses/responses-leg.ts
@@ -28,6 +28,7 @@ import {
type CachedReasoningItem,
rememberReasoningForCall,
} from "../../../../adapters/reasoning-cache.js";
+import type { ResponsesItem } from "../../../../code-mode/types.js";
import { getLogLevel, log } from "../../../../logger.js";
import type { UsageCacheDetail } from "../../token-tracker.js";
import { formatRawSseLogPayload } from "../openai-sse.js";
@@ -48,8 +49,30 @@ export interface Usage {
cacheDetail?: UsageCacheDetail;
}
+/**
+ * How long a leg keeps reading after its exec call completed, for the
+ * terminal event's usage (R1: it arrives milliseconds after the exec item).
+ */
+export const EXEC_DRAIN_MS = 10_000;
+
export type LegOutcome =
- | { kind: "done"; usage?: Usage; incomplete?: string }
+ | {
+ kind: "done";
+ usage?: Usage;
+ incomplete?: string;
+ /** Code mode only: this leg's assistant text and reasoning items, in order. */
+ items?: ResponsesItem[];
+ }
+ | {
+ /** Code mode: the leg ended on an exec call (never emitted as a block). */
+ kind: "exec";
+ callId: string;
+ input: string;
+ precedingItems: ResponsesItem[];
+ usage?: Usage;
+ /** `response.incomplete` cut the exec input: it is never run. */
+ truncated: boolean;
+ }
| { kind: "stall"; afterMs: number }
| {
kind: "degenerate-wrapper";
@@ -81,8 +104,24 @@ export interface ResponsesLegContext {
* output-bearing event arrived first. Absent or 0 ⇒ unarmed.
*/
stall?: { thresholdMs: number; headersAt: number; clock?: StallClock };
+ /**
+ * Code mode is on for this turn: a `custom_tool_call` named `exec` is held
+ * (never a block) and ends the leg as an `exec` outcome. Off ⇒ such items are
+ * ignored, exactly as before code mode existed.
+ */
+ codeMode?: boolean;
}
+/** A held exec call (code mode). */
+type HeldExec = {
+ callId: string;
+ itemId?: string;
+ acc: string;
+ doneInput?: string;
+ /** Set at `output_item.done`: the input is whole. */
+ final?: string;
+};
+
type FnCall = {
name: string;
arguments: string;
@@ -152,9 +191,37 @@ class ResponsesLeg {
// Tool blocks that have been started but not yet stopped, in emission order.
private readonly openToolBlocks = new Set<FnCall>();
private readonly heldWrappers = new Map<string, HeldWrapper>();
+ // Code mode only: the held exec call, and this leg's assistant text and
+ // reasoning items in order (an exec's precedingItems; a turn record's items).
+ private heldExec: HeldExec | undefined;
+ private readonly items: ResponsesItem[] = [];
+ private drainTimer: ReturnType<typeof setTimeout> | undefined;
+ private cancelReader: (() => void) | undefined;
constructor(private readonly ctx: ResponsesLegContext) {}
+ /** The exec outcome for a held exec whose input is whole. */
+ private execOutcome(truncated: boolean): LegOutcome {
+ const held = this.heldExec as HeldExec;
+ return {
+ kind: "exec",
+ callId: held.callId,
+ input: held.final ?? held.doneInput ?? held.acc,
+ precedingItems: [...this.items],
+ usage: this.usage,
+ truncated,
+ };
+ }
+
+ /** The held exec's input is whole: stop reading at the terminal event, or after EXEC_DRAIN_MS. */
+ private startDrain(): void {
+ if (this.drainTimer) return;
+ this.drainTimer = setTimeout(() => {
+ log(`[ResponsesSSE] no terminal event ${EXEC_DRAIN_MS} ms after the exec call; using it`);
+ this.cancelReader?.();
+ }, EXEC_DRAIN_MS);
+ }
+
private get sink(): ClaudeSseSink {
return this.ctx.sink;
}
@@ -172,6 +239,9 @@ class ResponsesLeg {
const onAbort = () => {
reader.cancel().catch(() => {});
};
+ this.cancelReader = () => {
+ reader.cancel().catch(() => {});
+ };
if (signal.aborted) onAbort();
else signal.addEventListener("abort", onAbort, { once: true });
@@ -241,6 +311,11 @@ class ResponsesLeg {
if (signal.aborted) return { kind: "cancelled", usage: this.usage };
const stalledOutcome = stalled();
if (stalledOutcome) return stalledOutcome;
+ if (this.heldExec?.final !== undefined) {
+ // The exec call was whole before the socket died: run it.
+ this.closeAll();
+ return this.execOutcome(false);
+ }
log(`[ResponsesSSE] Stream error: ${error}`);
this.closeReasoning();
this.closeText();
@@ -254,11 +329,27 @@ class ResponsesLeg {
} finally {
guard?.disarm();
signal.removeEventListener("abort", onAbort);
+ if (this.drainTimer) clearTimeout(this.drainTimer);
}
if (signal.aborted) return { kind: "cancelled", usage: this.usage };
- const stalledOutcome = stalled();
- if (stalledOutcome) return stalledOutcome;
+ return stalled() ?? this.endOfBody();
+ }
+
+ /** The body ended without a stop: how the leg ended. */
+ private endOfBody(): LegOutcome {
+ if (this.heldExec) {
+ this.closeAll();
+ if (this.heldExec.final !== undefined) return this.execOutcome(false);
+ // The body ended inside the exec call: nothing whole to run.
+ log("[ResponsesSSE] stream ended inside the exec call");
+ return {
+ kind: "stream-error",
+ error: new Error("the stream ended inside the exec call"),
+ openToolBlock: false,
+ usage: this.usage,
+ };
+ }
// The stream ended while a wrapper was still held: its arguments were never
// completed, so there is nothing valid to expand.
@@ -273,6 +364,7 @@ class ResponsesLeg {
kind: "done",
usage: this.usage,
...(this.incompleteReason ? { incomplete: this.incompleteReason } : {}),
+ ...(this.ctx.codeMode ? { items: [...this.items] } : {}),
};
}
@@ -319,6 +411,10 @@ class ResponsesLeg {
case "response.function_call_arguments.done":
this.onArgumentsDone(event);
return undefined;
+ case "response.custom_tool_call_input.delta":
+ case "response.custom_tool_call_input.done":
+ this.onExecInput(event);
+ return undefined;
case "response.output_item.done":
return this.onItemDone(event);
case "response.incomplete":
@@ -326,6 +422,8 @@ class ResponsesLeg {
case "response.completed":
case "response.done":
this.usage = readUsage(event.response?.usage ?? event.usage) ?? this.usage;
+ // A whole exec call needs nothing after the terminal event's usage.
+ if (this.heldExec?.final !== undefined) return { stop: this.execOutcome(false) };
return undefined;
case "error":
case "response.failed": {
@@ -350,7 +448,72 @@ class ResponsesLeg {
this.sink.delta(this.textIdx, { type: "text_delta", text: event.delta || "" });
}
+ /** Code mode: the held exec's streamed input (`.delta`) or its whole input (`.done`). */
+ private onExecInput(event: ResponsesEvent): void {
+ const held = this.heldExec;
+ if (!held) return;
+ const id = event.item_id ?? event.call_id;
+ if (id !== undefined && id !== held.itemId && id !== held.callId) return;
+ if (event.type === "response.custom_tool_call_input.delta") held.acc += event.delta || "";
+ else if (typeof event.input === "string") held.doneInput = event.input;
+ }
+
+ /** Code mode: the held exec's `output_item.done` makes its input whole; other items are recorded. */
+ private onCodeModeItemDone(item: ResponsesEvent): boolean {
+ if (item?.type !== "custom_tool_call") {
+ this.recordItem(item);
+ return false;
+ }
+ const held = this.heldExec;
+ if (held && (item.call_id === held.callId || item.id === held.itemId)) {
+ const itemInput = typeof item.input === "string" ? item.input : undefined;
+ held.final = held.doneInput ?? itemInput ?? held.acc;
+ this.startDrain();
+ }
+ return true;
+ }
+
+ /** Code mode: hold the exec call. The text and reasoning before it are closed, never reopened. */
+ private onCustomToolCallAdded(item: ResponsesEvent): void {
+ if (item.name !== "exec" || this.heldExec) {
+ log(`[ResponsesSSE] ignoring custom_tool_call ${item.name ?? ""} (not the exec call)`);
+ return;
+ }
+ this.closeReasoning();
+ this.closeText();
+ this.heldExec = {
+ callId: item.call_id || item.id,
+ itemId: item.id,
+ acc: typeof item.input === "string" ? item.input : "",
+ };
+ log(`[ResponsesSSE] holding exec call ${this.heldExec.callId}`);
+ }
+
+ /** Code mode: an assistant message or reasoning item, kept verbatim for the turn's items. */
+ private recordItem(item: ResponsesEvent): void {
+ if (item?.type === "message" && item.role === "assistant" && Array.isArray(item.content)) {
+ const content = item.content
+ .filter((p: ResponsesEvent) => p?.type === "output_text" && typeof p.text === "string")
+ .map((p: ResponsesEvent) => ({ type: "output_text", text: p.text }));
+ if (content.length === 0) return;
+ const message: ResponsesItem = { type: "message", role: "assistant", content };
+ if (typeof item.phase === "string") message.phase = item.phase;
+ this.items.push(message);
+ } else if (item?.type === "reasoning" && item.encrypted_content) {
+ this.items.push({
+ type: "reasoning",
+ content: item.content ?? [],
+ encrypted_content: item.encrypted_content,
+ summary: item.summary ?? [],
+ });
+ }
+ }
+
private onItemAdded(event: ResponsesEvent): void {
+ if (this.ctx.codeMode && event.item?.type === "custom_tool_call") {
+ this.onCustomToolCallAdded(event.item);
+ return;
+ }
if (event.item?.type !== "function_call") return;
const itemId = event.item.id;
const openaiCallId = event.item.call_id || itemId;
@@ -467,6 +630,7 @@ class ResponsesLeg {
summary: item.summary ?? [],
});
}
+ if (this.ctx.codeMode && this.onCodeModeItemDone(item)) return undefined;
if (item?.type !== "function_call") return undefined;
const callId = item.call_id || item.id;
@@ -509,6 +673,12 @@ class ResponsesLeg {
if (event.response?.usage) {
this.usage = readUsage(event.response.usage, this.usage, true);
}
+ // Code mode: an exec whose input was still streaming is cut, and never run.
+ if (this.heldExec) {
+ const truncated = this.heldExec.final === undefined;
+ if (truncated) log("[ResponsesSSE] exec input cut by response.incomplete; not run");
+ return { stop: this.execOutcome(truncated) };
+ }
// A wrapper still held when the response is cut short never got its
// arguments judged: degenerate by definition.
const pending = this.heldWrappers.values().next().value;
diff --git a/packages/cli/src/handlers/shared/stream-parsers/responses/responses-turn.ts b/packages/cli/src/handlers/shared/stream-parsers/responses/responses-turn.ts
index bc4cb139..14be136c 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/responses/responses-turn.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/responses/responses-turn.ts
@@ -29,17 +29,45 @@
* NOT done in 7b (the status is already flushed), NOT in FallbackHandler (a
* model fault must never move a subscription user onto a metered hop), NOT in
* the transport. After the flush every failure ends with an SSE `error`.
+ *
+ * ── CODE MODE (`openai-codex`, `codeMode` hooks present) ─────────────────────
+ *
+ * A leg can end on an exec call (never emitted as a block). The hooks run the
+ * script and answer with one of two steps:
+ * - emit-tool-uses: the script blocked on nested tool calls → each becomes a
+ * whole tool_use block, the turn's Responses items are stored, and the
+ * message ends `tool_use` (Claude Code runs them; the next request resumes
+ * the script);
+ * - continue: the script finished without blocking → another leg whose body
+ * is the previous leg's body with this leg's items and the exec call +
+ * output appended to `input`.
+ * Bounded by MAX_LEGS_PER_TURN and MAX_INTERSTITIAL_EXECS. A retry inside a
+ * code-mode turn re-sends the CURRENT leg's body, not the first one.
*/
/** Retries per leg position: 3 attempts in all. */
export const MAX_LEG_RETRIES = 2;
+/**
+ * Code mode: upstream legs one downstream message may use (continuations
+ * after an exec that did not block, plus retries), and exec calls answered
+ * inside one message. Either bound ends the message with
+ * {@link CODE_MODE_STOP_TEXT}.
+ */
+export const MAX_LEGS_PER_TURN = 6;
+export const MAX_INTERSTITIAL_EXECS = 6;
+export const CODE_MODE_STOP_TEXT = `\n\n[claudish: stopped after ${MAX_INTERSTITIAL_EXECS} code-mode attempts]`;
+
type RetryDecision =
| { action: "finish" }
| { action: "retry" }
| { action: "end-tool-use" }
+ | { action: "stop-attempts" }
| { action: "error"; message: string };
+/** The next leg's response, or undefined when the turn has ended instead. */
+type NextLeg = { response: Response; headersAt: number } | undefined;
+
/** The trigger in words, for the log line and the exhaustion error. */
function triggerLabel(outcome: LegOutcome): string {
if (outcome.kind === "stall") return `stall ${Math.round(outcome.afterMs / 1000)}s`;
@@ -58,6 +86,11 @@ function sum(values: number[]): number {
return values.reduce((a, b) => a + b, 0);
}
+import type {
+ CodeModeTurnHooks,
+ ExecTurnStep,
+ ResponsesItem,
+} from "../../../../code-mode/types.js";
import { log } from "../../../../logger.js";
import type { LegResponse } from "../../responses-leg-issuer.js";
import type { UsageCacheDetail } from "../../token-tracker.js";
@@ -70,7 +103,9 @@ import {
} from "./responses-leg.js";
import { type StallClock, type StallPolicy, systemStallClock } from "./stall-guard.js";
-export interface ResponsesTurnOptions extends Omit<ResponsesLegContext, "sink"> {
+export interface ResponsesTurnOptions extends Omit<ResponsesLegContext, "sink" | "codeMode"> {
+ /** Code mode for this turn (architecture §5.11). Absent ⇒ exec calls are ignored, as before. */
+ codeMode?: CodeModeTurnHooks;
/**
* Real per-provider context window (tokens) for the model on THIS backend.
* Used only to make the overflow message specific; omit/0 → left out.
@@ -111,6 +146,12 @@ export interface ResponsesRetryOptions {
firstHeadersAt?: number;
/** The stall guard's clock; tests inject a fake one. Default wall clock. */
stallClock?: StallClock;
+ /**
+ * The primary request's upstream body (an object with an `input` array). A
+ * code-mode continuation leg appends to it; absent ⇒ no continuation leg can
+ * be built and an exec that did not block ends the message with an error.
+ */
+ primaryBody?: () => unknown;
}
/** The in-band text for an in-stream API error; context overflow gets an actionable message. */
@@ -162,8 +203,16 @@ function stopReasonFor(incomplete: string | undefined, hasToolUse: boolean): str
class ResponsesTurn {
/** One entry per leg, in order; undefined for a leg that reported no usage. */
private readonly legUsages: Array<Usage | undefined> = [];
+ /** Retries in the whole turn (telemetry). */
private legRetries = 0;
+ /** Retries at the current leg position (a code-mode continuation starts a new one). */
+ private positionRetries = 0;
private usageReported = false;
+ /** The current leg's body; undefined ⇒ the primary request's (`issueLeg(undefined)`). */
+ private currentBody: unknown = undefined;
+ private interstitialExecs = 0;
+ /** Code mode: the Responses items of earlier legs of this message, interstitial execs included. */
+ private readonly turnItems: ResponsesItem[] = [];
constructor(
private readonly sink: ClaudeSseSink,
@@ -230,26 +279,11 @@ class ResponsesTurn {
...this.opts,
sink: this.sink,
stall: this.stallFor(headersAt),
+ codeMode: this.opts.codeMode !== undefined,
});
this.legUsages.push("usage" in outcome ? outcome.usage : undefined);
- const decision = this.retryDecision(outcome);
- if (decision.action === "finish") {
- await this.finish(outcome);
- return;
- }
- if (decision.action === "end-tool-use") {
- // The calls already stopped stand; the trigger's leg is dropped.
- log(`[ResponsesTurn] ${triggerLabel(outcome)} after tool_use blocks — ending tool_use`);
- await this.finishCompleted("tool_use");
- return;
- }
- if (decision.action === "error") {
- log(`[ResponsesTurn] ${decision.message} → error event`);
- this.sink.endWithError(decision.message);
- this.reportUsage();
- return;
- }
- const next = await this.issueRetry(outcome);
+ const next =
+ outcome.kind === "exec" ? await this.onExec(outcome) : await this.afterLeg(outcome);
if (!next) return;
response = next.response;
headersAt = next.headersAt;
@@ -262,6 +296,33 @@ class ResponsesTurn {
}
}
+ /** A leg that did not end on an exec call: finish, retry, or end the message otherwise. */
+ private async afterLeg(outcome: LegOutcome): Promise<NextLeg> {
+ const decision = this.retryDecision(outcome);
+ if (decision.action === "finish") {
+ await this.finish(outcome);
+ return undefined;
+ }
+ if (decision.action === "end-tool-use") {
+ // The calls already stopped stand; the trigger's leg is dropped.
+ log(`[ResponsesTurn] ${triggerLabel(outcome)} after tool_use blocks — ending tool_use`);
+ this.recordTurn([]);
+ await this.finishCompleted("tool_use");
+ return undefined;
+ }
+ if (decision.action === "stop-attempts") {
+ await this.stopAttempts();
+ return undefined;
+ }
+ if (decision.action === "error") {
+ log(`[ResponsesTurn] ${decision.message} → error event`);
+ this.sink.endWithError(decision.message);
+ this.reportUsage();
+ return undefined;
+ }
+ return this.issueRetry(outcome);
+ }
+
private retryDecision(outcome: LegOutcome): RetryDecision {
if (outcome.kind !== "degenerate-wrapper" && outcome.kind !== "stall") {
return { action: "finish" };
@@ -277,12 +338,178 @@ class ResponsesTurn {
}
if (this.sink.hasCompletedToolBlock) return { action: "end-tool-use" };
const max = retry.maxLegRetries ?? MAX_LEG_RETRIES;
- if (this.legRetries >= max) {
- return { action: "error", message: exhaustedMessage(outcome, this.legRetries + 1) };
+ if (this.positionRetries >= max) {
+ return { action: "error", message: exhaustedMessage(outcome, this.positionRetries + 1) };
+ }
+ if (this.opts.codeMode && this.legUsages.length >= MAX_LEGS_PER_TURN) {
+ return { action: "stop-attempts" };
}
return { action: "retry" };
}
+ // ─── code mode ──────────────────────────────────────────────────────
+
+ /** Store this message's Responses items (code mode); a throwing hook changes nothing. */
+ private recordTurn(finalItems: ResponsesItem[]): void {
+ const hooks = this.opts.codeMode;
+ if (!hooks) return;
+ const items = [...this.turnItems, ...finalItems];
+ if (items.length === 0) return;
+ try {
+ hooks.onTurnEnd({
+ anchor: this.sink.firstToolUseId,
+ items,
+ texts: this.sink.textBlocks(),
+ });
+ } catch (err) {
+ log(`[ResponsesTurn] storing the turn record failed: ${err}`);
+ }
+ }
+
+ /** The legs bound was reached: a visible note, then `end_turn`. */
+ private async stopAttempts(): Promise<void> {
+ log(`[ResponsesTurn] code-mode bound reached after ${this.legUsages.length} leg(s)`);
+ const idx = this.sink.nextIndex();
+ this.sink.startBlock(idx, { type: "text", text: "" });
+ this.sink.delta(idx, { type: "text_delta", text: CODE_MODE_STOP_TEXT });
+ this.sink.stopBlock(idx);
+ this.recordTurn([]);
+ await this.finishCompleted("end_turn");
+ }
+
+ /** The behavior layer's one chance to rewrite a buffered nested call. */
+ private repairNested(name: string, args: string): string {
+ if (this.opts.shouldBufferTool?.(name) !== true) return args;
+ try {
+ const repaired = this.opts.onToolCall?.(name, args);
+ if (typeof repaired === "string" && repaired !== args) {
+ log(`[ResponsesSSE] tool call repaired: ${name}`);
+ return repaired;
+ }
+ } catch (err) {
+ log(`[ResponsesSSE] onToolCall threw for ${name}: ${err}`);
+ }
+ return args;
+ }
+
+ private abandon(callId: string): void {
+ try {
+ this.opts.codeMode?.onTurnAbandoned(callId);
+ } catch (err) {
+ log(`[ResponsesTurn] abandoning exec ${callId} failed: ${err}`);
+ }
+ }
+
+ /** A leg ended on an exec call: run it through the hooks. */
+ private async onExec(outcome: Extract<LegOutcome, { kind: "exec" }>): Promise<NextLeg> {
+ const hooks = this.opts.codeMode;
+ if (!hooks || this.sink.signal.aborted) {
+ this.reportUsage();
+ return undefined;
+ }
+ let step: ExecTurnStep;
+ try {
+ step = await hooks.onExecCall({
+ callId: outcome.callId,
+ input: outcome.input,
+ precedingItems: outcome.precedingItems,
+ truncated: outcome.truncated,
+ });
+ } catch (err) {
+ log(`[ResponsesTurn] exec ${outcome.callId} failed in claudish: ${err}`);
+ this.sink.endWithError(`claudish could not run the exec call: ${err}`);
+ this.reportUsage();
+ return undefined;
+ }
+ if (step.kind === "emit-tool-uses") {
+ await this.emitNested(outcome, step.blocks);
+ return undefined;
+ }
+ return this.continueAfterExec(outcome, step.appendItems);
+ }
+
+ /** The script blocked: its calls become tool_use blocks and the message ends `tool_use`. */
+ private async emitNested(
+ outcome: Extract<LegOutcome, { kind: "exec" }>,
+ blocks: Extract<ExecTurnStep, { kind: "emit-tool-uses" }>["blocks"]
+ ): Promise<void> {
+ if (this.sink.signal.aborted) {
+ this.abandon(outcome.callId);
+ this.reportUsage();
+ return;
+ }
+ for (const block of blocks) {
+ this.sink.emitToolUse(
+ block.id,
+ block.name,
+ this.repairNested(block.name, JSON.stringify(block.input ?? {}))
+ );
+ }
+ log(`[ResponsesTurn] exec ${outcome.callId} relayed as ${blocks.length} tool_use block(s)`);
+ this.recordTurn(outcome.precedingItems);
+ if (this.sink.isCancelled) {
+ // Claude Code never saw the batch: its retry starts a new upstream turn.
+ this.abandon(outcome.callId);
+ this.reportUsage();
+ return;
+ }
+ await this.finishCompleted("tool_use");
+ }
+
+ /** The script finished without blocking: another leg carries its output to the model. */
+ private async continueAfterExec(
+ outcome: Extract<LegOutcome, { kind: "exec" }>,
+ appendItems: ResponsesItem[]
+ ): Promise<NextLeg> {
+ if (this.sink.hasToolUse) {
+ // A stray classic call already went out in this message: no further leg.
+ log(`[ResponsesTurn] exec ${outcome.callId} after a tool_use block — ending tool_use`);
+ this.recordTurn(outcome.precedingItems);
+ await this.finishCompleted("tool_use");
+ return undefined;
+ }
+ this.interstitialExecs++;
+ this.turnItems.push(...outcome.precedingItems, ...appendItems);
+ if (
+ this.interstitialExecs > MAX_INTERSTITIAL_EXECS ||
+ this.legUsages.length >= MAX_LEGS_PER_TURN
+ ) {
+ await this.stopAttempts();
+ return undefined;
+ }
+ const retry = this.opts.retry;
+ const base = (this.currentBody ?? retry?.primaryBody?.()) as
+ | { input?: unknown; [field: string]: unknown }
+ | undefined;
+ if (!retry || !base || typeof base !== "object" || !Array.isArray(base.input)) {
+ log("[ResponsesTurn] code-mode continuation has no request body → error event");
+ this.sink.endWithError("claudish: the code-mode continuation has no request body");
+ this.reportUsage();
+ return undefined;
+ }
+ const body = { ...base, input: [...base.input, ...outcome.precedingItems, ...appendItems] };
+ this.currentBody = body;
+ this.positionRetries = 0;
+ log(
+ `[ResponsesTurn] exec ${outcome.callId} finished without tool calls — leg ${this.legUsages.length + 1}`
+ );
+ return this.issue(body);
+ }
+
+ /** Issue a leg with `body` (undefined ⇒ the primary body); undefined when the turn ended instead. */
+ private async issue(body: unknown): Promise<NextLeg> {
+ const retry = this.opts.retry;
+ if (!retry || this.sink.signal.aborted) {
+ this.reportUsage();
+ return undefined;
+ }
+ const leg = await retry.issueLeg(body, this.sink.signal);
+ if (leg.kind === "ok") return { response: leg.response, headersAt: leg.headersAt };
+ if (leg.kind === "error") this.sink.endWithError(leg.message);
+ this.reportUsage();
+ return undefined;
+ }
+
/** Issue the next leg; undefined when the turn has ended instead. */
/**
* This leg's stall guard settings: the final attempt (no retry left) gets the
@@ -295,7 +522,7 @@ class ResponsesTurn {
if (!retry || !policy || !("finalThresholdMs" in policy) || policy.thresholdMs <= 0) {
return undefined;
}
- const isFinal = this.legRetries >= (retry.maxLegRetries ?? MAX_LEG_RETRIES);
+ const isFinal = this.positionRetries >= (retry.maxLegRetries ?? MAX_LEG_RETRIES);
return {
thresholdMs: isFinal ? policy.finalThresholdMs : policy.thresholdMs,
headersAt,
@@ -303,23 +530,15 @@ class ResponsesTurn {
};
}
- private async issueRetry(
- outcome: LegOutcome
- ): Promise<{ response: Response; headersAt: number } | undefined> {
+ /** Re-send the current leg's body (the primary one until a code-mode continuation). */
+ private async issueRetry(outcome: LegOutcome): Promise<NextLeg> {
const retry = this.opts.retry;
if (!retry) return undefined;
this.legRetries++;
+ this.positionRetries++;
const max = retry.maxLegRetries ?? MAX_LEG_RETRIES;
- log(`[ResponsesTurn] ${triggerLabel(outcome)} — retry ${this.legRetries}/${max}`);
- if (this.sink.signal.aborted) {
- this.reportUsage();
- return undefined;
- }
- const leg = await retry.issueLeg(undefined, this.sink.signal);
- if (leg.kind === "ok") return { response: leg.response, headersAt: leg.headersAt };
- if (leg.kind === "error") this.sink.endWithError(leg.message);
- this.reportUsage();
- return undefined;
+ log(`[ResponsesTurn] ${triggerLabel(outcome)} — retry ${this.positionRetries}/${max}`);
+ return this.issue(this.currentBody);
}
/** A completed turn: `message_delta` + `message_stop`, usage, then the turn hooks. */
@@ -344,6 +563,7 @@ class ResponsesTurn {
`[ResponsesSSE] Truncated response (${outcome.incomplete}) → stop_reason=${stopReason}`
);
}
+ this.recordTurn(outcome.items ?? []);
await this.finishCompleted(stopReason);
return;
}