Skip to content

fix(doctor): inspection stages a full copy of the live state database on every run - #193648

Merged
fuller-stack-dev merged 2 commits into
openclaw:mainfrom
fuller-stack-dev:fix/doctor-inspect-live-wal-in-place
Nov 5, 2026
Merged

fuller-stack-dev merged 2 commits into
openclaw:mainfrom
fuller-stack-dev:fix/doctor-inspect-live-wal-in-place

Conversation

@fuller-stack-dev

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

Copy link
Copy Markdown
Member

What Problem This Solves

Fixes: Doctor's database inspection writes a full private copy of the live shared-state database to the cache on every run, only to read a few rows of metadata. On one Mac host the state database was 12.6 GB, so each openclaw doctor or openclaw triage run staged another 12.6 GB copy, and interrupted runs left those copies behind (on Nov 4 they used about 49 GB together with agent-database copies).

User Impact

When the Gateway is running, Doctor now inspects the shared-state database in place and stages no copy of it. In the test below, a 6.1 MB live WAL source went from 6,128,256 staged bytes per inspection to 0, with an identical inspection result. Doctor still uses a private copy whenever reading in place would be unsafe or unproven.

Why This Change Was Made

Doctor's per-store inspection reads only the engine marker, schema facts, the state content version and the Gateway lease row. When the source is a live WAL database with both -wal and -shm present and a verified live Gateway owner, the existing read-only child now runs that inspection on the source itself: a mode=ro connection inside one read transaction (tryInspectSqliteReadOnlyInProcess, the same path startup metadata reads use). The child is a separate process, so closing the source cannot release the Gateway's POSIX locks. A WAL reader takes no lock that a writer's commit waits on, and a read-only connection cannot write the database or its WAL.

Everything else keeps today's private copy:

  • no verified live owner, including an idle or crashed WAL;
  • a WAL without its -shm index, which SQLite would have to rebuild;
  • rollback-journal sources, hot journal or not, because a rollback reader's SHARED lock would hold off the writer's commit;
  • stores whose inspection depends on a port descriptor's inspectSqliteSource. Only the shared-state database has a Gateway owner record, and its inspection does not use the descriptor;
  • any failure of the in-place attempt.

The inspection steps moved unchanged into sqlite-store-inspection.ts, so the copy path and the in-place path run the same code on one read-only connection.

Not in this PR:

  • Unowned, cleanly closed sources (immutable=1). This needs a fence against a Gateway starting mid-read and will come as a separate PR.
  • APFS clones for the remaining copies. I'm keeping clone: "never". The cases that still copy are mostly WAL or journal families that need private recovery, and recovery writes into the copy anyway. A clone also leaves the Gateway's database sharing extents with the copy, so the Gateway's later writes copy-on-write and fragment its own file long after Doctor has exited. Removing the copy entirely, as here and in the follow-up, is the better saving.
  • Agent databases. They have no per-database owner record, so a live in-place read of an agent database needs its own liveness proof first.

Evidence

Red ran on 480d50305383. The branch is rebased on a54bdb3c608f; the commits in between touch none of the inspection, read-only child, owner or snapshot paths, and the focused tests were rerun after the rebase.

Red on base, pnpm exec vitest run src/infra/database-engine/diagnostics.test.ts:
reads a verified live WAL source in place instead of copying it failed with expected [ 6128256, 6128256 ] to deeply equal [ Any<Number> ]. Both inspections of a live 6.1 MB source (1,892,352-byte database + 4,235,392-byte WAL) staged a 6,128,256-byte copy.

Green on head, same file: 29 passed.

  • Live WAL, verified owner: the forced-copy reference run stages 6,128,256 bytes; the in-place run stages nothing; the two DatabaseStoreDiagnostic results are equal; the database and WAL SHA-256 are unchanged.
  • Live writer: inside the open read transaction, a separate process commits with busy_timeout=0. The commit succeeds and is visible afterwards.
  • Negative controls, each still staging a copy larger than 4 MB with source bytes unchanged: an unowned live WAL, a WAL whose -shm is missing after the writer was killed, and a hot rollback journal left by a killed writer.

