refactor(event_loop): route message appends through Agent._append_messages#3131
Merged
opieter-aws merged 1 commit intoJul 9, 2026
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
|
Assessment: Approve Clean, well-scoped refactor. I verified Review notes
Nice consolidation that removes real duplication called out in #2836. |
zastrowm
approved these changes
Jul 9, 2026
This was referenced Jul 9, 2026
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.
Description
The
Agentexposes a single append chokepoint,_append_messages, that assigns a durabletracking_idand firesMessageAddedEventfor every message added to history. Three sites inevent_loop.py— the assistant model response, the tool-result message, and the cancelled-tool-result message — bypassed it and hand-replicated those steps inline. That duplication was a concern raised in #2836 .This routes all three appends through
agent._append_messages(...), matching the structured-output force path that already used it. The duplicated_ensure_tracking_id/append/ hook-invocation blocks collapse to one call each, and the now-unusedMessageAddedEventand_ensure_tracking_idimports are dropped. No behavior change: the sametracking_idassignment andMessageAddedEventfiring happen as before, and theyield ToolResultMessageEvent(...)ordering after the tool-result appends is preserved (the append is fully awaited before the yield). As a side benefit, the previously-untested cancel branch now gets the chokepoint's guarantees.This is a Python-only change. The TypeScript SDK already routes every append through
Agent._appendMessage/_appendMessageAndFireHooksand mintstrackingIdatMessageconstruction rather than via an "ensure" step, so it has no equivalent duplication to consolidate.The test fixtures needed a matching update. The event-loop tests use a
Mockagent whose_append_messageswas an auto-child mock that skipped the real logic; those fixtures now bind the realAgent._append_messagesso appends assign tracking ids and fireMessageAddedEventexactly as production does. The structured-output tests, which mock_append_messagesand don't inspectagent.messages, switch fromassert_called_once()to asserting the force prompt appears among the appended messages, since the assistant-response site now flows through the same mock too.Related Issues
Follow-up to #2836
Documentation PR
No documentation changes needed — internal refactor with no public API or behavior change.
Type of Change
Other: Internal refactor — no behavior or public API change.
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.