Skip to content

fix: retain agent database lease owners during cleanup - #138454

Merged
steipete merged 1 commit into
mainfrom
codex/team-agent-lease-owner-20260904
Sep 4, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/team-agent-lease-owner-20260904

Conversation

@steipete

@steipete steipete commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Resolves a problem where closing a cached agent database after its environment changes could create an unrelated shared-state database and leave its original durable lease behind. The stale lease can block database maintenance. The same producer defect reproduces an empty-state violation during incognito transcript cleanup; it does not persist the incognito transcript itself.

Why This Change Was Made

The cache retained either an absent environment or the caller’s mutable environment object. Release then resolved the current directory and supervision mode instead of the lease’s acquisition owner. Capture the canonical state directory and supervision flag at acquisition, and reuse that private snapshot for claim, failed-open cleanup, cached close, eviction, and Worker settlement. Existing release consumers stay unchanged. No schema, stored representation, migration, configuration, or RPC changes.

User Impact

Cleanup releases the lease in the store that granted it without creating state in a later environment. Incognito sessions retain their no-disk behavior. Production changes are 14 added / 5 removed lines; the nine-line increase establishes owned acquisition facts in place of a borrowed environment. Cache hits and incognito opens return before this capture.

Evidence

  • Source-pinned Linux proof at public base 7167fb6f1959e9ecb9381e33c4c0a36b6865459a, Node 24.19.0, four effective CPUs, canonical frozen dependencies, and complete source/mode guards.
  • Real SQLite/incognito API sequence: open durable A with omitted env, switch ambient state to B, create/reload an incognito transcript, then close cached handles. Baseline creates B’s shared DB/WAL/SHM and retains A’s UUID; candidate releases A and leaves B empty. The incognito-only control passes in both variants under the same 60-second budget.
  • Seven new regression cases fail on the baseline and pass with the repair. Owner and sibling coverage totals 195 passed / 6 skipped across nine files. After a test-only fault-injection correction, all six cases in that file were proved RED→GREEN again; the eviction fixture and production source remained byte-identical.
  • Required changed checks have cumulative coverage: 20 stages passed before a test-only typescript/unbound-method lint failure; the corrected test passes lint, its consuming test type graph, formatting/ratchets, and all eight previously unrun guards. The original aggregate exit 1 remains recorded; this is not a claim that the original aggregate passed. The source-performance build and final source guard pass. Independent P0–P2 review is clean.
  • Initial hosted CI run 33902882481 passed 109 jobs; only control-ui-performance and the aggregate gate failed. On Node 24.20.0, startup gzip was 350,388 B against a 350,141 B limit. The exact CI merge changes only the three state files; its UI and dependency inputs match its main base. Already-merged fix(channels): keep quiet progress and the tool log under toolProgress #137342 passed its UI gate at 348,987 B under the unchanged limit. One patch-identical refresh onto main 0062f2b127bbbdc9fd7db65c679066a11101b6c3 preserves all three reviewed postimages; refreshed CI run 33906807123 completed successfully on exact head 5fa2b514aeff1b1cd5d5d8b5af368dd7ebbe6c3e, including both Windows jobs and openclaw/ci-gate.
  • Earlier transport quoting failure, baseline fixture contamination, and lint failure are retained. Exact acquired-UUID cleanup isolates the regression fixture; target-instance fault injection avoids a global prototype override. All four owned Linux leases were stopped and confirmed completed.

The original hosted log does not identify the previously retained durable handle, so this proves the producer defect and matching failure order/shape, not the exact hosted predecessor. No overall Gateway latency improvement is claimed.

@clawsweeper

clawsweeper Bot commented Sep 4, 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 self-assigned this Sep 4, 2026
@steipete
steipete marked this pull request as ready for review September 4, 2026 17:52
@openclaw-barnacle openclaw-barnacle Bot added size: S maintainer Maintainer-authored PR labels Sep 4, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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 Sep 4, 2026
@clawsweeper

clawsweeper Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 4, 2026, 2:43 PM ET / 18:43 UTC.

ClawSweeper review

What this changes

The PR captures an agent database lease’s resolved state-store directory and external-supervision status when acquired, then uses that immutable snapshot for later cleanup.

Merge readiness

⛔ Blocked before merge - 1 item remains

Keep open. The introduced snapshot repairs the actual mutable-environment ownership defect across normal close, eviction, failed-open cleanup, and Worker settlement; no blocking patch defect was found. Current main still has the pre-fix source, and this maintainer-authored, maintainer-labeled PR is not eligible for automated cleanup closure.

Priority: P2
Reviewed head: 5fa2b514aeff1b1cd5d5d8b5af368dd7ebbe6c3e

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A solid owner-boundary repair with focused behavior-level SQLite coverage and no actionable review finding.
Proof confidence 🌊 off-meta tidepool Not applicable: The authoritative repository state identifies a MEMBER author, so the external-contributor proof gate does not apply. The captured PR body nevertheless records a real SQLite before/after sequence aligned with the changed opener and cleanup paths.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The authoritative repository state identifies a MEMBER author, so the external-contributor proof gate does not apply. The captured PR body nevertheless records a real SQLite before/after sequence aligned with the changed opener and cleanup paths.
Evidence reviewed 6 items Introduced owner snapshot: The opener resolves the state directory and external-supervision fact once, passes that snapshot to lease acquisition, and retains it in the cache for later cleanup.
All release paths use the retained owner: Cached close releases with the retained environment, while the Worker settlement path does the same after its independent handle-close sequence; immediate and retained failed-open paths also store or use the snapshot.
State-store callee is environment-selected: Lease release performs its delete through the state database using the supplied options, so retaining the original environment is the necessary owner-boundary fix.
Findings None None.
Security None None.

