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.

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

"""GET /v1/models lists the full server catalog (loaded + locally available)."""

import asyncio
import json
import sys
from pathlib import Path

_BACKEND = Path(__file__).resolve().parents[1]
if str(_BACKEND) not in sys.path:
    sys.path.insert(0, str(_BACKEND))

import routes.inference as inf  # noqa: E402
from core.inference import local_model_resolver as resolver  # noqa: E402
import routes.models as models_mod
import pytest


@pytest.fixture(autouse = True)
def _no_host_default_hf_cache(monkeypatch, tmp_path):
    """/v1/models appends media rows from a scan that walks every known HF cache home, a host
    HF_HOME and the platform default included; keep it off whatever the host has downloaded."""
    from utils import hf_cache_settings

    monkeypatch.delitem(hf_cache_settings._EXPLICIT_CACHE_ENV, "HF_HOME", raising = False)
    monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "xdg"))
    # The scan also reads hf_default_cache_dir() (ignores XDG), the LM Studio / Hermes / Ollama / oMLX dirs and ./models.
    import utils.paths

    monkeypatch.setattr(utils.paths, "hf_default_cache_dir", lambda: tmp_path / "default_hub")
    for name in (
        "lmstudio_model_dirs",
        "hermes_model_dirs",
        "ollama_model_dirs",
        "omlx_model_dirs",
    ):
        monkeypatch.setattr(utils.paths, name, lambda: [])
    monkeypatch.chdir(tmp_path)
    # Module state an earlier test may leave: its media picks and a resident image or video engine.
    monkeypatch.setattr(inf, "_MEDIA_PICK_CACHE", {"at": None, "picks": {}})
    monkeypatch.setattr(inf, "_resident_media_status", lambda task: None)


class _Info:
    def __init__(
        self,
        id,
        display_name,
        model_id = None,
        is_gguf = True,
    ):
        self.id = id
        self.display_name = display_name
        self.model_id = model_id
        self.is_gguf = is_gguf  # drives the files-based GGUF check in the test


class _FakeLlama:
    is_loaded = True
    model_identifier = "/srv/models/Qwen3-Q4.gguf"
    context_length = 4096
    max_context_length = None
    native_context_length = None

    def __init__(self, loaded = True):
        self.is_loaded = loaded


class _FakeUnsloth:
    active_model_name = None
    models: dict = {}
    context_length = None
    max_seq_length = None


def test_catalog_lists_loaded_and_available(monkeypatch):
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: _FakeLlama())
    monkeypatch.setattr(inf, "get_inference_backend", lambda: _FakeUnsloth())

    async def _fake_catalog():
        return [
            _Info("/data/models/Qwen3-Q4.gguf", "Qwen3-Q4"),
            _Info("/data/models/Llama-8B-Q8.gguf", "Llama-8B-Q8"),
            # HF-cache GGUF has model_format unset, so only a files-based check lists it.
            _Info("models--org--Foo", "Foo", model_id = "org/Foo"),
            # Non-GGUF (safetensors/MLX): the orchestrator serves it, so it is listed too.
            _Info("/data/models/Mistral-7B", "Mistral-7B", is_gguf = False),
        ]

    monkeypatch.setattr(inf, "_cached_local_catalog", _fake_catalog)
    # Format and the quant labels come from one on-disk scan; drive both off the flag.
    monkeypatch.setattr(
        resolver,
        "local_servable_model",
        lambda info: (True, ("Q8_0",)) if info.is_gguf else (False, ()),
    )

    data = asyncio.run(inf._openai_catalog_objects())
    ids = {m["id"]: m for m in data}

    assert ids["Qwen3-Q4"]["loaded"] is True
    assert ids["Qwen3-Q4"]["status"] == {"value": "loaded"}
    assert ids["Qwen3-Q4"]["context_length"] == 4096
    assert ids["Llama-8B-Q8"]["loaded"] is False
    assert ids["Llama-8B-Q8"]["status"] == {"value": "unloaded"}
    assert ids["Llama-8B-Q8"]["quant"] == "Q8_0"
    assert ids["org/Foo"]["loaded"] is False
    assert ids["Mistral-7B"]["loaded"] is False
    assert "quant" not in ids["Mistral-7B"]
    assert [m["id"] for m in data].count("Qwen3-Q4") == 1
    blob = json.dumps(data)
    assert ".gguf" not in blob
    assert "/srv/" not in blob
    assert "/data/" not in blob


