fix(agent): close tool-result tails on invalid-tool and truncated-tool early returns - #69810
Closed
fangliquanflq wants to merge 1 commit into
Closed
Conversation
…l early returns Invalid-tool exhaustion and truncated-tool early returns skipped finalize_turn, leaving role=tool transcripts that become tool→user on the next turn for strict providers. Call close_interrupted_tool_sequence before persist on those paths (same as interrupt aborts).
Collaborator
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?
Closes durable tool-result tails when the conversation loop stops early for invalid tool names or truncated tool-call arguments, so the next user turn is not
tool -> userfor strict providers (same failure class as #48879).Bug Cause
Invalid-tool 3-strike exhaustion, truncated JSON args (
finish_reason=tool_calls), and length/stream truncated-tool exhaustion after retries allpersist+returnwithout reachingfinalize_turn. Prior retries or successful tool batches leaverole=toolas the transcript tail.close_interrupted_tool_sequencealready fixes this for interrupt aborts, but these sibling early returns never called it.Reproduction Steps
messages[-1].role, then send another user message.Expected: transcript ends on
assistantwith the partial/error text; next turn isassistant -> user.Before fix: transcript ends on
tool; next turn istool -> user(Gemini/Claude reject or hallucinate).Fix
Call
close_interrupted_tool_sequence(messages, _final_response)immediately before_persist_sessionon the three early-return paths inagent/conversation_loop.py. Added behavior-contract tests for invalid-tool exhaustion and both truncated-tool paths.Related Issue
No issue
Type of Change
Changes Made
agent/conversation_loop.py-- close tool tails on invalid-tool exhaustion, truncated-JSON refusal, and length/stream truncated-tool exhaustiontests/agent/test_empty_tool_name_loop_dampening.py-- assert assistant tail after invalid-tool 3-strike partialtests/run_agent/test_run_agent.py-- assert assistant tail after truncated-tool early returns following a successful tool batchHow to Test
messages[-1].role == "assistant".scripts/run_tests.sh tests/agent/test_empty_tool_name_loop_dampening.py tests/run_agent/test_run_agent.py -q -k "closes_tool_tail or truncated_tool_args_detected or length_with_tool_calls_returns_partial"Checklist
Code
scripts/run_tests.shon relevant tests and they passDocumentation & Housekeeping
cli-config.yaml.exampleif I added/changed config keys -- N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows -- N/A