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.

11.2 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

"""search_conversation is offered per thread, not per backend, so this loop needs both GGUF guards."""

import os
import sys
import threading

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from core.inference.safetensors_agentic import run_safetensors_tool_loop  # noqa: E402
from core.inference.tool_call_parser import RAG_MAX_SEARCHES_PER_TURN  # noqa: E402

MESSAGES = [
    {"role": "user", "content": "what was the code from earlier"},
    {"role": "assistant", "content": "let me look"},
    {"role": "user", "content": "please"},
]


def _searching_model(calls_wanted):
    """A model that issues one distinct conversation search per iteration."""
    state = {"n": 0}

    def single_turn(messages):
        state["n"] += 1
        if state["n"] > calls_wanted:
            yield "done"
            return
        call = (
            '<tool_call>{"name":"search_conversation","arguments":'
            '{"query":"the code %d"}}</tool_call>' % state["n"]
        )
        buffer = ""
        for char in call:
            buffer += char
            yield buffer

    return single_turn


def _run(calls_wanted, execute_tool):
    return list(
        run_safetensors_tool_loop(
            single_turn = _searching_model(calls_wanted),
            messages = list(MESSAGES),
            tools = [{"type": "function", "function": {"name": "search_conversation"}}],
            execute_tool = execute_tool,
            cancel_event = threading.Event(),
            max_tool_iterations = calls_wanted + 1,
            thread_id = "t-sf",
        )
    )


def test_the_loop_hands_its_search_the_active_branch():
    """Without it the search falls back to the whole stored DAG, siblings included."""
    seen = []

    def execute_tool(name, arguments, **kwargs):
        seen.append(kwargs.get("conversation_branch"))
        return "an earlier turn"

    _run(1, execute_tool)

    [branch] = seen
    assert branch[: len(MESSAGES)] == MESSAGES and len(branch) == len(MESSAGES) + 1


def test_conversation_searches_share_the_per_turn_cap():
    """Paraphrased re-searches slip past the duplicate guard, so one per-turn cap is shared by all."""
    executed = []

    def execute_tool(name, arguments, **kwargs):
        executed.append(arguments.get("query"))
        return "an earlier turn"

    _run(RAG_MAX_SEARCHES_PER_TURN + 2, execute_tool)

    assert len(executed) == RAG_MAX_SEARCHES_PER_TURN


def test_the_budget_charges_token_dense_text_at_its_real_rate():
    """Four chars per token is wrong for CJK; spend must price non-ASCII at a token per character."""
    from core.inference.context_window import (
        estimate_messages_tokens,
        estimate_messages_tokens_dense,
        prompt_budget,
    )

    dense_messages = [
        {"role": "user", "content": "設定を確認してください。" * 40},
        {"role": "assistant", "content": "承知しました。" * 40},
        {"role": "user", "content": "先ほどのコードは何でしたか"},
    ]
    seen = {}
    tools = [{"type": "function", "function": {"name": "search_conversation"}}]

    list(
        run_safetensors_tool_loop(
            single_turn = _searching_model(1),
            messages = list(dense_messages),
            tools = tools,
            execute_tool = lambda name, arguments, **kwargs: seen.update(kwargs) or "an earlier turn",
            cancel_event = threading.Event(),
            max_tool_iterations = 2,
            thread_id = "t-sf",
            context_length = 4096,
            max_tokens = 512,
        )
    )

    budget = seen.get("conversation_budget_tokens")
    assert budget is not None
    assert budget <= prompt_budget(4096, 512) - estimate_messages_tokens_dense(dense_messages)
    assert estimate_messages_tokens_dense(dense_messages) > estimate_messages_tokens(dense_messages)


def test_the_dense_estimate_matches_the_flat_one_on_plain_ascii():
    """It corrects a known undercount, it does not make every English chat pessimistic."""
    from core.inference.context_window import (
        estimate_messages_tokens,
        estimate_messages_tokens_dense,
    )
    assert estimate_messages_tokens_dense(MESSAGES) == estimate_messages_tokens(MESSAGES)


def test_the_loop_budgets_its_search_against_this_models_context():
    """The loop must budget its search against this model's context, or top_k 8 appends unbudgeted."""
    from core.inference.context_window import estimate_messages_tokens, prompt_budget

    seen = {}
    tools = [{"type": "function", "function": {"name": "search_conversation"}}]

    def execute_tool(name, arguments, **kwargs):
        seen.update(kwargs)
        return "an earlier turn"

    list(
        run_safetensors_tool_loop(
            single_turn = _searching_model(1),
            messages = list(MESSAGES),
            tools = tools,
            execute_tool = execute_tool,
            cancel_event = threading.Event(),
            max_tool_iterations = 2,
            thread_id = "t-sf",
            context_length = 4096,
            max_tokens = 512,
        )
    )

    budget = seen.get("conversation_budget_tokens")
    assert budget is not None
    assert budget <= prompt_budget(4096, 512) - estimate_messages_tokens(tools)
    assert budget < prompt_budget(4096, 512) - estimate_messages_tokens(MESSAGES)


def _model_that_thinks_out_loud(preamble, stats_holder, reported):
    """A model that writes prose before its one search, reporting usage as a turn does."""
    state = {"n": 0}

    def single_turn(messages):
        stats_holder["stats"] = None
        state["n"] += 1
        if state["n"] > 1:
            yield "done"
            return
        call = (
            preamble
            + '<tool_call>{"name":"search_conversation","arguments":{"query":"the code"}}'
            + "</tool_call>"
        )
        buffer = ""
        for char in call:
            buffer += char
            yield buffer
        stats_holder["stats"] = {"usage": {"prompt_tokens": reported}}

    return single_turn


def _budget_after_a_preamble(preamble, reported, stats_holder):
    seen = {}
    list(
        run_safetensors_tool_loop(
            single_turn = _model_that_thinks_out_loud(preamble, stats_holder, reported),
            messages = [
                {"role": "user", "content": "recall the deployment steps. " * 420},
                {"role": "user", "content": "what was the code from earlier"},
            ],
            tools = [{"type": "function", "function": {"name": "search_conversation"}}],
            execute_tool = lambda name, arguments, **kwargs: seen.update(kwargs) or "an earlier turn",
            cancel_event = threading.Event(),
            max_tool_iterations = 2,
            thread_id = "t-sf",
            context_length = 8192,
            max_tokens = 512,
            generation_stats_holder = stats_holder,
        )
    )
    return seen["conversation_budget_tokens"]


def test_the_budget_charges_this_turns_own_output_in_tokens_not_bytes():
    """Charge the turn's own output in tokens, not bytes: 4600 English chars are about 1150 tokens."""
    from core.inference.context_window import retrieval_budget

    preamble = "the answer is somewhere in the earlier turns. " * 100
    budget = _budget_after_a_preamble(preamble, 3_067, {})

    assert budget > 3_000
    assert budget < retrieval_budget(8192, 512, 3_067, reply_returns = True)


def test_the_budget_spends_the_count_the_turn_reported():
    """The reported prompt count beats the character estimate, which would hand out room not there."""
    budget = _budget_after_a_preamble("thinking. ", 6_000, {})

    assert 0 < budget < 1_800


def test_the_budget_estimates_when_no_usage_was_reported():
    """Unreported turns fall back to the estimate, never zero, which would switch recall off entirely."""
    from core.inference.context_window import (
        estimate_messages_tokens_dense,
        retrieval_budget,
    )

    seen = {}
    tools = [{"type": "function", "function": {"name": "search_conversation"}}]

    list(
        run_safetensors_tool_loop(
            single_turn = _searching_model(1),
            messages = list(MESSAGES),
            tools = tools,
            execute_tool = lambda name, arguments, **kwargs: seen.update(kwargs) or "an earlier turn",
            cancel_event = threading.Event(),
            max_tool_iterations = 2,
            thread_id = "t-sf",
            context_length = 4096,
            max_tokens = 512,
            generation_stats_holder = {"stats": None},
        )
    )

    budget = seen["conversation_budget_tokens"]
    assert budget > 0
    assert budget <= retrieval_budget(
        4096, 512, estimate_messages_tokens_dense(tools), reply_returns = True
    )


def test_the_orchestrator_gives_the_loop_a_holder_of_its_own(monkeypatch):
    """The loop needs its own per-turn stats holder; the request-wide holder accumulates every turn."""
    import core.inference.safetensors_agentic as agentic
    from core.inference.orchestrator import InferenceOrchestrator

    seen = {}

    def capture_loop(**kwargs):
        seen.update(kwargs)
        return iter(())

    monkeypatch.setattr(agentic, "run_safetensors_tool_loop", capture_loop)
    backend = InferenceOrchestrator.__new__(InferenceOrchestrator)
    backend.active_model_name = "sf-model"
    backend.models = {"sf-model": {"context_length": 4096}}
    stats_holder = {"stats": {"usage": {"prompt_tokens": 999}}}

    list(
        backend.generate_chat_completion_with_tools(
            messages = list(MESSAGES),
            tools = [{"type": "function", "function": {"name": "search_conversation"}}],
            stats_holder = stats_holder,
        )
    )

    assert isinstance(seen["generation_stats_holder"], dict)
    assert seen["generation_stats_holder"] is not stats_holder
    assert seen["generation_stats_holder"] == {}


def _tool_loop_max_tokens(monkeypatch, max_tokens):
    import core.inference.safetensors_agentic as agentic
    from core.inference.orchestrator import InferenceOrchestrator

    seen = {}

    def capture_loop(**kwargs):
        seen.update(kwargs)
        return iter(())

    monkeypatch.setattr(agentic, "run_safetensors_tool_loop", capture_loop)
    backend = InferenceOrchestrator.__new__(InferenceOrchestrator)
    backend.active_model_name = "sf-model"
    backend.models = {"sf-model": {"context_length": 4096}}

    list(
        backend.generate_chat_completion_with_tools(
            messages = list(MESSAGES),
            tools = [],
            max_tokens = max_tokens,
        )
    )
    return seen["max_tokens"]


def test_an_absent_client_limit_stays_absent_through_the_loop(monkeypatch):
    """Zero counts as absent too; only a positive limit is forwarded."""
    assert _tool_loop_max_tokens(monkeypatch, None) is None
    assert _tool_loop_max_tokens(monkeypatch, 0) is None
    assert _tool_loop_max_tokens(monkeypatch, 512) == 512


def test_the_loop_omits_the_budget_when_the_context_is_unknown():
    """An absent budget must not become a budget of zero, which would refuse every search."""
    seen = {}

    def execute_tool(name, arguments, **kwargs):
        seen.update(kwargs)
        return "an earlier turn"

    _run(1, execute_tool)

    assert "conversation_budget_tokens" not in seen