Skip to content

fix(plugin-state): treat missing plugin-state table as empty on read-only fresh stores - #117257

Merged
steipete merged 3 commits into
mainfrom
fix/freshboot-plugin-state
Aug 1, 2026
Merged

steipete merged 3 commits into
mainfrom
fix/freshboot-plugin-state

Conversation

@steipete

@steipete steipete commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes #117249. Booting the gateway against a fresh, empty OPENCLAW_STATE_DIR exits code 7 — "startup migrations did not complete cleanly; refusing to report the gateway ready" — because legacy-state migration detection errors on a store that simply has no data yet. The startup lease system creates state/openclaw.sqlite first (only schema_meta + state_leases), then detection reads plugin state through a read-only connection that cannot run the lazy table ensure: SELECT fails with no such table: plugin_state_entries, wrapped as PluginStateStoreError, surfacing as "Failed detecting …" warnings for the Matrix/Reef/Workboard detectors, which block ready. First-run out-of-box path broken; the workaround was a manual openclaw doctor --fix.

Why This Change Was Made

The read-only helper already returns undefined when the DB file is missing — the benign "no legacy state" answer. This fix extends the same semantics to the missing-table case at one narrow point (withPluginStateDatabaseReadOnly): a matcher requiring ERR_SQLITE_ERROR and the exact message no such table: plugin_state_entries returns undefined; every other SQLite failure still throws wrapped. No table creation from read paths, no schema-version change, no startup reordering — this is the additive lazy-ensure storage model working as designed: reads before the first write mean an empty store.

User Impact

A brand-new install reaches gateway-ready without a doctor step. No behavior change for populated stores; genuine corruption still fails loudly.

Evidence

  • Live before/after on real builds: before (main-based integration build, 2026-07-31): exit 7 with seven "Failed detecting … PluginStateStoreError" warnings on a fresh state dir. After (same build + this fix): fresh state dir, no doctor → readyz=200 in 7s, {"ready":true,"failing":[]}, zero detection warnings, detectors complete ("Recorded Matrix inbound dedupe migration completion (0 SQLite roots, 0 JSON roots scanned)"), 13 plugins loaded in 1.1s.
  • New regression tests: (a) plugin-state lookup/list/key-range/count against a DB file containing only unrelated tables return empty instead of throwing; (b) boundary-level: real startup-lease bootstrap followed by migration detection yields zero warnings (src/infra/state-migrations.test.ts).
  • Focused suites: plugin-state 76/76 (4 files); state-migrations detection tests 75/75.
  • Pre-existing, unrelated on main: 14 macOS-local failures in state-migrations.workspace-setup-sandbox.test.ts (/var vs /private/var canonicalization) — reproduced identically on a checkout without this change; Linux CI is authoritative there. Follow-up filed separately.
  • Structured autoreview (Codex, gpt-5.6-sol, high): clean, no actionable findings.
  • Matched error shape verified against the real driver: code: ERR_SQLITE_ERROR, message: no such table: plugin_state_entries.

🤖 Generated with Claude Code

Review refinement (accepted finding)

The empty-state fallback is now gated on the genuine lease-bootstrap shape: a read-only schema discriminator (SELECT 1 FROM sqlite_schema WHERE type='table' AND name NOT IN ('schema_meta','state_leases') LIMIT 1, run on the already-open handle) returns empty only for true fresh checkpoints; any store initialized beyond the checkpoint keeps failing loudly (PLUGIN_STATE_READ_FAILED) so a damaged canonical schema can never masquerade as empty. New corruption-case test covers exactly that; fresh-case fixture now uses the real startup-checkpoint bootstrap. Suites: fresh-store 2/2, plugin-state 77/77, migration regression 75/75; autoreview clean.

…only fresh stores

Startup leases create state/openclaw.sqlite before the first plugin-state
write, so legacy-state migration detection read the file through a read-only
connection and failed on the missing plugin_state_entries table, blocking
fresh-state-dir gateway boots from reaching ready.

Fixes #117249
@openclaw-barnacle openclaw-barnacle Bot added size: S maintainer Maintainer-authored PR labels Aug 1, 2026
@steipete
steipete marked this pull request as ready for review August 1, 2026 06:10
@steipete steipete closed this Aug 1, 2026
@steipete steipete reopened this Aug 1, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. labels Aug 1, 2026
@clawsweeper

clawsweeper Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 1, 2026, 2:44 AM ET / 06:44 UTC.

ClawSweeper review

What this changes

The PR makes read-only plugin-state queries report empty results for a fresh startup-checkpoint SQLite database that has not yet created its lazy plugin-state table, while retaining errors for initialized damaged stores.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

The latest head addresses the prior corruption-boundary finding: it returns empty state only when the database has the startup checkpoint’s two tables and keeps a missing plugin-state table visible as an error once any other state table exists. No additional actionable patch defect was found; this remains the viable fix candidate for the linked open bug and needs normal exact-head CI plus maintainer merge review.

