Skip to content

fix: agents delete leaves no stale database registry row - #124347

Merged
steipete merged 2 commits into
mainfrom
steipete/agent-delete-deregister
Aug 16, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/agent-delete-deregister

Conversation

@steipete

@steipete steipete commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Related: #109956 (deleted-agent registry leak only; this does not address the separate phantom-writer or retained-present-database policy questions)

What Problem This Solves

Fixes an issue where openclaw agents delete --force could leave the deleted agent's durable agent_databases row behind when no usable Gateway was reachable and the CLI used its local fallback. The database files were gone, but registry discovery still reported them until an operator happened to run openclaw doctor.

The live report showed the long-term effect clearly: 3 configured agents versus 15 registry rows after ordinary churn, with doctor later emitting Removed missing agent database registry entry and Skipped missing registered agent database for state the delete owner should have finalized itself.

Why This Change Was Made

The Gateway RPC deletion path already had the correct architecture: begin a durable deletion journal, remove roster authority, finish filesystem cleanup, deregister exact database ownership through the registry owner, and only then complete the journal. The leak was the CLI transport/credential fallback, which was a separate non-journaled implementation.

The fallback now joins the same lifecycle model. It records or resumes the deletion journal before roster mutation, retains database registration while any file cleanup is incomplete, atomically deregisters every row for the deleted agent through the registry owner's unregisterOpenClawAgentDatabases API, and completes the journal last. A crash before deregistration leaves the row plus unfinished journal; a crash after deregistration but before completion retries the idempotent deregistration and completes. No schema or schema-version change is involved.

The independent claws remove agent-deletion path now releases database registration through the same registry owner only after its existing removal state reaches complete. Doctor reconciliation remains unchanged as the safety net for old versions, crashes, and manual file removal.

User Impact

Completed agent deletion now leaves no stale database registry entry, including when the Gateway is unavailable or requires credentials and the CLI falls back locally. Interrupted local deletions can be retried by agent id even after the roster entry was already removed.

This deliberately does not remove the harmless empty agents/<id>/ container: the journal owns the agent and sessions subdirectories, while safely claiming the parent would require a separate empty/default-layout ownership check. Completed deletion-journal rows also remain durable one-row-per-agent tombstones until recreation claims them; changing that authority-retention contract is outside this repair.

Production LOC: +103/-28 (net +75) | Tests: +90/-0. The production growth is the durable offline deletion/recovery boundary that was previously absent, plus one registry-owner bulk removal operation; it replaces the non-resumable fallback rather than adding a second policy path.

AI-assisted implementation; the resulting lifecycle and failure semantics were reviewed directly and with the repository autoreview workflow.

Evidence

Before

Live reproduction on 2026-08-15 at eb13f5719f0:

before agents add                 0
after  agents add                 0
after  one agent turn             1
after  agents delete --force      1

Accumulated profile state: 3 configured agents, 15 agent_databases rows. Doctor then repaired the 12 deleted-agent rows downstream.

After

Exact built-tree macOS proof with isolated OPENCLAW_STATE_DIR, OPENCLAW_SKIP_CHANNELS=1, no Gateway port, and read-only local Ollama qwen3:4b-instruct:

before agents add                 0
after  agents add                 0
after  successful agent turn      1
after  agents delete --force      0
after  doctor --fix               0

The Ollama turn returned OK (30,260 input tokens, 2 output tokens). The subsequent doctor run completed without either registry reconciliation message.

Automated validation

  • node scripts/run-vitest.mjs src/commands/agents.delete.test.ts src/claws/lifecycle-state.test.ts src/infra/state-migrations.media-persistence-targets.test.ts src/gateway/server-methods/agents-mutate.test.ts — 155 tests passed across 4 shards.
  • The new command regression registers an actual database row, exercises the no-Gateway fallback, and asserts the row is gone. It fails on the pre-fix implementation because that fallback never called the registry owner.
  • The interrupted-delete regression fails cleanup after roster commit, verifies the journal and registry row remain, retries with the roster entry absent, and verifies deregistration plus journal completion.
  • Existing synthetic-orphan doctor coverage remains green and still asserts both repair messages plus row removal.
  • node scripts/check-changed.mjs — green on Blacksmith Testbox (tbx_01m043kxmskdhv8cp9pmw298sy, Actions run 31920094059).
  • pnpm build equivalent through the hydrated Testbox — green in 3m25s (tbx_01m044wgf3trv1yd6ebbrdpqsb, Actions run 31920942565; wrapper exit 0).
  • .agents/skills/autoreview/scripts/autoreview --mode branch --base origin/main --stream-engine-output — clean, no accepted/actionable findings.

@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 the commands Command implementations label Aug 16, 2026
@steipete
steipete marked this pull request as ready for review August 16, 2026 02:05
@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Aug 16, 2026
@steipete
steipete merged commit 6432ac3 into main Aug 16, 2026
254 checks passed
@steipete
steipete deleted the steipete/agent-delete-deregister branch August 16, 2026 02:16
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands Command implementations maintainer Maintainer-authored PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant