Repository navigation
fix(state): doctor --fix loops on "migration required" for older state databases - #124282
Conversation
…e databases Databases at user_version < 3 cannot complete the STRICT table rebuild when they predate a first-use additive column such as device_bootstrap_tokens.setup_id. The rebuild validates each table against its full canonical column set, so it fails with: SQLite table device_bootstrap_tokens does not match its canonical columns (missing setup_id) The repair runs in a single transaction, so this rolls back the audit-events-v2 work that already succeeded. The next open re-detects the legacy audit ledger and reports that migration instead, which names a table unrelated to the real failure and leaves doctor --fix repeating the same message. It also early returns out of runLegacyStateMigrations, skipping the config machine-state migration, so the gateway keeps failing config validation. STATE_PERSISTENT_SCHEMA_COMPATIBILITY.allowedMissingColumns already treats these columns as legitimately absent, so the persistent schema contract and the STRICT rebuild disagreed about the same database. Ensure the first-use additive columns immediately before the STRICT rebuild in both paths that run it, gated on previousVersion < the STRICT schema version. This mirrors repairLegacyGatewayRestartHandoffsForStrictMigration directly above it. It is not a semantic change: the rebuild recreates each table from canonical SQL that already declares these columns, so the resulting shape is identical either way, and scoping it to pre-STRICT databases keeps the absent-until-first-use behaviour for every normal open. Co-Authored-By: Claude Opus 5 <[email protected]>
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Codex review: needs maintainer review before merge. Reviewed August 15, 2026, 6:45 PM ET / 22:45 UTC. ClawSweeper reviewWhat this changesThe PR adds the three deferred shared-state columns before pre-v3 SQLite STRICT rebuilds and adds regression coverage for Doctor repair and gateway startup. Regression provenancePossible regression — suspected (reviewed change). No predecessor PR is attributed. Merge readinessThe PR remains necessary: current main still invokes the generic STRICT rebuild without first materializing the three state-owned first-use columns it permits to be absent. The provided patch places that preparation at the state-schema boundary in both Doctor and writable-startup paths; no concrete correctness defect was found, but exact-head CI should finish before merge. Priority: P1 Review scores
Verification
How this fits togetherThe shared-state database is opened by both Doctor repair and gateway startup. Those paths prepare legacy schema data, rebuild older tables as SQLite STRICT, and then provide the canonical state store to the running gateway. flowchart LR
A[Older shared-state database] --> B[Doctor repair or gateway startup]
B --> C[State schema preparation]
C --> D[STRICT table rebuild]
D --> E[Canonical shared-state database]
E --> F[Gateway runs]
Before merge
Agent review detailsSecurityNone. PR surfaceSource +29, Tests +202. Total +231 across 3 files. View PR surface stats
Review metrics
Stored data modelPersistent data-model change detected: Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep first-use deferral for normal current-schema opens, but materialize the three verified nullable columns immediately before both pre-v3 STRICT rebuild paths and merge once exact-head CI is green. Do we have a high-confidence way to reproduce the issue? Yes—source inspection gives a high-confidence reproduction shape: a pre-v3 database missing one allowed first-use column reaches the generic exact-column STRICT check through either migration path. Is this the best way to solve the issue? Yes—the supplied repair keeps generic STRICT behavior strict and reconciles the exception in the shared-state owner immediately before the only rebuilds that require canonical columns. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 5374c110fb99. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (3 earlier review cycles) |
…olumns Adds independent regression cases for the two routes the shared helper covers but the original fixture did not exercise directly: - session_groups.cwd / .worktree, the other first-use additive columns, so the coverage is not specific to device_bootstrap_tokens.setup_id - the writable cold-open path, which upgrades the same database during gateway startup without doctor running first Both fail on an unpatched tree with the canonical column mismatch and pass with the fix, alongside the existing doctor-repair case. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Thanks for the review — addressed the coverage gap called out under "Resolve merge risk (P1)". The original fixture only exercised the doctor repair route with
Verified these are genuine regression coverage rather than tests that merely pass: with the two source files reverted to With the fix restored, all three pass, and the wider suite stays green: The test file also now builds its legacy fixture through a small helper that strips a given column set from any canonical table, so adding future first-use columns to the coverage is a one-line change. Note the branch is one commit behind |
Materializing these columns before the STRICT rebuild is only safe while every one of them is bare and nullable: the rebuild adds them to tables that already hold rows, so a NOT NULL, defaulted, constrained, or key-bearing column would fail the migration or silently rewrite existing data. That property was documented as a convention on the definition list. Assert it against the canonical schema instead, one case per first-use column, so adding a non-nullable column to the list fails CI rather than pre-STRICT upgrades. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Thanks — addressed the remaining P1 risk directly.
Agreed, and that was only a convention documented in a comment on the definition list. Pushed This matters precisely because of the mechanism you flagged: the pre-STRICT rebuild materializes these columns on tables that already hold rows, so a non-nullable or constrained addition would either fail the migration or silently rewrite existing data. Adding such a column to Current declarations, all bare nullable:
Confirmed green on Suite on this branch: Branch is still one commit behind |
|
Following up as promised on the exact-head CI gate. Required checks are now green on No mechanical work remains on my side — the earlier bare-nullable risk is now enforced by One note for whoever lands it: the branch is still one commit behind |
|
Merged via squash.
|
…e databases (openclaw#124282) * fix(state): doctor --fix loops on "migration required" for older state databases Databases at user_version < 3 cannot complete the STRICT table rebuild when they predate a first-use additive column such as device_bootstrap_tokens.setup_id. The rebuild validates each table against its full canonical column set, so it fails with: SQLite table device_bootstrap_tokens does not match its canonical columns (missing setup_id) The repair runs in a single transaction, so this rolls back the audit-events-v2 work that already succeeded. The next open re-detects the legacy audit ledger and reports that migration instead, which names a table unrelated to the real failure and leaves doctor --fix repeating the same message. It also early returns out of runLegacyStateMigrations, skipping the config machine-state migration, so the gateway keeps failing config validation. STATE_PERSISTENT_SCHEMA_COMPATIBILITY.allowedMissingColumns already treats these columns as legitimately absent, so the persistent schema contract and the STRICT rebuild disagreed about the same database. Ensure the first-use additive columns immediately before the STRICT rebuild in both paths that run it, gated on previousVersion < the STRICT schema version. This mirrors repairLegacyGatewayRestartHandoffsForStrictMigration directly above it. It is not a semantic change: the rebuild recreates each table from canonical SQL that already declares these columns, so the resulting shape is identical either way, and scoping it to pre-STRICT databases keeps the absent-until-first-use behaviour for every normal open. Co-Authored-By: Claude Opus 5 <[email protected]> * test(state): cover the cold-open route and session_groups first-use columns Adds independent regression cases for the two routes the shared helper covers but the original fixture did not exercise directly: - session_groups.cwd / .worktree, the other first-use additive columns, so the coverage is not specific to device_bootstrap_tokens.setup_id - the writable cold-open path, which upgrades the same database during gateway startup without doctor running first Both fail on an unpatched tree with the canonical column mismatch and pass with the fix, alongside the existing doctor-repair case. Co-Authored-By: Claude Opus 5 <[email protected]> * test(state): enforce that first-use additive columns stay bare nullable Materializing these columns before the STRICT rebuild is only safe while every one of them is bare and nullable: the rebuild adds them to tables that already hold rows, so a NOT NULL, defaulted, constrained, or key-bearing column would fail the migration or silently rewrite existing data. That property was documented as a convention on the definition list. Assert it against the canonical schema instead, one case per first-use column, so adding a non-nullable column to the list fails CI rather than pre-STRICT upgrades. Co-Authored-By: Claude Opus 5 <[email protected]> --------- Co-authored-by: Claude Opus 5 <[email protected]>
What Problem This Solves
Fixes an issue where operators upgrading an older install would hit an unbreakable
openclaw doctor --fixloop and a gateway that never starts, when their shared state database predates a first-use additive column.openclaw doctor --fixreports:That is the command that produced the message.
--force,--non-interactive,openclaw update --channel beta, and reinstalling all fail the same way in about 8 seconds. The gateway stays in astatus=78/CONFIGrestart loop.The reported migration is not the failing one. The real failure is:
Why This Change Was Made
A database at
user_version < 3must run the STRICT table rebuild. That rebuild validates every canonical table against its full canonical column set, butdevice_bootstrap_tokens.setup_id(and nowsession_groups.cwd/.worktree) are intentionally excluded from the startup additive columns and deferred to first use, so they are absent on older databases.Because the repair runs in a single transaction, that failure rolls back the
audit-events-v2work that had already succeeded. The next open re-detects the legacy audit ledger and reports that migration instead, which is why the surfaced error names an unrelated table and never converges.It also has a second effect:
runLegacyStateMigrationsearly-returns when the state schema step warns, which skipsmigrateLegacyConfigMachineState. Retired config keys are therefore never moved intoconfig_machine_state,openclaw.jsonis never rewritten, and the gateway keeps failing config validation — so the visible symptom looks like a config problem while the state error is logged fail-open at boot.STATE_PERSISTENT_SCHEMA_COMPATIBILITY.allowedMissingColumnsalready declares these columns legitimately absent, so the persistent schema contract and the STRICT rebuild disagreed about the same database.The fix ensures the first-use additive columns immediately before the STRICT rebuild, in both paths that run it, gated on
previousVersion < OPENCLAW_STATE_STRICT_SCHEMA_VERSION. This mirrorsrepairLegacyGatewayRestartHandoffsForStrictMigrationdirectly above it, added for the same class of bug (#110237).This is not a semantic change: the rebuild recreates each table from canonical SQL that already declares these columns, so the resulting shape is identical either way. Ensuring them first only stops the rebuild refusing to start, and scoping to pre-STRICT databases preserves absent-until-first-use for every normal open.
Non-goal:
src/infra/sqlite-strict.tsis deliberately untouched. It is generic infra shared with agent databases, and its exact-canonical-columns contract is the safer default. The first-use deferral is state-schema policy, so the state layer reconciles it before invoking the generic migrator. Happy to move it there instead if maintainers prefer.User Impact
Operators whose state database predates
setup_idcan upgrade normally:openclaw doctor --fixcompletes, the config migration that was being skipped now runs, and the gateway starts. Previously the only way through was to add the column manually withALTER TABLE. No behaviour change for databases already at the current schema version.Evidence
New regression test
src/state/openclaw-state-db-first-use-column-strict-migration.test.tsreproduces the exact production error onmain:and passes with the fix.
Focused suites:
Wider run across the areas this touches (
src/state/,src/infra/sqlite-strict.test.ts,src/cli/plugins-cli.list.test.ts,src/skills/workshop/,src/flows/doctor-core-checks.test.ts) → 972 passed, 0 failed.Replayed against a real affected database (75 MB,
user_version = 1, 55,381audit_events, nosetup_id), which previously required a manualALTER TABLEto get through. With this change and no manual intervention:Post-migration:
user_version1 → 8, all 55,381 audit rows preserved,PRAGMA integrity_check→ok, 123 STRICT tables,device_bootstrap_tokens.setup_idpresent.oxfmt --checkandoxlintpass on all changed files. Full-repotsc --noEmitwas not run locally (it OOMs on the 15 GB machine used); relying on CI for that.CHANGELOG.mdintentionally untouched per CONTRIBUTING.Related: #110186 and #110237 are the same family (STRICT migration blocked on real-world databases), but distinct root causes.