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.

26.1 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

"""Hosted web_search and code_execution stay with the provider; default permission_mode as Codex."""

import asyncio
import ast
import pathlib
import threading
from types import SimpleNamespace

import pytest

from core.inference.providers import provider_hosted_tools
from core.inference.tools import is_high_risk_tool_call


_ROUTE_SOURCE = pathlib.Path(__file__).resolve().parents[1] / "routes" / "inference.py"


def _drive(coro):
    return asyncio.new_event_loop().run_until_complete(coro)


class LoopEntered(Exception):
    """stream_with_studio_tools was called; carries the ToolLoopPolicy."""


class FakeExternalClient:
    """Stands in for ExternalProviderClient, recording the passthrough call."""

    last: dict = {}
    served_template = None

    def __init__(self, **kwargs):
        FakeExternalClient.last = {"ctor": kwargs, "passthrough": None}
        self.provider_type = kwargs.get("provider_type")

    def stream_chat_completion(self, **kwargs):
        FakeExternalClient.last["passthrough"] = kwargs

        async def gen():
            yield 'data: {"choices":[{"delta":{"content":"hi"}}]}\n\n'
            yield "data: [DONE]\n\n"

        return gen()

    async def served_chat_template(self, model):
        return FakeExternalClient.served_template

    async def served_model_reads_images(self, model):
        return None

    async def close(self):
        return None


def _request():
    async def is_disconnected():
        return False

    return SimpleNamespace(
        # the confirm gate asks over these frames
        headers = {"X-Unsloth-Events": "1"},
        state = SimpleNamespace(skip_api_monitor = True),
        is_disconnected = is_disconnected,
    )


@pytest.fixture(autouse = True)
def _clean_policy():
    from state.tool_policy import reset_tool_policy

    reset_tool_policy()
    yield
    reset_tool_policy()


def _install(monkeypatch, provider_type: str):
    from core.inference.providers import get_base_url
    from routes import inference as inf

    monkeypatch.setattr(
        inf.providers_db,
        "get_provider",
        lambda _pid: {
            "id": _pid,
            "provider_type": provider_type,
            "base_url": get_base_url(provider_type) or "http://127.0.0.1:8080/v1",
            "display_name": "Saved connection",
            "is_enabled": True,
        },
    )
    monkeypatch.setattr(inf, "resolve_provider_api_key_or_400", lambda *a, **k: "k")
    monkeypatch.setattr(inf, "ExternalProviderClient", FakeExternalClient)

    def _loop_raiser(*a, **k):
        raise LoopEntered(k.get("policy"))

    monkeypatch.setattr(inf, "stream_with_studio_tools", _loop_raiser)
    return inf


def _payload(**overrides):
    from models.inference import ChatCompletionRequest

    base = dict(
        messages = [{"role": "user", "content": "what is 2+2?"}],
        provider_id = "saved-1",
        external_model = "gpt-5.4",
        stream = True,
    )
    base.update(overrides)
    return ChatCompletionRequest(**base)


def _run(inf, payload):
    async def go():
        resp = await inf._proxy_to_external_provider(payload, _request(), current_subject = "t")
        return [chunk async for chunk in resp.body_iterator]

    return _drive(go())


# the old gate, kept as code so expectations derive from it rather than restate it
def _merge_base_takes_studio_loop(payload, provider_type: str) -> bool:
    from routes.inference import _explicit_studio_tool_loop_requested
    return (
        provider_type == "openai_codex"
        and payload.stream is True
        and _explicit_studio_tool_loop_requested(payload)
    )


# the pre-PR wire shape for hosted-tool pills: two keys, no permission_mode or mcp_enabled
HOSTED_PROVIDERS = ("openai", "gemini", "openrouter", "kimi", "anthropic")

HOSTED_SELECTIONS = (
    ["web_search"],
    ["code_execution"],
    ["web_search", "code_execution"],
    ["web_search", "web_fetch", "code_execution", "image_generation"],
)

SELF_HOSTED_PROVIDERS = ("llama_cpp", "vllm", "ollama", "custom")


