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.

38.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

import hashlib
import json
from pathlib import Path

import pytest

from routes.chat_generation_runs import (
    CreateChatGenerationRun,
    _contains_sensitive_key,
    _event_cursor,
    _sanitize_request,
)
from storage import chat_generation_runs_db as runs_db
from storage import studio_db
from state.tool_policy import reset_tool_policy, set_tool_policy, set_tool_policy_default
from utils.paths import studio_db_path


@pytest.fixture(autouse = True)
def _clean_tool_policy():
    reset_tool_policy()
    yield
    reset_tool_policy()


def _seed_thread(
    thread_id = "thread-1",
    user_id = "user-1",
    text = "Hello",
    created_at = 1,
):
    studio_db.upsert_chat_thread(
        {
            "id": thread_id,
            "title": "Chat",
            "modelType": "base",
            "modelId": "local",
            "createdAt": created_at,
        }
    )
    studio_db.upsert_chat_message(
        {
            "id": user_id,
            "threadId": thread_id,
            "role": "user",
            "content": [{"type": "text", "text": text}],
            "createdAt": created_at + 1,
        }
    )


@pytest.fixture
def chat_home():
    _seed_thread()


def _request(**overrides):
    request = {"model": "local", "messages": [{"role": "user", "content": "Hello"}], "stream": True}
    request.update(overrides)
    return request


def _model(**overrides):
    return CreateChatGenerationRun(
        runId = "run-1",
        threadId = "thread-1",
        userMessageId = "user-1",
        assistantMessageId = "assistant-1",
        requestPayload = _request(**overrides),
    )


def _tool_messages(arguments):
    return [
        {
            "role": "assistant",
            "tool_calls": [
                {
                    "id": "call-1",
                    "type": "function",
                    "function": {"name": "lookup", "arguments": arguments},
                }
            ],
        }
    ]


def _assert_protected(message):
    with pytest.raises(studio_db.ChatMessageProtectedError):
        studio_db.upsert_chat_message(message)


def _create(
    run_id = "run-1",
    owner = "alice",
    request = None,
):
    return runs_db.create_run(
        run_id = run_id,
        owner_subject = owner,
        thread_id = "thread-1",
        user_message_id = "user-1",
        assistant_message_id = "assistant-1" if run_id == "run-1" else f"assistant-{run_id}",
        request_payload = request or _request(),
    )


def test_create_is_owner_scoped_idempotent_and_binds_placeholder(chat_home):
    run, created = _create()
    replay, replay_created = _create()
    assert created is True and replay_created is False
    assert replay == run
    assert runs_db.get_run("run-1", "bob") is None
    assert [run["id"] for run in runs_db.list_active("thread-1")] == ["run-1"]
    message = studio_db.get_chat_message("thread-1", "assistant-1")
    assert message["metadata"] == {
        "generationRunId": "run-1",
        "generationSeq": 0,
        "generationStatus": "queued",
        "serverManaged": True,
    }
    _assert_protected({**message, "content": [{"type": "text", "text": "stale overwrite"}]})

    synced = studio_db.sync_chat_messages("thread-1", [], prune_missing = True)
    assert {message["id"] for message in synced} == {"user-1", "assistant-1"}
    assert runs_db.get_run("run-1", "alice") is not None
    explicitly_pruned = studio_db.sync_chat_messages(
        "thread-1",
        [],
        prune_missing = True,
        deleted_message_ids = {"assistant-1"},
    )
    assert {message["id"] for message in explicitly_pruned} == {"user-1", "assistant-1"}
    assert runs_db.get_run("run-1", "alice") is not None
    with pytest.raises(runs_db.ChatGenerationConflictError):
        _create(owner = "bob")
    with pytest.raises(runs_db.ChatGenerationConflictError):
        _create(request = _request(max_tokens = 9))


@pytest.mark.parametrize("admit_before_route", [True, False])
def test_fork_refuses_durable_run_before_supervisor_registration(
    chat_home, monkeypatch, admit_before_route
):
    from fastapi import HTTPException
    from routes import chat_history
    from state import active_generations

    assert "thread-1" not in active_generations.active_thread_ids()
    if admit_before_route:
        _create()
    else:
        original_fork = chat_history.fork_chat_thread

        def admit_then_fork(**kwargs):
            _create()
            return original_fork(**kwargs)

        monkeypatch.setattr(chat_history, "fork_chat_thread", admit_then_fork)

    with pytest.raises(HTTPException) as exc:
        chat_history.fork_thread(
            "thread-1",
            chat_history.ChatForkRequest(newThreadId = "fork-1", createdAt = 10),
            current_subject = "alice",
        )
    assert exc.value.status_code == 409
    assert "still generating" in exc.value.detail
    assert studio_db.get_chat_thread("fork-1") is None


def test_fork_resolves_tip_after_concurrent_generation_completes(chat_home, monkeypatch):
    from routes import chat_history

    original_fork = chat_history.fork_chat_thread

    def complete_then_fork(**kwargs):
        _create()
        token = runs_db.get_worker_token("run-1")
        assert runs_db.mark_running("run-1", token)
        message = studio_db.get_chat_message("thread-1", "assistant-1")
        message["content"] = [{"type": "text", "text": "Finished reply"}]
        message["metadata"].update({"generationSeq": 1, "generationStatus": "running"})
        studio_db.upsert_chat_message(message)
        runs_db.finish_run("run-1", worker_token = token, status = "completed")
        return original_fork(**kwargs)

    monkeypatch.setattr(chat_history, "fork_chat_thread", complete_then_fork)
    response = chat_history.fork_thread(
        "thread-1",
        chat_history.ChatForkRequest(newThreadId = "fork-1", createdAt = 10),
        current_subject = "alice",
    )
    assert response.thread.forkedFromMessageId == "assistant-1"
    assert response.messages[-1].content == [{"type": "text", "text": "Finished reply"}]


def test_shared_thread_rejects_a_second_subjects_active_generation(chat_home):
    _create()
    with pytest.raises(runs_db.ChatGenerationConflictError, match = "active generation"):
        _create("run-2", owner = "bob")
    assert [run["id"] for run in runs_db.list_active("thread-1")] == ["run-1"]
    assert studio_db.get_chat_message("thread-1", "assistant-run-2") is None


def test_explicit_prune_deletes_terminal_generation_messages(chat_home):
    user = studio_db.get_chat_message("thread-1", "user-1")
    user["attachments"] = [{"id": "file-1", "name": "notes.txt"}]
    studio_db.upsert_chat_message(user)
    _create()
    with pytest.raises(studio_db.ChatMessageProtectedError):
        studio_db.delete_chat_attachment("user-1", "file-1")
    stale_assistant = studio_db.get_chat_message("thread-1", "assistant-1")
    active = studio_db.sync_chat_messages("thread-1", [], prune_missing = True)
    assert {message["id"] for message in active} == {"user-1", "assistant-1"}

    token = runs_db.get_worker_token("run-1")
    assert runs_db.mark_running("run-1", token)
    runs_db.finish_run("run-1", worker_token = token, status = "completed")
    assert studio_db.delete_chat_attachment("user-1", "file-1") is True
    user = studio_db.get_chat_message("thread-1", "user-1")
    assert user.get("attachments") == []

    retained = studio_db.sync_chat_messages("thread-1", [user], prune_missing = True)
    assert {message["id"] for message in retained} == {"user-1", "assistant-1"}
    assert runs_db.get_run("run-1", "alice") is not None

    deleted = studio_db.sync_chat_messages(
        "thread-1",
        [user],
        prune_missing = True,
        deleted_message_ids = {"assistant-1"},
    )
    assert [message["id"] for message in deleted] == ["user-1"]
    assert studio_db.get_chat_message("thread-1", "assistant-1") is None
    assert runs_db.get_run("run-1", "alice") is None

    stale_sync = studio_db.sync_chat_messages(
        "thread-1", [user, stale_assistant], prune_missing = True
    )
    assert [message["id"] for message in stale_sync] == ["user-1"]
    assert studio_db.get_chat_message("thread-1", "assistant-1") is None
    _assert_protected(stale_assistant)


def test_terminal_run_does_not_unprotect_a_message_shared_with_an_active_run(chat_home):
    user = studio_db.get_chat_message("thread-1", "user-1")
    user["attachments"] = [{"id": "file-1", "name": "notes.txt"}]
    studio_db.upsert_chat_message(user)
    _create()
    token = runs_db.get_worker_token("run-1")
    assert runs_db.mark_running("run-1", token)
    runs_db.finish_run("run-1", worker_token = token, status = "completed")
    _create("run-2")

    with pytest.raises(studio_db.ChatMessageProtectedError):
        studio_db.delete_chat_attachment("user-1", "file-1")
    studio_db.sync_chat_messages(
        "thread-1",
        [],
        prune_missing = True,
        deleted_message_ids = {"user-1"},
    )

    assert studio_db.get_chat_message("thread-1", "user-1") is not None
    assert runs_db.get_run("run-2") is not None


def test_generation_message_writes_are_run_bound_and_monotonic(chat_home):
    _create()
    token = runs_db.get_worker_token("run-1")
    assert runs_db.mark_running("run-1", token)
    runs_db.append_events("run-1", token, [("chunk", {"text": "A"})])
    message = studio_db.get_chat_message("thread-1", "assistant-1")
    message["content"] = [{"type": "text", "text": "A"}]
    message["metadata"].update({"generationSeq": 3, "generationStatus": "running"})
    studio_db.upsert_chat_message(message)

    forged_terminal = {
        **message,
        "metadata": {
            **message["metadata"],
            "generationStatus": "completed",
            "generationSettled": True,
        },
    }
    _assert_protected(forged_terminal)
    for metadata in (
        {**message["metadata"], "generationSeq": 2},
        {**message["metadata"], "generationRunId": "other-run"},
    ):
        _assert_protected(
            {**message, "content": [{"type": "text", "text": "stale"}], "metadata": metadata}
        )
    runs_db.finish_run("run-1", worker_token = token, status = "completed", finish_reason = "length")
    stale = {
        **message,
        "content": [{"type": "text", "text": "downgraded"}],
        "metadata": {**message["metadata"], "generationStatus": "running"},
    }
    _assert_protected(stale)
    studio_db.sync_chat_messages("thread-1", [stale])
    stored = studio_db.get_chat_message("thread-1", "assistant-1")
    assert stored["content"] == [{"type": "text", "text": "A"}]
    assert stored["metadata"]["generationStatus"] == "completed"
    stored["metadata"]["generationSettled"] = True
    _assert_protected(stored)
    stored["metadata"].update(
        {"generationSeq": 4, "generationSettled": True, "responseDetails": {"durationMs": 1}}
    )
    studio_db.upsert_chat_message(stored)
    stored["metadata"]["generationSettled"] = False
    _assert_protected(stored)
    authoritative = studio_db.get_chat_message("thread-1", "assistant-1")
    stale = {
        **authoritative,
        "metadata": {
            key: value
            for key, value in authoritative["metadata"].items()
            if key not in {"incomplete", "responseDetails"}
        },
    }
    _assert_protected(stale)
    studio_db.sync_chat_messages("thread-1", [stale])
    preserved = studio_db.get_chat_message("thread-1", "assistant-1")["metadata"]
    assert preserved["incomplete"] == {"reason": "length"}
    assert preserved["responseDetails"] == {"durationMs": 1}


def test_settled_generation_response_can_be_explicitly_edited(chat_home):
    _create()
    token = runs_db.get_worker_token("run-1")
    assert runs_db.mark_running("run-1", token)
    assert runs_db.finish_run("run-1", worker_token = token, status = "completed", finish_reason = "stop")
    run = runs_db.get_run("run-1", "alice")
    stored = studio_db.get_chat_message("thread-1", "assistant-1")
    stored["metadata"].update(
        {
            "generationSeq": run["lastEventSeq"],
            "generationStatus": "completed",
            "generationSettled": True,
        }
    )
    studio_db.upsert_chat_message(stored)
    authoritative = studio_db.get_chat_message("thread-1", "assistant-1")
    edited = {key: value for key, value in authoritative.items() if key != "metadata"}
    edited["content"] = [{"type": "text", "text": "edited"}]

    _assert_protected(edited)
    saved = studio_db.upsert_chat_message(edited, allow_generation_edit = True)
    assert saved["content"] == [{"type": "text", "text": "edited"}]
    assert saved.get("metadata") is None
    assert runs_db.get_run("run-1", "alice") is None
    _assert_protected(authoritative)


def test_explicit_edit_keeps_display_metadata_but_not_the_run_claim(chat_home):
    """An explicit edit keeps display metadata but drops the run claim so the edited turn detaches."""
    _create()
    token = runs_db.get_worker_token("run-1")
    assert runs_db.mark_running("run-1", token)
    streamed = studio_db.get_chat_message("thread-1", "assistant-1")
    streamed["metadata"].update(
        {
            "generationStatus": "running",
            "timing": {"tokensPerSecond": 42.5, "durationMs": 1200},
            "contextUsage": {"promptTokens": 900, "contextLength": 4096},
        }
    )
    studio_db.upsert_chat_message(streamed)
    assert runs_db.finish_run(
        "run-1", worker_token = token, status = "completed", finish_reason = "length"
    )
    run = runs_db.get_run("run-1", "alice")
    settled = studio_db.get_chat_message("thread-1", "assistant-1")
    settled["metadata"].update(
        {
            "generationSeq": run["lastEventSeq"],
            "generationStatus": "completed",
            "generationSettled": True,
        }
    )
    studio_db.upsert_chat_message(settled)
    authoritative = studio_db.get_chat_message("thread-1", "assistant-1")
    assert authoritative["metadata"]["incomplete"] == {"reason": "length"}

    edited = {**authoritative, "content": [{"type": "text", "text": "edited"}]}
    with pytest.raises(studio_db.ChatMessageProtectedError):
        studio_db.upsert_chat_message(edited, allow_generation_edit = True)

    edited["metadata"] = {
        key: value
        for key, value in authoritative["metadata"].items()
        if key
        not in {
            "serverManaged",
            "generationRunId",
            "generationSeq",
            "generationStatus",
            "generationSettled",
        }
    }
    saved = studio_db.upsert_chat_message(edited, allow_generation_edit = True)
    assert saved["content"] == [{"type": "text", "text": "edited"}]
    assert saved["metadata"] == {
        "incomplete": {"reason": "length"},
        "timing": {"tokensPerSecond": 42.5, "durationMs": 1200},
        "contextUsage": {"promptTokens": 900, "contextLength": 4096},
    }
    assert runs_db.get_run("run-1", "alice") is None


