Skip to content

refactor: record which doctor --fix steps have work before the Gateway stop can become conditional - #206152

Merged
fuller-stack-dev merged 2 commits into
openclaw:mainfrom
fuller-stack-dev:doctor-pending-step1
Nov 26, 2026
Merged

fuller-stack-dev merged 2 commits into
openclaw:mainfrom
fuller-stack-dev:doctor-pending-step1

Conversation

@fuller-stack-dev

Copy link
Copy Markdown
Member

Related: #204775

What Problem This Solves

Every openclaw doctor --fix stops a running Gateway before it knows whether any repair has work, and at 4 GiB of state that stop lasts about 40 to 45 seconds. Skipping the stop safely needs each repair step to answer "is there work?" without writing. No step can answer that today, and nothing records which steps would say "no".

This is step 1 of the six-step plan in the #204775 design comment: give every step that answer and trace it. Nothing changes in what Doctor does.

User Impact

None visible. doctor --fix still stops the Gateway, runs every step and restores it, as before. With OPENCLAW_GATEWAY_STARTUP_TRACE=1 the trace now has one doctor.pending.<step> line per step with pending=work|none|undecidable and check=owner|default. The next PRs use these lines to see which steps still lack a check.

Why This Change Was Made

  • One small contract in doctor-maintenance-pending.ts. A step's check returns work, none or undecidable. A step without a check counts as work, and an undecidable answer or a thrown error counts as work too. A check must not write, must not open a store in a mode that can create or modify files, and must not copy a database.
  • Every step inside the stop is wired in, at the place where it runs: legacy state move, client credentials, state database repair, database migrations, state schema, runtime checks, config flow, each of the 81 contributions (through an optional pending on the contribution contract), external config, maintenance-ready, boot lifecycle, NOCOW, SQLite reclamation, retained runtimes and service definition repair.
  • Real checks only where an existing read-only predicate could be reused as is:
    • legacy state move: resolvePendingLegacyStateDirMigrationPaths (lstat only);
    • state schema: the prepared preflight's pendingMigrations (no IO); the step's own gate now calls the same check, so the trace cannot drift from the gate;
    • NOCOW: the existing inspectDoctorSqliteNoCow result. A failed inspection (for example, no lsattr) is undecidable. Before this change that case was indistinguishable from "nothing to do".
  • Everything else stays on the default. Composing the state database repair predicates is not trivially safe: needsOpenClawStateDatabaseSchemaRepair copies the database under an update capture and runs an integrity check, and the repair also changes file modes on every run. These are listed for steps 3 to 5.
  • The trace uses the existing startup trace (measureGatewayBootstrapStep). No new stored field, config option, CLI flag or schema.
  • doctor-health.ts was at its 700-line cap. The post-check NOCOW, reclamation and retained-runtime block moved unchanged into doctor-health-maintenance-steps.ts. The contribution timing moved into measureDoctorContribution in the contribution utilities, so that file keeps its cap too.

Update behavior

There is no update-path change. A published updater that runs Doctor on this build runs the same steps in the same order. The only difference is extra trace lines when the trace variable is set.

Evidence

Real cell, Linux, systemd user Gateway, npm install, 4 GiB agent database with 1 MiB/s of writes, /readyz probed every 250 ms. Base pack is main at 541ebfbbe75. Head pack is this PR's first commit, 3366380abca. Both packs use unpublished version numbers. Two openclaw doctor --fix --non-interactive --yes runs per cell:

Run Base outage Head outage Doctor's own "stopped for" (base / head)
first --fix 44.1 s 44.9 s 44.0 s / 44.5 s
second --fix 46.1 s 39.6 s 46.3 s / 39.5 s
  • Both cells stopped and restarted the Gateway the same way: a new MainPID each run, NRestarts=0, stop request to stopped 0.3 s, started to ready 6.5 to 7.0 s, exit 0. The difference between runs is host noise: head's second run is faster than base's.
  • Each pending check took at most 0.2 ms.
  • Head prints 94 doctor.pending.* lines per run: legacy-state-dir and state-schema are none (owner check), sqlite-nocow is undecidable (owner check), and the other 91 are work (default).
  • sqlite-nocow is undecidable because the cell's store is on btrfs and its image has no lsattr. Base prints the same NOCOW check skipped: lsattr is unavailable note and silently treats it as nothing to do. On such a host a conditional stop must still stop.

