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 { 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,
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 '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,
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 '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 defaultModel: "gpt-5-codex",
238 scripts: [
239 {
240 id: "script-1",
241 name: "Build",
242 command: "bun run build",
243 icon: "build",
244 runOnWorktreeCreate: false,
245 },
246 ],
247 createdAt: "2026-02-24T00:00:00.000Z",
248 updatedAt: "2026-02-24T00:00:01.000Z",
249 deletedAt: null,
250 },
251 ]);
252 assert.deepEqual(snapshot.threads, [
253 {
254 id: ThreadId.makeUnsafe("thread-1"),
255 projectId: asProjectId("project-1"),
256 title: "Thread 1",
257 model: "gpt-5-codex",
258 interactionMode: "default",
259 runtimeMode: "full-access",
260 branch: null,
261 worktreePath: null,
262 latestTurn: {
263 turnId: asTurnId("turn-1"),
264 state: "completed",
265 requestedAt: "2026-02-24T00:00:08.000Z",
266 startedAt: "2026-02-24T00:00:08.000Z",
267 completedAt: "2026-02-24T00:00:08.000Z",
268 assistantMessageId: asMessageId("message-1"),
269 sourceProposedPlan: {
270 threadId: ThreadId.makeUnsafe("thread-1"),
271 planId: "plan-1",
272 },
273 },
274 createdAt: "2026-02-24T00:00:02.000Z",
275 updatedAt: "2026-02-24T00:00:03.000Z",
276 deletedAt: null,
277 messages: [
278 {
279 id: asMessageId("message-1"),
280 role: "assistant",
281 text: "hello from projection",
282 turnId: asTurnId("turn-1"),
283 streaming: false,
284 createdAt: "2026-02-24T00:00:04.000Z",
285 updatedAt: "2026-02-24T00:00:05.000Z",
286 },
287 ],
288 proposedPlans: [
289 {
290 id: "plan-1",
291 turnId: asTurnId("turn-1"),
292 planMarkdown: "# Ship it",
293 implementedAt: "2026-02-24T00:00:05.500Z",
294 implementationThreadId: ThreadId.makeUnsafe("thread-2"),
295 createdAt: "2026-02-24T00:00:05.000Z",
296 updatedAt: "2026-02-24T00:00:05.500Z",
297 },
298 ],
299 activities: [
300 {
301 id: asEventId("activity-1"),
302 tone: "info",
303 kind: "runtime.note",
304 summary: "provider started",
305 payload: { stage: "start" },
306 turnId: asTurnId("turn-1"),
307 createdAt: "2026-02-24T00:00:06.000Z",
308 },
309 ],
310 checkpoints: [
311 {
312 turnId: asTurnId("turn-1"),
313 checkpointTurnCount: 1,
314 checkpointRef: asCheckpointRef("checkpoint-1"),
315 status: "ready",
316 files: [{ path: "README.md", kind: "modified", additions: 2, deletions: 1 }],
317 assistantMessageId: asMessageId("message-1"),
318 completedAt: "2026-02-24T00:00:08.000Z",
319 },
320 ],
321 session: {
322 threadId: ThreadId.makeUnsafe("thread-1"),
323 status: "running",
324 providerName: "codex",
325 runtimeMode: "approval-required",
326 activeTurnId: asTurnId("turn-1"),
327 lastError: null,
328 updatedAt: "2026-02-24T00:00:07.000Z",
329 },
330 },
331 ]);
332 }),
333 );
334});