Skip to content

bug: agent rename is not gated on volume-base ownership — the #1664 one-row-per-base invariant is enforced at create only #1671

Closed

Description

@obasilakis

Found during the /review of PR #1666 (#1664/#1665/#1667). That PR establishes the invariant that one ownership row claims one volume base and enforces it at agent creation. Rename — the only other producer of volume_base_name — is not gated, so it can still mint the collision.

Summary

PR #1666 added agent_ownership.volume_base_name and gated creation on it:

# services/agent_service/crud.py:298
if not config.ephemeral and db.is_volume_base_reserved(config.name):
    raise HTTPException(status_code=409, detail="Agent name unavailable: its data volumes still belong to another agent (it was renamed).")

db.is_volume_base_reserved has exactly two enforcement call-sites — create (above) and the purge/orphan sweep (cleanup_service.py:855,965). The rename endpoint checks neither the volume base nor anything that stands in for it:

# routers/agent_rename.py:95 — Docker container only
existing = get_agent_container(sanitized_name)

# db/agent_settings/metadata.py:137 — agent_name column only
taken = conn.execute(select(1).where(agent_ownership.c.agent_name == new_name)).first()

Neither sees volume_base_name, so renaming into a base another row already claims is permitted.

Component

Backend / Agent Rename (routers/agent_rename.py, db/agent_settings/metadata.py::rename_agent) × the #1664 volume-identity model

Priority

P1 — cross-tenant data exposure of the same class as #1667, but requires a two-rename sequence and is not a regression (pre-#1666 this path was equally ungated, with worse consequences).

Reproduction Steps

An ordinary ops swap is enough:

  1. Create agent prod-agent; enable file sharing (creates agent-prod-agent-public). Volumes live under base prod-agent.
  2. Rename prod-agent → prod-agent-old. Row: agent_name='prod-agent-old', volume_base_name='prod-agent'. Volumes keep the old base — by design (bug: #1581 orphan-volume sweep can destroy a live renamed agent's home volume (rename keeps old volume name+label) #1664).
  3. Create a new agent named prod-agent → 409, correctly refused by the bug: #1581 orphan-volume sweep can destroy a live renamed agent's home volume (rename keeps old volume name+label) #1664 create gate.
  4. Instead, create agent staging-agent, then rename staging-agent → prod-agent:
    • get_agent_container('prod-agent') → None (the container is agent-prod-agent-old now) → passes.
    • select(1).where(agent_name == 'prod-agent') → no row (it's prod-agent-old) → passes.
    • Rename succeeds. Row: agent_name='prod-agent', volume_base_name='staging-agent'.
  5. Two rows now claim base prod-agent: prod-agent-old via its pin, and prod-agent via its name.
  6. Enable file sharing on the new prod-agent → db.get_public_volume_name('prod-agent') → agent-prod-agent-public → volume_get finds the other agent's public volume → mounted.

Root Cause

The workspace volume is safe: it resolves through the row's own pin (lifecycle._workspace_volume_name). But get_public_volume_name / get_shared_volume_name name off the live agent name by design (db/agent_settings/file_sharing.py:60, db/shared_folders.py:222) — which #1666's is_volume_base_reserved docstring documents as intentional ("a renamed agent still creates new volumes under its CURRENT name … therefore legitimately owns volumes under two bases"). That design is sound only while a base has a single claimant. Rename is the one path that can give it two.

Impact

  1. Cross-agent, potentially cross-owner disclosure — the new prod-agent get-then-creates its way onto the old agent's agent-prod-agent-public; both containers then write the same disk. This is exactly the silent-adopt class bug: agent creation silently reuses a pre-existing Docker volume of the same name (get-then-create, no adopt/refuse decision) #1667 closed on the create path.
  2. Permanent volume leak — once two rows claim the base, the fix(volumes): rename-safe volume identity — sweep, purge, create and recovery (#1664, #1665, #1667) #1666 purge guard skips removal for both (is_volume_base_reserved(base) stays true after either row is purged), and the orphan sweep never reclaims it for the same reason. The volumes become unreachable and undeletable by any automated path.

PR #1666's purge path already anticipates this collision, but attributes it only to installs predating the create gate:

volume_base_name carries no unique constraint, and installs predating the create-time gate (crud.py, #1664) can hold a collision: agent new pinned to base old PLUS a live agent literally named old, both on the same volumes.

Rename can mint it fresh, post-fix. The purge handling is correctly fail-safe (skip, don't destroy), so this is a gap in prevention, not in safety.

Suggested Fix

Mirror the create gate in the rename endpoint, after the existing container/name checks:

# routers/agent_rename.py
if db.is_volume_base_reserved(sanitized_name):
    raise HTTPException(
        status_code=409,
        detail=(
            "Name unavailable: its data volumes still belong to another "
            "agent (it was renamed). Pick a different name."
        ),
    )

Enforcing it inside db.rename_agent's transaction (alongside the existing taken check) would be stronger — it closes the TOCTOU between the router check and the write, and covers any future caller. Either way it needs a regression test named for this issue; tests/unit/test_1664_renamed_agent_volume_safety.py has the fixtures (TestVolumeBaseOwnership) and is the natural home.

Worth considering alongside: a partial unique index on volume_base_name would make the invariant structural rather than checked, but the union semantics (a row claims a base via agent_name or volume_base_name) don't map onto a single-column constraint cleanly.

Related

Activity

  1. added
    priority-p1
    type-bug
    theme-infrastructure
    on Jul 17, 2026
  2. dolho commented on Jul 17, 2026

    @dolho
    Contributor

    Fixed in #1666 (74f4a9a7) rather than as a follow-up — will close when that PR merges.

    Reasoning for pulling it in: you scoped this as a prevention gap, not a safety one, which is accurate, but #1666's own purge guard assumes a single claimant. With two rows on a base, is_volume_base_reserved stays true after either is purged, so the guard skips both bases and the orphan sweep never reclaims them — the volumes become unreachable and undeletable by any automated path. Shipping the invariant enforced at one of its two producers would have left that guard resting on something the code doesn't guarantee.

    Implemented your stronger option: enforced inside db.rename_agent's transaction (chokepoint — closes the check-then-write gap, covers future callers), plus a router gate raised before the container is stopped/renamed, so a refusal leaves nothing half-done and returns an actionable 409 instead of that path's generic 500. That's the #1445 router+chokepoint pattern you were pointing at.

    One correction to the suggested one-liner — it refuses a legitimate rename-back: an agent created B and renamed to A keeps pin B, so is_volume_base_reserved('B') is True via its own row, and renaming A→B (which it owns, single claimant) would be refused and told its own volumes belong to someone else. So the predicate grew exclude_agent — "does anyone else claim this base?" — and rename passes itself.

    Verified on a real DB, your repro verbatim: the swap is now refused, exactly one row claims base prod afterwards, and B→A→B still works. 5 regression tests in TestRenameIsGatedOnVolumeBase.

    Re: the partial unique index — agreed it doesn't map cleanly. The claim is agent_name OR volume_base_name, so a single-column index can't express it, and materialising the base into every row to make it structural would trade away the zero-backfill property #1664 was built around. Checked-not-structural for now; worth revisiting if a third producer ever appears.

  3. added
    status-in-dev
    on Jul 17, 2026
  4. AndriiPasternak31 commented on Jul 17, 2026

    @AndriiPasternak31
    Contributor

    /claim — taking the rename-side gap: the one-row-per-volume-base invariant from #1666 is enforced at create (is_volume_base_reserved, crud.py:298) but not on rename. Picking this up as part of the open-ticket push; will sync with @dolho on the exact boundary before building.

  5. added
    status-in-progress
    on Jul 17, 2026
  6. github-actions commented on Jul 17, 2026

    @github-actions

    Claimed by @AndriiPasternak31. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/1671-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #1671
  7. removed
    status-in-progress
    on Jul 20, 2026
  8. removed
    status-in-dev
    on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

priority-p1theme-infrastructuretype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions