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

"""sk-unsloth callers must not write or run as the operator; None would trigger a token lookup."""

from unittest.mock import MagicMock

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient

from auth.authentication import (
    authenticated_via_api_key,
    get_current_credential,
    get_current_subject,
)
from routes import export as export_routes
from routes.data_recipe import jobs as data_recipe_jobs_routes

OFFICIAL_HF = "https://huggingface.co"


async def _fake_ensure_export_supported():
    pass


def _app(via_api_key: bool) -> FastAPI:
    app = FastAPI()
    app.include_router(data_recipe_jobs_routes.router, prefix = "/api/data-recipe")
    app.include_router(export_routes.router, prefix = "/api")
    app.dependency_overrides[get_current_subject] = lambda: "alice"
    app.dependency_overrides[get_current_credential] = lambda: ("alice", "cred-1")
    app.dependency_overrides[authenticated_via_api_key] = lambda: via_api_key
    return app


def _publish_manager(monkeypatch, seen):
    mgr = MagicMock()
    mgr.get_status.return_value = {
        "status": "completed",
        "execution_type": "full",
        "artifact_path": "/tmp/artifacts",
    }
    monkeypatch.setattr(data_recipe_jobs_routes, "get_job_manager", lambda: mgr)
    monkeypatch.setattr(
        data_recipe_jobs_routes,
        "publish_recipe_dataset",
        # **kwargs keeps the stub from going stale when the route grows an argument.
        lambda *, artifact_path, repo_id, description, hf_token, private, **kwargs: (
            seen.update(token = hf_token, **kwargs)
            or f"{kwargs.get('link_endpoint') or OFFICIAL_HF}/datasets/{repo_id}"
        ),
    )


def test_publish_refuses_an_api_key_without_a_token(monkeypatch):
    """The hole this PR exists for: publish_recipe_dataset passes the token straight to
    HuggingFaceHubClient and card.push_to_hub, so None publishes as the host's login."""
    _publish_manager(monkeypatch, {})
    response = TestClient(_app(via_api_key = True)).post(
        "/api/data-recipe/jobs/job-1/publish",
        json = {"repo_id": "org/dataset", "description": "d", "hf_token": None},
    )
    assert response.status_code == 400
    assert "required to publish datasets" in response.json()["detail"]


@pytest.mark.parametrize(
    "via_api_key,token,expected",
    [(True, "hf_caller", "hf_caller"), (False, None, None)],
)
def test_publish_allows_a_token_bearing_key_and_a_ui_session(
    monkeypatch, via_api_key, token, expected
):
    seen: dict = {}
    _publish_manager(monkeypatch, seen)
    response = TestClient(_app(via_api_key)).post(
        "/api/data-recipe/jobs/job-1/publish",
        json = {"repo_id": "org/dataset", "description": "d", "hf_token": token},
    )
    assert response.status_code == 200
    assert seen["token"] == expected
    assert seen["link_endpoint"] == OFFICIAL_HF


_EXPORTS = [
    ("/api/export/merged", "export_merged_model", {}),
    ("/api/export/base", "export_base_model", {}),
    ("/api/export/gguf", "export_gguf", {"quantization_method": "q4_k_m"}),
    ("/api/export/lora", "export_lora_adapter", {}),
]


def _backend(monkeypatch, method):
    monkeypatch.setattr(export_routes, "_ensure_export_supported", _fake_ensure_export_supported)
    backend = MagicMock()
    getattr(backend, method).return_value = (True, "ok", "/tmp/export")
    monkeypatch.setattr(export_routes, "get_export_backend", lambda: backend)
    monkeypatch.setattr(
        export_routes, "_export_details", lambda *a, **kw: {"output_path": "/tmp/export"}
    )
    return backend


@pytest.mark.parametrize("endpoint,method,extra", _EXPORTS)
def test_push_refuses_an_api_key_without_a_token(monkeypatch, endpoint, method, extra):
    monkeypatch.setattr(export_routes, "_ensure_export_supported", _fake_ensure_export_supported)
    response = TestClient(_app(via_api_key = True)).post(
        endpoint,
        json = {"save_directory": "/tmp/x", "push_to_hub": True, "repo_id": "o/m", **extra},
    )
    assert response.status_code == 400
    assert "required to push to Hub" in response.json()["detail"]