Other checks:

  • test/scripts/pr-wrapper-source-closure.test.ts and test/scripts/eager-import-closure.test.ts: pass, with the two new modules added to the wrapper inventory.
  • Related suites (read-only worker operations and session, startup metadata live read, admitted diagnostics, every src/commands/doctor-database*.test.ts): all pass except doctor-database-diagnostics.test.ts's snapshot. That snapshot fails identically on unmodified main on this Mac because it picks up a real managed-update-handoffs.sqlite in the shared /tmp/openclaw-<uid> directory.
  • pnpm check:changed: format and lint pass after fixes. Remaining local failures are unrelated to this diff: changelog attributions (in CHANGELOG/records/2026.11.1.md on main), dead exports (in src/gateway and src/cron), and the tsgo wrappers, which refuse this checkout's shared pnpm store. Running the compiler directly (tsrs -p tsconfig.core.json and -p test/tsconfig/tsconfig.core.test.infra.json) reports one error, in packages/ai/src/transports/openai-responses-websocket.ts, which unmodified main reports too. CI's typecheck is the gate.
  • scripts/check-madge-import-cycles.ts: 0 cycles. The first push had one, because the read-only operation registry imported the live reader's type; the registry now reads a leaf contract (database-store-live-read.worker-contract.ts), and BackupEngine moved to the leaf database-engine/types.ts. Runtime import cycles: 0.
  • pnpm check:test-lines-ratchet: passes; the +147 lines are allocated in config/test-lines-allocations/doctor-inspect-live-wal-in-place.txt.
  • Autoreview (Codex gpt-6.1-sol, branch mode against origin/main, P0–P2): scoped-clean on both commits.

Test file audit (src/infra/database-engine/diagnostics.test.ts, read in full): I removed a prepareSqliteReadOnlyLocation spy that only passed through to the real function and was never asserted. Every other case covers distinct behaviour and is unchanged. The new tests own their temp directories, writer processes and owner record, and clean them up through onTestFinished.

@clawsweeper

clawsweeper Bot commented Nov 5, 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 scripts Repository scripts size: M labels Nov 5, 2026
…ull copy

Doctor's per-store inspection copied every SQLite source into the snapshot
cache before reading its engine marker, schema facts, content version and
lease row. A verified live WAL source is now read in place by the existing
read-only child inside one read transaction; every other source, and any
failure, keeps the private copy.
…y's import graph

The read-only operation registry imported the live reader's type, which pulled
the snapshot and worker graph back into the registry and formed an import cycle.
The registry now reads a leaf contract, and BackupEngine lives with the other
leaf engine types.
@fuller-stack-dev
fuller-stack-dev force-pushed the fix/doctor-inspect-live-wal-in-place branch from 7bf20df to 5245a19 Compare November 5, 2026 18:08
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 5, 2026
@clawsweeper

clawsweeper Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Doctor inspects verified live WAL databases through the existing isolated reader instead of staging a full private copy.

Example: Inspect a verified live shared-state database with a 6,128,256-byte database and WAL family.

  • Before: Each inspection stages 6,128,256 bytes.
  • After: The supplied regression reports zero staged bytes and an identical diagnostic result.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Useful, focused implementation with meaningful boundary tests and no actionable defect found; confidence in shipped behavior remains limited to the supplied harness evidence.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored PR is outside the ordinary contributor proof gate; supplied evidence exercises the diagnostic owner with real SQLite processes and reports eliminated staging, writer progress and recovery controls, but remains test-harness evidence.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Running diagnostics against a large live state database consumes substantial temporary disk space and I/O.
Reason: The maintainer-authored direction delivers a concrete resource saving while deliberately preserving unsafe-source and descriptor-dependent fallback behavior.

Merge readiness

✅ Ready for maintainer review

Keep this PR open: it addresses unnecessary database copying still present on main, preserves recovery fallbacks, and introduces no definite correctness defect found in this review.