@pytest.mark.parametrize(
    "finish_reason, tab_reason",
    [("stop", "empty"), ("length", "context_window")],
)
def test_finished_generation_whose_settle_was_refused_can_still_be_edited(
    chat_home, finish_reason, tab_reason
):
    """#13938: a settle whose `incomplete` disagrees with the producer's is refused, so the row
    never gets generationSettled; the run is finished, so an edit must still detach it."""
    _create()
    token = runs_db.get_worker_token("run-1")
    assert runs_db.mark_running("run-1", token)
    assert runs_db.finish_run(
        "run-1", worker_token = token, status = "completed", finish_reason = finish_reason
    )
    run = runs_db.get_run("run-1", "alice")
    stored = studio_db.get_chat_message("thread-1", "assistant-1")
    settle = json.loads(json.dumps(stored))
    settle["metadata"].update(
        {
            "generationSeq": run["lastEventSeq"],
            "generationStatus": "completed",
            "generationSettled": True,
            "incomplete": {"reason": tab_reason},
        }
    )
    studio_db.sync_chat_messages("thread-1", [settle])
    stored = studio_db.get_chat_message("thread-1", "assistant-1")
    assert stored["metadata"].get("generationSettled") is not True, "the settle landed"

    edited = {key: value for key, value in stored.items() if key != "metadata"}
    edited["content"] = [{"type": "text", "text": "edited"}]
    edited["metadata"] = {"incomplete": {"reason": tab_reason}}
    saved = studio_db.upsert_chat_message(edited, allow_generation_edit = True)
    assert saved["content"] == [{"type": "text", "text": "edited"}]
    assert runs_db.get_run("run-1", "alice") is None
    _assert_protected(settle)


def test_running_generation_still_cannot_be_explicitly_edited(chat_home):
    _create()
    token = runs_db.get_worker_token("run-1")
    assert runs_db.mark_running("run-1", token)
    stored = studio_db.get_chat_message("thread-1", "assistant-1")
    edited = {key: value for key, value in stored.items() if key != "metadata"}
    edited["content"] = [{"type": "text", "text": "edited"}]
    with pytest.raises(studio_db.ChatMessageProtectedError):
        studio_db.upsert_chat_message(edited, allow_generation_edit = True)
    assert runs_db.get_run("run-1", "alice")["status"] == "running"


def test_refreshed_generation_can_be_edited_when_tab_created_at_differs(chat_home):
    """#15004: Refresh/Reload lets create_run insert the assistant row with its own
    created_at. The tab keeps the Date it minted for the bubble, so an edit must still
    detach the finished run."""
    _create()
    token = runs_db.get_worker_token("run-1")
    assert runs_db.mark_running("run-1", token)
    assert runs_db.finish_run("run-1", worker_token = token, status = "completed", finish_reason = "stop")
    stored = studio_db.get_chat_message("thread-1", "assistant-1")
    assert stored["createdAt"] != 1
    edited = {key: value for key, value in stored.items() if key != "metadata"}
    edited["content"] = [{"type": "text", "text": "edited after refresh"}]
    edited["createdAt"] = 1
    edited["metadata"] = {"incomplete": {"reason": "stop"}}
    saved = studio_db.upsert_chat_message(edited, allow_generation_edit = True)
    assert saved["content"] == [{"type": "text", "text": "edited after refresh"}]
    assert runs_db.get_run("run-1", "alice") is None


def test_batched_events_have_gapless_cursor_and_terminal_flush(chat_home):
    run, _created = _create()
    worker_token = runs_db.get_worker_token("run-1")
    assert runs_db.mark_running("run-1", worker_token) is True
    assert runs_db.append_events(
        "run-1", worker_token, [("chunk", {"i": 1}), ("chunk", {"i": 2})]
    ) == [3, 4]
    cancelling = runs_db.request_cancel("run-1", "alice")
    assert cancelling["status"] == "cancelling"
    terminal = runs_db.finish_run(
        "run-1",
        worker_token = worker_token,
        status = "completed",
        finish_reason = "stop",
        pending_events = [("chunk", {"i": 3})],
    )
    assert terminal["status"] == "cancelled"
    events = runs_db.list_events("run-1")
    assert [event["seq"] for event in events] == list(range(1, 8))
    assert [event["payload"].get("i") for event in events if event["type"] == "chunk"] == [1, 2, 3]
    assert runs_db.list_events("run-1", after = 4)[0]["seq"] == 5
    assert runs_db.request_cancel("run-1", "alice")["lastEventSeq"] == 7