@pytest.mark.parametrize("endpoint,method,extra", _EXPORTS)
def test_push_with_a_token_reaches_the_backend_unchanged(monkeypatch, endpoint, method, extra):
    backend = _backend(monkeypatch, method)
    response = TestClient(_app(via_api_key = True)).post(
        endpoint,
        json = {
            "save_directory": "/tmp/x",
            "push_to_hub": True,
            "repo_id": "o/m",
            "hf_token": "hf_caller",
            **extra,
        },
    )
    assert response.status_code == 200
    assert getattr(backend, method).call_args.kwargs["hf_token"] == "hf_caller"


@pytest.mark.parametrize("endpoint,method,extra", _EXPORTS)
@pytest.mark.parametrize("via_api_key,expected", [(True, False), (False, None)])
def test_a_local_export_carries_the_callers_identity(
    monkeypatch, endpoint, method, extra, via_api_key, expected
):
    """No push, but a local GGUF export fetches its base and imatrix from the Hub, so it needs a token."""
    backend = _backend(monkeypatch, method)
    response = TestClient(_app(via_api_key)).post(
        endpoint, json = {"save_directory": "/tmp/x", "push_to_hub": False, **extra}
    )
    assert response.status_code == 200
    assert getattr(backend, method).call_args.kwargs["hf_token"] is expected


@pytest.mark.parametrize(
    "via_api_key,token,expected_token,expected_flag",
    [
        (True, None, False, False),
        (True, "hf_caller", "hf_caller", False),
        (False, None, None, True),
    ],
)
def test_load_checkpoint_forwards_the_policy(
    monkeypatch, via_api_key, token, expected_token, expected_flag
):
    monkeypatch.setattr(export_routes, "_ensure_export_supported", _fake_ensure_export_supported)
    backend = MagicMock()
    backend.load_checkpoint.return_value = (True, "loaded")
    monkeypatch.setattr(export_routes, "get_export_backend", lambda: backend)

    response = TestClient(_app(via_api_key)).post(
        "/api/load-checkpoint", json = {"checkpoint_path": "o/m", "hf_token": token}
    )
    assert response.status_code == 200
    kwargs = backend.load_checkpoint.call_args.kwargs
    assert kwargs["hf_token"] is expected_token or kwargs["hf_token"] == expected_token
    assert kwargs["allow_ambient"] is expected_flag


@pytest.fixture
def worker_in_process(monkeypatch):
    """Let run_export_process run here without it taking over the pytest process:
    _setup_log_capture dup2s pipes over fds 1 and 2 with no teardown, and
    UNSLOTH_OFFLINE_PROBE keeps a live Hub request out of a unit test."""
    from core.export import worker

    monkeypatch.setattr(worker, "_setup_log_capture", lambda resp_queue: None)
    monkeypatch.setenv("UNSLOTH_OFFLINE_PROBE", "0")
    return worker


@pytest.mark.parametrize(
    "allow_ambient,caller_token,env_token,disable_implicit,passed",
    [
        (False, None, None, "1", None),
        (False, "hf_caller", None, "1", "hf_caller"),
        (True, None, "hf_operator_secret", None, None),
    ],
)
def test_the_worker_environment_matches_the_callers_policy(
    monkeypatch, worker_in_process, allow_ambient, caller_token, env_token, disable_implicit, passed
):
    import os

    worker = worker_in_process
    for key in ("HF_TOKEN", "HF_HUB_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HUGGINGFACEHUB_API_TOKEN"):
        monkeypatch.setenv(key, "hf_operator_secret")
    monkeypatch.delenv("HF_HUB_DISABLE_IMPLICIT_TOKEN", raising = False)

    seen: dict = {}

    def _fake_activate(path, token):
        seen["HF_TOKEN"] = os.environ.get("HF_TOKEN")
        seen["HF_HUB_TOKEN"] = os.environ.get("HF_HUB_TOKEN")
        seen["DISABLE_IMPLICIT"] = os.environ.get("HF_HUB_DISABLE_IMPLICIT_TOKEN")
        seen["passed"] = token
        raise SystemExit(0)

    monkeypatch.setattr(worker, "_activate_transformers_version", _fake_activate)
    with pytest.raises(SystemExit):
        worker.run_export_process(
            cmd_queue = MagicMock(),
            resp_queue = MagicMock(),
            config = {
                "checkpoint_path": "/tmp/model",
                "allow_ambient": allow_ambient,
                "hf_token": caller_token,
            },
        )

    assert seen["HF_TOKEN"] == env_token
    assert seen["DISABLE_IMPLICIT"] == disable_implicit
    if not allow_ambient:
        assert seen["HF_HUB_TOKEN"] is None
    assert seen["passed"] == passed


