Repository navigation
fix(query): guard malformed inbound frames and send control error response - #1538
Open
yniantongtian-oss wants to merge 1 commit into
Open
yniantongtian-oss wants to merge 1 commit into
yniantongtian-oss wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Closes #1515
Summary
Guards
Queryagainst malformed inbound frames from the transport so they no longer crash the background_read_messagesloop withKeyError,AttributeError, orTypeError, and ensures malformedcontrol_requestpayloads with a validrequest_idreceive an errorcontrol_response.Changes
src/claude_agent_sdk/_internal/query.pyQuery._read_messages: Drop non-dict frames with a warning; validatecontrol_response(responseis adictwith non-emptystrrequest_id),control_request(non-emptystrrequest_idbefore spawning handler),control_cancel_request(non-emptystrrequest_idbefore.pop()), andtranscript_mirror(non-emptystrfilePathandlistentriesbeforeenqueue).Query._handle_control_request: Guard missing/invalidrequest_id, and moverequest(dict) andsubtype(non-emptystr) validation inside thetryblock so malformed payloads send acontrol_responsewithsubtype: errorovertransport.write.Query._track_task_lifecycle: Validatetask_idis a non-emptystr(logging a warning on non-Nonenon-stringtask_id) and ensuretask_type/statusarestrbefore checking membership inDEFERRING_TASK_TYPESandTERMINAL_TASK_STATUSES.tests/test_query.pyTestMalformedFramescovering dropped malformed frames, unhashablecontrol_cancel_requestIDs with inflight requests, malformedtranscript_mirrorandsystemtask lifecycle frames, and malformedcontrol_requesterror responses across bothasyncioandtriobackends.Verification
ruff check src/ tests/ scripts/— passedruff format --check src/ tests/ scripts/— passedmypy src/ scripts/— passed (Success: no issues found in 34 source files)pytest tests/test_query.py tests/test_transcript_mirror.py tests/test_tool_callbacks.py --no-cov— 245 passedpytest tests/ --no-cov(Windowswin32, Python 3.13.14) — 1753 passed, 21 skipped