def test_a_resident_non_gguf_model_is_marked_loaded_and_stays_quantless(monkeypatch):
    # llama-only residency lists the serving model as unloaded; ungated hf_variant stamps a stale quant.
    class _Orchestrator:
        active_model_name = "/data/models/Mistral-7B"
        models: dict = {}
        context_length = None
        max_seq_length = None

    llama = _FakeLlama(loaded = False)
    llama.hf_variant = "Q4_K_M"
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: llama)
    monkeypatch.setattr(inf, "get_inference_backend", lambda: _Orchestrator())

    async def _fake_catalog():
        return [_Info("/data/models/Mistral-7B", "Mistral-7B", is_gguf = False)]

    monkeypatch.setattr(inf, "_cached_local_catalog", _fake_catalog)
    monkeypatch.setattr(resolver, "local_servable_model", lambda info: (False, ()))
    ids = {m["id"]: m for m in asyncio.run(inf._openai_catalog_objects())}
    assert ids["Mistral-7B"]["loaded"] is True
    assert "quant" not in ids["Mistral-7B"]


def test_a_manually_loaded_non_gguf_model_has_one_catalog_row(monkeypatch):
    class _Orchestrator:
        active_model_name = "/srv/lmstudio/mlx-community/Qwen3-8B-4bit"
        models: dict = {}
        context_length = 8192
        max_seq_length = None

    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: _FakeLlama(loaded = False))
    monkeypatch.setattr(inf, "get_inference_backend", lambda: _Orchestrator())

    info = _Info(
        "/srv/lmstudio/mlx-community/Qwen3-8B-4bit",
        "Qwen3-8B-4bit",
        model_id = "mlx-community/Qwen3-8B-4bit",
        is_gguf = False,
    )
    info.path = info.id

    async def _fake_catalog():
        return [info]

    monkeypatch.setattr(inf, "_cached_local_catalog", _fake_catalog)
    monkeypatch.setattr(resolver, "local_servable_model", lambda _info: (False, ()))
    data = asyncio.run(inf._openai_catalog_objects())

    assert data == [
        {
            "id": "Qwen3-8B-4bit",
            "object": "model",
            "created": data[0]["created"],
            "owned_by": "unsloth-studio",
            "context_length": 8192,
            "loaded": True,
            "status": {"value": "loaded"},
        }
    ]


def test_non_gguf_catalog_dedupe_keeps_a_distinct_same_basename_path(monkeypatch):
    class _Orchestrator:
        active_model_name = "/srv/a/publisher/model"
        models: dict = {}
        context_length = None
        max_seq_length = None

    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: _FakeLlama(loaded = False))
    monkeypatch.setattr(inf, "get_inference_backend", lambda: _Orchestrator())

    resident = _Info("/srv/a/publisher/model", "model", model_id = "publisher-a/model", is_gguf = False)
    available = _Info(
        "/srv/b/publisher/model", "model", model_id = "publisher-b/model", is_gguf = False
    )
    resident.path = resident.id
    available.path = available.id

    async def _fake_catalog():
        return [resident, available]

    monkeypatch.setattr(inf, "_cached_local_catalog", _fake_catalog)
    monkeypatch.setattr(resolver, "local_servable_model", lambda _info: (False, ()))
    ids = {entry["id"]: entry for entry in asyncio.run(inf._openai_catalog_objects())}

    assert set(ids) == {"model", "publisher-b/model"}
    assert ids["model"]["loaded"] is True
    assert ids["publisher-b/model"]["loaded"] is False


def test_catalog_lock_is_per_loop():
    # A module-level asyncio.Lock binds to its first loop and can hang others, so the lock is per-loop.
    async def _get():
        return inf._catalog_lock()

    a = asyncio.run(_get())
    b = asyncio.run(_get())
    assert a is not b
    assert not hasattr(inf, "_CATALOG_LOCK")