def test_a_non_ambient_worker_holds_no_credential_for_the_next_caller(
    monkeypatch, worker_in_process
):
    """One worker serves many callers, so a credential in its environment is a credential
    in the ambient position for whoever exports next, the caller's own included."""
    import os

    worker = worker_in_process
    for key in (
        "HF_TOKEN",
        "HF_HUB_TOKEN",
        "HUGGING_FACE_HUB_TOKEN",
        "HUGGINGFACE_HUB_TOKEN",
        "HUGGINGFACEHUB_API_TOKEN",
        "HF_OIDC_RESOURCE",
    ):
        monkeypatch.setenv(key, "hf_operator_secret")
    monkeypatch.delenv("HF_HUB_DISABLE_IMPLICIT_TOKEN", raising = False)

    seen: dict = {}

    def _fake_activate(path, token):
        seen["env"] = {
            k: os.environ.get(k)
            for k in (
                "HF_TOKEN",
                "HF_HUB_TOKEN",
                "HUGGING_FACE_HUB_TOKEN",
                "HUGGINGFACE_HUB_TOKEN",
                "HUGGINGFACEHUB_API_TOKEN",
                "HF_OIDC_RESOURCE",
            )
        }
        seen["passed"] = token
        raise SystemExit(0)

    monkeypatch.setattr(worker, "_activate_transformers_version", _fake_activate)
    with pytest.raises(SystemExit):
        worker.run_export_process(
            cmd_queue = MagicMock(),
            resp_queue = MagicMock(),
            config = {
                "checkpoint_path": "/tmp/model",
                "allow_ambient": False,
                "hf_token": "hf_caller",
            },
        )

    assert seen["env"] == dict.fromkeys(seen["env"], None), seen["env"]
    assert seen["passed"] == "hf_caller"


def test_an_old_orchestrator_config_keeps_the_previous_behaviour(monkeypatch, worker_in_process):
    """A config from before this change has neither key; it must read as ambient."""
    import os

    worker = worker_in_process
    monkeypatch.setenv("HF_TOKEN", "hf_operator_secret")
    monkeypatch.delenv("HF_HUB_DISABLE_IMPLICIT_TOKEN", raising = False)

    seen: dict = {}

    def _fake_activate(path, token):
        seen["HF_TOKEN"] = os.environ.get("HF_TOKEN")
        seen["DISABLE_IMPLICIT"] = os.environ.get("HF_HUB_DISABLE_IMPLICIT_TOKEN")
        raise SystemExit(0)

    monkeypatch.setattr(worker, "_activate_transformers_version", _fake_activate)
    with pytest.raises(SystemExit):
        worker.run_export_process(
            cmd_queue = MagicMock(),
            resp_queue = MagicMock(),
            config = {"checkpoint_path": "/tmp/m", "hf_token": None},
        )

    assert seen["HF_TOKEN"] == "hf_operator_secret"
    assert seen["DISABLE_IMPLICIT"] is None


@pytest.mark.parametrize(
    "allow_ambient,caller_token,expected",
    [(False, None, False), (False, "hf_caller", "hf_caller"), (True, None, None)],
)
def test_the_load_preflight_runs_under_the_callers_credential(
    monkeypatch, allow_ambient, caller_token, expected
):
    """model_config's shared-cache guards read is_anonymous(), so a plain None walks past
    them; every preflight helper but tier detection gets the same canonical value."""
    from core.export import worker
    from utils import security as security_pkg
    from utils import transformers_version
    from utils.models import model_config

    seen: dict = {}

    def _record(key, value, result):
        seen[key] = value
        return result

    class _Decision:
        blocked = False

    monkeypatch.setattr(
        transformers_version,
        "latest_tier_active_for",
        lambda name, token: _record("tier", token, False),
    )
    monkeypatch.setattr(
        model_config,
        "get_base_model_from_lora_identifier",
        lambda path, token: _record("lora_base", token, None),
    )
    monkeypatch.setattr(
        security_pkg, "security_load_subdirs", lambda t, token: _record("subdirs", token, ())
    )
    monkeypatch.setattr(security_pkg, "load_scan_target", lambda t, subdirs: (t, subdirs))
    monkeypatch.setattr(
        security_pkg,
        "evaluate_file_security",
        lambda target, hf_token, load_subdirs: _record("file_security", hf_token, _Decision()),
    )

    backend = MagicMock()
    backend.load_checkpoint.return_value = (True, "loaded")
    backend.is_vision = backend.is_peft = False

    worker._handle_load(
        backend,
        {
            "checkpoint_path": "owner/model",
            "load_in_4bit": True,
            "hf_token": caller_token,
            "allow_ambient": allow_ambient,
        },
        MagicMock(),
    )

    assert seen["lora_base"] == expected
    assert seen["subdirs"] == expected
    assert seen["file_security"] == expected
    assert backend.load_checkpoint.call_args.kwargs["hf_token"] == expected
    assert seen["tier"] == (expected or None)


@pytest.mark.parametrize(
    "hf_token,expected", [(False, False), ("hf_caller", "hf_caller"), (None, None)]
)
def test_the_weight_loader_never_receives_none_for_an_anonymous_caller(
    monkeypatch, hf_token, expected
):
    """The whole point of the sentinel: hf_login(None) calls get_token(), which reads the
    operator's stored login from disk, and no environment scrub touches that."""
    from core.export import export as export_backend_module

    seen: dict = {}

    class _Stop(Exception):
        pass

    class _Loader:
        @staticmethod
        def from_pretrained(**kwargs):
            seen["loader"] = kwargs["token"]
            raise _Stop()

    monkeypatch.setattr(export_backend_module, "_export_runtime_available", lambda: True)
    monkeypatch.setattr(export_backend_module, "_hf_offline", lambda: False)
    monkeypatch.setattr(
        export_backend_module,
        "detect_audio_type",
        lambda model_id, hf_token, local_files_only: seen.setdefault("audio", hf_token) and None,
    )
    monkeypatch.setattr(
        export_backend_module,
        "is_vision_model",
        lambda model_id, hf_token, local_files_only: bool(seen.setdefault("vision", hf_token))
        and False,
    )
    monkeypatch.setattr(export_backend_module, "FastLanguageModel", _Loader)

    export_backend_module.ExportBackend().load_checkpoint(
        checkpoint_path = "owner/model", hf_token = hf_token
    )

    assert seen["audio"] == (expected or None)
    assert seen["vision"] == (expected or None)
    assert seen["loader"] == expected


def test_hf_login_reads_none_as_fetch_the_operators_stored_token():
    """The upstream contract the sentinel exists for. If this flips, the threading is moot."""
    import inspect

    # Not importorskip: unsloth raises a plain ImportError on torch-less installs.
    try:
        from unsloth.models._utils import hf_login
    except ImportError as exc:
        pytest.skip(f"unsloth needs torch, which this install does not ship: {exc}")

    src = inspect.getsource(hf_login)
    assert "if token is None:" in src and "get_token()" in src
    assert hf_login(False) is False


@pytest.mark.parametrize("hf_token,expected", [(False, False), ("hf_caller", "hf_caller")])
def test_a_local_gguf_lora_conversion_carries_the_sentinel(
    monkeypatch, tmp_path, hf_token, expected
):
    """save.py substitutes get_token() only when the token is None, so False has to reach it."""
    from core.export import export as export_backend_module

    seen: dict = {}

    class _FakeModel:
        peft_config: dict = {}

        @staticmethod
        def save_pretrained_gguf(
            save_directory,
            tokenizer,
            save_method = None,
            quantization_method = None,
            token = None,
        ):
            seen["token"] = token

    monkeypatch.setattr(export_backend_module, "_export_runtime_available", lambda: True)
    monkeypatch.setattr(export_backend_module, "_IS_MLX", False)
    monkeypatch.setattr(export_backend_module, "_apply_wsl_sudo_patch", lambda: None)

    backend = export_backend_module.ExportBackend()
    backend.current_model = _FakeModel()
    backend.current_tokenizer = object()
    backend.is_peft = True

    success, message, _path = backend.export_lora_adapter(
        save_directory = str(tmp_path), gguf = True, hf_token = hf_token
    )
    assert success, message
    assert seen["token"] == expected


@pytest.mark.parametrize("hf_token,expected", [(False, False), ("hf_caller", "hf_caller")])
def test_a_local_merged_save_carries_the_sentinel(monkeypatch, tmp_path, hf_token, expected):
    """A local merge resolves its base repo, and a None token falls back to the operator's stored login."""
    from core.export import export as export_backend_module

    seen: dict = {}

    class _FakeModel:
        @staticmethod
        def save_pretrained_merged(
            save_directory,
            tokenizer,
            save_method = None,
            token = None,
        ):
            seen["token"] = token

    monkeypatch.setattr(export_backend_module, "_export_runtime_available", lambda: True)
    monkeypatch.setattr(export_backend_module, "_IS_MLX", False)

    backend = export_backend_module.ExportBackend()
    backend.current_model = _FakeModel()
    backend.current_tokenizer = object()

    backend.export_merged_model(save_directory = str(tmp_path), hf_token = hf_token)
    assert seen["token"] == expected


