@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 457 lines 17.1 KB
Raw
1 import { assert, it } from "@effect/vitest";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as SqlClient from "effect/sql/SqlClient";
5
6 import { runMigrations } from "../Migrations.ts";
7 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
8
9 const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layer({ filename: ":memory:" })));
10
11 layer("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 `;
62 yield* sql`
63 INSERT INTO projection_thread_sessions (
64 thread_id,
65 status,
66 provider_name,
67 provider_session_id,
68 provider_thread_id,
69 active_turn_id,
70 last_error,
71 updated_at,
72 runtime_mode
73 )
74 VALUES (
75 'thread-session',
76 'running',
77 'claudeAgent',
78 'provider-session-1',
79 'provider-thread-1',
80 NULL,
81 NULL,
82 '2026-01-01T00:00:00.000Z',
83 'full-access'
84 )
85 `;
86 yield* sql`
87 INSERT INTO orchestration_events (
88 event_id,
89 aggregate_kind,
90 stream_id,
91 stream_version,
92 event_type,
93 occurred_at,
94 command_id,
95 causation_event_id,
96 correlation_id,
97 actor_kind,
98 payload_json,
99 metadata_json
100 )
101 VALUES
102 (
103 'event-project-created',
104 'project',
105 'project-1',
106 1,
107 'project.created',
108 '2026-01-01T00:00:00.000Z',
109 'command-project-created',
110 NULL,
111 'correlation-project-created',
112 'user',
113 '{"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"}',
114 '{}'
115 ),
116 (
117 'event-project-created-fallback',
118 'project',
119 'project-2',
120 1,
121 'project.created',
122 '2026-01-01T00:00:00.000Z',
123 'command-project-created-fallback',
124 NULL,
125 'correlation-project-created-fallback',
126 'user',
127 '{"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"}',
128 '{}'
129 ),
130 (
131 'event-project-created-null-model',
132 'project',
133 'project-3',
134 1,
135 'project.created',
136 '2026-01-01T00:00:00.000Z',
137 'command-project-created-null-model',
138 NULL,
139 'correlation-project-created-null-model',
140 'user',
141 '{"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"}',
142 '{}'
143 ),
144 (
145 'event-thread-created',
146 'thread',
147 'thread-1',
148 1,
149 'thread.created',
150 '2026-01-01T00:00:00.000Z',
151 'command-thread-created',
152 NULL,
153 'correlation-thread-created',
154 'user',
155 '{"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"}',
156 '{}'
157 ),
158 (
159 'event-thread-created-fallback',
160 'thread',
161 'thread-2',
162 1,
163 'thread.created',
164 '2026-01-01T00:00:00.000Z',
165 'command-thread-created-fallback',
166 NULL,
167 'correlation-thread-created-fallback',
168 'user',
169 '{"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"}',
170 '{}'
171 ),
172 (
173 'event-turn-start-requested',
174 'thread',
175 'thread-1',
176 2,
177 'thread.turn-start-requested',
178 '2026-01-01T00:00:00.000Z',
179 'command-turn-start-requested',
180 NULL,
181 'correlation-turn-start-requested',
182 'user',
183 '{"threadId":"thread-1","turnId":"turn-1","input":"hi","model":"gpt-5.4","modelOptions":{"codex":{"fastMode":true},"claudeAgent":{"effort":"max"}},"deliveryMode":"buffered"}',
184 '{}'
185 ),
186 (
187 'event-thread-created-no-model',
188 'thread',
189 'thread-3',
190 1,
191 'thread.created',
192 '2026-01-01T00:00:00.000Z',
193 'command-thread-created-no-model',
194 NULL,
195 'correlation-thread-created-no-model',
196 'user',
197 '{"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"}',
198 '{}'
199 )
200 `;
201 }
202
203 yield* runMigrations({ toMigrationInclusive: 16 });
204
205 {
206 const projectRows = yield* sql<{
207 readonly projectId: string;
208 readonly defaultModelSelection: string | null;
209 }>`
210 SELECT
211 project_id AS "projectId",
212 default_model_selection_json AS "defaultModelSelection"
213 FROM projection_projects
214 ORDER BY project_id
215 `;
216 assert.deepStrictEqual(projectRows, [
217 {
218 projectId: "project-claude",
219 defaultModelSelection: '{"provider":"claudeAgent","model":"claude-opus-4-6"}',
220 },
221 {
222 projectId: "project-codex",
223 defaultModelSelection: '{"provider":"codex","model":"gpt-5.4"}',
224 },
225 { projectId: "project-null", defaultModelSelection: null },
226 ]);
227
228 const threadRows = yield* sql<{
229 readonly threadId: string;
230 readonly modelSelection: string | null;
231 }>`
232 SELECT
233 thread_id AS "threadId",
234 model_selection_json AS "modelSelection"
235 FROM projection_threads
236 ORDER BY thread_id
237 `;
238 assert.deepStrictEqual(threadRows, [
239 {
240 threadId: "thread-claude",
241 modelSelection: '{"provider":"claudeAgent","model":"claude-opus-4-6"}',
242 },
243 {
244 threadId: "thread-codex",
245 modelSelection: '{"provider":"codex","model":"gpt-5.4"}',
246 },
247 {
248 threadId: "thread-session",
249 modelSelection: '{"provider":"claudeAgent","model":"gpt-5.4"}',
250 },
251 ]);
252
253 const eventRows = yield* sql<{
254 readonly payloadJson: string;
255 }>`
256 SELECT payload_json AS "payloadJson"
257 FROM orchestration_events
258 ORDER BY rowid ASC
259 `;
260 assert.deepStrictEqual(JSON.parse(eventRows[0]!.payloadJson), {
261 projectId: "project-1",
262 title: "Project",
263 workspaceRoot: "/tmp/project",
264 defaultModelSelection: {
265 provider: "claudeAgent",
266 model: "claude-opus-4-6",
267 options: {
268 effort: "max",
269 },
270 },
271 scripts: [],
272 createdAt: "2026-01-01T00:00:00.000Z",
273 updatedAt: "2026-01-01T00:00:00.000Z",
274 });
275 assert.deepStrictEqual(JSON.parse(eventRows[1]!.payloadJson), {
276 projectId: "project-2",
277 title: "Fallback Project",
278 workspaceRoot: "/tmp/project-2",
279 defaultModelSelection: {
280 provider: "claudeAgent",
281 model: "claude-opus-4-6",
282 options: {
283 reasoningEffort: "low",
284 },
285 },
286 scripts: [],
287 createdAt: "2026-01-01T00:00:00.000Z",
288 updatedAt: "2026-01-01T00:00:00.000Z",
289 });
290 assert.deepStrictEqual(JSON.parse(eventRows[2]!.payloadJson), {
291 projectId: "project-3",
292 title: "Null Model Project",
293 workspaceRoot: "/tmp/project-3",
294 defaultModelSelection: null,
295 scripts: [],
296 createdAt: "2026-01-01T00:00:00.000Z",
297 updatedAt: "2026-01-01T00:00:00.000Z",
298 });
299 assert.deepStrictEqual(JSON.parse(eventRows[3]!.payloadJson), {
300 threadId: "thread-1",
301 projectId: "project-1",
302 title: "Thread",
303 modelSelection: {
304 provider: "claudeAgent",
305 model: "claude-opus-4-6",
306 options: {
307 effort: "max",
308 thinking: false,
309 },
310 },
311 runtimeMode: "full-access",
312 interactionMode: "default",
313 branch: null,
314 worktreePath: null,
315 createdAt: "2026-01-01T00:00:00.000Z",
316 updatedAt: "2026-01-01T00:00:00.000Z",
317 });
318 assert.deepStrictEqual(JSON.parse(eventRows[4]!.payloadJson), {
319 threadId: "thread-2",
320 projectId: "project-1",
321 title: "Fallback Thread",
322 modelSelection: {
323 provider: "codex",
324 model: "gpt-5.4",
325 options: {
326 effort: "max",
327 },
328 },
329 runtimeMode: "full-access",
330 interactionMode: "default",
331 branch: null,
332 worktreePath: null,
333 createdAt: "2026-01-01T00:00:00.000Z",
334 updatedAt: "2026-01-01T00:00:00.000Z",
335 });
336 assert.deepStrictEqual(JSON.parse(eventRows[5]!.payloadJson), {
337 threadId: "thread-1",
338 turnId: "turn-1",
339 input: "hi",
340 modelSelection: {
341 provider: "codex",
342 model: "gpt-5.4",
343 options: {
344 fastMode: true,
345 },
346 },
347 deliveryMode: "buffered",
348 });
349 assert.deepStrictEqual(JSON.parse(eventRows[6]!.payloadJson), {
350 threadId: "thread-3",
351 projectId: "project-1",
352 title: "Ancient Thread",
353 modelSelection: {
354 provider: "codex",
355 model: "gpt-5.4",
356 },
357 runtimeMode: "full-access",
358 interactionMode: "default",
359 branch: null,
360 worktreePath: null,
361 createdAt: "2026-01-01T00:00:00.000Z",
362 updatedAt: "2026-01-01T00:00:00.000Z",
363 });
364 }
365 }),
366 );
367 });
368
369 layer("044_ClearAutomaticProjectModelDefaults", (it) => {
370 it.effect("clears create-time seeds and preserves explicit project defaults", () =>
371 Effect.gen(function* () {
372 const sql = yield* SqlClient.SqlClient;
373 yield* runMigrations({ toMigrationInclusive: 43 });
374
375 yield* sql`
376 INSERT INTO projection_projects (
377 project_id,
378 title,
379 workspace_root,
380 default_model_selection_json,
381 default_thread_env_mode,
382 favicon_path,
383 scripts_json,
384 created_at,
385 updated_at,
386 deleted_at
387 )
388 VALUES
389 ('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),
390 ('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),
391 ('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)
392 `;
393
394 yield* sql`
395 INSERT INTO orchestration_events (
396 event_id,
397 aggregate_kind,
398 stream_id,
399 stream_version,
400 event_type,
401 occurred_at,
402 command_id,
403 causation_event_id,
404 correlation_id,
405 actor_kind,
406 payload_json,
407 metadata_json
408 )
409 VALUES
410 ('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"}}', '{}'),
411 ('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"}}', '{}'),
412 ('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"}', '{}'),
413 ('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"}}', '{}'),
414 ('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"}]}}', '{}')
415 `;
416
417 yield* runMigrations({ toMigrationInclusive: 44 });
418
419 const projects = yield* sql<{
420 readonly projectId: string;
421 readonly selection: string | null;
422 }>`
423 SELECT
424 project_id AS "projectId",
425 default_model_selection_json AS "selection"
426 FROM projection_projects
427 ORDER BY project_id
428 `;
429 assert.deepStrictEqual(projects, [
430 { projectId: "project-auto", selection: null },
431 {
432 projectId: "project-explicit",
433 selection:
434 '{"instanceId":"codex","model":"gpt-5.6-sol","options":[{"id":"reasoningEffort","value":"high"}]}',
435 },
436 { projectId: "project-title-only", selection: null },
437 ]);
438
439 const createdEvents = yield* sql<{
440 readonly streamId: string;
441 readonly model: string | null;
442 }>`
443 SELECT
444 stream_id AS "streamId",
445 json_extract(payload_json, '$.defaultModelSelection.model') AS "model"
446 FROM orchestration_events
447 WHERE event_type = 'project.created'
448 ORDER BY stream_id
449 `;
450 assert.deepStrictEqual(createdEvents, [
451 { streamId: "project-auto", model: null },
452 { streamId: "project-explicit", model: "gpt-5.6-sol" },
453 { streamId: "project-title-only", model: null },
454 ]);
455 }),
456 );
457 });