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.

179 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Backend-neutral tool-call parser for all backends; tolerates missing closing tags from truncation."""

# Lazy annotations keep the standalone python 3.9 import working.
from __future__ import annotations

import json
import bisect
import re
from typing import Any, Optional

# Qwen/Hermes, Qwen3.5 XML and Gemma 4 live in core.tool_healing; this module adds the rest
from core import tool_healing as _tool_healing
from core.inference.relaxed_json import relaxed_allowed_at, relaxed_json_object, relaxed_object_end

# Shared with tool_healing so every markerless parse path applies the same guard; wrapped and marker forms are unaffected.
_markerless_promotable = _tool_healing._markerless_promotable
_markerless_blocked_execution = _tool_healing._markerless_blocked_execution
_markerless_execution_class = _tool_healing._markerless_execution_class


# Flip the streaming buffer STREAMING->DRAINING so partial markup never leaks.
TOOL_XML_SIGNALS = (
    "<tool_call>",
    "<function=",
    '<function name="',
    "<|python_tag|>",
    "[TOOL_CALLS]",
    "<|tool_call>",
    # Bare rehearsal marker (``name[ARGS]{...}``): hold it so it is promoted, not leaked as prose.
    "[ARGS]",
    # DeepSeek R1 / V3 / V3.1 -- 5 opener variants llama.cpp keeps.
    "<|tool▁calls▁begin|>",
    "<|tool▁call▁begin|>",
    "<|tool_calls_begin|>",
    "<|tool▁calls|>",
    "<|tool calls begin|>",
    "<|tool\\_calls\\_begin|>",
    "<|tool_calls_section_begin|>",
    "<|tool_call_begin|>",
    "<|content_invoke_tool_json|>",
    "<minimax:tool_call>",
)


# DeepSeek opener variants; shared by parse and strip so a parsed signal is always stripped
_DEEPSEEK_OPEN_ALT = (
    r"tool▁calls▁begin|tool_calls_begin|tool calls begin|tool\\_calls\\_begin|tool▁calls"
)
_DEEPSEEK_OPEN_RE_SRC = r"<|(?:" + _DEEPSEEK_OPEN_ALT + r")|>"

# Closed pairs only mid-stream (_TOOL_ALL_PATS eats unclosed tails at end-of-turn); name class tracks the OpenAI pattern so hyphenated MCP names parse.
_TOOL_CLOSED_PATS = [
    re.compile(r"<tool_call>.*?</tool_call>", re.DOTALL),
    # Span to the real ``</function>`` so a literal one inside a value can't truncate the strip
    re.compile(
        r'<function(?:=[\w.\-]+|\s+name="[\w.\-]+")>'
        r'(?:(?!<function(?:=[\w.\-]+|\s+name="[\w.\-]+")>).)*'
        r"</function>",
        re.DOTALL,
    ),
    re.compile(r"<\|tool_call>.*?<tool_call\|>", re.DOTALL),
    re.compile(r"\[TOOL_CALLS\]\s*\[.*?\](?:\s*</s>)?", re.DOTALL),
    # Mistral v11+ ``[TOOL_CALLS]name{json}`` (may chain), close at ``}``.
    re.compile(r"\[TOOL_CALLS\]\s*[\w\.\-]+\s*(?:\[ARGS\])?\s*\{.*?\}", re.DOTALL),
    # DeepSeek R1 / V3 / V3.1: full envelope (any opener variant) ... end.
    re.compile(_DEEPSEEK_OPEN_RE_SRC + r".*?<|tool▁calls▁end|>", re.DOTALL),
    # Kimi K2: ``<|tool_calls_section_begin|>...<|tool_calls_section_end|>``.
    re.compile(r"<\|tool_calls_section_begin\|>.*?<\|tool_calls_section_end\|>", re.DOTALL),
    # Kimi K2 section-less closed call; else the catch-all below eats trailing prose to EOS
    re.compile(r"<\|tool_call_begin\|>.*?<\|tool_call_end\|>", re.DOTALL),
]
_TOOL_ALL_PATS = _TOOL_CLOSED_PATS + [
    re.compile(r"<tool_call>.*$", re.DOTALL),
    re.compile(r'<function(?:=[\w.\-]+|\s+name="[\w.\-]+")>.*$', re.DOTALL),
    # Drop a truncated tail only when a call-shaped start follows, or a bare marker sits at EOF.
    re.compile(r"<\|tool_call>(?=\s*call\s*:|\s*$).*$", re.DOTALL),
    re.compile(
        r"\[TOOL_CALLS\](?=\s*(?:[\[{]|[A-Za-z_][\w.\-]*(?:[\[{]|\s*$))|\s*$).*$",
        re.DOTALL,
    ),
    re.compile(
        r"<\|python_tag\|>(?=\s*(?:\{|[A-Za-z_][\w.]*\()|\s*$).*$",
        re.DOTALL,
    ),
    # DeepSeek envelopes truncated mid-stream (any opener); same call-shaped lookahead as above
    re.compile(
        _DEEPSEEK_OPEN_RE_SRC + r"(?=\s*(?:<|tool▁call▁begin|>|function)|\s*$).*$",
        re.DOTALL,
    ),
    re.compile(r"<|tool▁call▁begin|>(?=\s*function|\s*$).*$", re.DOTALL),
    re.compile(
        r"<\|tool_calls_section_begin\|>(?=\s*<\|tool_call_begin\|>|\s*$).*$",
        re.DOTALL,
    ),
    re.compile(
        r"<\|tool_call_begin\|>(?=\s*[A-Za-z_][\w.\-]*:\d|\s*$).*$",
        re.DOTALL,
    ),
    # Gemma wrapper-less ``call:NAME{...}`` is handled by ``_strip_gemma_wrapperless_calls`` (enabled-name gate)
]


TOOL_ERROR_PREFIXES = (
    "Error:",
    "Error ",
    "Search failed",
    "Execution error",
    "Blocked:",
    "Exit code",
    "Failed to fetch",
    "Failed to resolve",
    "No query provided",
)

RENDER_HTML_REPEAT_NUDGE = (
    "Error: render_html was already called for this response. Do not call "
    "render_html again in this response unless the user asks for changes. "
    "Provide the final answer now."
)

TOOL_ERROR_NUDGE = (
    "\n\nThe tool call encountered an issue. Please try a different "
    "approach or rephrase your request."
)

BUDGET_EXHAUSTED_NUDGE = (
    "You have used all available tool calls. Based on everything you "
    "have found so far, provide your final answer now. Do not call "
    "any more tools."
)

# The exact-args dup guard misses paraphrased re-searches, so also cap KB searches per turn
RAG_MAX_SEARCHES_PER_TURN = 3
# Retrieved passages land in the protected current exchange, so uncapped searches overflow.
RAG_SEARCH_TOOLS = frozenset({"search_knowledge_base", "search_conversation"})
RAG_SEARCH_CAP_NUDGE = (
    "You have already searched the knowledge base several times this turn. "
    "Do not search again. Answer the question using the passages already "
    "retrieved above; if they do not contain the answer, say so plainly."
)


# Narrow on purpose: "install"/"add"/"open" are advice to the user, not work this turn.
_ACTION_VERB = (
    r"(?:search|check|look|find|fetch|get|call|use|run|query|invoke|analy[sz]e"
    r"|review|inspect|read|gather|examine|retrieve|browse|consult|verify"
    r"|confirm|compute|calculate|determine|identify|render)"
)
# Sign-offs that name no work only close a question to the user; help you with an action after it still reads as a plan.
_SIGN_OFF = r"(?:dig\s+in|help\s+analy[sz]e)\b(?=[^\w]*\Z)"  # \Z not $: a later line is still work
_HELP_OFFER = (
    r"(?:do(?:ing)?\s+my\s+best|try\s+my\s+best|be\s+(?:able|happy|glad)\s+to\b"
    r"|assist\b|help\s+you\b(?!\s+" + _ACTION_VERB + r")|give\s+you\s+accurate\b"
    r"|" + _SIGN_OFF + r")"
)
# Forward-looking intent: the model says what it *will* do, not a final answer.
INTENT_SIGNAL = re.compile(
    r"(?im)("
    # Direct intent ("I'll"); lookahead drops negated forms ("I will not").
    r"\b(i['\u2019](ll|m going to|m gonna)|i am (going to|gonna)|i will|i shall)\b"
    r"(?!\s+(?:not|never)\b)(?!\s+" + _HELP_OFFER + r")"
    r"|"
    # "let me know" hands control back rather than announcing an action.
    r"\b(?:let me|allow me)\b(?!\s+(?:not|never|know)\b)"
    # bare "assist" still names work here ("let me assist by searching"), so only the sign-offs skip the "to"
    r"(?!\s+" + _SIGN_OFF + r")(?!\s+to\s+" + _HELP_OFFER + r")"
    r"|"
    # "first" must open a sentence and be followed by a plan, else it is prose.
    r"(?:^|[.!?]\s+)\s*(?:the\s+)?first\s+step\b"
    r"|(?:^|[.!?]\s+)\s*first\s*[,:–—-]?\s+(?:my|our)\s+(?:plan|approach|step)\b"
    r"|(?:^|[.!?]\s+)\s*first\s*[,:–—-]?\s+(?:i|we|let['’]?s|let us)\b"
    r"|(?:^|[.!?]\s+)\s*first\s*[,:–—-]?\s+" + _ACTION_VERB + r"\b"
    r"|"
    # "Step N" counts only before an action (same line, next non-blank line or its list item) or as a "Step N:" heading:
    # a bare match re-prompted finished answers like "Step 1 result: 391", re-running every tool.
    r"\bstep \d+[ \t*_]*(?::[ \t*_\r]*$|(?:[:,.)\u2013\u2014-][ \t*_]*)?(?:\r?\n(?:[ \t]*\r?\n)?[ \t*_#>-]*(?:\d+[.)][ \t*_]*)?)?(?:(?:let['’]?s|let us)\b|"
    + _ACTION_VERB
    + r"\b))"
    r"|"
    r"\bhere['\u2019]?s (?:my |the |a )?(?:plan|approach)"
    r"|"
    r"\b(?:now i|next i)\b"
    r")"
)
# Matches the GGUF default of 3 re-prompts; the safetensors and MLX loops inherit this cap from here.
MAX_ACT_REPROMPTS = 3
REPROMPT_MAX_CHARS = 2000
# Composer badge shown while a hidden re-prompt regenerates, else the UI looks hung; matched exactly by utils/tool-status.ts, keep in sync.
NUDGE_TOOL_CALLS_STATUS = "Nudging tool calls"


def is_short_intent_without_action(text: str) -> bool:
    stripped = text.strip()
    return 0 < len(stripped) < REPROMPT_MAX_CHARS and INTENT_SIGNAL.search(stripped) is not None


# Leading marks are kept unless quotes/brackets, so ".NET", "C++" and "C#" stay distinct.
_REPEAT_TRAIL_PUNCT = ".,;:!?\"'`()[]{}<>‘’“”"
_REPEAT_LEAD_PUNCT = "\"'`([{‘“"


def _normalize_for_repeat(text: str) -> str:
    words = []
    for word in text.lower().split():
        stripped = word.rstrip(_REPEAT_TRAIL_PUNCT).lstrip(_REPEAT_LEAD_PUNCT)
        # Keep marks-only tokens: value is 5 and value is < 5 differ, else dropping the < throws the corrected attempt away.
        words.append(stripped or word)
    return " ".join(words)


# Exact after normalisation: every fuzzy relaxation tried lost real corrections. A missed
# repeat costs one nudge; a false one strands the plan.
def is_reprompt_repeat(text: str, previous: str) -> bool:
    return is_reprompt_restatement(text, previous)


# Discards the turn, so appended answers must not match and deletions can flip meaning.
def is_reprompt_restatement(text: str, previous: str) -> bool:
    if not previous:
        return False
    a, b = _normalize_for_repeat(text), _normalize_for_repeat(previous)
    return bool(a) and a == b


def reprompt_to_act_message(tool_hint: str) -> str:
    """The user message appended when re-prompting a plan-without-action turn."""
    return (
        "You have access to enabled tools. If a tool is needed to satisfy "
        "the user's request or complete the action you described, call "
        f"{tool_hint} now. If no tool is needed, provide the final answer "
        "and follow the user's requested format."
    )


# Only the tail is carried: replaying the whole thought reproduces the same ending.
_LENGTH_PROGRESS_TAIL_CHARS = 600


def unfinished_thought_progress(reasoning: str) -> str:
    """The short note that stands in for a thought the window cut off."""
    tail = reasoning.strip()[-_LENGTH_PROGRESS_TAIL_CHARS:].lstrip()
    return f"Where I had got to:\n{tail}"


def starved_result_message(tool_name: str, result: str) -> str:
    """Said on the first such call, since the room is known before the tool runs, not after repeats."""
    return (
        f"{result}\n\n"
        f"[No room in the window for this result, so {tool_name} returned nothing usable. "
        "Re-reading will not help. Continue from what you have, and deliver the work in "
        "parts if it will not fit at once.]"
    )


def repeated_result_message(tool_name: str, times: int, last_result: str) -> str:
    """Appended to a result the tool has now returned unchanged several times. The last result is
    kept rather than replaced: it may be the truncation notice that caused the repeats, and
    dropping it would leave the model with less than it had."""
    return (
        f"{last_result}\n\n"
        f"[{tool_name} returned exactly this {times} times; it will not change. Continue "
        "from what you have, in parts if needed, and finish the task.]"
    )


def thinking_exhausted_message(context_length: Optional[int] = None) -> str:
    """Lower reasoning effort: a thought that did not fit once will not fit on retry. Build text by substitution, not format on pre-spaced fragments."""
    window = f"{context_length}-token " if context_length else ""
    return (
        f"The model spent the whole of its {window}window on reasoning and had none "
        "left for an answer, twice in a row.\n\n"
        "To get past this:\n"
        "- Lower the reasoning effort, or turn thinking off\n"
        "- Or raise the context length, so a thought and an answer both fit\n"
        "- Or ask for a smaller piece of the task at a time"
    )


def reasoning_cap_spent_message(max_tokens: Optional[int] = None) -> str:
    """Max Tokens set by the caller stopped it, not the window, so raising the window is the wrong lever; as plain content the frontend cannot fix it."""
    allowance = f" of {max_tokens} tokens" if max_tokens else ""
    return (
        f"The model used its whole output allowance{allowance} on reasoning and had none "
        "left for an answer.\n\n"
        "To get past this:\n"
        "- Raise Max Tokens, so a thought and an answer both fit\n"
        "- Or lower the reasoning effort, or turn thinking off\n"
        "- Or ask for a smaller piece of the task at a time"
    )


def continue_after_length_message() -> str:
    """The user message appended when a turn ended inside its own reasoning. The instruction is to
    ACT, not to think more carefully: the previous turn ended with nothing to show because
    thinking consumed the whole window, so asking for more deliberation is asking for the same
    ending a second time."""
    return (
        "You ran out of room while thinking, so nothing was produced. Stop thinking and "
        "act: call a tool or answer now. If it will not all fit, deliver the first part "
        "and continue after."
    )


# Qwen / Hermes ``<tool_call>{json}``.
_TC_JSON_START_RE = re.compile(r"<tool_call>\s*\{")
# Qwen3.5 ``<function=name>`` and attribute form ``<function name="name">`` (MiniCPM-5, MiniMax-M2).
_TC_FUNC_START_RE = re.compile(r'<function(?:=([\w\.\-]+)|\s+name="([\w\.\-]+)")>\s*')
# Stop at the close tag even when prose follows, else prose leaks into args.
_TC_END_TAG_RE = re.compile(r"</(?:tool_call|function)>")
# Byte-identical to the healer's; shared so the pair cannot drift.
_TC_FUNC_CLOSE_RE = _tool_healing._TC_FUNC_CLOSE_RE
# Horizontal whitespace only so the wrapping newline and code indentation survive.
_TC_PARAM_START_RE = re.compile(
    r'<(?:parameter|param)(?:=([\w\.\-]+)|\s+name="([\w\.\-]+)")>[^\S\n]*'
)
_TC_PARAM_CLOSE_RE = re.compile(r"\s*</(?:parameter|param)>\s*$")

# Llama-3 ``<|python_tag|>NAME.call(...)``.
_LLAMA3_PYTHON_TAG = "<|python_tag|>"
_LLAMA3_PY_CALL_RE = re.compile(
    r"<\|python_tag\|>\s*([\w\.\-]+)\s*\.\s*call\s*\(",
)
# Anchored at a fixed offset so ``.call(`` inside JSON args is ignored.
_LLAMA3_PY_CALL_HEAD_RE = re.compile(r"\s*([\w\.\-]+)\s*\.\s*call\s*\(")
_LLAMA3_CALL_CHAIN_RE = re.compile(r"\s*;\s*([\w\.\-]+)\s*\.\s*call\s*\(")
# Hand-scanned, not finditer, to stay linear on a truncated body (ReDoS).
_LLAMA3_KEY_RE = re.compile(r"\w+")
_LLAMA3_WS_RE = re.compile(r"\s*")
# ``(?![\w.])`` stops ``1.2.3`` being truncated to ``1.2``.
_LLAMA3_NUM_RE = re.compile(r"-?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?(?![\w.])")
_LLAMA3_LIT_RE = re.compile(r"true|false|null")

# Mistral v11+ chains triggers, each followed by ``name{json}`` or ``name[ARGS]{json}``.
_MISTRAL_TRIGGER = "[TOOL_CALLS]"
_MISTRAL_ARGS_MARKER = "[ARGS]"
# Mistral Small 3.2 emits ``name[CALL_ID]<id>[ARGS]{json}``.
_MISTRAL_CALL_ID_MARKER = "[CALL_ID]"
# A ``[TOOL_CALLS]`` inside Magistral's ``[THINK]`` block is chain-of-thought, not a call.
_MISTRAL_THINK_OPEN = "[THINK]"
_MISTRAL_THINK_CLOSE = "[/THINK]"
_MISTRAL_V11_NAME_RE = re.compile(r"\s*([\w\.\-]+)\s*")

# DeepSeek markers (full-width pipe U+FF5C, block U+2581); five outer-open variants like llama.cpp.
_DEEPSEEK_BEGIN_RE = re.compile(_DEEPSEEK_OPEN_RE_SRC)
_DEEPSEEK_END = "<|tool▁calls▁end|>"
_DEEPSEEK_CALL_BEGIN = "<|tool▁call▁begin|>"
_DEEPSEEK_SEP = "<|tool▁sep|>"
_DEEPSEEK_CALL_END = "<|tool▁call▁end|>"
# R1 wraps args in a ```json fence; str.find, since the regex forms are O(N^2) on truncation.
_DEEPSEEK_R1_FUNC_MARKER = "function" + _DEEPSEEK_SEP
_DEEPSEEK_R1_FENCE = "\n```json\n"
_DEEPSEEK_R1_CLOSE_RE = re.compile(r"```[\s\r\n]*" + re.escape(_DEEPSEEK_CALL_END))

# GLM 4.5-4.7 name class keeps prose like <tool_call>not a call</tool_call> unparsed; the newline after the name is optional, so zero-arg calls close at once.
_GLM_TC_OPEN_RE = re.compile(r"<tool_call>\s*([\w.\-]+)\s*(?=\n|<arg_key>|</tool_call>)")
_GLM_TC_CLOSE = "</tool_call>"
_GLM_ARG_KEY_OPEN = "<arg_key>"
_GLM_ARG_KEY_CLOSE = "</arg_key>"
_GLM_ARG_VAL_OPEN = "<arg_value>"
_GLM_ARG_VAL_CLOSE = "</arg_value>"
# Only unambiguous JSON literals decode; bare ``42``/``true``/``null`` stay strings.
_GLM_JSON_NUMERIC_RE = re.compile(r"-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?")

# Kimi K2 / Moonshot (ASCII pipes). Id ``functions.NAME:IDX`` -- strip ``functions.``/``:N`` for the name.
_KIMI_SECTION_BEGIN = "<|tool_calls_section_begin|>"
_KIMI_SECTION_END = "<|tool_calls_section_end|>"
_KIMI_CALL_BEGIN = "<|tool_call_begin|>"
_KIMI_ARG_BEGIN = "<|tool_call_argument_begin|>"
_KIMI_CALL_END = "<|tool_call_end|>"
_KIMI_ID_RE = re.compile(r"^(?:functions\.)?([\w\.\-]+)(?::(\d+))?$")

# MiniMax-M2 / M2.1: ``<minimax:tool_call><invoke name="NAME"><parameter name="KEY">VALUE</parameter></invoke></minimax:tool_call>``.
_MINIMAX_BLOCK_OPEN = "<minimax:tool_call>"
_MINIMAX_BLOCK_CLOSE = "</minimax:tool_call>"
_MINIMAX_INVOKE_OPEN_RE = re.compile(r'<invoke\s+name="([\w\.\-]+)">')
_MINIMAX_INVOKE_CLOSE = "</invoke>"

# Gemma 4: ``<|tool_call>call:NAME{...}<tool_call|>``, ``<|"|>`` wraps strings.
_GEMMA_TC_RE = re.compile(r"<\|tool_call>\s*call\s*:\s*([\w\.\-]+)\s*\{")
_GEMMA_STR_BEGIN = '<|"|>'
_GEMMA_STR_END = '<|"|>'
_GEMMA_TC_END = "<tool_call|>"

# skip_special_tokens strips the wrapper, so streamed Gemma calls arrive bare.
# A value OPENED by Gemma's string delimiter (``key:<|"|>``); a literal one inside another quote is data.
_GEMMA_QUOTED_VALUE_RE = re.compile(r'(?:^|[{,\[])\s*[A-Za-z_][\w.\-]*\s*:\s*<\|"\|>')
_GEMMA_BARE_TC_RE = re.compile(r"(?<!\w)call\s*:\s*([\w\.\-]+)\s*\{")
# Partial prefix so the streaming buffer holds it instead of leaking visible text.
_GEMMA_BARE_TC_PREFIX_RE = re.compile(r"(?<!\w)call\s*(?::\s*[\w\.\-]*)?$")
# Keys start with a letter/underscore so ``10:00, 11:00`` in a value isn't misread as a new key
_GEMMA_KEY_RE = re.compile(r"\s*([A-Za-z_][\w.\-]*)\s*:")
# JSON number literals only, as tool_healing decides for the wrapped form (int()/float() take "007", "+1", "nan").
_GEMMA_JSON_NUMBER_RE = re.compile(r"-?(?:0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?", re.ASCII)


def leading_bare_gemma_call_is_promotable(stripped: str, enabled_tool_names) -> bool:
    """Must agree with the parser or the turn is held to EOS; the call:partial prefix stays ungated since call:term may still become call:termdict."""
    m = _GEMMA_BARE_TC_RE.match(stripped)
    return m is not None and _markerless_promotable(m.group(1), enabled_tool_names)


# Brackets-only depth: a stray ``}`` must not end the span early.
def _balanced_bracket_end(src: str, start: int) -> "int | None":
    return _tool_healing._balanced_bracket_end(src, start, braces_count = False)


def _skip_mistral_call_id(text: str, pos: int) -> int:
    """Skip an optional ``[CALL_ID]<id>`` (Mistral Small 3.2); return the next token pos."""
    n = len(text)
    i = pos
    while i < n and text[i] in " \t\n\r":
        i += 1
    if not text.startswith(_MISTRAL_CALL_ID_MARKER, i):
        return pos
    i += len(_MISTRAL_CALL_ID_MARKER)
    while i < n and text[i] in " \t\n\r":
        i += 1
    # The id is a short opaque token; stop at whitespace or the next marker.
    while i < n and text[i] not in " \t\n\r[{":
        i += 1
    while i < n and text[i] in " \t\n\r":
        i += 1
    return i


def _strip_mistral_reasoning(content: str) -> str:
    """Drop a leading Magistral ``[THINK]...[/THINK]`` so a ``[TOOL_CALLS]`` inside reasoning is not
    taken as a real call; an unclosed ``[THINK]`` drops from it on."""
    i = 0
    n = len(content)
    while i < n and content[i] in " \t\n\r":
        i += 1
    if not content.startswith(_MISTRAL_THINK_OPEN, i):
        return content
    close = content.find(_MISTRAL_THINK_CLOSE, i + len(_MISTRAL_THINK_OPEN))
    if close == -1:
        return content[:i]
    return content[:i] + content[close + len(_MISTRAL_THINK_CLOSE) :]


def _strip_mistral_closed_calls(text: str) -> str:
    """Strip cleanly-closed ``[TOOL_CALLS]`` blocks (array, ``name{json}``, ``name[ARGS]{json}``)
    via balanced scanning -- a non-greedy ``\\{.*?\\}`` would truncate at the first ``}`` and
    lose nested JSON. Unclosed runs are left for ``final=True`` cleanup."""
    n = len(text)
    out = []
    cursor = 0
    while cursor < n:
        idx = text.find(_MISTRAL_TRIGGER, cursor)
        if idx == -1:
            out.append(text[cursor:])
            break
        out.append(text[cursor:idx])
        body_start = idx + len(_MISTRAL_TRIGGER)
        i = body_start
        while i < n and text[i] in " \t\n\r":
            i += 1
        if i < n and text[i] == "[":
            end = _balanced_bracket_end(text, i)
            if end is None:
                out.append(text[idx:])
                break
            cursor = end + 1
            if text.startswith("</s>", cursor):
                cursor += len("</s>")
            continue
        # Single-object shape ``[TOOL_CALLS] { json }``
        if i < n and text[i] == "{":
            end = _balanced_brace_end(text, i)
            if end is None:
                out.append(text[idx:])
                break
            cursor = end + 1
            if text.startswith("</s>", cursor):
                cursor += len("</s>")
            continue
        name_match = _MISTRAL_V11_NAME_RE.match(text, i)
        if not name_match:
            out.append(text[idx:body_start])
            cursor = body_start
            continue
        i = name_match.end()
        while i < n and text[i] in " \t\n\r":
            i += 1
        i = _skip_mistral_call_id(text, i)
        if text.startswith(_MISTRAL_ARGS_MARKER, i):
            i += len(_MISTRAL_ARGS_MARKER)
            while i < n and text[i] in " \t\n\r":
                i += 1
        if i >= n or text[i] != "{":
            out.append(text[idx:i])
            cursor = i
            continue
        end = _balanced_brace_end(text, i)
        if end is None:
            out.append(text[idx:])
            break
        cursor = end + 1
        # Consume the optional EOS too so ``</s>`` is not left as content.
        if text.startswith("</s>", cursor):
            cursor += len("</s>")
    return "".join(out)


# Strips run per segment after ``strip_outside_think``, so a call may follow a reasoning close.
_GEMMA_ANCHOR_CLOSERS = ("</think>", "[/THINK]", "</thinking>")


def _gemma_call_is_anchored(text: str, start: int, floor: int) -> bool:
    """Display keeps an unanchored markerless call as prose; the parser is not gated this way, so a mid-prose call stays visible rather than deleting the answer."""
    i = start - 1
    while i >= floor and text[i] in " \t\r":
        i -= 1
    if i < floor or text[i] == "\n":
        return True
    return any(text.endswith(closer, floor, i + 1) for closer in _GEMMA_ANCHOR_CLOSERS)


def blocked_markerless_prefix_end(text: str, start: int, enabled_tool_names) -> int:
    """A blocked run is consumed markup: its peer anchors to it, and the GGUF card sniff skips it."""
    cursor = start
    while True:
        probe = text[cursor:]
        lead = strip_llama3_leading_sentinels(probe.lstrip(" \t\n\r;"))
        stripped = lead
        offset = cursor + (len(probe) - len(lead))
        gem = _GEMMA_BARE_TC_RE.match(text, offset)
        if gem is not None and _markerless_blocked_execution(gem.group(1), enabled_tool_names):
            end = _gemma_body_brace_end(text, gem.end() - 1)
            if end is None:
                return cursor
            cursor = end + 1
            continue
        reh = _tool_healing._REHEARSAL_RE.match(text, offset)
        if reh is not None and _markerless_blocked_execution(reh.group(1), enabled_tool_names):
            end = _tool_healing._balanced_json_span(text, reh.end())
            if end is None:
                return cursor
            cursor = end + 1
            continue
        if stripped.startswith("{"):
            end = _balanced_brace_end(text, offset)
            name = _top_level_bare_json_name(text[offset : (end + 1) if end else len(text)])
            if end is not None and _markerless_blocked_execution(name, enabled_tool_names):
                cursor = end + 1
                continue
        # Consume the ``;`` separator too, or the peer behind it is unanchored and its raw text survives.
        return cursor + len(text[cursor:]) - len(text[cursor:].lstrip(" \t\n\r;"))


# Equal-length mask keeps offsets exact; private-use char is valid in JSON and matches nothing.
_BLOCKED_BODY_MASK = ""
_BLOCKED_BODY_MASK_RUN_RE = re.compile("+")
# The aliases ``_parse_bare_json_call`` accepts for the argument object.
_BARE_JSON_ARGS_KEYS = ("arguments", "parameters", "args")


def _decoded_key(literal: str) -> "str | None":
    """The VALUE of a JSON string literal, or None. ``"argu\\u006dents"`` is ``arguments`` to
    ``json.loads``; comparing the source spelling left such a body unmasked and the healer
    promoted the wrapper quoted inside it."""
    if "\\" not in literal:
        return literal[1:-1] if len(literal) >= 2 else None
    try:
        value = json.loads(literal)
    except ValueError:
        return None
    return value if isinstance(value, str) else None


# Keys read to classify a call; arguments are handled apart, as a blocked call keeps its structure.
_BARE_JSON_CLASSIFY_KEYS = ("name", "function")


def _top_level_maskable_values(
    text: str,
    start: int,
    end: int,
    keep: str = None,
) -> list:
    """Blanks every top-level field of a blocked call except arguments and the classification in use; the other classification field is data too."""
    spans: list = []
    skip = _BARE_JSON_ARGS_KEYS
    depth = 0
    i = start
    while i < end:
        ch = text[i]
        if ch == '"':
            j = i + 1
            while j < end and text[j] != '"':
                j += 2 if text[j] == "\\" else 1
            key = _decoded_key(text[i : j + 1]) if depth == 1 else None
            if depth == 1 and key not in skip:
                k = j + 1
                while k < end and text[k].isspace():
                    k += 1
                if k < end and text[k] == ":":
                    # The key is data too: a wrapper spelled into the key of a truncated object must not be promoted.
                    if key not in _BARE_JSON_CLASSIFY_KEYS:
                        spans.append((i + 1, min(j, end)))
                    k += 1
                    while k < end and text[k].isspace():
                        k += 1
                    if k < end and text[k] in "{[":
                        closer = _balanced_brace_end if text[k] == "{" else _balanced_bracket_end
                        stop = closer(text, k)
                        if stop is None:
                            spans.extend(_string_content_spans(text, k + 1, end))
                            return spans
                        spans.extend(_string_content_spans(text, k + 1, stop))
                        i = stop + 1
                        continue
                    if k < end and text[k] == '"':
                        stop = k + 1
                        while stop < end and text[stop] != '"':
                            stop += 2 if text[stop] == "\\" else 1
                        in_use = (
                            key in _BARE_JSON_CLASSIFY_KEYS
                            and keep is not None
                            and _decoded_key(text[k : min(stop, end) + 1]) == keep
                        )
                        if not in_use:
                            spans.append((k + 1, min(stop, end)))
                        if stop >= end:
                            return spans
                        i = stop + 1
                        continue
                    # A scalar holds no markup; keep walking so later fields still mask.
                    i = k
                    continue
            i = j + 1
            continue
        if ch in "{[":
            depth += 1
        elif ch in "}]":
            depth -= 1
            if depth == 0:
                return spans
        i += 1
    return spans


def _top_level_args_values(text: str, start: int, end: int) -> list:
    """Every top-level arguments value, not the first match: json.loads keeps the last duplicate key."""
    values: list = []
    depth = 0
    i = start
    while i < end:
        ch = text[i]
        if ch == '"':
            j = i + 1
            while j < end and text[j] != '"':
                j += 2 if text[j] == "\\" else 1
            if depth == 1 and _decoded_key(text[i : j + 1]) in _BARE_JSON_ARGS_KEYS:
                k = j + 1
                while k < end and text[k].isspace():
                    k += 1
                if k < end and text[k] == ":":
                    k += 1
                    while k < end and text[k].isspace():
                        k += 1
                    # Arrays too: the healer still reads markup inside one.
                    if k < end and text[k] in "{[":
                        closer = _balanced_brace_end if text[k] == "{" else _balanced_bracket_end
                        stop = closer(text, k)
                        if stop is None:
                            # Truncated mid-stream: the body reaches the healer and is scanned to ``end``, not left unmasked.
                            values.append((k + 1, end, False))
                            return values
                        values.append((k + 1, stop, False))
                        i = stop + 1
                        continue
                    if k < end and text[k] == '"':
                        stop = k + 1
                        while stop < end and text[stop] != '"':
                            stop += 2 if text[stop] == "\\" else 1
                        if stop >= end:
                            values.append((k + 1, end, True))
                            return values
                        values.append((k + 1, stop, True))
                        i = stop + 1
                        continue
                    # A malformed scalar body still holds raw wrapper syntax the healer reads.
                    boundary = _next_top_level_comma(text, k, end)
                    values.append((k, end if boundary is None else boundary, False))
                    return values
            i = j + 1
            continue
        if ch in "{[":
            depth += 1
        elif ch in "}]":
            depth -= 1
            if depth == 0:
                return values
        i += 1
    return values


def _string_content_spans(text: str, start: int, end: int) -> list:
    """String interiors only; Gemma's <|"|> is a quote, or masking runs past the delimiter."""
    spans: list = []
    i = start
    while i < end:
        if text.startswith(_tool_healing._GEMMA_QUOTE, i):
            close = text.find(_tool_healing._GEMMA_QUOTE, i + len(_tool_healing._GEMMA_QUOTE), end)
            if close < 0:
                break
            spans.append((i + len(_tool_healing._GEMMA_QUOTE), close))
            i = close + len(_tool_healing._GEMMA_QUOTE)
            continue
        if text[i] == '"':
            j = i + 1
            while j < end:
                if text[j] == "\\":
                    j += 2
                    continue
                if text[j] == '"':
                    break
                j += 1
            if j >= end:
                break
            spans.append((i + 1, j))
            i = j + 1
            continue
        i += 1
    return spans


