Skip to content

fix(sqlite): Gateway never frees snapshot copies left in other cache namespaces - #193652

Merged
fuller-stack-dev merged 4 commits into
openclaw:mainfrom
fuller-stack-dev:fix/sqlite-snapshot-stranded-namespace-sweep
Nov 5, 2026
Merged

fuller-stack-dev merged 4 commits into
openclaw:mainfrom
fuller-stack-dev:fix/sqlite-snapshot-stranded-namespace-sweep

Conversation

@fuller-stack-dev

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

Copy link
Copy Markdown
Member

Related: #193174, #193229

What Problem This Solves

Fixes: SQLite snapshot copies already left under ~/Library/Caches/openclaw/ by earlier Doctor or openclaw triage runs are never deleted, because they sit outside the namespace the Gateway sweeps.

On one Mac host, two triage runs left 49 GB of copies in sqlite-0e5d8581… and sqlite-ac549b06…, beside the installation's own sqlite-9565507d… namespace. Older releases also left flat openclaw-sqlite-readonly-v2-* directories directly under the cache parent.

User Impact

The Gateway's existing maintenance sweep now also frees snapshot copies stranded in sibling sqlite-<hash> namespaces and directly under the shared cache parent, once they have been idle for a day. Copies that a live process still owns or reads are kept, as before.

Why This Change Was Made

PR #193229 stops new copies from landing in per-run namespaces, and #193174 fixes the cleanup failure on cancellation. Neither one removes copies that are already on disk.

After the pass over its own namespace, reclaimAbandonedSqliteSnapshots now lists the cache parent. It runs the same staging-sweep worker on each sibling sqlite-<24 hex> namespace and on the parent itself, which holds the flat openclaw-sqlite-readonly-v2-* entries. The worker accepts exactly those roots: its own namespace, a sibling namespace, or their shared parent.

Liveness is unchanged:

  • Every nested owner.sqlite must be taken with BEGIN EXCLUSIVE and timeout: 0.
  • allowLegacy: false still rejects pre-token layouts.
  • Anything with an unknown owner or layout is skipped.

Copies outside this installation's namespace must be idle for 24 hours across the whole tree (reusing SQLITE_SNAPSHOT_LEGACY_AGE_MS), because another installation may be between allocation and first use. The installation's own namespace keeps its 15-minute grace.

The sweep is best-effort. Errors on a sibling root, including an unreadable one, are counted as skipped and never thrown, and empty namespace directories are left in place. The own-namespace pass behaves as before.

Evidence

Compatibility with copies left by the latest stable release (v2026.11.1, 4ffdd2047d19). In a disposable HOME, cache and state dir, that tag's own staging code made two nested copies directly under <cache>/openclaw/, which is where v2026.11.1 stages them (it has no sqlite-<hash> namespaces):

  • one whose process was SIGKILLed while holding both creator tokens, which leaves an empty owner.sqlite and a 512-byte -journal at each level, as on the affected host;
  • one whose v2026.11.1 process stays alive holding them.

The directory also held a tokenless openclaw-sqlite-readonly-v2-* directory, a pre-token openclaw-sqlite-readonly-<pid>-* directory, and a nested copy outside the sweep roots. Everything was aged to 25 hours, then swept:

Sweep Result Left on disk
current origin/main { removed: 0, bytes: 0, skipped: 0 } everything
this branch { removed: 1, bytes: 73728, skipped: 2 } the live copy, the tokenless and pre-token directories, the outside copy
this branch, after the live v2026.11.1 process exits (re-aged) { removed: 1, bytes: 73728, skipped: 1 } the tokenless and pre-token directories, the outside copy

Head d4b60a86e0c0, which contains origin/main 8eb01599c83a. The red/green runs below used base e0466ffe4d11; after merging main (whose only change on these paths adds a read-only lookup to tmp-openclaw-dir.ts), the reclamation and temp-root tests were rerun: 29 passed.