How this fits together

Agent database handles acquire durable leases in OpenClaw’s shared SQLite state store before opening. Cache close, eviction, failed opens, and worker shutdown must remove the lease from that same state-store owner without creating state in a later environment.

flowchart LR
  A[Agent database open] --> B[Resolve state-store owner]
  B --> C[Acquire durable lease]
  C --> D[Cache database handle]
  D --> E[Close, eviction, or worker shutdown]
  E --> F[Release original lease]
  F --> G[Shared state database]
Loading

Before merge

  • Resolve merge risk (P1) - Production code grows by nine net lines; that growth is justified only because the acquisition owner must retain both the resolved state directory and the external-supervision permission for every later lease release.
Agent review details

Security

None.

PR surface

Source +9, Tests +163. Total +172 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 14 5 +9
Tests 2 163 0 +163
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 177 5 +172

Review metrics

Metric Value Why it matters
Production vs test LOC production +14/-5 (net +9); tests +163/-0 The production increase owns the two acquisition facts; the test additions exercise all cleanup release families.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Keep the local two-field acquisition snapshot: it is the minimum state that identifies both the original store and its write permission, so a net-neutral cleanup refactor cannot preserve the invariant.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Keep the local two-field acquisition snapshot: it is the minimum state that identifies both the original store and its write permission, so a net-neutral cleanup refactor cannot preserve the invariant.

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

Yes, source-reproducible: opening durable A, changing the state environment to B, then closing or evicting the cached handle follows the old mutable environment into the wrong state store. The new focused tests encode that order for normal, external-supervisor, Worker, failed-open, and eviction cleanup.

Is this the best way to solve the issue?

Yes. The state-store choice is made by the lease callee from its environment, and the opener is the only boundary that knows the resolved acquisition directory plus supervision state; a close-only workaround would miss eviction and failed-open paths.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 26f87a37009b.

Labels

Label justifications:

  • P2: A stale durable lease can block agent database maintenance, but the repair is bounded to the state lifecycle.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The authoritative repository state identifies a MEMBER author, so the external-contributor proof gate does not apply. The captured PR body nevertheless records a real SQLite before/after sequence aligned with the changed opener and cleanup paths.

Evidence

What I checked:

  • Introduced owner snapshot: The opener resolves the state directory and external-supervision fact once, passes that snapshot to lease acquisition, and retains it in the cache for later cleanup. (src/state/openclaw-agent-db.ts:323, 5fa2b514aeff)
  • All release paths use the retained owner: Cached close releases with the retained environment, while the Worker settlement path does the same after its independent handle-close sequence; immediate and retained failed-open paths also store or use the snapshot. (src/state/openclaw-agent-db.ts:524, 5fa2b514aeff)
  • State-store callee is environment-selected: Lease release performs its delete through the state database using the supplied options, so retaining the original environment is the necessary owner-boundary fix. (src/state/openclaw-agent-db-lease.ts:128, 5fa2b514aeff)
  • Focused regression coverage: Four table-driven ownership variants cover ambient, explicit, externally supervised, and Worker cleanup; two failed-open variants and one eviction case cover the remaining release paths with direct SQLite lease assertions. (src/state/openclaw-agent-db.lease-owner.test.ts:36, 5fa2b514aeff)
  • Current main still needs the change: The fetched main revision retains the old source blob, whose lease claim and cached release state use the caller environment rather than an acquisition snapshot. (src/state/openclaw-agent-db.ts:321, 26f87a37009b)
  • PR parentage and release position: The exact reviewed head has the declared base as its sole recorded parent; no local release tag contains the unmerged head. (5fa2b514aeff)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Shakker: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (1 earlier review cycle)
  • reviewed 2026-09-04T18:02:49.159Z sha 53cc9f9 :: blocked before merge. :: none

@steipete

steipete commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

The nine-line production increase is accepted for this repair. The opener is the only boundary that knows both the resolved state directory and supervision mode at acquisition. Keeping those two facts in one private snapshot replaces the borrowed environment in the existing lease cache; every release path uses it, and no public configuration or persistence contract changes.

This is the mitigation recommended by the review. A cleanup-only guard would miss eviction and failed-open paths; retaining the entire caller environment would carry unnecessary state. The source review and the real SQLite RED→GREEN proof support the current owner-level fix. No additional code change is needed for this item.

Exact-head CI remains a separate landing gate. The current run has a UI performance failure being handled separately; the lease-related compact test shard passed. The original run and its diagnostic will remain recorded.

Capture the canonical state directory and supervision facts when a durable
agent database lease is acquired. Reuse that owned snapshot for every
release path after ambient or caller-provided environments change.

Cover normal close, eviction, Worker settlement, and immediate or retained
failed-open cleanup without changing schemas or persisted data.
@steipete
steipete force-pushed the codex/team-agent-lease-owner-20260904 branch from 53cc9f9 to 5fa2b51 Compare September 4, 2026 18:36
@steipete
steipete merged commit 3871120 into main Sep 4, 2026
188 checks passed
@steipete
steipete deleted the codex/team-agent-lease-owner-20260904 branch September 4, 2026 18:58
@steipete

steipete commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 5, 2026
Capture the canonical state directory and supervision facts when a durable
agent database lease is acquired. Reuse that owned snapshot for every
release path after ambient or caller-provided environments change.

Cover normal close, eviction, Worker settlement, and immediate or retained
failed-open cleanup without changing schemas or persisted data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary 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.

1 participant