def test_empty_and_errored_scans_are_cached(monkeypatch):
    # Cache validity keys on timestamp, so empty or errored scans are still cached for the TTL.
    for outcome in ("empty", "error"):
        calls = {"n": 0}

        def _scan(
            _root,
            _outcome = outcome,
            **_kwargs,
        ):
            calls["n"] += 1
            if _outcome == "error":
                raise RuntimeError("scan blew up")
            return []

        monkeypatch.setattr(models_mod, "collect_local_models", _scan)
        monkeypatch.setattr(inf, "_CATALOG_CACHE", {"at": 0.0, "models": []})

        async def _run():
            return [await inf._cached_local_catalog() for _ in range(3)]

        results = asyncio.run(_run())
        assert results == [[], [], []], outcome
        assert calls["n"] == 1, f"{outcome} scan ran {calls['n']}x (TTL not honored)"


def test_catalog_ttl_starts_after_scan_completes(monkeypatch):
    # Timestamp is taken after the scan, else a slow scan leaves the cache born-expired.

    clock = {"t": 1000.0}
    monkeypatch.setattr(inf.time, "monotonic", lambda: clock["t"])
    monkeypatch.setattr(inf, "_CATALOG_CACHE", {"at": 0.0, "models": []})

    calls = {"n": 0}

    def _slow_scan(_root, **_kwargs):
        calls["n"] += 1
        clock["t"] += inf._CATALOG_TTL_S + 10
        return [_Info("/m/A.gguf", "A")]

    monkeypatch.setattr(models_mod, "collect_local_models", _slow_scan)

    async def _run():
        first = await inf._cached_local_catalog()
        second = await inf._cached_local_catalog()
        return first, second

    first, second = asyncio.run(_run())
    assert [i.id for i in first] == ["/m/A.gguf"]
    assert calls["n"] == 1, "TTL started before the scan -> cache born expired, rescanned"


def test_retrieve_loaded_model_skips_catalog_scan(monkeypatch):
    # Retrieving a loaded id must resolve from the loaded set alone, never paying
    # for the filesystem scan that _cached_local_catalog drives.
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: _FakeLlama())
    monkeypatch.setattr(inf, "get_inference_backend", lambda: _FakeUnsloth())

    async def _boom():
        raise AssertionError("catalog scan must not run for a loaded id")

    monkeypatch.setattr(inf, "_cached_local_catalog", _boom)

    model = asyncio.run(inf.openai_retrieve_model("Qwen3-Q4", current_subject = "t"))
    assert model["id"] == "Qwen3-Q4"
    assert model["loaded"] is True
    assert model["status"] == {"value": "loaded"}


def test_cached_local_catalog_offloads_and_caches(monkeypatch):
    # Scan off the loop via asyncio.to_thread and cache it, so bursts do not re-scan.
    calls = {"scan": 0, "threaded": 0}

    def _fake_collect(_root, **_kwargs):
        calls["scan"] += 1
        return [_Info("/data/models/A.gguf", "A")]

    monkeypatch.setattr(models_mod, "collect_local_models", _fake_collect)

    real_to_thread = inf.asyncio.to_thread

    async def _counting_to_thread(fn, *a, **k):
        calls["threaded"] += 1
        return await real_to_thread(fn, *a, **k)

    monkeypatch.setattr(inf.asyncio, "to_thread", _counting_to_thread)
    monkeypatch.setattr(inf, "_CATALOG_CACHE", {"at": 0.0, "models": []})

    async def _run():
        first = await inf._cached_local_catalog()
        second = await inf._cached_local_catalog()
        return first, second

    first, second = asyncio.run(_run())
    assert [i.id for i in first] == ["/data/models/A.gguf"]
    assert second is first or [i.id for i in second] == [i.id for i in first]
    assert calls["scan"] == 1
    assert calls["threaded"] == 1


def test_monitor_active_model_is_a_public_id_not_a_host_path(monkeypatch):
    # The settings UI renders this and --secure serves it publicly, so never a load path.
    class _Llama:
        is_loaded = True
        model_identifier = "/home/me/.cache/huggingface/hub/models--org--A-GGUF/snapshots/abc"
        hf_variant = "UD-Q4_K_XL"
        _openai_advertised_id = "org/A-GGUF"

    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: _Llama())
    assert inf._monitor_active_model() == "org/A-GGUF:UD-Q4_K_XL"


def test_monitor_active_model_cleans_a_path_with_no_advertised_id(monkeypatch):
    class _Llama:
        is_loaded = True
        model_identifier = "/data/models/Llama-8B-Q8.gguf"
        hf_variant = None
        _openai_advertised_id = None

    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: _Llama())
    label = inf._monitor_active_model()
    assert "/" not in label and ".gguf" not in label


def test_monitor_reports_nothing_once_a_non_gguf_model_is_unloaded(monkeypatch):
    # Unload clears active_model_name but not the alias; reading it ungated reported a ghost model.
    class _Orchestrator:
        active_model_name = None
        _openai_advertised_id = "unsloth/Qwen3-MLX"

    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: _FakeLlama(loaded = False))
    monkeypatch.setattr(inf, "_peek_inference_backend", lambda: _Orchestrator())
    assert inf._monitor_active_model() is None


def test_a_non_gguf_model_reports_one_id_across_every_v1_surface(monkeypatch):
    class _Orchestrator:
        active_model_name = "/srv/lmstudio/mlx-community/Qwen3-8B-4bit"
        _openai_advertised_id = "mlx-community/Qwen3-8B-4bit"
        models: dict = {}
        context_length = None
        max_seq_length = None

    orchestrator = _Orchestrator()
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: _FakeLlama(loaded = False))
    monkeypatch.setattr(inf, "get_inference_backend", lambda: orchestrator)

    listed = inf._openai_model_objects()[0]["id"]
    retrieved = asyncio.run(
        inf.openai_retrieve_model(orchestrator.active_model_name, current_subject = "t")
    )
    assert listed == "mlx-community/Qwen3-8B-4bit"
    assert retrieved["id"] == listed and retrieved["loaded"] is True
    assert inf._orchestrator_public_model_id(orchestrator) == listed


def test_lifecycle_label_recovers_the_repo_id_from_an_hf_cache_path():
    snap = "/home/me/.cache/huggingface/hub/models--unsloth--gemma-4-E4B-it-GGUF/snapshots/bfc15c3"
    assert (
        inf._lifecycle_model_label(snap, "UD-Q4_K_XL") == "unsloth/gemma-4-E4B-it-GGUF:UD-Q4_K_XL"
    )


def test_lifecycle_model_label_is_path_free():
    label = inf._lifecycle_model_label("/data/models/Llama-8B-Q8.gguf", "Q8_0")
    assert "/" not in label and ".gguf" not in label
    assert inf._lifecycle_model_label("org/A-GGUF", "Q4_K_M") == "org/A-GGUF:Q4_K_M"
    assert inf._lifecycle_model_label("org/A-GGUF:Q4_K_M", "Q8_0") == "org/A-GGUF:Q4_K_M"


def test_a_standalone_gguf_does_not_advertise_a_quant_that_stops_resolving(monkeypatch):
    # Resolver stores standalone files without quants, so a pinned <stem>:<quant> would 404.
    from core.inference.local_model_resolver import _LocalGgufEntry

    standalone = _LocalGgufEntry("Qwen3-Q4", "/srv/models/Qwen3-Q4.gguf", ())
    repo = _LocalGgufEntry("org/Foo", "/hf/models--org--Foo/snapshots/a", ("Q4_K_M",))
    monkeypatch.setattr(resolver, "_scan", (1.0, {"qwen3-q4": standalone, "org/foo": repo}))
    monkeypatch.setattr(inf, "get_inference_backend", lambda: _FakeUnsloth())

    llama = _FakeLlama()
    llama.hf_variant = "Q4_K_M"
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: llama)
    assert "quant" not in inf._openai_model_objects()[0]

    llama.model_identifier = "org/Foo"
    assert inf._openai_model_objects()[0]["quant"] == "Q4_K_M"

    # A cold index cannot prove a reference; publishing unproven pins hands out ones that fail.
    monkeypatch.setattr(resolver, "_scan", (0.0, {}))
    # Stub the walk: a real multi-root scan can exceed the cold-wait budget and 503 under load.
    monkeypatch.setattr(resolver, "_build_index", lambda: {})
    monkeypatch.setattr(resolver, "warm_index_soon", lambda: None)
    assert "quant" not in inf._openai_model_objects()[0]


