@devries-cameron20 / t3code Mirror · read-only / commits / 293669d759
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, 13:15 UTC

fix(server): remove abandoned v1 database copies on startup (#25301)

Co-authored-by: Matěj Suchánek <16311043+immakermatty@users.noreply.github.com>

maria committed Oct 29, 2026 at 17:51 UTC 293669d759c009ce61b47510eceb0703c4c08b0c
2 files changed +55 -1
apps/server/src/persistence/initializeV2Database.test.ts
+26
@@ -1,4 +1,5 @@
1 // @effect-diagnostics nodeBuiltinImport:off
2 +import * as NodeChildProcess from "node:child_process";
3 import * as NodeFS from "node:fs";
4 import * as NodeOS from "node:os";
5 import * as NodePath from "node:path";
@@ -182,3 +183,28 @@ it.effect("starts fresh without V1 and never imports over existing V2 state", ()
183 Effect.ensuring(Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true }))),
184 );
185 });
186 +
187 +it.effect("removes copies left by a killed start, but not one that is still running", () => {
188 + const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-v2-abandoned-"));
189 + const killed = NodeChildProcess.spawnSync(process.execPath, ["-e", ""]).pid;
190 + const running = `.v2-import-${process.pid}-Kq81Zx`;
191 + const unrelated = ".v2-import-not-a-copy";
192 + return Effect.gen(function* () {
193 + for (const name of [`.v2-import-${killed}-Wm3nPa`, ".v2-import-Q3xZ9a", running]) {
194 + NodeFS.mkdirSync(NodePath.join(directory, name));
195 + NodeFS.writeFileSync(NodePath.join(directory, name, "snapshot.sqlite"), "partial copy");
196 + }
197 + NodeFS.mkdirSync(NodePath.join(directory, unrelated));
198 + const destinationPath = NodePath.join(directory, "statev2.sqlite");
199 + NodeFS.writeFileSync(destinationPath, "existing V2 database");
200 + yield* initializeV2Database(destinationPath);
201 + assert.deepEqual(
202 + NodeFS.readdirSync(directory).sort(),
203 + [running, unrelated, "statev2.sqlite"].sort(),
204 + );
205 + assert.equal(NodeFS.readFileSync(destinationPath, "utf8"), "existing V2 database");
206 + }).pipe(
207 + Effect.provide(NodeServices.layer),
208 + Effect.ensuring(Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true }))),
209 + );
210 +});
apps/server/src/persistence/initializeV2Database.ts
+29 -1
@@ -14,6 +14,33 @@ export class V2DatabaseImportError extends Schema.TaggedError<V2DatabaseImportEr
14 }
15 }
16
17 +const isProcessAlive = (pid: number): boolean => {
18 + if (!Number.isInteger(pid) || pid <= 0) return false;
19 + try {
20 + process.kill(pid, 0);
21 + return true;
22 + } catch (error) {
23 + return error instanceof Error && "code" in error && error.code === "EPERM";
24 + }
25 +};
26 +
27 +// Legacy names have no PID; newer copies stay while their process is alive.
28 +const removeAbandonedCopies = Effect.fn("removeAbandonedCopies")(function* (directory: string) {
29 + const fs = yield* FileSystem.FileSystem;
30 + const path = yield* Path.Path;
31 + const entries = yield* fs.readDirectory(directory).pipe(
32 + Effect.catchIf(
33 + (error) => error.reason._tag === "NotFound",
34 + () => Effect.succeed([]),
35 + ),
36 + );
37 + for (const entry of entries) {
38 + const copy = /^\.v2-import-(?:(\d+)-)?[A-Za-z0-9]{6}$/.exec(entry);
39 + if (copy === null || (copy[1] !== undefined && isProcessAlive(Number(copy[1])))) continue;
40 + yield* fs.remove(path.join(directory, entry), { recursive: true });
41 + }
42 +});
43 +
44 /** Its copied legacy tables remain the source for lazy transcript import. */
45 export const initializeV2Database = Effect.fn("initializeV2Database")(function* (
46 destinationPath: string,
@@ -22,12 +49,13 @@ export const initializeV2Database = Effect.fn("initializeV2Database")(function*
49 const path = yield* Path.Path;
50 const directory = path.dirname(destinationPath);
51 const sourcePath = path.join(directory, "state.sqlite");
52 + yield* removeAbandonedCopies(directory).pipe(Effect.ignore({ log: true }));
53 yield* Effect.gen(function* () {
54 if (yield* fs.exists(destinationPath)) return;
55 if (!(yield* fs.exists(sourcePath))) return;
56 const temporaryDirectory = yield* fs.makeTempDirectoryScoped({
57 directory,
30 - prefix: ".v2-import-",
58 + prefix: `.v2-import-${process.pid}-`,
59 });
60 const snapshotPath = path.join(temporaryDirectory, "snapshot.sqlite");
61 yield* Effect.tryPromise(async () => {