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.

13.4 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

"""Truncated calls must not run yet keep their text, and every closed tool card must have opened."""

from __future__ import annotations

import asyncio
import json
import threading

import pytest

from core.inference import studio_tool_loop as loop_mod
from core.inference.studio_tool_loop import (
    ToolLoopPolicy,
    ToolLoopRun,
    stream_with_studio_tools,
)


_DONE = "data: [DONE]"


def _sse(
    delta = None,
    finish = None,
    **extra,
) -> str:
    choice: dict = {"index": 0, "delta": delta if delta is not None else {}}
    if finish is not None:
        choice["finish_reason"] = finish
    payload: dict = {"choices": [choice]}
    payload.update(extra)
    return "data: " + json.dumps(payload, ensure_ascii = False)


def _tool(name: str) -> dict:
    return {
        "type": "function",
        "function": {
            "name": name,
            "description": "",
            "parameters": {
                "type": "object",
                "properties": {"query": {"type": "string"}},
                "required": ["query"],
            },
        },
    }


WEB = _tool("web_search")


class FakeTransport:
    """Replays scripted turns; records what the loop asked for each time."""

    def __init__(
        self,
        turns,
        *,
        heals = True,
        max_turns = 20,
    ):
        self.turns = [list(turn) for turn in turns]
        self.heals_text_tool_calls = heals
        self.requests: list[dict] = []
        self.max_turns = max_turns

    def stream(self, *, messages, tools, tool_choice, cancel_event):
        self.requests.append(
            {
                "messages": [dict(message) for message in messages],
                "tools": tools,
                "tool_choice": tool_choice,
            }
        )
        assert len(self.requests) <= self.max_turns, "loop never terminated"
        lines = self.turns.pop(0) if self.turns else [_DONE]

        async def _gen():
            for line in lines:
                yield line

        return _gen()


@pytest.fixture
def executed(monkeypatch):
    calls: list[dict] = []

    def _execute(name, arguments, **kwargs):
        calls.append({"name": name, "arguments": arguments, **kwargs})
        return f"RESULT<{name}>"

    monkeypatch.setattr(loop_mod, "execute_tool", _execute)
    monkeypatch.setattr(loop_mod, "build_rag_autoinject", lambda *a, **k: None)
    monkeypatch.setattr(loop_mod, "is_high_risk_tool_call", lambda name, args: False)
    return calls


def _run(transport, **policy_kwargs):
    fields = {
        "tools": [WEB],
        "max_calls": 25,
        "timeout": 300,
        "permission_mode": "off",
        "confirm_calls": False,
        "bypass_permissions": False,
        "rag_scope": None,
    }
    fields.update(policy_kwargs)

    async def _collect():
        out: list[str] = []
        agen = stream_with_studio_tools(
            transport,
            run = ToolLoopRun(
                messages = [{"role": "user", "content": "hi"}],
                session_id = "s1",
                thread_id = "t1",
            ),
            policy = ToolLoopPolicy(**fields),
            cancel_event = threading.Event(),
        )
        async for line in agen:
            out.append(line)
        return out

    return asyncio.run(asyncio.wait_for(_collect(), timeout = 30.0))


def _payloads(lines):
    for line in lines:
        if not line.startswith("data: "):
            continue
        raw = line[6:]
        if raw == "[DONE]":
            continue
        try:
            payload = json.loads(raw)
        except ValueError:
            continue
        if isinstance(payload, dict):
            yield payload


def _events(lines, kind):
    return [payload for payload in _payloads(lines) if payload.get("type") == kind]


def _visible_text(lines) -> str:
    text = []
    for payload in _payloads(lines):
        if isinstance(payload.get("type"), str):
            continue
        for choice in payload.get("choices") or []:
            if not isinstance(choice, dict):
                continue
            delta = choice.get("delta")
            content = delta.get("content") if isinstance(delta, dict) else None
            if isinstance(content, str):
                text.append(content)
    return "".join(text)


def _call_turn(
    call_id = "c1",
    name = "web_search",
    arguments = '{"query":"q"}',
):
    return [
        _sse(
            {
                "tool_calls": [
                    {"index": 0, "id": call_id, "function": {"name": name, "arguments": arguments}}
                ]
            }
        ),
        _sse(finish = "tool_calls"),
        _DONE,
    ]


