Skip to content

fix(agents): delete an agent whose container is gone (#1747) - #1748

Merged
vybe merged 1 commit into
devfrom
fix/1747-delete-without-container
Jul 23, 2026
Merged

vybe merged 1 commit into
devfrom
fix/1747-delete-without-container

Conversation

@dolho

@dolho dolho commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1747.

The bug

DELETE /api/agents/{name} decided existence from Docker:

container = get_agent_container(agent_name)
if not container:
    raise HTTPException(status_code=404, detail="Agent not found")

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 calling POST /start on an agent you cannot see.

The fix

404 now requires both no container and no live row; container teardown becomes conditional.

container = get_agent_container(agent_name)
if not container and not db.is_agent_live(agent_name):
    raise HTTPException(status_code=404, detail="Agent not found")

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 generic except and 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

create agent -> docker rm -f agent-stuck-probe   (live row, no container)
DELETE  -> HTTP 200,  deleted_at set             (was 404 forever)

unknown agent         -> 404   (still)
already soft-deleted  -> 404   (still)

Two corrections to the issue as I filed it

  1. "the name is reusable" was wrong. After this delete the name stays reserved — that is the normal Soft-delete all entities + configurable data retention policy #834 soft-delete policy for every delete (180 days, is_agent_name_reserved sees 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.
  2. Scope. The same three-line guard appears in four endpoints in this module — delete, stop, logs, activities. My first patch attempt used a blanket string replace and would have changed all four; the assertion caught it. Only delete traps the agent, so only delete is changed. (activities 404ing 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 with ast and 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 of is_agent_live across all three states (live / already-deleted / unknown) and the containerless soft-delete.

Related suites: 858 passed, 3 skipped.

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]>
@github-actions

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validated via /validate-pr: focused fix, named regression tests, security scan clean. Approving for the 0.8.5 payload.

@vybe
vybe merged commit 1af50db into dev Jul 23, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants