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("026_CanonicalizeModelSelectionOptions", (it) => {
11 it.effect("converts legacy object-shape options into array-shape on projections and events", () =>
12 Effect.gen(function* () {
13 const sql = yield* SqlClient.SqlClient;
14
15 yield* runMigrations({ toMigrationInclusive: 25 });
16
17 yield* sql`
18 INSERT INTO projection_projects (
19 project_id,
20 title,
21 workspace_root,
22 default_model_selection_json,
23 scripts_json,
24 created_at,
25 updated_at,
26 deleted_at
27 )
28 VALUES
29 (
30 'project-legacy',
31 'Legacy options project',
32 '/tmp/legacy',
33 '{"provider":"claudeAgent","model":"claude-opus-4-6","options":{"effort":"max","fastMode":true}}',
34 '[]',
35 '2026-01-01T00:00:00.000Z',
36 '2026-01-01T00:00:00.000Z',
37 NULL
38 ),
39 (
40 'project-no-options',
41 'No options project',
42 '/tmp/no-options',
43 '{"provider":"codex","model":"gpt-5.4"}',
44 '[]',
45 '2026-01-01T00:00:00.000Z',
46 '2026-01-01T00:00:00.000Z',
47 NULL
48 ),
49 (
50 'project-null-selection',
51 'Null model selection project',
52 '/tmp/null-selection',
53 NULL,
54 '[]',
55 '2026-01-01T00:00:00.000Z',
56 '2026-01-01T00:00:00.000Z',
57 NULL
58 ),
59 (
60 'project-already-array',
61 'Already-canonical options project',
62 '/tmp/already-array',
63 '{"provider":"codex","model":"gpt-5.4","options":[{"id":"reasoningEffort","value":"high"}]}',
64 '[]',
65 '2026-01-01T00:00:00.000Z',
66 '2026-01-01T00:00:00.000Z',
67 NULL
68 )
69 `;
70
71 yield* sql`
72 INSERT INTO projection_threads (
73 thread_id,
74 project_id,
75 title,
76 model_selection_json,
77 branch,
78 worktree_path,
79 latest_turn_id,
80 created_at,
81 updated_at,
82 archived_at,
83 latest_user_message_at,
84 pending_approval_count,
85 pending_user_input_count,
86 has_actionable_proposed_plan,
87 deleted_at,
88 runtime_mode,
89 interaction_mode
90 )
91 VALUES
92 (
93 'thread-legacy',
94 'project-legacy',
95 'Legacy thread',
96 '{"provider":"claudeAgent","model":"claude-opus-4-6","options":{"effort":"max","thinking":false,"contextWindow":"1m"}}',
97 NULL, NULL, NULL,
98 '2026-01-01T00:00:00.000Z',
99 '2026-01-01T00:00:00.000Z',
100 NULL, NULL, 0, 0, 0, NULL,
101 'full-access', 'default'
102 ),
103 (
104 'thread-empty-options',
105 'project-legacy',
106 'Empty options thread',
107 '{"provider":"codex","model":"gpt-5.4","options":{}}',
108 NULL, NULL, NULL,
109 '2026-01-01T00:00:00.000Z',
110 '2026-01-01T00:00:00.000Z',
111 NULL, NULL, 0, 0, 0, NULL,
112 'full-access', 'default'
113 ),
114 (
115 'thread-drop-garbage',
116 'project-legacy',
117 'Thread with non-scalar entries',
118 '{"provider":"claudeAgent","model":"claude-opus-4-6","options":{"effort":"high","thinking":{"enabled":true,"budgetTokens":2000},"emptyStr":" ","nullish":null}}',
119 NULL, NULL, NULL,
120 '2026-01-01T00:00:00.000Z',
121 '2026-01-01T00:00:00.000Z',
122 NULL, NULL, 0, 0, 0, NULL,
123 'full-access', 'default'
124 ),
125 (
126 'thread-no-options',
127 'project-legacy',
128 'No options thread',
129 '{"provider":"codex","model":"gpt-5.4"}',
130 NULL, NULL, NULL,
131 '2026-01-01T00:00:00.000Z',
132 '2026-01-01T00:00:00.000Z',
133 NULL, NULL, 0, 0, 0, NULL,
134 'full-access', 'default'
135 ),
136 (
137 'thread-already-array',
138 'project-legacy',
139 'Already array thread',
140 '{"provider":"codex","model":"gpt-5.4","options":[{"id":"fastMode","value":true}]}',
141 NULL, NULL, NULL,
142 '2026-01-01T00:00:00.000Z',
143 '2026-01-01T00:00:00.000Z',
144 NULL, NULL, 0, 0, 0, NULL,
145 'full-access', 'default'
146 )
147 `;
148
149 yield* sql`
150 INSERT INTO orchestration_events (
151 event_id,
152 aggregate_kind,
153 stream_id,
154 stream_version,
155 event_type,
156 occurred_at,
157 command_id,
158 causation_event_id,
159 correlation_id,
160 actor_kind,
161 payload_json,
162 metadata_json
163 )
164 VALUES
165 (
166 'event-project-created',
167 'project',
168 'project-legacy',
169 1,
170 'project.created',
171 '2026-01-01T00:00:00.000Z',
172 'cmd-pc',
173 NULL,
174 'corr-pc',
175 'user',
176 '{"projectId":"project-legacy","title":"Project","workspaceRoot":"/tmp/legacy","defaultModelSelection":{"provider":"claudeAgent","model":"claude-opus-4-6","options":{"effort":"max","fastMode":true}},"scripts":[],"createdAt":"2026-01-01T00:00:00.000Z","updatedAt":"2026-01-01T00:00:00.000Z"}',
177 '{}'
178 ),
179 (
180 'event-project-meta-updated',
181 'project',
182 'project-legacy',
183 2,
184 'project.meta-updated',
185 '2026-01-01T00:00:00.000Z',
186 'cmd-pmu',
187 NULL,
188 'corr-pmu',
189 'user',
190 '{"projectId":"project-legacy","defaultModelSelection":{"provider":"codex","model":"gpt-5.4","options":{"reasoningEffort":"low"}},"updatedAt":"2026-01-01T00:00:00.000Z"}',
191 '{}'
192 ),
193 (
194 'event-project-null-selection',
195 'project',
196 'project-legacy',
197 3,
198 'project.meta-updated',
199 '2026-01-01T00:00:00.000Z',
200 'cmd-null',
201 NULL,
202 'corr-null',
203 'user',
204 '{"projectId":"project-legacy","defaultModelSelection":null,"updatedAt":"2026-01-01T00:00:00.000Z"}',
205 '{}'
206 ),
207 (
208 'event-thread-created',
209 'thread',
210 'thread-legacy',
211 1,
212 'thread.created',
213 '2026-01-01T00:00:00.000Z',
214 'cmd-tc',
215 NULL,
216 'corr-tc',
217 'user',
218 '{"threadId":"thread-legacy","projectId":"project-legacy","title":"Thread","modelSelection":{"provider":"claudeAgent","model":"claude-opus-4-6","options":{"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"}',
219 '{}'
220 ),
221 (
222 'event-thread-meta-updated',
223 'thread',
224 'thread-legacy',
225 2,
226 'thread.meta-updated',
227 '2026-01-01T00:00:00.000Z',
228 'cmd-tmu',
229 NULL,
230 'corr-tmu',
231 'user',
232 '{"threadId":"thread-legacy","modelSelection":{"provider":"codex","model":"gpt-5.4","options":{"fastMode":true}},"updatedAt":"2026-01-01T00:00:00.000Z"}',
233 '{}'
234 ),
235 (
236 'event-thread-turn-start',
237 'thread',
238 'thread-legacy',
239 3,
240 'thread.turn-start-requested',
241 '2026-01-01T00:00:00.000Z',
242 'cmd-tts',
243 NULL,
244 'corr-tts',
245 'user',
246 '{"threadId":"thread-legacy","messageId":"msg-1","modelSelection":{"provider":"claudeAgent","model":"claude-opus-4-6","options":{"effort":"high","contextWindow":"1m"}},"runtimeMode":"full-access","interactionMode":"default","createdAt":"2026-01-01T00:00:00.000Z"}',
247 '{}'
248 ),
249 (
250 'event-thread-already-array',
251 'thread',
252 'thread-legacy',
253 4,
254 'thread.created',
255 '2026-01-01T00:00:00.000Z',
256 'cmd-taa',
257 NULL,
258 'corr-taa',
259 'user',
260 '{"threadId":"thread-already-array","projectId":"project-legacy","title":"Already Array","modelSelection":{"provider":"codex","model":"gpt-5.4","options":[{"id":"reasoningEffort","value":"medium"}]},"runtimeMode":"full-access","interactionMode":"default","branch":null,"worktreePath":null,"createdAt":"2026-01-01T00:00:00.000Z","updatedAt":"2026-01-01T00:00:00.000Z"}',
261 '{}'
262 ),
263 (
264 'event-activity-append',
265 'thread',
266 'thread-legacy',
267 5,
268 'thread.activity-appended',
269 '2026-01-01T00:00:00.000Z',
270 'cmd-aa',
271 NULL,
272 'corr-aa',
273 'user',
274 '{"threadId":"thread-legacy","activity":{"id":"a","tone":"info","kind":"k","summary":"s","payload":null,"turnId":null,"createdAt":"2026-01-01T00:00:00.000Z"}}',
275 '{}'
276 )
277 `;
278
279 yield* runMigrations({ toMigrationInclusive: 26 });
280
281 // Projection projects
282 const projectRows = yield* sql<{
283 readonly projectId: string;
284 readonly defaultModelSelection: string | null;
285 }>`
286 SELECT
287 project_id AS "projectId",
288 default_model_selection_json AS "defaultModelSelection"
289 FROM projection_projects
290 ORDER BY project_id
291 `;
292 assert.deepStrictEqual(
293 projectRows.map((row) => ({
294 projectId: row.projectId,
295 selection: row.defaultModelSelection ? JSON.parse(row.defaultModelSelection) : null,
296 })),
297 [
298 {
299 projectId: "project-already-array",
300 selection: {
301 provider: "codex",
302 model: "gpt-5.4",
303 options: [{ id: "reasoningEffort", value: "high" }],
304 },
305 },
306 {
307 projectId: "project-legacy",
308 selection: {
309 provider: "claudeAgent",
310 model: "claude-opus-4-6",
311 options: [
312 { id: "effort", value: "max" },
313 { id: "fastMode", value: true },
314 ],
315 },
316 },
317 {
318 projectId: "project-no-options",
319 selection: { provider: "codex", model: "gpt-5.4" },
320 },
321 { projectId: "project-null-selection", selection: null },
322 ],
323 );
324
325 // Projection threads
326 const threadRows = yield* sql<{
327 readonly threadId: string;
328 readonly modelSelection: string | null;
329 }>`
330 SELECT
331 thread_id AS "threadId",
332 model_selection_json AS "modelSelection"
333 FROM projection_threads
334 ORDER BY thread_id
335 `;
336 assert.deepStrictEqual(
337 threadRows.map((row) => ({
338 threadId: row.threadId,
339 selection: row.modelSelection ? JSON.parse(row.modelSelection) : null,
340 })),
341 [
342 {
343 threadId: "thread-already-array",
344 selection: {
345 provider: "codex",
346 model: "gpt-5.4",
347 options: [{ id: "fastMode", value: true }],
348 },
349 },
350 {
351 threadId: "thread-drop-garbage",
352 selection: {
353 provider: "claudeAgent",
354 model: "claude-opus-4-6",
355 // Only the scalar string survives; nested object, whitespace
356 // string, and null are dropped.
357 options: [{ id: "effort", value: "high" }],
358 },
359 },
360 {
361 threadId: "thread-empty-options",
362 selection: { provider: "codex", model: "gpt-5.4", options: [] },
363 },
364 {
365 threadId: "thread-legacy",
366 selection: {
367 provider: "claudeAgent",
368 model: "claude-opus-4-6",
369 options: [
370 { id: "effort", value: "max" },
371 { id: "thinking", value: false },
372 { id: "contextWindow", value: "1m" },
373 ],
374 },
375 },
376 {
377 threadId: "thread-no-options",
378 selection: { provider: "codex", model: "gpt-5.4" },
379 },
380 ],
381 );
382
383 // Orchestration events
384 const eventRows = yield* sql<{
385 readonly eventId: string;
386 readonly payloadJson: string;
387 }>`
388 SELECT event_id AS "eventId", payload_json AS "payloadJson"
389 FROM orchestration_events
390 ORDER BY event_id
391 `;
392
393 const payloads = Object.fromEntries(
394 eventRows.map((row) => [row.eventId, JSON.parse(row.payloadJson)]),
395 );
396
397 assert.deepStrictEqual(payloads["event-project-created"].defaultModelSelection, {
398 provider: "claudeAgent",
399 model: "claude-opus-4-6",
400 options: [
401 { id: "effort", value: "max" },
402 { id: "fastMode", value: true },
403 ],
404 });
405
406 assert.deepStrictEqual(payloads["event-project-meta-updated"].defaultModelSelection, {
407 provider: "codex",
408 model: "gpt-5.4",
409 options: [{ id: "reasoningEffort", value: "low" }],
410 });
411
412 assert.strictEqual(payloads["event-project-null-selection"].defaultModelSelection, null);
413
414 assert.deepStrictEqual(payloads["event-thread-created"].modelSelection, {
415 provider: "claudeAgent",
416 model: "claude-opus-4-6",
417 options: [
418 { id: "effort", value: "max" },
419 { id: "thinking", value: false },
420 ],
421 });
422
423 assert.deepStrictEqual(payloads["event-thread-meta-updated"].modelSelection, {
424 provider: "codex",
425 model: "gpt-5.4",
426 options: [{ id: "fastMode", value: true }],
427 });
428
429 assert.deepStrictEqual(payloads["event-thread-turn-start"].modelSelection, {
430 provider: "claudeAgent",
431 model: "claude-opus-4-6",
432 options: [
433 { id: "effort", value: "high" },
434 { id: "contextWindow", value: "1m" },
435 ],
436 });
437
438 // Already-array records are left untouched.
439 assert.deepStrictEqual(payloads["event-thread-already-array"].modelSelection, {
440 provider: "codex",
441 model: "gpt-5.4",
442 options: [{ id: "reasoningEffort", value: "medium" }],
443 });
444
445 // Events with no modelSelection at all are untouched.
446 assert.isUndefined(payloads["event-activity-append"].modelSelection);
447 assert.isUndefined(payloads["event-activity-append"].defaultModelSelection);
448 }),
449 );
450});