fix(agent): add return_exceptions=True to asyncio.gather in context reference expansion - #62902
Closed
x7peeps wants to merge 2 commits into
Closed
Conversation
…eference expansion In preprocess_context_references_async(), multiple @ references are expanded concurrently via asyncio.gather(). Without return_exceptions, if one coroutine raises an unexpected exception, the entire gather crashes — discarding all other successful expansions. Added return_exceptions=True and guarded the loop against BaseException items (which can appear when a coroutine fails outside its internal try/except). Failed references are appended to the warnings list so the caller sees the error without the whole operation collapsing.
teknium1
reviewed
Jul 11, 2026
teknium1
left a comment
Contributor
There was a problem hiding this comment.
Thanks for isolating the concurrent expansion path.
Problems
- On current
main, every implemented expansion branch is inside_expand_reference()'sexcept Exceptionboundary (agent/context_references.py:225-243), while the gather is atagent/context_references.py:161-171. That means ordinary expansion failures already become(warning, None)before reachinggather; the newBaseExceptionresult branch changes behavior for cancellation/control-flow exceptions rather than the failure mode described here. - The PR adds no regression coverage. The concurrent tests at
tests/agent/test_context_refs_concurrent.py:25-53cover successful ordering only, not partial success after an escaping expansion failure.
Suggested changes
- Please add a current-main reproducer for an exception that can actually escape
_expand_reference(), and assert that successful sibling context is retained. - If cancellation is the intended case, define and test its propagation contract rather than converting it to a warning.
Automated hermes-sweeper review.
| ) | ||
| ), | ||
| return_exceptions=True, | ||
| ) |
Contributor
There was a problem hiding this comment.
_expand_reference() already catches all Exception subclasses for its implemented branches (agent/context_references.py:225-243), so this primarily changes handling for cancellation/control-flow exceptions. Please add a reproducer for an exception that can escape on current main, or preserve and test cancellation propagation explicitly.
Contributor
Author
|
Thanks for the review, @teknium1! 🙏 |
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
Add
return_exceptions=Truetoasyncio.gather()inpreprocess_context_references_async()to prevent a single failed reference expansion from crashing the entire gather and discarding all other successful expansions.Problem
In
agent/context_references.py, multiple@references are expanded concurrently viaasyncio.gather(). Withoutreturn_exceptions=True, if any single coroutine raises an unexpected exception (one that escapes the internaltry/exceptin_expand_reference), the entire gather propagates the exception — losing all other successfully expanded references.This is the same bug class as prior merged PRs #59954 and #61717.
Fix
return_exceptions=Trueto theasyncio.gather()callBaseExceptionitems (which can appear when a coroutine fails outside its internal try/except)Testing