def _escaped_string_content_spans(text: str, start: int, end: int) -> list:
    """Literal contents in backslash-escaped JSON; a delimiter quote has one backslash, inner ones three."""
    spans: list = []
    open_at = -1
    i = start
    while i < end:
        if text[i] == '"':
            run = 0
            k = i - 1
            while k >= start and text[k] == "\\":
                run += 1
                k -= 1
            if run == 1:
                if open_at < 0:
                    open_at = i + 1
                else:
                    # Stop before the closing ``\``, so the delimiter survives intact.
                    spans.append((open_at, i - 1))
                    open_at = -1
        i += 1
    return spans


# A wrapper immediately before makes the call trusted. ``[CALL_ID]`` is not one: alone it
# would grant trust no wrapper gave.
_MARKERLESS_TRUSTED_PREFIXES = (
    "<|tool_call>",
    "[TOOL_CALLS]",
    "<tool_call>",
    "<|python_tag|>",
)


def _merge_spans(spans: list) -> list:
    """Merges only overlapping spans; adjacent ones stay apart, as their boundary opens the next call."""
    merged: list = []
    for start, end in sorted(spans):
        if merged and start < merged[-1][1]:
            merged[-1] = (merged[-1][0], max(merged[-1][1], end))
            continue
        merged.append((start, end))
    return merged


def _strictly_inside(spans: list, pos: int) -> bool:
    """Whether ``pos`` sits INSIDE one of ``spans`` rather than opening it: a blocked
    rehearsal is itself tool markup, so its own span starts where it does."""
    i = bisect.bisect_right(spans, (pos, pos)) - 1
    return i >= 0 and spans[i][0] < pos < spans[i][1]


# Wrappers tool_healing does not parse: a blocked candidate in their arguments is call data.
_INFERENCE_WRAPPER_OPENERS = (
    _LLAMA3_PYTHON_TAG,
    "<|content_invoke_tool_json|>",
    _DEEPSEEK_CALL_BEGIN,
    _DEEPSEEK_SEP,
    _KIMI_CALL_BEGIN,
    "<|tool_call_argument_begin|>",
)


# Openers whose body can hold a markerless call as ARGUMENT text.
_CONTAINING_WRAPPERS = (
    "<tool_call>",
    "<|tool_call>",
    "<function=",
    '<function name="',
    "[TOOL_CALLS]",
    "<minimax:tool_call>",
) + _INFERENCE_WRAPPER_OPENERS


def _balanced_paren_end(text: str, paren_start: int) -> "int | None":
    """Index of the ``)`` matching the ``(`` at ``paren_start``, or None. Quotes are honoured
    so a paren inside a kwarg string does not close the call."""
    if paren_start >= len(text) or text[paren_start] != "(":
        return None
    depth = 0
    quote = ""
    i = paren_start
    while i < len(text):
        ch = text[i]
        if quote:
            if ch == "\\":
                i += 2
                continue
            if ch == quote:
                quote = ""
        elif ch in "\"'":
            quote = ch
        elif ch == "(":
            depth += 1
        elif ch == ")":
            depth -= 1
            if depth == 0:
                return i
        i += 1
    return None


def _only_a_code_fence(between: str) -> bool:
    """Blank or a fence token only; DeepSeek-R1 fences its arguments, so whitespace alone is too strict."""
    stripped = between.strip()
    return not stripped or _FENCE_ONLY_RE.fullmatch(stripped) is not None


_FENCE_ONLY_RE = re.compile(r"[\w.\-]*[ \t]*\n?[ \t]*`{3,}[a-zA-Z0-9_+-]*")


# Caps only the non-blank gap a code fence could fill; the blank part is unbounded. Refusing
# a real fence wrongly would mask the wrapper body with U+E000.
_MAX_FENCE_CHARS = 4096


def _inference_wrapper_spans(text: str) -> list:
    """Spans covering the argument object of each inference-only wrapped call."""
    spans: list = []
    for opener in _INFERENCE_WRAPPER_OPENERS:
        pos = text.find(opener)
        brace = -2  # Not yet sought. Distinct from -1, which means there is none left.
        # Gap without blank ends, held as indices so it is never copied.
        core_end = 0
        core_start = -1
        while pos != -1:
            after = pos + len(opener)
            # Re-seek the next ``{`` only once it falls behind this opener; seeking per opener is quadratic.
            if brace != -1 and brace < after:
                brace = text.find("{", after)
                core_end = brace
                while core_end > 0 and text[core_end - 1].isspace():
                    core_end -= 1
                core_start = -1
            # None at or after this opener means none at or after any later one either.
            if brace == -1:
                break
            # Only the object that follows the marker directly; anything else is not its body.
            if core_end <= after:
                trusted = True  # Blank all the way to the object, however long.
            else:
                if core_start < after:
                    core_start = after
                    while core_start < core_end and text[core_start].isspace():
                        core_start += 1
                trusted = core_end - core_start <= _MAX_FENCE_CHARS and _only_a_code_fence(
                    text[core_start:core_end]
                )
            if trusted:
                end = _balanced_brace_end(text, brace)
                if end is not None:
                    spans.append((pos, end + 1))
            pos = text.find(opener, pos + 1)
    # Llama-3 .call(k=v) kwargs are not a JSON body so the brace scan misses them; mask only the interior, or a span reaching the paren merges and stops masking.
    for m in _LLAMA3_PY_CALL_RE.finditer(text):
        end = _balanced_paren_end(text, m.end() - 1)
        spans.append((m.end(), len(text) if end is None else end))
    # Attribute-form calls have no JSON body; protect the element interior so a genuine call's
    # arguments are not masked.
    close = -2  # Not yet sought, as ``brace`` above.
    for m in _ATTR_FUNC_OPEN_RE.finditer(text):
        # Re-seek only once the close falls behind this opener; seeking per opener is quadratic.
        if close != -1 and close < m.end():
            close = text.find("</function>", m.end())
        spans.append((m.end(), len(text) if close < 0 else close))
    # A streaming Mistral array has no ``]`` yet but is still parsed; trust the envelope through EOS.
    pos = text.find(_MISTRAL_TRIGGER)
    while pos != -1:
        i = pos + len(_MISTRAL_TRIGGER)
        while i < len(text) and text[i].isspace():
            i += 1
        if i < len(text) and text[i] == "[" and _balanced_bracket_end(text, i) is None:
            spans.append((i + 1, len(text)))
        pos = text.find(_MISTRAL_TRIGGER, pos + 1)
    return spans


def _has_gemma_bare_trigger(text: str) -> bool:
    """C-level pre-check: the word call alone matches too often, so also require the regex's colon."""
    n = len(text)
    at = text.find(_GEMMA_BARE_SENTINEL)
    while at >= 0:
        i = at + len(_GEMMA_BARE_SENTINEL)
        while i < n and text[i].isspace():
            i += 1
        if i < n and text[i] == ":":
            return True
        at = text.find(_GEMMA_BARE_SENTINEL, at + 1)
    return False


# A fenced json block is a real call for some templates, so skip the fence and mask inside.
_LEADING_JSON_FENCE_RE = re.compile(r"^`{3,}[a-zA-Z0-9_+-]*[ \t]*\r?\n")


def _strip_leading_code_fence(text: str) -> str:
    """``text`` without an opening code fence line, else unchanged."""
    m = _LEADING_JSON_FENCE_RE.match(text)
    return text[m.end() :] if m else text


# Cheap necessary condition for a blocked name being present anywhere in the text.
_EXECUTION_CLASS_HINTS = tuple(_tool_healing.EXECUTION_CLASS_TOOL_NAMES) + (
    _tool_healing._MCP_TOOL_PREFIX,
)


# Openers that own the text behind them; ``call:`` is Gemma's (sentinel defined below).
_MARKUP_OPENERS = _CONTAINING_WRAPPERS + ("[ARGS]", "call:")


def _has_execution_class_hint(text: str) -> bool:
    """Any backslash counts: a JSON escape can spell python, which no literal search would find."""
    return "\\" in text or any(hint in text for hint in _EXECUTION_CLASS_HINTS)