Recordings are replays of the raw cell logs. Each shows, per run, the systemd PIDs before and after, Doctor's stop and restart lines, the per-step pending lines on head, and the measured outage.

RED/BASE, 541ebfbbe75 (no pending trace):

red-base-541ebfbbe75.mp4

GREEN/HEAD, 3366380abca:

green-head-3366380abca.mp4
Raw trace lines, head, first run (home path shortened)
[gateway] startup trace: doctor.pending.legacy-state-dir 0.1ms total=26448.2ms start=26448.1ms pending=none check=owner
[gateway] startup trace: doctor.pending.client-credentials 0.0ms total=29599.8ms start=29599.8ms pending=work check=default
[gateway] startup trace: doctor.pending.state-database-repair 0.0ms total=29600.0ms start=29600.0ms pending=work check=default
[gateway] startup trace: doctor.pending.database-migrations 0.0ms total=29620.7ms start=29620.7ms pending=work check=default
[gateway] startup trace: doctor.pending.state-schema 0.1ms total=29894.5ms start=29894.4ms pending=none check=owner
[gateway] startup trace: doctor.pending.runtime-checks 0.0ms total=29894.9ms start=29894.8ms pending=work check=default
~/.openclaw/state/openclaw.sqlite: Error: NOCOW check skipped: lsattr is unavailable or could not read file attributes.
~/.openclaw/agents/main/agent/openclaw-agent.sqlite: Error: NOCOW check skipped: lsattr is unavailable or could not read file attributes.
[gateway] startup trace: doctor.pending.config-flow 0.0ms total=29955.3ms start=29955.2ms pending=work check=default
[gateway] startup trace: doctor.pending.contribution.doctor:write-config-migrations 0.0ms total=36960.5ms start=36960.5ms pending=work check=default
[gateway] startup trace: doctor.pending.contribution.doctor:agent-database-admission 0.0ms total=37169.5ms start=37169.4ms pending=work check=default
(79 more contribution lines, all pending=work check=default)
[gateway] startup trace: doctor.pending.external-config 0.0ms total=49214.2ms start=49214.2ms pending=work check=default
[gateway] startup trace: doctor.pending.maintenance-ready 0.0ms total=49215.5ms start=49215.5ms pending=work check=default
[gateway] startup trace: doctor.pending.boot-lifecycle 0.0ms total=49659.2ms start=49659.1ms pending=work check=default
[gateway] startup trace: doctor.pending.sqlite-nocow 0.2ms total=49666.5ms start=49666.3ms pending=undecidable check=owner
[gateway] startup trace: doctor.pending.retained-runtimes 0.0ms total=49666.6ms start=49666.6ms pending=work check=default
[gateway] startup trace: doctor.pending.service-definition 0.1ms total=50294.7ms start=50294.7ms pending=work check=default
Raw systemd and outage lines, both cells
base 541ebfbbe75, doctor1: before MainPID=1472 NRestarts=0; doctor-exit=0 doctor-ms=60467; after MainPID=7260 NRestarts=0
  OUTAGE (/readyz): 44.1s  stopped->started: 37.1s; stop request->stopped: 0.3s; started->ready: 7.0s
base 541ebfbbe75, doctor2: before MainPID=7260 NRestarts=0; doctor-exit=0 doctor-ms=64177; after MainPID=13400 NRestarts=0
  OUTAGE (/readyz): 46.1s  stopped->started: 39.6s; stop request->stopped: 0.3s; started->ready: 6.5s
head 3366380abca, doctor1: before MainPID=1472 NRestarts=0; doctor-exit=0 doctor-ms=60272; after MainPID=7347 NRestarts=0
  OUTAGE (/readyz): 44.9s  stopped->started: 37.6s; stop request->stopped: 0.3s; started->ready: 7.0s
head 3366380abca, doctor2: before MainPID=7347 NRestarts=0; doctor-exit=0 doctor-ms=57299; after MainPID=13319 NRestarts=0
  OUTAGE (/readyz): 39.6s  stopped->started: 32.6s; stop request->stopped: 0.3s; started->ready: 6.8s

