@devries-cameron20 / t3code Mirror · read-only / commits / 8aa5be2f02
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 23, 21:45 UTC

fix(server): the SQLite WAL file shrinks back after large writes (#13684)

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Theo Browne committed Sep 25, 2026 at 19:28 UTC 8aa5be2f02c3b4b9b5b3b86acbf5f2b0467438f4
2 files changed +37 -1
apps/server/src/persistence/Layers/Sqlite.test.ts
+31 -1
@@ -10,7 +10,11 @@ import * as Effect from "effect/Effect";
10 import * as Layer from "effect/Layer";
11 import * as SqlClient from "effect/unstable/sql/SqlClient";
12
13 -import { SqlitePersistenceMemory, makeSqlitePersistenceLive } from "./Sqlite.ts";
13 +import {
14 + SqlitePersistenceMemory,
15 + WAL_SIZE_LIMIT_BYTES,
16 + makeSqlitePersistenceLive,
17 +} from "./Sqlite.ts";
18
19 const lockHolderSource = `
20 const { DatabaseSync } = require("node:sqlite");
@@ -57,6 +61,32 @@ it.effect("waits out a concurrent writer instead of failing with SQLITE_BUSY", (
61 );
62 });
63
64 +it.effect("shrinks the WAL file back to the size limit after a large write", () => {
65 + const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-wal-"));
66 + const dbPath = NodePath.join(tempDir, "state.sqlite");
67 + const walFileSize = () => NodeFS.statSync(`${dbPath}-wal`).size;
68 + // About 25% more 4 KB rows than the limit holds, in one transaction.
69 + const rowCount = Math.ceil((WAL_SIZE_LIMIT_BYTES * 1.25) / 4000);
70 +
71 + return Effect.gen(function* () {
72 + const sql = yield* SqlClient.SqlClient;
73 + yield* sql`CREATE TABLE wal_probe(payload BLOB)`;
74 + yield* sql`
75 + WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ${rowCount})
76 + INSERT INTO wal_probe(payload) SELECT randomblob(4000) FROM n
77 + `;
78 + assert.isAbove(walFileSize(), WAL_SIZE_LIMIT_BYTES);
79 +
80 + // The auto-checkpoint after the large commit copied every frame into the
81 + // database, so the next commit restarts the WAL and cuts the file back.
82 + yield* sql`INSERT INTO wal_probe(payload) VALUES (x'00')`;
83 + assert.isAtMost(walFileSize(), WAL_SIZE_LIMIT_BYTES);
84 + }).pipe(
85 + Effect.provide(makeSqlitePersistenceLive(dbPath).pipe(Layer.provide(NodeServices.layer))),
86 + Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))),
87 + );
88 +});
89 +
90 it.effect("applies busy_timeout in the shared persistence setup", () =>
91 Effect.gen(function* () {
92 const sql = yield* SqlClient.SqlClient;
apps/server/src/persistence/Layers/Sqlite.ts
+6
@@ -8,6 +8,9 @@ import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
8 import { runMigrations } from "../Migrations.ts";
9 import { ServerConfig } from "../../config.ts";
10
11 +// Size the -wal file is cut back to on the first commit after a WAL reset.
12 +export const WAL_SIZE_LIMIT_BYTES = 32 * 1024 * 1024;
13 +
14 const setup = Layer.effectDiscard(
15 Effect.gen(function* () {
16 const sql = yield* SqlClient.SqlClient;
@@ -15,6 +18,9 @@ const setup = Layer.effectDiscard(
18 yield* sql`PRAGMA busy_timeout = 5000;`;
19 yield* sql`PRAGMA foreign_keys = ON;`;
20 yield* sql`PRAGMA journal_mode = WAL;`;
21 + // PASSIVE checkpoints never shrink the -wal file, so it otherwise keeps its
22 + // largest size until the last connection closes.
23 + yield* sql.unsafe(`PRAGMA journal_size_limit = ${WAL_SIZE_LIMIT_BYTES};`);
24 yield* runMigrations();
25 }),
26 );