fix: remove synthetic error message injection, fix session resume after repeated failures#2303
Merged
fix: remove synthetic error message injection, fix session resume after repeated failures#2303
Conversation
…er repeated failures Two changes to the error handler in the agent loop: 1. Remove the 'if not pending_handled' block that injected fake [System error during processing: ...] messages into conversation history. These polluted history, burned tokens on retries, and could violate role alternation by injecting as role=user. The tool_calls error-result path (role=tool) is preserved. 2. Append the error final_response as an assistant message when hitting the iteration limit, so session resume doesn't produce consecutive user messages.
This was referenced Mar 21, 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.
Summary
Two fixes to the error handler in the main agent loop (
run_agent.py).1. Remove synthetic error message injection
The
if not pending_handledblock injected[System error during processing: ...]messages into conversation history when API calls or response parsing failed. Problems:role: user, misattributing system errors to the userThe error is already printed to the user via
print(f"❌ {error_msg}"). The retry loop continues without needing a fake message.The
role: toolerror-result path for pending tool_calls is preserved — that one is necessary because the API requires matching tool results.2. Append error response at iteration limit
When all retries fail and the agent hits the iteration limit, the error
final_responsewas returned but never appended to messages. On session resume, this caused consecutive user messages ([user, user]). Now the error response is appended asrole: assistant.Live tested
[user, assistant, user, assistant]alternationrole: toolerror results (preserved)Closes #2263, #2253, #2236, #2231 (all reported the same role violation).
All 5671 tests pass.