Skip to content

The agent that grows with you

fix(agent): strip stray arg tags to line boundary, not end of text

AdamPlatin123dfb0a8846e45
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py
index cfb6e700272..51c12f20f6b 100644
--- a/agent/agent_runtime_helpers.py
+++ b/agent/agent_runtime_helpers.py
@@ -62,14 +62,14 @@ _STRAY_TOOL_CALL_CLOSER_PATTERN = re.compile(
rf'</(?:{_NS_PREFIX}(?:{"|".join(_TOOL_CALL_TAG_NAMES)}|function))>\s*', re.IGNORECASE
)
-# A tool-call opener with no closer, or GLM-style argument markup
-# (<arg_key>/<arg_value>) outside any closed block, means the stream was
-# cut mid-serialization of a text-channel tool call (#101899). The call
-# can't be recovered; strip from the block-boundary opener (or the line
-# holding the first stray argument tag) to the end of the text.
+# An unclosed tool call is unrecoverable (#101899), so drop its remaining block.
+# Stray argument tags only identify fragment lines, not the rest of the text
+# (#102303). Require a line-start tag or a line-ending closer (wait</arg_value>)
+# so inline prose mentions and subsequent prose survive.
_UNTERMINATED_TOOL_CALL_PATTERN = re.compile(
rf'(?:^|\n)[ \t]*<{_NS_PREFIX}(?:{"|".join(_TOOL_CALL_TAG_NAMES)})\b[^>]*>.*$'
- r'|(?:^|\n)[^\n<]*</?arg_(?:key|value)\b.*$',
+ r'|(?:^|\n)[ \t]*</?arg_(?:key|value)\b[^\n]*'
+ r'|(?:^|\n)[^\n<]*</arg_(?:key|value)>[ \t\r]*(?=\n|$)',
re.DOTALL | re.IGNORECASE,
)
diff --git a/hermes_cli/cli_render.py b/hermes_cli/cli_render.py
index 195cb945ddb..aacff161fd2 100644
--- a/hermes_cli/cli_render.py
+++ b/hermes_cli/cli_render.py
@@ -63,11 +63,13 @@ def _strip_reasoning_tags(text: str) -> str:
r'</(?:(?:[\w.-]+:)?(?:tool_call|tool_calls|tool_result|function_call|function_calls|function))>\s*', '', cleaned,
flags=re.IGNORECASE,
)
- # Unterminated opener / stray <arg_key>/<arg_value> markup = stream cut
- # mid tool-call serialization (#101899); strip to end of text.
+ # Unclosed calls are unrecoverable (#101899), but stray argument markup
+ # only identifies fragment lines (#102303). Keep inline prose and later
+ # lines while still removing a line-ending fragment like wait</arg_value>.
cleaned = re.sub(
r'(?:^|\n)[ \t]*<(?:[\w.-]+:)?(?:tool_call|tool_calls|tool_result|function_call|function_calls)\b[^>]*>.*$'
- r'|(?:^|\n)[^\n<]*</?arg_(?:key|value)\b.*$',
+ r'|(?:^|\n)[ \t]*</?arg_(?:key|value)\b[^\n]*'
+ r'|(?:^|\n)[^\n<]*</arg_(?:key|value)>[ \t\r]*(?=\n|$)',
'',
cleaned,
flags=re.DOTALL | re.IGNORECASE,
diff --git a/tests/agent/test_strip_reasoning_tags_cli.py b/tests/agent/test_strip_reasoning_tags_cli.py
index cf8557544fb..6890445317c 100644
--- a/tests/agent/test_strip_reasoning_tags_cli.py
+++ b/tests/agent/test_strip_reasoning_tags_cli.py
@@ -6,6 +6,9 @@ final displayed assistant text (after streaming) without depending on the
AIAgent instance. It must stay in sync with run_agent.py::_strip_think_blocks
for tool-call tag coverage."""
+from functools import partial
+
+import pytest
from agent.agent_runtime_helpers import strip_think_blocks
from cli import _strip_reasoning_tags
@@ -57,11 +60,37 @@ class TestToolCallStripping:
def test_empty_string(self):
assert _strip_reasoning_tags("") == ""
- def test_cut_tool_call_stripped_to_visible_prefix(self):
- """Both strippers drop the unrecoverable tail; only prose survives."""
- assert _strip_reasoning_tags(_CUT_FRAGMENT) == "Both gates started."
- assert strip_think_blocks(None, _CUT_FRAGMENT).strip() == "Both gates started."
- assert strip_think_blocks(None, "Waiting.\n<tool_call>process_manage").strip() == "Waiting."
+ @pytest.mark.parametrize(
+ "stripper", [_strip_reasoning_tags, partial(strip_think_blocks, None)],
+ ids=["display", "storage"],
+ )
+ @pytest.mark.parametrize(
+ ("text", "expected"),
+ [
+ (_CUT_FRAGMENT, "Both gates started."),
+ (_CUT_FRAGMENT.split("\n", 1)[1], ""),
+ (_CUT_FRAGMENT + "\nThird line must survive.",
+ "Both gates started.\nThird line must survive."),
+ ("Waiting.\n<tool_call>process_manage", "Waiting."),
+ ],
+ ids=["original-fragment", "fragment-only", "prose-after-fragment", "unclosed-call"],
+ )
+ def test_cut_tool_call_stripped_to_visible_prefix(self, stripper, text, expected):
+ """A cut call is unrecoverable, but unrelated prose must not be lost."""
+ assert stripper(text).strip() == expected
+
+ @pytest.mark.parametrize(
+ "stripper", [_strip_reasoning_tags, partial(strip_think_blocks, None)],
+ ids=["display", "storage"],
+ )
+ @pytest.mark.parametrize("tag", ["arg_key", "arg_value", "/arg_key", "/arg_value"])
+ def test_bracketed_arg_tag_prose_and_tail_survive(self, stripper, tag):
+ text = (
+ "First line stays.\n"
+ f"The <{tag}> holds the parameter name.\n"
+ "Third line must survive."
+ )
+ assert stripper(text) == text
def test_complete_block_and_inline_prose_mentions_untouched(self):
for out in (_strip_reasoning_tags(_COMPLETE_WITH_PROSE),
@@ -69,4 +98,3 @@ class TestToolCallStripping:
assert "Use <function> in JS. The arg_key field maps to arg_value." in out
assert out.rstrip().endswith("Done.")
assert "<tool_call>" not in out
-