Skip to content

fix(agents): wait for database resources during deletion - #157631

Merged
steipete merged 1 commit into
mainfrom
codex/agent-recreation-drain-20260924
Sep 24, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/agent-recreation-drain-20260924

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Agent deletion can report cleanup complete while database resources created during the purge are still closing.

User Impact

Deletion now waits for those resources before allowing its cleanup to finish. A failed close retains the original handle for the existing retry path. Database formats, retention, permissions, and public APIs are unchanged.

Why This Change Was Made

The initial deletion preparation already awaited resource drainage, but a later cold purge could open another native handle and publish changes that admitted new resources. Its cleanup callback used the synchronous closer, which revoked resources without joining their asynchronous close.

The existing cleanup owner now awaits the exact-path close and its retry, retires callback write authority before disposal yields, and retains its native borrow until close succeeds. This also prevents idle eviction from dropping the handle during a slow or failed drain.

Evidence

  • Two real purge/publication cases failed on the original code because cleanup settled while a registered close acknowledgment was held. Both pass after the repair.
  • Two additional real idle-timer controls exposed premature native eviction during a held close and after a failed close. Moving the existing borrow release after successful disposal makes both pass, including subsequent retry and fresh registration.
  • The complete cleanup owner file passes 22/22 cases (98 s wall including worker compilation). Resource, purge, and foreign-lifecycle recovery siblings pass 41/41 (59 s wall). These tests use the real SQLite handle, public resource registry, deletion journal, and purge entrypoint; only the close acknowledgment and timer passage are controlled.
  • Core and all 25 test type graphs passed before the final release-order refinement; core and the owning test graph passed again on the final source. Targeted lint, formatting, line-cap checks, dead-export scans, schema/boundary guards, and zero runtime import cycles pass. Final independent P2 review is clean.

The original Gateway CI failures reported Agent database resources are closing after deletion and recreation. This proves a reached missing-drain defect, but the old logs do not identify their exact blocking resource. It does not claim to fix the separate create-ACK/config-publication race. The full Gateway recreation flow remains part of CI validation; no local full-Gateway run is claimed.

@clawsweeper

clawsweeper Bot commented Sep 24, 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 September 24, 2026 22:18
@clawsweeper

clawsweeper Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 24, 2026, 6:26 PM ET / 22:26 UTC.

ClawSweeper review

What this changes

The branch makes agent deletion await database resources opened during session purging, retain a failed handle for retry, and test delayed and failed closes.

Merge readiness

✅ Ready for maintainer review

Current main still uses the synchronous close that can finish deletion cleanup before a later resource closes. The branch addresses that gap through the existing cleanup owner, and this review found no concrete blocking defect.

Priority: P2
Reviewed head: 0cd6fbc37bbe6675551f8aba8747dfaa2fba6e72

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The focused production-boundary scenarios and small owner change support a normal good rating, with full Gateway validation still left to the ordinary PR checks.
Proof confidence 🐚 platinum hermit (4/6) Not applicable: The supplied repository state identifies a member-authored PR, so the external-contributor proof gate does not apply. Its body reports before-and-after results from the real SQLite purge and resource-registry path with an injected close delay and failure; no full Gateway recreation run is claimed. No stored-data contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The supplied repository state identifies a member-authored PR, so the external-contributor proof gate does not apply. Its body reports before-and-after results from the real SQLite purge and resource-registry path with an injected close delay and failure; no full Gateway recreation run is claimed. No stored-data contract changes.
Evidence reviewed 7 items Current main still has the gap: The current-main cleanup callback releases its idle borrow and calls the synchronous database closer, which revokes asynchronous resources without joining their close promises.
Changed cleanup owner: The proposed scope retires callback write authority before awaiting each registered close, preserves failures for retry, and checks deletion ownership again after disposal.
Exact handle ownership: The changed callback awaits the exact-path asynchronous close and releases the native borrow only after that close succeeds.
Findings None None.
Security None None.

How this fits together

Gateway agent deletion prepares database paths, removes the agent from configuration, and purges its session rows. The deletion cleanup scope controls a database reopened during that purge and must close its resources before deletion proceeds.

