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