fix(agent): strip <think> blocks from stored assistant content
Inline reasoning tags in an assistant message's content field leak to every downstream consumer: messaging platforms (#8878, #9568), API replay of prior turns, session transcript, CLI recap, generated session titles, and context compression. _extract_reasoning() already captures the reasoning text into msg['reasoning'] separately, so the raw tags in content are redundant. Stripping once at the storage boundary in _build_assistant_message() cleans the content for every downstream path in one place — no per-platform or per-path stripper needed. Measured impact on a real MiniMax M2.7-highspeed session (per @luoyejiaoe-source, #9306): 55% of assistant messages started with <think> blocks, 51/100 session titles were polluted, 16% content-size reduction. 3 new regression tests in TestBuildAssistantMessage: closed-pair strip with reasoning capture, no-think-tag passthrough, and unterminated-block strip. Resolves #8878 and #9568. Originally proposed as PR #9250.
This commit is contained in:
14
run_agent.py
14
run_agent.py
@@ -7294,6 +7294,20 @@ class AIAgent:
|
||||
if reasoning_text:
|
||||
reasoning_text = _sanitize_surrogates(reasoning_text)
|
||||
|
||||
# Strip inline reasoning tags (<think>…</think> etc.) from the stored
|
||||
# assistant content. Reasoning was already captured into
|
||||
# ``reasoning_text`` above (either from structured fields or the
|
||||
# inline-block fallback), so the raw tags in content are redundant.
|
||||
# Leaving them in place caused reasoning to leak to messaging
|
||||
# platforms (#8878, #9568), inflate context on subsequent turns
|
||||
# (#9306 observed 16% content-size reduction on a real MiniMax
|
||||
# session), and pollute generated session titles. One strip at the
|
||||
# storage boundary cleans content for every downstream consumer:
|
||||
# API replay, session transcript, gateway delivery, CLI display,
|
||||
# compression, title generation.
|
||||
if isinstance(_san_content, str) and _san_content:
|
||||
_san_content = self._strip_think_blocks(_san_content).strip()
|
||||
|
||||
msg = {
|
||||
"role": "assistant",
|
||||
"content": _san_content,
|
||||
|
||||
Reference in New Issue
Block a user