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