Open History to see the last commit
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;
35}
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());
103}
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: [
132 "item/fileChange/requestApproval",
133 "serverRequest/resolved",
134 "item/fileChange/outputDelta",
135 "turn/diff/updated",
136 "turn/completed",
137 ],
138 turnStartCount: 1,
139 turnCompletedCount: 1,
140 approvalRequestCount: 1,
141 },
142 subagent: {
143 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
144 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
145 turnStartCount: 1,
146 turnCompletedCount: 3,
147 approvalRequestCount: 0,
148 },
149 subagent_continue: {
150 outgoing: [
151 "initialize",
152 "initialized",
153 "thread/start",
154 "turn/start/spawn",
155 "turn/start/continue",
156 ],
157 incoming: [
158 "turn/started/root-1",
159 "turn/completed/child-1",
160 "turn/completed/root-1",
161 "turn/started/root-2",
162 "turn/completed/child-2",
163 "turn/completed/root-2",
164 ],
165 turnStartCount: 0,
166 turnCompletedCount: 0,
167 approvalRequestCount: 0,
168 },
169 subagent_v2: {
170 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
171 incoming: [
172 "turn/started",
173 "item/completed/subAgentActivity-started",
174 "turn/started/child",
175 "item/completed/child-answer",
176 "turn/completed",
177 ],
178 turnStartCount: 1,
179 turnCompletedCount: 2,
180 approvalRequestCount: 0,
181 },
182 subagent_v2_nested: {
183 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
184 incoming: [
185 "turn/started",
186 "item/completed/root-subagent",
187 "turn/started/child",
188 "item/completed/child-subagent",
189 "turn/started/grandchild",
190 "item/completed/grandchild-subagent",
191 "turn/started/leaf",
192 "item/completed/leaf-answer",
193 "turn/completed",
194 ],
195 turnStartCount: 1,
196 turnCompletedCount: 4,
197 approvalRequestCount: 0,
198 },
199 multi_turn: {
200 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
201 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
202 turnStartCount: 2,
203 turnCompletedCount: 2,
204 approvalRequestCount: 0,
205 },
206 plan_questions: {
207 outgoing: [
208 "initialize",
209 "initialized",
210 "thread/start",
211 "turn/start",
212 "item/tool/requestUserInput",
213 ],
214 incoming: [
215 "turn/started",
216 "turn/completed",
217 "item/tool/requestUserInput",
218 "serverRequest/resolved",
219 "item/agentMessage/delta",
220 ],
221 turnStartCount: 1,
222 turnCompletedCount: 1,
223 approvalRequestCount: 0,
224 },
225 proposed_plan: {
226 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
227 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
228 turnStartCount: 1,
229 turnCompletedCount: 1,
230 approvalRequestCount: 0,
231 },
232 provider_thread_resume: {
233 outgoing: ["initialize", "initialized", "thread/start", "thread/resume", "turn/start"],
234 incoming: ["thread/started", "turn/started", "turn/completed", "item/agentMessage/delta"],
235 turnStartCount: 2,
236 turnCompletedCount: 2,
237 approvalRequestCount: 0,
238 },
239 queued_turn: {
240 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
241 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
242 turnStartCount: 2,
243 turnCompletedCount: 2,
244 approvalRequestCount: 0,
245 },
246 message_steering: {
247 outgoing: ["initialize", "initialized", "thread/start", "turn/start", "turn/steer"],
248 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
249 turnStartCount: 1,
250 turnCompletedCount: 1,
251 approvalRequestCount: 0,
252 },
253 turn_interrupt: {
254 outgoing: ["initialize", "initialized", "thread/start", "turn/start", "turn/interrupt"],
255 incoming: ["turn/started", "turn/completed"],
256 turnStartCount: 1,
257 turnCompletedCount: 1,
258 approvalRequestCount: 0,
259 },
260 turn_interrupt_mid_tool: {
261 outgoing: [
262 "initialize",
263 "initialized",
264 "thread/start",
265 "turn/start",
266 "turn/interrupt",
267 "thread/backgroundTerminals/terminate",
268 ],
269 incoming: ["turn/started", "item/started", "turn/completed"],
270 turnStartCount: 1,
271 turnCompletedCount: 1,
272 approvalRequestCount: 0,
273 },
274 thread_rollback: {
275 outgoing: [
276 "initialize",
277 "initialized",
278 "thread/start",
279 "turn/start",
280 "thread/read",
281 "thread/rollback",
282 ],
283 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
284 turnStartCount: 3,
285 turnCompletedCount: 3,
286 approvalRequestCount: 0,
287 },
288 thread_fork_native_continue: {
289 outgoing: ["initialize", "initialized", "thread/start", "thread/fork", "turn/start"],
290 incoming: ["thread/started", "turn/started", "turn/completed", "item/agentMessage/delta"],
291 turnStartCount: 3,
292 turnCompletedCount: 3,
293 approvalRequestCount: 0,
294 },
295 thread_fork_native_siblings: {
296 outgoing: ["initialize", "initialized", "thread/start", "thread/fork", "turn/start"],
297 incoming: ["thread/started", "turn/started", "turn/completed", "item/agentMessage/delta"],
298 turnStartCount: 3,
299 turnCompletedCount: 3,
300 approvalRequestCount: 0,
301 },
302 thread_merge_back_continue: {
303 outgoing: ["initialize", "initialized", "thread/start", "thread/fork", "turn/start"],
304 incoming: ["thread/started", "turn/started", "turn/completed", "item/agentMessage/delta"],
305 turnStartCount: 4,
306 turnCompletedCount: 4,
307 approvalRequestCount: 0,
308 },
309 thread_merge_back_siblings: {
310 outgoing: ["initialize", "initialized", "thread/start", "thread/fork", "turn/start"],
311 incoming: ["thread/started", "turn/started", "turn/completed", "item/agentMessage/delta"],
312 turnStartCount: 6,
313 turnCompletedCount: 6,
314 approvalRequestCount: 0,
315 },
316 todo_list: {
317 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
318 incoming: ["turn/started", "turn/completed", "turn/plan/updated", "item/agentMessage/delta"],
319 turnStartCount: 1,
320 turnCompletedCount: 1,
321 approvalRequestCount: 0,
322 },
323 web_search: {
324 outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
325 incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
326 turnStartCount: 1,
327 turnCompletedCount: 1,
328 approvalRequestCount: 0,
329 },
330} as const;
331
332const decodeCodexTranscript = Schema.decodeUnknownEffect(
333 CodexReplay.CodexAppServerReplayTranscript,
334);
335const readTranscript = Effect.fn("readCodexReplayFixture")(function* (file: URL) {
336 return yield* readProviderReplayTranscript(file);
337}, Effect.provide(NodeServices.layer));
338
339function labels(
340 transcript: ProviderReplayTranscript,
341 type: "expect_outbound" | "emit_inbound",
342): ReadonlyArray<string> {
343 return transcript.entries.flatMap((entry) => {
344 if (entry.type !== type || entry.label === undefined) {
345 return [];
346 }
347 return [entry.label];
348 });
349}
350
351function countLabel(
352 transcript: ProviderReplayTranscript,
353 type: "expect_outbound" | "emit_inbound",
354 label: string,
355) {
356 return labels(transcript, type).filter((entryLabel) => entryLabel === label).length;
357}
358
359function countApprovalRequests(transcript: ProviderReplayTranscript) {
360 return labels(transcript, "emit_inbound").filter((label) => label.endsWith("/requestApproval"))
361 .length;
362}
363
364function isRecord(value: unknown): value is Record<string, unknown> {
365 return typeof value === "object" && value !== null && !Array.isArray(value);
366}
367
368function readPath(value: unknown, path: ReadonlyArray<string | number>): unknown {
369 let current = value;
370 for (const segment of path) {
371 if (typeof segment === "number") {
372 if (!Array.isArray(current)) {
373 throw new Error(`Expected array while reading ${path.join(".")}.`);
374 }
375 current = current[segment];
376 continue;
377 }
378
379 if (!isRecord(current)) {
380 throw new Error(`Expected object while reading ${path.join(".")}.`);
381 }
382 current = current[segment];
383 }
384 return current;
385}
386
387function readString(value: unknown, path: ReadonlyArray<string | number>): string {
388 const current = readPath(value, path);
389 if (typeof current !== "string") {
390 throw new Error(`Expected string at ${path.join(".")}.`);
391 }
392 return current;
393}
394
395function readArray(value: unknown, path: ReadonlyArray<string | number>): ReadonlyArray<unknown> {
396 const current = readPath(value, path);
397 if (!Array.isArray(current)) {
398 throw new Error(`Expected array at ${path.join(".")}.`);
399 }
400 return current;
401}
402
403function findProtocolEntry(
404 transcript: ProviderReplayTranscript,
405 type: "expect_outbound" | "emit_inbound",
406 label: string,
407 occurrence = 0,
408): ProtocolReplayEntry {
409 const matches = transcript.entries.filter(
410 (entry): entry is ProtocolReplayEntry => entry.type === type && entry.label === label,
411 );
412 const entry = matches[occurrence];
413 if (!entry) {
414 throw new Error(`Missing ${type} ${label} occurrence ${occurrence}.`);
415 }
416 return entry;
417}
418
419function agentMessageTexts(transcript: ProviderReplayTranscript): ReadonlyArray<string> {
420 return transcript.entries.flatMap((entry) => {
421 if (entry.type !== "emit_inbound" || entry.label !== "item/completed") {
422 return [];
423 }
424
425 const item = readPath(entry.frame, ["params", "item"]);
426 if (!isRecord(item) || item.type !== "agentMessage" || typeof item.text !== "string") {
427 return [];
428 }
429 return [item.text];
430 });
431}
432
433function assertScenarioExpectations(transcript: ProviderReplayTranscript) {
434 const expectation =
435 scenarioExpectations[transcript.scenario as keyof typeof scenarioExpectations];
436 const outgoingLabels = labels(transcript, "expect_outbound");
437 const incomingLabels = labels(transcript, "emit_inbound");
438
439 assert.isDefined(expectation, `missing scenario expectation for ${transcript.scenario}`);
440 for (const label of expectation.outgoing) {
441 assert.include(outgoingLabels, label, `${transcript.scenario} missing outgoing ${label}`);
442 }
443 for (const label of expectation.incoming) {
444 assert.include(incomingLabels, label, `${transcript.scenario} missing incoming ${label}`);
445 }
446
447 assert.equal(countLabel(transcript, "expect_outbound", "turn/start"), expectation.turnStartCount);
448 assert.equal(
449 countLabel(transcript, "emit_inbound", "turn/completed"),
450 expectation.turnCompletedCount,
451 );
452 assert.equal(countApprovalRequests(transcript), expectation.approvalRequestCount);
453}
454
455function assertProviderThreadResumeSemantics(transcript: ProviderReplayTranscript) {
456 if (transcript.scenario !== "provider_thread_resume") {
457 return;
458 }
459
460 const startThreadId = readString(
461 findProtocolEntry(transcript, "emit_inbound", "thread/start").frame,
462 ["result", "thread", "id"],
463 );
464 const resumeRequestedThreadId = readString(
465 findProtocolEntry(transcript, "expect_outbound", "thread/resume").frame,
466 ["params", "threadId"],
467 );
468 const resumedThreadFrame = findProtocolEntry(transcript, "emit_inbound", "thread/resume").frame;
469 const resumedThreadId = readString(resumedThreadFrame, ["result", "thread", "id"]);
470 const resumedTurns = readArray(resumedThreadFrame, ["result", "thread", "turns"]);
471 const secondTurnThreadId = readString(
472 findProtocolEntry(transcript, "expect_outbound", "turn/start", 1).frame,
473 ["params", "threadId"],
474 );
475 const texts = agentMessageTexts(transcript);
476 const secondFinalText = texts[1] ?? "";
477
478 assert.equal(
479 resumeRequestedThreadId,
480 startThreadId,
481 "thread/resume must request the provider thread created by thread/start",
482 );
483 assert.equal(
484 resumedThreadId,
485 startThreadId,
486 "thread/resume must return the same provider thread id",
487 );
488 assert.equal(
489 secondTurnThreadId,
490 startThreadId,
491 "turn after resume must run on the resumed provider thread",
492 );
493 assert.isAtLeast(resumedTurns.length, 1, "thread/resume response must include prior turns");
494
495 const resumedFirstTurnItems = readArray(resumedTurns[0], ["items"]);
496 const resumedFirstTurnAgentText = resumedFirstTurnItems
497 .filter(isRecord)
498 .filter((item) => item.type === "agentMessage")
499 .map((item) => item.text)
500 .find((text): text is string => typeof text === "string");
501
502 assert.equal(
503 resumedFirstTurnAgentText,
504 PROVIDER_THREAD_RESUME_FIRST_FINAL,
505 "thread/resume response must hydrate the prior assistant answer",
506 );
507 assert.include(
508 secondFinalText,
509 PROVIDER_THREAD_RESUME_FIRST_FINAL,
510 "second turn must demonstrate access to resumed conversation history",
511 );
512 assert.include(
513 secondFinalText,
514 PROVIDER_THREAD_RESUME_SECOND_FINAL,
515 "second turn must include its own completion marker",
516 );
517}
518
519function assertContinuedForkSemantics(transcript: ProviderReplayTranscript) {
520 if (transcript.scenario !== "thread_fork_native_continue") {
521 return;
522 }
523
524 const sourceThreadId = readString(
525 findProtocolEntry(transcript, "emit_inbound", "thread/start").frame,
526 ["result", "thread", "id"],
527 );
528 const forkRequestThreadId = readString(
529 findProtocolEntry(transcript, "expect_outbound", "thread/fork").frame,
530 ["params", "threadId"],
531 );
532 const forkThreadId = readString(
533 findProtocolEntry(transcript, "emit_inbound", "thread/fork").frame,
534 ["result", "thread", "id"],
535 );
536 const firstForkTurnThreadId = readString(
537 findProtocolEntry(transcript, "expect_outbound", "turn/start", 1).frame,
538 ["params", "threadId"],
539 );
540 const secondForkTurnThreadId = readString(
541 findProtocolEntry(transcript, "expect_outbound", "turn/start", 2).frame,
542 ["params", "threadId"],
543 );
544 const recallPrompt = readString(
545 findProtocolEntry(transcript, "expect_outbound", "turn/start", 2).frame,
546 ["params", "input", 0, "text"],
547 );
548
549 assert.equal(forkRequestThreadId, sourceThreadId);
550 assert.notEqual(forkThreadId, sourceThreadId);
551 assert.equal(firstForkTurnThreadId, forkThreadId);
552 assert.equal(secondForkTurnThreadId, forkThreadId);
553 assert.notInclude(recallPrompt, THREAD_FORK_NATIVE_CONTINUE_SOURCE_MARKER);
554 assert.notInclude(recallPrompt, THREAD_FORK_NATIVE_CONTINUE_FORK_MARKER);
555 const texts = agentMessageTexts(transcript);
556 for (const expected of [
557 "source marker stored",
558 "fork marker stored",
559 THREAD_FORK_NATIVE_CONTINUE_RECALL,
560 ]) {
561 assert.include(texts, expected);
562 }
563 assert.equal(
564 texts.at(-1),
565 THREAD_FORK_NATIVE_CONTINUE_RECALL,
566 "the second fork-local turn must recall both the inherited source marker and fork-local marker",
567 );
568}
569
570function assertSiblingForkSemantics(transcript: ProviderReplayTranscript) {
571 if (transcript.scenario !== "thread_fork_native_siblings") {
572 return;
573 }
574
575 const sourceThreadId = readString(
576 findProtocolEntry(transcript, "emit_inbound", "thread/start").frame,
577 ["result", "thread", "id"],
578 );
579 const firstForkFrame = findProtocolEntry(transcript, "emit_inbound", "thread/fork", 0).frame;
580 const secondForkFrame = findProtocolEntry(transcript, "emit_inbound", "thread/fork", 1).frame;
581 const firstForkId = readString(firstForkFrame, ["result", "thread", "id"]);
582 const secondForkId = readString(secondForkFrame, ["result", "thread", "id"]);
583 const firstForkParentId = readString(firstForkFrame, ["result", "thread", "forkedFromId"]);
584 const secondForkParentId = readString(secondForkFrame, ["result", "thread", "forkedFromId"]);
585 const firstForkTurnThreadId = readString(
586 findProtocolEntry(transcript, "expect_outbound", "turn/start", 1).frame,
587 ["params", "threadId"],
588 );
589 const secondForkTurnThreadId = readString(
590 findProtocolEntry(transcript, "expect_outbound", "turn/start", 2).frame,
591 ["params", "threadId"],
592 );
593 const texts = agentMessageTexts(transcript);
594 const firstRecall = texts[1]?.replace(/\s*\|\s*/u, "|") ?? "";
595 const secondRecall = texts[2]?.replace(/\s*\|\s*/u, "|") ?? "";
596
597 assert.equal(firstForkParentId, sourceThreadId);
598 assert.equal(secondForkParentId, sourceThreadId);
599 assert.notEqual(firstForkId, secondForkId);
600 assert.equal(firstForkTurnThreadId, firstForkId);
601 assert.equal(secondForkTurnThreadId, secondForkId);
602 assert.equal(firstRecall, "sibling-source-8R3D|sibling-first-5L2P");
603 assert.notInclude(firstRecall, "sibling-second-9N6C");
604 assert.equal(secondRecall, "sibling-source-8R3D|sibling-second-9N6C");
605 assert.notInclude(secondRecall, "sibling-first-5L2P");
606}
607
608function assertMergeBackSemantics(transcript: ProviderReplayTranscript) {
609 if (transcript.scenario !== "thread_merge_back_continue") {
610 return;
611 }
612
613 const sourceThreadId = readString(
614 findProtocolEntry(transcript, "emit_inbound", "thread/start").frame,
615 ["result", "thread", "id"],
616 );
617 const forkThreadId = readString(
618 findProtocolEntry(transcript, "emit_inbound", "thread/fork").frame,
619 ["result", "thread", "id"],
620 );
621 const mergeThreadId = readString(
622 findProtocolEntry(transcript, "expect_outbound", "turn/start", 2).frame,
623 ["params", "threadId"],
624 );
625 const recallFrame = findProtocolEntry(transcript, "expect_outbound", "turn/start", 3).frame;
626 const recallThreadId = readString(recallFrame, ["params", "threadId"]);
627 const recallPrompt = readString(recallFrame, ["params", "input", 0, "text"]);
628 const texts = agentMessageTexts(transcript);
629
630 assert.notEqual(forkThreadId, sourceThreadId);
631 assert.equal(mergeThreadId, sourceThreadId);
632 assert.equal(recallThreadId, sourceThreadId);
633 assert.notInclude(recallPrompt, THREAD_MERGE_BACK_SOURCE_MARKER);
634 assert.notInclude(recallPrompt, THREAD_MERGE_BACK_FORK_MARKER);
635 assert.equal(texts.at(-1)?.replace(/\s*\|\s*/u, "|"), THREAD_MERGE_BACK_RECALL);
636}
637
638function assertSiblingMergeBackSemantics(transcript: ProviderReplayTranscript) {
639 if (transcript.scenario !== "thread_merge_back_siblings") {
640 return;
641 }
642
643 const sourceThreadId = readString(
644 findProtocolEntry(transcript, "emit_inbound", "thread/start").frame,
645 ["result", "thread", "id"],
646 );
647 const firstForkId = readString(
648 findProtocolEntry(transcript, "emit_inbound", "thread/fork", 0).frame,
649 ["result", "thread", "id"],
650 );
651 const secondForkId = readString(
652 findProtocolEntry(transcript, "emit_inbound", "thread/fork", 1).frame,
653 ["result", "thread", "id"],
654 );
655 const firstMergeThreadId = readString(
656 findProtocolEntry(transcript, "expect_outbound", "turn/start", 3).frame,
657 ["params", "threadId"],
658 );
659 const secondMergeThreadId = readString(
660 findProtocolEntry(transcript, "expect_outbound", "turn/start", 4).frame,
661 ["params", "threadId"],
662 );
663 const recallFrame = findProtocolEntry(transcript, "expect_outbound", "turn/start", 5).frame;
664 const recallPrompt = readString(recallFrame, ["params", "input", 0, "text"]);
665 const finalText = agentMessageTexts(transcript)
666 .at(-1)
667 ?.replace(/\s*\|\s*/gu, "|");
668
669 assert.notEqual(firstForkId, secondForkId);
670 assert.equal(firstMergeThreadId, sourceThreadId);
671 assert.equal(secondMergeThreadId, sourceThreadId);
672 assert.notInclude(recallPrompt, THREAD_MERGE_BACK_SIBLINGS_SOURCE_MARKER);
673 assert.notInclude(recallPrompt, THREAD_MERGE_BACK_SIBLINGS_FIRST_MARKER);
674 assert.notInclude(recallPrompt, THREAD_MERGE_BACK_SIBLINGS_SECOND_MARKER);
675 assert.equal(finalText, THREAD_MERGE_BACK_SIBLINGS_RECALL);
676}
677
678describe("Codex replay fixtures", () => {
679 it.effect("loads each canonical Codex fixture as an app-server replay transcript", () =>
680 Effect.gen(function* () {
681 for (const fixture of CODEX_REPLAY_TRANSCRIPTS) {
682 const transcript = yield* readTranscript(fixture.transcriptFile);
683 const codexTranscript = yield* decodeCodexTranscript(transcript);
684 const first = transcript.entries[0];
685
686 assert.equal(codexTranscript.provider, "codex");
687 assert.equal(codexTranscript.protocol, "codex.app-server");
688 assert.equal(codexTranscript.scenario, fixture.recordedScenario);
689 assert.deepEqual(codexTranscript.entries.at(-1), {
690 type: "runtime_exit",
691 status: "success",
692 });
693 assert.equal(first?.type, "expect_outbound");
694 if (first?.type !== "expect_outbound") {
695 throw new Error(
696 `Expected ${fixture.recordedScenario} to start with initialize outbound frame.`,
697 );
698 }
699 assert.equal(first.label, "initialize");
700
701 assertScenarioExpectations(transcript);
702 assertProviderThreadResumeSemantics(transcript);
703 assertContinuedForkSemantics(transcript);
704 assertSiblingForkSemantics(transcript);
705 assertMergeBackSemantics(transcript);
706 assertSiblingMergeBackSemantics(transcript);
707 }
708 }),
709 );
710
711 it("covers the expected replay suite exactly", () => {
712 assert.deepEqual(
713 CODEX_REPLAY_TRANSCRIPTS.map((fixture) => fixture.recordedScenario).toSorted(),
714 Object.keys(scenarioExpectations).toSorted(),
715 );
716 });
717
718 it("rejects conflicting recorded scenarios for one canonical transcript", () => {
719 const transcriptFile = new URL(
720 "./fixtures/queued_turn/codex_transcript.ndjson",
721 import.meta.url,
722 );
723
724 assert.throws(() =>
725 uniqueCanonicalTranscripts([
726 {
727 registrationScenario: "queued_turn",
728 recordedScenario: "queued_turn",
729 transcriptFile,
730 },
731 {
732 registrationScenario: "conflicting_alias",
733 recordedScenario: "different_recording",
734 transcriptFile,
735 },
736 ]),
737 );
738 });
739});