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.

20.3 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

"""A cached GGUF still runs for its owner when the Hub refuses the repo; other callers stay refused."""

from pathlib import Path
from types import SimpleNamespace

import huggingface_hub
import pytest

import core.inference.llama_cpp as llama_cpp
import hub.utils.hf_tokens as hf_tokens
import utils.hf_cache_settings as hf_cache_settings
import utils.models.model_config as mc
from hub.utils.hf_tokens import (
    AmbientAuthorizedToken,
    collecting_hub_token_rejections,
    hub_refused_cached_copy_warning,
)
from utils.models.model_config import GgufRepoUnreadableError, ModelConfig

REPO = "unsloth/Qwen3-0.6B-GGUF"
VARIANT = "UD-Q4_K_XL"
GGUF = f"Qwen3-0.6B-{VARIANT}.gguf"
REV = "a" * 40


class RepositoryNotFoundError(Exception):
    """Matched by type name, as detect_gguf_model_remote does."""

    def __init__(
        self,
        message,
        status_code = 401,
    ):
        super().__init__(message)
        self.response = SimpleNamespace(status_code = status_code, headers = {})


class GatedRepoError(RepositoryNotFoundError):
    pass


@pytest.fixture(autouse = True)
def _isolated(tmp_path, monkeypatch):
    monkeypatch.delenv("HF_HUB_OFFLINE", raising = False)
    monkeypatch.delenv("TRANSFORMERS_OFFLINE", raising = False)
    monkeypatch.setattr(
        hf_cache_settings, "get_hf_cache_paths", lambda: SimpleNamespace(hub_cache = tmp_path)
    )
    monkeypatch.setattr(mc.time, "sleep", lambda *_: None)
    monkeypatch.setattr(hf_tokens, "_ambient_hf_token", lambda: (True, None))
    monkeypatch.setattr(
        llama_cpp.LlamaCppBackend,
        "_find_llama_server_binary",
        staticmethod(lambda *, include_denied = False: "/fake/llama-server"),
    )
    monkeypatch.setattr(mc, "is_vision_model", lambda *a, **k: False)
    monkeypatch.setattr(mc, "detect_audio_type", lambda *a, **k: None)
    monkeypatch.setattr(mc, "is_model_cached", lambda *a, **k: False)
    import hub.utils.gguf_sources as gguf_sources

    monkeypatch.setattr(gguf_sources, "gguf_cache_snapshots", lambda repo: [])
    from collections import OrderedDict

    from hub.services.models import gguf_variants

    monkeypatch.setattr(gguf_variants, "_VARIANT_REQUIREMENT_LAST_KNOWN", OrderedDict())
    monkeypatch.setattr(gguf_variants, "_VARIANT_REQUIREMENT_FORGOTTEN", False)
    return tmp_path


def _snapshot(root: Path) -> Path:
    snapshot = root / f"models--{REPO.replace('/', '--')}" / "snapshots" / REV
    snapshot.mkdir(parents = True, exist_ok = True)
    refs = snapshot.parent.parent / "refs"
    refs.mkdir(exist_ok = True)
    (refs / "main").write_text(REV)
    return snapshot


def _download(root: Path, *names: str) -> Path:
    snapshot = _snapshot(root)
    for name in names:
        path = snapshot / name
        path.parent.mkdir(parents = True, exist_ok = True)
        path.write_bytes(b"GGUF" + b"\0" * 64)
    return snapshot


def _hub(behaviour):
    def model_info(repo_id, **kwargs):
        if isinstance(behaviour, BaseException):
            raise behaviour
        return SimpleNamespace(siblings = [SimpleNamespace(rfilename = f) for f in behaviour])

    return model_info


def _refuse(monkeypatch, error = None):
    monkeypatch.setattr(
        huggingface_hub,
        "model_info",
        _hub(error if error is not None else RepositoryNotFoundError("401 Client Error")),
    )


def _load(**kwargs):
    with collecting_hub_token_rejections() as rejections:
        config = ModelConfig.from_identifier(REPO, **kwargs)
    return config, rejections


@pytest.mark.parametrize(
    "error",
    [
        RepositoryNotFoundError("401 Client Error"),
        RepositoryNotFoundError("404 Client Error", status_code = 404),
        GatedRepoError("403 Client Error", status_code = 403),
    ],
    ids = ["401", "404", "gated"],
)
@pytest.mark.parametrize("variant", [VARIANT, None], ids = ["variant", "auto"])
def test_owner_runs_the_downloaded_copy_when_the_hub_refuses(
    monkeypatch, _isolated, error, variant
):
    snapshot = _download(_isolated, GGUF)
    _refuse(monkeypatch, error)
    config, rejections = _load(gguf_variant = variant, owner_session = True)
    assert config.is_gguf
    assert config.gguf_hf_repo is None, "must not go back to the Hub for the weights"
    assert Path(config.gguf_file) == snapshot / GGUF
    assert config.identifier == REPO
    assert config.gguf_variant == VARIANT
    assert rejections.served_from_cache == [REPO]


