@devries-cameron20 / t3code Mirror · read-only / commits / 9257b10060
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 22, 22:00 UTC

fix(server): threads swept by old desktop builds show their real settled time again (#15948)

Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

shivam committed Oct 20, 2026 at 10:59 UTC 9257b10060691dde3072a79162f645d2660263bb
6 files changed +664 -4
apps/server/src/persistence/Migrations.ts
+2
@@ -68,6 +68,7 @@ import Migration0062 from "./Migrations/062_ThreadFailureLookupIndex.ts";
68 import Migration0063 from "./Migrations/063_DropUnusedV1Projections.ts";
69 import Migration0064 from "./Migrations/064_CanonicalizeThreadPullRequestLinks.ts";
70 import Migration0065 from "./Migrations/065_CodexUsageNativeTurnIndex.ts";
71 +import Migration0066 from "./Migrations/066_RepairClientSettlementTimestamps.ts";
72
73 export const migrationEntries = [
74 [1, "OrchestrationEvents", Migration0001],
@@ -136,6 +137,7 @@ export const migrationEntries = [
137 [63, "DropUnusedV1Projections", Migration0063],
138 [64, "CanonicalizeThreadPullRequestLinks", Migration0064],
139 [65, "CodexUsageNativeTurnIndex", Migration0065],
140 + [66, "RepairClientSettlementTimestamps", Migration0066],
141 ] as const;
142
143 export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/055_OrchestrationV2.test.ts
+3 -1
@@ -15,7 +15,7 @@ layer("055_OrchestrationV2", (it) => {
15 Effect.sync(() => {
16 assert.deepStrictEqual(
17 migrationEntries.map(([id]) => id),
18 - Array.from({ length: 65 }, (_, index) => index + 1),
18 + Array.from({ length: 66 }, (_, index) => index + 1),
19 );
20 }),
21 );
@@ -39,6 +39,7 @@ layer("055_OrchestrationV2", (it) => {
39 [63, "DropUnusedV1Projections"],
40 [64, "CanonicalizeThreadPullRequestLinks"],
41 [65, "CodexUsageNativeTurnIndex"],
42 + [66, "RepairClientSettlementTimestamps"],
43 ]);
44 assert.deepStrictEqual(yield* runMigrations(), []);
45
@@ -70,6 +71,7 @@ layer("055_OrchestrationV2", (it) => {
71 { migration_id: 63, name: "DropUnusedV1Projections" },
72 { migration_id: 64, name: "CanonicalizeThreadPullRequestLinks" },
73 { migration_id: 65, name: "CodexUsageNativeTurnIndex" },
74 + { migration_id: 66, name: "RepairClientSettlementTimestamps" },
75 ]);
76
77 const tables = yield* sql<{ readonly name: string }>`
apps/server/src/persistence/Migrations/064_CanonicalizeThreadPullRequestLinks.test.ts
+2 -3
@@ -123,11 +123,10 @@ layer("064_CanonicalizeThreadPullRequestLinks", (it) => {
123 'user', ${v1Payload}, '{}', 1)
124 `;
125
126 - assert.deepEqual(yield* runMigrations(), [
126 + assert.deepEqual(yield* runMigrations({ toMigrationInclusive: 64 }), [
127 [64, "CanonicalizeThreadPullRequestLinks"],
128 - [65, "CodexUsageNativeTurnIndex"],
128 ]);
130 - assert.deepEqual(yield* runMigrations(), []);
129 + assert.deepEqual(yield* runMigrations({ toMigrationInclusive: 64 }), []);
130 const rows = yield* sql<{ readonly thread_id: string; readonly payload_json: string }>`
131 SELECT thread_id, payload_json FROM orchestration_v2_projection_threads
132 `;
apps/server/src/persistence/Migrations/066_RepairClientSettlementTimestamps.test.ts new
+371
@@ -0,0 +1,371 @@
1 +import { assert, it } from "@effect/vitest";
2 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 +import * as DateTime from "effect/DateTime";
4 +import * as Effect from "effect/Effect";
5 +import * as Layer from "effect/Layer";
6 +import * as SqlClient from "effect/sql/SqlClient";
7 +
8 +import * as EventStore from "../../orchestration-v2/EventStore.ts";
9 +import * as ProjectionMaintenance from "../../orchestration-v2/ProjectionMaintenance.ts";
10 +import * as ProjectionStore from "../../orchestration-v2/ProjectionStore.ts";
11 +import { runMigrations } from "../Migrations.ts";
12 +import Migration0066 from "./066_RepairClientSettlementTimestamps.ts";
13 +
14 +const layer = it.layer(
15 + ProjectionMaintenance.layer.pipe(
16 + Layer.provideMerge(
17 + Layer.mergeAll(EventStore.layer, ProjectionStore.layer).pipe(
18 + Layer.provideMerge(NodeSqliteClient.layer({ filename: ":memory:" })),
19 + ),
20 + ),
21 + ),
22 +);
23 +const createdAt = "2026-03-01T00:00:00.000Z";
24 +const sweptAt = "2026-09-04T13:33:00.000Z";
25 +const userMessageAt = "2026-04-01T00:00:00.000Z";
26 +const finishedAt = "2026-06-01T00:05:00.000Z";
27 +const manualAt = "2026-10-01T00:00:00.000Z";
28 +const plusSeconds = (iso: string, seconds: number) =>
29 + DateTime.makeUnsafe(iso).pipe(DateTime.add({ seconds }), DateTime.formatIso);
30 +
31 +layer("066_RepairClientSettlementTimestamps", (it) => {
32 + it.effect("repairs only sweep stamps, preserves overrides, and survives a V2 rebuild", () =>
33 + Effect.gen(function* () {
34 + const sql = yield* SqlClient.SqlClient;
35 + yield* runMigrations({ toMigrationInclusive: 64 });
36 + assert.deepEqual(
37 + yield* sql`SELECT name FROM sqlite_master WHERE name = 'projection_turns'`,
38 + [],
39 + );
40 + let eventNumber = 0;
41 + const insertEvent = (
42 + threadId: string,
43 + eventType: string,
44 + occurredAt: string,
45 + payload: Record<string, unknown>,
46 + applicationVersion = 1,
47 + actor = "client",
48 + appVersion: string | null = "0.0.37",
49 + ) => {
50 + const version = eventNumber++;
51 + return sql`
52 + INSERT INTO orchestration_events (
53 + event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at,
54 + command_id, actor_kind, payload_json, metadata_json, application_event_version
55 + ) VALUES (
56 + ${`event-${version}`}, 'thread', ${threadId}, ${version}, ${eventType}, ${occurredAt},
57 + ${`command-${version}`}, ${actor}, ${JSON.stringify(payload)},
58 + ${JSON.stringify({ origin: { appVersion } })}, ${applicationVersion}
59 + )
60 + `;
61 + };
62 + const insertSettlement = Effect.fnUntraced(function* (
63 + id: string,
64 + at: string,
65 + appVersion: string | null = "0.0.37",
66 + actor = "client",
67 + ) {
68 + yield* sql`
69 + INSERT INTO projection_threads (
70 + thread_id, project_id, title, model_selection_json, created_at, updated_at,
71 + settled_override, settled_at
72 + ) VALUES (
73 + ${id}, 'project-1', ${id}, '{"instanceId":"codex","model":"gpt-5.4"}',
74 + ${createdAt}, ${at}, 'settled', ${at}
75 + )
76 + `;
77 + yield* insertEvent(
78 + id,
79 + "thread.settled",
80 + at,
81 + { threadId: id, settledAt: at, updatedAt: at },
82 + 1,
83 + actor,
84 + appVersion,
85 + );
86 + });
87 +
88 + // A finished turn must come from retained events after 063 drops projection_turns.
89 + for (const [at, status, activeTurnId] of [
90 + ["2026-06-01T00:00:00.000Z", "running", "turn-1"],
91 + [finishedAt, "ready", null],
92 + ["2026-06-03T00:00:00.000Z", "stopped", null],
93 + ] as const) {
94 + yield* insertEvent(
95 + "sweep-2",
96 + "thread.session-set",
97 + at,
98 + {
99 + threadId: "sweep-2",
100 + session: { status, activeTurnId, updatedAt: at },
101 + },
102 + 1,
103 + "server",
104 + );
105 + if (status === "running") {
106 + for (let index = 0; index < 500; index++) {
107 + yield* insertEvent(
108 + "sweep-2",
109 + "thread.turn-start-requested",
110 + "invalid",
111 + {
112 + createdAt: "invalid",
113 + },
114 + 1,
115 + "server",
116 + );
117 + }
118 + }
119 + }
120 + for (let index = 0; index < 10; index++) {
121 + yield* insertSettlement(
122 + `sweep-${index}`,
123 + plusSeconds(sweptAt, index * 2),
124 + `0.0.${35 + (index % 4)}`,
125 + );
126 + }
127 + // Completed or reverted turns must not be finished again by an idle restart.
128 + for (const [id, eventType, at, payload] of [
129 + [
130 + "sweep-5",
131 + "thread.message-sent",
132 + userMessageAt,
133 + {
134 + turnId: "finished",
135 + role: "assistant",
136 + streaming: true,
137 + createdAt: userMessageAt,
138 + },
139 + ],
140 + [
141 + "sweep-5",
142 + "thread.message-sent",
143 + finishedAt,
144 + {
145 + turnId: "finished",
146 + role: "assistant",
147 + streaming: false,
148 + createdAt: userMessageAt,
149 + updatedAt: finishedAt,
150 + },
151 + ],
152 + [
153 + "sweep-6",
154 + "thread.session-set",
155 + "invalid",
156 + {
157 + session: { status: "running", activeTurnId: "invalid-start", updatedAt: "invalid" },
158 + },
159 + ],
160 + [
161 + "sweep-6",
162 + "thread.session-set",
163 + finishedAt,
164 + {
165 + session: { status: "ready", activeTurnId: null, updatedAt: "invalid" },
166 + },
167 + ],
168 + [
169 + "sweep-7",
170 + "thread.session-set",
171 + userMessageAt,
172 + {
173 + session: { status: "running", activeTurnId: "discarded", updatedAt: userMessageAt },
174 + },
175 + ],
176 + ["sweep-7", "thread.reverted", finishedAt, { turnCount: 0 }],
177 + ] as const) {
178 + yield* insertEvent(id, eventType, at, payload, 1, "server");
179 + }
180 + for (const id of ["sweep-5", "sweep-6", "sweep-7"]) {
181 + yield* insertEvent(
182 + id,
183 + "thread.session-set",
184 + "2026-06-03T00:00:00.000Z",
185 + {
186 + session: {
187 + status: "stopped",
188 + activeTurnId: null,
189 + updatedAt: "2026-06-03T00:00:00.000Z",
190 + },
191 + },
192 + 1,
193 + "server",
194 + );
195 + }
196 + yield* sql`
197 + INSERT INTO projection_thread_messages (
198 + message_id, thread_id, role, text, is_streaming, created_at, updated_at
199 + ) VALUES
200 + ('before', 'sweep-0', 'user', 'Prompt', 0, ${userMessageAt}, ${userMessageAt}),
201 + ('after', 'sweep-0', 'user', 'Later prompt', 0, ${manualAt}, ${manualAt})
202 + `;
203 + yield* insertEvent(
204 + "sweep-2",
205 + "thread.turn-start-requested",
206 + manualAt,
207 + {
208 + threadId: "sweep-2",
209 + createdAt: manualAt,
210 + },
211 + 1,
212 + "server",
213 + );
214 + yield* sql`UPDATE projection_threads SET settled_at = ${manualAt} WHERE thread_id = 'sweep-3'`;
215 + yield* sql`UPDATE projection_threads SET settled_override = 'active', settled_at = NULL WHERE thread_id = 'sweep-4'`;
216 +
217 + // This manual settle is within two minutes, but outside the sweep's 30-second gap.
218 + yield* insertSettlement("manual", plusSeconds(sweptAt, 18 + 45));
219 + // Ten events for nine distinct threads must not qualify.
220 + for (let index = 0; index < 9; index++) {
221 + yield* insertSettlement(
222 + `small-${index}`,
223 + plusSeconds("2026-09-03T00:00:00.000Z", index * 2),
224 + );
225 + }
226 + const repeatedAt = "2026-09-03T00:00:18.000Z";
227 + yield* insertEvent("small-8", "thread.settled", repeatedAt, { settledAt: repeatedAt });
228 + yield* sql`UPDATE projection_threads SET settled_at = ${repeatedAt} WHERE thread_id = 'small-8'`;
229 + for (let index = 0; index < 10; index++) {
230 + yield* insertSettlement(
231 + `spread-${index}`,
232 + plusSeconds("2026-09-03T01:00:00.000Z", index * 15),
233 + );
234 + yield* insertSettlement(
235 + `late-${index}`,
236 + plusSeconds("2026-09-05T00:00:00.000Z", index * 2),
237 + );
238 + }
239 + yield* insertSettlement("wrong-version", sweptAt, "0.0.40");
240 + yield* insertSettlement("missing-version", sweptAt, null);
241 + yield* insertSettlement("server", sweptAt, "0.0.37", "server");
242 + yield* insertSettlement("different-stamp", sweptAt);
243 + yield* sql`
244 + UPDATE orchestration_events SET payload_json = json_set(payload_json, '$.settledAt', ${createdAt})
245 + WHERE stream_id = 'different-stamp'
246 + `;
247 + const untouched = sql`
248 + SELECT * FROM projection_threads WHERE thread_id NOT LIKE 'sweep-%' ORDER BY thread_id
249 + `;
250 + const untouchedBefore = yield* untouched;
251 +
252 + for (const index of [0, 2, 3, 4]) {
253 + const id = `sweep-${index}`;
254 + const badStamp = plusSeconds(sweptAt, index * 2);
255 + const thread = {
256 + id,
257 + projectId: "project-1",
258 + title: id,
259 + providerInstanceId: "codex",
260 + modelSelection: { instanceId: "codex", model: "gpt-5.4" },
261 + runtimeMode: "full-access",
262 + interactionMode: "default",
263 + createdBy: "user",
264 + creationSource: "web",
265 + branch: null,
266 + worktreePath: null,
267 + activeProviderThreadId: null,
268 + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: id },
269 + forkedFrom: null,
270 + createdAt,
271 + updatedAt: badStamp,
272 + archivedAt: null,
273 + deletedAt: null,
274 + settledOverride: "settled",
275 + settledAt: badStamp,
276 + };
277 + yield* insertEvent(id, "thread.created", badStamp, thread, 2);
278 + const current = {
279 + ...thread,
280 + settledOverride: index === 4 ? "active" : "settled",
281 + settledAt: index === 4 ? null : index === 3 ? manualAt : badStamp,
282 + };
283 + if (index === 4) {
284 + yield* insertEvent(
285 + id,
286 + "thread.metadata-updated",
287 + badStamp,
288 + {
289 + ...thread,
290 + settledOverride: "active",
291 + },
292 + 2,
293 + );
294 + }
295 + if (index !== 2) yield* insertEvent(id, "thread.metadata-updated", manualAt, current, 2);
296 + yield* sql`
297 + INSERT INTO orchestration_v2_projection_threads (
298 + thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
299 + created_at, updated_at, payload_json
300 + ) VALUES (
301 + ${id}, 'project-1', ${id}, 'codex', 'full-access', 'default',
302 + ${createdAt}, ${badStamp}, ${JSON.stringify(current)}
303 + )
304 + `;
305 + }
306 + const v1History = sql`
307 + SELECT * FROM orchestration_events WHERE application_event_version = 1 ORDER BY sequence
308 + `;
309 + const v1Before = yield* v1History;
310 + const overrides = sql`
311 + SELECT * FROM orchestration_events
312 + WHERE application_event_version = 2 AND event_type = 'thread.metadata-updated'
313 + AND stream_id IN ('sweep-3', 'sweep-4') ORDER BY sequence
314 + `;
315 + const overridesBefore = yield* overrides;
316 +
317 + yield* runMigrations({ toMigrationInclusive: 66 });
318 + const legacy = yield* sql`
319 + SELECT thread_id, settled_override, settled_at FROM projection_threads
320 + WHERE thread_id LIKE 'sweep-%' ORDER BY thread_id
321 + `;
322 + assert.deepEqual(
323 + legacy,
324 + Array.from({ length: 10 }, (_, index) => ({
325 + thread_id: `sweep-${index}`,
326 + settled_override: index === 4 ? "active" : "settled",
327 + settled_at:
328 + index === 4
329 + ? null
330 + : ([
331 + userMessageAt,
332 + createdAt,
333 + finishedAt,
334 + manualAt,
335 + null,
336 + finishedAt,
337 + finishedAt,
338 + userMessageAt,
339 + ][index] ?? createdAt),
340 + })),
341 + );
342 + assert.deepEqual(yield* untouched, untouchedBefore);
343 + assert.deepEqual(yield* v1History, v1Before);
344 + assert.deepEqual(yield* overrides, overridesBefore);
345 + const imported = sql`
346 + SELECT thread_id,
347 + json_extract(payload_json, '$.settledAt') AS settled_at,
348 + json_extract(payload_json, '$.settledOverride') AS settled_override,
349 + json_extract(payload_json, '$.title') AS title
350 + FROM orchestration_v2_projection_threads ORDER BY thread_id
351 + `;
352 + const expectedImported = legacy
353 + .filter((_, index) => [0, 2, 3, 4].includes(index))
354 + .map((row) => ({ ...row, title: row.thread_id }));
355 + assert.deepEqual(yield* imported, expectedImported);
356 + const snapshot = Effect.all([
357 + sql`SELECT * FROM projection_threads ORDER BY thread_id`,
358 + sql`SELECT * FROM orchestration_v2_projection_threads ORDER BY thread_id`,
359 + sql`SELECT * FROM orchestration_events ORDER BY sequence`,
360 + ]);
361 + const repaired = yield* snapshot;
362 + assert.deepEqual(yield* runMigrations({ toMigrationInclusive: 66 }), []);
363 + yield* Migration0066;
364 + assert.deepEqual(yield* snapshot, repaired);
365 +
366 + const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
367 + assert.isTrue((yield* maintenance.rebuild).valid);
368 + assert.deepEqual(yield* imported, expectedImported);
369 + }),
370 + );
371 +});
apps/server/src/persistence/Migrations/066_RepairClientSettlementTimestamps.ts new
+284
@@ -0,0 +1,284 @@
1 +import * as DateTime from "effect/DateTime";
2 +import * as Effect from "effect/Effect";
3 +import * as Option from "effect/Option";
4 +import * as SqlClient from "effect/sql/SqlClient";
5 +
6 +const timestamp = (value: string | null) =>
7 + value === null ? undefined : Option.getOrUndefined(DateTime.make(value));
8 +
9 +export default Effect.gen(function* () {
10 + const sql = yield* SqlClient.SqlClient;
11 + // Manual settles have the same shape; only the affected builds' dense sweeps qualify.
12 + const settlements = yield* sql<{ readonly thread_id: string; readonly occurred_at: string }>`
13 + WITH
14 + legacy_settlements AS (
15 + SELECT
16 + stream_id AS thread_id,
17 + occurred_at,
18 + unixepoch(occurred_at, 'subsec') AS occurred_seconds
19 + FROM orchestration_events
20 + WHERE aggregate_kind = 'thread'
21 + AND event_type = 'thread.settled'
22 + AND actor_kind = 'client'
23 + AND application_event_version = 1
24 + AND json_type(payload_json, '$.settledAt') = 'text'
25 + AND json_extract(payload_json, '$.settledAt') = occurred_at
26 + AND json_extract(metadata_json, '$.origin.appVersion') IN ('0.0.35', '0.0.36', '0.0.37', '0.0.38')
27 + AND julianday(occurred_at) < julianday('2026-09-05T00:00:00.000Z')
28 + ),
29 + ordered AS (
30 + SELECT
31 + thread_id,
32 + occurred_at,
33 + occurred_seconds,
34 + occurred_seconds - LAG(occurred_seconds) OVER (ORDER BY occurred_seconds, thread_id)
35 + AS gap_seconds
36 + FROM legacy_settlements
37 + ),
38 + clustered AS (
39 + SELECT
40 + thread_id,
41 + occurred_at,
42 + occurred_seconds,
43 + SUM(CASE WHEN gap_seconds IS NULL OR gap_seconds > 30 THEN 1 ELSE 0 END)
44 + OVER (ORDER BY occurred_seconds, thread_id) AS cluster_id
45 + FROM ordered
46 + ),
47 + burst_clusters AS (
48 + SELECT
49 + cluster_id,
50 + COUNT(DISTINCT thread_id) AS distinct_thread_count,
51 + MAX(occurred_seconds) - MIN(occurred_seconds) AS span_seconds
52 + FROM clustered
53 + GROUP BY cluster_id
54 + ),
55 + automatic_settlements AS (
56 + SELECT clustered.thread_id, clustered.occurred_at
57 + FROM clustered
58 + JOIN burst_clusters ON burst_clusters.cluster_id = clustered.cluster_id
59 + WHERE burst_clusters.distinct_thread_count >= 10
60 + AND burst_clusters.span_seconds <= 120
61 + )
62 + SELECT DISTINCT thread_id, occurred_at FROM automatic_settlements
63 + ORDER BY thread_id, occurred_at
64 + `;
65 + const byThread = Map.groupBy(settlements, (settlement) => settlement.thread_id);
66 +
67 + for (const [threadId, sweeps] of byThread) {
68 + const repairs = sweeps.map((sweep) => ({
69 + occurredAt: sweep.occurred_at,
70 + cutoff: DateTime.toEpochMillis(DateTime.makeUnsafe(sweep.occurred_at)),
71 + activityAt: null as string | null,
72 + activityTime: -Infinity,
73 + }));
74 + const recordActivity = (value: string | null) => {
75 + const date = timestamp(value);
76 + if (date === undefined) return;
77 + const time = DateTime.toEpochMillis(date);
78 + for (const repair of repairs) {
79 + if (time <= repair.cutoff && time > repair.activityTime) {
80 + repair.activityAt = value;
81 + repair.activityTime = time;
82 + }
83 + }
84 + };
85 +
86 + for (const repair of repairs) {
87 + const messages = yield* sql<{ readonly created_at: string }>`
88 + SELECT created_at FROM projection_thread_messages
89 + WHERE thread_id = ${threadId} AND role = 'user'
90 + AND julianday(created_at) <= julianday(${repair.occurredAt})
91 + ORDER BY julianday(created_at) DESC LIMIT 1
92 + `;
93 + recordActivity(messages[0]?.created_at ?? null);
94 + }
95 +
96 + // 063 dropped projection_turns. Track open turns from retained V1 events so an
97 + // idle session stopping cannot masquerade as a turn finishing.
98 + const turns = new Map<
99 + string,
100 + { state: "running" | "completed" | "interrupted" | "error"; checkpoint: number | null }
101 + >();
102 + const runningTurnIds = new Set<string>();
103 + let activeTurnId: string | null = null;
104 + let afterSequence = 0;
105 + const pageSize = 500;
106 + while (true) {
107 + const events = yield* sql<{
108 + readonly sequence: number;
109 + readonly event_type: string;
110 + readonly occurred_at: string;
111 + readonly turn_id: string | null;
112 + readonly created_at: string | null;
113 + readonly updated_at: string | null;
114 + readonly completed_at: string | null;
115 + readonly role: string | null;
116 + readonly streaming: number | null;
117 + readonly status: string | null;
118 + readonly session_turn_id: string | null;
119 + readonly session_updated_at: string | null;
120 + readonly checkpoint: number | null;
121 + readonly turn_count: number | null;
122 + }>`
123 + SELECT sequence, event_type, occurred_at,
124 + json_extract(payload_json, '$.turnId') AS turn_id,
125 + json_extract(payload_json, '$.createdAt') AS created_at,
126 + json_extract(payload_json, '$.updatedAt') AS updated_at,
127 + json_extract(payload_json, '$.completedAt') AS completed_at,
128 + json_extract(payload_json, '$.role') AS role,
129 + json_extract(payload_json, '$.streaming') AS streaming,
130 + json_extract(payload_json, '$.session.status') AS status,
131 + json_extract(payload_json, '$.session.activeTurnId') AS session_turn_id,
132 + json_extract(payload_json, '$.session.updatedAt') AS session_updated_at,
133 + json_extract(payload_json, '$.checkpointTurnCount') AS checkpoint,
134 + json_extract(payload_json, '$.turnCount') AS turn_count
135 + FROM orchestration_events INDEXED BY idx_orch_events_stream_sequence
136 + WHERE aggregate_kind = 'thread' AND stream_id = ${threadId}
137 + AND application_event_version = 1 AND sequence > ${afterSequence}
138 + AND event_type IN (
139 + 'thread.created', 'thread.turn-start-requested', 'thread.session-set',
140 + 'thread.message-sent', 'thread.turn-interrupt-requested',
141 + 'thread.turn-diff-completed', 'thread.reverted'
142 + )
143 + ORDER BY sequence LIMIT ${pageSize}
144 + `;
145 + for (const event of events) {
146 + const turn = event.turn_id === null ? undefined : turns.get(event.turn_id);
147 + switch (event.event_type) {
148 + case "thread.created":
149 + turns.clear();
150 + runningTurnIds.clear();
151 + activeTurnId = null;
152 + break;
153 + case "thread.turn-start-requested":
154 + recordActivity(event.created_at);
155 + break;
156 + case "thread.session-set": {
157 + activeTurnId = event.status === "running" ? event.session_turn_id : null;
158 + const endedAt = timestamp(event.session_updated_at)
159 + ? event.session_updated_at
160 + : event.occurred_at;
161 + if (activeTurnId !== null) {
162 + for (const id of runningTurnIds) {
163 + if (id !== activeTurnId) {
164 + turns.get(id)!.state = "completed";
165 + runningTurnIds.delete(id);
166 + recordActivity(endedAt);
167 + }
168 + }
169 + const active = turns.get(activeTurnId);
170 + if (active === undefined) {
171 + turns.set(activeTurnId, { state: "running", checkpoint: null });
172 + runningTurnIds.add(activeTurnId);
173 + recordActivity(event.occurred_at);
174 + } else if (active.state === "interrupted") {
175 + active.state = "running";
176 + runningTurnIds.add(activeTurnId);
177 + }
178 + } else if (
179 + event.status === "idle" ||
180 + event.status === "ready" ||
181 + event.status === "error" ||
182 + event.status === "interrupted" ||
183 + event.status === "stopped"
184 + ) {
185 + for (const id of runningTurnIds) {
186 + const running = turns.get(id)!;
187 + running.state =
188 + event.status === "error"
189 + ? "error"
190 + : event.status === "interrupted" || event.status === "stopped"
191 + ? "interrupted"
192 + : "completed";
193 + recordActivity(endedAt);
194 + }
195 + runningTurnIds.clear();
196 + }
197 + break;
198 + }
199 + case "thread.message-sent":
200 + if (event.role !== "assistant" || event.turn_id === null) break;
201 + recordActivity(event.created_at);
202 + if (event.streaming === 0 && activeTurnId !== event.turn_id) {
203 + if (turn === undefined || turn.state === "running") recordActivity(event.updated_at);
204 + if (turn === undefined) {
205 + turns.set(event.turn_id, { state: "completed", checkpoint: null });
206 + } else if (turn.state === "running") {
207 + turn.state = "completed";
208 + }
209 + runningTurnIds.delete(event.turn_id);
210 + } else if (turn === undefined) {
211 + turns.set(event.turn_id, { state: "running", checkpoint: null });
212 + runningTurnIds.add(event.turn_id);
213 + }
214 + break;
215 + case "thread.turn-interrupt-requested":
216 + if (event.turn_id === null) break;
217 + recordActivity(event.created_at);
218 + turns.set(event.turn_id, {
219 + state: "interrupted",
220 + checkpoint: turn?.checkpoint ?? null,
221 + });
222 + runningTurnIds.delete(event.turn_id);
223 + break;
224 + case "thread.turn-diff-completed":
225 + if (event.turn_id === null) break;
226 + recordActivity(event.completed_at);
227 + turns.set(event.turn_id, {
228 + state:
229 + activeTurnId === event.turn_id
230 + ? (turn?.state ?? "running")
231 + : turn?.state === "interrupted"
232 + ? "interrupted"
233 + : "completed",
234 + checkpoint: event.checkpoint,
235 + });
236 + if (turns.get(event.turn_id)!.state === "running") {
237 + runningTurnIds.add(event.turn_id);
238 + } else {
239 + runningTurnIds.delete(event.turn_id);
240 + }
241 + break;
242 + case "thread.reverted":
243 + for (const [id, previous] of turns) {
244 + if (previous.checkpoint === null || previous.checkpoint > (event.turn_count ?? 0)) {
245 + turns.delete(id);
246 + runningTurnIds.delete(id);
247 + }
248 + }
249 + break;
250 + }
251 + }
252 + if (events.length < pageSize) break;
253 + afterSequence = events.at(-1)!.sequence;
254 + }
255 +
256 + for (const repair of repairs) {
257 + yield* sql`
258 + UPDATE projection_threads
259 + SET settled_at = COALESCE(${repair.activityAt}, created_at)
260 + WHERE thread_id = ${threadId} AND settled_override = 'settled'
261 + AND settled_at = ${repair.occurredAt}
262 + `;
263 + yield* sql`
264 + UPDATE orchestration_v2_projection_threads
265 + SET payload_json = json_set(payload_json, '$.settledAt',
266 + COALESCE(${repair.activityAt}, json_extract(payload_json, '$.createdAt')))
267 + WHERE thread_id = ${threadId}
268 + AND json_extract(payload_json, '$.settledOverride') = 'settled'
269 + AND json_extract(payload_json, '$.settledAt') = ${repair.occurredAt}
270 + `;
271 + // V2 snapshots must retain the repair after a rebuild; V1 history stays immutable.
272 + yield* sql`
273 + UPDATE orchestration_events
274 + SET payload_json = json_set(payload_json, '$.settledAt',
275 + COALESCE(${repair.activityAt}, json_extract(payload_json, '$.createdAt'),
276 + json_extract(payload_json, '$.settledAt')))
277 + WHERE aggregate_kind = 'thread' AND stream_id = ${threadId}
278 + AND application_event_version = 2
279 + AND json_extract(payload_json, '$.settledOverride') = 'settled'
280 + AND json_extract(payload_json, '$.settledAt') = ${repair.occurredAt}
281 + `;
282 + }
283 + }
284 +});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+2
@@ -45,6 +45,7 @@ describe("V2 preview upgrade", () => {
45 [63, "DropUnusedV1Projections"],
46 [64, "CanonicalizeThreadPullRequestLinks"],
47 [65, "CodexUsageNativeTurnIndex"],
48 + [66, "RepairClientSettlementTimestamps"],
49 ]);
50 assert.deepStrictEqual(yield* runMigrations(), []);
51 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -133,6 +134,7 @@ describe("V2 preview upgrade", () => {
134 [63, "DropUnusedV1Projections"],
135 [64, "CanonicalizeThreadPullRequestLinks"],
136 [65, "CodexUsageNativeTurnIndex"],
137 + [66, "RepairClientSettlementTimestamps"],
138 ]);
139 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
140 );