def test_a_loaded_alias_advertises_the_quant_that_is_actually_loaded(monkeypatch):
    # Marking the alias loaded while still publishing the preferred on-disk quant said
    # alias:Q4 was loaded while Q8 was serving, and pinning that 404s with switching off.
    monkeypatch.setattr(inf, "get_inference_backend", lambda: _FakeUnsloth())
    llama = _FakeLlama()
    llama.hf_variant = "Q8_0"
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: llama)

    alias = _Info("/srv/models", "Qwen3", model_id = "publisher/Qwen3")
    alias.path = "/srv/models"

    async def _fake_catalog():
        return [alias]

    monkeypatch.setattr(inf, "_cached_local_catalog", _fake_catalog)
    monkeypatch.setattr(resolver, "local_servable_model", lambda info: (True, ("Q4_K_M", "Q8_0")))
    ids = {m["id"]: m for m in asyncio.run(inf._openai_catalog_objects())}
    assert ids["publisher/Qwen3"]["loaded"] is True
    assert ids["publisher/Qwen3"]["quant"] == "Q8_0"


def test_a_nested_model_directory_is_not_the_resident_one(monkeypatch):
    # Indexed models can nest; a plain prefix test marked the outer model resident. Innermost wins.
    outer = _Info("/models/A", "A", model_id = "publisher/A")
    outer.path = "/models/A"
    inner = _Info("/models/A/sub/B", "B", model_id = "publisher/B")
    inner.path = "/models/A/sub/B"
    monkeypatch.setitem(inf._CATALOG_CACHE, "models", [outer, inner])

    llama = _FakeLlama()
    llama.gguf_path = "/models/A/sub/B/model-Q4_K_M.gguf"
    llama.model_identifier = llama.gguf_path
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: llama)
    monkeypatch.setattr(inf, "get_inference_backend", lambda: _FakeUnsloth())

    assert inf._resolves_to_resident("/models/A/sub/B") is True
    assert inf._resolves_to_resident("/models/A") is False
    # Empty index: no nesting to tell apart, so the directory-to-file match must still hold.
    monkeypatch.setitem(inf._CATALOG_CACHE, "models", [])
    assert inf._resolves_to_resident("/models/A") is True


def test_a_transformers_model_does_not_mark_a_gguf_alias_loaded(monkeypatch):
    # A Transformers model in a dir holding GGUF exports is not GGUF; marking it loaded pins bad quants.
    unsloth = _FakeUnsloth()
    unsloth.active_model_name = "/srv/models"
    monkeypatch.setattr(inf, "get_inference_backend", lambda: unsloth)
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: _FakeLlama(loaded = False))

    alias = _Info("/srv/models", "Qwen3", model_id = "publisher/Qwen3")
    alias.path = "/srv/models"

    async def _fake_catalog():
        return [alias]

    monkeypatch.setattr(inf, "_cached_local_catalog", _fake_catalog)
    monkeypatch.setattr(resolver, "local_servable_model", lambda info: (True, ("Q4_K_M",)))
    ids = {m["id"]: m for m in asyncio.run(inf._openai_catalog_objects())}
    assert ids["publisher/Qwen3"]["loaded"] is False


def test_an_alias_for_the_resident_weights_is_not_listed_as_unloaded(monkeypatch):
    # Absolute-path GGUF keys by basename, so id-only dedup would re-emit the alias as not loaded.
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: _FakeLlama())
    monkeypatch.setattr(inf, "get_inference_backend", lambda: _FakeUnsloth())

    alias = _Info("/srv/models", "Qwen3", model_id = "publisher/Qwen3")
    alias.path = "/srv/models"

    async def _fake_catalog():
        return [alias]

    monkeypatch.setattr(inf, "_cached_local_catalog", _fake_catalog)
    monkeypatch.setattr(resolver, "local_servable_model", lambda info: (True, ("Q4_K_M",)))
    ids = {m["id"]: m for m in asyncio.run(inf._openai_catalog_objects())}
    assert ids["publisher/Qwen3"]["loaded"] is True


