Skip to content

fix(doctor): avoid inherited state maintenance during repair - #166413

Closed
dlmccoy wants to merge 7 commits into
openclaw:mainfrom
dlmccoy:fix/doctor-lease-archive-resume
Closed

dlmccoy wants to merge 7 commits into
openclaw:mainfrom
dlmccoy:fix/doctor-lease-archive-resume

Conversation

@dlmccoy

@dlmccoy dlmccoy commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

openclaw doctor --fix can lose its agent-database maintenance lease when a shared-state handle left open by preflight keeps its optional WAL maintenance worker alive during Doctor's exclusive repair.

User Impact

Doctor now drains that inherited shared-state maintenance before it opens Doctor-scoped resources. The offline repair no longer competes with itself for the state store.

Why This Change Was Made

The fix closes the cached preflight handle only after Doctor has acquired exclusive maintenance ownership, then reasserts that ownership before proceeding. It deliberately does not change SQLite busy-timeout or lease-duration behavior.

Evidence

  • New regression is red on upstream main: the inherited shared-state handle remains open after maintenance.acquire().
  • With this change: pnpm test src/commands/doctor-maintenance.sqlite-reclamation.test.ts --maxWorkers=1 — 2 tests passed (20.25s wall time).
  • pnpm build — passed (3m 57s).
  • Local P0–P2 autoreview — scoped-clean; no actionable findings.
  • pnpm check could not complete because of pre-existing max-lines-baseline shrink requirements in unrelated files and spawnSync git ENOBUFS; this focused PR does not alter those surfaces.

Scope

This intentionally does not add archive-resume cursoring. That path needs a separate design because a partial cursor could bypass canonical-file exclusion and verification receipt semantics.

@clawsweeper

clawsweeper Bot commented Oct 7, 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.

@clawsweeper

clawsweeper Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Doctor settles inherited shared-state WAL maintenance before opening repair resources while preserving independent readers.

Example: Run openclaw doctor --fix with a shared-state handle retained by preflight.

  • Before: Its WAL worker can compete with Doctor's exclusive repair and cause lost-maintenance-lease failures.
  • After: Doctor joins the inherited WAL work, closes its cached handle, and proceeds under revalidated maintenance ownership.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) A focused, useful repair with real runtime and upgrade evidence, meaningful lifecycle coverage, and no open finding; current-head CI remains pending.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): Recorded production WAL execution and successful real CLI Doctor repair exercise the changed cache owner; published-updater evidence additionally records retained settings and data with healthy Gateway startup, while remaining attributed to its original candidate.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Doctor repair can fail because preflight's retained shared-state WAL worker competes with its own exclusive maintenance.
Reason: The narrow change restores reliable repair without adding configuration, changing permissions, or replacing stored-data contracts.

Merge readiness

✅ Ready for maintainer review

This PR provides a useful, bounded Doctor fix that current main still lacks; no actionable patch defect was found.

Priority: P2
Reviewed head: ad2d75cd642c06d499d58903242ba71478fad3dd

Before merge

None.

Findings

None.

Agent review details

How this fits together

Doctor's offline maintenance owner receives the selected state directory and requester authority, settles inherited SQLite work, and admits scoped repair resources.

flowchart TD
A[Doctor preflight] --> B[Cached shared state handle]
C[Exclusive maintenance owner] --> D[Settle inherited WAL work]
B --> D
D --> E[Retire cached handle]
E --> F[Revalidate ownership]
F --> G[Admit Doctor repair resources]
Loading

Technical review

Best possible solution:

Land the focused cached-owner settlement fix through the normal workflow once current-head CI completes.

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

Source shows current main admitting repair resources while the inherited cached WAL owner remains live; the contributor reports a failing baseline regression and supplies real-path observations, but this review ran no reproduction.

Is this the best way to solve the issue?

The repair uses the existing cache and maintenance owners, joins accepted WAL work, preserves independent readers, and retains requester and physical-ownership checks.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Doctor maintenance custody keeps the original intent (5b3fd54: Retain exclusive maintenance ownership across legacy state-directory relocation and repair.)
  • Independent database clients keeps the original intent (cb24f20: Doctor cleanup must preserve existing and independently admitted clients.)
  • WAL retirement ordering keeps the original intent (70ca834: Join accepted asynchronous WAL work before checkpointing and closing native handles.)
  • Path-based physical-handle cleanup changes intended behavior with a stated reason (7fcf912: Shared-state lifecycle owns physical identity and resource drainage; the PR explains using the cached identity to preserve alias cleanup without another reader admission.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 5b3fd54 adds src/commands/doctor-maintenance-state.ts:50 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 5b3fd542e9f4; files: src/commands/doctor-maintenance-state.ts)
  • Dallin Romney: Raw commit 93e8737 adds src/commands/doctor-maintenance-state.ts:65 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 93e87378063a; files: src/commands/doctor-maintenance-state.ts)