_HEALED_TURN = [
    _sse({"content": "Let me compute that. "}),
    _sse({"content": '<tool_call>{"name": "web_search", '}),
    _sse({"content": '"arguments": {"query": "42"}}</tool_call>'}),
    _sse({"content": " follow-up"}),
    _sse(finish = "length"),
    _DONE,
]


def test_truncated_healed_call_releases_its_own_markup(executed):
    """A truncated call's markup must be released back into the visible text, not silently dropped."""
    lines = _run(FakeTransport([_HEALED_TURN]))

    assert executed == [], "a truncated call must never be executed"
    visible = _visible_text(lines)
    assert "Let me compute that. " in visible
    assert " follow-up" in visible
    assert (
        '<tool_call>{"name": "web_search", "arguments": {"query": "42"}}</tool_call>' in visible
    ), f"the promoted span was lost from the stream: {visible!r}"


def test_untruncated_healed_call_still_hides_its_markup(executed):
    """The release above is for truncation only: a normal turn still executes."""
    turn = list(_HEALED_TURN[:-2]) + [_sse(finish = "stop"), _DONE]
    lines = _run(FakeTransport([turn, [_sse({"content": "done"}), _sse(finish = "stop"), _DONE]]))

    assert [call["name"] for call in executed] == ["web_search"]
    assert "<tool_call>" not in _visible_text(lines)


def test_truncated_structured_call_relays_nothing_extra(executed):
    """A provider-emitted call had no markup removed, so there is none to give back."""
    turn = [
        _sse({"content": "thinking"}),
        _sse(
            {
                "tool_calls": [
                    {
                        "index": 0,
                        "id": "c1",
                        "function": {"name": "web_search", "arguments": '{"que'},
                    }
                ]
            }
        ),
        _sse(finish = "length"),
        _DONE,
    ]
    lines = _run(FakeTransport([turn]))

    assert executed == []
    assert _visible_text(lines) == "thinking"
    # the delta was already relayed, so close c1's card like any unrun call
    assert _card_ids(lines, "tool_end") == _card_ids(lines, "tool_start") == ["c1"]
    assert "output limit" in _events(lines, "tool_end")[0]["result"]


def test_a_truncated_call_never_streamed_gets_no_card(executed):
    """A call recovered from text was never streamed, so the healer's released span already tells
    the user; a card would report it twice."""
    turn = [
        _sse({"content": '<tool_call>{"name": "web_search", "arg'}),
        _sse(finish = "length"),
        _DONE,
    ]
    lines = _run(FakeTransport([turn]))

    assert executed == []
    assert _events(lines, "tool_start") == []
    assert _events(lines, "tool_end") == []


def _card_ids(lines, kind):
    return [event.get("tool_call_id") for event in _events(lines, kind)]


def _overflow_turns():
    """Two parallel calls, one budget slot left: the overflow's broken history reaches the provider."""
    return [
        [
            _sse(
                {
                    "tool_calls": [
                        {
                            "index": 0,
                            "id": "c1",
                            "function": {"name": "web_search", "arguments": '{"query":"a"}'},
                        },
                        {
                            "index": 1,
                            "id": "c2",
                            "function": {"name": "web_search", "arguments": '{"query":"b"}'},
                        },
                    ]
                }
            ),
            _sse(finish = "tool_calls"),
            _DONE,
        ],
        [_sse({"content": "final"}), _sse(finish = "stop"), _DONE],
    ]


def test_budget_exhausted_call_does_not_close_a_card_it_never_opened(executed):
    """A tool_end with no tool_start closes a card the client never drew."""
    transport = FakeTransport(_overflow_turns())
    lines = _run(transport, max_calls = 1)

    assert len(executed) == 1, "the budget must still be enforced"
    assert _card_ids(lines, "tool_end") == _card_ids(
        lines, "tool_start"
    ), "every closed card must have been opened, in order"


