Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: keep a backticked tool-call tag in the answer instead of cutting it (#16132)

Daniel Han5ce3d7cce838
diff --git a/studio/backend/core/inference/tool_call_parser.py b/studio/backend/core/inference/tool_call_parser.py
index 31ab1a22e44..f004d6e7f8f 100644
--- a/studio/backend/core/inference/tool_call_parser.py
+++ b/studio/backend/core/inference/tool_call_parser.py
@@ -1063,9 +1063,16 @@ def _mask_blocked_bodies(
enabled_tool_names,
*,
think: bool = False,
+ mentions: bool = False,
) -> tuple:
- """With think, reasoning is masked so a rehearsed call cannot execute; parse only, strip passes think=False. Returns (masked_text, bodies), restored in order."""
+ """With think, reasoning is masked so a rehearsed call cannot execute; parse only, strip passes think=False. With
+ mentions (display strips), a tag alone in backticks is masked so no strip arm reads it as an opener or closer.
+ Returns (masked_text, bodies), restored in order."""
spans = _blocked_markerless_body_spans(text, enabled_tool_names)
+ if mentions:
+ mention_spans = _tool_healing.tag_mention_spans(text)
+ if mention_spans:
+ spans = _merge_spans(spans + mention_spans)
if think:
# Merged, not just sorted: sorting alone moves the masking cursor backward and re-appends the reasoning block of a blocked body unmasked.
think_spans = _tool_healing._think_spans_outside_tool_markup(text)
@@ -1375,7 +1382,7 @@ def strip_tool_markup(
)
# Reasoning stays verbatim, display included; a literal think marker in call arguments is data and goes with the call; blocked call bodies are masked.
- masked, bodies = _mask_blocked_bodies(text, enabled_tool_names)
+ masked, bodies = _mask_blocked_bodies(text, enabled_tool_names, mentions = True)
result = _tool_healing.strip_outside_think(masked, _strip_segment)
if bodies:
restored = _unmask_blocked_bodies(result, bodies)
@@ -1767,7 +1774,7 @@ class StreamingMarkupStripper:
# Mask as strip_tool_markup does: consumers get append-only snapshots, so a corrupted one
# emitted earlier could never be repaired.
- masked, bodies = _mask_blocked_bodies(text, self._enabled_tool_names)
+ masked, bodies = _mask_blocked_bodies(text, self._enabled_tool_names, mentions = True)
result = _tool_healing.strip_outside_think(masked, _seg)
if not bodies:
return result
@@ -2548,9 +2555,14 @@ def _parse_function_xml(
allow_incomplete: bool = True,
) -> list[dict]:
out: list[dict] = []
- # Skip openers inside an open parameter value, else they become a second call.
+ mention_spans = _tool_healing.tag_mention_spans(content)
+ # Skip openers inside an open parameter value, else they become a second call; one alone in backticks names the format.
inside = _tool_healing._OpenParameterIndex(content, **_OPEN_PARAMETER_TAGS).inside
- func_starts = [fm for fm in _TC_FUNC_START_RE.finditer(content) if not inside(fm.start())]
+ func_starts = [
+ fm
+ for fm in _TC_FUNC_START_RE.finditer(content)
+ if not inside(fm.start()) and not _tool_healing.in_tag_mention(mention_spans, fm.start())
+ ]
for idx, fm in enumerate(func_starts):
# group(1) is ``<function=name>``, group(2) is ``<function name="...">``.
func_name = fm.group(1) or fm.group(2)
diff --git a/studio/backend/core/tool_healing.py b/studio/backend/core/tool_healing.py
index 20d835d04e9..003d01ef4df 100644
--- a/studio/backend/core/tool_healing.py
+++ b/studio/backend/core/tool_healing.py
@@ -233,6 +233,75 @@ _CODE_SPAN_RE = re.compile(
re.DOTALL | re.MULTILINE,
)
+# A single tool tag (and at most a name) alone in an inline code span (`<tool_call>`, `[TOOL_CALLS]name`) names the
+# format: no call body fits inside. No lookahead past the closing run, so append-only growth never revokes a match.
+# Runs of three or more are fences; reasoning tags decide the think segmentation, not a call.
+_TAG_MENTION_RE = re.compile(
+ r"(?<!`)(``?)[ \t]*("
+ r"<(?!/?think(?:ing)?>)[^<>`\n]+>[\w.\-]*"
+ r"|\[/?(?:TOOL_CALLS|ARGS|CALL_ID)\][\w.\-]*"
+ r")[ \t]*\1"
+)
+# Any tag still pairs its backticks above, but only tool-call families are returned: masking `<div>` changes no strip
+# output. A match never crosses a newline, so pairing restarts per line and only lines holding a tool tag are scanned.
+_TOOL_TAG_PREFIXES = (
+ "<tool",
+ "</tool",
+ "<function",
+ "</function",
+ "<param",
+ "</param",
+ "<arg_",
+ "</arg_",
+ "<|",
+ "<|",
+ "[",
+)
+_TOOL_TAG_HINT_RE = re.compile(
+ r"</?(?:tool|function|param|arg_)|<[||]|\[/?(?:TOOL_CALLS|ARGS|CALL_ID)\]"
+)
+
+
+def tag_mention_spans(text: str) -> list[tuple[int, int]]:
+ """Ordered spans of the tag inside each lone-tag inline code span (backticks excluded)."""
+ if "`" not in text:
+ return []
+ spans = []
+ line_end = 0
+ for hint in _TOOL_TAG_HINT_RE.finditer(text):
+ if hint.start() < line_end:
+ continue
+ line_start = text.rfind("\n", 0, hint.start()) + 1
+ line_end = text.find("\n", hint.end())
+ if line_end < 0:
+ line_end = len(text)
+ spans.extend(
+ m.span(2)
+ for m in _TAG_MENTION_RE.finditer(text, line_start, line_end)
+ if m.group(2).startswith(_TOOL_TAG_PREFIXES)
+ )
+ if not spans:
+ return spans
+ # One inside a real call (found with the mentions blanked, so none reads as an opener) is that call's data:
+ # masking it would let the call's strip swallow a mask and drop every mention's protection.
+ parts, prev = [], 0
+ for start, end in spans:
+ parts.append(text[prev:start])
+ parts.append(" " * (end - start))
+ prev = end
+ parts.append(text[prev:])
+ calls = _tool_call_markup_spans("".join(parts))
+ if not calls:
+ return spans
+ return [s for s in spans if not any(cs <= s[0] < ce for cs, ce in calls)]
+
+
+def in_tag_mention(spans, pos: int) -> bool:
+ """Whether ``pos`` falls in one of ``tag_mention_spans`` (a span may start at ``pos``)."""
+ i = bisect.bisect_right(spans, (pos, float("inf"))) - 1
+ return i >= 0 and spans[i][0] <= pos < spans[i][1]
+
+
# Above this size skip the balanced scan; the linear regex catch-all bounds pathological output.
_MAX_BRACKET_SCAN_CHARS = 1_000_000
@@ -1073,10 +1142,13 @@ def parse_tool_calls_from_text(
i = bisect.bisect_right(coverage_starts, pos) - 1
return i >= 0 and pos < coverage_max_ends[i]
+ mention_spans = tag_mention_spans(content) if "<function" in content else []
func_starts = [
fm
for fm in _TC_FUNC_START_RE.finditer(content)
if not inside(fm.start())
+ # `<function=NAME>` alone in backticks names the format; the display keeps it, so it must not run.
+ and not in_tag_mention(mention_spans, fm.start())
and not _in_think(fm.start())
and not _in_blocked(fm.start())
and not _covered(fm.start())
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 1d5ac990066..189d1ae9479 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -6632,10 +6632,10 @@ def _strip_tool_xml_for_display(
return _TOOL_XML_RE.sub(_keep_inactive_rehearsal, seg)
return _TOOL_XML_CLOSED_RE.sub("", seg)
- # Mask blocked-call bodies (prose) so these passes do not edit them.
+ # Mask blocked-call bodies and lone backticked tags (prose) so these passes do not edit them.
from core.inference.tool_call_parser import _mask_blocked_bodies, _unmask_blocked_bodies
- masked, bodies = _mask_blocked_bodies(text, enabled_tool_names)
+ masked, bodies = _mask_blocked_bodies(text, enabled_tool_names, mentions = True)
result = strip_outside_think(masked, _strip_segment)
if not bodies:
return result
@@ -6664,6 +6664,55 @@ _DISPLAY_GEMMA_CALL_RE = _re.compile(r"(?<!\w)call\s*:")
_DISPLAY_GEMMA_TAIL_RE = _re.compile(r"(?<!\w)c(?:a(?:l(?:l\s*)?)?)?\Z")
+# A backticked tool tag whose closing backticks have not arrived: it may still become a lone-tag mention (prose,
+# see ``tool_healing.tag_mention_spans``), so a stream must not show it and then take it back.
+_PENDING_MENTION_RE = _re.compile(
+ r"(?<!`)(``?)[ \t]*("
+ r"(?:<(?!/?think(?:ing)?>)[^<>`\n]*(?:>[\w.\-]*)?|\[/?[A-Z_]*(?:\][\w.\-]*)?)"
+ r"[ \t]*)(`*)\Z"
+)
+_PENDING_MENTION_MAX_CHARS = 256
+
+
+def _pending_mention_cut(text: str) -> int:
+ """Where to cut a cumulative stream so a still-open backticked tool tag is held back, else -1. The cut keeps
+ the backticks, so for a tag the strip would remove anyway the held display equals the full strip."""
+ lo = max(0, len(text) - _PENDING_MENTION_MAX_CHARS)
+ # The tag holds no backtick, so its opening run is the last one before any trailing closers.
+ end = len(text)
+ while end > lo and text[end - 1] == "`":
+ end -= 1
+ i = text.rfind("`", lo, end)
+ if i < 0:
+ return -1
+ while i > lo and text[i - 1] == "`":
+ i -= 1
+ m = _PENDING_MENTION_RE.match(text, i)
+ if m is None or len(m.group(3)) >= len(m.group(1)):
+ return -1
+ tag = m.group(2).rstrip(" \t")
+ if not any(tag.startswith(s) or s.startswith(tag) for s in _DISPLAY_STRIP_SENTINELS):
+ return -1
+ return m.start(2)
+
+
+def _hold_pending_mention(text: str, auto_heal_tool_calls: bool) -> str:
+ """A cumulative stream event with a still-open backticked tool tag held back; the strips only remove text
+ with Auto-Heal on, so only then can a shown tag be taken back."""
+ if not auto_heal_tool_calls:
+ return text
+ cut = _pending_mention_cut(text)
+ return text[:cut] if cut >= 0 else text
+
+
+def _held_mention_display(display_strip, raw_text: str, auto_heal_tool_calls: bool):
+ """The display a turn owes when it ends with ``_hold_pending_mention`` still holding a backticked tag, else None.
+ Its closing backticks never came, so it is not a mention: the full strip decides, as for a non-streaming reply."""
+ if not raw_text or not auto_heal_tool_calls or _pending_mention_cut(raw_text) < 0:
+ return None
+ return display_strip.strip(raw_text)
+
+
# After the first sentinel: ``P + O + B`` with P free of ``<``, ``[``, ``{`` and ``call:``, O one of these openers.
_DISPLAY_HOLD_OPENER_RE = _re.compile(
r'<tool_call>(?:\s*<function(?:=[\w.\-]+|\s+name="[\w.\-]+")>)?'
@@ -6897,14 +6946,18 @@ class _ReasoningSpanGuard:
auto_heal_tool_calls: bool,
enabled_tool_names: Optional[set] = None,
wrapped_so_far = None,
+ hold: bool = True,
) -> str:
+ """``hold=False`` is the end-of-turn strip: a backticked tag still open there is final text."""
wrap = self._wrap_for(text, wrapped_so_far)
display = self._display_strip(auto_heal_tool_calls, enabled_tool_names)
if wrap is None:
- return display.strip(text)
+ return display.strip(_hold_pending_mention(text, auto_heal_tool_calls and hold))
# Clamp: mid-stream the close tag has not arrived yet, so the whole text is trace.
end = min(len(self._OPEN) + int(wrap.get("len", 0)) + len(self._CLOSE), len(text))
- return text[:end] + display.strip(text[end:])
+ return text[:end] + display.strip(
+ _hold_pending_mention(text[end:], auto_heal_tool_calls and hold)
+ )
def _display_strip(
self, auto_heal_tool_calls: bool, enabled_tool_names
@@ -32155,11 +32208,38 @@ async def produce_openai_chat_completions(
enabled_tool_names = _gguf_display_tool_names,
)
reasoning_extractor = _new_chat_reasoning_extractor()
+ _gguf_turn_raw = ""
_stream_usage = None
_stream_timings = None
_stream_finish = None
approval_flush_pending = False
+ def _release_held_mention():
+ nonlocal prev_text, _gguf_turn_raw
+ clean = _held_mention_display(
+ _gguf_display_strip, _gguf_turn_raw, _gguf_auto_heal_tool_calls
+ )
+ _gguf_turn_raw = ""
+ if clean is None:
+ return []
+ new_text = clean[len(prev_text) :]
+ prev_text = clean
+ chunks = []
+ if not new_text:
+ return chunks
+ reasoning_delta, visible_delta = reasoning_extractor.feed(new_text)
+ if reasoning_delta:
+ api_monitor.mark_first_token(monitor_id)
+ chunks.append(
+ _gguf_chat_delta_line(
+ ChoiceDelta(reasoning_content = reasoning_delta)
+ )
+ )
+ if visible_delta:
+ api_monitor.append_reply(monitor_id, visible_delta)
+ chunks.append(_gguf_chat_delta_line(ChoiceDelta(content = visible_delta)))
+ return chunks
+
def _flush_reasoning_extractor():
final_reasoning, final_visible = reasoning_extractor.finish()
chunks = []
@@ -32253,7 +32333,7 @@ async def produce_openai_chat_completions(
if event["type"] == "status":
# Empty status marks an iteration boundary; reset the cumulative cursor.
if not event["text"]:
- for chunk in _flush_reasoning_extractor():
+ for chunk in _release_held_mention() + _flush_reasoning_extractor():
yield chunk
prev_text = ""
reasoning_extractor = _new_chat_reasoning_extractor()
@@ -32276,7 +32356,7 @@ async def produce_openai_chat_completions(
# see the card would otherwise report the tool run as its TTFT.
if _ui_events:
api_monitor.mark_first_token(monitor_id, decoded = False)
- for chunk in _flush_reasoning_extractor():
+ for chunk in _release_held_mention() + _flush_reasoning_extractor():
yield chunk
prev_text = ""
reasoning_extractor = _new_chat_reasoning_extractor()
@@ -32329,7 +32409,10 @@ async def produce_openai_chat_completions(
# Sanitize the full cumulative text then diff, so cross-chunk XML tags are handled.
raw_cumulative = event.get("text", "")
- clean_cumulative = _gguf_display_strip.strip(raw_cumulative)
+ _gguf_turn_raw = raw_cumulative
+ clean_cumulative = _gguf_display_strip.strip(
+ _hold_pending_mention(raw_cumulative, _gguf_auto_heal_tool_calls)
+ )
new_text = clean_cumulative[len(prev_text) :]
prev_text = clean_cumulative
if not new_text:
@@ -32344,7 +32427,7 @@ async def produce_openai_chat_completions(
api_monitor.append_reply(monitor_id, visible_delta)
yield _gguf_chat_delta_line(ChoiceDelta(content = visible_delta))
- for chunk in _flush_reasoning_extractor():
+ for chunk in _release_held_mention() + _flush_reasoning_extractor():
yield chunk
final_chunk = ChatCompletionChunk(
@@ -34023,6 +34106,35 @@ async def produce_openai_chat_completions(
)
reasoning_extractor = _new_sf_reasoning_extractor()
approval_flush_pending = False
+ _sf_turn_raw = ""
+
+ def _sf_release_held_mention():
+ nonlocal prev_text, _sf_turn_raw
+ clean = _held_mention_display(
+ _sf_display_strip, _sf_turn_raw, _sf_auto_heal_tool_calls
+ )
+ _sf_turn_raw = ""
+ if clean is None:
+ return []
+ new_text = clean[len(prev_text) :]
+ prev_text = clean
+ out = []
+ if not new_text:
+ return out
+ reasoning_delta, visible_delta = reasoning_extractor.feed(new_text)
+ if reasoning_delta:
+ api_monitor.mark_first_token(monitor_id)
+ out.append(
+ _chat_reasoning_chunk(
+ completion_id, created, model_name, reasoning_delta
+ )
+ )
+ if visible_delta:
+ api_monitor.append_reply(monitor_id, visible_delta)
+ out.append(
+ _chat_content_chunk(completion_id, created, model_name, visible_delta)
+ )
+ return out
def _sf_flush_reasoning():
# Drain the extractor at turn/stream end (mirrors GGUF); only visible text hits the monitor.
@@ -34101,7 +34213,7 @@ async def produce_openai_chat_completions(
if event["type"] == "status":
if not event["text"]:
# Iteration boundary: flush reasoning, then a fresh prefilled extractor for the next turn.
- for _c in _sf_flush_reasoning():
+ for _c in _sf_release_held_mention() + _sf_flush_reasoning():
yield _c
prev_text = ""
reasoning_extractor = _new_sf_reasoning_extractor()
@@ -34130,7 +34242,7 @@ async def produce_openai_chat_completions(
if _ui_events:
api_monitor.mark_first_token(monitor_id, decoded = False)
# Flush reasoning before tool_start so the thinking block closes ahead of the card.
- for _c in _sf_flush_reasoning():
+ for _c in _sf_release_held_mention() + _sf_flush_reasoning():
yield _c
prev_text = ""
reasoning_extractor = _new_sf_reasoning_extractor()
@@ -34151,7 +34263,10 @@ async def produce_openai_chat_completions(
# Diff cumulative cleaned text against last snapshot.
raw_cumulative = event.get("text", "")
- clean_cumulative = _sf_display_strip.strip(raw_cumulative)
+ _sf_turn_raw = raw_cumulative
+ clean_cumulative = _sf_display_strip.strip(
+ _hold_pending_mention(raw_cumulative, _sf_auto_heal_tool_calls)
+ )
new_text = clean_cumulative[len(prev_text) :]
prev_text = clean_cumulative
if not new_text:
@@ -34167,7 +34282,7 @@ async def produce_openai_chat_completions(
api_monitor.append_reply(monitor_id, visible_delta)
yield _chat_content_chunk(completion_id, created, model_name, visible_delta)
- for _c in _sf_flush_reasoning():
+ for _c in _sf_release_held_mention() + _sf_flush_reasoning():
yield _c
# Request-scoped holder so concurrent streams cannot read each other's stats.
_stats = _sf_stats_holder.get("stats")
@@ -44191,6 +44306,7 @@ class _AnthropicToolEventRules:
self._tool_blocks_emitted = 0
self._drop_until_tool_end = False
self._span_guard = _ReasoningSpanGuard(think_provenance)
+ self._turn_raw = ""
# Last drop-branch keepalive, seeded to stream start so a chatty tool busy past the
# stall window still gets one though its events are dropped.
self._last_drop_keepalive = time.monotonic()
@@ -44209,11 +44325,17 @@ class _AnthropicToolEventRules:
self._last_drop_keepalive = _now
return [_OPENAI_PASSTHROUGH_SSE_KEEPALIVE]
return []
+ # Released before the tool block opens (the emitter's cursor still holds the turn) and before a cursor reset.
+ released = (
+ self._release_held_mention()
+ if etype in ("tool_start", "tool_end") or (etype == "status" and not event.get("text"))
+ else []
+ )
if self._drop_until_tool_end:
# Skip through the dropped call's tool_end.
if etype == "tool_end":
self._drop_until_tool_end = False
- return []
+ return released
if etype == "metadata":
_fr = event.get("finish_reason")
if _fr is not None:
@@ -44222,6 +44344,7 @@ class _AnthropicToolEventRules:
# [TOOL_CALLS] prose, which a raw sub corrupts.
if etype == "content":
event = dict(event)
+ self._turn_raw = event["text"]
event["text"] = self._span_guard.strip(
event["text"],
auto_heal_tool_calls = True,
@@ -44231,7 +44354,7 @@ class _AnthropicToolEventRules:
if etype == "tool_start":
if self._disable_parallel_tool_use and self._tool_blocks_emitted >= 1:
self._drop_until_tool_end = True
- return []
+ return released
self._ends_on_tool_use = True
elif etype == "tool_end":
self._tool_blocks_emitted += 1
@@ -44241,13 +44364,31 @@ class _AnthropicToolEventRules:
self._ends_on_tool_use = False
elif etype == "content" and event.get("text"):
self._ends_on_tool_use = False
- return self._emitter.feed(event)
+ return released + self._emitter.feed(event)
+
+ def _release_held_mention(self) -> list:
+ """A backticked tag still held when its turn ends never closed: emit what the full strip keeps."""
+ raw, self._turn_raw = self._turn_raw, ""
+ if not raw or _pending_mention_cut(raw) < 0:
+ return []
+ return self._emitter.feed(
+ {
+ "type": "content",
+ "text": self._span_guard.strip(
+ raw,
+ auto_heal_tool_calls = True,
+ enabled_tool_names = self._display_names,
+ hold = False,
+ ),
+ }
+ )
def finish(self) -> list:
+ released = self._release_held_mention()
stop_reason = openai_finish_to_anthropic_stop(
self._finish_reason, had_tool_calls = self._ends_on_tool_use
)
- return self._emitter.finish(stop_reason = stop_reason, stop_sequence = None)
+ return released + self._emitter.finish(stop_reason = stop_reason, stop_sequence = None)
async def _anthropic_tool_stream(
@@ -44593,31 +44734,56 @@ def _anthropic_tool_response_from_events(
_span_guard = _ReasoningSpanGuard(think_provenance)
# Key presence means genuine reasoning; the value may be None if only the count was recorded.
_block_wrap: dict = {}
+ turn_event = None
+
+ def _add_text(clean: str) -> None:
+ nonlocal prev_text, ends_on_tool_use
+ new = clean[len(prev_text) :]
+ prev_text = clean
+ if new:
+ ends_on_tool_use = False
+ if content_blocks and isinstance(content_blocks[-1], AnthropicResponseTextBlock):
+ content_blocks[-1].text += new
+ else:
+ content_blocks.append(AnthropicResponseTextBlock(text = new))
+ # The wrap belongs to the block that OPENS this turn's span (a client tool_use can interrupt it).
+ if _span_guard.claimed and new.startswith("<think>"):
+ _block_wrap[len(content_blocks) - 1] = _span_guard.claimed_wrap
+
+ def _release_held_mention() -> None:
+ nonlocal turn_event
+ last, turn_event = turn_event, None
+ if last is None or _pending_mention_cut(last["text"]) < 0:
+ return
+ _add_text(
+ _span_guard.strip(
+ last["text"],
+ auto_heal_tool_calls = True,
+ enabled_tool_names = _display_names,
+ wrapped_so_far = last.get(_WRAPPED_SO_FAR),
+ hold = False,
+ )
+ )
for event in events:
etype = event.get("type", "")
if etype == "content":
+ turn_event = event
# Strip leaked tool XML (protected helper keeps think rehearsal and trailing prose).
- clean = _span_guard.strip(
- event["text"],
- auto_heal_tool_calls = True,
- enabled_tool_names = _display_names,
- wrapped_so_far = event.get(_WRAPPED_SO_FAR),
- )
- new = clean[len(prev_text) :]
- prev_text = clean
- if new:
- ends_on_tool_use = False
- if content_blocks and isinstance(content_blocks[-1], AnthropicResponseTextBlock):
- content_blocks[-1].text += new
- else:
- content_blocks.append(AnthropicResponseTextBlock(text = new))
- # The wrap belongs to the block that OPENS this turn's span (a client tool_use can interrupt it).
- if _span_guard.claimed and new.startswith("<think>"):
- _block_wrap[len(content_blocks) - 1] = _span_guard.claimed_wrap
+ _add_text(
+ _span_guard.strip(
+ event["text"],
+ auto_heal_tool_calls = True,
+ enabled_tool_names = _display_names,
+ wrapped_so_far = event.get(_WRAPPED_SO_FAR),
+ )
+ )
elif etype == "status" and not event.get("text"):
+ _release_held_mention()
prev_text = ""
elif etype == "tool_start":
+ # Text ahead of the tool_use block, while prev_text still holds the turn.
+ _release_held_mention()
tool_call_id = event["tool_call_id"]
arguments = event.get("arguments", {})
existing_tool_block = tool_blocks_by_id.get(tool_call_id) if tool_call_id else None
@@ -44642,6 +44808,7 @@ def _anthropic_tool_response_from_events(
content_blocks.append(tool_block)
ends_on_tool_use = True
elif etype == "tool_end":
+ _release_held_mention()
prev_text = ""
_span_guard.tool_end()
# Text-parsed calls restart at call_0 each loop, so a later call may reuse this id; open a new block.
@@ -44662,6 +44829,7 @@ def _anthropic_tool_response_from_events(
_fr = event.get("finish_reason")
if _fr is not None:
captured_finish_reason = _fr
+ _release_held_mention()
# With provenance use the span guard wrap for that turn, not a re-derived one; else leading-tag rule.
if parse_think:
diff --git a/studio/backend/tests/data/refactor_guard/ast_inventory.json b/studio/backend/tests/data/refactor_guard/ast_inventory.json
index cd508b7100d..af9566ab000 100644
--- a/studio/backend/tests/data/refactor_guard/ast_inventory.json
+++ b/studio/backend/tests/data/refactor_guard/ast_inventory.json
@@ -1832,7 +1832,7 @@
"_mask_blocked_bodies": {
"decorators": [],
"kind": "def",
- "signature": "text: str, enabled_tool_names, *, think: bool=False"
+ "signature": "text: str, enabled_tool_names, *, think: bool=False, mentions: bool=False"
},
"_merge_spans": {
"decorators": [],
@@ -2233,7 +2233,9 @@
"_TOOL_OPEN_XML_TAIL_PATS[1]": "'<\\\\|tool_call>.*$' flags=48",
"_TOOL_OPEN_XML_TAIL_PATS[2]": "'<function=[\\\\w-]+>.*$' flags=48",
"_WRAP_OPEN_RE": "'<tool_call>\\\\s*$' flags=32",
- "_WRAP_CLOSE_RE": "'\\\\s*</tool_call>' flags=32"
+ "_WRAP_CLOSE_RE": "'\\\\s*</tool_call>' flags=32",
+ "_TAG_MENTION_RE": "'(?<!`)(``?)[ \\\\t]*(<(?!/?think(?:ing)?>)[^<>`\\\\n]+>[\\\\w.\\\\-]*|\\\\[/?(?:TOOL_CALLS|ARGS|CALL_ID)\\\\][\\\\w.\\\\-]*)[ \\\\t]*\\\\1' flags=32",
+ "_TOOL_TAG_HINT_RE": "'</?(?:tool|function|param|arg_)|<[|\uff5c]|\\\\[/?(?:TOOL_CALLS|ARGS|CALL_ID)\\\\]' flags=32"
},
"regexes": {
"re.compile('(?:<tool_call>|<\\\\|content_invoke_tool_json\\\\|>)\\\\s*\\\\{')": 1,
@@ -2268,7 +2270,9 @@
"re.compile('^[ \\\\t]*(?:>[ \\\\t]*)*(?:(?P<bt>```+)[^`\\\\n]*$.*?(?:^[ \\\\t]*(?:>[ \\\\t]*)*(?P=bt)`*[ \\\\t\\\\r]*$|\\\\Z)|(?P<tl>~~~+)[^\\\\n]*$.*?(?:^[ \\\\t]*(?:>[ \\\\t]*)*(?P=tl)~*[ \\\\t\\\\r]*$|\\\\Z))|``(?:[^`]|`(?!`))*?``|`[^`\\\\n]*`', re.DOTALL | re.MULTILINE)": 1,
"re.match('\\\\s*\\\\[/TOOL_CALLS\\\\]', content[end:])": 1,
"re.compile('<tool_call>\\\\s*$')": 1,
- "re.compile('\\\\s*</tool_call>')": 1
+ "re.compile('\\\\s*</tool_call>')": 1,
+ "re.compile('(?<!`)(``?)[ \\\\t]*(<(?!/?think(?:ing)?>)[^<>`\\\\n]+>[\\\\w.\\\\-]*|\\\\[/?(?:TOOL_CALLS|ARGS|CALL_ID)\\\\][\\\\w.\\\\-]*)[ \\\\t]*\\\\1')": 1,
+ "re.compile('</?(?:tool|function|param|arg_)|<[|\uff5c]|\\\\[/?(?:TOOL_CALLS|ARGS|CALL_ID)\\\\]')": 1
},
"symbols": {
"EXECUTION_CLASS_TOOL_NAMES": {
@@ -2620,6 +2624,25 @@
},
"_OPEN_PARAMETER_SCAN_LIMIT": {
"kind": "assign"
+ },
+ "_TAG_MENTION_RE": {
+ "kind": "assign"
+ },
+ "_TOOL_TAG_HINT_RE": {
+ "kind": "assign"
+ },
+ "_TOOL_TAG_PREFIXES": {
+ "kind": "assign"
+ },
+ "in_tag_mention": {
+ "decorators": [],
+ "kind": "def",
+ "signature": "spans, pos: int"
+ },
+ "tag_mention_spans": {
+ "decorators": [],
+ "kind": "def",
+ "signature": "text: str"
}
}
}
diff --git a/studio/backend/tests/data/refactor_guard/golden_outputs.json b/studio/backend/tests/data/refactor_guard/golden_outputs.json
index 1efa136c070..257baa5cca6 100644
--- a/studio/backend/tests/data/refactor_guard/golden_outputs.json
+++ b/studio/backend/tests/data/refactor_guard/golden_outputs.json
@@ -132,7 +132,8 @@
"parse_tool_calls_from_text": "8eea9ebd03da248ba65bde8dcaa1faa9bedd1ec92f6c244a1be1e83edfd207bd",
"strip_outside_think": "3a97a231706b3ab23b3c7f28e69170eca6f749ed0388dd93bccc25150e97faf6",
"strip_tool_call_markup": "1e30f8bc5fc374b58de007afd10702e5d4e6c4017f4352f653f3590b2077bcdc",
- "strip_tool_patterns": "80677e0f8c3a42f9c58dc6384581d72b7bd548643608011f0ffe5bc4369b2e92"
+ "strip_tool_patterns": "80677e0f8c3a42f9c58dc6384581d72b7bd548643608011f0ffe5bc4369b2e92",
+ "tag_mention_spans": "dfc40db7931fd004fde1eb93afce5cb06b1ef0c0a17ae239299beac64ebab2dd"
},
"corpus": "03bce2b8121055afd12bab57f8811206877d9fbbec3186b3ac5b3107b9e967b3"
}
diff --git a/studio/backend/tests/data/refactor_guard/runtime_inventory.json b/studio/backend/tests/data/refactor_guard/runtime_inventory.json
index 3d6d27e418f..b201c8cfe8b 100644
--- a/studio/backend/tests/data/refactor_guard/runtime_inventory.json
+++ b/studio/backend/tests/data/refactor_guard/runtime_inventory.json
@@ -281,7 +281,7 @@
"_markerless_blocked_execution": "core.tool_healing._markerless_blocked_execution(name, enabled_tool_names) -> 'bool'",
"_markerless_execution_class": "core.tool_healing._markerless_execution_class(name) -> 'bool'",
"_markerless_promotable": "core.tool_healing._markerless_promotable(name, enabled_tool_names) -> 'bool'",
- "_mask_blocked_bodies": "core.inference.tool_call_parser._mask_blocked_bodies(text: 'str', enabled_tool_names, *, think: 'bool' = False) -> 'tuple'",
+ "_mask_blocked_bodies": "core.inference.tool_call_parser._mask_blocked_bodies(text: 'str', enabled_tool_names, *, think: 'bool' = False, mentions: 'bool' = False) -> 'tuple'",
"_merge_spans": "core.inference.tool_call_parser._merge_spans(spans: 'list') -> 'list'",
"_mistral_region_end": "core.inference.tool_call_parser._mistral_region_end(text: 'str', idx: 'int', *, open_envelope_runs_to_eof: 'bool' = False) -> 'int | None'",
"_next_top_level_comma": "core.inference.tool_call_parser._next_top_level_comma(text: 'str', start: 'int', end: 'int') -> \"'int | None'\"",
@@ -379,6 +379,7 @@
"_REHEARSAL_RE",
"_REHEARSAL_STRIP_PATS",
"_REHEARSAL_TAIL_STRIP_RE",
+ "_TAG_MENTION_RE",
"_TC_END_TAG_RE",
"_TC_FUNC_CLOSED_PAT",
"_TC_FUNC_CLOSE_RE",
@@ -397,6 +398,8 @@
"_TOOL_CLOSED_BLOCK_PATS",
"_TOOL_CLOSED_PATS",
"_TOOL_OPEN_XML_TAIL_PATS",
+ "_TOOL_TAG_HINT_RE",
+ "_TOOL_TAG_PREFIXES",
"_WRAP_CLOSE_RE",
"_WRAP_OPEN_RE",
"_balanced_brace_end",
@@ -437,6 +440,7 @@
"annotations",
"apply_tool_strip_patterns",
"bisect",
+ "in_tag_mention",
"itertools",
"json",
"marker_coverage",
@@ -444,7 +448,8 @@
"re",
"strip_outside_think",
"strip_tool_call_markup",
- "strip_tool_patterns"
+ "strip_tool_patterns",
+ "tag_mention_spans"
],
"functions": {
"_balanced_brace_end": "core.tool_healing._balanced_brace_end(content: 'str', brace_start: 'int', *, gemma_quotes: 'bool' = False) -> 'int | None'",
@@ -487,7 +492,9 @@
"parse_tool_calls_from_text": "core.tool_healing.parse_tool_calls_from_text(content: 'str', *, id_offset: 'int' = 0, allow_incomplete: 'bool' = True, enabled_tool_names=None, with_spans: 'bool' = False)",
"strip_outside_think": "core.tool_healing.strip_outside_think(text: 'str', strip_segment) -> 'str'",
"strip_tool_call_markup": "core.tool_healing.strip_tool_call_markup(text: 'str', *, final: 'bool' = False, enabled_tool_names=None) -> 'str'",
- "strip_tool_patterns": "core.tool_healing.strip_tool_patterns(text: 'str', patterns) -> 'str'"
+ "strip_tool_patterns": "core.tool_healing.strip_tool_patterns(text: 'str', patterns) -> 'str'",
+ "in_tag_mention": "core.tool_healing.in_tag_mention(spans, pos: 'int') -> 'bool'",
+ "tag_mention_spans": "core.tool_healing.tag_mention_spans(text: 'str') -> 'list[tuple[int, int]]'"
}
}
}
diff --git a/studio/backend/tests/test_backticked_tag_mention.py b/studio/backend/tests/test_backticked_tag_mention.py
new file mode 100644
index 00000000000..282ad68dde8
--- /dev/null
+++ b/studio/backend/tests/test_backticked_tag_mention.py
@@ -0,0 +1,586 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""A tool tag alone in an inline code span is prose: shown by every display strip, never executed.
+
+A model explaining tool calling writes "Use `<tool_call>` tags." The display strips read the
+backticked opener as an unclosed call at end of turn and cut the answer to "Use `", and a
+backticked `<function=NAME>` was even run as a zero-argument call. Real calls (after a mention,
+with backticks in their arguments, inside a code fence) must still be hidden and executed.
+"""
+
+import json
+import sys
+from pathlib import Path
+
+import pytest
+
+BACKEND_ROOT = Path(__file__).resolve().parents[1]
+if str(BACKEND_ROOT) not in sys.path:
+ sys.path.insert(0, str(BACKEND_ROOT))
+
+from core import tool_healing # noqa: E402
+from core.inference.tool_call_parser import ( # noqa: E402
+ _BLOCKED_BODY_MASK,
+ StreamingMarkupStripper,
+ parse_tool_calls_from_text,
+ strip_tool_markup,
+)
+from routes.inference import ( # noqa: E402
+ _held_mention_display,
+ _hold_pending_mention,
+ _ReasoningSpanGuard,
+ _StreamingDisplayStrip,
+ _strip_tool_xml,
+ _strip_tool_xml_for_display,
+)
+
+NAMES = {"web_search", "python"}
+
+MENTIONS = [
+ "`<tool_call>`",
+ "`</tool_call>`",
+ "``<tool_call>``",
+ "` <tool_call> `",
+ "`<tool_call>web_search`",
+ "`<function=web_search>`",
+ '`<function name="web_search">`',
+ "`</function>`",
+ "`<parameter=query>`",
+ "`</parameter>`",
+ "`<|tool_call>`",
+ "`<tool_call|>`",
+ "`[TOOL_CALLS]`",
+ "`[TOOL_CALLS]web_search`",
+ "`[/TOOL_CALLS]`",
+ "`[ARGS]`",
+ "`<|python_tag|>`",
+ "`<|tool▁calls▁begin|>`",
+ "`<|tool_calls_section_begin|>`",
+ "`<|tool_call_begin|>`",
+]
+
+PROSE = [
+ "Use {m} tags.\nok",
+ "Use {m}",
+ "Open with {m} and close with `</tool_call>`, then stop.",
+ "Wrap the JSON in {m} and `</function>` pairs.\n\nThat is all.",
+]
+
+JSON_CALL = '<tool_call>\n{"name": "web_search", "arguments": {"query": "unsloth"}}\n</tool_call>'
+FUNC_CALL = "<function=web_search>\n<parameter=query>\nunsloth\n</parameter>\n</function>"
+
+
+def _route(text, heal = True):
+ return _strip_tool_xml_for_display(text, auto_heal_tool_calls = heal, enabled_tool_names = NAMES)
+
+
+def _calls(text, heal = True):
+ out = parse_tool_calls_from_text(text, allow_incomplete = heal, enabled_tool_names = NAMES)
+ return [(c["function"]["name"], json.loads(c["function"]["arguments"])) for c in out]
+
+
+def _prose_cases():
+ return [p.format(m = m) for m in MENTIONS for p in PROSE]
+
+
[email protected]("text", _prose_cases())
+def test_backticked_tag_is_shown_by_every_display_strip(text):
+ assert _route(text) == text
+ assert _route(text, heal = False) == text
+ assert _strip_tool_xml(text, NAMES) == text
+ assert strip_tool_markup(text, final = True, enabled_tool_names = NAMES) == text.strip()
+ assert strip_tool_markup(text, final = False, enabled_tool_names = NAMES) == text
+ assert (
+ _ReasoningSpanGuard(None).strip(text, auto_heal_tool_calls = True, enabled_tool_names = NAMES)
+ == text
+ )
+
+
[email protected]("text", _prose_cases())
+def test_backticked_tag_is_never_executed(text):
+ assert _calls(text, heal = True) == []
+ assert _calls(text, heal = False) == []
+
+
+def test_reported_example():
+ text = "Use `<tool_call>` tags.\nok"
+ assert _route(text) == text
+ assert strip_tool_markup(text, final = True, enabled_tool_names = NAMES) == text
+
+
+def test_backticked_function_opener_no_longer_runs_a_zero_arg_call():
+ # Before: Auto-Heal ran web_search({}) for the first, strict mode for the second.
+ assert _calls("Use `<function=web_search>` tags.\nok") == []
+ assert _calls("Use `<function=web_search>` and `</function>` tags.", heal = False) == []
+
+
+def _assert_streams(text, stripper_factory, expect_final):
+ stripper = stripper_factory()
+ prev = ""
+ for i in range(1, len(text) + 1):
+ shown = stripper.strip(text[:i])
+ assert shown.startswith(prev), (text[:i], prev, shown)
+ prev = shown
+ assert prev == expect_final
+
+
+PROSE_STREAM_CASES = [
+ "Use `<tool_call>` tags.\nok",
+ "Use ``<tool_call>`` and `</tool_call>`, or `<function=web_search>`.\nok",
+ "Emit `[TOOL_CALLS]web_search` then `<|python_tag|>`.\nok",
+ "Open `<tool_call>` and close `</tool_call>`.\nok",
+ "Close with `</tool_call>`.\nok",
+ "Run `ls`<b>`x`, then `<|tool_call>` and ` <|tool▁calls▁begin|> `.\nok",
+]
+STREAM_CASES = PROSE_STREAM_CASES + [
+ "Use `<tool_call>` tags.\n" + JSON_CALL + "\nDone.",
+ "Use `<function=web_search>` tags.\n" + FUNC_CALL,
+]
+
+
+class _RouteStream:
+ """What the chat route runs per cumulative content event."""
+
+ def __init__(self, heal = True):
+ self._heal = heal
+ self._strip = _StreamingDisplayStrip(auto_heal_tool_calls = heal, enabled_tool_names = NAMES)
+
+ def strip(self, text):
+ return self._strip.strip(_hold_pending_mention(text, self._heal))
+
+
[email protected]("text", PROSE_STREAM_CASES)
+def test_route_stream_is_append_only_and_ends_on_the_one_shot_strip(text):
+ # The route sends clean[len(prev):] per event, so a display that shrinks and regrows duplicates text.
+ _assert_streams(text, _RouteStream, _route(text))
+ assert _route(text) == text
+
+
+class _GuardStream:
+ """The Messages API emitter: one span guard fed every cumulative content event."""
+
+ def __init__(self):
+ self._guard = _ReasoningSpanGuard(None)
+
+ def strip(self, text):
+ return self._guard.strip(text, auto_heal_tool_calls = True, enabled_tool_names = NAMES)
+
+
[email protected]("text", PROSE_STREAM_CASES)
+def test_messages_api_stream_is_append_only(text):
+ _assert_streams(text, _GuardStream, text)
+
+
[email protected]("text", STREAM_CASES)
+def test_route_stream_ends_on_the_one_shot_strip(text):
+ stripper = _RouteStream()
+ for i in range(1, len(text) + 1):
+ shown = stripper.strip(text[:i])
+ assert shown == _route(text)
+ assert shown.startswith(text.split("\n", 1)[0])
+
+
[email protected]("seg_final", [True, False])
[email protected]("text", STREAM_CASES)
+def test_generator_stream_matches_a_fresh_strip_at_every_prefix(text, seg_final):
+ stripper = StreamingMarkupStripper(NAMES, seg_final = seg_final)
+ for i in range(1, len(text) + 1):
+ prefix = text[:i]
+ assert stripper.strip(prefix) == StreamingMarkupStripper(NAMES, seg_final = seg_final).strip(
+ prefix
+ ), prefix
+
+
[email protected]("call", [JSON_CALL, FUNC_CALL])
+def test_real_call_after_a_mention_is_hidden_and_executed(call):
+ text = "Use `<tool_call>` or `<function=x>` tags.\n" + call
+ assert _calls(text) == [("web_search", {"query": "unsloth"})]
+ assert _route(text) == "Use `<tool_call>` or `<function=x>` tags.\n"
+ assert strip_tool_markup(text, final = True, enabled_tool_names = NAMES) == (
+ "Use `<tool_call>` or `<function=x>` tags."
+ )
+
+
+ "query",
+ ["run `ls` then `pwd`", "explain `<tool_call>` tags", "`</function>` closes it"],
+)
+def test_real_call_with_backticks_in_its_arguments(query):
+ for call in (
+ '<tool_call>\n{"name": "web_search", "arguments": {"query": %s}}\n</tool_call>'
+ % json.dumps(query),
+ f"<function=web_search>\n<parameter=query>\n{query}\n</parameter>\n</function>",
+ ):
+ text = "Searching.\n" + call
+ calls = _calls(text)
+ assert calls == [("web_search", {"query": query})], call
+ assert _BLOCKED_BODY_MASK not in calls[0][1]["query"]
+ assert strip_tool_markup(text, final = True, enabled_tool_names = NAMES) == "Searching."
+ if "</function>" in query and call.startswith("<tool_call>"):
+ # Pre-existing and unchanged: the route regex closes a <tool_call> at a quoted </function>.
+ assert _route(text) == _strip_tool_xml_for_display(
+ text.replace("`", "\x00"), auto_heal_tool_calls = True, enabled_tool_names = NAMES
+ ).replace("\x00", "`")
+ continue
+ assert _route(text) == "Searching.\n"
+
+
[email protected]("call", [JSON_CALL, FUNC_CALL])
+def test_fenced_call_keeps_current_behaviour(call):
+ # A fenced call is still run (current behaviour), so its markup stays hidden.
+ text = "Running it:\n```\n" + call + "\n```\nok"
+ assert _calls(text) == [("web_search", {"query": "unsloth"})]
+ assert "<tool_call>" not in _route(text) and "<function=" not in _route(text)
+
+
+def test_auto_heal_off_route_is_verbatim_and_strict_parse_ignores_mentions():
+ text = "Use `<function=web_search>` and `</function>`.\n`<tool_call>` too."
+ assert _route(text, heal = False) == text
+ # Nothing is stripped with Auto-Heal off, so nothing is held either.
+ stream = _RouteStream(heal = False)
+ assert [stream.strip(text[:i]) for i in range(1, len(text) + 1)] == [
+ text[:i] for i in range(1, len(text) + 1)
+ ]
+ assert _calls(text, heal = False) == []
+
+
+def test_stream_display_strip_itself_still_equals_the_full_strip():
+ # The hold lives in the callers, so the incremental strip keeps its exact contract.
+ for text in STREAM_CASES:
+ strip = _StreamingDisplayStrip(auto_heal_tool_calls = True, enabled_tool_names = NAMES)
+ for i in range(1, len(text) + 1):
+ assert strip.strip(text[:i]) == _route(text[:i])
+
+
+def test_mention_spans_exclude_code_that_holds_more_than_a_tag():
+ assert tool_healing.tag_mention_spans('a `<tool_call>{"x": 1}` b') == []
+ assert tool_healing.tag_mention_spans("a `<think>` b") == []
+ assert tool_healing.tag_mention_spans("no backticks <tool_call>") == []
+ text = "a `<tool_call>` b"
+ (span,) = tool_healing.tag_mention_spans(text)
+ assert text[span[0] : span[1]] == "<tool_call>"
+ assert tool_healing.in_tag_mention([span], span[0])
+ assert not tool_healing.in_tag_mention([span], span[1])
+
+
[email protected]("chunk", [1, 3])
+ "answer",
+ [
+ "Use `<tool_call>` tags.\nok",
+ "Use `<function=web_search>` tags.\nok",
+ "Open `<|tool_call>` and close `</tool_call>`.\nok",
+ ],
+)
+def test_gguf_tool_loop_to_route_stream_end_to_end(monkeypatch, answer, chunk):
+ """Real GGUF tool loop on a fake llama-server stream, then the route's per-event strip and delta send."""
+ sys.path.insert(0, str(BACKEND_ROOT / "tests"))
+ from test_llama_cpp_tool_loop import _done, _finish, _make_backend, _sse, _web_search_tool
+
+ executed = []
+ monkeypatch.setattr(
+ "core.inference.tools.execute_tool",
+ lambda name, arguments, **_k: executed.append((name, arguments)) or "ok",
+ )
+ tokens = [answer[i : i + chunk] for i in range(0, len(answer), chunk)]
+ streams = [
+ [_sse({"content": t}) for t in tokens] + [_finish("stop"), _done()],
+ [_sse({"content": "unexpected second turn"}), _finish("stop"), _done()],
+ ]
+ backend = _make_backend(monkeypatch, streams, [])
+ events = list(
+ backend.generate_chat_completion_with_tools(
+ messages = [{"role": "user", "content": "How do I call tools?"}],
+ tools = [_web_search_tool()],
+ max_tool_iterations = 2,
+ auto_heal_tool_calls = True,
+ )
+ )
+ strip = _RouteStream()
+ prev, client = "", ""
+ for event in events:
+ if event.get("type") == "content":
+ clean = strip.strip(event.get("text", ""))
+ client += clean[len(prev) :]
+ prev = clean
+ assert executed == []
+ assert client == answer
+
+
+# A stream that ends while a backticked tag is still held: the closing backticks never come, so it is
+# not a mention, and every streaming path must end on what the one-shot strip keeps.
+UNCLOSED_ENDINGS = [
+ "Pass the JSON after `[ARGS]",
+ "Close it with `</functio",
+ "Close it with `</function>",
+ "Open with `<tool_call>",
+ "Open with ``<function=web_search>",
+ "Open with ` <tool_call> ",
+ "Emit `[TOOL_CALLS]web_search",
+ "Use `<tool_call>` tags, then `<|tool_call",
+]
+
+
+def _route_stream_with_release(text, step = 1):
+ """The route loop: per-event held strip and delta send, then the end-of-turn release."""
+ stream = _RouteStream()
+ prev, client = "", ""
+ cuts = list(range(1, len(text) + 1, step)) + [len(text)]
+ for i in cuts:
+ clean = stream.strip(text[:i])
+ assert clean.startswith(prev), (text[:i], prev, clean)
+ client += clean[len(prev) :]
+ prev = clean
+ final = _held_mention_display(stream._strip, text, True)
+ if final is not None:
+ assert final.startswith(prev), (prev, final)
+ client += final[len(prev) :]
+ return client
+
+
[email protected]("text", UNCLOSED_ENDINGS + PROSE_STREAM_CASES)
+def test_route_stream_release_ends_on_the_one_shot_strip(text):
+ assert _route_stream_with_release(text) == _route(text)
+ assert _route_stream_with_release(text, step = 3) == _route(text)
+
+
[email protected]("text", STREAM_CASES)
+def test_route_stream_release_leaves_a_real_call_alone(text):
+ # Nothing is held once the turn ends on a real call, so the release adds nothing.
+ strip = _StreamingDisplayStrip(auto_heal_tool_calls = True, enabled_tool_names = NAMES)
+ assert _held_mention_display(strip, text, True) is None
+
+
+def test_release_is_a_no_op_when_nothing_is_held():
+ strip = _StreamingDisplayStrip(auto_heal_tool_calls = True, enabled_tool_names = NAMES)
+ assert _held_mention_display(strip, "Use `<tool_call>` tags.", True) is None
+ assert _held_mention_display(strip, "Open with `<tool_call>", False) is None
+ assert _held_mention_display(strip, "", True) is None
+
+
+def _route_sse_visible(monkeypatch, backend_kind, events):
+ """POST /chat/completions (stream) on a backend that replays ``events``; returns the streamed content."""
+ from types import SimpleNamespace
+
+ from fastapi import FastAPI
+ from fastapi.testclient import TestClient
+
+ from auth.authentication import get_current_subject
+ import routes.inference as inference_route
+
+ sys.path.insert(0, str(BACKEND_ROOT / "tests"))
+ from llama_backend_double import FakeLlamaCppBackend
+
+ selected_tools = [{"type": "function", "function": {"name": "web_search"}}]
+
+ class EventBackend(FakeLlamaCppBackend):
+ supports_tools = True
+ context_length = 8192
+ active_model_name = "sf-model"
+ models = {"sf-model": {"context_length": 8192}}
+
+ def generate_chat_completion_with_tools(self, **kwargs):
+ yield from events
+
+ event_backend = EventBackend()
+ monkeypatch.setattr(inference_route, "_effective_enable_tools", lambda payload: True)
+
+ async def select_tools(payload, **kwargs):
+ return selected_tools
+
+ monkeypatch.setattr(inference_route, "_select_request_tools", select_tools)
+ if backend_kind == "gguf":
+ monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: event_backend)
+ else:
+ monkeypatch.setattr(
+ inference_route,
+ "get_llama_cpp_backend",
+ lambda: SimpleNamespace(is_loaded = False, supports_tools = False),
+ )
+ monkeypatch.setattr(inference_route, "get_inference_backend", lambda: event_backend)
+ monkeypatch.setattr(
+ inference_route,
+ "_detect_safetensors_features",
+ lambda *args, **kwargs: {"supports_tools": True, "supports_reasoning": False},
+ )
+ app = FastAPI()
+ app.include_router(inference_route.router)
+ app.dependency_overrides[get_current_subject] = lambda: "test-user"
+ with TestClient(app) as client:
+ response = client.post(
+ "/chat/completions",
+ json = {
+ "messages": [{"role": "user", "content": "How do I call tools?"}],
+ "stream": True,
+ "enable_tools": True,
+ "auto_heal_tool_calls": True,
+ "permission_mode": "off",
+ "sandbox_level": "low",
+ },
+ )
+ assert response.status_code == 200, response.text
+ chunks = [
+ json.loads(line[6:]) for line in response.text.splitlines() if line.startswith("data: {")
+ ]
+ return "".join(
+ choice.get("delta", {}).get("content") or ""
+ for chunk in chunks
+ for choice in chunk.get("choices", [])
+ )
+
+
+def _turn_events(text):
+ return [{"type": "content", "text": text[:i]} for i in range(1, len(text) + 1)]
+
+
[email protected]("backend_kind", ["gguf", "safetensors"])
[email protected]("text", UNCLOSED_ENDINGS[:4] + ["Use `<tool_call>` tags.\nok"])
+def test_route_sse_stream_releases_a_tag_held_at_the_end(monkeypatch, backend_kind, text):
+ visible = _route_sse_visible(monkeypatch, backend_kind, _turn_events(text))
+ assert visible == _route(text)
+
+
[email protected]("backend_kind", ["gguf", "safetensors"])
[email protected]("boundary", ["status", "tool_start"])
+def test_route_sse_stream_releases_a_held_tag_at_a_turn_boundary(
+ monkeypatch, backend_kind, boundary
+):
+ first, second = "Checking `[ARGS]", "Done with `<tool_call>"
+ if boundary == "status":
+ reset = [{"type": "status", "text": ""}]
+ else:
+ reset = [
+ {
+ "type": "tool_start",
+ "tool_name": "web_search",
+ "tool_call_id": "c1",
+ "arguments": {},
+ },
+ {"type": "tool_end", "tool_name": "web_search", "tool_call_id": "c1", "result": "ok"},
+ ]
+ events = _turn_events(first) + reset + _turn_events(second)
+ visible = _route_sse_visible(monkeypatch, backend_kind, events)
+ assert visible == _route(first) + _route(second)
+
+
+def _messages_stream_text(events):
+ from core.inference.anthropic_compat import AnthropicStreamEmitter
+ from routes.inference import _AnthropicToolEventRules
+
+ emitter = AnthropicStreamEmitter(parse_think = False)
+ lines = list(emitter.start("msg_1", "test-model"))
+ rules = _AnthropicToolEventRules(emitter, None, False, None)
+ for event in events:
+ lines.extend(rules.feed(event))
+ lines.extend(rules.finish())
+ text = ""
+ for line in lines:
+ for raw in line.split("\n"):
+ if raw.startswith("data: "):
+ delta = json.loads(raw[6:]).get("delta", {})
+ if delta.get("type") == "text_delta":
+ text += delta["text"]
+ return text
+
+
+def _messages_reduced_text(events):
+ from routes.inference import _anthropic_tool_response_from_events
+ response = _anthropic_tool_response_from_events(
+ events, "msg_1", "test-model", parse_think = False
+ )
+ return "".join(b["text"] for b in json.loads(response.body)["content"] if b["type"] == "text")
+
+
+def _guard_full(text):
+ # The one-shot strip a non-streaming reply gets (no declared tools, so no name gate).
+ full = _strip_tool_xml_for_display(text, auto_heal_tool_calls = True, enabled_tool_names = None)
+ assert (
+ _ReasoningSpanGuard(None).strip(
+ text, auto_heal_tool_calls = True, enabled_tool_names = None, hold = False
+ )
+ == full
+ )
+ return full
+
+
[email protected]("collect", [_messages_stream_text, _messages_reduced_text])
[email protected]("text", UNCLOSED_ENDINGS + PROSE_STREAM_CASES)
+def test_messages_api_releases_a_tag_held_at_the_end(collect, text):
+ assert collect(_turn_events(text)) == _guard_full(text)
+
+
[email protected]("collect", [_messages_stream_text, _messages_reduced_text])
[email protected]("boundary", ["status", "tool_end"])
+def test_messages_api_releases_a_held_tag_at_a_turn_boundary(collect, boundary):
+ first, second = "Checking `[ARGS]", "Done with `<tool_call>"
+ if boundary == "status":
+ reset = [{"type": "status", "text": ""}]
+ else:
+ reset = [
+ {"type": "tool_start", "tool_name": "lookup", "tool_call_id": "c1", "arguments": {}},
+ {"type": "tool_end", "tool_name": "lookup", "tool_call_id": "c1", "result": "ok"},
+ ]
+ text = collect(_turn_events(first) + reset + _turn_events(second))
+ assert text == _guard_full(first) + _guard_full(second)
+ # The unclosed `<tool_call> is an opener to the full strip; the released `[ARGS] is shown once.
+ assert text == "Checking `[ARGS]Done with `"
+
+
+_TOOL_ROUND = [
+ {"type": "tool_start", "tool_name": "lookup", "tool_call_id": "c1", "arguments": {}},
+ {"type": "tool_end", "tool_name": "lookup", "tool_call_id": "c1", "result": "ok"},
+]
+
+
+def test_messages_api_stream_releases_a_held_tag_before_the_tool_block():
+ from core.inference.anthropic_compat import AnthropicStreamEmitter
+ from routes.inference import _AnthropicToolEventRules
+
+ emitter = AnthropicStreamEmitter(parse_think = False)
+ lines = list(emitter.start("msg_1", "test-model"))
+ rules = _AnthropicToolEventRules(emitter, None, False, None)
+ for event in _turn_events("Checking `[ARGS]") + _TOOL_ROUND:
+ lines.extend(rules.feed(event))
+ lines.extend(rules.finish())
+ open_blocks, order = set(), []
+ for line in lines:
+ for raw in line.split("\n"):
+ if not raw.startswith("data: "):
+ continue
+ ev = json.loads(raw[6:])
+ if ev["type"] == "content_block_start":
+ assert not open_blocks, f"block {ev['index']} opened while {open_blocks} still open"
+ open_blocks.add(ev["index"])
+ order.append(ev["content_block"]["type"])
+ elif ev["type"] == "content_block_stop":
+ open_blocks.discard(ev["index"])
+ assert not open_blocks
+ assert order[0] == "text" and "tool_use" in order
+ assert order.index("tool_use") > 0 and "text" not in order[order.index("tool_use") :]
+
+
+def test_messages_api_reducer_keeps_held_text_ahead_of_the_tool_block():
+ from routes.inference import _anthropic_tool_response_from_events
+
+ response = _anthropic_tool_response_from_events(
+ _turn_events("Checking `[ARGS]") + _TOOL_ROUND, "msg_1", "test-model", parse_think = False
+ )
+ types = [b["type"] for b in json.loads(response.body)["content"]]
+ assert types[0] == "text"
+ assert "text" not in types[1:], types
+
+
+def test_mention_survives_a_real_call_quoting_a_backticked_tag():
+ call = '<tool_call>{"name": "web_search", "arguments": {"query": "explain `<tool_call>` tags"}}</tool_call>'
+ text = "Use `<tool_call>` tags.\n" + call
+ assert _route(text) == "Use `<tool_call>` tags.\n"
+ assert (
+ strip_tool_markup(text, final = True, enabled_tool_names = NAMES) == "Use `<tool_call>` tags."
+ )
+ assert _calls(text) == [("web_search", {"query": "explain `<tool_call>` tags"})]
+ # Once streamed, the sentence is never taken back (a partial real opener's brief leak is pre-existing).
+ sentence = "Use `<tool_call>` tags.\n"
+ stream = _StreamingDisplayStrip(auto_heal_tool_calls = True, enabled_tool_names = NAMES)
+ for i in range(len(sentence), len(text) + 1):
+ assert stream.strip(_hold_pending_mention(text[:i], True)).startswith(sentence), i
diff --git a/studio/backend/tests/test_function_xml_linear_scan.py b/studio/backend/tests/test_function_xml_linear_scan.py
index c7bdc19015b..4b1a894b4b6 100644
--- a/studio/backend/tests/test_function_xml_linear_scan.py
+++ b/studio/backend/tests/test_function_xml_linear_scan.py
@@ -108,7 +108,7 @@ CORPUS = _random_texts(200, seed = 2, max_pieces = 40) + [
'<function name="f"><param name="p">1</param></function>\n<function name="a">',
]
# sha256 of the outputs below for CORPUS, recorded on the per-tag scan implementation before the index.
-RECORDED_DIGEST = "b0b70a5748fe61c622cf53d1247a53bf78f3150c017940bee3f83b0298ecec2c"
+RECORDED_DIGEST = "b1be1ceccae573b9e224fdc4464d76816ecaa9f8023062c26384ca4455339a87"
def _outputs(text):
diff --git a/studio/backend/tests/tools/refactor_guard.py b/studio/backend/tests/tools/refactor_guard.py
index e09990a0504..d886e3122bd 100644
--- a/studio/backend/tests/tools/refactor_guard.py
+++ b/studio/backend/tests/tools/refactor_guard.py
@@ -273,7 +273,11 @@ _VARIANTS_KEY = "@variants"
_BOTH_WAYS = ("final", "seg_final", "with_spans", "allow_incomplete", "gemma_quotes")
-_TEXT_ADAPTERS = {"_gemma_arguments_to_json": lambda text: _gemma_argument_body(text)}
+_TEXT_ADAPTERS = {
+ "_gemma_arguments_to_json": lambda text: _gemma_argument_body(text),
+ # The corpus holds no backticked tags; backtick each one so the spans vary with the text.
+ "tag_mention_spans": lambda text: re.sub(r"<[^<>`\n]+>|\[/?TOOL_CALLS\]", r"`\g<0>`", text),
+}
def _sweep_offsets(text: str):