@devries-cameron20 / t3code-1 / commits / ab2bf940f6
Fork 0

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

fix(server): migrated databases can return freed space to disk (#30066)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Advait Paliwal committed Nov 14, 2026 at 17:33 UTC ab2bf940f620f4ddf920588eb6c6cc88f2e67428
2 files changed +31 -12
apps/server/src/persistence/initializeV2Database.test.ts
+3 -1
@@ -132,7 +132,7 @@ it.effect("includes committed WAL data and does not publish a failed snapshot",
132 const source = new NodeSqlite.DatabaseSync(sourcePath);
133 try {
134 source.exec(
135 - "PRAGMA journal_mode=WAL; CREATE TABLE messages(text TEXT); INSERT INTO messages VALUES ('committed'); BEGIN; INSERT INTO messages VALUES ('uncommitted');",
135 + "PRAGMA journal_mode=WAL; CREATE TABLE messages(text TEXT); INSERT INTO messages VALUES ('committed'); CREATE TABLE dropped(blob BLOB); INSERT INTO dropped VALUES (randomblob(65536)); DROP TABLE dropped; BEGIN; INSERT INTO messages VALUES ('uncommitted');",
136 );
137 yield* initializeV2Database(destinationPath);
138 const copy = new NodeSqlite.DatabaseSync(destinationPath, { readOnly: true });
@@ -144,6 +144,8 @@ it.effect("includes committed WAL data and does not publish a failed snapshot",
144 .map((row) => row.text),
145 ["committed"],
146 );
147 + assert.deepEqual(copy.prepare("PRAGMA auto_vacuum").get(), { auto_vacuum: 2 });
148 + assert.deepEqual(copy.prepare("PRAGMA freelist_count").get(), { freelist_count: 0 });
149 } finally {
150 copy.close();
151 }
apps/server/src/persistence/initializeV2Database.ts
+28 -11
@@ -1,4 +1,4 @@
1 -import * as NodeSqlite from "node:sqlite";
1 +import * as NodeWorkerThreads from "node:worker_threads";
2
3 import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
@@ -26,6 +26,20 @@ const isProcessAlive = (pid: number): boolean => {
26 }
27 };
28
29 +// V1 files predate auto_vacuum, and a page backup would keep that. VACUUM INTO compacts the copy and
30 +// enables incremental mode, so the server can return freed pages; the worker keeps it off the event loop.
31 +const snapshotWorkerSource = `
32 +const { workerData } = require("node:worker_threads");
33 +const { DatabaseSync } = require("node:sqlite");
34 +const database = new DatabaseSync(workerData.sourcePath, { readOnly: true });
35 +try {
36 + database.exec("PRAGMA auto_vacuum = INCREMENTAL");
37 + database.prepare("VACUUM INTO ?").run(workerData.snapshotPath);
38 +} finally {
39 + database.close();
40 +}
41 +`;
42 +
43 // Legacy names have no PID; newer copies stay while their process is alive.
44 const removeAbandonedCopies = Effect.fn("removeAbandonedCopies")(function* (directory: string) {
45 const fs = yield* FileSystem.FileSystem;
@@ -60,16 +74,19 @@ export const initializeV2Database = Effect.fn("initializeV2Database")(function*
74 prefix: `.v2-import-${process.pid}-`,
75 });
76 const snapshotPath = path.join(temporaryDirectory, "snapshot.sqlite");
63 - yield* Effect.tryPromise({
64 - try: async () => {
65 - const database = new NodeSqlite.DatabaseSync(sourcePath, { readOnly: true });
66 - try {
67 - await NodeSqlite.backup(database, snapshotPath);
68 - } finally {
69 - database.close();
70 - }
71 - },
72 - catch: (cause) => new V2DatabaseImportError({ sourcePath, destinationPath, cause }),
77 + yield* Effect.callback<void, V2DatabaseImportError>((resume) => {
78 + const fail = (cause: unknown) =>
79 + resume(Effect.fail(new V2DatabaseImportError({ sourcePath, destinationPath, cause })));
80 + const worker = new NodeWorkerThreads.Worker(snapshotWorkerSource, {
81 + eval: true,
82 + workerData: { sourcePath, snapshotPath },
83 + });
84 + worker.once("error", fail);
85 + worker.once("exit", (code) =>
86 + code === 0 ? resume(Effect.void) : fail(new Error(`Snapshot worker exited with ${code}`)),
87 + );
88 + // Interruption waits for the worker to exit before the scope removes its directory.
89 + return Effect.promise(() => worker.terminate());
90 });
91 // Publish only a complete snapshot, without replacing an existing V2 database.
92 yield* fs.link(snapshotPath, destinationPath).pipe(