The best way to control Claude Code, Codex and more from any computer or phone
fix(cli): keep the old database when project or thread records are missing (#38325)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Aritra Das committed
Nov 17, 2026 at 13:29 UTC
94dc06e7ac13a337f9cbebe5f1f89d0d47976399
5 files changed
+107
-13
apps/server/src/cli/doctor.test.ts
+2
@@ -68,6 +68,7 @@ it.effect(
68
reclaimableBytes: 1000,
69
incrementalVacuumEnabled: false,
70
legacyDatabaseBytes: 4000,
71
+ legacyOnlyRecords: { projects: 0, threads: 0 },
72
}),
73
compact: () =>
74
startServer.pipe(
@@ -151,6 +152,7 @@ it.effect("warns how long reclaiming can take and reports each step with its ela
152
reclaimableBytes: 0,
153
incrementalVacuumEnabled: false,
154
legacyDatabaseBytes: null,
155
+ legacyOnlyRecords: null,
156
}),
157
compact: (_databasePath, onStep) =>
158
Effect.gen(function* () {
apps/server/src/cli/doctor.ts
+15
-2
@@ -68,10 +68,20 @@ export const runDoctor = Effect.fn("runDoctor")(function* (
68
yield* Console.log(
69
`Database: ${formatBytes(status.databaseBytes)}, ${formatBytes(status.reclaimableBytes)} reclaimable.`,
70
);
71
+ const legacyRecords = status.legacyOnlyRecords;
72
if (status.legacyDatabaseBytes !== null) {
73
yield* Console.log(
74
`Database from before 0.1.0: ${formatBytes(status.legacyDatabaseBytes)}, kept for rolling back.`,
75
);
76
+ if (legacyRecords === null) {
77
+ yield* Console.log(
78
+ " Could not check for missing projects or threads. Keep the old database.",
79
+ );
80
+ } else if (legacyRecords.projects > 0 || legacyRecords.threads > 0) {
81
+ yield* Console.log(
82
+ ` The current database is missing legacy records. Projects missing: ${legacyRecords.projects}. Threads missing: ${legacyRecords.threads}. Keep the old database for recovery.`,
83
+ );
84
+ }
85
}
86
const repairs = [
87
...(status.incrementalVacuumEnabled && status.reclaimableBytes === 0
@@ -90,13 +100,16 @@ export const runDoctor = Effect.fn("runDoctor")(function* (
100
status.reclaimableBytes >= DATABASE_COMPACTION_RECOMMENDED_BYTES,
101
},
102
]),
93
- ...(status.legacyDatabaseBytes === null
103
+ ...(status.legacyDatabaseBytes === null ||
104
+ legacyRecords === null ||
105
+ legacyRecords.projects > 0 ||
106
+ legacyRecords.threads > 0
107
? []
108
: [
109
{
110
id: "legacy" as const,
111
title: "Delete the database from before 0.1.0",
99
- description: `Frees ${formatBytes(status.legacyDatabaseBytes)}. Your threads and settings are already in the current database.\n Warning: after this, a T3 Code version before 0.1.0 starts with no threads or settings, so you can no longer roll back.`,
112
+ description: `Frees ${formatBytes(status.legacyDatabaseBytes)}.\n Warning: after this, a T3 Code version before 0.1.0 starts with no threads or settings, so you can no longer roll back.`,
113
recommended: false,
114
},
115
]),
apps/server/src/persistence/DatabaseMaintenance.test.ts
+24
@@ -41,6 +41,8 @@ it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))
41
const legacy = path.join(userdata, "state.sqlite");
42
yield* Effect.gen(function* () {
43
const sql = yield* SqlClient.SqlClient;
44
+ yield* sql`CREATE TABLE projection_projects (project_id TEXT PRIMARY KEY)`;
45
+ yield* sql`CREATE TABLE orchestration_v2_projection_threads (thread_id TEXT PRIMARY KEY)`;
46
yield* sql`CREATE TABLE retained_data (value TEXT)`;
47
yield* sql`INSERT INTO retained_data VALUES ('keep')`;
48
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath })));
@@ -52,9 +54,31 @@ it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))
54
yield* sql`PRAGMA journal_mode = WAL`;
55
yield* sql`CREATE TABLE v1_data (value TEXT)`;
56
yield* sql`INSERT INTO v1_data VALUES ('rollback')`;
57
+ yield* sql`CREATE TABLE projection_projects (project_id TEXT PRIMARY KEY, deleted_at TEXT)`;
58
+ yield* sql`CREATE TABLE projection_threads (thread_id TEXT PRIMARY KEY, deleted_at TEXT)`;
59
+ yield* sql`INSERT INTO projection_projects VALUES ('legacy-project', NULL)`;
60
+ yield* sql`INSERT INTO projection_threads VALUES ('legacy-thread', NULL), ('deleted-thread', '2026-10-01')`;
61
}).pipe(Effect.provide(openLegacy));
62
assert.isAbove((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes ?? 0, 0);
63
64
+ assert.deepEqual((yield* maintenance.inspect(dbPath)).legacyOnlyRecords, {
65
+ projects: 1,
66
+ threads: 1,
67
+ });
68
+ const missing = yield* Effect.flip(maintenance.removeLegacyDatabase(dbPath));
69
+ assert.equal(missing._tag, "LegacyDatabaseHasUnmigratedRecordsError");
70
+ assert.isTrue(yield* fs.exists(legacy));
71
+
72
+ yield* Effect.gen(function* () {
73
+ const sql = yield* SqlClient.SqlClient;
74
+ yield* sql`INSERT INTO projection_projects VALUES ('legacy-project')`;
75
+ yield* sql`INSERT INTO orchestration_v2_projection_threads VALUES ('legacy-thread')`;
76
+ }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath })));
77
+ assert.deepEqual((yield* maintenance.inspect(dbPath)).legacyOnlyRecords, {
78
+ projects: 0,
79
+ threads: 0,
80
+ });
81
+
82
const refused = yield* Effect.gen(function* () {
83
const sql = yield* SqlClient.SqlClient;
84
yield* sql`SELECT value FROM v1_data`;
apps/server/src/persistence/DatabaseMaintenance.ts
+62
-10
@@ -11,6 +11,7 @@ import * as Schema from "effect/Schema";
11
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect has no free-space query.
12
import * as NodeFSP from "node:fs/promises";
13
import * as NodeOS from "node:os";
14
+import * as NodeURL from "node:url";
15
import * as SqlClient from "effect/sql/SqlClient";
16
import { isSqlError } from "effect/sql/SqlError";
17
@@ -66,7 +67,16 @@ export class LegacyDatabaseRemoveError extends Schema.TaggedError<LegacyDatabase
67
{ path: Schema.String, cause: Schema.Defect() },
68
) {
69
override get message() {
69
- return "Could not delete the database from before 0.1.0. Check that this user can write to the data directory.";
70
+ return "Could not delete the database from before 0.1.0. Check that both databases are readable and the data directory is writable.";
71
+ }
72
+}
73
+
74
+export class LegacyDatabaseHasUnmigratedRecordsError extends Schema.TaggedError<LegacyDatabaseHasUnmigratedRecordsError>()(
75
+ "LegacyDatabaseHasUnmigratedRecordsError",
76
+ { projects: Schema.Int, threads: Schema.Int },
77
+) {
78
+ override get message() {
79
+ return `The current database is missing legacy records. Projects missing: ${this.projects}. Threads missing: ${this.threads}. Keep the old database for recovery.`;
80
}
81
}
82
@@ -79,6 +89,8 @@ export class DiskSpaceCheckError extends Schema.TaggedError<DiskSpaceCheckError>
89
}
90
}
91
92
+type LegacyOnlyRecords = { readonly projects: number; readonly threads: number };
93
+
94
export type DatabaseCompactStep = "prune-events" | "vacuum" | "rebuild-search" | "checkpoint";
95
96
export class DatabaseMaintenance extends Context.Service<
@@ -91,6 +103,7 @@ export class DatabaseMaintenance extends Context.Service<
103
readonly incrementalVacuumEnabled: boolean;
104
/** Size of the database from before 0.1.0 still kept beside it, or null. */
105
readonly legacyDatabaseBytes: number | null;
106
+ readonly legacyOnlyRecords: LegacyOnlyRecords | null;
107
},
108
DatabaseMissingError | DatabaseInspectError
109
>;
@@ -113,7 +126,10 @@ export class DatabaseMaintenance extends Context.Service<
126
/** Deletes only the pre-0.1.0 database; earlier versions still use it. */
127
readonly removeLegacyDatabase: (
128
databasePath: string,
116
- ) => Effect.Effect<void, DatabaseInUseError | LegacyDatabaseRemoveError>;
129
+ ) => Effect.Effect<
130
+ void,
131
+ DatabaseInUseError | LegacyDatabaseRemoveError | LegacyDatabaseHasUnmigratedRecordsError
132
+ >;
133
}
134
>()("t3/persistence/DatabaseMaintenance") {}
135
@@ -143,7 +159,7 @@ const lockExclusively = Effect.fn("DatabaseMaintenance.lockExclusively")(functio
159
) {
160
const sql = yield* SqlClient.SqlClient;
161
yield* sql`PRAGMA busy_timeout = 0`;
146
- yield* sql`PRAGMA locking_mode = EXCLUSIVE`;
162
+ yield* sql`PRAGMA main.locking_mode = EXCLUSIVE`;
163
yield* sql`BEGIN EXCLUSIVE`.pipe(
164
Effect.mapError((cause) =>
165
isSqlError(cause) && cause.reason._tag === "LockTimeoutError"
@@ -166,6 +182,29 @@ const make = Effect.gen(function* () {
182
}
183
});
184
185
+ const readLegacyOnlyRecords = Effect.fn("DatabaseMaintenance.readLegacyOnlyRecords")(function* (
186
+ databasePath: string,
187
+ ) {
188
+ const sql = yield* SqlClient.SqlClient;
189
+ const currentUri = `${NodeURL.pathToFileURL(databasePath).href}?mode=ro`;
190
+ yield* sql`ATTACH DATABASE ${currentUri} AS current_v2`;
191
+ const [counts] = yield* sql<LegacyOnlyRecords>`
192
+ SELECT
193
+ (SELECT COUNT(*) FROM main.projection_projects legacy
194
+ WHERE legacy.deleted_at IS NULL AND NOT EXISTS (
195
+ SELECT 1 FROM current_v2.projection_projects current
196
+ WHERE current.project_id = legacy.project_id
197
+ )) AS projects,
198
+ (SELECT COUNT(*) FROM main.projection_threads legacy
199
+ WHERE legacy.deleted_at IS NULL AND NOT EXISTS (
200
+ SELECT 1 FROM current_v2.orchestration_v2_projection_threads current
201
+ WHERE current.thread_id = legacy.thread_id
202
+ )) AS threads
203
+ `;
204
+ if (!counts) return yield* Effect.die(new Error("Missing result from legacy database counts"));
205
+ return counts;
206
+ });
207
+
208
const inspect = Effect.fn("DatabaseMaintenance.inspect")(
209
function* (databasePath: string) {
210
yield* locate(databasePath);
@@ -176,7 +215,14 @@ const make = Effect.gen(function* () {
215
const legacyDatabaseBytes = (yield* fs.exists(legacy))
216
? Number((yield* fs.stat(legacy)).size)
217
: null;
179
- return { ...storage, legacyDatabaseBytes };
218
+ const legacyOnlyRecords =
219
+ legacyDatabaseBytes === null
220
+ ? null
221
+ : yield* readLegacyOnlyRecords(databasePath).pipe(
222
+ Effect.provide(NodeSqliteClient.layer({ filename: legacy, readonly: true })),
223
+ Effect.orElseSucceed(() => null),
224
+ );
225
+ return { ...storage, legacyDatabaseBytes, legacyOnlyRecords };
226
},
227
(effect, databasePath) =>
228
effect.pipe(
@@ -242,21 +288,27 @@ const make = Effect.gen(function* () {
288
{ discard: true },
289
);
290
const connection = NodeSqliteClient.layer({ filename: legacy });
291
+ const check = lockExclusively(legacy).pipe(
292
+ Effect.andThen(readLegacyOnlyRecords(databasePath)),
293
+ Effect.flatMap((counts) =>
294
+ counts.projects > 0 || counts.threads > 0
295
+ ? new LegacyDatabaseHasUnmigratedRecordsError(counts)
296
+ : Effect.void,
297
+ ),
298
+ );
299
if ((yield* HostProcess.Platform) === "win32") {
300
// Windows blocks unlink while any SQLite handle is open, including our own.
247
- yield* lockExclusively(legacy).pipe(Effect.provide(connection));
301
+ yield* check.pipe(Effect.provide(connection));
302
yield* removeFiles;
303
} else {
250
- yield* lockExclusively(legacy).pipe(
251
- Effect.andThen(removeFiles),
252
- Effect.provide(connection),
253
- );
304
+ yield* check.pipe(Effect.andThen(removeFiles), Effect.provide(connection));
305
}
306
},
307
(effect, databasePath) =>
308
effect.pipe(
309
Effect.mapError((cause) =>
259
- cause._tag === "DatabaseInUseError"
310
+ cause._tag === "DatabaseInUseError" ||
311
+ cause._tag === "LegacyDatabaseHasUnmigratedRecordsError"
312
? cause
313
: new LegacyDatabaseRemoveError({ path: legacyPath(databasePath), cause }),
314
),
docs/user/background-service.md
+4
-1
@@ -199,4 +199,7 @@ once next to the database for its write-ahead log.
199
If you upgraded from a version before 0.1.0, its database (`state.sqlite`) is
200
still kept next to the current one so you can roll back. `t3 doctor` offers to
201
delete it, but never selects that repair for you and `--fix` skips it: once it
202
-is gone, a version before 0.1.0 starts with no threads or settings.
202
+is gone, a version before 0.1.0 starts with no threads or settings. It does
203
+not offer to delete it while any project or thread you have not deleted is
204
+missing from the current database, so the old file stays available for
205
+recovery.