Skip to content

fix(sqlite): validate state before writable setup - #104952

Merged
vincentkoc merged 1 commit into
mainfrom
fix/sqlite-runtime-state-integrity
Jul 12, 2026
Merged

vincentkoc merged 1 commit into
mainfrom
fix/sqlite-runtime-state-integrity

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Jul 12, 2026 •

Copy link
Copy Markdown
Member

What Problem This Solves

Shared-state initialization configured SQLite journaling and schema state before proving that table/index contents were internally consistent. A stale secondary index could therefore survive PRAGMA quick_check and reach writable runtime, doctor repair, or startup checkpoint setup.

This also needs to preserve SQLite crash recovery: a read-only preflight cannot recover a hot rollback journal after an interrupted writer.

Refs #71689

Why This Change Was Made

  • Open the state database writable so SQLite can perform required hot-journal or WAL recovery.
  • Run full quick_check plus integrity_check immediately after recovery and before any OpenClaw-owned pragma, schema, repair, or checkpoint mutation.
  • Apply the same ordering to runtime open, doctor schema repair, and startup migration checkpoint setup.
  • Add destructive stale-index coverage showing quick_check=ok while integrity_check catches missing index rows.
  • Add a killed-writer rollback-journal probe proving the committed row is restored before integrity validation.

User Impact

OpenClaw now refuses to initialize or repair a shared state database whose table/index state is inconsistent, without blocking normal SQLite crash recovery. Corruption evidence is preserved for operator recovery instead of being followed by further application writes.

Evidence

  • Testbox-through-Crabbox tbx_01kxa82b1e9x5aa41yq01exk84 (quick-crab): 170/170 focused state, migration, and doctor tests passed on the patch-identical pre-final-rebase head.
  • The destructive hot rollback-journal recovery probe passed on Linux in 347 ms.
  • Existing eight-process fresh initialization and additive schema-upgrade races remained green.
  • Fresh autoreview: no accepted/actionable findings, correctness confidence 0.86.
  • OPENCLAW_TESTBOX=1 scripts/pr prepare-run 104952 passed hosted gates for PR head fa09521458ce8db10ba03da2acd3b42105cfa845.
  • pnpm check:changed scoped the patch to core, coreTests; all patch-specific checks passed. The repository-wide Plugin SDK API baseline check currently reports hash drift independent of these two state files.

@vincentkoc vincentkoc self-assigned this Jul 12, 2026
@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Jul 12, 2026
@vincentkoc

Copy link
Copy Markdown
Member Author

Land-ready verification for fa09521458ce8db10ba03da2acd3b42105cfa845.

Behavior proof:

  • Before: the destructive fixture returns quick_check=ok while integrity_check reports rows missing from a stale secondary index.
  • After: runtime open, doctor schema repair, and startup checkpoint setup all stop before OpenClaw-owned mutation.
  • Crash recovery: a killed writer leaves a hot rollback journal; reopening restores the committed value, removes the journal, and passes integrity_check.

Validation:

  • corepack pnpm test:serial src/state/openclaw-state-db.test.ts src/infra/state-migrations.test.ts src/commands/doctor-state-migrations.test.ts
    • 170/170 passed on patch-identical Testbox-through-Crabbox tbx_01kxa82b1e9x5aa41yq01exk84 (quick-crab).
    • Includes eight-process fresh initialization and additive schema-upgrade races.
  • Fresh autoreview: no accepted/actionable findings, correctness confidence 0.86.
  • OPENCLAW_TESTBOX=1 scripts/pr prepare-run 104952
    • hosted gates passed for exact PR head fa09521458ce8db10ba03da2acd3b42105cfa845.

Known gap:

  • The POSIX SIGKILL crash fixture is skipped on native Windows. Platform-neutral corruption paths are covered; broader Windows/macOS/Linux/ARM stress remains in the continuing SQLite hardening track.

The local changed gate scoped the patch to core, coreTests; its only failure was repository-wide Plugin SDK baseline hash drift unrelated to these files. The repo-native hosted prepare gate passed.

@vincentkoc
vincentkoc marked this pull request as ready for review July 12, 2026 04:13
@vincentkoc
vincentkoc merged commit 46aa7ae into main Jul 12, 2026
113 of 115 checks passed
@vincentkoc
vincentkoc deleted the fix/sqlite-runtime-state-integrity branch July 12, 2026 04:13
@vincentkoc

Copy link
Copy Markdown
Member Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant