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

main
ts 213 lines 7.31 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 const MODEL_SELECTION = '{"instanceId":"codex","model":"gpt-5.6-sol"}';
12
13 layer("046_RepairAutomaticSettlementTimestamps", (it) => {
14 it.effect("repairs automatic stamps and leaves manual settlement alone", () =>
15 Effect.gen(function* () {
16 const sql = yield* SqlClient.SqlClient;
17 yield* runMigrations({ toMigrationInclusive: 45 });
18
19 yield* sql`
20 INSERT INTO projection_threads (
21 thread_id,
22 project_id,
23 title,
24 model_selection_json,
25 latest_turn_id,
26 created_at,
27 updated_at,
28 latest_user_message_at,
29 settled_override,
30 settled_at,
31 deleted_at
32 )
33 VALUES
34 (
35 'thread-auto', 'project-1', 'Automatic', ${MODEL_SELECTION}, 'turn-auto',
36 '2026-05-01T00:00:00.000Z', '2026-09-01T00:00:00.000Z', '2026-06-01T00:00:00.000Z',
37 'settled', '2026-09-01T00:00:00.000Z', NULL
38 ),
39 (
40 'thread-auto-no-activity', 'project-1', 'Automatic without activity', ${MODEL_SELECTION}, NULL,
41 '2026-05-02T00:00:00.000Z', '2026-09-01T00:00:00.000Z', NULL,
42 'settled', '2026-09-01T00:00:00.000Z', NULL
43 ),
44 (
45 'thread-auto-later-activity', 'project-1', 'Automatic then active', ${MODEL_SELECTION}, NULL,
46 '2026-05-03T00:00:00.000Z', '2026-09-03T00:00:00.000Z', '2026-09-03T00:00:00.000Z',
47 'settled', '2026-09-01T00:00:00.000Z', NULL
48 ),
49 (
50 'thread-manual', 'project-1', 'Manual', ${MODEL_SELECTION}, NULL,
51 '2026-05-01T00:00:00.000Z', '2026-08-10T00:00:00.000Z', '2026-06-10T00:00:00.000Z',
52 'settled', '2026-08-10T00:00:00.000Z', NULL
53 ),
54 (
55 'thread-resettled', 'project-1', 'Manually re-settled', ${MODEL_SELECTION}, NULL,
56 '2026-05-01T00:00:00.000Z', '2026-09-02T00:00:00.000Z', '2026-06-05T00:00:00.000Z',
57 'settled', '2026-09-02T00:00:00.000Z', NULL
58 )
59 `;
60
61 yield* sql`
62 INSERT INTO projection_thread_messages (
63 message_id, thread_id, turn_id, role, text, is_streaming, created_at, updated_at
64 )
65 VALUES
66 ('message-auto', 'thread-auto', 'turn-auto', 'user', 'Prompt', 0, '2026-06-01T00:00:00.000Z', '2026-06-01T00:00:00.000Z'),
67 ('message-later-old', 'thread-auto-later-activity', NULL, 'user', 'Prompt', 0, '2026-06-20T00:00:00.000Z', '2026-06-20T00:00:00.000Z'),
68 ('message-later-new', 'thread-auto-later-activity', NULL, 'user', 'Prompt', 0, '2026-09-03T00:00:00.000Z', '2026-09-03T00:00:00.000Z'),
69 ('message-manual', 'thread-manual', NULL, 'user', 'Prompt', 0, '2026-06-10T00:00:00.000Z', '2026-06-10T00:00:00.000Z'),
70 ('message-resettled', 'thread-resettled', NULL, 'user', 'Prompt', 0, '2026-06-05T00:00:00.000Z', '2026-06-05T00:00:00.000Z')
71 `;
72
73 yield* sql`
74 INSERT INTO projection_turns (
75 thread_id, turn_id, state, requested_at, started_at, completed_at, checkpoint_files_json
76 )
77 VALUES (
78 'thread-auto', 'turn-auto', 'completed',
79 '2026-06-02T00:00:00.000Z', '2026-06-02T00:01:00.000Z', '2026-06-03T00:00:00.000Z', '[]'
80 )
81 `;
82
83 const settledEvent = (
84 eventId: string,
85 threadId: string,
86 version: number,
87 occurredAt: string,
88 commandId: string,
89 settledAt: string,
90 ) =>
91 sql`
92 INSERT INTO orchestration_events (
93 event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at,
94 command_id, causation_event_id, correlation_id, actor_kind, payload_json, metadata_json
95 )
96 VALUES (
97 ${eventId}, 'thread', ${threadId}, ${version}, 'thread.settled', ${occurredAt},
98 ${commandId}, NULL, ${commandId},
99 ${commandId.startsWith("server:") ? "server" : "client"},
100 ${JSON.stringify({ threadId, settledAt, updatedAt: occurredAt })}, '{}'
101 )
102 `;
103 const automatic = (threadId: string) => `server:auto-settle:${threadId}:uuid`;
104 const sweptAt = "2026-09-01T00:00:00.000Z";
105
106 yield* settledEvent(
107 "event-auto",
108 "thread-auto",
109 0,
110 sweptAt,
111 automatic("thread-auto"),
112 sweptAt,
113 );
114 yield* settledEvent(
115 "event-auto-repeat",
116 "thread-auto",
117 1,
118 "2026-09-01T00:00:05.000Z",
119 "command-repeat",
120 sweptAt,
121 );
122 yield* settledEvent(
123 "event-auto-no-activity",
124 "thread-auto-no-activity",
125 0,
126 sweptAt,
127 automatic("thread-auto-no-activity"),
128 sweptAt,
129 );
130 yield* settledEvent(
131 "event-auto-later-activity",
132 "thread-auto-later-activity",
133 0,
134 sweptAt,
135 automatic("thread-auto-later-activity"),
136 sweptAt,
137 );
138 yield* settledEvent(
139 "event-manual",
140 "thread-manual",
141 0,
142 "2026-08-10T00:00:00.000Z",
143 "command-manual",
144 "2026-08-10T00:00:00.000Z",
145 );
146 yield* settledEvent(
147 "event-resettled-auto",
148 "thread-resettled",
149 0,
150 sweptAt,
151 automatic("thread-resettled"),
152 sweptAt,
153 );
154 yield* settledEvent(
155 "event-resettled-manual",
156 "thread-resettled",
157 1,
158 "2026-09-02T00:00:00.000Z",
159 "command-resettled-manual",
160 "2026-09-02T00:00:00.000Z",
161 );
162
163 const eventsBefore =
164 yield* sql`SELECT payload_json FROM orchestration_events ORDER BY event_id`;
165
166 yield* runMigrations({ toMigrationInclusive: 46 });
167
168 const threads = yield* sql<{
169 readonly threadId: string;
170 readonly settledAt: string;
171 readonly updatedAt: string;
172 }>`
173 SELECT
174 thread_id AS "threadId",
175 settled_at AS "settledAt",
176 updated_at AS "updatedAt"
177 FROM projection_threads
178 ORDER BY thread_id
179 `;
180 assert.deepStrictEqual(threads, [
181 {
182 threadId: "thread-auto",
183 settledAt: "2026-06-03T00:00:00.000Z",
184 updatedAt: "2026-09-01T00:00:00.000Z",
185 },
186 {
187 threadId: "thread-auto-later-activity",
188 settledAt: "2026-06-20T00:00:00.000Z",
189 updatedAt: "2026-09-03T00:00:00.000Z",
190 },
191 {
192 threadId: "thread-auto-no-activity",
193 settledAt: "2026-05-02T00:00:00.000Z",
194 updatedAt: "2026-09-01T00:00:00.000Z",
195 },
196 {
197 threadId: "thread-manual",
198 settledAt: "2026-08-10T00:00:00.000Z",
199 updatedAt: "2026-08-10T00:00:00.000Z",
200 },
201 {
202 threadId: "thread-resettled",
203 settledAt: "2026-09-02T00:00:00.000Z",
204 updatedAt: "2026-09-02T00:00:00.000Z",
205 },
206 ]);
207
208 const eventsAfter =
209 yield* sql`SELECT payload_json FROM orchestration_events ORDER BY event_id`;
210 assert.deepStrictEqual(eventsAfter, eventsBefore);
211 }),
212 );
213 });