Skip to content

fix(sqlite): reject orphaned database state before mutation - #105222

Merged
vincentkoc merged 3 commits into
mainfrom
fix/sqlite-foreign-key-integrity
Jul 12, 2026
Merged

vincentkoc merged 3 commits into
mainfrom
fix/sqlite-foreign-key-integrity

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Jul 12, 2026 •

Copy link
Copy Markdown
Member

Related: #101290

What Problem This Solves

Fixes an issue where users opening, backing up, verifying, or vacuuming an OpenClaw SQLite database could have orphaned foreign-key rows accepted as healthy because SQLite's quick_check and integrity_check do not validate foreign keys.

Why This Change Was Made

The shared fail-closed integrity gate now also runs foreign_key_check before any writable initialization, checkpoint, VACUUM, VACUUM INTO, or canonical backup verification. Diagnostics are deterministic and bounded, preserve 64-bit rowids, and identify the child table, row, parent table, and foreign-key id without changing the existing doctor report contract.

This intentionally detects and rejects inconsistent state; it does not guess at an automatic repair for orphaned user data.

User Impact

Corrupt databases with orphaned references are rejected before OpenClaw mutates or snapshots them. Valid databases behave as before. Operators receive a specific error that identifies the violated relationship instead of a misleading healthy result.

AI-assisted: yes. The implementation and tests were reviewed with the repository autoreview workflow.

Evidence

  • Blacksmith Testbox through Crabbox tbx_01kxasf4b71wbmwahctjrbrxn6, Actions run https://github.com/openclaw/openclaw/actions/runs/29187032505: 166 focused tests passed across the shared integrity helper, pragma-shadow bypass, shared-state open/repair/startup, per-agent open/schema setup, backup creation, backup verification, and doctor compaction.
  • Patch-identical Testbox tbx_01kxarrf09rdv1qczgbq92be9d: tsgo:core, tsgo:core:test, and targeted oxlint passed.
  • Structured autoreview: clean, 0.96 confidence, no accepted or actionable findings.
  • pnpm check:changed reaches formatting and repository guards, then is blocked by the existing docs/.generated/plugin-sdk-api-baseline.sha256 drift. The same baseline check was reproduced on a pristine origin/main worktree; this PR does not touch plugin SDK surfaces.

@vincentkoc vincentkoc self-assigned this Jul 12, 2026
@openclaw-barnacle openclaw-barnacle Bot added commands Command implementations size: M maintainer Maintainer-authored PR labels Jul 12, 2026
@vincentkoc
vincentkoc marked this pull request as ready for review July 12, 2026 09:14
@openclaw-barnacle openclaw-barnacle Bot added the docs Improvements or additions to documentation label Jul 12, 2026
@vincentkoc

Copy link
Copy Markdown
Member Author

Land-ready verification for 122ca28f6ff827311cb5a3f8abee6d97acadc3b0.

Behavior proof:

  • Reproduced that SQLite returns ok from both quick_check and integrity_check for orphaned rows.
  • The shared gate now rejects those rows with direct PRAGMA foreign_key_check before writable setup, checkpointing, VACUUM, VACUUM INTO, archive publication, or canonical backup acceptance.
  • Added coverage proving a real table named pragma_foreign_key_check cannot shadow or bypass validation.
  • Diagnostics preserve 64-bit rowids, handle WITHOUT ROWID, sort deterministically, and remain bounded.

Validation:

  • Blacksmith Testbox through Crabbox tbx_01kxasf4b71wbmwahctjrbrxn6: 166 focused tests passed across integrity, shared/agent startup, backup create/verify, and doctor compaction.
  • The same Testbox passed pnpm tsgo:core, pnpm tsgo:core:test, and targeted oxlint for all changed TypeScript files.
  • Exact-head GitHub CI run https://github.com/openclaw/openclaw/actions/runs/29187415285 completed with no pending or failed checks.
  • Structured autoreview completed clean after the pragma-shadow fix and Node >=22.19 compatibility validation.
  • scripts/pr review-validate-artifacts 105222 passed with zero findings.
  • OPENCLAW_TESTBOX=1 scripts/pr prepare-run 105222 passed using hosted exact-head proof.

Known proof gap: this narrow pragma hardening was not rerun on native Windows or ARM. The broader cross-platform snapshot/restore/high-I/O matrix remains follow-up work; no platform-specific SQLite API or filesystem behavior is introduced here.

@vincentkoc
vincentkoc merged commit 1592d42 into main Jul 12, 2026
115 of 121 checks passed
@vincentkoc
vincentkoc deleted the fix/sqlite-foreign-key-integrity branch July 12, 2026 09:31
@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

commands Command implementations docs Improvements or additions to documentation maintainer Maintainer-authored PR size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant