Skip to content

fix(sqlite): keep read-only state journal-aware - #113404

Merged
vincentkoc merged 4 commits into
mainfrom
fix/sqlite-crash-recovery-stress-20260724
Jul 25, 2026
Merged

vincentkoc merged 4 commits into
mainfrom
fix/sqlite-crash-recovery-stress-20260724

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

Related: #113306

What Problem This Solves

Fixes read-only state diagnostics that could bypass SQLite journal handling with immutable=1, require writes beside a sidecar-free WAL database, or inspect a mixed database/sidecar generation while a writer reset WAL or replaced the source file.

Why This Change Was Made

Read-only state access now resolves SQLite's canonical filename and prepares a private, single-file snapshot before validation. Rollback journals and complete active WAL families use SQLite locking plus online backup under a pinned read transaction. Incomplete WAL families are copied with stable source and sidecar fingerprints so crash residue can be inspected without creating source coordination files.

The private snapshot is normalized to rollback journaling, checked for integrity and canonical current-schema indexes, and cleaned on close with retry support for transient cleanup failures.

Private staging-directory setup is isolated in a dependency-light module so state diagnostics do not pull snapshot transforms, process logging, or plugin/runtime initialization into their import graph.

User Impact

Doctor and update-planning diagnostics get a consistent point-in-time view that includes committed WAL state, preserves source bytes and directory entries, rejects unsafe indexes, and does not race source replacement or journal-mode transitions. Writable startup remains the sole owner of recovery and maintenance.

Evidence

  • Exact head: 7d82a818d12d271d2469172ae178f507cb360fbe.
  • Focused Blacksmith Testbox proof: 229 passed; 6 expected platform-specific skips on Linux (tbx_01kyb9q5fe6kaswcmwc56masd7).
  • Exact-head pnpm deadcode:exports: production, full-tree, and script scans passed with zero findings.
  • Exact-head pnpm check:changed: formatting, API/schema/database guards, core/core-test typecheck, import cycles, database-first guards, and the full core lint sweep passed in 17m08s.
  • Exact-head GitHub CI passed 58 jobs, including build artifacts, bundled metadata, SQLite flip proof, QA profiles, and native Windows checks-windows-node-test.
  • Fresh structured autoreview on the complete patch and final lint-only delta reported no accepted or actionable findings.
  • Behavioral coverage includes hot rollback recovery, 16 MiB active-WAL I/O, atomic MEMORY-journal pairs, rollback-to-WAL transitions, same-size WAL reset, canonical symlink lookup, missing WAL/SHM pairs, source replacement, cleanup retry, unsafe indexes, and source-byte/directory-entry preservation.
  • Exact-head CI run: https://github.com/openclaw/openclaw/actions/runs/30135620488

@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Jul 24, 2026
@vincentkoc vincentkoc self-assigned this Jul 24, 2026
@vincentkoc
vincentkoc marked this pull request as ready for review July 24, 2026 20:45
@vincentkoc
vincentkoc force-pushed the fix/sqlite-crash-recovery-stress-20260724 branch from aca5322 to 046065e Compare July 24, 2026 20:47
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P1 High-priority user-facing bug, regression, or broken workflow. labels Jul 24, 2026
@clawsweeper

clawsweeper Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 24, 2026, 4:51 PM ET / 20:51 UTC.

ClawSweeper review

What this changes

The PR makes existing OpenClaw state-database read-only access respect SQLite locking and journals, then verifies quarantine, integrity, and canonical schema state before diagnostics read the database.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

Keep this active maintainer-authored PR open for normal merge review. The branch addresses a concrete state-integrity failure mode and includes targeted crash-recovery evidence; it is neither superseded by current main nor eligible for automated cleanup. Before landing, reviewers should confirm all read-only diagnostic callers surface the new verification failures appropriately and let the in-progress CI finish.

