@devries-cameron20 / t3code Mirror · read-only / commits / 9ef12568cb
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, 03:15 UTC

fix(server): keep writes moving with a larger WAL limit (#31804)

Yash Singh committed Nov 5, 2026 at 22:34 UTC 9ef12568cb1fff60600f7f8556e6daf6fe16c874
3 files changed +89 -2
apps/server/src/persistence/Sqlite.test.ts
+87
@@ -2,6 +2,8 @@
2 import * as NodeFS from "node:fs";
3 import * as NodeOS from "node:os";
4 import * as NodePath from "node:path";
5 +import * as NodeSqlite from "node:sqlite";
6 +import type * as NodeWorkerThreads from "node:worker_threads";
7
8 import * as NodeServices from "@effect/platform-node/NodeServices";
9 import { assert, it } from "@effect/vitest";
@@ -9,9 +11,94 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
11 import * as Effect from "effect/Effect";
12 import * as Layer from "effect/Layer";
13 import * as SqlClient from "effect/sql/SqlClient";
14 +import { vi } from "vite-plus/test";
15
16 import * as SqlitePersistence from "./Sqlite.ts";
17
18 +const workers = vi.hoisted((): Array<NodeWorkerThreads.Worker> => []);
19 +
20 +vi.mock("node:worker_threads", async (importOriginal) => {
21 + const actual = await importOriginal<typeof NodeWorkerThreads>();
22 + class RecordedWorker extends actual.Worker {
23 + constructor(...args: ConstructorParameters<typeof actual.Worker>) {
24 + super(...args);
25 + workers.push(this);
26 + }
27 + }
28 + return { ...actual, Worker: RecordedWorker };
29 +});
30 +
31 +it.effect("keeps writes moving past a 48 MiB WAL while a reader retains its snapshot", () => {
32 + const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-burst-"));
33 + const dbPath = NodePath.join(tempDir, "state.sqlite");
34 + const persistence = SqlitePersistence.layerFromPath(dbPath).pipe(
35 + Layer.provide(NodeServices.layer),
36 + );
37 +
38 + return Effect.gen(function* () {
39 + const sql = yield* SqlClient.SqlClient;
40 + const checkpointWorker = workers.at(-1)!;
41 + yield* sql`CREATE TABLE burst_probe(id INTEGER PRIMARY KEY, payload BLOB)`;
42 + yield* sql`INSERT INTO burst_probe VALUES (1, NULL)`;
43 + yield* sql`PRAGMA wal_checkpoint(TRUNCATE)`;
44 + const reader = yield* Effect.acquireRelease(
45 + Effect.sync(() => new NodeSqlite.DatabaseSync(dbPath, { readOnly: true })),
46 + (database) => Effect.sync(() => database.close()),
47 + );
48 + reader.exec("BEGIN");
49 + assert.equal(reader.prepare("SELECT COUNT(*) AS count FROM burst_probe").get()?.count, 1);
50 +
51 + yield* sql.withTransaction(
52 + sql`INSERT INTO burst_probe VALUES (2, zeroblob(${48 * 1024 * 1024}))`,
53 + );
54 + const state = yield* Effect.callback<{
55 + readonly log: number;
56 + readonly checkpointed: number;
57 + readonly blocked: boolean;
58 + }>((resume) => {
59 + const onMessage = (message: {
60 + readonly type?: string;
61 + readonly log?: number;
62 + readonly checkpointed?: number;
63 + readonly blocked?: boolean;
64 + }) => {
65 + if (
66 + message.type === "checkpoint" &&
67 + typeof message.log === "number" &&
68 + message.log > 10_000 &&
69 + typeof message.checkpointed === "number" &&
70 + typeof message.blocked === "boolean"
71 + ) {
72 + checkpointWorker.off("message", onMessage);
73 + resume(
74 + Effect.succeed({
75 + log: message.log,
76 + checkpointed: message.checkpointed,
77 + blocked: message.blocked,
78 + }),
79 + );
80 + }
81 + };
82 + checkpointWorker.on("message", onMessage);
83 + checkpointWorker.ref();
84 + // oxlint-disable-next-line unicorn/require-post-message-target-origin -- Node workers do not accept a target origin.
85 + checkpointWorker.postMessage("barrier");
86 + return Effect.sync(() => checkpointWorker.off("message", onMessage));
87 + });
88 + assert.isBelow(state.checkpointed, state.log);
89 + assert.isFalse(state.blocked);
90 +
91 + yield* sql.withTransaction(sql`INSERT INTO burst_probe VALUES (3, NULL)`);
92 + assert.equal(reader.prepare("SELECT COUNT(*) AS count FROM burst_probe").get()?.count, 1);
93 + reader.exec("COMMIT");
94 + assert.equal(reader.prepare("SELECT COUNT(*) AS count FROM burst_probe").get()?.count, 3);
95 + }).pipe(
96 + Effect.scoped,
97 + Effect.provide(persistence),
98 + Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))),
99 + );
100 +});
101 +
102 // Linux names an open, already-deleted file "<path> (deleted)" in /proc/self/fd.
103 // SQLite names its temp files "etilqs_*" and, without TMPDIR, puts them in
104 // /var/tmp while os.tmpdir() is /tmp, so match the name, not the directory.
apps/server/src/persistence/Sqlite.ts
+1 -1
@@ -9,7 +9,7 @@ import { runMigrations } from "./Migrations.ts";
9 import { initializeV2Database } from "./initializeV2Database.ts";
10 import * as ServerConfig from "../config.ts";
11
12 -const WAL_SIZE_LIMIT_BYTES = 32 * 1024 * 1024;
12 +const WAL_SIZE_LIMIT_BYTES = 128 * 1024 * 1024;
13
14 const layerSetup = Layer.effectDiscard(
15 Effect.gen(function* () {
packages/shared/src/nodeSqliteClient.ts
+1 -1
@@ -486,7 +486,7 @@ const makeReader = Effect.fnUntraced(function* (
486
487 const CHECKPOINT_FRAMES = 1000;
488 const CHECKPOINT_IDLE_MS = 100;
489 -const CHECKPOINT_BACKPRESSURE_FRAMES = 10 * CHECKPOINT_FRAMES;
489 +const CHECKPOINT_BACKPRESSURE_FRAMES = 32_768;
490
491 const readSafeSqliteInteger = (value: unknown) => {
492 const number = typeof value === "bigint" ? Number(value) : value;