Open History to see the last commit
1import { CheckpointRef, EventId, MessageId, ProjectId, ThreadId, TurnId } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import { Effect, Layer } from "effect";
4import * as SqlClient from "effect/unstable/sql/SqlClient";
5
6import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
7import { RepositoryIdentityResolverLive } from "../../project/Layers/RepositoryIdentityResolver.ts";
8import { ORCHESTRATION_PROJECTOR_NAMES } from "./ProjectionPipeline.ts";
9import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
10import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
11
12const asProjectId = (value: string): ProjectId => ProjectId.make(value);
13const asTurnId = (value: string): TurnId => TurnId.make(value);
14const asMessageId = (value: string): MessageId => MessageId.make(value);
15const asEventId = (value: string): EventId => EventId.make(value);
16const asCheckpointRef = (value: string): CheckpointRef => CheckpointRef.make(value);
17
18const projectionSnapshotLayer = it.layer(
19 OrchestrationProjectionSnapshotQueryLive.pipe(
20 Layer.provideMerge(RepositoryIdentityResolverLive),
21 Layer.provideMerge(SqlitePersistenceMemory),
22 ),
23);
24
25projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
26 it.effect("hydrates read model from projection tables and computes snapshot sequence", () =>
27 Effect.gen(function* () {
28 const snapshotQuery = yield* ProjectionSnapshotQuery;
29 const sql = yield* SqlClient.SqlClient;
30
31 yield* sql`DELETE FROM projection_projects`;
32 yield* sql`DELETE FROM projection_state`;
33 yield* sql`DELETE FROM projection_thread_proposed_plans`;
34 yield* sql`DELETE FROM projection_turns`;
35
36 yield* sql`
37 INSERT INTO projection_projects (
38 project_id,
39 title,
40 workspace_root,
41 default_model_selection_json,
42 scripts_json,
43 created_at,
44 updated_at,
45 deleted_at
46 )
47 VALUES (
48 'project-1',
49 'Project 1',
50 '/tmp/project-1',
51 '{"provider":"codex","model":"gpt-5-codex"}',
52 '[{"id":"script-1","name":"Build","command":"bun run build","icon":"build","runOnWorktreeCreate":false}]',
53 '2026-02-24T00:00:00.000Z',
54 '2026-02-24T00:00:01.000Z',
55 NULL
56 )
57 `;
58
59 yield* sql`
60 INSERT INTO projection_threads (
61 thread_id,
62 project_id,
63 title,
64 model_selection_json,
65 runtime_mode,
66 interaction_mode,
67 branch,
68 worktree_path,
69 latest_turn_id,
70 latest_user_message_at,
71 pending_approval_count,
72 pending_user_input_count,
73 has_actionable_proposed_plan,
74 created_at,
75 updated_at,
76 deleted_at
77 )
78 VALUES (
79 'thread-1',
80 'project-1',
81 'Thread 1',
82 '{"provider":"codex","model":"gpt-5-codex"}',
83 'full-access',
84 'default',
85 NULL,
86 NULL,
87 'turn-1',
88 '2026-02-24T00:00:04.000Z',
89 1,
90 0,
91 0,
92 '2026-02-24T00:00:02.000Z',
93 '2026-02-24T00:00:03.000Z',
94 NULL
95 )
96 `;
97
98 yield* sql`
99 INSERT INTO projection_thread_messages (
100 message_id,
101 thread_id,
102 turn_id,
103 role,
104 text,
105 is_streaming,
106 created_at,
107 updated_at
108 )
109 VALUES (
110 'message-1',
111 'thread-1',
112 'turn-1',
113 'assistant',
114 'hello from projection',
115 0,
116 '2026-02-24T00:00:04.000Z',
117 '2026-02-24T00:00:05.000Z'
118 )
119 `;
120
121 yield* sql`
122 INSERT INTO projection_thread_proposed_plans (
123 plan_id,
124 thread_id,
125 turn_id,
126 plan_markdown,
127 implemented_at,
128 implementation_thread_id,
129 created_at,
130 updated_at
131 )
132 VALUES (
133 'plan-1',
134 'thread-1',
135 'turn-1',
136 '# Ship it',
137 '2026-02-24T00:00:05.500Z',
138 'thread-2',
139 '2026-02-24T00:00:05.000Z',
140 '2026-02-24T00:00:05.500Z'
141 )
142 `;
143
144 yield* sql`
145 INSERT INTO projection_thread_activities (
146 activity_id,
147 thread_id,
148 turn_id,
149 tone,
150 kind,
151 summary,
152 payload_json,
153 created_at
154 )
155 VALUES (
156 'activity-1',
157 'thread-1',
158 'turn-1',
159 'info',
160 'runtime.note',
161 'provider started',
162 '{"stage":"start"}',
163 '2026-02-24T00:00:06.000Z'
164 )
165 `;
166
167 yield* sql`
168 INSERT INTO projection_thread_sessions (
169 thread_id,
170 status,
171 provider_name,
172 provider_session_id,
173 provider_thread_id,
174 runtime_mode,
175 active_turn_id,
176 last_error,
177 updated_at
178 )
179 VALUES (
180 'thread-1',
181 'running',
182 'codex',
183 'provider-session-1',
184 'provider-thread-1',
185 'approval-required',
186 'turn-1',
187 NULL,
188 '2026-02-24T00:00:07.000Z'
189 )
190 `;
191
192 yield* sql`
193 INSERT INTO projection_turns (
194 thread_id,
195 turn_id,
196 pending_message_id,
197 source_proposed_plan_thread_id,
198 source_proposed_plan_id,
199 assistant_message_id,
200 state,
201 requested_at,
202 started_at,
203 completed_at,
204 checkpoint_turn_count,
205 checkpoint_ref,
206 checkpoint_status,
207 checkpoint_files_json
208 )
209 VALUES (
210 'thread-1',
211 'turn-1',
212 NULL,
213 'thread-1',
214 'plan-1',
215 'message-1',
216 'completed',
217 '2026-02-24T00:00:08.000Z',
218 '2026-02-24T00:00:08.000Z',
219 '2026-02-24T00:00:08.000Z',
220 1,
221 'checkpoint-1',
222 'ready',
223 '[{"path":"README.md","kind":"modified","additions":2,"deletions":1}]'
224 )
225 `;
226
227 let sequence = 5;
228 for (const projector of Object.values(ORCHESTRATION_PROJECTOR_NAMES)) {
229 yield* sql`
230 INSERT INTO projection_state (
231 projector,
232 last_applied_sequence,
233 updated_at
234 )
235 VALUES (
236 ${projector},
237 ${sequence},
238 '2026-02-24T00:00:09.000Z'
239 )
240 `;
241 sequence += 1;
242 }
243
244 const snapshot = yield* snapshotQuery.getSnapshot();
245
246 assert.equal(snapshot.snapshotSequence, 5);
247 assert.equal(snapshot.updatedAt, "2026-02-24T00:00:09.000Z");
248 assert.deepEqual(snapshot.projects, [
249 {
250 id: asProjectId("project-1"),
251 title: "Project 1",
252 workspaceRoot: "/tmp/project-1",
253 repositoryIdentity: null,
254 defaultModelSelection: {
255 provider: "codex",
256 model: "gpt-5-codex",
257 },
258 scripts: [
259 {
260 id: "script-1",
261 name: "Build",
262 command: "bun run build",
263 icon: "build",
264 runOnWorktreeCreate: false,
265 },
266 ],
267 createdAt: "2026-02-24T00:00:00.000Z",
268 updatedAt: "2026-02-24T00:00:01.000Z",
269 deletedAt: null,
270 },
271 ]);
272 assert.deepEqual(snapshot.threads, [
273 {
274 id: ThreadId.make("thread-1"),
275 projectId: asProjectId("project-1"),
276 title: "Thread 1",
277 modelSelection: {
278 provider: "codex",
279 model: "gpt-5-codex",
280 },
281 interactionMode: "default",
282 runtimeMode: "full-access",
283 branch: null,
284 worktreePath: null,
285 latestTurn: {
286 turnId: asTurnId("turn-1"),
287 state: "completed",
288 requestedAt: "2026-02-24T00:00:08.000Z",
289 startedAt: "2026-02-24T00:00:08.000Z",
290 completedAt: "2026-02-24T00:00:08.000Z",
291 assistantMessageId: asMessageId("message-1"),
292 sourceProposedPlan: {
293 threadId: ThreadId.make("thread-1"),
294 planId: "plan-1",
295 },
296 },
297 createdAt: "2026-02-24T00:00:02.000Z",
298 updatedAt: "2026-02-24T00:00:03.000Z",
299 archivedAt: null,
300 deletedAt: null,
301 messages: [
302 {
303 id: asMessageId("message-1"),
304 role: "assistant",
305 text: "hello from projection",
306 turnId: asTurnId("turn-1"),
307 streaming: false,
308 createdAt: "2026-02-24T00:00:04.000Z",
309 updatedAt: "2026-02-24T00:00:05.000Z",
310 },
311 ],
312 proposedPlans: [
313 {
314 id: "plan-1",
315 turnId: asTurnId("turn-1"),
316 planMarkdown: "# Ship it",
317 implementedAt: "2026-02-24T00:00:05.500Z",
318 implementationThreadId: ThreadId.make("thread-2"),
319 createdAt: "2026-02-24T00:00:05.000Z",
320 updatedAt: "2026-02-24T00:00:05.500Z",
321 },
322 ],
323 activities: [
324 {
325 id: asEventId("activity-1"),
326 tone: "info",
327 kind: "runtime.note",
328 summary: "provider started",
329 payload: { stage: "start" },
330 turnId: asTurnId("turn-1"),
331 createdAt: "2026-02-24T00:00:06.000Z",
332 },
333 ],
334 checkpoints: [
335 {
336 turnId: asTurnId("turn-1"),
337 checkpointTurnCount: 1,
338 checkpointRef: asCheckpointRef("checkpoint-1"),
339 status: "ready",
340 files: [{ path: "README.md", kind: "modified", additions: 2, deletions: 1 }],
341 assistantMessageId: asMessageId("message-1"),
342 completedAt: "2026-02-24T00:00:08.000Z",
343 },
344 ],
345 session: {
346 threadId: ThreadId.make("thread-1"),
347 status: "running",
348 providerName: "codex",
349 runtimeMode: "approval-required",
350 activeTurnId: asTurnId("turn-1"),
351 lastError: null,
352 updatedAt: "2026-02-24T00:00:07.000Z",
353 },
354 },
355 ]);
356
357 const shellSnapshot = yield* snapshotQuery.getShellSnapshot();
358 assert.equal(shellSnapshot.snapshotSequence, 5);
359 assert.deepEqual(shellSnapshot.projects, [
360 {
361 id: asProjectId("project-1"),
362 title: "Project 1",
363 workspaceRoot: "/tmp/project-1",
364 repositoryIdentity: null,
365 defaultModelSelection: {
366 provider: "codex",
367 model: "gpt-5-codex",
368 },
369 scripts: [
370 {
371 id: "script-1",
372 name: "Build",
373 command: "bun run build",
374 icon: "build",
375 runOnWorktreeCreate: false,
376 },
377 ],
378 createdAt: "2026-02-24T00:00:00.000Z",
379 updatedAt: "2026-02-24T00:00:01.000Z",
380 },
381 ]);
382 assert.deepEqual(shellSnapshot.threads, [
383 {
384 id: ThreadId.make("thread-1"),
385 projectId: asProjectId("project-1"),
386 title: "Thread 1",
387 modelSelection: {
388 provider: "codex",
389 model: "gpt-5-codex",
390 },
391 interactionMode: "default",
392 runtimeMode: "full-access",
393 branch: null,
394 worktreePath: null,
395 latestTurn: {
396 turnId: asTurnId("turn-1"),
397 state: "completed",
398 requestedAt: "2026-02-24T00:00:08.000Z",
399 startedAt: "2026-02-24T00:00:08.000Z",
400 completedAt: "2026-02-24T00:00:08.000Z",
401 assistantMessageId: asMessageId("message-1"),
402 sourceProposedPlan: {
403 threadId: ThreadId.make("thread-1"),
404 planId: "plan-1",
405 },
406 },
407 createdAt: "2026-02-24T00:00:02.000Z",
408 updatedAt: "2026-02-24T00:00:03.000Z",
409 archivedAt: null,
410 session: {
411 threadId: ThreadId.make("thread-1"),
412 status: "running",
413 providerName: "codex",
414 runtimeMode: "approval-required",
415 activeTurnId: asTurnId("turn-1"),
416 lastError: null,
417 updatedAt: "2026-02-24T00:00:07.000Z",
418 },
419 latestUserMessageAt: "2026-02-24T00:00:04.000Z",
420 hasPendingApprovals: true,
421 hasPendingUserInput: false,
422 hasActionableProposedPlan: false,
423 },
424 ]);
425
426 const threadDetail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1"));
427 assert.equal(threadDetail._tag, "Some");
428 if (threadDetail._tag === "Some") {
429 assert.deepEqual(threadDetail.value, snapshot.threads[0]);
430 }
431 }),
432 );
433
434 it.effect(
435 "reads targeted project, thread, and count queries without hydrating the full snapshot",
436 () =>
437 Effect.gen(function* () {
438 const snapshotQuery = yield* ProjectionSnapshotQuery;
439 const sql = yield* SqlClient.SqlClient;
440
441 yield* sql`DELETE FROM projection_projects`;
442 yield* sql`DELETE FROM projection_threads`;
443 yield* sql`DELETE FROM projection_turns`;
444
445 yield* sql`
446 INSERT INTO projection_projects (
447 project_id,
448 title,
449 workspace_root,
450 default_model_selection_json,
451 scripts_json,
452 created_at,
453 updated_at,
454 deleted_at
455 )
456 VALUES
457 (
458 'project-active',
459 'Active Project',
460 '/tmp/workspace',
461 '{"provider":"codex","model":"gpt-5-codex"}',
462 '[]',
463 '2026-03-01T00:00:00.000Z',
464 '2026-03-01T00:00:01.000Z',
465 NULL
466 ),
467 (
468 'project-deleted',
469 'Deleted Project',
470 '/tmp/deleted',
471 NULL,
472 '[]',
473 '2026-03-01T00:00:02.000Z',
474 '2026-03-01T00:00:03.000Z',
475 '2026-03-01T00:00:04.000Z'
476 )
477 `;
478
479 yield* sql`
480 INSERT INTO projection_threads (
481 thread_id,
482 project_id,
483 title,
484 model_selection_json,
485 runtime_mode,
486 interaction_mode,
487 branch,
488 worktree_path,
489 latest_turn_id,
490 created_at,
491 updated_at,
492 archived_at,
493 deleted_at
494 )
495 VALUES
496 (
497 'thread-first',
498 'project-active',
499 'First Thread',
500 '{"provider":"codex","model":"gpt-5-codex"}',
501 'full-access',
502 'default',
503 NULL,
504 NULL,
505 NULL,
506 '2026-03-01T00:00:05.000Z',
507 '2026-03-01T00:00:06.000Z',
508 NULL,
509 NULL
510 ),
511 (
512 'thread-second',
513 'project-active',
514 'Second Thread',
515 '{"provider":"codex","model":"gpt-5-codex"}',
516 'full-access',
517 'default',
518 NULL,
519 NULL,
520 NULL,
521 '2026-03-01T00:00:07.000Z',
522 '2026-03-01T00:00:08.000Z',
523 NULL,
524 NULL
525 ),
526 (
527 'thread-deleted',
528 'project-active',
529 'Deleted Thread',
530 '{"provider":"codex","model":"gpt-5-codex"}',
531 'full-access',
532 'default',
533 NULL,
534 NULL,
535 NULL,
536 '2026-03-01T00:00:09.000Z',
537 '2026-03-01T00:00:10.000Z',
538 NULL,
539 '2026-03-01T00:00:11.000Z'
540 )
541 `;
542
543 const counts = yield* snapshotQuery.getCounts();
544 assert.deepEqual(counts, {
545 projectCount: 2,
546 threadCount: 3,
547 });
548
549 const project = yield* snapshotQuery.getActiveProjectByWorkspaceRoot("/tmp/workspace");
550 assert.equal(project._tag, "Some");
551 if (project._tag === "Some") {
552 assert.equal(project.value.id, asProjectId("project-active"));
553 }
554
555 const missingProject = yield* snapshotQuery.getActiveProjectByWorkspaceRoot("/tmp/missing");
556 assert.equal(missingProject._tag, "None");
557
558 const firstThreadId = yield* snapshotQuery.getFirstActiveThreadIdByProjectId(
559 asProjectId("project-active"),
560 );
561 assert.equal(firstThreadId._tag, "Some");
562 if (firstThreadId._tag === "Some") {
563 assert.equal(firstThreadId.value, ThreadId.make("thread-first"));
564 }
565 }),
566 );
567
568 it.effect("reads single-thread checkpoint context without hydrating unrelated threads", () =>
569 Effect.gen(function* () {
570 const snapshotQuery = yield* ProjectionSnapshotQuery;
571 const sql = yield* SqlClient.SqlClient;
572
573 yield* sql`DELETE FROM projection_projects`;
574 yield* sql`DELETE FROM projection_threads`;
575 yield* sql`DELETE FROM projection_turns`;
576
577 yield* sql`
578 INSERT INTO projection_projects (
579 project_id,
580 title,
581 workspace_root,
582 default_model_selection_json,
583 scripts_json,
584 created_at,
585 updated_at,
586 deleted_at
587 )
588 VALUES (
589 'project-context',
590 'Context Project',
591 '/tmp/context-workspace',
592 NULL,
593 '[]',
594 '2026-03-02T00:00:00.000Z',
595 '2026-03-02T00:00:01.000Z',
596 NULL
597 )
598 `;
599
600 yield* sql`
601 INSERT INTO projection_threads (
602 thread_id,
603 project_id,
604 title,
605 model_selection_json,
606 runtime_mode,
607 interaction_mode,
608 branch,
609 worktree_path,
610 latest_turn_id,
611 created_at,
612 updated_at,
613 archived_at,
614 deleted_at
615 )
616 VALUES (
617 'thread-context',
618 'project-context',
619 'Context Thread',
620 '{"provider":"codex","model":"gpt-5-codex"}',
621 'full-access',
622 'default',
623 'feature/perf',
624 '/tmp/context-worktree',
625 NULL,
626 '2026-03-02T00:00:02.000Z',
627 '2026-03-02T00:00:03.000Z',
628 NULL,
629 NULL
630 )
631 `;
632
633 yield* sql`
634 INSERT INTO projection_turns (
635 thread_id,
636 turn_id,
637 pending_message_id,
638 source_proposed_plan_thread_id,
639 source_proposed_plan_id,
640 assistant_message_id,
641 state,
642 requested_at,
643 started_at,
644 completed_at,
645 checkpoint_turn_count,
646 checkpoint_ref,
647 checkpoint_status,
648 checkpoint_files_json
649 )
650 VALUES
651 (
652 'thread-context',
653 'turn-1',
654 NULL,
655 NULL,
656 NULL,
657 NULL,
658 'completed',
659 '2026-03-02T00:00:04.000Z',
660 '2026-03-02T00:00:04.000Z',
661 '2026-03-02T00:00:04.000Z',
662 1,
663 'checkpoint-a',
664 'ready',
665 '[]'
666 ),
667 (
668 'thread-context',
669 'turn-2',
670 NULL,
671 NULL,
672 NULL,
673 NULL,
674 'completed',
675 '2026-03-02T00:00:05.000Z',
676 '2026-03-02T00:00:05.000Z',
677 '2026-03-02T00:00:05.000Z',
678 2,
679 'checkpoint-b',
680 'ready',
681 '[]'
682 )
683 `;
684
685 const context = yield* snapshotQuery.getThreadCheckpointContext(
686 ThreadId.make("thread-context"),
687 );
688 assert.equal(context._tag, "Some");
689 if (context._tag === "Some") {
690 assert.deepEqual(context.value, {
691 threadId: ThreadId.make("thread-context"),
692 projectId: asProjectId("project-context"),
693 workspaceRoot: "/tmp/context-workspace",
694 worktreePath: "/tmp/context-worktree",
695 checkpoints: [
696 {
697 turnId: asTurnId("turn-1"),
698 checkpointTurnCount: 1,
699 checkpointRef: asCheckpointRef("checkpoint-a"),
700 status: "ready",
701 files: [],
702 assistantMessageId: null,
703 completedAt: "2026-03-02T00:00:04.000Z",
704 },
705 {
706 turnId: asTurnId("turn-2"),
707 checkpointTurnCount: 2,
708 checkpointRef: asCheckpointRef("checkpoint-b"),
709 status: "ready",
710 files: [],
711 assistantMessageId: null,
712 completedAt: "2026-03-02T00:00:05.000Z",
713 },
714 ],
715 });
716 }
717 }),
718 );
719
720 it.effect("keeps thread detail activity ordering consistent with shell snapshot ordering", () =>
721 Effect.gen(function* () {
722 const snapshotQuery = yield* ProjectionSnapshotQuery;
723 const sql = yield* SqlClient.SqlClient;
724
725 yield* sql`DELETE FROM projection_projects`;
726 yield* sql`DELETE FROM projection_threads`;
727 yield* sql`DELETE FROM projection_thread_activities`;
728 yield* sql`DELETE FROM projection_state`;
729
730 yield* sql`
731 INSERT INTO projection_projects (
732 project_id,
733 title,
734 workspace_root,
735 default_model_selection_json,
736 scripts_json,
737 created_at,
738 updated_at,
739 deleted_at
740 )
741 VALUES (
742 'project-1',
743 'Project 1',
744 '/tmp/project-1',
745 '{"provider":"codex","model":"gpt-5-codex"}',
746 '[]',
747 '2026-04-01T00:00:00.000Z',
748 '2026-04-01T00:00:01.000Z',
749 NULL
750 )
751 `;
752
753 yield* sql`
754 INSERT INTO projection_threads (
755 thread_id,
756 project_id,
757 title,
758 model_selection_json,
759 runtime_mode,
760 interaction_mode,
761 branch,
762 worktree_path,
763 latest_turn_id,
764 latest_user_message_at,
765 pending_approval_count,
766 pending_user_input_count,
767 has_actionable_proposed_plan,
768 created_at,
769 updated_at,
770 deleted_at
771 )
772 VALUES (
773 'thread-1',
774 'project-1',
775 'Thread 1',
776 '{"provider":"codex","model":"gpt-5-codex"}',
777 'full-access',
778 'default',
779 NULL,
780 NULL,
781 NULL,
782 NULL,
783 0,
784 0,
785 0,
786 '2026-04-01T00:00:02.000Z',
787 '2026-04-01T00:00:03.000Z',
788 NULL
789 )
790 `;
791
792 yield* sql`
793 INSERT INTO projection_thread_activities (
794 activity_id,
795 thread_id,
796 turn_id,
797 tone,
798 kind,
799 summary,
800 payload_json,
801 sequence,
802 created_at
803 )
804 VALUES
805 (
806 'activity-unsequenced',
807 'thread-1',
808 NULL,
809 'info',
810 'runtime.note',
811 'unsequenced first',
812 '{"source":"unsequenced"}',
813 NULL,
814 '2026-04-01T00:00:06.000Z'
815 ),
816 (
817 'activity-sequence-2',
818 'thread-1',
819 NULL,
820 'info',
821 'runtime.note',
822 'sequence two',
823 '{"source":"sequence-2"}',
824 2,
825 '2026-04-01T00:00:04.000Z'
826 ),
827 (
828 'activity-sequence-1',
829 'thread-1',
830 NULL,
831 'info',
832 'runtime.note',
833 'sequence one',
834 '{"source":"sequence-1"}',
835 1,
836 '2026-04-01T00:00:05.000Z'
837 )
838 `;
839
840 const snapshot = yield* snapshotQuery.getSnapshot();
841 const threadDetail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1"));
842
843 assert.equal(threadDetail._tag, "Some");
844 if (threadDetail._tag === "Some") {
845 assert.deepEqual(threadDetail.value.activities, snapshot.threads[0]?.activities ?? []);
846 }
847
848 assert.deepEqual(snapshot.threads[0]?.activities ?? [], [
849 {
850 id: asEventId("activity-unsequenced"),
851 tone: "info",
852 kind: "runtime.note",
853 summary: "unsequenced first",
854 payload: { source: "unsequenced" },
855 turnId: null,
856 createdAt: "2026-04-01T00:00:06.000Z",
857 },
858 {
859 id: asEventId("activity-sequence-1"),
860 tone: "info",
861 kind: "runtime.note",
862 summary: "sequence one",
863 payload: { source: "sequence-1" },
864 turnId: null,
865 sequence: 1,
866 createdAt: "2026-04-01T00:00:05.000Z",
867 },
868 {
869 id: asEventId("activity-sequence-2"),
870 tone: "info",
871 kind: "runtime.note",
872 summary: "sequence two",
873 payload: { source: "sequence-2" },
874 turnId: null,
875 sequence: 2,
876 createdAt: "2026-04-01T00:00:04.000Z",
877 },
878 ]);
879 }),
880 );
881
882 it.effect("uses projection_threads.latest_turn_id for targeted thread latest turn queries", () =>
883 Effect.gen(function* () {
884 const snapshotQuery = yield* ProjectionSnapshotQuery;
885 const sql = yield* SqlClient.SqlClient;
886
887 yield* sql`DELETE FROM projection_projects`;
888 yield* sql`DELETE FROM projection_threads`;
889 yield* sql`DELETE FROM projection_turns`;
890
891 yield* sql`
892 INSERT INTO projection_projects (
893 project_id,
894 title,
895 workspace_root,
896 default_model_selection_json,
897 scripts_json,
898 created_at,
899 updated_at,
900 deleted_at
901 )
902 VALUES (
903 'project-1',
904 'Project 1',
905 '/tmp/project-1',
906 '{"provider":"codex","model":"gpt-5-codex"}',
907 '[]',
908 '2026-04-02T00:00:00.000Z',
909 '2026-04-02T00:00:01.000Z',
910 NULL
911 )
912 `;
913
914 yield* sql`
915 INSERT INTO projection_threads (
916 thread_id,
917 project_id,
918 title,
919 model_selection_json,
920 runtime_mode,
921 interaction_mode,
922 branch,
923 worktree_path,
924 latest_turn_id,
925 latest_user_message_at,
926 pending_approval_count,
927 pending_user_input_count,
928 has_actionable_proposed_plan,
929 created_at,
930 updated_at,
931 archived_at,
932 deleted_at
933 )
934 VALUES (
935 'thread-1',
936 'project-1',
937 'Thread 1',
938 '{"provider":"codex","model":"gpt-5-codex"}',
939 'full-access',
940 'default',
941 NULL,
942 NULL,
943 'turn-running',
944 '2026-04-02T00:00:04.000Z',
945 0,
946 0,
947 0,
948 '2026-04-02T00:00:02.000Z',
949 '2026-04-02T00:00:03.000Z',
950 NULL,
951 NULL
952 )
953 `;
954
955 yield* sql`
956 INSERT INTO projection_turns (
957 thread_id,
958 turn_id,
959 pending_message_id,
960 source_proposed_plan_thread_id,
961 source_proposed_plan_id,
962 assistant_message_id,
963 state,
964 requested_at,
965 started_at,
966 completed_at,
967 checkpoint_turn_count,
968 checkpoint_ref,
969 checkpoint_status,
970 checkpoint_files_json
971 )
972 VALUES
973 (
974 'thread-1',
975 'turn-completed',
976 'message-user-1',
977 NULL,
978 NULL,
979 'message-assistant-1',
980 'completed',
981 '2026-04-02T00:00:05.000Z',
982 '2026-04-02T00:00:06.000Z',
983 '2026-04-02T00:00:20.000Z',
984 5,
985 'checkpoint-5',
986 'ready',
987 '[]'
988 ),
989 (
990 'thread-1',
991 'turn-running',
992 'message-user-2',
993 NULL,
994 NULL,
995 NULL,
996 'running',
997 '2026-04-02T00:00:30.000Z',
998 '2026-04-02T00:00:30.000Z',
999 NULL,
1000 NULL,
1001 NULL,
1002 NULL,
1003 '[]'
1004 )
1005 `;
1006
1007 const threadShell = yield* snapshotQuery.getThreadShellById(ThreadId.make("thread-1"));
1008 assert.equal(threadShell._tag, "Some");
1009 if (threadShell._tag === "Some") {
1010 assert.equal(threadShell.value.latestTurn?.turnId, asTurnId("turn-running"));
1011 assert.equal(threadShell.value.latestTurn?.state, "running");
1012 assert.equal(threadShell.value.latestTurn?.startedAt, "2026-04-02T00:00:30.000Z");
1013 }
1014
1015 const threadDetail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1"));
1016 assert.equal(threadDetail._tag, "Some");
1017 if (threadDetail._tag === "Some") {
1018 assert.equal(threadDetail.value.latestTurn?.turnId, asTurnId("turn-running"));
1019 assert.equal(threadDetail.value.latestTurn?.state, "running");
1020 assert.equal(threadDetail.value.latestTurn?.startedAt, "2026-04-02T00:00:30.000Z");
1021 }
1022 }),
1023 );
1024});