Skip to content

fix: stop doctor --fix rewriting the plugin registry on a healthy install - #206195

Merged
fuller-stack-dev merged 3 commits into
openclaw:mainfrom
fuller-stack-dev:fix/doctor-plugin-registry-noop
Nov 26, 2026
Merged

fuller-stack-dev merged 3 commits into
openclaw:mainfrom
fuller-stack-dev:fix/doctor-plugin-registry-noop

Conversation

@fuller-stack-dev

Copy link
Copy Markdown
Member

Related: #204775

What Problem This Solves

Every openclaw doctor --fix on a healthy install rewrote the persisted plugin registry, even when no plugin had changed. Each run stored a new revision of the whole index, so a second clean run never left the state database as it found it. This is step 3 of the plan in #204775: a clean run should write nothing. A conditional Gateway stop (step 6) can only skip the stop when every step can say "nothing to do" and mean it.

User Impact

On a healthy install, doctor --fix no longer rewrites the plugin registry. The step took 0.09 to 0.24 s instead of 0.36 to 1.19 s in the measured cells (the higher figures are runs under strace). When the registry really is stale (a plugin added or removed, a damaged record), Doctor still rebuilds it as before. With OPENCLAW_GATEWAY_STARTUP_TRACE=1 the plugin registry step now reports its real answer (pending=none check=owner or pending=work check=owner) instead of the default.

Why This Change Was Made

  • Doctor already had a no-op guard here. It rediscovers the plugin inventory and skips the write when it equals the stored index. The guard compared raw discovery with what the store returns. The store keeps only bundledDist of a plugin's build metadata, so every bundled plugin that declares worker entries, static assets or a host version always looked different. In the cell, 21 of 79 bundled plugins differed in packageBuild alone.
  • The plugin index writer now exports projectInstalledPluginIndexForStore: the index as the writer would store it and the reader would return it. Doctor compares through it, so "unchanged" means "the write would store the same index". Serialization moved out of the row writer into a helper both paths use, so the two cannot drift apart.
  • The guard is now checkPluginRegistryRepairPending. The repair uses it to decide whether to write, and the plugin registry contribution uses it as its step 1 pending check, so the trace and the write share one predicate. It runs twice per Doctor run under maintenance (trace, then repair), 0.04 to 0.13 s each in the cells. Its state reads reuse Doctor's maintenance connection: run 2 made the same number of read-only database copies on base and head. Contribution pending checks now receive the flow context. No other contribution declares one yet.
  • Invalid persisted records still go to the repair path, which reports them under its lease as before. The check returns undecidable for them, which counts as work.
  • No schema, stored field, config option or retention change. The stored index has the same shape. It is just not rewritten when nothing changed.

Every always-firing write site from the step 1 table, checked

Two cells on unmodified main traced every SQL statement that changed rows in the Doctor process tree, plus every write-intent file syscall (strace -f). They also snapshotted every store before and after each run, compared against a plain systemctl --user restart of the Gateway with no Doctor.

Site (owner) On a healthy second run This PR Pending check
Plugin registry refresh (commands/doctor-plugin-registry.ts, plugins/installed-plugin-index-store-write.ts) rewrote config_machine_state[plugins.installedIndex] (new revision, new index) conditional owner: checkPluginRegistryRepairPending
State database repair: writable open and file modes (state/openclaw-state-db-permissions.ts, doctor-config-preflight.ts) no change to bytes or mtime. applyPrivateModeSync already skips a same-mode chmod. The r+ open writes nothing. unchanged step 4: the composed predicate needs needsOpenClawStateDatabaseSchemaRepair (copy plus integrity check)
Additive agent schemas and migration backups (doctor-heartbeat-migration-backup.ts) no agent database write and no backup file. It already runs only for pending migrations. unchanged step 4 (preflight pendingMigrations)
Config preflight (doctor-config-preflight.ts, doctor-config-flow.ts) no write to openclaw.json, its backups or any state row. Every repair there is gated by its own detector. unchanged step 4 (compose the detectors)
Boot lifecycle (infra/gateway-boot-lifecycle.ts) a write transaction that matches zero rows. No row changes and no bytes change. unchanged step 5
Codex session routes (doctor/shared/codex-route-warnings.ts) no write. The forced auth-profile rewrite needs an OpenAI Codex auth-profile id map, which a healthy install does not have. unchanged step 5
Shell completion (doctor-completion.ts) no write. It returns early when the profile is installed and the cache exists, and under --non-interactive. unchanged step 5
Daemon runtime pin (daemon/runtime-pin-state.ts) no write. It runs only while the migration receipt is incomplete. unchanged step 5
pnpm launcher (infra/update-launcher-repair.ts) no write. It rewrites only a launcher that names a removed stage. unchanged n/a on npm