def test_embeddings_monitor_row_names_the_loaded_model_not_the_client_alias(monkeypatch):
    from types import SimpleNamespace

    import httpx
    from core.inference.api_monitor import ApiMonitor

    class Request:
        state = SimpleNamespace()
        url = SimpleNamespace(path = "/v1/embeddings")
        method = "POST"

        async def json(self):
            return {"input": "hello", "model": "UNSLOTH-bge-m3"}

        async def is_disconnected(self):
            return False

    class Client:
        async def aclose(self):
            return None

        async def post(self, *_args, **_kwargs):
            return httpx.Response(200, json = {"data": [{"embedding": [0.1]}], "usage": {}})

    llama = SimpleNamespace(
        is_loaded = True,
        is_embedding_gguf = True,
        base_url = "http://llama.test",
        context_length = 4096,
        model_identifier = "/cache/models--org--bge-m3/snapshots/abc",
        hf_variant = "q8_0",
        _openai_advertised_id = "bge-m3",
    )
    monitor = ApiMonitor(max_entries = 3)
    monkeypatch.setattr(inf, "api_monitor", monitor)
    monkeypatch.setattr(inf, "_cancelable_nonstreaming_client", Client)
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: llama)
    monkeypatch.setattr(inf, "_should_validate_before_switch", lambda: False)
    monkeypatch.setattr(inf, "_names_studio_embedder", lambda _model: None)

    async def _passthrough(request, _subject, **_kwargs):
        return await request.json()

    monkeypatch.setattr(inf, "_auto_switch_from_request_body", _passthrough)

    response = asyncio.run(inf.openai_embeddings(Request(), current_subject = "test"))
    assert response.status_code == 200
    [entry] = monitor.snapshot()
    assert entry["model"] == "bge-m3:q8_0"


def _pin_setup(
    monkeypatch,
    listed,
    resident_quant = None,
):
    from core.inference.local_model_resolver import _LocalGgufEntry

    loaded = [m for m in listed if m.get("loaded")]

    async def _catalog():
        return listed

    monkeypatch.setattr(inf, "_openai_model_objects", lambda: loaded)
    monkeypatch.setattr(inf, "_openai_catalog_objects", _catalog)
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: _FakeLlama(loaded = bool(loaded)))
    monkeypatch.setattr(inf, "get_inference_backend", lambda: _FakeUnsloth())
    entry = _LocalGgufEntry(
        "publisher/Qwen3", "/hf/models--publisher--Qwen3/snapshots/a", ("Q8_0", "Q4_K_M")
    )
    monkeypatch.setattr(resolver, "_index", lambda: {"publisher/qwen3": entry})


def _retrieve(model_id):
    return asyncio.run(inf.openai_retrieve_model(model_id, current_subject = "t"))


def _base_row(**extra):
    return {
        "id": "publisher/Qwen3",
        "object": "model",
        "created": 1,
        "owned_by": "unsloth-studio",
        **extra,
    }


def test_retrieve_resolves_an_on_disk_quant_pin(monkeypatch):
    _pin_setup(monkeypatch, [_base_row(loaded = False, quant = "Q8_0")])

    model = _retrieve("publisher/qwen3:q4_k_m")
    assert model["id"] == "publisher/Qwen3:Q4_K_M"
    assert model["quant"] == "Q4_K_M"
    assert model["loaded"] is False
    # Open WebUI's llama.cpp provider calls a row without status loaded.
    assert model["status"] == {"value": "unloaded"}


def test_retrieve_pin_of_the_resident_quant_keeps_its_loaded_fields(monkeypatch):
    _pin_setup(monkeypatch, [_base_row(loaded = True, quant = "Q8_0", context_length = 4096)])

    model = _retrieve("publisher/Qwen3:Q8_0")
    assert model["loaded"] is True
    assert model["context_length"] == 4096
    other = _retrieve("publisher/Qwen3:Q4_K_M")
    assert other["loaded"] is False
    assert "context_length" not in other


def test_retrieve_pin_of_the_resident_quant_on_a_cold_index(monkeypatch):
    _pin_setup(monkeypatch, [_base_row(loaded = True, context_length = 4096)])
    llama = _FakeLlama()
    llama.hf_variant = "Q8_0"
    llama.model_identifier = "publisher/Qwen3"
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: llama)

    model = _retrieve("publisher/Qwen3:Q8_0")
    assert model["loaded"] is True
    assert model["quant"] == "Q8_0"
    assert _retrieve("publisher/Qwen3:Q4_K_M")["loaded"] is False


