Skip to content

fix(update): prevent idle agent cleanup outside maintenance scope - #186783

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-migrate-agent-db-close-scope-20260919
Oct 28, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-migrate-agent-db-close-scope-20260919

Conversation

@steipete

@steipete steipete commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes agent database cleanup warnings during native update/Doctor maintenance lasting longer than the one-shot CLI's 30-second idle window.

User Impact

Maintenance retains its agent database handles until scoped cleanup can checkpoint, close, and release their leases with the required authority. No schema, configuration, retention policy, or authorization changes are required. The candidate runtime must contain this fix; the installed updater needs no change.

Why This Change Was Made

Agent handles already belong to the maintenance resource scope, but their detached idle timer also attempted to close them. The timer checkpointed and closed SQLite before its shared-state lease release failed with StateDatabaseMaintenanceScopeError. A later scoped retry could clear the lease but could no longer publish the original clean-close seal.

Idle eviction now consults the existing resource owner and leaves maintenance handles to that scope. Ordinary idle eviction is unchanged. This adds seven net production lines (an existing-owner lookup and early return, plus import/comment formatting) to enforce the existing ownership boundary; it adds no cache, retry, exception suppression, or alternate cleanup path.

Evidence

  • Regression at the real maintenance-lock boundary with two writable agent databases: fails on the base because both handles are prematurely closed; passes with the fix, verifies lease removal, clean-close seals, and persisted data.
  • Real-clock isolated Doctor maintenance, two agent databases, 31 seconds: base emits two matching maintenance-scope errors and closes both handles early; candidate keeps both open until scoped release and emits no such errors. Both arms preserve the written data and finish with zero durable agent leases. Full-integrity runs produce 0/2 clean-close seals on base versus 2/2 on candidate. A normal QUICK Doctor admission deliberately has no full-verification seal; both sides retain that policy.
  • Focused lifecycle/clean-close tests: 45 passed in 12.40 seconds (new regression 681 ms).
  • Existing native migration tests: 12 passed on base (122.03 seconds) and candidate (84.73 seconds), including a shared schema crossing. Timings are validation cost, not a performance comparison. These previously missed the defect because they did not exercise CLI idle expiry outside the maintenance context.
  • Independent subagent review (autoreview route unavailable): complete final local diff against fef5e6bfc485, published as 16a8dfc8a351 after an unrelated UI-only rebase; P2+, scoped-clean with no actionable findings. The helper refused before inference because its requested provider did not match operator configuration; configuration was not changed. Ownership escape after skipped expiry was source-reviewed, not separately exercised.
  • Changed-file oxfmt and lint passed. Broad validation is delegated to PR CI; no Testbox or production access was used.

This is a correctness fix on idle cleanup, with no performance claim or hot-path change. Gateway load testing and owner microbenchmarks would not exercise the faulty maintenance timer.

@clawsweeper

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

@steipete
steipete marked this pull request as ready for review October 28, 2026 13:40
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T13:42:34.103582Z 16a8dfc Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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 Oct 28, 2026
@clawsweeper

clawsweeper Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

The PR prevents idle eviction of agent database handles owned by an active maintenance resource scope.

Example: Doctor maintenance holds two writable agent databases for 31 seconds.

  • Before: The 30-second idle timer closes the handles outside maintenance custody, causing lease-release warnings and losing clean-close seals.
  • After: The handles remain open until scoped cleanup closes them and releases their leases.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The patch is focused and source-supported; independently inspectable behavioral evidence is limited to the regression and the author's verification report.
Proof confidence 🦐 gold shrimp (3/6) The ordinary contributor proof gate is exempt for this maintainer-authored PR; its body reports isolated Doctor maintenance with two databases beyond idle expiry, while the inspectable regression exercises the real lock boundary in process.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Long-running Doctor or native update maintenance emits database cleanup warnings and loses clean-close seals when idle eviction closes its handles early.
Reason: The maintainer-authored change restores existing resource ownership with a narrow guard and a meaningful regression.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR fixes premature maintenance-handle cleanup still present on main, with no actionable patch defects found.

Priority: P2
Reviewed head: 16a8dfc8a351b95aa72f1e5fc2b0d0e88cb2ea76

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: The body reports real-clock Doctor verification, but no attached terminal output independently demonstrates that shipped-entry-point scenario.
Agent review details

How this fits together

The agent database lifecycle manages cached SQLite handles, idle timers, durable leases, and cleanup coordinated with Doctor's maintenance resource scope.

flowchart TD
  A[Doctor maintenance] --> B[Scope owned agent handles]
  B --> C[Detached idle timer]
  C --> D[Existing resource owner lookup]
  D --> E[Defer idle eviction]
  B --> F[Scoped checkpoint close and lease release]
Loading

Technical review

Best possible solution:

Keep maintenance-owned handles under their existing scope until scoped cleanup completes, preserving ordinary idle eviction.

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

