Add turn-interrupt replay coverage and protocol logging

- add interrupt and mid-tool replay fixtures for Claude and Codex
- log Claude Agent SDK protocol frames to native event traces
- project Codex commandExecution start events into orchestration updates
This commit is contained in:
Julius Marminge committed 2026-08-07 13:17:27 +02:00
1 parent 29934f5dd8
commit fa9a1916f8
22 files changed
+2472 -74

No files matched your search

@@ -25,6 +25,9 @@ import {
TOOL_CALL_READ_ONLY_PROMPT,
TOOL_CALL_READ_ONLY_WORKSPACE_ROOT,
TOOL_CALL_WRITE_PROMPT,
TURN_INTERRUPT_MID_TOOL_PROMPT,
TURN_INTERRUPT_PROMPT,
TURN_INTERRUPT_RECOVERY_PROMPT,
WORKSPACE_NEVER_POLICY,
WEB_SEARCH_PROMPT,
} from "../src/orchestration-v2/testkit/fixtures/shared.ts";
@@ -60,6 +63,26 @@ const CLAUDE_RECORDINGS = {
queryMode: "active_steering",
enableTools: true,
},
turn_interrupt_mid_tool: {
prompts: [TURN_INTERRUPT_MID_TOOL_PROMPT],
defaultTranscriptFile: "fixtures/turn_interrupt_mid_tool/claude_transcript.ndjson",
queryMode: "interrupt",
enableTools: true,
interruptAfter: "tool_use",
},
turn_interrupt: {
prompts: [TURN_INTERRUPT_PROMPT],
defaultTranscriptFile: "fixtures/turn_interrupt/claude_transcript.ndjson",
queryMode: "interrupt",
enableTools: true,
},
turn_interrupt_restart: {
prompts: [TURN_INTERRUPT_MID_TOOL_PROMPT, TURN_INTERRUPT_RECOVERY_PROMPT],
defaultTranscriptFile: "fixtures/turn_interrupt_restart/claude_transcript.ndjson",
queryMode: "interrupt_restart",
enableTools: true,
interruptAfter: "tool_use",
},
tool_call_read_only: {
prompts: [TOOL_CALL_READ_ONLY_PROMPT],
defaultTranscriptFile: "fixtures/tool_call_read_only/claude_transcript.ndjson",
@@ -102,7 +125,12 @@ function readArgValue(name: string): string | undefined {
return index >= 0 ? args[index + 1] : undefined;
}
type ClaudeRecordingQueryMode = "streaming" | "restart" | "active_steering" | "interrupt_restart";
type ClaudeRecordingQueryMode =
| "streaming"
| "restart"
| "active_steering"
| "interrupt"
| "interrupt_restart";
function selectedQueryMode(defaultMode: ClaudeRecordingQueryMode): ClaudeRecordingQueryMode {
const raw = readArgValue("--query-mode") ?? process.env.T3_CLAUDE_REPLAY_QUERY_MODE;
@@ -113,12 +141,13 @@ function selectedQueryMode(defaultMode: ClaudeRecordingQueryMode): ClaudeRecordi
raw === "streaming" ||
raw === "restart" ||
raw === "active_steering" ||
raw === "interrupt" ||
raw === "interrupt_restart"
) {
return raw;
}
throw new Error(
`Unsupported Claude replay query mode '${raw}'. Use 'streaming', 'restart', 'active_steering', or 'interrupt_restart'.`,
`Unsupported Claude replay query mode '${raw}'. Use 'streaming', 'restart', 'active_steering', 'interrupt', or 'interrupt_restart'.`,
);
}
@@ -253,6 +282,7 @@ try {
? {}
: { allowDangerouslySkipPermissions: queryPolicy.allowDangerouslySkipPermissions }),
...(queryPolicy.installPermissionCallback ? { enablePermissionCallback: true } : {}),
...("interruptAfter" in recording ? { interruptAfter: recording.interruptAfter } : {}),
});
await mkdir(path.dirname(outputPath), { recursive: true });
await writeFile(outputPath, encodeTranscriptNdjson(transcript), "utf8");
@@ -23,6 +23,7 @@ import {
THREAD_ROLLBACK_SECOND_PROMPT,
TODO_LIST_PROMPT,
TOOL_CALL_WRITE_PROMPT,
TURN_INTERRUPT_MID_TOOL_PROMPT,
TURN_INTERRUPT_PROMPT,
} from "../src/orchestration-v2/testkit/fixtures/shared.ts";
@@ -51,6 +52,7 @@ const SCENARIO_NAMES = [
"proposed_plan",
"message_steering",
"turn_interrupt",
"turn_interrupt_mid_tool",
"thread_rollback",
] as const;
@@ -89,7 +91,8 @@ type ReplayStep =
readonly type: "interruptedTurn";
readonly label: string;
readonly prompt: string;
readonly interruptAfterMs: number;
readonly interruptAfterMs?: number;
readonly interruptAfterCommandExecutionStarted?: boolean;
readonly turnOverrides?: Omit<TurnStartParams, "input" | "threadId">;
}
| {
@@ -527,6 +530,31 @@ function scenarios(): ReadonlyArray<ReplayScenario> {
},
],
},
{
name: "turn_interrupt_mid_tool",
fileName: "turn_interrupt_mid_tool.ndjson",
description:
"One active turn is interrupted after Codex has already executed a local command.",
runs: [
{
name: "interrupt-after-command-execution",
description: "Start a turn, wait for command execution, then send turn/interrupt.",
prompt: TURN_INTERRUPT_MID_TOOL_PROMPT,
turnDefaults: {
approvalPolicy: "never",
sandboxPolicy: workspaceWriteSandbox(),
},
steps: [
{
type: "interruptedTurn",
label: "interrupt-after-command-execution",
prompt: TURN_INTERRUPT_MID_TOOL_PROMPT,
interruptAfterCommandExecutionStarted: true,
},
],
},
],
},
{
name: "thread_rollback",
fileName: "thread_rollback.ndjson",
@@ -679,11 +707,13 @@ function installReplayHandlers({
client,
startTurn,
completeTurn,
startCommandExecution,
beforeApprovalResponse,
}: {
readonly client: CodexClient.CodexAppServerClientShape;
readonly startTurn: (turnId: string) => Effect.Effect<void>;
readonly completeTurn: (turnId: string) => Effect.Effect<void>;
readonly startCommandExecution: (turnId: string) => Effect.Effect<void>;
readonly beforeApprovalResponse: () => Effect.Effect<void>;
}) {
return Effect.all(
@@ -746,6 +776,16 @@ function installReplayHandlers({
client.handleServerNotification("turn/completed", (payload) =>
completeTurn(payload.turn.id).pipe(Effect.ignore),
),
client.handleServerNotification("item/completed", (payload) =>
isRecord(payload.item) && payload.item.type === "commandExecution"
? startCommandExecution(payload.turnId).pipe(Effect.ignore)
: Effect.void,
),
client.handleServerNotification("item/started", (payload) =>
isRecord(payload.item) && payload.item.type === "commandExecution"
? startCommandExecution(payload.turnId).pipe(Effect.ignore)
: Effect.void,
),
],
{ discard: true },
);
@@ -763,6 +803,7 @@ function runReplaySession({
return Effect.gen(function* () {
const startedTurns = new Map<string, Deferred.Deferred<void>>();
const completedTurns = new Map<string, Deferred.Deferred<void>>();
const startedCommandExecutions = new Map<string, Deferred.Deferred<void>>();
let approvalGate: Deferred.Deferred<void> | undefined;
const getStarted = (turnId: string) => {
const existing = startedTurns.get(turnId);
@@ -782,17 +823,36 @@ function runReplaySession({
Effect.tap((deferred) => Effect.sync(() => completedTurns.set(turnId, deferred))),
);
};
const getCommandExecutionStarted = (turnId: string) => {
const existing = startedCommandExecutions.get(turnId);
if (existing) {
return Effect.succeed(existing);
}
return Deferred.make<void>().pipe(
Effect.tap((deferred) => Effect.sync(() => startedCommandExecutions.set(turnId, deferred))),
);
};
const startTurn = (turnId: string) =>
getStarted(turnId).pipe(Effect.flatMap((deferred) => Deferred.succeed(deferred, void 0)));
const completeTurn = (turnId: string) =>
getCompletion(turnId).pipe(Effect.flatMap((deferred) => Deferred.succeed(deferred, void 0)));
const startCommandExecution = (turnId: string) =>
getCommandExecutionStarted(turnId).pipe(
Effect.flatMap((deferred) => Deferred.succeed(deferred, void 0)),
);
const beforeApprovalResponse = () =>
approvalGate ? Deferred.await(approvalGate) : Effect.void;
const initializeClient = Effect.gen(function* () {
const client = yield* CodexClient.CodexAppServerClient;
yield* installReplayHandlers({ client, startTurn, completeTurn, beforeApprovalResponse });
yield* installReplayHandlers({
client,
startTurn,
completeTurn,
startCommandExecution,
beforeApprovalResponse,
});
yield* client.request("initialize", {
clientInfo: CODEX_CLIENT_INFO,
@@ -840,7 +900,12 @@ function runReplaySession({
}
if (step.type === "interruptedTurn") {
yield* Effect.sleep(`${step.interruptAfterMs} millis`);
if (step.interruptAfterCommandExecutionStarted === true) {
const commandExecutionStarted = yield* getCommandExecutionStarted(turnId);
yield* Deferred.await(commandExecutionStarted);
} else {
yield* Effect.sleep(`${step.interruptAfterMs ?? 1_500} millis`);
}
yield* client.request("turn/interrupt", {
threadId,
turnId,