The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeOS from "node:os"; |
| 2 | |
| 3 | import { CODEX_THREAD_CONFIG } from "../src/orchestration-v2/Adapters/CodexAdapterV2.ts"; |
| 4 | import { revertCodexThread } from "../src/provider/CodexThreadRevert.ts"; |
| 5 | import { buildCodexInitializeParams } from "../src/provider/CodexProvider.ts"; |
| 6 | import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 7 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 8 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 9 | import { resolveSpawnCommand } from "@t3tools/shared/shell"; |
| 10 | import * as Console from "effect/Console"; |
| 11 | import * as Deferred from "effect/Deferred"; |
| 12 | import * as FileSystem from "effect/FileSystem"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as Path from "effect/Path"; |
| 15 | import * as PlatformError from "effect/PlatformError"; |
| 16 | import * as Config from "effect/Config"; |
| 17 | import * as Option from "effect/Option"; |
| 18 | import * as Effect from "effect/Effect"; |
| 19 | import { ChildProcess, ChildProcessSpawner } from "effect/process"; |
| 20 | |
| 21 | import * as CodexClient from "effect-codex-app-server/client"; |
| 22 | import type * as CodexSchema from "effect-codex-app-server/schema"; |
| 23 | |
| 24 | import { |
| 25 | MESSAGE_STEERING_INITIAL_PROMPT, |
| 26 | MESSAGE_STEERING_STEER_PROMPT, |
| 27 | MULTI_TURN_FIRST_PROMPT, |
| 28 | MULTI_TURN_SECOND_PROMPT, |
| 29 | PLAN_QUESTIONS_PROMPT, |
| 30 | PROPOSED_PLAN_PROMPT, |
| 31 | PROVIDER_THREAD_RESUME_FIRST_PROMPT, |
| 32 | PROVIDER_THREAD_RESUME_SECOND_PROMPT, |
| 33 | SIMPLE_PROMPT, |
| 34 | SUBAGENT_CONTINUE_PARENT_PROMPT, |
| 35 | SUBAGENT_CONTINUE_PROMPT, |
| 36 | SUBAGENT_PROMPT, |
| 37 | SUBAGENT_V2_APPROVAL_PROMPT, |
| 38 | SUBAGENT_V2_PROMPT, |
| 39 | SUBAGENT_V2_NESTED_APPROVAL_PROMPT, |
| 40 | SUBAGENT_V2_NESTED_PROMPT, |
| 41 | THREAD_ROLLBACK_AFTER_PROMPT, |
| 42 | THREAD_ROLLBACK_FIRST_PROMPT, |
| 43 | THREAD_ROLLBACK_SECOND_PROMPT, |
| 44 | THREAD_MERGE_BACK_SIBLINGS_FIRST_FORK_PROMPT, |
| 45 | THREAD_MERGE_BACK_SIBLINGS_FIRST_HANDOFF_PROMPT, |
| 46 | THREAD_MERGE_BACK_SIBLINGS_RECALL_PROMPT, |
| 47 | THREAD_MERGE_BACK_SIBLINGS_SECOND_FORK_PROMPT, |
| 48 | THREAD_MERGE_BACK_SIBLINGS_SECOND_HANDOFF_PROMPT, |
| 49 | THREAD_MERGE_BACK_SIBLINGS_SOURCE_PROMPT, |
| 50 | TODO_LIST_PROMPT, |
| 51 | TOOL_CALL_WRITE_PROMPT, |
| 52 | TURN_INTERRUPT_MID_TOOL_PROMPT, |
| 53 | COMMAND_OUTPUT_STREAMING_PROMPT, |
| 54 | TURN_INTERRUPT_PROMPT, |
| 55 | WEB_SEARCH_PROMPT, |
| 56 | } from "../src/orchestration-v2/testkit/fixtures/shared.ts"; |
| 57 | import { codexReplayRecordingOutputRecords } from "./codexReplayRecordingRecords.ts"; |
| 58 | import { makeReplayRecorderDeferredRegistry } from "./replayRecorderDeferredRegistry.ts"; |
| 59 | |
| 60 | const CODEX_REPLAY_PLAN_MODE_DEVELOPER_INSTRUCTIONS = |
| 61 | process.env.T3_CODEX_REPLAY_PLAN_DEVELOPER_INSTRUCTIONS ?? |
| 62 | "You are in Plan mode. Prefer request_user_input for clarifying questions. When presenting a complete plan, wrap it in <proposed_plan> and </proposed_plan>."; |
| 63 | // Match V2 adapter frames so recordings replay unchanged. |
| 64 | const CODEX_CLIENT_CAPABILITIES = { |
| 65 | experimentalApi: true, |
| 66 | optOutNotificationMethods: ["turn/diff/updated"], |
| 67 | extensions: { "io.modelcontextprotocol/ui": { mimeTypes: ["text/html;profile=mcp-app"] } }, |
| 68 | } as const; |
| 69 | const CODEX_REPLAY_DEFAULT_MODEL = "gpt-6-luna"; |
| 70 | const CODEX_REPLAY_MODEL_OVERRIDE = readArgValue("--model") ?? process.env.T3_CODEX_REPLAY_MODEL; |
| 71 | |
| 72 | const SCENARIO_NAMES = [ |
| 73 | "simple", |
| 74 | "tool_call_read_only_on_request", |
| 75 | "tool_call_workspace_never", |
| 76 | "tool_call_restricted_granular", |
| 77 | "subagent", |
| 78 | "subagent_continue", |
| 79 | "subagent_v2", |
| 80 | "subagent_v2_approval", |
| 81 | "subagent_v2_nested", |
| 82 | "subagent_v2_nested_approval", |
| 83 | "multi_turn", |
| 84 | "queued_turn", |
| 85 | "provider_thread_resume", |
| 86 | "todo_list", |
| 87 | "web_search", |
| 88 | "plan_questions", |
| 89 | "proposed_plan", |
| 90 | "message_steering", |
| 91 | "turn_interrupt", |
| 92 | "turn_interrupt_mid_tool", |
| 93 | "command_output_streaming", |
| 94 | "thread_rollback", |
| 95 | "thread_rollback_after_restart", |
| 96 | "thread_rollback_to_stopped_turn", |
| 97 | "thread_merge_back_siblings", |
| 98 | ] as const; |
| 99 | |
| 100 | type ScenarioName = (typeof SCENARIO_NAMES)[number]; |
| 101 | type TurnStartParams = CodexSchema.ClientRequestParamsByMethod["turn/start"] & { |
| 102 | readonly collaborationMode?: CodexSchema.V2TurnStartParams__CollaborationMode; |
| 103 | }; |
| 104 | type TurnStartInput = TurnStartParams["input"]; |
| 105 | type TurnStartResponse = CodexSchema.ClientRequestResponsesByMethod["turn/start"]; |
| 106 | type SandboxPolicy = NonNullable<TurnStartParams["sandboxPolicy"]>; |
| 107 | type ApprovalPolicy = NonNullable<TurnStartParams["approvalPolicy"]>; |
| 108 | |
| 109 | interface ReplayRun { |
| 110 | readonly name: string; |
| 111 | readonly prompt?: string; |
| 112 | readonly description: string; |
| 113 | readonly steps: ReadonlyArray<ReplayStep>; |
| 114 | readonly turnDefaults?: Omit<TurnStartParams, "input" | "threadId">; |
| 115 | readonly interactionMode?: "plan"; |
| 116 | /** Recorder overrides; replay ignores keys listed in the header's `recorderThreadConfigKeys`. */ |
| 117 | readonly threadConfig?: Readonly<Record<string, boolean | number | string>>; |
| 118 | } |
| 119 | |
| 120 | type ReplayStep = |
| 121 | | { |
| 122 | readonly type: "turn"; |
| 123 | readonly label: string; |
| 124 | readonly prompt: string; |
| 125 | readonly thread?: string; |
| 126 | readonly turnOverrides?: Omit<TurnStartParams, "input" | "threadId">; |
| 127 | } |
| 128 | | { |
| 129 | readonly type: "steeredTurn"; |
| 130 | readonly label: string; |
| 131 | readonly prompt: string; |
| 132 | readonly steer: string; |
| 133 | readonly turnOverrides?: Omit<TurnStartParams, "input" | "threadId">; |
| 134 | } |
| 135 | | { |
| 136 | readonly type: "interruptedTurn"; |
| 137 | readonly label: string; |
| 138 | readonly prompt: string; |
| 139 | readonly interruptAfterMs?: number; |
| 140 | readonly interruptAfterCommandExecutionStarted?: boolean; |
| 141 | readonly turnOverrides?: Omit<TurnStartParams, "input" | "threadId">; |
| 142 | } |
| 143 | | { |
| 144 | readonly type: "rollback"; |
| 145 | readonly label: string; |
| 146 | readonly numTurns: number; |
| 147 | } |
| 148 | | { |
| 149 | readonly type: "fork"; |
| 150 | readonly label: string; |
| 151 | readonly from?: string; |
| 152 | readonly as?: string; |
| 153 | }; |
| 154 | type TurnReplayStep = Exclude< |
| 155 | ReplayStep, |
| 156 | { readonly type: "rollback" } | { readonly type: "fork" } |
| 157 | >; |
| 158 | |
| 159 | interface ReplayScenario { |
| 160 | readonly name: ScenarioName; |
| 161 | readonly fileName: `${ScenarioName}.ndjson`; |
| 162 | readonly description: string; |
| 163 | readonly model?: string; |
| 164 | readonly runs: ReadonlyArray<ReplayRun>; |
| 165 | } |
| 166 | |
| 167 | interface Recorder { |
| 168 | readonly path: string; |
| 169 | readonly setVersion: (version: string) => Effect.Effect<void>; |
| 170 | readonly writeRecord: ( |
| 171 | record: Record<string, unknown>, |
| 172 | ) => Effect.Effect<void, PlatformError.PlatformError>; |
| 173 | readonly flush: () => Effect.Effect<void, PlatformError.PlatformError>; |
| 174 | } |
| 175 | |
| 176 | function readArgValue(name: string): string | undefined { |
| 177 | const args = process.argv.slice(2); |
| 178 | const index = args.indexOf(name); |
| 179 | return index >= 0 ? args[index + 1] : undefined; |
| 180 | } |
| 181 | |
| 182 | function readArgValues(name: string): ReadonlyArray<string> { |
| 183 | const args = process.argv.slice(2); |
| 184 | return args.flatMap((arg, index) => (arg === name && args[index + 1] ? [args[index + 1]!] : [])); |
| 185 | } |
| 186 | |
| 187 | function defaultTranscriptUrl(scenario: ReplayScenario): URL { |
| 188 | return new URL( |
| 189 | `../src/orchestration-v2/testkit/fixtures/${scenario.name}/codex_transcript.ndjson`, |
| 190 | import.meta.url, |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | function isRecord(value: unknown): value is Record<string, unknown> { |
| 195 | return typeof value === "object" && value !== null; |
| 196 | } |
| 197 | |
| 198 | function parseScenarios(): ReadonlyArray<ScenarioName> { |
| 199 | const rawValues = [ |
| 200 | ...readArgValues("--scenario"), |
| 201 | ...(process.env.T3_CODEX_REPLAY_SCENARIOS ? [process.env.T3_CODEX_REPLAY_SCENARIOS] : []), |
| 202 | ]; |
| 203 | const requested = rawValues.length > 0 ? rawValues : ["simple"]; |
| 204 | const names = requested.flatMap((value) => |
| 205 | value |
| 206 | .split(",") |
| 207 | .map((part) => part.trim()) |
| 208 | .filter((part) => part.length > 0), |
| 209 | ); |
| 210 | |
| 211 | if (names.includes("all")) { |
| 212 | return SCENARIO_NAMES; |
| 213 | } |
| 214 | |
| 215 | const expandedNames = names.flatMap((name) => |
| 216 | name === "tool_call" |
| 217 | ? [ |
| 218 | "tool_call_read_only_on_request", |
| 219 | "tool_call_workspace_never", |
| 220 | "tool_call_restricted_granular", |
| 221 | ] |
| 222 | : [name], |
| 223 | ); |
| 224 | |
| 225 | const invalid = expandedNames.filter( |
| 226 | (name): name is string => !SCENARIO_NAMES.includes(name as ScenarioName), |
| 227 | ); |
| 228 | if (invalid.length > 0) { |
| 229 | throw new Error(`Unknown scenario(s): ${invalid.join(", ")}`); |
| 230 | } |
| 231 | |
| 232 | return [...new Set(expandedNames)] as ReadonlyArray<ScenarioName>; |
| 233 | } |
| 234 | |
| 235 | function classifyJsonRpcPayload(payload: unknown): string { |
| 236 | if (!isRecord(payload)) { |
| 237 | return "unknown"; |
| 238 | } |
| 239 | if (typeof payload.method === "string" && "id" in payload) { |
| 240 | return "request"; |
| 241 | } |
| 242 | if (typeof payload.method === "string") { |
| 243 | return "notification"; |
| 244 | } |
| 245 | if ("id" in payload && "error" in payload) { |
| 246 | return "error_response"; |
| 247 | } |
| 248 | if ("id" in payload && "result" in payload) { |
| 249 | return "response"; |
| 250 | } |
| 251 | return "unknown"; |
| 252 | } |
| 253 | |
| 254 | function protocolMethod(payload: unknown): string | undefined { |
| 255 | return isRecord(payload) && typeof payload.method === "string" ? payload.method : undefined; |
| 256 | } |
| 257 | |
| 258 | function protocolId(payload: unknown): string | number | undefined { |
| 259 | if (!isRecord(payload)) { |
| 260 | return undefined; |
| 261 | } |
| 262 | return typeof payload.id === "string" || typeof payload.id === "number" ? payload.id : undefined; |
| 263 | } |
| 264 | |
| 265 | function protocolResult(payload: unknown): unknown { |
| 266 | return isRecord(payload) && "result" in payload ? payload.result : undefined; |
| 267 | } |
| 268 | |
| 269 | function inferCodexVersion(payload: unknown): string | undefined { |
| 270 | const result = protocolResult(payload); |
| 271 | if (!isRecord(result)) { |
| 272 | return undefined; |
| 273 | } |
| 274 | |
| 275 | if (typeof result.userAgent === "string") { |
| 276 | const match = result.userAgent.match(/\/([0-9][^\s)]*)/u); |
| 277 | if (match?.[1]) { |
| 278 | return match[1]; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | const thread = result.thread; |
| 283 | return isRecord(thread) && typeof thread.cliVersion === "string" ? thread.cliVersion : undefined; |
| 284 | } |
| 285 | |
| 286 | function turnInput(prompt: string): TurnStartInput { |
| 287 | return [{ type: "text", text: prompt }]; |
| 288 | } |
| 289 | |
| 290 | function getTurnId(response: TurnStartResponse): string { |
| 291 | return response.turn.id; |
| 292 | } |
| 293 | |
| 294 | function readOnlyFullAccessSandbox(): SandboxPolicy { |
| 295 | return { |
| 296 | networkAccess: false, |
| 297 | type: "readOnly", |
| 298 | }; |
| 299 | } |
| 300 | |
| 301 | function readOnlyRestrictedSandbox(): SandboxPolicy { |
| 302 | return { |
| 303 | networkAccess: false, |
| 304 | type: "readOnly", |
| 305 | }; |
| 306 | } |
| 307 | |
| 308 | function workspaceWriteSandbox(): SandboxPolicy { |
| 309 | return { |
| 310 | networkAccess: false, |
| 311 | type: "workspaceWrite", |
| 312 | writableRoots: [], |
| 313 | }; |
| 314 | } |
| 315 | |
| 316 | function granularApprovalPolicy(): ApprovalPolicy { |
| 317 | return { |
| 318 | granular: { |
| 319 | mcp_elicitations: true, |
| 320 | request_permissions: true, |
| 321 | rules: true, |
| 322 | sandbox_approval: true, |
| 323 | skill_approval: true, |
| 324 | }, |
| 325 | }; |
| 326 | } |
| 327 | |
| 328 | function scenarioModel(scenario: ReplayScenario): string { |
| 329 | return CODEX_REPLAY_MODEL_OVERRIDE ?? scenario.model ?? CODEX_REPLAY_DEFAULT_MODEL; |
| 330 | } |
| 331 | |
| 332 | function planCollaborationMode(model: string): CodexSchema.V2TurnStartParams__CollaborationMode { |
| 333 | return { |
| 334 | mode: "plan", |
| 335 | settings: { |
| 336 | developer_instructions: CODEX_REPLAY_PLAN_MODE_DEVELOPER_INSTRUCTIONS, |
| 337 | model, |
| 338 | reasoning_effort: "medium", |
| 339 | }, |
| 340 | }; |
| 341 | } |
| 342 | |
| 343 | function scenarios(): ReadonlyArray<ReplayScenario> { |
| 344 | return [ |
| 345 | { |
| 346 | name: "simple", |
| 347 | fileName: "simple.ndjson", |
| 348 | description: "One thread and one turn with a deterministic text-only response.", |
| 349 | runs: [ |
| 350 | { |
| 351 | name: "simple", |
| 352 | description: "Single text-only turn.", |
| 353 | prompt: SIMPLE_PROMPT, |
| 354 | steps: [{ type: "turn", label: "simple", prompt: SIMPLE_PROMPT }], |
| 355 | }, |
| 356 | ], |
| 357 | }, |
| 358 | { |
| 359 | name: "tool_call_read_only_on_request", |
| 360 | fileName: "tool_call_read_only_on_request.ndjson", |
| 361 | description: |
| 362 | "Write a small fixture file with read-only full filesystem visibility and on-request approvals.", |
| 363 | // gpt-6-luna declines to attempt the write under a read-only sandbox; gpt-6-sol tries it. |
| 364 | model: "gpt-6-sol", |
| 365 | runs: [ |
| 366 | { |
| 367 | name: "read-only-on-request", |
| 368 | description: |
| 369 | "Write action under read-only full filesystem visibility with on-request approvals.", |
| 370 | prompt: TOOL_CALL_WRITE_PROMPT, |
| 371 | turnDefaults: { |
| 372 | approvalPolicy: "on-request", |
| 373 | sandboxPolicy: readOnlyFullAccessSandbox(), |
| 374 | }, |
| 375 | steps: [{ type: "turn", label: "write-fixture-file", prompt: TOOL_CALL_WRITE_PROMPT }], |
| 376 | }, |
| 377 | ], |
| 378 | }, |
| 379 | { |
| 380 | name: "tool_call_workspace_never", |
| 381 | fileName: "tool_call_workspace_never.ndjson", |
| 382 | description: |
| 383 | "Write a small fixture file with workspace-write sandbox policy and never approvals.", |
| 384 | runs: [ |
| 385 | { |
| 386 | name: "workspace-never", |
| 387 | description: |
| 388 | "Write action under workspace-write policy with never approvals for baseline no-prompt behavior.", |
| 389 | prompt: TOOL_CALL_WRITE_PROMPT, |
| 390 | turnDefaults: { |
| 391 | approvalPolicy: "never", |
| 392 | sandboxPolicy: workspaceWriteSandbox(), |
| 393 | }, |
| 394 | steps: [{ type: "turn", label: "write-fixture-file", prompt: TOOL_CALL_WRITE_PROMPT }], |
| 395 | }, |
| 396 | ], |
| 397 | }, |
| 398 | { |
| 399 | name: "tool_call_restricted_granular", |
| 400 | fileName: "tool_call_restricted_granular.ndjson", |
| 401 | description: |
| 402 | "Write a small fixture file with restricted read access and granular approval flags enabled.", |
| 403 | // gpt-5.6-terra uses apply_patch, which raises this approval; gpt-6 uses the shell. |
| 404 | model: "gpt-5.6-terra", |
| 405 | runs: [ |
| 406 | { |
| 407 | name: "restricted-granular", |
| 408 | description: |
| 409 | "Write action under restricted read access with granular approval flags enabled, intended to capture permission request flows when Codex escalates.", |
| 410 | prompt: TOOL_CALL_WRITE_PROMPT, |
| 411 | turnDefaults: { |
| 412 | approvalPolicy: granularApprovalPolicy(), |
| 413 | sandboxPolicy: readOnlyRestrictedSandbox(), |
| 414 | }, |
| 415 | steps: [ |
| 416 | { |
| 417 | type: "turn", |
| 418 | label: "write-fixture-file", |
| 419 | prompt: TOOL_CALL_WRITE_PROMPT, |
| 420 | }, |
| 421 | ], |
| 422 | }, |
| 423 | ], |
| 424 | }, |
| 425 | { |
| 426 | name: "subagent", |
| 427 | fileName: "subagent.ndjson", |
| 428 | description: "One root turn that asks Codex to spawn two collab agents.", |
| 429 | // gpt-5.6-luna still runs multi-agent v1, the only live source of collabAgentToolCall. |
| 430 | model: "gpt-5.6-luna", |
| 431 | runs: [ |
| 432 | { |
| 433 | name: "two-subagents", |
| 434 | description: "Root turn asks for two subagents reading different files.", |
| 435 | prompt: SUBAGENT_PROMPT, |
| 436 | // Without this, children read the recording user's own skills into the fixture. |
| 437 | threadConfig: { "skills.include_instructions": false }, |
| 438 | turnDefaults: { |
| 439 | approvalPolicy: "on-request", |
| 440 | sandboxPolicy: readOnlyFullAccessSandbox(), |
| 441 | }, |
| 442 | steps: [{ type: "turn", label: "spawn-two-subagents", prompt: SUBAGENT_PROMPT }], |
| 443 | }, |
| 444 | ], |
| 445 | }, |
| 446 | { |
| 447 | name: "subagent_v2", |
| 448 | fileName: "subagent_v2.ndjson", |
| 449 | description: |
| 450 | "One root turn on a multi-agent v2 model that spawns one native subagent and waits for it.", |
| 451 | runs: [ |
| 452 | { |
| 453 | name: "spawn-v2-subagent", |
| 454 | description: |
| 455 | "The default model runs multi-agent v2, so Codex emits subAgentActivity items.", |
| 456 | steps: [{ type: "turn", label: "spawn-v2-subagent", prompt: SUBAGENT_V2_PROMPT }], |
| 457 | }, |
| 458 | ], |
| 459 | }, |
| 460 | { |
| 461 | name: "subagent_v2_approval", |
| 462 | fileName: "subagent_v2_approval.ndjson", |
| 463 | description: |
| 464 | "One root turn in approval-required mode whose multi-agent v2 subagent runs a command that needs approval.", |
| 465 | runs: [ |
| 466 | { |
| 467 | name: "spawn-v2-subagent-needing-approval", |
| 468 | description: |
| 469 | "The child inherits the root's approval policy, so its write asks the client for approval on the child's native thread and turn.", |
| 470 | turnDefaults: { |
| 471 | approvalPolicy: "untrusted", |
| 472 | sandboxPolicy: { type: "readOnly" }, |
| 473 | }, |
| 474 | steps: [ |
| 475 | { |
| 476 | type: "turn", |
| 477 | label: "spawn-v2-subagent-needing-approval", |
| 478 | prompt: SUBAGENT_V2_APPROVAL_PROMPT, |
| 479 | }, |
| 480 | ], |
| 481 | }, |
| 482 | ], |
| 483 | }, |
| 484 | { |
| 485 | name: "subagent_v2_nested", |
| 486 | fileName: "subagent_v2_nested.ndjson", |
| 487 | description: |
| 488 | "One root turn on a multi-agent v2 model whose subagent spawns a subagent that spawns a leaf.", |
| 489 | runs: [ |
| 490 | { |
| 491 | name: "spawn-nested-v2-subagents", |
| 492 | description: "Multi-agent v2; depth 3 lets each child spawn again.", |
| 493 | threadConfig: { "agents.max_depth": 3 }, |
| 494 | steps: [ |
| 495 | { |
| 496 | type: "turn", |
| 497 | label: "spawn-nested-v2-subagents", |
| 498 | prompt: SUBAGENT_V2_NESTED_PROMPT, |
| 499 | }, |
| 500 | ], |
| 501 | }, |
| 502 | ], |
| 503 | }, |
| 504 | { |
| 505 | name: "subagent_v2_nested_approval", |
| 506 | fileName: "subagent_v2_nested_approval.ndjson", |
| 507 | description: |
| 508 | "One root turn in approval-required mode whose multi-agent v2 subagent spawns a subagent that runs a command that needs approval.", |
| 509 | runs: [ |
| 510 | { |
| 511 | name: "spawn-nested-v2-subagent-needing-approval", |
| 512 | description: |
| 513 | "Depth 2 lets the first child spawn once. The grandchild inherits the root's approval policy, so its write asks the client for approval on the grandchild's native thread and turn.", |
| 514 | threadConfig: { "agents.max_depth": 2 }, |
| 515 | turnDefaults: { |
| 516 | approvalPolicy: "untrusted", |
| 517 | sandboxPolicy: { type: "readOnly" }, |
| 518 | }, |
| 519 | steps: [ |
| 520 | { |
| 521 | type: "turn", |
| 522 | label: "spawn-nested-v2-subagent-needing-approval", |
| 523 | prompt: SUBAGENT_V2_NESTED_APPROVAL_PROMPT, |
| 524 | }, |
| 525 | ], |
| 526 | }, |
| 527 | ], |
| 528 | }, |
| 529 | { |
| 530 | name: "subagent_continue", |
| 531 | fileName: "subagent_continue.ndjson", |
| 532 | description: |
| 533 | "A root turn spawns one native Codex subagent, then a later root turn resumes and messages the same child thread.", |
| 534 | // gpt-5.6-luna still runs multi-agent v1, the only live source of collabAgentToolCall. |
| 535 | model: "gpt-5.6-luna", |
| 536 | runs: [ |
| 537 | { |
| 538 | name: "continued-subagent", |
| 539 | description: |
| 540 | "The second root turn asks Codex to continue the subagent created by the first root turn.", |
| 541 | steps: [ |
| 542 | { |
| 543 | type: "turn", |
| 544 | label: "spawn-subagent", |
| 545 | prompt: SUBAGENT_CONTINUE_PROMPT, |
| 546 | }, |
| 547 | { |
| 548 | type: "turn", |
| 549 | label: "continue-subagent", |
| 550 | prompt: SUBAGENT_CONTINUE_PARENT_PROMPT, |
| 551 | }, |
| 552 | ], |
| 553 | }, |
| 554 | ], |
| 555 | }, |
| 556 | { |
| 557 | name: "multi_turn", |
| 558 | fileName: "multi_turn.ndjson", |
| 559 | description: "One thread with two sequential user turns.", |
| 560 | runs: [ |
| 561 | { |
| 562 | name: "two-turns-same-thread", |
| 563 | description: "Second turn starts after the first root turn completes.", |
| 564 | steps: [ |
| 565 | { |
| 566 | type: "turn", |
| 567 | label: "first", |
| 568 | prompt: MULTI_TURN_FIRST_PROMPT, |
| 569 | }, |
| 570 | { |
| 571 | type: "turn", |
| 572 | label: "second", |
| 573 | prompt: MULTI_TURN_SECOND_PROMPT, |
| 574 | }, |
| 575 | ], |
| 576 | }, |
| 577 | ], |
| 578 | }, |
| 579 | { |
| 580 | name: "queued_turn", |
| 581 | fileName: "queued_turn.ndjson", |
| 582 | description: "A second user turn queued behind an active turn starts after it completes.", |
| 583 | runs: [ |
| 584 | { |
| 585 | name: "queued-second-turn", |
| 586 | description: |
| 587 | "The orchestrator holds the queued message until the first turn completes, so the provider sees two sequential turns.", |
| 588 | steps: [ |
| 589 | { type: "turn", label: "first", prompt: MULTI_TURN_FIRST_PROMPT }, |
| 590 | { type: "turn", label: "queued", prompt: MULTI_TURN_SECOND_PROMPT }, |
| 591 | ], |
| 592 | }, |
| 593 | ], |
| 594 | }, |
| 595 | { |
| 596 | name: "provider_thread_resume", |
| 597 | fileName: "provider_thread_resume.ndjson", |
| 598 | description: |
| 599 | "One provider-native thread is started, completed, then resumed by thread id in a fresh app-server session.", |
| 600 | runs: [ |
| 601 | { |
| 602 | name: "resume-provider-thread", |
| 603 | description: |
| 604 | "First turn completes, the app-server runtime is restarted, thread/resume loads the existing provider thread, then a second turn completes.", |
| 605 | steps: [ |
| 606 | { |
| 607 | type: "turn", |
| 608 | label: "first-before-resume", |
| 609 | prompt: PROVIDER_THREAD_RESUME_FIRST_PROMPT, |
| 610 | }, |
| 611 | { |
| 612 | type: "turn", |
| 613 | label: "second-after-resume", |
| 614 | prompt: PROVIDER_THREAD_RESUME_SECOND_PROMPT, |
| 615 | }, |
| 616 | ], |
| 617 | }, |
| 618 | ], |
| 619 | }, |
| 620 | { |
| 621 | name: "todo_list", |
| 622 | fileName: "todo_list.ndjson", |
| 623 | description: "One turn that asks Codex to emit progress through update_plan.", |
| 624 | runs: [ |
| 625 | { |
| 626 | name: "todo-list", |
| 627 | description: "Default-mode turn that should surface turn/plan/updated notifications.", |
| 628 | prompt: TODO_LIST_PROMPT, |
| 629 | turnDefaults: { |
| 630 | approvalPolicy: "never", |
| 631 | sandboxPolicy: readOnlyFullAccessSandbox(), |
| 632 | }, |
| 633 | steps: [{ type: "turn", label: "todo-list", prompt: TODO_LIST_PROMPT }], |
| 634 | }, |
| 635 | ], |
| 636 | }, |
| 637 | { |
| 638 | name: "web_search", |
| 639 | fileName: "web_search.ndjson", |
| 640 | description: |
| 641 | "One turn with a native Codex webSearch item that should become normalized progress.", |
| 642 | runs: [ |
| 643 | { |
| 644 | name: "web-search", |
| 645 | description: "Default-mode turn that should surface a webSearch item.", |
| 646 | steps: [{ type: "turn", label: "web-search", prompt: WEB_SEARCH_PROMPT }], |
| 647 | }, |
| 648 | ], |
| 649 | }, |
| 650 | { |
| 651 | name: "plan_questions", |
| 652 | fileName: "plan_questions.ndjson", |
| 653 | description: "One plan-mode turn that asks a structured clarifying question.", |
| 654 | runs: [ |
| 655 | { |
| 656 | name: "plan-questions", |
| 657 | description: "Plan-mode turn intended to surface item/tool/requestUserInput.", |
| 658 | prompt: PLAN_QUESTIONS_PROMPT, |
| 659 | interactionMode: "plan", |
| 660 | turnDefaults: { |
| 661 | approvalPolicy: "never", |
| 662 | sandboxPolicy: readOnlyFullAccessSandbox(), |
| 663 | }, |
| 664 | steps: [{ type: "turn", label: "plan-questions", prompt: PLAN_QUESTIONS_PROMPT }], |
| 665 | }, |
| 666 | ], |
| 667 | }, |
| 668 | { |
| 669 | name: "proposed_plan", |
| 670 | fileName: "proposed_plan.ndjson", |
| 671 | description: "One plan-mode turn that emits a proposed plan document.", |
| 672 | runs: [ |
| 673 | { |
| 674 | name: "proposed-plan", |
| 675 | description: |
| 676 | "Plan-mode turn intended to surface item/plan/delta and completed plan item.", |
| 677 | prompt: PROPOSED_PLAN_PROMPT, |
| 678 | interactionMode: "plan", |
| 679 | turnDefaults: { |
| 680 | approvalPolicy: "never", |
| 681 | sandboxPolicy: readOnlyFullAccessSandbox(), |
| 682 | }, |
| 683 | steps: [{ type: "turn", label: "proposed-plan", prompt: PROPOSED_PLAN_PROMPT }], |
| 684 | }, |
| 685 | ], |
| 686 | }, |
| 687 | { |
| 688 | name: "message_steering", |
| 689 | fileName: "message_steering.ndjson", |
| 690 | description: "One active turn receives an immediate turn/steer request.", |
| 691 | runs: [ |
| 692 | { |
| 693 | name: "immediate-steer", |
| 694 | description: "Start a turn, then immediately steer the active root turn.", |
| 695 | steps: [ |
| 696 | { |
| 697 | type: "steeredTurn", |
| 698 | label: "steered", |
| 699 | prompt: MESSAGE_STEERING_INITIAL_PROMPT, |
| 700 | steer: MESSAGE_STEERING_STEER_PROMPT, |
| 701 | }, |
| 702 | ], |
| 703 | }, |
| 704 | ], |
| 705 | }, |
| 706 | { |
| 707 | name: "turn_interrupt", |
| 708 | fileName: "turn_interrupt.ndjson", |
| 709 | description: "One active turn is interrupted before it finishes naturally.", |
| 710 | runs: [ |
| 711 | { |
| 712 | name: "interrupt-active-turn", |
| 713 | description: "Start a long-running turn, then send turn/interrupt.", |
| 714 | prompt: TURN_INTERRUPT_PROMPT, |
| 715 | turnDefaults: { |
| 716 | approvalPolicy: "never", |
| 717 | sandboxPolicy: workspaceWriteSandbox(), |
| 718 | }, |
| 719 | steps: [ |
| 720 | { |
| 721 | type: "interruptedTurn", |
| 722 | label: "interrupt-active-turn", |
| 723 | prompt: TURN_INTERRUPT_PROMPT, |
| 724 | interruptAfterMs: 1_500, |
| 725 | }, |
| 726 | ], |
| 727 | }, |
| 728 | ], |
| 729 | }, |
| 730 | { |
| 731 | name: "turn_interrupt_mid_tool", |
| 732 | fileName: "turn_interrupt_mid_tool.ndjson", |
| 733 | description: |
| 734 | "One active turn is interrupted after Codex has already executed a local command.", |
| 735 | runs: [ |
| 736 | { |
| 737 | name: "interrupt-after-command-execution", |
| 738 | description: "Start a turn, wait for command execution, then send turn/interrupt.", |
| 739 | prompt: TURN_INTERRUPT_MID_TOOL_PROMPT, |
| 740 | turnDefaults: { |
| 741 | approvalPolicy: "never", |
| 742 | sandboxPolicy: workspaceWriteSandbox(), |
| 743 | }, |
| 744 | steps: [ |
| 745 | { |
| 746 | type: "interruptedTurn", |
| 747 | label: "interrupt-after-command-execution", |
| 748 | prompt: TURN_INTERRUPT_MID_TOOL_PROMPT, |
| 749 | interruptAfterCommandExecutionStarted: true, |
| 750 | }, |
| 751 | ], |
| 752 | }, |
| 753 | ], |
| 754 | }, |
| 755 | { |
| 756 | name: "command_output_streaming", |
| 757 | fileName: "command_output_streaming.ndjson", |
| 758 | description: "One command prints over several seconds, so Codex streams its output deltas.", |
| 759 | runs: [ |
| 760 | { |
| 761 | name: "command-output-streaming", |
| 762 | description: "Run a foreground command that prints a line a second.", |
| 763 | prompt: COMMAND_OUTPUT_STREAMING_PROMPT, |
| 764 | turnDefaults: { |
| 765 | approvalPolicy: "never", |
| 766 | sandboxPolicy: workspaceWriteSandbox(), |
| 767 | }, |
| 768 | steps: [ |
| 769 | { |
| 770 | type: "turn", |
| 771 | label: "command-output-streaming", |
| 772 | prompt: COMMAND_OUTPUT_STREAMING_PROMPT, |
| 773 | }, |
| 774 | ], |
| 775 | }, |
| 776 | ], |
| 777 | }, |
| 778 | { |
| 779 | name: "thread_rollback", |
| 780 | fileName: "thread_rollback.ndjson", |
| 781 | description: |
| 782 | "One thread completes two turns, rolls back the most recent turn, then starts another turn.", |
| 783 | runs: [ |
| 784 | { |
| 785 | name: "rollback-one-turn", |
| 786 | description: |
| 787 | "Two completed turns, thread/revert before the second turn, then a post-rollback turn.", |
| 788 | steps: [ |
| 789 | { |
| 790 | type: "turn", |
| 791 | label: "first-before-rollback", |
| 792 | prompt: THREAD_ROLLBACK_FIRST_PROMPT, |
| 793 | }, |
| 794 | { |
| 795 | type: "turn", |
| 796 | label: "second-before-rollback", |
| 797 | prompt: THREAD_ROLLBACK_SECOND_PROMPT, |
| 798 | }, |
| 799 | { |
| 800 | type: "rollback", |
| 801 | label: "rollback-latest-turn", |
| 802 | numTurns: 1, |
| 803 | }, |
| 804 | { |
| 805 | type: "turn", |
| 806 | label: "post-rollback", |
| 807 | prompt: THREAD_ROLLBACK_AFTER_PROMPT, |
| 808 | }, |
| 809 | ], |
| 810 | }, |
| 811 | ], |
| 812 | }, |
| 813 | { |
| 814 | name: "thread_rollback_after_restart", |
| 815 | fileName: "thread_rollback_after_restart.ndjson", |
| 816 | description: |
| 817 | "One thread completes two turns, the app-server restarts, then the thread rolls back its latest turn and starts another turn.", |
| 818 | runs: [ |
| 819 | { |
| 820 | name: "rollback-after-restart", |
| 821 | description: |
| 822 | "Two completed turns, a fresh app-server that has not loaded the thread, thread/revert before the second turn, then a post-rollback turn.", |
| 823 | steps: [ |
| 824 | { |
| 825 | type: "turn", |
| 826 | label: "first-before-rollback", |
| 827 | prompt: THREAD_ROLLBACK_FIRST_PROMPT, |
| 828 | }, |
| 829 | { |
| 830 | type: "turn", |
| 831 | label: "second-before-rollback", |
| 832 | prompt: THREAD_ROLLBACK_SECOND_PROMPT, |
| 833 | }, |
| 834 | { |
| 835 | type: "rollback", |
| 836 | label: "rollback-latest-turn", |
| 837 | numTurns: 1, |
| 838 | }, |
| 839 | { |
| 840 | type: "turn", |
| 841 | label: "post-rollback", |
| 842 | prompt: THREAD_ROLLBACK_AFTER_PROMPT, |
| 843 | }, |
| 844 | ], |
| 845 | }, |
| 846 | ], |
| 847 | }, |
| 848 | { |
| 849 | name: "thread_rollback_to_stopped_turn", |
| 850 | fileName: "thread_rollback_to_stopped_turn.ndjson", |
| 851 | description: |
| 852 | "One completed turn, one turn stopped mid-tool, one completed turn, thread/revert back to the stopped turn, then a post-rollback turn.", |
| 853 | runs: [ |
| 854 | { |
| 855 | name: "rollback-to-stopped-turn", |
| 856 | description: |
| 857 | "Edit the message after a stopped turn: revert the turn after it and keep the stopped turn.", |
| 858 | turnDefaults: { |
| 859 | approvalPolicy: "never", |
| 860 | sandboxPolicy: workspaceWriteSandbox(), |
| 861 | }, |
| 862 | steps: [ |
| 863 | { |
| 864 | type: "turn", |
| 865 | label: "first-before-rollback", |
| 866 | prompt: THREAD_ROLLBACK_FIRST_PROMPT, |
| 867 | }, |
| 868 | { |
| 869 | type: "interruptedTurn", |
| 870 | label: "stopped-mid-tool", |
| 871 | prompt: TURN_INTERRUPT_MID_TOOL_PROMPT, |
| 872 | interruptAfterCommandExecutionStarted: true, |
| 873 | }, |
| 874 | { |
| 875 | type: "turn", |
| 876 | label: "second-before-rollback", |
| 877 | prompt: THREAD_ROLLBACK_SECOND_PROMPT, |
| 878 | }, |
| 879 | { |
| 880 | type: "rollback", |
| 881 | label: "rollback-to-stopped-turn", |
| 882 | numTurns: 1, |
| 883 | }, |
| 884 | { |
| 885 | type: "turn", |
| 886 | label: "post-rollback", |
| 887 | prompt: THREAD_ROLLBACK_AFTER_PROMPT, |
| 888 | }, |
| 889 | ], |
| 890 | }, |
| 891 | ], |
| 892 | }, |
| 893 | { |
| 894 | name: "thread_merge_back_siblings", |
| 895 | fileName: "thread_merge_back_siblings.ndjson", |
| 896 | description: |
| 897 | "Two sibling fork deltas are merged sequentially into one source provider thread and recalled together.", |
| 898 | // gpt-6-luna often recalls acknowledgements; gpt-6-sol recalls the markers. |
| 899 | model: "gpt-6-sol", |
| 900 | runs: [ |
| 901 | { |
| 902 | name: "merge-native-sibling-forks", |
| 903 | description: |
| 904 | "Proves repeated merge-back handoffs accumulate on the source while native sibling forks remain independent.", |
| 905 | steps: [ |
| 906 | { |
| 907 | type: "turn", |
| 908 | label: "source", |
| 909 | prompt: THREAD_MERGE_BACK_SIBLINGS_SOURCE_PROMPT, |
| 910 | thread: "source", |
| 911 | }, |
| 912 | { |
| 913 | type: "fork", |
| 914 | label: "fork-first", |
| 915 | from: "source", |
| 916 | as: "first", |
| 917 | }, |
| 918 | { |
| 919 | type: "turn", |
| 920 | label: "first-fork-delta", |
| 921 | prompt: THREAD_MERGE_BACK_SIBLINGS_FIRST_FORK_PROMPT, |
| 922 | thread: "first", |
| 923 | }, |
| 924 | { |
| 925 | type: "fork", |
| 926 | label: "fork-second", |
| 927 | from: "source", |
| 928 | as: "second", |
| 929 | }, |
| 930 | { |
| 931 | type: "turn", |
| 932 | label: "second-fork-delta", |
| 933 | prompt: THREAD_MERGE_BACK_SIBLINGS_SECOND_FORK_PROMPT, |
| 934 | thread: "second", |
| 935 | }, |
| 936 | { |
| 937 | type: "turn", |
| 938 | label: "merge-first", |
| 939 | prompt: THREAD_MERGE_BACK_SIBLINGS_FIRST_HANDOFF_PROMPT, |
| 940 | thread: "source", |
| 941 | }, |
| 942 | { |
| 943 | type: "turn", |
| 944 | label: "merge-second", |
| 945 | prompt: THREAD_MERGE_BACK_SIBLINGS_SECOND_HANDOFF_PROMPT, |
| 946 | thread: "source", |
| 947 | }, |
| 948 | { |
| 949 | type: "turn", |
| 950 | label: "source-recall", |
| 951 | prompt: THREAD_MERGE_BACK_SIBLINGS_RECALL_PROMPT, |
| 952 | thread: "source", |
| 953 | }, |
| 954 | ], |
| 955 | }, |
| 956 | ], |
| 957 | }, |
| 958 | ]; |
| 959 | } |
| 960 | |
| 961 | function recorderThreadConfigMetadata(scenario: ReplayScenario) { |
| 962 | const keys = [...new Set(scenario.runs.flatMap((run) => Object.keys(run.threadConfig ?? {})))]; |
| 963 | return keys.length === 0 ? {} : { recorderThreadConfigKeys: keys }; |
| 964 | } |
| 965 | |
| 966 | const makeRecorder = Effect.fn("record-codex-app-server-replay-fixture.makeRecorder")(function* ({ |
| 967 | outPath, |
| 968 | scenario, |
| 969 | checkout, |
| 970 | }: { |
| 971 | readonly outPath: string; |
| 972 | readonly scenario: ReplayScenario; |
| 973 | readonly checkout: string; |
| 974 | }): Effect.fn.Return<Recorder, PlatformError.PlatformError, FileSystem.FileSystem> { |
| 975 | const fs = yield* FileSystem.FileSystem; |
| 976 | let version = "unknown"; |
| 977 | const records: Array<Record<string, unknown>> = []; |
| 978 | const setVersion = (nextVersion: string) => |
| 979 | Effect.sync(() => { |
| 980 | version = nextVersion; |
| 981 | }); |
| 982 | const writeRecord = (record: Record<string, unknown>) => |
| 983 | Effect.sync(() => { |
| 984 | records.push(record); |
| 985 | }); |
| 986 | const flush = () => { |
| 987 | const outputRecords = codexReplayRecordingOutputRecords(records, { |
| 988 | workspace: process.cwd(), |
| 989 | machine: { |
| 990 | home: NodeOS.homedir(), |
| 991 | hostname: NodeOS.hostname(), |
| 992 | checkout, |
| 993 | }, |
| 994 | }); |
| 995 | return fs.writeFileString( |
| 996 | outPath, |
| 997 | `${[ |
| 998 | { |
| 999 | type: "transcript_start", |
| 1000 | provider: "codex", |
| 1001 | protocol: "codex.app-server", |
| 1002 | version, |
| 1003 | scenario: scenario.name, |
| 1004 | metadata: { |
| 1005 | source: "record-codex-app-server-replay-fixture", |
| 1006 | fileName: scenario.fileName, |
| 1007 | description: scenario.description, |
| 1008 | model: scenarioModel(scenario), |
| 1009 | ...recorderThreadConfigMetadata(scenario), |
| 1010 | }, |
| 1011 | }, |
| 1012 | ...outputRecords, |
| 1013 | ] |
| 1014 | .map((record) => JSON.stringify(record)) |
| 1015 | .join("\n")}\n`, |
| 1016 | ); |
| 1017 | }; |
| 1018 | |
| 1019 | return { path: outPath, setVersion, writeRecord, flush }; |
| 1020 | }); |
| 1021 | |
| 1022 | function layerCodex({ recorder }: { readonly recorder: Recorder }) { |
| 1023 | const clientRequestMethodById = new Map<string, string>(); |
| 1024 | const serverRequestMethodById = new Map<string, string>(); |
| 1025 | const clientOptions: CodexClient.CodexAppServerClientOptions = { |
| 1026 | logIncoming: true, |
| 1027 | logOutgoing: true, |
| 1028 | logger: Effect.fn("logger")(function (event) { |
| 1029 | if (event.stage === "raw") { |
| 1030 | return Effect.void; |
| 1031 | } |
| 1032 | |
| 1033 | const id = protocolId(event.payload); |
| 1034 | const idKey = id === undefined ? undefined : String(id); |
| 1035 | const method = protocolMethod(event.payload); |
| 1036 | const messageKind = classifyJsonRpcPayload(event.payload); |
| 1037 | let correlatedRequestMethod: string | undefined; |
| 1038 | |
| 1039 | if (messageKind === "request" && idKey && method) { |
| 1040 | if (event.direction === "outgoing") { |
| 1041 | clientRequestMethodById.set(idKey, method); |
| 1042 | } else { |
| 1043 | serverRequestMethodById.set(idKey, method); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | if (messageKind === "response" || messageKind === "error_response") { |
| 1048 | if (event.direction === "incoming" && idKey) { |
| 1049 | correlatedRequestMethod = clientRequestMethodById.get(idKey); |
| 1050 | clientRequestMethodById.delete(idKey); |
| 1051 | } |
| 1052 | if (event.direction === "outgoing" && idKey) { |
| 1053 | correlatedRequestMethod = serverRequestMethodById.get(idKey); |
| 1054 | serverRequestMethodById.delete(idKey); |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | const version = inferCodexVersion(event.payload); |
| 1059 | const updateVersion = version ? recorder.setVersion(version) : Effect.void; |
| 1060 | |
| 1061 | const label = method ?? correlatedRequestMethod; |
| 1062 | const record = |
| 1063 | event.direction === "outgoing" |
| 1064 | ? { |
| 1065 | type: "expect_outbound", |
| 1066 | ...(label ? { label } : {}), |
| 1067 | frame: event.payload, |
| 1068 | } |
| 1069 | : { |
| 1070 | type: "emit_inbound", |
| 1071 | ...(label ? { label } : {}), |
| 1072 | frame: event.payload, |
| 1073 | }; |
| 1074 | |
| 1075 | return Effect.gen(function* () { |
| 1076 | yield* updateVersion; |
| 1077 | yield* recorder.writeRecord(record); |
| 1078 | }).pipe(Effect.ignore); |
| 1079 | }), |
| 1080 | }; |
| 1081 | |
| 1082 | return Layer.effect( |
| 1083 | CodexClient.CodexAppServerClient, |
| 1084 | Effect.gen(function* () { |
| 1085 | // Inheriting the vp shim's VP_TOOL_RECURSION makes Codex's shell `node` calls fail. |
| 1086 | const { VP_TOOL_RECURSION: _vpToolRecursion, ...environment } = |
| 1087 | yield* HostProcess.Environment; |
| 1088 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 1089 | const commandName = environment.T3_CODEX_BIN ?? environment.CODEX_BIN ?? "codex"; |
| 1090 | const spawnCommand = yield* resolveSpawnCommand(commandName, ["app-server"], { |
| 1091 | env: environment, |
| 1092 | }); |
| 1093 | const handle = yield* spawner.spawn( |
| 1094 | ChildProcess.make(spawnCommand.command, spawnCommand.args, { |
| 1095 | cwd: process.cwd(), |
| 1096 | env: environment, |
| 1097 | shell: spawnCommand.shell, |
| 1098 | }), |
| 1099 | ); |
| 1100 | const context = yield* Layer.build(CodexClient.layerChildProcess(handle, clientOptions)); |
| 1101 | return yield* Effect.service(CodexClient.CodexAppServerClient).pipe(Effect.provide(context)); |
| 1102 | }), |
| 1103 | ); |
| 1104 | } |
| 1105 | |
| 1106 | function installReplayHandlers({ |
| 1107 | client, |
| 1108 | startTurn, |
| 1109 | completeTurn, |
| 1110 | startCommandExecution, |
| 1111 | beforeApprovalResponse, |
| 1112 | runningCommandProcessIds, |
| 1113 | }: { |
| 1114 | readonly client: CodexClient.CodexAppServerClient["Service"]; |
| 1115 | readonly startTurn: (turnId: string) => Effect.Effect<void>; |
| 1116 | readonly completeTurn: (turnId: string) => Effect.Effect<void>; |
| 1117 | readonly startCommandExecution: (turnId: string) => Effect.Effect<void>; |
| 1118 | readonly beforeApprovalResponse: () => Effect.Effect<void>; |
| 1119 | readonly runningCommandProcessIds: Map<string, Map<string, string>>; |
| 1120 | }) { |
| 1121 | return Effect.all( |
| 1122 | [ |
| 1123 | client.handleServerRequest("item/tool/requestUserInput", (payload) => |
| 1124 | Effect.succeed({ |
| 1125 | answers: Object.fromEntries( |
| 1126 | payload.questions.map((question) => [ |
| 1127 | question.id, |
| 1128 | { |
| 1129 | answers: |
| 1130 | question.options && question.options.length > 0 |
| 1131 | ? [question.options[0]!.label] |
| 1132 | : ["ok"], |
| 1133 | }, |
| 1134 | ]), |
| 1135 | ), |
| 1136 | }), |
| 1137 | ), |
| 1138 | client.handleServerRequest("item/commandExecution/requestApproval", () => |
| 1139 | beforeApprovalResponse().pipe(Effect.as({ decision: "accept" })), |
| 1140 | ), |
| 1141 | client.handleServerRequest("item/fileChange/requestApproval", () => |
| 1142 | beforeApprovalResponse().pipe(Effect.as({ decision: "accept" })), |
| 1143 | ), |
| 1144 | client.handleServerRequest("item/permissions/requestApproval", (payload) => |
| 1145 | beforeApprovalResponse().pipe( |
| 1146 | Effect.as({ |
| 1147 | permissions: payload.permissions, |
| 1148 | scope: "turn" as const, |
| 1149 | }), |
| 1150 | ), |
| 1151 | ), |
| 1152 | client.handleServerRequest("mcpServer/elicitation/request", () => |
| 1153 | Effect.succeed({ action: "accept" }), |
| 1154 | ), |
| 1155 | client.handleServerRequest("item/tool/call", (payload) => |
| 1156 | Effect.succeed({ |
| 1157 | contentItems: [ |
| 1158 | { |
| 1159 | text: `Replay dynamic tool handler did not execute external tool: ${payload.tool}`, |
| 1160 | type: "inputText" as const, |
| 1161 | }, |
| 1162 | ], |
| 1163 | success: false, |
| 1164 | }), |
| 1165 | ), |
| 1166 | client.handleServerRequest("applyPatchApproval", () => |
| 1167 | Effect.succeed({ decision: "approved" }), |
| 1168 | ), |
| 1169 | client.handleServerRequest("execCommandApproval", () => |
| 1170 | Effect.succeed({ decision: "approved" }), |
| 1171 | ), |
| 1172 | client.handleUnknownServerRequest((method) => |
| 1173 | Effect.die(new Error(`Unhandled Codex app-server request in replay recorder: ${method}`)), |
| 1174 | ), |
| 1175 | client.handleServerNotification("turn/started", (payload) => |
| 1176 | startTurn(payload.turn.id).pipe(Effect.ignore), |
| 1177 | ), |
| 1178 | client.handleServerNotification("turn/completed", (payload) => |
| 1179 | completeTurn(payload.turn.id).pipe(Effect.ignore), |
| 1180 | ), |
| 1181 | client.handleServerNotification("item/completed", (payload) => { |
| 1182 | if (payload.item.type !== "commandExecution") return Effect.void; |
| 1183 | runningCommandProcessIds.get(payload.turnId)?.delete(payload.item.id); |
| 1184 | return startCommandExecution(payload.turnId).pipe(Effect.ignore); |
| 1185 | }), |
| 1186 | client.handleServerNotification("item/started", (payload) => { |
| 1187 | if (payload.item.type !== "commandExecution") return Effect.void; |
| 1188 | const processId = payload.item.processId; |
| 1189 | if (payload.item.status === "inProgress" && typeof processId === "string") { |
| 1190 | const running = runningCommandProcessIds.get(payload.turnId) ?? new Map(); |
| 1191 | running.set(payload.item.id, processId); |
| 1192 | runningCommandProcessIds.set(payload.turnId, running); |
| 1193 | } |
| 1194 | return startCommandExecution(payload.turnId).pipe(Effect.ignore); |
| 1195 | }), |
| 1196 | ], |
| 1197 | { discard: true }, |
| 1198 | ); |
| 1199 | } |
| 1200 | |
| 1201 | const runReplaySession = Effect.fn("record-codex-app-server-replay-fixture.runReplaySession")( |
| 1202 | function* ({ |
| 1203 | scenario, |
| 1204 | run, |
| 1205 | recorder, |
| 1206 | }: { |
| 1207 | readonly scenario: ReplayScenario; |
| 1208 | readonly run: ReplayRun; |
| 1209 | readonly recorder: Recorder; |
| 1210 | }) { |
| 1211 | const startedTurns = yield* makeReplayRecorderDeferredRegistry(); |
| 1212 | const completedTurns = yield* makeReplayRecorderDeferredRegistry(); |
| 1213 | const startedCommandExecutions = yield* makeReplayRecorderDeferredRegistry(); |
| 1214 | let approvalGate: Deferred.Deferred<void> | undefined; |
| 1215 | const getStarted = startedTurns.getOrCreate; |
| 1216 | const getCompletion = completedTurns.getOrCreate; |
| 1217 | const getCommandExecutionStarted = startedCommandExecutions.getOrCreate; |
| 1218 | const startTurn = startedTurns.succeed; |
| 1219 | const completeTurn = completedTurns.succeed; |
| 1220 | const startCommandExecution = startedCommandExecutions.succeed; |
| 1221 | const beforeApprovalResponse = () => |
| 1222 | approvalGate ? Deferred.await(approvalGate) : Effect.void; |
| 1223 | const model = scenarioModel(scenario); |
| 1224 | const threadRuntimeParams = { cwd: process.cwd(), model, config: CODEX_THREAD_CONFIG }; |
| 1225 | const lastTurnIdByThread = new Map<string, string>(); |
| 1226 | const runningCommandProcessIds = new Map<string, Map<string, string>>(); |
| 1227 | |
| 1228 | const initializeClient = Effect.gen(function* () { |
| 1229 | const client = yield* CodexClient.CodexAppServerClient; |
| 1230 | |
| 1231 | yield* installReplayHandlers({ |
| 1232 | client, |
| 1233 | startTurn, |
| 1234 | completeTurn, |
| 1235 | startCommandExecution, |
| 1236 | beforeApprovalResponse, |
| 1237 | runningCommandProcessIds, |
| 1238 | }); |
| 1239 | |
| 1240 | yield* client.request("initialize", { |
| 1241 | clientInfo: buildCodexInitializeParams().clientInfo, |
| 1242 | capabilities: CODEX_CLIENT_CAPABILITIES, |
| 1243 | }); |
| 1244 | |
| 1245 | yield* client.notify("initialized", undefined); |
| 1246 | |
| 1247 | return client; |
| 1248 | }); |
| 1249 | |
| 1250 | const runTurnStep = Effect.fn("record-codex-app-server-replay-fixture.runTurnStep")(function* ( |
| 1251 | client: CodexClient.CodexAppServerClient["Service"], |
| 1252 | threadId: string, |
| 1253 | step: TurnReplayStep, |
| 1254 | ) { |
| 1255 | const turnParams: TurnStartParams = { |
| 1256 | approvalPolicy: "never", |
| 1257 | sandboxPolicy: { type: "dangerFullAccess" }, |
| 1258 | cwd: process.cwd(), |
| 1259 | model, |
| 1260 | summary: "detailed", |
| 1261 | approvalsReviewer: "user", |
| 1262 | ...(run.interactionMode === "plan" |
| 1263 | ? { collaborationMode: planCollaborationMode(model) } |
| 1264 | : {}), |
| 1265 | ...run.turnDefaults, |
| 1266 | ...step.turnOverrides, |
| 1267 | input: turnInput(step.prompt), |
| 1268 | threadId, |
| 1269 | }; |
| 1270 | |
| 1271 | if (step.type === "steeredTurn") { |
| 1272 | approvalGate = yield* Deferred.make<void>(); |
| 1273 | } |
| 1274 | |
| 1275 | const turn = yield* client.request("turn/start", turnParams); |
| 1276 | const turnId = getTurnId(turn); |
| 1277 | const started = yield* getStarted(turnId); |
| 1278 | yield* getCompletion(turnId); |
| 1279 | |
| 1280 | if (step.type === "steeredTurn") { |
| 1281 | yield* Deferred.await(started); |
| 1282 | yield* client.request("turn/steer", { |
| 1283 | expectedTurnId: turnId, |
| 1284 | input: turnInput(step.steer), |
| 1285 | threadId, |
| 1286 | }); |
| 1287 | if (approvalGate) { |
| 1288 | yield* Deferred.succeed(approvalGate, void 0); |
| 1289 | approvalGate = undefined; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | if (step.type === "interruptedTurn") { |
| 1294 | if (step.interruptAfterCommandExecutionStarted === true) { |
| 1295 | const commandExecutionStarted = yield* getCommandExecutionStarted(turnId); |
| 1296 | yield* Deferred.await(commandExecutionStarted); |
| 1297 | } else { |
| 1298 | yield* Effect.sleep(`${step.interruptAfterMs ?? 1_500} millis`); |
| 1299 | } |
| 1300 | yield* client.request("turn/interrupt", { |
| 1301 | threadId, |
| 1302 | turnId, |
| 1303 | }); |
| 1304 | for (const processId of runningCommandProcessIds.get(turnId)?.values() ?? []) { |
| 1305 | yield* client.raw.request("thread/backgroundTerminals/terminate", { |
| 1306 | threadId, |
| 1307 | processId, |
| 1308 | }); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | const completed = yield* getCompletion(turnId); |
| 1313 | yield* Deferred.await(completed); |
| 1314 | lastTurnIdByThread.set(threadId, turnId); |
| 1315 | }); |
| 1316 | |
| 1317 | if (scenario.name === "provider_thread_resume") { |
| 1318 | const firstStep = run.steps[0]; |
| 1319 | const secondStep = run.steps[1]; |
| 1320 | if ( |
| 1321 | !firstStep || |
| 1322 | !secondStep || |
| 1323 | firstStep.type === "rollback" || |
| 1324 | firstStep.type === "fork" || |
| 1325 | secondStep.type === "rollback" || |
| 1326 | secondStep.type === "fork" |
| 1327 | ) { |
| 1328 | throw new Error("provider_thread_resume replay recording requires two turn steps."); |
| 1329 | } |
| 1330 | |
| 1331 | const firstThread = yield* Effect.gen(function* () { |
| 1332 | const client = yield* initializeClient; |
| 1333 | const thread = yield* client.request("thread/start", threadRuntimeParams); |
| 1334 | yield* runTurnStep(client, thread.thread.id, firstStep); |
| 1335 | return thread; |
| 1336 | }).pipe( |
| 1337 | Effect.provide( |
| 1338 | layerCodex({ |
| 1339 | recorder, |
| 1340 | }), |
| 1341 | ), |
| 1342 | ); |
| 1343 | |
| 1344 | yield* recorder.writeRecord({ |
| 1345 | type: "runtime_exit", |
| 1346 | status: "success", |
| 1347 | }); |
| 1348 | |
| 1349 | yield* Effect.gen(function* () { |
| 1350 | const client = yield* initializeClient; |
| 1351 | const thread = yield* client.request("thread/resume", { |
| 1352 | threadId: firstThread.thread.id, |
| 1353 | excludeTurns: true, |
| 1354 | ...threadRuntimeParams, |
| 1355 | }); |
| 1356 | yield* runTurnStep(client, thread.thread.id, secondStep); |
| 1357 | }).pipe( |
| 1358 | Effect.provide( |
| 1359 | layerCodex({ |
| 1360 | recorder, |
| 1361 | }), |
| 1362 | ), |
| 1363 | ); |
| 1364 | return; |
| 1365 | } |
| 1366 | |
| 1367 | if (scenario.name === "thread_rollback_after_restart") { |
| 1368 | const [first, second, rollback, after] = run.steps; |
| 1369 | if ( |
| 1370 | first === undefined || |
| 1371 | second === undefined || |
| 1372 | rollback?.type !== "rollback" || |
| 1373 | after === undefined || |
| 1374 | first.type === "rollback" || |
| 1375 | first.type === "fork" || |
| 1376 | second.type === "rollback" || |
| 1377 | second.type === "fork" || |
| 1378 | after.type === "rollback" || |
| 1379 | after.type === "fork" |
| 1380 | ) { |
| 1381 | throw new Error( |
| 1382 | "thread_rollback_after_restart replay recording requires turn, turn, rollback, turn.", |
| 1383 | ); |
| 1384 | } |
| 1385 | |
| 1386 | const threadId = yield* Effect.gen(function* () { |
| 1387 | const client = yield* initializeClient; |
| 1388 | const thread = yield* client.request("thread/start", threadRuntimeParams); |
| 1389 | yield* runTurnStep(client, thread.thread.id, first); |
| 1390 | yield* runTurnStep(client, thread.thread.id, second); |
| 1391 | return thread.thread.id; |
| 1392 | }).pipe(Effect.provide(layerCodex({ recorder }))); |
| 1393 | |
| 1394 | yield* recorder.writeRecord({ |
| 1395 | type: "runtime_exit", |
| 1396 | status: "success", |
| 1397 | }); |
| 1398 | |
| 1399 | yield* Effect.gen(function* () { |
| 1400 | const client = yield* initializeClient; |
| 1401 | const resumeParams = { threadId, excludeTurns: true, ...threadRuntimeParams }; |
| 1402 | // Fresh app-servers report notLoaded; resume before thread/revert and again on the next turn. |
| 1403 | yield* client.request("thread/read", { threadId, includeTurns: false }); |
| 1404 | yield* client.request("thread/resume", resumeParams); |
| 1405 | yield* revertCodexThread(client, threadId, rollback.numTurns); |
| 1406 | yield* client.request("thread/resume", resumeParams); |
| 1407 | yield* runTurnStep(client, threadId, after); |
| 1408 | }).pipe(Effect.provide(layerCodex({ recorder }))); |
| 1409 | return; |
| 1410 | } |
| 1411 | |
| 1412 | yield* Effect.gen(function* () { |
| 1413 | const client = yield* initializeClient; |
| 1414 | const thread = yield* client.request("thread/start", { |
| 1415 | ...threadRuntimeParams, |
| 1416 | config: { ...threadRuntimeParams.config, ...run.threadConfig }, |
| 1417 | }); |
| 1418 | let activeThreadId = thread.thread.id; |
| 1419 | const threadIds = new Map<string, string>([["source", thread.thread.id]]); |
| 1420 | |
| 1421 | for (const [stepIndex, step] of run.steps.entries()) { |
| 1422 | if (step.type === "rollback") { |
| 1423 | yield* client.request("thread/read", { threadId: activeThreadId, includeTurns: false }); |
| 1424 | yield* revertCodexThread(client, activeThreadId, step.numTurns); |
| 1425 | continue; |
| 1426 | } |
| 1427 | if (step.type === "fork") { |
| 1428 | const sourceThreadId = |
| 1429 | step.from === undefined ? activeThreadId : threadIds.get(step.from); |
| 1430 | if (sourceThreadId === undefined) { |
| 1431 | throw new Error(`Unknown replay thread alias '${step.from}'.`); |
| 1432 | } |
| 1433 | const lastTurnId = lastTurnIdByThread.get(sourceThreadId); |
| 1434 | const forked = yield* client.request("thread/fork", { |
| 1435 | threadId: sourceThreadId, |
| 1436 | ...(lastTurnId === undefined ? {} : { lastTurnId }), |
| 1437 | ...threadRuntimeParams, |
| 1438 | }); |
| 1439 | activeThreadId = forked.thread.id; |
| 1440 | if (step.as !== undefined) { |
| 1441 | threadIds.set(step.as, forked.thread.id); |
| 1442 | } |
| 1443 | continue; |
| 1444 | } |
| 1445 | |
| 1446 | const threadAlias = "thread" in step ? step.thread : undefined; |
| 1447 | const turnThreadId = |
| 1448 | threadAlias === undefined ? activeThreadId : threadIds.get(threadAlias); |
| 1449 | if (turnThreadId === undefined) { |
| 1450 | throw new Error(`Unknown replay thread alias '${threadAlias}'.`); |
| 1451 | } |
| 1452 | yield* runTurnStep(client, turnThreadId, step); |
| 1453 | void stepIndex; |
| 1454 | } |
| 1455 | }).pipe( |
| 1456 | Effect.provide( |
| 1457 | layerCodex({ |
| 1458 | recorder, |
| 1459 | }), |
| 1460 | ), |
| 1461 | ); |
| 1462 | }, |
| 1463 | ); |
| 1464 | |
| 1465 | const runScenario = Effect.fn("record-codex-app-server-replay-fixture.runScenario")(function* ({ |
| 1466 | scenario, |
| 1467 | outPath, |
| 1468 | }: { |
| 1469 | readonly scenario: ReplayScenario; |
| 1470 | readonly outPath: string; |
| 1471 | }) { |
| 1472 | const fs = yield* FileSystem.FileSystem; |
| 1473 | const path = yield* Path.Path; |
| 1474 | const checkout = path.resolve(yield* path.fromFileUrl(new URL("../../..", import.meta.url))); |
| 1475 | const recorder = yield* makeRecorder({ outPath, scenario, checkout }); |
| 1476 | |
| 1477 | yield* fs.makeDirectory(path.dirname(outPath), { recursive: true }); |
| 1478 | yield* Console.log(`Writing ${scenario.name} Codex replay events to ${recorder.path}`); |
| 1479 | |
| 1480 | yield* Effect.forEach(scenario.runs, (run) => runReplaySession({ scenario, run, recorder }), { |
| 1481 | concurrency: 1, |
| 1482 | }); |
| 1483 | |
| 1484 | yield* recorder.writeRecord({ |
| 1485 | type: "runtime_exit", |
| 1486 | status: "success", |
| 1487 | }); |
| 1488 | yield* recorder.flush(); |
| 1489 | }); |
| 1490 | |
| 1491 | const RecorderEnv = Config.all({ |
| 1492 | T3_CODEX_REPLAY_OUT_DIR: Config.String("T3_CODEX_REPLAY_OUT_DIR").pipe( |
| 1493 | Config.option, |
| 1494 | Config.map(Option.getOrUndefined), |
| 1495 | ), |
| 1496 | T3_CODEX_REPLAY_OUT: Config.String("T3_CODEX_REPLAY_OUT").pipe( |
| 1497 | Config.option, |
| 1498 | Config.map(Option.getOrUndefined), |
| 1499 | ), |
| 1500 | }); |
| 1501 | |
| 1502 | const program = Effect.gen(function* () { |
| 1503 | const recorderEnv = yield* RecorderEnv; |
| 1504 | const path = yield* Path.Path; |
| 1505 | const requestedScenarios = parseScenarios(); |
| 1506 | const allScenarios = scenarios(); |
| 1507 | const outDir = readArgValue("--out-dir") ?? recorderEnv.T3_CODEX_REPLAY_OUT_DIR; |
| 1508 | const singleOutPath = readArgValue("--out") ?? recorderEnv.T3_CODEX_REPLAY_OUT; |
| 1509 | const selected = allScenarios.filter((scenario) => requestedScenarios.includes(scenario.name)); |
| 1510 | |
| 1511 | if (selected.length === 0) { |
| 1512 | throw new Error("No replay scenarios selected."); |
| 1513 | } |
| 1514 | if (singleOutPath && selected.length !== 1) { |
| 1515 | throw new Error("--out / T3_CODEX_REPLAY_OUT can only be used with exactly one --scenario."); |
| 1516 | } |
| 1517 | |
| 1518 | yield* Effect.forEach( |
| 1519 | selected, |
| 1520 | (scenario) => |
| 1521 | Effect.gen(function* () { |
| 1522 | const defaultOutPath = yield* path.fromFileUrl(defaultTranscriptUrl(scenario)); |
| 1523 | yield* runScenario({ |
| 1524 | scenario, |
| 1525 | outPath: |
| 1526 | singleOutPath ?? (outDir ? path.join(outDir, scenario.fileName) : defaultOutPath), |
| 1527 | }); |
| 1528 | }), |
| 1529 | { concurrency: 1 }, |
| 1530 | ); |
| 1531 | }); |
| 1532 | |
| 1533 | program.pipe(Effect.scoped, Effect.provide(NodeServices.layer), NodeRuntime.runMain); |