def test_batched_events_preserve_receipt_timestamps(chat_home):
    _create()
    worker_token = runs_db.get_worker_token("run-1")
    assert runs_db.mark_running("run-1", worker_token) is True
    runs_db.append_events(
        "run-1",
        worker_token,
        [("chunk", {"i": 1}, 1001), ("chunk", {"i": 2}, 1002)],
    )
    chunks = [event for event in runs_db.list_events("run-1") if event["type"] == "chunk"]
    assert [event["createdAt"] for event in chunks] == [1001, 1002]


def test_stream_batches_do_not_rewrite_studio_db_while_the_keeper_holds_the_wal(chat_home):
    """#9934: each event batch closed the last connection, checkpointing studio.db."""
    _create()
    worker_token = runs_db.get_worker_token("run-1")
    db_path = Path(str(studio_db_path()))
    wal_path = Path(f"{db_path}-wal")

    assert studio_db.open_wal_keeper() is True
    try:
        before = hashlib.sha256(db_path.read_bytes()).hexdigest()
        for index in range(5):
            runs_db.append_events("run-1", worker_token, [("chunk", {"text": str(index)})])
            assert wal_path.is_file()
        assert hashlib.sha256(db_path.read_bytes()).hexdigest() == before
    finally:
        studio_db.close_wal_keeper()
    assert not wal_path.exists()
    assert len(runs_db.list_events("run-1")) >= 5


def test_cancel_before_registration_and_startup_orphan_reconciliation(chat_home):
    queued, _created = _create("queued")
    cancelled = runs_db.request_cancel("queued", "alice")
    assert cancelled["status"] == "cancelled"
    assert runs_db.mark_running("queued", runs_db.get_worker_token("queued")) is False
    orphan, _created = _create("orphan", request = _request(seed = 2))
    assert runs_db.mark_running("orphan", runs_db.get_worker_token("orphan")) is True
    assert runs_db.reconcile_orphaned_runs() == 1
    orphan = runs_db.get_run("orphan", "alice")
    assert (orphan["status"], orphan["finishReason"]) == ("failed", "interrupted")
    assert runs_db.list_events("orphan")[-1]["payload"]["interrupted"] is True


def test_a_stop_in_flight_survives_a_restart_as_a_cancellation(chat_home):
    """A stop in flight across a restart settles as cancelled, not as a backend failure."""
    _create()
    runs_db.mark_running("run-1", runs_db.get_worker_token("run-1"))
    assert runs_db.request_cancel("run-1", "alice")["status"] == "cancelling"

    assert runs_db.reconcile_orphaned_runs() == 1

    run = runs_db.get_run("run-1", "alice")
    assert (run["status"], run["finishReason"]) == ("cancelled", "cancelled")
    assert run["error"] is None
    assert runs_db.list_events("run-1")[-1]["type"] == "run.cancelled"


def test_an_uncancelled_run_still_reconciles_as_interrupted(chat_home):
    """The cancellation branch above must not swallow a genuine restart."""
    _create()
    runs_db.mark_running("run-1", runs_db.get_worker_token("run-1"))

    assert runs_db.reconcile_orphaned_runs() == 1

    run = runs_db.get_run("run-1", "alice")
    assert (run["status"], run["finishReason"]) == ("failed", "interrupted")
    assert run["error"] == "Unsloth restarted during generation"
    assert runs_db.list_events("run-1")[-1]["payload"]["interrupted"] is True


def test_deleted_run_id_is_tombstoned_against_stale_tabs(chat_home):
    _original, _created = _create()
    studio_db.delete_chat_threads(["thread-1"])
    _seed_thread("thread-2", "user-2", "Next", 3)
    with pytest.raises(runs_db.ChatGenerationConflictError, match = "already been used"):
        runs_db.create_run(
            run_id = "run-1",
            owner_subject = "alice",
            thread_id = "thread-2",
            user_message_id = "user-2",
            assistant_message_id = "assistant-2",
            request_payload = _request(seed = 2),
        )
    assert runs_db.get_run("run-1", "alice") is None


_SYNC_USER = {
    "id": "user-1",
    "threadId": "thread-1",
    "role": "user",
    "content": [{"type": "text", "text": "Hello"}],
    "createdAt": 2,
}