Fields that change on a clean run by design (the run's own record)

These change between the end of run 1 and the end of run 2 on this head. Each is also changed by a plain Gateway restart without Doctor, or belongs to the stop itself:

  • ~/.openclaw.update-captures/doctor-<id>: Doctor's pre-repair capture, one per stopped run, newest kept. It is part of the stop (step 6).
  • state database gateway_boot_lifecycle (the stopped boot gets completed_at_ms, outcome, reason, and a row for the new boot), config_machine_state[gateway.crashes] and [operator.channelPolicy] (updated_at_ms, the new boot's identity), config_health_entries and diagnostic_events[config-snapshot] (observation times), official_external_plugin_catalog_snapshots (saved_at, updated_at_ms): written by the restarted Gateway.
  • agent_databases.last_seen_at: set on every open of an agent database, by the Gateway and by Doctor. schema_version and size_bytes stay the same.
  • state_leases and agent_database_leases: custody leases. Doctor inserts and deletes its own within the run, and the remaining rows belong to the new Gateway process.
  • ~/.openclaw/logs/openclaw-<date>.log (appended), lock files under ~/.openclaw/tmp/openclaw-<uid>/, ~/.openclaw/tmp/plugin-captures/<id> (Gateway plugin-load scratch), openclaw.json.last-good (inode and mtime), and SQLite -wal and -shm sidecars.

Unchanged on both runs: openclaw.json, the systemd unit file, the agent database file, and every other row.

Update behavior

There is no update-path change. A published updater that runs Doctor on this build runs the same repairs in the same order. A plugin registry that is stale after an update (new bundled plugins or new build metadata) still differs from discovery in the stored fields and is rebuilt as before. The cells show the guard does not overreach: with a plugin record removed, the step reports pending=work and rebuilds the index.

Evidence

Real cells: Linux, systemd user Gateway, npm install of a packed build under an unpublished version, 64 MiB agent database, /readyz probed every 250 ms. Base is main at d0f76989509. Head is this PR's first commit, 94aa43b7f32. Each snapshot cell runs openclaw doctor --fix --non-interactive --yes three times. Runs 1 and 2 are under strace -f and a harness-only node:sqlite statement trace (NODE_OPTIONS=--import, Doctor process tree only). A plain Gateway restart runs between runs 2 and 3. Every store is hashed and its rows are dumped before and after each step.

base d0f76989509 head 94aa43b7f32
Doctor-only row changes, end of run 1 to end of run 2 (beyond a plain restart) config_machine_state[plugins.installedIndex]: index, revision none
plugins.installedIndex revision across runs 2 and 3 changes on every run unchanged
Row-changing SQL in the run 2 Doctor tree, apart from leases and agent_databases 1 config_machine_state upsert none
Plugin registry step on a clean run (pending check plus repair) 0.36 to 1.19 s, "Plugin registry refreshed" 0.09 to 0.24 s, no write
doctor.pending.contribution.doctor:plugin-registry pending=work check=default pending=none check=owner
Read-only database copies made by run 2 (strace) 78 to 80 78

Guard cells (same setup, no strace). After one settling run, a script seeded real work while the Gateway served: state database mode 0644, a startup_failed boot row with a maintenance startup reason, and the persisted plugin index with one plugin record removed. Then doctor --fix ran twice:

base head
work run: state database mode 644 → 600 644 → 600
work run: seeded boot row startup_failure_repaired startup_failure_repaired
work run: plugin index 78 → 79 plugins, rewritten 78 → 79 plugins, rewritten, pending=work check=owner
next clean run: plugin index rewritten again untouched, pending=none check=owner
/readyz outage, settle / work / clean 26.9 / 23.4 / 24.5 s 29.5 / 27.0 / 26.0 s

The guard outages differ by about 2.5 s while the plugin registry step itself is 0.3 s faster on head. Host load was higher during the head guard cell (1-minute load 6.4 vs 4.1 at start), and the clean third runs of the snapshot cells went the other way (Doctor reported 28.7 s of stop on base and 23.3 s on head). I read this as host noise.

Recordings replay the raw cell logs: the trace line, Doctor's own output, the row-changing SQL, the per-store diff against a plain restart, the index revision at every snapshot, and the guard cell.

RED/BASE, d0f76989509:

doctor-plugin-registry-red-base.mp4

GREEN/HEAD, 94aa43b7f32:

doctor-plugin-registry-green-head.mp4
Raw evidence, base (home paths shortened)
=== RED/BASE: main d0f76989509
cell snap-base: systemd user Gateway, npm install of pack 'base' (2026.11.21 d0f76989509)

---- doctor2: openclaw doctor --fix --non-interactive --yes (second and third run on a healthy install)
  trace: doctor.pending.contribution.doctor:plugin-registry pending=work check=default (0.0ms)
  doctor: Plugin registry refreshed: 48/79 enabled plugins indexed.
  doctor: The Gateway was stopped for 64.2 s.

---- doctor3: openclaw doctor --fix --non-interactive --yes (second and third run on a healthy install)
  trace: doctor.pending.contribution.doctor:plugin-registry pending=work check=default (0.0ms)
  doctor: Plugin registry refreshed: 48/79 enabled plugins indexed.
  doctor: The Gateway was stopped for 28.7 s.

---- SQL statements in the doctor2 process tree that changed state-database rows (harness trace)
    2 state/openclaw.sqlite all: delete from "agent_database_leases" where "lease_id" = ? returning "path", "agent_id"  
    7 state/openclaw.sqlite run: delete from "state_leases" where "scope" = ? and "lease_key" = ? and "owner" = ?  
    2 state/openclaw.sqlite run: insert into "agent_database_leases" ("lease_id", "provenance", "agent_id", "path", "owner_pid", "owner_start_time", "opened_at"
    3 state/openclaw.sqlite run: insert into "agent_databases" ("agent_id", "path", "schema_version", "last_seen_at", "size_bytes") values (?, ?, ?, ?, ?) on co
    1 state/openclaw.sqlite run: insert into "config_machine_state" ("state_key", "value_json", "updated_at_ms") values (?, ?, ?) on conflict ("state_key") do u
    7 state/openclaw.sqlite run: insert into "state_leases" ("scope", "lease_key", "owner", "expires_at", "heartbeat_at", "payload_json", "created_at", "updated
    2 state/openclaw.sqlite run: update "agent_database_leases" set "opened_at" = ? where "lease_id" = ? and "agent_id" = ? and "path" = ? and "owner_pid" = ?  
    4 state/openclaw.sqlite run: update "state_leases" set "expires_at" = ?, "heartbeat_at" = ?, "updated_at" = ? where "scope" = ? and "lease_key" = ? and "own

== snap-base: end of doctor1 -> end of doctor2
-- files changed by doctor2 that a plain restart does not change:
   ~/.openclaw.update-captures/doctor-<uuid> (Doctor's pre-repair capture, replaced each run): added
-- rows changed by doctor2 that a plain restart does not change:
   ~/.openclaw/state/openclaw.sqlite config_machine_state[plugins.installedIndex]: updated_at_ms value_json value_json keys: index,revision
-- also changed by a plain restart (Gateway's own boot and serving): 22 file entries, 9 rows
   ~/.openclaw/state/openclaw.sqlite agent_database_leases[*]: agent_id lease_id opened_at owner_pid owner_start_time path provenance
   ~/.openclaw/state/openclaw.sqlite agent_databases[main]: last_seen_at
   ~/.openclaw/state/openclaw.sqlite config_health_entries[~/.openclaw/openclaw.json]: last_known_good_json last_promoted_good_json updated_at_ms
   ~/.openclaw/state/openclaw.sqlite config_machine_state[gateway.crashes]: updated_at_ms value_json value_json keys: active
   ~/.openclaw/state/openclaw.sqlite config_machine_state[operator.channelPolicy]: updated_at_ms
   ~/.openclaw/state/openclaw.sqlite diagnostic_events[config-snapshot]: created_at
   ~/.openclaw/state/openclaw.sqlite gateway_boot_lifecycle[*]: completed_at_ms outcome reason
   ~/.openclaw/state/openclaw.sqlite official_external_plugin_catalog_snapshots[None]: saved_at updated_at_ms
   ~/.openclaw/state/openclaw.sqlite state_leases[*]: created_at expires_at heartbeat_at owner payload_json updated_at
   plugins.installedIndex revision at doctor1-snap-after: 1795731270514
   plugins.installedIndex revision at doctor2-snap-before: 1795731270514
   plugins.installedIndex revision at doctor2-snap-after: 1795731376419
   plugins.installedIndex revision at restart-snap-after: 1795731376419
   plugins.installedIndex revision at doctor3-snap-before: 1795731376419
   plugins.installedIndex revision at doctor3-snap-after: 1795731458264

---- guard cell guard-base: seed real work, then doctor --fix twice
  seeded: mode 644, boot row seeded-startup-failure, index plugins 78 (removed xai), revision 1795730582663
  after: mode 600, boot row outcome startup_failure_repaired, index plugins 79, revision 1795730620391
  after: mode 600, boot row outcome startup_failure_repaired, index plugins 79, revision 1795730658319
  doctorwork: pending=work check=default; 1 registry write(s)
  doctorclean: pending=work check=default; 1 registry write(s)
  doctor1: OUTAGE (/readyz) 26.9 s, doctor wall 39.7 s
  doctorwork: OUTAGE (/readyz) 23.4 s, doctor wall 37.0 s
  doctorclean: OUTAGE (/readyz) 24.5 s, doctor wall 38.5 s
Raw evidence, head (home paths shortened)
=== GREEN/HEAD: 94aa43b7f32
cell snap-head: systemd user Gateway, npm install of pack 'head' (2026.11.22 94aa43b7f32)

---- doctor2: openclaw doctor --fix --non-interactive --yes (second and third run on a healthy install)
  trace: doctor.pending.contribution.doctor:plugin-registry pending=none check=owner (104.1ms)
  doctor: (no plugin registry write reported)
  doctor: The Gateway was stopped for 50.2 s.

---- doctor3: openclaw doctor --fix --non-interactive --yes (second and third run on a healthy install)
  trace: doctor.pending.contribution.doctor:plugin-registry pending=none check=owner (48.2ms)
  doctor: (no plugin registry write reported)
  doctor: The Gateway was stopped for 23.3 s.

---- SQL statements in the doctor2 process tree that changed state-database rows (harness trace)
    2 state/openclaw.sqlite all: delete from "agent_database_leases" where "lease_id" = ? returning "path", "agent_id"  
    6 state/openclaw.sqlite run: delete from "state_leases" where "scope" = ? and "lease_key" = ? and "owner" = ?  
    2 state/openclaw.sqlite run: insert into "agent_database_leases" ("lease_id", "provenance", "agent_id", "path", "owner_pid", "owner_start_time", "opened_at"
    3 state/openclaw.sqlite run: insert into "agent_databases" ("agent_id", "path", "schema_version", "last_seen_at", "size_bytes") values (?, ?, ?, ?, ?) on co
    6 state/openclaw.sqlite run: insert into "state_leases" ("scope", "lease_key", "owner", "expires_at", "heartbeat_at", "payload_json", "created_at", "updated
    2 state/openclaw.sqlite run: update "agent_database_leases" set "opened_at" = ? where "lease_id" = ? and "agent_id" = ? and "path" = ? and "owner_pid" = ?  
    4 state/openclaw.sqlite run: update "state_leases" set "expires_at" = ?, "heartbeat_at" = ?, "updated_at" = ? where "scope" = ? and "lease_key" = ? and "own

== snap-head: end of doctor1 -> end of doctor2
-- files changed by doctor2 that a plain restart does not change:
   ~/.openclaw.update-captures/doctor-<uuid> (Doctor's pre-repair capture, replaced each run): added
-- rows changed by doctor2 that a plain restart does not change:
-- also changed by a plain restart (Gateway's own boot and serving): 21 file entries, 9 rows
   ~/.openclaw/state/openclaw.sqlite agent_database_leases[*]: agent_id lease_id opened_at owner_pid owner_start_time path provenance
   ~/.openclaw/state/openclaw.sqlite agent_databases[main]: last_seen_at
   ~/.openclaw/state/openclaw.sqlite config_health_entries[~/.openclaw/openclaw.json]: last_known_good_json last_promoted_good_json updated_at_ms
   ~/.openclaw/state/openclaw.sqlite config_machine_state[gateway.crashes]: updated_at_ms value_json value_json keys: active
   ~/.openclaw/state/openclaw.sqlite config_machine_state[operator.channelPolicy]: updated_at_ms
   ~/.openclaw/state/openclaw.sqlite diagnostic_events[config-snapshot]: created_at
   ~/.openclaw/state/openclaw.sqlite gateway_boot_lifecycle[*]: completed_at_ms outcome reason
   ~/.openclaw/state/openclaw.sqlite official_external_plugin_catalog_snapshots[None]: saved_at updated_at_ms
   ~/.openclaw/state/openclaw.sqlite state_leases[*]: created_at expires_at heartbeat_at owner payload_json updated_at
   plugins.installedIndex revision at doctor1-snap-after: 1795729597116
   plugins.installedIndex revision at doctor2-snap-before: 1795729597116
   plugins.installedIndex revision at doctor2-snap-after: 1795729597116
   plugins.installedIndex revision at restart-snap-after: 1795729597116
   plugins.installedIndex revision at doctor3-snap-before: 1795729597116
   plugins.installedIndex revision at doctor3-snap-after: 1795729597116

---- guard cell guard-head: seed real work, then doctor --fix twice
  seeded: mode 644, boot row seeded-startup-failure, index plugins 78 (removed xai), revision 1795730843196
  after: mode 600, boot row outcome startup_failure_repaired, index plugins 79, revision 1795730934647
  after: mode 600, boot row outcome startup_failure_repaired, index plugins 79, revision 1795730934647
  doctorwork: pending=work check=owner; 1 registry write(s)
  doctorclean: pending=none check=owner; 0 registry write(s)
  doctor1: OUTAGE (/readyz) 29.5 s, doctor wall 44.7 s
  doctorwork: OUTAGE (/readyz) 27.0 s, doctor wall 42.1 s
  doctorclean: OUTAGE (/readyz) 26.0 s, doctor wall 41.3 s
Root cause, from a debug build of base that printed the guard's differences
key plugins
  len 79 vs 79
  [5] agentsapi.packageBuild: {} | {"openclawVersion":"2026.11.10"}
  [14] browser.packageBuild: {} | {"workerEntries":["./native-host-entry.ts", ...]}
  [21] cua-computer.packageBuild: {"bundledDist":true} | {"bundledDist":true,"openclawVersion":"2026.11.10"}
  (18 more plugins, all packageBuild only)
health issues: []

Left is the stored index as read back. Right is fresh discovery. The store's reader admits only packageBuild.bundledDist.

Tests

  • src/commands/doctor-plugin-registry.test.ts, new case: a bundled plugin declares workerEntries and openclawVersion beside bundledDist. After the first repair builds the registry, a second repair leaves the state database and its WAL byte-identical with the same mtimes, and the check returns none. With a plugin added, the check returns work, the repair rewrites the index with both plugins, and the stored build metadata is { bundledDist: true }.
    • Red: with the projection removed (raw comparison, as on base), the store-unchanged assertion fails because the second repair rewrites the row.
  • Focused suites on the merged head, all green: doctor-plugin-registry.test.ts, doctor-plugin-registry-generation-repair.test.ts, doctor-maintenance-pending.test.ts, doctor-health-contributions.test.ts, doctor-health-contributions.cancellation.test.ts, installed-plugin-index-store-write.custody.test.ts, installed-plugin-index-store-write.receipt.test.ts, installed-plugin-index-store.test.ts. The new tests from fix: shorten doctor --fix Gateway downtime by removing old updater folders before the stop #206185 (doctor-maintenance.requester-authority.test.ts, doctor-health.migration-refusal.test.ts, doctor-retained-runtime.settlement.test.ts) also pass after merging main: 224 tests in total.
  • Also green: pnpm tsgo:core, pnpm tsgo:core:test, oxlint on the touched files (including the 700-line cap on doctor-plugin-registry.ts), the session identity ratchet and git diff --check.
  • Files audited in full: src/commands/doctor-plugin-registry.test.ts. The existing "refreshes an existing registry during repair" case still covers the plain no-op without build metadata, so it stays. Nothing was removed, and the file has no skips.
  • Review: $autoreview (Codex route, branch mode against origin/main, P0 to P2) returned scoped-clean.

Not proven

  • macOS, Windows, pnpm and Git installs. The change is in Doctor's comparison and the index serializer, which are platform-independent, but no cell ran there.
  • A store larger than 64 MiB. Nothing in this change scales with the agent database.
  • The cells ran on 94aa43b7f32. The branch then merged main twice (fix: shorten doctor --fix Gateway downtime by removing old updater folders before the stop #206185 in retained-runtime cleanup, plus unrelated agent and channel changes). The focused suites ran again on the merged head. The cells did not.
  • Run-to-run outage noise was not measured beyond these three runs per cell.

Every healthy doctor --fix rewrote the persisted plugin index with a new
revision. The no-op guard compared raw rediscovery with the stored index,
but the store keeps only bundledDist of package build metadata, so any
bundled plugin declaring worker entries, assets or a host version always
looked changed.

The guard now compares the rediscovered index in the form the store
would write and read it back, and it is the plugin registry
contribution's pending check, so the trace and the write share one
predicate.
@clawsweeper

clawsweeper Bot commented Nov 26, 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 Nov 26, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. labels Nov 26, 2026
@fuller-stack-dev
fuller-stack-dev merged commit fcc9a06 into openclaw:main Nov 26, 2026
123 checks passed
@fuller-stack-dev

Copy link
Copy Markdown
Member Author

Merged via squash.

@fuller-stack-dev
fuller-stack-dev deleted the fix/doctor-plugin-registry-noop branch November 26, 2026 22:36
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 27, 2026
…206195)

Every healthy doctor --fix rewrote the persisted plugin index with a new
revision. The no-op guard compared raw rediscovery with the stored index,
but the store keeps only bundledDist of package build metadata, so any
bundled plugin declaring worker entries, assets or a host version always
looked changed.

The guard now compares the rediscovered index in the form the store
would write and read it back, and it is the plugin registry
contribution's pending check, so the trace and the write share one
predicate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands Command implementations impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. P2 Normal backlog priority with limited blast radius. size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant