Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 63 additions & 8 deletions agent/think_scrubber.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,15 @@ def feed(self, text: str) -> str:
buf, self._CLOSE_TAGS,
)
if close_idx == -1:
# No close yet — hold back a potential partial
# close-tag prefix; discard everything else.
held = self._max_partial_suffix(buf, self._CLOSE_TAGS)
self._buf = buf[-held:] if held else ""
# No close yet — accumulate everything into _buf
# so flush() can recover the final answer if the
# model inlined reasoning + answer inside an
# unclosed <think> (see flush() docstring).
# _max_partial_suffix is no longer needed here
# because we keep the full buffer; the next feed()
# will prepend _buf to the new text and re-scan
# for close tags across the boundary.
self._buf = (self._buf or "") + buf
return "".join(out)
# Found close: discard block content + tag, continue.
buf = buf[close_idx + close_len:]
Expand Down Expand Up @@ -204,14 +209,64 @@ def feed(self, text: str) -> str:
def flush(self) -> str:
"""End-of-stream flush.

If still inside an unterminated block, held-back content is
discarded — leaking partial reasoning is worse than a
truncated answer. Otherwise the held-back partial-tag tail is
emitted verbatim (it turned out not to be a real tag prefix).
If still inside an unterminated block, the default behaviour is
to discard held-back content — leaking partial reasoning is
worse than a truncated answer.

However, some OpenAI-compatible LLM gateways (e.g. relays
fronting MiniMax-M1 / Step-3.7-Flash / Intern-S2-Preview)
inline the model's reasoning AND the final answer inside
``delta.content`` wrapped in a single ``<think>`` open tag with
NO matching ``</think>`` close. In that pattern the model
intends:

reasoning = "The user is asking for 2+2. The answer is 4."
final_answer = "4"

…and streams it as a single content sequence with the answer
on the line after the reasoning prose. Discarding everything
at flush() time throws away the final answer too, leaving the
agent with an empty ``content`` and forcing an "Empty response
from model — retrying" loop.

To recover the answer in this case without leaking reasoning
on truncated streams, we look for the last newline in the
held-back buffer and emit whatever came after it as the
visible response. Reasoning models that follow the
reasoning-then-newline-then-answer pattern are recovered; if
there is no newline (e.g. pure truncated reasoning with no
answer) the original discard-everything behaviour is
preserved.

Set ``recover_unclosed_final_answer=False`` on the scrubber
instance (or via the ``AICQ_HERMES_RECOVER_UNCLOSED_FINAL_ANSWER``
env var, see aicq-hermes plugin) to opt out and restore the
strict discard behaviour.
"""
if self._in_block:
held = self._buf
self._buf = ""
self._in_block = False
if not held:
return ""
# Opt-out knob for plugins/users who want the strict
# discard behaviour (e.g. when running against a gateway
# that emits properly closed tags).
if not getattr(self, "recover_unclosed_final_answer", True):
return ""
# Find the last newline; emit whatever came after it as
# the visible response. Reasoning models that follow the
# reasoning-then-newline-then-answer pattern are recovered.
last_nl = held.rfind("\n")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A newline does not establish that the following text is a final answer: an interrupted ordinary multiline `` trace has the identical shape. This would expose its last reasoning line, contrary to the existing strict non-leak contract. Please gate recovery on a signal that can distinguish this gateway format, or retain generic discard.

if last_nl != -1 and last_nl + 1 < len(held):
tail = held[last_nl + 1:]
tail = self._strip_orphan_close_tags(tail)
if tail:
self._last_emitted_ended_newline = tail.endswith("\n")
return tail
# No newline — fall through to the original discard
# behaviour. This preserves the existing test contract:
# _drive(s, ["<think>reasoning text with no close"]) == ""
return ""
tail = self._buf
self._buf = ""
Expand Down
55 changes: 55 additions & 0 deletions tests/agent/test_think_scrubber.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,61 @@ def test_flush_on_empty_scrubber(self) -> None:
s = StreamingThinkScrubber()
assert s.flush() == ""

def test_flush_recovers_final_answer_after_newline_in_unclosed_block(self) -> None:
"""Unclosed <think> with reasoning + newline + final answer.

Some OpenAI-compatible LLM gateways (e.g. relays fronting
MiniMax-M1 / Step-3.7-Flash / Intern-S2-Preview) inline the
model's reasoning AND the final answer inside ``delta.content``
wrapped in a single ``<think>`` open tag with NO matching
``</think>`` close. The model intends:

reasoning = "The user is asking for 2+2. The answer is 4."
final_answer = "4"

…and streams it as a single content sequence with the answer
on the line after the reasoning prose. flush() should recover
the final answer rather than discarding it.
"""
s = StreamingThinkScrubber()
deltas = ["<think>The user is asking for 2+2. The answer is 4.\n4"]
assert _drive(s, deltas) == "4"

def test_flush_recovers_final_answer_split_across_deltas(self) -> None:
"""Same recovery, but reasoning and answer arrive in separate deltas."""
s = StreamingThinkScrubber()
deltas = [
"<think>The user is asking for 2+2.",
" The answer is 4.\n",
"4",
]
assert _drive(s, deltas) == "4"

def test_flush_recovers_final_answer_multiline_reasoning(self) -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the inverse regression here: an unclosed multiline reasoning-only trace must still return empty. The current tests establish recovery examples but do not protect the scrubber's existing no-reasoning-leak guarantee.

"""Multi-line reasoning followed by the final answer.

The recovery picks the LAST newline, so multi-line reasoning
prose is correctly stripped and only the final answer is
emitted.
"""
s = StreamingThinkScrubber()
deltas = [
"<think>Let me think about this.\n",
"2+2 is definitely 4.\n",
"I'm confident the answer is 4.\n",
"4",
]
assert _drive(s, deltas) == "4"

def test_flush_opt_out_recovers_unclosed_final_answer(self) -> None:
"""Plugins can opt out of the recovery by setting
``recover_unclosed_final_answer=False`` on the scrubber instance.
"""
s = StreamingThinkScrubber()
s.recover_unclosed_final_answer = False
deltas = ["<think>reasoning\nfinal answer"]
assert _drive(s, deltas) == ""


class TestRealisticStreaming:
"""Character-by-character streaming must work as well as larger chunks."""
Expand Down