def _edited_generated_assistant():
    """Settle a generated reply, edit it, and return the stale pre-edit copy another tab holds."""
    studio_db.upsert_chat_message(
        {
            "id": "assistant-1",
            "threadId": "thread-1",
            "role": "assistant",
            "parentId": "user-1",
            "content": [],
            "createdAt": 3,
            "metadata": {},
        }
    )
    _create()
    token = runs_db.get_worker_token("run-1")
    runs_db.mark_running("run-1", token)
    runs_db.finish_run("run-1", worker_token = token, status = "completed", finish_reason = "stop")

    metadata = dict(studio_db.get_chat_message("thread-1", "assistant-1")["metadata"])
    metadata["generationSeq"] = int(runs_db.get_run("run-1", "alice")["lastEventSeq"])
    metadata["generationSettled"] = True
    settled = {
        "id": "assistant-1",
        "threadId": "thread-1",
        "role": "assistant",
        "parentId": "user-1",
        "content": [{"type": "text", "text": "generated answer"}],
        "createdAt": 3,
        "metadata": metadata,
    }
    studio_db.sync_chat_messages("thread-1", [_SYNC_USER, settled])
    assert (
        studio_db.get_chat_message("thread-1", "assistant-1")["metadata"]["generationSettled"]
        is True
    ), "the settle write did not land, so anything built on it would prove nothing"
    stale_tab_copy = json.loads(json.dumps(settled))

    studio_db.upsert_chat_message(
        {
            "id": "assistant-1",
            "threadId": "thread-1",
            "role": "assistant",
            "parentId": "user-1",
            "content": [{"type": "text", "text": "edited by hand"}],
            "createdAt": 3,
            "metadata": {},
        },
        allow_generation_edit = True,
    )
    assert runs_db.get_run("run-1", "alice") is None, "the edit did not detach the run"
    return stale_tab_copy


def test_a_stale_tab_sync_cannot_prune_an_edited_generated_assistant(chat_home):
    """A stale tab's sync must not prune an edited generated reply, whose run row the edit dropped."""
    stale_tab_copy = _edited_generated_assistant()

    studio_db.sync_chat_messages("thread-1", [_SYNC_USER, stale_tab_copy], prune_missing = True)

    survivor = studio_db.get_chat_message("thread-1", "assistant-1")
    assert survivor is not None, "the stale tab's sync deleted the user's edited message"
    assert survivor["content"] == [{"type": "text", "text": "edited by hand"}]


def test_an_explicit_delete_still_removes_a_detached_generated_assistant(chat_home):
    """The retention above is snapshot-pruning only; naming the id must still delete it."""
    stale_tab_copy = _edited_generated_assistant()

    studio_db.sync_chat_messages(
        "thread-1",
        [_SYNC_USER, stale_tab_copy],
        prune_missing = True,
        deleted_message_ids = ["assistant-1"],
    )
    assert studio_db.get_chat_message("thread-1", "assistant-1") is None


@pytest.mark.parametrize(
    "override,detail",
    [
        ({"provider_id": "external"}, "only for local"),
        ({"rag_scope": {"access_token": "secret"}}, "Credentials"),
        ({"rag_scope": {"signing_key": "secret"}}, "Credentials"),
        ({"rag_scope": {"ssh_key": "secret"}}, "Credentials"),
        ({"rag_scope": {"encryption_key": "secret"}}, "Credentials"),
        ({"rag_scope": {"secret_key": "secret"}}, "Credentials"),
        ({"rag_scope": {"api_token": "secret"}}, "Credentials"),
        (
            {
                "messages": [
                    {"role": "user", "content": "Hello", "extra_content": {"api_key": "secret"}}
                ]
            },
            "Credentials",
        ),
        (
            {"messages": _tool_messages('{"api_key":"secret"}')},
            "Credentials",
        ),
    ],
)
def test_request_sanitization_rejects_nonlocal_or_sensitive_payloads(override, detail):
    with pytest.raises(Exception, match = detail):
        _sanitize_request(_model(**override))


def test_request_sanitization_pins_server_owned_fields():
    sanitized = _sanitize_request(_model(stream = False, cancel_id = "legacy", thread_id = "wrong"))
    assert sanitized["stream"] is True
    assert sanitized["cancel_id"] == "run-1"
    assert sanitized["thread_id"] == "thread-1"


def test_run_automatically_replays_with_the_no_sandbox_gate_armed():
    from models.inference import ChatCompletionRequest

    sanitized = _sanitize_request(_model(permission_mode = "off"))
    assert "confirm_tool_calls" not in sanitized
    replayed = ChatCompletionRequest.model_validate(sanitized)
    assert replayed.permission_mode == "off" and replayed.confirm_tool_calls is False
    assert replayed._off_confirm_opt_out is False


def test_an_explicit_off_opt_out_survives_the_replay():
    from models.inference import ChatCompletionRequest

    sanitized = _sanitize_request(_model(permission_mode = "off", confirm_tool_calls = False))
    replayed = ChatCompletionRequest.model_validate(sanitized)
    assert replayed._off_confirm_opt_out is True


def test_request_sanitization_treats_message_text_as_data():
    sanitized = _sanitize_request(
        _model(messages = [{"role": "user", "content": '{"api_key":"example"}'}])
    )
    assert sanitized["messages"][0]["content"] == '{"api_key":"example"}'


@pytest.mark.parametrize("key", ["key", "lookup_key", "monkey", "hockey", "keyboard"])
def test_request_sanitization_accepts_benign_tool_argument_keys(key):
    arguments = json.dumps({key: "value"})
    sanitized = _sanitize_request(_model(messages = _tool_messages(arguments)))
    assert sanitized["messages"][0]["tool_calls"][0]["function"]["arguments"] == arguments


