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: run a second tool call glued to the first in DeepSeek envelopes or one Hermes tag (#16026)
Daniel Han1100377235cc
diff --git a/studio/backend/core/inference/tool_call_parser.py b/studio/backend/core/inference/tool_call_parser.pyindex 57886803bfb..d0e8d75b833 100644--- a/studio/backend/core/inference/tool_call_parser.py+++ b/studio/backend/core/inference/tool_call_parser.py@@ -3873,16 +3873,61 @@ def _parse_deepseek_tool_calls(allow_incomplete: bool = True,) -> list[dict]:"""R1 wraps arguments in a json fence, V3.x does not; the five opener variants follow llama.cpp."""- out: list[dict] = []begin = _DEEPSEEK_BEGIN_RE.search(content)if not begin:- return out+ return []+ out, end_pos = _parse_deepseek_envelope(+ content, begin, id_offset = id_offset, allow_incomplete = allow_incomplete+ )+ # Auto-Heal only: a model that closes its envelope and opens another runs both, but only once the+ # first envelope parses strictly, and only closed, strictly parsed envelopes follow it.+ if (+ out+ and allow_incomplete+ and end_pos >= 0+ and _DEEPSEEK_BEGIN_RE.search(content, end_pos + len(_DEEPSEEK_END)) is not None+ and _parse_deepseek_envelope(content, begin, id_offset = 0, allow_incomplete = False)[0]+ ):+ out.extend(+ _parse_glued_deepseek_envelopes(+ content, end_pos + len(_DEEPSEEK_END), id_offset = id_offset + len(out)+ )+ )+ return out+++# Code, think tags or other markup between two envelopes: the later one is quoted, not a call.+_DEEPSEEK_GLUE_GAP_STOP_RE = re.compile(r"[`<>\[\]{}]")+++def _parse_glued_deepseek_envelopes(content: str, pos: int, *, id_offset: int) -> list[dict]:+ """Closed envelopes after the first, separated by whitespace or plain prose only. Linear: each+ envelope is scanned once and the walk stops at the first gap or envelope that does not qualify."""+ out: list[dict] = []+ while True:+ nxt = _DEEPSEEK_BEGIN_RE.search(content, pos)+ if nxt is None or _DEEPSEEK_GLUE_GAP_STOP_RE.search(content, pos, nxt.start()):+ return out+ calls, end_pos = _parse_deepseek_envelope(+ content, nxt, id_offset = id_offset + len(out), allow_incomplete = False+ )+ if not calls:+ return out+ out.extend(calls)+ pos = end_pos + len(_DEEPSEEK_END)+++def _parse_deepseek_envelope(+ content: str, begin: "re.Match", *, id_offset: int, allow_incomplete: bool+) -> "tuple[list[dict], int]":+ """Calls in the envelope opened by ``begin`` and the index of its close (-1 when unclosed)."""+ out: list[dict] = []scan_start = begin.end()# Search outside JSON strings: an argument may contain the literal end token.end_pos = _find_outside_json_strings(content, _DEEPSEEK_END, scan_start)# Strict mode: an unclosed envelope is truncated; reject, don't heal to EOF.if not allow_incomplete and end_pos < 0:- return out+ return out, end_posscan_end = end_pos if end_pos >= 0 else len(content)body = content[scan_start:scan_end]relaxed = allow_incomplete@@ -3932,7 +3977,7 @@ def _parse_deepseek_tool_calls()pos = close_m.end() if close_m else brace_end + 1if out:- return out+ return out, end_pospos = 0while pos < len(body):@@ -3977,7 +4022,7 @@ def _parse_deepseek_tool_calls()# Seeking the end marker could land on a later call's and skip the one between.pos = brace_end + 1- return out+ return out, end_posdef _parse_glm_tool_calls(diff --git a/studio/backend/core/tool_healing.py b/studio/backend/core/tool_healing.pyindex 8eedf4032ac..3103b6e94b5 100644--- a/studio/backend/core/tool_healing.py+++ b/studio/backend/core/tool_healing.py@@ -781,6 +781,52 @@ def marker_coverage(content: str) -> list[tuple[int, int]]:return _marker_coverage(content, _build_markers(content))+def _is_strict_json(text: str) -> bool:+ try:+ json.loads(text)+ except (json.JSONDecodeError, ValueError):+ return False+ return True+++def _glued_call_objects(content: str, pos: int, end: int):+ """Call objects glued after a parsed one inside the same ``<tool_call>``, up to its paired close at+ ``end``: ``[(start, name, arguments_json)]``, or None unless every object between is a strict+ ``{"name": .., "arguments"|"parameters": {..}}`` and only whitespace separates them and the close.+ Braces are matched outside JSON strings, so a ``}{`` or closing tag inside an argument never splits+ a call; the window is this marker's own coverage, so the scan stays linear."""+ close_m = _TC_END_TAG_RE.search(content, pos, end)+ if close_m is None or close_m.end() != end:+ return None+ window = content[pos : close_m.start()]+ out = []+ i = 0+ n = len(window)+ while True:+ while i < n and window[i] in " \t\r\n":+ i += 1+ if i >= n:+ break+ if window[i] != "{":+ return None+ obj_end = _balanced_brace_end(window, i)+ if obj_end is None:+ return None+ try:+ obj = json.loads(window[i : obj_end + 1])+ except (json.JSONDecodeError, ValueError):+ return None+ if not isinstance(obj, dict):+ return None+ g_name = obj.get("name")+ g_args = obj.get("arguments") if "arguments" in obj else obj.get("parameters")+ if not isinstance(g_name, str) or not g_name or not isinstance(g_args, dict):+ return None+ out.append((pos + i, g_name, json.dumps(g_args)))+ i = obj_end + 1+ return out or None++def _relaxed_allowed_at(content: str, pos: int) -> bool:from core.inference.relaxed_json import relaxed_allowed_at # noqa: PLC0415 -- cycle at import timereturn relaxed_allowed_at(content, pos)@@ -907,6 +953,22 @@ def parse_tool_calls_from_text(close_m = close_re.match(content, span_end + ws)if close_m:span_end = close_m.end()+ glued = None+ if (+ allow_incomplete+ and kind == "json"+ and close_m is None+ and name+ and _is_strict_json(content[m.end() - 1 : brace_end + 1])+ ):+ glued = _glued_call_objects(content, span_end, coverage[idx][1])+ if glued:+ # Heal: several call objects glued inside one closed tag; the spans tile up to the close.+ parsed_items.append((start, glued[0][0], name, arguments))+ for g_idx, (g_start, g_name, g_args) in enumerate(glued):+ g_end = glued[g_idx + 1][0] if g_idx + 1 < len(glued) else coverage[idx][1]+ parsed_items.append((g_start, g_end, g_name, g_args))+ continueparsed_items.append((start, span_end, name, arguments))func_starts = [diff --git a/studio/backend/tests/data/refactor_guard/ast_inventory.json b/studio/backend/tests/data/refactor_guard/ast_inventory.jsonindex be189c6e8b2..01c7c710ee5 100644--- a/studio/backend/tests/data/refactor_guard/ast_inventory.json+++ b/studio/backend/tests/data/refactor_guard/ast_inventory.json@@ -1108,6 +1108,7 @@"_BARE_JSON_NAME_RE": "'\"name\"\\\\s*:\\\\s*\"([^\"]+)\"' flags=32","_BLOCKED_BODY_MASK_RUN_RE": "'\\ue000+' flags=32","_DEEPSEEK_BEGIN_RE": "'<|(?:tool▁calls▁begin|tool_calls_begin|tool calls begin|tool\\\\\\\\_calls\\\\\\\\_begin|tool▁calls)|>' flags=32",+ "_DEEPSEEK_GLUE_GAP_STOP_RE": "'[`<>\\\\[\\\\]{}]' flags=32","_DEEPSEEK_R1_CLOSE_RE": "'```[\\\\s\\\\r\\\\n]*<|tool▁call▁end|>' flags=32","_EMBEDDED_MARKER_RE": "'<|(?:tool▁calls▁begin|tool_calls_begin|tool calls begin|tool\\\\\\\\_calls\\\\\\\\_begin|tool▁calls)|>|<\\\\|tool_calls_section_begin\\\\|>|<\\\\|tool_call_begin\\\\|>' flags=32","_FENCE_ONLY_RE": "'[\\\\w.\\\\-]*[ \\\\t]*\\\\n?[ \\\\t]*`{3,}[a-zA-Z0-9_+-]*' flags=32",@@ -1204,6 +1205,7 @@"re.compile('<tool_call>|<function(?:=|\\\\s+name=\")|<\\\\|tool_call>|<\\\\|python_tag\\\\|>(?=\\\\s*(?:\\\\{|[A-Za-z_][\\\\w.]*\\\\())')": 1,"re.compile('<|tool▁call▁begin|>(?=\\\\s*function|\\\\s*$).*$', re.DOTALL)": 1,"re.compile('[\\\\w.\\\\-]*[ \\\\t]*\\\\n?[ \\\\t]*`{3,}[a-zA-Z0-9_+-]*')": 1,+ "re.compile('[`<>\\\\[\\\\]{}]')": 1,"re.compile('\\\\A\\\\s*\\\\{\\\\s*\"name\"\\\\s*:\\\\s*\"([\\\\w.\\\\-]+)\"\\\\s*,\\\\s*\"(?:arguments|parameters)\"\\\\s*:\\\\s*\\\\{\\\\s*\"([A-Za-z_][\\\\w\\\\-]*)\"\\\\s*:\\\\s*\"(.*)\"\\\\s*\\\\}\\\\s*\\\\}\\\\s*\\\\Z', re.DOTALL)": 1,"re.compile('\\\\[TOOL_CALLS\\\\](?=\\\\s*(?:[\\\\[{]|[A-Za-z_][\\\\w.\\\\-]*(?:[\\\\[{]|\\\\s*$))|\\\\s*$).*$', re.DOTALL)": 1,"re.compile('\\\\[TOOL_CALLS\\\\]\\\\s*[\\\\w\\\\.\\\\-]+\\\\s*(?:\\\\[ARGS\\\\])?\\\\s*\\\\{.*?\\\\}', re.DOTALL)": 1,@@ -1365,6 +1367,9 @@"_DEEPSEEK_END": {"kind": "assign"},+ "_DEEPSEEK_GLUE_GAP_STOP_RE": {+ "kind": "assign"+ },"_DEEPSEEK_OPEN_ALT": {"kind": "assign"},@@ -1846,6 +1851,11 @@"kind": "def","signature": "content: str, *, id_offset: int"},+ "_parse_deepseek_envelope": {+ "decorators": [],+ "kind": "def",+ "signature": "content: str, begin: 're.Match', *, id_offset: int, allow_incomplete: bool"+ },"_parse_deepseek_tool_calls": {"decorators": [],"kind": "def",@@ -1866,6 +1876,11 @@"kind": "def","signature": "content: str, *, id_offset: int, allow_incomplete: bool=True"},+ "_parse_glued_deepseek_envelopes": {+ "decorators": [],+ "kind": "def",+ "signature": "content: str, pos: int, *, id_offset: int"+ },"_parse_kimi_section_body": {"decorators": [],"kind": "def",@@ -2384,6 +2399,11 @@"kind": "def","signature": "text: str"},+ "_glued_call_objects": {+ "decorators": [],+ "kind": "def",+ "signature": "content: str, pos: int, end: int"+ },"_in_code": {"decorators": [],"kind": "def",@@ -2394,6 +2414,11 @@"kind": "def","signature": "content: str, pos: int, *, param_start_re=None, param_closers=(_PARAM_CLOSE_TAG,), func_closers=(_FUNC_CLOSE_TAG,)"},+ "_is_strict_json": {+ "decorators": [],+ "kind": "def",+ "signature": "text: str"+ },"_iter_bracket_spans": {"decorators": [],"kind": "def",diff --git a/studio/backend/tests/data/refactor_guard/golden_outputs.json b/studio/backend/tests/data/refactor_guard/golden_outputs.jsonindex 659f6b920df..1efa136c070 100644--- a/studio/backend/tests/data/refactor_guard/golden_outputs.json+++ b/studio/backend/tests/data/refactor_guard/golden_outputs.json@@ -41,10 +41,12 @@"_open_inline_code_run": "2f27bf94223e8b14b8faf656fbe7b59f75a087118c516ff26366865179666517","_parse_bare_rehearsals": "c809e331067827ef1f773b2613bbcb9c9c1138f55e12bd37cbfe35919cceba54","_parse_closed_call_unescaped_quotes": "f5b2cba82abb209bd409fbe327b458c0177a2d09c18b0fab2cd75b6ea64e12bb",- "_parse_deepseek_tool_calls": "b16631fa1f86628069da6662060cd78f6c2b1edc87e15011bcec5103a450cabd",+ "_parse_deepseek_envelope": "653199a9e3fcbd75f8bfddfc908daed5eb131bcee6a71604abd5d92e43ccacf5",+ "_parse_deepseek_tool_calls": "0311aec4be4f82d360a9c5545e12ddfda8d8faec9ab1cd616a9be9b0d3cbe30d","_parse_function_xml": "8e47d9a73bea635e90c3a8c56b8be91b3e42d1bbf75c36795fad2bdefeb71696","_parse_gemma_tool_calls": "b2042d58638346a09c9f52674bb0b525a60bd2a442c8d7afb793faa790656654","_parse_glm_tool_calls": "a59501129afac2b10bf29a43097954b5f9cbcebbcf6202982e43f37c7510a190",+ "_parse_glued_deepseek_envelopes": "c7cde49e1f179148c50e6122852a0ec6603c18ffd317ac7ef2ea81aa618102ae","_parse_kimi_section_body": "5dc402646fc4b4a4fa5c3221f341502911f8904b4248d3108e3c3d028ed85e0f","_parse_kimi_tool_calls": "5dc402646fc4b4a4fa5c3221f341502911f8904b4248d3108e3c3d028ed85e0f","_parse_llama3_bare_json": "09fc31f1a2b49be47fccc672ed6463d09db27903596373188a1752952f9be2be",@@ -84,7 +86,7 @@"is_reprompt_restatement": "5a3fe224ba657bf9ea1706ef997b81e5c97535b0e03d6699962f386ecbaf15a0","is_short_intent_without_action": "1ed247c04b2b2bae560ef52224328500d36644b68274015b8efd2c2b9072ac4b","leading_bare_gemma_call_is_promotable": "f97184954908b5ae23160649f0aadf26a8f3f1b65534d9cc350d91504f478a94",- "parse_tool_calls_from_text": "93d746eaac6c90e7910e5c4b310e5218add5d0340919d4ab843e55563f9c9334",+ "parse_tool_calls_from_text": "0dafd418ab35c8d7e32de2114c01bc05f965d84369e001842cb8d77d15fcf1dd","promotable_gemma_call_pos": "9439dc754e212c8a7a203c6740a782e4885c2c61ba413fc4ff0948ea7200e966","repeated_result_message": "d6e89e731201e216a32e40ccaadc19eaf0f796cee28f4af73675b731666f7e27","reprompt_to_act_message": "27b5a6e3694684acde1db0e63b2415462f24ab5a5825e37a30f00912ebcac01f",@@ -105,7 +107,9 @@"_func_close_index": "6acc50e8842fd97bd297b013f967ba710ccf25a0c1ba9fcd64eafaa7b71d410f","_gemma_arguments_to_json": "f8b51e3654421e8d545146d573ca46f4bce4a04749ce353547ef5d597ead481b","_gemma_span_ranges": "dec49cf5f9e60d5c68690be91e9897e66633abfaa821ee38558b4d83d40a4cd9",+ "_glued_call_objects": "9ce70747ddc172e0c332061b57ec831c7d7029d58299ce4aa51153d3646e742a","_inside_open_parameter": "b94f91fd87588ec8c9dd80f2962c17644d1360ee57c456ea4ab6a90ca12420e7",+ "_is_strict_json": "1003e41e9b5757df0cb1c3fb4e89530661b943862a18e4cbd68b05e4d343b9c4","_iter_bracket_spans": "2def0d358f3aa7790bcc69158040f1e685dde85b870d1b1c8feaa3f179df2188","_json_object_body": "e814b8ed198a73fe7f35306b15d5e796e453449a3d47e369c22732ba32adf18d","_loads_wrapped_call": "bf2c040c5c2e68222d018ecc7fd29a7062bcd12fabaaba7216e7887797d5497e",diff --git a/studio/backend/tests/data/refactor_guard/runtime_inventory.json b/studio/backend/tests/data/refactor_guard/runtime_inventory.jsonindex e96715bd741..e3df7a48b81 100644--- a/studio/backend/tests/data/refactor_guard/runtime_inventory.json+++ b/studio/backend/tests/data/refactor_guard/runtime_inventory.json@@ -30,6 +30,7 @@"_DEEPSEEK_CALL_BEGIN","_DEEPSEEK_CALL_END","_DEEPSEEK_END",+ "_DEEPSEEK_GLUE_GAP_STOP_RE","_DEEPSEEK_OPEN_ALT","_DEEPSEEK_OPEN_RE_SRC","_DEEPSEEK_R1_CLOSE_RE",@@ -161,10 +162,12 @@"_open_inline_code_run","_parse_bare_rehearsals","_parse_closed_call_unescaped_quotes",+ "_parse_deepseek_envelope","_parse_deepseek_tool_calls","_parse_function_xml","_parse_gemma_tool_calls","_parse_glm_tool_calls",+ "_parse_glued_deepseek_envelopes","_parse_kimi_section_body","_parse_kimi_tool_calls","_parse_llama3_bare_json",@@ -279,10 +282,12 @@"_open_inline_code_run": "core.inference.tool_call_parser._open_inline_code_run(text: 'str', pos: 'int') -> 'bool'","_parse_bare_rehearsals": "core.inference.tool_call_parser._parse_bare_rehearsals(content: 'str', *, id_offset: 'int' = 0, enabled_tool_names: 'Optional[set]' = None) -> 'list[dict]'","_parse_closed_call_unescaped_quotes": "core.inference.tool_call_parser._parse_closed_call_unescaped_quotes(content: 'str', *, id_offset: 'int') -> 'list[dict]'",+ "_parse_deepseek_envelope": "core.inference.tool_call_parser._parse_deepseek_envelope(content: 'str', begin: \"'re.Match'\", *, id_offset: 'int', allow_incomplete: 'bool') -> \"'tuple[list[dict], int]'\"","_parse_deepseek_tool_calls": "core.inference.tool_call_parser._parse_deepseek_tool_calls(content: 'str', *, id_offset: 'int', allow_incomplete: 'bool' = True) -> 'list[dict]'","_parse_function_xml": "core.inference.tool_call_parser._parse_function_xml(content: 'str', *, id_offset: 'int', allow_incomplete: 'bool' = True) -> 'list[dict]'","_parse_gemma_tool_calls": "core.inference.tool_call_parser._parse_gemma_tool_calls(content: 'str', *, id_offset: 'int', allow_incomplete: 'bool' = True, enabled_tool_names: 'Optional[set]' = None) -> 'list[dict]'","_parse_glm_tool_calls": "core.inference.tool_call_parser._parse_glm_tool_calls(content: 'str', *, id_offset: 'int', allow_incomplete: 'bool' = True) -> 'list[dict]'",+ "_parse_glued_deepseek_envelopes": "core.inference.tool_call_parser._parse_glued_deepseek_envelopes(content: 'str', pos: 'int', *, id_offset: 'int') -> 'list[dict]'","_parse_kimi_section_body": "core.inference.tool_call_parser._parse_kimi_section_body(body: 'str', *, id_offset: 'int', allow_incomplete: 'bool' = True) -> 'list[dict]'","_parse_kimi_tool_calls": "core.inference.tool_call_parser._parse_kimi_tool_calls(content: 'str', *, id_offset: 'int', allow_incomplete: 'bool' = True) -> 'list[dict]'","_parse_llama3_bare_json": "core.inference.tool_call_parser._parse_llama3_bare_json(content: 'str', *, id_offset: 'int', allow_incomplete: 'bool' = True, enabled_tool_names: 'Optional[set]' = None) -> 'list[dict]'",@@ -391,8 +396,10 @@"_func_close_index","_gemma_arguments_to_json","_gemma_span_ranges",+ "_glued_call_objects","_in_code","_inside_open_parameter",+ "_is_strict_json","_iter_bracket_spans","_json_object_body","_loads_wrapped_call",@@ -436,8 +443,10 @@"_func_close_index": "core.tool_healing._func_close_index(content: 'str', body_start: 'int', body: 'str') -> 'int'","_gemma_arguments_to_json": "core.tool_healing._gemma_arguments_to_json(args_src: 'str') -> 'dict'","_gemma_span_ranges": "core.tool_healing._gemma_span_ranges(text: 'str') -> 'list'",+ "_glued_call_objects": "core.tool_healing._glued_call_objects(content: 'str', pos: 'int', end: 'int')","_in_code": "core.tool_healing._in_code(spans, pos: 'int') -> 'bool'","_inside_open_parameter": "core.tool_healing._inside_open_parameter(content: 'str', pos: 'int', *, param_start_re=None, param_closers=('</parameter>',), func_closers=('</function>',)) -> 'bool'",+ "_is_strict_json": "core.tool_healing._is_strict_json(text: 'str') -> 'bool'","_iter_bracket_spans": "core.tool_healing._iter_bracket_spans(text: 'str', start: 'int' = 0, enabled_tool_names=None)","_json_object_body": "core.tool_healing._json_object_body(body: 'str') -> 'dict | None'","_loads_wrapped_call": "core.tool_healing._loads_wrapped_call(text: 'str', *, relaxed: 'bool' = True) -> 'dict'",diff --git a/studio/backend/tests/test_tool_call_glued_second_call.py b/studio/backend/tests/test_tool_call_glued_second_call.pynew file mode 100644index 00000000000..793c9301564--- /dev/null+++ b/studio/backend/tests/test_tool_call_glued_second_call.py@@ -0,0 +1,147 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Auto-Heal runs a second call glued after the first: a second DeepSeek envelope, or a second+call object inside one <tool_call>. Strict mode, quoted markup and split arguments are unchanged."""++from __future__ import annotations++import json+import sys+import time+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)++from core import tool_healing+from core.inference.tool_call_parser import parse_tool_calls_from_text++_TOOLS = {"web_search", "search_docs", "python"}+_DS_BEGIN = "<|tool▁calls▁begin|>"+_DS_CALL = "<|tool▁call▁begin|>"+_DS_SEP = "<|tool▁sep|>"+_DS_CALL_END = "<|tool▁call▁end|>"+_DS_END = "<|tool▁calls▁end|>"+++def _ds(name: str, args: dict) -> str:+ return _DS_BEGIN + _DS_CALL + name + _DS_SEP + json.dumps(args) + _DS_CALL_END + _DS_END+++def _obj(name: str, args: dict) -> str:+ return json.dumps({"name": name, "arguments": args})+++def _calls(text: str, heal: bool = True) -> list:+ out = parse_tool_calls_from_text(text, allow_incomplete = heal, enabled_tool_names = _TOOLS)+ return [(c["function"]["name"], json.loads(c["function"]["arguments"])) for c in out]+++A = ("web_search", {"query": "mars rover news"})+B = ("search_docs", {"query": "rope scaling", "limit": 5, "exact": True})++[email protected]("gap", ["", "\n", " \n\n", "\nAnd also:\n"])+def test_second_deepseek_envelope_runs(gap):+ text = _ds(*A) + gap + _ds(*B)+ assert _calls(text) == [A, B]+ # Auto-Heal off keeps the first envelope only.+ assert _calls(text, heal = False) == [A]+++def test_three_deepseek_envelopes_run_in_order():+ c = ("python", {"code": "print(1)"})+ assert _calls(_ds(*A) + _ds(*B) + "\n" + _ds(*c)) == [A, B, c]+++ "gap",+ ["\n```\n", " `", "<think>", "\n<tool_call>", "\n[TOOL_CALLS]", "\nExample: {", "<br>"],+)+def test_second_deepseek_envelope_after_markup_stays_text(gap):+ assert _calls(_ds(*A) + gap + _ds(*B)) == [A]+++def test_second_deepseek_envelope_needs_a_close():+ truncated = _ds(*B)[: -len(_DS_END)]+ assert _calls(_ds(*A) + truncated) == [A]+++def test_deepseek_end_markers_inside_an_argument_do_not_split():+ nasty = {"code": _DS_END + _DS_BEGIN + _DS_CALL + "web_search" + _DS_SEP + '{"query": "x"}'}+ assert _calls(_ds("python", nasty)) == [("python", nasty)]+ assert _calls(_ds("python", nasty) + _ds(*A)) == [("python", nasty), A]++[email protected]("sep", ["", "\n", " "])+def test_two_objects_in_one_tool_call_tag_run(sep):+ text = "<tool_call>\n" + _obj(*A) + sep + _obj(*B) + "\n</tool_call>"+ assert _calls(text) == [A, B]+ assert _calls(text, heal = False) == []+++ "between",+ ["\nAnd also:\n", ",", "\n```\n", "\n</tool_call><tool_call>\n"],+)+def test_one_tag_with_non_whitespace_between_objects_is_unchanged(between):+ text = "<tool_call>\n" + _obj(*A) + between + _obj(*B) + "\n</tool_call>"+ calls = _calls(text)+ assert calls[:1] == [A]+ if between.strip() != "</tool_call><tool_call>":+ assert calls == [A]+++ "code",+ [+ "x = {'a': 1}{'b': 2}",+ '"}{"name": "web_search", "arguments": {"query": "evil"}}',+ "</tool_call><tool_call>\n" + _obj("web_search", {"query": "evil"}),+ "s = '}{' + '</tool_call>'",+ ],+)+def test_braces_and_closers_inside_a_string_never_split_a_call(code):+ one = "<tool_call>\n" + _obj("python", {"code": code}) + "\n</tool_call>"+ assert _calls(one) == [("python", {"code": code})]+ glued = "<tool_call>\n" + _obj("python", {"code": code}) + _obj(*A) + "\n</tool_call>"+ assert _calls(glued) in ([("python", {"code": code}), A], [("python", {"code": code})])+++def test_a_glued_object_that_is_not_a_call_keeps_main_behaviour():+ text = "<tool_call>\n" + _obj(*A) + '{"result": 1}' + "\n</tool_call>"+ assert _calls(text) == [A]+ text = "<tool_call>\n" + _obj(*A) + '{"name": "x", "arguments": "y"}' + "\n</tool_call>"+ assert _calls(text) == [A]+++def test_glued_spans_tile_the_whole_tag():+ text = "pre <tool_call>\n" + _obj(*A) + _obj(*B) + "\n</tool_call> post"+ calls, spans = tool_healing.parse_tool_calls_from_text(text, with_spans = True)+ assert [c["function"]["name"] for c in calls] == ["web_search", "search_docs"]+ assert [c["id"] for c in calls] == ["call_0", "call_1"]+ assert spans[0][0] == text.index("<tool_call>")+ assert spans[0][1] == spans[1][0]+ assert spans[1][1] == text.index("</tool_call>") + len("</tool_call>")+++def test_glued_parsing_stays_linear_on_large_inputs():+ unit_ds = _ds(*A)+ many_ds = unit_ds * (100_000 // len(unit_ds))+ many_obj = "<tool_call>" + _obj(*A) * (100_000 // len(_obj(*A))) + "</tool_call>"+ open_braces = "<tool_call>" + _obj(*A) + "{" * 100_000 + "</tool_call>"+ for text, want in ((many_ds, 100_000 // len(unit_ds)), (many_obj, None), (open_braces, 1)):+ t0 = time.perf_counter()+ calls = parse_tool_calls_from_text(text, enabled_tool_names = _TOOLS)+ assert time.perf_counter() - t0 < 5.0+ if want is not None:+ assert len(calls) == want+++def test_a_near_json_first_object_glues_nothing():+ first = "{'name': 'web_search', 'arguments': {'query': 'mars rover news'}}"+ assert _calls("<tool_call>\n" + first + "\n" + _obj(*B) + "\n</tool_call>") == [A]diff --git a/studio/backend/tests/tools/refactor_guard.py b/studio/backend/tests/tools/refactor_guard.pyindex 7275715bf35..e09990a0504 100644--- a/studio/backend/tests/tools/refactor_guard.py+++ b/studio/backend/tests/tools/refactor_guard.py@@ -262,6 +262,8 @@ _ARG_FIXTURES = {"markers": lambda text: _tool_healing_build_markers(text),"patterns": lambda text: _tool_healing_all_pats(),"strip_segment": lambda text: (lambda segment, is_last: segment),+ # The DeepSeek opener match the envelope parser is handed; None (no opener) pins the raise.+ "begin": lambda text: _parser_deepseek_begin(text),}@@ -301,6 +303,11 @@ def _parser_first_foreign_signal(text: str):return tool_call_parser._first_foreign_tool_signal(text)+def _parser_deepseek_begin(text: str):+ from core.inference import tool_call_parser+ return tool_call_parser._DEEPSEEK_BEGIN_RE.search(text)++def _tool_healing_build_markers(text: str):from core import tool_healingreturn tool_healing._build_markers(text)