def _blocked_markerless_body_spans(text: str, enabled_tool_names) -> list:
    """Whole balanced body of each blocked call, raw Gemma values included; an unclosed body runs to EOF."""
    spans: list = []
    # Walk the chain as the parser does; a promotable call's arguments are its own input.
    protected: list = []
    # Hoisted: it scans the whole buffer, and the resume below runs once per prose gap.
    execution_hint = _has_execution_class_hint(text)
    cursor = 0
    while cursor < len(text):
        rest = text[cursor:]
        probe = _strip_leading_code_fence(strip_llama3_leading_sentinels(rest.lstrip(" \t\r\n;")))
        shift = cursor + len(rest) - len(probe)
        lead = _leading_json_value_end(probe)
        if not lead:
            # An unclosed leading object still parses, so an execution body stays opaque through EOF.
            truncated_name = _top_level_bare_json_name(probe)
            if probe.startswith("{") and _markerless_execution_class(truncated_name):
                spans.extend(
                    (a + shift, b + shift)
                    for a, b in _top_level_maskable_values(probe, 0, len(probe), truncated_name)
                )
                # Mask the whole argument span: raw syntax outside strings would otherwise be promoted.
                spans.extend(
                    (begin + shift, stop + shift)
                    for begin, stop, _ in _top_level_args_values(probe, 0, len(probe))
                )
            # A value starts here but does not resolve: the rest is its truncated body, not a sibling.
            if probe[:1] in ("{", "["):
                break
            # Prose may prefix the call: resume at the next brace, but only when an execution-class name is present. Linear: every brace resolves or breaks above.
            nxt = text.find("{", shift) if execution_hint else -1
            if nxt < 0:
                break
            # Only across inert prose: markup in the gap owns the object behind it, as arguments or as its marker, or a python_tag command gets blanked instead of run.
            if any(opener in text[cursor:nxt] for opener in _MARKUP_OPENERS):
                break
            cursor = nxt
            continue
        # A leading ARRAY is a valid JSON value with no object in it, so ``index`` raised.
        obj = probe.find("{", 0, lead)
        if obj < 0:
            cursor = shift + lead
            continue
        name = _top_level_bare_json_name(probe[:lead])
        values = _top_level_args_values(probe, obj, lead)
        # Name only, not the enabled gate: a disabled execution name must still hide its body, or a quoted wrapper inside it gets promoted on its own.
        if _markerless_execution_class(name):
            # Mask every data field, not just arguments: scans read only the name, so the rest can stay opaque.
            spans.extend(
                (a + shift, b + shift)
                for a, b in _top_level_maskable_values(probe, obj, lead, name)
            )
            for begin, stop, is_string in values:
                inner = (
                    _escaped_string_content_spans(probe, begin, stop)
                    if is_string
                    else _string_content_spans(probe, begin, stop)
                )
                spans.extend((a + shift, b + shift) for a, b in inner)
        elif _markerless_promotable(name, enabled_tool_names):
            protected.extend((begin + shift, stop + shift) for begin, stop, _ in values)
        cursor = shift + lead
    candidates: list = []
    if _has_gemma_bare_trigger(text):
        candidates += [("gemma", m) for m in _GEMMA_BARE_TC_RE.finditer(text)]
    if "[ARGS]" in text:
        candidates += [("rehearsal", m) for m in _tool_healing._REHEARSAL_RE.finditer(text)]
    if candidates:
        candidates.sort(key = lambda c: c[1].start())
        # Linear merge and bisect; scan only for a wrapper that could hold one. The bare rehearsal literal is excluded: a span never excludes its own candidate.
        trusted = _merge_spans(
            (
                _tool_healing._tool_call_markup_spans(text) + _inference_wrapper_spans(text)
                if any(opener in text for opener in _CONTAINING_WRAPPERS)
                else []
            )
            + protected
        )
        covered = 0
        for kind, m in candidates:
            head = text[: m.start()].rstrip()
            if (
                m.start() < covered
                # Strictly inside: a rehearsal's own span starts where it does, so ``<=`` would exclude it.
                or _strictly_inside(trusted, m.start())
                or any(head.endswith(prefix) for prefix in _MARKERLESS_TRUSTED_PREFIXES)
                or not _markerless_execution_class(m.group(1))
            ):
                continue
            # ``find`` settles an open body in C; walking from the brace per snapshot is quadratic.
            body_start = m.end() if kind == "gemma" else m.end() + 1
            if text.find("}", body_start) < 0:
                end = None
            elif kind == "gemma":
                end = _gemma_body_brace_end(text, m.end() - 1)
            else:
                end = _tool_healing._balanced_json_span(text, m.end())
            if end is None:
                # Truncated: the rest is this call's arguments. Stopping here keeps the walk linear.
                spans.append((body_start, len(text)))
                break
            spans.append((body_start, end))
            covered = end
    spans = [(start, end) for start, end in spans if end > start]
    spans.sort()
    # Keep spans disjoint so mask runs map one-to-one to restored bodies.
    merged: list = []
    for start, end in spans:
        if merged and start < merged[-1][1]:
            merged[-1] = (merged[-1][0], max(merged[-1][1], end))
            continue
        merged.append((start, end))
    return merged


def _mask_blocked_bodies(
    text: str,
    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. 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)
        # The XML/JSON-only helper misses inference-only wrappers; a literal think tag in their arguments is data, or masking it hands the tool U+E000 to run.
        inference_spans = _inference_wrapper_spans(text)
        if inference_spans:
            think_spans = [
                (s, e)
                for (s, e) in think_spans
                if not any(ws <= s < we for ws, we in inference_spans)
            ]
        spans = _merge_spans(spans + think_spans)
    if not spans:
        return text, []
    out: list = []
    bodies: list = []
    prev = 0
    for start, end in spans:
        out.append(text[prev:start])
        bodies.append(text[start:end])
        out.append(_BLOCKED_BODY_MASK * (end - start))
        prev = end
    out.append(text[prev:])
    return "".join(out), bodies


def _unmask_blocked_bodies(text: str, bodies: list) -> Optional[str]:
    """Put the bodies back, or ``None`` when a pass disturbed the runs and the caller
    should fall back to the unmasked result rather than emit a mangled body."""
    if not bodies:
        return text
    restored = iter(bodies)
    count = 0

    def _put(_m):
        nonlocal count
        count += 1
        return next(restored, "")

    result = _BLOCKED_BODY_MASK_RUN_RE.sub(_put, text)
    return result if count == len(bodies) else None


def _search_from_literal(pattern: re.Pattern, literal: str, text: str, pos: int):
    """``pattern.search(text, pos)`` for a pattern whose every match starts with ``literal``: the regex is
    tried only where ``find`` lands, not at every position (a lookbehind still sees the text before)."""
    at = text.find(literal, pos)
    while at >= 0:
        m = pattern.match(text, at)
        if m:
            return m
        at = text.find(literal, at + 1)
    return None


def _strip_gemma_wrapperless_calls(text: str, enabled_tool_names: Optional[set] = None) -> str:
    """Strips closed bare call:NAME{...} calls; non-promotable, quoted and unanchored ones stay visible; None strips every anchored closed non-execution call."""
    if _whole_content_is_json_value(text):
        return text
    n = len(text)
    out = []
    # Mirror the parse scan: a leading JSON answer's span is data, kept visible.
    cursor = _leading_json_value_end(text) or 0
    if cursor:
        out.append(text[:cursor])
    # Floor advances only past removed calls, so call:a{} call:b{} stays a pair; a blocked prefix in either format anchors, else its peer is promoted and replays.
    floor = blocked_markerless_prefix_end(text, cursor, enabled_tool_names)
    code_spans, code_from = None, 0
    while cursor < n:
        m = _search_from_literal(_GEMMA_BARE_TC_RE, "call", text, cursor)
        if not m:
            out.append(text[cursor:])
            break
        # A blocked call holds its position so the call beside it stays anchored; still stripped.
        blocked = _markerless_blocked_execution(m.group(1), enabled_tool_names)
        promotable = _markerless_promotable(m.group(1), enabled_tool_names)
        quoted = False
        if promotable:
            if code_spans is None:
                code_spans = _tool_healing._code_spans(text, code_from)
            quoted = _tool_healing._in_code(code_spans, m.start())
        parsed_as_call = promotable and not quoted
        keep_as_prose = not parsed_as_call or not _gemma_call_is_anchored(text, m.start(), floor)
        brace = m.end() - 1
        # Same boundary scanner as the parser: strip exactly what it consumed.
        end = _gemma_body_brace_end(text, brace)
        closed = end is not None
        next_index = (end + 1) if closed else len(text)
        if not closed:
            out.append(text[cursor:] if keep_as_prose else text[cursor : m.start()])
            break
        # Advancing only in the blocked branch makes the strip non-idempotent.
        if keep_as_prose:
            out.append(text[cursor:next_index])
            if blocked:
                floor = blocked_markerless_prefix_end(text, next_index, enabled_tool_names)
        else:
            out.append(text[cursor : m.start()])
            floor = blocked_markerless_prefix_end(text, next_index, enabled_tool_names)
        # Rescan past every call the parser promotes, kept unanchored or not, as the parser does.
        if parsed_as_call and _tool_healing._in_code(code_spans, next_index):
            code_spans, code_from = None, next_index
        cursor = next_index
    return "".join(out)


_FUNC_CLOSE_TAG_RE = re.compile(r"</function>")


def _strip_function_xml_calls(text: str, *, final: bool) -> str:
    """Strip ``<function=...>`` calls by mirroring the parser: an opener inside an open
    ``<parameter>`` is data and each call closes at its first ``</function>`` that is not
    parameter data; ``final`` drops a trailing unclosed call."""
    inside = _tool_healing._OpenParameterIndex(text, **_OPEN_PARAMETER_TAGS).inside
    starts = [m for m in _TC_FUNC_START_RE.finditer(text) if not inside(m.start())]
    if not starts:
        return text
    out: list[str] = []
    pos = 0
    for idx, m in enumerate(starts):
        if m.start() < pos:
            continue  # opener already inside a consumed call span
        out.append(text[pos : m.start()])
        next_start = starts[idx + 1].start() if idx + 1 < len(starts) else len(text)
        close = None
        for cm in _FUNC_CLOSE_TAG_RE.finditer(text, m.end(), next_start):
            if not inside(cm.start()):
                close = cm  # first close that is not parameter data = the real close
                break
        if close is not None:
            pos = close.end()
        elif final:
            pos = len(text)  # trailing unclosed call -- drop to EOF
        else:
            out.append(text[m.start() :])  # keep the unclosed call buffered mid-stream
            pos = len(text)
            break
    out.append(text[pos:])
    return "".join(out)


def _glm_value_close(
    text: str,
    vs: int,
    *,
    strict: bool = False,
) -> int:
    """Index of the real </arg_value> at vs with balanced quotes; strict mode refuses the in-quote fallback so truncated args never run. -1 if unclosed."""
    n = len(text)
    search = vs
    first_candidate = -1
    quote = ""
    prev = ":"
    prev_raw = ":"
    qpos = vs  # quote-state cursor; advanced incrementally to each candidate
    while True:
        ve = text.find(_GLM_ARG_VAL_CLOSE, search)
        if ve < 0:
            return -1 if strict else first_candidate
        j = ve + len(_GLM_ARG_VAL_CLOSE)
        while j < n and text[j] in " \t\r\n":
            j += 1
        if j >= n or text.startswith(_GLM_ARG_KEY_OPEN, j) or text.startswith(_GLM_TC_CLOSE, j):
            while qpos < ve:
                ch = text[qpos]
                if quote:
                    if ch == "\\" and qpos + 1 < ve:
                        qpos += 2
                        continue
                    if ch == quote:
                        quote = ""
                elif ch in "\"'" and (prev in ":{[(,=" or (ch == '"' and prev_raw.isspace())):
                    quote = ch
                if not ch.isspace():
                    prev = ch
                prev_raw = ch
                qpos += 1
            if not quote:
                return ve
            if first_candidate < 0:
                first_candidate = ve
        search = ve + len(_GLM_ARG_VAL_CLOSE)


def _strip_glm_calls(text: str, *, final: bool) -> str:
    """Scans to the real </tool_call> so a literal one inside a value is data; Qwen JSON goes to the regex arms; final drops a truncated call, else it buffers."""
    out: list[str] = []
    cursor = 0
    n = len(text)
    while True:
        m = _GLM_TC_OPEN_RE.search(text, cursor)
        if not m:
            break
        apos = m.end()
        close = -1
        while True:
            ks = text.find(_GLM_ARG_KEY_OPEN, apos)
            tc = text.find(_GLM_TC_CLOSE, apos)
            if tc >= 0 and (ks < 0 or tc < ks):
                close = tc
                break
            if ks < 0:
                break  # no close and no more keys -- truncated body
            ke = text.find(_GLM_ARG_KEY_CLOSE, ks + len(_GLM_ARG_KEY_OPEN))
            if ke < 0:
                break
            vstart = ke + len(_GLM_ARG_KEY_CLOSE)
            while vstart < n and text[vstart] in " \t\r\n":
                vstart += 1
            if not text.startswith(_GLM_ARG_VAL_OPEN, vstart):
                apos = ke + len(_GLM_ARG_KEY_CLOSE)
                continue
            vs = vstart + len(_GLM_ARG_VAL_OPEN)
            ve = _glm_value_close(text, vs)
            if ve < 0:
                break  # unclosed <arg_value> -- truncated
            apos = ve + len(_GLM_ARG_VAL_CLOSE)
        if close >= 0:
            out.append(text[cursor : m.start()])
            cursor = close + len(_GLM_TC_CLOSE)
            continue
        if final:
            out.append(text[cursor : m.start()])
            cursor = n
        break
    out.append(text[cursor:])
    return "".join(out)


def _strip_minimax_calls(text: str, *, final: bool) -> str:
    """Strip ``<minimax:tool_call>`` blocks at the close the parser uses; ``final`` drops a trailing unclosed block."""
    if _MINIMAX_BLOCK_OPEN not in text:
        return text
    out: list[str] = []
    pos = 0
    for start, end, closed in _minimax_block_spans(text):
        if not (closed or final):
            break
        out.append(text[pos:start])
        pos = end
    out.append(text[pos:])
    return "".join(out)


def strip_segment(
    segment: str,
    *,
    seg_final: bool,
    enabled_tool_names: Optional[set] = None,
) -> str:
    """Strips markup from one non-think segment; seg_final enables end-of-turn arms. routes/inference.py differs, pinned by tests/test_route_strip_drift.py."""
    seg = _strip_mistral_closed_calls(segment)
    seg = _strip_minimax_calls(seg, final = seg_final)
    # Name-gated: an inactive ``foo[ARGS]{..}`` is prose and is kept.
    seg = _tool_healing._strip_bracket_tag_calls(seg, enabled_tool_names = enabled_tool_names)
    if seg_final:
        # Markerless Gemma ``call:NAME{...}``, name-gated like the parse gate.
        seg = _strip_gemma_wrapperless_calls(seg, enabled_tool_names)
    # Scan, not regex, so a literal ``<function=...>`` inside a value stays data.
    seg = _strip_function_xml_calls(seg, final = seg_final)
    # GLM 4.x: scan to the real </tool_call> so a literal one inside a value is data; Qwen <tool_call>{json} is left to the regex arms.
    seg = _strip_glm_calls(seg, final = seg_final)
    pats = _TOOL_ALL_PATS if seg_final else _TOOL_CLOSED_PATS
    required_pairs = (
        ("</tool_call>", "<tool_call>"),
        ("</function>", "<function"),
        ("<tool_call|>", "<|tool_call>"),
        ("]", "[TOOL_CALLS]"),
        ("}", "[TOOL_CALLS]"),
        ("<|tool▁calls▁end|>", "<|"),
        ("<|tool_calls_section_end|>", "<|tool_calls_section_begin|>"),
        ("<|tool_call_end|>", "<|tool_call_begin|>"),
    )
    for pat_idx, pat in enumerate(pats):
        if pat_idx < len(required_pairs):
            token, opener = required_pairs[pat_idx]
            first_opener = seg.find(opener)
            if first_opener < 0:
                continue
            last_close = seg.rfind(token)
            if last_close < 0:
                continue
            scan_end = last_close + len(token)
            if pat_idx == 3:
                # The Mistral array arm consumes an optional \s*</s> past the ] the bound is taken from; without this the EOS survives in the output.
                eos = scan_end
                while eos < len(seg) and seg[eos].isspace():
                    eos += 1
                if seg.startswith("</s>", eos):
                    scan_end = eos + 4
            if scan_end == len(seg):
                seg = pat.sub("", seg)
            else:
                seg = pat.sub("", seg[:scan_end]) + seg[scan_end:]
        else:
            seg = pat.sub("", seg)
    if seg_final:
        # Trailing partial rehearsal the balanced scan cannot close; gated so prose ``foo[ARGS] ...`` survives.
        seg = _tool_healing.apply_tool_strip_patterns(
            seg,
            [_tool_healing._REHEARSAL_TAIL_STRIP_RE],
            enabled_tool_names = enabled_tool_names,
        )
    return seg


def strip_tool_markup(
    text: str,
    *,
    final: bool = False,
    enabled_tool_names: Optional[set] = None,
) -> str:
    """final=False buffers partial markup; final=True drops unclosed tails and trims. Disabled or example names stay prose; None strips every closed call."""
    if final:
        # Magistral ``[THINK]`` is not the ``<think>`` the reasoning channel renders.
        text = _strip_mistral_reasoning(text)

    def _strip_segment(segment: str, is_last: bool) -> str:
        return strip_segment(
            segment,
            seg_final = final and is_last,
            enabled_tool_names = enabled_tool_names,
        )

    # 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, mentions = True)
    result = _tool_healing.strip_outside_think(masked, _strip_segment)
    if bodies:
        restored = _unmask_blocked_bodies(result, bodies)
        result = (
            restored
            if restored is not None
            else _tool_healing.strip_outside_think(text, _strip_segment)
        )
    return result.strip() if final else result


# Every strip arm is anchored on one of these literals; StreamingMarkupStripper skips the scan
# without them. test_refactor_guard.py fuzzes this, so add new arms' literals here.
_STRIP_SENTINELS = (
    "<tool_call",  # Qwen/Hermes open + GLM, and the <tool_call|> Gemma closer
    "<|tool_call",  # Gemma open, Kimi <|tool_call_begin|> / <|tool_calls_section_begin|>
    "<function",  # Qwen3.5 <function=name> and <function name="...">
    "[TOOL_CALLS]",
    "[ARGS]",  # reasoning-model rehearsal
    "<|python_tag|>",  # Llama-3 built-in tools
    "<|content_invoke_tool_json|>",
    "<minimax:tool_call>",
    "<|",  # DeepSeek's full-width pipe opens every one of its markers
    "<think",  # reasoning segmentation
    "</think",
    "[THINK",
    "[/THINK",
    "call",  # markerless Gemma; ``call\s*:`` tolerates whitespace before the colon
)
# Confirmed against ``_GEMMA_BARE_TC_RE``, not taken at face value: see ``_first_sentinel``
_GEMMA_BARE_SENTINEL = "call"
_SENTINEL_MAX_LEN = max(len(sentinel) for sentinel in _STRIP_SENTINELS)
# Bytes sampled from each end when checking that a buffer continues the previous one.
_EXTENSION_SAMPLE = 64

# Literals that can change the strip of an open ``prefix + <tool_call>{...`` body (see ``_gate_try_arm``).
_OPEN_CALL_OPENER = "<tool_call>"
_OPEN_CALL_TRIGGERS = (
    "<tool_call",
    "</tool_call",
    "<|tool_call",
    "<function",
    "[TOOL_CALLS]",
    "[ARGS]",
    "<minimax:tool_call>",
    "<|",
    "<think",
    "</think",
    "[THINK",
    "[/THINK",
)
_OPEN_CALL_TRIGGER_MAX_LEN = max(len(trigger) for trigger in _OPEN_CALL_TRIGGERS)
_OPEN_CALL_WS_RE = re.compile(r"\s*")
# ``<function=NAME>`` (bare or inside ``<tool_call>``) closes at ``</function>``; other bodies get the same set.
_OPEN_BODY_TRIGGERS = _OPEN_CALL_TRIGGERS + ("</function",)
_OPEN_BODY_TRIGGER_MAX_LEN = max(len(trigger) for trigger in _OPEN_BODY_TRIGGERS)
# The scanner's opener without its trailing ``\s*``, so the body starts at a fixed offset.
_OPEN_FUNCTION_OPENER_RE = re.compile(r'<function(?:=[\w\.\-]+|\s+name="[\w\.\-]+")>')


def _promotable_gemma_call_pos(text: str, start: int, enabled_tool_names) -> int:
    """The sentinel find is exact, not a heuristic: sweeping call-free prose per chunk was quadratic."""
    # Every match starts with the literal: same matches as ``finditer``, without sweeping the buffer per chunk.
    m = _search_from_literal(_GEMMA_BARE_TC_RE, _GEMMA_BARE_SENTINEL, text, start)
    while m is not None:
        if _markerless_promotable(m.group(1), enabled_tool_names):
            return m.start()
        m = _search_from_literal(_GEMMA_BARE_TC_RE, _GEMMA_BARE_SENTINEL, text, m.end())
    return -1


def _first_sentinel(
    text: str,
    start: int,
    enabled_tool_names = None,
) -> int:
    """Lowest index >= start of a strip sentinel, or -1. A bare call counts if its name is removable or still partial, else prose forces a full strip per token."""
    best = -1
    for sentinel in _STRIP_SENTINELS:
        if sentinel == _GEMMA_BARE_SENTINEL:
            continue
        found = text.find(sentinel, start)
        if found >= 0 and (best < 0 or found < best):
            best = found
    at = start
    while True:
        found = text.find(_GEMMA_BARE_SENTINEL, at)
        if found < 0 or (0 <= best <= found):
            return best
        if _GEMMA_BARE_TC_PREFIX_RE.match(text, found):
            return found
        m = _GEMMA_BARE_TC_RE.match(text, found)
        if m is not None and _markerless_promotable(m.group(1), enabled_tool_names):
            return found
        at = found + len(_GEMMA_BARE_SENTINEL)