PR surface

Source +27, Tests +109. Total +136 across 6 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 32 5 +27
Tests 4 115 6 +109
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 147 11 +136

Labels

Label changes:

No label changes.

Label justifications:

  • P2: A bounded Doctor repair failure caused by inherited SQLite maintenance warrants normal bug-fix priority.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

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.

History

Review history (13 earlier review cycles; latest 8 shown)
  • reviewed 2026-10-07T18:35:32.048Z sha 11b85e2aeffdc6ed7cdae5ea849195995c3671ea :: needs real behavior proof before merge. :: [P2] Drain the cached WAL owner without closing independent readers
  • reviewed 2026-10-07T18:47:32.227Z sha 68f3922399af9dfe80b22c8fcf80d27300986093 :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-08T22:38:55.352Z sha 68f3922399af9dfe80b22c8fcf80d27300986093 :: needs changes before merge. :: [P2] [P2] Preserve physical identity when closing a cached pathname
  • reviewed 2026-10-08T22:53:08.746Z sha 2bdb438ee7c1a06c8fbb02ebf87dc49ce8739db1 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-10T13:12:46.699Z sha 7d053a6a4d87c19ce7ee070297a3c1592d8b2fed :: needs maintainer review before merge. :: none
  • reviewed 2026-10-10T13:30:40.093Z sha da8d37bbf1792ebdd565c156103d93693abf639d :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-10T20:23:42.540Z sha 55a8afb5daca1e9416126f007a555c5256f73c67 :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-10T22:10:17.063Z sha 55a8afb5daca1e9416126f007a555c5256f73c67 :: needs maintainer review before merge. :: none

Reviewed October 12, 2026, 9:40 PM ET / October 13, 2026, 01:40 UTC (Revision 14).

@dlmccoy

dlmccoy commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

After-fix runtime proof

I ran the patched candidate at 92a35ba26ec01ef372e604032d836d1e945bd150 through the real CLI entry point:

openclaw doctor --fix --non-interactive --no-workspace-suggestions

Setup: a complete isolated snapshot of the affected Mac state and configuration, using the candidate's Node 24 runtime. I quarantined only the snapshot's copied gateway-owner artifacts before the run; those artifacts referenced the still-running live gateway PID, while the original incident occurred after the gateway was stopped. Live state and the live Gateway were not modified or stopped.

Observed result: exit status 0; Doctor reached Doctor complete. The run wrote agent-database clean-close receipts, and a post-run SQLite query reported 0 remaining copied agent-handle leases. The output contained no OpenClawStateLeaseError, no lost-maintenance-lease message, and no state-owner contention after the snapshot-owner preparation.

Scope note: the snapshot reported unrelated cron-authority and skill-path warnings caused by its intentionally isolated paths; Doctor continued and completed. They are not attributed to this change.

This is supplemental runtime evidence in addition to the focused real-SQLite regression and full build already recorded in the PR body.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 7, 2026
@dlmccoy
dlmccoy marked this pull request as ready for review October 7, 2026 04:22
@dlmccoy
dlmccoy force-pushed the fix/doctor-lease-archive-resume branch from 92a35ba to d2714a6 Compare October 7, 2026 06:55
@clawsweeper clawsweeper Bot added merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. 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 proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 7, 2026
@dlmccoy

dlmccoy commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Current-head WAL-settlement evidence

Updated head: 11b85e2aeffdc6ed7cdae5ea849195995c3671ea.

  • Doctor now awaits closeOpenClawStateDatabaseByPathAsync while it holds its maintenance owner. This retires the inherited cache owner at the exact state-db path and joins its WAL cancellation before Doctor-scoped resources open.
  • The focused real-SQLite regression deliberately holds the inherited WAL cancellation promise. While it is held, maintenance.acquire() cannot advance and the inherited native handle remains open; after release, the inherited handle is closed and a Doctor-scoped replacement is usable.
  • The companion regression confirms a separately managed resource remains usable while Doctor drains the custom agent handle.

Verification on this exact head:

pnpm test src/commands/doctor-maintenance.sqlite-reclamation.test.ts --maxWorkers=1
2 tests passed

pnpm format:check
passed

pnpm build
passed

I also ran the source CLI against an isolated copied Mac-state snapshot after quarantining only copied gateway-owner and agent-handle lease artifacts. It entered Doctor and performed recovery work, but the copied snapshot contains an unrelated malformed agent database; Doctor correctly refused that migration. I am not presenting that as a successful end-to-end proof. The deterministic regression above covers the requested pending-inherited-WAL settlement boundary on this head.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@dlmccoy

dlmccoy commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Addressed ClawSweeper P2 in 68f3922399af9dfe80b22c8fcf80d27300986093.

