Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/openrouter_agent/model_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
84 changes: 84 additions & 0 deletions tests/unit/test_next_turn_params_executed_calls.py
Original file line number Diff line number Diff line change
@@ -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"
Loading