def test_retrieve_pin_reads_residency_from_the_slot_holding_the_model(monkeypatch):
    _pin_setup(monkeypatch, [_base_row(loaded = True, context_length = 4096)])
    primary, extra = _FakeLlama(), _FakeLlama()
    primary.model_identifier, primary.hf_variant = "other/Model", "Q4_K_M"
    extra.model_identifier, extra.hf_variant = "publisher/Qwen3", "Q8_0"
    current = {"backend": primary}

    def _in_slot(slot, fn):
        current["backend"] = extra if slot is not None else primary
        try:
            return fn()
        finally:
            current["backend"] = primary

    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: current["backend"])
    monkeypatch.setattr(inf.model_slots, "visible", lambda: ["extra"])
    monkeypatch.setattr(inf.model_slots, "in_slot", _in_slot)

    assert _retrieve("publisher/Qwen3:Q8_0")["loaded"] is True
    assert _retrieve("publisher/Qwen3:Q4_K_M")["loaded"] is False


def test_retrieve_pin_accepts_a_legacy_label_and_keeps_the_task(monkeypatch):
    from core.inference.local_model_resolver import _LocalGgufEntry

    _pin_setup(monkeypatch, [_base_row(loaded = False, quant = "Q8_0", task = "text-to-speech")])
    entry = _LocalGgufEntry(
        "publisher/Qwen3",
        "/hf/models--publisher--Qwen3/snapshots/a",
        ("Q8_0", "Q4_K_M"),
        aliases = (("bf16", "Q4_K_M"),),
    )
    monkeypatch.setattr(resolver, "_index", lambda: {"publisher/qwen3": entry})

    model = _retrieve("publisher/Qwen3:BF16")
    assert model["id"] == "publisher/Qwen3:Q4_K_M"
    assert model["task"] == "text-to-speech"


def test_retrieve_pin_404s_when_the_quant_or_model_is_not_listed(monkeypatch):
    import pytest
    from fastapi import HTTPException

    _pin_setup(monkeypatch, [_base_row(loaded = False, quant = "Q8_0")])
    for missing in ("publisher/Qwen3:Q2_K", "publisher/Qwen3:latest", "other/Model:Q8_0"):
        with pytest.raises(HTTPException) as err:
            _retrieve(missing)
        assert err.value.status_code == 404, missing

    # Indexed but not in this caller's listing (e.g. hidden from a managed account).
    _pin_setup(monkeypatch, [])
    with pytest.raises(HTTPException):
        _retrieve("publisher/Qwen3:Q4_K_M")


def _multi_quant_listing(
    monkeypatch,
    llama,
    quants = ("Q8_0", "Q4_K_M"),
):
    from core.inference.local_model_resolver import _LocalGgufEntry

    repo = _Info("/hf/models--publisher--Qwen3", "Qwen3", model_id = "publisher/Qwen3")
    repo.path = "/hf/models--publisher--Qwen3"
    single = _Info("/hf/models--org--Foo", "Foo", model_id = "org/Foo")
    single.path = "/hf/models--org--Foo"
    mistral = _Info("/data/models/Mistral-7B", "Mistral-7B", is_gguf = False)
    mistral.path = "/data/models/Mistral-7B"

    async def _fake_catalog():
        return [repo, single, mistral]

    def _servable(info):
        if not info.is_gguf:
            return (False, ())
        return (True, quants) if info is repo else (True, ("Q4_K_M",))

    monkeypatch.setattr(inf, "_cached_local_catalog", _fake_catalog)
    monkeypatch.setattr(resolver, "local_servable_model", _servable)
    monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: llama)
    monkeypatch.setattr(inf, "get_inference_backend", lambda: _FakeUnsloth())
    monkeypatch.setattr(inf, "_peek_inference_backend", lambda: None)
    _index_holding(monkeypatch, quants)


def _index_holding(monkeypatch, quants):
    from core.inference.local_model_resolver import _LocalGgufEntry

    index = {
        "publisher/qwen3": _LocalGgufEntry(
            "publisher/Qwen3", "/hf/models--publisher--Qwen3/snapshots/a", quants
        )
    }
    monkeypatch.setattr(resolver, "_scan", (1.0, index))
    monkeypatch.setattr(resolver, "_index", lambda: index)
    monkeypatch.setattr(resolver, "warm_index_soon", lambda: None)


