From e392c27f7e6981313198c078dd6b3ef01aeb4b1f Mon Sep 17 00:00:00 2001
From: 04cb <0x04cb@gmail.com>
Date: Sat, 11 Apr 2026 00:47:23 +0800
Subject: [PATCH] fix(utils): anchor unclosed think-tag regex to string start
(#3004)
---
nanobot/utils/helpers.py | 4 ++--
tests/utils/test_strip_think.py | 29 +++++++++++++++++++++++++++++
2 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/nanobot/utils/helpers.py b/nanobot/utils/helpers.py
index 3b4f9f25a..1bfd9f18b 100644
--- a/nanobot/utils/helpers.py
+++ b/nanobot/utils/helpers.py
@@ -17,10 +17,10 @@ from loguru import logger
def strip_think(text: str) -> str:
"""Remove thinking blocks and any unclosed trailing tag."""
text = re.sub(r"[\s\S]*?", "", text)
- text = re.sub(r"[\s\S]*$", "", text)
+ text = re.sub(r"^\s*[\s\S]*$", "", text)
# Gemma 4 and similar models use ... blocks
text = re.sub(r"[\s\S]*?", "", text)
- text = re.sub(r"[\s\S]*$", "", text)
+ text = re.sub(r"^\s*[\s\S]*$", "", text)
return text.strip()
diff --git a/tests/utils/test_strip_think.py b/tests/utils/test_strip_think.py
index 6710dfc93..5828c6d1f 100644
--- a/tests/utils/test_strip_think.py
+++ b/tests/utils/test_strip_think.py
@@ -34,3 +34,32 @@ class TestStripThinkTag:
def test_empty_string(self):
assert strip_think("") == ""
+
+
+class TestStripThinkFalsePositive:
+ """Ensure mid-content / tags are NOT stripped (#3004)."""
+
+ def test_backtick_think_tag_preserved(self):
+ text = "*Think Stripping:* A new utility to strip `` tags from output."
+ assert strip_think(text) == text
+
+ def test_prose_think_tag_preserved(self):
+ text = "The model emits at the start of its response."
+ assert strip_think(text) == text
+
+ def test_code_block_think_tag_preserved(self):
+ text = "Example:\n```\ntext = re.sub(r\"[\\s\\S]*\", \"\", text)\n```\nDone."
+ assert strip_think(text) == text
+
+ def test_backtick_thought_tag_preserved(self):
+ text = "Gemma 4 uses `` blocks for reasoning."
+ assert strip_think(text) == text
+
+ def test_prefix_unclosed_think_still_stripped(self):
+ assert strip_think("reasoning without closing") == ""
+
+ def test_prefix_unclosed_think_with_whitespace(self):
+ assert strip_think(" reasoning...") == ""
+
+ def test_prefix_unclosed_thought_still_stripped(self):
+ assert strip_think("reasoning without closing") == ""