flowchart LR
A[Gateway delete request] --> B[Deletion preparation]
B --> C[Session row purge]
C --> D[Database cleanup scope]
D --> E{Resource close succeeds?}
E -- Yes --> F[Close SQLite handle and lease]
E -- No --> G[Retain handle for retry]
F --> H[Deletion proceeds]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +1, Tests +116. Total +117 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 15 14 +1
Tests 1 116 0 +116
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 131 14 +117

Review metrics

Metric Value Why it matters
Production and test lines production +15/-14; tests +116/-0 The net production change is small; the growth exercises delayed close and retry behavior.

Technical review

Best possible solution:

Deletion should settle only after late database resources close, while failed closes retain custody for retry and stale callbacks cannot write.

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

Yes. Current-main source exposes the synchronous close, and the supplied test describes a reproducible delayed-close path through the purge entrypoint; this read-only review did not run it.

Is this the best way to solve the issue?

Yes. Awaiting the existing exact-path close within the deletion cleanup owner is a narrow repair that preserves its retry and write-authority boundaries.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P2: The fix addresses a bounded agent deletion and recreation failure with focused lifecycle coverage.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.

Label justifications:

  • P2: The fix addresses a bounded agent deletion and recreation failure with focused lifecycle coverage.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.

Evidence

What I checked:

  • Current main still has the gap: The current-main cleanup callback releases its idle borrow and calls the synchronous database closer, which revokes asynchronous resources without joining their close promises. (src/state/openclaw-agent-db.ts:439, fdebe3495d3d)
  • Changed cleanup owner: The proposed scope retires callback write authority before awaiting each registered close, preserves failures for retry, and checks deletion ownership again after disposal. (src/state/agent-deletion-cleanup.ts:124, 0cd6fbc37bbe)
  • Exact handle ownership: The changed callback awaits the exact-path asynchronous close and releases the native borrow only after that close succeeds. (src/state/openclaw-agent-db.ts:439, 0cd6fbc37bbe)
  • Production boundary and focused coverage: Gateway deletion awaits the session purge; the new test drives that purge with a real SQLite handle and resource registry while controlling a close acknowledgment, then checks settlement, failed-close retry, and lease release. The PR body reports these cases failed before the change and passed after it; this review did not execute tests. (src/state/agent-deletion-cleanup.test.ts:73, 0cd6fbc37bbe)
  • Resource drain contract: The existing exact-path asynchronous closer drains registered resources before native closure and retains failed resource custody for a later attempt. (src/state/openclaw-agent-db-resources.ts:211, 0cd6fbc37bbe)
  • Release check: The latest supplied release tag still has synchronous cleanup closers; the proposed wait is neither in that release nor in the inspected current-main revision. (src/state/agent-deletion-cleanup.ts:52, eb377ac59e6c)

Likely related people:

  • steipete: 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.

@steipete
steipete enabled auto-merge (squash) September 24, 2026 22:42
@steipete

Copy link
Copy Markdown
Contributor Author

Exact-head CI passed. The Gateway recreation shard explicitly ran and passed both product cases: recreating/registering a deleted agent through one real Gateway process (5,162 ms), and deleting agent files from an external state directory to Trash (1,188 ms). The shard passed all 258 tests across 23 files, testing merge 1a6593137b300722a2cb7a733c5ee0affbdefd3c with this PR’s 0cd6fbc37bbe head.

This adds actual Gateway-flow evidence to the controlled before/after cleanup regressions. The original failed job’s exact blocking resource remains unidentified; the repair’s causal claims remain the missing joined close and premature idle eviction.

@steipete
steipete merged commit 53f8b33 into main Sep 24, 2026
267 of 268 checks passed
@steipete
steipete deleted the codex/agent-recreation-drain-20260924 branch September 24, 2026 23:07
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash auto-merge.

steipete added a commit to SebTardif/openclaw that referenced this pull request Sep 24, 2026
Carry fixed main 6f60b82 with the joined database-drain repair from openclaw#157631.
The existing service identity patch is unchanged. Reuse the canonical type-only
npm-pack import correction from openclaw#157659 so the composed core graph typechecks.

