-
Notifications
You must be signed in to change notification settings - Fork 873
Preserve actionable initialize errors after CLI process exit #702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shuofengzhang
wants to merge
2
commits into
anthropics:main
Choose a base branch
from
shuofengzhang:fix-initialize-error-during-execution-message
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴
ProcessErroris downgraded to a bareExceptionon line 262:pending_error = Exception(self._last_execution_error). This means callers using the documentedexcept ProcessError as e:pattern (README line 263) will not catch this error, and the structuredexit_code/stderrattributes are lost. Should beProcessError(self._last_execution_error, exit_code=e.exit_code, stderr=e.stderr).Extended reasoning...
What the bug is
On line 262 of
query.py, when aProcessErroris caught and_last_execution_erroris set, the code creates a new bareExceptionto replace it:This discards the
ProcessErrortype and its structured attributes (exit_code,stderr), replacing them with a plainExceptioncarrying only the error message string.How it propagates
The
pending_erroris stored inpending_control_results[request_id](line 267). When_send_control_request()retrieves this result, it checksisinstance(result, Exception)and raises it (lines 434-435). This propagates throughinitialize()and ultimately to the caller ofquery(). The caller receives a bareExceptioninstead of the expectedProcessError.Why existing code doesn't prevent it
The test added by this PR (
test_initialize_uses_error_during_execution_result_text) catches withexcept Exception as e:, which catches all exception types indiscriminately. If the test usedexcept ProcessError as e:— the pattern documented in the README — it would fail, exposing the type downgrade.Step-by-step proof
resultmessage withsubtype="error_during_execution"containing"No conversation found with session ID ab2c985b"_read_messages()stores this inself._last_execution_errortransport.read_messages()to raiseProcessError("Command failed with exit code 1", exit_code=1, stderr="Check stderr output for details")except Exception as e:block catches thisProcessErrorisinstance(e, ProcessError)is True and_last_execution_erroris set, line 262 createspending_error = Exception("No conversation found with session ID ab2c985b")— a bareExceptionExceptionis stored inpending_control_resultsand later raised by_send_control_request()except ProcessError as e:(as documented in README line 263) will NOT catch this — it falls through as an unhandledExceptione.exit_codeande.stderrare unavailable since plainExceptionhas no such attributesImpact
This breaks the public API contract for error handling.
ProcessErroris part of the SDK's public API (exported in__init__.py, documented in the README). Any SDK consumer following the recommended error handling pattern will experience unhandled exceptions in the specific scenario this PR aims to improve.Fix
Replace line 262 with:
This preserves the actionable error message (the PR's goal) while maintaining the correct exception type and structured attributes. The test should also be updated to catch
ProcessErrorspecifically to prevent future regressions.