@pytest.mark.parametrize(
    "token",
    [None, AmbientAuthorizedToken("hf_" + "u" * 34)],
    ids = ["ambient", "ui_saved_token"],
)
def test_the_owner_session_is_authorized_with_or_without_a_saved_token(
    monkeypatch, _isolated, token
):
    _download(_isolated, GGUF)
    _refuse(monkeypatch)
    config, _ = _load(hf_token = token, gguf_variant = VARIANT, owner_session = True)
    assert config.is_gguf and config.gguf_file


def test_the_warning_names_the_repo():
    warning = hub_refused_cached_copy_warning(REPO)
    assert REPO in warning and "downloaded" in warning and "updates" in warning


def test_a_split_copy_missing_a_shard_is_refused(monkeypatch, _isolated):
    _download(_isolated, f"{VARIANT}/Qwen3-0.6B-{VARIANT}-00001-of-00002.gguf")
    _refuse(monkeypatch)
    with pytest.raises(GgufRepoUnreadableError):
        _load(gguf_variant = VARIANT, owner_session = True)


def test_a_copy_its_manifest_calls_incomplete_is_refused(monkeypatch, _isolated):
    _download(_isolated, GGUF)
    import hub.utils.gguf_sources as gguf_sources

    monkeypatch.setattr(
        gguf_sources, "cached_gguf_manifest_complete", lambda repo, quant, snapshot: False
    )
    _refuse(monkeypatch)
    with pytest.raises(GgufRepoUnreadableError):
        _load(gguf_variant = VARIANT, owner_session = True)


def test_only_a_projector_on_disk_is_refused(monkeypatch, _isolated):
    _download(_isolated, "mmproj-F16.gguf")
    _refuse(monkeypatch)
    with pytest.raises(GgufRepoUnreadableError):
        _load(owner_session = True)


@pytest.mark.parametrize(
    "token",
    [False, None, "hf_" + "k" * 34],
    ids = ["anonymous", "api_key_without_token", "api_key_with_token"],
)
def test_callers_the_cache_rules_refuse_stay_refused(monkeypatch, _isolated, token):
    # owner_session=False (API key or managed account): no token means anonymous, not the installation's.
    _download(_isolated, GGUF)
    _refuse(monkeypatch)
    monkeypatch.setattr(
        hf_tokens,
        "_explicit_token_reaches_repo",
        lambda repo, token, repo_type, offline = False: False,
    )
    with pytest.raises(GgufRepoUnreadableError):
        _load(hf_token = token, gguf_variant = VARIANT)


@pytest.mark.parametrize(
    "verdict", [True, None], ids = ["cached_auth_check_yes", "probe_cannot_answer"]
)
def test_a_token_the_hub_just_refused_is_not_outranked_by_the_cache_rule(
    monkeypatch, _isolated, verdict
):
    # A cached /auth-check yes or a timed-out probe must not let another caller read a refused repo.
    _download(_isolated, GGUF)
    _refuse(monkeypatch, GatedRepoError("403 Client Error", status_code = 403))
    monkeypatch.setattr(
        hf_tokens,
        "_explicit_token_reaches_repo",
        lambda repo, token, repo_type, offline = False: verdict,
    )
    monkeypatch.setattr(hf_tokens, "_caller_populated_the_cache", lambda *a, **k: True)
    monkeypatch.setattr(hf_tokens, "_repo_present_on_disk", lambda *a, **k: True)
    for _ in range(2):
        with pytest.raises(GgufRepoUnreadableError):
            _load(hf_token = "hf_" + "k" * 34, gguf_variant = VARIANT, owner_session = False)


def test_a_revoked_grant_is_refused_even_for_a_managed_accounts_token(monkeypatch, _isolated):
    _download(_isolated, GGUF)
    _refuse(monkeypatch)
    monkeypatch.setattr(
        hf_tokens,
        "_explicit_token_reaches_repo",
        lambda repo, token, repo_type, offline = False: False,
    )
    with pytest.raises(GgufRepoUnreadableError):
        _load(hf_token = "hf_" + "m" * 34, gguf_variant = VARIANT, owner_session = False)


