Skip to content

fix(agent): refuse responses whose streamed tool call differs from the completed one - #39

Merged
alexb-openrouter merged 2 commits into
mainfrom
devin/1795351812-bind-streamed-tool-calls
Nov 23, 2026
Merged

alexb-openrouter merged 2 commits into
mainfrom
devin/1795351812-bind-streamed-tool-calls

Conversation

@alexb-openrouter

@alexb-openrouter alexb-openrouter commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Risk: an app that shows get_tool_calls_stream() to a user as its approval screen and then resumes with approve_tool_calls=[id] could run a different call than the one the user saw. The stream view is rebuilt from response.output_item.added / response.function_call_arguments.* events, while pending state and execution use the function_call items in response.completed.response. Nothing checked that the two agree, and once the stream had yielded an ID, get_tool_calls_stream() skipped the terminal call with that ID. A response source that sends pay({"amount":1}) in the stream and pay({"amount":1000}) in the completed response got amount=1 displayed and amount=1000 executed after approval. Triggering this needs control of the raw Responses stream (a broken or malicious upstream or proxy). The effect stays inside the app that embeds the SDK.

Fix (fail closed):

  • New assert_streamed_tool_calls_match_response(events, response) in stream_transformers.py. For every function call finalized in the stream (function_call_arguments.done), it finds the completed-response function_call with the same call_id (or the same item id) and raises ValueError if the name or parsed arguments differ.
  • ModelResult._coerce_response runs it on both stream paths (async iterable and list of events) before returning the completed response. It raises before tool calls are extracted, pending state is saved, or any tool runs, so a later approve_tool_calls for that ID has nothing to run.
  • get_tool_calls_stream() and build_tool_call_stream() now also read call_id (the generated SDK's snake_case field) before falling back to the item id. Before, typed SDK events made the stream report the item ID (fc_...), which is not the ID approve_tool_calls expects.
before: stream shows pay {"amount":1}; pending pay {"amount":1000}; approve ["call_1"] -> ran amount=1000
after:  ValueError: Streamed function_call "call_1" does not match the function_call in the completed response; ... -> nothing saved, nothing ran

Calls that appear only in the completed response, or only in the stream, are not affected by the check. A stream-only call is never saved or run, and a terminal-only call is still yielded by get_tool_calls_stream() as before.

Tests: tests/unit/test_streamed_tool_call_binding.py. 5 of the 7 tests fail on main and all 7 pass with the fix; the matching-call controls pass on both. I also checked before/after with the real OpenRouter client and SSE decoder against a 127.0.0.1 mock Responses server (same item ID and distinct item IDs). ruff check, ruff format --check, mypy src tests and pytest tests/unit (149 passed) pass locally.

@devin-ai-integration

devin-ai-integration Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access that start with 'Devin'.

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration
devin-ai-integration Bot marked this pull request as ready for review November 22, 2026 12:51
@OpenRouterTeam OpenRouterTeam deleted a comment from devin-ai-integration Bot Nov 23, 2026
@alexb-openrouter
alexb-openrouter merged commit 5933bae into main Nov 23, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant