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

ProjectionSnapshotQuery.test.ts

1,032 lines28.7 KB

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