"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(swarm): redact worker event previews
Haozhe Wue7d72be57493
diff --git a/agent/src/swarm/worker.py b/agent/src/swarm/worker.pyindex 80de1e87b..18b469064 100644--- a/agent/src/swarm/worker.py+++ b/agent/src/swarm/worker.py@@ -30,6 +30,17 @@ logger = logging.getLogger(__name__)_DEFAULT_MAX_ITERATIONS = int(os.getenv("SWARM_WORKER_MAX_ITER", "50"))_DEFAULT_TIMEOUT_SECONDS = int(os.getenv("SWARM_WORKER_TIMEOUT", "300"))_MAX_TOKEN_ESTIMATE = 60_000+_SENSITIVE_TOOL_ARGUMENT_KEYS = {+ "api_key",+ "authorization",+ "content",+ "env",+ "headers",+ "passphrase",+ "password",+ "secret",+ "token",+}def _emit(@@ -335,8 +346,8 @@ def run_worker("role": "user","content": (f"[SYSTEM] You have {remaining} iterations remaining. "- "Stop calling tools and immediately output your final analysis summary as plain text. "- "Do not call any more tools."+ "If report.md is not written yet, make one final write_file call for report.md. "+ "Otherwise stop calling tools and output your final analysis summary as plain text."),})@@ -411,7 +422,7 @@ def run_worker(_emit(event_callback, "tool_call", agent_id, task_id,{"tool": tc.name, "iteration": iteration,- "arguments": {k: (v if len(str(v)) <= 200 else str(v)[:200] + "...") for k, v in tc.arguments.items() if k != "run_dir"}},+ "arguments": _preview_tool_arguments(tc.arguments)},)tc_start = time.monotonic()args = {**tc.arguments, "run_dir": str(artifact_dir)}@@ -421,7 +432,7 @@ def run_worker(event_callback, "tool_result", agent_id, task_id,{"tool": tc.name, "elapsed_ms": int(tc_elapsed * 1000),"status": "ok", "iteration": iteration,- "result_preview": str(result)[:500]},+ "result_preview": str(result)[:200]},)messages.append(ContextBuilder.format_tool_result(tc.id, tc.name, result[:10_000])@@ -455,6 +466,29 @@ def _best_summary(messages: list[dict], fallback: str) -> str:return fallback+def _preview_tool_arguments(arguments: dict) -> dict[str, str]:+ """Return a short, redacted argument preview for streamed events."""+ preview: dict[str, str] = {}+ for key, value in arguments.items():+ if key == "run_dir":+ continue+ if _is_sensitive_tool_argument(key):+ preview[key] = "[redacted]"+ continue+ text = str(value)+ preview[key] = text if len(text) <= 200 else text[:200] + "..."+ return preview+++def _is_sensitive_tool_argument(key: str) -> bool:+ """Return whether a tool argument name should be redacted in events."""+ normalized = key.strip().lower()+ return normalized in _SENSITIVE_TOOL_ARGUMENT_KEYS or any(+ marker in normalized+ for marker in ("api_key", "authorization", "password", "secret", "token")+ )++def _resolve_summary(artifact_dir: Path, fallback: str) -> str:"""Return report.md content if it exists, otherwise fall back to text."""report_path = artifact_dir / "report.md"diff --git a/agent/tests/test_swarm_worker_report.py b/agent/tests/test_swarm_worker_report.pyindex 2f30796d8..bb849ecf3 100644--- a/agent/tests/test_swarm_worker_report.py+++ b/agent/tests/test_swarm_worker_report.py@@ -2,11 +2,9 @@from __future__ import annotations-import jsonfrom pathlib import Path-from unittest.mock import MagicMock-from src.swarm.worker import _resolve_summary, _best_summary, WorkerResult+from src.swarm.worker import _preview_tool_arguments, _resolve_summary, _best_summarydef test_resolve_summary_reads_report_md(tmp_path: Path) -> None:@@ -54,3 +52,27 @@ def test_resolve_summary_handles_read_error(tmp_path: Path) -> None:"""_resolve_summary returns fallback if reading report.md fails."""result = _resolve_summary(Path("/nonexistent/path/xyz"), "fallback")assert result == "fallback"+++def test_preview_tool_arguments_redacts_sensitive_values() -> None:+ """Event previews should not leak file bodies or credentials."""+ preview = _preview_tool_arguments({+ "path": "report.md",+ "content": "# very long confidential report",+ "headers": {"Authorization": "Bearer secret-token"},+ "api_token": "secret-token",+ "run_dir": "/tmp/hidden",+ })++ assert preview == {+ "path": "report.md",+ "content": "[redacted]",+ "headers": "[redacted]",+ "api_token": "[redacted]",+ }+++def test_preview_tool_arguments_truncates_non_sensitive_values() -> None:+ """Non-sensitive event previews stay short."""+ preview = _preview_tool_arguments({"query": "A" * 250})+ assert preview["query"] == "A" * 200 + "..."