"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(agent): refuse an identical call on its second failure, not its first
Haozhe Wua113ee0c74df
diff --git a/README.md b/README.mdindex 20adef6a5..e05e91fde 100644--- a/README.md+++ b/README.md@@ -409,12 +409,13 @@ Most runs follow the same evidence path: route the request, load the right markeThe agent stops with a visible recovery request after eight consecutive tool-calliterations without a new successful observation. Repeating the same read result-or a failed call does not reset this budget. Identical failed calls are skipped-until a successful state-changing tool executes or a new run starts; changed-arguments can still execute. The recovery asks for an artifact path or permission-to rerun the missing research step, and the run remains failed even if older-metrics exist. This guard is separate from the wall-clock activity watchdog.-+or a failed call does not reset this budget. An identical call is refused from+its SECOND failure onward — one retry after a transient failure (a rate limit, a+network blip, a tool timeout) still runs — until a successful state-changing tool+executes or a new run starts; changed arguments can always execute. The recovery+asks for an artifact path or permission to rerun the missing research step, and+the run remains failed even if older metrics exist. This guard is separate from+the wall-clock activity watchdog.| Layer | What happens ||-------|--------------|diff --git a/agent/src/agent/loop.py b/agent/src/agent/loop.pyindex 9dd8d1f0d..2e61814d0 100644--- a/agent/src/agent/loop.py+++ b/agent/src/agent/loop.py@@ -2126,12 +2126,12 @@ class AgentLoop:# un-serialisable call would collapse into a single identity and# the second one would be skipped without ever running.dedup_key = self._identical_call_key(tc.name, tc.arguments)- if dedup_key is not None and dedup_key in self._tool_progress.failed:+ if dedup_key is not None and self._tool_progress.is_blocked(dedup_key):failed_result = json.dumps({"status": "error","skipped": True,- "reason": "This exact call already failed in this run. Change strategy or ask the user for help.",+ "reason": "This exact call already failed repeatedly in this run. Change strategy or ask the user for help.",})self._record_blocked_tool_call(diff --git a/agent/src/agent/tool_progress.py b/agent/src/agent/tool_progress.pyindex f20b4e9ca..258848ba3 100644--- a/agent/src/agent/tool_progress.py+++ b/agent/src/agent/tool_progress.py@@ -6,6 +6,15 @@ import hashlibimport jsonNO_PROGRESS_LIMIT = 8+#: Identical failures tolerated before an exact repeat is refused. Blocking+#: on the FIRST one makes a transient failure permanent: a rate limit, a+#: network blip or the 1800s tool timeout says nothing about the arguments,+#: and only a successful *mutating* call clears the ledger -- which a pure+#: research run may never make. The second identical failure is the one that+#: shows the call itself is the problem. #1353's loop reissued the same+#: rejected path ~35 times, so a threshold of 2 still kills it well inside+#: NO_PROGRESS_LIMIT.+FAILURE_BLOCK_THRESHOLD = 2RECOVERY_MESSAGE = ("I stopped because repeated tool attempts did not produce new information. ""I cannot reliably answer from the previous summary alone. Please provide "@@ -17,7 +26,7 @@ class ToolProgress:"""Bound repeated discovery without treating activity as new evidence."""def __init__(self) -> None:- self.failed: set[tuple[str, str]] = set()+ self.failed: dict[tuple[str, str], int] = {}self._observations: set[tuple[str, str | None, str]] = set()self._new_observation = Falseself.stalled_iterations = 0@@ -34,7 +43,7 @@ class ToolProgress:"""Record an executed call; synthetic skips must not enter this ledger."""if not success:if key is not None:- self.failed.add(key)+ self.failed[key] = self.failed.get(key, 0) + 1returnif not is_readonly:self.failed.clear()@@ -54,6 +63,18 @@ class ToolProgress:self._observations.add(observation)self._new_observation = True+ def is_blocked(self, key: tuple[str, str]) -> bool:+ """Whether this exact call failed often enough to refuse a repeat.++ Args:+ key: Canonical call identity (name, serialized arguments).++ Returns:+ ``True`` once the identity reached :data:`FAILURE_BLOCK_THRESHOLD`+ failures without an intervening successful mutation.+ """+ return self.failed.get(key, 0) >= FAILURE_BLOCK_THRESHOLD+def finish_iteration(self) -> bool:"""Return whether the run exhausted its consecutive no-progress budget."""self.stalled_iterations = (diff --git a/agent/tests/test_agent_loop_deterministic_cache.py b/agent/tests/test_agent_loop_deterministic_cache.pyindex 6abd2b9f0..0a549db7f 100644--- a/agent/tests/test_agent_loop_deterministic_cache.py+++ b/agent/tests/test_agent_loop_deterministic_cache.py@@ -175,12 +175,15 @@ def test_failed_deterministic_call_is_not_cached(agent_factory) -> None:agent,tool.name,run_dir,- [{"a": 1}, {"a": 1}, {"a": 2}],+ [{"a": 1}, {"a": 1}, {"a": 1}, {"a": 2}],)- assert len(tool.calls) == 2 # Changed arguments still execute.- assert len(messages) == 3- assert json.loads(messages[1]["content"])["skipped"] is True+ # One honest retry runs (a transient failure says nothing about the+ # arguments); the THIRD identical attempt is refused, and changed+ # arguments still execute.+ assert len(tool.calls) == 3+ assert len(messages) == 4+ assert json.loads(messages[2]["content"])["skipped"] is Trueassert not any(r["type"] == "tool_result_cached" for r in records)diff --git a/agent/tests/test_agent_loop_no_progress.py b/agent/tests/test_agent_loop_no_progress.pyindex 7e1d9f7e6..c3499df19 100644--- a/agent/tests/test_agent_loop_no_progress.py+++ b/agent/tests/test_agent_loop_no_progress.py@@ -90,7 +90,9 @@ def test_failed_discovery_stops_with_visible_recovery(tmp_path, vary_arguments):assert result["status"] == "failed"assert result["iterations"] == 8- assert tool.calls == (8 if vary_arguments else 1)+ # A repeated identity is refused on the SECOND failure, not the first:+ # one honest retry runs, the third attempt is blocked.+ assert tool.calls == (8 if vary_arguments else 2)assert "no_progress" in result["reason"]assert "path" in result["content"]assert "rerun" in result["content"]@@ -133,7 +135,9 @@ def test_failed_call_ledger_resets_for_a_new_run(tmp_path):first = loop.run("Read the artifact.")second = loop.run("Try again after I repaired the artifact.")assert first["iterations"] == second["iterations"] == 8- assert tool.calls == 2+ # Two executions per run (fail, retry, then blocked), and the second run+ # starts from an empty ledger rather than inheriting the first run's.+ assert tool.calls == 4def test_old_metrics_do_not_turn_no_progress_into_success(tmp_path):@@ -194,3 +198,44 @@ def test_authorization_denial_does_not_poison_an_unexecuted_call(tmp_path):loop._process_tool_calls([call], ContextBuilder, messages, trace, [], 2)trace.close()assert tool.calls == 1+++def test_a_transient_failure_survives_one_identical_retry(tmp_path):+ """The case the block is NOT for: a read that fails once for a reason that+ has nothing to do with its arguments.++ Only a successful *mutating* call clears the failed ledger, and a research+ run may have none, so refusing on the first failure made a rate limit /+ network blip / tool timeout permanent for the whole run.+ """+ tool = DiscoveryTool(+ [+ '{"status":"error","error":"rate limited, retry later"}',+ '{"status":"ok","data":"artifact"}',+ ]+ )+ loop, _, _ = build_loop(tmp_path, tool, DiscoveryLLM(finish_after=2))+ result = loop.run("Read the artifact.")++ assert tool.calls == 2, "the identical retry after a transient failure must run"+ assert result["status"] == "success"+++def test_failure_block_threshold_is_two_sided():+ """Both sides of the gate, so a future change cannot quietly move it."""+ from src.agent.tool_progress import ToolProgress++ progress = ToolProgress()+ key = ("read_document", '{"path":"missing.csv"}')++ progress.record("read_document", key, '{"status":"error"}', success=False)+ assert not progress.is_blocked(key), "one failure must not block a retry"++ progress.record("read_document", key, '{"status":"error"}', success=False)+ assert progress.is_blocked(key), "the second identical failure must block"++ progress.record(+ "repair_file", ("repair_file", "{}"), '{"status":"ok"}',+ success=True, is_readonly=False,+ )+ assert not progress.is_blocked(key), "a successful mutation clears the ledger"