@pytest.mark.parametrize("provider_type", HOSTED_PROVIDERS)
@pytest.mark.parametrize("selection", HOSTED_SELECTIONS)
def test_a_hosted_tool_request_still_reaches_the_provider(monkeypatch, provider_type, selection):
    """Shape 1: only hosted names, on a provider that hosts them."""
    inf = _install(monkeypatch, provider_type)
    payload = _payload(enable_tools = True, enabled_tools = selection)

    assert _merge_base_takes_studio_loop(payload, provider_type) is False

    chunks = _run(inf, payload)
    passthrough = FakeExternalClient.last["passthrough"]
    assert passthrough is not None, "the Unsloth loop stole a hosted-tool request"
    assert passthrough["enabled_tools"] == selection
    assert passthrough["stream"] is True
    assert any("hi" in chunk for chunk in chunks)


@pytest.mark.parametrize("provider_type", HOSTED_PROVIDERS)
def test_a_studio_hosted_provider_receives_the_current_date(monkeypatch, provider_type):
    inf = _install(monkeypatch, provider_type)
    monkeypatch.setattr(
        inf,
        "current_date_prompt_line",
        lambda **_kwargs: "The current date is 2026-08-15.",
    )

    _run(inf, _payload())

    messages = FakeExternalClient.last["passthrough"]["messages"]
    assert messages[0] == {"role": "system", "content": "The current date is 2026-08-15."}
    assert messages[1] == {"role": "user", "content": "what is 2+2?"}


def test_an_api_request_without_resolved_server_tools_stays_undated(monkeypatch):
    inf = _install(monkeypatch, "openai")
    monkeypatch.setattr(inf, "_request_has_api_key", lambda _request: True)
    monkeypatch.setattr(inf, "_request_is_internal_workflow", lambda _request: False)
    monkeypatch.setattr(
        inf,
        "current_date_prompt_line",
        lambda **_kwargs: "The current date is 2026-08-15.",
    )

    _run(
        inf,
        _payload(
            enable_tools = True,
            enabled_tools = ["unknown_tool"],
            run_tools_locally = True,
        ),
    )

    assert FakeExternalClient.last["passthrough"]["messages"] == [
        {"role": "user", "content": "what is 2+2?"}
    ]


@pytest.mark.parametrize("provider_type", ("ollama", "llama_cpp", "vllm"))
def test_a_local_server_keeps_its_own_system_prompt_when_studio_sends_no_system_turn(
    monkeypatch, provider_type
):
    inf = _install(monkeypatch, provider_type)
    monkeypatch.setattr(
        inf,
        "current_date_prompt_line",
        lambda **_kwargs: "The current date is 2026-08-15.",
    )

    _run(inf, _payload())

    assert FakeExternalClient.last["passthrough"]["messages"] == [
        {"role": "user", "content": "what is 2+2?"}
    ]


def test_a_llama_cpp_connection_dates_a_served_template_with_no_default(monkeypatch):
    inf = _install(monkeypatch, "llama_cpp")
    monkeypatch.setattr(
        FakeExternalClient,
        "served_template",
        "{% for m in messages %}<|im_start|>{{ m['role'] }}\n{{ m['content'] }}<|im_end|>\n{% endfor %}",
    )
    monkeypatch.setattr(
        inf,
        "current_date_prompt_line",
        lambda **_kwargs: "The current date is 2026-08-15.",
    )

    _run(inf, _payload())

    assert FakeExternalClient.last["passthrough"]["messages"] == [
        {"role": "system", "content": "The current date is 2026-08-15."},
        {"role": "user", "content": "what is 2+2?"},
    ]


def test_an_ollama_connection_still_dates_a_studio_composed_system_prompt(monkeypatch):
    """Over-firing would drop the date for every Ollama user, not just empty-prompt ones."""
    inf = _install(monkeypatch, "ollama")
    monkeypatch.setattr(
        inf,
        "current_date_prompt_line",
        lambda **_kwargs: "The current date is 2026-08-15.",
    )

    _run(
        inf,
        _payload(
            messages = [
                {"role": "system", "content": "Be terse."},
                {"role": "user", "content": "what is 2+2?"},
            ]
        ),
    )

    assert FakeExternalClient.last["passthrough"]["messages"] == [
        {"role": "system", "content": "The current date is 2026-08-15.\n\nBe terse."},
        {"role": "user", "content": "what is 2+2?"},
    ]


def test_a_custom_connection_still_gets_the_synthesized_turn(monkeypatch):
    inf = _install(monkeypatch, "custom")
    monkeypatch.setattr(
        inf,
        "current_date_prompt_line",
        lambda **_kwargs: "The current date is 2026-08-15.",
    )

    _run(inf, _payload())

    assert FakeExternalClient.last["passthrough"]["messages"] == [
        {"role": "system", "content": "The current date is 2026-08-15."},
        {"role": "user", "content": "what is 2+2?"},
    ]


def test_full_access_on_ollama_keeps_the_date_the_nudge_costs_nothing_to_carry(monkeypatch):
    """Ollama full access keeps the date line: its own system turn displaces the Modelfile anyway."""
    inf = _install(monkeypatch, "ollama")
    monkeypatch.setattr(
        inf,
        "current_date_prompt_line",
        lambda **_kwargs: "The current date is 2026-08-15.",
    )
    seen = {}

    def _capture(*_args, **kwargs):
        seen["messages"] = list(kwargs["run"].messages)
        raise LoopEntered(kwargs.get("policy"))

    monkeypatch.setattr(inf, "stream_with_studio_tools", _capture)

    with pytest.raises(LoopEntered):
        _run(
            inf,
            _payload(
                enable_tools = True,
                enabled_tools = ["terminal"],
                run_tools_locally = True,
                bypass_permissions = True,
            ),
        )

    assert seen["messages"][0]["role"] == "system"
    assert seen["messages"][0]["content"].startswith("The current date is 2026-08-15.\n\n")
    assert "sandbox" in seen["messages"][0]["content"], "the Full access nudge is still delivered"


def test_a_hosted_code_execution_is_not_dropped(monkeypatch):
    """The regression in one line: `code_execution` has no local implementation,
    so a loop that captures this request executes web_search itself and silently
    never runs the other half of what the user turned on."""
    inf = _install(monkeypatch, "openai")
    _run(inf, _payload(enable_tools = True, enabled_tools = ["web_search", "code_execution"]))
    assert "code_execution" in (FakeExternalClient.last["passthrough"]["enabled_tools"] or [])


def test_a_code_execution_with_run_tools_locally_still_answers_the_confirm_gate(monkeypatch):
    """run_tools_locally must not let a hosted-only code_execution turn skip the confirm-gate 400."""
    from fastapi import HTTPException

    inf = _install(monkeypatch, "openai")
    # Class-level state; an untouched record is evidence that nothing was sent
    FakeExternalClient.last = {}
    payload = _payload(
        enable_tools = True,
        enabled_tools = ["code_execution"],
        run_tools_locally = True,
        confirm_tool_calls = True,
    )
    with pytest.raises(HTTPException) as excinfo:
        _run(inf, payload)
    assert excinfo.value.status_code == 400
    assert FakeExternalClient.last.get("passthrough") is None, "ran unconfirmed"


def test_a_code_execution_with_run_tools_locally_still_reaches_the_provider(monkeypatch):
    """And with no confirmation asked for, it proxies exactly as it always did."""
    inf = _install(monkeypatch, "openai")
    _run(
        inf,
        _payload(
            enable_tools = True,
            enabled_tools = ["code_execution"],
            run_tools_locally = True,
        ),
    )
    assert FakeExternalClient.last["passthrough"]["enabled_tools"] == ["code_execution"]


@pytest.mark.parametrize("provider_type", SELF_HOSTED_PROVIDERS)
def test_a_self_hosted_provider_still_runs_studios_own_web_search(monkeypatch, provider_type):
    """Self-hosted servers have no hosted tools, so the request means Unsloth's local loop."""
    assert provider_hosted_tools(provider_type) == frozenset()
    inf = _install(monkeypatch, provider_type)
    with pytest.raises(LoopEntered):
        _run(inf, _payload(enable_tools = True, enabled_tools = ["web_search"]))


@pytest.mark.parametrize(
    "overrides",
    [
        {"enable_tools": True, "enabled_tools": ["python"]},
        {"enable_tools": True, "enabled_tools": ["terminal"]},
        {"enable_tools": True, "enabled_tools": ["web_search", "python"]},
        {"enable_tools": True, "enabled_tools": ["web_search"], "mcp_enabled": True},
        {"enable_tools": True},
    ],
)
def test_a_local_only_selection_takes_the_loop_on_a_hosted_provider(monkeypatch, overrides):
    """One Unsloth-only or MCP name is unambiguous, so hosted providers also take the loop."""
    # imported inside the function, so patch core.inference.tools, not routes.inference
    monkeypatch.setattr(
        "core.inference.tools.get_enabled_mcp_tools",
        lambda: _noop_mcp(),
    )
    inf = _install(monkeypatch, "openai")
    with pytest.raises(LoopEntered):
        _run(inf, _payload(**overrides))


async def _noop_mcp():
    return []