Tests

  • New src/commands/doctor-maintenance-pending.test.ts:
    • the contract: no check means work, traced as check=default; none is the only answer that is not work; a thrown check is traced undecidable and counts as work. All are asserted on the real trace line on stderr.
    • legacy state move: work with a legacy root and no current root, none once the current root exists. The tree's bytes and mtimes are unchanged in both cases. An unreadable home is undecidable.
    • state schema: real preflight on a fixture state database gives none. The same database against a newer supported version gives work. Database family bytes and mtimes are unchanged.
    • Red check: making the default and the error path return none fails 3 of the 8 cases.
  • src/commands/doctor-sqlite-nocow.test.ts, new case: work, then none once NOCOW is set, then undecidable when lsattr is unavailable. The WAL store, its sidecars and the directory keep their bytes, mtimes and inode.
  • Focused suites on the merged head 6221503cc12, all green: src/flows/doctor-health*.test.ts, src/commands/doctor-maintenance*.test.ts, src/commands/doctor-sqlite-nocow.test.ts, src/flows/doctor-retained-runtime.test.ts (246 passed, 4 skipped; 232 passed, 11 skipped; 99 passed; 3 passed; the skips are existing platform skips). Also green: pnpm tsgo:core, pnpm tsgo:core:test, the line-cap ratchet, the session identity ratchet and git diff --check.
  • Files audited in full: both test files above. The existing NOCOW file's platform skips are justified (Linux-only /proc and fuser). Nothing was removed.
  • Review: $autoreview (Codex route, branch mode against origin/main, P0 to P2) returned scoped-clean.

