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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: skip the passthrough healer reparse while no held call can close (#15618)
Daniel Han63b85ca3ee07
diff --git a/studio/backend/core/inference/passthrough_healing.py b/studio/backend/core/inference/passthrough_healing.pyindex 8e3b3c87c88..6c2b99a73ad 100644--- a/studio/backend/core/inference/passthrough_healing.py+++ b/studio/backend/core/inference/passthrough_healing.py@@ -7,6 +7,7 @@ UNSLOTH_DISABLE_TOOL_CALL_HEALING=1 opt out"""import jsonimport os+import refrom collections.abc import Mappingfrom typing import Any, Optional@@ -14,7 +15,11 @@ from core.inference.tool_loop_controller import (coerce_arguments_by_schema,coerce_tool_arguments,)-from core.tool_healing import parse_tool_calls_from_text+from core.tool_healing import (+ _MISTRAL_ARRAY_RE,+ _MISTRAL_BRACKET_RE,+ parse_tool_calls_from_text,+)# Only formats this parser can promote, narrower than TOOL_XML_SIGNALS: a loop-only marker would buffer a streamed call as prose unpromoted._HEAL_SIGNALS = (@@ -258,6 +263,145 @@ def _closed_signal_span(buffer: str) -> Optional[tuple[int, int]]:return min(spans, key = lambda span: span[0]) if spans else None+# A strict parse needs a JSON/Gemma close tag or </function>; else only a balanced Mistral/rehearsal bracket call parses.+_STRICT_CLOSE_TOKENS = ("</tool_call>", "<|end_message|>", "<tool_call|>", "</function>")+_CALLS_TAG = "[TOOL_CALLS]"+_ARGS_TAG = "[ARGS]"+_WS_RE = re.compile(r"\s*")+_SPAN_SKIP_RE = re.compile(r'[^"{}\\]*')+_STRING_SKIP_RE = re.compile(r'[^"\\]*')+_BRACKET_SKIP_RE = re.compile(r'[^"\[\]{}]*')+++def _resume_json_span(text: str, state: list) -> Optional[int]:+ """``_balanced_json_span`` resumed from ``state`` (i, depth, in_string, escape) over appended text."""+ i, depth, in_string, escape = state+ n = len(text)+ while i < n:+ if escape:+ escape = False+ i += 1+ continue+ i = (_STRING_SKIP_RE if in_string else _SPAN_SKIP_RE).match(text, i).end()+ if i >= n:+ break+ ch = text[i]+ if ch == "\\":+ escape = True+ elif in_string:+ in_string = False+ elif ch == '"':+ in_string = True+ elif ch == "{":+ depth += 1+ else:+ depth -= 1+ if depth == 0:+ return i+ i += 1+ state[:] = (i, depth, in_string, escape)+ return None+++def _resume_bracket_end(text: str, state: list) -> Optional[int]:+ """``_balanced_bracket_end`` resumed from ``state`` (i, depth, in_string); a string escape at EOF waits."""+ i, depth, in_string, _escape = state+ n = len(text)+ while i < n:+ if in_string:+ i = _STRING_SKIP_RE.match(text, i).end()+ if i >= n:+ break+ if text[i] == "\\":+ if i + 1 >= n:+ break+ i += 2+ continue+ in_string = False+ i += 1+ continue+ i = _BRACKET_SKIP_RE.match(text, i).end()+ if i >= n:+ break+ ch = text[i]+ if ch == '"':+ in_string = True+ elif ch in "[{":+ depth += 1+ else:+ depth -= 1+ if depth == 0:+ return i+ i += 1+ state[:] = (i, depth, in_string, False)+ return None+++class _StrictParseGate:+ """Per-healer scan of a held, append-only buffer: False only when a strict parse of it is provably empty.++ Each chunk scans only the appended text; any buffer that does not extend the last one starts over."""++ __slots__ = ("_buf", "_closer", "_calls", "_args", "_scans")++ def __init__(self) -> None:+ self._reset()++ def _reset(self) -> None:+ self._buf = ""+ self._closer = False+ self._calls: list = []+ self._args: list = []+ # (open index, is array) -> resumable scan state, or the close index once balanced.+ self._scans: dict = {}++ def may_parse(self, buffer: str) -> bool:+ prev_len = len(self._buf)+ if not buffer.startswith(self._buf):+ self._reset()+ prev_len = 0+ self._buf = buffer+ if self._closer:+ return True+ for token in _STRICT_CLOSE_TOKENS:+ # Back off so a token split across chunks is still found.+ if buffer.find(token, max(0, prev_len - len(token) + 1)) >= 0:+ self._closer = True+ return True+ for tag, found in ((_CALLS_TAG, self._calls), (_ARGS_TAG, self._args)):+ pos = buffer.find(tag, max(0, prev_len - len(tag) + 1))+ while pos >= 0:+ found.append(pos)+ pos = buffer.find(tag, pos + len(tag))+ if not self._calls and not self._args:+ return False+ # Every opener _iter_bracket_spans can try; regexes rerun since a tail match can still change.+ opens = []+ for pos in self._calls:+ m = _MISTRAL_BRACKET_RE.match(buffer, pos)+ if m is not None:+ opens.append((m.end(), False))+ m = _MISTRAL_ARRAY_RE.match(buffer, pos)+ if m is not None:+ opens.append((m.end(), True))+ for pos in self._args:+ brace = _WS_RE.match(buffer, pos + len(_ARGS_TAG)).end()+ if buffer.startswith("{", brace):+ opens.append((brace, False))+ scans = self._scans+ for key in opens:+ state = scans.get(key)+ if state is None:+ state = scans[key] = [key[0], 0, False, False]+ elif not isinstance(state, list):+ return True+ end = (_resume_bracket_end if key[1] else _resume_json_span)(buffer, state)+ if end is not None:+ scans[key] = end+ return True+ return False++def _partial_signal_suffix(buffer: str) -> int:"""Length of the longest buffer suffix that is a proper prefix of a signal."""for length in range(min(len(buffer), _MAX_SIGNAL_LEN - 1), 0, -1):@@ -292,6 +436,7 @@ class StreamToolCallHealer:self._id_offset = 0# Promotion is destructive; keep spans so a discarded call can return the model's words.self._promoted_spans: dict[str, str] = {}+ self._strict_gate = _StrictParseGate()# Structured delta.tool_calls upstream: grammar mode already works, so healing goes dormant.self.dormant = False@@ -332,14 +477,21 @@ class StreamToolCallHealer:events.append(("text", emit))self._buffer = self._buffer[len(self._buffer) - keep :]return events- parsed, spans = parse_tool_calls_from_text(- self._buffer,- id_offset = self._id_offset,- allow_incomplete = False,- with_spans = True,+ # A whole-buffer reparse per chunk is quadratic in the held size.+ may_parse = self._strict_gate.may_parse(self._buffer)+ parsed, spans = (+ parse_tool_calls_from_text(+ self._buffer,+ id_offset = self._id_offset,+ allow_incomplete = False,+ with_spans = True,+ )+ if may_parse+ else ([], []))if not parsed:- closed_span = _closed_signal_span(self._buffer)+ # Its close tags are gate tokens, so a closed gate means no closed span.+ closed_span = _closed_signal_span(self._buffer) if may_parse else Noneif closed_span:_start, end = closed_spanevents.append(("text", self._buffer[:end]))diff --git a/studio/backend/tests/test_passthrough_healing_strict_gate.py b/studio/backend/tests/test_passthrough_healing_strict_gate.pynew file mode 100644index 00000000000..e7481f1a647--- /dev/null+++ b/studio/backend/tests/test_passthrough_healing_strict_gate.py@@ -0,0 +1,142 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The streaming healer's reparse gate is invisible: every chunking emits what an always-reparsing healer emits."""++from __future__ import annotations++import json+import random+import sys+from pathlib import Path++import pytest++_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)+if _BACKEND_DIR not in sys.path:+ sys.path.insert(0, _BACKEND_DIR)++import core.inference.passthrough_healing as ph # noqa: E402+from core.tool_healing import _balanced_bracket_end, _balanced_json_span # noqa: E402+++class _AlwaysParse:+ def may_parse(self, buffer):+ return True+++class _ReparsingHealer(ph.StreamToolCallHealer):+ """The pre-gate behaviour: a full strict reparse on every held chunk."""++ def __init__(self, *args, **kwargs):+ super().__init__(*args, **kwargs)+ self._strict_gate = _AlwaysParse()+++NAMES = ("web_search", "terminal", "python")+TOOLS = [+ {+ "type": "function",+ "function": {+ "name": name,+ "parameters": {+ "type": "object",+ "properties": {"query": {"type": "string"}},+ "required": ["query"],+ },+ },+ }+ for name in NAMES+]+GQ = '<|"|>'+DOCS = [+ 'Hi <tool_call>{"name": "web_search", "arguments": {"query": "a \\"}\\" {b}"}}</tool_call> after',+ '<tool_call>\n{"name": "terminal", "parameters": {"query": "ls", "n": [1, {"a": "}"}]}}\n</tool_call>',+ '<tool_call>{"name": "undeclared", "arguments": {}}</tool_call><tool_call>{"name": "python", "arguments": {"query": "x"',+ "<|tool_call>call:web_search{query:" + GQ + "x {y} 中文" + GQ + ",n:3}<tool_call|> tail",+ "<|tool_call>call:web_search{query:" + GQ + "never closed {",+ "<function=terminal>\n<parameter=query>\nls {a} </function> x\n</parameter>\n</function> ok",+ "<tool_call>\n<function=python>\n<parameter=query>print({})</parameter>\n</function>\n</tool_call>",+ 'Sure.[TOOL_CALLS]web_search[ARGS]{"query": "x = {1}\\\\\\" ]"} more',+ '[TOOL_CALLS] terminal {"query": "a", "o": {"k": [1, "]"]}}',+ '[TOOL_CALLS]python[CALL_ID]abc123[ARGS]{"query": "q"}[/TOOL_CALLS]',+ '[TOOL_CALLS][{"name": "web_search", "arguments": {"query": "]}"}}{"name": "python", "arguments": {}}] x',+ '[TOOL_CALLS]web_search[ARGS]{"query": "truncated \\',+ 'Prose python[ARGS] \n{"query": "1"} and ```\npython[ARGS]{"query": "2"}\n```',+ 'web_search<|content_invoke_tool_json|>{"name": "web_search", "args": {"query": "z"}}<|end_message|>',+ '<think>plan <tool_call>{"name": "python", "arguments": {}}</tool_call></think><tool_call>{"name": "python", "arguments": {"query": "y"}}</tool_call>',+ "Prose with {braces} [and] brackets, a < b, 🙂 and no call at all.",+]+++def _events(healer_cls, chunks):+ healer = healer_cls(set(NAMES), TOOLS)+ events = [healer.feed(chunk) for chunk in chunks]+ events.append(healer.finalize())+ return json.dumps(events, ensure_ascii = False)+++def _chunkings(doc, rng):+ yield [doc[i : i + 1] for i in range(len(doc))]+ for i in range(1, len(doc)):+ yield [doc[:i], doc[i:]]+ for high in (3, 9, 40):+ chunks, i = [], 0+ while i < len(doc):+ step = rng.randint(1, high)+ chunks.append(doc[i : i + step])+ i += step+ yield chunks++[email protected]("doc", DOCS)+def test_every_chunking_matches_always_reparsing_healer(doc):+ rng = random.Random(len(doc))+ for chunks in _chunkings(doc, rng):+ assert _events(ph.StreamToolCallHealer, chunks) == _events(_ReparsingHealer, chunks), chunks+++def test_resumed_scans_match_balanced_scans_at_every_prefix():+ rng = random.Random(0)+ alphabet = '{}[]"\\a 中'+ for _ in range(400):+ body = "".join(rng.choice(alphabet) for _ in range(rng.randint(1, 30)))+ for opener, scan, resume in (+ ("{", _balanced_json_span, ph._resume_json_span),+ ("[", _balanced_bracket_end, ph._resume_bracket_end),+ ):+ text = opener + body+ state, end = [0, 0, False, False], None+ for length in range(1, len(text) + 1):+ prefix = text[:length]+ if end is None:+ end = resume(prefix, state)+ assert end == scan(prefix, 0), (text, length)+++ "opening, closing",+ [+ ('<tool_call>{"name": "python", "arguments": {"query": "', '"}}</tool_call>'),+ ("<|tool_call>call:python{query:" + GQ, GQ + "}<tool_call|>"),+ ("<function=python>\n<parameter=query>\n", "\n</parameter>\n</function>"),+ ('[TOOL_CALLS]python[ARGS]{"query": "', '"}'),+ ('[TOOL_CALLS][{"name": "python", "arguments": {"query": "', '"}}]'),+ ],+)+def test_long_held_call_is_not_reparsed_per_chunk(monkeypatch, opening, closing):+ calls = []+ real_parse = ph.parse_tool_calls_from_text+ monkeypatch.setattr(+ ph,+ "parse_tool_calls_from_text",+ lambda *a, **k: calls.append(1) or real_parse(*a, **k),+ )+ text = opening + "x = {1}; y = [2]\\n" * 200 + closing+ healer = ph.StreamToolCallHealer({"python"}, TOOLS)+ events = []+ for i in range(0, len(text), 4):+ events += healer.feed(text[i : i + 4])+ events += healer.finalize()+ assert [kind for kind, _ in events] == ["tool_call"]+ assert len(calls) <= 2