"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(swarm): reject path-shaped agent ids before the retry rmtree
Haozhe Wu5d6fbef30200
diff --git a/agent/src/swarm/worker.py b/agent/src/swarm/worker.pyindex cbe772610..8f96b6346 100644--- a/agent/src/swarm/worker.py+++ b/agent/src/swarm/worker.py@@ -309,9 +309,48 @@ def agent_artifact_dir(run_dir: Path, agent_id: str) -> Path:The single source of truth for this path — shared with the retry loopin ``runtime.py`` so the two can never compute it differently and drift- apart.+ apart. The guard belongs here rather than only in+ ``clear_agent_artifacts`` because this path is also used by ``run_worker``+ for ``mkdir``; validating the shared constructor protects both directory+ creation and recursive cleanup from the same path escape.++ 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."""- return run_dir / "artifacts" / agent_id+ 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"+ )+ return artifact_dirdef clear_agent_artifacts(artifact_dir: Path) -> None:diff --git a/agent/tests/test_swarm_retry_artifact_isolation.py b/agent/tests/test_swarm_retry_artifact_isolation.pyindex 264790dbb..63bb5a2c8 100644--- a/agent/tests/test_swarm_retry_artifact_isolation.py+++ b/agent/tests/test_swarm_retry_artifact_isolation.py@@ -24,6 +24,8 @@ from __future__ import annotationsfrom pathlib import Pathfrom unittest.mock import patch+import pytest+from src.swarm.models import SwarmAgentSpec, SwarmTask, WorkerResultfrom src.swarm.runtime import SwarmRuntimefrom src.swarm.store import SwarmStore@@ -69,6 +71,28 @@ def test_agent_artifact_dir_matches_run_worker_construction(tmp_path: Path) -> Nassert agent_artifact_dir(run_dir, "analyst") == run_dir / "artifacts" / "analyst"+ "agent_id", ["..", "/abs/path", "", ".", "a/b", r"a\\b"]+)+def test_agent_artifact_dir_rejects_path_shaped_ids(+ tmp_path: Path, agent_id: str+) -> None:+ with pytest.raises(ValueError, match="agent id"):+ agent_artifact_dir(tmp_path / "run-x", agent_id)+++def test_agent_artifact_dir_rejects_canonical_symlink_escape(tmp_path: Path) -> None:+ run_dir = tmp_path / "run-x"+ artifact_root = run_dir / "artifacts"+ artifact_root.mkdir(parents=True)+ outside = tmp_path / "outside"+ outside.mkdir()+ (artifact_root / "analyst").symlink_to(outside, target_is_directory=True)++ with pytest.raises(ValueError, match="agent id"):+ agent_artifact_dir(run_dir, "analyst")++def test_clear_agent_artifacts_removes_nested_files_and_dirs(tmp_path: Path) -> None:artifact_dir = tmp_path / "artifacts" / "analyst"(artifact_dir / "charts").mkdir(parents=True)