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