Regression tests in the new src/infra/sqlite-snapshot-reclamation.test.ts drive reclaimAbandonedSqliteSnapshots through the real staging-sweep worker process, with real staging tokens and nested copies whose child creator has exited:

  • "reclaims day-old copies stranded beside this installation's namespace": a 25-hour-old copy in a sibling namespace, one directly under the cache parent, and a 1-hour-old copy in the installation's own namespace are all removed. Both namespace directories remain.
    • base: fails (removed: 1; only the own-namespace copy)
    • this branch: passes
  • "keeps stranded copies that are recent, held, or carry no lifetime token" (negative controls): a 1-hour-old sibling copy, a copy whose creator token is live, a copy with a live read token, a tokenless openclaw-sqlite-readonly-v2-* directory, and an unreadable sibling namespace give { removed: 0, bytes: 0, skipped: 5 }, and every directory stays. After the creator and reader release, the same two copies are reclaimed.
    • base: fails (skipped: 0; siblings are never inspected)
    • this branch: passes

Commands:

  • vitest run src/infra/sqlite-snapshot-reclamation.test.ts src/infra/sqlite-snapshot-reclamation.process.test.ts: 3 passed. The existing process test (a killed byte-copy creator in the own namespace) is unchanged.
  • pnpm check:changed: format, lint (core and scripts), runtime import cycles and the other guards pass. Two steps fail for reasons outside this change. The dead-export scan reports existing unused exports in src/gateway/auth.ts, src/gateway/github-publication-workflows.ts and a cron test-support file, none of them touched here. The local tsgo typecheck refuses this checkout's shared pnpm store ("Declaration input escapes checkout"), so CI's typecheck is the gate for that.
  • Test-lines ratchet: net growth +105, recorded exactly in config/test-lines-allocations/sqlite-snapshot-stranded-namespace-sweep.txt. The check passes (slack 11).
  • vitest run on the 26 test files that import reclamation, retirement, the read-only worker or Gateway maintenance: 435 passed, 1 skipped. One suite, src/cli/update-cli/update-command-post-core-admission.process.test.ts, fails at setup because it expects a built candidate dist worker. It fails the same way in a checkout without this change.
  • Autoreview (Codex, branch mode against origin/main, P0–P2): scoped-clean on 0701b48827cc.

Docs: docs/reference/database-schemas/integrity-and-recovery.md now describes the sibling-namespace and cache-parent sweep, its 24-hour grace beside the own namespace's 15 minutes, and the layouts that still need offline cleanup.

Test file audit: src/infra/sqlite-snapshot-reclamation.test.ts is new. It owns its temp root through the shared tracker, stubs only XDG_CACHE_HOME and OPENCLAW_STATE_DIR (restored after each test), restores the unreadable directory's mode before cleanup, and releases every token it takes. src/infra/sqlite-snapshot-reclamation.process.test.ts was read and is unchanged.

… namespace

Doctor's private state copy and older releases staged SQLite snapshots in
sibling sqlite-<hash> namespaces or directly under the shared cache parent.
The Gateway sweep only looked in its own namespace, so those copies stayed
forever. Sweep them too after a day of inactivity, using the same lifetime
tokens that fence live creators and readers.
@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.

@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. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. 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

Extend Gateway snapshot cleanup to abandoned token-bearing copies in sibling cache namespaces and the shared cache parent after 24 hours.

Example: A killed v2026.11.1 process leaves a nested snapshot directly under the shared cache parent, idle for 25 hours.

  • Before: Gateway maintenance never visits the copy.
  • After: Maintenance reclaims it while preserving copies held by live creators or readers.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Focused, correct patch with meaningful native-worker coverage and stable-artifact compatibility evidence; supplied execution evidence remains below the shipped Gateway entry point.
Proof confidence 🦐 gold shrimp (3/6) The ordinary contributor proof gate does not apply to this maintainer-authored PR; supplied isolated-cache results exercise stable-release creator tokens and the production reclamation owner, including killed and live creators and protected outside-root controls.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Large disposable SQLite copies remain indefinitely after Doctor or triage runs.
Reason: The reported disk consumption warrants recovery, and the maintainer-authored direction explicitly accepts cleanup across same-user cache namespaces with conservative lifetime checks.

Merge readiness

✅ Ready for maintainer review

This PR addresses a storage leak that remains on current main; the previous documentation finding is fixed, and no actionable introduced defect remains.

Priority: P1
Reviewed head: d4b60a86e0c0a8207a035a2ba250459cf2f1e475

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: The supplied evidence does not show the delayed Gateway maintenance entry point sweeping sibling and flat stable-release copies.
Agent review details

