Open History to see the last commit
1import {
2 CheckpointRef,
3 EventId,
4 MessageId,
5 ProjectId,
6 ThreadId,
7 TurnId,
8} from "@t3tools/contracts";
9import { assert, it } from "@effect/vitest";
10import { Effect, Layer } from "effect";
11import * as SqlClient from "effect/unstable/sql/SqlClient";
12
13import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
14import { ORCHESTRATION_PROJECTOR_NAMES } from "./ProjectionPipeline.ts";
15import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
16import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
17
18const asProjectId = (value: string): ProjectId => ProjectId.makeUnsafe(value);
19const asTurnId = (value: string): TurnId => TurnId.makeUnsafe(value);
20const asMessageId = (value: string): MessageId => MessageId.makeUnsafe(value);
21const asEventId = (value: string): EventId => EventId.makeUnsafe(value);
22const asCheckpointRef = (value: string): CheckpointRef => CheckpointRef.makeUnsafe(value);
23
24const projectionSnapshotLayer = it.layer(
25 OrchestrationProjectionSnapshotQueryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
26);
27
28projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
29 it.effect("hydrates read model from projection tables and computes snapshot sequence", () =>
30 Effect.gen(function* () {
31 const snapshotQuery = yield* ProjectionSnapshotQuery;
32 const sql = yield* SqlClient.SqlClient;
33
34 yield* sql`DELETE FROM projection_projects`;
35 yield* sql`DELETE FROM projection_state`;
36 yield* sql`DELETE FROM projection_turns`;
37
38 yield* sql`
39 INSERT INTO projection_projects (
40 project_id,
41 title,
42 workspace_root,
43 default_model,
44 scripts_json,
45 created_at,
46 updated_at,
47 deleted_at
48 )
49 VALUES (
50 'project-1',
51 'Project 1',
52 '/tmp/project-1',
53 'gpt-5-codex',
54 '[{"id":"script-1","name":"Build","command":"bun run build","icon":"build","runOnWorktreeCreate":false}]',
55 '2026-02-24T00:00:00.000Z',
56 '2026-02-24T00:00:01.000Z',
57 NULL
58 )
59 `;
60
61 yield* sql`
62 INSERT INTO projection_threads (
63 thread_id,
64 project_id,
65 title,
66 model,
67 branch,
68 worktree_path,
69 latest_turn_id,
70 created_at,
71 updated_at,
72 deleted_at
73 )
74 VALUES (
75 'thread-1',
76 'project-1',
77 'Thread 1',
78 'gpt-5-codex',
79 NULL,
80 NULL,
81 'turn-1',
82 '2026-02-24T00:00:02.000Z',
83 '2026-02-24T00:00:03.000Z',
84 NULL
85 )
86 `;
87
88 yield* sql`
89 INSERT INTO projection_thread_messages (
90 message_id,
91 thread_id,
92 turn_id,
93 role,
94 text,
95 is_streaming,
96 created_at,
97 updated_at
98 )
99 VALUES (
100 'message-1',
101 'thread-1',
102 'turn-1',
103 'assistant',
104 'hello from projection',
105 0,
106 '2026-02-24T00:00:04.000Z',
107 '2026-02-24T00:00:05.000Z'
108 )
109 `;
110
111 yield* sql`
112 INSERT INTO projection_thread_activities (
113 activity_id,
114 thread_id,
115 turn_id,
116 tone,
117 kind,
118 summary,
119 payload_json,
120 created_at
121 )
122 VALUES (
123 'activity-1',
124 'thread-1',
125 'turn-1',
126 'info',
127 'runtime.note',
128 'provider started',
129 '{"stage":"start"}',
130 '2026-02-24T00:00:06.000Z'
131 )
132 `;
133
134 yield* sql`
135 INSERT INTO projection_thread_sessions (
136 thread_id,
137 status,
138 provider_name,
139 provider_session_id,
140 provider_thread_id,
141 runtime_mode,
142 active_turn_id,
143 last_error,
144 updated_at
145 )
146 VALUES (
147 'thread-1',
148 'running',
149 'codex',
150 'provider-session-1',
151 'provider-thread-1',
152 'approval-required',
153 'turn-1',
154 NULL,
155 '2026-02-24T00:00:07.000Z'
156 )
157 `;
158
159 yield* sql`
160 INSERT INTO projection_turns (
161 thread_id,
162 turn_id,
163 pending_message_id,
164 assistant_message_id,
165 state,
166 requested_at,
167 started_at,
168 completed_at,
169 checkpoint_turn_count,
170 checkpoint_ref,
171 checkpoint_status,
172 checkpoint_files_json
173 )
174 VALUES (
175 'thread-1',
176 'turn-1',
177 NULL,
178 'message-1',
179 'completed',
180 '2026-02-24T00:00:08.000Z',
181 '2026-02-24T00:00:08.000Z',
182 '2026-02-24T00:00:08.000Z',
183 1,
184 'checkpoint-1',
185 'ready',
186 '[{"path":"README.md","kind":"modified","additions":2,"deletions":1}]'
187 )
188 `;
189
190 let sequence = 5;
191 for (const projector of Object.values(ORCHESTRATION_PROJECTOR_NAMES)) {
192 yield* sql`
193 INSERT INTO projection_state (
194 projector,
195 last_applied_sequence,
196 updated_at
197 )
198 VALUES (
199 ${projector},
200 ${sequence},
201 '2026-02-24T00:00:09.000Z'
202 )
203 `;
204 sequence += 1;
205 }
206
207 const snapshot = yield* snapshotQuery.getSnapshot();
208
209 assert.equal(snapshot.snapshotSequence, 5);
210 assert.equal(snapshot.updatedAt, "2026-02-24T00:00:09.000Z");
211 assert.deepEqual(snapshot.projects, [
212 {
213 id: asProjectId("project-1"),
214 title: "Project 1",
215 workspaceRoot: "/tmp/project-1",
216 defaultModel: "gpt-5-codex",
217 scripts: [
218 {
219 id: "script-1",
220 name: "Build",
221 command: "bun run build",
222 icon: "build",
223 runOnWorktreeCreate: false,
224 },
225 ],
226 createdAt: "2026-02-24T00:00:00.000Z",
227 updatedAt: "2026-02-24T00:00:01.000Z",
228 deletedAt: null,
229 },
230 ]);
231 assert.deepEqual(snapshot.threads, [
232 {
233 id: ThreadId.makeUnsafe("thread-1"),
234 projectId: asProjectId("project-1"),
235 title: "Thread 1",
236 model: "gpt-5-codex",
237 interactionMode: "default",
238 runtimeMode: "full-access",
239 branch: null,
240 worktreePath: null,
241 latestTurn: {
242 turnId: asTurnId("turn-1"),
243 state: "completed",
244 requestedAt: "2026-02-24T00:00:08.000Z",
245 startedAt: "2026-02-24T00:00:08.000Z",
246 completedAt: "2026-02-24T00:00:08.000Z",
247 assistantMessageId: asMessageId("message-1"),
248 },
249 createdAt: "2026-02-24T00:00:02.000Z",
250 updatedAt: "2026-02-24T00:00:03.000Z",
251 deletedAt: null,
252 messages: [
253 {
254 id: asMessageId("message-1"),
255 role: "assistant",
256 text: "hello from projection",
257 turnId: asTurnId("turn-1"),
258 streaming: false,
259 createdAt: "2026-02-24T00:00:04.000Z",
260 updatedAt: "2026-02-24T00:00:05.000Z",
261 },
262 ],
263 proposedPlans: [],
264 activities: [
265 {
266 id: asEventId("activity-1"),
267 tone: "info",
268 kind: "runtime.note",
269 summary: "provider started",
270 payload: { stage: "start" },
271 turnId: asTurnId("turn-1"),
272 createdAt: "2026-02-24T00:00:06.000Z",
273 },
274 ],
275 checkpoints: [
276 {
277 turnId: asTurnId("turn-1"),
278 checkpointTurnCount: 1,
279 checkpointRef: asCheckpointRef("checkpoint-1"),
280 status: "ready",
281 files: [{ path: "README.md", kind: "modified", additions: 2, deletions: 1 }],
282 assistantMessageId: asMessageId("message-1"),
283 completedAt: "2026-02-24T00:00:08.000Z",
284 },
285 ],
286 session: {
287 threadId: ThreadId.makeUnsafe("thread-1"),
288 status: "running",
289 providerName: "codex",
290 runtimeMode: "approval-required",
291 activeTurnId: asTurnId("turn-1"),
292 lastError: null,
293 updatedAt: "2026-02-24T00:00:07.000Z",
294 },
295 },
296 ]);
297 }),
298 );
299});