Priority: P0
Reviewed head: 625dd5bba50bffe25b237b5029e0230c9a1cdd77

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Strong live proof and focused regression coverage support a narrow repair; the earlier missing-corruption guard is now addressed on the current head.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The PR body provides a concrete real gateway before/after run on a fresh state directory, including the original exit-7 failure and the repaired readyz=200 result with zero migration warnings.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR body provides a concrete real gateway before/after run on a fresh state directory, including the original exit-7 failure and the repaired readyz=200 result with zero migration warnings.
Evidence reviewed 7 items Narrow fresh-checkpoint fallback: The proposed helper catches only the exact SQLite missing-table error, then returns empty state only when no tables exist beyond schema_meta and state_leases; otherwise it rethrows the original failure.
Checkpoint schema contract: The state owner creates schema_meta and state_leases plus indexes for the startup checkpoint, matching the discriminator’s intended minimal table shape.
Fresh and damaged-store regression coverage: The new focused tests assert empty lookup/list/range/count results for the genuine checkpoint and PLUGIN_STATE_READ_FAILED for a database that has another initialized state table but lacks the canonical plugin-state table.
Findings None None.
Security None None.

How this fits together

Gateway startup first creates a minimal shared SQLite checkpoint database for migration leasing, then runs legacy-state detectors that read plugin state. Those detector outcomes determine whether startup migrations complete and whether the gateway reports ready.

flowchart LR
  A[Fresh state directory] --> B[Startup migration lease]
  B --> C[Checkpoint SQLite database]
  C --> D[Read-only legacy-state detectors]
  D --> E{Plugin-state table missing?}
  E -->|Checkpoint tables only| F[Report empty legacy state]
  E -->|Other state tables exist| G[Return storage error]
  F --> H[Gateway can become ready]
  G --> I[Migration warning blocks ready]
Loading

Before merge

  • Resolve merge risk (P2) - The fallback is availability-sensitive: if the startup checkpoint later gains another benign table without updating the discriminator and its regression test, a fresh store could retain the original readiness failure.
  • Complete next step (P2) - No repair lane is needed: the prior concrete finding is resolved, and the remaining work is ordinary exact-head CI and protected maintainer merge review.
Agent review details

Security

None.

PR surface

Source +29, Tests +159. Total +188 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 30 1 +29
Tests 2 159 0 +159
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 189 1 +188

Review metrics

None.

Stored data model

Persistent data-model change detected: migration/backfill/repair: src/infra/state-migrations.test.ts, serialized state: src/infra/state-migrations.test.ts, unknown-data-model-change: src/infra/state-migrations.test.ts. Migration or upgrade compatibility proof is recorded; maintainers should verify it before merge.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #117249
Summary: This PR is the focused candidate fix for the linked fresh-state gateway-readiness bug.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge-risk options

Maintainer options:

  1. Accept the constrained startup fallback (recommended)
    Merge after normal exact-head validation, retaining the two-table discriminator and corruption regression so genuine initialized-store damage remains visible.
  2. Pause if the checkpoint contract changes
    If startup checkpoint initialization is expanded, update the discriminator and its fresh-store proof before accepting this fallback.

Technical review

Best possible solution:

Keep the two-table checkpoint discriminator and its paired fresh-store and damaged-schema tests, then merge only after the exact current head has completed normal CI and maintainer review.

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

Yes, with high confidence from source: the startup lease creates the two-table checkpoint before migration detection, while read-only plugin-state queries require the lazy table. The supplied live main-based before/after run corroborates that path, although this read-only review did not execute it.

Is this the best way to solve the issue?

Yes. The refined PR fixes the owner boundary at the read-only plugin-state helper, does not create schema from a read path, and explicitly preserves failure behavior for initialized stores missing their canonical table.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 78486e27511c.

Labels

Label justifications:

  • P0: The repaired path is a default first-run gateway startup that can otherwise refuse readiness until the operator runs Doctor manually.
  • merge-risk: 🚨 availability: The patch changes how a SQLite error is handled during gateway startup, where an overbroad fallback could conceal damaged state and affect readiness.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body provides a concrete real gateway before/after run on a fresh state directory, including the original exit-7 failure and the repaired readyz=200 result with zero migration warnings.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body provides a concrete real gateway before/after run on a fresh state directory, including the original exit-7 failure and the repaired readyz=200 result with zero migration warnings.

Evidence

What I checked:

Likely related people:

  • steipete: Git blame attributes the current-main read-only plugin-state helper to Peter Steinberger, and the supplied PR commits show the same author refining the fresh-versus-damaged schema boundary. (role: original helper author and recent area contributor; confidence: high; commits: d8ba00f8a262, 625dd5bba50b; files: src/plugin-state/plugin-state-store.sqlite.ts, src/infra/startup-migration-checkpoint.ts)

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.

History

Review history (2 earlier review cycles)
  • reviewed 2026-08-01T06:17:25.609Z sha 3827d99 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-01T06:24:37.203Z sha 1e1d800 :: found issues before merge. :: [P2] Restrict the empty-state fallback to fresh checkpoint stores

@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. and removed rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 1, 2026
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 1, 2026
@steipete
steipete merged commit 02419ed into main Aug 1, 2026
130 of 139 checks passed
@steipete
steipete deleted the fix/freshboot-plugin-state branch August 1, 2026 07:04
@steipete

steipete commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 2, 2026
…only fresh stores (openclaw#117257)

* fix(plugin-state): treat missing plugin-state table as empty on read-only fresh stores

Startup leases create state/openclaw.sqlite before the first plugin-state
write, so legacy-state migration detection read the file through a read-only
connection and failed on the missing plugin_state_entries table, blocking
fresh-state-dir gateway boots from reaching ready.

Fixes openclaw#117249

* test(plugin-state): split fresh-store read coverage into focused file

* fix(plugin-state): distinguish fresh stores from damaged schemas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S 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.

[Bug]: Fresh state dir gateway boot refuses ready — legacy-state detection errors on missing plugin_state table

1 participant