Doctor now settles and retires only the inherited cached WAL owner before repair admission; it no longer performs database-wide async resource drainage. The new regression keeps an independent same-database current reader open through Doctor acquisition/release and verifies it remains usable afterward. The existing delayed-cancellation regression still proves Doctor waits for inherited WAL settlement before opening repair resources.

Verification: node scripts/run-vitest.mjs run src/commands/doctor-maintenance.sqlite-reclamation.test.ts (3 passed); direct changed-file oxlint passed.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot removed the merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. label Oct 7, 2026
@dlmccoy

dlmccoy commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up evidence for the current head 68f3922399af9dfe80b22c8fcf80d27300986093:

I reran the real-path proof in a fresh isolated state directory using the current source tree. It opens the actual shared-state SQLite/WAL handle, writes a pending record, invokes the real periodic WAL maintenance path, then enters Doctor’s exclusive maintenance owner and runs the real SQLite reclamation repair. No mocked cancellation/delay is involved.

Redacted observation:

{
  "walBytes": 1499712,
  "databaseBytes": 1470464,
  "logFrames": 364,
  "checkpointedFrames": 364,
  "maintenanceState": "complete",
  "inheritedSharedStateClosedBeforeRepair": true,
  "repair": {
    "autoVacuum": 2,
    "retainedValue": "preserved",
    "integrityCheck": "ok"
  }
}

The proof asserts all of the following before succeeding: the WAL maintenance manager remains owned while checkpointing; its inherited cached shared-state handle is closed before Doctor opens exclusive repair resources; the repaired database has incremental auto-vacuum (2), retains the seeded row, and passes PRAGMA integrity_check.

Separately, I ran the current source doctor --fix --non-interactive --no-workspace-suggestions under a copied config and isolated state while the managed Gateway was intentionally stopped by a non-respawning launchd one-shot; it completed with exit 0, then the job restored the original managed Gateway and the post-restore probe passed. Expected gateway-unreachable diagnostics occurred only during that intentional stop.

Please re-review this current head; the previous evidence comment did not demonstrate a real pending WAL settlement.

@dlmccoy

dlmccoy commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Current-head redacted real-path WAL-settlement and successful-repair proof is in #166413 (comment).

@clawsweeper

clawsweeper Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper

clawsweeper Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added 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 proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 10, 2026
@dlmccoy
dlmccoy force-pushed the fix/doctor-lease-archive-resume branch from da8d37b to 55a8afb Compare October 10, 2026 20:15
@dlmccoy

dlmccoy commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

Published-updater compatibility proof (redacted)

Validated on the current PR head 55a8afb5daca1e9416126f007a555c5256f73c67 with:

pnpm test:docker:published-upgrade-survivor

Result: passed (the selected lane completed in 755s).

  • Started from the published stable baseline: openclaw@latest → 2026.9.9.
  • Seeded the harness’s existing-user fixture (gateway/token reference, model-provider references, agent/workspace state, skills, plugin/channel configuration, and logs); no live credentials or provider calls were supplied.
  • Used the real published updater to install the packed candidate from this exact PR head; installed candidate identity matched.
  • Non-interactive Doctor passed, then post-Doctor config validation and the preservation assertions passed.
  • Gateway startup passed; /healthz in 2s, /readyz in 1s, and RPC status in 4s.
  • The run also passed the legacy plugin-dependency cleanup and runtime-deps-symlink repair assertions.

The generated summary is redacted (only fixture references, no credential values, personal IDs, or external URLs) and records status: passed, updateOutcome: success, and candidateInstallMode: updater.

@dlmccoy

dlmccoy commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

The requested redacted published-updater compatibility proof for the current head is here: #166413 (comment)

@clawsweeper

clawsweeper Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed 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. labels Oct 10, 2026
@dlmccoy
dlmccoy force-pushed the fix/doctor-lease-archive-resume branch from 55a8afb to ad2d75c Compare October 13, 2026 01:31
@dlmccoy
dlmccoy force-pushed the fix/doctor-lease-archive-resume branch 2 times, most recently from 4212541 to e252968 Compare October 20, 2026 16:34
@dlmccoy
dlmccoy force-pushed the fix/doctor-lease-archive-resume branch from 10f88e8 to f07c444 Compare October 20, 2026 17:54
@steipete

Copy link
Copy Markdown
Contributor

Thank you @dlmccoy! The WAL maintenance owner already prevents inherited timers from borrowing Doctor's authority (8b16e53), and #170022 suspends shared-worker timers during maintenance, so Doctor keeps its lease and repair continues. Closing since current main already covers this; please reopen with a reproduction on a build that includes it if you still see the problem.

@steipete steipete closed this Oct 22, 2026
@dlmccoy
dlmccoy deleted the fix/doctor-lease-archive-resume branch October 22, 2026 02:38
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. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: S 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.

3 participants