def test_timeouts_keep_todays_cache_route(monkeypatch, _isolated):
    _download(_isolated, GGUF)
    monkeypatch.setattr(huggingface_hub, "model_info", _hub(TimeoutError("read timed out")))
    monkeypatch.setattr(
        mc,
        "list_gguf_variants",
        lambda identifier, hf_token = None: (
            [mc.GgufVariantInfo(filename = GGUF, quant = VARIANT, size_bytes = 1)],
            False,
        ),
    )
    config, rejections = _load(gguf_variant = VARIANT, owner_session = True)
    assert config.is_gguf and config.gguf_hf_repo == REPO
    assert rejections.served_from_cache == []


def test_a_healthy_hub_is_unchanged(monkeypatch, _isolated):
    _download(_isolated, GGUF)
    monkeypatch.setattr(huggingface_hub, "model_info", _hub([GGUF, "README.md"]))
    monkeypatch.setattr(
        mc,
        "list_gguf_variants",
        lambda identifier, hf_token = None: (
            [mc.GgufVariantInfo(filename = GGUF, quant = VARIANT, size_bytes = 1)],
            False,
        ),
    )
    config, rejections = _load(gguf_variant = VARIANT, owner_session = True)
    assert config.is_gguf and config.gguf_hf_repo == REPO
    assert rejections.served_from_cache == []


def test_an_uncached_refused_repo_still_raises_the_clear_error(monkeypatch, _isolated):
    _refuse(monkeypatch)
    with pytest.raises(GgufRepoUnreadableError, match = "HTTP 401"):
        _load(gguf_variant = VARIANT, owner_session = True)


def test_the_load_response_carries_the_cached_copy_warning():
    from models.inference import LoadResponse
    from routes.inference import _with_token_rejected_warning

    response = LoadResponse(status = "loaded", model = REPO, display_name = REPO, inference = {})
    with collecting_hub_token_rejections() as rejections:
        assert _with_token_rejected_warning(response, rejections) is response
        rejections.served_from_cache += [REPO, REPO]
        warned = _with_token_rejected_warning(response, rejections)
    assert warned.memory_warning == hub_refused_cached_copy_warning(REPO)


@pytest.mark.parametrize(
    "managed,api_key,expected",
    [(False, False, True), (False, True, False), (True, False, False)],
    ids = ["owner_ui", "api_key", "managed_account"],
)
def test_only_the_owners_own_session_counts_as_the_owner(monkeypatch, managed, api_key, expected):
    import routes.inference as inference

    monkeypatch.setattr(inference.account_access, "managed_account", lambda: managed)
    monkeypatch.setattr(inference, "_request_has_api_key", lambda request: api_key)
    assert inference._owner_session(object()) is expected
    assert inference._owner_session(None) is False


def test_an_internal_workflow_key_is_not_the_owner(monkeypatch):
    # Data-recipe subprocesses hold an internal sk-unsloth key, never the owner's session.
    import routes.inference as inference

    monkeypatch.setattr(inference.account_access, "managed_account", lambda: False)
    monkeypatch.setattr(inference, "_request_api_key_token", lambda request: "sk-unsloth-internal")
    monkeypatch.setattr(inference.auth_storage, "is_internal_api_key", lambda token: True)
    assert inference._request_used_api_key(object()) is False
    assert inference._owner_session(object()) is False


def test_a_cancelled_download_is_not_run_when_the_hub_refuses(monkeypatch, _isolated):
    _download(_isolated, GGUF)
    import hub.utils.gguf_sources as gguf_sources

    monkeypatch.setattr(
        gguf_sources, "cached_gguf_source_partial", lambda repo, quant, snapshot: True
    )
    _refuse(monkeypatch)
    with pytest.raises(GgufRepoUnreadableError):
        _load(gguf_variant = VARIANT, owner_session = True)


def test_auto_selection_falls_back_to_the_next_complete_variant(monkeypatch, _isolated):
    snapshot = _download(
        _isolated,
        f"{VARIANT}/Qwen3-0.6B-{VARIANT}-00001-of-00002.gguf",
        "Qwen3-0.6B-Q8_0.gguf",
    )
    _refuse(monkeypatch)
    config, rejections = _load(owner_session = True)
    assert Path(config.gguf_file) == snapshot / "Qwen3-0.6B-Q8_0.gguf"
    assert config.gguf_variant == "Q8_0"
    assert rejections.served_from_cache == [REPO]