def _is_provisional_call(text: str, found: int) -> bool:
    """A bare call qualifies only while it ends the buffer; the hit must expire or it pins the strip."""
    return text.startswith(_GEMMA_BARE_SENTINEL, found) and not _GEMMA_BARE_TC_RE.match(text, found)


def _unmatched_think_closer(text: str, start: int = 0) -> int:
    """A stray closer makes strip_outside_think span from offset 0, so nothing may shift that offset."""
    best = -1
    for opener, closer in (("<think>", "</think>"), ("[THINK]", "[/THINK]")):
        close_at = text.find(closer, start)
        if close_at < 0:
            continue
        open_at = text.find(opener, start)
        if 0 <= open_at < close_at:
            continue
        if best < 0 or close_at < best:
            best = close_at
    return best


def _safe_cut(text: str, first: int) -> int:
    """Largest safe cut, snapped to line start for the ^ fence anchor; TOOL_CALLS/ARGS arms need no backtick or tilde before it; rehearsal reaches back to its name."""
    if first <= 0:
        return 0
    if len(text) > _tool_healing._MAX_BRACKET_SCAN_CHARS:
        return 0
    cut = first
    if text.startswith("[ARGS]", first):
        while cut > 0 and not text[cut - 1].isspace():
            cut -= 1
    cut = text.rfind("\n", 0, cut) + 1
    head = text[:cut]
    if "`" in head or "~" in head:
        return 0
    # An unmatched closer makes everything before it reasoning, decided from offset 0; cut only
    # when nothing precedes it.
    close_at = _unmatched_think_closer(text)
    if 0 <= close_at and first < close_at:
        return 0
    # Cutting inside an open <parameter> loses the context that makes a nested <function> opener literal, and the nested markup leaks into the display.
    if "<param" in head and _inside_open_parameter(text, cut):
        return 0
    return cut


class StreamingMarkupStripper:
    """Strip tool markup from an append-only buffer without rescanning it every token.

    The streaming display path used to call the full strip on the entire accumulated response once
    per content token, which is a linear scan with ~10 regex passes per token and therefore
    quadratic in the response length. Two properties make that avoidable, both asserted by
    ``test_refactor_guard.py``: text containing no sentinel from ``_STRIP_SENTINELS`` is returned
    unchanged, so a prose-only response does no regex work at all; and no arm can match before the
    first sentinel, so ``strip(text) == text[:first] + strip(text[first:])``. The scan for that
    first sentinel resumes where the previous call stopped (overlapping by ``_SENTINEL_MAX_LEN - 1``
    so a sentinel split across two appends is still seen), which makes the pre-markup phase
    amortized constant per token rather than linear.

    The instance assumes append-only growth: one instance per buffer, appended to, with ``reset()``
    before a new buffer. A shorter buffer, or one whose sampled head, middle or previous tail no
    longer matches, re-derives the state, so an ordinary rewind is handled. That check samples
    rather than compares in full, because comparing in full would be linear in the buffer and
    reintroduce the cost this class removes, so it is a guard against misuse rather than a guarantee
    for arbitrary call sequences.
    """

    __slots__ = (
        "_enabled_tool_names",
        "_seg_final",
        "_scanned_upto",
        "_first",
        "_floor",
        "_floor_out",
        "_floor_identity",
        "_degenerate",
        "_open_at",
        "_open_scanned",
        "_seen_len",
        "_seen_head",
        "_seen_mid",
        "_seen_tail",
        "_gate_on",
        "_gate_out",
        "_gate_dead",
        "_gate_from",
        "_gate_triggers",
        "_gate_scanned",
        "_gate_call_ws",
    )

    def __init__(
        self,
        enabled_tool_names: Optional[set] = None,
        *,
        seg_final: bool = True,
    ):
        self._enabled_tool_names = enabled_tool_names
        # End-of-turn arms for the last segment: on for tool loops, where partial markup must never render; off for final answer (final=False).
        self._seg_final = seg_final
        self._reset()

    def _reset(self):
        self._scanned_upto = 0
        self._first = -1
        # Settled prefix: text below ``_floor`` is final, and ``_floor_out`` is its output.
        self._floor = 0
        self._floor_out = ""
        # True while nothing has been removed, so the sentinel-free path can return the original string instead of rebuilding it every token.
        self._floor_identity = True
        # Markup at offset 0: nothing can settle, so skip the bookkeeping.
        self._degenerate = False
        self._open_at = -1
        self._open_scanned = 0
        # Length and both end samples of the last buffer, never the buffer itself: see ``_note``
        self._seen_len = -1
        self._seen_head = ""
        self._seen_mid = ""
        self._seen_tail = ""
        self._gate_on = False
        self._gate_out = ""
        # (floor, first) the gate can never arm for; append-only growth cannot undo its reason.
        self._gate_dead = None
        self._gate_from = 0
        self._gate_triggers = _OPEN_CALL_TRIGGERS
        self._gate_scanned = 0
        # Where a ``call`` followed only by whitespace so far resumes its colon check, else -1.
        self._gate_call_ws = -1

    def _gate_clean(self, text: str) -> bool:
        """Scan only text appended since the last call (plus an overlap) for anything that could change the strip."""
        n = len(text)
        triggers = self._gate_triggers
        overlap = (
            _OPEN_CALL_TRIGGER_MAX_LEN
            if triggers is _OPEN_CALL_TRIGGERS
            else _OPEN_BODY_TRIGGER_MAX_LEN
        )
        lo = self._gate_scanned - overlap + 1
        if lo < self._gate_from:
            lo = self._gate_from
        for trigger in triggers:
            if text.find(trigger, lo) >= 0:
                return False
        at = self._gate_call_ws
        if at >= 0:
            at = _OPEN_CALL_WS_RE.match(text, at).end()
            if at < n:
                if text[at] == ":":
                    return False
                at = -1
            self._gate_call_ws = at
        lo = self._gate_scanned - len(_GEMMA_BARE_SENTINEL) + 1
        found = text.find(_GEMMA_BARE_SENTINEL, lo if lo > self._gate_from else self._gate_from)
        while found >= 0:
            at = _OPEN_CALL_WS_RE.match(text, found + len(_GEMMA_BARE_SENTINEL)).end()
            if at == n:
                self._gate_call_ws = at
                break
            if text[at] == ":":
                return False
            found = text.find(_GEMMA_BARE_SENTINEL, found + 1)
        self._gate_scanned = n
        return True

    def _gate_try_arm(self, text: str, seg_start: int, out: str, seg_out: str):
        """Arm on ``text[seg_start:]`` = ``P + O + B``: P without ``<``, ``[``, ``{``, ``call\\s*:``; O ``<tool_call>``
        and/or ``<function=NAME>``; B without a trigger or ``call\\s*:``. Arms reaching past O run to the end, the rest
        need ``<`` or ``[`` in P, so the segment strips to P (``seg_final``) or itself until a trigger arrives."""
        key = (self._floor, self._first)
        if self._gate_dead == key:
            return
        start = self._floor + self._first
        prefix = text[seg_start:start]
        if (
            # ``<param`` in the kept head makes the cut depend on later text (``_inside_open_parameter``).
            text.find("<param", self._floor, seg_start) >= 0
            or "<" in prefix
            or "[" in prefix
            or "{" in prefix
            or _has_gemma_bare_trigger(prefix)
            or len(text) - self._floor > _tool_healing._MAX_BRACKET_SCAN_CHARS
            # Holding the caller's buffer would make each of its appends a copy; only the final strip is cached.
            or (self._seg_final and out is text)
        ):
            self._gate_dead = key
            return
        triggers = _OPEN_BODY_TRIGGERS
        if text.startswith(_OPEN_CALL_OPENER, start):
            body = start + len(_OPEN_CALL_OPENER)
            nxt = _OPEN_CALL_WS_RE.match(text, body).end()
            if nxt == len(text):
                return
            if text[nxt] == "{":
                triggers = _OPEN_CALL_TRIGGERS
            elif text[nxt] == "<":
                fn = _OPEN_FUNCTION_OPENER_RE.match(text, nxt)
                if fn is not None:
                    body = fn.end()
                elif text.find(">", nxt) < 0:
                    # Possibly a ``<function=NAME>`` still arriving.
                    return
        else:
            fn = _OPEN_FUNCTION_OPENER_RE.match(text, start)
            if fn is None:
                rest = len(text) - start
                if text.find(">", start) < 0 and (
                    (rest < len(_OPEN_CALL_OPENER) and _OPEN_CALL_OPENER.startswith(text[start:]))
                    or text.startswith("<function", start)
                ):
                    return
                self._gate_dead = key
                return
            body = fn.end()
        # Fail closed if the full strip just run disagrees with the reasoning above.
        if seg_out != (prefix if self._seg_final else text[seg_start:]):
            self._gate_dead = key
            return
        self._gate_from = body
        self._gate_triggers = triggers
        self._gate_scanned = body
        self._gate_call_ws = -1
        if not self._gate_clean(text):
            self._gate_dead = key
            return
        self._gate_on = True
        self._gate_out = out if self._seg_final else ""

    def _gate_hold(self, text: str):
        """The cached result while the gate holds, else None (and the gate is dropped for this call)."""
        if len(text) - self._floor > _tool_healing._MAX_BRACKET_SCAN_CHARS or not self._gate_clean(
            text
        ):
            self._gate_on = False
            self._gate_out = ""
            self._gate_dead = (self._floor, self._first)
            return None
        if self._seg_final:
            return self._gate_out
        return text if self._floor_identity else self._floor_out + text[self._floor :]

    def _think_block(self, text: str, first: int) -> str:
        """Returns settled, open, or empty (full strip); fires only if the opener is the first sentinel and the body has no other markup, as a closer in args is data."""
        for opener, closer in (("<think>", "</think>"), ("[THINK]", "[/THINK]")):
            if text.startswith(opener, first):
                break
        else:
            return ""
        body_start = first + len(opener)
        # Resume inside the body, overlapping enough that a closer or sentinel split across two appends is still seen
        resume = body_start
        if self._open_at == first and self._open_scanned > body_start:
            resume = max(body_start, self._open_scanned - _SENTINEL_MAX_LEN + 1)
        end = text.find(closer, resume)
        if end < 0:
            # Scan from ``resume``: restarting at the opener each token is quadratic on reasoning bodies.
            if _first_sentinel(text, resume, self._enabled_tool_names) >= 0:
                self._open_at = -1
                return ""
            self._open_at = first
            self._open_scanned = len(text)
            return "open"
        self._open_at = -1
        end += len(closer)
        body = text[self._floor : end]
        if (
            _first_sentinel(
                body.replace(opener, "").replace(closer, ""), 0, self._enabled_tool_names
            )
            >= 0
        ):
            return ""
        settled = self._full_strip(body)
        self._floor_out += settled
        self._floor_identity = self._floor_identity and settled == body
        self._floor = end
        self._first = -1
        self._scanned_upto = end
        return "settled"

    def _needs_whole_buffer(self, text: str) -> bool:
        """Splitting could cut answer text when a Gemma call or stray closer is present, so strip whole."""
        return _promotable_gemma_call_pos(text, self._floor, self._enabled_tool_names) >= 0 or (
            self._floor > 0 and _unmatched_think_closer(text, self._floor) >= 0
        )

    def _full_strip(self, text: str) -> str:
        def _seg(segment: str, is_last: bool) -> str:
            # Streaming has no separate ``final`` pass
            return strip_segment(
                segment,
                seg_final = is_last and self._seg_final,
                enabled_tool_names = self._enabled_tool_names,
            )

        # 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, mentions = True)
        result = _tool_healing.strip_outside_think(masked, _seg)
        if not bodies:
            return result
        restored = _unmask_blocked_bodies(result, bodies)
        return restored if restored is not None else _tool_healing.strip_outside_think(text, _seg)

    def reset(self):
        """Required before a new buffer: _is_extension only samples the ends, so a different buffer
        can pass."""
        self._reset()

    def _note(self, text: str):
        """Keep measurements, not the buffer: a held reference makes CPython copy on every += append."""
        self._seen_len = len(text)
        # A buffer at or under the sample size aliases itself; the forced copy is bounded by the sample size.
        self._seen_head = text[:_EXTENSION_SAMPLE]
        self._seen_tail = text[-_EXTENSION_SAMPLE:]
        mid = len(text) // 2
        self._seen_mid = text[mid : mid + _EXTENSION_SAMPLE]

    def _is_extension(self, text: str) -> bool:
        """Samples head, middle and tail in O(1); a misuse guard, not proof. Call reset() on new buffers."""
        end = self._seen_len
        if end < 0:
            return True
        if len(text) < end:
            return False
        sample = _EXTENSION_SAMPLE
        if text[:sample] != self._seen_head:
            return False
        if text[end - sample : end] != self._seen_tail:
            return False
        mid = end // 2
        return text[mid : mid + sample] == self._seen_mid

    def strip(self, text: str) -> str:
        if not self._is_extension(text):
            self._reset()

        if self._gate_on:
            out = self._gate_hold(text)
            if out is not None:
                self._note(text)
                return out

        if self._degenerate:
            # Guard on ``_floor``: with nothing settled both arms match, so the check would only add two scans.
            if self._floor and not self._needs_whole_buffer(text):
                seg = self._full_strip(text[self._floor :])
                out = self._floor_out + seg
                self._gate_try_arm(text, self._floor, out, seg)
            else:
                out = self._full_strip(text)
                if not self._floor:
                    self._gate_try_arm(text, 0, out, out)
            self._note(text)
            return out

        while True:
            if self._first < 0:
                # Resume from the last scan point, overlapping so a sentinel split across appends is caught; avoids a rescan per token.
                resume = self._scanned_upto - _SENTINEL_MAX_LEN + 1
                found = _first_sentinel(
                    text,
                    resume if resume > self._floor else self._floor,
                    self._enabled_tool_names,
                )
                if found < 0:
                    self._scanned_upto = len(text)
                    return self._unchanged(text)
                if _is_provisional_call(text, found):
                    # A call at buffer end qualifies only because text ends inside it; commit nothing and resume here, or later tokens get whole-buffer checks.
                    self._scanned_upto = found
                    return self._unchanged(text)
                self._first = found - self._floor
            state = self._think_block(text, self._floor + self._first)
            if state == "open":
                return self._unchanged(text)
            if state != "settled":
                break

        tail = text[self._floor :]
        # Markup at offset 0: nothing can settle, so skip the bookkeeping from here on.
        self._degenerate = self._first == 0
        # Not cached: the run before the sentinel may still have been streaming.
        cut = _safe_cut(tail, self._first)
        # Run after the cut, not before: with nothing settled, the whole-buffer strip already is the split.
        if (cut or self._floor) and self._needs_whole_buffer(text):
            out = self._full_strip(text)
            self._note(text)
            return out
        seg = self._full_strip(tail[cut:]) if cut else self._full_strip(tail)
        stripped = tail[:cut] + seg if cut else seg
        out = self._floor_out + stripped if self._floor else stripped
        self._gate_try_arm(text, self._floor + cut, out, seg)
        self._note(text)
        return out

    def _unchanged(self, text: str) -> str:
        """Result when nothing after the settled prefix needs stripping."""
        self._note(text)
        # With nothing removed, return the string passed in, not a rebuilt copy per token; keep no reference, or the caller cannot grow it in place.
        return text if self._floor_identity else self._floor_out + text[self._floor :]


def has_tool_signal(text: str) -> bool:
    return any(s in text for s in TOOL_XML_SIGNALS)


# A Qwen/Hermes envelope whose args carry DeepSeek/Kimi markers must parse as the outer call.
_EMBEDDED_MARKER_RE = re.compile(
    _DEEPSEEK_OPEN_RE_SRC
    + "|"
    + re.escape(_KIMI_SECTION_BEGIN)
    + "|"
    + re.escape(_KIMI_CALL_BEGIN)
    + "|"
    + re.escape(_MINIMAX_BLOCK_OPEN)
)
# The lookahead mirrors the ``_TOOL_ALL_PATS`` python_tag arm so a prose mention is not one.
_OUTER_ENVELOPE_OPEN_RE = re.compile(
    r'<tool_call>|<function(?:=|\s+name=")|<\|tool_call>'
    r"|<\|python_tag\|>(?=\s*(?:\{|[A-Za-z_][\w.]*\())"
)
# Each spans to its real final close so a literal close tag inside a value is data.
_OUTER_ENVELOPE_CLOSED_PATS = (
    re.compile(r"<tool_call>(?:(?!<tool_call>).)*</tool_call>", re.DOTALL),
    _TOOL_CLOSED_PATS[1],
    re.compile(r"<\|tool_call>.*?<tool_call\|>", re.DOTALL),
)


def _marker_inside_leading_envelope(content: str, enabled_tool_names: Optional[set] = None) -> bool:
    first_marker = _EMBEDDED_MARKER_RE.search(content)
    if first_marker is None:
        return False
    # A leading bare-JSON or Mistral call is an outer envelope too: a DeepSeek/Kimi marker in its argument strings is data.
    i = 0
    n = len(content)
    while i < n and content[i] in " \t\n\r":
        i += 1
    if content.startswith("{", i):
        end = _balanced_brace_end(content, i)
        if end is not None and i < first_marker.start():
            name = _top_level_bare_json_name(content[i : end + 1])
            if name is not None and (enabled_tool_names is None or name in enabled_tool_names):
                # A closed leading call owns the turn: markers inside are argument data, markers after are examples.
                return True
            if name is not None and first_marker.start() <= end:
                # Disabled-name leading objects are prose: markers inside stay data; later ones go to the pre-pass.
                return True
    elif content.startswith(_MISTRAL_TRIGGER, i):
        end = _mistral_region_end(content, i)
        if end is not None and i < first_marker.start():
            return True
    # Closed outer call before the first marker owns the turn; pre-pass must not steal examples or data.
    required_pairs = (
        ("</tool_call>", "<tool_call>"),
        ("</function>", "<function"),
        ("<tool_call|>", "<|tool_call>"),
    )
    for _pat, (token, opener) in zip(_OUTER_ENVELOPE_CLOSED_PATS, required_pairs):
        first_opener = content.find(opener)
        if first_opener < 0:
            continue
        last_close = content.rfind(token)
        if last_close < 0:
            continue
        m = _pat.search(content, 0, last_close + len(token))
        if m is not None and m.start() < first_marker.start():
            return True
    residue = content
    for _pat, (token, opener) in zip(_OUTER_ENVELOPE_CLOSED_PATS, required_pairs):
        first_opener = residue.find(opener)
        if first_opener < 0:
            continue
        last_close = residue.rfind(token)
        if last_close < 0:
            continue
        scan_end = last_close + len(token)
        if scan_end == len(residue):
            residue = _pat.sub("", residue)
        else:
            residue = _pat.sub("", residue[:scan_end]) + residue[scan_end:]
    marker = _EMBEDDED_MARKER_RE.search(residue)
    if marker is None:
        return True
    # An unclosed opener before the marker is a truncated outer call holding it as data.
    opener = _OUTER_ENVELOPE_OPEN_RE.search(residue)
    return opener is not None and opener.start() < marker.start()


def _mistral_region_end(
    text: str,
    idx: int,
    *,
    open_envelope_runs_to_eof: bool = False,
) -> int | None:
    """A streaming open envelope counts as reaching EOF; refusing it runs a wrapper quoted inside."""
    n = len(text)
    i = idx + len(_MISTRAL_TRIGGER)
    while i < n and text[i] in " \t\n\r":
        i += 1
    if i < n and text[i] == "[":
        end = _balanced_bracket_end(text, i)
        if end is None:
            return n if open_envelope_runs_to_eof else None
        return end + 1
    if i < n and text[i] == "{":
        end = _balanced_brace_end(text, i)
        if end is None:
            return n if open_envelope_runs_to_eof else None
        return end + 1
    name_match = _MISTRAL_V11_NAME_RE.match(text, i)
    if not name_match:
        return None
    i = name_match.end()
    while i < n and text[i] in " \t\n\r":
        i += 1
    i = _skip_mistral_call_id(text, i)
    if text.startswith(_MISTRAL_ARGS_MARKER, i):
        i += len(_MISTRAL_ARGS_MARKER)
        while i < n and text[i] in " \t\n\r":
            i += 1
    if i >= n or text[i] != "{":
        return None
    end = _balanced_brace_end(text, i)
    return None if end is None else end + 1


def _xml_signal_inside_leading_mistral(content: str) -> bool:
    """True when a parseable Mistral call is the first tool emission in document order: it owns the
    turn, so later XML (quoted in its arguments or in trailing prose) is not promoted over it. A
    signal BEFORE the trigger keeps normal order."""
    trig = content.find(_MISTRAL_TRIGGER)
    if trig < 0:
        return False
    first_xml = _first_foreign_tool_signal(content)
    if first_xml is not None and first_xml < trig:
        return False
    # Only plain prose precedes the trigger: a visible preface must not hand the turn to a later XML literal
    # (preamble-tolerant, like the wrapperless-Gemma guard). Prose that merely mentions the marker has no parseable
    # region and keeps the normal order.
    return _mistral_region_end(content, trig, open_envelope_runs_to_eof = True) is not None


