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.makeUnsafe(value);
13const asTurnId = (value: string): TurnId => TurnId.makeUnsafe(value);
14const asMessageId = (value: string): MessageId => MessageId.makeUnsafe(value);
15const asEventId = (value: string): EventId => EventId.makeUnsafe(value);
16const asCheckpointRef = (value: string): CheckpointRef => CheckpointRef.makeUnsafe(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 branch,
66 worktree_path,
67 latest_turn_id,
68 created_at,
69 updated_at,
70 deleted_at
71 )
72 VALUES (
73 'thread-1',
74 'project-1',
75 'Thread 1',
76 '{"provider":"codex","model":"gpt-5-codex"}',
77 NULL,
78 NULL,
79 'turn-1',
80 '2026-02-24T00:00:02.000Z',
81 '2026-02-24T00:00:03.000Z',
82 NULL
83 )
84 `;
85
86 yield* sql`
87 INSERT INTO projection_thread_messages (
88 message_id,
89 thread_id,
90 turn_id,
91 role,
92 text,
93 is_streaming,
94 created_at,
95 updated_at
96 )
97 VALUES (
98 'message-1',
99 'thread-1',
100 'turn-1',
101 'assistant',
102 'hello from projection',
103 0,
104 '2026-02-24T00:00:04.000Z',
105 '2026-02-24T00:00:05.000Z'
106 )
107 `;
108
109 yield* sql`
110 INSERT INTO projection_thread_proposed_plans (
111 plan_id,
112 thread_id,
113 turn_id,
114 plan_markdown,
115 implemented_at,
116 implementation_thread_id,
117 created_at,
118 updated_at
119 )
120 VALUES (
121 'plan-1',
122 'thread-1',
123 'turn-1',
124 '# Ship it',
125 '2026-02-24T00:00:05.500Z',
126 'thread-2',
127 '2026-02-24T00:00:05.000Z',
128 '2026-02-24T00:00:05.500Z'
129 )
130 `;
131
132 yield* sql`
133 INSERT INTO projection_thread_activities (
134 activity_id,
135 thread_id,
136 turn_id,
137 tone,
138 kind,
139 summary,
140 payload_json,
141 created_at
142 )
143 VALUES (
144 'activity-1',
145 'thread-1',
146 'turn-1',
147 'info',
148 'runtime.note',
149 'provider started',
150 '{"stage":"start"}',
151 '2026-02-24T00:00:06.000Z'
152 )
153 `;
154
155 yield* sql`
156 INSERT INTO projection_thread_sessions (
157 thread_id,
158 status,
159 provider_name,
160 provider_session_id,
161 provider_thread_id,
162 runtime_mode,
163 active_turn_id,
164 last_error,
165 updated_at
166 )
167 VALUES (
168 'thread-1',
169 'running',
170 'codex',
171 'provider-session-1',
172 'provider-thread-1',
173 'approval-required',
174 'turn-1',
175 NULL,
176 '2026-02-24T00:00:07.000Z'
177 )
178 `;
179
180 yield* sql`
181 INSERT INTO projection_turns (
182 thread_id,
183 turn_id,
184 pending_message_id,
185 source_proposed_plan_thread_id,
186 source_proposed_plan_id,
187 assistant_message_id,
188 state,
189 requested_at,
190 started_at,
191 completed_at,
192 checkpoint_turn_count,
193 checkpoint_ref,
194 checkpoint_status,
195 checkpoint_files_json
196 )
197 VALUES (
198 'thread-1',
199 'turn-1',
200 NULL,
201 'thread-1',
202 'plan-1',
203 'message-1',
204 'completed',
205 '2026-02-24T00:00:08.000Z',
206 '2026-02-24T00:00:08.000Z',
207 '2026-02-24T00:00:08.000Z',
208 1,
209 'checkpoint-1',
210 'ready',
211 '[{"path":"README.md","kind":"modified","additions":2,"deletions":1}]'
212 )
213 `;
214
215 let sequence = 5;
216 for (const projector of Object.values(ORCHESTRATION_PROJECTOR_NAMES)) {
217 yield* sql`
218 INSERT INTO projection_state (
219 projector,
220 last_applied_sequence,
221 updated_at
222 )
223 VALUES (
224 ${projector},
225 ${sequence},
226 '2026-02-24T00:00:09.000Z'
227 )
228 `;
229 sequence += 1;
230 }
231
232 const snapshot = yield* snapshotQuery.getSnapshot();
233
234 assert.equal(snapshot.snapshotSequence, 5);
235 assert.equal(snapshot.updatedAt, "2026-02-24T00:00:09.000Z");
236 assert.deepEqual(snapshot.projects, [
237 {
238 id: asProjectId("project-1"),
239 title: "Project 1",
240 workspaceRoot: "/tmp/project-1",
241 repositoryIdentity: null,
242 defaultModelSelection: {
243 provider: "codex",
244 model: "gpt-5-codex",
245 },
246 scripts: [
247 {
248 id: "script-1",
249 name: "Build",
250 command: "bun run build",
251 icon: "build",
252 runOnWorktreeCreate: false,
253 },
254 ],
255 createdAt: "2026-02-24T00:00:00.000Z",
256 updatedAt: "2026-02-24T00:00:01.000Z",
257 deletedAt: null,
258 },
259 ]);
260 assert.deepEqual(snapshot.threads, [
261 {
262 id: ThreadId.makeUnsafe("thread-1"),
263 projectId: asProjectId("project-1"),
264 title: "Thread 1",
265 modelSelection: {
266 provider: "codex",
267 model: "gpt-5-codex",
268 },
269 interactionMode: "default",
270 runtimeMode: "full-access",
271 branch: null,
272 worktreePath: null,
273 latestTurn: {
274 turnId: asTurnId("turn-1"),
275 state: "completed",
276 requestedAt: "2026-02-24T00:00:08.000Z",
277 startedAt: "2026-02-24T00:00:08.000Z",
278 completedAt: "2026-02-24T00:00:08.000Z",
279 assistantMessageId: asMessageId("message-1"),
280 sourceProposedPlan: {
281 threadId: ThreadId.makeUnsafe("thread-1"),
282 planId: "plan-1",
283 },
284 },
285 createdAt: "2026-02-24T00:00:02.000Z",
286 updatedAt: "2026-02-24T00:00:03.000Z",
287 archivedAt: null,
288 deletedAt: null,
289 messages: [
290 {
291 id: asMessageId("message-1"),
292 role: "assistant",
293 text: "hello from projection",
294 turnId: asTurnId("turn-1"),
295 streaming: false,
296 createdAt: "2026-02-24T00:00:04.000Z",
297 updatedAt: "2026-02-24T00:00:05.000Z",
298 },
299 ],
300 proposedPlans: [
301 {
302 id: "plan-1",
303 turnId: asTurnId("turn-1"),
304 planMarkdown: "# Ship it",
305 implementedAt: "2026-02-24T00:00:05.500Z",
306 implementationThreadId: ThreadId.makeUnsafe("thread-2"),
307 createdAt: "2026-02-24T00:00:05.000Z",
308 updatedAt: "2026-02-24T00:00:05.500Z",
309 },
310 ],
311 activities: [
312 {
313 id: asEventId("activity-1"),
314 tone: "info",
315 kind: "runtime.note",
316 summary: "provider started",
317 payload: { stage: "start" },
318 turnId: asTurnId("turn-1"),
319 createdAt: "2026-02-24T00:00:06.000Z",
320 },
321 ],
322 checkpoints: [
323 {
324 turnId: asTurnId("turn-1"),
325 checkpointTurnCount: 1,
326 checkpointRef: asCheckpointRef("checkpoint-1"),
327 status: "ready",
328 files: [{ path: "README.md", kind: "modified", additions: 2, deletions: 1 }],
329 assistantMessageId: asMessageId("message-1"),
330 completedAt: "2026-02-24T00:00:08.000Z",
331 },
332 ],
333 session: {
334 threadId: ThreadId.makeUnsafe("thread-1"),
335 status: "running",
336 providerName: "codex",
337 runtimeMode: "approval-required",
338 activeTurnId: asTurnId("turn-1"),
339 lastError: null,
340 updatedAt: "2026-02-24T00:00:07.000Z",
341 },
342 },
343 ]);
344 }),
345 );
346
347 it.effect(
348 "reads targeted project, thread, and count queries without hydrating the full snapshot",
349 () =>
350 Effect.gen(function* () {
351 const snapshotQuery = yield* ProjectionSnapshotQuery;
352 const sql = yield* SqlClient.SqlClient;
353
354 yield* sql`DELETE FROM projection_projects`;
355 yield* sql`DELETE FROM projection_threads`;
356 yield* sql`DELETE FROM projection_turns`;
357
358 yield* sql`
359 INSERT INTO projection_projects (
360 project_id,
361 title,
362 workspace_root,
363 default_model_selection_json,
364 scripts_json,
365 created_at,
366 updated_at,
367 deleted_at
368 )
369 VALUES
370 (
371 'project-active',
372 'Active Project',
373 '/tmp/workspace',
374 '{"provider":"codex","model":"gpt-5-codex"}',
375 '[]',
376 '2026-03-01T00:00:00.000Z',
377 '2026-03-01T00:00:01.000Z',
378 NULL
379 ),
380 (
381 'project-deleted',
382 'Deleted Project',
383 '/tmp/deleted',
384 NULL,
385 '[]',
386 '2026-03-01T00:00:02.000Z',
387 '2026-03-01T00:00:03.000Z',
388 '2026-03-01T00:00:04.000Z'
389 )
390 `;
391
392 yield* sql`
393 INSERT INTO projection_threads (
394 thread_id,
395 project_id,
396 title,
397 model_selection_json,
398 runtime_mode,
399 interaction_mode,
400 branch,
401 worktree_path,
402 latest_turn_id,
403 created_at,
404 updated_at,
405 archived_at,
406 deleted_at
407 )
408 VALUES
409 (
410 'thread-first',
411 'project-active',
412 'First Thread',
413 '{"provider":"codex","model":"gpt-5-codex"}',
414 'full-access',
415 'default',
416 NULL,
417 NULL,
418 NULL,
419 '2026-03-01T00:00:05.000Z',
420 '2026-03-01T00:00:06.000Z',
421 NULL,
422 NULL
423 ),
424 (
425 'thread-second',
426 'project-active',
427 'Second Thread',
428 '{"provider":"codex","model":"gpt-5-codex"}',
429 'full-access',
430 'default',
431 NULL,
432 NULL,
433 NULL,
434 '2026-03-01T00:00:07.000Z',
435 '2026-03-01T00:00:08.000Z',
436 NULL,
437 NULL
438 ),
439 (
440 'thread-deleted',
441 'project-active',
442 'Deleted Thread',
443 '{"provider":"codex","model":"gpt-5-codex"}',
444 'full-access',
445 'default',
446 NULL,
447 NULL,
448 NULL,
449 '2026-03-01T00:00:09.000Z',
450 '2026-03-01T00:00:10.000Z',
451 NULL,
452 '2026-03-01T00:00:11.000Z'
453 )
454 `;
455
456 const counts = yield* snapshotQuery.getCounts();
457 assert.deepEqual(counts, {
458 projectCount: 2,
459 threadCount: 3,
460 });
461
462 const project = yield* snapshotQuery.getActiveProjectByWorkspaceRoot("/tmp/workspace");
463 assert.equal(project._tag, "Some");
464 if (project._tag === "Some") {
465 assert.equal(project.value.id, asProjectId("project-active"));
466 }
467
468 const missingProject = yield* snapshotQuery.getActiveProjectByWorkspaceRoot("/tmp/missing");
469 assert.equal(missingProject._tag, "None");
470
471 const firstThreadId = yield* snapshotQuery.getFirstActiveThreadIdByProjectId(
472 asProjectId("project-active"),
473 );
474 assert.equal(firstThreadId._tag, "Some");
475 if (firstThreadId._tag === "Some") {
476 assert.equal(firstThreadId.value, ThreadId.makeUnsafe("thread-first"));
477 }
478 }),
479 );
480
481 it.effect("reads single-thread checkpoint context without hydrating unrelated threads", () =>
482 Effect.gen(function* () {
483 const snapshotQuery = yield* ProjectionSnapshotQuery;
484 const sql = yield* SqlClient.SqlClient;
485
486 yield* sql`DELETE FROM projection_projects`;
487 yield* sql`DELETE FROM projection_threads`;
488 yield* sql`DELETE FROM projection_turns`;
489
490 yield* sql`
491 INSERT INTO projection_projects (
492 project_id,
493 title,
494 workspace_root,
495 default_model_selection_json,
496 scripts_json,
497 created_at,
498 updated_at,
499 deleted_at
500 )
501 VALUES (
502 'project-context',
503 'Context Project',
504 '/tmp/context-workspace',
505 NULL,
506 '[]',
507 '2026-03-02T00:00:00.000Z',
508 '2026-03-02T00:00:01.000Z',
509 NULL
510 )
511 `;
512
513 yield* sql`
514 INSERT INTO projection_threads (
515 thread_id,
516 project_id,
517 title,
518 model_selection_json,
519 runtime_mode,
520 interaction_mode,
521 branch,
522 worktree_path,
523 latest_turn_id,
524 created_at,
525 updated_at,
526 archived_at,
527 deleted_at
528 )
529 VALUES (
530 'thread-context',
531 'project-context',
532 'Context Thread',
533 '{"provider":"codex","model":"gpt-5-codex"}',
534 'full-access',
535 'default',
536 'feature/perf',
537 '/tmp/context-worktree',
538 NULL,
539 '2026-03-02T00:00:02.000Z',
540 '2026-03-02T00:00:03.000Z',
541 NULL,
542 NULL
543 )
544 `;
545
546 yield* sql`
547 INSERT INTO projection_turns (
548 thread_id,
549 turn_id,
550 pending_message_id,
551 source_proposed_plan_thread_id,
552 source_proposed_plan_id,
553 assistant_message_id,
554 state,
555 requested_at,
556 started_at,
557 completed_at,
558 checkpoint_turn_count,
559 checkpoint_ref,
560 checkpoint_status,
561 checkpoint_files_json
562 )
563 VALUES
564 (
565 'thread-context',
566 'turn-1',
567 NULL,
568 NULL,
569 NULL,
570 NULL,
571 'completed',
572 '2026-03-02T00:00:04.000Z',
573 '2026-03-02T00:00:04.000Z',
574 '2026-03-02T00:00:04.000Z',
575 1,
576 'checkpoint-a',
577 'ready',
578 '[]'
579 ),
580 (
581 'thread-context',
582 'turn-2',
583 NULL,
584 NULL,
585 NULL,
586 NULL,
587 'completed',
588 '2026-03-02T00:00:05.000Z',
589 '2026-03-02T00:00:05.000Z',
590 '2026-03-02T00:00:05.000Z',
591 2,
592 'checkpoint-b',
593 'ready',
594 '[]'
595 )
596 `;
597
598 const context = yield* snapshotQuery.getThreadCheckpointContext(
599 ThreadId.makeUnsafe("thread-context"),
600 );
601 assert.equal(context._tag, "Some");
602 if (context._tag === "Some") {
603 assert.deepEqual(context.value, {
604 threadId: ThreadId.makeUnsafe("thread-context"),
605 projectId: asProjectId("project-context"),
606 workspaceRoot: "/tmp/context-workspace",
607 worktreePath: "/tmp/context-worktree",
608 checkpoints: [
609 {
610 turnId: asTurnId("turn-1"),
611 checkpointTurnCount: 1,
612 checkpointRef: asCheckpointRef("checkpoint-a"),
613 status: "ready",
614 files: [],
615 assistantMessageId: null,
616 completedAt: "2026-03-02T00:00:04.000Z",
617 },
618 {
619 turnId: asTurnId("turn-2"),
620 checkpointTurnCount: 2,
621 checkpointRef: asCheckpointRef("checkpoint-b"),
622 status: "ready",
623 files: [],
624 assistantMessageId: null,
625 completedAt: "2026-03-02T00:00:05.000Z",
626 },
627 ],
628 });
629 }
630 }),
631 );
632});