fix(server): make SQLite checkpoints durable on macOS #19304
main fromrefs/pull/19304/head·merged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubSince #19205 set synchronous = NORMAL, WAL checkpoints are the only point where SQLite syncs the database to disk. On macOS, plain fsync() does not flush the drive's write cache, so a hard shutdown can still lose or corrupt statev2.sqlite (#13544).
This sets PRAGMA checkpoint_fullfsync = ON, so checkpoint syncs use F_FULLFSYNC on macOS. Other platforms ignore it. PRAGMA fullfsync stays off, so normal commits stay cheap.
The pragma is per connection, so it is set on both connections that write the state database:
- The shared setup in
apps/server/src/persistence/Sqlite.ts, next tosynchronous = NORMAL. The server and CLI paths use this. - The direct connection in
apps/server/scripts/t3-sqlite-state.ts, which does not use the shared setup.
Not changed: the read-only reader worker in packages/shared/src/nodeSqliteClient.ts (a read-only connection cannot checkpoint), the read-only V1 source in initializeV2Database.ts, and the throwaway dev databases in migrate-dev-db.ts and scripts/mobile-showcase-environment.ts.
Fixes #13544.
Credit#
This replaces #14457 by @SoroushRF, which had conflicts and pointed to old paths. Their commit is cherry-picked with authorship kept. The second commit only updates comments for the current paths.
Co-authored-by: Soroush Baraouf <soroushraouf44@gmail.com>
Proof#
vp test run src/persistence/Sqlite.test.ts scripts/t3-sqlite-state.test.ts: 10 passed. Both new tests readPRAGMA checkpoint_fullfsyncback as1. With only the two pragma lines removed, exactly those two tests fail and read back0.vp lintandvp fmt --checkon the changed files pass.- Not verified on macOS hardware. The tests confirm the setting is applied. They do not exercise
F_FULLFSYNCor reproduce power loss.
Reviewed with sol-loop: 1 round with GPT-6.1-Sol (approved, no findings).
Takeover and changes by Claude Opus 5.5 in Claude Code, working in T3 Code.
🤖 Generated with Claude Code
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XS
juliusmarminge added labelsmacroscope-review
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire 5.0 KiB 5.0 KiB +7 B (+0.1%) 6.8 KiB ✅ Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB +7 B (+0.6%) 2.0 KiB ✅ Codex Live turn WebSocket decoded 20.9 KiB 20.9 KiB 0 B (0.0%) 29.3 KiB ✅ Codex Live turn messages 2 2 0 (0.0%) 8 ✅ Claude Total thread wire 5.0 KiB 5.0 KiB −24 B (−0.5%) 6.8 KiB ✅ Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB −24 B (−2.0%) 2.0 KiB ✅ Claude Live turn WebSocket decoded 21.2 KiB 21.2 KiB −41 B (−0.2%) 29.3 KiB ✅ Claude Live turn messages 2 1 −1 (−50.0%) 8 ✅ Baseline:
173a970· PR result:8986708· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
- Codex decoded thread snapshot: 108.5 KiB
- Claude decoded thread snapshot: 108.8 KiB
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.
Note
🤖 Claude Opus 5.5 responding on behalf of Theo
Tested on macOS 26.5.1 (Apple Silicon, internal SSD). The fix works. It also brings back main-thread stalls on a busy disk. I measured the stalls; I did not see them in the app.
What works
vp test run src/persistence/Sqlite.test.ts scripts/t3-sqlite-state.test.ts: 10/10 pass on macOS.- Syscall trace. I used a
DYLD_INSERT_LIBRARIESshim on an unsigned copy of Node 24.19. The shim logs everyfsync()andfcntl(F_FULLFSYNC). I ran the realSqlitePersistence.layerFromPathon a file DB with 3000 single-row commits:- Normal commits: 0 syncs (
synchronous = NORMALworks as intended). - Checkpoints: every sync is
F_FULLFSYNConstatev2.sqliteandstatev2.sqlite-wal. Plainfsync()was not used.
- Normal commits: 0 syncs (
- Bundled SQLite: Node 24.19 has 3.53.3 and Electron 44.4.5 has 3.53.4. Neither has
SQLITE_NO_SYNC, and both read the pragma back as1. So desktop andnpx t3both get the barrier.
Cost
The writer
DatabaseSyncruns on the main thread (nodeSqliteClient.ts:443). Auto-checkpoints run inside the commit that crosses 1000 pages. So eachF_FULLFSYNCblocks the event loop.Each WAL cycle now has 2 slow commits, not 1. The first-frame WAL header sync after a reset also uses the checkpoint sync flags.
Benchmark: 20k commits of 1.5 KB with the server pragmas, raw
node:sqlite:Disk Setting Slow commits Median slow commit Worst commit Idle (3 runs) OFF 40 1.6–2.5 ms 5.3–5.5 ms Idle (3 runs) ON 80 6.0–7.0 ms 13.4–15.4 ms Busy (6 runs) OFF 40–52 6.5–26 ms 98 ms – 1.3 s Busy (6 runs) ON 80–84 9.4–25 ms 59 ms – 3.8 s "Busy" means a second process rewrote a 256 MiB file with
fsyncin a loop. This is a lot of load, but builds,git checkoutand installs do similar work. Busy worst cases with ON were 3.7 s, 2.5 s, 3.8 s, 1.4 s, 79 ms and 59 ms. With OFF, only one run went past 315 ms.F_FULLFSYNCflushes the whole drive cache, so it waits for other processes' writes. That is the "slow disk drops clients" case that #19205 fixed. It now happens about once per 4 MB of WAL, not on every commit.Opinion
The durability fix is correct. On an idle disk the cost is small. My concern is the busy-disk tail. Two ways forward:
- Merge as is and accept rare multi-second stalls under heavy I/O.
- Move the barrier off the main thread. Set
wal_autocheckpoint = 0on the writer. Then runPRAGMA wal_checkpoint(PASSIVE)withcheckpoint_fullfsync = ONfrom a worker connection, on a timer or WAL-size trigger. This also removes the plain-fsync checkpoint stall the writer has on main today.
Option 2 is a bigger change. It could be a follow-up if you want this durability fix in now.
Not tested: real power loss.
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Approvability#
Verdict: Not approved
Macroscope's review found this PR not approvable — This scoped durability fix changes the SQLite persistence default by enabling full filesystem barriers for checkpoints in both the server and direct state-tool connections. The behavior is tested and the impact is clear, but the product-level default change can affect macOS checkpoint performance and warrants human assessment.
No code changes detected at
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).8986708. Prior analysis still applies.