Skip to content

fix: skip retained deleted agent stores during plugin cleanup - #158473

Merged
obviyus merged 2 commits into
openclaw:mainfrom
anyech:fix/deleted-agent-shutdown-cleanup
Sep 29, 2026
Merged

obviyus merged 2 commits into
openclaw:mainfrom
anyech:fix/deleted-agent-shutdown-cleanup

Conversation

@anyech

@anyech anyech commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes #157645: Gateway shutdown fails when a deleted agent's historical directory remains on disk.

User Impact

Operators can retain deleted-agent history and still shut down cleanly. Healthy agents remain eligible for plugin cleanup. This fix introduces no schema, configuration, permission, or public API changes.

Why This Change Was Made

Plugin cleanup skips unconfigured agents positively identified by the existing completed-deletion journal. Database admission remains authoritative when that advisory read fails, and individual store failures are recorded without preventing cleanup of healthy siblings. Shared stores retain deleted agents' logical rows.

Thanks to @anyech for both reporting the failure and contributing the fix.

No overlap with Pash/Sarah changes: their authorship history has no hits on the four changed files.

Evidence

Final head: 32b1ba2f329e3dff1d29719c0b30f841b115794c. The merge of bb869836e98b resolved a confirmed GitHub merge conflict by preserving both sides' Gateway test imports. The PR remains +377/-16 across the same four files.

  • Real base/candidate Gateway pair: built base 0e6c5a8d891, created a session using gateway call sessions.create, then deleted its agent using gateway call agents.delete with deleteFiles:false. SIGINT caused both loaded plugins' session-store cleanup to fail with “agent retired is deleted”; the boot receipt recorded forced_stop. The final built candidate served an active-agent session and logged completed cleanly in 450ms, including agent database clean-close receipt: written. The retained database's SHA-256 was unchanged. The active database first went through the newer main branch's required Doctor migration; retained history remained untouched.
  • Published updater to final installed candidate: installed published [email protected] (eb377ac) into an isolated npm prefix and created retained history through its actual Gateway RPCs, not a seeded deletion journal. Published shutdown reproduced the failure. Its unmodified update --tag <candidate.tgz> --no-restart --yes --json returned status: ok, activated the package built from the exact final head, and reported serviceRestartSafe: true (309.373s wall). The installed candidate then served sessions.create, closed cleanly in 662ms, exited 0, and recorded clean_stop. The retained database remained byte-identical across update and shutdown. The earlier contributor head also passed this complete upgrade cell before the conflict-resolution merge.
  • Canonical scoped tests on final head: pnpm test src/plugins/host-hook-cleanup.session-store.test.ts --maxWorkers=1: 20/20, 156.84s wall. pnpm test src/gateway/server-close.agent-databases.test.ts --maxWorkers=1: 4 passed, 1 existing Linux-only test skipped, 236.61s wall. Measured on macOS arm64 / Node 24.21.0, including cold worker preparation. The retained-agent Gateway case protects registered shutdown/disposal, which the plugin-only cases cannot establish.
  • Built the installable final candidate, rebuilt its UI, and passed the package tarball integrity check. All proof Gateways used explicit isolated profiles, home/state/config paths, and unique loopback ports. All have exited.

The installed updater retained existing Doctor advisories for preserved history and the deliberately minimal fixture configuration. It correctly skipped managed-service changes for relocated state. No updater or UI behavior was changed by this patch.

@clawsweeper

clawsweeper Bot commented Sep 26, 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 P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 26, 2026
@clawsweeper

clawsweeper Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 29, 2026, 2:42 AM ET / 06:42 UTC (Revision 9).

ClawSweeper review

What this changes

Plugin cleanup skips session stores belonging to recorded, retained deleted agents, continues cleaning active stores after individual failures, and adds shutdown and store-level regression tests.

Merge readiness

✅ Ready for maintainer review

Keep this PR open. Current main still has the reported shutdown cleanup path, while the branch has a focused fix and real Gateway and upgrade evidence tied to its final head. The linked shutdown issue remains open; the related Doctor fix addresses a different lifecycle path.

Priority: P1
Reviewed head: 32b1ba2f329e3dff1d29719c0b30f841b115794c

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Focused ownership, final-head runtime and upgrade proof, and scoped regressions support a solid review result.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): The changed plugin cleanup owner is exercised through a built Gateway and the published updater: the reported shutdown failure occurred before the fix, while the exact final-head candidate served an active session, closed cleanly, and preserved retained database bytes. No stored-data contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The changed plugin cleanup owner is exercised through a built Gateway and the published updater: the reported shutdown failure occurred before the fix, while the exact final-head candidate served an active session, closed cleanly, and preserved retained database bytes. No stored-data contract changes.
Evidence reviewed 7 items Introduced cleanup guard: The branch checks the existing admission refusal, then skips only unconfigured agents present in the retained-deletion set; each store failure is recorded while later targets continue.
Deletion journal read: The new helper reads completed retained identities through the existing shared-state read worker; it adds no stored format.
Current main remains affected: The fetched main implementation still loops over discovered targets with only an admission-refusal check before session-store cleanup; the branch's retained-deletion guard is absent.
Findings None None.
Security None None.

How this fits together

The Gateway asks loaded plugins to release session-store resources during shutdown or retirement. Store discovery supplies agent targets, and cleanup updates eligible session state before the Gateway records its close outcome.

flowchart LR
  A[Gateway shutdown] --> B[Plugin cleanup]
  C[Discovered session stores] --> B
  D[Deletion journal] --> E{Retained deleted agent?}
  B --> E
  E -->|Yes| F[Preserve historical store]
  E -->|No| G[Clean eligible store]
  G --> H[Close outcome]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +44, Tests +317. Total +361 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 2 56 12 +44
Tests 2 321 4 +317
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 377 16 +361

Review metrics

Metric Value Why it matters
Scoped test wall time 156.84s plugin cleanup; 236.61s Gateway close The PR records the repository-requested cost of its focused regression suites, including the registered shutdown path.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #157645
Summary: This PR is the candidate fix for the linked shutdown issue; the Doctor and deletion-time reader work concern separate lifecycle paths.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Technical review

Best possible solution:

Land the focused shutdown fix through the normal PR gates while preserving retained history and completing cleanup for healthy stores.

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

Yes. The PR records a published Gateway reproduction using sessions.create, agents.delete with deleteFiles:false, and shutdown; current main still has the affected cleanup loop.

Is this the best way to solve the issue?

Yes. The branch uses the existing deletion journal and per-store admission boundary, while its Gateway test covers the registered shutdown path.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 46147f5e5d13.

Labels

Label changes:

No label changes.

Label justifications:

  • P1: The linked report describes shutdown failing for operators who retain deleted-agent history on a released OpenClaw version.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The changed plugin cleanup owner is exercised through a built Gateway and the published updater: the reported shutdown failure occurred before the fix, while the exact final-head candidate served an active session, closed cleanly, and preserved retained database bytes. No stored-data contract changes.
  • proof: sufficient: Contributor real behavior proof is sufficient. The changed plugin cleanup owner is exercised through a built Gateway and the published updater: the reported shutdown failure occurred before the fix, while the exact final-head candidate served an active session, closed cleanly, and preserved retained database bytes. No stored-data contract changes.

Evidence

What I checked:

  • Introduced cleanup guard: The branch checks the existing admission refusal, then skips only unconfigured agents present in the retained-deletion set; each store failure is recorded while later targets continue. (src/plugins/host-hook-cleanup.ts:79, 32b1ba2f329e)
  • Deletion journal read: The new helper reads completed retained identities through the existing shared-state read worker; it adds no stored format. (src/state/agent-deletion-discovery.ts:25, 32b1ba2f329e)
  • Current main remains affected: The fetched main implementation still loops over discovered targets with only an admission-refusal check before session-store cleanup; the branch's retained-deletion guard is absent. (src/plugins/host-hook-cleanup.ts:60, 46147f5e5d13)
  • Production boundary test: The added Gateway test registers a plugin, retains a completed deleted-agent journal entry, closes the server, and checks active-store cleanup and retained database survival. (src/gateway/server-close.agent-databases.test.ts:52, 32b1ba2f329e)
  • Final-head real behavior proof: The PR body reports a published 2026.9.6 Gateway failing shutdown with history retained through actual RPCs. After the published updater installed the package built from this head, the Gateway served an active session, exited 0 with clean_stop, and left the retained database byte-identical. (32b1ba2f329e)
  • Prior review follow-up: The prior completed review had no findings and requested conflict resolution with both Gateway test imports retained. The current PR discussion reports the normal merge commit and final-head Gateway and updater validation. (src/gateway/server-close.agent-databases.test.ts:1, 32b1ba2f329e)

Likely related people:

  • Peter Steinberger: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Dan Scarafoni: 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 (8 earlier review cycles)
  • reviewed 2026-09-26T00:15:53.877Z sha d908e5ed2bd6045cf82609613d725429c4e51bc8 :: needs real behavior proof before merge. :: [P1] Keep cleaning healthy stores when the deletion snapshot read fails
  • reviewed 2026-09-26T01:31:56.141Z sha f8d4bc53fedf93393a25996572c1bd9a314d9346 :: blocked before merge. :: none
  • reviewed 2026-09-26T01:41:09.021Z sha 4ece373 :: blocked before merge. :: none
  • reviewed 2026-09-26T02:10:29.527Z sha 4ece373 :: blocked before merge. :: none
  • reviewed September 26, 2026, 9:45 PM ET / September 27, 2026, 01:45 UTC (Revision 5) sha unknown :: blocked before merge. :: none
  • reviewed September 26, 2026, 9:45 PM ET / September 27, 2026, 01:45 UTC (Revision 6) sha unknown :: blocked before merge. :: none
  • reviewed September 26, 2026, 9:45 PM ET / September 27, 2026, 01:45 UTC (Revision 7) sha unknown :: blocked before merge. :: none
  • reviewed 2026-09-29T05:52:30.388Z sha 4ece373 :: blocked before merge. :: none

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. and removed triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Sep 26, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. 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. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 26, 2026
@anyech
anyech force-pushed the fix/deleted-agent-shutdown-cleanup branch from f8d4bc5 to 4ece373 Compare September 26, 2026 01:34
@clawsweeper clawsweeper Bot added merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Sep 26, 2026
@obviyus

obviyus commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Both remaining revision-7 items now have observed evidence on 4ece37391240a56bee5ea18bbc59d2cd0d116bb2. The published 2026.9.6 updater successfully installed the exact candidate against retained-agent state created through the published Gateway's sessions.create and agents.delete(deleteFiles:false) RPCs. The upgraded installed Gateway served an active-agent session, logged completed cleanly in 4408ms, exited 0, and persisted clean_stop; the retained database's SHA-256 stayed unchanged. The same published fixture had failed shutdown before the update.

Canonical pnpm test <file> --maxWorkers=1 wall times: plugin cleanup 89.08s (20/20); Gateway close 154.79s (5/5). The PR body now includes the base/candidate CLI proof, installed-updater proof, and measured times. These were evidence gaps, not code findings; the existing narrow owner fix and contributor commits remain unchanged, and the guarded push was a no-op. Thanks @anyech for reporting and fixing this.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot removed the merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. label Sep 29, 2026
@obviyus

obviyus commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Resolved the confirmed merge conflict in normal merge commit 32b1ba2f329e3dff1d29719c0b30f841b115794c, preserving the contributor history and both sides' Gateway test imports. The runtime fix remains +377/-16 across four files.

Revalidated this exact combined head, including a fresh package build and the unmodified published 2026.9.6 updater against the retained-history fixture. Update returned status: ok; the installed final candidate served an active session, wrote the agent clean-close receipt, logged completed cleanly in 662ms, exited 0, and persisted clean_stop. The retained database stayed byte-identical. Canonical scoped test wall times are now 156.84s (20/20 plugin cleanup) and 236.61s (4 passed, existing Linux-only case skipped on macOS). The PR body contains the final-head evidence. Thanks again @anyech.

@obviyus

obviyus commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@obviyus
obviyus merged commit 66799b3 into openclaw:main Sep 29, 2026
239 of 240 checks passed
@obviyus

obviyus commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Merged as 66799b3. Thanks @anyech for fixing the deleted-agent shutdown cleanup, and for the report in #157645 (now closed). Exact-head Gateway proof showed shutdown completing in 450 ms from source and 662 ms after the published-driver upgrade, with exit code 0, a clean-stop receipt, and the retained deleted-agent database unchanged. CI passed and ClawSweeper marked the exact head ready for maintainer review.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 30, 2026
…aw#158473)

Gateway shutdown no longer fails or hangs when an agent's directory was deleted while the Gateway was running. Cleanup now skips the missing agent's state instead of trying to reopen it, so shutdown finishes and records a clean stop.

Fixes openclaw#157645.

Proof: a real Gateway, run from source and from an installed build, with an agent directory deleted before shutdown. The installed build shut down in 662 ms with exit 0 and a clean_stop receipt, leaving the deleted agent's retained database unchanged. An upgrade from the published build reports ok with serviceRestartSafe true.

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

Labels

gateway Gateway runtime P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M 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.

Gateway shutdown session-store cleanup fails on retained directory of a deleted agent

2 participants