How this fits together

Gateway maintenance discovers cached SQLite snapshots, delegates reclamation to the existing native worker, and records removed bytes and retained directories.

flowchart TD
  A[Gateway maintenance] --> B[Discover eligible cache roots]
  B --> C[Native staging sweep worker]
  C --> D[Check age ownership and nested tokens]
  D --> E[Retain live or unknown copies]
  D --> F[Retire abandoned snapshot payloads]
  F --> G[Report reclaimed bytes]
Loading

Technical review

Best possible solution:

Use the existing retirement worker to reclaim eligible abandoned copies while preserving live tokens, unknown layouts, primary databases, and backups.

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

Current-main source establishes the missed-root path, and the supplied base/head results demonstrate stranded copies surviving before the change; this review ran no reproduction.

Is this the best way to solve the issue?

The change is focused: discovery stays with Gateway maintenance, deletion stays with the existing retirement owner, and both new test groups protect distinct observable failures.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Installation-only reclamation and shared-cache exclusion changes intended behavior with a stated reason (c16858b: Automatic cleanup was limited to installation-attributable snapshots; shared-cache copies remained offline maintenance.)
  • Retirement age and token protocol keeps the original intent (e68dfaf: Retirement preserves live snapshot custody and bounds payload release; this PR exports the existing age constant without changing that protocol.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit c16858b adds src/infra/sqlite-snapshot-reclamation.ts:12 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: c16858baa256; files: src/infra/sqlite-snapshot-reclamation.ts)

PR surface

Source +45, Tests +105, Docs +8, Other +2. Total +160 across 5 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 2 59 14 +45
Tests 1 105 0 +105
Docs 1 11 3 +8
Config 0 0 0 0
Generated 0 0 0 0
Other 1 2 0 +2
Total 5 177 17 +160

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 regression coverage Production net +45 lines; tests +105 lines Growth extends the existing sweep owner and adds distinct reclamation and retention controls without introducing another cleanup subsystem.

Labels

Label changes:

  • 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.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🦐 gold shrimp, so this older rating label is no longer current.
  • remove status: ⏳ waiting on author: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P1: The reported Doctor and triage leftovers consumed 49 GB, and current main cannot automatically recover that space.
  • 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.

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 (1 earlier review cycle)
  • reviewed 2026-11-05T18:23:57.485Z sha b48581c :: blocked before merge. :: [P3] Update the recovery guide for cross-namespace reclamation

Reviewed November 5, 2026, 2:31 PM ET / 19:31 UTC (Revision 2).

@fuller-stack-dev

Copy link
Copy Markdown
Member Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. 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 rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 5, 2026
@fuller-stack-dev
fuller-stack-dev merged commit 5e0d1bf into openclaw:main Nov 5, 2026
147 of 148 checks passed
@fuller-stack-dev

Copy link
Copy Markdown
Member Author

Merged via squash.

@fuller-stack-dev
fuller-stack-dev deleted the fix/sqlite-snapshot-stranded-namespace-sweep branch November 5, 2026 19:56
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 6, 2026
…namespaces (openclaw#193652)

* fix(sqlite): reclaim snapshot copies stranded beside the installation namespace

Doctor's private state copy and older releases staged SQLite snapshots in
sibling sqlite-<hash> namespaces or directly under the shared cache parent.
The Gateway sweep only looked in its own namespace, so those copies stayed
forever. Sweep them too after a day of inactivity, using the same lifetime
tokens that fence live creators and readers.

* docs: describe the cross-namespace snapshot sweep in the recovery guide
wangmiao0668000666 pushed a commit to wangmiao0668000666/openclaw that referenced this pull request Nov 6, 2026
…namespaces (openclaw#193652)

* fix(sqlite): reclaim snapshot copies stranded beside the installation namespace

Doctor's private state copy and older releases staged SQLite snapshots in
sibling sqlite-<hash> namespaces or directly under the shared cache parent.
The Gateway sweep only looked in its own namespace, so those copies stayed
forever. Sweep them too after a day of inactivity, using the same lifetime
tokens that fence live creators and readers.

* docs: describe the cross-namespace snapshot sweep in the recovery guide
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. 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.

1 participant