Skip to content

"Vibe-Trading: Your Personal Trading Agent"

refactor(swarm): _is_error_result via JSON parse with substring fallback

Haozhe Wufd9e5fc6894e
diff --git a/agent/src/swarm/worker.py b/agent/src/swarm/worker.py
index ebd1269c3..c351fa8a4 100644
--- a/agent/src/swarm/worker.py
+++ b/agent/src/swarm/worker.py
@@ -621,9 +621,27 @@ def _is_data_agent(agent_spec: SwarmAgentSpec) -> bool:
def _is_error_result(result: str) -> bool:
- """Heuristic: did a tool call return an error envelope?"""
- head = (result or "").strip()[:160].lower()
- return head.startswith("{") and ('"status": "error"' in head or '"status":"error"' in head)
+ """Did a tool call return a top-level error envelope?
+
+ Parses the result as JSON and checks for a top-level ``status == "error"``.
+ A nested ``status`` (e.g. inside ``data``) is intentionally ignored — only
+ the envelope matters for the deliverable contract.
+
+ Falls back to a fast substring check on the head for truncated or
+ non-JSON payloads, so the function is robust to streaming / partial
+ output without ever raising.
+ """
+ text = (result or "").strip()
+ if not text or not text.startswith("{"):
+ return False
+ try:
+ parsed = json.loads(text)
+ except (ValueError, TypeError):
+ # Truncated / non-JSON payload — keep the original heuristic so we
+ # never raise from a classifier on the worker hot path.
+ head = text[:160].lower()
+ return '"status": "error"' in head or '"status":"error"' in head
+ return isinstance(parsed, dict) and parsed.get("status") == "error"
def _classify_deliverable(
diff --git a/agent/tests/test_swarm_output_contract.py b/agent/tests/test_swarm_output_contract.py
index 30af4ce49..40d27c1ac 100644
--- a/agent/tests/test_swarm_output_contract.py
+++ b/agent/tests/test_swarm_output_contract.py
@@ -30,6 +30,7 @@ from src.swarm.store import SwarmStore
from src.swarm.worker import (
_classify_deliverable,
_is_data_agent,
+ _is_error_result,
_report_written,
)
import src.swarm.runtime as rt
@@ -153,3 +154,55 @@ def test_genuine_completion_still_succeeds(tmp_path, monkeypatch):
run = _run(tmp_path, None)
assert run.status == RunStatus.completed
assert run.final_report == REAL_REPORT
+
+
+# ---- _is_error_result: JSON parse + truncation fallback -------------------
+# Follow-up from #119: the substring head-match could (a) false-positive on
+# a nested ``status`` field and (b) false-negate when the envelope sat past
+# the 160-char head. Parsing the envelope as JSON pins both.
+
+
+def test_is_error_result_top_level_error():
+ assert _is_error_result('{"status": "error", "error": "bad key"}') is True
+ assert _is_error_result('{"status":"error"}') is True
+
+
+def test_is_error_result_top_level_ok():
+ assert _is_error_result('{"status": "ok", "content": "..."}') is False
+
+
+def test_is_error_result_nested_error_no_false_positive():
+ """A nested ``status`` (e.g. inside ``data``) must NOT count — only the
+ envelope status matters for the deliverable contract."""
+ nested = '{"status": "ok", "data": {"status": "error", "detail": "x"}}'
+ assert _is_error_result(nested) is False
+
+
+def test_is_error_result_error_past_substring_head():
+ """G2: an error envelope where ``status`` sits past the 160-char head
+ (long preamble in another field). Substring head-match used to miss
+ this; JSON parse catches it."""
+ long_field = "x" * 200
+ payload = '{"meta": "' + long_field + '", "status": "error"}'
+ assert _is_error_result(payload) is True
+
+
+def test_is_error_result_truncated_falls_back_to_substring():
+ """Truncated / unparseable JSON still gets the original substring
+ classifier; the function must never raise on the worker hot path."""
+ truncated = '{"status": "error", "trace": "...' # missing closing quote
+ assert _is_error_result(truncated) is True
+
+
+def test_is_error_result_non_json_safe():
+ assert _is_error_result("") is False
+ assert _is_error_result(None) is False # type: ignore[arg-type]
+ assert _is_error_result("plain text output") is False
+ assert _is_error_result("[1, 2, 3]") is False # JSON array, not envelope
+
+
+def test_is_error_result_other_status_values():
+ """Only ``"error"`` counts; ``"warning"`` / ``"degenerate"`` etc. are
+ not error envelopes (the worker still credits them as a tool call)."""
+ assert _is_error_result('{"status": "degenerate", "warning": "T=0"}') is False
+ assert _is_error_result('{"status": "warning"}') is False