Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CodexReplayFixtures.integration.test.ts

740 lines26.5 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import type { ProviderReplayTranscript } from "@t3tools/contracts";
4import * as CodexReplay from "effect-codex-app-server/replay";
5import * as Effect from "effect/Effect";
6import * as Schema from "effect/Schema";
7​
8import { ORCHESTRATOR_REPLAY_FIXTURES } from "./fixtures/index.ts";
9import {
10 THREAD_FORK_NATIVE_CONTINUE_FORK_MARKER,
11 THREAD_FORK_NATIVE_CONTINUE_RECALL,
12 THREAD_FORK_NATIVE_CONTINUE_SOURCE_MARKER,
13 THREAD_MERGE_BACK_FORK_MARKER,
14 THREAD_MERGE_BACK_RECALL,
15 THREAD_MERGE_BACK_SIBLINGS_FIRST_MARKER,
16 THREAD_MERGE_BACK_SIBLINGS_RECALL,
17 THREAD_MERGE_BACK_SIBLINGS_SECOND_MARKER,
18 THREAD_MERGE_BACK_SIBLINGS_SOURCE_MARKER,
19 THREAD_MERGE_BACK_SOURCE_MARKER,
20} from "./fixtures/shared.ts";
21import { readProviderReplayTranscript } from "./ReplayTranscriptNdjson.ts";
22​
23const PROVIDER_THREAD_RESUME_FIRST_FINAL = "provider thread resume fixture first turn complete";
24const PROVIDER_THREAD_RESUME_SECOND_FINAL = "provider thread resume fixture second turn complete";
25​
26type ProtocolReplayEntry = Extract<
27 ProviderReplayTranscript["entries"][number],
28 { readonly type: "expect_outbound" | "emit_inbound" }
29>;
30​
31interface CodexReplayFixtureRegistration {
32 readonly registrationScenario: string;
33 readonly recordedScenario: string;
34 readonly transcriptFile: URL;
36​
37const CODEX_REPLAY_FIXTURE_REGISTRATIONS = ORCHESTRATOR_REPLAY_FIXTURES.flatMap((fixture) =>
38 fixture.providers
39 .filter((provider) => provider.driver === "codex")
40 .map((provider) => ({
41 registrationScenario: fixture.name,
42 recordedScenario: provider.recordedScenario ?? fixture.name,
43 transcriptFile: provider.transcriptFile,
44 })),
45).concat([
46 {
47 registrationScenario: "provider_thread_resume",
48 recordedScenario: "provider_thread_resume",
49 transcriptFile: new URL(
50 "./fixtures/provider_thread_resume/codex_transcript.ndjson",
51 import.meta.url,
52 ),
53 },
54 {
55 registrationScenario: "thread_fork_native_continue",
56 recordedScenario: "thread_fork_native_continue",
57 transcriptFile: new URL(
58 "./fixtures/thread_fork_native_continue/codex_transcript.ndjson",
59 import.meta.url,
60 ),
61 },
62 {
63 registrationScenario: "thread_fork_native_siblings",
64 recordedScenario: "thread_fork_native_siblings",
65 transcriptFile: new URL(
66 "./fixtures/thread_fork_native_siblings/codex_transcript.ndjson",
67 import.meta.url,
68 ),
69 },
70 {
71 registrationScenario: "thread_merge_back_continue",
72 recordedScenario: "thread_merge_back_continue",
73 transcriptFile: new URL(
74 "./fixtures/thread_merge_back_continue/codex_transcript.ndjson",
75 import.meta.url,
76 ),
77 },
78 {
79 registrationScenario: "thread_merge_back_siblings",
80 recordedScenario: "thread_merge_back_siblings",
81 transcriptFile: new URL(
82 "./fixtures/thread_merge_back_siblings/codex_transcript.ndjson",
83 import.meta.url,
84 ),
85 },
86]);
87​
88function uniqueCanonicalTranscripts(
89 registrations: ReadonlyArray<CodexReplayFixtureRegistration>,
90): ReadonlyArray<CodexReplayFixtureRegistration> {
91 const canonicalTranscripts = new Map<string, CodexReplayFixtureRegistration>();
92 for (const registration of registrations) {
93 const canonicalUrl = registration.transcriptFile.href;
94 const existing = canonicalTranscripts.get(canonicalUrl);
95 if (existing !== undefined && existing.recordedScenario !== registration.recordedScenario) {
96 throw new Error(
97 `Codex replay transcript ${canonicalUrl} has conflicting recorded scenarios ${existing.recordedScenario} (${existing.registrationScenario}) and ${registration.recordedScenario} (${registration.registrationScenario}).`,
98 );
99 }
100 canonicalTranscripts.set(canonicalUrl, existing ?? registration);
101 }
102 return Array.from(canonicalTranscripts.values());
104​
105const CODEX_REPLAY_TRANSCRIPTS = uniqueCanonicalTranscripts(CODEX_REPLAY_FIXTURE_REGISTRATIONS);
106​
107const scenarioExpectations = {
108 simple: {
109 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
110 incoming: ["thread/started", "turn/started", "turn/completed"],
111 turnStartCount: 1,
112 turnCompletedCount: 1,
113 approvalRequestCount: 0,
114 },
115 tool_call_read_only_on_request: {
116 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
117 incoming: ["item/commandExecution/requestApproval", "serverRequest/resolved", "turn/completed"],
118 turnStartCount: 1,
119 turnCompletedCount: 1,
120 approvalRequestCount: 1,
121 },
122 tool_call_workspace_never: {
123 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
124 incoming: ["turn/completed"],
125 turnStartCount: 1,
126 turnCompletedCount: 1,
127 approvalRequestCount: 0,
128 },
129 tool_call_restricted_granular: {
130 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
131 incoming: ["item/fileChange/requestApproval", "serverRequest/resolved", "turn/completed"],
132 turnStartCount: 1,
133 turnCompletedCount: 1,
134 approvalRequestCount: 1,
135 },
136 subagent: {
137 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
138 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
139 turnStartCount: 1,
140 turnCompletedCount: 3,
141 approvalRequestCount: 0,
142 },
143 subagent_continue: {
144 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
145 incoming: ["turn/started", "item/completed", "turn/completed"],
146 turnStartCount: 2,
147 turnCompletedCount: 4,
148 approvalRequestCount: 0,
149 },
150 subagent_v2: {
151 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
152 incoming: ["turn/started", "item/completed", "item/agentMessage/delta", "turn/completed"],
153 turnStartCount: 1,
154 turnCompletedCount: 2,
155 approvalRequestCount: 0,
156 },
157 subagent_v2_approval: {
158 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
159 incoming: [
160 "item/commandExecution/requestApproval",
161 "serverRequest/resolved",
162 "item/completed",
163 "turn/completed",
164 ],
165 turnStartCount: 1,
166 turnCompletedCount: 2,
167 approvalRequestCount: 1,
168 },
169 subagent_v2_nested: {
170 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
171 incoming: ["turn/started", "item/completed", "item/agentMessage/delta", "turn/completed"],
172 turnStartCount: 1,
173 turnCompletedCount: 4,
174 approvalRequestCount: 0,
175 },
176 subagent_v2_nested_approval: {
177 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
178 incoming: [
179 "item/commandExecution/requestApproval",
180 "serverRequest/resolved",
181 "item/completed",
182 "turn/completed",
183 ],
184 turnStartCount: 1,
185 turnCompletedCount: 3,
186 approvalRequestCount: 1,
187 },
188 multi_turn: {
189 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
190 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
191 turnStartCount: 2,
192 turnCompletedCount: 2,
193 approvalRequestCount: 0,
194 },
195 plan_questions: {
196 outgoing: [
197 "initialize",
198 "initialized",
199 "thread/start",
200 "turn/start",
201 "item/tool/requestUserInput",
202 ],
203 incoming: [
204 "turn/started",
205 "turn/completed",
206 "item/tool/requestUserInput",
207 "serverRequest/resolved",
208 "item/agentMessage/delta",
209 ],
210 turnStartCount: 1,
211 turnCompletedCount: 1,
212 approvalRequestCount: 0,
213 },
214 proposed_plan: {
215 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
216 incoming: ["turn/started", "turn/completed", "item/plan/delta"],
217 turnStartCount: 1,
218 turnCompletedCount: 1,
219 approvalRequestCount: 0,
220 },
221 provider_thread_resume: {
222 outgoing: ["initialize", "initialized", "thread/start", "thread/resume", "turn/start"],
223 incoming: ["thread/started", "turn/started", "turn/completed", "item/agentMessage/delta"],
224 turnStartCount: 2,
225 turnCompletedCount: 2,
226 approvalRequestCount: 0,
227 },
228 queued_turn: {
229 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
230 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
231 turnStartCount: 2,
232 turnCompletedCount: 2,
233 approvalRequestCount: 0,
234 },
235 message_steering: {
236 outgoing: ["initialize", "initialized", "thread/start", "turn/start", "turn/steer"],
237 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
238 turnStartCount: 1,
239 turnCompletedCount: 1,
240 approvalRequestCount: 0,
241 },
242 turn_interrupt: {
243 outgoing: ["initialize", "initialized", "thread/start", "turn/start", "turn/interrupt"],
244 incoming: ["turn/started", "turn/completed"],
245 turnStartCount: 1,
246 turnCompletedCount: 1,
247 approvalRequestCount: 0,
248 },
249 turn_interrupt_mid_tool: {
250 outgoing: [
251 "initialize",
252 "initialized",
253 "thread/start",
254 "turn/start",
255 "turn/interrupt",
256 "thread/backgroundTerminals/terminate",
257 ],
258 incoming: ["turn/started", "item/started", "turn/completed"],
259 turnStartCount: 1,
260 turnCompletedCount: 1,
261 approvalRequestCount: 0,
262 },
263 thread_rollback: {
264 outgoing: [
265 "initialize",
266 "initialized",
267 "thread/start",
268 "turn/start",
269 "thread/read",
270 "thread/turns/list",
271 "thread/revert",
272 ],
273 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
274 turnStartCount: 3,
275 turnCompletedCount: 3,
276 approvalRequestCount: 0,
277 },
278 thread_fork_native_continue: {
279 outgoing: ["initialize", "initialized", "thread/start", "thread/fork", "turn/start"],
280 incoming: ["thread/started", "turn/started", "turn/completed", "item/agentMessage/delta"],
281 turnStartCount: 3,
282 turnCompletedCount: 3,
283 approvalRequestCount: 0,
284 },
285 thread_fork_native_siblings: {
286 outgoing: ["initialize", "initialized", "thread/start", "thread/fork", "turn/start"],
287 incoming: ["thread/started", "turn/started", "turn/completed", "item/agentMessage/delta"],
288 turnStartCount: 3,
289 turnCompletedCount: 3,
290 approvalRequestCount: 0,
291 },
292 thread_merge_back_continue: {
293 outgoing: ["initialize", "initialized", "thread/start", "thread/fork", "turn/start"],
294 incoming: ["thread/started", "turn/started", "turn/completed", "item/agentMessage/delta"],
295 turnStartCount: 4,
296 turnCompletedCount: 4,
297 approvalRequestCount: 0,
298 },
299 thread_merge_back_siblings: {
300 outgoing: ["initialize", "initialized", "thread/start", "thread/fork", "turn/start"],
301 incoming: ["thread/started", "turn/started", "turn/completed", "item/agentMessage/delta"],
302 turnStartCount: 6,
303 turnCompletedCount: 6,
304 approvalRequestCount: 0,
305 },
306 todo_list: {
307 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
308 incoming: ["turn/started", "turn/completed", "turn/plan/updated", "item/agentMessage/delta"],
309 turnStartCount: 1,
310 turnCompletedCount: 1,
311 approvalRequestCount: 0,
312 },
313 web_search: {
314 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
315 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
316 turnStartCount: 1,
317 turnCompletedCount: 1,
318 approvalRequestCount: 0,
319 },
320} as const;
321​
322const decodeCodexTranscript = Schema.decodeUnknownEffect(
323 CodexReplay.CodexAppServerReplayTranscript,
324);
325const readTranscript = Effect.fn("readCodexReplayFixture")(function* (file: URL) {
326 return yield* readProviderReplayTranscript(file);
327}, Effect.provide(NodeServices.layer));
328​
329function labels(
330 transcript: ProviderReplayTranscript,
331 type: "expect_outbound" | "emit_inbound",
332): ReadonlyArray<string> {
333 return transcript.entries.flatMap((entry) => {
334 if (entry.type !== type || entry.label === undefined) {
335 return [];
336 }
337 return [entry.label];
338 });
340​
341function countLabel(
342 transcript: ProviderReplayTranscript,
343 type: "expect_outbound" | "emit_inbound",
344 label: string,
345) {
346 return labels(transcript, type).filter((entryLabel) => entryLabel === label).length;
348​
349function countApprovalRequests(transcript: ProviderReplayTranscript) {
350 return labels(transcript, "emit_inbound").filter((label) => label.endsWith("/requestApproval"))
351 .length;
353​
354function isRecord(value: unknown): value is Record<string, unknown> {
355 return typeof value === "object" && value !== null && !Array.isArray(value);
357​
358function readPath(value: unknown, path: ReadonlyArray<string | number>): unknown {
359 let current = value;
360 for (const segment of path) {
361 if (typeof segment === "number") {
362 if (!Array.isArray(current)) {
363 throw new Error(`Expected array while reading ${path.join(".")}.`);
364 }
365 current = current[segment];
366 continue;
367 }
368​
369 if (!isRecord(current)) {
370 throw new Error(`Expected object while reading ${path.join(".")}.`);
371 }
372 current = current[segment];
373 }
374 return current;
376​
377function readString(value: unknown, path: ReadonlyArray<string | number>): string {
378 const current = readPath(value, path);
379 if (typeof current !== "string") {
380 throw new Error(`Expected string at ${path.join(".")}.`);
381 }
382 return current;
384​
385function readArray(value: unknown, path: ReadonlyArray<string | number>): ReadonlyArray<unknown> {
386 const current = readPath(value, path);
387 if (!Array.isArray(current)) {
388 throw new Error(`Expected array at ${path.join(".")}.`);
389 }
390 return current;
392​
393function findProtocolEntry(
394 transcript: ProviderReplayTranscript,
395 type: "expect_outbound" | "emit_inbound",
396 label: string,
397 occurrence = 0,
398): ProtocolReplayEntry {
399 const matches = transcript.entries.filter(
400 (entry): entry is ProtocolReplayEntry => entry.type === type && entry.label === label,
401 );
402 const entry = matches[occurrence];
403 if (!entry) {
404 throw new Error(`Missing ${type} ${label} occurrence ${occurrence}.`);
405 }
406 return entry;
408​
409function agentMessageTexts(transcript: ProviderReplayTranscript): ReadonlyArray<string> {
410 return transcript.entries.flatMap((entry) => {
411 if (entry.type !== "emit_inbound" || entry.label !== "item/completed") {
412 return [];
413 }
414​
415 const item = readPath(entry.frame, ["params", "item"]);
416 if (!isRecord(item) || item.type !== "agentMessage" || typeof item.text !== "string") {
417 return [];
418 }
419 return [item.text];
420 });
422​
423function assertScenarioExpectations(transcript: ProviderReplayTranscript) {
424 const expectation =
425 scenarioExpectations[transcript.scenario as keyof typeof scenarioExpectations];
426 const outgoingLabels = labels(transcript, "expect_outbound");
427 const incomingLabels = labels(transcript, "emit_inbound");
428​
429 assert.isDefined(expectation, `missing scenario expectation for ${transcript.scenario}`);
430 for (const label of expectation.outgoing) {
431 assert.include(outgoingLabels, label, `${transcript.scenario} missing outgoing ${label}`);
432 }
433 for (const label of expectation.incoming) {
434 assert.include(incomingLabels, label, `${transcript.scenario} missing incoming ${label}`);
435 }
436​
437 assert.equal(countLabel(transcript, "expect_outbound", "turn/start"), expectation.turnStartCount);
438 assert.equal(
439 countLabel(transcript, "emit_inbound", "turn/completed"),
440 expectation.turnCompletedCount,
441 );
442 assert.equal(countApprovalRequests(transcript), expectation.approvalRequestCount);
444​
445function assertProviderThreadResumeSemantics(transcript: ProviderReplayTranscript) {
446 if (transcript.scenario !== "provider_thread_resume") {
447 return;
448 }
449​
450 const startThreadId = readString(
451 findProtocolEntry(transcript, "emit_inbound", "thread/start").frame,
452 ["result", "thread", "id"],
453 );
454 const resumeRequestedThreadId = readString(
455 findProtocolEntry(transcript, "expect_outbound", "thread/resume").frame,
456 ["params", "threadId"],
457 );
458 const resumeRequest = findProtocolEntry(transcript, "expect_outbound", "thread/resume").frame;
459 const resumedThreadFrame = findProtocolEntry(transcript, "emit_inbound", "thread/resume").frame;
460 const resumedThreadId = readString(resumedThreadFrame, ["result", "thread", "id"]);
461 const secondTurnThreadId = readString(
462 findProtocolEntry(transcript, "expect_outbound", "turn/start", 1).frame,
463 ["params", "threadId"],
464 );
465 const texts = agentMessageTexts(transcript);
466 const secondFinalText = texts[1] ?? "";
467​
468 assert.equal(
469 resumeRequestedThreadId,
470 startThreadId,
471 "thread/resume must request the provider thread created by thread/start",
472 );
473 assert.equal(
474 resumedThreadId,
475 startThreadId,
476 "thread/resume must return the same provider thread id",
477 );
478 assert.equal(
479 secondTurnThreadId,
480 startThreadId,
481 "turn after resume must run on the resumed provider thread",
482 );
483 // The adapter resumes with excludeTurns, so history reaches the model, not the response.
484 assert.equal(readPath(resumeRequest, ["params", "excludeTurns"]), true);
485 assert.lengthOf(readArray(resumedThreadFrame, ["result", "thread", "turns"]), 0);
486 assert.equal(texts[0], PROVIDER_THREAD_RESUME_FIRST_FINAL);
487 assert.include(
488 secondFinalText,
489 PROVIDER_THREAD_RESUME_FIRST_FINAL,
490 "second turn must demonstrate access to resumed conversation history",
491 );
492 assert.include(
493 secondFinalText,
494 PROVIDER_THREAD_RESUME_SECOND_FINAL,
495 "second turn must include its own completion marker",
496 );
498​
499function assertContinuedForkSemantics(transcript: ProviderReplayTranscript) {
500 if (transcript.scenario !== "thread_fork_native_continue") {
501 return;
502 }
503​
504 const sourceThreadId = readString(
505 findProtocolEntry(transcript, "emit_inbound", "thread/start").frame,
506 ["result", "thread", "id"],
507 );
508 const forkRequestThreadId = readString(
509 findProtocolEntry(transcript, "expect_outbound", "thread/fork").frame,
510 ["params", "threadId"],
511 );
512 const forkThreadId = readString(
513 findProtocolEntry(transcript, "emit_inbound", "thread/fork").frame,
514 ["result", "thread", "id"],
515 );
516 const firstForkTurnThreadId = readString(
517 findProtocolEntry(transcript, "expect_outbound", "turn/start", 1).frame,
518 ["params", "threadId"],
519 );
520 const secondForkTurnThreadId = readString(
521 findProtocolEntry(transcript, "expect_outbound", "turn/start", 2).frame,
522 ["params", "threadId"],
523 );
524 const recallPrompt = readString(
525 findProtocolEntry(transcript, "expect_outbound", "turn/start", 2).frame,
526 ["params", "input", 0, "text"],
527 );
528​
529 assert.equal(forkRequestThreadId, sourceThreadId);
530 assert.notEqual(forkThreadId, sourceThreadId);
531 assert.equal(firstForkTurnThreadId, forkThreadId);
532 assert.equal(secondForkTurnThreadId, forkThreadId);
533 assert.notInclude(recallPrompt, THREAD_FORK_NATIVE_CONTINUE_SOURCE_MARKER);
534 assert.notInclude(recallPrompt, THREAD_FORK_NATIVE_CONTINUE_FORK_MARKER);
535 const texts = agentMessageTexts(transcript);
536 for (const expected of [
537 "source marker stored",
538 "fork marker stored",
539 THREAD_FORK_NATIVE_CONTINUE_RECALL,
540 ]) {
541 assert.include(texts, expected);
542 }
543 assert.equal(
544 texts.at(-1),
545 THREAD_FORK_NATIVE_CONTINUE_RECALL,
546 "the second fork-local turn must recall both the inherited source marker and fork-local marker",
547 );
549​
550function assertSiblingForkSemantics(transcript: ProviderReplayTranscript) {
551 if (transcript.scenario !== "thread_fork_native_siblings") {
552 return;
553 }
554​
555 const sourceThreadId = readString(
556 findProtocolEntry(transcript, "emit_inbound", "thread/start").frame,
557 ["result", "thread", "id"],
558 );
559 const firstForkFrame = findProtocolEntry(transcript, "emit_inbound", "thread/fork", 0).frame;
560 const secondForkFrame = findProtocolEntry(transcript, "emit_inbound", "thread/fork", 1).frame;
561 const firstForkId = readString(firstForkFrame, ["result", "thread", "id"]);
562 const secondForkId = readString(secondForkFrame, ["result", "thread", "id"]);
563 const firstForkParentId = readString(firstForkFrame, ["result", "thread", "forkedFromId"]);
564 const secondForkParentId = readString(secondForkFrame, ["result", "thread", "forkedFromId"]);
565 const firstForkTurnThreadId = readString(
566 findProtocolEntry(transcript, "expect_outbound", "turn/start", 1).frame,
567 ["params", "threadId"],
568 );
569 const secondForkTurnThreadId = readString(
570 findProtocolEntry(transcript, "expect_outbound", "turn/start", 2).frame,
571 ["params", "threadId"],
572 );
573 const texts = agentMessageTexts(transcript);
574 const firstRecall = texts[1]?.replace(/\s*\|\s*/u, "|") ?? "";
575 const secondRecall = texts[2]?.replace(/\s*\|\s*/u, "|") ?? "";
576​
577 assert.equal(firstForkParentId, sourceThreadId);
578 assert.equal(secondForkParentId, sourceThreadId);
579 assert.notEqual(firstForkId, secondForkId);
580 assert.equal(firstForkTurnThreadId, firstForkId);
581 assert.equal(secondForkTurnThreadId, secondForkId);
582 assert.equal(firstRecall, "sibling-source-8R3D|sibling-first-5L2P");
583 assert.notInclude(firstRecall, "sibling-second-9N6C");
584 assert.equal(secondRecall, "sibling-source-8R3D|sibling-second-9N6C");
585 assert.notInclude(secondRecall, "sibling-first-5L2P");
587​
588function assertMergeBackSemantics(transcript: ProviderReplayTranscript) {
589 if (transcript.scenario !== "thread_merge_back_continue") {
590 return;
591 }
592​
593 const sourceThreadId = readString(
594 findProtocolEntry(transcript, "emit_inbound", "thread/start").frame,
595 ["result", "thread", "id"],
596 );
597 const forkThreadId = readString(
598 findProtocolEntry(transcript, "emit_inbound", "thread/fork").frame,
599 ["result", "thread", "id"],
600 );
601 const mergeThreadId = readString(
602 findProtocolEntry(transcript, "expect_outbound", "turn/start", 2).frame,
603 ["params", "threadId"],
604 );
605 const recallFrame = findProtocolEntry(transcript, "expect_outbound", "turn/start", 3).frame;
606 const recallThreadId = readString(recallFrame, ["params", "threadId"]);
607 const recallPrompt = readString(recallFrame, ["params", "input", 0, "text"]);
608 const texts = agentMessageTexts(transcript);
609​
610 assert.notEqual(forkThreadId, sourceThreadId);
611 assert.equal(mergeThreadId, sourceThreadId);
612 assert.equal(recallThreadId, sourceThreadId);
613 assert.notInclude(recallPrompt, THREAD_MERGE_BACK_SOURCE_MARKER);
614 assert.notInclude(recallPrompt, THREAD_MERGE_BACK_FORK_MARKER);
615 assert.equal(texts.at(-1)?.replace(/\s*\|\s*/u, "|"), THREAD_MERGE_BACK_RECALL);
617​
618function assertSiblingMergeBackSemantics(transcript: ProviderReplayTranscript) {
619 if (transcript.scenario !== "thread_merge_back_siblings") {
620 return;
621 }
622​
623 const sourceThreadId = readString(
624 findProtocolEntry(transcript, "emit_inbound", "thread/start").frame,
625 ["result", "thread", "id"],
626 );
627 const firstForkId = readString(
628 findProtocolEntry(transcript, "emit_inbound", "thread/fork", 0).frame,
629 ["result", "thread", "id"],
630 );
631 const secondForkId = readString(
632 findProtocolEntry(transcript, "emit_inbound", "thread/fork", 1).frame,
633 ["result", "thread", "id"],
634 );
635 const firstMergeThreadId = readString(
636 findProtocolEntry(transcript, "expect_outbound", "turn/start", 3).frame,
637 ["params", "threadId"],
638 );
639 const secondMergeThreadId = readString(
640 findProtocolEntry(transcript, "expect_outbound", "turn/start", 4).frame,
641 ["params", "threadId"],
642 );
643 const recallFrame = findProtocolEntry(transcript, "expect_outbound", "turn/start", 5).frame;
644 const recallPrompt = readString(recallFrame, ["params", "input", 0, "text"]);
645 const finalText = agentMessageTexts(transcript)
646 .at(-1)
647 ?.replace(/\s*\|\s*/gu, "|");
648​
649 assert.notEqual(firstForkId, secondForkId);
650 assert.equal(firstMergeThreadId, sourceThreadId);
651 assert.equal(secondMergeThreadId, sourceThreadId);
652 assert.notInclude(recallPrompt, THREAD_MERGE_BACK_SIBLINGS_SOURCE_MARKER);
653 assert.notInclude(recallPrompt, THREAD_MERGE_BACK_SIBLINGS_FIRST_MARKER);
654 assert.notInclude(recallPrompt, THREAD_MERGE_BACK_SIBLINGS_SECOND_MARKER);
655 assert.equal(finalText, THREAD_MERGE_BACK_SIBLINGS_RECALL);
657​
658/**
659 * Codex announces a v2 child with the parent's `subAgentActivity(started)`
660 * before the child's first `turn/started`; the adapter registers children
661 * from that activity.
662 */
663function assertSubagentActivityPrecedesChildTurns(transcript: ProviderReplayTranscript) {
664 if (transcript.scenario !== "subagent_v2" && transcript.scenario !== "subagent_v2_nested") {
665 return;
666 }
667​
668 const announcedChildren = new Set<string>();
669 const startedChildren = new Set<string>();
670 const rootThreadId = readString(
671 findProtocolEntry(transcript, "emit_inbound", "thread/start").frame,
672 ["result", "thread", "id"],
673 );
674 for (const entry of transcript.entries) {
675 if (entry.type !== "emit_inbound") continue;
676 const method = readPath(entry.frame, ["method"]);
677 if (method === "item/started") {
678 const item = readPath(entry.frame, ["params", "item"]);
679 if (isRecord(item) && item.type === "subAgentActivity" && item.kind === "started") {
680 announcedChildren.add(readString(item, ["agentThreadId"]));
681 }
682 }
683 if (method === "turn/started") {
684 const threadId = readString(entry.frame, ["params", "threadId"]);
685 if (threadId === rootThreadId) continue;
686 assert.isTrue(
687 announcedChildren.has(threadId),
688 `${transcript.scenario}: child ${threadId} started a turn before its subAgentActivity`,
689 );
690 startedChildren.add(threadId);
691 }
692 }
693 assert.equal(
694 startedChildren.size,
695 transcript.scenario === "subagent_v2" ? 1 : 3,
696 `${transcript.scenario}: unexpected number of child turns`,
697 );
699​
700describe("Codex replay fixtures", () => {
701 it.effect("loads each canonical Codex fixture as an app-server replay transcript", () =>
702 Effect.gen(function* () {
703 for (const fixture of CODEX_REPLAY_TRANSCRIPTS) {
704 const transcript = yield* readTranscript(fixture.transcriptFile);
705 const codexTranscript = yield* decodeCodexTranscript(transcript);
706 const first = transcript.entries[0];
707​
708 assert.equal(codexTranscript.provider, "codex");
709 assert.equal(codexTranscript.protocol, "codex.app-server");
710 assert.equal(codexTranscript.scenario, fixture.recordedScenario);
711 assert.deepEqual(codexTranscript.entries.at(-1), {
712 type: "runtime_exit",
713 status: "success",
714 });
715 assert.equal(first?.type, "expect_outbound");
716 if (first?.type !== "expect_outbound") {
717 throw new Error(
718 `Expected ${fixture.recordedScenario} to start with initialize outbound frame.`,
719 );
720 }
721 assert.equal(first.label, "initialize");
722​
723 assertScenarioExpectations(transcript);
724 assertProviderThreadResumeSemantics(transcript);
725 assertContinuedForkSemantics(transcript);
726 assertSiblingForkSemantics(transcript);
727 assertMergeBackSemantics(transcript);
728 assertSiblingMergeBackSemantics(transcript);
729 assertSubagentActivityPrecedesChildTurns(transcript);
730 }
731 }),
732 );
733​
734 it("covers the expected replay suite exactly", () => {
735 assert.deepEqual(
736 CODEX_REPLAY_TRANSCRIPTS.map((fixture) => fixture.recordedScenario).toSorted(),
737 Object.keys(scenarioExpectations).toSorted(),
738 );
739 });
740});