Not proven

  • macOS and Windows Doctor runs, and pnpm or Git installs. The change adds only trace lines and in-memory or lstat checks there, so none of them should behave differently, but none ran.
  • The undecidable path on a host where lsattr exists but fails. The unit test covers it with a stubbed tool.
  • The cell ran on 3366380abca. The branch later merged main (unrelated changes, including fix: remove the updater copies that earlier releases leave behind after an update #206124 in retained-runtime cleanup). The focused suites were rerun on the merged head; the cell was not.

Each Doctor maintenance step now reports a read-only pending decision
(work, none or undecidable) to the startup trace. Steps without an owner
check default to work. The legacy state move, state schema migration and
SQLite NOCOW rewrite use their existing predicates. Every step still runs
and the Gateway is still stopped on every --fix.

The post-check store rewrites and runtime cleanup move to a sibling module
unchanged, keeping doctor-health.ts under its line cap.
@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. clawsweeper:intake:feature Preliminary submission classification; full source review pending. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:feature Preliminary submission classification; full source review pending. labels Nov 26, 2026
@clawsweeper

clawsweeper Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Codex review: passed.

What this changes

This PR records conservative pending-work decisions for Doctor maintenance steps while preserving existing repairs and Gateway shutdown behavior.

Example: Run openclaw doctor --fix with OPENCLAW_GATEWAY_STARTUP_TRACE=1 on a btrfs host without lsattr.

  • Before: Doctor reports that NOCOW inspection was skipped, without a pending-work classification.
  • After: The trace also records doctor.pending.sqlite-nocow with pending=undecidable check=owner.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The patch is focused and has credible CLI evidence, but the required latest-stable upgrade scenario remains unproven.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is not required for maintainer- or bot-authored pull requests.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Refactor · Worth it: Yes
User problem: Doctor stops a healthy Gateway before knowing whether maintenance needs it offline, and developers lack per-step pending-work diagnostics.
Reason: The maintainer-authored scope establishes conservative observability for the documented staged plan without prematurely changing shutdown or repair behavior.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR provides useful groundwork for reducing Doctor outages, and inspection found no introduced correctness or security defect.

Reviewed head: 6221503cc12b764cbf8a344f1a50c85f93271003

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: Provide the required v2026.11.10-to-candidate upgrade run followed by Doctor repair, showing preserved settings and data and successful Gateway restoration, with sensitive values redacted.
Agent review details

How this fits together

Doctor coordinates offline state repairs and Gateway restoration; the added checks consume existing path metadata, schema-preflight results, and NOCOW inspection results and emit startup-trace diagnostics.

flowchart TD
  A[Doctor CLI] --> B[Maintenance custody and Gateway stop]
  B --> C[Existing repair steps]
  C --> D[Pending work checks]
  D --> E[Startup trace]
  C --> F[Existing repairs and cleanup]
  F --> G[Gateway restoration]
Loading

Technical review

Best possible solution:

Retain this conservative instrumentation stage and defer conditional Gateway shutdown until the remaining repair owners can reliably identify pending work.

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

This is staged instrumentation rather than a bug fix; the supplied base and head CLI logs demonstrate the diagnostic difference.

Is this the best way to solve the issue?

Reusing the startup trace and existing owner predicates is a focused approach, and preserving unconditional repairs avoids silently dropping maintenance before detection is complete.

AGENTS.md: found and applied where relevant.

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

Provenance checked

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Pinned introduction and unchanged repair gates: The introduced hunks add tracing and extract the post-check maintenance block; NOCOW admission, reclamation selection, retained-runtime cleanup, and restoration retain their existing conditions and order. (src/flows/doctor-health-maintenance-steps.ts:25, 6221503cc12b)
  • Conservative decision contract: Missing checks return work, thrown checks become undecidable, and only none returns false; production callers do not use that return value to skip repairs. (src/commands/doctor-maintenance-pending.ts:23, 6221503cc12b)
  • Staged design and ownership: The six-step plan in Design: prepared-release updates (state, ownership and plan) #204775 (comment) explicitly stages tracing before conditional shutdown and explains why diagnosis alone would miss repairs.
  • Current main still lacks this instrumentation: Fetched main retains the original inline maintenance block and has no pending-work tracing at these call sites. (src/flows/doctor-health.ts:503, bf298a53334a)
  • Relevant production-path evidence: The PR supplies Linux systemd CLI logs with a 4 GiB database under writes, pending trace output, successful exits, and verified Gateway restoration; the relevant production files are unchanged between the exercised first commit and reviewed merge head. (6221503cc12b)
  • Media inspection limitation: The prepared manifest reports ffprobe failed: spawnSync ffprobe ENOENT for both linked recordings; browser fallback returned Cache miss, so the recordings were not visually verified and assessment uses the public raw logs.

Likely related people:

  • Jason (Json): Raw commit 8901ed7 adds src/commands/doctor-maintenance-restoration.ts:181 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 8901ed7f43bb; files: src/commands/doctor-maintenance-restoration.ts)
  • Peter Steinberger: Raw commit 3a97d3d adds src/flows/doctor-health-startup.ts:164 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 3a97d3dad150; files: src/flows/doctor-health-startup.ts)
  • Ayaan Zaidi: Raw commit 8ac1d4e adds src/flows/doctor-health-contributions.ts:714 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 8ac1d4ea0b5c; files: src/flows/doctor-health-contributions.ts)

PR surface

Source +152, Tests +165. Total +317 across 12 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 10 189 37 +152
Tests 2 166 1 +165
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 12 355 38 +317

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Review metrics

Metric Value Why it matters
Production versus test changes Production +189/-37 lines; tests +166/-1 lines Growth implements the shared decision contract, repair-site instrumentation, and distinct read-only decision tests; the extracted maintenance block preserves existing ownership.

Labels

Label changes:

  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rank-up moves

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

  • Record a v2026.11.10-to-candidate upgrade and subsequent Doctor repair with existing settings and data preserved.

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 November 26, 2026, 3:34 PM ET / 20:34 UTC.

@fuller-stack-dev
fuller-stack-dev merged commit d0f7698 into openclaw:main Nov 26, 2026
130 checks passed
@fuller-stack-dev

Copy link
Copy Markdown
Member Author

Merged via squash.

@fuller-stack-dev
fuller-stack-dev deleted the doctor-pending-step1 branch November 26, 2026 20:39
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 27, 2026
)

Each Doctor maintenance step now reports a read-only pending decision
(work, none or undecidable) to the startup trace. Steps without an owner
check default to work. The legacy state move, state schema migration and
SQLite NOCOW rewrite use their existing predicates. Every step still runs
and the Gateway is still stopped on every --fix.

The post-check store rewrites and runtime cleanup move to a sibling module
unchanged, keeping doctor-health.ts under its line cap.
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. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant