Skip to content

fix(state): heal state.db before reading settings - #1902

Merged
frahlg merged 1 commit into
masterfrom
f2-heal-state-db-before-load
Oct 27, 2026
Merged

frahlg merged 1 commit into
masterfrom
f2-heal-state-db-before-load

Conversation

@frahlg

@frahlg frahlg commented Oct 27, 2026

Copy link
Copy Markdown
Member

Item F2 of the stable plan.

Problem

Boot reads settings from state.db (config.Load, main.go:412) before state.Open (main.go:507), and only state.Open runs the snapshot heal. Damage to the first 4 KiB of state.db makes Load fail with read stored settings: file is not a database (26), Core exits, and systemd restarts it into the same failure every 5 s. A good state.db.snapshot sits next to it unused.

The verified-good marker makes it worse. state.db.clean survives crashes, and openChecked skips the check while it exists. After a power loss on a box that booted before, even state.Open alone would not heal.

A second path: if Load reads revision R from a damaged but readable file and state.Open then restores a snapshot at revision R-1, InitializeStorage exits with "database recovery lost current settings". The next boot then runs R-1 anyway, and no longer reports the heal.

Change

  • state.HealBeforeLoad(path) reads the settings row. Only on a SQLite corrupt (11) or not-a-database (26) error, and only when the snapshot passes quick_check, it removes the marker and runs openChecked, which restores the snapshot. A missing, busy or readable file, or one without a usable snapshot, is left alone, so a healthy boot does no extra checks and the owner's damaged file is never swapped for an empty one before Load.
  • config.HealSettingsDatabase(seed) finds the database the seed names and calls it. main.go calls it before config.Load and adds the event to the store, so it shows on /api/health.
  • InitializeStorage accepts an older settings revision only when this boot restored state.db from its snapshot. It runs the snapshot's settings and adds a heal event that names both revisions. Every other mismatch still exits.
  • Store.HealEvents and the new RecordHealEvent take healMu.

Tests

  • TestCorruptStateDBWithGoodSnapshotHealsBeforeLoad is the audit repro (caa-133), renamed. It fails on master in both subtests (marker present and marker removed) and passes here.
  • TestOlderSnapshotSettingsRunAndAreReported fails with "database recovery lost current settings" without the InitializeStorage change and passes with it.
  • TestHealthyStateDBIsNotHealedBeforeLoad, TestHealBeforeLoadWithoutSnapshotChangesNothing, TestHealBeforeLoadIgnoresMissingDatabase.
  • go test -race -count=1 ./internal/state/... ./internal/config/... ./cmd/ftw/... ./internal/api/...: ok.
  • go vet ./... and go build ./...: ok.

Risk

Medium. The new code runs only after a settings read has failed with SQLite corruption, a case where Core exited before. The snapshot check and restore then run before the API port binds. The snapshot leaves out bulky history tables, so the check is short. A restore can bring back settings older than the last save; /api/health now says so on the boot that does it. If the API port changed between the two revisions, the box keeps the newer port until the next restart.

🤖 Generated with Claude Code

Boot read settings from state.db (config.Load) before state.Open ran
the snapshot heal, so a damaged state.db stopped every start. Boot now
restores state.db from a usable snapshot first when the settings read
fails with SQLite corruption, even if the verified-good marker is
present. Without a usable snapshot nothing changes. When state.Open
restores an older snapshot after Load, the snapshot's settings run and
/api/health reports the step back instead of Core exiting.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T13:45:10.061340Z e4a90dc PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@frahlg
frahlg merged commit 2be6210 into master Oct 27, 2026
23 checks passed
@frahlg
frahlg deleted the f2-heal-state-db-before-load branch November 11, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant