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

projector.test.ts

955 lines28.6 KB

1import {
2 CommandId,
3 EventId,
4 ProjectId,
5 ProviderDriverKind,
6 ThreadId,
7 type OrchestrationEvent,
8} from "@t3tools/contracts";
9import * as Effect from "effect/Effect";
10import { describe, expect, it } from "vite-plus/test";
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 = "2026-01-01T00:00:00.000Z";
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 = "2026-01-01T00:00:00.000Z";
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 = "2026-01-01T00:00:00.000Z";
138 const later = "2026-01-01T00:00:01.000Z";
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 = "2026-01-01T00:00:00.000Z";
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 settledAt = "2026-02-23T08:01:00.000Z";
271 const [afterRunning, afterReady] = await Effect.runPromise(
272 Effect.flatMap(
273 projectEvent(
274 afterCreate,
275 makeEvent({
276 sequence: 2,
277 type: "thread.session-set",
278 aggregateKind: "thread",
279 aggregateId: "thread-1",
280 occurredAt: startedAt,
281 commandId: "cmd-running",
282 payload: {
283 threadId: "thread-1",
284 session: {
285 threadId: "thread-1",
286 status: "running",
287 providerName: "codex",
288 providerSessionId: "session-1",
289 providerThreadId: "provider-thread-1",
290 runtimeMode: "approval-required",
291 activeTurnId: "turn-1",
292 lastError: null,
293 updatedAt: startedAt,
294 },
295 },
296 }),
297 ),
298 (running) =>
299 Effect.map(
300 projectEvent(
301 running,
302 makeEvent({
303 sequence: 3,
304 type: "thread.session-set",
305 aggregateKind: "thread",
306 aggregateId: "thread-1",
307 occurredAt: settledAt,
308 commandId: "cmd-ready",
309 payload: {
310 threadId: "thread-1",
311 session: {
312 threadId: "thread-1",
313 status: "ready",
314 providerName: "codex",
315 providerSessionId: "session-1",
316 providerThreadId: "provider-thread-1",
317 runtimeMode: "approval-required",
318 activeTurnId: null,
319 lastError: null,
320 updatedAt: settledAt,
321 },
322 },
323 }),
324 ),
325 (ready) => [running, ready] as const,
326 ),
327 ),
328 );
329​
330 const thread = afterRunning.threads[0];
331 expect(thread?.latestTurn?.turnId).toBe("turn-1");
332 expect(thread?.session?.status).toBe("running");
333​
334 // Leaving the "running" session status settles the running turn with the
335 // session timestamp as the turn end.
336 const settledThread = afterReady.threads[0];
337 expect(settledThread?.latestTurn?.turnId).toBe("turn-1");
338 expect(settledThread?.latestTurn?.state).toBe("completed");
339 expect(settledThread?.latestTurn?.completedAt).toBe(settledAt);
340 });
341​
342 it("updates canonical thread runtime mode from thread.runtime-mode-set", async () => {
343 const createdAt = "2026-02-23T08:00:00.000Z";
344 const updatedAt = "2026-02-23T08:00:05.000Z";
345 const model = createEmptyReadModel(createdAt);
346​
347 const afterCreate = await Effect.runPromise(
348 projectEvent(
349 model,
350 makeEvent({
351 sequence: 1,
352 type: "thread.created",
353 aggregateKind: "thread",
354 aggregateId: "thread-1",
355 occurredAt: createdAt,
356 commandId: "cmd-create",
357 payload: {
358 threadId: "thread-1",
359 projectId: "project-1",
360 title: "demo",
361 modelSelection: {
362 provider: ProviderDriverKind.make("codex"),
363 model: "gpt-5.3-codex",
364 },
365 runtimeMode: "full-access",
366 branch: null,
367 worktreePath: null,
368 createdAt,
369 updatedAt: createdAt,
370 },
371 }),
372 ),
373 );
374​
375 const afterUpdate = await Effect.runPromise(
376 projectEvent(
377 afterCreate,
378 makeEvent({
379 sequence: 2,
380 type: "thread.runtime-mode-set",
381 aggregateKind: "thread",
382 aggregateId: "thread-1",
383 occurredAt: updatedAt,
384 commandId: "cmd-runtime-mode-set",
385 payload: {
386 threadId: "thread-1",
387 runtimeMode: "approval-required",
388 updatedAt,
389 },
390 }),
391 ),
392 );
393​
394 expect(afterUpdate.threads[0]?.runtimeMode).toBe("approval-required");
395 expect(afterUpdate.threads[0]?.updatedAt).toBe(updatedAt);
396 });
397​
398 it("marks assistant messages completed with non-streaming updates", async () => {
399 const createdAt = "2026-02-23T09:00:00.000Z";
400 const deltaAt = "2026-02-23T09:00:01.000Z";
401 const completeAt = "2026-02-23T09:00:03.500Z";
402 const model = createEmptyReadModel(createdAt);
403​
404 const afterCreate = await Effect.runPromise(
405 projectEvent(
406 model,
407 makeEvent({
408 sequence: 1,
409 type: "thread.created",
410 aggregateKind: "thread",
411 aggregateId: "thread-1",
412 occurredAt: createdAt,
413 commandId: "cmd-create",
414 payload: {
415 threadId: "thread-1",
416 projectId: "project-1",
417 title: "demo",
418 modelSelection: {
419 provider: ProviderDriverKind.make("codex"),
420 model: "gpt-5.3-codex",
421 },
422 runtimeMode: "full-access",
423 branch: null,
424 worktreePath: null,
425 createdAt,
426 updatedAt: createdAt,
427 },
428 }),
429 ),
430 );
431​
432 const afterDelta = await Effect.runPromise(
433 projectEvent(
434 afterCreate,
435 makeEvent({
436 sequence: 2,
437 type: "thread.message-sent",
438 aggregateKind: "thread",
439 aggregateId: "thread-1",
440 occurredAt: deltaAt,
441 commandId: "cmd-delta",
442 payload: {
443 threadId: "thread-1",
444 messageId: "assistant:msg-1",
445 role: "assistant",
446 text: "hello",
447 turnId: "turn-1",
448 streaming: true,
449 createdAt: deltaAt,
450 updatedAt: deltaAt,
451 },
452 }),
453 ),
454 );
455​
456 const afterComplete = await Effect.runPromise(
457 projectEvent(
458 afterDelta,
459 makeEvent({
460 sequence: 3,
461 type: "thread.message-sent",
462 aggregateKind: "thread",
463 aggregateId: "thread-1",
464 occurredAt: completeAt,
465 commandId: "cmd-complete",
466 payload: {
467 threadId: "thread-1",
468 messageId: "assistant:msg-1",
469 role: "assistant",
470 text: "",
471 turnId: "turn-1",
472 streaming: false,
473 createdAt: completeAt,
474 updatedAt: completeAt,
475 },
476 }),
477 ),
478 );
479​
480 const message = afterComplete.threads[0]?.messages[0];
481 expect(message?.id).toBe("assistant:msg-1");
482 expect(message?.text).toBe("hello");
483 expect(message?.streaming).toBe(false);
484 expect(message?.updatedAt).toBe(completeAt);
485 });
486​
487 it("prunes reverted turn messages from in-memory thread snapshot", async () => {
488 const createdAt = "2026-02-23T10:00:00.000Z";
489 const model = createEmptyReadModel(createdAt);
490​
491 const afterCreate = await Effect.runPromise(
492 projectEvent(
493 model,
494 makeEvent({
495 sequence: 1,
496 type: "thread.created",
497 aggregateKind: "thread",
498 aggregateId: "thread-1",
499 occurredAt: createdAt,
500 commandId: "cmd-create",
501 payload: {
502 threadId: "thread-1",
503 projectId: "project-1",
504 title: "demo",
505 modelSelection: {
506 provider: ProviderDriverKind.make("codex"),
507 model: "gpt-5.3-codex",
508 },
509 runtimeMode: "full-access",
510 branch: null,
511 worktreePath: null,
512 createdAt,
513 updatedAt: createdAt,
514 },
515 }),
516 ),
517 );
518​
519 const events: ReadonlyArray<OrchestrationEvent> = [
520 makeEvent({
521 sequence: 2,
522 type: "thread.message-sent",
523 aggregateKind: "thread",
524 aggregateId: "thread-1",
525 occurredAt: "2026-02-23T10:00:01.000Z",
526 commandId: "cmd-user-1",
527 payload: {
528 threadId: "thread-1",
529 messageId: "user-msg-1",
530 role: "user",
531 text: "First edit",
532 turnId: null,
533 streaming: false,
534 createdAt: "2026-02-23T10:00:01.000Z",
535 updatedAt: "2026-02-23T10:00:01.000Z",
536 },
537 }),
538 makeEvent({
539 sequence: 3,
540 type: "thread.message-sent",
541 aggregateKind: "thread",
542 aggregateId: "thread-1",
543 occurredAt: "2026-02-23T10:00:02.000Z",
544 commandId: "cmd-assistant-1",
545 payload: {
546 threadId: "thread-1",
547 messageId: "assistant-msg-1",
548 role: "assistant",
549 text: "Updated README to v2.\n",
550 turnId: "turn-1",
551 streaming: false,
552 createdAt: "2026-02-23T10:00:02.000Z",
553 updatedAt: "2026-02-23T10:00:02.000Z",
554 },
555 }),
556 makeEvent({
557 sequence: 4,
558 type: "thread.turn-diff-completed",
559 aggregateKind: "thread",
560 aggregateId: "thread-1",
561 occurredAt: "2026-02-23T10:00:02.500Z",
562 commandId: "cmd-turn-1-complete",
563 payload: {
564 threadId: "thread-1",
565 turnId: "turn-1",
566 checkpointTurnCount: 1,
567 checkpointRef: "refs/t3/checkpoints/thread-1/turn/1",
568 status: "ready",
569 files: [],
570 assistantMessageId: "assistant-msg-1",
571 completedAt: "2026-02-23T10:00:02.500Z",
572 },
573 }),
574 makeEvent({
575 sequence: 5,
576 type: "thread.activity-appended",
577 aggregateKind: "thread",
578 aggregateId: "thread-1",
579 occurredAt: "2026-02-23T10:00:02.750Z",
580 commandId: "cmd-activity-1",
581 payload: {
582 threadId: "thread-1",
583 activity: {
584 id: "activity-1",
585 tone: "tool",
586 kind: "tool.started",
587 summary: "Edit file started",
588 payload: { toolKind: "command" },
589 turnId: "turn-1",
590 createdAt: "2026-02-23T10:00:02.750Z",
591 },
592 },
593 }),
594 makeEvent({
595 sequence: 6,
596 type: "thread.message-sent",
597 aggregateKind: "thread",
598 aggregateId: "thread-1",
599 occurredAt: "2026-02-23T10:00:03.000Z",
600 commandId: "cmd-user-2",
601 payload: {
602 threadId: "thread-1",
603 messageId: "user-msg-2",
604 role: "user",
605 text: "Second edit",
606 turnId: null,
607 streaming: false,
608 createdAt: "2026-02-23T10:00:03.000Z",
609 updatedAt: "2026-02-23T10:00:03.000Z",
610 },
611 }),
612 makeEvent({
613 sequence: 7,
614 type: "thread.message-sent",
615 aggregateKind: "thread",
616 aggregateId: "thread-1",
617 occurredAt: "2026-02-23T10:00:04.000Z",
618 commandId: "cmd-assistant-2",
619 payload: {
620 threadId: "thread-1",
621 messageId: "assistant-msg-2",
622 role: "assistant",
623 text: "Updated README to v3.\n",
624 turnId: "turn-2",
625 streaming: false,
626 createdAt: "2026-02-23T10:00:04.000Z",
627 updatedAt: "2026-02-23T10:00:04.000Z",
628 },
629 }),
630 makeEvent({
631 sequence: 8,
632 type: "thread.turn-diff-completed",
633 aggregateKind: "thread",
634 aggregateId: "thread-1",
635 occurredAt: "2026-02-23T10:00:04.500Z",
636 commandId: "cmd-turn-2-complete",
637 payload: {
638 threadId: "thread-1",
639 turnId: "turn-2",
640 checkpointTurnCount: 2,
641 checkpointRef: "refs/t3/checkpoints/thread-1/turn/2",
642 status: "ready",
643 files: [],
644 assistantMessageId: "assistant-msg-2",
645 completedAt: "2026-02-23T10:00:04.500Z",
646 },
647 }),
648 makeEvent({
649 sequence: 9,
650 type: "thread.activity-appended",
651 aggregateKind: "thread",
652 aggregateId: "thread-1",
653 occurredAt: "2026-02-23T10:00:04.750Z",
654 commandId: "cmd-activity-2",
655 payload: {
656 threadId: "thread-1",
657 activity: {
658 id: "activity-2",
659 tone: "tool",
660 kind: "tool.completed",
661 summary: "Edit file complete",
662 payload: { toolKind: "command" },
663 turnId: "turn-2",
664 createdAt: "2026-02-23T10:00:04.750Z",
665 },
666 },
667 }),
668 makeEvent({
669 sequence: 10,
670 type: "thread.reverted",
671 aggregateKind: "thread",
672 aggregateId: "thread-1",
673 occurredAt: "2026-02-23T10:00:05.000Z",
674 commandId: "cmd-revert",
675 payload: {
676 threadId: "thread-1",
677 turnCount: 1,
678 },
679 }),
680 ];
681​
682 const afterRevert = await events.reduce<Promise<ReturnType<typeof createEmptyReadModel>>>(
683 (statePromise, event) =>
684 statePromise.then((state) => Effect.runPromise(projectEvent(state, event))),
685 Promise.resolve(afterCreate),
686 );
687​
688 const thread = afterRevert.threads[0];
689 expect(thread?.messages.map((message) => ({ role: message.role, text: message.text }))).toEqual(
690 [
691 { role: "user", text: "First edit" },
692 { role: "assistant", text: "Updated README to v2.\n" },
693 ],
694 );
695 expect(
696 thread?.activities.map((activity) => ({ id: activity.id, turnId: activity.turnId })),
697 ).toEqual([{ id: "activity-1", turnId: "turn-1" }]);
698 expect(thread?.checkpoints.map((checkpoint) => checkpoint.checkpointTurnCount)).toEqual([1]);
699 expect(thread?.latestTurn?.turnId).toBe("turn-1");
700 });
701​
702 it("does not fallback-retain messages tied to removed turn IDs", async () => {
703 const createdAt = "2026-02-26T12:00:00.000Z";
704 const model = createEmptyReadModel(createdAt);
705​
706 const afterCreate = await Effect.runPromise(
707 projectEvent(
708 model,
709 makeEvent({
710 sequence: 1,
711 type: "thread.created",
712 aggregateKind: "thread",
713 aggregateId: "thread-revert",
714 occurredAt: createdAt,
715 commandId: "cmd-create-revert",
716 payload: {
717 threadId: "thread-revert",
718 projectId: "project-1",
719 title: "demo",
720 modelSelection: {
721 provider: ProviderDriverKind.make("codex"),
722 model: "gpt-5.3-codex",
723 },
724 runtimeMode: "full-access",
725 branch: null,
726 worktreePath: null,
727 createdAt,
728 updatedAt: createdAt,
729 },
730 }),
731 ),
732 );
733​
734 const events: ReadonlyArray<OrchestrationEvent> = [
735 makeEvent({
736 sequence: 2,
737 type: "thread.turn-diff-completed",
738 aggregateKind: "thread",
739 aggregateId: "thread-revert",
740 occurredAt: "2026-02-26T12:00:01.000Z",
741 commandId: "cmd-turn-1",
742 payload: {
743 threadId: "thread-revert",
744 turnId: "turn-1",
745 checkpointTurnCount: 1,
746 checkpointRef: "refs/t3/checkpoints/thread-revert/turn/1",
747 status: "ready",
748 files: [],
749 assistantMessageId: "assistant-keep",
750 completedAt: "2026-02-26T12:00:01.000Z",
751 },
752 }),
753 makeEvent({
754 sequence: 3,
755 type: "thread.message-sent",
756 aggregateKind: "thread",
757 aggregateId: "thread-revert",
758 occurredAt: "2026-02-26T12:00:01.100Z",
759 commandId: "cmd-assistant-keep",
760 payload: {
761 threadId: "thread-revert",
762 messageId: "assistant-keep",
763 role: "assistant",
764 text: "kept",
765 turnId: "turn-1",
766 streaming: false,
767 createdAt: "2026-02-26T12:00:01.100Z",
768 updatedAt: "2026-02-26T12:00:01.100Z",
769 },
770 }),
771 makeEvent({
772 sequence: 4,
773 type: "thread.turn-diff-completed",
774 aggregateKind: "thread",
775 aggregateId: "thread-revert",
776 occurredAt: "2026-02-26T12:00:02.000Z",
777 commandId: "cmd-turn-2",
778 payload: {
779 threadId: "thread-revert",
780 turnId: "turn-2",
781 checkpointTurnCount: 2,
782 checkpointRef: "refs/t3/checkpoints/thread-revert/turn/2",
783 status: "ready",
784 files: [],
785 assistantMessageId: "assistant-remove",
786 completedAt: "2026-02-26T12:00:02.000Z",
787 },
788 }),
789 makeEvent({
790 sequence: 5,
791 type: "thread.message-sent",
792 aggregateKind: "thread",
793 aggregateId: "thread-revert",
794 occurredAt: "2026-02-26T12:00:02.050Z",
795 commandId: "cmd-user-remove",
796 payload: {
797 threadId: "thread-revert",
798 messageId: "user-remove",
799 role: "user",
800 text: "removed",
801 turnId: "turn-2",
802 streaming: false,
803 createdAt: "2026-02-26T12:00:02.050Z",
804 updatedAt: "2026-02-26T12:00:02.050Z",
805 },
806 }),
807 makeEvent({
808 sequence: 6,
809 type: "thread.message-sent",
810 aggregateKind: "thread",
811 aggregateId: "thread-revert",
812 occurredAt: "2026-02-26T12:00:02.100Z",
813 commandId: "cmd-assistant-remove",
814 payload: {
815 threadId: "thread-revert",
816 messageId: "assistant-remove",
817 role: "assistant",
818 text: "removed",
819 turnId: "turn-2",
820 streaming: false,
821 createdAt: "2026-02-26T12:00:02.100Z",
822 updatedAt: "2026-02-26T12:00:02.100Z",
823 },
824 }),
825 makeEvent({
826 sequence: 7,
827 type: "thread.reverted",
828 aggregateKind: "thread",
829 aggregateId: "thread-revert",
830 occurredAt: "2026-02-26T12:00:03.000Z",
831 commandId: "cmd-revert",
832 payload: {
833 threadId: "thread-revert",
834 turnCount: 1,
835 },
836 }),
837 ];
838​
839 const afterRevert = await events.reduce<Promise<ReturnType<typeof createEmptyReadModel>>>(
840 (statePromise, event) =>
841 statePromise.then((state) => Effect.runPromise(projectEvent(state, event))),
842 Promise.resolve(afterCreate),
843 );
844​
845 const thread = afterRevert.threads[0];
846 expect(
847 thread?.messages.map((message) => ({
848 id: message.id,
849 role: message.role,
850 turnId: message.turnId,
851 })),
852 ).toEqual([{ id: "assistant-keep", role: "assistant", turnId: "turn-1" }]);
853 });
854​
855 it("caps message and checkpoint retention for long-lived threads", async () => {
856 const createdAt = "2026-03-01T10:00:00.000Z";
857 const model = createEmptyReadModel(createdAt);
858​
859 const afterCreate = await Effect.runPromise(
860 projectEvent(
861 model,
862 makeEvent({
863 sequence: 1,
864 type: "thread.created",
865 aggregateKind: "thread",
866 aggregateId: "thread-capped",
867 occurredAt: createdAt,
868 commandId: "cmd-create-capped",
869 payload: {
870 threadId: "thread-capped",
871 projectId: "project-1",
872 title: "capped",
873 modelSelection: {
874 provider: ProviderDriverKind.make("codex"),
875 model: "gpt-5-codex",
876 },
877 runtimeMode: "full-access",
878 branch: null,
879 worktreePath: null,
880 createdAt,
881 updatedAt: createdAt,
882 },
883 }),
884 ),
885 );
886​
887 const messageEvents: ReadonlyArray<OrchestrationEvent> = Array.from(
888 { length: 2_100 },
889 (_, index) =>
890 makeEvent({
891 sequence: index + 2,
892 type: "thread.message-sent",
893 aggregateKind: "thread",
894 aggregateId: "thread-capped",
895 occurredAt: `2026-03-01T10:00:${String(index % 60).padStart(2, "0")}.000Z`,
896 commandId: `cmd-message-${index}`,
897 payload: {
898 threadId: "thread-capped",
899 messageId: `msg-${index}`,
900 role: "assistant",
901 text: `message-${index}`,
902 turnId: `turn-${index}`,
903 streaming: false,
904 createdAt: `2026-03-01T10:00:${String(index % 60).padStart(2, "0")}.000Z`,
905 updatedAt: `2026-03-01T10:00:${String(index % 60).padStart(2, "0")}.000Z`,
906 },
907 }),
908 );
909 const afterMessages = await messageEvents.reduce<
910 Promise<ReturnType<typeof createEmptyReadModel>>
911 >(
912 (statePromise, event) =>
913 statePromise.then((state) => Effect.runPromise(projectEvent(state, event))),
914 Promise.resolve(afterCreate),
915 );
916​
917 const checkpointEvents: ReadonlyArray<OrchestrationEvent> = Array.from(
918 { length: 600 },
919 (_, index) =>
920 makeEvent({
921 sequence: index + 2_102,
922 type: "thread.turn-diff-completed",
923 aggregateKind: "thread",
924 aggregateId: "thread-capped",
925 occurredAt: `2026-03-01T10:30:${String(index % 60).padStart(2, "0")}.000Z`,
926 commandId: `cmd-checkpoint-${index}`,
927 payload: {
928 threadId: "thread-capped",
929 turnId: `turn-${index}`,
930 checkpointTurnCount: index + 1,
931 checkpointRef: `refs/t3/checkpoints/thread-capped/turn/${index + 1}`,
932 status: "ready",
933 files: [],
934 assistantMessageId: `msg-${index}`,
935 completedAt: `2026-03-01T10:30:${String(index % 60).padStart(2, "0")}.000Z`,
936 },
937 }),
938 );
939 const finalState = await checkpointEvents.reduce<
940 Promise<ReturnType<typeof createEmptyReadModel>>
941 >(
942 (statePromise, event) =>
943 statePromise.then((state) => Effect.runPromise(projectEvent(state, event))),
944 Promise.resolve(afterMessages),
945 );
946​
947 const thread = finalState.threads[0];
948 expect(thread?.messages).toHaveLength(2_000);
949 expect(thread?.messages[0]?.id).toBe("msg-100");
950 expect(thread?.messages.at(-1)?.id).toBe("msg-2099");
951 expect(thread?.checkpoints).toHaveLength(500);
952 expect(thread?.checkpoints[0]?.turnId).toBe("turn-100");
953 expect(thread?.checkpoints.at(-1)?.turnId).toBe("turn-599");
954 });
955});