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

projector.test.ts

915 lines26.9 KB

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