Priority: P2
Reviewed head: 5245a1981f80bfbdc75dfd9fb9f00e31e4f41f85

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: The supplied evidence does not include a shipped Doctor or triage run observing eliminated staging against a verified live Gateway.
Agent review details

How this fits together

Doctor supplies store paths and expected schema facts to database diagnostics, which inspect SQLite or PostgreSQL metadata and return health findings and recovery guidance.

flowchart TD
  A[Doctor inspection] --> B[Store diagnostics]
  B --> C[Verified live WAL check]
  C --> D[Isolated read transaction]
  C --> E[Private copy fallback]
  D --> F[Shared metadata inspection]
  E --> F
  F --> G[Diagnostic result]
Loading

Technical review

Best possible solution:

Use the existing isolated reader for verified live WAL inspection, share metadata interpretation with the copy path, and retain private recovery for all other sources.

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

Current main visibly stages every inspected SQLite store, and the PR supplies a reported red/green regression using a real WAL writer; this read-only review executed no reproduction.

Is this the best way to solve the issue?

The change removes unnecessary copying at the diagnostic owner, preserves metadata semantics, and reuses established transport and transaction machinery without introducing persisted state.

AGENTS.md: found and applied where relevant.

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

Provenance checked

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 6cf7c47 adds src/infra/database-engine/diagnostics.ts:185 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 6cf7c4775738; files: src/infra/database-engine/diagnostics.ts)
  • Vincent Koc: Raw commit f61cb97 adds src/infra/database-engine/diagnostics.test.ts:291 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: f61cb97d8250; files: src/infra/database-engine/diagnostics.test.ts)

PR surface

Source +118, Tests +147, Other +4. Total +269 across 10 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 7 235 117 +118
Tests 1 152 5 +147
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 2 4 0 +4
Total 10 391 122 +269

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.

Labels

Label changes:

  • add P2: Repeated multi-gigabyte staging creates meaningful disk and I/O overhead in operator diagnostics, with a focused implementation boundary.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp 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:

  • P2: Repeated multi-gigabyte staging creates meaningful disk and I/O overhead in operator diagnostics, with a focused implementation boundary.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp 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.

  • Show a shipped Doctor inspection against an isolated live Gateway with zero staging and unchanged diagnostic output.

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 5, 2026, 1:25 PM ET / 18:25 UTC.

@fuller-stack-dev
fuller-stack-dev merged commit f7e58b8 into openclaw:main Nov 5, 2026
162 checks passed
@fuller-stack-dev

Copy link
Copy Markdown
Member Author

Merged via squash.

@fuller-stack-dev
fuller-stack-dev deleted the fix/doctor-inspect-live-wal-in-place branch November 5, 2026 19:09
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 6, 2026
… on every run (openclaw#193648)

* fix(doctor): inspect a live shared-state database without staging a full copy

Doctor's per-store inspection copied every SQLite source into the snapshot
cache before reading its engine marker, schema facts, content version and
lease row. A verified live WAL source is now read in place by the existing
read-only child inside one read transaction; every other source, and any
failure, keeps the private copy.

* fix(doctor): keep the live-read operation contract out of the registry's import graph

The read-only operation registry imported the live reader's type, which pulled
the snapshot and worker graph back into the registry and formed an import cycle.
The registry now reads a leaf contract, and BackupEngine lives with the other
leaf engine types.
wangmiao0668000666 pushed a commit to wangmiao0668000666/openclaw that referenced this pull request Nov 6, 2026
… on every run (openclaw#193648)

* fix(doctor): inspect a live shared-state database without staging a full copy

Doctor's per-store inspection copied every SQLite source into the snapshot
cache before reading its engine marker, schema facts, content version and
lease row. A verified live WAL source is now read in place by the existing
read-only child inside one read transaction; every other source, and any
failure, keeps the private copy.

* fix(doctor): keep the live-read operation contract out of the registry's import graph

The read-only operation registry imported the live reader's type, which pulled
the snapshot and worker graph back into the registry and formed an import cycle.
The registry now reads a leaf contract, and BackupEngine lives with the other
leaf engine types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. scripts Repository scripts size: L 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