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

orchestrationV2.test.ts

570 lines18.1 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as DateTime from "effect/DateTime";
3import * as Schema from "effect/Schema";
4​
5import {
6 CheckpointId,
7 CheckpointRef,
8 CheckpointScopeId,
9 ContextTransferId,
10 CommandId,
11 EventId,
12 MessageId,
13 NodeId,
14 ProjectId,
15 ProviderInstanceId,
16 ProviderReplayTranscript,
17 ProviderThreadId,
18 RunId,
19 ThreadId,
20 TurnItemId,
21} from "./index.ts";
22import {
23 OrchestrationV2Checkpoint,
24 OrchestrationV2CheckpointScope,
25 OrchestrationV2Command,
26 OrchestrationV2DomainEvent,
27 OrchestrationV2Subagent,
28 OrchestrationV2ThreadProjection,
29 OrchestrationV2TurnItem,
30} from "./orchestrationV2.ts";
31​
32const now = DateTime.makeUnsafe("2026-04-20T00:00:00.000Z");
33const decodeOrchestrationV2Command = Schema.decodeUnknownSync(OrchestrationV2Command);
34const decodeOrchestrationV2TurnItem = Schema.decodeUnknownSync(OrchestrationV2TurnItem);
35​
36describe("orchestration V2 contracts", () => {
37 it("decodes nested checkpoint scopes without making child scopes advance app run count", () => {
38 const rootScope = Schema.decodeUnknownSync(OrchestrationV2CheckpointScope)({
39 id: "scope-root-1",
40 threadId: "thread-1",
41 runId: "run-1",
42 nodeId: "node-root-1",
43 parentScopeId: null,
44 providerThreadId: "provider-thread-1",
45 kind: "root_run",
46 ordinalWithinParent: 1,
47 advancesAppRunCount: true,
48 cwd: "/tmp/project",
49 createdAt: now,
50 });
51 const childScope = Schema.decodeUnknownSync(OrchestrationV2CheckpointScope)({
52 id: "scope-child-1",
53 threadId: "thread-1",
54 runId: "run-1",
55 nodeId: "node-child-1",
56 parentScopeId: rootScope.id,
57 providerThreadId: "provider-thread-child-1",
58 kind: "subagent",
59 ordinalWithinParent: 1,
60 advancesAppRunCount: false,
61 cwd: "/tmp/project",
62 createdAt: now,
63 });
64​
65 expect(rootScope.advancesAppRunCount).toBe(true);
66 expect(childScope.parentScopeId).toBe(rootScope.id);
67 expect(childScope.advancesAppRunCount).toBe(false);
68 });
69​
70 it("decodes checkpoint captures that attach to scopes, nodes, and optional app run ordinals", () => {
71 const checkpoint = Schema.decodeUnknownSync(OrchestrationV2Checkpoint)({
72 id: "checkpoint-1",
73 threadId: "thread-1",
74 scopeId: "scope-child-1",
75 runId: "run-1",
76 nodeId: "node-child-1",
77 parentCheckpointId: "checkpoint-root-1",
78 ordinalWithinScope: 1,
79 appRunOrdinal: null,
80 ref: "git-ref-1",
81 status: "ready",
82 files: [{ path: "package.json", kind: "modified", additions: 2, deletions: 1 }],
83 capturedAt: now,
84 });
85​
86 expect(checkpoint.appRunOrdinal).toBeNull();
87 expect(checkpoint.scopeId).toBe(CheckpointScopeId.make("scope-child-1"));
88 expect(checkpoint.parentCheckpointId).toBe(CheckpointId.make("checkpoint-root-1"));
89 });
90​
91 it("decodes command and domain event shapes for command-to-projection tests", () => {
92 const command = decodeOrchestrationV2Command({
93 type: "message.dispatch",
94 createdBy: "user",
95 creationSource: "web",
96 commandId: "command-1",
97 threadId: "thread-1",
98 messageId: "message-1",
99 text: "hello",
100 attachments: [],
101 dispatchMode: { type: "start_immediately" },
102 });
103 const event = Schema.decodeUnknownSync(OrchestrationV2DomainEvent)({
104 id: "event-1",
105 type: "run.created",
106 threadId: "thread-1",
107 runId: "run-1",
108 occurredAt: now,
109 payload: {
110 id: "run-1",
111 threadId: "thread-1",
112 ordinal: 1,
113 providerInstanceId: "codex",
114 modelSelection: {
115 instanceId: "codex",
116 model: "gpt-5.4",
117 },
118 providerThreadId: "provider-thread-1",
119 userMessageId: "message-1",
120 rootNodeId: null,
121 activeAttemptId: null,
122 status: "queued",
123 requestedAt: now,
124 startedAt: null,
125 completedAt: null,
126 checkpointId: null,
127 contextHandoffId: null,
128 },
129 });
130​
131 expect(command.commandId).toBe(CommandId.make("command-1"));
132 expect(event.id).toBe(EventId.make("event-1"));
133 if (event.type !== "run.created") {
134 throw new Error(`Expected run.created, received ${event.type}.`);
135 }
136 expect(event.payload.id).toBe(RunId.make("run-1"));
137 });
138​
139 it("decodes app-owned delegated task commands", () => {
140 const command = decodeOrchestrationV2Command({
141 type: "delegated_task.request",
142 createdBy: "user",
143 creationSource: "web",
144 commandId: "command-delegate-1",
145 parentThreadId: "thread-parent-1",
146 parentRunId: "run-parent-1",
147 parentNodeId: "node-parent-1",
148 task: "Inspect the API boundary.",
149 title: "API inspection",
150 modelSelection: {
151 instanceId: "claudeAgent",
152 model: "claude-sonnet-4-6",
153 },
154 runtimeMode: "approval-required",
155 interactionMode: "plan",
156 });
157​
158 expect(command.type).toBe("delegated_task.request");
159 if (command.type !== "delegated_task.request") {
160 throw new Error("expected delegated_task.request");
161 }
162 expect(command.parentThreadId).toBe(ThreadId.make("thread-parent-1"));
163 expect(command.parentRunId).toBe(RunId.make("run-parent-1"));
164 expect(command.parentNodeId).toBe(NodeId.make("node-parent-1"));
165 });
166​
167 it("decodes durable created-thread timeline records", () => {
168 const command = decodeOrchestrationV2Command({
169 type: "thread.created.record",
170 commandId: "command-thread-record-1",
171 parentThreadId: "thread-parent-1",
172 parentRunId: "run-parent-1",
173 parentNodeId: "node-parent-1",
174 targetThreadId: "thread-child-1",
175 targetRunId: "run-child-1",
176 });
177 const item = decodeOrchestrationV2TurnItem({
178 id: "turn-item-thread-created-1",
179 type: "thread_created",
180 threadId: "thread-parent-1",
181 runId: "run-parent-1",
182 nodeId: "node-parent-1",
183 providerThreadId: "provider-thread-parent-1",
184 providerTurnId: null,
185 nativeItemRef: null,
186 parentItemId: null,
187 ordinal: 4,
188 status: "completed",
189 title: "Child thread",
190 targetThreadId: "thread-child-1",
191 targetRunId: "run-child-1",
192 targetProviderInstanceId: "claude-default",
193 targetModel: "claude-sonnet-4-6",
194 startedAt: now,
195 completedAt: now,
196 updatedAt: now,
197 });
198​
199 expect(command.type).toBe("thread.created.record");
200 if (command.type !== "thread.created.record") {
201 throw new Error("expected thread.created.record");
202 }
203 expect(command.targetThreadId).toBe(ThreadId.make("thread-child-1"));
204 expect(item.type).toBe("thread_created");
205 if (item.type !== "thread_created") {
206 throw new Error("expected thread_created");
207 }
208 expect(item.targetRunId).toBe(RunId.make("run-child-1"));
209 });
210​
211 it("decodes provider-neutral replay transcripts", () => {
212 const transcript = Schema.decodeUnknownSync(ProviderReplayTranscript)({
213 provider: "codex",
214 protocol: "codex.app-server",
215 version: "0.120.0",
216 scenario: "simple",
217 metadata: {
218 source: "real-probe",
219 },
220 entries: [
221 {
222 type: "expect_outbound",
223 label: "initialize",
224 frame: { id: 1, method: "initialize" },
225 },
226 {
227 type: "emit_inbound",
228 label: "initialize-result",
229 frame: { id: 1, result: { ok: true } },
230 },
231 {
232 type: "runtime_exit",
233 status: "success",
234 },
235 ],
236 });
237​
238 expect(transcript.entries).toHaveLength(3);
239 expect(transcript.protocol).toBe("codex.app-server");
240 });
241​
242 it("decodes strictly typed turn items for known tools and dynamic fallback tools", () => {
243 const fileChange = Schema.decodeUnknownSync(OrchestrationV2TurnItem)({
244 id: "turn-item-file-change-1",
245 type: "file_change",
246 threadId: "thread-1",
247 runId: "run-1",
248 nodeId: "node-file-change-1",
249 providerThreadId: "provider-thread-1",
250 providerTurnId: "provider-turn-1",
251 nativeItemRef: { driver: "codex", nativeId: "item-file-change-1", strength: "strong" },
252 parentItemId: null,
253 ordinal: 3,
254 status: "completed",
255 title: "Edited package.json",
256 fileName: "package.json",
257 additions: 4,
258 deletions: 2,
259 diffStr: "@@ fixture diff",
260 startedAt: now,
261 completedAt: now,
262 updatedAt: now,
263 });
264 const dynamicTool = Schema.decodeUnknownSync(OrchestrationV2TurnItem)({
265 id: "turn-item-dynamic-1",
266 type: "dynamic_tool",
267 threadId: "thread-1",
268 runId: "run-1",
269 nodeId: "node-dynamic-1",
270 providerThreadId: "provider-thread-1",
271 providerTurnId: "provider-turn-1",
272 nativeItemRef: { driver: "codex", nativeId: "item-dynamic-1", strength: "strong" },
273 parentItemId: null,
274 ordinal: 4,
275 status: "completed",
276 title: "Custom tool",
277 toolName: "custom.lookup",
278 input: { query: "fixture" },
279 output: { ok: true },
280 startedAt: now,
281 completedAt: now,
282 updatedAt: now,
283 });
284​
285 expect(fileChange.type).toBe("file_change");
286 if (fileChange.type !== "file_change") {
287 throw new Error("expected file_change");
288 }
289 expect(fileChange.fileName).toBe("package.json");
290 expect(fileChange.additions).toBe(4);
291 expect(dynamicTool.id).toBe(TurnItemId.make("turn-item-dynamic-1"));
292 });
293​
294 it("decodes bounded provider failures as expected error turn items", () => {
295 const errorItem = Schema.decodeUnknownSync(OrchestrationV2TurnItem)({
296 id: "turn-item-error-1",
297 type: "error",
298 threadId: "thread-1",
299 runId: "run-1",
300 nodeId: "node-root-1",
301 providerThreadId: "provider-thread-1",
302 providerTurnId: "provider-turn-1",
303 nativeItemRef: null,
304 parentItemId: null,
305 ordinal: 199,
306 status: "failed",
307 title: "Provider error",
308 failure: {
309 class: "validation_error",
310 message: "Invalid reasoning effort.",
311 code: "invalid_request",
312 retryable: false,
313 },
314 startedAt: now,
315 completedAt: now,
316 updatedAt: now,
317 });
318​
319 expect(errorItem.type).toBe("error");
320 if (errorItem.type !== "error") throw new Error("expected error item");
321 expect(errorItem.failure.message).toBe("Invalid reasoning effort.");
322 expect(() =>
323 Schema.decodeUnknownSync(OrchestrationV2TurnItem)({
324 ...errorItem,
325 failure: { ...errorItem.failure, message: "x".repeat(4_097) },
326 }),
327 ).toThrow();
328 });
329​
330 it("decodes provider-native subagent lifecycle records and timeline items", () => {
331 const subagent = Schema.decodeUnknownSync(OrchestrationV2Subagent)({
332 id: "node-subagent-1",
333 threadId: "thread-1",
334 runId: "run-1",
335 parentNodeId: "node-root-1",
336 origin: "provider_native",
337 createdBy: "agent",
338 driver: "codex",
339 providerInstanceId: "codex",
340 providerThreadId: "provider-thread-subagent-1",
341 childThreadId: null,
342 nativeTaskRef: {
343 driver: "codex",
344 nativeId: "native-task-1",
345 strength: "strong",
346 },
347 prompt: "Inspect package.json",
348 title: "Package audit",
349 model: "gpt-5.4",
350 status: "completed",
351 progress: "Inspecting package metadata",
352 result: "Package is private.",
353 startedAt: now,
354 completedAt: now,
355 updatedAt: now,
356 });
357 const turnItem = Schema.decodeUnknownSync(OrchestrationV2TurnItem)({
358 id: "turn-item-subagent-1",
359 type: "subagent",
360 threadId: "thread-1",
361 runId: "run-1",
362 nodeId: subagent.id,
363 providerThreadId: subagent.providerThreadId,
364 providerTurnId: "provider-turn-1",
365 nativeItemRef: subagent.nativeTaskRef,
366 parentItemId: null,
367 ordinal: 2,
368 status: "completed",
369 title: subagent.title,
370 subagentId: subagent.id,
371 origin: subagent.origin,
372 driver: subagent.driver,
373 providerInstanceId: subagent.providerInstanceId,
374 childThreadId: subagent.childThreadId,
375 prompt: subagent.prompt,
376 progress: subagent.progress,
377 result: subagent.result,
378 startedAt: now,
379 completedAt: now,
380 updatedAt: now,
381 });
382​
383 expect(subagent.origin).toBe("provider_native");
384 expect(subagent.progress).toBe("Inspecting package metadata");
385 expect(subagent.childThreadId).toBeNull();
386 expect(turnItem.type).toBe("subagent");
387 if (turnItem.type !== "subagent") throw new Error("expected subagent item");
388 expect(turnItem.progress).toBe("Inspecting package metadata");
389 });
390​
391 it("decodes thread projections with an ordered turn item rendering stream", () => {
392 const projection = Schema.decodeUnknownSync(OrchestrationV2ThreadProjection)({
393 thread: {
394 createdBy: "user",
395 creationSource: "web",
396 id: "thread-1",
397 projectId: "project-1",
398 title: "Thread",
399 providerInstanceId: "codex",
400 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5-codex" },
401 runtimeMode: "full-access",
402 interactionMode: "default",
403 branch: null,
404 worktreePath: null,
405 activeProviderThreadId: null,
406 lineage: {
407 parentThreadId: null,
408 relationshipToParent: null,
409 rootThreadId: "thread-1",
410 },
411 forkedFrom: null,
412 createdAt: now,
413 updatedAt: now,
414 archivedAt: null,
415 deletedAt: null,
416 },
417 runs: [],
418 attempts: [],
419 nodes: [],
420 subagents: [],
421 providerSessions: [],
422 providerThreads: [],
423 providerTurns: [],
424 runtimeRequests: [],
425 messages: [],
426 plans: [],
427 turnItems: [
428 {
429 id: "turn-item-command-1",
430 type: "command_execution",
431 threadId: "thread-1",
432 runId: "run-1",
433 nodeId: "node-command-1",
434 providerThreadId: "provider-thread-1",
435 providerTurnId: "provider-turn-1",
436 nativeItemRef: { driver: "codex", nativeId: "item-command-1", strength: "strong" },
437 parentItemId: null,
438 ordinal: 1,
439 status: "completed",
440 title: "Ran command",
441 input: "bun typecheck",
442 output: "Tasks: 10 successful",
443 exitCode: 0,
444 startedAt: now,
445 completedAt: now,
446 updatedAt: now,
447 },
448 ],
449 visibleTurnItems: [
450 {
451 position: 0,
452 visibility: "local",
453 sourceThreadId: "thread-1",
454 sourceItemId: "turn-item-command-1",
455 item: {
456 id: "turn-item-command-1",
457 type: "command_execution",
458 threadId: "thread-1",
459 runId: "run-1",
460 nodeId: "node-command-1",
461 providerThreadId: "provider-thread-1",
462 providerTurnId: "provider-turn-1",
463 nativeItemRef: { driver: "codex", nativeId: "item-command-1", strength: "strong" },
464 parentItemId: null,
465 ordinal: 1,
466 status: "completed",
467 title: "Ran command",
468 input: "bun typecheck",
469 output: "Tasks: 10 successful",
470 exitCode: 0,
471 startedAt: now,
472 completedAt: now,
473 updatedAt: now,
474 },
475 },
476 ],
477 checkpointScopes: [],
478 checkpoints: [],
479 contextHandoffs: [],
480 contextTransfers: [],
481 updatedAt: now,
482 });
483​
484 expect(projection.turnItems.map((item) => item.type)).toEqual(["command_execution"]);
485 });
486​
487 it("decodes orchestration lifecycle turn items for compaction, handoff, and fork UI", () => {
488 const compaction = Schema.decodeUnknownSync(OrchestrationV2TurnItem)({
489 id: "turn-item-compaction-1",
490 type: "compaction",
491 threadId: "thread-1",
492 runId: "run-1",
493 nodeId: "node-compaction-1",
494 providerThreadId: "provider-thread-1",
495 providerTurnId: "provider-turn-1",
496 nativeItemRef: null,
497 parentItemId: null,
498 ordinal: 5,
499 status: "running",
500 title: "Compacting context...",
501 driver: "codex",
502 beforeTokenCount: 180000,
503 startedAt: now,
504 completedAt: null,
505 updatedAt: now,
506 });
507 const handoff = Schema.decodeUnknownSync(OrchestrationV2TurnItem)({
508 id: "turn-item-handoff-1",
509 type: "handoff",
510 threadId: "thread-1",
511 runId: "run-2",
512 nodeId: null,
513 providerThreadId: "provider-thread-claude-1",
514 providerTurnId: null,
515 nativeItemRef: null,
516 parentItemId: null,
517 ordinal: 6,
518 status: "completed",
519 title: "Handed off to Claude",
520 contextHandoffId: "handoff-1",
521 fromProviderThreadIds: ["provider-thread-codex-1"],
522 toProviderThreadId: "provider-thread-claude-1",
523 fromProviderInstanceIds: ["codex"],
524 toProviderInstanceId: "claudeAgent",
525 strategy: "delta_since_target_last_seen",
526 summary: "Codex completed the setup work.",
527 startedAt: now,
528 completedAt: now,
529 updatedAt: now,
530 });
531 const fork = Schema.decodeUnknownSync(OrchestrationV2TurnItem)({
532 id: "turn-item-fork-1",
533 type: "fork",
534 threadId: "thread-1",
535 runId: "run-2",
536 nodeId: "node-subagent-1",
537 providerThreadId: "provider-thread-child-1",
538 providerTurnId: null,
539 nativeItemRef: null,
540 parentItemId: null,
541 ordinal: 7,
542 status: "completed",
543 title: "Forked subagent thread",
544 source: { type: "node", nodeId: "node-subagent-1" },
545 targetThreadId: "thread-fork-1",
546 startedAt: now,
547 completedAt: now,
548 updatedAt: now,
549 });
550​
551 expect(compaction.type).toBe("compaction");
552 expect(compaction.status).toBe("running");
553 expect(handoff.type).toBe("handoff");
554 if (handoff.type !== "handoff") {
555 throw new Error("expected handoff");
556 }
557 expect(handoff.toProviderInstanceId).toBe("claudeAgent");
558 expect(fork.type).toBe("fork");
559 });
560​
561 it("exports the V2 branded ids through the public contracts entrypoint", () => {
562 expect(ThreadId.make("thread-1")).toBe("thread-1");
563 expect(ProjectId.make("project-1")).toBe("project-1");
564 expect(MessageId.make("message-1")).toBe("message-1");
565 expect(NodeId.make("node-1")).toBe("node-1");
566 expect(ProviderThreadId.make("provider-thread-1")).toBe("provider-thread-1");
567 expect(CheckpointRef.make("git-ref-1")).toBe("git-ref-1");
568 expect(ContextTransferId.make("context-transfer-1")).toBe("context-transfer-1");
569 });
570});