Skip to content

The agent that grows with you

refactor(agent/think_scrubber): pre-lowercase tags, regex orphan-close strip, compact scan loops

Tekniumdbe1f6555cec
diff --git a/agent/think_scrubber.py b/agent/think_scrubber.py
index 82a27e3b2ae..bb41604e2d1 100644
--- a/agent/think_scrubber.py
+++ b/agent/think_scrubber.py
@@ -1,32 +1,17 @@
"""Stateful scrubber for reasoning/thinking blocks in streamed assistant text.
-The regex ``run_agent._strip_think_blocks`` is correct for a complete string but,
-run per-delta, erases an opening ``<think>`` that arrives alone in one delta, so
-downstream state machines never see the open tag and leak reasoning. This class
-centralises tag suppression upstream: partial tags at delta boundaries are held
-back until resolved, and ``flush()`` releases held-back prose that was not a tag.
-
-Usage::
-
- scrubber = StreamingThinkScrubber()
- for delta in stream:
- visible = scrubber.feed(delta)
- if visible:
- emit(visible)
- tail = scrubber.flush() # at end of stream
-
-Call ``reset()`` at the top of each turn so an interrupted block cannot taint
-the next turn. Tags handled (case-insensitive): ``<think>``, ``<thinking>``,
-``<reasoning>``, ``<thought>``, ``<REASONING_SCRATCHPAD>``.
-
-Boundary rule: an opening tag only starts a block at a block boundary (stream
-start, after a newline, or with only whitespace emitted on the current line), so
-prose that *mentions* ``<think>`` is not suppressed. Closed pairs
-(``<think>X</think>``) are always suppressed — a closed pair is intentional.
+The regex ``_strip_think_blocks`` is correct for a complete string but, run per-delta,
+erases an opening ``<think>`` that arrives alone, so downstream state machines leak
+reasoning. This class holds partial tags at delta boundaries until resolved; ``flush()``
+releases held-back prose that was not a tag. Call ``reset()`` at the top of each turn.
+Boundary rule: an open tag only starts a block at a block boundary (stream start, after a
+newline, or with only whitespace on the current line), so prose that *mentions*
+``<think>`` is not suppressed; closed pairs are always suppressed (intentional).
"""
from __future__ import annotations
+import re
from typing import Tuple
__all__ = ["StreamingThinkScrubber"]
@@ -41,18 +26,16 @@ class StreamingThinkScrubber:
whether an open tag at buffer position 0 sits at a block boundary).
"""
- _OPEN_TAG_NAMES: Tuple[str, ...] = (
- "think",
- "thinking",
- "reasoning",
- "thought",
- "REASONING_SCRATCHPAD",
- )
+ _OPEN_TAG_NAMES: Tuple[str, ...] = ("think", "thinking", "reasoning", "thought", "REASONING_SCRATCHPAD")
- # Literal tag strings so the hot path does string ops, not regex per feed().
- _OPEN_TAGS: Tuple[str, ...] = tuple(f"<{name}>" for name in _OPEN_TAG_NAMES)
- _CLOSE_TAGS: Tuple[str, ...] = tuple(f"</{name}>" for name in _OPEN_TAG_NAMES)
+ # Lowercased literal tags so the hot path does string ops, not regex per feed().
+ _OPEN_TAGS: Tuple[str, ...] = tuple(f"<{name.lower()}>" for name in _OPEN_TAG_NAMES)
+ _CLOSE_TAGS: Tuple[str, ...] = tuple(f"</{name.lower()}>" for name in _OPEN_TAG_NAMES)
_MAX_TAG_LEN: int = max(len(tag) for tag in _OPEN_TAGS + _CLOSE_TAGS)
+ # Orphan close tag plus trailing whitespace (matches _strip_think_blocks case 3).
+ _ORPHAN_CLOSE_RE = re.compile(
+ "(?:" + "|".join(re.escape(t) for t in _CLOSE_TAGS) + r")[ \t\n\r]*", re.IGNORECASE
+ )
def __init__(self) -> None:
self.reset()
@@ -65,11 +48,10 @@ class StreamingThinkScrubber:
def _emit(self, out: list[str], text: str) -> None:
"""Append visible prose to *out* (orphan close tags stripped) and track the newline flag."""
+ text = self._strip_orphan_close_tags(text)
if text:
- text = self._strip_orphan_close_tags(text)
- if text:
- out.append(text)
- self._last_emitted_ended_newline = text.endswith("\n")
+ out.append(text)
+ self._last_emitted_ended_newline = text.endswith("\n")
def feed(self, text: str) -> str:
"""Feed one delta; return the scrubbed visible portion.
@@ -90,15 +72,14 @@ class StreamingThinkScrubber:
# No close yet: hold back a possible partial close-tag prefix, drop the rest.
held = self._max_partial_suffix(buf, self._CLOSE_TAGS)
self._buf = buf[-held:] if held else ""
- return "".join(out)
+ break
buf = buf[close_idx + close_len:]
self._in_block = False
continue
- # Priority 1: closed <tag>X</tag> pair anywhere (no boundary gating —
- # even inline pairs are almost certainly leaked reasoning).
- # Priority 2: unterminated open tag at a block boundary (gated so
- # prose that mentions '<think>' isn't over-stripped). Earliest wins.
+ # Priority 1: closed <tag>X</tag> pair anywhere (even inline pairs are almost
+ # certainly leaked reasoning). Priority 2: unterminated open tag at a block
+ # boundary (gated so prose mentioning '<think>' isn't over-stripped). Earliest wins.
pair = self._find_earliest_closed_pair(buf)
open_idx, open_len = self._find_open_at_boundary(buf, out)
if pair is not None and (open_idx == -1 or pair[0] <= open_idx):
@@ -117,23 +98,19 @@ class StreamingThinkScrubber:
self._max_partial_suffix(buf, self._OPEN_TAGS),
self._max_partial_suffix(buf, self._CLOSE_TAGS),
)
- if held:
- self._emit(out, buf[:-held])
- self._buf = buf[-held:]
- else:
- self._emit(out, buf)
- return "".join(out)
+ self._emit(out, buf[:-held] if held else buf)
+ self._buf = buf[-held:] if held else ""
+ break
return "".join(out)
def flush(self) -> str:
"""End-of-stream flush.
- Inside an unterminated block the held-back content is discarded (leaking
- partial reasoning is worse than a truncated answer); otherwise the
- held-back tail is emitted verbatim. Always resets the boundary flag:
- intra-turn retries flush then stream again without ``reset()``, and a
- stale False flag made the new stream's opening ``<think>`` look mid-line.
+ Inside an unterminated block the held-back content is discarded (leaking partial
+ reasoning is worse than a truncated answer); otherwise the tail is emitted verbatim.
+ Always resets the boundary flag: intra-turn retries flush then stream again without
+ ``reset()``, and a stale False flag made the new stream's opening ``<think>`` look mid-line.
"""
tail = "" if self._in_block else self._buf
self._buf = ""
@@ -147,63 +124,43 @@ class StreamingThinkScrubber:
def _find_first_tag(buf: str, tags: Tuple[str, ...]) -> Tuple[int, int]:
"""Return (earliest_index, tag_length) over *tags* (case-insensitive), or (-1, 0)."""
buf_lower = buf.lower()
- best_idx = -1
- best_len = 0
- for tag in tags:
- idx = buf_lower.find(tag.lower())
- if idx != -1 and (best_idx == -1 or idx < best_idx):
- best_idx = idx
- best_len = len(tag)
- return best_idx, best_len
+ hits = [(idx, len(tag)) for tag in tags if (idx := buf_lower.find(tag)) != -1]
+ return min(hits) if hits else (-1, 0)
def _find_earliest_closed_pair(self, buf: str):
"""Return (start_idx, end_idx) of the earliest ``<tag>...</tag>`` pair, else None.
- Case-insensitive and non-greedy (closest close after the open wins),
- matching ``_strip_think_blocks`` case 1; the earliest open tag wins.
+ Case-insensitive and non-greedy (closest close after the open wins); the earliest open tag wins.
"""
buf_lower = buf.lower()
best: "tuple[int, int] | None" = None
for open_tag, close_tag in zip(self._OPEN_TAGS, self._CLOSE_TAGS):
- open_lower = open_tag.lower()
- close_lower = close_tag.lower()
- open_idx = buf_lower.find(open_lower)
+ open_idx = buf_lower.find(open_tag)
if open_idx == -1:
continue
- close_idx = buf_lower.find(close_lower, open_idx + len(open_lower))
- if close_idx == -1:
- continue
- if best is None or open_idx < best[0]:
- best = (open_idx, close_idx + len(close_lower))
+ close_idx = buf_lower.find(close_tag, open_idx + len(open_tag))
+ if close_idx != -1 and (best is None or open_idx < best[0]):
+ best = (open_idx, close_idx + len(close_tag))
return best
def _find_open_at_boundary(self, buf: str, already_emitted: list[str]) -> Tuple[int, int]:
"""Return the earliest block-boundary open-tag (idx, len), or (-1, 0)."""
buf_lower = buf.lower()
- best_idx = -1
- best_len = 0
+ best_idx, best_len = -1, 0
for tag in self._OPEN_TAGS:
- tag_lower = tag.lower()
- search_start = 0
- while True:
- idx = buf_lower.find(tag_lower, search_start)
- if idx == -1:
- break
- if self._is_block_boundary(buf, idx, already_emitted):
- if best_idx == -1 or idx < best_idx:
- best_idx = idx
- best_len = len(tag)
- break # first boundary hit for this tag is enough
- search_start = idx + 1
+ idx = buf_lower.find(tag)
+ while idx != -1 and not self._is_block_boundary(buf, idx, already_emitted):
+ idx = buf_lower.find(tag, idx + 1)
+ if idx != -1 and (best_idx == -1 or idx < best_idx):
+ best_idx, best_len = idx, len(tag)
return best_idx, best_len
def _is_block_boundary(self, buf: str, idx: int, already_emitted: list[str]) -> bool:
"""True iff position *idx* in *buf* is a block boundary.
- Boundary = position 0 with the prior emission ending in a newline (or
- nothing emitted yet), or any position whose preceding text on the current
- line is whitespace-only (when no newline precedes it in *buf*, the prior
- emission must also have ended with a newline).
+ Boundary = position 0 with the prior emission ending in a newline (or nothing emitted
+ yet), or any position whose preceding text on the current line is whitespace-only
+ (when no newline precedes it in *buf*, the prior emission must also have ended with a newline).
"""
prior_newline = (
already_emitted[-1].endswith("\n") if already_emitted else self._last_emitted_ended_newline
@@ -222,16 +179,11 @@ class StreamingThinkScrubber:
Full-length matches are real tags handled elsewhere, not held-back partials.
"""
- if not buf:
- return 0
buf_lower = buf.lower()
- max_check = min(len(buf_lower), cls._MAX_TAG_LEN - 1)
- for i in range(max_check, 0, -1):
+ for i in range(min(len(buf_lower), cls._MAX_TAG_LEN - 1), 0, -1):
suffix = buf_lower[-i:]
- for tag in tags:
- tag_lower = tag.lower()
- if len(tag_lower) > i and tag_lower.startswith(suffix):
- return i
+ if any(len(tag) > i and tag.startswith(suffix) for tag in tags):
+ return i
return 0
@classmethod
@@ -239,24 +191,4 @@ class StreamingThinkScrubber:
"""Remove close tags with no matching open (always noise) plus trailing whitespace."""
if "</" not in text:
return text
- text_lower = text.lower()
- out: list[str] = []
- i = 0
- while i < len(text):
- matched = False
- if text_lower[i:i + 2] == "</":
- for tag in cls._CLOSE_TAGS:
- tag_lower = tag.lower()
- tag_len = len(tag_lower)
- if text_lower[i:i + tag_len] == tag_lower:
- # Skip the tag and trailing whitespace (matches _strip_think_blocks case 3).
- j = i + tag_len
- while j < len(text) and text[j] in " \t\n\r":
- j += 1
- i = j
- matched = True
- break
- if not matched:
- out.append(text[i])
- i += 1
- return "".join(out)
+ return cls._ORPHAN_CLOSE_RE.sub("", text)