def test_a_unknown_tool_names_never_read_as_hosted(monkeypatch):
    """Unknown names fail toward the loop, which owns the local catalog, not the provider."""
    from routes.inference import _selects_only_provider_hosted_tools

    payload = _payload(enable_tools = True, enabled_tools = ["web_search", "not_a_tool"])
    assert _selects_only_provider_hosted_tools(payload, "openai") is False


@pytest.mark.parametrize("bad", [None, 5, {"web_search": True}, ["web_search", 5]])
def test_a_malformed_enabled_tools_is_not_a_hosted_request(bad):
    from routes.inference import _selects_only_provider_hosted_tools
    payload = SimpleNamespace(enabled_tools = bad, mcp_enabled = False)
    assert _selects_only_provider_hosted_tools(payload, "openai") is False


def test_a_codex_declares_no_hosted_tools():
    """Codex's `web_search` is Unsloth's own tool run by the Codex loop, so the
    hosted check must never fire there."""
    assert provider_hosted_tools("openai_codex") == frozenset()


def test_mcp_intent_with_no_tools_is_not_refused_for_a_prompt_it_can_never_show(monkeypatch):
    """mcp_enabled must wait for the selected catalog; with no tools the loop never prompts."""
    monkeypatch.setattr(
        "core.inference.tools.get_enabled_mcp_tools",
        lambda: _noop_mcp(),
    )
    inf = _install(monkeypatch, "openai")
    payload = _payload(mcp_enabled = True)

    async def is_disconnected():
        return False

    headerless = SimpleNamespace(
        headers = {},
        state = SimpleNamespace(skip_api_monitor = True),
        is_disconnected = is_disconnected,
    )

    async def go():
        resp = await inf._proxy_to_external_provider(payload, headerless, current_subject = "t")
        return [chunk async for chunk in resp.body_iterator]

    assert _drive(go())


def test_tool_choice_none_is_not_refused_for_a_prompt_it_can_never_show(monkeypatch):
    """The catalogue is non-empty here, but stream_with_studio_tools withdraws it every
    turn under tool_choice "none", so no call and no approval prompt can happen. A
    headerless stream must still get its clean text answer."""
    inf = _install(monkeypatch, "openai")
    payload = _payload(enable_tools = True, enabled_tools = ["python"], tool_choice = "none")

    async def is_disconnected():
        return False

    headerless = SimpleNamespace(
        headers = {},
        state = SimpleNamespace(skip_api_monitor = True),
        is_disconnected = is_disconnected,
    )

    async def go():
        resp = await inf._proxy_to_external_provider(payload, headerless, current_subject = "t")
        return [chunk async for chunk in resp.body_iterator]

    with pytest.raises(LoopEntered):
        _drive(go())


def test_a_refused_request_does_not_strand_a_running_monitor_entry(monkeypatch):
    """A refusal after api_monitor.start must close its entry, since running entries are exempt from
    trimming and would strand /api/inference/monitor in generating."""
    from core.inference.api_monitor import ApiMonitor
    from fastapi import HTTPException

    inf = _install(monkeypatch, "openai")
    monitor = ApiMonitor(max_entries = 3)
    monkeypatch.setattr(inf, "api_monitor", monitor)
    payload = _payload(enable_tools = True, enabled_tools = ["python"])

    async def is_disconnected():
        return False

    headerless = SimpleNamespace(
        headers = {},
        state = SimpleNamespace(),
        url = SimpleNamespace(path = "/v1/chat/completions"),
        method = "POST",
        is_disconnected = is_disconnected,
    )

    async def go():
        return await inf._proxy_to_external_provider(payload, headerless, current_subject = "t")

    with pytest.raises(HTTPException) as exc:
        _drive(go())
    assert exc.value.status_code == 400
    assert monitor.active_count() == 0
    [entry] = monitor.snapshot()
    assert entry["status"] == "error"


def test_b_an_omitted_permission_mode_arms_the_auto_gate(monkeypatch):
    """`permission_mode` unset on a streaming request resolves to "auto" with
    the confirm gate ON, so high-risk calls still prompt."""
    inf = _install(monkeypatch, "llama_cpp")
    payload = _payload(enable_tools = True, enabled_tools = ["python"])
    assert payload.permission_mode is None
    assert payload.confirm_tool_calls is None

    with pytest.raises(LoopEntered) as excinfo:
        _run(inf, payload)
    policy = excinfo.value.args[0]
    assert policy.permission_mode == "auto"
    assert policy.confirm_calls is True


