fix(agent): strip orphan tool_calls in repair_message_sequence (#56980) - #800
Closed
hashbender wants to merge 1 commit into
Closed
fix(agent): strip orphan tool_calls in repair_message_sequence (#56980)#800hashbender wants to merge 1 commit into
hashbender wants to merge 1 commit into
Conversation
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.
What does this PR do?
Adds Pass 3 to
repair_message_sequencethat strips orphantool_callsfrom assistant messages whosetool_call_ids have no matchingtoolresult messages in the sequence. This prevents HTTP 400 "insufficient tool messages following tool_calls message" from strict providers (DeepSeek v4, Kimi) when context compaction, partial session resume, or retry loops leave dangling tool-call references.Related Issue
Fixes NousResearch#56980
Type of Change
Changes Made
agent/agent_runtime_helpers.py: Added Pass 3 inrepair_message_sequence— builds a set of alltool_call_ids present intoolmessages, then stripstool_callsentries from assistant messages whose ids are absent. Keeps assistant text content intact; only removes the orphan calls.tests/run_agent/test_message_sequence_repair.py: Added 3 tests covering the new pass — orphan tool_calls at sequence end, parallel tool_calls with partial results missing, and no-op when all tool_calls have matching results.How to Test
python -m pytest tests/run_agent/test_message_sequence_repair.py -v— all 28 tests should pass, including the 3 new ones (test_repair_strips_orphan_tool_calls_at_end,test_repair_strips_orphan_parallel_tool_calls,test_repair_preserves_complete_tool_calls).test_repair_strips_orphan_tool_calls_at_endwhich simulates the exact scenario from the issue: an assistant message withtool_callsat the end of the sequence with no matching tool result.Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/AMirror-of: NousResearch#56990
NousResearch#56990