def test_offline_type_detection_is_not_degraded_by_the_sentinel(monkeypatch):
    """model_config's cache guards refuse an anonymous cached read, which offline turns a
    cached vision model into a text one. Detection keeps the plain token for that reason."""
    from core.export import export as export_backend_module

    seen: dict = {}

    class _Stop(Exception):
        pass

    class _Loader:
        @staticmethod
        def from_pretrained(**kwargs):
            seen["loader"] = kwargs["token"]
            raise _Stop()

    monkeypatch.setattr(export_backend_module, "_export_runtime_available", lambda: True)
    monkeypatch.setattr(export_backend_module, "_hf_offline", lambda: True)
    monkeypatch.setattr(
        export_backend_module,
        "detect_audio_type",
        lambda model_id, hf_token, local_files_only: seen.setdefault("audio", hf_token) and None,
    )
    monkeypatch.setattr(
        export_backend_module,
        "is_vision_model",
        lambda model_id, hf_token, local_files_only: bool(seen.setdefault("vision", hf_token))
        and False,
    )
    monkeypatch.setattr(export_backend_module, "FastLanguageModel", _Loader)

    export_backend_module.ExportBackend().load_checkpoint(
        checkpoint_path = "owner/vlm", hf_token = False
    )

    assert seen["audio"] is None, "an anonymous probe must not be forced down the guard"
    assert seen["vision"] is None
    assert seen["loader"] is False, "the loader still gets the sentinel"


def test_offline_tier_detection_is_not_degraded_by_the_sentinel(monkeypatch, tmp_path):
    """_load_config_json refuses a hub-cache read for the sentinel, so offline a cached model
    whose tier is only in its config.json drops to the default sidecar."""
    import json
    from types import SimpleNamespace

    from utils import transformers_version as tv

    repo = "acme/private-finetune"
    repo_dir = tmp_path / ("models--" + repo.replace("/", "--"))
    (repo_dir / "snapshots" / "abc123").mkdir(parents = True)
    (repo_dir / "snapshots" / "abc123" / "config.json").write_text(
        json.dumps({"model_type": "qwen3_moe", "architectures": ["Qwen3MoeForCausalLM"]})
    )
    (repo_dir / "refs").mkdir(parents = True)
    (repo_dir / "refs" / "main").write_text("abc123")

    monkeypatch.setattr(tv, "get_hf_cache_paths", lambda: SimpleNamespace(hub_cache = tmp_path))
    monkeypatch.setenv("HF_HUB_OFFLINE", "1")
    monkeypatch.setenv("UNSLOTH_DISABLE_TIER_PROBE", "1")

    def _tier(hf_token):
        for cache in (
            tv._config_json_cache,
            tv._config_needs_510_cache,
            tv._config_needs_550_cache,
            tv._config_needs_530_cache,
            tv._tokenizer_class_cache,
        ):
            cache.clear()
        return tv.get_transformers_tier(repo, hf_token)

    assert _tier(False) == "default", "the sentinel is what breaks the cached tier read"
    assert _tier(None) == "530", "the plain token reads it, as it did before this change"


def test_every_export_entry_point_declares_the_anonymous_sentinel_type():
    """``str | None`` on a parameter that receives ``False`` invites normalising it back
    to ``None``, which is the ambient state."""
    import inspect

    from core.export.export import ExportBackend
    from core.export.orchestrator import ExportOrchestrator
    from hub.utils.hf_tokens import HfTokenArg

    offenders = []
    for owner in (ExportBackend, ExportOrchestrator):
        for name in (
            "load_checkpoint",
            "export_merged_model",
            "export_base_model",
            "export_gguf",
            "export_lora_adapter",
        ):
            fn = getattr(owner, name, None)
            if fn is None:
                continue
            param = inspect.signature(fn).parameters.get("hf_token")
            if param is not None and param.annotation != HfTokenArg:
                offenders.append(f"{owner.__name__}.{name}: {param.annotation}")

    assert not offenders, "these can receive the sentinel but do not say so: " + "; ".join(
        offenders
    )