@pytest.mark.parametrize(
    "nudge_tool_calls", [None, False, True], ids = ["omitted", "disabled", "enabled"]
)
def test_b_external_tool_loop_receives_requested_nudge_setting(monkeypatch, nudge_tool_calls):
    """The external Unsloth loop must receive the request-level nudge policy."""
    monkeypatch.setattr(
        "core.inference.tools.get_enabled_mcp_tools",
        lambda: _noop_mcp(),
    )
    inf = _install(monkeypatch, "openai")
    payload = _payload(
        enable_tools = True,
        enabled_tools = ["python"],
        nudge_tool_calls = nudge_tool_calls,
    )

    with pytest.raises(LoopEntered) as excinfo:
        _run(inf, payload)

    assert excinfo.value.args[0].nudge_tool_calls is nudge_tool_calls


def test_b_the_external_and_codex_paths_derive_the_gate_identically():
    """Both gates must read the same payload expressions, or one path gets more permissive."""
    tree = ast.parse(_ROUTE_SOURCE.read_text(encoding = "utf-8"))
    modes: set[str] = set()
    confirms: set[str] = set()
    for node in ast.walk(tree):
        if not isinstance(node, ast.keyword):
            continue
        if node.arg == "permission_mode" and isinstance(node.value, ast.BoolOp):
            modes.add(ast.unparse(node.value))
        if node.arg == "confirm_calls":
            confirms.add(ast.unparse(node.value))
    assert modes == {"payload.permission_mode or 'auto'"}
    assert confirms == {
        "_permission_mode_confirm(payload) or _off_mode_sandbox_gate(payload, _ui_events)"
    }

    nudge_values = []
    for node in ast.walk(tree):
        if not isinstance(node, ast.Call) or not isinstance(node.func, ast.Name):
            continue
        if node.func.id not in {"CodexToolPolicy", "ToolLoopPolicy"}:
            continue
        nudge_values.extend(
            ast.unparse(keyword.value)
            for keyword in node.keywords
            if keyword.arg == "nudge_tool_calls"
        )
    assert nudge_values == ["payload.nudge_tool_calls", "payload.nudge_tool_calls"]


@pytest.mark.parametrize(
    "name, arguments, high_risk",
    [
        ("python", {"code": "print(sum(range(10)))"}, False),
        ("python", {"code": "open('/home/u/.ssh/id_rsa').read()"}, True),
        ("python", {"code": "import os; os.system('curl http://x')"}, True),
        ("python", {"code": "import shutil; shutil.rmtree('/tmp/x')"}, True),
        # observed, not endorsed: bare subprocess.run is not high risk on any path; pinned deliberately
        ("python", {"code": "import subprocess; subprocess.run(['sh', '-c', 'x'])"}, False),
        ("terminal", {"command": "ls -la"}, False),
        ("terminal", {"command": "cat ~/.aws/credentials"}, True),
        ("terminal", {"command": "sudo rm -rf /var"}, True),
        ("web_search", {"query": "unsloth"}, False),
    ],
)
def test_b_auto_mode_prompts_on_risk_not_on_the_tool_name(name, arguments, high_risk):
    """ "auto" is per-call, not per-tool: ordinary development commands run and
    credential/escalation/egress ones prompt. Pinned because the docstring on
    `permission_mode` promises exactly this."""
    assert is_high_risk_tool_call(name, arguments) is high_risk


