Repository navigation
fix(agents): delete an agent whose container is gone (#1747) - #1748
Merged
Merged
Conversation
DELETE /api/agents/{name} looked up the Docker container first and treated its
absence as "agent not found". But an agent's identity lives in agent_ownership,
not Docker, so an agent with a live row and no container was simultaneously
invisible (the listing is Docker-as-truth, Invariant #11), undeletable, and
still holding its own name — escapable only by calling POST /start on an agent
you cannot see, which is undiscoverable and fails in exactly the situations that
produce a missing container.
Agents reach that state routinely. The #834 Phase 1c recovery flow leaves them
there BY DESIGN (metadata-only, "operator runs POST /start"), so the documented
recovery path has an undeletable window in it; a docker prune, daemon reset, or
a crash between the row write and container creation does the same.
The ephemeral branch immediately above already handles the identical "live row,
no container" residue — its comment states a half-discarded ghost "must be
force-discardable, never 404". Ordinary agents never got that treatment; this
extends the same reasoning to them.
Existence is now decided by the row: 404 requires BOTH no container AND no live
ownership row, so an unknown name still 404s and an already-soft-deleted agent
still 404s (is_agent_live is False for it). Container teardown becomes
conditional — calling container_stop(None) would otherwise raise into the
generic except and log a misleading error. Everything downstream (soft-delete,
queue cancel, Redis keyspace clearing, name reservation) keys off the agent
NAME and never needed a container.
Scoped to DELETE. The same three-line guard appears in stop/logs/activities;
those return 404 for a missing container too, but none of them traps the agent,
so they are left alone.
Note: the name stays reserved after this delete. That is the normal #834
soft-delete policy for every delete, not something this changes — the issue's
acceptance criterion claiming the name becomes immediately reusable was wrong.
Verified live: create agent, docker rm its container, DELETE -> 200 with
deleted_at set (was 404 forever). Unknown agent and already-deleted agent both
still 404.
Related to #1747
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Resolve by running |
vybe
approved these changes
Jul 23, 2026
vybe
left a comment
Contributor
There was a problem hiding this comment.
Validated via /validate-pr: focused fix, named regression tests, security scan clean. Approving for the 0.8.5 payload.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1747.
The bug
DELETE /api/agents/{name}decided existence from Docker:Identity lives in
agent_ownership, so an agent with a live row and no container was invisible (the listing is Docker-as-truth, Invariant #11), undeletable, and still holding its own name — escapable only by callingPOST /starton an agent you cannot see.The fix
404 now requires both no container and no live row; container teardown becomes conditional.
Everything downstream — soft-delete, queue cancel, Redis keyspace clearing, name reservation — keys off the agent name and never needed a container. Teardown is guarded because
container_stop(None)would otherwise raise into the genericexceptand log a misleading error.This is the same reasoning the ephemeral branch directly above already applies: it sits before the container lookup precisely so a half-discarded ghost ("live row, no container") is force-discardable, "never 404". Ordinary agents just never got it. A test pins that ordering so this change didn't disturb #69.
Verified live
Two corrections to the issue as I filed it
is_agent_name_reservedsees soft-deleted rows), not something this fix should change. The AC is corrected in the commit message; recreating under the same name still returns 409, correctly.delete,stop,logs,activities. My first patch attempt used a blanket string replace and would have changed all four; the assertion caught it. Onlydeletetraps the agent, so onlydeleteis changed. (activities404ing on a missing container is arguably odd for a DB-backed read, but that is a separate question.)Tests
tests/unit/test_1747_delete_without_container.py— 6 passed. The static checks parse the module withastand assert against the delete endpoint's source specifically, because that same guard exists in four functions and a file-wide string search would happily pass on the wrong one. Plus behavioural coverage ofis_agent_liveacross all three states (live / already-deleted / unknown) and the containerless soft-delete.Related suites: 858 passed, 3 skipped.