def _parse_bare_rehearsals(
    content: str,
    *,
    id_offset: int = 0,
    enabled_tool_names: Optional[set] = None,
) -> list[dict]:
    """Rehearsal kind only, so Mistral [TOOL_CALLS] is not double-counted; think blocks are skipped."""
    out: list[dict] = []
    think_spans = _tool_healing._think_spans_outside_tool_markup(content)
    for start, end, kind, m in _tool_healing._iter_bracket_spans(
        content, enabled_tool_names = enabled_tool_names
    ):
        if kind != "rehearsal":
            continue
        if any(s <= start < e for s, e in think_spans):
            continue
        try:
            payload = json.loads(content[m.end() : end])
        except (json.JSONDecodeError, ValueError):
            continue
        if not isinstance(payload, dict):
            continue
        out.append(
            {
                "id": f"call_{id_offset + len(out)}",
                "type": "function",
                "function": {"name": m.group(1), "arguments": json.dumps(payload)},
            }
        )
    return out


_ATTR_FUNC_OPEN_RE = re.compile(r'<function\s+name="')


def _first_foreign_tool_signal(content: str) -> int | None:
    """Offset of the first tool signal a non-envelope parser would fire on (XML forms plus
    ``<|python_tag|>``, which also runs before the Mistral parser)."""
    first = None
    for sig in ("<tool_call>", "<|tool_call>", "<function=", "<|python_tag|>"):
        p = content.find(sig)
        if p >= 0 and (first is None or p < first):
            first = p
    attr = _ATTR_FUNC_OPEN_RE.search(content)
    if attr is not None and (first is None or attr.start() < first):
        first = attr.start()
    # A marker inside a leading JSON object routes through the same guard; if disabled, drop the object and parse the tail so a later real call runs.
    marker = _EMBEDDED_MARKER_RE.search(content)
    if marker is not None and (first is None or marker.start() < first):
        first = marker.start()
    return first


def _xml_signal_inside_leading_bare_json(content: str) -> bool:
    """True when the first foreign tool signal is a quoted literal inside a LEADING bare-JSON call
    object or JSON answer -- data, not a real call (sibling of
    ``_xml_signal_inside_leading_mistral``)."""
    i = 0
    n = len(content)
    while i < n and content[i] in " \t\n\r":
        i += 1
    if i >= n or content[i] not in "{[":
        return False
    if content[i] == "[":
        # A leading array is only ever a structured answer; its literals are data.
        end = _balanced_bracket_end(content, i)
        if end is None:
            return False
        try:
            json.loads(content[i : end + 1])
        except ValueError:
            return False
        first_xml = _first_foreign_tool_signal(content)
        trig = content.find(_MISTRAL_TRIGGER)
        if trig >= 0 and (first_xml is None or trig < first_xml):
            first_xml = trig
        return first_xml is not None and i < first_xml < end
    end = _balanced_brace_end(content, i)
    if end is None:
        return False
    if _top_level_bare_json_name(content[i : end + 1]) is None:
        # A nameless object that parses as real JSON is an answer; quoted markup in it is data.
        try:
            json.loads(content[i : end + 1])
        except ValueError:
            return False
    first_xml = _first_foreign_tool_signal(content)
    trig = content.find(_MISTRAL_TRIGGER)
    if trig >= 0 and (first_xml is None or trig < first_xml):
        first_xml = trig
    # In a balanced body the signal is data; after the closed object the leading call owns the turn.
    return first_xml is not None and i < first_xml


def _signal_inside_leading_wrapperless_gemma(
    content: str, enabled_tool_names: Optional[set]
) -> bool:
    """A quoted literal inside a leading promotable Gemma call; non-promotable names are prose."""
    first = _first_foreign_tool_signal(content)
    trig = content.find(_MISTRAL_TRIGGER)
    if trig >= 0 and (first is None or trig < first):
        first = trig
    if first is None:
        return False
    # What matters is a promotable balanced call beginning before the first foreign signal.
    cursor = 0
    while True:
        m = _GEMMA_BARE_TC_RE.search(content, cursor)
        if m is None or m.start() > first:
            return False
        if not _markerless_promotable(m.group(1), enabled_tool_names):
            cursor = m.end()
            continue
        end = _gemma_body_brace_end(content, m.end() - 1)
        if end is None:
            return False
        if m.end() - 1 < first <= end:
            return True
        # A call that closes before the signal still owns the turn; name-agnostic mode keeps inside-only.
        return enabled_tool_names is not None and end < first


def _inside_leading_markerless_body(
    content: str, signal: int, enabled_tool_names: Optional[set]
) -> bool:
    """Inside the body only; a call closed before the signal leaves a real call that must still run."""
    cursor = 0
    while True:
        gem = _GEMMA_BARE_TC_RE.search(content, cursor)
        reh = _tool_healing._REHEARSAL_RE.search(content, cursor)
        m = min((x for x in (gem, reh) if x is not None), key = lambda x: x.start(), default = None)
        if m is None or m.start() > signal:
            return False
        if not _markerless_promotable(m.group(1), enabled_tool_names):
            cursor = m.end()
            continue
        if m is gem:
            body, end = m.end() - 1, _gemma_body_brace_end(content, m.end() - 1)
        else:
            body, end = m.end(), _tool_healing._balanced_json_span(content, m.end())
        return end is not None and body < signal <= end


def _disabled_gemma_call_end_containing_signal(
    content: str, enabled_tool_names: Optional[set]
) -> int | None:
    """End offset (exclusive) of the earliest PROSE (non-promotable) wrapper-less Gemma call whose
    balanced body holds the first foreign signal, else None. A prose name makes the quoted literal
    data, so the caller drops the span and parses the tail; a promotable call defers to the
    enabled-call guard."""
    first = _first_foreign_tool_signal(content)
    trig = content.find(_MISTRAL_TRIGGER)
    if trig >= 0 and (first is None or trig < first):
        first = trig
    if first is None:
        return None
    cursor = 0
    while True:
        m = _GEMMA_BARE_TC_RE.search(content, cursor)
        if m is None or m.start() > first:
            return None
        if _markerless_promotable(m.group(1), enabled_tool_names):
            return None
        end = _gemma_body_brace_end(content, m.end() - 1)
        if end is None:
            cursor = m.end()
            continue
        if m.end() - 1 < first <= end:
            return end + 1
        cursor = end + 1


def parse_tool_calls_from_text(
    content: str,
    *,
    id_offset: int = 0,
    allow_incomplete: bool = True,
    enabled_tool_names: Optional[set] = None,
) -> list[dict]:
    """Return OpenAI-format tool calls, first-match wins so calls are never double-counted.
    ``allow_incomplete=True`` (default) heals truncated calls (missing close tag / unclosed
    parameter); ``False`` accepts only well-formed closed calls (trailing prose tolerated),
    matching llama-server's strict path when Auto-Heal is off. ``enabled_tool_names`` gates only
    the markerless Llama-3.2 bare-JSON form (the marker-based forms carry an explicit signal, so
    a disabled-tool name there is a real call attempt); ``None`` keeps the name-agnostic
    behaviour."""
    # Loop, not recurse (one level per JSONL line hit RecursionError). A skipped run's tail is stripped
    # and masked again before any dispatch, or reasoning after a quoted opener stays executable.
    while True:
        # Drop Magistral [THINK]...[/THINK] BEFORE dispatch: a rehearsed call inside it must never be promoted, and
        # the parse path must agree with the display strip.
        content = _strip_mistral_reasoning(content)

        # Equal-length mask, so the spans this returns still index the caller's text: a blocked
        # call's arguments are quoted prose, and a wrapped call nested there was promoted.
        content, _blocked_bodies = _mask_blocked_bodies(content, enabled_tool_names, think = True)

        # A leading bare-JSON value is decided FIRST: a string argument quoting tool markup (XML or a Mistral trigger)
        # must stay data, so the bare-JSON parser takes the outer call before any other pass. Precedes the Mistral
        # guard, whose preamble tolerance would otherwise claim a trigger quoted inside the leading object.
        skipped = False
        while _xml_signal_inside_leading_bare_json(content):
            calls = _parse_llama3_bare_json(
                content, id_offset = id_offset, enabled_tool_names = enabled_tool_names
            )
            if calls:
                return calls
            i = 0
            while i < len(content) and content[i] in " \t\n\r":
                i += 1
            # The guard guarantees a balanced leading value (object or array).
            end = (_balanced_brace_end if content[i] == "{" else _balanced_bracket_end)(content, i)
            content = content[end + 1 :]
            skipped = True
        if skipped:
            continue

        # A leading enabled wrapper-less Gemma call is decided BEFORE the Mistral guard: its body reads as prose to
        # the preamble tolerance below, so a quoted [TOOL_CALLS] would otherwise steal the turn.
        if _signal_inside_leading_wrapperless_gemma(content, enabled_tool_names):
            calls = _parse_gemma_tool_calls(
                content,
                id_offset = id_offset,
                allow_incomplete = allow_incomplete,
                enabled_tool_names = enabled_tool_names,
            )
            if calls:
                return calls

        # A DISABLED wrapper-less Gemma call is prose: drop the span and parse the tail BEFORE the Mistral guard,
        # whose preamble tolerance would otherwise parse a quoted trigger.
        _prose_end = _disabled_gemma_call_end_containing_signal(content, enabled_tool_names)
        if _prose_end is None:
            break
        content = content[_prose_end:]

    # A leading [TOOL_CALLS] call owns the turn even with a prose preface; XML quoted in its arguments is not promoted over it.
    if _xml_signal_inside_leading_mistral(content):
        calls = _parse_mistral_tool_calls(
            content, id_offset = id_offset, allow_incomplete = allow_incomplete
        )
        if calls:
            # A bare rehearsal after the Mistral call is a peer call; promote it too.
            calls.extend(
                _parse_bare_rehearsals(
                    content,
                    id_offset = id_offset + len(calls),
                    enabled_tool_names = enabled_tool_names,
                )
            )
            return calls

    # DeepSeek/Kimi markers are unique, so try them first unless an outer envelope opens earlier.
    if not _marker_inside_leading_envelope(content, enabled_tool_names):
        # Dispatch by earliest opener so a quoted example inside the other format cannot hijack the turn.
        _ds = _DEEPSEEK_BEGIN_RE.search(content)
        _ds_pos = _ds.start() if _ds else len(content)
        _km_section = content.find(_KIMI_SECTION_BEGIN)
        _km_bare = content.find(_KIMI_CALL_BEGIN)
        _km_pos = min(p for p in (_km_section, _km_bare, len(content)) if p >= 0)
        _mm_pos = content.find(_MINIMAX_BLOCK_OPEN)
        pre_pass = [
            (_ds_pos, _parse_deepseek_tool_calls),
            (_km_pos, _parse_kimi_tool_calls),
            (_mm_pos if _mm_pos >= 0 else len(content), _parse_minimax_tool_calls),
        ]
        pre_pass.sort(key = lambda pair: pair[0])
        for _pos, parser in pre_pass:
            calls = parser(content, id_offset = id_offset, allow_incomplete = allow_incomplete)
            if calls:
                return calls

    # tool_healing does not know the attribute form, so a quoted <tool_call> would beat it.
    attr = _ATTR_FUNC_OPEN_RE.search(content)
    if attr is not None:
        first_other = None
        for sig in (
            "<tool_call>",
            "<|tool_call>",
            "<function=",
            "<|python_tag|>",
            _MISTRAL_TRIGGER,
        ):
            p = content.find(sig)
            if p >= 0 and (first_other is None or p < first_other):
                first_other = p
        if first_other is None or attr.start() < first_other:
            calls = _parse_function_xml(
                content, id_offset = id_offset, allow_incomplete = allow_incomplete
            )
            if calls:
                return calls

    # tool_healing does not know ``<|python_tag|>``, so gate a leading one here.
    py_tag = content.find(_LLAMA3_PYTHON_TAG)
    if py_tag >= 0:
        first_other = None
        for sig in ("<tool_call>", "<|tool_call>", "<function=", _MISTRAL_TRIGGER):
            p = content.find(sig)
            if p >= 0 and (first_other is None or p < first_other):
                first_other = p
        attr = _ATTR_FUNC_OPEN_RE.search(content)
        if attr is not None and (first_other is None or attr.start() < first_other):
            first_other = attr.start()
        if first_other is None or py_tag < first_other:
            calls = _parse_llama3_python_tag(
                content, id_offset = id_offset, allow_incomplete = allow_incomplete
            )
            if calls:
                return calls

    # tool_healing dispatches by opener, so a wrapper quoted in a rehearsal's args would win.
    _reh_signal = _first_foreign_tool_signal(content)
    if _reh_signal is not None and _inside_leading_markerless_body(
        content, _reh_signal, enabled_tool_names
    ):
        calls = _parse_bare_rehearsals(
            content, id_offset = id_offset, enabled_tool_names = enabled_tool_names
        )
        if calls:
            return calls

    # Formats shared with tool_healing (strict/Auto-Heal contract). ``enabled_tool_names`` gates
    # bare rehearsals so an inactive ``foo[ARGS]{..}`` stays prose.
    calls = _tool_healing.parse_tool_calls_from_text(
        content,
        id_offset = id_offset,
        allow_incomplete = allow_incomplete,
        enabled_tool_names = enabled_tool_names,
    )
    if calls:
        return calls

    # Only when tool_healing found nothing, so a strict-rejected call is not re-healed; blank marker spans first, since nested markup in them is data.
    fallback_content = content
    coverage = _tool_healing.marker_coverage(content)
    if coverage:
        chars = list(content)
        for cov_start, cov_end in coverage:
            for i in range(cov_start, min(cov_end, len(chars))):
                chars[i] = " "
        fallback_content = "".join(chars)
    for parser in (
        _parse_glm_tool_calls,
        _parse_function_xml,
        _parse_llama3_python_tag,
        _parse_mistral_tool_calls,
    ):
        calls = parser(fallback_content, id_offset = id_offset, allow_incomplete = allow_incomplete)
        if calls:
            return calls

    # Only a leading call object matches, so ``call:NAME{...}`` in its args stays data.
    calls = _parse_llama3_bare_json(
        fallback_content, id_offset = id_offset, enabled_tool_names = enabled_tool_names
    )
    if calls:
        return calls

    # Gemma wrapper-less ``call:NAME{...}``: markerless, so the same enabled-name gate applies
    calls = _parse_gemma_tool_calls(
        fallback_content,
        id_offset = id_offset,
        allow_incomplete = allow_incomplete,
        enabled_tool_names = enabled_tool_names,
    )
    if calls or not allow_incomplete:
        return calls
    # Last, and only when nothing parsed: a closed call whose one string argument holds unescaped quotes.
    return _parse_closed_call_unescaped_quotes(content, id_offset = id_offset)


# ``{"name": N, "arguments"|"parameters": {"KEY": "VALUE"}}`` with nothing after VALUE but the closers.
_ONE_STRING_ARG_CALL_RE = re.compile(
    r'\A\s*\{\s*"name"\s*:\s*"([\w.\-]+)"\s*,\s*"(?:arguments|parameters)"\s*:\s*'
    r'\{\s*"([A-Za-z_][\w\-]*)"\s*:\s*"(.*)"\s*\}\s*\}\s*\Z',
    re.DOTALL,
)
# A lost second argument or outer key (``", "key":``, ``", 'key':``, ``", key:``, ``"}, "key":``): the
# value's end is no longer knowable, so it is refused.
_SECOND_KEY_RE = re.compile(
    r'"[\s}\]]*,\s*(?:"[A-Za-z_][\w\-]*"|\'[A-Za-z_][\w\-]*\'|[A-Za-z_][\w\-]*)\s*:'
)


def _escape_bare_quotes(raw: str) -> str:
    """Escape the ``"`` no backslash escapes. A model that left bare quotes was not JSON-escaping quotes, so a
    ``\\"`` it wrote next to them is the code's own escape and stays a backslash-quote; other escapes (``\\n``)
    are JSON's. With no bare quote at all, every escape is JSON's."""
    tokens: list[str] = []
    bare = False
    i, n = 0, len(raw)
    while i < n:
        if raw[i] == "\\" and i + 1 < n:
            tokens.append(raw[i : i + 2])
            i += 2
            continue
        bare = bare or raw[i] == '"'
        tokens.append(raw[i])
        i += 1
    if not bare:
        return raw
    return "".join('\\"' if tok == '"' else '\\\\\\"' if tok == '\\"' else tok for tok in tokens)


def _parse_closed_call_unescaped_quotes(content: str, *, id_offset: int) -> list[dict]:
    """A ``<tool_call>`` closed by ``</tool_call>`` whose single string argument (code, a command) holds
    quotes the model did not escape, so strict JSON and brace matching both lose the call. The closer
    bounds the body, so the value is everything between its opening quote and the final ``"}}``. Only a
    single-argument call, outside reasoning and code fences, with no sign of a second argument."""
    out: list[dict] = []
    if _TC_JSON_START_RE.search(content) is None:
        return out
    think_spans = _tool_healing._think_spans_outside_tool_markup(content)
    code_spans = _tool_healing._code_spans(content)
    # Inside another marker's span (an unclosed call before it) the opener is that call's argument data.
    coverage = _tool_healing.marker_coverage(content)
    pos = 0
    while True:
        start = content.find(_OPEN_CALL_OPENER, pos)
        if start < 0:
            return out
        body_start = start + len(_OPEN_CALL_OPENER)
        end = content.find("</tool_call>", body_start)
        if end < 0:
            return out
        pos = end + len("</tool_call>")
        if (
            any(a <= start < b for a, b in think_spans)
            or any(a < start < b for a, b in coverage)
            or _tool_healing._in_code(code_spans, start)
        ):
            continue
        body = content[body_start:end]
        if _OPEN_CALL_OPENER in body:
            continue
        m = _ONE_STRING_ARG_CALL_RE.match(body)
        if m is None or _SECOND_KEY_RE.search(m.group(3)):
            continue
        try:
            value = json.loads('"' + _escape_bare_quotes(m.group(3)) + '"', strict = False)
        except (json.JSONDecodeError, ValueError):
            continue
        out.append(
            {
                "id": f"call_{id_offset + len(out)}",
                "type": "function",
                "function": {"name": m.group(1), "arguments": json.dumps({m.group(2): value})},
            }
        )


def _loads_call_object(text: str, *, relaxed: bool = True):
    """A wrapped call's JSON object: strict first; near-JSON a model sampled (single quotes, a trailing comma,
    True/None, unquoted keys) only when strict fails and healing is on. None when neither reads an object."""
    try:
        return json.loads(text)
    except (json.JSONDecodeError, ValueError):
        return relaxed_json_object(text) if relaxed else None


def _call_object_end(
    text: str,
    start: int,
    relaxed: bool = True,
) -> "tuple[int | None, bool]":
    """The call object's closing brace, and whether near-JSON may still be read. A relaxed read matches
    single-quoted strings too, so a brace in one never ends the call and scanning never resumes inside its
    argument; an object that never closes that way gets JSON brace matching, strict for the rest of the text."""
    if relaxed and relaxed_allowed_at(text, start):
        end = relaxed_object_end(text, start)
        if end is not None:
            return end, True
    return _balanced_brace_end(text, start), False


def _parse_tool_call_json(
    content: str,
    *,
    id_offset: int,
    allow_incomplete: bool = True,
) -> list[dict]:
    out: list[dict] = []
    relaxed = allow_incomplete
    for m in _TC_JSON_START_RE.finditer(content):
        brace_start = m.end() - 1
        end, relaxed = _call_object_end(content, brace_start, relaxed)
        if end is None:
            continue
        # Strict: a body that never closed ``<tool_call>`` is truncated; trailing prose is tolerated.
        if not allow_incomplete and not content[end + 1 :].lstrip().startswith("</tool_call>"):
            continue
        obj = _loads_call_object(content[brace_start : end + 1], relaxed = relaxed)
        if not isinstance(obj, dict):
            continue
        name = obj.get("name", "")
        args = obj.get("arguments")
        if args is None:
            args = obj.get("parameters", {})
        if isinstance(args, dict):
            args_str = json.dumps(args)
        elif isinstance(args, str):
            args_str = args
        else:
            args_str = json.dumps({"value": args})
        if not name:
            continue
        out.append(
            {
                "id": f"call_{id_offset + len(out)}",
                "type": "function",
                "function": {"name": name, "arguments": args_str},
            }
        )
    return out


# Identical to the healer's copy; imported so the two cannot drift.
_trim_param_value = _tool_healing._trim_param_value
_json_object_body = _tool_healing._json_object_body


# Shared by the helper and ``_tool_healing._OpenParameterIndex`` so the two cannot drift.
_OPEN_PARAMETER_TAGS = {
    "param_start_re": _TC_PARAM_START_RE,
    "param_closers": ("</parameter>", "</param>"),
    "func_closers": ("</function>", "</tool_call>"),
}


def _inside_open_parameter(text: str, pos: int) -> bool:
    """An opener inside an unclosed parameter block is a literal in an argument value, not a nested call."""
    return _tool_healing._inside_open_parameter(text, pos, **_OPEN_PARAMETER_TAGS)


