fix: strip tool invocation artifacts from assistant text to prevent context explosion (#1492) - #2136
Open
xielixing wants to merge 1 commit into
Open
fix: strip tool invocation artifacts from assistant text to prevent context explosion (#1492)#2136xielixing wants to merge 1 commit into
xielixing wants to merge 1 commit into
Conversation
…ontext explosion (GCWing#1492)
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.
Fix #1492: Strip tool invocation artifacts from assistant text to prevent context explosion
Problem
When a model outputs
<tool_calls>XML as plain text content (instead of structured tool-call deltas), the system re-parses it as actual tool calls, causing infinite recursion and context explosion. The leaked XML passes through to the next round's context unchanged.Root Cause
In
round_executor.rs, after receiving the assistant's text content, the code callsstrip_bitfun_memory_citations()but does NOT check for tool invocation artifacts. Leaked<tool_calls>XML passes through as-is to the next round's context, where it gets re-parsed and causes the infinite loop described in #1492.Fix
The
write_content_sanitizer.rsmodule already has reusablecontains_tool_invocation_artifacts()andstrip_tool_invocation_artifacts()functions — they were previously only applied to Write toolcontentarguments. This PR applies them to assistant text output as well:round_executor.rs: Afterstrip_bitfun_memory_citations, conditionally check and strip tool invocation artifacts from the assistant text, with awarn!log for observability.write_content_sanitizer.rs: Added regression teststrips_leaked_tool_calls_xml_from_assistant_textcovering the exact [Bug]: LLM 工具调用 XML 泄露导致快速无限膨胀污染上下文 #1492 scenario.Validation
cargo check -p bitfun-core— passescargo test -p bitfun-core write_content_sanitizer -- --nocapture— all 5 tests pass (4 original + 1 new regression test)Files Changed
src/crates/assembly/core/src/agentic/execution/round_executor.rs(9 lines added)src/crates/assembly/core/src/agentic/execution/write_content_sanitizer.rs(23 lines added)Closes #1492