Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import { Effect, Layer } from "effect";
3import * as SqlClient from "effect/unstable/sql/SqlClient";
4
5import { runMigrations } from "../Migrations.ts";
6import * as NodeSqliteClient from "../NodeSqliteClient.ts";
7
8const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layerMemory()));
9
10layer("016_CanonicalizeModelSelections", (it) => {
11 it.effect(
12 "migrates legacy projection rows and event payloads to the canonical model-selection shape",
13 () =>
14 Effect.gen(function* () {
15 const sql = yield* SqlClient.SqlClient;
16
17 // Setup base state
18 {
19 yield* runMigrations({ toMigrationInclusive: 15 });
20
21 yield* sql`
22 INSERT INTO projection_projects (
23 project_id,
24 title,
25 workspace_root,
26 default_model,
27 scripts_json,
28 created_at,
29 updated_at,
30 deleted_at
31 )
32 VALUES
33 ('project-codex', 'Codex project', '/tmp/project-codex', 'gpt-5.4', '[]', '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z', NULL),
34 ('project-claude', 'Claude project', '/tmp/project-claude', 'claude-sonnet-4-6', '[]', '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z', NULL),
35 ('project-null', 'Null project', '/tmp/project-null', NULL, '[]', '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z', NULL)
36 `;
37 yield* sql`
38 UPDATE projection_projects
39 SET default_model = 'claude-opus-4-6'
40 WHERE project_id = 'project-claude'
41 `;
42 yield* sql`
43 INSERT INTO projection_threads (
44 thread_id,
45 project_id,
46 title,
47 model,
48 branch,
49 worktree_path,
50 latest_turn_id,
51 created_at,
52 updated_at,
53 deleted_at,
54 runtime_mode,
55 interaction_mode
56 )
57 VALUES
58 ('thread-session', 'project-codex', 'Session thread', 'gpt-5.4', NULL, NULL, NULL, '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z', NULL, 'full-access', 'default'),
59 ('thread-claude', 'project-claude', 'Claude thread', 'claude-opus-4-6', NULL, NULL, NULL, '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z', NULL, 'full-access', 'default'),
60 ('thread-codex', 'project-codex', 'Codex thread', 'gpt-5.4', NULL, NULL, NULL, '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z', NULL, 'full-access', 'default'),
61 ('thread-legacy-options', 'project-claude', 'Legacy options thread', 'claude-opus-4-6', NULL, NULL, NULL, '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z', NULL, 'full-access', 'default')
62 `;
63 yield* sql`
64 INSERT INTO projection_thread_sessions (
65 thread_id,
66 status,
67 provider_name,
68 provider_session_id,
69 provider_thread_id,
70 active_turn_id,
71 last_error,
72 updated_at,
73 runtime_mode
74 )
75 VALUES (
76 'thread-session',
77 'running',
78 'claudeAgent',
79 'provider-session-1',
80 'provider-thread-1',
81 NULL,
82 NULL,
83 '2026-01-01T00:00:00.000Z',
84 'full-access'
85 )
86 `;
87 yield* sql`
88 INSERT INTO orchestration_events (
89 event_id,
90 aggregate_kind,
91 stream_id,
92 stream_version,
93 event_type,
94 occurred_at,
95 command_id,
96 causation_event_id,
97 correlation_id,
98 actor_kind,
99 payload_json,
100 metadata_json
101 )
102 VALUES
103 (
104 'event-project-created',
105 'project',
106 'project-1',
107 1,
108 'project.created',
109 '2026-01-01T00:00:00.000Z',
110 'command-project-created',
111 NULL,
112 'correlation-project-created',
113 'user',
114 '{"projectId":"project-1","title":"Project","workspaceRoot":"/tmp/project","defaultModel":"claude-opus-4-6","defaultModelOptions":{"codex":{"reasoningEffort":"high"},"claudeAgent":{"effort":"max"}},"scripts":[],"createdAt":"2026-01-01T00:00:00.000Z","updatedAt":"2026-01-01T00:00:00.000Z"}',
115 '{}'
116 ),
117 (
118 'event-project-created-fallback',
119 'project',
120 'project-2',
121 1,
122 'project.created',
123 '2026-01-01T00:00:00.000Z',
124 'command-project-created-fallback',
125 NULL,
126 'correlation-project-created-fallback',
127 'user',
128 '{"projectId":"project-2","title":"Fallback Project","workspaceRoot":"/tmp/project-2","defaultModel":"claude-opus-4-6","defaultModelOptions":{"codex":{"reasoningEffort":"low"}},"scripts":[],"createdAt":"2026-01-01T00:00:00.000Z","updatedAt":"2026-01-01T00:00:00.000Z"}',
129 '{}'
130 ),
131 (
132 'event-project-created-null-model',
133 'project',
134 'project-3',
135 1,
136 'project.created',
137 '2026-01-01T00:00:00.000Z',
138 'command-project-created-null-model',
139 NULL,
140 'correlation-project-created-null-model',
141 'user',
142 '{"projectId":"project-3","title":"Null Model Project","workspaceRoot":"/tmp/project-3","defaultModel":null,"scripts":[],"createdAt":"2026-01-01T00:00:00.000Z","updatedAt":"2026-01-01T00:00:00.000Z"}',
143 '{}'
144 ),
145 (
146 'event-thread-created',
147 'thread',
148 'thread-1',
149 1,
150 'thread.created',
151 '2026-01-01T00:00:00.000Z',
152 'command-thread-created',
153 NULL,
154 'correlation-thread-created',
155 'user',
156 '{"threadId":"thread-1","projectId":"project-1","title":"Thread","model":"claude-opus-4-6","modelOptions":{"codex":{"reasoningEffort":"high"},"claudeAgent":{"effort":"max","thinking":false}},"runtimeMode":"full-access","interactionMode":"default","branch":null,"worktreePath":null,"createdAt":"2026-01-01T00:00:00.000Z","updatedAt":"2026-01-01T00:00:00.000Z"}',
157 '{}'
158 ),
159 (
160 'event-thread-created-fallback',
161 'thread',
162 'thread-2',
163 1,
164 'thread.created',
165 '2026-01-01T00:00:00.000Z',
166 'command-thread-created-fallback',
167 NULL,
168 'correlation-thread-created-fallback',
169 'user',
170 '{"threadId":"thread-2","projectId":"project-1","title":"Fallback Thread","model":"gpt-5.4","modelOptions":{"claudeAgent":{"effort":"max"}},"runtimeMode":"full-access","interactionMode":"default","branch":null,"worktreePath":null,"createdAt":"2026-01-01T00:00:00.000Z","updatedAt":"2026-01-01T00:00:00.000Z"}',
171 '{}'
172 ),
173 (
174 'event-turn-start-requested',
175 'thread',
176 'thread-1',
177 2,
178 'thread.turn-start-requested',
179 '2026-01-01T00:00:00.000Z',
180 'command-turn-start-requested',
181 NULL,
182 'correlation-turn-start-requested',
183 'user',
184 '{"threadId":"thread-1","turnId":"turn-1","input":"hi","model":"gpt-5.4","modelOptions":{"codex":{"fastMode":true},"claudeAgent":{"effort":"max"}},"deliveryMode":"buffered"}',
185 '{}'
186 ),
187 (
188 'event-thread-created-no-model',
189 'thread',
190 'thread-3',
191 1,
192 'thread.created',
193 '2026-01-01T00:00:00.000Z',
194 'command-thread-created-no-model',
195 NULL,
196 'correlation-thread-created-no-model',
197 'user',
198 '{"threadId":"thread-3","projectId":"project-1","title":"Ancient Thread","runtimeMode":"full-access","interactionMode":"default","branch":null,"worktreePath":null,"createdAt":"2026-01-01T00:00:00.000Z","updatedAt":"2026-01-01T00:00:00.000Z"}',
199 '{}'
200 )
201 `;
202 }
203
204 // Execute migration under test
205 yield* runMigrations({ toMigrationInclusive: 16 });
206
207 // Assert expected state
208 {
209 const projectRows = yield* sql<{
210 readonly projectId: string;
211 readonly defaultModelSelection: string | null;
212 }>`
213 SELECT
214 project_id AS "projectId",
215 default_model_selection_json AS "defaultModelSelection"
216 FROM projection_projects
217 ORDER BY project_id
218 `;
219 assert.deepStrictEqual(projectRows, [
220 {
221 projectId: "project-claude",
222 defaultModelSelection: '{"provider":"claudeAgent","model":"claude-opus-4-6"}',
223 },
224 {
225 projectId: "project-codex",
226 defaultModelSelection: '{"provider":"codex","model":"gpt-5.4"}',
227 },
228 { projectId: "project-null", defaultModelSelection: null },
229 ]);
230
231 const threadRows = yield* sql<{
232 readonly threadId: string;
233 readonly modelSelection: string | null;
234 }>`
235 SELECT
236 thread_id AS "threadId",
237 model_selection_json AS "modelSelection"
238 FROM projection_threads
239 ORDER BY thread_id
240 `;
241 assert.deepStrictEqual(threadRows, [
242 {
243 threadId: "thread-claude",
244 modelSelection: '{"provider":"claudeAgent","model":"claude-opus-4-6"}',
245 },
246 {
247 threadId: "thread-codex",
248 modelSelection: '{"provider":"codex","model":"gpt-5.4"}',
249 },
250 {
251 threadId: "thread-legacy-options",
252 modelSelection: '{"provider":"claudeAgent","model":"claude-opus-4-6"}',
253 },
254 {
255 threadId: "thread-session",
256 modelSelection: '{"provider":"claudeAgent","model":"gpt-5.4"}',
257 },
258 ]);
259
260 const eventRows = yield* sql<{
261 readonly payloadJson: string;
262 }>`
263 SELECT payload_json AS "payloadJson"
264 FROM orchestration_events
265 ORDER BY rowid ASC
266 `;
267
268 assert.deepStrictEqual(JSON.parse(eventRows[0]!.payloadJson), {
269 projectId: "project-1",
270 title: "Project",
271 workspaceRoot: "/tmp/project",
272 defaultModelSelection: {
273 provider: "claudeAgent",
274 model: "claude-opus-4-6",
275 options: {
276 effort: "max",
277 },
278 },
279 scripts: [],
280 createdAt: "2026-01-01T00:00:00.000Z",
281 updatedAt: "2026-01-01T00:00:00.000Z",
282 });
283
284 assert.deepStrictEqual(JSON.parse(eventRows[1]!.payloadJson), {
285 projectId: "project-2",
286 title: "Fallback Project",
287 workspaceRoot: "/tmp/project-2",
288 defaultModelSelection: {
289 provider: "claudeAgent",
290 model: "claude-opus-4-6",
291 options: {
292 reasoningEffort: "low",
293 },
294 },
295 scripts: [],
296 createdAt: "2026-01-01T00:00:00.000Z",
297 updatedAt: "2026-01-01T00:00:00.000Z",
298 });
299
300 assert.deepStrictEqual(JSON.parse(eventRows[2]!.payloadJson), {
301 projectId: "project-3",
302 title: "Null Model Project",
303 workspaceRoot: "/tmp/project-3",
304 defaultModelSelection: null,
305 scripts: [],
306 createdAt: "2026-01-01T00:00:00.000Z",
307 updatedAt: "2026-01-01T00:00:00.000Z",
308 });
309
310 assert.deepStrictEqual(JSON.parse(eventRows[3]!.payloadJson), {
311 threadId: "thread-1",
312 projectId: "project-1",
313 title: "Thread",
314 modelSelection: {
315 provider: "claudeAgent",
316 model: "claude-opus-4-6",
317 options: {
318 effort: "max",
319 thinking: false,
320 },
321 },
322 runtimeMode: "full-access",
323 interactionMode: "default",
324 branch: null,
325 worktreePath: null,
326 createdAt: "2026-01-01T00:00:00.000Z",
327 updatedAt: "2026-01-01T00:00:00.000Z",
328 });
329
330 assert.deepStrictEqual(JSON.parse(eventRows[4]!.payloadJson), {
331 threadId: "thread-2",
332 projectId: "project-1",
333 title: "Fallback Thread",
334 modelSelection: {
335 provider: "codex",
336 model: "gpt-5.4",
337 options: {
338 effort: "max",
339 },
340 },
341 runtimeMode: "full-access",
342 interactionMode: "default",
343 branch: null,
344 worktreePath: null,
345 createdAt: "2026-01-01T00:00:00.000Z",
346 updatedAt: "2026-01-01T00:00:00.000Z",
347 });
348
349 assert.deepStrictEqual(JSON.parse(eventRows[5]!.payloadJson), {
350 threadId: "thread-1",
351 turnId: "turn-1",
352 input: "hi",
353 modelSelection: {
354 provider: "codex",
355 model: "gpt-5.4",
356 options: {
357 fastMode: true,
358 },
359 },
360 deliveryMode: "buffered",
361 });
362
363 assert.deepStrictEqual(JSON.parse(eventRows[6]!.payloadJson), {
364 threadId: "thread-3",
365 projectId: "project-1",
366 title: "Ancient Thread",
367 modelSelection: {
368 provider: "codex",
369 model: "gpt-5.4",
370 },
371 runtimeMode: "full-access",
372 interactionMode: "default",
373 branch: null,
374 worktreePath: null,
375 createdAt: "2026-01-01T00:00:00.000Z",
376 updatedAt: "2026-01-01T00:00:00.000Z",
377 });
378 }
379 }),
380 );
381});