HKUDS/Vibe-Trading · error · ValueError

Invalid swarm agent id {agent_id!r}: expected one safe path

Error message

Invalid swarm agent id {agent_id!r}: expected one safe path segment

What it means

agent_artifact_dir builds per-worker artifact directories and requires agent_id to be one safe path segment: a non-empty string that is not '.'/'..' and contains no '/' or '\\'. Violations raise ValueError before any filesystem access; a subsequent resolve() check also blocks symlink escapes out of the artifact root.

Source

Thrown at agent/src/swarm/worker.py:369

    Args:
        run_dir: Root directory for the swarm run.
        agent_id: Single safe path segment identifying the agent.

    Raises:
        ValueError: If ``agent_id`` is not a single safe path segment or the
            resolved artifact directory is not exactly one level below the
            resolved ``run_dir/artifacts`` directory.
    """
    artifact_root = run_dir / "artifacts"
    if (
        not isinstance(agent_id, str)
        or not agent_id
        or agent_id in {".", ".."}
        or "/" in agent_id
        or "\\" in agent_id
    ):
        raise ValueError(
            f"Invalid swarm agent id {agent_id!r}: expected one safe path segment"
        )

    artifact_dir = artifact_root / agent_id
    resolved_root = artifact_root.resolve()
    resolved_dir = artifact_dir.resolve()
    try:
        relative = resolved_dir.relative_to(resolved_root)
    except ValueError as exc:
        raise ValueError(
            f"Invalid swarm agent id {agent_id!r}: artifact path escapes "
            "the run artifacts directory"
        ) from exc
    if len(relative.parts) != 1:
        raise ValueError(
            f"Invalid swarm agent id {agent_id!r}: artifact path must be "
            "one level below the run artifacts directory"
        )

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Use flat, sanitized ids like 'researcher-1' or 'worker-a3f'
  2. Generate agent ids yourself (uuid/slug) instead of trusting external input
  3. Normalize/validate ids at the boundary with the same rules (no separators, not ./..)

Example fix

# before
agent_artifact_dir(root, agent_id='team/worker-1')
# after
agent_artifact_dir(root, agent_id='team-worker-1')
Defensive patterns

Strategy: type-guard

Validate before calling

import re
agent_id = re.sub(r'[^A-Za-z0-9._-]', '-', agent_id or '').strip('-.') or 'worker-anon'

Type guard

def is_safe_agent_id(a) -> bool:
    return (isinstance(a, str) and bool(a) and a not in {'.','..'} and '/' not in a and '\\' not in a)

Try / catch

try:
    agent_artifact_dir(root, agent_id)
except ValueError as e:
    if 'Invalid swarm agent id' in str(e): agent_id = slugify(agent_id); retry
    else: raise

Prevention

When it happens

Trigger: Calling it with agent_id='worker/1', '..\\escape', '.', '', or a non-string; attacker-controlled ids from network input used as filenames.

Common situations: Passing composite worker names or URLs as agent ids; concurrent test runs probing path-shaped ids; ids containing backslashes on Linux after Windows-style generation.

Related errors


AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28). Data as JSON: /api/errors/990dd65aa28f83a4. Report an issue: GitHub.