Open History to see the last commit
1import {
2 CommandId,
3 EventId,
4 ProjectId,
5 ThreadId,
6 type OrchestrationEvent,
7} from "@t3tools/contracts";
8import { Effect } from "effect";
9import { describe, expect, it } from "vitest";
10
11import { createEmptyReadModel, projectEvent } from "./projector.ts";
12
13function makeEvent(input: {
14 sequence: number;
15 type: OrchestrationEvent["type"];
16 occurredAt: string;
17 aggregateKind: OrchestrationEvent["aggregateKind"];
18 aggregateId: string;
19 commandId: string | null;
20 payload: unknown;
21}): OrchestrationEvent {
22 return {
23 sequence: input.sequence,
24 eventId: EventId.makeUnsafe(`event-${input.sequence}`),
25 type: input.type,
26 aggregateKind: input.aggregateKind,
27 aggregateId:
28 input.aggregateKind === "project"
29 ? ProjectId.makeUnsafe(input.aggregateId)
30 : ThreadId.makeUnsafe(input.aggregateId),
31 occurredAt: input.occurredAt,
32 commandId: input.commandId === null ? null : CommandId.makeUnsafe(input.commandId),
33 causationEventId: null,
34 correlationId: null,
35 metadata: {},
36 payload: input.payload as never,
37 } as OrchestrationEvent;
38}
39
40describe("orchestration projector", () => {
41 it("applies thread.created events", async () => {
42 const now = new Date().toISOString();
43 const model = createEmptyReadModel(now);
44
45 const next = await Effect.runPromise(
46 projectEvent(
47 model,
48 makeEvent({
49 sequence: 1,
50 type: "thread.created",
51 aggregateKind: "thread",
52 aggregateId: "thread-1",
53 occurredAt: now,
54 commandId: "cmd-thread-create",
55 payload: {
56 threadId: "thread-1",
57 projectId: "project-1",
58 title: "demo",
59 model: "gpt-5-codex",
60 runtimeMode: "full-access",
61 branch: null,
62 worktreePath: null,
63 createdAt: now,
64 updatedAt: now,
65 },
66 }),
67 ),
68 );
69
70 expect(next.snapshotSequence).toBe(1);
71 expect(next.threads).toEqual([
72 {
73 id: "thread-1",
74 projectId: "project-1",
75 title: "demo",
76 model: "gpt-5-codex",
77 runtimeMode: "full-access",
78 interactionMode: "default",
79 branch: null,
80 worktreePath: null,
81 latestTurn: null,
82 createdAt: now,
83 updatedAt: now,
84 deletedAt: null,
85 messages: [],
86 proposedPlans: [],
87 activities: [],
88 workUnits: [],
89 checkpoints: [],
90 session: null,
91 },
92 ]);
93 });
94
95 it("fails when event payload cannot be decoded by runtime schema", async () => {
96 const now = new Date().toISOString();
97 const model = createEmptyReadModel(now);
98
99 await expect(
100 Effect.runPromise(
101 projectEvent(
102 model,
103 makeEvent({
104 sequence: 1,
105 type: "thread.created",
106 aggregateKind: "thread",
107 aggregateId: "thread-1",
108 occurredAt: now,
109 commandId: "cmd-invalid",
110 payload: {
111 // missing required threadId
112 projectId: "project-1",
113 title: "demo",
114 model: "gpt-5-codex",
115 branch: null,
116 worktreePath: null,
117 createdAt: now,
118 updatedAt: now,
119 },
120 }),
121 ),
122 ),
123 ).rejects.toBeDefined();
124 });
125
126 it("applies thread.work-unit-upserted events", async () => {
127 const now = new Date().toISOString();
128 const model = createEmptyReadModel(now);
129
130 const afterCreate = await Effect.runPromise(
131 projectEvent(
132 model,
133 makeEvent({
134 sequence: 1,
135 type: "thread.created",
136 aggregateKind: "thread",
137 aggregateId: "thread-1",
138 occurredAt: now,
139 commandId: "cmd-thread-create",
140 payload: {
141 threadId: "thread-1",
142 projectId: "project-1",
143 title: "demo",
144 model: "gpt-5-codex",
145 runtimeMode: "full-access",
146 branch: null,
147 worktreePath: null,
148 createdAt: now,
149 updatedAt: now,
150 },
151 }),
152 ),
153 );
154
155 const next = await Effect.runPromise(
156 projectEvent(
157 afterCreate,
158 makeEvent({
159 sequence: 2,
160 type: "thread.work-unit-upserted",
161 aggregateKind: "thread",
162 aggregateId: "thread-1",
163 occurredAt: now,
164 commandId: "cmd-work-unit-upsert",
165 payload: {
166 threadId: "thread-1",
167 workUnit: {
168 id: "wu:thread-1:turn:turn-1:root",
169 turnId: "turn-1",
170 parentWorkUnitId: null,
171 kind: "primary_agent",
172 state: "running",
173 title: "Primary agent",
174 detail: null,
175 spawnedByActivityId: null,
176 providerRefs: {
177 providerTurnId: "provider-turn-1",
178 },
179 startedAt: now,
180 updatedAt: now,
181 completedAt: null,
182 },
183 },
184 }),
185 ),
186 );
187
188 expect(next.threads[0]?.workUnits).toEqual([
189 {
190 id: "wu:thread-1:turn:turn-1:root",
191 turnId: "turn-1",
192 parentWorkUnitId: null,
193 kind: "primary_agent",
194 state: "running",
195 title: "Primary agent",
196 detail: null,
197 spawnedByActivityId: null,
198 providerRefs: {
199 providerTurnId: "provider-turn-1",
200 },
201 startedAt: now,
202 updatedAt: now,
203 completedAt: null,
204 },
205 ]);
206 });
207
208 it("keeps projector forward-compatible for unhandled event types", async () => {
209 const now = new Date().toISOString();
210 const model = createEmptyReadModel(now);
211
212 const next = await Effect.runPromise(
213 projectEvent(
214 model,
215 makeEvent({
216 sequence: 7,
217 type: "thread.turn-start-requested",
218 aggregateKind: "thread",
219 aggregateId: "thread-1",
220 occurredAt: "2026-01-01T00:00:00.000Z",
221 commandId: "cmd-unhandled",
222 payload: {
223 threadId: "thread-1",
224 messageId: "message-1",
225 runtimeMode: "approval-required",
226 createdAt: "2026-01-01T00:00:00.000Z",
227 },
228 }),
229 ),
230 );
231
232 expect(next.snapshotSequence).toBe(7);
233 expect(next.updatedAt).toBe("2026-01-01T00:00:00.000Z");
234 expect(next.threads).toEqual([]);
235 });
236
237 it("tracks latest turn id from session lifecycle events", async () => {
238 const createdAt = "2026-02-23T08:00:00.000Z";
239 const startedAt = "2026-02-23T08:00:05.000Z";
240 const model = createEmptyReadModel(createdAt);
241
242 const afterCreate = await Effect.runPromise(
243 projectEvent(
244 model,
245 makeEvent({
246 sequence: 1,
247 type: "thread.created",
248 aggregateKind: "thread",
249 aggregateId: "thread-1",
250 occurredAt: createdAt,
251 commandId: "cmd-create",
252 payload: {
253 threadId: "thread-1",
254 projectId: "project-1",
255 title: "demo",
256 model: "gpt-5.3-codex",
257 runtimeMode: "full-access",
258 branch: null,
259 worktreePath: null,
260 createdAt,
261 updatedAt: createdAt,
262 },
263 }),
264 ),
265 );
266
267 const afterRunning = await Effect.runPromise(
268 projectEvent(
269 afterCreate,
270 makeEvent({
271 sequence: 2,
272 type: "thread.session-set",
273 aggregateKind: "thread",
274 aggregateId: "thread-1",
275 occurredAt: startedAt,
276 commandId: "cmd-running",
277 payload: {
278 threadId: "thread-1",
279 session: {
280 threadId: "thread-1",
281 status: "running",
282 providerName: "codex",
283 providerSessionId: "session-1",
284 providerThreadId: "provider-thread-1",
285 runtimeMode: "approval-required",
286 activeTurnId: "turn-1",
287 lastError: null,
288 updatedAt: startedAt,
289 },
290 },
291 }),
292 ),
293 );
294
295 const thread = afterRunning.threads[0];
296 expect(thread?.latestTurn?.turnId).toBe("turn-1");
297 expect(thread?.session?.status).toBe("running");
298 });
299
300 it("updates canonical thread runtime mode from thread.runtime-mode-set", async () => {
301 const createdAt = "2026-02-23T08:00:00.000Z";
302 const updatedAt = "2026-02-23T08:00:05.000Z";
303 const model = createEmptyReadModel(createdAt);
304
305 const afterCreate = await Effect.runPromise(
306 projectEvent(
307 model,
308 makeEvent({
309 sequence: 1,
310 type: "thread.created",
311 aggregateKind: "thread",
312 aggregateId: "thread-1",
313 occurredAt: createdAt,
314 commandId: "cmd-create",
315 payload: {
316 threadId: "thread-1",
317 projectId: "project-1",
318 title: "demo",
319 model: "gpt-5.3-codex",
320 runtimeMode: "full-access",
321 branch: null,
322 worktreePath: null,
323 createdAt,
324 updatedAt: createdAt,
325 },
326 }),
327 ),
328 );
329
330 const afterUpdate = await Effect.runPromise(
331 projectEvent(
332 afterCreate,
333 makeEvent({
334 sequence: 2,
335 type: "thread.runtime-mode-set",
336 aggregateKind: "thread",
337 aggregateId: "thread-1",
338 occurredAt: updatedAt,
339 commandId: "cmd-runtime-mode-set",
340 payload: {
341 threadId: "thread-1",
342 runtimeMode: "approval-required",
343 updatedAt,
344 },
345 }),
346 ),
347 );
348
349 expect(afterUpdate.threads[0]?.runtimeMode).toBe("approval-required");
350 expect(afterUpdate.threads[0]?.updatedAt).toBe(updatedAt);
351 });
352
353 it("marks assistant messages completed with non-streaming updates", async () => {
354 const createdAt = "2026-02-23T09:00:00.000Z";
355 const deltaAt = "2026-02-23T09:00:01.000Z";
356 const completeAt = "2026-02-23T09:00:03.500Z";
357 const model = createEmptyReadModel(createdAt);
358
359 const afterCreate = await Effect.runPromise(
360 projectEvent(
361 model,
362 makeEvent({
363 sequence: 1,
364 type: "thread.created",
365 aggregateKind: "thread",
366 aggregateId: "thread-1",
367 occurredAt: createdAt,
368 commandId: "cmd-create",
369 payload: {
370 threadId: "thread-1",
371 projectId: "project-1",
372 title: "demo",
373 model: "gpt-5.3-codex",
374 runtimeMode: "full-access",
375 branch: null,
376 worktreePath: null,
377 createdAt,
378 updatedAt: createdAt,
379 },
380 }),
381 ),
382 );
383
384 const afterDelta = await Effect.runPromise(
385 projectEvent(
386 afterCreate,
387 makeEvent({
388 sequence: 2,
389 type: "thread.message-sent",
390 aggregateKind: "thread",
391 aggregateId: "thread-1",
392 occurredAt: deltaAt,
393 commandId: "cmd-delta",
394 payload: {
395 threadId: "thread-1",
396 messageId: "assistant:msg-1",
397 role: "assistant",
398 text: "hello",
399 turnId: "turn-1",
400 streaming: true,
401 createdAt: deltaAt,
402 updatedAt: deltaAt,
403 },
404 }),
405 ),
406 );
407
408 const afterComplete = await Effect.runPromise(
409 projectEvent(
410 afterDelta,
411 makeEvent({
412 sequence: 3,
413 type: "thread.message-sent",
414 aggregateKind: "thread",
415 aggregateId: "thread-1",
416 occurredAt: completeAt,
417 commandId: "cmd-complete",
418 payload: {
419 threadId: "thread-1",
420 messageId: "assistant:msg-1",
421 role: "assistant",
422 text: "",
423 turnId: "turn-1",
424 streaming: false,
425 createdAt: completeAt,
426 updatedAt: completeAt,
427 },
428 }),
429 ),
430 );
431
432 const message = afterComplete.threads[0]?.messages[0];
433 expect(message?.id).toBe("assistant:msg-1");
434 expect(message?.text).toBe("hello");
435 expect(message?.streaming).toBe(false);
436 expect(message?.updatedAt).toBe(completeAt);
437 });
438
439 it("prunes reverted turn messages from in-memory thread snapshot", async () => {
440 const createdAt = "2026-02-23T10:00:00.000Z";
441 const model = createEmptyReadModel(createdAt);
442
443 const afterCreate = await Effect.runPromise(
444 projectEvent(
445 model,
446 makeEvent({
447 sequence: 1,
448 type: "thread.created",
449 aggregateKind: "thread",
450 aggregateId: "thread-1",
451 occurredAt: createdAt,
452 commandId: "cmd-create",
453 payload: {
454 threadId: "thread-1",
455 projectId: "project-1",
456 title: "demo",
457 model: "gpt-5.3-codex",
458 runtimeMode: "full-access",
459 branch: null,
460 worktreePath: null,
461 createdAt,
462 updatedAt: createdAt,
463 },
464 }),
465 ),
466 );
467
468 const events: ReadonlyArray<OrchestrationEvent> = [
469 makeEvent({
470 sequence: 2,
471 type: "thread.message-sent",
472 aggregateKind: "thread",
473 aggregateId: "thread-1",
474 occurredAt: "2026-02-23T10:00:01.000Z",
475 commandId: "cmd-user-1",
476 payload: {
477 threadId: "thread-1",
478 messageId: "user-msg-1",
479 role: "user",
480 text: "First edit",
481 turnId: null,
482 streaming: false,
483 createdAt: "2026-02-23T10:00:01.000Z",
484 updatedAt: "2026-02-23T10:00:01.000Z",
485 },
486 }),
487 makeEvent({
488 sequence: 3,
489 type: "thread.message-sent",
490 aggregateKind: "thread",
491 aggregateId: "thread-1",
492 occurredAt: "2026-02-23T10:00:02.000Z",
493 commandId: "cmd-assistant-1",
494 payload: {
495 threadId: "thread-1",
496 messageId: "assistant-msg-1",
497 role: "assistant",
498 text: "Updated README to v2.\n",
499 turnId: "turn-1",
500 streaming: false,
501 createdAt: "2026-02-23T10:00:02.000Z",
502 updatedAt: "2026-02-23T10:00:02.000Z",
503 },
504 }),
505 makeEvent({
506 sequence: 4,
507 type: "thread.turn-diff-completed",
508 aggregateKind: "thread",
509 aggregateId: "thread-1",
510 occurredAt: "2026-02-23T10:00:02.500Z",
511 commandId: "cmd-turn-1-complete",
512 payload: {
513 threadId: "thread-1",
514 turnId: "turn-1",
515 checkpointTurnCount: 1,
516 checkpointRef: "refs/t3/checkpoints/thread-1/turn/1",
517 status: "ready",
518 files: [],
519 assistantMessageId: "assistant-msg-1",
520 completedAt: "2026-02-23T10:00:02.500Z",
521 },
522 }),
523 makeEvent({
524 sequence: 5,
525 type: "thread.activity-appended",
526 aggregateKind: "thread",
527 aggregateId: "thread-1",
528 occurredAt: "2026-02-23T10:00:02.750Z",
529 commandId: "cmd-activity-1",
530 payload: {
531 threadId: "thread-1",
532 activity: {
533 id: "activity-1",
534 tone: "tool",
535 kind: "tool.started",
536 summary: "Edit file started",
537 payload: { toolKind: "command" },
538 turnId: "turn-1",
539 workUnitId: null,
540 createdAt: "2026-02-23T10:00:02.750Z",
541 },
542 },
543 }),
544 makeEvent({
545 sequence: 6,
546 type: "thread.message-sent",
547 aggregateKind: "thread",
548 aggregateId: "thread-1",
549 occurredAt: "2026-02-23T10:00:03.000Z",
550 commandId: "cmd-user-2",
551 payload: {
552 threadId: "thread-1",
553 messageId: "user-msg-2",
554 role: "user",
555 text: "Second edit",
556 turnId: null,
557 streaming: false,
558 createdAt: "2026-02-23T10:00:03.000Z",
559 updatedAt: "2026-02-23T10:00:03.000Z",
560 },
561 }),
562 makeEvent({
563 sequence: 7,
564 type: "thread.message-sent",
565 aggregateKind: "thread",
566 aggregateId: "thread-1",
567 occurredAt: "2026-02-23T10:00:04.000Z",
568 commandId: "cmd-assistant-2",
569 payload: {
570 threadId: "thread-1",
571 messageId: "assistant-msg-2",
572 role: "assistant",
573 text: "Updated README to v3.\n",
574 turnId: "turn-2",
575 streaming: false,
576 createdAt: "2026-02-23T10:00:04.000Z",
577 updatedAt: "2026-02-23T10:00:04.000Z",
578 },
579 }),
580 makeEvent({
581 sequence: 8,
582 type: "thread.turn-diff-completed",
583 aggregateKind: "thread",
584 aggregateId: "thread-1",
585 occurredAt: "2026-02-23T10:00:04.500Z",
586 commandId: "cmd-turn-2-complete",
587 payload: {
588 threadId: "thread-1",
589 turnId: "turn-2",
590 checkpointTurnCount: 2,
591 checkpointRef: "refs/t3/checkpoints/thread-1/turn/2",
592 status: "ready",
593 files: [],
594 assistantMessageId: "assistant-msg-2",
595 completedAt: "2026-02-23T10:00:04.500Z",
596 },
597 }),
598 makeEvent({
599 sequence: 9,
600 type: "thread.activity-appended",
601 aggregateKind: "thread",
602 aggregateId: "thread-1",
603 occurredAt: "2026-02-23T10:00:04.750Z",
604 commandId: "cmd-activity-2",
605 payload: {
606 threadId: "thread-1",
607 activity: {
608 id: "activity-2",
609 tone: "tool",
610 kind: "tool.completed",
611 summary: "Edit file complete",
612 payload: { toolKind: "command" },
613 turnId: "turn-2",
614 workUnitId: null,
615 createdAt: "2026-02-23T10:00:04.750Z",
616 },
617 },
618 }),
619 makeEvent({
620 sequence: 10,
621 type: "thread.reverted",
622 aggregateKind: "thread",
623 aggregateId: "thread-1",
624 occurredAt: "2026-02-23T10:00:05.000Z",
625 commandId: "cmd-revert",
626 payload: {
627 threadId: "thread-1",
628 turnCount: 1,
629 },
630 }),
631 ];
632
633 const afterRevert = await events.reduce<Promise<ReturnType<typeof createEmptyReadModel>>>(
634 (statePromise, event) =>
635 statePromise.then((state) => Effect.runPromise(projectEvent(state, event))),
636 Promise.resolve(afterCreate),
637 );
638
639 const thread = afterRevert.threads[0];
640 expect(thread?.messages.map((message) => ({ role: message.role, text: message.text }))).toEqual(
641 [
642 { role: "user", text: "First edit" },
643 { role: "assistant", text: "Updated README to v2.\n" },
644 ],
645 );
646 expect(
647 thread?.activities.map((activity) => ({ id: activity.id, turnId: activity.turnId })),
648 ).toEqual([{ id: "activity-1", turnId: "turn-1" }]);
649 expect(thread?.checkpoints.map((checkpoint) => checkpoint.checkpointTurnCount)).toEqual([1]);
650 expect(thread?.latestTurn?.turnId).toBe("turn-1");
651 });
652
653 it("does not fallback-retain messages tied to removed turn IDs", async () => {
654 const createdAt = "2026-02-26T12:00:00.000Z";
655 const model = createEmptyReadModel(createdAt);
656
657 const afterCreate = await Effect.runPromise(
658 projectEvent(
659 model,
660 makeEvent({
661 sequence: 1,
662 type: "thread.created",
663 aggregateKind: "thread",
664 aggregateId: "thread-revert",
665 occurredAt: createdAt,
666 commandId: "cmd-create-revert",
667 payload: {
668 threadId: "thread-revert",
669 projectId: "project-1",
670 title: "demo",
671 model: "gpt-5.3-codex",
672 runtimeMode: "full-access",
673 branch: null,
674 worktreePath: null,
675 createdAt,
676 updatedAt: createdAt,
677 },
678 }),
679 ),
680 );
681
682 const events: ReadonlyArray<OrchestrationEvent> = [
683 makeEvent({
684 sequence: 2,
685 type: "thread.turn-diff-completed",
686 aggregateKind: "thread",
687 aggregateId: "thread-revert",
688 occurredAt: "2026-02-26T12:00:01.000Z",
689 commandId: "cmd-turn-1",
690 payload: {
691 threadId: "thread-revert",
692 turnId: "turn-1",
693 checkpointTurnCount: 1,
694 checkpointRef: "refs/t3/checkpoints/thread-revert/turn/1",
695 status: "ready",
696 files: [],
697 assistantMessageId: "assistant-keep",
698 completedAt: "2026-02-26T12:00:01.000Z",
699 },
700 }),
701 makeEvent({
702 sequence: 3,
703 type: "thread.message-sent",
704 aggregateKind: "thread",
705 aggregateId: "thread-revert",
706 occurredAt: "2026-02-26T12:00:01.100Z",
707 commandId: "cmd-assistant-keep",
708 payload: {
709 threadId: "thread-revert",
710 messageId: "assistant-keep",
711 role: "assistant",
712 text: "kept",
713 turnId: "turn-1",
714 streaming: false,
715 createdAt: "2026-02-26T12:00:01.100Z",
716 updatedAt: "2026-02-26T12:00:01.100Z",
717 },
718 }),
719 makeEvent({
720 sequence: 4,
721 type: "thread.turn-diff-completed",
722 aggregateKind: "thread",
723 aggregateId: "thread-revert",
724 occurredAt: "2026-02-26T12:00:02.000Z",
725 commandId: "cmd-turn-2",
726 payload: {
727 threadId: "thread-revert",
728 turnId: "turn-2",
729 checkpointTurnCount: 2,
730 checkpointRef: "refs/t3/checkpoints/thread-revert/turn/2",
731 status: "ready",
732 files: [],
733 assistantMessageId: "assistant-remove",
734 completedAt: "2026-02-26T12:00:02.000Z",
735 },
736 }),
737 makeEvent({
738 sequence: 5,
739 type: "thread.message-sent",
740 aggregateKind: "thread",
741 aggregateId: "thread-revert",
742 occurredAt: "2026-02-26T12:00:02.050Z",
743 commandId: "cmd-user-remove",
744 payload: {
745 threadId: "thread-revert",
746 messageId: "user-remove",
747 role: "user",
748 text: "removed",
749 turnId: "turn-2",
750 streaming: false,
751 createdAt: "2026-02-26T12:00:02.050Z",
752 updatedAt: "2026-02-26T12:00:02.050Z",
753 },
754 }),
755 makeEvent({
756 sequence: 6,
757 type: "thread.message-sent",
758 aggregateKind: "thread",
759 aggregateId: "thread-revert",
760 occurredAt: "2026-02-26T12:00:02.100Z",
761 commandId: "cmd-assistant-remove",
762 payload: {
763 threadId: "thread-revert",
764 messageId: "assistant-remove",
765 role: "assistant",
766 text: "removed",
767 turnId: "turn-2",
768 streaming: false,
769 createdAt: "2026-02-26T12:00:02.100Z",
770 updatedAt: "2026-02-26T12:00:02.100Z",
771 },
772 }),
773 makeEvent({
774 sequence: 7,
775 type: "thread.reverted",
776 aggregateKind: "thread",
777 aggregateId: "thread-revert",
778 occurredAt: "2026-02-26T12:00:03.000Z",
779 commandId: "cmd-revert",
780 payload: {
781 threadId: "thread-revert",
782 turnCount: 1,
783 },
784 }),
785 ];
786
787 const afterRevert = await events.reduce<Promise<ReturnType<typeof createEmptyReadModel>>>(
788 (statePromise, event) =>
789 statePromise.then((state) => Effect.runPromise(projectEvent(state, event))),
790 Promise.resolve(afterCreate),
791 );
792
793 const thread = afterRevert.threads[0];
794 expect(
795 thread?.messages.map((message) => ({
796 id: message.id,
797 role: message.role,
798 turnId: message.turnId,
799 })),
800 ).toEqual([{ id: "assistant-keep", role: "assistant", turnId: "turn-1" }]);
801 });
802
803 it("caps message and checkpoint retention for long-lived threads", async () => {
804 const createdAt = "2026-03-01T10:00:00.000Z";
805 const model = createEmptyReadModel(createdAt);
806
807 const afterCreate = await Effect.runPromise(
808 projectEvent(
809 model,
810 makeEvent({
811 sequence: 1,
812 type: "thread.created",
813 aggregateKind: "thread",
814 aggregateId: "thread-capped",
815 occurredAt: createdAt,
816 commandId: "cmd-create-capped",
817 payload: {
818 threadId: "thread-capped",
819 projectId: "project-1",
820 title: "capped",
821 model: "gpt-5-codex",
822 runtimeMode: "full-access",
823 branch: null,
824 worktreePath: null,
825 createdAt,
826 updatedAt: createdAt,
827 },
828 }),
829 ),
830 );
831
832 const messageEvents: ReadonlyArray<OrchestrationEvent> = Array.from(
833 { length: 2_100 },
834 (_, index) =>
835 makeEvent({
836 sequence: index + 2,
837 type: "thread.message-sent",
838 aggregateKind: "thread",
839 aggregateId: "thread-capped",
840 occurredAt: `2026-03-01T10:00:${String(index % 60).padStart(2, "0")}.000Z`,
841 commandId: `cmd-message-${index}`,
842 payload: {
843 threadId: "thread-capped",
844 messageId: `msg-${index}`,
845 role: "assistant",
846 text: `message-${index}`,
847 turnId: `turn-${index}`,
848 streaming: false,
849 createdAt: `2026-03-01T10:00:${String(index % 60).padStart(2, "0")}.000Z`,
850 updatedAt: `2026-03-01T10:00:${String(index % 60).padStart(2, "0")}.000Z`,
851 },
852 }),
853 );
854 const afterMessages = await messageEvents.reduce<
855 Promise<ReturnType<typeof createEmptyReadModel>>
856 >(
857 (statePromise, event) =>
858 statePromise.then((state) => Effect.runPromise(projectEvent(state, event))),
859 Promise.resolve(afterCreate),
860 );
861
862 const checkpointEvents: ReadonlyArray<OrchestrationEvent> = Array.from(
863 { length: 600 },
864 (_, index) =>
865 makeEvent({
866 sequence: index + 2_102,
867 type: "thread.turn-diff-completed",
868 aggregateKind: "thread",
869 aggregateId: "thread-capped",
870 occurredAt: `2026-03-01T10:30:${String(index % 60).padStart(2, "0")}.000Z`,
871 commandId: `cmd-checkpoint-${index}`,
872 payload: {
873 threadId: "thread-capped",
874 turnId: `turn-${index}`,
875 checkpointTurnCount: index + 1,
876 checkpointRef: `refs/t3/checkpoints/thread-capped/turn/${index + 1}`,
877 status: "ready",
878 files: [],
879 assistantMessageId: `msg-${index}`,
880 completedAt: `2026-03-01T10:30:${String(index % 60).padStart(2, "0")}.000Z`,
881 },
882 }),
883 );
884 const finalState = await checkpointEvents.reduce<
885 Promise<ReturnType<typeof createEmptyReadModel>>
886 >(
887 (statePromise, event) =>
888 statePromise.then((state) => Effect.runPromise(projectEvent(state, event))),
889 Promise.resolve(afterMessages),
890 );
891
892 const thread = finalState.threads[0];
893 expect(thread?.messages).toHaveLength(2_000);
894 expect(thread?.messages[0]?.id).toBe("msg-100");
895 expect(thread?.messages.at(-1)?.id).toBe("msg-2099");
896 expect(thread?.checkpoints).toHaveLength(500);
897 expect(thread?.checkpoints[0]?.turnId).toBe("turn-100");
898 expect(thread?.checkpoints.at(-1)?.turnId).toBe("turn-599");
899 });
900});