Source confirms the detached timer can close a maintenance-owned handle before lease release; the author reports a matching base failure and candidate success, which this read-only review did not rerun.

Is this the best way to solve the issue?

The existing resource-owner lookup is the smallest complete fix: it prevents unauthorized cleanup at its producer while retaining the established scoped closer and cache-transfer behavior.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • CLI and Gateway idle-retention policy keeps the original intent (f8ce98e: Release unused CLI database leases promptly so Doctor need not wait through the Gateway's longer reuse window.)
  • Idle and shutdown cleanup ownership keeps the original intent (ebe793f: Retire settled handles and publish clean-close receipts while active borrowers retain their leases.)
  • Shared borrower and writable lifecycle separation keeps the original intent (ee6efea: Keep passive readers independent of the writable import graph while preserving shared lifecycle ownership.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

PR surface

Source +7, Tests +43. Total +50 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 8 1 +7
Tests 1 43 0 +43
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 51 1 +50

Labels

Label changes:

  • add P2: The fix addresses maintenance cleanup warnings and missing clean-close seals without demonstrated data loss or an unusable update workflow.
  • 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: The fix addresses maintenance cleanup warnings and missing clean-close seals without demonstrated data loss or an unusable update workflow.
  • 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.

Reviewed October 28, 2026, 10:03 AM ET / 14:03 UTC.

@steipete
steipete merged commit 5ec6954 into main Oct 28, 2026
223 checks passed
@steipete
steipete deleted the steipete/perf-micro-migrate-agent-db-close-scope-20260919 branch October 28, 2026 14:09
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 29, 2026
steipete added a commit that referenced this pull request Oct 30, 2026
Drain dependent readers before the final writer so graceful shutdown can preserve its clean-close seal. This addresses a residual seal-loss path after #186783.

Admit exact current-schema agent databases through SQLite recovery and bounded structural checks, then verify integrity and foreign keys in the background. Only a successful full check permits a seal; proven corruption revokes admission and persists quarantine with Doctor guidance. Keep migration and index-repair gates foreground, and discard verification results after configured database paths change.
steipete added a commit that referenced this pull request Oct 30, 2026
Drain dependent readers before the final writer so graceful shutdown can preserve its clean-close seal. This addresses a residual seal-loss path after #186783.

Admit exact current-schema agent databases through SQLite recovery and bounded structural checks, then verify integrity and foreign keys in the background. Only a successful full check permits a seal; proven corruption revokes admission and persists quarantine with Doctor guidance. Keep migration and index-repair gates foreground, and discard verification results after configured database paths change.
steipete added a commit that referenced this pull request Oct 30, 2026
* perf(state): keep agent startup inspection proportionate

Drain dependent readers before the final writer so graceful shutdown can preserve its clean-close seal. This addresses a residual seal-loss path after #186783.

Admit exact current-schema agent databases through SQLite recovery and bounded structural checks, then verify integrity and foreign keys in the background. Only a successful full check permits a seal; proven corruption revokes admission and persists quarantine with Doctor guidance. Keep migration and index-repair gates foreground, and discard verification results after configured database paths change.

* refactor(state): keep quarantine dispatch behind verification boundary

* fix(state): settle deferred writers when readers retire

Resume existing shutdown idle timers when dependent resources release, preserving clean seals and the pre-sidecar lease handoff. Keep bounded SQLite header facts at their native catalog owner, separate the verifier leaf contract, and retain foreground repair cancellation proof.

* docs(state): distinguish startup admission from crash recovery receipts

* fix(state): let resource owners settle before idle writer cleanup

* fix(sessions): retire idle readers during shutdown cleanup

* test(gateway): keep configured stores in the suite registry

* docs(state): align idle retirement with restart marker persistence
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 31, 2026
…8459)

* perf(state): keep agent startup inspection proportionate

Drain dependent readers before the final writer so graceful shutdown can preserve its clean-close seal. This addresses a residual seal-loss path after openclaw#186783.

Admit exact current-schema agent databases through SQLite recovery and bounded structural checks, then verify integrity and foreign keys in the background. Only a successful full check permits a seal; proven corruption revokes admission and persists quarantine with Doctor guidance. Keep migration and index-repair gates foreground, and discard verification results after configured database paths change.

* refactor(state): keep quarantine dispatch behind verification boundary

* fix(state): settle deferred writers when readers retire

Resume existing shutdown idle timers when dependent resources release, preserving clean seals and the pre-sidecar lease handoff. Keep bounded SQLite header facts at their native catalog owner, separate the verifier leaf contract, and retain foreground repair cancellation proof.

* docs(state): distinguish startup admission from crash recovery receipts

* fix(state): let resource owners settle before idle writer cleanup

* fix(sessions): retire idle readers during shutdown cleanup

* test(gateway): keep configured stores in the suite registry

* docs(state): align idle retirement with restart marker persistence
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. 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