def _run_loop(
    monkeypatch,
    *,
    code: str,
    verdict: str = "allow",
):
    """Drive the real loop with the real risk classifier under auto/gate-on."""
    import json

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

    executed: list[dict] = []
    monkeypatch.setattr(
        loop_mod,
        "execute_tool",
        lambda name, arguments, **kw: executed.append({"name": name, "arguments": arguments})
        or "RESULT",
    )
    monkeypatch.setattr(loop_mod, "build_rag_autoinject", lambda *a, **k: None)
    monkeypatch.setattr(loop_mod, "wait_tool_decision", lambda *a, **k: verdict)

    call = {
        "index": 0,
        "id": "call_a",
        "function": {"name": "python", "arguments": json.dumps({"code": code})},
    }
    turns = [
        ["data: " + json.dumps({"choices": [{"index": 0, "delta": {"tool_calls": [call]}}]})],
        ["data: " + json.dumps({"choices": [{"index": 0, "delta": {"content": "done"}}]})],
    ]

    class _Transport:
        heals_text_tool_calls = False

        def stream(self, *, messages, tools, tool_choice, cancel_event):
            lines = turns.pop(0) if turns else ["data: [DONE]"]

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

            return _gen()

    async def _collect():
        out = []
        agen = stream_with_studio_tools(
            _Transport(),
            run = ToolLoopRun(
                messages = [{"role": "user", "content": "hi"}],
                session_id = "s1",
                thread_id = "t1",
                tool_choice = None,
            ),
            policy = ToolLoopPolicy(
                tools = [
                    {
                        "type": "function",
                        "function": {
                            "name": "python",
                            "description": "",
                            "parameters": {"type": "object", "properties": {}},
                        },
                    }
                ],
                max_calls = 25,
                timeout = 300,
                permission_mode = "auto",
                confirm_calls = True,
                bypass_permissions = False,
                rag_scope = None,
            ),
            cancel_event = threading.Event(),
        )
        async for line in agen:
            out.append(line)
        return out

    lines = asyncio.new_event_loop().run_until_complete(_collect())
    starts = []
    for line in lines:
        if not line.startswith("data: ") or line[6:].strip() == "[DONE]":
            continue
        payload = json.loads(line[6:])
        if payload.get("type") == "tool_start":
            starts.append(payload)
    return starts, executed


def test_b_a_benign_python_call_runs_without_an_approval_frame(monkeypatch):
    starts, executed = _run_loop(monkeypatch, code = "print(2 + 2)")
    assert [s["awaiting_confirmation"] for s in starts] == [False]
    assert [c["name"] for c in executed] == ["python"]


def test_b_a_credential_reading_python_call_is_gated(monkeypatch):
    starts, executed = _run_loop(
        monkeypatch,
        code = "print(open('/home/u/.ssh/id_rsa').read())",
        verdict = "deny",
    )
    assert [s["awaiting_confirmation"] for s in starts] == [True]
    assert executed == []


def test_a_request_without_tools_never_scans_the_skill_roots(monkeypatch):
    inf = _install(monkeypatch, "openai")

    def _scan():
        raise AssertionError("skill roots scanned for a request that asked for no tools")

    monkeypatch.setattr(inf, "_enabled_agent_skills", _scan)
    chunks = _run(inf, _payload(enable_tools = False))
    assert FakeExternalClient.last["passthrough"] is not None
    assert any("hi" in chunk for chunk in chunks)


def test_the_conversation_id_reaches_the_provider_client(monkeypatch):
    inf = _install(monkeypatch, "openrouter")
    _run(inf, _payload(enable_tools = False, thread_id = "thread-7"))
    assert FakeExternalClient.last["passthrough"]["thread_id"] == "thread-7"

    def _loop_raiser(transport, **_):
        raise LoopEntered(transport._request_kwargs)

    monkeypatch.setattr(inf, "stream_with_studio_tools", _loop_raiser)
    with pytest.raises(LoopEntered) as excinfo:
        _run(inf, _payload(enable_tools = True, enabled_tools = ["python"], thread_id = "thread-7"))
    assert excinfo.value.args[0]["thread_id"] == "thread-7"


@pytest.mark.parametrize(
    "provider_type, model, caching",
    [
        ("anthropic", "claude-sonnet-4-6", None),
        ("openrouter", "anthropic/claude-sonnet-4.6", None),
        ("openrouter", "~anthropic/claude-opus-latest", True),
        ("anthropic", "claude-sonnet-4-6", False),
        ("openrouter", "deepseek/deepseek-v3.2", None),
    ],
)
def test_a_thread_from_yesterday_leaves_every_user_turn_verbatim(
    monkeypatch, provider_type, model, caching
):
    inf = _install(monkeypatch, provider_type)
    monkeypatch.setattr(
        inf, "current_date_prompt_line", lambda **_k: "The current date is 2026-08-16."
    )
    monkeypatch.setattr(inf, "_request_has_api_key", lambda _request: False)
    history = [
        {"role": "user", "content": "first"},
        {"role": "assistant", "content": "ok"},
        {"role": "user", "content": "second"},
    ]
    _run(
        inf,
        _payload(
            messages = history,
            external_model = model,
            enable_tools = False,
            thread_id = "thread-7",
            enable_prompt_caching = caching,
        ),
    )
    sent = FakeExternalClient.last["passthrough"]
    assert sent["thread_id"] == "thread-7"
    assert [m["content"] for m in sent["messages"] if m["role"] != "system"] == [
        "first",
        "ok",
        "second",
    ]