Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(swarm): save nested output files safely

xkam7arc2a20d220992
diff --git a/agent/src/swarm/worker.py b/agent/src/swarm/worker.py
index 5b2ae848b..bd17bcb47 100644
--- a/agent/src/swarm/worker.py
+++ b/agent/src/swarm/worker.py
@@ -8,7 +8,6 @@ from __future__ import annotations
import json
import logging
-import os
import time
from datetime import datetime, timezone
from pathlib import Path
@@ -958,14 +957,34 @@ def _write_summary(artifact_dir: Path, summary: str) -> None:
def _collect_artifacts(artifact_dir: Path) -> list[str]:
- """Collect all artifact file paths from agent's artifact directory.
+ """Collect regular artifacts as deterministic run-relative paths.
Args:
artifact_dir: Path to artifacts/{agent_id}/ directory.
Returns:
- List of artifact file path strings.
+ Sorted POSIX-style paths relative to the swarm run directory. Symlinks
+ and files that resolve outside the agent artifact directory are omitted.
"""
if not artifact_dir.exists():
return []
- return [str(p) for p in artifact_dir.iterdir() if p.is_file()]
+
+ run_dir = artifact_dir.parent.parent.resolve()
+ artifact_root = artifact_dir.resolve()
+ if not artifact_root.is_relative_to(run_dir):
+ return []
+
+ artifacts: list[str] = []
+ for path in artifact_dir.rglob("*"):
+ try:
+ if path.is_symlink():
+ continue
+ resolved = path.resolve(strict=True)
+ if not resolved.is_relative_to(artifact_root) or not resolved.is_file():
+ continue
+ artifacts.append(resolved.relative_to(run_dir).as_posix())
+ except (OSError, RuntimeError, ValueError):
+ # A concurrently removed file, symlink loop, or containment
+ # failure is not a durable artifact and must not escape the run.
+ continue
+ return sorted(artifacts)
diff --git a/agent/tests/test_swarm_output_contract.py b/agent/tests/test_swarm_output_contract.py
index 0ecf7c722..12b764779 100644
--- a/agent/tests/test_swarm_output_contract.py
+++ b/agent/tests/test_swarm_output_contract.py
@@ -34,6 +34,7 @@ from src.swarm.models import (
from src.swarm.store import SwarmStore
from src.swarm.worker import (
_classify_deliverable,
+ _collect_artifacts,
_is_data_agent,
_is_error_result,
_report_written,
@@ -297,3 +298,34 @@ def test_tool_result_event_status_and_evidence_credit_agree(
tool_result = next(event for event in events if event.type == "tool_result")
assert tool_result.data["status"] == expected_event_status
assert worker_result.status == expected_worker_status
+
+
+def test_collect_artifacts_recurses_and_returns_sorted_run_relative_paths(
+ tmp_path: Path,
+) -> None:
+ artifact_dir = tmp_path / "run" / "artifacts" / "analyst"
+ (artifact_dir / "nested").mkdir(parents=True)
+ (artifact_dir / "other").mkdir()
+ (artifact_dir / "summary.md").write_text("summary", encoding="utf-8")
+ (artifact_dir / "nested" / "report.json").write_text("{}", encoding="utf-8")
+ (artifact_dir / "other" / "report.json").write_text("{}", encoding="utf-8")
+
+ assert _collect_artifacts(artifact_dir) == [
+ "artifacts/analyst/nested/report.json",
+ "artifacts/analyst/other/report.json",
+ "artifacts/analyst/summary.md",
+ ]
+
+
+def test_collect_artifacts_rejects_symlink_escape(tmp_path: Path) -> None:
+ artifact_dir = tmp_path / "run" / "artifacts" / "analyst"
+ artifact_dir.mkdir(parents=True)
+ outside = tmp_path / "outside.txt"
+ outside.write_text("private", encoding="utf-8")
+ escape = artifact_dir / "escape.txt"
+ try:
+ escape.symlink_to(outside)
+ except (OSError, NotImplementedError):
+ pytest.skip("symlinks are not available on this platform")
+
+ assert _collect_artifacts(artifact_dir) == []