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
30 changes: 30 additions & 0 deletions src/openrouter_agent/model_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ async def _run(self) -> Any:
final_response_enabled = allow_final_response is not False
resolvable_pending = [c for c in calls if self._call_is_auto_resolvable(c, tools)]
if final_response_enabled and resolvable_pending:
self._assert_unique_call_ids(calls, tools)
turn_context = {"number_of_turns": turn_number + 1, "turn_request": current_request}
requires_approval, executable_calls, gate_results = await self._resolve_approval_gate(
calls, tools, turn_context
Expand Down Expand Up @@ -902,6 +903,7 @@ async def _run(self) -> Any:
continue
break

self._assert_unique_call_ids(calls, tools)
turn_context = {"number_of_turns": turn_number + 1, "turn_request": current_request}
requires_approval, executable_calls, hook_resolved_unsent = await self._resolve_approval_gate(
calls,
Expand Down Expand Up @@ -1051,6 +1053,25 @@ def _call_is_auto_resolvable(self, call: ParsedToolCall, tools: Sequence[Tool])
tool = self._find_tool(call.name, tools)
return bool(tool and is_auto_resolvable_tool(tool))

def _assert_unique_call_ids(self, calls: Sequence[ParsedToolCall], tools: Sequence[Tool]) -> None:
"""Refuse a batch the loop would execute when two calls share a `call_id`.

Approvals, rejections and tool outputs are all matched to calls by that
id, so a repeated id would let one decision apply to a different call.
The batch runs nothing. Batches with no executable calls are left to the
caller, who owns correlation for manual tools.
"""
if not any(self._call_is_auto_resolvable(call, tools) for call in calls):
return
seen: set[str] = set()
for call in calls:
if call.id in seen:
raise ValueError(
f'Response contains more than one function_call with call_id "{call.id}"; '
"refusing to execute the batch because tool outputs are matched to calls by call_id."
)
seen.add(call.id)

async def _build_resume_request(self, request: Mapping[str, Any]) -> Dict[str, Any]:
self._fresh_items_for_state = []
approved = set(self.options.get("approve_tool_calls") or [])
Expand All @@ -1059,6 +1080,15 @@ async def _build_resume_request(self, request: Mapping[str, Any]) -> Dict[str, A
if self._state is not None and self._state.unsent_tool_results:
outputs.extend(unsent_results_to_api_format(self._state.unsent_tool_results))
pending: List[ParsedToolCall] = self._state.pending_tool_calls if self._state is not None else []
unsent_ids = [result.call_id for result in (self._state.unsent_tool_results or [])] if self._state else []
seen_ids: set[str] = set(unsent_ids)
for call in pending or []:
if call.id in seen_ids:
raise ValueError(
f'Saved state has more than one tool call with call_id "{call.id}"; '
"refusing to resume because approvals are matched to calls by call_id."
)
seen_ids.add(call.id)
for call in pending or []:
tool = self._find_tool(call.name, self.options.get("tools") or [])
if call.id in rejected:
Expand Down
150 changes: 150 additions & 0 deletions tests/unit/test_duplicate_call_ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
"""Tool calls that share a call_id must not let one approval run another call."""

from __future__ import annotations

import dataclasses
from typing import Any, List

import pytest

from openrouter_agent import call_model, step_count_is, tool
from tests._fixtures import MemoryStateAccessor, QueuedClient, function_call_item, make_response, text_response


def _tools(executed: List[str]) -> List[Any]:
def read(_params: Any, _context: Any) -> Any:
executed.append("read_config")
return {"ok": True}

def delete(_params: Any, _context: Any) -> Any:
executed.append("delete_record")
return {"deleted": True}

return [
tool(name="read_config", input_schema=dict, output_schema=dict, execute=read, require_approval=True),
tool(name="delete_record", input_schema=dict, output_schema=dict, execute=delete, require_approval=True),
]


def _batch(first_id: str, second_id: str) -> dict:
return make_response(
"r1",
[
function_call_item(first_id, "read_config", '{"path":"/public"}'),
function_call_item(second_id, "delete_record", '{"id":"rec"}'),
],
)


async def test_approving_the_harmless_call_cannot_run_a_protected_call_with_the_same_id() -> None:
executed: List[str] = []
tools = _tools(executed)
state = MemoryStateAccessor()
client = QueuedClient([_batch("call_x", "call_x"), text_response("r2", "done")])

with pytest.raises(ValueError, match='more than one function_call with call_id "call_x"'):
await call_model(client, {"model": "m", "input": "go", "tools": tools, "state": state}).get_response()

assert executed == []
assert len(client.requests) == 1
assert state.stored is None or not state.stored.pending_tool_calls


async def test_distinct_ids_still_run_only_the_approved_call() -> None:
executed: List[str] = []
tools = _tools(executed)
state = MemoryStateAccessor()
client = QueuedClient([_batch("call_a", "call_b"), text_response("r2", "done")])

await call_model(client, {"model": "m", "input": "go", "tools": tools, "state": state}).get_response()
assert state.stored is not None
assert [c.id for c in state.stored.pending_tool_calls or []] == ["call_a", "call_b"]

await call_model(
client, {"model": "m", "input": "continue", "tools": tools, "state": state, "approve_tool_calls": ["call_a"]}
).get_response()

assert executed == ["read_config"]


async def test_auto_executed_batch_with_duplicate_ids_runs_nothing() -> None:
executed: List[str] = []

def run(_params: Any, _context: Any) -> Any:
executed.append("run")
return {"ok": True}

auto = tool(name="lookup", input_schema=dict, output_schema=dict, execute=run)
client = QueuedClient(
[
make_response("r1", [function_call_item("call_x", "lookup"), function_call_item("call_x", "lookup")]),
text_response("r2", "done"),
]
)

with pytest.raises(ValueError, match="call_x"):
await call_model(client, {"model": "m", "input": "go", "tools": [auto]}).get_response()

assert executed == []
assert len(client.requests) == 1


async def test_allow_final_response_path_refuses_duplicate_ids() -> None:
executed: List[str] = []
tools = _tools(executed)
client = QueuedClient([_batch("call_x", "call_x"), text_response("r2", "done")])

with pytest.raises(ValueError, match="call_x"):
await call_model(
client,
{
"model": "m",
"input": "go",
"tools": tools,
"state": MemoryStateAccessor(),
"stop_when": step_count_is(0),
"allow_final_response": True,
},
).get_response()

assert executed == []
assert len(client.requests) == 1


async def test_manual_only_batch_with_duplicate_ids_is_returned_to_the_caller() -> None:
manual = tool(name="exec_command", input_schema=dict, execute=False)
state = MemoryStateAccessor()
client = QueuedClient(
[
make_response(
"r1", [function_call_item("call_x", "exec_command"), function_call_item("call_x", "exec_command")]
)
]
)

result = call_model(client, {"model": "m", "input": "go", "tools": [manual], "state": state})
await result.get_response()

assert [c.id for c in await result.get_pending_tool_calls()] == ["call_x", "call_x"]


async def test_resume_refuses_saved_state_with_duplicate_pending_ids() -> None:
executed: List[str] = []
tools = _tools(executed)
state = MemoryStateAccessor()
client = QueuedClient([_batch("call_a", "call_b"), text_response("r2", "done")])
await call_model(client, {"model": "m", "input": "go", "tools": tools, "state": state}).get_response()
assert state.stored is not None
pending = state.stored.pending_tool_calls or []
state.stored = dataclasses.replace(
state.stored, pending_tool_calls=[pending[0], dataclasses.replace(pending[1], id="call_a")]
)

with pytest.raises(ValueError, match='more than one tool call with call_id "call_a"'):
await call_model(
client,
{"model": "m", "input": "continue", "tools": tools, "state": state, "approve_tool_calls": ["call_a"]},
).get_response()

assert executed == []
assert len(client.requests) == 1
Loading