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

CodexReplayFixtures.integration.test.ts

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