@devries-cameron20 / t3code-1 / commits / 4a53d80239
Fork 0

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

fix(server): database folders on btrfs are No_COW so SQLite stops fragmenting (#36518)

Julius Marminge committed Nov 13, 2026 at 22:58 UTC 4a53d80239dfcea6a5701a080c70fc3f2aa0da31
3 files changed +45 -1
apps/server/src/config.ts
+7 -1
@@ -10,6 +10,7 @@ import * as Schema from "effect/Schema";
10
11 import type { OtlpEndpointSource } from "@t3tools/contracts";
12 import { sweepStalePendingAttachments } from "./attachmentStore.ts";
13 +import { markNoCopyOnWrite } from "./noCopyOnWrite.ts";
14 import {
15 DEFAULT_SIGNAL_EXPORT,
16 nodeProcessRuntimeAttributes,
@@ -179,9 +180,14 @@ export const ensureServerDirectories = Effect.fn(function* (derivedPaths: Server
180 const fs = yield* FileSystem.FileSystem;
181 const path = yield* Path.Path;
182
183 + yield* fs.makeDirectory(derivedPaths.stateDir, { recursive: true });
184 + // Only before the database exists: a No_COW WAL beside an existing copy-on-write database
185 + // could not be reflinked into one backup folder with it. When unsure, leave the folder as is.
186 + if (!(yield* fs.exists(derivedPaths.dbPath).pipe(Effect.orElseSucceed(() => true)))) {
187 + yield* Effect.promise(() => markNoCopyOnWrite(derivedPaths.stateDir));
188 + }
189 yield* Effect.all(
190 [
184 - fs.makeDirectory(derivedPaths.stateDir, { recursive: true }),
191 fs.makeDirectory(derivedPaths.logsDir, { recursive: true }),
192 fs.makeDirectory(derivedPaths.providerLogsDir, { recursive: true }),
193 fs.makeDirectory(derivedPaths.terminalLogsDir, { recursive: true }),
apps/server/src/noCopyOnWrite.ts new
+36
@@ -0,0 +1,36 @@
1 +// @effect-diagnostics nodeBuiltinImport:off
2 +// The service launcher shares this and runs without an Effect runtime.
3 +import * as NodeChildProcess from "node:child_process";
4 +import * as NodeFSP from "node:fs/promises";
5 +
6 +const BTRFS_SUPER_MAGIC = 0x9123683e;
7 +
8 +async function onBtrfs(target: string): Promise<boolean> {
9 + // oxlint-disable-next-line t3code/no-global-process-runtime -- Shared with the standalone launcher.
10 + if (process.platform !== "linux") return false;
11 + const stats = await NodeFSP.statfs(target).catch(() => undefined);
12 + return stats?.type === BTRFS_SUPER_MAGIC;
13 +}
14 +
15 +const run = (command: string, args: ReadonlyArray<string>) =>
16 + new Promise<string | undefined>((resolve) => {
17 + NodeChildProcess.execFile(command, args, (error, stdout) =>
18 + resolve(error === null ? stdout : undefined),
19 + );
20 + });
21 +
22 +/**
23 + * SQLite rewrites pages in place, which fragments copy-on-write files on btrfs. Files created
24 + * in a No_COW directory inherit the flag, as systemd-journald does for its journals. Node has no
25 + * ioctl, so this runs chattr. Btrfs refuses reflinks between No_COW and copy-on-write files.
26 + */
27 +export async function markNoCopyOnWrite(directory: string): Promise<void> {
28 + if (await onBtrfs(directory)) await run("chattr", ["+C", directory]);
29 +}
30 +
31 +export async function isNoCopyOnWrite(file: string): Promise<boolean> {
32 + if (!(await onBtrfs(file))) return false;
33 + // lsattr skips symlinks; its first field is the flag string, e.g. "---------------C------".
34 + const output = await run("lsattr", ["-d", await NodeFSP.realpath(file).catch(() => file)]);
35 + return output?.trimStart().split(/\s/, 1)[0]?.includes("C") ?? false;
36 +}
apps/server/src/serviceLauncher.ts
+2
@@ -8,6 +8,7 @@ import * as NodeFSP from "node:fs/promises";
8 import * as NodePath from "node:path";
9 import * as NodeUtil from "node:util";
10 import { acquireServiceSelectionLock } from "./cloud/serviceSelectionLock.ts";
11 +import { isNoCopyOnWrite, markNoCopyOnWrite } from "./noCopyOnWrite.ts";
12
13 import type {
14 PendingServiceUpdate,
@@ -438,6 +439,7 @@ export async function databaseBackupBlocker(
439 const { readFreeBytes, canClone } = { ...filesystemBackupSpaceProbe, ...probe };
440 const backupRoot = NodePath.join(baseDir, "runtime", "db-backup");
441 await NodeFSP.mkdir(backupRoot, { recursive: true, mode: 0o700 });
442 + if (await isNoCopyOnWrite(dbPath)) await markNoCopyOnWrite(backupRoot);
443 await removeAbandonedBackupCopies(backupRoot);
444 const [files, free] = await Promise.all([statDatabaseFiles(dbPath), readFreeBytes(backupRoot)]);
445 const size = files.reduce((total, file) => total + file.size, 0);