@pytest.mark.parametrize(
    "key",
    ["api_key", "access_key", "private_key", "secret_key", "signing_key", "ssh_key"],
)
def test_request_sanitization_rejects_known_credential_keys(key):
    arguments = json.dumps({key: "secret"})
    with pytest.raises(Exception, match = "Credentials"):
        _sanitize_request(_model(messages = _tool_messages(arguments)))


@pytest.mark.parametrize("value", ['{"api_key":"secret"', '"{\\"api_key\\":\\"secret\\"}"'])
def test_request_sanitization_scans_json_string_envelopes(value):
    assert _contains_sensitive_key(value) is True


def test_request_sanitization_bounds_nested_envelopes():
    nested = {"value": None}
    for _ in range(100):
        nested = {"value": nested}
    assert _contains_sensitive_key(nested) is True
    assert _contains_sensitive_key("[" * 5000 + "0" + "]" * 5000) is True
    with pytest.raises(Exception, match = "Credentials"):
        _sanitize_request(
            _model(messages = [{"role": "user", "content": "hello", "extra_content": nested}])
        )


@pytest.mark.parametrize("messages", [None, 1, [{"role": "user", "content": None}]])
def test_request_sanitization_returns_json_safe_validation_errors(messages):
    with pytest.raises(Exception) as exc_info:
        _sanitize_request(_model(messages = messages))
    assert exc_info.value.status_code == 422
    json.dumps(exc_info.value.detail)


@pytest.mark.parametrize(
    "overrides",
    [{"provider_id": ""}, {"provider_id": None, "encrypted_api_key": None}, {"tools": []}],
)
def test_request_sanitization_accepts_empty_optional_routing(overrides):
    assert _sanitize_request(_model(**overrides))["stream"] is True


# Tool turns are durable by default; UNSLOTH_STUDIO_DURABLE_TOOL_TURNS=0 restores refusal.


def test_request_sanitization_admits_a_tool_enabled_turn_by_default():
    assert _sanitize_request(_model(tools = [{"type": "function"}]))["stream"] is True
    assert _sanitize_request(_model(enable_tools = True))["stream"] is True


def test_request_sanitization_refuses_a_tool_enabled_turn_when_the_toggle_is_off(monkeypatch):
    monkeypatch.setenv("UNSLOTH_STUDIO_DURABLE_TOOL_TURNS", "0")
    with pytest.raises(Exception, match = "Tool-enabled chat runs use the legacy streaming path"):
        _sanitize_request(_model(tools = [{"type": "function"}]))
    with pytest.raises(Exception, match = "Tool-enabled chat runs use the legacy streaming path"):
        _sanitize_request(_model(enable_tools = True))


def test_a_launcher_default_tool_policy_is_admitted_by_default_and_refused_under_the_toggle(
    monkeypatch,
):
    set_tool_policy_default(True)
    assert _sanitize_request(_model())["stream"] is True
    monkeypatch.setenv("UNSLOTH_STUDIO_DURABLE_TOOL_TURNS", "0")
    with pytest.raises(Exception, match = "Tool-enabled chat runs use the legacy streaming path"):
        _sanitize_request(_model())


def test_a_cli_tools_override_is_admitted_even_when_the_request_disables_tools(monkeypatch):
    set_tool_policy(True)
    assert _sanitize_request(_model(enable_tools = False))["stream"] is True
    monkeypatch.setenv("UNSLOTH_STUDIO_DURABLE_TOOL_TURNS", "0")
    with pytest.raises(Exception, match = "Tool-enabled chat runs use the legacy streaming path"):
        _sanitize_request(_model(enable_tools = False))


def test_a_checkpoint_recall_tool_loop_is_admitted_by_default_and_refused_under_the_toggle(
    monkeypatch,
):
    import routes.inference as inference_routes

    monkeypatch.setattr(
        inference_routes, "_checkpoint_recall_may_enable_tools", lambda request: True, raising = False
    )
    assert _sanitize_request(_model(enable_tools = False))["stream"] is True
    monkeypatch.setenv("UNSLOTH_STUDIO_DURABLE_TOOL_TURNS", "0")
    with pytest.raises(Exception, match = "Tool-enabled chat runs use the legacy streaming path"):
        _sanitize_request(_model(enable_tools = False))


def test_event_cursor_rejects_values_outside_sqlite_integer_range():
    with pytest.raises(Exception, match = "cursor is too large"):
        _event_cursor(10**30, None)
    with pytest.raises(Exception, match = "cursor is too large"):
        _event_cursor(None, str(10**30))
    with pytest.raises(Exception, match = "must be an integer"):
        _event_cursor(None, "²")
    with pytest.raises(Exception, match = "cursor is too large"):
        _event_cursor(None, "9" * 4301)