def test_every_hub_write_route_names_the_ambient_policy():
    """The check that would have caught the two routes #10126 reported."""
    import inspect

    from auth.authentication import allow_ambient_hf_token
    from routes import export as export_routes
    from routes.data_recipe import jobs as jobs_routes

    gated = {
        (export_routes, "load_checkpoint"),
        (export_routes, "export_merged_model"),
        (export_routes, "export_base_model"),
        (export_routes, "export_gguf"),
        (export_routes, "export_lora_adapter"),
        (jobs_routes, "publish_job_dataset"),
    }
    missing = []
    for module, name in sorted(gated, key = lambda pair: pair[1]):
        fn = getattr(module, name)
        param = inspect.signature(fn).parameters.get("allow_ambient")
        if (
            param is None
            or getattr(param.default, "dependency", None) is not allow_ambient_hf_token
        ):
            missing.append(f"{module.__name__}.{name}")

    assert not missing, "these reach a Hub write without the ambient policy: " + ", ".join(missing)


def test_the_mcp_export_tool_never_goes_ambient(monkeypatch):
    """The MCP export tool reaches the export routes as the agent's API key, so the routes'
    own policy holds: allow_ambient is False for that caller. The tool sends the agent's token
    in the body of both calls, and a push without one meets the route's refusal instead of
    the server's own Hugging Face token."""
    import json

    from studio_mcp import export_jobs

    from .mcp_harness import bodies, fake_studio, run_export_job

    export_jobs._reset()
    backend = _backend(monkeypatch, "export_gguf")

    def status(request, body):
        # Like the real route: it names the checkpoint the last load put in the worker.
        loaded = bodies(studio, "/api/export/load-checkpoint")
        return {
            "last_op_seq": 0,
            "current_checkpoint": loaded[-1]["checkpoint_path"] if loaded else None,
        }

    studio = fake_studio(
        {
            ("GET", "/api/models/checkpoints"): {
                "outputs_dir": "/o",
                "models": [
                    {"name": "run", "checkpoints": [{"display_name": "run", "path": "/o/run"}]}
                ],
            },
            ("POST", "/api/export/load-checkpoint"): {"success": True, "message": "Loaded"},
            ("GET", "/api/export/status"): status,
        }
    )
    studio.state.bind_host = "127.0.0.1"
    studio.include_router(export_routes.router, prefix = "/api/export")
    studio.dependency_overrides[get_current_subject] = lambda: "unsloth"

    def export(**args):
        # The job runs in the background; a fixed number of back-to-back polls can beat it on a slow host.
        sent = {"checkpoint": "run", "format": "gguf", "save_directory": "out", **args}
        return run_export_job(monkeypatch, studio, sent)[1]["structuredContent"]

    with_token = export(push_to_hub = True, repo_id = "me/m", hf_token = "hf_agent")
    without = export(push_to_hub = True, repo_id = "me/m")
    export_jobs._reset()

    loads = [
        {k: v for k, v in body.items() if k != "op_id"}
        for body in bodies(studio, "/api/export/load-checkpoint")
    ]
    assert with_token["status"] == "completed"
    assert loads[0] == {"checkpoint_path": "/o/run", "max_seq_length": 2048, "hf_token": "hf_agent"}
    assert backend.export_gguf.call_args.kwargs["hf_token"] == "hf_agent"
    assert without["status"] == "failed"
    assert without["error"].startswith(
        "Hugging Face token is required to push to Hub when authenticated via API key."
    )
    assert backend.export_gguf.call_count == 1
    assert "hf_token" not in loads[1]
    assert json.dumps(loads).count("hf_agent") == 1


def test_the_worker_can_still_disable_implicit_tokens_when_it_starts():
    """HF_HUB_DISABLE_IMPLICIT_TOKEN is latched at import, so any early huggingface_hub import breaks it."""
    import subprocess
    import sys
    import textwrap
    from pathlib import Path

    backend = Path(__file__).resolve().parent.parent
    probe = textwrap.dedent(
        """
        import sys
        sys.path.insert(0, %r)
        import core.export.worker  # noqa: F401
        print("imported" if "huggingface_hub" in sys.modules else "clear")
        """
    ) % str(backend)
    out = subprocess.run([sys.executable, "-c", probe], capture_output = True, text = True, timeout = 300)
    assert out.returncode == 0, out.stderr[-2000:]
    assert out.stdout.strip().endswith("clear"), (
        "core.export.worker imports huggingface_hub at module scope, so "
        "HF_HUB_DISABLE_IMPLICIT_TOKEN is already latched by the time the scrub runs"
    )