fix: keep stdin open for can_use_tool permission responses#1106
Open
Echolonius wants to merge 1 commit into
Open
fix: keep stdin open for can_use_tool permission responses#1106Echolonius wants to merge 1 commit into
Echolonius wants to merge 1 commit into
Conversation
wait_for_result_and_end_input() kept stdin open until the first result only when SDK MCP servers or hooks were present. A can_use_tool callback also needs the control protocol: the CLI sends can_use_tool requests mid-turn and the SDK writes the permission response back over stdin. With only can_use_tool configured (the query() + AsyncIterable path its own ValueError directs users to), stdin closed as soon as the input stream was exhausted, so every permission request failed with "Tool permission request failed: Error: Stream closed" and the callback was never invoked. Include can_use_tool in the keep-stdin-open condition, matching the existing behavior for SDK MCP servers and hooks.
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.
Fixes #1105.
Problem
wait_for_result_and_end_input()keeps stdin open until the first result only when SDK MCP servers or hooks are present. Acan_use_toolcallback needs the same treatment: the CLI deliverscan_use_toolpermission requests over the control protocol mid-turn, and the SDK writes the permission response back over stdin.With only
can_use_toolconfigured (thequery()+AsyncIterablepath that the callback's ownValueErrordirects users to), stdin closed as soon as the input stream was exhausted. Every subsequent permission request then failed withTool permission request failed: Error: Stream closed, the callback was never invoked, and the model burned its turns retrying the tool.Fix
Add
self.can_use_toolto the keep-stdin-open condition — the same contract already applied for SDK MCP servers (#731) and hooks.Verification
can_use_toolonly, the callback never fired and every Write failed withStream closed; adding a no-op hook whose matcher never fires (which only flips this condition) made the same run succeed. After this fix, the no-hook case invokes the callback and completes:can_use_tool calls: ['Write'], file created.test_streaming_prompt_with_can_use_tool_waits_for_result: the mock transport enforces the real CLI contract — thecan_use_toolcontrol request arrives only after the input stream is exhausted, the result is not produced until the permission response has been written, and writes afterend_input()raise. Fails onmainwithRuntimeError: stdin closed; passes with the fix.pytest tests/test_query.py tests/test_tool_callbacks.py: 75 passed.ruff check/ruff format --checkclean.🤖 Generated with Claude Code
https://claude.ai/code/session_01YcMi8ny9DUBDf6y6m76HWk