From c8c1c77f39655ed39ae4237733749602707b7794 Mon Sep 17 00:00:00 2001 From: Alex Bujduveanu Date: Mon, 30 Nov 2026 15:37:02 +0000 Subject: [PATCH] fix(agent): run next_turn_params only for calls whose tool body succeeded A call whose arguments failed input_schema validation, or whose execute raised, still ran its next_turn_params callbacks and could change the next request. Only add a call to executed_calls when its result has no error, matching hook-blocked and invalid-JSON calls, which were already skipped. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- src/openrouter_agent/model_result.py | 4 +- .../test_next_turn_params_executed_calls.py | 84 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_next_turn_params_executed_calls.py diff --git a/src/openrouter_agent/model_result.py b/src/openrouter_agent/model_result.py index 37e11bd..611a796 100644 --- a/src/openrouter_agent/model_result.py +++ b/src/openrouter_agent/model_result.py @@ -932,6 +932,7 @@ async def _run(self) -> Any: outputs: List[Any] = unsent_results_to_api_format(hook_resolved_unsent) if hook_resolved_unsent else [] paused: List[ParsedToolCall] = [] + # Calls whose tool body ran without error; only these may drive next_turn_params. executed_calls: List[ParsedToolCall] = [] for call in executable_calls: tool = self._find_tool(call.name, tools) @@ -987,7 +988,8 @@ async def _run(self) -> Any: ) output = await self._tool_result_to_output(effective_call, tool, result) outputs.append(output) - executed_calls.append(effective_call) + if result.get("error") is None: + executed_calls.append(effective_call) self._tool_outputs.append(output) await self._append_event({"type": "tool.call_output", "output": output, "timestamp": _now_ms()}) step.tool_results.append(result) diff --git a/tests/unit/test_next_turn_params_executed_calls.py b/tests/unit/test_next_turn_params_executed_calls.py new file mode 100644 index 0000000..6b95304 --- /dev/null +++ b/tests/unit/test_next_turn_params_executed_calls.py @@ -0,0 +1,84 @@ +"""next_turn_params must run only for calls whose tool body ran and succeeded.""" + +from __future__ import annotations + +from typing import Any, Dict, List + +from pydantic import BaseModel + +from openrouter_agent import HookEntry, HookName, HooksManager, call_model, tool +from tests._fixtures import QueuedClient, text_response, tool_call_response + + +class SelectInput(BaseModel): + target: str + + +def _selector(seen: List[Any], **overrides: Any) -> Any: + def next_instructions(args: Any, _ctx: Any) -> str: + seen.append(args) + return "widened" + + options: Dict[str, Any] = { + "name": "select", + "input_schema": SelectInput, + "execute": lambda params, ctx: {"ok": True}, + "next_turn_params": {"instructions": next_instructions}, + } + options.update(overrides) + return tool(**options) + + +async def _run(selector: Any, arguments: str, hooks: Any = None) -> QueuedClient: + client = QueuedClient( + [ + tool_call_response("r1", "select", call_id="call_1", arguments=arguments), + text_response("r2", "done"), + ] + ) + request: Dict[str, Any] = {"model": "test/model", "input": "go", "instructions": "narrow", "tools": [selector]} + if hooks is not None: + request["hooks"] = hooks + await call_model(client, request).get_response() + return client + + +async def test_successful_call_applies_next_turn_params() -> None: + seen: List[Any] = [] + client = await _run(_selector(seen), '{"target": "x"}') + assert seen == [{"target": "x"}] + assert client.requests[1]["instructions"] == "widened" + + +async def test_hook_blocked_call_does_not_apply_next_turn_params() -> None: + seen: List[Any] = [] + hooks = HooksManager() + hooks.on(HookName.PreToolUse.value, HookEntry(handler=lambda payload, ctx: {"block": "no"})) + client = await _run(_selector(seen), '{"target": "x"}', hooks) + assert seen == [] + assert client.requests[1]["instructions"] == "narrow" + + +async def test_input_schema_rejected_call_does_not_apply_next_turn_params() -> None: + seen: List[Any] = [] + client = await _run(_selector(seen), '{"wrong": 1}') + assert seen == [] + assert client.requests[1]["instructions"] == "narrow" + + +async def test_failed_call_does_not_apply_next_turn_params() -> None: + seen: List[Any] = [] + + def boom(params: Any, _ctx: Any) -> Any: + raise RuntimeError("tool failed") + + client = await _run(_selector(seen, execute=boom), '{"target": "x"}') + assert seen == [] + assert client.requests[1]["instructions"] == "narrow" + + +async def test_invalid_json_call_does_not_apply_next_turn_params() -> None: + seen: List[Any] = [] + client = await _run(_selector(seen), "{not json") + assert seen == [] + assert client.requests[1]["instructions"] == "narrow"