Open History to see the last commit
1import {
2 CheckpointRef,
3 EventId,
4 MessageId,
5 ProjectId,
6 ThreadId,
7 TurnId,
8 ProviderInstanceId,
9} from "@t3tools/contracts";
10import { assert, it } from "@effect/vitest";
11import { Effect, Layer } from "effect";
12import * as SqlClient from "effect/unstable/sql/SqlClient";
13
14import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
15import { RepositoryIdentityResolver } from "../../project/Services/RepositoryIdentityResolver.ts";
16import { RepositoryIdentityResolverLive } from "../../project/Layers/RepositoryIdentityResolver.ts";
17import { ORCHESTRATION_PROJECTOR_NAMES } from "./ProjectionPipeline.ts";
18import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
19import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
20
21const asProjectId = (value: string): ProjectId => ProjectId.make(value);
22const asTurnId = (value: string): TurnId => TurnId.make(value);
23const asMessageId = (value: string): MessageId => MessageId.make(value);
24const asEventId = (value: string): EventId => EventId.make(value);
25const asCheckpointRef = (value: string): CheckpointRef => CheckpointRef.make(value);
26
27const projectionSnapshotLayer = it.layer(
28 OrchestrationProjectionSnapshotQueryLive.pipe(
29 Layer.provideMerge(RepositoryIdentityResolverLive),
30 Layer.provideMerge(SqlitePersistenceMemory),
31 ),
32);
33
34projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
35 it.effect("hydrates read model from projection tables and computes snapshot sequence", () =>
36 Effect.gen(function* () {
37 const snapshotQuery = yield* ProjectionSnapshotQuery;
38 const sql = yield* SqlClient.SqlClient;
39
40 yield* sql`DELETE FROM projection_projects`;
41 yield* sql`DELETE FROM projection_state`;
42 yield* sql`DELETE FROM projection_thread_proposed_plans`;
43 yield* sql`DELETE FROM projection_turns`;
44
45 yield* sql`
46 INSERT INTO projection_projects (
47 project_id,
48 title,
49 workspace_root,
50 default_model_selection_json,
51 scripts_json,
52 created_at,
53 updated_at,
54 deleted_at
55 )
56 VALUES (
57 'project-1',
58 'Project 1',
59 '/tmp/project-1',
60 '{"provider":"codex","model":"gpt-5-codex"}',
61 '[{"id":"script-1","name":"Build","command":"bun run build","icon":"build","runOnWorktreeCreate":false}]',
62 '2026-02-24T00:00:00.000Z',
63 '2026-02-24T00:00:01.000Z',
64 NULL
65 )
66 `;
67
68 yield* sql`
69 INSERT INTO projection_threads (
70 thread_id,
71 project_id,
72 title,
73 model_selection_json,
74 runtime_mode,
75 interaction_mode,
76 branch,
77 worktree_path,
78 latest_turn_id,
79 latest_user_message_at,
80 pending_approval_count,
81 pending_user_input_count,
82 has_actionable_proposed_plan,
83 created_at,
84 updated_at,
85 deleted_at
86 )
87 VALUES (
88 'thread-1',
89 'project-1',
90 'Thread 1',
91 '{"provider":"codex","model":"gpt-5-codex"}',
92 'full-access',
93 'default',
94 NULL,
95 NULL,
96 'turn-1',
97 '2026-02-24T00:00:04.000Z',
98 1,
99 0,
100 0,
101 '2026-02-24T00:00:02.000Z',
102 '2026-02-24T00:00:03.000Z',
103 NULL
104 )
105 `;
106
107 yield* sql`
108 INSERT INTO projection_thread_messages (
109 message_id,
110 thread_id,
111 turn_id,
112 role,
113 text,
114 is_streaming,
115 created_at,
116 updated_at
117 )
118 VALUES (
119 'message-1',
120 'thread-1',
121 'turn-1',
122 'assistant',
123 'hello from projection',
124 0,
125 '2026-02-24T00:00:04.000Z',
126 '2026-02-24T00:00:05.000Z'
127 )
128 `;
129
130 yield* sql`
131 INSERT INTO projection_thread_proposed_plans (
132 plan_id,
133 thread_id,
134 turn_id,
135 plan_markdown,
136 implemented_at,
137 implementation_thread_id,
138 created_at,
139 updated_at
140 )
141 VALUES (
142 'plan-1',
143 'thread-1',
144 'turn-1',
145 '# Ship it',
146 '2026-02-24T00:00:05.500Z',
147 'thread-2',
148 '2026-02-24T00:00:05.000Z',
149 '2026-02-24T00:00:05.500Z'
150 )
151 `;
152
153 yield* sql`
154 INSERT INTO projection_thread_activities (
155 activity_id,
156 thread_id,
157 turn_id,
158 tone,
159 kind,
160 summary,
161 payload_json,
162 created_at
163 )
164 VALUES (
165 'activity-1',
166 'thread-1',
167 'turn-1',
168 'info',
169 'runtime.note',
170 'provider started',
171 '{"stage":"start"}',
172 '2026-02-24T00:00:06.000Z'
173 )
174 `;
175
176 yield* sql`
177 INSERT INTO projection_thread_sessions (
178 thread_id,
179 status,
180 provider_name,
181 provider_session_id,
182 provider_thread_id,
183 runtime_mode,
184 active_turn_id,
185 last_error,
186 updated_at
187 )
188 VALUES (
189 'thread-1',
190 'running',
191 'codex',
192 'provider-session-1',
193 'provider-thread-1',
194 'approval-required',
195 'turn-1',
196 NULL,
197 '2026-02-24T00:00:07.000Z'
198 )
199 `;
200
201 yield* sql`
202 INSERT INTO projection_turns (
203 thread_id,
204 turn_id,
205 pending_message_id,
206 source_proposed_plan_thread_id,
207 source_proposed_plan_id,
208 assistant_message_id,
209 state,
210 requested_at,
211 started_at,
212 completed_at,
213 checkpoint_turn_count,
214 checkpoint_ref,
215 checkpoint_status,
216 checkpoint_files_json
217 )
218 VALUES (
219 'thread-1',
220 'turn-1',
221 NULL,
222 'thread-1',
223 'plan-1',
224 'message-1',
225 'completed',
226 '2026-02-24T00:00:08.000Z',
227 '2026-02-24T00:00:08.000Z',
228 '2026-02-24T00:00:08.000Z',
229 1,
230 'checkpoint-1',
231 'ready',
232 '[{"path":"README.md","kind":"modified","additions":2,"deletions":1}]'
233 )
234 `;
235
236 let sequence = 5;
237 for (const projector of Object.values(ORCHESTRATION_PROJECTOR_NAMES)) {
238 yield* sql`
239 INSERT INTO projection_state (
240 projector,
241 last_applied_sequence,
242 updated_at
243 )
244 VALUES (
245 ${projector},
246 ${sequence},
247 '2026-02-24T00:00:09.000Z'
248 )
249 `;
250 sequence += 1;
251 }
252
253 const snapshot = yield* snapshotQuery.getSnapshot();
254
255 assert.equal(snapshot.snapshotSequence, 5);
256 assert.equal(snapshot.updatedAt, "2026-02-24T00:00:09.000Z");
257 assert.deepEqual(snapshot.projects, [
258 {
259 id: asProjectId("project-1"),
260 title: "Project 1",
261 workspaceRoot: "/tmp/project-1",
262 repositoryIdentity: null,
263 defaultModelSelection: {
264 instanceId: ProviderInstanceId.make("codex"),
265 model: "gpt-5-codex",
266 },
267 scripts: [
268 {
269 id: "script-1",
270 name: "Build",
271 command: "bun run build",
272 icon: "build",
273 runOnWorktreeCreate: false,
274 },
275 ],
276 createdAt: "2026-02-24T00:00:00.000Z",
277 updatedAt: "2026-02-24T00:00:01.000Z",
278 deletedAt: null,
279 },
280 ]);
281 assert.deepEqual(snapshot.threads, [
282 {
283 id: ThreadId.make("thread-1"),
284 projectId: asProjectId("project-1"),
285 title: "Thread 1",
286 modelSelection: {
287 instanceId: ProviderInstanceId.make("codex"),
288 model: "gpt-5-codex",
289 },
290 interactionMode: "default",
291 runtimeMode: "full-access",
292 branch: null,
293 worktreePath: null,
294 latestTurn: {
295 turnId: asTurnId("turn-1"),
296 state: "completed",
297 requestedAt: "2026-02-24T00:00:08.000Z",
298 startedAt: "2026-02-24T00:00:08.000Z",
299 completedAt: "2026-02-24T00:00:08.000Z",
300 assistantMessageId: asMessageId("message-1"),
301 sourceProposedPlan: {
302 threadId: ThreadId.make("thread-1"),
303 planId: "plan-1",
304 },
305 },
306 createdAt: "2026-02-24T00:00:02.000Z",
307 updatedAt: "2026-02-24T00:00:03.000Z",
308 archivedAt: null,
309 deletedAt: null,
310 messages: [
311 {
312 id: asMessageId("message-1"),
313 role: "assistant",
314 text: "hello from projection",
315 turnId: asTurnId("turn-1"),
316 streaming: false,
317 createdAt: "2026-02-24T00:00:04.000Z",
318 updatedAt: "2026-02-24T00:00:05.000Z",
319 },
320 ],
321 proposedPlans: [
322 {
323 id: "plan-1",
324 turnId: asTurnId("turn-1"),
325 planMarkdown: "# Ship it",
326 implementedAt: "2026-02-24T00:00:05.500Z",
327 implementationThreadId: ThreadId.make("thread-2"),
328 createdAt: "2026-02-24T00:00:05.000Z",
329 updatedAt: "2026-02-24T00:00:05.500Z",
330 },
331 ],
332 activities: [
333 {
334 id: asEventId("activity-1"),
335 tone: "info",
336 kind: "runtime.note",
337 summary: "provider started",
338 payload: { stage: "start" },
339 turnId: asTurnId("turn-1"),
340 createdAt: "2026-02-24T00:00:06.000Z",
341 },
342 ],
343 checkpoints: [
344 {
345 turnId: asTurnId("turn-1"),
346 checkpointTurnCount: 1,
347 checkpointRef: asCheckpointRef("checkpoint-1"),
348 status: "ready",
349 files: [{ path: "README.md", kind: "modified", additions: 2, deletions: 1 }],
350 assistantMessageId: asMessageId("message-1"),
351 completedAt: "2026-02-24T00:00:08.000Z",
352 },
353 ],
354 session: {
355 threadId: ThreadId.make("thread-1"),
356 status: "running",
357 providerName: "codex",
358 runtimeMode: "approval-required",
359 activeTurnId: asTurnId("turn-1"),
360 lastError: null,
361 updatedAt: "2026-02-24T00:00:07.000Z",
362 },
363 },
364 ]);
365
366 const shellSnapshot = yield* snapshotQuery.getShellSnapshot();
367 assert.equal(shellSnapshot.snapshotSequence, 5);
368 assert.deepEqual(shellSnapshot.projects, [
369 {
370 id: asProjectId("project-1"),
371 title: "Project 1",
372 workspaceRoot: "/tmp/project-1",
373 repositoryIdentity: null,
374 defaultModelSelection: {
375 instanceId: ProviderInstanceId.make("codex"),
376 model: "gpt-5-codex",
377 },
378 scripts: [
379 {
380 id: "script-1",
381 name: "Build",
382 command: "bun run build",
383 icon: "build",
384 runOnWorktreeCreate: false,
385 },
386 ],
387 createdAt: "2026-02-24T00:00:00.000Z",
388 updatedAt: "2026-02-24T00:00:01.000Z",
389 },
390 ]);
391 assert.deepEqual(shellSnapshot.threads, [
392 {
393 id: ThreadId.make("thread-1"),
394 projectId: asProjectId("project-1"),
395 title: "Thread 1",
396 modelSelection: {
397 instanceId: ProviderInstanceId.make("codex"),
398 model: "gpt-5-codex",
399 },
400 interactionMode: "default",
401 runtimeMode: "full-access",
402 branch: null,
403 worktreePath: null,
404 latestTurn: {
405 turnId: asTurnId("turn-1"),
406 state: "completed",
407 requestedAt: "2026-02-24T00:00:08.000Z",
408 startedAt: "2026-02-24T00:00:08.000Z",
409 completedAt: "2026-02-24T00:00:08.000Z",
410 assistantMessageId: asMessageId("message-1"),
411 sourceProposedPlan: {
412 threadId: ThreadId.make("thread-1"),
413 planId: "plan-1",
414 },
415 },
416 createdAt: "2026-02-24T00:00:02.000Z",
417 updatedAt: "2026-02-24T00:00:03.000Z",
418 archivedAt: null,
419 session: {
420 threadId: ThreadId.make("thread-1"),
421 status: "running",
422 providerName: "codex",
423 runtimeMode: "approval-required",
424 activeTurnId: asTurnId("turn-1"),
425 lastError: null,
426 updatedAt: "2026-02-24T00:00:07.000Z",
427 },
428 latestUserMessageAt: "2026-02-24T00:00:04.000Z",
429 hasPendingApprovals: true,
430 hasPendingUserInput: false,
431 hasActionableProposedPlan: false,
432 },
433 ]);
434
435 const threadDetail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1"));
436 assert.equal(threadDetail._tag, "Some");
437 if (threadDetail._tag === "Some") {
438 assert.deepEqual(threadDetail.value, snapshot.threads[0]);
439 }
440 }),
441 );
442
443 it.effect(
444 "reads targeted project, thread, and count queries without hydrating the full snapshot",
445 () =>
446 Effect.gen(function* () {
447 const snapshotQuery = yield* ProjectionSnapshotQuery;
448 const sql = yield* SqlClient.SqlClient;
449
450 yield* sql`DELETE FROM projection_projects`;
451 yield* sql`DELETE FROM projection_threads`;
452 yield* sql`DELETE FROM projection_turns`;
453
454 yield* sql`
455 INSERT INTO projection_projects (
456 project_id,
457 title,
458 workspace_root,
459 default_model_selection_json,
460 scripts_json,
461 created_at,
462 updated_at,
463 deleted_at
464 )
465 VALUES
466 (
467 'project-active',
468 'Active Project',
469 '/tmp/workspace',
470 '{"provider":"codex","model":"gpt-5-codex"}',
471 '[]',
472 '2026-03-01T00:00:00.000Z',
473 '2026-03-01T00:00:01.000Z',
474 NULL
475 ),
476 (
477 'project-deleted',
478 'Deleted Project',
479 '/tmp/deleted',
480 NULL,
481 '[]',
482 '2026-03-01T00:00:02.000Z',
483 '2026-03-01T00:00:03.000Z',
484 '2026-03-01T00:00:04.000Z'
485 )
486 `;
487
488 yield* sql`
489 INSERT INTO projection_threads (
490 thread_id,
491 project_id,
492 title,
493 model_selection_json,
494 runtime_mode,
495 interaction_mode,
496 branch,
497 worktree_path,
498 latest_turn_id,
499 created_at,
500 updated_at,
501 archived_at,
502 deleted_at
503 )
504 VALUES
505 (
506 'thread-first',
507 'project-active',
508 'First Thread',
509 '{"provider":"codex","model":"gpt-5-codex"}',
510 'full-access',
511 'default',
512 NULL,
513 NULL,
514 NULL,
515 '2026-03-01T00:00:05.000Z',
516 '2026-03-01T00:00:06.000Z',
517 NULL,
518 NULL
519 ),
520 (
521 'thread-second',
522 'project-active',
523 'Second Thread',
524 '{"provider":"codex","model":"gpt-5-codex"}',
525 'full-access',
526 'default',
527 NULL,
528 NULL,
529 NULL,
530 '2026-03-01T00:00:07.000Z',
531 '2026-03-01T00:00:08.000Z',
532 NULL,
533 NULL
534 ),
535 (
536 'thread-deleted',
537 'project-active',
538 'Deleted Thread',
539 '{"provider":"codex","model":"gpt-5-codex"}',
540 'full-access',
541 'default',
542 NULL,
543 NULL,
544 NULL,
545 '2026-03-01T00:00:09.000Z',
546 '2026-03-01T00:00:10.000Z',
547 NULL,
548 '2026-03-01T00:00:11.000Z'
549 )
550 `;
551
552 const counts = yield* snapshotQuery.getCounts();
553 assert.deepEqual(counts, {
554 projectCount: 2,
555 threadCount: 3,
556 });
557
558 const project = yield* snapshotQuery.getActiveProjectByWorkspaceRoot("/tmp/workspace");
559 assert.equal(project._tag, "Some");
560 if (project._tag === "Some") {
561 assert.equal(project.value.id, asProjectId("project-active"));
562 }
563
564 const missingProject = yield* snapshotQuery.getActiveProjectByWorkspaceRoot("/tmp/missing");
565 assert.equal(missingProject._tag, "None");
566
567 const firstThreadId = yield* snapshotQuery.getFirstActiveThreadIdByProjectId(
568 asProjectId("project-active"),
569 );
570 assert.equal(firstThreadId._tag, "Some");
571 if (firstThreadId._tag === "Some") {
572 assert.equal(firstThreadId.value, ThreadId.make("thread-first"));
573 }
574 }),
575 );
576
577 it.effect("reads single-thread checkpoint context without hydrating unrelated threads", () =>
578 Effect.gen(function* () {
579 const snapshotQuery = yield* ProjectionSnapshotQuery;
580 const sql = yield* SqlClient.SqlClient;
581
582 yield* sql`DELETE FROM projection_projects`;
583 yield* sql`DELETE FROM projection_threads`;
584 yield* sql`DELETE FROM projection_turns`;
585
586 yield* sql`
587 INSERT INTO projection_projects (
588 project_id,
589 title,
590 workspace_root,
591 default_model_selection_json,
592 scripts_json,
593 created_at,
594 updated_at,
595 deleted_at
596 )
597 VALUES (
598 'project-context',
599 'Context Project',
600 '/tmp/context-workspace',
601 NULL,
602 '[]',
603 '2026-03-02T00:00:00.000Z',
604 '2026-03-02T00:00:01.000Z',
605 NULL
606 )
607 `;
608
609 yield* sql`
610 INSERT INTO projection_threads (
611 thread_id,
612 project_id,
613 title,
614 model_selection_json,
615 runtime_mode,
616 interaction_mode,
617 branch,
618 worktree_path,
619 latest_turn_id,
620 created_at,
621 updated_at,
622 archived_at,
623 deleted_at
624 )
625 VALUES (
626 'thread-context',
627 'project-context',
628 'Context Thread',
629 '{"provider":"codex","model":"gpt-5-codex"}',
630 'full-access',
631 'default',
632 'feature/perf',
633 '/tmp/context-worktree',
634 NULL,
635 '2026-03-02T00:00:02.000Z',
636 '2026-03-02T00:00:03.000Z',
637 NULL,
638 NULL
639 )
640 `;
641
642 yield* sql`
643 INSERT INTO projection_turns (
644 thread_id,
645 turn_id,
646 pending_message_id,
647 source_proposed_plan_thread_id,
648 source_proposed_plan_id,
649 assistant_message_id,
650 state,
651 requested_at,
652 started_at,
653 completed_at,
654 checkpoint_turn_count,
655 checkpoint_ref,
656 checkpoint_status,
657 checkpoint_files_json
658 )
659 VALUES
660 (
661 'thread-context',
662 'turn-1',
663 NULL,
664 NULL,
665 NULL,
666 NULL,
667 'completed',
668 '2026-03-02T00:00:04.000Z',
669 '2026-03-02T00:00:04.000Z',
670 '2026-03-02T00:00:04.000Z',
671 1,
672 'checkpoint-a',
673 'ready',
674 '[]'
675 ),
676 (
677 'thread-context',
678 'turn-2',
679 NULL,
680 NULL,
681 NULL,
682 NULL,
683 'completed',
684 '2026-03-02T00:00:05.000Z',
685 '2026-03-02T00:00:05.000Z',
686 '2026-03-02T00:00:05.000Z',
687 2,
688 'checkpoint-b',
689 'ready',
690 '[]'
691 )
692 `;
693
694 const context = yield* snapshotQuery.getThreadCheckpointContext(
695 ThreadId.make("thread-context"),
696 );
697 assert.equal(context._tag, "Some");
698 if (context._tag === "Some") {
699 assert.deepEqual(context.value, {
700 threadId: ThreadId.make("thread-context"),
701 projectId: asProjectId("project-context"),
702 workspaceRoot: "/tmp/context-workspace",
703 worktreePath: "/tmp/context-worktree",
704 checkpoints: [
705 {
706 turnId: asTurnId("turn-1"),
707 checkpointTurnCount: 1,
708 checkpointRef: asCheckpointRef("checkpoint-a"),
709 status: "ready",
710 files: [],
711 assistantMessageId: null,
712 completedAt: "2026-03-02T00:00:04.000Z",
713 },
714 {
715 turnId: asTurnId("turn-2"),
716 checkpointTurnCount: 2,
717 checkpointRef: asCheckpointRef("checkpoint-b"),
718 status: "ready",
719 files: [],
720 assistantMessageId: null,
721 completedAt: "2026-03-02T00:00:05.000Z",
722 },
723 ],
724 });
725 }
726 }),
727 );
728
729 it.effect("keeps thread detail activity ordering consistent with shell snapshot ordering", () =>
730 Effect.gen(function* () {
731 const snapshotQuery = yield* ProjectionSnapshotQuery;
732 const sql = yield* SqlClient.SqlClient;
733
734 yield* sql`DELETE FROM projection_projects`;
735 yield* sql`DELETE FROM projection_threads`;
736 yield* sql`DELETE FROM projection_thread_activities`;
737 yield* sql`DELETE FROM projection_state`;
738
739 yield* sql`
740 INSERT INTO projection_projects (
741 project_id,
742 title,
743 workspace_root,
744 default_model_selection_json,
745 scripts_json,
746 created_at,
747 updated_at,
748 deleted_at
749 )
750 VALUES (
751 'project-1',
752 'Project 1',
753 '/tmp/project-1',
754 '{"provider":"codex","model":"gpt-5-codex"}',
755 '[]',
756 '2026-04-01T00:00:00.000Z',
757 '2026-04-01T00:00:01.000Z',
758 NULL
759 )
760 `;
761
762 yield* sql`
763 INSERT INTO projection_threads (
764 thread_id,
765 project_id,
766 title,
767 model_selection_json,
768 runtime_mode,
769 interaction_mode,
770 branch,
771 worktree_path,
772 latest_turn_id,
773 latest_user_message_at,
774 pending_approval_count,
775 pending_user_input_count,
776 has_actionable_proposed_plan,
777 created_at,
778 updated_at,
779 deleted_at
780 )
781 VALUES (
782 'thread-1',
783 'project-1',
784 'Thread 1',
785 '{"provider":"codex","model":"gpt-5-codex"}',
786 'full-access',
787 'default',
788 NULL,
789 NULL,
790 NULL,
791 NULL,
792 0,
793 0,
794 0,
795 '2026-04-01T00:00:02.000Z',
796 '2026-04-01T00:00:03.000Z',
797 NULL
798 )
799 `;
800
801 yield* sql`
802 INSERT INTO projection_thread_activities (
803 activity_id,
804 thread_id,
805 turn_id,
806 tone,
807 kind,
808 summary,
809 payload_json,
810 sequence,
811 created_at
812 )
813 VALUES
814 (
815 'activity-unsequenced',
816 'thread-1',
817 NULL,
818 'info',
819 'runtime.note',
820 'unsequenced first',
821 '{"source":"unsequenced"}',
822 NULL,
823 '2026-04-01T00:00:06.000Z'
824 ),
825 (
826 'activity-sequence-2',
827 'thread-1',
828 NULL,
829 'info',
830 'runtime.note',
831 'sequence two',
832 '{"source":"sequence-2"}',
833 2,
834 '2026-04-01T00:00:04.000Z'
835 ),
836 (
837 'activity-sequence-1',
838 'thread-1',
839 NULL,
840 'info',
841 'runtime.note',
842 'sequence one',
843 '{"source":"sequence-1"}',
844 1,
845 '2026-04-01T00:00:05.000Z'
846 )
847 `;
848
849 const snapshot = yield* snapshotQuery.getSnapshot();
850 const threadDetail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1"));
851
852 assert.equal(threadDetail._tag, "Some");
853 if (threadDetail._tag === "Some") {
854 assert.deepEqual(threadDetail.value.activities, snapshot.threads[0]?.activities ?? []);
855 }
856
857 assert.deepEqual(snapshot.threads[0]?.activities ?? [], [
858 {
859 id: asEventId("activity-unsequenced"),
860 tone: "info",
861 kind: "runtime.note",
862 summary: "unsequenced first",
863 payload: { source: "unsequenced" },
864 turnId: null,
865 createdAt: "2026-04-01T00:00:06.000Z",
866 },
867 {
868 id: asEventId("activity-sequence-1"),
869 tone: "info",
870 kind: "runtime.note",
871 summary: "sequence one",
872 payload: { source: "sequence-1" },
873 turnId: null,
874 sequence: 1,
875 createdAt: "2026-04-01T00:00:05.000Z",
876 },
877 {
878 id: asEventId("activity-sequence-2"),
879 tone: "info",
880 kind: "runtime.note",
881 summary: "sequence two",
882 payload: { source: "sequence-2" },
883 turnId: null,
884 sequence: 2,
885 createdAt: "2026-04-01T00:00:04.000Z",
886 },
887 ]);
888 }),
889 );
890
891 it.effect("uses projection_threads.latest_turn_id for targeted thread latest turn queries", () =>
892 Effect.gen(function* () {
893 const snapshotQuery = yield* ProjectionSnapshotQuery;
894 const sql = yield* SqlClient.SqlClient;
895
896 yield* sql`DELETE FROM projection_projects`;
897 yield* sql`DELETE FROM projection_threads`;
898 yield* sql`DELETE FROM projection_turns`;
899
900 yield* sql`
901 INSERT INTO projection_projects (
902 project_id,
903 title,
904 workspace_root,
905 default_model_selection_json,
906 scripts_json,
907 created_at,
908 updated_at,
909 deleted_at
910 )
911 VALUES (
912 'project-1',
913 'Project 1',
914 '/tmp/project-1',
915 '{"provider":"codex","model":"gpt-5-codex"}',
916 '[]',
917 '2026-04-02T00:00:00.000Z',
918 '2026-04-02T00:00:01.000Z',
919 NULL
920 )
921 `;
922
923 yield* sql`
924 INSERT INTO projection_threads (
925 thread_id,
926 project_id,
927 title,
928 model_selection_json,
929 runtime_mode,
930 interaction_mode,
931 branch,
932 worktree_path,
933 latest_turn_id,
934 latest_user_message_at,
935 pending_approval_count,
936 pending_user_input_count,
937 has_actionable_proposed_plan,
938 created_at,
939 updated_at,
940 archived_at,
941 deleted_at
942 )
943 VALUES (
944 'thread-1',
945 'project-1',
946 'Thread 1',
947 '{"provider":"codex","model":"gpt-5-codex"}',
948 'full-access',
949 'default',
950 NULL,
951 NULL,
952 'turn-running',
953 '2026-04-02T00:00:04.000Z',
954 0,
955 0,
956 0,
957 '2026-04-02T00:00:02.000Z',
958 '2026-04-02T00:00:03.000Z',
959 NULL,
960 NULL
961 )
962 `;
963
964 yield* sql`
965 INSERT INTO projection_turns (
966 thread_id,
967 turn_id,
968 pending_message_id,
969 source_proposed_plan_thread_id,
970 source_proposed_plan_id,
971 assistant_message_id,
972 state,
973 requested_at,
974 started_at,
975 completed_at,
976 checkpoint_turn_count,
977 checkpoint_ref,
978 checkpoint_status,
979 checkpoint_files_json
980 )
981 VALUES
982 (
983 'thread-1',
984 'turn-completed',
985 'message-user-1',
986 NULL,
987 NULL,
988 'message-assistant-1',
989 'completed',
990 '2026-04-02T00:00:05.000Z',
991 '2026-04-02T00:00:06.000Z',
992 '2026-04-02T00:00:20.000Z',
993 5,
994 'checkpoint-5',
995 'ready',
996 '[]'
997 ),
998 (
999 'thread-1',
1000 'turn-running',
1001 'message-user-2',
1002 NULL,
1003 NULL,
1004 NULL,
1005 'running',
1006 '2026-04-02T00:00:30.000Z',
1007 '2026-04-02T00:00:30.000Z',
1008 NULL,
1009 NULL,
1010 NULL,
1011 NULL,
1012 '[]'
1013 )
1014 `;
1015
1016 const threadShell = yield* snapshotQuery.getThreadShellById(ThreadId.make("thread-1"));
1017 assert.equal(threadShell._tag, "Some");
1018 if (threadShell._tag === "Some") {
1019 assert.equal(threadShell.value.latestTurn?.turnId, asTurnId("turn-running"));
1020 assert.equal(threadShell.value.latestTurn?.state, "running");
1021 assert.equal(threadShell.value.latestTurn?.startedAt, "2026-04-02T00:00:30.000Z");
1022 }
1023
1024 const threadDetail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1"));
1025 assert.equal(threadDetail._tag, "Some");
1026 if (threadDetail._tag === "Some") {
1027 assert.equal(threadDetail.value.latestTurn?.turnId, asTurnId("turn-running"));
1028 assert.equal(threadDetail.value.latestTurn?.state, "running");
1029 assert.equal(threadDetail.value.latestTurn?.startedAt, "2026-04-02T00:00:30.000Z");
1030 }
1031 }),
1032 );
1033
1034 it.effect("uses projection_threads.latest_turn_id for bulk command and shell snapshots", () =>
1035 Effect.gen(function* () {
1036 const snapshotQuery = yield* ProjectionSnapshotQuery;
1037 const sql = yield* SqlClient.SqlClient;
1038
1039 yield* sql`DELETE FROM projection_projects`;
1040 yield* sql`DELETE FROM projection_threads`;
1041 yield* sql`DELETE FROM projection_turns`;
1042 yield* sql`DELETE FROM projection_state`;
1043
1044 yield* sql`
1045 INSERT INTO projection_projects (
1046 project_id,
1047 title,
1048 workspace_root,
1049 default_model_selection_json,
1050 scripts_json,
1051 created_at,
1052 updated_at,
1053 deleted_at
1054 )
1055 VALUES (
1056 'project-1',
1057 'Project 1',
1058 '/tmp/project-1',
1059 '{"provider":"codex","model":"gpt-5-codex"}',
1060 '[]',
1061 '2026-04-03T00:00:00.000Z',
1062 '2026-04-03T00:00:01.000Z',
1063 NULL
1064 )
1065 `;
1066
1067 yield* sql`
1068 INSERT INTO projection_threads (
1069 thread_id,
1070 project_id,
1071 title,
1072 model_selection_json,
1073 runtime_mode,
1074 interaction_mode,
1075 branch,
1076 worktree_path,
1077 latest_turn_id,
1078 latest_user_message_at,
1079 pending_approval_count,
1080 pending_user_input_count,
1081 has_actionable_proposed_plan,
1082 created_at,
1083 updated_at,
1084 archived_at,
1085 deleted_at
1086 )
1087 VALUES (
1088 'thread-1',
1089 'project-1',
1090 'Thread 1',
1091 '{"provider":"codex","model":"gpt-5-codex"}',
1092 'full-access',
1093 'default',
1094 NULL,
1095 NULL,
1096 'turn-running',
1097 '2026-04-03T00:00:04.000Z',
1098 0,
1099 0,
1100 0,
1101 '2026-04-03T00:00:02.000Z',
1102 '2026-04-03T00:00:03.000Z',
1103 NULL,
1104 NULL
1105 )
1106 `;
1107
1108 yield* sql`
1109 INSERT INTO projection_turns (
1110 thread_id,
1111 turn_id,
1112 pending_message_id,
1113 source_proposed_plan_thread_id,
1114 source_proposed_plan_id,
1115 assistant_message_id,
1116 state,
1117 requested_at,
1118 started_at,
1119 completed_at,
1120 checkpoint_turn_count,
1121 checkpoint_ref,
1122 checkpoint_status,
1123 checkpoint_files_json
1124 )
1125 VALUES
1126 (
1127 'thread-1',
1128 'turn-running',
1129 'message-user-2',
1130 NULL,
1131 NULL,
1132 NULL,
1133 'running',
1134 '2026-04-03T00:00:30.000Z',
1135 '2026-04-03T00:00:30.000Z',
1136 NULL,
1137 NULL,
1138 NULL,
1139 NULL,
1140 '[]'
1141 ),
1142 (
1143 'thread-1',
1144 'turn-completed',
1145 'message-user-1',
1146 NULL,
1147 NULL,
1148 'message-assistant-1',
1149 'completed',
1150 '2026-04-03T00:00:05.000Z',
1151 '2026-04-03T00:00:06.000Z',
1152 '2026-04-03T00:00:20.000Z',
1153 NULL,
1154 NULL,
1155 NULL,
1156 '[]'
1157 )
1158 `;
1159
1160 yield* sql`
1161 INSERT INTO projection_state (projector, last_applied_sequence, updated_at)
1162 VALUES
1163 (${ORCHESTRATION_PROJECTOR_NAMES.projects}, 3, '2026-04-03T00:00:40.000Z'),
1164 (${ORCHESTRATION_PROJECTOR_NAMES.threads}, 3, '2026-04-03T00:00:40.000Z'),
1165 (${ORCHESTRATION_PROJECTOR_NAMES.threadMessages}, 3, '2026-04-03T00:00:40.000Z'),
1166 (${ORCHESTRATION_PROJECTOR_NAMES.threadProposedPlans}, 3, '2026-04-03T00:00:40.000Z'),
1167 (${ORCHESTRATION_PROJECTOR_NAMES.threadActivities}, 3, '2026-04-03T00:00:40.000Z'),
1168 (${ORCHESTRATION_PROJECTOR_NAMES.threadSessions}, 3, '2026-04-03T00:00:40.000Z'),
1169 (${ORCHESTRATION_PROJECTOR_NAMES.checkpoints}, 3, '2026-04-03T00:00:40.000Z')
1170 `;
1171
1172 const commandReadModel = yield* snapshotQuery.getCommandReadModel();
1173 assert.equal(commandReadModel.threads[0]?.latestTurn?.turnId, asTurnId("turn-running"));
1174 assert.equal(commandReadModel.threads[0]?.latestTurn?.state, "running");
1175
1176 const shellSnapshot = yield* snapshotQuery.getShellSnapshot();
1177 assert.equal(shellSnapshot.threads[0]?.latestTurn?.turnId, asTurnId("turn-running"));
1178 assert.equal(shellSnapshot.threads[0]?.latestTurn?.state, "running");
1179
1180 const fullSnapshot = yield* snapshotQuery.getSnapshot();
1181 assert.equal(fullSnapshot.threads[0]?.latestTurn?.turnId, asTurnId("turn-running"));
1182 assert.equal(fullSnapshot.threads[0]?.latestTurn?.state, "running");
1183 }),
1184 );
1185
1186 it.effect("keeps deleted project and thread tombstones in the command read model", () =>
1187 Effect.gen(function* () {
1188 const snapshotQuery = yield* ProjectionSnapshotQuery;
1189 const sql = yield* SqlClient.SqlClient;
1190
1191 yield* sql`DELETE FROM projection_projects`;
1192 yield* sql`DELETE FROM projection_threads`;
1193 yield* sql`DELETE FROM projection_turns`;
1194 yield* sql`DELETE FROM projection_state`;
1195
1196 yield* sql`
1197 INSERT INTO projection_projects (
1198 project_id,
1199 title,
1200 workspace_root,
1201 default_model_selection_json,
1202 scripts_json,
1203 created_at,
1204 updated_at,
1205 deleted_at
1206 )
1207 VALUES (
1208 'project-deleted',
1209 'Deleted Project',
1210 '/tmp/deleted-project',
1211 '{"provider":"codex","model":"gpt-5-codex"}',
1212 '[]',
1213 '2026-04-05T00:00:00.000Z',
1214 '2026-04-05T00:00:01.000Z',
1215 '2026-04-05T00:00:02.000Z'
1216 )
1217 `;
1218
1219 yield* sql`
1220 INSERT INTO projection_threads (
1221 thread_id,
1222 project_id,
1223 title,
1224 model_selection_json,
1225 runtime_mode,
1226 interaction_mode,
1227 branch,
1228 worktree_path,
1229 latest_turn_id,
1230 latest_user_message_at,
1231 pending_approval_count,
1232 pending_user_input_count,
1233 has_actionable_proposed_plan,
1234 created_at,
1235 updated_at,
1236 archived_at,
1237 deleted_at
1238 )
1239 VALUES (
1240 'thread-deleted',
1241 'project-deleted',
1242 'Deleted Thread',
1243 '{"provider":"codex","model":"gpt-5-codex"}',
1244 'full-access',
1245 'default',
1246 NULL,
1247 NULL,
1248 'turn-deleted',
1249 NULL,
1250 0,
1251 0,
1252 0,
1253 '2026-04-05T00:00:03.000Z',
1254 '2026-04-05T00:00:04.000Z',
1255 NULL,
1256 '2026-04-05T00:00:05.000Z'
1257 )
1258 `;
1259
1260 yield* sql`
1261 INSERT INTO projection_turns (
1262 thread_id,
1263 turn_id,
1264 pending_message_id,
1265 source_proposed_plan_thread_id,
1266 source_proposed_plan_id,
1267 assistant_message_id,
1268 state,
1269 requested_at,
1270 started_at,
1271 completed_at,
1272 checkpoint_turn_count,
1273 checkpoint_ref,
1274 checkpoint_status,
1275 checkpoint_files_json
1276 )
1277 VALUES (
1278 'thread-deleted',
1279 'turn-deleted',
1280 'message-deleted-user',
1281 NULL,
1282 NULL,
1283 'message-deleted-assistant',
1284 'completed',
1285 '2026-04-05T00:00:04.100Z',
1286 '2026-04-05T00:00:04.200Z',
1287 '2026-04-05T00:00:04.300Z',
1288 NULL,
1289 NULL,
1290 NULL,
1291 '[]'
1292 )
1293 `;
1294
1295 const commandReadModel = yield* snapshotQuery.getCommandReadModel();
1296 assert.equal(commandReadModel.projects[0]?.id, asProjectId("project-deleted"));
1297 assert.equal(commandReadModel.projects[0]?.deletedAt, "2026-04-05T00:00:02.000Z");
1298 assert.equal(commandReadModel.threads[0]?.id, ThreadId.make("thread-deleted"));
1299 assert.equal(commandReadModel.threads[0]?.deletedAt, "2026-04-05T00:00:05.000Z");
1300 assert.equal(commandReadModel.threads[0]?.latestTurn?.turnId, asTurnId("turn-deleted"));
1301 assert.equal(commandReadModel.threads[0]?.latestTurn?.state, "completed");
1302
1303 const fullSnapshot = yield* snapshotQuery.getSnapshot();
1304 assert.equal(fullSnapshot.threads[0]?.id, ThreadId.make("thread-deleted"));
1305 assert.equal(fullSnapshot.threads[0]?.latestTurn?.turnId, asTurnId("turn-deleted"));
1306 assert.equal(fullSnapshot.threads[0]?.latestTurn?.state, "completed");
1307
1308 const shellSnapshot = yield* snapshotQuery.getShellSnapshot();
1309 assert.equal(shellSnapshot.projects.length, 0);
1310 assert.equal(shellSnapshot.threads.length, 0);
1311 }),
1312 );
1313});
1314
1315it.effect(
1316 "ProjectionSnapshotQuery dedupes repository identity resolution by workspace root and skips deleted projects for shell snapshots",
1317 () => {
1318 const resolveCalls: string[] = [];
1319 const layer = OrchestrationProjectionSnapshotQueryLive.pipe(
1320 Layer.provideMerge(
1321 Layer.succeed(RepositoryIdentityResolver, {
1322 resolve: (cwd: string) =>
1323 Effect.sync(() => {
1324 resolveCalls.push(cwd);
1325 return {
1326 canonicalKey: `github.com/acme${cwd}`,
1327 locator: {
1328 source: "git-remote" as const,
1329 remoteName: "origin",
1330 remoteUrl: `https://github.com/acme${cwd}.git`,
1331 },
1332 rootPath: cwd,
1333 };
1334 }),
1335 }),
1336 ),
1337 Layer.provideMerge(SqlitePersistenceMemory),
1338 );
1339
1340 return Effect.gen(function* () {
1341 const snapshotQuery = yield* ProjectionSnapshotQuery;
1342 const sql = yield* SqlClient.SqlClient;
1343
1344 yield* sql`DELETE FROM projection_projects`;
1345 yield* sql`DELETE FROM projection_threads`;
1346 yield* sql`DELETE FROM projection_turns`;
1347 yield* sql`DELETE FROM projection_state`;
1348
1349 yield* sql`
1350 INSERT INTO projection_projects (
1351 project_id,
1352 title,
1353 workspace_root,
1354 default_model_selection_json,
1355 scripts_json,
1356 created_at,
1357 updated_at,
1358 deleted_at
1359 )
1360 VALUES
1361 (
1362 'project-1',
1363 'Shared Project 1',
1364 '/tmp/shared-root',
1365 '{"provider":"codex","model":"gpt-5-codex"}',
1366 '[]',
1367 '2026-04-04T00:00:00.000Z',
1368 '2026-04-04T00:00:01.000Z',
1369 NULL
1370 ),
1371 (
1372 'project-2',
1373 'Shared Project 2',
1374 '/tmp/shared-root',
1375 '{"provider":"codex","model":"gpt-5-codex"}',
1376 '[]',
1377 '2026-04-04T00:00:02.000Z',
1378 '2026-04-04T00:00:03.000Z',
1379 NULL
1380 ),
1381 (
1382 'project-3',
1383 'Deleted Project',
1384 '/tmp/deleted-root',
1385 '{"provider":"codex","model":"gpt-5-codex"}',
1386 '[]',
1387 '2026-04-04T00:00:04.000Z',
1388 '2026-04-04T00:00:05.000Z',
1389 '2026-04-04T00:00:06.000Z'
1390 )
1391 `;
1392
1393 const shellSnapshot = yield* snapshotQuery.getShellSnapshot();
1394 assert.deepStrictEqual(resolveCalls.toSorted(), ["/tmp/shared-root"]);
1395 assert.equal(shellSnapshot.projects.length, 2);
1396 assert.equal(shellSnapshot.projects[0]?.repositoryIdentity?.rootPath, "/tmp/shared-root");
1397 assert.equal(shellSnapshot.projects[1]?.repositoryIdentity?.rootPath, "/tmp/shared-root");
1398
1399 resolveCalls.length = 0;
1400
1401 const fullSnapshot = yield* snapshotQuery.getSnapshot();
1402 assert.deepStrictEqual(resolveCalls.toSorted(), ["/tmp/deleted-root", "/tmp/shared-root"]);
1403 assert.equal(fullSnapshot.projects.length, 3);
1404 assert.equal(fullSnapshot.projects[2]?.repositoryIdentity?.rootPath, "/tmp/deleted-root");
1405 }).pipe(Effect.provide(layer));
1406 },
1407);