Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix: persist swarm worker reports and improve summary extraction

Peng Qian37e287c6c72a
diff --git a/agent/src/swarm/presets/equity_research_team.yaml b/agent/src/swarm/presets/equity_research_team.yaml
index 1ee7021fa..077b29412 100644
--- a/agent/src/swarm/presets/equity_research_team.yaml
+++ b/agent/src/swarm/presets/equity_research_team.yaml
@@ -25,7 +25,7 @@ agents:
tools: [bash, read_file, write_file, load_skill, read_url]
skills: [tushare, okx-market, yfinance, web-reader, global-macro]
max_iterations: 50
- timeout_seconds: 600
+ timeout_seconds: 900
max_retries: 1
- id: sector_analyst
@@ -50,7 +50,7 @@ agents:
tools: [bash, read_file, write_file, load_skill, factor_analysis]
skills: [tushare, yfinance, fundamental-filter, multi-factor, us-etf-flow, sector-rotation]
max_iterations: 50
- timeout_seconds: 600
+ timeout_seconds: 900
max_retries: 1
- id: stock_picker
@@ -76,7 +76,7 @@ agents:
tools: [bash, read_file, write_file, load_skill, backtest, factor_analysis]
skills: [tushare, yfinance, strategy-generate, technical-basic, multi-factor, earnings-revision]
max_iterations: 50
- timeout_seconds: 600
+ timeout_seconds: 900
max_retries: 1
- id: aggregator
@@ -102,7 +102,7 @@ agents:
tools: [bash, read_file, write_file]
skills: [report-generate]
max_iterations: 50
- timeout_seconds: 600
+ timeout_seconds: 900
max_retries: 1
tasks:
diff --git a/agent/src/swarm/worker.py b/agent/src/swarm/worker.py
index a5969523c..c81536981 100644
--- a/agent/src/swarm/worker.py
+++ b/agent/src/swarm/worker.py
@@ -179,9 +179,11 @@ def build_worker_prompt(
"- Do NOT write long Python code inside bash. Use write_file + bash.\n"
"- Do NOT fetch data with curl/requests. Use the patterns from load_skill (yfinance, OKX API via Python).\n"
"- If a script fails, read the error, fix with `edit_file`, re-run. Max 2 retries per script.\n\n"
- "**Phase 3 — Summarize (0 tool calls):**\n"
- "- Write your final findings as a concise markdown summary directly in your response.\n"
- "- Include specific numbers, dates, and actionable conclusions.\n"
+ "**Phase 3 — Summarize (MUST use write_file):**\n"
+ "- You MUST call `write_file` with path `report.md` to save your final report as a markdown file.\n"
+ "- This is REQUIRED, not optional. Your final response MUST include a write_file call for report.md.\n"
+ "- The report must include specific numbers, dates, and actionable conclusions.\n"
+ "- After writing report.md, output a brief 2-3 sentence summary in your text response.\n"
"- Respond in the same language as the task prompt."
)
@@ -296,9 +298,10 @@ def run_worker(
# Check timeout
elapsed = time.monotonic() - t0
if elapsed > timeout:
- summary = last_assistant_content or f"Worker timed out after {elapsed:.0f}s ({iteration} iterations)"
+ summary = _best_summary(messages, last_assistant_content) or f"Worker timed out after {elapsed:.0f}s ({iteration} iterations)"
_emit(event_callback, "worker_timeout", agent_id, task_id, {"elapsed": elapsed})
_write_summary(artifact_dir, summary)
+ _persist_messages(artifact_dir, messages)
return WorkerResult(
status="timeout",
summary=summary,
@@ -404,7 +407,8 @@ def run_worker(
for tc in response.tool_calls:
_emit(
event_callback, "tool_call", agent_id, task_id,
- {"tool": tc.name, "iteration": iteration},
+ {"tool": tc.name, "iteration": iteration,
+ "arguments": {k: v for k, v in tc.arguments.items() if k != "run_dir"}},
)
tc_start = time.monotonic()
args = {**tc.arguments, "run_dir": str(artifact_dir)}
@@ -413,16 +417,18 @@ def run_worker(
_emit(
event_callback, "tool_result", agent_id, task_id,
{"tool": tc.name, "elapsed_ms": int(tc_elapsed * 1000),
- "status": "ok", "iteration": iteration},
+ "status": "ok", "iteration": iteration,
+ "result": str(result)[:5000]},
)
messages.append(
ContextBuilder.format_tool_result(tc.id, tc.name, result[:10_000])
)
# Hit iteration limit — use last meaningful content as summary
- summary = last_assistant_content or f"Worker hit iteration limit ({max_iterations} iterations)"
+ summary = _best_summary(messages, last_assistant_content) or f"Worker hit iteration limit ({max_iterations} iterations)"
_emit(event_callback, "worker_iteration_limit", agent_id, task_id)
_write_summary(artifact_dir, summary)
+ _persist_messages(artifact_dir, messages)
return WorkerResult(
status="completed",
summary=summary,
@@ -433,6 +439,30 @@ def run_worker(
)
+def _best_summary(messages: list[dict], fallback: str) -> str:
+ """Extract the best summary from all assistant messages."""
+ texts = [
+ m["content"] for m in messages
+ if m.get("role") == "assistant" and m.get("content")
+ and len(m["content"].strip()) > 100
+ ]
+ if texts:
+ return max(texts, key=len)
+ return fallback
+
+
+def _persist_messages(artifact_dir: Path, messages: list[dict]) -> None:
+ """Persist messages to disk for post-mortem analysis."""
+ try:
+ path = artifact_dir / "messages.json"
+ path.write_text(
+ json.dumps(messages, ensure_ascii=False, indent=2, default=str),
+ encoding="utf-8",
+ )
+ except Exception:
+ logger.warning("Failed to persist messages to %s", artifact_dir, exc_info=True)
+
+
def _write_summary(artifact_dir: Path, summary: str) -> None:
"""Write worker summary to artifacts directory.