def test_budget_exhausted_result_is_declared_by_an_assistant_tool_call(executed):
    """An orphan role="tool" message is a 400 from OpenAI, DeepSeek and vLLM."""
    transport = FakeTransport(_overflow_turns())
    _run(transport, max_calls = 1)

    assert len(transport.requests) > 1, "the overflow must reach a follow-up turn"
    for request in transport.requests:
        declared = {
            call.get("id")
            for message in request["messages"]
            if message.get("role") == "assistant"
            for call in message.get("tool_calls") or []
        }
        orphans = [
            message["tool_call_id"]
            for message in request["messages"]
            if message.get("role") == "tool" and message.get("tool_call_id") not in declared
        ]
        assert not orphans, f"role=tool messages with no matching assistant tool_calls: {orphans}"


def test_disabled_call_card_is_opened_before_it_is_closed(executed):
    """Same invariant on the controller's no-op branch."""
    transport = FakeTransport(
        [_call_turn(name = "terminal"), [_sse({"content": "final"}), _sse(finish = "stop"), _DONE]]
    )
    lines = _run(transport, tools = [WEB])

    assert executed == []
    assert _card_ids(lines, "tool_end") == _card_ids(lines, "tool_start")


def test_truncated_mcp_call_card_carries_server_display_name(tmp_path, monkeypatch, executed):
    """The unrun card for a truncated MCP call must show the server display name."""
    from storage import mcp_servers_db

    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))
    monkeypatch.setattr(mcp_servers_db, "_schema_ready", set())
    mcp_servers_db.create_server(id = "srv1", display_name = "GitHub", url = "https://a/m")

    turn = [
        _sse(
            {
                "tool_calls": [
                    {
                        "index": 0,
                        "id": "m1",
                        "function": {"name": "mcp__srv1__create_issue", "arguments": '{"tit'},
                    }
                ]
            }
        ),
        _sse(finish = "length"),
        _DONE,
    ]
    lines = _run(FakeTransport([turn]), tools = [WEB, _tool("mcp__srv1__create_issue")])

    assert executed == []
    assert _card_ids(lines, "tool_end") == _card_ids(lines, "tool_start") == ["m1"]
    assert _events(lines, "tool_start")[0]["provenance"].get("mcp_server") == "GitHub"
    assert _events(lines, "tool_end")[0]["provenance"].get("mcp_server") == "GitHub"


def test_budget_exhausted_mcp_card_carries_server_display_name(tmp_path, monkeypatch, executed):
    """The unrun card for a budget-exhausted MCP call must show the display name."""
    from storage import mcp_servers_db

    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))
    monkeypatch.setattr(mcp_servers_db, "_schema_ready", set())
    mcp_servers_db.create_server(id = "srv1", display_name = "GitHub", url = "https://a/m")

    turns = [
        [
            _sse(
                {
                    "tool_calls": [
                        {
                            "index": 0,
                            "id": "c1",
                            "function": {"name": "web_search", "arguments": '{"query":"a"}'},
                        },
                        {
                            "index": 1,
                            "id": "m1",
                            "function": {
                                "name": "mcp__srv1__create_issue",
                                "arguments": '{"title":"b"}',
                            },
                        },
                    ]
                }
            ),
            _sse(finish = "tool_calls"),
            _DONE,
        ],
        [_sse({"content": "done"}), _sse(finish = "stop"), _DONE],
    ]
    lines = _run(FakeTransport(turns), tools = [WEB, _tool("mcp__srv1__create_issue")], max_calls = 1)

    assert len(executed) == 1, "the budget must still be enforced"
    mcp_starts = [
        e for e in _events(lines, "tool_start") if e["tool_name"] == "mcp__srv1__create_issue"
    ]
    assert len(mcp_starts) == 1
    assert mcp_starts[0]["provenance"].get("mcp_server") == "GitHub"


def test_an_unrun_card_shows_the_id_the_call_named():
    """The loop builds its own tool_start, so the exact-text guarantee is checked here too."""
    import json as _json

    from core.inference.studio_tool_loop import _unrun_call_card

    lines = _unrun_call_card(
        tool_name = "del_rec",
        tool_call_id = "c1",
        arguments = {"id": 9007199254740993},
        result = "not run",
        provenance = {"source": "local"},
    )
    start = _json.loads(lines[0].removeprefix("data: ").strip())

    assert start["type"] == "tool_start"
    assert start["arguments_text"] == '{"id":9007199254740993}'