Skip to content

fix(doctor): doctor --fix fails on PostgreSQL stores whose auth profiles need migration - #189587

Closed
ceckert wants to merge 1 commit into
openclaw:mainfrom
ceckert:fix/doctor-auth-profile-pg-backup
Closed

ceckert wants to merge 1 commit into
openclaw:mainfrom
ceckert:fix/doctor-auth-profile-pg-backup

Conversation

@ceckert

@ceckert ceckert commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes: openclaw doctor --fix always exits 1 with "SQLite database cannot be snapshotted safely: … Auth profile backup differs from the planned source; retry Doctor." when a PostgreSQL-backed state or agent store holds auth profiles that Doctor needs to migrate, such as a legacy claude-cli: alias.

User Impact

User impact: Doctor can migrate stored auth profiles on PostgreSQL installations, saving a verified PostgreSQL backup first, instead of failing on every run. Retrying never helps today, so any service or container that runs Doctor before startup cannot start.

Why This Change Was Made

Before rewriting stored auth profiles, Doctor saves a verified backup and checks that it holds exactly the rows it planned from. Both checks assumed SQLite. On PostgreSQL the .sqlite file at that path is only the store's anchor: planning reads the rows from PostgreSQL, but the backup copied the anchor file, which holds no auth rows, so the check could never pass.

For PostgreSQL owners, the backup now uses the same exact capture as Doctor's pre-migration PostgreSQL backups (<db>.auth-profile-migration-<id>.postgres.bak). The planned rows are checked against the live store between two fingerprints of it, and Doctor refuses if anything changed during the capture. SQLite owners keep the existing snapshot and check. The step moves into its own module, so doctor-auth-flat-profiles.ts gets shorter.

Evidence

Shipped CLI against disposable PostgreSQL 16 with synthetic credentials. The state store is on PostgreSQL. After an initializing doctor --fix, the shared auth store row is set to a legacy claude-cli:work API-key profile, the shape an older release leaves behind, and config references that profile. Then openclaw doctor --fix --non-interactive runs.

main (OpenClaw 2026.10.9 (4a95b4e)) this branch (OpenClaw 2026.10.9 (b11c4d6))
doctor --fix exit 1 0
Doctor output SQLite database cannot be snapshotted safely: <root>/state/state/openclaw.sqlite. Auth profile backup differs from the planned source; retry Doctor. Saved auth profile migration backup: … and Migrated stored auth profile aliases in …
Stored profile afterwards unchanged: claude-cli:work anthropic:work, same synthetic key
Backup none <root>/state/state/openclaw.sqlite.auth-profile-migration-<id>.postgres.bak: anchor.sqlite, database.sqlite, manifest.json, still holding the claude-cli:work row

The same failure took down a production gateway: every pre-start Doctor exited 1 on this step.

  • New src/commands/doctor/auth-alias-migration.postgres.test.ts covers both the shared state store and an agent store on real PostgreSQL. Each migrates the alias and leaves a completed backup that still holds the pre-migration row. If the rows change mid-capture, Doctor refuses and leaves the store unmigrated. The migrate cases failed on main with the exact error above.
  • The existing SQLite suites pass: auth-alias-migration, auth-alias-preservation, doctor-auth-flat-profiles and doctor-health-contributions.
  • pnpm check:changed passes, except the dead-export scan, which reports src/tts/directive-facts.ts on main already.

Doctor's auth-profile migration backup copied the SQLite anchor file and
checked it for the planned auth rows, but PostgreSQL owners keep those rows in
PostgreSQL, so every `doctor --fix` failed with "Auth profile backup differs
from the planned source". PostgreSQL owners now get the exact PostgreSQL
capture Doctor uses before schema migrations, with the planned rows checked
against the live store between two fingerprints.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clawsweeper

clawsweeper Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added commands Command implementations size: M labels Oct 31, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 31, 2026
@clawsweeper

clawsweeper Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

Doctor backs up PostgreSQL auth-profile stores through the PostgreSQL backup owner before migrating their credentials.