@pytest.mark.parametrize("field", ["image_base64", "audio_base64", "video_base64"])
def test_request_sanitization_rejects_inline_media(field):
    """Inline media is refused on the durable path, since request_json would persist the base64 blob."""
    with pytest.raises(Exception, match = "legacy streaming path"):
        _sanitize_request(_model(**{field: "iVBORw0KGgo="}))


class TestEmbeddedImagesStayOffTheDurablePath:
    """Nested inline images must be refused too, or every follow-up turn re-persists the blob."""

    _PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="

    def _msg(self, parts):
        from models.inference import ChatMessage
        return ChatMessage(role = "user", content = parts)

    def test_an_inline_data_url_image_in_history_is_detected(self):
        from routes.inference import _messages_have_embedded_image
        messages = [
            self._msg(
                [
                    {"type": "text", "text": "what is this?"},
                    {
                        "type": "image_url",
                        "image_url": {"url": f"data:image/png;base64,{self._PNG}"},
                    },
                ]
            ),
            self._msg([{"type": "text", "text": "and what colour was it?"}]),
        ]
        assert _messages_have_embedded_image(messages) is True

    def test_a_remote_image_url_is_not_treated_as_embedded(self):
        """Deliberately narrower than 'has an image'. A remote URL is a short string and costs
        nothing to persist, so it has no business forcing a turn off the durable path."""
        from routes.inference import _messages_have_embedded_image

        messages = [
            self._msg(
                [
                    {"type": "text", "text": "what is this?"},
                    {"type": "image_url", "image_url": {"url": "https://example.com/cat.png"}},
                ]
            )
        ]
        assert _messages_have_embedded_image(messages) is False

    def test_a_plain_text_thread_is_not_treated_as_media(self):
        """Negative control: if this ever returns True the guard would push every text turn onto
        the legacy path and the feature would silently stop working."""
        from routes.inference import _messages_have_embedded_image

        assert (
            _messages_have_embedded_image([self._msg([{"type": "text", "text": "hello"}])]) is False
        )
        from models.inference import ChatMessage

        assert _messages_have_embedded_image([ChatMessage(role = "user", content = "hello")]) is False


_INLINE_PNG_PART = {
    "type": "image_url",
    "image_url": {
        "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAA"
        "DUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
    },
}


def _image_thread(*follow_up):
    return [
        {"role": "user", "content": [{"type": "text", "text": "what is this?"}, _INLINE_PNG_PART]},
        {"role": "assistant", "content": "A red square."},
        *follow_up,
    ]


def test_a_text_follow_up_to_an_inline_image_is_admitted_without_storing_the_blob():
    from core.inference.chat_generation_runs import detach_embedded_images

    messages = _image_thread(
        {"role": "user", "content": [{"type": "text", "text": "and what colour was it again?"}]}
    )
    sanitized = _sanitize_request(_model(messages = messages))
    images = detach_embedded_images(sanitized)
    url = _INLINE_PNG_PART["image_url"]["url"]
    assert url not in json.dumps(sanitized)
    assert list(images.values()) == [url]


def test_a_different_image_is_a_different_stored_request():
    from core.inference.chat_generation_runs import detach_embedded_images

    follow_up = {"role": "user", "content": "again"}
    first = _sanitize_request(_model(messages = _image_thread(follow_up)))
    other = _image_thread(follow_up)
    other[0]["content"][1] = {
        "type": "image_url",
        "image_url": {"url": "data:image/png;base64,AAAA"},
    }
    second = _sanitize_request(_model(messages = other))
    detach_embedded_images(first)
    detach_embedded_images(second)
    assert first != second


def test_an_image_on_the_latest_turn_stays_legacy():
    messages = [
        {"role": "user", "content": "hello"},
        {"role": "assistant", "content": "hi"},
        {"role": "user", "content": [{"type": "text", "text": "what is this?"}, _INLINE_PNG_PART]},
    ]
    with pytest.raises(Exception, match = "Media chat runs use the legacy streaming path"):
        _sanitize_request(_model(messages = messages))


def test_a_remote_image_url_does_not_force_a_turn_off_the_durable_path():
    """Narrower than 'has an image' on purpose: a remote URL is a short string, so persisting it
    costs nothing and there is no reason to give up durability for it."""
    messages = [
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "what is this?"},
                {"type": "image_url", "image_url": {"url": "https://example.com/cat.png"}},
            ],
        },
    ]
    assert _sanitize_request(_model(messages = messages))["stream"] is True


def test_an_ordinary_text_thread_is_still_admitted():
    """Negative control. If the new guard over-matched, every text turn would fall back to the
    legacy stream and the whole feature would quietly stop working while these tests stayed green."""
    messages = [{"role": "user", "content": [{"type": "text", "text": "hello"}]}]
    assert _sanitize_request(_model(messages = messages))["stream"] is True