Co-authored-by: Sebastien Tardif <[email protected]>
Co-authored-by: Jason (Json) <[email protected]>
vincentkoc added a commit that referenced this pull request Sep 25, 2026
* commit '0484d7a3ecf52589b0d3d98741e58e5f6b5bf661': (68 commits)
  fix(subagents): stop completion transcript retry loops (#157571)
  perf(sessions): remove duplicate foreground history reference scans (#157572)
  improve(typesafe): defer startup work until the first decision (#157380)
  fix(update): restore npm-pack helper typechecking (#157659)
  fix(packaging): avoid heap exhaustion on large bundles (#157471)
  refactor(infra): share process error handler registries (#157632)
  chore(ui): refresh control ui locales (#157573)
  fix(gateway): release remote Skills subscriptions when shutdown drains (#157592)
  perf(plugins): reuse declared tool membership during resolution (#156205)
  fix(ui): pasted text previews fail before sending (#157390)
  fix(agents): wait for database resources during deletion (#157631)
  improve: move operator profile database reads off the Gateway thread (#155800)
  fix: explain rejected settings without losing edits (#157549)
  fix: keep Gateway responsive while recording trajectory events (#157598)
  fix(auth): show SIWC email and selected model account identity (#157629)
  docs: explain Codex app approval precedence (#155089)
  refactor(wizard): share select prompt requests (#157607)
  fix(ui): omit empty sections from conversation Markdown exports (#157590)
  fix(doctor): explain update lint supervisor refusals (#156547)
  fix(ui): GitHub folder links lose repository context (#157619)
  ...
vincentkoc added a commit that referenced this pull request Sep 25, 2026
* commit '0484d7a3ecf52589b0d3d98741e58e5f6b5bf661': (68 commits)
  fix(subagents): stop completion transcript retry loops (#157571)
  perf(sessions): remove duplicate foreground history reference scans (#157572)
  improve(typesafe): defer startup work until the first decision (#157380)
  fix(update): restore npm-pack helper typechecking (#157659)
  fix(packaging): avoid heap exhaustion on large bundles (#157471)
  refactor(infra): share process error handler registries (#157632)
  chore(ui): refresh control ui locales (#157573)
  fix(gateway): release remote Skills subscriptions when shutdown drains (#157592)
  perf(plugins): reuse declared tool membership during resolution (#156205)
  fix(ui): pasted text previews fail before sending (#157390)
  fix(agents): wait for database resources during deletion (#157631)
  improve: move operator profile database reads off the Gateway thread (#155800)
  fix: explain rejected settings without losing edits (#157549)
  fix: keep Gateway responsive while recording trajectory events (#157598)
  fix(auth): show SIWC email and selected model account identity (#157629)
  docs: explain Codex app approval precedence (#155089)
  refactor(wizard): share select prompt requests (#157607)
  fix(ui): omit empty sections from conversation Markdown exports (#157590)
  fix(doctor): explain update lint supervisor refusals (#156547)
  fix(ui): GitHub folder links lose repository context (#157619)
  ...
vincentkoc added a commit that referenced this pull request Sep 25, 2026
* refactor(ai): share Mistral text delta appends

* Merge main to incorporate the upstream update type fix

* commit '0484d7a3ecf52589b0d3d98741e58e5f6b5bf661': (68 commits)
  fix(subagents): stop completion transcript retry loops (#157571)
  perf(sessions): remove duplicate foreground history reference scans (#157572)
  improve(typesafe): defer startup work until the first decision (#157380)
  fix(update): restore npm-pack helper typechecking (#157659)
  fix(packaging): avoid heap exhaustion on large bundles (#157471)
  refactor(infra): share process error handler registries (#157632)
  chore(ui): refresh control ui locales (#157573)
  fix(gateway): release remote Skills subscriptions when shutdown drains (#157592)
  perf(plugins): reuse declared tool membership during resolution (#156205)
  fix(ui): pasted text previews fail before sending (#157390)
  fix(agents): wait for database resources during deletion (#157631)
  improve: move operator profile database reads off the Gateway thread (#155800)
  fix: explain rejected settings without losing edits (#157549)
  fix: keep Gateway responsive while recording trajectory events (#157598)
  fix(auth): show SIWC email and selected model account identity (#157629)
  docs: explain Codex app approval precedence (#155089)
  refactor(wizard): share select prompt requests (#157607)
  fix(ui): omit empty sections from conversation Markdown exports (#157590)
  fix(doctor): explain update lint supervisor refusals (#156547)
  fix(ui): GitHub folder links lose repository context (#157619)
  ...

Co-authored-by: Vincent Koc <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 25, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 25, 2026
* refactor(ai): share Mistral text delta appends

* Merge main to incorporate the upstream update type fix

* commit 'dda1f68bfa102cb804392c0e7cc22c0566a0944a': (68 commits)
  fix(subagents): stop completion transcript retry loops (openclaw#157571)
  perf(sessions): remove duplicate foreground history reference scans (openclaw#157572)
  improve(typesafe): defer startup work until the first decision (openclaw#157380)
  fix(update): restore npm-pack helper typechecking (openclaw#157659)
  fix(packaging): avoid heap exhaustion on large bundles (openclaw#157471)
  refactor(infra): share process error handler registries (openclaw#157632)
  chore(ui): refresh control ui locales (openclaw#157573)
  fix(gateway): release remote Skills subscriptions when shutdown drains (openclaw#157592)
  perf(plugins): reuse declared tool membership during resolution (openclaw#156205)
  fix(ui): pasted text previews fail before sending (openclaw#157390)
  fix(agents): wait for database resources during deletion (openclaw#157631)
  improve: move operator profile database reads off the Gateway thread (openclaw#155800)
  fix: explain rejected settings without losing edits (openclaw#157549)
  fix: keep Gateway responsive while recording trajectory events (openclaw#157598)
  fix(auth): show SIWC email and selected model account identity (openclaw#157629)
  docs: explain Codex app approval precedence (openclaw#155089)
  refactor(wizard): share select prompt requests (openclaw#157607)
  fix(ui): omit empty sections from conversation Markdown exports (openclaw#157590)
  fix(doctor): explain update lint supervisor refusals (openclaw#156547)
  fix(ui): GitHub folder links lose repository context (openclaw#157619)
  ...

Co-authored-by: Vincent Koc <[email protected]>
vincentkoc added a commit that referenced this pull request Sep 25, 2026
* refactor(ui): share slash-command failure results

* Merge main to incorporate the upstream update type fix

* commit '0484d7a3ecf52589b0d3d98741e58e5f6b5bf661': (68 commits)
  fix(subagents): stop completion transcript retry loops (#157571)
  perf(sessions): remove duplicate foreground history reference scans (#157572)
  improve(typesafe): defer startup work until the first decision (#157380)
  fix(update): restore npm-pack helper typechecking (#157659)
  fix(packaging): avoid heap exhaustion on large bundles (#157471)
  refactor(infra): share process error handler registries (#157632)
  chore(ui): refresh control ui locales (#157573)
  fix(gateway): release remote Skills subscriptions when shutdown drains (#157592)
  perf(plugins): reuse declared tool membership during resolution (#156205)
  fix(ui): pasted text previews fail before sending (#157390)
  fix(agents): wait for database resources during deletion (#157631)
  improve: move operator profile database reads off the Gateway thread (#155800)
  fix: explain rejected settings without losing edits (#157549)
  fix: keep Gateway responsive while recording trajectory events (#157598)
  fix(auth): show SIWC email and selected model account identity (#157629)
  docs: explain Codex app approval precedence (#155089)
  refactor(wizard): share select prompt requests (#157607)
  fix(ui): omit empty sections from conversation Markdown exports (#157590)
  fix(doctor): explain update lint supervisor refusals (#156547)
  fix(ui): GitHub folder links lose repository context (#157619)
  ...

* Merge frozen main to include the question fixture authority fix

* commit '229ab8a1fe957d3d4822309a39ddf9c7ec616395': (25 commits)
  test(ui): capture live operator authority in guest question fixture
  refactor(agents): deslop runners, harness and recovery (#157294)
  fix(auth): preserve Copilot tenant credentials during Doctor repair (#139131)
  refactor(msteams): share stored service URL validation (#157706)
  fix(codex): honor configured tool PATH in native commands (#157290)
  refactor(msteams): share approval kind labels (#157705)
  fix(sessions): keep session results current during cold reads (#156121)
  fix(testing): capture bounded upgrade integrity diagnostics (#157535)
  refactor(i18n): reuse native branch candidate extraction (#157636)
  fix(update): preserve recovery files after ownership refusal (#148890)
  fix(test): prevent late announcements from contaminating registry tests (#157681)
  fix(e2e): scan full onboarding logs for prompts (#157624)
  refactor(session-tools): derive contracts from schemas (#156365)
  refactor(channels): derive config contracts from schemas (#156367)
  perf(test): reuse MCP archives and batch worker fixture copies (#157689)
  refactor: reuse prepared fixtures in slow core tests (#157685)
  refactor(codex): reuse marketplace reference selection (#157621)
  chore(ui): refresh control ui locales (#157710)
  docs(gateway): show how we build OpenClaw on Team (#157675)
  refactor: verify IRC surrogate chunking over loopback (#157580)
  ...

* chore: merge frozen main prerequisite repairs

Co-authored-by: Vincent Koc <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 26, 2026
* refactor(ui): share slash-command failure results

* Merge main to incorporate the upstream update type fix

* commit 'dda1f68bfa102cb804392c0e7cc22c0566a0944a': (68 commits)
  fix(subagents): stop completion transcript retry loops (openclaw#157571)
  perf(sessions): remove duplicate foreground history reference scans (openclaw#157572)
  improve(typesafe): defer startup work until the first decision (openclaw#157380)
  fix(update): restore npm-pack helper typechecking (openclaw#157659)
  fix(packaging): avoid heap exhaustion on large bundles (openclaw#157471)
  refactor(infra): share process error handler registries (openclaw#157632)
  chore(ui): refresh control ui locales (openclaw#157573)
  fix(gateway): release remote Skills subscriptions when shutdown drains (openclaw#157592)
  perf(plugins): reuse declared tool membership during resolution (openclaw#156205)
  fix(ui): pasted text previews fail before sending (openclaw#157390)
  fix(agents): wait for database resources during deletion (openclaw#157631)
  improve: move operator profile database reads off the Gateway thread (openclaw#155800)
  fix: explain rejected settings without losing edits (openclaw#157549)
  fix: keep Gateway responsive while recording trajectory events (openclaw#157598)
  fix(auth): show SIWC email and selected model account identity (openclaw#157629)
  docs: explain Codex app approval precedence (openclaw#155089)
  refactor(wizard): share select prompt requests (openclaw#157607)
  fix(ui): omit empty sections from conversation Markdown exports (openclaw#157590)
  fix(doctor): explain update lint supervisor refusals (openclaw#156547)
  fix(ui): GitHub folder links lose repository context (openclaw#157619)
  ...

* Merge frozen main to include the question fixture authority fix

* commit 'dc64cc3a3a3cf38fff1e44c1d7af42a32929d7e4': (25 commits)
  test(ui): capture live operator authority in guest question fixture
  refactor(agents): deslop runners, harness and recovery (openclaw#157294)
  fix(auth): preserve Copilot tenant credentials during Doctor repair (openclaw#139131)
  refactor(msteams): share stored service URL validation (openclaw#157706)
  fix(codex): honor configured tool PATH in native commands (openclaw#157290)
  refactor(msteams): share approval kind labels (openclaw#157705)
  fix(sessions): keep session results current during cold reads (openclaw#156121)
  fix(testing): capture bounded upgrade integrity diagnostics (openclaw#157535)
  refactor(i18n): reuse native branch candidate extraction (openclaw#157636)
  fix(update): preserve recovery files after ownership refusal (openclaw#148890)
  fix(test): prevent late announcements from contaminating registry tests (openclaw#157681)
  fix(e2e): scan full onboarding logs for prompts (openclaw#157624)
  refactor(session-tools): derive contracts from schemas (openclaw#156365)
  refactor(channels): derive config contracts from schemas (openclaw#156367)
  perf(test): reuse MCP archives and batch worker fixture copies (openclaw#157689)
  refactor: reuse prepared fixtures in slow core tests (openclaw#157685)
  refactor(codex): reuse marketplace reference selection (openclaw#157621)
  chore(ui): refresh control ui locales (openclaw#157710)
  docs(gateway): show how we build OpenClaw on Team (openclaw#157675)
  refactor: verify IRC surrogate chunking over loopback (openclaw#157580)
  ...

* chore: merge frozen main prerequisite repairs

Co-authored-by: Vincent Koc <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant