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

projector.test.ts

916 lines27.1 KB

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