Skip to content

fix: strip tool invocation artifacts from assistant text to prevent context explosion (#1492) - #2136

Open
xielixing wants to merge 1 commit into
GCWing:mainfrom
xielixing:fix/issue-1492
Open

fix: strip tool invocation artifacts from assistant text to prevent context explosion (#1492)#2136
xielixing wants to merge 1 commit into
GCWing:mainfrom
xielixing:fix/issue-1492

Conversation

@xielixing

Copy link
Copy Markdown

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 calls strip_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.rs module already has reusable contains_tool_invocation_artifacts() and strip_tool_invocation_artifacts() functions — they were previously only applied to Write tool content arguments. This PR applies them to assistant text output as well:

  1. round_executor.rs: After strip_bitfun_memory_citations, conditionally check and strip tool invocation artifacts from the assistant text, with a warn! log for observability.
  2. write_content_sanitizer.rs: Added regression test strips_leaked_tool_calls_xml_from_assistant_text covering the exact [Bug]: LLM 工具调用 XML 泄露导致快速无限膨胀污染上下文 #1492 scenario.

Validation

  • cargo check -p bitfun-core — passes
  • cargo 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: LLM 工具调用 XML 泄露导致快速无限膨胀污染上下文

1 participant