Skip to content

fix(doctor): migrate legacy agent databases and bound registry writes to the active state dir - #120906

Merged
steipete merged 5 commits into
mainfrom
fix/doctor-agent-db-migration
Aug 9, 2026
Merged

steipete merged 5 commits into
mainfrom
fix/doctor-agent-db-migration

Conversation

@steipete

@steipete steipete commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Four coupled defects around agent-database migration, all reproduced live against a copy of a real state dir:

  1. Legacy runtime journals brick migration. Agent DBs created during the briefly-landed-then-reverted f91de52 window (May 2026) carry acp_parent_stream_events (and trajectory_runtime_events) in a pre-session_id, non-STRICT shape. The canonical schema re-landed at v12 with session_id and no ALTER path, so the previousVersion < 11 STRICT rebuild in ensureAgentSchema fails assertMatchingColumns ("missing session_id") and the whole doctor migration rolls back — the DB is stuck at v9 forever, and the gateway refuses to start.
  2. Registry-only discovery vs disk-scanning readers. The media-persistence migration iterated only agent_databases registry rows, while doctor's session steps scan the agents tree on disk. An on-disk DB with no registry row (state dir copied or restored) is never migrated — and can never self-register because openOpenClawAgentDatabase asserts the media version before registering. Doctor's later steps then crash on the unmigrated DB.
  3. Registry absolute paths escape the active state dir. With OPENCLAW_STATE_DIR pointing at a copy, the copied registry rows still name the ORIGINAL ~/.openclaw/agents/... paths, and the migration followed them — opening and (absent defect 1) writing schema migrations into a foreign live state dir. Verified live: a sandboxed doctor --fix attempted exactly that against the real home state dir.
  4. One failing doctor step aborts the whole run with circular advice. runDoctorHealthContributions had no per-step isolation, so the crash from defect 2 killed doctor entirely — with the error text "run openclaw doctor --fix", printed by doctor --fix itself.

Why This Change Was Made

  • ensureAgentSchema gains a structure-gated drop for the reverted-lineage runtime journals (both are derived runtime state; drop-and-recreate is the sanctioned shape, precedent dropLegacyMemoryIndexSchema).
  • Migration target discovery becomes a single resolver (state-migrations.media-persistence-targets.ts) that unions configured session-store targets, registry rows, and disk-discovered default-layout DBs, deduplicated by filesystem identity with explicit owner authority: configuration → recorded registry fact → directory-name inference. Filesystem operations use original paths (no lexical .. collapse across symlinks); realpath is used only for identity/boundary checks.
  • Registry rows resolving outside the active state dir that are not configured stores are skipped, unregistered, and warned about — a doctor run can no longer write into another state dir. Custom out-of-tree session.store paths (a documented contract) still migrate, now even without a registry row.
  • Doctor health contributions are individually isolated: a throwing step degrades to a named warning and the remaining steps run.

No schema-version bump; no runtime steady-state behavior changes; runtime asserts stay.

User Impact

Users upgrading DBs that crossed the reverted May window, and anyone who copies/restores/moves a state dir (backup restore, machine migration, sandboxing), get a working openclaw doctor --fix instead of a permanently stuck gateway with circular advice. Doctor can no longer mutate a different installation's databases.

Evidence

Live before/after on a copy of a real state dir (v9 main agent DB with legacy acp_parent_stream_events, 21 registry rows pointing at the original ~/.openclaw):

  • Before: doctor --fix exits 1 with OpenClawAgentDatabaseMediaMigrationRequiredError ... run openclaw doctor --fix, after attempting migration of the ORIGINAL state dir's DBs (blocked only by defect 1).
  • After: exit 0; main migrates v9 → v16 with canonical acp_parent_stream_events (session_id, STRICT); all 21 foreign registry rows skipped + unregistered with warnings; original ~/.openclaw agent DB mtimes byte-identical before/after (sorted stat comparison).

Tests: node scripts/run-vitest.mjs src/infra/state-migrations (503 passed / 2 skipped incl. new coverage for legacy-journal rebuild, unregistered disk discovery, foreign-row skip with untouched bytes, configured-store discovery without registry rows, symlinked .. path identity, owner-authority precedence, doctor step isolation), node scripts/run-vitest.mjs src/state/openclaw-agent-db (111 passed / 4 skipped), doctor-health suite (107 passed).

@openclaw-barnacle openclaw-barnacle Bot added size: L maintainer Maintainer-authored PR labels Aug 9, 2026
@steipete
steipete marked this pull request as ready for review August 9, 2026 05:12
@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. P1 High-priority user-facing bug, regression, or broken workflow. labels Aug 9, 2026
@steipete steipete self-assigned this Aug 9, 2026
@steipete
steipete merged commit 4c2d8d8 into main Aug 9, 2026
187 checks passed
@steipete
steipete deleted the fix/doctor-agent-db-migration branch August 9, 2026 06:34
@steipete

steipete commented Aug 9, 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 10, 2026
… to the active state dir (openclaw#120906)

* fix(doctor): migrate legacy agent databases discovered on disk and bound registry writes to the active state dir

* fix(doctor): discover configured agent databases without registry rows

* fix(doctor): preserve filesystem and configured database identity

* fix(doctor): prioritize configured agent database identity

* fix(doctor): prefer recorded agent database ownership
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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: L status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant