Skip to content

feat: let doctor --fix tell when its startup repairs have nothing to do - #206246

Merged
fuller-stack-dev merged 8 commits into
openclaw:mainfrom
fuller-stack-dev:doctor-pending-step4
Nov 27, 2026
Merged

fuller-stack-dev merged 8 commits into
openclaw:mainfrom
fuller-stack-dev:doctor-pending-step4

Conversation

@fuller-stack-dev

@fuller-stack-dev fuller-stack-dev commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Related: #204775

What Problem This Solves

Every openclaw doctor --fix stops a healthy Gateway before it knows whether any step has work. The plan in #204775 makes that stop conditional, but only once every step can say "nothing to do" and mean it. This is step 4: the startup steps inside the stop (state database repair, database migrations, runtime checks) still reported the default work, so a later conditional stop could never skip them.

User Impact

None yet. Every --fix still stops the Gateway and runs every step. With OPENCLAW_GATEWAY_STARTUP_TRACE=1, these steps now report their real decision:

Step Healthy install (4 GiB agent database) With real work
state-database-repair pending=none check=owner, 7 to 10 ms pending=work (mode 0644, orphan delivery row), and the repair still runs
runtime-checks pending=none check=owner, 17 to 50 ms pending=work (unusable deletion journal row), and the quarantine still runs
database-migrations pending=undecidable check=owner, under 1 ms (see below) pending=work (pending agent schema migration), and the migration still runs
config-flow unchanged, pending=work check=default unchanged

Why This Change Was Made

