"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(agent): recover from native claude-cli tool attempts
Yufeng He9c3bf7bbb47f
diff --git a/README.md b/README.mdindex 6cc68b354..af43cfd93 100644--- a/README.md+++ b/README.md@@ -1087,7 +1087,7 @@ Authentication priority is `COPILOT_GITHUB_TOKEN`, `GH_TOKEN`, `GITHUB_TOKEN`, s### Claude Code CLI provider-The `claude-cli` provider uses the [Claude Code](https://claude.com/claude-code) CLI you already have installed and logged in. Each agent step runs one headless `claude -p` process that only chooses the next step — call these tools, or give the final answer. Vibe-Trading runs the tools itself, so the evidence ledger, grounding checks and swarm deliverable checks behave exactly as with an API provider. It does not read `ANTHROPIC_API_KEY`.+The `claude-cli` provider uses the [Claude Code](https://claude.com/claude-code) CLI you already have installed and logged in. Each agent step runs one headless `claude -p` process that only chooses the next step — call these tools, or give the final answer. Vibe-Trading runs the tools itself, so the evidence ledger, grounding checks and swarm deliverable checks behave exactly as with an API provider. It does not read `ANTHROPIC_API_KEY`. Native CLI tools stay disabled. If the model tries one, Vibe-Trading feeds the refusal back and asks for a host tool call instead of accepting a claim that the tools are unavailable.Log in once with Claude Code, then check that Vibe-Trading can see the login:diff --git a/agent/src/agent/context.py b/agent/src/agent/context.pyindex f1f00abd6..69e32e30f 100644--- a/agent/src/agent/context.py+++ b/agent/src/agent/context.py@@ -350,8 +350,10 @@ class ContextBuilder:tool_discovery = ("The complete available-tool catalog follows. Only loaded tools have full schemas ""in this request. Use load_tools(names=[...]) to load the exact names you need, "- "preferably together, then call them directly. Loading does not execute a tool. "- "No catalog entry is hidden by search or ranking.\n\n" + self.registry.catalog()+ "preferably together, then request them through the host's tool-call protocol. "+ "Loading does not execute a tool. "+ "No catalog entry is hidden by search or ranking.\n\n"+ + self.registry.catalog())return _SYSTEM_PROMPT.format(diff --git a/agent/src/agent/tool_loading.py b/agent/src/agent/tool_loading.pyindex 5a1d4fe72..4eb09c9dd 100644--- a/agent/src/agent/tool_loading.py+++ b/agent/src/agent/tool_loading.py@@ -78,8 +78,9 @@ class DeferredToolRegistry(ToolRegistry):# prefix/order for providers that cache common prompt prefixes.self._loaded.update(dict.fromkeys(names))result: dict[str, Any] = {- "status": "ok", "loaded": list(dict.fromkeys(names)),- "message": "Full schemas are available in the next request. Call the tools directly; loading has not executed them.",+ "status": "ok",+ "loaded": list(dict.fromkeys(names)),+ "message": "Full schemas are available in the next request. Request them through the host's tool-call protocol in your next reply; loading has not executed them.",}return result@@ -90,13 +91,16 @@ class LoadToolsTool(BaseTool):name = "load_tools"description = ("Load full argument schemas for tools named in the system catalog. "- "Select all tools needed for the next steps in one call, then call them "- "directly in the next turn. This only exposes schemas; it executes no tool."+ "Select all tools needed for the next steps in one call, then request them "+ "through the host's tool-call protocol in the next turn. This only exposes "+ "schemas; it executes no tool.")parameters = {- "type": "object", "properties": {+ "type": "object",+ "properties": {"names": {"type": "array", "items": {"type": "string"}, "minItems": 1},- }, "required": ["names"],+ },+ "required": ["names"],}repeatable = Truediff --git a/agent/src/providers/claude_cli.py b/agent/src/providers/claude_cli.pyindex 012b28132..46f72d11e 100644--- a/agent/src/providers/claude_cli.py+++ b/agent/src/providers/claude_cli.py@@ -72,7 +72,9 @@ def child_env() -> dict[str, str]:_PROTOCOL = ("\n\n## Tool-calling protocol\n""You cannot execute tools yourself; the host executes them and returns the "- "results in the next turn. Respond with action=tool_calls and the calls you "+ "results in the next turn. The CLI's native tools are disabled: never call "+ "them, and never interpret their refusal as host tools being unavailable. "+ "Respond with action=tool_calls and the calls you ""want (name + arguments that match the tool's JSON schema), or action=final ""with your complete answer in content once you have what you need.\n""Available tools (OpenAI function format):\n"@@ -351,6 +353,18 @@ def _usage(payload: dict[str, Any]) -> dict[str, int]:}+def _native_attempt_reason(payload: dict[str, Any]) -> str:+ """Identify a native tool attempt without parsing the model's prose."""+ if payload.get("permission_denials"):+ return "permission_denials is non-empty"+ turns = payload.get("num_turns")+ if isinstance(turns, int) and turns > 1:+ # Native tools are disabled for a one-decision request. Extra turns are+ # the remaining signal when the CLI does not itemize the denied calls.+ return f"num_turns={turns}"+ return ""++def _decide(*,model: str,@@ -406,6 +420,17 @@ def _decide(else [])if not calls:+ native_reason = _native_attempt_reason(payload) if allow_tools else ""+ if native_reason:+ user_input += (+ "\n\n[host] Native tool calls were attempted and refused; none was executed "+ f"({native_reason}). This does not mean the host tools are unavailable or broken. "+ "Do not call the CLI's native tools. Reply again with action=tool_calls "+ "using the host-provided schemas, or action=final with a complete answer "+ "in content if no tool is needed."+ )+ failure = f"native tool calls kept bypassing the host protocol ({native_reason})"+ continue# Under --json-schema, payload["result"] is the structured output as text, so# an empty content must not fall back to it: the user would see raw JSON.content = str(output.get("content") or "")diff --git a/agent/tests/test_claude_cli_provider.py b/agent/tests/test_claude_cli_provider.pyindex 6bedc1493..7090418cb 100644--- a/agent/tests/test_claude_cli_provider.py+++ b/agent/tests/test_claude_cli_provider.py@@ -136,6 +136,103 @@ def test_final_without_content_is_fed_back_and_retried(fake_cli) -> None:assert message.usage_metadata["output_tokens"] == 10+def test_native_attempt_is_fed_back_with_the_original_prompt(fake_cli) -> None:+ first = payload({"action": "final", "content": "No such tool available"})+ first["num_turns"] = 2+ set_replies, calls = fake_cli+ set_replies(+ {"payload": first},+ {+ "payload": payload(+ {+ "action": "tool_calls",+ "tool_calls": [+ {+ "name": "get_market_data",+ "arguments": {"codes": ["HYPE-USDT"]},+ }+ ],+ }+ )+ },+ )++ message = claude_cli.ClaudeCliLLM(tools=[tool("get_market_data")]).invoke(+ [{"role": "user", "content": "run the backtest"}]+ )++ assert message.tool_calls[0]["name"] == "get_market_data"+ stdin = calls()[1]["stdin"]+ assert "run the backtest" in stdin+ assert "Native tool calls" in stdin+ assert "action=tool_calls" in stdin+++def test_permission_denials_are_fed_back_instead_of_accepting_final(fake_cli) -> None:+ first = payload({"action": "final", "content": "tools are unavailable"})+ first["permission_denials"] = [{"tool_name": "bash"}]+ set_replies, calls = fake_cli+ set_replies(+ {"payload": first},+ {"payload": payload({"action": "final", "content": "recovered"})},+ )++ message = claude_cli.ClaudeCliLLM(tools=[tool("get_market_data")]).invoke(+ [{"role": "user", "content": "x"}]+ )++ assert message.content == "recovered"+ assert "Native tool calls" in calls()[1]["stdin"]+++def test_repeated_native_attempts_exhaust_the_existing_retry_budget(fake_cli) -> None:+ reply = payload({"action": "final", "content": "tools are broken"})+ reply["num_turns"] = 2+ set_replies, calls = fake_cli+ set_replies({"payload": reply})++ with pytest.raises(RuntimeError, match="native tool calls kept bypassing"):+ claude_cli.ClaudeCliLLM(tools=[tool("get_market_data")]).invoke(+ [{"role": "user", "content": "x"}]+ )++ assert len(calls()) == claude_cli._MAX_RETRIES + 1+++def test_valid_structured_calls_are_not_retried_after_native_metadata(fake_cli) -> None:+ reply = payload(+ {+ "action": "tool_calls",+ "tool_calls": [{"name": "get_market_data", "arguments": {}}],+ }+ )+ reply["permission_denials"] = [{"tool_name": "bash"}]+ set_replies, calls = fake_cli+ set_replies({"payload": reply})++ message = claude_cli.ClaudeCliLLM(tools=[tool("get_market_data")]).invoke(+ [{"role": "user", "content": "x"}]+ )++ assert message.tool_calls[0]["name"] == "get_market_data"+ assert len(calls()) == 1+++def test_single_turn_final_answer_does_not_trigger_native_retry(fake_cli) -> None:+ reply = payload({"action": "final", "content": "done"})+ reply["num_turns"] = 1+ reply["permission_denials"] = []+ set_replies, calls = fake_cli+ set_replies({"payload": reply})++ message = claude_cli.ClaudeCliLLM(tools=[tool("get_market_data")]).invoke(+ [{"role": "user", "content": "x"}]+ )++ assert message.content == "done"+ assert len(calls()) == 1++def test_final_never_echoes_the_structured_output_as_content(fake_cli) -> None:set_replies, _ = fake_cliset_replies({"payload": payload({"action": "final", "content": " "})})diff --git a/agent/tests/test_deferred_tool_loading.py b/agent/tests/test_deferred_tool_loading.pyindex bdb6cae75..01e0aff49 100644--- a/agent/tests/test_deferred_tool_loading.py+++ b/agent/tests/test_deferred_tool_loading.py@@ -47,6 +47,21 @@ def test_every_tool_is_visible_and_loading_preserves_exact_schema():assert len(deferred.get_definitions()) == len(source) + 1 # Full manifest.+def test_loading_message_uses_the_host_tool_protocol():+ deferred = DeferredToolRegistry(registry())++ result = deferred.expose(["read_observation_2"])++ assert result["message"] == (+ "Full schemas are available in the next request. Request them through "+ "the host's tool-call protocol in your next reply; loading has not executed them."+ )+ assert "host's tool-call protocol" in deferred.get("load_tools").description+ prompt = ContextBuilder(deferred, WorkspaceMemory()).build_system_prompt()+ assert "then request them through the host's tool-call protocol" in prompt+ assert "then call them directly" not in prompt++def test_loading_cannot_add_a_tool_outside_the_current_registry():deferred = DeferredToolRegistry(registry())before = request_definitions(deferred)