Skip to content

Commit f4649dd

Browse files
t3dotggclaude
andcommitted
fix: migrate-dev-db skips every thread the server would recover
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 89ed105 commit f4649dd

2 files changed

Lines changed: 52 additions & 18 deletions

File tree

‎apps/server/scripts/migrate-dev-db.test.ts‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,31 @@ const createFixtureSource = Effect.fn("createMigrateDevDbFixtureSource")(functio
3838

3939
const forkPayload =
4040
'{"lineage":{"parentThreadId":"stopped-thread","relationshipToParent":"fork","rootThreadId":"stopped-thread"}}';
41+
const subagentPayload =
42+
'{"lineage":{"parentThreadId":"subagent-parent","relationshipToParent":"subagent","rootThreadId":"subagent-parent"},"forkedFrom":{"type":"node","nodeId":"node-1"}}';
43+
// Excluded threads are newer than the kept family, so only the filters
44+
// can keep them out of a one-family-per-project clone.
4145
const threads = [
42-
["stopped-thread", "project-kept", "completed", "{}"],
43-
["fork-thread", "project-kept", "completed", forkPayload],
44-
["running-thread", "project-kept", "running", "{}"],
45-
["settled-thread", "project-kept", "completed", '{"settledAt":"2026-08-01"}'],
46-
["limit-thread", "project-kept", "completed", '{"limitRecovery":{"autoResume":true}}'],
47-
["deleted-project-thread", "project-deleted", "completed", "{}"],
46+
["stopped-thread", "project-kept", "completed", "{}", "2026-08-01"],
47+
["fork-thread", "project-kept", "completed", forkPayload, "2026-08-02"],
48+
["running-thread", "project-kept", "running", "{}", "2026-08-05"],
49+
["settled-thread", "project-kept", "completed", '{"settledAt":"2026-08-01"}', "2026-08-05"],
50+
[
51+
"limit-thread",
52+
"project-kept",
53+
"completed",
54+
'{"limitRecovery":{"autoResume":true}}',
55+
"2026-08-05",
56+
],
57+
// Its result never reached the parent, so startup would deliver it.
58+
["subagent-parent", "project-kept", "completed", "{}", "2026-08-05"],
59+
["subagent-child", "project-kept", "completed", subagentPayload, "2026-08-05"],
60+
["deleted-project-thread", "project-deleted", "completed", "{}", "2026-08-05"],
4861
] as const;
49-
for (const [threadId, projectId, runStatus, payload] of threads) {
62+
for (const [threadId, projectId, runStatus, payload, updatedAt] of threads) {
5063
yield* sql`INSERT INTO orchestration_v2_projection_threads
5164
(thread_id, project_id, title, default_provider, runtime_mode, interaction_mode, created_at, updated_at, payload_json)
52-
VALUES (${threadId}, ${projectId}, ${threadId}, 'codex', 'full-access', 'default', '2026-08-01', '2026-08-01', ${payload})`;
65+
VALUES (${threadId}, ${projectId}, ${threadId}, 'codex', 'full-access', 'default', '2026-08-01', ${updatedAt}, ${payload})`;
5366
yield* sql`INSERT INTO orchestration_v2_projection_runs
5467
(run_id, thread_id, ordinal, provider, status, requested_at, payload_json)
5568
VALUES (${`run-${threadId}`}, ${threadId}, 1, 'codex', ${runStatus}, '2026-08-01', '{}')`;
@@ -60,7 +73,7 @@ const createFixtureSource = Effect.fn("createMigrateDevDbFixtureSource")(functio
6073
// A provider session shared by two threads names its latest writer.
6174
yield* sql`INSERT INTO orchestration_v2_projection_provider_sessions
6275
(provider_session_id, thread_id, provider, status, updated_at, payload_json)
63-
VALUES ('session-shared', 'running-thread', 'codex', 'ready', '2026-08-01', '{}')`;
76+
VALUES ('session-shared', 'running-thread', 'codex', 'stopped', '2026-08-01', '{}')`;
6477
yield* sql`INSERT INTO orchestration_v2_projection_provider_session_bindings
6578
(provider_session_id, thread_id)
6679
VALUES ('session-shared', 'running-thread'), ('session-shared', 'stopped-thread')`;

‎apps/server/scripts/migrate-dev-db.ts‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ import { resolveWorktreeT3Home } from "@t3tools/shared/devHome";
3232
import * as Console from "effect/Console";
3333
import * as Effect from "effect/Effect";
3434
import * as FileSystem from "effect/FileSystem";
35+
import * as Layer from "effect/Layer";
3536
import * as Option from "effect/Option";
3637
import * as Path from "effect/Path";
3738
import * as Schema from "effect/Schema";
3839
import * as SqlClient from "effect/unstable/sql/SqlClient";
3940
import { Command, Flag } from "effect/unstable/cli";
4041

42+
import * as ProjectionStore from "../src/orchestration-v2/ProjectionStore.ts";
4143
import { migrationManifest, runMigrations } from "../src/persistence/Migrations.ts";
4244
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
4345

@@ -233,18 +235,33 @@ const ensureNotInUse = Effect.fn("ensureDevDbNotInUse")(function* (databasePath:
233235
}
234236
});
235237

238+
const RECOVERY_KINDS: ReadonlyArray<ProjectionStore.ProjectionRecoveryKind> = [
239+
"queued-runs",
240+
"runtime",
241+
"subagent-results",
242+
"delegated-completions",
243+
];
244+
236245
const pruneSnapshot = Effect.fn("pruneDevDbSnapshot")(function* (input: RunMigrateDevDbInput) {
237246
const sql = yield* SqlClient.SqlClient;
238247

239-
// Threads the server would resume or start work on by itself: in-flight or
240-
// queued runs, undelivered delegated results, and usage-limit recovery.
241-
yield* sql`CREATE TEMP TABLE live_threads AS
248+
// Threads the server would resume or start work on by itself. Its own
249+
// recovery queries find most of them; usage-limit recovery also depends on
250+
// settings, so any thread whose latest run failed or that has a recovery
251+
// choice counts as live too.
252+
const projections = yield* ProjectionStore.ProjectionStoreV2;
253+
const recoveryThreadIds = yield* Effect.forEach(RECOVERY_KINDS, (kind) =>
254+
projections.getRecoveryThreadIds(kind),
255+
);
256+
yield* sql`CREATE TEMP TABLE live_threads (thread_id TEXT PRIMARY KEY)`;
257+
for (const threadId of new Set(recoveryThreadIds.flat())) {
258+
yield* sql`INSERT INTO live_threads (thread_id) VALUES (${threadId})`;
259+
}
260+
yield* sql`INSERT OR IGNORE INTO live_threads (thread_id)
242261
SELECT r.thread_id FROM orchestration_v2_projection_runs r
243-
WHERE r.status IN ('preparing', 'queued', 'starting', 'running', 'waiting')
244-
OR json_type(r.payload_json, '$.delegatedCompletion.delivery') = 'object'
245-
OR (r.status = 'failed' AND r.ordinal = (
246-
SELECT MAX(latest.ordinal) FROM orchestration_v2_projection_runs latest
247-
WHERE latest.thread_id = r.thread_id))
262+
WHERE r.status = 'failed' AND r.ordinal = (
263+
SELECT MAX(latest.ordinal) FROM orchestration_v2_projection_runs latest
264+
WHERE latest.thread_id = r.thread_id)
248265
UNION
249266
SELECT thread_id FROM orchestration_v2_projection_threads
250267
WHERE json_extract(payload_json, '$.limitRecovery') IS NOT NULL`;
@@ -484,7 +501,11 @@ export const runMigrateDevDb = Effect.fn("runMigrateDevDb")(function* (
484501
`Pruning to ${input.projects} projects, ${input.threadsPerProject} stopped threads each...`,
485502
);
486503
const result = yield* pruneSnapshot(input).pipe(
487-
Effect.provide(NodeSqliteClient.layer({ filename: snapshotPath })),
504+
Effect.provide(
505+
ProjectionStore.layer.pipe(
506+
Layer.provideMerge(NodeSqliteClient.layer({ filename: snapshotPath })),
507+
),
508+
),
488509
wrapPhase("prune", snapshotPath),
489510
);
490511

0 commit comments

Comments
 (0)