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

016_CanonicalizeModelSelections.test.ts

462 lines17.5 KB

1import { assert, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as SqlClient from "effect/sql/SqlClient";
5​
6import { runMigrations } from "../Migrations.ts";
7import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
8​
9const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layer({ filename: ":memory:" })));
10​
11layer("016_CanonicalizeModelSelections", (it) => {
12 it.effect(
13 "migrates legacy projection rows and event payloads to the canonical model-selection shape",
14 () =>
15 Effect.gen(function* () {
16 const sql = yield* SqlClient.SqlClient;
17​
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 yield* runMigrations({ toMigrationInclusive: 16 });
205​
206 {
207 const projectRows = yield* sql<{
208 readonly projectId: string;
209 readonly defaultModelSelection: string | null;
210 }>`
211 SELECT
212 project_id AS "projectId",
213 default_model_selection_json AS "defaultModelSelection"
214 FROM projection_projects
215 ORDER BY project_id
216 `;
217 assert.deepStrictEqual(projectRows, [
218 {
219 projectId: "project-claude",
220 defaultModelSelection: '{"provider":"claudeAgent","model":"claude-opus-4-6"}',
221 },
222 {
223 projectId: "project-codex",
224 defaultModelSelection: '{"provider":"codex","model":"gpt-5.4"}',
225 },
226 { projectId: "project-null", defaultModelSelection: null },
227 ]);
228​
229 const threadRows = yield* sql<{
230 readonly threadId: string;
231 readonly modelSelection: string | null;
232 }>`
233 SELECT
234 thread_id AS "threadId",
235 model_selection_json AS "modelSelection"
236 FROM projection_threads
237 ORDER BY thread_id
238 `;
239 assert.deepStrictEqual(threadRows, [
240 {
241 threadId: "thread-claude",
242 modelSelection: '{"provider":"claudeAgent","model":"claude-opus-4-6"}',
243 },
244 {
245 threadId: "thread-codex",
246 modelSelection: '{"provider":"codex","model":"gpt-5.4"}',
247 },
248 {
249 threadId: "thread-legacy-options",
250 modelSelection: '{"provider":"claudeAgent","model":"claude-opus-4-6"}',
251 },
252 {
253 threadId: "thread-session",
254 modelSelection: '{"provider":"claudeAgent","model":"gpt-5.4"}',
255 },
256 ]);
257​
258 const eventRows = yield* sql<{
259 readonly payloadJson: string;
260 }>`
261 SELECT payload_json AS "payloadJson"
262 FROM orchestration_events
263 ORDER BY rowid ASC
264 `;
265 assert.deepStrictEqual(JSON.parse(eventRows[0]!.payloadJson), {
266 projectId: "project-1",
267 title: "Project",
268 workspaceRoot: "/tmp/project",
269 defaultModelSelection: {
270 provider: "claudeAgent",
271 model: "claude-opus-4-6",
272 options: {
273 effort: "max",
274 },
275 },
276 scripts: [],
277 createdAt: "2026-01-01T00:00:00.000Z",
278 updatedAt: "2026-01-01T00:00:00.000Z",
279 });
280 assert.deepStrictEqual(JSON.parse(eventRows[1]!.payloadJson), {
281 projectId: "project-2",
282 title: "Fallback Project",
283 workspaceRoot: "/tmp/project-2",
284 defaultModelSelection: {
285 provider: "claudeAgent",
286 model: "claude-opus-4-6",
287 options: {
288 reasoningEffort: "low",
289 },
290 },
291 scripts: [],
292 createdAt: "2026-01-01T00:00:00.000Z",
293 updatedAt: "2026-01-01T00:00:00.000Z",
294 });
295 assert.deepStrictEqual(JSON.parse(eventRows[2]!.payloadJson), {
296 projectId: "project-3",
297 title: "Null Model Project",
298 workspaceRoot: "/tmp/project-3",
299 defaultModelSelection: null,
300 scripts: [],
301 createdAt: "2026-01-01T00:00:00.000Z",
302 updatedAt: "2026-01-01T00:00:00.000Z",
303 });
304 assert.deepStrictEqual(JSON.parse(eventRows[3]!.payloadJson), {
305 threadId: "thread-1",
306 projectId: "project-1",
307 title: "Thread",
308 modelSelection: {
309 provider: "claudeAgent",
310 model: "claude-opus-4-6",
311 options: {
312 effort: "max",
313 thinking: false,
314 },
315 },
316 runtimeMode: "full-access",
317 interactionMode: "default",
318 branch: null,
319 worktreePath: null,
320 createdAt: "2026-01-01T00:00:00.000Z",
321 updatedAt: "2026-01-01T00:00:00.000Z",
322 });
323 assert.deepStrictEqual(JSON.parse(eventRows[4]!.payloadJson), {
324 threadId: "thread-2",
325 projectId: "project-1",
326 title: "Fallback Thread",
327 modelSelection: {
328 provider: "codex",
329 model: "gpt-5.4",
330 options: {
331 effort: "max",
332 },
333 },
334 runtimeMode: "full-access",
335 interactionMode: "default",
336 branch: null,
337 worktreePath: null,
338 createdAt: "2026-01-01T00:00:00.000Z",
339 updatedAt: "2026-01-01T00:00:00.000Z",
340 });
341 assert.deepStrictEqual(JSON.parse(eventRows[5]!.payloadJson), {
342 threadId: "thread-1",
343 turnId: "turn-1",
344 input: "hi",
345 modelSelection: {
346 provider: "codex",
347 model: "gpt-5.4",
348 options: {
349 fastMode: true,
350 },
351 },
352 deliveryMode: "buffered",
353 });
354 assert.deepStrictEqual(JSON.parse(eventRows[6]!.payloadJson), {
355 threadId: "thread-3",
356 projectId: "project-1",
357 title: "Ancient Thread",
358 modelSelection: {
359 provider: "codex",
360 model: "gpt-5.4",
361 },
362 runtimeMode: "full-access",
363 interactionMode: "default",
364 branch: null,
365 worktreePath: null,
366 createdAt: "2026-01-01T00:00:00.000Z",
367 updatedAt: "2026-01-01T00:00:00.000Z",
368 });
369 }
370 }),
371 );
372});
373​
374layer("044_ClearAutomaticProjectModelDefaults", (it) => {
375 it.effect("clears create-time seeds and preserves explicit project defaults", () =>
376 Effect.gen(function* () {
377 const sql = yield* SqlClient.SqlClient;
378 yield* runMigrations({ toMigrationInclusive: 43 });
379​
380 yield* sql`
381 INSERT INTO projection_projects (
382 project_id,
383 title,
384 workspace_root,
385 default_model_selection_json,
386 default_thread_env_mode,
387 favicon_path,
388 scripts_json,
389 created_at,
390 updated_at,
391 deleted_at
392 )
393 VALUES
394 ('project-auto', 'Auto', '/tmp/auto', '{"instanceId":"codex","model":"gpt-5.6-sol"}', NULL, NULL, '[]', '2026-08-01T00:00:00.000Z', '2026-08-01T00:00:00.000Z', NULL),
395 ('project-title-only', 'Title only', '/tmp/title-only', '{"instanceId":"codex","model":"gpt-5.6-sol"}', NULL, NULL, '[]', '2026-08-01T00:00:00.000Z', '2026-08-02T00:00:00.000Z', NULL),
396 ('project-explicit', 'Explicit', '/tmp/explicit', '{"instanceId":"codex","model":"gpt-5.6-sol","options":[{"id":"reasoningEffort","value":"high"}]}', NULL, NULL, '[]', '2026-08-01T00:00:00.000Z', '2026-08-02T00:00:00.000Z', NULL)
397 `;
398​
399 yield* sql`
400 INSERT INTO orchestration_events (
401 event_id,
402 aggregate_kind,
403 stream_id,
404 stream_version,
405 event_type,
406 occurred_at,
407 command_id,
408 causation_event_id,
409 correlation_id,
410 actor_kind,
411 payload_json,
412 metadata_json
413 )
414 VALUES
415 ('event-auto-create', 'project', 'project-auto', 0, 'project.created', '2026-08-01T00:00:00.000Z', 'command-auto-create', NULL, 'command-auto-create', 'client', '{"defaultModelSelection":{"instanceId":"codex","model":"gpt-5.6-sol"}}', '{}'),
416 ('event-title-create', 'project', 'project-title-only', 0, 'project.created', '2026-08-01T00:00:00.000Z', 'command-title-create', NULL, 'command-title-create', 'client', '{"defaultModelSelection":{"instanceId":"codex","model":"gpt-5.6-sol"}}', '{}'),
417 ('event-title-update', 'project', 'project-title-only', 1, 'project.meta-updated', '2026-08-02T00:00:00.000Z', 'command-title-update', NULL, 'command-title-update', 'client', '{"title":"Renamed"}', '{}'),
418 ('event-explicit-create', 'project', 'project-explicit', 0, 'project.created', '2026-08-01T00:00:00.000Z', 'command-explicit-create', NULL, 'command-explicit-create', 'client', '{"defaultModelSelection":{"instanceId":"codex","model":"gpt-5.6-sol"}}', '{}'),
419 ('event-explicit-update', 'project', 'project-explicit', 1, 'project.meta-updated', '2026-08-02T00:00:00.000Z', 'command-explicit-update', NULL, 'command-explicit-update', 'client', '{"defaultModelSelection":{"instanceId":"codex","model":"gpt-5.6-sol","options":[{"id":"reasoningEffort","value":"high"}]}}', '{}')
420 `;
421​
422 yield* runMigrations({ toMigrationInclusive: 44 });
423​
424 const projects = yield* sql<{
425 readonly projectId: string;
426 readonly selection: string | null;
427 }>`
428 SELECT
429 project_id AS "projectId",
430 default_model_selection_json AS "selection"
431 FROM projection_projects
432 ORDER BY project_id
433 `;
434 assert.deepStrictEqual(projects, [
435 { projectId: "project-auto", selection: null },
436 {
437 projectId: "project-explicit",
438 selection:
439 '{"instanceId":"codex","model":"gpt-5.6-sol","options":[{"id":"reasoningEffort","value":"high"}]}',
440 },
441 { projectId: "project-title-only", selection: null },
442 ]);
443​
444 const createdEvents = yield* sql<{
445 readonly streamId: string;
446 readonly model: string | null;
447 }>`
448 SELECT
449 stream_id AS "streamId",
450 json_extract(payload_json, '$.defaultModelSelection.model') AS "model"
451 FROM orchestration_events
452 WHERE event_type = 'project.created'
453 ORDER BY stream_id
454 `;
455 assert.deepStrictEqual(createdEvents, [
456 { streamId: "project-auto", model: null },
457 { streamId: "project-explicit", model: "gpt-5.6-sol" },
458 { streamId: "project-title-only", model: null },
459 ]);
460 }),
461 );
462});