def test_the_refusal_fallback_passes_the_snapshot_root_as_the_companion_root(
    monkeypatch, _isolated
):
    monkeypatch.setattr(
        hf_cache_settings,
        "get_hf_cache_paths",
        lambda: SimpleNamespace(hub_cache = _isolated, source = "environment"),
    )
    snapshot = _download(_isolated, f"distilled/Qwen3-0.6B-{VARIANT}.gguf")
    _refuse(monkeypatch)
    seen = []
    real = ModelConfig.from_identifier.__func__

    def spy(cls, model_id, *args, **kwargs):
        if model_id != REPO:
            seen.append(kwargs.get("gguf_companion_roots"))
        return real(cls, model_id, *args, **kwargs)

    monkeypatch.setattr(ModelConfig, "from_identifier", classmethod(spy))
    config, _ = _load(owner_session = True)
    assert config.is_gguf
    assert seen == [(str(snapshot),)]


def test_a_repo_not_named_gguf_still_runs_its_downloaded_gguf(monkeypatch, _isolated):
    monkeypatch.setattr(mc, "_looks_like_gguf_repo", lambda *a, **k: False)
    snapshot = _download(_isolated, GGUF)
    _refuse(monkeypatch)
    config, rejections = _load(gguf_variant = VARIANT, owner_session = True)
    assert Path(config.gguf_file) == snapshot / GGUF
    assert rejections.served_from_cache == [REPO]


def test_auto_selection_finds_a_complete_variant_in_an_older_snapshot(monkeypatch, _isolated):
    import os

    older = _download(_isolated, "Qwen3-0.6B-Q8_0.gguf")
    newer = older.parent / ("b" * 40)
    (newer / VARIANT).mkdir(parents = True)
    (newer / VARIANT / f"Qwen3-0.6B-{VARIANT}-00001-of-00002.gguf").write_bytes(
        b"GGUF" + b"\0" * 64
    )
    (older.parent.parent / "refs" / "main").write_text("b" * 40)
    os.utime(older, (1_000_000_000, 1_000_000_000))
    _refuse(monkeypatch)
    config, rejections = _load(owner_session = True)
    assert Path(config.gguf_file) == older / "Qwen3-0.6B-Q8_0.gguf"
    assert config.gguf_variant == "Q8_0"
    assert rejections.served_from_cache == [REPO]


def test_the_downloaded_copy_keeps_its_repo_for_the_download_interlock(monkeypatch, _isolated):
    # Not gguf_hf_repo, which would fetch from the Hub.
    _download(_isolated, GGUF)
    _refuse(monkeypatch)
    config, _ = _load(gguf_variant = VARIANT, owner_session = True)
    assert config.gguf_hf_repo is None
    assert config.gguf_cache_repo == REPO


def test_a_missing_llama_server_fails_before_the_cached_copy_is_returned(monkeypatch, _isolated):
    _download(_isolated, GGUF)
    _refuse(monkeypatch)
    monkeypatch.setattr(
        llama_cpp.LlamaCppBackend,
        "_find_llama_server_binary",
        staticmethod(lambda *, include_denied = False: None),
    )
    with pytest.raises(llama_cpp.LlamaServerNotFoundError):
        _load(gguf_variant = VARIANT, owner_session = True)


@pytest.mark.parametrize("listed", ["root_first", "subdir_first"])
def test_auto_selection_prefers_the_root_checkpoint_whatever_the_listing_order(
    monkeypatch, _isolated, listed
):
    root, sub = "Qwen3-0.6B-Q6_K.gguf", "distilled/Qwen3-0.6B-Q6_K.gguf"
    snapshot = _download(_isolated, root, sub)
    order = [root, sub] if listed == "root_first" else [sub, root]
    monkeypatch.setattr(mc, "_hf_cache_main_gguf_files", lambda repo, **k: list(order))
    _refuse(monkeypatch)
    config, _ = _load(owner_session = True)
    assert Path(config.gguf_file) == snapshot / root


def test_auto_selection_finds_a_copy_in_a_remembered_cache(
    monkeypatch, _isolated, tmp_path_factory
):
    import hub.utils.gguf_sources as gguf_sources

    remembered = _download(tmp_path_factory.mktemp("previous_cache"), GGUF)
    monkeypatch.setattr(
        gguf_sources,
        "gguf_cache_snapshots",
        lambda repo: [remembered] if repo == REPO else [],
    )
    _refuse(monkeypatch)
    config, _ = _load(owner_session = True)
    assert Path(config.gguf_file) == remembered / GGUF
    assert config.gguf_variant == VARIANT