Priority: P1
Reviewed head: 046065ee04a69fc63fc11dbcefc30a55272b33f1

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The PR supplies a focused crash-recovery scenario and bounded implementation, while final confidence depends on caller-path review and the in-progress exact-head checks.
Proof confidence 🐚 platinum hermit (4/6) Not applicable: This is a maintainer-authored PR; its body nevertheless provides concrete after-fix crash-recovery and Testbox evidence for reviewer inspection.
Patch quality 🦐 gold shrimp (3/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This is a maintainer-authored PR; its body nevertheless provides concrete after-fix crash-recovery and Testbox evidence for reviewer inspection.
Evidence reviewed 5 items PR intent and real crash scenario: The PR body describes a killed writer leaving 256 dirty pages, where an explicit immutable read sees uncommitted rows while the proposed read-only path fails closed and writable reopening recovers the committed state.
Read-only open removes immutable URI policy: The branch deletes the helper that converted ordinary state paths to immutable SQLite URIs, avoiding immutable mode’s disabled locking and journal detection behavior.
State verification is moved into read-only access: The proposed read-only database function adds terminal-failure, quarantine, integrity/maintenance, and supported-schema checks before returning a diagnostic database handle.
Findings None None.
Security None None.

How this fits together

OpenClaw state diagnostics open the shared SQLite state database to inspect and repair runtime state. This path feeds doctor and maintenance reporting, so it must not treat pages from an interrupted writer as committed state or silently accept quarantined, corrupt, or noncanonical databases.

flowchart LR
  A[State database files] --> B[Read-only diagnostics]
  B --> C[Quarantine check]
  C --> D[SQLite journal-aware open]
  D --> E[Integrity and schema validation]
  E --> F[Trusted diagnostic result]
  D --> G[Hot journal or invalid state]
  G --> H[Fail closed]
  H --> I[Writable recovery path]
Loading

Before merge

  • Resolve merge risk (P1) - The new fail-closed verification path can change how doctor or other read-only diagnostics report an interrupted, quarantined, corrupt, or noncanonical database; confirm every caller presents the typed failure without masking recovery guidance.
  • Resolve merge risk (P1) - The PR’s CI is still running, including SQLite-specific and Windows checks, so merge readiness remains unconfirmed for the exact head.
  • Complete next step (P2) - This active maintainer-owned PR needs normal source-path review and completion of its in-progress CI, not an automated repair or cleanup action.
Agent review details

Security

None.

PR surface

Source -1, Tests +102. Total +101 across 5 files.

View PR surface stats
Area Files Added Removed Net
Source 2 26 27 -1
Tests 3 142 40 +102
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 168 67 +101

Review metrics

None.

Stored data model

Persistent data-model change detected: serialized state: src/state/openclaw-database-paths.windows.test.ts, serialized state: src/state/openclaw-state-db.test.ts, serialized state: src/state/openclaw-state-db.ts, unknown-data-model-change: src/state/openclaw-state-db.test.ts, unknown-data-model-change: src/state/openclaw-state-db.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Retain journal-aware SQLite opens, preserve the explicit fail-closed diagnostic behavior, and land only after caller-level error handling and the targeted cross-platform checks confirm that writable recovery remains the supported remediation.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Retain journal-aware SQLite opens, preserve the explicit fail-closed diagnostic behavior, and land only after caller-level error handling and the targeted cross-platform checks confirm that writable recovery remains the supported remediation.

Do we have a high-confidence way to reproduce the issue?

Yes in source, with medium confidence: the added crash fixture creates a hot rollback journal after a killed writer and contrasts immutable reads with the proposed journal-aware read-only open. The supplied PR evidence reports that scenario ran successfully, but this review could not independently execute it.

Is this the best way to solve the issue?

Yes, provisionally: removing immutable mode at the shared read-only state boundary is narrower and safer than attempting to detect every journal sidecar manually. The remaining merge check is that all diagnostic callers correctly expose the resulting verification failures and recovery path.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 2ee8730450dc.

Labels

Label changes:

  • add P1: This repair protects operators from diagnostics accepting uncommitted SQLite state after a killed writer, a high-impact state-integrity failure mode.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🦐 gold shrimp.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This is a maintainer-authored PR; its body nevertheless provides concrete after-fix crash-recovery and Testbox evidence for reviewer inspection.

Label justifications:

  • P1: This repair protects operators from diagnostics accepting uncommitted SQLite state after a killed writer, a high-impact state-integrity failure mode.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🦐 gold shrimp.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This is a maintainer-authored PR; its body nevertheless provides concrete after-fix crash-recovery and Testbox evidence for reviewer inspection.

Evidence

What I checked:

  • PR intent and real crash scenario: The PR body describes a killed writer leaving 256 dirty pages, where an explicit immutable read sees uncommitted rows while the proposed read-only path fails closed and writable reopening recovers the committed state. (src/state/openclaw-state-db.test.ts:537, 046065ee04a6)
  • Read-only open removes immutable URI policy: The branch deletes the helper that converted ordinary state paths to immutable SQLite URIs, avoiding immutable mode’s disabled locking and journal detection behavior. (src/infra/node-sqlite.ts:24, 046065ee04a6)
  • State verification is moved into read-only access: The proposed read-only database function adds terminal-failure, quarantine, integrity/maintenance, and supported-schema checks before returning a diagnostic database handle. (src/state/openclaw-state-db.ts:296, 046065ee04a6)
  • Related report is adjacent rather than a duplicate: The linked report concerns snapshot create/restore durability and pathname-identity cleanup, whereas this PR changes the separate shared-state diagnostic open path. (src/state/openclaw-state-db.ts:296, 046065ee04a6)
  • Current review state: The PR is an active, mergeable maintainer PR created on July 24, 2026; several broad checks remain in progress, so there is no stale-branch or cleanup basis. (046065ee04a6)

Likely related people:

  • vincentkoc: Authored the linked SQLite durability report and this focused state-database repair, indicating current ownership context; historical ownership requires the normal maintainer source-history pass before landing. (role: current state-database contributor; confidence: low; commits: 046065ee04a6; files: src/state/openclaw-state-db.ts, src/state/openclaw-state-db.test.ts, src/infra/node-sqlite.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Confirm each caller of read-only state access renders verification failures and writable-recovery guidance correctly.
  • Wait for the SQLite-specific, Windows, build, lint, and typecheck lanes to complete for the current head.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@vincentkoc
vincentkoc force-pushed the fix/sqlite-crash-recovery-stress-20260724 branch 3 times, most recently from 8c407cd to 722c791 Compare July 24, 2026 23:14
@vincentkoc
vincentkoc force-pushed the fix/sqlite-crash-recovery-stress-20260724 branch from 722c791 to 4050253 Compare July 24, 2026 23:21
@vincentkoc
vincentkoc force-pushed the fix/sqlite-crash-recovery-stress-20260724 branch from 2eae9f1 to 7d82a81 Compare July 25, 2026 00:09
@vincentkoc

Copy link
Copy Markdown
Member Author

Land-ready proof completed on exact head 7d82a818d12d271d2469172ae178f507cb360fbe.

Validation:

  • Blacksmith Testbox tbx_01kyb9q5fe6kaswcmwc56masd7
  • corepack pnpm test:serial src/infra/sqlite-snapshot.test.ts src/infra/sqlite-readonly-location.test.ts src/snapshot/local-repository.test.ts src/state/openclaw-state-db.test.ts src/state/openclaw-database-paths.windows.test.ts src/claws/doctor.test.ts src/claws/update-plan.test.ts test/scripts/sqlite-sessions-transcripts-flip-proof.e2e.test.ts: 229 passed, 6 expected platform-specific skips on Linux
  • corepack pnpm deadcode:exports: zero unused exports in production, full-tree, and script scans
  • corepack pnpm check:changed: passed in 17m08s, including formatting, core/core-test typecheck, full core lint, import cycles, schema/API guards, and database-first storage guards
  • GitHub CI: 58 jobs passed, including build artifacts, bundled metadata, SQLite flip proof, QA profiles, and native Windows checks-windows-node-test
  • Fresh structured autoreview: complete patch clean; final lint-only delta clean
  • scripts/pr review-validate-artifacts 113404: passed with zero findings
  • OPENCLAW_TESTBOX=1 scripts/pr prepare-run 113404: passed

Exact-head CI: https://github.com/openclaw/openclaw/actions/runs/30135620488

Covered behavior includes hot rollback recovery, active WAL reads, same-size WAL reset, rollback-to-WAL transition, source replacement, missing WAL/SHM pairs, unsafe indexes, retryable cleanup, source-byte preservation, and the end-to-end SQLite sessions/transcripts flip.

No touched-surface proof gaps remain. Direct WSL2, macOS arm64, Docker multi-arch, Linux-variant, sandbox, vacuum/restore crash-point, and high-I/O fault-injection proof continues under #113306 after this bounded fix lands.

@vincentkoc
vincentkoc merged commit ec7f8e9 into main Jul 25, 2026
124 checks passed
@vincentkoc
vincentkoc deleted the fix/sqlite-crash-recovery-stress-20260724 branch July 25, 2026 00:33
@vincentkoc

Copy link
Copy Markdown
Member Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 25, 2026
* fix(sqlite): keep read-only state journal-aware

* fix(sqlite): snapshot read-only state safely

* fix(sqlite): retry read snapshot replacement races

* fix(sqlite): isolate private staging directory setup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes maintainer Maintainer-authored PR P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: XL status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant