Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): make SQLite checkpoints durable on macOS #19304

Mergedt3dotgg(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/19304/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHub

Since #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 to synchronous = 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 read PRAGMA checkpoint_fullfsync back as 1. With only the two pragma lines removed, exactly those two tests fail and read back 0.
  • vp lint and vp fmt --check on the changed files pass.
  • Not verified on macOS hardware. The tests confirm the setting is applied. They do not exercise F_FULLFSYNC or 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

    2. juliusmarminge added labelsmacroscope-review

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      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 8986708. Prior analysis still applies.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire5.0 KiB5.0 KiB+7 B (+0.1%)6.8 KiB✅
      CodexThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      CodexLive turn WebSocket wire1.2 KiB1.2 KiB+7 B (+0.6%)2.0 KiB✅
      CodexLive turn WebSocket decoded20.9 KiB20.9 KiB0 B (0.0%)29.3 KiB✅
      CodexLive turn messages220 (0.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB−24 B (−0.5%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.2 KiB1.2 KiB−24 B (−2.0%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.2 KiB21.2 KiB−41 B (−0.2%)29.3 KiB✅
      ClaudeLive turn messages21−1 (−50.0%)8✅

      Baseline: 173a970 · PR result: 8986708 · Source CI: success

      Scenario 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.

    5. 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_LIBRARIES shim on an unsigned copy of Node 24.19. The shim logs every fsync() and fcntl(F_FULLFSYNC). I ran the real SqlitePersistence.layerFromPath on a file DB with 3000 single-row commits:
        • Normal commits: 0 syncs (synchronous = NORMAL works as intended).
        • Checkpoints: every sync is F_FULLFSYNC on statev2.sqlite and statev2.sqlite-wal. Plain fsync() was not used.
      • 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 as 1. So desktop and npx t3 both get the barrier.

      Cost

      The writer DatabaseSync runs on the main thread (nodeSqliteClient.ts:443). Auto-checkpoints run inside the commit that crosses 1000 pages. So each F_FULLFSYNC blocks 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:

      DiskSettingSlow commitsMedian slow commitWorst commit
      Idle (3 runs)OFF401.6–2.5 ms5.3–5.5 ms
      Idle (3 runs)ON806.0–7.0 ms13.4–15.4 ms
      Busy (6 runs)OFF40–526.5–26 ms98 ms – 1.3 s
      Busy (6 runs)ON80–849.4–25 ms59 ms – 3.8 s

      "Busy" means a second process rewrote a 256 MiB file with fsync in a loop. This is a lot of load, but builds, git checkout and 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_FULLFSYNC flushes 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:

      1. Merge as is and accept rare multi-second stalls under heavy I/O.
      2. Move the barrier off the main thread. Set wal_autocheckpoint = 0 on the writer. Then run PRAGMA wal_checkpoint(PASSIVE) with checkpoint_fullfsync = ON from 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.

Sign in to join this conversation.