Repository navigation
fix(agent): refuse responses whose streamed tool call differs from the completed one - #39
Merged
alexb-openrouter merged 2 commits intoNov 23, 2026
Conversation
Contributor
|
I'll fix CI failures and address comments from users with write access that start with 'Devin'.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Risk: an app that shows
get_tool_calls_stream()to a user as its approval screen and then resumes withapprove_tool_calls=[id]could run a different call than the one the user saw. The stream view is rebuilt fromresponse.output_item.added/response.function_call_arguments.*events, while pending state and execution use thefunction_callitems inresponse.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 sendspay({"amount":1})in the stream andpay({"amount":1000})in the completed response gotamount=1displayed andamount=1000executed 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):
assert_streamed_tool_calls_match_response(events, response)instream_transformers.py. For every function call finalized in the stream (function_call_arguments.done), it finds the completed-responsefunction_callwith the samecall_id(or the same itemid) and raisesValueErrorif thenameor parsedargumentsdiffer.ModelResult._coerce_responseruns 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 laterapprove_tool_callsfor that ID has nothing to run.get_tool_calls_stream()andbuild_tool_call_stream()now also readcall_id(the generated SDK's snake_case field) before falling back to the itemid. Before, typed SDK events made the stream report the item ID (fc_...), which is not the IDapprove_tool_callsexpects.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 onmainand all 7 pass with the fix; the matching-call controls pass on both. I also checked before/after with the realOpenRouterclient 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 testsandpytest tests/unit(149 passed) pass locally.