Repository navigation
fix(agents): agents delete leaves stale agent files when database sidecars reappear during deletion - #124461
Conversation
… 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.
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Codex review: needs maintainer review before merge. Reviewed August 16, 2026, 3:09 AM ET / 07:09 UTC. ClawSweeper reviewWhat this changesThe PR makes Gateway agent deletion sweep files that reappear at paths recorded as absent during preparation, including recreated SQLite WAL and SHM sidecars. Regression provenancePossible regression — suspected (reviewed change). No predecessor PR is attributed. Merge readinessKeep 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 Review scores
Verification
How this fits togetherGateway 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
Before merge
Agent review detailsSecurityNone. PR surfaceSource 0, Tests +44. Total +44 across 2 files. View PR surface stats
Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest 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. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
|
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:
Local proof:
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. |
|
Merged via squash.
|
… 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.
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/-shmsidecars 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 deletenow 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
node scripts/run-vitest.mjs src/gateway/server-methods/agents-mutate.test.ts— 100/100 passed (rerun after rebase onto currentmain).