Cap the WAL file beside Litestream: journal_size_limit, quiet gaps, checkpoint settings (follow-up to #91) #118
Labels
No labels
bug
enhancement
epic
needs-info
needs-triage
ready-for-agent
ready-for-human
svd:go
svd:hold
wontfix
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
dev/mail-mind-mcp#118
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Follow-up to #91, which names the cause with evidence. This issue holds the fix. The options are open until triage.
Problem
With Litestream 0.3.13 beside the sync worker, the WAL grows at the write rate for as long as the writer never pauses for one second, and the file never shrinks below its high-water mark until it passes 2 GB. Reproduced: 800 MB in 90 s of 100-row commits; 8.6 MB for the same writer without Litestream. Live: 0.99 GB beside a 2.08 GB database during a backfill (#91), 60 MB high-water after the 2026-11-01 restart with the embedder as the only writer.
Why (short form, details on #91)
wal_checkpoint(TRUNCATE)returns busy while Litestream runs (31 s in the reproduction).Candidate fixes (decide at triage; they combine)
PRAGMA journal_size_limiton the sync worker's connection, for example 64 MiB. SQLite truncates the WAL file to that limit after any checkpoint that resets it, including Litestream's RESTART. This caps the file without touching the checkpoint cadence. Cheapest, reversible, one line plus a test.checkpoint-interval,min-checkpoint-page-count,max-checkpoint-page-countare settable per db in 0.3.13. Lowermax-checkpoint-page-countmakes RESTART attempts start earlier but does not fix the busy result.truncate-page-countis not exposed in 0.3.13 config. Record what is and is not tunable in the deploy comments.database is lockederrors but does not stop a backfill from starving RESTART.Acceptance
Out of scope
Filed under: operator word "implement #91" sha256:4cf5e63dc126
Decided at triage 2026-11-02. Tested first:
PRAGMA journal_size_limiton the sync connection does not shrink the WAL beside Litestream 0.3.13. In the reproduction a RESTART landed, the writer committed 20 more times, and the file stayed at 261 MB. Litestream's bookkeeping insert right after its checkpoint is the first write that restarts the log, from a connection with no limit. Option 1 of the body is withdrawn. The routing rule 3 proposal was overruled by the operator: the change is a sync-worker pragma call and one config knob, and the ruling is this label.Agent Brief
Category: bug
Summary: Bound the WAL beside Litestream: truncate at sync-worker start, and give Litestream a quiet gap on request
Current behavior:
Litestream holds a permanent read transaction, so only its own checkpoints reset the WAL. Its RESTART checkpoint runs every second with a fixed 1 s busy timeout and returns busy while a writer is active. A backfill commits every 100 to 200 ms, so the WAL grows at the write rate until the writer pauses (800 MB in 90 s in the reproduction; 0.99 GB live). After a RESTART the file is reused but never shrunk; Litestream truncates only above 500,000 pages. An app
wal_checkpoint(TRUNCATE)returns busy while Litestream runs (31 s in the reproduction) and completes in 9 ms when it does not.Desired behavior:
PRAGMA wal_checkpoint(TRUNCATE)once with a short busy timeout of its own (about 2 s), logs the three result numbers and whether it was busy, and continues either way. At pod start the sync worker holds the database before Litestream takes its read lock (the live log shows Litestream waiting 65 s), so this shrinks a WAL left over from the previous run.[sync] checkpoint_gap_seconds(0 disables; 2 to 60) andcheckpoint_gap_every(batches, 1 to 1000, default 20). When on, after everycheckpoint_gap_everybackfill batches, and after every embed batch and attachment-embed batch, the worker holds no transaction and issues no write forcheckpoint_gap_seconds. The gap must exceed Litestream's monitor interval plus its busy timeout, so the minimum is 2.mail_statusshows the WAL file size in bytes beside the database size, so the cap is observable without exec. The health endpoint is unchanged.journal_size_limitdoes not apply beside Litestream 0.3.13;truncate-page-countis not configurable in 0.3.13;max-checkpoint-page-countdoes not cure the busy result; the quiet gap is the lever.Key interfaces:
Store.__init__writer path: the startup checkpoint, after the schema migration and before the first write.SyncConfig: the two new fields with bounds and defaults;mailmind checkprints them.SyncWorkerbackfill loop andEmbedWorkerbatch loop: the gap hook, one shared helper so both sleep the same way and honour the stop event.Store.statusand themail_statusrenderer: WAL size field. The status counts issue (#110) is separate; the new field is oneos.stat.Acceptance criteria:
checkpoint_gap_seconds=2, a test that drives 60 backfill batches through the worker observes a pause of at least 2 s with no open transaction after each 20.checkpoint_gap_seconds=2, checkpoint_gap_every=20ends with a WAL high-water under one fifth of the no-gap run on the same machine, recorded in the PR.mail_statusoutput includes the WAL size; the evidence tool and health endpoint are unchanged.make checkpasses.Out of scope:
journal_mode.⏸ SVD architect gate: dev/imap-mailmind-mcp#118 cap-the-wal-file-beside-litestream-journal-size-limit-quiet
Status: CLEAN after 0 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity
substrate· dispatch efforthighRun:
20261101T230708Z-408d2345Held because: CLEAN but 5 open risk(s) (3 verification · 2 decision) — HOLD-ALL parks everything. The risks, verbatim:
Open risks:
Change: 1. Startup truncate. Add Store._startup_checkpoint, called in the writer branch of Store.init after the meta upsert (store.py:517-523) and before self._actions_conn = self._conn (store.py:524). It sets busy_timeout to 2000, runs PRAGMA wal_checkpoint(TRUNCATE), and restores busy_timeout 30000 in finally (the _connect value, store.py:2242). It logs 'startup checkpoint' with extra busy, wal_frames and checkpointed, treats an OperationalError as busy, and never raises. Add those three keys to the _JsonFormatter allowlist (cli.py:41-58).
2. Shared helper in store.py. Store.quiet(seconds, stop) holds self._lock with no transaction open and calls stop.wait(seconds). Holding the lock makes the gap cover the whole sync process. QuietGap(store, seconds, every) has a stop event, a counter guarded by a lock, after_batch() (pauses on every Nth batch) and pause() (pauses every time).
3. Config. SyncConfig gains checkpoint_gap_seconds (float, default 0, ge=0, le=60, with a field_validator rejecting values between 0 and 2, mirroring ServerConfig._path_shape at config.py:64-69) and checkpoint_gap_every (int, default 20, 1 to 1000).
4. Wiring. SyncWorker.init builds self.gap and passes gap= to open_source (keyword-only, forwarded to AccountSync and JmapSource as a dataclass field after blobs) and to EmbedWorker.from_config. run_forever sets self.gap.stop = stop before any thread starts.
5. Hooks. IMAP _backfill, after the 'backfill batch' log (ingest.py:234-237): if gap.after_batch(): break. JMAP _add (jmap.py:620-621): call after_batch() without break, because _backfill, _changes and _reconcile write EMAIL_STATE after _add returns. EmbedWorker._account: if gap.pause(): break, just before the existing pause at embed.py:244; this one site covers both message and attachment batches.
6. Status. Store.status adds wal_bytes from a single stat of the -wal file, mirroring db_bytes (store.py:2115,2125). mail_status line 682 becomes 'db: wal_bytes=N'.
7. cmd_check prints 'sync: checkpoint_gap_seconds=... checkpoint_gap_every=...'.
8. Comment blocks go above the backup service in compose.yaml:56 and deploy/k8s/base/deployment.yaml:157: journal_size_limit does not apply; truncate-page-count is not configurable in 0.3.13; max-checkpoint-page-count does not cure the busy result; the gap is the lever. config.example.toml [sync] gets the two keys, commented out.
Files:
src/imap_mailmind_mcp/store.py·src/imap_mailmind_mcp/config.py·src/imap_mailmind_mcp/source.py·src/imap_mailmind_mcp/ingest.py·src/imap_mailmind_mcp/jmap.py·src/imap_mailmind_mcp/embed.py·src/imap_mailmind_mcp/server.py·src/imap_mailmind_mcp/cli.py·compose.yaml·deploy/k8s/base/deployment.yaml·config.example.toml·tests/test_store.py·tests/test_ingest.py·tests/test_config.py·tests/test_embed.py·tests/test_server.pyDecision: accept the 5 open risk(s) above and dispatch this spec (
svd:go), or park (svd:skip) / re-spec (svd:respec)?Your call (when this issue is held at the gate via
svd:hold):svd:go— approve; Half-B dispatch accepts exactly this spec (open risks accepted)svd:firebesidesvd:go— approve AND authorise the Half-B fire from the phone; never on its ownsvd:skip— park it; dispatch refuses it and the issue stays for a humansvd:respec— discard this spec; the next Half-A run re-produces it with this record as prior-attempt contextSpec:
/Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-cap-the-wal-file-beside-litestream-journal-size-limit-quiet.md— next:codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-cap-the-wal-file-beside-litestream-journal-size-limit-quiet.mdspec-verify-dispatch Half B for dev/imap-mailmind-mcp#118 cap-the-wal-file-beside-litestream-journal-size-limit-quiet completed integration verify.
7b693feaab7b693fe: no Forgejo commit statuses (absent). Local: 794 passed, 8 skipped; make check green.7b693fehas no commit statuses on git.groupthink.asia; CI files are added by the architect (no pipeline run in history) — reported as absent, not blocking (gh#633)codex-svd ship --run-id 20261101T230708Z-408d2345 --confirm-merge)