Each check lives with its step's owner and calls the predicates the step acts on, so the trace and the write cannot disagree. Contract for every check: no writable open, no file creation, no database copy, no lock. A check that throws counts as undecidable, which counts as work.

  • State database repair (state/openclaw-state-db-repair-pending.ts). The existing needsOpenClawStateDatabaseSchemaRepair answers a different question (schema version and legacy shapes, for the backup and state-schema steps) and copies the database under an updater's capture. The new check opens the live file read-only and decides from what the index and readability scopes act on: broadened file modes (the same mode comparison ensureOpenClawStatePermissions skips on), a quarantine or recorded open failure, the integrity check the repair runs (quick check, or the full scan's damaged-index plan under --full-integrity-check), the dangling Skill Workshop review index, orphan task delivery rows, and the registry key the repair asserts first. The repair now calls the shared hasRecordedStateDatabaseDamage and classifyStateDatabaseIntegrity, and hasOpenClawStatePermissionDrift shares the target list with ensureOpenClawStatePermissions.

    • It cannot decide, and reports undecidable: PostgreSQL, an updater's migration capture or an artifact-preserving read (both would need a copy), and a WAL store no connection holds open, because a read-only open creates the -wal and -shm files (readOnlyOpenWouldCreateSqliteSidecars). Ownership refusals are not checked; they are preconditions, not work.
    • What the quick check cannot see, neither can the quick repair: a damaged index key needs --full-integrity-check in both (test "decides damaged index keys").
  • Database migrations (doctor-heartbeat-migration-backup.ts). checkDoctorMigrationBackupsPending uses the step's own gates: a pending, refused or indeterminate store in the prepared preflight, needsSharedStateMigrationBackup (now the backup's gate too), an automatic heartbeat repair, and needsHeartbeatMigrationBackup (now the heartbeat backup's gate). It reads live stores (withLiveStateReads), never copies.

    • Why it is undecidable inside the stop. The heartbeat predicate reads each agent database. Once the Gateway stops, nothing holds the agent store open and its sidecars are gone, so the read would create them. Beside a serving Gateway (where step 6 will run the checks) the sidecars exist and the check decides; the unit test holds the store open as the Gateway does and gets none. Making it decidable under the stop needs an immutable read path through the agent read owner; I left that for step 6 rather than widen this PR.
  • Runtime checks (doctor-health-startup.ts composes owners): the deletion journal (checkDoctorAgentDeletionJournalPending, the repair's gates plus the rows quarantine reads via the now-exported readAgentDeletionJournalRecoverySource), Control UI assets (checkUiProtocolFreshnessPending, the same detector and the prompter's auto-approve rule), the Bun launcher (planning split from the repair) and the pnpm launcher (hasPublishedPnpmLauncherRepairWork, the read-only half of the repair). Source install notes, plugin runtime links, NOCOW inspection and startup hints only print notes. The pnpm launcher repair was written by us (fix: keep the openclaw command and Gateway working after updating a pnpm install #201735, fix(update): repair only this account's pnpm launcher and never keep set-ID bits #203447), not by the updater owner, so it got a check too. A stale launcher owned by another account still reports work although the repair refuses it; that over-reports, never under-reports.

  • Config flow stays at the default. I enumerated its write sites from source (preflight, flow body and runDoctorRepairSequence). About 50 sites write conditionally. These have no read-only detector:

    • Sites that open or lease on every run: the state schema step in doctor mode, which bypasses needsOpenClawStateDatabaseSchemaRepair; the media persistence lease and registry last_seen_at; the cold navigation lease; the post-core plugin convergence lease; plugin source capture directories; and the Tailscale profile migration's writable open.
    • Repairs whose plan exists only inside the function: legacy auth profile stores, the auth profile import, configured plugin installs, skill workshop retirement and ACP metadata.
    • Plugin-defined doctor migrations.

    Step 3 measured that these write no store bytes on a clean run. A none answer still needs a detector per site, and several of those depend on network or plugin code. Composing only the existing detectors (detectLegacyStateMigrations, collectCronCodexRuntimePolicyTargetsReadOnly, detectLegacyExecApprovals and about 20 others) would be wrong in the "none" direction, so the flow keeps check=default. The site-by-site inventory goes in the step 4 note on Design: prepared-release updates (state, ownership and plan) #204775.

The first CI run also flagged one uncommented type assertion in the permission drift helper; it is gone.

No schema, stored field, config option, protocol, dependency, retention or permission change.

Update behavior

No update-path change. A published updater that runs Doctor on this build runs the same repairs in the same order. Under an updater's migration capture every new check reports undecidable, so nothing could be skipped there later either.

Evidence

Cells: Linux, systemd user Gateway, npm install of a pack under an unpublished version, 4.4 GB agent database (3.3 MB state database) under 1 MiB/s of writes, /readyz probed every 250 ms. Base is main at fcc9a0648c2; head is f720994f8f1 (this PR's first commit). Each doctor cell runs openclaw doctor --fix --non-interactive --yes three times on a healthy install. Each guard cell then seeds real work while the Gateway serves (state database mode 0644, an orphan task_delivery_state row, an unusable deletion journal row for the reserved openclaw agent) and runs doctor --fix twice.

base fcc9a0648c2 head f720994f8f1
state-database-repair, healthy work check=default none check=owner (6.9 to 9.7 ms)
runtime-checks, healthy work check=default none check=owner (17 to 50 ms)
database-migrations, healthy work check=default undecidable check=owner (under 1 ms)
Outage, three healthy runs 41.6 / 41.9 / 38.4 s 44.9 / 41.9 / 41.9 s
Guard: seeded work repaired (mode 600, orphan recovered, journal quarantined) same repairs, traced work check=owner
Guard: next clean run work check=default none check=owner
Guard outage, settle / work / clean 38.8 / 39.4 / 36.1 s 40.6 / 41.6 / 40.3 s

The outage difference (about 1 to 3 s) is larger than the checks' own cost (under 60 ms in total). Host load was 13 at the start of the head doctor cell and about 5 at the start of the base one, so I read it as host noise. Nothing is skipped yet. The checks' cost depends on the state database (quick check) and on the agent database's heartbeat_outcomes rows, not on the agent database file size, so at 10 GiB I expect the same tens of milliseconds. That is an estimate; I did not run a 10 GiB cell.

Recordings replay the raw cell logs.

RED/BASE, fcc9a0648c2:

red-base.mp4

GREEN/HEAD, f720994f8f1:

green-head.mp4
Raw evidence, base (home paths shortened)
=== RED/BASE: main fcc9a0648c2
cell red-base: Linux, systemd user Gateway, npm install of pack 2026.11.24 fcc9a0648c2, agent database 4435914752 bytes under 1 MiB/s of writes, /readyz every 250 ms

---- doctor1: OPENCLAW_GATEWAY_STARTUP_TRACE=1 openclaw doctor --fix --non-interactive --yes (healthy install)
  trace: doctor.pending.state-database-repair pending=work check=default (0.0ms)
  trace: doctor.pending.database-migrations pending=work check=default (0.0ms)
  trace: doctor.pending.state-schema pending=none check=owner (0.1ms)
  trace: doctor.pending.runtime-checks pending=work check=default (0.0ms)
  trace: doctor.pending.config-flow pending=work check=default (0.0ms)
  doctor: The Gateway was stopped for 41.3 s.

---- doctor2: OPENCLAW_GATEWAY_STARTUP_TRACE=1 openclaw doctor --fix --non-interactive --yes (healthy install)
  trace: doctor.pending.state-database-repair pending=work check=default (0.0ms)
  trace: doctor.pending.database-migrations pending=work check=default (0.0ms)
  trace: doctor.pending.state-schema pending=none check=owner (0.1ms)
  trace: doctor.pending.runtime-checks pending=work check=default (0.0ms)
  trace: doctor.pending.config-flow pending=work check=default (0.0ms)
  doctor: The Gateway was stopped for 41.7 s.

---- doctor3: OPENCLAW_GATEWAY_STARTUP_TRACE=1 openclaw doctor --fix --non-interactive --yes (healthy install)
  trace: doctor.pending.state-database-repair pending=work check=default (0.0ms)
  trace: doctor.pending.database-migrations pending=work check=default (0.0ms)
  trace: doctor.pending.state-schema pending=none check=owner (0.1ms)
  trace: doctor.pending.runtime-checks pending=work check=default (0.0ms)
  trace: doctor.pending.config-flow pending=work check=default (0.0ms)
  doctor: The Gateway was stopped for 38.1 s.

  doctor1: OUTAGE (/readyz) 41.6 s, doctor wall 58.7 s
  doctor2: OUTAGE (/readyz) 41.9 s, doctor wall 57.4 s
  doctor3: OUTAGE (/readyz) 38.4 s, doctor wall 57.6 s

---- guard cell guard-base (pack 2026.11.24 fcc9a0648c2): seed real work while the Gateway serves, then doctor --fix twice
  seeded: mode 644, orphan delivery row seeded-orphan, journal row openclaw
  after: mode 600, orphan delivery rows 0, journal rows openclaw 0, recovery receipts 0
  after: mode 600, orphan delivery rows 0, journal rows openclaw 0, recovery receipts 0
  doctorwork:
    trace: doctor.pending.state-database-repair pending=work check=default (0.0ms)
    trace: doctor.pending.runtime-checks pending=work check=default (0.0ms)
    doctor: Preserved and recovered 1 orphan task delivery rows; backup and row export: ~/.openclaw/state/openclaw-task-delivery-recover
    doctor: Quarantined unusable agent deletion journal records and recorded a Doctor receipt listing held stores.
  doctorclean:
    trace: doctor.pending.state-database-repair pending=work check=default (0.0ms)
    trace: doctor.pending.runtime-checks pending=work check=default (0.0ms)
  doctor1: OUTAGE (/readyz) 38.8 s, doctor wall 57.1 s
  doctorwork: OUTAGE (/readyz) 39.4 s, doctor wall 54.3 s
  doctorclean: OUTAGE (/readyz) 36.1 s, doctor wall 51.3 s
Raw evidence, head (home paths shortened)
=== GREEN/HEAD: f720994f8f1
cell green-head: Linux, systemd user Gateway, npm install of pack 2026.11.25 f720994f8f1, agent database 4435914752 bytes under 1 MiB/s of writes, /readyz every 250 ms

---- doctor1: OPENCLAW_GATEWAY_STARTUP_TRACE=1 openclaw doctor --fix --non-interactive --yes (healthy install)
  trace: doctor.pending.state-database-repair pending=none check=owner (9.2ms)
  trace: doctor.pending.database-migrations pending=undecidable check=owner (0.8ms)
  trace: doctor.pending.state-schema pending=none check=owner (0.1ms)
  trace: doctor.pending.runtime-checks pending=none check=owner (50.4ms)
  trace: doctor.pending.config-flow pending=work check=default (0.0ms)
  doctor: The Gateway was stopped for 44.8 s.

---- doctor2: OPENCLAW_GATEWAY_STARTUP_TRACE=1 openclaw doctor --fix --non-interactive --yes (healthy install)
  trace: doctor.pending.state-database-repair pending=none check=owner (9.7ms)
  trace: doctor.pending.database-migrations pending=undecidable check=owner (0.6ms)
  trace: doctor.pending.state-schema pending=none check=owner (0.1ms)
  trace: doctor.pending.runtime-checks pending=none check=owner (40.0ms)
  trace: doctor.pending.config-flow pending=work check=default (0.1ms)
  doctor: The Gateway was stopped for 41.4 s.

---- doctor3: OPENCLAW_GATEWAY_STARTUP_TRACE=1 openclaw doctor --fix --non-interactive --yes (healthy install)
  trace: doctor.pending.state-database-repair pending=none check=owner (6.9ms)
  trace: doctor.pending.database-migrations pending=undecidable check=owner (0.9ms)
  trace: doctor.pending.state-schema pending=none check=owner (0.1ms)
  trace: doctor.pending.runtime-checks pending=none check=owner (30.7ms)
  trace: doctor.pending.config-flow pending=work check=default (0.0ms)
  doctor: The Gateway was stopped for 41.9 s.

  doctor1: OUTAGE (/readyz) 44.9 s, doctor wall 63.7 s
  doctor2: OUTAGE (/readyz) 41.9 s, doctor wall 61.1 s
  doctor3: OUTAGE (/readyz) 41.9 s, doctor wall 60.1 s

---- guard cell guard-head (pack 2026.11.25 f720994f8f1): seed real work while the Gateway serves, then doctor --fix twice
  seeded: mode 644, orphan delivery row seeded-orphan, journal row openclaw
  after: mode 600, orphan delivery rows 0, journal rows openclaw 0, recovery receipts 0
  after: mode 600, orphan delivery rows 0, journal rows openclaw 0, recovery receipts 0
  doctorwork:
    trace: doctor.pending.state-database-repair pending=work check=owner (6.9ms)
    trace: doctor.pending.runtime-checks pending=work check=owner (20.6ms)
    doctor: Preserved and recovered 1 orphan task delivery rows; backup and row export: ~/.openclaw/state/openclaw-task-delivery-recover
    doctor: Quarantined unusable agent deletion journal records and recorded a Doctor receipt listing held stores.
  doctorclean:
    trace: doctor.pending.state-database-repair pending=none check=owner (6.7ms)
    trace: doctor.pending.runtime-checks pending=none check=owner (29.6ms)
  doctor1: OUTAGE (/readyz) 40.6 s, doctor wall 55.5 s
  doctorwork: OUTAGE (/readyz) 41.6 s, doctor wall 60.3 s
  doctorclean: OUTAGE (/readyz) 40.3 s, doctor wall 56.7 s

The guard checker reports "recovery receipts 0" because it only looked under agents/openclaw/recovery, which is not where this receipt went. Doctor reported recording the receipt, and the journal row is gone on both builds.

Tests (real stores, no mocks of the behavior). Each "work" case proves the check leaves bytes, mtimes and inodes unchanged (database and WAL; -shm by inode only, since its index changes on every read), then runs the real repair and gets none:

  • src/state/openclaw-state-db-repair-pending.test.ts (new): healthy store (quick and full) is none with no snapshot staging calls; a closed WAL store is undecidable and gets no sidecars; mode 0644, a quarantine record, an orphan delivery row, a damaged index key (full only, matching the repair) and the dangling Workshop index each report work until the repair runs.

  • src/commands/doctor-maintenance-pending.test.ts: a real v26 agent store is work, the real prepareDoctorMigrationBackups migrates it under maintenance, then none; an unusable journal row is work, the real repair quarantines it, then none and the repair has nothing left.

  • src/infra/update-launcher-repair.test.ts: the stale launcher is true with unchanged bytes, mtime and inode, false after the repair, and false in all four "cannot prove it is ours" cases.

  • Red per predicate: removing each clause (mode drift, recorded damage, orphan rows, full index scan, sidecar guard, journal read, pnpm stale detection, pending migrations) fails its test; restored, all pass.

  • Also green on the merged head: doctor-agent-deletion-journal.test.ts, doctor-agent-deletion-journal-restore.test.ts, doctor-heartbeat-migration-backup.test.ts, doctor-schema-migrations.test.ts, doctor-bun-cli-launcher.test.ts, doctor-ui.test.ts, doctor-ui.repair.test.ts, doctor-health-startup.test.ts, doctor-health-startup.signal.test.ts, doctor-health.dangling-workshop-index.test.ts, doctor-health.deletion-recovery.test.ts, openclaw-state-db.test.ts, openclaw-state-db.index-repair.test.ts, openclaw-state-db-permissions.test.ts, openclaw-state-db.permissions.test.ts, openclaw-state-db-task-delivery-recovery.test.ts, openclaw-quarantine-store.test.ts, doctor-migration-backup.test.ts, test/scripts/update-restart-module-outcome.test.ts; pnpm tsgo:core, pnpm tsgo:core:test, oxlint and oxfmt on the touched files, the session identity ratchet.

  • test/scripts/pr-wrapper-source-closure.test.ts and eager-import-closure.test.ts failed in the first CI run: openclaw-state-db-repair.ts is in the PR wrapper's source closure and now imports openclaw-state-db-repair-pending.ts, so scripts/pr-lib/wrapper-components.txt lists it. The closure test passes locally after that. (Locally eager-import-closure.test.ts cannot run on this worktree's hoisted install, which lacks package-local node_modules; CI covers it.)

  • Files audited in full: doctor-maintenance-pending.test.ts and update-launcher-repair.test.ts. Nothing was removed; the existing cases still cover distinct behavior, and the launcher cases now also assert the check.

  • Review: $autoreview (Codex route, branch mode against origin/main, P0 to P2) returned scoped-clean.

  • test/scripts/pr-worktree-provision.test.ts (not every launched provisioner received its private store) fails in the first CI run and fails the same way locally on the main parent 54c7dc134e8; this PR does not touch it.

Not proven

  • The state database check's decision before the stop. The cells trace it inside the stop, where Doctor's own connection keeps the sidecars. Step 6 moves it before the stop, where the Gateway's connection does.
  • A 10 GiB cell, macOS, Windows, pnpm, Bun and Git installs, and PostgreSQL (undecidable by design).
  • A cell with a pending schema migration. That case is covered by the unit test against a real v26 store and the real step.
  • The cells ran on f720994f8f1. The branch then merged main three times, including step 5 (feat: let each Doctor repair say whether it has work before the Gateway stops #206247, the contribution checks; one import conflict in doctor-maintenance-pending.test.ts) and fix: shorten the first update's outage by skipping an idle second Doctor #206227 (idle second Doctor). Their new tests, the focused suites above, all 57 Doctor health and maintenance test files, pnpm tsgo:core and every baseline ratchet pass on the merged head. The cells did not run again.

…ecks

Doctor's state database repair, database migrations and runtime checks
now trace their real pending decision (work, none or undecidable)
instead of the default. Each check reads the live stores without
copying them, opening them for writing or creating files, and reuses
the predicates its step acts on. No step changes what it does.
@clawsweeper

clawsweeper Bot commented Nov 27, 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: XL labels Nov 27, 2026
@clawsweeper clawsweeper Bot added the P2 Normal backlog priority with limited blast radius. label Nov 27, 2026
The state database repair now imports its pending-check module, which
the PR wrapper inventory must carry. Also drop a type assertion from the
permission drift target list.
@openclaw-barnacle openclaw-barnacle Bot added the scripts Repository scripts label Nov 27, 2026
@fuller-stack-dev
fuller-stack-dev merged commit c32abfe into openclaw:main Nov 27, 2026
298 of 300 checks passed
@fuller-stack-dev

Copy link
Copy Markdown
Member Author

Merged via squash.

@fuller-stack-dev
fuller-stack-dev deleted the doctor-pending-step4 branch November 27, 2026 03:18
vincentkoc added a commit that referenced this pull request Nov 27, 2026
* commit '7df6cb759f9e604921536a700e2f527c5516e7de': (185 commits)
  docs: clarify what telemetry OpenClaw sends and how to opt out
  refactor(ui): remove retired stream-follow test seams (#206316)
  fix: distinguish missing command results from unknown outcomes (#206313)
  fix(build): reuse stamps after identical pnpm installs (#206282)
  chore(ci): inventory Gateway model catalog worker lifecycles (#206251)
  fix: keep failed image inspections inside chat activity (#206283)
  fix: clarify sent-message labels and chat activity icons (#206048)
  fix(cron): use the service's free memory for extra automation runs (#206322)
  test(android): reuse the shared TLS fixture (#206280)
  fix(ui): restore New Session recovery during startup (#206319)
  test(macos): align new Gateway URL expectations (#206281)
  feat: let doctor --fix tell when its startup repairs have nothing to do (#206246)
  fix(android): pause idle gateway discovery without blocking consumers (#196209)
  fix(ci): prepare the Android SDK for scheduled CodeQL scans (#203843)
  fix(memory): read delegated cgroup pressure from the mapped leaf (#206308)
  improve(config): reuse plugin metadata during auto-enable (#206289)
  improve: copy only the selected model for agent completions (#206287)
  fix(pr): refresh tooling before preparing reviewed incoming PRs (#206236)
  fix: keep Gateway ready while plugins drain after updates (#206299)
  fix(crabbox): keep unused prepared mirrors reusable (#206267)
  ...
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 27, 2026
…do (openclaw#206246)

* feat(doctor): give the startup maintenance steps read-only pending checks

Doctor's state database repair, database migrations and runtime checks
now trace their real pending decision (work, none or undecidable)
instead of the default. Each check reads the live stores without
copying them, opening them for writing or creating files, and reuses
the predicates its step acts on. No step changes what it does.

* fix(doctor): list the state repair check in the PR wrapper closure

The state database repair now imports its pending-check module, which
the PR wrapper inventory must carry. Also drop a type assertion from the
permission drift target list.

* fix(doctor): name the database migrations check apart from the backup completion check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands Command implementations P2 Normal backlog priority with limited blast radius. scripts Repository scripts size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant