Skip to content

fix(agents): agents delete leaves stale agent files when database sidecars reappear during deletion - #124461

Merged
steipete merged 1 commit into
mainfrom
steipete/fix-agents-delete-residue
Aug 16, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/fix-agents-delete-residue

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where operators running openclaw agents delete <id> against a gateway would see the command exit 0 and report success while ~157 files survived under $OPENCLAW_STATE_DIR/agents/<id>/ — the agent directory was never actually removed.

During the deletion window, the session purge reopens the agent database and recreates the -wal/-shm sidecars after cleanup preparation had already recorded those journal paths as absent. The sweep's identity guard classified the recreated files as foreign replacements ("cleanup path appeared after deletion preparation"), preserved them, ancestor protection cascaded over the entire agent tree, and the deletion journal finished "complete" over a surviving tree — a silent failure on the default path.

Why This Change Was Made

The deletion journal fence already blocks legitimate claims beneath prepared paths for the duration of the deletion, so a file that appears at a prepared-absent path during the window can only be leaked deleted-agent state (recreated WAL sidecars, runtime home rewrites). The fix adopts the appeared occupant's identity at prepared-absent paths so the sweep removes it, instead of preserving it and poisoning the ancestor chain. Prepared-present paths keep the existing identity-mismatch protection unchanged, so genuine operator file replacements are still preserved.

Named accepted tradeoff: a same-user process deliberately planting a file at a prepared-absent path during the deletion window now gets swept with the agent tree. This is the same local trust boundary already accepted by the existing Trash-race comment in this module — processes running as the operator's user inside the state dir are trusted.

Production LOC delta: net 0 (5 added / 5 removed in src/gateway/server-methods/agents.ts); the fix replaces the mistaken guard branch in the owner rather than adding a downstream compensation.

User Impact

openclaw agents delete now actually deletes the agent directory on the gateway path. Previously the command reported success while leaving the full agent state tree (database, sessions, config residue) on disk, which also blocked re-creating an agent with a clean identity.

Evidence

  • Regression tests fail pre-fix (verified by stashing the production change): recreated WAL sidecars between preparation and cleanup are trashed with the agent directory, and recovery sweeps a file that appeared at a journaled prepared-absent path.
  • node scripts/run-vitest.mjs src/gateway/server-methods/agents-mutate.test.ts — 100/100 passed (rerun after rebase onto current main).
  • CLI delete suite: 19/19 passed; sibling gateway server-method suites green.
  • Autoreview clean (0.96, no actionable findings).

… deletion

Gateway agents.delete closed the agent database (unlinking -wal/-shm)
during preparation, then awaited config/cron/session-purge work that
reopened the database and recreated the sidecars. The sweep classified
those recreated files as foreign replacements ("cleanup path appeared
after deletion preparation"), preserved them, cascaded ancestor
protection over the whole agent directory, and finished the deletion
journal with exit 0 while ~157 files survived under agents/<id>/.

The deletion journal fence already blocks legitimate claims beneath
prepared paths, so a file that appears at a prepared-absent path can
only be leaked deleted-agent state: adopt its identity and sweep it
instead of preserving it. Prepared-present paths keep the existing
identity-mismatch protection for genuine operator replacements.

Regression tests: recreated WAL sidecars between preparation and
cleanup are trashed with the agent directory, and recovery sweeps a
file that appeared at a journaled prepared-absent path.
@clawsweeper

clawsweeper Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S maintainer Maintainer-authored PR labels Aug 16, 2026
@steipete
steipete marked this pull request as ready for review August 16, 2026 07:03
@clawsweeper clawsweeper Bot added merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. 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 Aug 16, 2026
@clawsweeper

clawsweeper Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 16, 2026, 3:09 AM ET / 07:09 UTC.

ClawSweeper review

What this changes

The PR makes Gateway agent deletion sweep files that reappear at paths recorded as absent during preparation, including recreated SQLite WAL and SHM sidecars.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

Keep open for maintainer merge review. Current main has the exact prepared-absent guard that silently preserves recreated agent files; this patch corrects that owner decision without a blocking source-review finding.

Priority: P1
Reviewed head: fdcfb7712861500de37691664bc978f5927949fb

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, source-backed owner-boundary repair with no blocking correctness or security finding.
Proof confidence 🌊 off-meta tidepool Not applicable: This member-authored PR is exempt from the external-contributor proof gate; its body nevertheless provides targeted after-fix validation claims.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This member-authored PR is exempt from the external-contributor proof gate; its body nevertheless provides targeted after-fix validation claims.
Evidence reviewed 6 items Current-main defect: Current main treats a non-null identity at a prepared-absent path as a mismatch, so cleanup skips the path instead of removing it.
Patch targets the owner: The branch replaces only that prepared-absent mismatch branch with identity adoption and adds a focused sidecar-recreation regression.
Durable fence contract: The journal fence rejects database registration and lease claims beneath an unfinished deletion, including cleanup paths.
Findings None None.
Security None None.

How this fits together

Gateway agents delete journals agent-owned paths, removes the agent from configuration, purges state, and then moves eligible files to Trash. The changed identity check decides whether a path found during cleanup is deleted-agent residue or a protected replacement.

flowchart LR
  A[Agent delete request] --> B[Journal and ownership fence]
  B --> C[Config and session cleanup]
  C --> D[Cleanup identity check]
  D --> E[Trash eligible files]
  E --> F[Deletion completion]
  C --> G[Recreated SQLite sidecars]
  G --> D
Loading

Before merge

  • Resolve merge risk (P1) - Merging intentionally sweeps any same-user file created at a prepared-absent deletion path; cooperative OpenClaw database claims are durably fenced, while adversarial local same-user races remain outside that trust boundary.
Agent review details

Security

None.

PR surface

Source 0, Tests +44. Total +44 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 5 5 0
Tests 1 51 7 +44
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 56 12 +44

Review metrics

Metric Value Why it matters
Production versus test delta production net 0; tests +44 net The branch replaces one cleanup-owner decision and adds targeted regression coverage without production growth.

Merge-risk options

Maintainer options:

  1. Accept the explicit local trust boundary (recommended)
    Merge with the documented boundary that durable fences protect cooperative OpenClaw claims while same-user files created inside the deleting agent’s paths are swept.

Technical review

Best possible solution:

Retain this net-neutral owner-boundary correction and merge once the protected maintainer review and exact-head checks are satisfied.

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

Yes, from source: current main skips a prepared-absent path once it exists, and the new focused test recreates WAL/SHM files between preparation and cleanup. The read-only review did not execute the test.

Is this the best way to solve the issue?

Yes. The patch fixes the cleanup owner’s incorrect classification; a later compensating sweep would retain the same silent-success failure mode.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P1: The default deletion command can report success while leaving the deleted agent’s persisted state behind.
  • add merge-risk: 🚨 session-state: The patch changes which recreated agent-state files are removed during deletion.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This member-authored PR is exempt from the external-contributor proof gate; its body nevertheless provides targeted after-fix validation claims.

Label justifications:

  • P1: The default deletion command can report success while leaving the deleted agent’s persisted state behind.
  • merge-risk: 🚨 session-state: The patch changes which recreated agent-state files are removed during deletion.
  • 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: This member-authored PR is exempt from the external-contributor proof gate; its body nevertheless provides targeted after-fix validation claims.

Evidence

What I checked:

Likely related people:

  • steipete: Blame and pickaxe history attribute the current prepared-absent branch to steipete’s commit, and steipete authored this focused repair. (role: introduced current behavior and recent area contributor; confidence: high; commits: e8e7598c5b00, fdcfb7712861; files: src/gateway/server-methods/agents.ts, src/gateway/server-methods/agents-mutate.test.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Wait for exact-head required checks and protected maintainer review before merge.

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

Copy link
Copy Markdown
Contributor Author

Pre-merge verification (maintainer landing under explicit authorization):

ClawSweeper before-merge item (P1 merge risk): Accepted per the recommended maintainer option 1. The durable journal fence protects cooperative OpenClaw claims; a same-user process planting files at prepared-absent deletion paths gets swept — the same local trust boundary as the existing Trash race comment in this module. This tradeoff is named in the PR body.

Rank-up moves:

  • "Wait for exact-head required checks and protected maintainer review before merge" — exact-head CI is green on fdcfb7712861500de37691664bc978f5927949fb (run 31932923452, GREEN via node scripts/watch-pr-ci.mjs 124461 fdcfb771286...). Maintainer review: this landing is performed under explicit maintainer authorization for this named PR; landing proceeds via the repo-native scripts/pr flow with Testbox validation.

Local proof:

  • node scripts/run-vitest.mjs src/gateway/server-methods/agents-mutate.test.ts — 100/100 passed, rerun after rebase onto origin/main (063ce57).
  • Regression tests verified to fail pre-fix (production change stashed).
  • CLI delete suite 19/19; sibling gateway server-method suites green.
  • Autoreview clean (0.96, no actionable findings).

Known proof gaps: none blocking; live-gateway end-to-end delete was validated during investigation of the original residue report (~157 surviving files), reproduced and fixed at the cleanup owner.

@steipete
steipete merged commit a2e27de into main Aug 16, 2026
257 checks passed
@steipete
steipete deleted the steipete/fix-agents-delete-residue branch August 16, 2026 07:27
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 17, 2026
… deletion (openclaw#124461)

Gateway agents.delete closed the agent database (unlinking -wal/-shm)
during preparation, then awaited config/cron/session-purge work that
reopened the database and recreated the sidecars. The sweep classified
those recreated files as foreign replacements ("cleanup path appeared
after deletion preparation"), preserved them, cascaded ancestor
protection over the whole agent directory, and finished the deletion
journal with exit 0 while ~157 files survived under agents/<id>/.

The deletion journal fence already blocks legitimate claims beneath
prepared paths, so a file that appears at a prepared-absent path can
only be leaked deleted-agent state: adopt its identity and sweep it
instead of preserving it. Prepared-present paths keep the existing
identity-mismatch protection for genuine operator replacements.

Regression tests: recreated WAL sidecars between preparation and
cleanup are trashed with the agent directory, and recovery sweeps a
file that appeared at a journaled prepared-absent path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. 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