def test_the_downloaded_copy_is_reported_as_a_hub_model(monkeypatch, _isolated):
    # is_local_model drives the chat guidance: Hub recovery vs 'put the drafter beside your file'.
    _download(_isolated, GGUF)
    _refuse(monkeypatch)
    config, _ = _load(gguf_variant = VARIANT, owner_session = True)
    assert config.is_local is False


@pytest.mark.parametrize("projector_on_disk", [False, True])
def test_a_companion_an_earlier_listing_named_must_be_on_disk(
    monkeypatch, _isolated, projector_on_disk
):
    from hub.services.models import gguf_variants
    from hub.utils import download_manifest

    snapshot = _download(_isolated, GGUF)
    if projector_on_disk:
        (snapshot / "mmproj-F16.gguf").write_bytes(b"GGUF" + b"\0" * 60)
    requirement = SimpleNamespace(
        expected_files = (
            download_manifest.ExpectedFile(GGUF, (snapshot / GGUF).stat().st_size),
            download_manifest.ExpectedFile("mmproj-F16.gguf", 64),
        )
    )
    gguf_variants._variant_requirement_cache_set_many(REPO, None, {VARIANT: requirement})
    _refuse(monkeypatch)
    for variant in (VARIANT, None):
        if projector_on_disk:
            config, _ = _load(gguf_variant = variant, owner_session = True)
            assert Path(config.gguf_file) == snapshot / GGUF
        else:
            with pytest.raises(GgufRepoUnreadableError):
                _load(gguf_variant = variant, owner_session = True)


def test_only_the_load_that_shows_the_warning_may_use_the_cached_copy(monkeypatch):
    # Auto-switch, idle restore and preview drop the response, so only /load enables the fallback.
    import asyncio

    import routes.inference as inference

    seen = []

    async def impl(*args, **kwargs):
        seen.append(inference._load_warnings_reach_user.get())
        return None

    monkeypatch.setattr(inference, "_load_model_impl", impl)
    from models.inference import LoadRequest

    request = LoadRequest(model_path = REPO)
    asyncio.run(inference._run_tracked_load_model_impl(request, object(), "subject"))
    assert seen == [True]
    assert inference._load_warnings_reach_user.get() is False


@pytest.mark.parametrize("status", [403, 404])
def test_a_refusal_known_only_by_its_status_takes_the_refused_repo_policy(
    monkeypatch, _isolated, status
):
    # Not retried via the ordinary cache route, where an API key would get the copy silently.
    class DisabledRepoError(Exception):
        def __init__(self):
            super().__init__(f"{status} Client Error")
            self.response = SimpleNamespace(status_code = status, headers = {})

    _download(_isolated, GGUF)
    _refuse(monkeypatch, DisabledRepoError())
    config, rejections = _load(gguf_variant = VARIANT, owner_session = True)
    assert rejections.served_from_cache == [REPO]
    with pytest.raises(GgufRepoUnreadableError):
        _load(hf_token = "hf_" + "k" * 34, gguf_variant = VARIANT, owner_session = False)


def test_status_keeps_the_cached_copy_warning_the_load_carried(monkeypatch):
    # The client re-applies /status after load, so the warning must also live there.
    import routes.inference as inference_routes
    from models.inference import LoadResponse

    backend = SimpleNamespace(last_load_warning = "Runs partly from disk.", hub_access_warning = None)
    monkeypatch.setattr(inference_routes, "get_llama_cpp_backend", lambda: backend)
    response = LoadResponse(
        status = "loaded",
        model = REPO,
        display_name = REPO,
        inference = {},
        is_gguf = True,
        memory_warning = backend.last_load_warning,
    )
    with collecting_hub_token_rejections() as rejections:
        rejections.served_from_cache.append(REPO)
        warned = inference_routes._with_token_rejected_warning(response, rejections)
        inference_routes._remember_hub_access_warning(warned, rejections)
    assert inference_routes._status_load_warning(backend) == warned.memory_warning
    assert hub_refused_cached_copy_warning(REPO) in warned.memory_warning

    with collecting_hub_token_rejections() as rejections:
        inference_routes._remember_hub_access_warning(response, rejections)
    assert inference_routes._status_load_warning(backend) == "Runs partly from disk."


def test_a_new_load_forgets_the_previous_hub_warning():
    backend = llama_cpp.LlamaCppBackend.__new__(llama_cpp.LlamaCppBackend)
    backend._last_load_warning = None
    backend._variant_fallback_warning = None
    backend.hub_access_warning = "Hugging Face refused it."
    backend._begin_load_warnings()
    assert backend.hub_access_warning is None