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

ProjectionSnapshotQuery.test.ts

627 lines17.5 KB

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