Example: A PostgreSQL auth store contains the legacy profile claude-cli:work.

  • Before: doctor --fix exits 1 because its SQLite-anchor backup lacks the planned auth rows.
  • After: Doctor saves an exact PostgreSQL backup and migrates the profile to anthropic:work with its credential intact.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Useful, focused repair with recorded CLI behavior and meaningful PostgreSQL regressions; the required existing-state upgrade evidence remains incomplete.
Proof confidence 🦐 gold shrimp (3/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Doctor repeatedly refuses to migrate legacy auth profiles on PostgreSQL, potentially preventing a service from starting.
Reason: PostgreSQL is an owned supported storage surface, and retrying cannot repair a backup taken from the wrong storage engine.

Merge readiness

⛔ Blocked before merge - 2 items remain

This PR addresses a current PostgreSQL Doctor failure that can block service startup, while preserving the existing migration safeguards.

Priority: P1
Reviewed head: b11c4d656c291afc91abaef2ead9ad4f0e3d54db

Before merge

  • Add real behavior proof - The PR records head b11c4d6 running the shipped Doctor CLI against PostgreSQL 16, migrating claude-cli:work to anthropic:work while retaining its credential and original backup row; compatibility proof still needs a v2026.10.9 SQLite-to-candidate upgrade with settings and data intact, followed by PostgreSQL adoption and the affected repair, with sensitive values redacted. After adding proof, update the PR body, then request a new review with @clawsweeper re-review. The PR author or someone with repository write access can request it.
  • Add data-model compatibility proof - The review found that existing stored data may not work after upgrade. Show that existing data still loads and works with this change.

Findings

None.

Tests

  • Missing end-to-end proof: The supplied CLI comparison seeds a current-build store; it does not show an upgrade from v2026.10.9 with existing settings and data preserved.
Agent review details

How this fits together

Doctor reads shared and per-agent credential stores, plans normalization, verifies a pre-migration backup, and commits through the existing auth-store transaction owners.

flowchart TD
  A[Doctor CLI] --> B[Auth migration plan]
  B --> C[Engine selection]
  C --> D[SQLite snapshot]
  C --> E[Exact PostgreSQL capture]
  D --> F[Source and owner revalidation]
  E --> F
  F --> G[Existing auth migration transactions]
Loading

Technical review

Best possible solution:

Retain the engine-aware backup repair under the existing Doctor migration and backup owners.

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

Current-main source establishes the anchor-versus-PostgreSQL mismatch, and the contributor records matching CLI failure and recovery; no target code was executed during this read-only review.

Is this the best way to solve the issue?

The patch fixes the backup boundary rather than bypassing verification, preserves SQLite behavior, and leaves credential normalization, receipts, ownership checks, and writes with their existing owners.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against c18dd0e81f40.

Provenance checked

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

PR surface

Source +92, Tests +126. Total +218 across 3 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 2 124 32 +92
Tests 1 126 0 +126
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 250 32 +218

Labels

Label changes:

  • add P1: The reported PostgreSQL migration failure makes every pre-start Doctor invocation fail and can prevent an existing Gateway service from starting.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Label justifications:

  • P1: The reported PostgreSQL migration failure makes every pre-start Doctor invocation fail and can prevent an existing Gateway service from starting.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Rank-up moves

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

  • Provide the stable-to-candidate upgrade evidence described in the compatibility assessment.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 31, 2026, 1:38 PM ET / 17:38 UTC.

@roboclaw-bot

Copy link
Copy Markdown
Collaborator

Thanks, @ceckert, for the report, initial fix, and PostgreSQL reproduction. I confirmed the SQLite-anchor mismatch still exists on main and carried the fix into #189639, with backend selection and captured-row verification handled by the database backup owner. The replacement also registers the PostgreSQL regressions in conformance CI and retains your co-author credit alongside steipete's.

Closing this PR in favor of that replacement; its CI and landing are being tracked there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands Command implementations P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: M status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants