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
- Use flat, sanitized ids like 'researcher-1' or 'worker-a3f'
- Generate agent ids yourself (uuid/slug) instead of trusting external input
- 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
- Generate agent ids internally (uuid/slug), never from raw input
- Apply the same one-segment rule at every boundary that names directories
- Fuzz-test ids containing separators and dot segments
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
- invalid preset name: {name!r}
- run_id {run_id!r} must be a bare run directory name
- survival_prob must be in (0.0, 1.0], got {survival_prob}
- tenor_years must be strictly positive, got {tenor_years}
- spread_bps must be non-negative, got {spread_bps}
AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28).
Data as JSON: /api/errors/990dd65aa28f83a4.
Report an issue: GitHub.