def _parse_function_xml(
    content: str,
    *,
    id_offset: int,
    allow_incomplete: bool = True,
) -> list[dict]:
    out: list[dict] = []
    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()) 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)
        body_start = fm.end()
        next_func = func_starts[idx + 1].start() if idx + 1 < len(func_starts) else len(content)
        # The first close not inside an open parameter ends the call (mirrors _strip_function_xml_calls).
        close_match = None
        for cm in _TC_END_TAG_RE.finditer(content, body_start, next_func):
            if not inside(cm.start()):
                close_match = cm
                break
        has_close = close_match is not None
        if has_close:
            body_end = close_match.start()
        else:
            body_end = min(len(content), next_func)
        # Strict mode: an unclosed function call is truncated -- do not heal it.
        if not allow_incomplete and not has_close:
            continue
        body = content[body_start:body_end]
        if body.rstrip().endswith("</function>"):
            body = _TC_FUNC_CLOSE_RE.sub("", body)

        args: dict = {}
        param_unclosed = False
        # A ``<parameter>`` opener inside an open parameter value is literal text.
        body_inside = _tool_healing._OpenParameterIndex(body, **_OPEN_PARAMETER_TAGS).inside
        param_starts = [
            pm for pm in _TC_PARAM_START_RE.finditer(body) if not body_inside(pm.start())
        ]
        if len(param_starts) == 1:
            pm = param_starts[0]
            raw_val = body[pm.end() :]
            param_closed = raw_val.rstrip().endswith(("</parameter>", "</param>"))
            if not param_closed:
                param_unclosed = True
            val = _TC_PARAM_CLOSE_RE.sub("", raw_val) if param_closed else raw_val
            args[pm.group(1) or pm.group(2)] = _trim_param_value(val)
        else:
            for pidx, pm in enumerate(param_starts):
                val_start = pm.end()
                next_param = (
                    param_starts[pidx + 1].start() if pidx + 1 < len(param_starts) else len(body)
                )
                raw_val = body[val_start:next_param]
                param_closed = raw_val.rstrip().endswith(("</parameter>", "</param>"))
                if not param_closed:
                    param_unclosed = True
                val = _TC_PARAM_CLOSE_RE.sub("", raw_val) if param_closed else raw_val
                args[pm.group(1) or pm.group(2)] = _trim_param_value(val)

        # Strict mode: a dangling parameter means the call was cut off; a closed zero-parameter call stays valid.
        if not allow_incomplete and param_unclosed:
            continue
        if allow_incomplete and not param_starts:
            args = _json_object_body(body) or args

        out.append(
            {
                "id": f"call_{id_offset + len(out)}",
                "type": "function",
                "function": {"name": func_name, "arguments": json.dumps(args)},
            }
        )
    return out


def _llama3_kv_value(body: str, p: int, n: int) -> tuple[Any, int | None]:
    """One ``.call`` value (string/number/true/false/null) at ``body[p:]``. Returns ``(value,
    consumed_len)`` or ``(None, None)`` if none matches."""
    if p >= n:
        return None, None
    if body[p] == '"':
        # ``"((?:\\.|[^"\\])*)"`` by hand so an unterminated quote is O(n), not O(n^2).
        j = p + 1
        while j < n:
            c = body[j]
            if c == "\\":
                # ``\\.`` needs a following non-newline char; else the body can't match.
                if j + 1 >= n or body[j + 1] == "\n":
                    return None, None
                j += 2
                continue
            if c == '"':
                raw = body[p + 1 : j]
                # json.loads keeps \n/\uXXXX escapes and literal UTF-8 (emoji/CJK) intact.
                try:
                    return json.loads('"' + raw + '"'), j + 1 - p
                except (json.JSONDecodeError, ValueError):
                    return raw, j + 1 - p
            j += 1
        return None, None  # unterminated
    nm = _LLAMA3_NUM_RE.match(body, p)
    if nm:
        v = nm.group(0)
        # Exponent forms decode as float too (1e-3 was read as 1).
        return (float(v) if any(c in v for c in ".eE") else int(v)), nm.end() - p
    lm = _LLAMA3_LIT_RE.match(body, p)
    if lm:
        return {"true": True, "false": False, "null": None}[lm.group(0)], lm.end() - p
    return None, None


def _parse_llama3_kv_args(body: str) -> dict[str, Any]:
    """``k=v, ...`` kwargs from a ``.call(...)`` body, left to right (later keys win). Linear
    hand-scan replacing the quadratic ``_LLAMA3_KV_RE.finditer`` walk."""
    args: dict[str, Any] = {}
    n = len(body)
    i = 0
    while i < n:
        km = _LLAMA3_KEY_RE.match(body, i)
        if km is None:
            i += 1
            continue
        p = _LLAMA3_WS_RE.match(body, km.end()).end()
        if p >= n or body[p] != "=":
            i = km.end()
            continue
        p = _LLAMA3_WS_RE.match(body, p + 1).end()
        val, length = _llama3_kv_value(body, p, n)
        if length is None:
            i = km.end()
            continue
        args[km.group(0)] = val
        i = p + length
    return args


def _parse_llama3_python_tag(
    content: str,
    *,
    id_offset: int,
    allow_incomplete: bool = True,
) -> list[dict]:
    """Parse the Llama-3 emissions: ``<|python_tag|>NAME.call(...)`` (built-in),
    ``<|python_tag|>{"name":..., "parameters":...}`` (custom), multi-call via ``; ``,
    ``parameters`` or ``arguments`` key."""
    out: list[dict] = []
    if _LLAMA3_PYTHON_TAG not in content:
        return out

    # 1. ``NAME.call(...)`` anchored to the tag so a quoted one inside JSON args is not a call.
    pos = content.find(_LLAMA3_PYTHON_TAG)
    truncated = False
    while pos >= 0 and not truncated:
        head = _LLAMA3_PY_CALL_HEAD_RE.match(content, pos + len(_LLAMA3_PYTHON_TAG))
        if head is None:
            break
        name = head.group(1)
        open_idx = head.end()
        i = open_idx
        while True:
            i = open_idx
            depth = 1
            in_string = False
            esc = False
            while i < len(content) and depth > 0:
                ch = content[i]
                if in_string:
                    if esc:
                        esc = False
                    elif ch == "\\":
                        esc = True
                    elif ch == '"':
                        in_string = False
                else:
                    if ch == '"':
                        in_string = True
                    elif ch == "(":
                        depth += 1
                    elif ch == ")":
                        depth -= 1
                        if depth == 0:
                            break
                i += 1
            # Truncated ``.call(...)`` with no closing paren: reject in strict mode instead of executing a partial.
            if not allow_incomplete and depth > 0:
                truncated = True
                break
            body = content[open_idx:i]
            out.append(
                {
                    "id": f"call_{id_offset + len(out)}",
                    "type": "function",
                    "function": {
                        "name": name,
                        "arguments": json.dumps(_parse_llama3_kv_args(body)),
                    },
                }
            )
            # ``)`` then optional ``; NAME.call(`` chains the next built-in call.
            chain = _LLAMA3_CALL_CHAIN_RE.match(content, i + 1)
            if chain is None:
                break
            name = chain.group(1)
            open_idx = chain.end()
        # Past the consumed region: a second ``<|python_tag|>`` may carry more calls.
        pos = content.find(_LLAMA3_PYTHON_TAG, i + 1)

    # 2. ``<|python_tag|>{json}``; raw_decode peels ``;``-separated objects.
    if not out:
        decoder = json.JSONDecoder()
        # Relaxed spans already tried never overlap, so every char is brace-matched at most once (no O(n^2)).
        relaxed_from = 0 if allow_incomplete else len(content)
        idx = content.find(_LLAMA3_PYTHON_TAG)
        while idx >= 0:
            search_from = idx + len(_LLAMA3_PYTHON_TAG)
            cursor = search_from
            while cursor < len(content):
                brace = content.find("{", cursor)
                if brace < 0:
                    break
                # Stop at the next ``<|python_tag|>``.
                next_tag = content.find(_LLAMA3_PYTHON_TAG, search_from, brace)
                if next_tag >= 0:
                    break
                try:
                    obj, end_offset = decoder.raw_decode(content[brace:])
                except (json.JSONDecodeError, ValueError):
                    obj = None
                    if brace >= relaxed_from and relaxed_allowed_at(content, brace):
                        close = relaxed_object_end(content, brace)
                        relaxed_from = len(content) if close is None else close + 1
                        if close is not None:
                            obj = relaxed_json_object(content[brace : close + 1])
                    if obj is None:
                        cursor = brace + 1
                        continue
                    end_offset = close + 1 - brace
                if not isinstance(obj, dict):
                    cursor = brace + end_offset
                    continue
                name = obj.get("name") or obj.get("function") or ""
                args = obj.get("parameters") if "parameters" in obj else obj.get("arguments", {})
                if isinstance(args, dict):
                    args_str = json.dumps(args)
                elif isinstance(args, str):
                    args_str = args
                else:
                    cursor = brace + end_offset
                    continue
                if name:
                    out.append(
                        {
                            "id": f"call_{id_offset + len(out)}",
                            "type": "function",
                            "function": {"name": name, "arguments": args_str},
                        }
                    )
                cursor = brace + end_offset
            idx = content.find(_LLAMA3_PYTHON_TAG, cursor)
    return out


# Llama-3 sentinels (chainable, any order) plus the template's role label.
_LLAMA3_BARE_JSON_SENTINELS = (
    "<|begin_of_text|>",
    "<|eot_id|>",
    "<|start_header_id|>",
    "<|end_header_id|>",
    "<|eom_id|>",
)
_LLAMA3_HEADER_ROLES = ("assistant", "user", "system", "tool", "ipython")


def strip_llama3_leading_sentinels(content: str) -> str:
    """Strip leading Llama-3 sentinels that can leak from a prior turn before a bare-JSON call; parser and streaming guards share it."""
    stripped = content.lstrip()
    while True:
        stripped = stripped.lstrip()
        matched = False
        for sentinel in _LLAMA3_BARE_JSON_SENTINELS:
            if stripped.startswith(sentinel):
                stripped = stripped[len(sentinel) :]
                if sentinel == "<|start_header_id|>":
                    for role in _LLAMA3_HEADER_ROLES:
                        if stripped.startswith(role):
                            stripped = stripped[len(role) :]
                            break
                matched = True
                break
        if not matched:
            return stripped


def _parse_llama3_bare_json(
    content: str,
    *,
    id_offset: int,
    allow_incomplete: bool = True,
    enabled_tool_names: Optional[set] = None,
) -> list[dict]:
    """Llama-3.2 ``custom_tools`` bare ``{"name":.., "parameters":{..}}`` (no ``<|python_tag|>``),
    strict so prose/echoes do not fire. ``enabled_tool_names`` gates on the parsed name so an
    ordinary JSON answer is not misread as a call to a disabled tool; ``None`` is name-agnostic."""
    out: list[dict] = []
    stripped = strip_llama3_leading_sentinels(content)
    if not stripped.startswith("{"):
        return out

    decoder = json.JSONDecoder()
    cursor = 0
    n = len(stripped)
    while cursor < n:
        # Skip whitespace and the Llama-3 ``;`` inter-call separator.
        while cursor < n and stripped[cursor] in " \t\n\r;":
            cursor += 1
        if cursor >= n or stripped[cursor] != "{":
            break
        try:
            obj, end_offset = decoder.raw_decode(stripped[cursor:])
        except (json.JSONDecodeError, ValueError):
            break
        if not isinstance(obj, dict):
            break
        name = obj.get("name") or obj.get("function") or ""
        if not isinstance(name, str) or not name:
            break
        blocked = _markerless_blocked_execution(name, enabled_tool_names)
        if not blocked and not _markerless_promotable(name, enabled_tool_names):
            # An unlisted name means a JSON answer: stop rather than promote nested objects.
            break
        # parameters must be a dict, else a looser check fires on a string like parameters sentence; arguments may be a dict or JSON string (OpenAI).
        if "parameters" in obj:
            args = obj.get("parameters")
            if not isinstance(args, dict):
                break
            args_str = json.dumps(args)
        elif "arguments" in obj:
            args = obj.get("arguments")
            if isinstance(args, dict):
                args_str = json.dumps(args)
            elif isinstance(args, str):
                try:
                    parsed = json.loads(args)
                except (json.JSONDecodeError, ValueError):
                    break
                if not isinstance(parsed, dict):
                    break
                args_str = args
            else:
                break
        else:
            break
        if blocked:
            # Call-shaped but blocked: keep decoding or a benign call behind it is dropped; the shape check runs first, so name-plus-result objects stay data.
            cursor += end_offset
            continue
        out.append(
            {
                "id": f"call_{id_offset + len(out)}",
                "type": "function",
                "function": {"name": name, "arguments": args_str},
            }
        )
        cursor += end_offset
    return out


def _parse_mistral_tool_calls(
    content: str,
    *,
    id_offset: int,
    allow_incomplete: bool = True,
) -> list[dict]:
    """Parse all Mistral emissions: pre-v11 ``[TOOL_CALLS][...]`` / ``[TOOL_CALLS]{...}`` and v11+
    ``[TOOL_CALLS]name{json}`` / ``[TOOL_CALLS]name[ARGS]{json}``."""
    out: list[dict] = []
    content = _strip_mistral_reasoning(content)
    idx = content.find(_MISTRAL_TRIGGER)
    if idx < 0:
        return out

    # Disambiguate the first occurrence: array / single object (pre-v11), or bare-name (v11+)
    j = idx + len(_MISTRAL_TRIGGER)
    k = j
    while k < len(content) and content[k] in " \t\n\r":
        k += 1
    if k >= len(content):
        return out

    if content[k] == "[":
        return _parse_mistral_array(content, k, id_offset, allow_incomplete = allow_incomplete)

    relaxed = allow_incomplete
    if content[k] == "{":
        # Pre-v11 single ``{"name":...}``; fall through without a ``name`` so v11+ still runs.
        end, relaxed = _call_object_end(content, k, relaxed)
        if end is not None:
            try:
                obj = _loads_call_object(content[k : end + 1], relaxed = relaxed)
                if isinstance(obj, dict) and obj.get("name"):
                    _consume_mistral_call(content[k : end + 1], out, id_offset, relaxed = relaxed)
                    return out
            except (json.JSONDecodeError, ValueError):
                pass

    # v11+: walk every ``[TOOL_CALLS]``, parsing ``name{json}`` or ``name[ARGS]{json}`` after each trigger
    pos = idx
    while pos >= 0:
        cur = pos + len(_MISTRAL_TRIGGER)
        nm = _MISTRAL_V11_NAME_RE.match(content, cur)
        if not nm:
            pos = content.find(_MISTRAL_TRIGGER, cur)
            continue
        name = nm.group(1)
        after_name = nm.end()
        after_name = _skip_mistral_call_id(content, after_name)
        if content.startswith(_MISTRAL_ARGS_MARKER, after_name):
            after_name += len(_MISTRAL_ARGS_MARKER)
        while after_name < len(content) and content[after_name] in " \t\n\r":
            after_name += 1
        if after_name >= len(content) or content[after_name] != "{":
            pos = content.find(_MISTRAL_TRIGGER, cur)
            continue
        end, relaxed = _call_object_end(content, after_name, relaxed)
        if end is None:
            break
        args = _loads_call_object(content[after_name : end + 1], relaxed = relaxed)
        if not isinstance(args, dict):
            pos = content.find(_MISTRAL_TRIGGER, end + 1)
            continue
        out.append(
            {
                "id": f"call_{id_offset + len(out)}",
                "type": "function",
                "function": {
                    "name": name,
                    "arguments": json.dumps(args),
                },
            }
        )
        pos = content.find(_MISTRAL_TRIGGER, end + 1)
    return out


def _parse_mistral_array(
    content: str,
    start: int,
    id_offset: int,
    allow_incomplete: bool = True,
) -> list[dict]:
    """Pre-v11 ``[TOOL_CALLS] [{...}, ...]`` array form."""
    out: list[dict] = []
    j = start
    depth = 0
    in_string = False
    esc = False
    while j < len(content):
        ch = content[j]
        if in_string:
            if esc:
                esc = False
            elif ch == "\\":
                esc = True
            elif ch == '"':
                in_string = False
        else:
            if ch == '"':
                in_string = True
            elif ch == "[":
                depth += 1
            elif ch == "]":
                depth -= 1
                if depth == 0:
                    break
        j += 1
    # An unclosed array is a truncated call; strict mode rejects it rather than salvage objects by hand.
    if not allow_incomplete and depth != 0:
        return out
    body = content[start : j + 1] if depth == 0 else content[start:]

    try:
        arr = json.loads(body)
        if isinstance(arr, list):
            for obj in arr:
                if isinstance(obj, dict):
                    _consume_mistral_call(json.dumps(obj), out, id_offset)
        return out
    except (json.JSONDecodeError, ValueError):
        if not allow_incomplete:
            return out

    # Advance past each balanced object instead of rescanning from every ``{`` (ReDoS).
    pos = 0
    blen = len(body)
    relaxed = allow_incomplete
    while pos < blen:
        brace = body.find("{", pos)
        if brace < 0:
            break
        end, relaxed = _call_object_end(body, brace, relaxed)
        if end is None:
            break  # truncated mid-object: nothing after it can balance
        _consume_mistral_call(body[brace : end + 1], out, id_offset, relaxed = relaxed)
        pos = end + 1
    return out


def _consume_mistral_call(
    obj_text: str,
    out: list[dict],
    id_offset: int,
    *,
    relaxed: bool = True,
) -> None:
    obj = _loads_call_object(obj_text, relaxed = relaxed)
    if not isinstance(obj, dict):
        return
    name = obj.get("name") or ""
    args = obj.get("arguments")
    if args is None:
        args = obj.get("parameters", {})
    if isinstance(args, dict):
        args_str = json.dumps(args)
    elif isinstance(args, str):
        args_str = args
    else:
        args_str = json.dumps({"value": args})
    if name:
        out.append(
            {
                "id": obj.get("id") or f"call_{id_offset + len(out)}",
                "type": "function",
                "function": {"name": name, "arguments": args_str},
            }
        )


def _whole_content_is_json_value(text: str) -> bool:
    """A JSON-only answer is data: a tool example quoted in it must neither run nor be stripped."""
    t = text.strip()
    if t[:1] not in "{[":
        return False
    try:
        json.loads(t)
    except ValueError:
        return False
    return True


def _leading_json_value_end(text: str) -> int | None:
    """Leading JSON answer span, possibly followed by prose; a mid-prose JSON blob is not one."""
    i = 0
    n = len(text)
    while i < n and text[i].isspace():
        i += 1
    if i >= n or text[i] not in "{[":
        return None
    end = (_balanced_brace_end if text[i] == "{" else _balanced_bracket_end)(text, i)
    if end is None:
        return None
    try:
        json.loads(text[i : end + 1])
    except ValueError:
        return None
    return end + 1


def _parse_gemma_tool_calls(
    content: str,
    *,
    id_offset: int,
    allow_incomplete: bool = True,
    enabled_tool_names: Optional[set] = None,
) -> list[dict]:
    """Bare call:NAME is gated by enabled_tool_names; execution and MCP names never promote bare, since a bare call may be attacker-quoted prose."""
    out: list[dict] = []
    # Defer only a real wrapped opener to tool_healing; a bare marker mention would lose the call.
    _wrapped = _GEMMA_TC_RE.search(content)
    # Not inside a leading promotable call's body: that is quoted argument text.
    if _wrapped is not None and not _inside_leading_markerless_body(
        content, _wrapped.start(), enabled_tool_names
    ):
        return out
    # A whole-content JSON value is a structured answer: quoted examples must not become calls
    if _whole_content_is_json_value(content):
        return out
    # Resume after each consumed body so a nested ``call:OTHER{...}`` is never re-matched.
    cursor = _leading_json_value_end(content) or 0
    # A blocked rehearsal's body is argument text too. One forward pass; restarting is quadratic.
    blocked_spans = []
    _reh_cursor = 0
    _reh_last_close = content.rfind("}")
    while _reh_last_close >= 0:
        _reh = _tool_healing._REHEARSAL_RE.search(content, _reh_cursor)
        if _reh is None or _reh.end() > _reh_last_close:
            break
        _reh_cursor = _reh.end()
        if not _markerless_blocked_execution(_reh.group(1), enabled_tool_names):
            continue
        _reh_end = _tool_healing._balanced_json_span(content, _reh.end())
        if _reh_end is None:
            # This body runs past the last closer, so no later opener can balance either.
            break
        blocked_spans.append((_reh.start(), _reh_end + 1))
        _reh_cursor = _reh_end + 1
    # Monotonic index into the sorted spans: re-testing every span per match is quadratic.
    blocked_i = 0
    code_spans, code_from = None, 0
    while True:
        m = _GEMMA_BARE_TC_RE.search(content, cursor)
        if m is None:
            break
        while blocked_i < len(blocked_spans) and blocked_spans[blocked_i][1] <= m.start():
            blocked_i += 1
        if blocked_i < len(blocked_spans) and blocked_spans[blocked_i][0] <= m.start():
            cursor = m.end()
            continue
        name = m.group(1)
        body_start = m.end() - 1
        end = _gemma_body_brace_end(content, body_start)
        if end is None:
            # Unclosed call: scanning on would promote quoted argument text.
            break
        cursor = end + 1
        if not _markerless_promotable(name, enabled_tool_names):
            continue
        if code_spans is None:
            code_spans = _tool_healing._code_spans(content, code_from)
        if _tool_healing._in_code(code_spans, m.start()):
            continue
        # Its arguments are data: a code span they open must not hide the sibling call behind it.
        if _tool_healing._in_code(code_spans, cursor):
            code_spans, code_from = None, cursor
        body = content[body_start + 1 : end]
        try:
            args = _gemma_bare_body_arguments(body)
        except Exception:
            args = {}
        out.append(
            {
                "id": f"call_{id_offset + len(out)}",
                "type": "function",
                "function": {"name": name, "arguments": json.dumps(args)},
            }
        )
    return out


# Shared with the healer; its Gemma quoting sits behind a flag this module never sets.
_balanced_brace_end = _tool_healing._balanced_brace_end


# Streaming rescans one growing body per token: saved walks resume at their old end.
_GEMMA_BODY_SCAN_SLOTS = 8
_GEMMA_BODY_SCAN_MIN = 256
_gemma_body_scans: list = [None] * _GEMMA_BODY_SCAN_SLOTS
_gemma_body_scan_next = 0


def _gemma_body_brace_end(text: str, brace_pos: int) -> int | None:
    """Closing brace of a Gemma body; quotes hide braces, but a single quote opens only at value start, so apostrophes in prose do not."""
    global _gemma_body_scan_next
    if brace_pos >= len(text) or text[brace_pos] != "{":
        return None
    n = len(text)
    depth = 0
    quote = ""
    prev = ""
    prev_raw = ""
    i = brace_pos
    hit = -1
    for k, entry in enumerate(tuple(_gemma_body_scans)):
        if (
            entry is not None
            and entry[1] == brace_pos
            and entry[2] > i
            and len(entry[0]) <= n
            and text.startswith(entry[0])
        ):
            if entry[3] is not None:
                return entry[3]
            i, depth, quote, prev, prev_raw = entry[2], entry[4], entry[5], entry[6], entry[7]
            hit = k
            break
    start = i
    end = None
    while i < n:
        ch = text[i]
        if quote:
            if ch == "\\":
                if i + 1 < n:
                    i += 2
                    continue
                # A trailing backslash escapes whatever arrives next: stop before it.
                break
            if ch == quote:
                quote = ""
        elif ch in "\"'" and (prev in ":{[(,=" or (ch == '"' and prev_raw.isspace())):
            quote = ch
        elif ch == "{":
            depth += 1
        elif ch == "}":
            depth -= 1
            if depth == 0:
                end = i
                break
        if not ch.isspace():
            prev = ch
        prev_raw = ch
        i += 1
    if i > start and (hit >= 0 or i - start >= _GEMMA_BODY_SCAN_MIN):
        prefix = text if end is None and i == n else text[: (end + 1 if end is not None else i)]
        slot = hit
        if slot < 0:
            slot = _gemma_body_scan_next % _GEMMA_BODY_SCAN_SLOTS
            _gemma_body_scan_next = slot + 1
        _gemma_body_scans[slot] = (prefix, brace_pos, i, end, depth, quote, prev, prev_raw)
    return end


_BARE_JSON_NAME_RE = re.compile(r'"name"\s*:\s*"([^"]+)"')


def _top_level_bare_json_name(probe: str) -> Optional[str]:
    """Top-level name of a bare-JSON object, skipping nested ones; truncated tail yields the name so far."""
    if not probe.startswith("{"):
        return None
    decoder = json.JSONDecoder()
    function_value = None  # the ``"function"`` alias, used only if no ``"name"`` key
    name_value = None  # last top-level ``"name"``, which is the one json.loads keeps
    i = 1
    n = len(probe)
    while i < n:
        while i < n and probe[i] in " \t\r\n,":
            i += 1
        if i >= n or probe[i] == "}":
            # End of the object: the last ``"name"`` wins, else a recorded ``"function"`` alias
            return name_value or function_value
        if probe[i] != '"':
            return name_value or function_value
        try:
            key, consumed = decoder.raw_decode(probe[i:])
        except (json.JSONDecodeError, ValueError):
            return name_value or function_value
        if not isinstance(key, str):
            return name_value or function_value
        i += consumed
        while i < n and probe[i] in " \t\r\n":
            i += 1
        if i >= n or probe[i] != ":":
            return name_value or function_value
        i += 1
        while i < n and probe[i] in " \t\r\n":
            i += 1
        if key == "name" and i < n:
            try:
                value, consumed = decoder.raw_decode(probe[i:])
            except (json.JSONDecodeError, ValueError):
                return name_value or function_value
            # Recorded, not returned: json.loads keeps the LAST duplicate, and a falsey last name still
            # overwrites, matching ``obj.get("name") or obj.get("function")``.
            name_value = value if isinstance(value, str) else ""
            i += consumed
            continue
        if key == "function" and i < n:
            # Like ``name``, the last duplicate ``function`` is the one json.loads keeps.
            try:
                value, consumed = decoder.raw_decode(probe[i:])
            except (json.JSONDecodeError, ValueError):
                return name_value or function_value
            function_value = value if isinstance(value, str) else ""
            i += consumed
            continue
        # A truncated value cannot prove a further name exists; report what was found.
        if i < n and probe[i] == "{":
            end = _balanced_brace_end(probe, i)
            if end is None:
                return name_value or function_value
            i = end + 1
        elif i < n and probe[i] == "[":
            end = _balanced_bracket_end(probe, i)
            if end is None:
                return name_value or function_value
            i = end + 1
        else:
            try:
                _value, consumed = decoder.raw_decode(probe[i:])
            except (json.JSONDecodeError, ValueError):
                # Resync instead of giving up: junk before the name key hid a blocked call.
                nxt = _next_top_level_comma(probe, i, n)
                if nxt is None:
                    return name_value or function_value
                i = nxt + 1
                continue
            i += consumed
    return name_value or function_value


def _next_top_level_comma(text: str, start: int, end: int) -> "int | None":
    """Lets the name scan step over one malformed field; commas inside strings or containers are skipped."""
    depth = 0
    i = start
    while i < end:
        ch = text[i]
        if ch == '"':
            i += 1
            while i < end and text[i] != '"':
                i += 2 if text[i] == "\\" else 1
        elif ch in "{[":
            depth += 1
        elif ch in "}]":
            if depth == 0:
                return None
            depth -= 1
        elif ch == "," and depth == 0:
            return i
        i += 1
    return None


def strip_leading_bare_json_call(text: str, enabled_tool_names: Optional[set] = None) -> str:
    """Consume the whole ;-chain, or a leftover executed call is replayed beside tool_calls."""
    remainder = text
    stripped_any = False
    # The blocked call-shaped objects the parser skipped, in order, so parse and strip agree.
    kept: list[str] = []

    def _out(tail: str) -> str:
        if not (stripped_any or kept):
            return text
        return ("".join(kept) + tail).lstrip()

    while True:
        probe = strip_llama3_leading_sentinels(remainder.lstrip())
        # Skip the Llama-3 ``;`` inter-call separator between chained calls.
        if stripped_any or kept:
            probe = probe.lstrip(" \t\n\r;")
        # Separator before ``probe`` shows only if its object is kept; stripping only trims a prefix.
        sep = remainder[: len(remainder) - len(probe)] if kept else ""
        if not (probe.startswith("{") and ('"name"' in probe or '"function"' in probe)):
            return _out(sep + probe)
        # Top-level name only: a nested ``"name"`` is data.
        name = _top_level_bare_json_name(probe)
        blocked = _markerless_blocked_execution(name, enabled_tool_names)
        if not blocked and not _markerless_promotable(name, enabled_tool_names):
            return _out(sep + probe)
        end = _balanced_brace_end(probe, 0)
        if end is None:
            return _out(sep + probe) if blocked else ""
        # A closed object must have the call shape, or it is content and stays visible.
        try:
            obj = json.loads(probe[: end + 1])
        except (json.JSONDecodeError, ValueError):
            return _out(sep + probe)
        if not _bare_json_call_shaped(obj):
            return _out(sep + probe)
        if blocked:
            kept.append(sep + probe[: end + 1])  # visible as text; the scan goes on
        else:
            stripped_any = True
        remainder = probe[end + 1 :]


def _bare_json_call_shaped(obj) -> bool:
    """The shape gate ``_parse_llama3_bare_json`` applies to a decoded object."""
    if not isinstance(obj, dict):
        return False
    # The parser needs a TOP-LEVEL name; a nested one (e.g. in a "result" value) is data, never stripped.
    name = obj.get("name") or obj.get("function") or ""
    if not isinstance(name, str) or not name:
        return False
    if "parameters" in obj:
        return isinstance(obj.get("parameters"), dict)
    args = obj.get("arguments")
    if isinstance(args, dict):
        return True
    if isinstance(args, str):
        try:
            return isinstance(json.loads(args), dict)
        except (json.JSONDecodeError, ValueError):
            return False
    return False


def blocked_bare_json_chain_may_continue(text: str, enabled_tool_names: Optional[set]) -> bool:
    """Holds only while a ;-peer can still follow; holding longer withholds the reply for nothing, and a cancel before EOS loses it."""
    probe = strip_llama3_leading_sentinels(text.lstrip())
    if not probe.startswith("{"):
        return False
    # Without any ``}`` the leading object cannot have closed; avoids a quadratic rewalk. Counting
    # braces is wrong: a closed object followed by an open peer is unbalanced too.
    if "}" not in probe:
        return False
    end = _balanced_brace_end(probe, 0)
    if end is None:
        return False
    try:
        obj = json.loads(probe[: end + 1])
    except (json.JSONDecodeError, ValueError):
        return False
    if not isinstance(obj, dict):
        return False
    name = obj.get("name") or obj.get("function") or ""
    if not _markerless_blocked_execution(name, enabled_tool_names):
        return False
    if not _bare_json_call_shaped(obj):
        return False
    suffix = probe[end + 1 :].lstrip(" \t\n\r;")
    while True:
        if not suffix:
            return True  # more may still arrive
        if not suffix.startswith("{"):
            # The parser scans the whole turn, so a Gemma peer behind the blocked object must not stream.
            return gemma_tail_may_hide_a_call(suffix, enabled_tool_names)
        peer_end = _balanced_brace_end(suffix, 0)
        if peer_end is None:
            return True  # still arriving; it may yet close as a call
        try:
            peer = json.loads(suffix[: peer_end + 1])
        except (json.JSONDecodeError, ValueError):
            return False
        if not isinstance(peer, dict) or not _bare_json_call_shaped(peer):
            return False
        peer_name = peer.get("name") or peer.get("function") or ""
        if _markerless_promotable(peer_name, enabled_tool_names):
            return True  # this peer WILL be promoted; it must not stream first
        if not _markerless_blocked_execution(peer_name, enabled_tool_names):
            return False  # a disabled name stops the chain scan
        suffix = suffix[peer_end + 1 :].lstrip(" \t\n\r;")


def promotable_gemma_call_pos(
    text: str,
    enabled_tool_names,
    start: int = 0,
    *,
    floor: int = 0,
    streaming: bool = False,
) -> int:
    """Bare Gemma has no TOOL_XML_SIGNALS entry, so streaming must find it; start is only a resume hint."""
    # Widen, do not seek: an rfind window could miss a ``call`` straddling the boundary.
    start = text.find(_GEMMA_BARE_SENTINEL, max(0, start - _MAX_GEMMA_PREFIX_TAIL))
    if start < 0:
        return -1
    names = enabled_tool_names() if callable(enabled_tool_names) else enabled_tool_names
    code_spans = None
    for m in _GEMMA_BARE_TC_RE.finditer(text, start):
        # Skip and keep scanning, never return early: a re-found call below the floor would read as no call, hiding a real call after it.
        if m.start() < floor or not _markerless_promotable(m.group(1), names):
            continue
        if code_spans is None:
            code_spans = _tool_healing._code_spans(text)
        if _tool_healing._in_code(code_spans, m.start()):
            continue
        if streaming and "\n" not in text[m.start() :] and _open_inline_code_run(text, m.start()):
            continue
        return m.start()
    return -1


_BACKTICK_RUN_RE = re.compile(r"`+")


def _open_inline_code_run(text: str, pos: int) -> bool:
    """Whether a backtick run earlier on ``pos``'s line is still waiting for its equal-length closer."""
    open_len = 0
    for run in _BACKTICK_RUN_RE.finditer(text, text.rfind("\n", 0, pos) + 1, pos):
        n = len(run.group())
        if not open_len:
            open_len = n
        elif n == open_len:
            open_len = 0
    return open_len > 0


# ``call`` plus separators and any name worth holding; the candidate can only sit at the end.
_MAX_GEMMA_PREFIX_TAIL = 256


def _last_bare_call_word(text: str) -> int:
    """Offset of the last ``call`` that starts a word, or -1 (so ``recall`` is skipped)."""
    idx = text.rfind("call")
    while idx > 0 and (text[idx - 1].isalnum() or text[idx - 1] == "_"):
        idx = text.rfind("call", 0, idx)
    return idx


def _partial_call_word_len(text: str) -> int:
    """Length of a trailing ``c``/``ca``/``cal`` that could still become ``call``: a chunk
    can end mid-word (the leading-position buffer covers these via ``"call:".startswith``)."""
    for n in (3, 2, 1):
        start = len(text) - n
        if start >= 0 and text[start:] == "call"[:n]:
            if start == 0 or not (text[start - 1].isalnum() or text[start - 1] == "_"):
                return n
    return 0


def gemma_tail_may_hide_a_call(tail: str, enabled_tool_names: Optional[set]) -> bool:
    """The parser searches the whole turn, so a blocked leading call may hide a Gemma peer."""
    for nxt in _GEMMA_BARE_TC_RE.finditer(tail):
        if _markerless_promotable(nxt.group(1), enabled_tool_names):
            return True
    # A name still being typed at the very end could become one.
    trailing = tail.rstrip()
    idx = _last_bare_call_word(trailing)
    if idx >= 0:
        rest = trailing[idx:]
        if "call:".startswith(rest) or _GEMMA_BARE_TC_PREFIX_RE.match(rest) is not None:
            return True
    if _partial_call_word_len(trailing):
        return True
    return not tail  # an empty tail may still grow a peer


def held_bare_gemma_tail_len(text: str, enabled_tool_names: Optional[set]) -> int:
    """Streaming holds a partial call:NAME tail until prose or a closed call releases it."""
    # ``pos`` not a slice, so ``(?<!\w)`` still sees the preceding char.
    partial = _GEMMA_BARE_TC_PREFIX_RE.search(text, max(0, len(text) - _MAX_GEMMA_PREFIX_TAIL))
    if partial is not None:
        return len(text) - partial.start()
    # Only an unclosed brace can still be an open body.
    if text.rfind("{") > text.rfind("}"):
        idx = _last_bare_call_word(text)
        m = _GEMMA_BARE_TC_RE.match(text, idx) if idx >= 0 else None
        if m is not None and _gemma_body_brace_end(text, m.end() - 1) is None:
            names = enabled_tool_names() if callable(enabled_tool_names) else enabled_tool_names
            if _markerless_promotable(m.group(1), names):
                return len(text) - idx
    return _partial_call_word_len(text)


def blocked_gemma_chain_may_continue(text: str, enabled_tool_names: Optional[set]) -> bool:
    """The tail is searched, not matched: a Gemma peer behind a ; or a sentence still counts."""
    probe = text.lstrip()
    m = _GEMMA_BARE_TC_RE.match(probe)
    if m is None or not _markerless_blocked_execution(m.group(1), enabled_tool_names):
        return False
    # Without a ``}`` after the opener the body cannot have closed; avoids a quadratic rewalk per
    # snapshot. Counting braces is wrong: a closed body plus an open peer is unbalanced too.
    if probe.find("}", m.end() - 1) < 0:
        return True
    end = _gemma_body_brace_end(probe, m.end() - 1)
    if end is None:
        return True  # body still arriving
    # Separators count as an empty tail, else the peer after them streams before promotion.
    return gemma_tail_may_hide_a_call(probe[end + 1 :].lstrip(" \t\n\r;"), enabled_tool_names)


def _gemma_balanced_brace_end(text: str, brace_pos: int, hard_stop: int) -> int | None:
    """Like ``_balanced_brace_end`` but skips ``<|"|>`` strings and matches {}/[] symmetrically."""
    if brace_pos >= len(text) or text[brace_pos] != "{":
        return None
    depth = 0
    i = brace_pos
    while i < hard_stop:
        if text.startswith(_GEMMA_STR_BEGIN, i):
            close = text.find(_GEMMA_STR_END, i + len(_GEMMA_STR_BEGIN))
            if close < 0:
                return None
            i = close + len(_GEMMA_STR_END)
            continue
        ch = text[i]
        if ch == "{" or ch == "[":
            depth += 1
        elif ch == "}" or ch == "]":
            depth -= 1
            if depth == 0:
                return i
        i += 1
    return None


def _gemma_parse_value(
    text: str,
    i: int,
    *,
    in_mapping: bool = False,
):
    """Returns (value, next, closed); closed is False when input ends early, so the caller uses raw text."""
    if text.startswith(_GEMMA_STR_BEGIN, i):
        close = text.find(_GEMMA_STR_END, i + len(_GEMMA_STR_BEGIN))
        if close < 0:
            return text[i + len(_GEMMA_STR_BEGIN) :], len(text), False
        return text[i + len(_GEMMA_STR_BEGIN) : close], close + len(_GEMMA_STR_END), True
    if text[i] == "{":
        return _gemma_parse_mapping(text, i)
    if text[i] == "[":
        return _gemma_parse_array(text, i)
    if text[i] in "\"'":
        # Raw-quoted string: delimiters inside are data (``{city:"New, York"}``).
        quote = text[i]
        j = i + 1
        n = len(text)
        while j < n:
            if text[j] == "\\" and j + 1 < n:
                j += 2
                continue
            if text[j] == quote:
                return text[i + 1 : j], j + 1, True
            j += 1
        return text[i + 1 :], n, False
    # Primitives follow the top-level delimiter rules: bracket depth and quote openers hide commas.
    end = i
    n = len(text)
    depth = 0
    quote = ""
    prev = ":"
    prev_raw = ":"
    while end < n and not text.startswith(_GEMMA_STR_BEGIN, end):
        ch = text[end]
        if quote:
            if ch == "\\" and end + 1 < n:
                end += 2
                continue
            if ch == quote:
                quote = ""
        elif ch in "\"'" and (prev in ":{[(,=" or (ch == '"' and prev_raw.isspace())):
            quote = ch
        elif ch in "{[(":
            depth += 1
        elif ch in "}])":
            if depth == 0:
                break
            depth -= 1
        elif ch == "," and depth == 0:
            if not in_mapping or _GEMMA_KEY_RE.match(text, end + 1):
                break
        if not ch.isspace():
            prev = ch
        prev_raw = ch
        end += 1
    if end == i:
        # Consume one char so callers always advance on malformed input.
        return "", i + 1, True
    return _gemma_coerce_scalar(text[i:end]), end, True


def _gemma_parse_array(text: str, start: int):
    """Parse a Gemma ``[...]`` array at ``text[start] == '['`` in one forward pass; returns ``(list,
    next_index, closed)``."""
    items: list[Any] = []
    i, n = start + 1, len(text)
    while i < n:
        while i < n and text[i] in " \t\n\r,":
            i += 1
        if i < n and text[i] == "]":
            return items, i + 1, True
        if i >= n:
            break
        v, i, _closed = _gemma_parse_value(text, i)
        items.append(v)
    return items, i, False


def _gemma_coerce_scalar(raw: str) -> Any:
    """Coerce an unquoted Gemma value to bool/int/float/None, else keep str (quotes stripped first
    so quoted/unquoted variants compare identical). Numbers are JSON number literals only."""
    raw = raw.strip()
    if not raw:
        return raw
    first = raw[0]
    if first in "-0123456789":
        # isascii(): int() also accepts non-ASCII digits.
        digits = raw[1:] if first == "-" else raw
        if digits.isdigit() and digits.isascii() and (digits[0] != "0" or len(digits) == 1):
            try:
                return int(raw)
            except ValueError:
                # Past sys.get_int_max_str_digits(); json.loads refuses it too.
                return raw
        if _GEMMA_JSON_NUMBER_RE.fullmatch(raw) is None:
            return raw
        value = float(raw)
        # "1e999" -> inf, which json.dumps writes as invalid bare Infinity.
        return raw if abs(value) == float("inf") else value
    if len(raw) >= 2 and first == raw[-1] and first in "\"'":
        return raw[1:-1]
    if raw == "true":
        return True
    if raw == "false":
        return False
    if raw == "null":
        return None
    return raw


def _gemma_strip_quoted_leaves(value: Any) -> Any:
    """Recursively unquote quoted string leaves of a nested stripped-stream value, so nested
    ``city:"New York"`` matches the top-level coercion (no stray quotes)."""
    if isinstance(value, str):
        v = value.strip()
        if len(v) >= 2 and v[0] == v[-1] and v[0] in "\"'":
            return v[1:-1]
        return value
    if isinstance(value, dict):
        return {k: _gemma_strip_quoted_leaves(v) for k, v in value.items()}
    if isinstance(value, list):
        return [_gemma_strip_quoted_leaves(v) for v in value]
    return value


_GEMMA_SLOT = "\ue000"
_GEMMA_SLOT_RE = re.compile(_GEMMA_SLOT + r"(\d+)" + _GEMMA_SLOT)


def _slot_gemma_values(body: str) -> "tuple[str, list[str]]":
    """``body`` with each ``<|"|>`` value that opens a value outside an ordinary quoted string replaced by a
    placeholder, and the raw values in order. Quotes open where the quote-less parser opens them."""
    out: list[str] = []
    raw: list[str] = []
    quote = ""
    prev = ":"
    i, n = 0, len(body)
    while i < n:
        ch = body[i]
        if quote:
            if ch == "\\" and i + 1 < n:
                out.append(body[i : i + 2])
                i += 2
                continue
            if ch == quote:
                quote = ""
        elif prev in ":{[," and body.startswith(_GEMMA_STR_END, i):
            end = body.find(_GEMMA_STR_END, i + len(_GEMMA_STR_END))
            if end < 0:
                out.append(body[i:])
                break
            raw.append(body[i + len(_GEMMA_STR_END) : end])
            out.append(f'"{_GEMMA_SLOT}{len(raw) - 1}{_GEMMA_SLOT}"')
            i = end + len(_GEMMA_STR_END)
            prev = '"'
            continue
        elif ch in "\"'" and prev in ":{[(,=":
            quote = ch
        out.append(ch)
        if not ch.isspace():
            prev = ch
        i += 1
    return "".join(out), raw


def _gemma_bare_body_arguments(body: str) -> dict[str, Any]:
    """Each ``<|"|>``-quoted value is read raw, every other value as the quote-less parser reads it (single
    quotes, nesting). A delimiter that does not end up a whole value (a literal one inside another quoted
    value) keeps the quote-less reading of the whole body."""
    if _GEMMA_QUOTED_VALUE_RE.search(body) is None or _GEMMA_SLOT in body:
        return _gemma_parse_stripped_body(body)
    slotted, raw = _slot_gemma_values(body)
    used: list[int] = []

    def fill(v: Any) -> Any:
        if isinstance(v, dict):
            return {k: fill(x) for k, x in v.items()}
        if isinstance(v, list):
            return [fill(x) for x in v]
        if isinstance(v, str) and _GEMMA_SLOT in v:
            m = _GEMMA_SLOT_RE.fullmatch(v)
            if m is None:
                raise ValueError("delimiter inside a value")
            used.append(int(m.group(1)))
            return raw[used[-1]]
        return v

    try:
        args = fill(_gemma_parse_stripped_body(slotted))
    except ValueError:
        args = None
    if args is None or sorted(used) != list(range(len(raw))):
        return _gemma_parse_stripped_body(body)
    return args


def _gemma_parse_stripped_body(body: str) -> dict[str, Any]:
    """Quote-less body: values end at the next top-level ', key:' boundary, so commas in code survive."""
    out: dict[str, Any] = {}
    i, n = 0, len(body)
    while i < n:
        m = _GEMMA_KEY_RE.match(body, i)
        if not m:
            break
        key = m.group(1)
        i = m.end()
        vstart = i
        depth = 0
        quote = ""
        prev = ":"
        prev_raw = ":"
        while i < n:
            ch = body[i]
            if quote:
                if ch == "\\" and i + 1 < n:
                    i += 2
                    continue
                if ch == quote:
                    quote = ""
            elif ch in "\"'" and (prev in ":{[(,=" or (ch == '"' and prev_raw.isspace())):
                quote = ch
            elif ch in "{[(":
                depth += 1
            elif ch in "}])":
                if depth > 0:
                    depth -= 1
            elif ch == "," and depth == 0 and _GEMMA_KEY_RE.match(body, i + 1):
                break
            if not ch.isspace():
                prev = ch
            prev_raw = ch
            i += 1
        raw_val = body[vstart:i].strip()
        if raw_val[:1] in "{[":
            # Only a fully consumed, closed parse; otherwise keep the raw string.
            parsed, end, closed = _gemma_parse_value(raw_val, 0)
            out[key] = (
                _gemma_strip_quoted_leaves(parsed)
                if (closed and end == len(raw_val))
                else _gemma_coerce_scalar(raw_val)
            )
        else:
            out[key] = _gemma_coerce_scalar(raw_val)
        if i < n and body[i] == ",":
            i += 1
    return out


def _gemma_parse_mapping(text: str, start: int):
    """Parse a Gemma ``{key:value, ...}`` mapping at ``text[start] == '{'`` in one forward pass;
    returns ``(dict, next_index, closed)`` (``closed`` True iff the matching ``}`` was reached)."""
    out: dict[str, Any] = {}
    i, n = start + 1, len(text)
    while i < n:
        while i < n and text[i] in " \t\n\r,":
            i += 1
        if i < n and text[i] == "}":
            return out, i + 1, True
        if i >= n:
            break
        if text.startswith(_GEMMA_STR_BEGIN, i):
            close = text.find(_GEMMA_STR_END, i + len(_GEMMA_STR_BEGIN))
            if close < 0:
                break
            key = text[i + len(_GEMMA_STR_BEGIN) : close]
            i = close + len(_GEMMA_STR_END)
        else:
            kstart = i
            while i < n and text[i] not in ":}":
                i += 1
            key = text[kstart:i].strip()
        while i < n and text[i] in " \t\n\r":
            i += 1
        if i < n and text[i] == ":":
            i += 1
        while i < n and text[i] in " \t\n\r":
            i += 1
        if i >= n:
            out[key] = None
            break
        if text[i] == "}":
            out[key] = None
            return out, i + 1, True
        v, i, _closed = _gemma_parse_value(text, i, in_mapping = True)
        out[key] = v
    return out, i, False


def _find_outside_json_strings(text: str, needle: str, start: int) -> int:
    """Index of ``needle`` at/after ``start`` OUTSIDE any JSON string, or -1: a marker inside an
    argument string must not be taken as the structural terminator."""
    i = start
    n = len(text)
    in_string = False
    esc = False
    while i < n:
        ch = text[i]
        if in_string:
            if esc:
                esc = False
            elif ch == "\\":
                esc = True
            elif ch == '"':
                in_string = False
            i += 1
            continue
        if ch == '"':
            in_string = True
            i += 1
            continue
        if text.startswith(needle, i):
            return i
        i += 1
    return -1


def _parse_deepseek_tool_calls(
    content: str,
    *,
    id_offset: int,
    allow_incomplete: bool = True,
) -> list[dict]:
    """R1 wraps arguments in a json fence, V3.x does not; the five opener variants follow llama.cpp."""
    begin = _DEEPSEEK_BEGIN_RE.search(content)
    if not begin:
        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, end_pos
    scan_end = end_pos if end_pos >= 0 else len(content)
    body = content[scan_start:scan_end]
    relaxed = allow_incomplete

    pos = 0
    while pos < len(body):
        fpos = body.find(_DEEPSEEK_R1_FUNC_MARKER, pos)
        if fpos < 0:
            break
        name_start = fpos + len(_DEEPSEEK_R1_FUNC_MARKER)
        nl = body.find("\n", name_start)
        if nl < 0:
            break
        if not body.startswith(_DEEPSEEK_R1_FENCE, nl):
            pos = name_start
            continue
        name = body[name_start:nl].strip()
        json_start = nl + len(_DEEPSEEK_R1_FENCE)
        if json_start >= len(body) or body[json_start] != "{":
            pos = json_start
            continue
        brace_end, relaxed = _call_object_end(body, json_start, relaxed)
        if brace_end is None:
            break
        args = _loads_call_object(body[json_start : brace_end + 1], relaxed = relaxed)
        if not isinstance(args, dict):
            pos = brace_end + 1
            continue
        # The close must immediately follow the JSON, else the search lands on a later call's terminator.
        after = brace_end + 1
        while after < len(body) and body[after] in " \t\r\n":
            after += 1
        close_m = _DEEPSEEK_R1_CLOSE_RE.match(body, after)
        if not allow_incomplete and close_m is None:
            pos = brace_end + 1
            continue
        if name:
            out.append(
                {
                    "id": f"call_{id_offset + len(out)}",
                    "type": "function",
                    "function": {
                        "name": name,
                        "arguments": json.dumps(args),
                    },
                }
            )
        pos = close_m.end() if close_m else brace_end + 1
    if out:
        return out, end_pos

    pos = 0
    while pos < len(body):
        sep_pos = body.find(_DEEPSEEK_SEP, pos)
        if sep_pos < 0:
            break
        # Walk left to the name start; stop at newline, `<` or `>`.
        name_start = sep_pos
        while name_start > pos and body[name_start - 1] not in "\n<>":
            name_start -= 1
        name = body[name_start:sep_pos].strip()
        json_start = sep_pos + len(_DEEPSEEK_SEP)
        while json_start < len(body) and body[json_start] in " \t\n\r":
            json_start += 1
        if json_start >= len(body) or body[json_start] != "{":
            pos = sep_pos + len(_DEEPSEEK_SEP)
            continue
        brace_end, relaxed = _call_object_end(body, json_start, relaxed)
        if brace_end is None:
            break
        if not allow_incomplete:
            after = brace_end + 1
            while after < len(body) and body[after] in " \t\r\n":
                after += 1
            if not body.startswith(_DEEPSEEK_CALL_END, after):
                pos = brace_end + 1
                continue
        args = _loads_call_object(body[json_start : brace_end + 1], relaxed = relaxed)
        if not isinstance(args, dict):
            pos = brace_end + 1
            continue
        if name:
            out.append(
                {
                    "id": f"call_{id_offset + len(out)}",
                    "type": "function",
                    "function": {
                        "name": name,
                        "arguments": json.dumps(args),
                    },
                }
            )
        # Seeking the end marker could land on a later call's and skip the one between.
        pos = brace_end + 1
    return out, end_pos


def _parse_glm_tool_calls(
    content: str,
    *,
    id_offset: int,
    allow_incomplete: bool = True,
    raw_values: bool = False,
) -> list[dict]:
    """GLM 4.x: back-to-back <tool_call> blocks, no envelope, each with arg_key/arg_value pairs.
    ``raw_values`` leaves every value a string, for a caller that types them by the tool's schema."""
    out: list[dict] = []
    pos = 0
    while pos < len(content):
        m = _GLM_TC_OPEN_RE.search(content, pos)
        if not m:
            break
        name = m.group(1).strip()
        apos = m.end()  # absolute position in ``content``; advances past each pair

        args: dict[str, Any] = {}
        valid = True
        close = -1
        # A value may contain a literal </tool_call>; the real close precedes the next <arg_key>.
        while True:
            ks = content.find(_GLM_ARG_KEY_OPEN, apos)
            tc = content.find(_GLM_TC_CLOSE, apos)
            if tc >= 0 and (ks < 0 or tc < ks):
                close = tc
                break
            if ks < 0:
                break  # no close and no more keys -- truncated body
            ke = content.find(_GLM_ARG_KEY_CLOSE, ks + len(_GLM_ARG_KEY_OPEN))
            if ke < 0:
                break
            vstart = ke + len(_GLM_ARG_KEY_CLOSE)
            while vstart < len(content) and content[vstart] in " \t\r\n":
                vstart += 1
            if not content.startswith(_GLM_ARG_VAL_OPEN, vstart):
                if not allow_incomplete:
                    valid = False
                apos = ke + len(_GLM_ARG_KEY_CLOSE)
                continue
            vs = vstart + len(_GLM_ARG_VAL_OPEN)
            # A first-match find would truncate values containing literal close tags.
            ve = _glm_value_close(content, vs, strict = not allow_incomplete)
            key = content[ks + len(_GLM_ARG_KEY_OPEN) : ke].strip()
            if ve < 0:
                # Strict rejects; Auto-Heal keeps the partial value (a truncated query is not a no-arg call).
                if not allow_incomplete:
                    valid = False
                    break
                # Bound the healed value at the next structural tag, not EOF, so a missing close tag cannot swallow the markup after it.
                nk = content.find(_GLM_ARG_KEY_OPEN, vs)
                tc = content.find(_GLM_TC_CLOSE, vs)
                bounds = [b for b in (nk, tc) if b >= 0]
                if not bounds:
                    args[key] = content[vs:].rstrip()
                    break
                bound = min(bounds)
                args[key] = content[vs:bound].rstrip()
                apos = bound
                continue
            raw_val = content[vs:ve]
            apos = ve + len(_GLM_ARG_VAL_CLOSE)
            # Keep the value raw so string whitespace survives (matches vLLM glm4_moe).
            probe = raw_val.strip()
            if not raw_values and (
                probe[:1] in "{["
                or probe in ("true", "false", "null")
                or _GLM_JSON_NUMERIC_RE.fullmatch(probe)
            ):
                try:
                    args[key] = json.loads(probe)
                    continue
                except (json.JSONDecodeError, ValueError):
                    pass
            args[key] = raw_val

        # Strict mode: a block with no </tool_call> is truncated; reject it.
        if not allow_incomplete and close < 0:
            valid = False

        if name and valid:
            out.append(
                {
                    "id": f"call_{id_offset + len(out)}",
                    "type": "function",
                    "function": {
                        "name": name,
                        "arguments": json.dumps(args),
                    },
                }
            )
        pos = close + len(_GLM_TC_CLOSE) if close >= 0 else len(content)
    return out


def _parse_kimi_tool_calls(
    content: str,
    *,
    id_offset: int,
    allow_incomplete: bool = True,
) -> list[dict]:
    """The full functions.NAME:IDX id is kept on tool_calls[i].id so the chat template round-trips it."""
    out: list[dict] = []
    outer_pos = 0
    while True:
        section_start = content.find(_KIMI_SECTION_BEGIN, outer_pos)
        if section_start < 0:
            break
        scan_start = section_start + len(_KIMI_SECTION_BEGIN)
        # Search outside JSON strings: an argument may contain the literal end token.
        section_end = _find_outside_json_strings(content, _KIMI_SECTION_END, scan_start)
        scan_end = section_end if section_end >= 0 else len(content)
        body = content[scan_start:scan_end]
        # Truncated tail: parse it; strict mode rejects a section lacking <|tool_calls_section_end|>.
        if section_end < 0:
            if allow_incomplete:
                out.extend(
                    _parse_kimi_section_body(
                        body, id_offset = id_offset + len(out), allow_incomplete = True
                    )
                )
            return out
        outer_pos = section_end + len(_KIMI_SECTION_END)
        out.extend(
            _parse_kimi_section_body(
                body, id_offset = id_offset + len(out), allow_incomplete = allow_incomplete
            )
        )

    # The section wrapper is optional: when the loop matched nothing, a bare <|tool_call_begin|> call parses as one section.
    if not out and _KIMI_CALL_BEGIN in content:
        out.extend(
            _parse_kimi_section_body(
                content, id_offset = id_offset, allow_incomplete = allow_incomplete
            )
        )
    return out


def _parse_kimi_section_body(
    body: str,
    *,
    id_offset: int,
    allow_incomplete: bool = True,
) -> list[dict]:
    """Parse one Kimi K2 section body (between begin / end markers)."""
    out: list[dict] = []
    pos = 0
    relaxed = allow_incomplete
    while pos < len(body):
        call_start = body.find(_KIMI_CALL_BEGIN, pos)
        if call_start < 0:
            break
        id_start = call_start + len(_KIMI_CALL_BEGIN)
        arg_begin = body.find(_KIMI_ARG_BEGIN, id_start)
        if arg_begin < 0:
            break
        full_id = body[id_start:arg_begin].strip()
        m = _KIMI_ID_RE.match(full_id)
        if m:
            name = m.group(1)
        else:
            base = full_id.split(":")[0]
            name = base[len("functions.") :] if base.startswith("functions.") else base
        # Drop bare-counter ids, as vLLM does; we have no schema to infer the name from.
        if name.isdigit():
            json_start = arg_begin + len(_KIMI_ARG_BEGIN)
            brace_end = (
                _balanced_brace_end(body, json_start)
                if (json_start < len(body) and body[json_start] == "{")
                else None
            )
            if brace_end is None:
                pos = arg_begin + len(_KIMI_ARG_BEGIN)
            else:
                pos = brace_end + 1
            continue
        json_start = arg_begin + len(_KIMI_ARG_BEGIN)
        while json_start < len(body) and body[json_start] in " \t\n\r":
            json_start += 1
        if json_start >= len(body) or body[json_start] != "{":
            pos = arg_begin + len(_KIMI_ARG_BEGIN)
            continue
        brace_end, relaxed = _call_object_end(body, json_start, relaxed)
        if brace_end is None:
            # Skip a malformed call but keep parsing later ones (vLLM recovers them).
            nxt = body.find(_KIMI_CALL_BEGIN, json_start)
            if nxt < 0:
                break
            pos = nxt
            continue
        args = _loads_call_object(body[json_start : brace_end + 1], relaxed = relaxed)
        if not isinstance(args, dict):
            pos = brace_end + 1
            continue
        if not allow_incomplete:
            # Strict mode rejects a call not closed by <|tool_call_end|> before the next <|tool_call_begin|>.
            end_marker = body.find(_KIMI_CALL_END, brace_end + 1)
            next_call = body.find(_KIMI_CALL_BEGIN, brace_end + 1)
            if end_marker < 0 or (next_call >= 0 and end_marker > next_call):
                pos = brace_end + 1
                continue
        if name:
            out.append(
                {
                    "id": full_id or f"call_{id_offset + len(out)}",
                    "type": "function",
                    "function": {
                        "name": name,
                        "arguments": json.dumps(args),
                    },
                }
            )
        # Seeking <|tool_call_end|> could skip the next call when this one's marker is missing.
        pos = brace_end + 1
    return out


class _MinimaxValueIndex:
    """Whether a tag sits inside an unclosed ``<parameter>`` value (argument text, not markup), O(log n) per query."""

    __slots__ = ("_param_starts", "_param_ends", "_param_closers", "_func_closers")

    def __init__(self, text: str):
        starts: list[int] = []
        ends: list[int] = []
        for m in _TC_PARAM_START_RE.finditer(text):
            starts.append(m.start())
            # Bounded at the tag's own ">", as the helper's endpos-limited match is.
            ends.append(text.index(">", m.start()) + 1)
        self._param_starts = starts
        self._param_ends = ends
        self._param_closers = self._positions(text, ("</parameter>", "</param>"))
        self._func_closers = self._positions(text, (_MINIMAX_INVOKE_CLOSE, _MINIMAX_BLOCK_CLOSE))

    @staticmethod
    def _positions(text: str, tags) -> list[int]:
        found: list[int] = []
        for tag in tags:
            at = text.find(tag)
            while at >= 0:
                found.append(at)
                at = text.find(tag, at + len(tag))
        return sorted(found)

    @staticmethod
    def _any_in(positions: list[int], lo: int, hi: int) -> bool:
        i = bisect.bisect_left(positions, lo)
        return i < len(positions) and positions[i] <= hi

    def inside(self, pos: int) -> bool:
        idx = bisect.bisect_right(self._param_ends, pos) - 1
        if idx < 0:
            return False
        last = self._param_starts[idx]
        if self._any_in(self._param_closers, last, pos):
            return False
        if self._param_closers and self._param_closers[-1] >= pos + 1:
            return True
        return not self._any_in(self._func_closers, last, pos)


def _minimax_block_spans(text: str) -> list[tuple[int, int, bool]]:
    """``(start, end, closed)`` per ``<minimax:tool_call>`` block; an unclosed block runs to the end of the text."""
    spans: list[tuple[int, int, bool]] = []
    if _MINIMAX_BLOCK_OPEN not in text:
        return spans
    inside = _MinimaxValueIndex(text).inside
    pos = 0
    while True:
        start = text.find(_MINIMAX_BLOCK_OPEN, pos)
        if start < 0:
            return spans
        if inside(start):
            pos = start + len(_MINIMAX_BLOCK_OPEN)
            continue
        close = text.find(_MINIMAX_BLOCK_CLOSE, start + len(_MINIMAX_BLOCK_OPEN))
        while close >= 0 and inside(close):
            close = text.find(_MINIMAX_BLOCK_CLOSE, close + 1)
        if close < 0:
            spans.append((start, len(text), False))
            return spans
        pos = close + len(_MINIMAX_BLOCK_CLOSE)
        spans.append((start, pos, True))


def _parse_minimax_tool_calls(
    content: str,
    *,
    id_offset: int,
    allow_incomplete: bool = True,
) -> list[dict]:
    """Values stay strings as in the ``<function=...>`` form; the tool loop coerces them by schema. A block inside
    reasoning (a ``<think>`` span, or before a stray ``</think>`` the template opened) is a rehearsal and is skipped."""
    out: list[dict] = []
    if _MINIMAX_BLOCK_OPEN not in content:
        return out
    # Tool-aware, so a think tag in another block's arguments neither hides nor opens reasoning.
    think_spans = _merge_spans(_tool_healing._think_spans_outside_tool_markup(content))
    think_starts = [s for s, _ in think_spans]
    inside = _MinimaxValueIndex(content).inside
    for start, end, closed in _minimax_block_spans(content):
        think_idx = bisect.bisect_right(think_starts, start) - 1
        if think_idx >= 0 and start < think_spans[think_idx][1]:
            continue
        body_end = end - len(_MINIMAX_BLOCK_CLOSE) if closed else end
        invokes = [
            m
            for m in _MINIMAX_INVOKE_OPEN_RE.finditer(content, start, body_end)
            if not inside(m.start())
        ]
        for idx, im in enumerate(invokes):
            next_invoke = invokes[idx + 1].start() if idx + 1 < len(invokes) else body_end
            close = content.find(_MINIMAX_INVOKE_CLOSE, im.end(), next_invoke)
            while close >= 0 and inside(close):
                close = content.find(_MINIMAX_INVOKE_CLOSE, close + 1, next_invoke)
            if close < 0 and not allow_incomplete:
                continue
            body = content[im.end() : close if close >= 0 else next_invoke]
            args: dict = {}
            param_unclosed = False
            body_inside = _MinimaxValueIndex(body).inside
            params = [pm for pm in _TC_PARAM_START_RE.finditer(body) if not body_inside(pm.start())]
            for pidx, pm in enumerate(params):
                next_param = params[pidx + 1].start() if pidx + 1 < len(params) else len(body)
                raw_val = body[pm.end() : next_param]
                param_closed = raw_val.rstrip().endswith(("</parameter>", "</param>"))
                if not param_closed:
                    param_unclosed = True
                val = _TC_PARAM_CLOSE_RE.sub("", raw_val) if param_closed else raw_val
                args[pm.group(1) or pm.group(2)] = _trim_param_value(val)
            if param_unclosed and not allow_incomplete:
                continue
            out.append(
                {
                    "id": f"call_{id_offset + len(out)}",
                    "type": "function",
                    "function": {"name": im.group(1), "arguments": json.dumps(args)},
                }
            )
    return out