chore: prevent orphan tool messages when enable_snapshot_clean is on
#3832
+124
−3
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
Summary
This PR fixes a state-leak issue in
ChatAgentthat can produce invalid message history (orphantoolmessages) across agent reuse scenarios.Root Cause
ChatAgentkeeps snapshot-clean metadata in_tool_output_history.reset()previously did not clear this cache._clean_snapshot_in_memory()could rewrite a cleanedFUNCTION/toolrecord even if the original referenced records no longer existed in current memory.toolmessages (without precedingassistant.tool_calls), which can trigger strict backend validation errors (e.g., Azure/LiteLLM 400).Fix Approach
ChatAgent.reset()now clears_tool_output_history._clean_snapshot_in_memory(), only rewrite when referenced record UUIDs still exist in storage.Why this is safe
toolrecords.Tests
Added unit tests in
test/agents/test_chat_agent.py:test_chat_agent_reset_clears_tool_output_historytest_clean_snapshot_in_memory_skips_missing_recordsChecklist
Go over all the following points, and put an
xin all the boxes that apply.Fixes #issue-numberin the PR description (required)pyproject.tomlanduv lockIf you are unsure about any of these, don't hesitate to ask. We are here to help!