perf: WAL synchronous=NORMAL — stop fsyncing every commit

lyra.db is on disk-backed ext4, where each WAL commit fsync'd (~0.15s here). Every
chat turn does several writes (remember user+assistant, summaries, poker logging),
so this was adding real per-turn latency, and made the dream loop + tests crawl.
synchronous=NORMAL is WAL's recommended companion: durable across app crashes, only
a power/OS crash can drop the last txn (never corrupts). Per-write dropped from
~0.4s to ~0.001s; test suite 72s -> 24s.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-21 05:12:13 +00:00
parent 67cf51a53f
commit cca8322ee2
+5
View File
@@ -131,6 +131,11 @@ def _connection() -> sqlite3.Connection:
# alongside the web server without tripping "database is locked".
_conn.execute("PRAGMA busy_timeout=5000")
_conn.execute("PRAGMA journal_mode=WAL")
# WAL's recommended companion: don't fsync on every commit (only at
# checkpoint). Safe against app crashes; a power/OS crash can lose the last
# txn but never corrupt. On disk-backed storage this turns ~0.15s-per-commit
# fsync latency into ~nothing — big win for per-turn writes + the dream loop.
_conn.execute("PRAGMA synchronous=NORMAL")
_conn.executescript(SCHEMA)
# Migrations for DBs created before a column existed (no-op if present).
for ddl in ("ALTER TABLE sessions ADD COLUMN mode TEXT",):