def _listed():
    return asyncio.run(inf.openai_list_models(current_subject = "t"))["data"]


def test_list_names_every_on_disk_quant_of_a_multi_quant_repo(monkeypatch):
    # #9340: dropdown-only clients cannot type ":Q4_K_M", so each quant needs its own row.
    _multi_quant_listing(monkeypatch, _FakeLlama(loaded = False))
    ids = [m["id"] for m in _listed()]
    at = ids.index("publisher/Qwen3")
    assert ids[at : at + 3] == ["publisher/Qwen3", "publisher/Qwen3:Q8_0", "publisher/Qwen3:Q4_K_M"]
    # One quant on disk, or no quant at all: the bare id already names it.
    assert not [i for i in ids if i.startswith(("org/Foo:", "Mistral-7B:"))]
    rows = {m["id"]: m for m in _listed()}
    assert rows["publisher/Qwen3"]["quant"] == "Q8_0"
    assert rows["publisher/Qwen3:Q4_K_M"] == {
        "id": "publisher/Qwen3:Q4_K_M",
        "object": "model",
        "created": rows["publisher/Qwen3"]["created"],
        "owned_by": "unsloth-studio",
        "display_name": "Qwen3",
        "loaded": False,
        "status": {"value": "unloaded"},
        "quant": "Q4_K_M",
    }
    # Every listed pin retrieves as the same object, so validating clients accept it.
    # Less "created": each call stamps its own second.
    for mid in ("publisher/Qwen3:Q8_0", "publisher/Qwen3:Q4_K_M"):
        assert {**_retrieve(mid), "created": 0} == {**rows[mid], "created": 0}
    # Internal callers (404 messages, retrieve) keep the bare catalog.
    assert "publisher/Qwen3:Q8_0" not in {
        m["id"] for m in asyncio.run(inf._openai_catalog_objects())
    }


def test_list_marks_only_the_resident_quant_pin_loaded(monkeypatch):
    llama = _FakeLlama()
    llama.model_identifier = "publisher/Qwen3"
    llama.hf_variant = "Q4_K_M"
    _multi_quant_listing(monkeypatch, llama)
    monkeypatch.setattr(inf, "_llama_public_model_id", lambda _b: "publisher/Qwen3")
    rows = {m["id"]: m for m in _listed()}
    assert rows["publisher/Qwen3"]["loaded"] is True
    assert rows["publisher/Qwen3:Q4_K_M"]["loaded"] is True
    assert rows["publisher/Qwen3:Q4_K_M"]["context_length"] == 4096
    assert rows["publisher/Qwen3:Q8_0"]["loaded"] is False
    assert "context_length" not in rows["publisher/Qwen3:Q8_0"]
    assert [m["id"] for m in _listed()].count("publisher/Qwen3:Q4_K_M") == 1


def test_list_skips_a_quant_pin_the_resolver_would_not_honour(monkeypatch):
    _multi_quant_listing(monkeypatch, _FakeLlama(loaded = False))
    _index_holding(monkeypatch, ("Q8_0",))
    ids = [m["id"] for m in _listed()]
    assert "publisher/Qwen3:Q8_0" in ids
    assert "publisher/Qwen3:Q4_K_M" not in ids


def test_list_builds_the_index_for_quant_pins_only_when_it_never_was(monkeypatch):
    # The index TTL is 5 s; rebuilding it per listing is the multi-root scan the catalog caches avoid.
    _multi_quant_listing(monkeypatch, _FakeLlama(loaded = False))
    monkeypatch.setattr(resolver, "_index", lambda: pytest.fail("listing rebuilt the index"))
    assert "publisher/Qwen3:Q4_K_M" in [m["id"] for m in _listed()]

    # Build the index when unbuilt, or a client that lists once at connect never sees a pin.
    built = resolver._scan[1]
    monkeypatch.setattr(resolver, "_scan", (0.0, {}))
    monkeypatch.setattr(resolver, "_index", lambda: built)
    assert "publisher/Qwen3:Q4_K_M" in [m["id"] for m in _listed()]