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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: read public Hub repos without a saved token the Hub rejects (#12158)
Daniel Hanecf63dd73868
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.pyindex 758134eeeed..2e814690646 100644--- a/studio/backend/core/inference/llama_cpp.py+++ b/studio/backend/core/inference/llama_cpp.py@@ -78,6 +78,11 @@ from core.inference.context_window import (from core.inference.llama_tool_schema import llama_grammar_toolsfrom core.inference.stream_errors import stream_error_from_chunkfrom hub.utils.hf_errors import modelscope_missing+from hub.utils.hf_tokens import (+ call_hub_with_anonymous_retry,+ hf_token_rejected_hint,+ is_rejected_credential_error,+)from core.inference.llama_server_args import (_CACHE_RAM_FLAGS,_CTX_CHECKPOINTS_FLAGS,@@ -2523,7 +2528,9 @@ def _fetch_swa_entry_from_hf(repo_id: str) -> Optional[object]:# Avoid caching the expected 404 for GGUF repos without config.json.if hf_file_definitely_absent(repo_id, "config.json"):return None- cfg_path = hf_hub_download(+ cfg_path = call_hub_with_anonymous_retry(+ hf_hub_download,+ None,repo_id,"config.json",repo_type = "model",@@ -3287,11 +3294,12 @@ def _cached_candidate_matches_revision_size(try:from huggingface_hub import get_paths_infoinfos = list(- get_paths_info(+ call_hub_with_anonymous_retry(+ get_paths_info,+ hf_token,repo_id,paths,revision = snap.name,- token = hf_token,))except Exception as e:@@ -3823,7 +3831,7 @@ def _resolve_variant_gguf_files(try:from huggingface_hub import list_repo_files- files = list_repo_files(hf_repo, token = hf_token)+ files = call_hub_with_anonymous_retry(list_repo_files, hf_token, hf_repo)gguf_files = _gguf_files_for_variant(files, hf_variant)if gguf_files:gguf_filename = gguf_files[0]@@ -17353,7 +17361,7 @@ class LlamaCppBackend:from huggingface_hub import get_paths_info, list_repo_filesfrom core.inference.openai_auto_download import _DISK_RESERVE_BYTES- files = list_repo_files(hf_repo, token = hf_token)+ files = call_hub_with_anonymous_retry(list_repo_files, hf_token, hf_repo)from hub.utils.gguf import drop_shadowed_appledouble_names, gguf_checkpoint_familygguf_files = [@@ -17371,7 +17379,9 @@ class LlamaCppBackend:return None# Sizes for all GGUF files- path_infos = list(get_paths_info(hf_repo, gguf_files, token = hf_token))+ path_infos = list(+ call_hub_with_anonymous_retry(get_paths_info, hf_token, hf_repo, gguf_files)+ )size_map = {p.path: (p.size or 0) for p in path_infos}# Group by variant: shards share a prefix before -NNNNN-of-NNNNN@@ -18691,7 +18701,9 @@ class LlamaCppBackend:try:from huggingface_hub import get_paths_info, try_to_load_from_cache- path_infos = list(get_paths_info(hf_repo, all_gguf_files, token = hf_token))+ path_infos = list(+ call_hub_with_anonymous_retry(get_paths_info, hf_token, hf_repo, all_gguf_files)+ )total_bytes = sum((p.size or 0) for p in path_infos)# Subtract bytes already in the HF cache so we only preflight@@ -18841,6 +18853,8 @@ class LlamaCppBackend:raise GgufDownloadCancelled(str(e)) from eraise RuntimeError(f"Failed to download GGUF file '{gguf_filename}' from {hf_repo}: {e}"+ # Only when a token went out: an anonymous 401 is a private or missing repo.+ f"{hf_token_rejected_hint(e) if is_rejected_credential_error(e, hf_token) else ''}")dl_elapsed = time.monotonic() - dl_start@@ -18936,7 +18950,9 @@ class LlamaCppBackend:if cancel_event.is_set():return Nonetry:- target = _pick_from(list_repo_files(hf_repo, token = hf_token))+ target = _pick_from(+ call_hub_with_anonymous_retry(list_repo_files, hf_token, hf_repo)+ )listing_answered = Truelisting_failed = Falsebreak@@ -19614,7 +19630,7 @@ class LlamaCppBackend:"""try:from huggingface_hub import model_info- info = model_info(hf_repo, token = hf_token, files_metadata = True)+ info = call_hub_with_anonymous_retry(model_info, hf_token, hf_repo, files_metadata = True)return {name: int(getattr(sibling, "size", 0) or 0)for sibling in (getattr(info, "siblings", None) or [])@@ -20886,7 +20902,7 @@ class LlamaCppBackend:# credential the child obtained elsewhere, or one held in a variable whose# name says nothing (GITHUB_PAT, MY_THING)._SECRET_VALUE_RES = (- re.compile(r"hf_[A-Za-z0-9]{20,}"),+ re.compile(r"hf_(?:oauth_[A-Za-z0-9._~+/=-]{20,}|[A-Za-z0-9]{20,})"),re.compile(r"github_pat_[A-Za-z0-9_]{20,}"),re.compile(r"gh[pousr]_[A-Za-z0-9]{20,}"),re.compile(r"sk-[A-Za-z0-9_\-]{20,}"),diff --git a/studio/backend/core/inference/worker.py b/studio/backend/core/inference/worker.pyindex 6a3f6be5328..2e58d7e5f1c 100644--- a/studio/backend/core/inference/worker.py+++ b/studio/backend/core/inference/worker.py@@ -12,6 +12,7 @@ mp.Queue, and exits on shutdown or unload. Pattern follows core/training/worker.from __future__ import annotations+import functoolsimport base64import inspectimport json@@ -509,9 +510,61 @@ def _worker_reclaimable_gpu_gb(config: dict) -> dict[str, float] | None:return None+# The token env before a load scrubbed it; the next load restores it.+_TOKEN_ENV_BEFORE_ANONYMOUS_LOAD: Optional[dict] = None+++def _token_env_keys() -> tuple:+ from hub.utils.hf_tokens import _HF_TOKEN_ENV_KEYS+ return (*_HF_TOKEN_ENV_KEYS, "HF_HUB_DISABLE_IMPLICIT_TOKEN")+++def _restore_token_environment() -> None:+ """Undo an earlier load's anonymous scrub (the token may have been replaced since)."""+ global _TOKEN_ENV_BEFORE_ANONYMOUS_LOAD+ saved, _TOKEN_ENV_BEFORE_ANONYMOUS_LOAD = _TOKEN_ENV_BEFORE_ANONYMOUS_LOAD, None+ for key, value in (saved or {}).items():+ if value is None:+ os.environ.pop(key, None)+ else:+ os.environ[key] = value+++def _drop_a_rejected_token(config: dict) -> None:+ """The Hub refused this load's token while anonymous reads worked: load the rest anonymously."""+ global _TOKEN_ENV_BEFORE_ANONYMOUS_LOAD+ from hub.utils.hf_tokens import saved_token_rejected++ token = _config_hf_token(config)+ if token is not False and saved_token_rejected(token):+ config["anonymous_hf_access"] = True+ if _TOKEN_ENV_BEFORE_ANONYMOUS_LOAD is None:+ _TOKEN_ENV_BEFORE_ANONYMOUS_LOAD = {k: os.environ.get(k) for k in _token_env_keys()}+ _apply_worker_hf_token_environment(config)+ logger.warning(+ "Hugging Face rejected the token for %s; loading it without the token.",+ config.get("model_name"),+ )+++def _in_token_rejection_scope(handler):+ """Run one load in its own rejected-token scope, so a verdict never outlives it."""++ @functools.wraps(handler)+ def scoped(*args, **kwargs):+ from hub.utils.hf_tokens import token_rejection_scope+ with token_rejection_scope():+ return handler(*args, **kwargs)++ return scoped+++@_in_token_rejection_scopedef _handle_load(backend, config: dict, resp_queue: Any) -> None:+ _restore_token_environment()try:mc = _build_model_config(config)+ _drop_a_rejected_token(config)hf_token = _config_hf_token(config)load_in_4bit = _resolve_lora_4bit(mc, config.get("load_in_4bit", True))diff --git a/studio/backend/core/research/redaction.py b/studio/backend/core/research/redaction.pyindex d7c1f011c29..7538bf87b3e 100644--- a/studio/backend/core/research/redaction.py+++ b/studio/backend/core/research/redaction.py@@ -61,7 +61,7 @@ _QUERY_OPAQUE_TOKEN = re.compile(r"\b(?:eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}"r"|sk-[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9_]{20,}"r"|github_pat_[A-Za-z0-9_]{20,}|xox[baprs]-[A-Za-z0-9-]{16,}"- r"|hf_[A-Za-z0-9]{20,}|glpat-[A-Za-z0-9_-]{20,}"+ r"|hf_(?:oauth_[A-Za-z0-9._~+/=-]{20,}|[A-Za-z0-9]{20,})|glpat-[A-Za-z0-9_-]{20,}"r"|AKIA[A-Z0-9]{16})\b")# International (+CC) or NANP-formatted phone numbers: requires separators or a leading "+" sodiff --git a/studio/backend/hub/services/models/gguf_variants.py b/studio/backend/hub/services/models/gguf_variants.pyindex 60fd0fc0a4c..8464fcf5e67 100644--- a/studio/backend/hub/services/models/gguf_variants.py+++ b/studio/backend/hub/services/models/gguf_variants.py@@ -26,6 +26,7 @@ from hub.utils import download_registryfrom hub.utils import inventory_scan as hf_cache_scanfrom hub.utils.hf_errors import hf_error_status, modelscope_missingfrom hub.utils.hf_tokens import cached_read_refused as hub_cached_read_refused+from hub.utils.hf_tokens import call_with_anonymous_retryfrom hub.utils.hf_cache_state import (incomplete_blob_hash,iter_destructive_repo_cache_dirs,@@ -215,10 +216,13 @@ def _fetch_gguf_variant_requirements(return {}try:from huggingface_hub import HfApi- info = HfApi(token = hf_token).model_info(- repo_id,- files_metadata = True,- timeout = _GGUF_METADATA_TIMEOUT_SECONDS,+ info = call_with_anonymous_retry(+ lambda token: HfApi(token = token).model_info(+ repo_id,+ files_metadata = True,+ timeout = _GGUF_METADATA_TIMEOUT_SECONDS,+ ),+ hf_token,)except Exception as e:logger.warning(diff --git a/studio/backend/hub/utils/download_registry.py b/studio/backend/hub/utils/download_registry.pyindex cc40fe22038..9bc2d418a5d 100644--- a/studio/backend/hub/utils/download_registry.py+++ b/studio/backend/hub/utils/download_registry.py@@ -801,7 +801,7 @@ def prepare_cache_for_transport(return total_purged-_HF_TOKEN_RE = re.compile(r"hf_[A-Za-z0-9]{20,}")+_HF_TOKEN_RE = re.compile(r"hf_(?:oauth_[A-Za-z0-9._~+/=-]{20,}|[A-Za-z0-9]{20,})")_BEARER_RE = re.compile(r"(?i)bearer\s+[A-Za-z0-9._\-]+")diff --git a/studio/backend/hub/utils/gguf.py b/studio/backend/hub/utils/gguf.pyindex 109dea25579..767ebec886c 100644--- a/studio/backend/hub/utils/gguf.py+++ b/studio/backend/hub/utils/gguf.py@@ -11,6 +11,7 @@ from dataclasses import dataclassfrom pathlib import Pathfrom typing import Iterable, Optional, Sequence+from hub.utils.hf_tokens import call_with_anonymous_retryfrom loggers import get_loggerfrom utils.paths.path_utils import (drop_shadowed_appledouble_names as _drop_shadowed_appledouble_names,@@ -1144,10 +1145,14 @@ def list_gguf_variants(return _ready_cached_variants(cached)try:- info = HfApi(token = hf_token).model_info(- repo_id,- files_metadata = True,- timeout = _GGUF_MODEL_INFO_TIMEOUT_SECONDS,+ # A refused credential retries once anonymously: a public listing still answers.+ info = call_with_anonymous_retry(+ lambda token: HfApi(token = token).model_info(+ repo_id,+ files_metadata = True,+ timeout = _GGUF_MODEL_INFO_TIMEOUT_SECONDS,+ ),+ hf_token,)except Exception as exc:if type(exc).__name__ in (diff --git a/studio/backend/hub/utils/hf_tokens.py b/studio/backend/hub/utils/hf_tokens.pyindex 79ea9a1d69b..9abc5f9d570 100644--- a/studio/backend/hub/utils/hf_tokens.py+++ b/studio/backend/hub/utils/hf_tokens.py@@ -8,9 +8,13 @@ from __future__ import annotationsimport hashlibimport hmacimport logging+import os+import reimport threadingimport time+import urllib.errorfrom contextlib import contextmanager+from contextvars import ContextVarfrom typing import Iterable, Literal, MutableMapping, Optional, Unionlogger = logging.getLogger(__name__)@@ -62,8 +66,13 @@ def apply_token_to_child_env(env: MutableMapping[str, str], hf_token: HfTokenArg"""Grant a spawned probe exactly its caller's credential.A child env is seeded from the parent's, so not *setting* a token is not denying one.- Only the sentinel scrubs; ``None`` keeps the inherited env on purpose.+ Only the sentinel scrubs; ``None`` keeps the inherited env on purpose. A token the Hub+ refused in this request while anonymous reads worked goes to the child as anonymous too+ (explicit tokens only: judging the ambient one imports huggingface_hub, which the pre-import+ tier probe must not)."""+ if isinstance(hf_token, str) and hf_token and saved_token_rejected(hf_token):+ hf_token = Falseif isinstance(hf_token, str) and hf_token:# Scrub before granting: setting HF_TOKEN alone leaves an operator credential# sitting in HF_HUB_TOKEN or a legacy alias, so the child holds two.@@ -1024,3 +1033,266 @@ def _probe_repo_access(if _is_probe_timeout(exc):raise _ProbeTimedOut from excreturn _probe_answer_from_exception(exc, response)+++# A refused credential (expired OAuth token, revoked key) gets 401 on every read, public repos+# included; an accepted one gets 404 for a repo it cannot see. A 401 blames the credential (#11551).+HUB_TOKEN_REJECTED_WARNING = (+ "Hugging Face rejected the saved token (it may be expired or revoked), so this public "+ "model was read without it. Update or clear the token in Settings to keep access to "+ "gated and private models."+)+HUB_TOKEN_REJECTED_ERROR = (+ "Hugging Face rejected the saved token (HTTP 401); it may be expired or revoked. "+ "Update or clear it in Settings, then try again."+)+++class HubTokenRejections:+ """What one request learned about its credential. Shared by the threads it hands off to."""++ __slots__ = ("recovered", "refused", "_rejected", "_recovered")++ def __init__(self) -> None:+ self.recovered = False+ self.refused = False+ # Digests, never tokens: a traceback or debugger can print this.+ self._rejected: "set[str]" = set()+ self._recovered: "set[str]" = set()++ @property+ def rejected(self) -> bool:+ return self.recovered or self.refused+++_hub_token_rejections: ContextVar[Optional[HubTokenRejections]] = ContextVar(+ "hub_token_rejections", default = None+)+++@contextmanager+def collecting_hub_token_rejections():+ """Scope one request's rejection notes; nested scopes share the outer one."""+ existing = _hub_token_rejections.get()+ if existing is not None:+ yield existing+ return+ sink = HubTokenRejections()+ reset = _hub_token_rejections.set(sink)+ try:+ yield sink+ finally:+ _hub_token_rejections.reset(reset)+++@contextmanager+def token_rejection_scope():+ """A fresh scope, forgotten on exit: one load's verdict must not reach the next."""+ sink = HubTokenRejections()+ reset = _hub_token_rejections.set(sink)+ try:+ yield sink+ finally:+ _hub_token_rejections.reset(reset)+++def hub_token_rejections() -> Optional[HubTokenRejections]:+ return _hub_token_rejections.get()+++def _sent_credential(hf_token: HfTokenArg) -> Optional[str]:+ """The credential a read with *hf_token* puts on the wire, or None when it sends none."""+ if is_anonymous(hf_token):+ return None+ if isinstance(hf_token, str):+ return hf_token.strip() or None+ if hf_token is None:+ if _implicit_token_disabled():+ # huggingface_hub sends no ambient token then, so a 401 is about the repo.+ return None+ return _wire_hf_token()+ return None+++def _wire_hf_token() -> Optional[str]:+ """The ambient token huggingface_hub sends on a ``token=None`` read (not ``_ambient_hf_token``,+ which also counts aliases it never sends)."""+ try:+ from huggingface_hub import get_token+ token = get_token()+ except Exception:+ return None+ return token.strip() if isinstance(token, str) and token.strip() else None+++def _implicit_token_disabled() -> bool:+ # huggingface_hub reads its import-time constant, not the env the worker changes later.+ try:+ from huggingface_hub import constants+ except Exception:+ return os.environ.get("HF_HUB_DISABLE_IMPLICIT_TOKEN", "").strip().lower() in (+ "1",+ "true",+ "yes",+ "on",+ )+ return bool(getattr(constants, "HF_HUB_DISABLE_IMPLICIT_TOKEN", False))+++# huggingface_hub's own wording (hf_raise_for_status), optionally behind "ClassName: ".+_HUB_401_TEXT = re.compile(r"^(?:[A-Za-z]\w*: )?401 Client Error\b")+++def is_token_rejection(exc: BaseException) -> bool:+ """A 401 anywhere in the chain: transports wrap Hub errors, and the download ladder rebuilds+ a child's error from its text."""+ seen: set[int] = set()+ pending: list[Optional[BaseException]] = [exc]+ while pending:+ link = pending.pop()+ if link is None or id(link) in seen:+ continue+ seen.add(id(link))+ status = getattr(getattr(link, "response", None), "status_code", None)+ if status == 401:+ return True+ if isinstance(link, urllib.error.HTTPError) and link.code == 401:+ return True+ if status is None and _HUB_401_TEXT.match(str(link)):+ return True+ pending += [link.__cause__, link.__context__]+ return False+++def is_rejected_credential_error(exc: BaseException, hf_token: HfTokenArg) -> bool:+ """Whether *exc* is Hugging Face refusing the credential a read with *hf_token* sent."""+ if is_anonymous(hf_token) or _is_probe_timeout(exc):+ return False+ return is_token_rejection(exc) and _sent_credential(hf_token) is not None+++def _credential_rejected_this_request(hf_token: HfTokenArg) -> bool:+ sink = _hub_token_rejections.get()+ if sink is None or not sink._rejected:+ return False+ credential = _sent_credential(hf_token)+ return credential is not None and _credential_identity(credential) in sink._rejected+++def note_saved_token_rejected(hf_token: HfTokenArg) -> None:+ """Record that this request's reads with *hf_token* were refused while anonymous ones worked."""+ sink = _hub_token_rejections.get()+ credential = _sent_credential(hf_token)+ if sink is None or credential is None:+ return+ identity = _credential_identity(credential)+ sink._rejected.add(identity)+ sink._recovered.add(identity)+ sink.recovered = True+++def saved_token_rejected(hf_token: HfTokenArg) -> bool:+ """Whether this request saw the Hub refuse *hf_token* while an anonymous read worked."""+ sink = _hub_token_rejections.get()+ credential = _sent_credential(hf_token)+ if sink is None or credential is None:+ return False+ return _credential_identity(credential) in sink._recovered+++def _is_missing_file(exc: BaseException) -> bool:+ """The Hub said a file is absent (not ``LocalEntryNotFoundError``, the offline cache miss)."""+ names = {cls.__name__ for cls in type(exc).__mro__}+ return "EntryNotFoundError" in names and "LocalEntryNotFoundError" not in names+++def call_with_anonymous_retry(read, hf_token: HfTokenArg):+ """Run ``read(token)``, once more anonymously if the Hub refuses the credential (a 401 on a+ read that sent one). Anonymous answers are public data only. When anonymous fails too, the+ ORIGINAL error is raised: it is the one callers classify.+ """+ if _credential_rejected_this_request(hf_token):+ # Refused earlier in this request: anonymous first, the credential only if that fails.+ try:+ result = read(False)+ except Exception as anonymous_exc:+ if not is_token_rejection(anonymous_exc):+ raise+ else:+ note_saved_token_rejected(hf_token)+ return result+ try:+ return read(hf_token)+ except Exception as exc:+ if is_rejected_credential_error(exc, hf_token):+ sink = _hub_token_rejections.get()+ if sink is not None:+ sink.refused = True+ raise+ try:+ return read(hf_token)+ except Exception as exc:+ if not is_rejected_credential_error(exc, hf_token):+ raise+ sink = _hub_token_rejections.get()+ credential = _sent_credential(hf_token)+ if sink is not None and credential is not None:+ sink._rejected.add(_credential_identity(credential))+ try:+ result = read(False)+ except Exception as anonymous_exc:+ if _is_cancellation(anonymous_exc):+ raise+ if _is_missing_file(anonymous_exc):+ # An optional file's absence is the answer callers need, not the 401.+ note_saved_token_rejected(hf_token)+ raise+ logger.info(+ "Hugging Face refused the credential (401); the anonymous retry failed too: %s",+ type(anonymous_exc).__name__,+ )+ if sink is not None:+ sink.refused = True+ raise exc from None+ logger.warning(+ "Hugging Face refused the credential (401); read anonymously instead. "+ "Update or clear the Hugging Face token."+ )+ note_saved_token_rejected(hf_token)+ return result+++def _is_cancellation(exc: BaseException) -> bool:+ """A cancelled load or download, however the transport layer spelled it."""+ if "Cancel" in type(exc).__name__:+ return True+ return isinstance(exc, RuntimeError) and str(exc).startswith("Cancelled")+++def call_hub_with_anonymous_retry(fn, hf_token: HfTokenArg, *args, **kwargs):+ """``call_with_anonymous_retry`` for a huggingface_hub-style ``fn(*args, token = ..., **kwargs)``."""+ return call_with_anonymous_retry(lambda token: fn(*args, token = token, **kwargs), hf_token)+++def anonymous_retrying(fn):+ """*fn* (a huggingface_hub read taking ``token=``) with the rejected-token retry built in."""++ def read(+ *args,+ token: HfTokenArg = None,+ **kwargs,+ ):+ return call_hub_with_anonymous_retry(fn, token, *args, **kwargs)++ return read+++def hf_token_rejected_hint(exc: BaseException) -> str:+ """User-facing sentence for a 401 that the anonymous retry could not recover, else ''."""+ if not is_token_rejection(exc):+ return ""+ return (+ " Hugging Face answered 401: the saved Hugging Face token may have expired or been "+ "revoked, or the repo is private or gated. Update the token in Settings, or remove it "+ "to read public repos."+ )diff --git a/studio/backend/hub/workers/hf_download.py b/studio/backend/hub/workers/hf_download.pyindex e4fb595691d..7893b7f757d 100644--- a/studio/backend/hub/workers/hf_download.py+++ b/studio/backend/hub/workers/hf_download.py@@ -160,8 +160,37 @@ def _install_parent_death_watchdog(parent_pid: int | None) -> None:).start()+# One job per process: once the Hub has refused the token, every later read skips it.+_REJECTED_TOKEN: str | None = None++def _hf_token_arg(hf_token: str | None) -> HfTokenArg:- return hf_token if hf_token else False+ if not hf_token or hf_token == _REJECTED_TOKEN:+ return False+ return hf_token+++def _metadata_read(fn, hf_token: str | None, *args, **kwargs):+ """A metadata read that retries once anonymously when the Hub rejects the token (HTTP 401)."""+ global _REJECTED_TOKEN+ from hub.utils.hf_tokens import (+ call_hub_with_anonymous_retry,+ collecting_hub_token_rejections,+ saved_token_rejected,+ )++ token = _hf_token_arg(hf_token)+ with collecting_hub_token_rejections():+ result = call_hub_with_anonymous_retry(fn, token, *args, **kwargs)+ rejected = token is not False and saved_token_rejected(token)+ if hf_token and rejected:+ _REJECTED_TOKEN = hf_token+ print(+ "Hugging Face rejected the saved token (HTTP 401); downloading without it. "+ "Update or remove the token in Settings if it has expired or been revoked.",+ file = sys.stderr,+ )+ return resultdef _retry_metadata_fetch(repo_id: str, fetch, *, label: str):@@ -187,9 +216,10 @@ def _model_info_with_retry(repo_id: str, hf_token: str | None):info = _retry_metadata_fetch(repo_id,- lambda timeout: hf_model_info(+ lambda timeout: _metadata_read(+ hf_model_info,+ hf_token,repo_id,- token = _hf_token_arg(hf_token),timeout = timeout,files_metadata = True,),@@ -232,10 +262,12 @@ def _reuse_unchanged_files(def _dataset_info_with_retry(repo_id: str, hf_token: str | None):from huggingface_hub import HfApi- api = HfApi(token = _hf_token_arg(hf_token))+ api = HfApi()return _retry_metadata_fetch(repo_id,- lambda timeout: api.dataset_info(+ lambda timeout: _metadata_read(+ api.dataset_info,+ hf_token,repo_id,timeout = timeout,files_metadata = True,diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex b45faad4ad7..8253b0d9b4d 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -81,7 +81,14 @@ from utils.audio_tokens import GGUF_TTS_AUDIO_TYPES as _GGUF_TTS_AUDIO_TYPESfrom utils.upload_limits import STT_AUDIO_B64_MAX_CHARS, STT_AUDIO_RAW_MAX_BYTESfrom hub.dependencies import get_hf_token, get_request_hf_tokenfrom hub.utils.hf_errors import modelscope_missing-from hub.utils.hf_tokens import HfTokenArg+from hub.utils.hf_tokens import (+ HUB_TOKEN_REJECTED_ERROR,+ is_token_rejection,+ HUB_TOKEN_REJECTED_WARNING,+ HfTokenArg,+ call_hub_with_anonymous_retry,+ collecting_hub_token_rejections,+)from hub.services.models.ollama import (acquire_ollama_model_ref,is_ollama_manifest_ref,@@ -11200,7 +11207,8 @@ def _remote_gguf_companion_bytes(from utils.models.drafters import dflash_budget_bytes, split_listing_is_completefrom utils.models.model_config import dspark_preference_key- info = model_info(repo, token = hf_token, files_metadata = True)+ # A refused token must not zero the companion bytes and slip past the training budget.+ info = call_hub_with_anonymous_retry(model_info, hf_token, repo, files_metadata = True)total = 0mtp_bytes = 0dspark_candidates: list[tuple[str, int]] = []@@ -11333,8 +11341,9 @@ def _remote_drafter_repo_bytes(spec: str, *, hf_token: Optional[str]) -> int:from utils.models.drafters import dflash_budget_bytes, split_listing_is_completefrom hub.utils.gguf import drop_shadowed_appledouble_siblings+ from hub.utils.hf_tokens import call_hub_with_anonymous_retry- info = model_info(repo, token = hf_token, files_metadata = True)+ info = call_hub_with_anonymous_retry(model_info, hf_token, repo, files_metadata = True)sizes: dict[str, int] = {}# A sidecar's few KB stands in for the drafter the launch then fetches, and this figure# is what admits or refuses a load beside a training run.@@ -16134,6 +16143,15 @@ def _cancel_scoped_load_attempt(return attempt, is_running+def _with_token_rejected_warning(response, token_rejections):+ """Tell the user the model loaded without their token, which Hugging Face refused."""+ if not token_rejections.recovered or not isinstance(response, LoadResponse):+ return response+ existing = response.memory_warning+ warning = f"{existing} {HUB_TOKEN_REJECTED_WARNING}" if existing else HUB_TOKEN_REJECTED_WARNING+ return response.model_copy(update = {"memory_warning": warning})++async def _run_tracked_load_model_impl(request: LoadRequest,fastapi_request: Request,@@ -16152,15 +16170,16 @@ async def _run_tracked_load_model_impl(try:if attempt.cancel_event.is_set():raise HTTPException(status_code = 409, detail = "Model load cancelled")- response = await _load_model_impl(- request,- fastapi_request,- current_subject,- current_request_counted = current_request_counted,- on_reload_confirmed = on_reload_confirmed,- load_cancel_event = attempt.cancel_event,- )- return response+ with collecting_hub_token_rejections() as token_rejections:+ response = await _load_model_impl(+ request,+ fastapi_request,+ current_subject,+ current_request_counted = current_request_counted,+ on_reload_confirmed = on_reload_confirmed,+ load_cancel_event = attempt.cancel_event,+ )+ return _with_token_rejected_warning(response, token_rejections)finally:if attempt.cancel_event.is_set() and not attempt.cancel_complete.is_set():if not await asyncio.to_thread(@@ -16587,6 +16606,7 @@ async def _load_model_impl(native_grant_backed = Falsemodel_log_label = request.model_pathgguf_load_stack = ExitStack()+ token_rejections = gguf_load_stack.enter_context(collecting_hub_token_rejections())try:# Validate user pass-through args up front so a managed-flag collision# returns 400 before any model work.@@ -17735,6 +17755,9 @@ async def _load_model_impl(if isinstance(e, SidecarSwapInProgress):# Lost the spawn-time race to a sidecar install/repair: retryable 409.raise HTTPException(status_code = 409, detail = str(e))+ if token_rejections.refused and (is_hf_authentication_error(e) or is_token_rejection(e)):+ logger.warning("Load of '%s' failed: Hugging Face rejected the token", model_log_label)+ raise HTTPException(status_code = 400, detail = HUB_TOKEN_REJECTED_ERROR)# Friendlier message for models Unsloth cannot load.redacted_msg = redact_native_paths(str(e))_unsupported_quantization = _unsupported_quantization_detail(redacted_msg)@@ -17945,6 +17968,7 @@ async def validate_model(model_log_label = request.model_pathollama_load_stack = ExitStack()+ token_rejections = ollama_load_stack.enter_context(collecting_hub_token_rejections())try:resolved_ollama_path = await _lease_ollama_model_ref(request,@@ -18292,7 +18316,11 @@ async def validate_model(return restore_inventory_handles(ValidateModelResponse(valid = True,- message = "Model identifier is valid.",+ message = (+ f"Model identifier is valid. {HUB_TOKEN_REJECTED_WARNING}"+ if token_rejections.recovered+ else "Model identifier is valid."+ ),identifier = model_log_label if native_grant_backed else config.identifier,resident = await asyncio.to_thread(_validated_target_is_resident,@@ -18333,6 +18361,8 @@ async def validate_model(except Exception as e:# Restored here rather than at each raise below: every branch quotes this string.redacted_msg = restore_inventory_handles(redact_native_paths(str(e)))+ if token_rejections.refused and (is_hf_authentication_error(e) or is_token_rejection(e)):+ raise HTTPException(status_code = 400, detail = HUB_TOKEN_REJECTED_ERROR)if is_hf_authentication_error(e):raise HTTPException(status_code = 400,@@ -18341,6 +18371,15 @@ async def validate_model("in Settings, and confirm access to this gated repository."),)+ if _raised_repository_not_found(e):+ # The Hub answers alike for a missing repo and a private one, so the message names both.+ raise HTTPException(+ status_code = 400,+ detail = (+ f"'{model_log_label}' was not found on Hugging Face, or it is private. "+ "Check the name, or add a token with access to it in Settings."+ ),+ )_unsupported_quantization = _unsupported_quantization_detail(redacted_msg)if _unsupported_quantization is not None:logger.warning(@@ -18389,6 +18428,21 @@ async def validate_model(ollama_load_stack.close()+def _raised_repository_not_found(error: BaseException) -> bool:+ seen: set[int] = set()+ current: Optional[BaseException] = error+ while current is not None and id(current) not in seen:+ seen.add(id(current))+ if type(current).__name__ == "RepositoryNotFoundError":+ return True+ if isinstance(current, (ValueError, RuntimeError)):+ return False+ current = current.__cause__ or (+ None if current.__suppress_context__ else current.__context__+ )+ return False++def _upgrade_check_config_target(request: TransformersUpgradeCheckRequest) -> str:"""The config.json this load will actually read, by the remote-code scan's precedence.diff --git a/studio/backend/tests/test_gguf_hub_unreadable.py b/studio/backend/tests/test_gguf_hub_unreadable.pyindex 76961448547..4574b888783 100644--- a/studio/backend/tests/test_gguf_hub_unreadable.py+++ b/studio/backend/tests/test_gguf_hub_unreadable.py@@ -10,6 +10,7 @@ import huggingface_hubimport pytestimport core.inference.llama_cpp as llama_cpp+import hub.utils.hf_tokens as hf_tokensimport utils.hf_cache_settings as hf_cache_settingsimport utils.models.model_config as mcfrom utils.models.model_config import (@@ -44,6 +45,9 @@ def _isolated(tmp_path, monkeypatch):hf_cache_settings, "get_hf_cache_paths", lambda: SimpleNamespace(hub_cache = tmp_path))monkeypatch.setattr(mc.time, "sleep", lambda *_: None)+ # No ambient credential, so a 401 here is the repo's answer and is not retried anonymously.+ monkeypatch.setattr(hf_tokens, "_ambient_hf_token", lambda: (True, None))+ monkeypatch.setattr(hf_tokens, "_wire_hf_token", lambda: None)monkeypatch.setattr(llama_cpp.LlamaCppBackend,"_find_llama_server_binary",diff --git a/studio/backend/tests/test_hf_optional_file_probe.py b/studio/backend/tests/test_hf_optional_file_probe.pyindex 828dc937446..dabd741b582 100644--- a/studio/backend/tests/test_hf_optional_file_probe.py+++ b/studio/backend/tests/test_hf_optional_file_probe.py@@ -214,7 +214,9 @@ def _functions(path: Path) -> dict:def _called_names(node: ast.AST) -> set[str]:- """Every function and method name called anywhere inside *node*, nested defs included."""+ """Every function and method name called anywhere inside *node*, nested defs included. A+ read handed to the rejected-token retry (``call_hub_with_anonymous_retry(hf_hub_download,+ ...)``) is called by it, so it counts as called here."""names: set[str] = set()for child in ast.walk(node):if not isinstance(child, ast.Call):@@ -223,6 +225,13 @@ def _called_names(node: ast.AST) -> set[str]:names.add(child.func.id)elif isinstance(child.func, ast.Attribute):names.add(child.func.attr)+ callee = getattr(child.func, "id", None) or getattr(child.func, "attr", None)+ if callee == "call_hub_with_anonymous_retry" and child.args:+ wrapped = child.args[0]+ if isinstance(wrapped, ast.Name):+ names.add(wrapped.id)+ elif isinstance(wrapped, ast.Attribute):+ names.add(wrapped.attr)return namesdiff --git a/studio/backend/tests/test_hub_token_rejected_download.py b/studio/backend/tests/test_hub_token_rejected_download.pynew file mode 100644index 00000000000..33963e56962--- /dev/null+++ b/studio/backend/tests/test_hub_token_rejected_download.py@@ -0,0 +1,418 @@+# 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 token the Hub rejects must not block downloads of repos anyone may read (#11551)."""++from __future__ import annotations++import os+import sys+import types+from pathlib import Path+from unittest.mock import patch++import httpx+import pytest++_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)+if _BACKEND_DIR not in sys.path:+ sys.path.insert(0, _BACKEND_DIR)++from huggingface_hub.errors import GatedRepoError, HfHubHTTPError, RepositoryNotFoundError++from hub.utils import hf_tokens+from hub.utils.hf_tokens import (+ call_hub_with_anonymous_retry,+ is_token_rejection,+ saved_token_rejected,+ token_rejection_scope,+)++BAD = "hf_oauth_" + "x" * 40+REPO = "unsloth/Qwen3-0.6B-GGUF"+VARIANT = "UD-Q4_K_XL"+MAIN = f"{VARIANT}/Qwen3-0.6B-{VARIANT}-00001-of-00002.gguf"+SHARD = f"{VARIANT}/Qwen3-0.6B-{VARIANT}-00002-of-00002.gguf"+++def _http_error(cls, status: int):+ request = httpx.Request("GET", f"https://huggingface.co/api/models/{REPO}")+ response = httpx.Response(status, request = request)+ return cls(f"{status} Client Error. (Request ID: test)", response = response)+++def _rejected():+ return _http_error(RepositoryNotFoundError, 401)+++class _Hub:+ """Answers anonymous reads; refuses any read carrying a token with the OAuth 401."""++ def __init__(+ self,+ *,+ private: bool = False,+ anonymous_error = None,+ ):+ self.private = private+ self.anonymous_error = anonymous_error+ self.tokens: list = []++ def __call__(+ self,+ *args,+ token = None,+ **kwargs,+ ):+ self.tokens.append(token)+ if token is False:+ if self.private:+ raise _rejected()+ if self.anonymous_error is not None:+ raise self.anonymous_error+ return "ok"+ raise _rejected()++[email protected](autouse = True)+def _fresh_scope():+ with token_rejection_scope():+ yield+++def test_rejected_token_retries_once_anonymously_and_is_remembered():+ hub = _Hub()+ assert call_hub_with_anonymous_retry(hub, BAD) == "ok"+ assert hub.tokens == [BAD, False]+ assert saved_token_rejected(BAD)+ assert call_hub_with_anonymous_retry(hub, BAD) == "ok"+ assert hub.tokens == [BAD, False, False]+++def test_private_repo_keeps_the_original_refusal():+ hub = _Hub(private = True)+ with pytest.raises(RepositoryNotFoundError) as info:+ call_hub_with_anonymous_retry(hub, BAD)+ assert hub.tokens == [BAD, False]+ assert info.value.response.status_code == 401+ assert not saved_token_rejected(BAD)+++def test_after_a_rejection_a_private_read_still_asks_with_the_token():+ hf_tokens.note_saved_token_rejected(BAD)+ hub = _Hub(private = True)+ with pytest.raises(RepositoryNotFoundError):+ call_hub_with_anonymous_retry(hub, BAD)+ assert hub.tokens == [False, BAD]+++def test_gated_repo_is_not_opened_by_the_retry():+ hub = _Hub(anonymous_error = _http_error(GatedRepoError, 401))+ with pytest.raises(RepositoryNotFoundError):+ call_hub_with_anonymous_retry(hub, BAD)+ assert hub.tokens == [BAD, False]+++ "error",+ [+ _http_error(HfHubHTTPError, 403),+ _http_error(RepositoryNotFoundError, 404),+ _http_error(HfHubHTTPError, 500),+ httpx.ReadTimeout("slow"),+ OSError("disk"),+ ],+ ids = ["403", "404", "500", "timeout", "oserror"],+)+def test_only_a_401_is_retried(error):+ tokens = []++ def read(*, token):+ tokens.append(token)+ raise error++ with pytest.raises(type(error)):+ call_hub_with_anonymous_retry(read, BAD)+ assert tokens == [BAD]+++def test_an_anonymous_caller_is_never_retried():+ hub = _Hub(private = True)+ with pytest.raises(RepositoryNotFoundError):+ call_hub_with_anonymous_retry(hub, False)+ assert hub.tokens == [False]+++def test_ambient_without_any_host_token_is_not_retried(monkeypatch):+ monkeypatch.setattr(hf_tokens, "_ambient_hf_token", lambda: (True, None))+ monkeypatch.setattr(hf_tokens, "_wire_hf_token", lambda: None)+ hub = _Hub()+ with pytest.raises(RepositoryNotFoundError):+ call_hub_with_anonymous_retry(hub, None)+ assert hub.tokens == [None]+++def test_ambient_host_token_is_retried(monkeypatch):+ monkeypatch.setattr(hf_tokens, "_ambient_hf_token", lambda: (True, BAD))+ monkeypatch.setattr(hf_tokens, "_wire_hf_token", lambda: BAD)+ hub = _Hub()+ assert call_hub_with_anonymous_retry(hub, None) == "ok"+ assert hub.tokens == [None, False]+++def test_scope_forgets_the_verdict():+ with token_rejection_scope():+ call_hub_with_anonymous_retry(_Hub(), BAD)+ assert saved_token_rejected(BAD)+ assert not saved_token_rejected(BAD)+++def test_a_different_token_is_not_marked_rejected():+ call_hub_with_anonymous_retry(_Hub(), BAD)+ assert not saved_token_rejected("hf_" + "y" * 34)+++def test_a_401_rebuilt_from_a_child_process_is_recognised():+ # The download ladder re-raises a child's error from its "<Class>: <message>" text.+ # The same bypass the ladder uses when the constructor demands a response.+ rebuilt = RepositoryNotFoundError.__new__(RepositoryNotFoundError)+ BaseException.__init__(rebuilt, "RepositoryNotFoundError: 401 Client Error. (Request ID: x)")+ assert is_token_rejection(rebuilt)+ assert is_token_rejection(RuntimeError("RepositoryNotFoundError: 401 Client Error. (x)"))+ assert not is_token_rejection(RuntimeError("403 Client Error. (x)"))+ wrapped = RuntimeError("download failed")+ wrapped.__cause__ = _rejected()+ assert is_token_rejection(wrapped)+++def test_xet_fallback_download_retries_anonymously(monkeypatch, tmp_path):+ from utils import hf_xet_fallback++ seen = []++ def shared(repo_id, filename, token, **kwargs):+ seen.append(token)+ if token is False:+ return str(tmp_path / filename)+ raise _rejected()++ monkeypatch.setattr(hf_xet_fallback, "_shared_hf_hub_download_with_xet_fallback", shared)+ path = hf_xet_fallback.hf_hub_download_with_xet_fallback(+ REPO, "model.gguf", BAD, cache_dir = str(tmp_path)+ )+ assert path == str(tmp_path / "model.gguf")+ assert seen == [BAD, False]+++def test_xet_fallback_download_of_a_private_repo_still_fails(monkeypatch, tmp_path):+ from utils import hf_xet_fallback++ seen = []++ def shared(repo_id, filename, token, **kwargs):+ seen.append(token)+ raise _rejected()++ monkeypatch.setattr(hf_xet_fallback, "_shared_hf_hub_download_with_xet_fallback", shared)+ with pytest.raises(RepositoryNotFoundError):+ hf_xet_fallback.hf_hub_download_with_xet_fallback(+ REPO, "model.gguf", BAD, cache_dir = str(tmp_path)+ )+ assert seen == [BAD, False]+++def _listing(+ repo_id,+ *,+ token = None,+ **_kwargs,+):+ if token is not False:+ raise _rejected()+ return [MAIN, SHARD, "README.md"]+++def test_variant_resolution_uses_the_anonymous_listing(monkeypatch):+ from core.inference import llama_cpp++ monkeypatch.setattr(llama_cpp, "_cached_variant_resolution", lambda *a, **k: (None, []))+ with patch("huggingface_hub.list_repo_files", _listing):+ name, shards = llama_cpp._resolve_variant_gguf_files(REPO, VARIANT, BAD)+ assert name == MAIN+ assert shards == [SHARD]+++def _backend_download(monkeypatch, tmp_path, *, private: bool):+ from core.inference import llama_cpp+ from utils import hf_xet_fallback++ monkeypatch.setattr(+ "utils.hf_cache_settings.get_hf_cache_paths",+ lambda: types.SimpleNamespace(hub_cache = tmp_path),+ )+ monkeypatch.setattr(llama_cpp, "cached_gguf_for_load", lambda *a, **k: None)+ monkeypatch.setattr(llama_cpp, "_cached_variant_resolution", lambda *a, **k: (None, []))+ downloads = []++ def shared(repo_id, filename, token, **kwargs):+ downloads.append((filename, token))+ if token is False and not private:+ return str(tmp_path / filename)+ raise _rejected()++ def paths_info(+ repo_id,+ paths,+ *,+ token = None,+ **_kwargs,+ ):+ if token is not False or private:+ raise _rejected()+ return [types.SimpleNamespace(path = p, size = 4) for p in paths]++ def listing(+ repo_id,+ *,+ token = None,+ **kwargs,+ ):+ if private:+ raise _rejected()+ return _listing(repo_id, token = token, **kwargs)++ monkeypatch.setattr(hf_xet_fallback, "_shared_hf_hub_download_with_xet_fallback", shared)+ with (+ patch("huggingface_hub.list_repo_files", listing),+ patch("huggingface_hub.get_paths_info", paths_info),+ patch("huggingface_hub.try_to_load_from_cache", lambda *a, **k: None),+ ):+ backend = llama_cpp.LlamaCppBackend()+ try:+ return backend._download_gguf(hf_repo = REPO, hf_variant = VARIANT, hf_token = BAD), downloads+ except Exception as exc:+ return exc, downloads+++def test_gguf_download_recovers_from_a_rejected_token(monkeypatch, tmp_path):+ out, downloads = _backend_download(monkeypatch, tmp_path, private = False)+ assert out == str(tmp_path / MAIN)+ assert downloads == [(MAIN, False), (SHARD, False)]+++def test_gguf_download_of_a_private_repo_names_the_token(monkeypatch, tmp_path):+ out, _ = _backend_download(monkeypatch, tmp_path, private = True)+ assert isinstance(out, RuntimeError)+ assert "token may have expired or been revoked" in str(out)+++def worker(monkeypatch):+ from hub.workers import hf_download++ monkeypatch.setattr(hf_download, "_REJECTED_TOKEN", None)+ monkeypatch.setattr(hf_download, "_METADATA_RETRY_DELAY", 0)+ return hf_download+++def test_worker_metadata_retries_and_the_download_follows(worker):+ seen = []++ def model_info(+ repo_id,+ *,+ token = None,+ **_kwargs,+ ):+ seen.append(token)+ if token is False:+ return types.SimpleNamespace(sha = "a" * 40, siblings = [])+ raise _rejected()++ with patch("huggingface_hub.model_info", model_info):+ info = worker._model_info_with_retry(REPO, BAD)+ assert info.sha == "a" * 40+ assert seen == [BAD, False]+ assert worker._hf_token_arg(BAD) is False+ assert worker._hf_token_arg("hf_" + "y" * 34) == "hf_" + "y" * 34+++def test_worker_private_repo_keeps_the_token(worker):+ seen = []++ def model_info(+ repo_id,+ *,+ token = None,+ **_kwargs,+ ):+ seen.append(token)+ raise _rejected()++ with patch("huggingface_hub.model_info", model_info):+ with pytest.raises(RepositoryNotFoundError):+ worker._model_info_with_retry(REPO, BAD)+ # The second attempt may ask anonymously first, having learned the token is refused.+ assert sorted(map(str, seen)) == sorted(map(str, [BAD, False, BAD, False]))+ assert worker._hf_token_arg(BAD) == BAD+++def test_worker_dataset_metadata_retries(worker):+ seen = []++ def dataset_info(+ self,+ repo_id,+ *,+ token = None,+ **_kwargs,+ ):+ seen.append(token)+ if token is False:+ return types.SimpleNamespace(sha = "b" * 40)+ raise _rejected()++ with patch("huggingface_hub.HfApi.dataset_info", dataset_info):+ info = worker._dataset_info_with_retry("unsloth/data", BAD)+ assert info.sha == "b" * 40+ assert seen == [BAD, False]+ assert worker._hf_token_arg(BAD) is False+++def test_inference_worker_drops_a_rejected_token_from_its_env(monkeypatch):+ from core.inference import worker++ env = {"HF_TOKEN": BAD, "HUGGING_FACE_HUB_TOKEN": BAD}+ monkeypatch.setattr(os, "environ", env)+ config = {"model_name": REPO, "hf_token": BAD}+ call_hub_with_anonymous_retry(_Hub(), BAD)+ worker._drop_a_rejected_token(config)+ assert worker._config_hf_token(config) is False+ assert "HF_TOKEN" not in env and "HUGGING_FACE_HUB_TOKEN" not in env+ assert env["HF_HUB_DISABLE_IMPLICIT_TOKEN"] == "1"+++def test_inference_worker_keeps_an_accepted_token(monkeypatch):+ from core.inference import worker++ env = {"HF_TOKEN": BAD}+ monkeypatch.setattr(os, "environ", env)+ config = {"model_name": REPO, "hf_token": BAD}+ worker._drop_a_rejected_token(config)+ assert worker._config_hf_token(config) == BAD+ assert env == {"HF_TOKEN": BAD}+++def test_inference_worker_load_scope_resets(monkeypatch):+ from core.inference import worker++ seen = []++ def load(backend, config, resp_queue):+ call_hub_with_anonymous_retry(_Hub(), BAD)+ seen.append(saved_token_rejected(BAD))++ worker._in_token_rejection_scope(load)(None, {}, None)+ assert worker._handle_load.__wrapped__ is not None+ assert seen == [True]+ assert not saved_token_rejected(BAD)diff --git a/studio/backend/tests/test_hub_token_rejected_retry.py b/studio/backend/tests/test_hub_token_rejected_retry.pynew file mode 100644index 00000000000..342159d9109--- /dev/null+++ b/studio/backend/tests/test_hub_token_rejected_retry.py@@ -0,0 +1,701 @@+# 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 token the Hub refuses (401 on every read) must not make public repos unreadable (#11551)."""++import re+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 (+ HUB_TOKEN_REJECTED_WARNING,+ call_with_anonymous_retry,+ collecting_hub_token_rejections,+ is_rejected_credential_error,+)+from utils.models.model_config import (+ GgufRepoUnreadableError,+ GgufVariantInfo,+ ModelConfig,+ _hub_model_info,+ shared_hub_model_info,+)+from utils.utils import is_hf_authentication_error++REPO = "unsloth/Qwen3-0.6B-GGUF"+GGUF = "Qwen3-0.6B-UD-Q4_K_XL.gguf"+OAUTH = "hf_oauth_" + "a" * 40+++class RepositoryNotFoundError(Exception):+ """Matched by type name, as the helpers do."""++ def __init__(+ self,+ message = "401 Client Error",+ status_code = 401,+ reason = None,+ ):+ super().__init__(message)+ headers = {"X-Error-Message": reason} if reason else {}+ self.response = SimpleNamespace(status_code = status_code, headers = headers)+++class HTTPError(Exception):+ def __init__(self, status_code):+ super().__init__(f"{status_code} Client Error")+ self.response = SimpleNamespace(status_code = status_code, headers = {})+++def _refused_with_a_token(calls):+ """The Hub's answer to a rejected OAuth token: 401 for any read that carries it."""++ def read(token):+ calls.append(token)+ if token is False:+ return "public answer"+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")++ return read++[email protected](autouse = True)+def _no_ambient_credential(monkeypatch):+ monkeypatch.setattr(hf_tokens, "_ambient_hf_token", lambda: (True, None))+ monkeypatch.setattr(hf_tokens, "_wire_hf_token", lambda: None)+++def test_a_refused_token_is_retried_once_anonymously():+ calls = []+ with collecting_hub_token_rejections() as rejections:+ assert call_with_anonymous_retry(_refused_with_a_token(calls), OAUTH) == "public answer"+ assert calls == [OAUTH, False]+ assert rejections.recovered and not rejections.refused+++def test_the_next_read_in_the_same_request_skips_the_refused_token():+ calls = []+ with collecting_hub_token_rejections():+ call_with_anonymous_retry(_refused_with_a_token(calls), OAUTH)+ call_with_anonymous_retry(_refused_with_a_token(calls), OAUTH)+ assert calls == [OAUTH, False, False]+++def test_a_new_request_asks_with_the_token_again():+ calls = []+ with collecting_hub_token_rejections():+ call_with_anonymous_retry(_refused_with_a_token(calls), OAUTH)+ with collecting_hub_token_rejections():+ call_with_anonymous_retry(_refused_with_a_token(calls), OAUTH)+ assert calls == [OAUTH, False, OAUTH, False]+++def test_another_credential_in_the_request_is_not_skipped():+ calls = []+ other = "hf_" + "b" * 34+ with collecting_hub_token_rejections():+ call_with_anonymous_retry(_refused_with_a_token(calls), OAUTH)+ call_with_anonymous_retry(_refused_with_a_token(calls), other)+ assert calls == [OAUTH, False, other, False]+++def test_an_accepted_token_reads_once():+ calls = []++ def read(token):+ calls.append(token)+ return "private answer"++ with collecting_hub_token_rejections() as rejections:+ assert call_with_anonymous_retry(read, OAUTH) == "private answer"+ assert calls == [OAUTH]+ assert not rejections.rejected+++def test_an_anonymous_401_is_the_repos_answer_and_is_not_retried():+ calls = []++ def read(token):+ calls.append(token)+ raise RepositoryNotFoundError()++ with pytest.raises(RepositoryNotFoundError):+ call_with_anonymous_retry(read, False)+ assert calls == [False]+++def test_no_credential_at_all_is_not_retried():+ calls = []++ def read(token):+ calls.append(token)+ raise RepositoryNotFoundError()++ with pytest.raises(RepositoryNotFoundError):+ call_with_anonymous_retry(read, None)+ assert calls == [None]+++def test_an_ambient_credential_is_retried(monkeypatch):+ monkeypatch.setattr(hf_tokens, "_ambient_hf_token", lambda: (True, OAUTH))+ monkeypatch.setattr(hf_tokens, "_wire_hf_token", lambda: OAUTH)+ calls = []++ def read(token):+ calls.append(token)+ if token is None:+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")+ return "public answer"++ assert call_with_anonymous_retry(read, None) == "public answer"+ assert calls == [None, False]+++ "failure",+ [HTTPError(403), HTTPError(404), HTTPError(429), TimeoutError("read timed out")],+ ids = ["403", "404", "429", "timeout"],+)+def test_other_failures_are_not_retried(failure):+ calls = []++ def read(token):+ calls.append(token)+ raise failure++ with pytest.raises(type(failure)):+ call_with_anonymous_retry(read, OAUTH)+ assert calls == [OAUTH]+ assert not is_rejected_credential_error(failure, OAUTH)+++def test_a_private_repo_raises_the_original_error_and_notes_the_refusal():+ calls = []+ original = RepositoryNotFoundError(reason = "OAuth token verification failed")++ def read(token):+ calls.append(token)+ if token is False:+ raise RepositoryNotFoundError(reason = "Invalid username or password.")+ raise original++ with collecting_hub_token_rejections() as rejections:+ with pytest.raises(RepositoryNotFoundError) as exc_info:+ call_with_anonymous_retry(read, OAUTH)+ assert exc_info.value is original+ assert calls == [OAUTH, False]+ assert rejections.refused and not rejections.recovered+++def test_the_model_info_scope_keeps_each_credential_its_own(monkeypatch):+ calls = []++ def model_info(repo_id, **kwargs):+ token = kwargs.get("token")+ calls.append(token)+ if token == OAUTH:+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")+ return SimpleNamespace(siblings = [], answered_for = token)++ monkeypatch.setattr(huggingface_hub, "model_info", model_info)+ other = "hf_" + "b" * 34+ with shared_hub_model_info(), collecting_hub_token_rejections():+ assert _hub_model_info(REPO, OAUTH).answered_for is False+ assert _hub_model_info(REPO, OAUTH).answered_for is False+ assert _hub_model_info(REPO, other).answered_for == other+ assert calls == [OAUTH, False, other]+++def _gguf_env(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(+ llama_cpp.LlamaCppBackend,+ "_find_llama_server_binary",+ staticmethod(lambda *, include_denied = False: "/fake/llama-server"),+ )+ monkeypatch.setattr(llama_cpp, "cached_gguf_for_load", lambda repo, variant, **kwargs: None)+ monkeypatch.setattr(+ mc,+ "list_gguf_variants",+ lambda identifier, hf_token = None: (+ [GgufVariantInfo(filename = GGUF, quant = "UD-Q4_K_XL", size_bytes = 1)],+ False,+ ),+ )+ 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)+ return tmp_path+++def _hub_refusing_the_token(calls, *, public = True):+ def model_info(repo_id, **kwargs):+ token = kwargs.get("token")+ calls.append(token)+ if token is False and public:+ return SimpleNamespace(siblings = [SimpleNamespace(rfilename = GGUF)])+ reason = "OAuth token verification failed" if token else "Invalid username or password."+ raise RepositoryNotFoundError(reason = reason)++ return model_info+++def test_a_public_gguf_repo_loads_despite_a_refused_token(monkeypatch, _gguf_env):+ calls = []+ monkeypatch.setattr(huggingface_hub, "model_info", _hub_refusing_the_token(calls))+ with collecting_hub_token_rejections() as rejections:+ config = ModelConfig.from_identifier(REPO, hf_token = OAUTH)+ assert config.is_gguf and config.gguf_hf_repo == REPO+ assert calls[:2] == [OAUTH, False]+ assert OAUTH not in calls[2:]+ assert rejections.recovered+++def test_a_repo_no_one_can_read_names_the_refused_token(monkeypatch, _gguf_env):+ snapshot = _gguf_env / f"models--{REPO.replace('/', '--')}" / "snapshots" / ("a" * 40)+ snapshot.mkdir(parents = True)+ (snapshot / GGUF).write_bytes(b"cached")+ calls = []+ monkeypatch.setattr(huggingface_hub, "model_info", _hub_refusing_the_token(calls, public = False))+ with pytest.raises(GgufRepoUnreadableError) as exc_info:+ ModelConfig.from_identifier(REPO, hf_token = OAUTH)+ msg = str(exc_info.value)+ assert "rejected the saved token" in msg and "Settings" in msg+ assert "HTTP 401" in msg+ assert calls == [OAUTH, False]+++def test_hub_gguf_listing_retries_anonymously(monkeypatch):+ from hub.utils import gguf as hub_gguf++ calls = []++ class FakeApi:+ def __init__(self, token = None):+ self.token = token++ def model_info(self, repo_id, **kwargs):+ calls.append(self.token)+ if self.token is not False:+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")+ return SimpleNamespace(siblings = [SimpleNamespace(rfilename = GGUF, size = 1)])++ monkeypatch.setattr(huggingface_hub, "HfApi", FakeApi)+ monkeypatch.setattr(hub_gguf, "_env_offline", lambda: False)+ variants, _has_vision, _siblings = hub_gguf.list_gguf_variants(REPO, hf_token = OAUTH)+ assert [v.filename for v in variants] == [GGUF]+ assert calls == [OAUTH, False]+++def test_a_repo_level_401_is_not_reported_as_a_bad_token():+ assert not is_hf_authentication_error(+ RepositoryNotFoundError(reason = "Invalid username or password.")+ )+++def test_a_refused_token_is_reported_as_one():+ assert is_hf_authentication_error(+ RepositoryNotFoundError(reason = "OAuth token verification failed")+ )+ with collecting_hub_token_rejections() as rejections:+ rejections.refused = True+ assert is_hf_authentication_error(RepositoryNotFoundError())+++def test_a_plain_401_still_reads_as_authentication():+ assert is_hf_authentication_error(HTTPError(401))+++def test_the_load_response_carries_the_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.recovered = True+ warned = _with_token_rejected_warning(response, rejections)+ assert warned.memory_warning == HUB_TOKEN_REJECTED_WARNING+++JWS = "hf_oauth_eyJhbGciOiJFUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.c2lnbmF0dXJlLXZhbHVl"+++def test_oauth_tokens_are_redacted_whole():+ from core.inference.llama_cpp import LlamaCppBackend+ from core.research import redaction as research_redaction+ from hub.utils.download_registry import scrub_secrets+ from utils.log_redaction import redact_log_text++ line = f"token={JWS} failed"+ assert "eyJ" not in redact_log_text(f"sent {JWS} to the Hub")+ assert "eyJ" not in scrub_secrets(line)+ assert any(p.fullmatch(JWS) for p in LlamaCppBackend._SECRET_VALUE_RES)+ assert research_redaction._QUERY_OPAQUE_TOKEN.search(JWS).group(0).startswith("hf_oauth_")+ assert re.search(r"hf_oauth_\S*eyJ", redact_log_text(line)) is None+++def test_an_ambient_token_the_hub_never_sees_is_not_blamed(monkeypatch):+ # huggingface_hub keeps the ambient token off the wire, so the 401 is about the repo.+ monkeypatch.setattr(hf_tokens, "_ambient_hf_token", lambda: (True, OAUTH))+ monkeypatch.setattr(hf_tokens, "_wire_hf_token", lambda: OAUTH)+ monkeypatch.setattr(huggingface_hub.constants, "HF_HUB_DISABLE_IMPLICIT_TOKEN", True)+ calls = []+ with collecting_hub_token_rejections() as rejections:+ with pytest.raises(RepositoryNotFoundError):+ call_with_anonymous_retry(_refused_with_a_token(calls), None)+ assert calls == [None]+ assert not rejections.rejected+++def test_the_remote_code_scan_reads_public_files_without_a_refused_token(monkeypatch):+ from hub.utils.hf_tokens import anonymous_retrying++ calls = []++ def hf_hub_download(+ repo_id,+ filename,+ *,+ token = None,+ cache_dir = None,+ ):+ calls.append(token)+ if token is False:+ return f"/cache/{repo_id}/{filename}"+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")++ download = anonymous_retrying(hf_hub_download)+ assert download(REPO, "config.json", token = OAUTH, cache_dir = "x") == f"/cache/{REPO}/config.json"+ assert calls == [OAUTH, False]+++def test_an_anonymous_download_401_does_not_blame_a_saved_token():+ exc = RepositoryNotFoundError()+ assert not is_rejected_credential_error(exc, False)+ assert is_rejected_credential_error(exc, OAUTH)+++def test_an_alias_huggingface_hub_never_sends_is_not_blamed(monkeypatch):+ # huggingface_hub's get_token() never reads HF_HUB_TOKEN, so this read went out anonymously.+ monkeypatch.setattr(hf_tokens, "_ambient_hf_token", lambda: (True, OAUTH))+ monkeypatch.setattr(hf_tokens, "_wire_hf_token", lambda: None)+ calls = []+ with pytest.raises(RepositoryNotFoundError):+ call_with_anonymous_retry(_refused_with_a_token(calls), None)+ assert calls == [None]+++def test_a_cancelled_anonymous_retry_stays_a_cancellation():+ def read(token):+ if token is False:+ raise RuntimeError("Cancelled")+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")++ with collecting_hub_token_rejections():+ with pytest.raises(RuntimeError, match = "Cancelled"):+ call_with_anonymous_retry(read, OAUTH)+++def test_the_pre_import_config_reads_retry_without_a_refused_token(monkeypatch):+ import io+ import json+ import urllib.error++ import utils.transformers_version as tv++ sent = []++ class _Resp(io.BytesIO):+ def __enter__(self):+ return self++ def __exit__(self, *exc):+ return False++ def urlopen(req, timeout):+ auth = req.get_header("Authorization")+ sent.append(auth)+ if auth:+ raise urllib.error.HTTPError(req.full_url, 401, "Unauthorized", {}, None)+ return _Resp(json.dumps({"model_type": "qwen3"}).encode())++ monkeypatch.setattr(tv, "_hf_urlopen", urlopen)+ assert tv._hf_json("https://huggingface.co/x/resolve/main/config.json", OAUTH) == {+ "model_type": "qwen3"+ }+ assert sent == [f"Bearer {OAUTH}", None]+ sent.clear()+ tv._hf_json("https://huggingface.co/x/resolve/main/config.json", None)+ assert sent == [None]+++def test_an_anonymous_first_success_after_a_transient_failure_is_recorded():+ from hub.utils.hf_tokens import saved_token_rejected++ attempts = {"anonymous": 0}++ def read(token):+ if token is False:+ attempts["anonymous"] += 1+ if attempts["anonymous"] == 1:+ raise TimeoutError("read timed out")+ return "public answer"+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")++ with collecting_hub_token_rejections() as rejections:+ with pytest.raises(RepositoryNotFoundError):+ call_with_anonymous_retry(read, OAUTH)+ assert not saved_token_rejected(OAUTH)+ assert call_with_anonymous_retry(read, OAUTH) == "public answer"+ assert saved_token_rejected(OAUTH)+ assert rejections.recovered+++def test_admission_sizing_counts_companions_when_the_token_is_refused(monkeypatch):+ import routes.inference as inference_routes++ def model_info(+ repo,+ token = None,+ files_metadata = False,+ ):+ if token is not False:+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")+ return SimpleNamespace(+ siblings = [+ SimpleNamespace(rfilename = "Qwen3-VL-2B-Q4_K_M.gguf", size = 1_000_000_000),+ SimpleNamespace(rfilename = "mmproj-F16.gguf", size = 800_000_000),+ ]+ )++ monkeypatch.setattr(huggingface_hub, "model_info", model_info)+ sized = inference_routes._remote_gguf_companion_bytes(+ "unsloth/Qwen3-VL-2B-Instruct-GGUF", hf_token = OAUTH, include_mmproj = True+ )+ assert sized == 800_000_000+++def test_training_admission_reads_safetensors_totals_without_a_refused_token(monkeypatch):+ from utils.hardware import hardware++ monkeypatch.setattr("utils.utils.hf_env_offline", lambda: False)++ def model_info(repo, token = None):+ if token is not False:+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")+ return SimpleNamespace(safetensors = {"total": 4_400_000_000})++ monkeypatch.setattr(huggingface_hub, "model_info", model_info)+ assert hardware._get_hf_safetensors_total_params("org/public-vlm", OAUTH) == 4_400_000_000+++def test_the_malware_status_is_read_without_a_refused_token(monkeypatch):+ from utils.security import file_security++ status = {"filesWithIssues": [{"path": "pytorch_model.bin", "level": "unsafe"}]}++ def model_info(+ repo,+ revision = None,+ token = None,+ securityStatus = False,+ timeout = None,+ ):+ if token is not False:+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")+ return SimpleNamespace(security_repo_status = status)++ monkeypatch.setattr(huggingface_hub, "model_info", model_info)+ assert file_security._fetch_security_status("org/public-model", OAUTH) == status+++def test_an_explicit_remote_drafter_is_sized_without_a_refused_token(monkeypatch):+ import routes.inference as inference_routes++ def model_info(+ repo,+ token = None,+ files_metadata = False,+ ):+ if token is not False:+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")+ return SimpleNamespace(+ siblings = [SimpleNamespace(rfilename = "drafter-Q8_0.gguf", size = 30 * 1024**3)]+ )++ monkeypatch.setattr(huggingface_hub, "model_info", model_info)+ sized = inference_routes._remote_drafter_repo_bytes("org/big-drafter-GGUF", hf_token = OAUTH)+ assert sized == 30 * 1024**3+++def test_security_weight_indexes_are_read_without_a_refused_token(monkeypatch, tmp_path):+ import json++ import utils.hf_probe+ from utils.security import file_security++ index = tmp_path / "model.safetensors.index.json"+ index.write_text(json.dumps({"weight_map": {"w": "model-00001-of-00002.safetensors"}}))++ def hf_hub_download(+ repo,+ filename,+ revision = None,+ token = None,+ cache_dir = None,+ ):+ if token is not False:+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")+ return str(index)++ monkeypatch.setattr(huggingface_hub, "hf_hub_download", hf_hub_download)+ monkeypatch.setattr(+ utils.hf_probe,+ "hf_file_definitely_absent",+ lambda repo, filename, **k: filename != "model.safetensors.index.json",+ )+ paths = file_security._indexed_shard_paths("org/public-model", OAUTH)+ assert paths == {"model-00001-of-00002.safetensors"}+++def test_the_gguf_sliding_window_config_is_read_without_a_refused_token(monkeypatch, tmp_path):+ import json++ import utils.hf_probe+ from core.inference import llama_cpp++ cfg = tmp_path / "config.json"+ cfg.write_text(json.dumps({"sliding_window_pattern": 6}))++ def hf_hub_download(+ repo,+ filename,+ repo_type = None,+ token = None,+ cache_dir = None,+ ):+ if token is not False:+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")+ return str(cfg)++ monkeypatch.setattr(hf_tokens, "_ambient_hf_token", lambda: (True, OAUTH))+ monkeypatch.setattr(hf_tokens, "_wire_hf_token", lambda: OAUTH)+ monkeypatch.setattr(huggingface_hub, "hf_hub_download", hf_hub_download)+ monkeypatch.setattr(utils.hf_probe, "hf_file_definitely_absent", lambda *a, **k: False)+ assert llama_cpp._fetch_swa_entry_from_hf("org/public-model") == 6+++def test_a_private_repo_after_a_public_recovery_is_reported_as_refused():+ # A public adapter recovered anonymously, then a private base no one can read.+ calls = []++ def private(token):+ calls.append(token)+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")++ with collecting_hub_token_rejections() as rejections:+ call_with_anonymous_retry(_refused_with_a_token(calls), OAUTH)+ with pytest.raises(RepositoryNotFoundError):+ call_with_anonymous_retry(private, OAUTH)+ assert calls == [OAUTH, False, False, OAUTH]+ assert rejections.refused+++def test_an_env_flag_set_after_import_does_not_stop_the_retry(monkeypatch):+ monkeypatch.setattr(hf_tokens, "_ambient_hf_token", lambda: (True, OAUTH))+ monkeypatch.setattr(hf_tokens, "_wire_hf_token", lambda: OAUTH)+ monkeypatch.setattr(huggingface_hub.constants, "HF_HUB_DISABLE_IMPLICIT_TOKEN", False)+ monkeypatch.setenv("HF_HUB_DISABLE_IMPLICIT_TOKEN", "1")+ calls = []++ def read(token):+ calls.append(token)+ if token is None:+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")+ return "public answer"++ assert call_with_anonymous_retry(read, None) == "public answer"+ assert calls == [None, False]+++def test_child_probes_read_anonymously_once_the_token_was_refused():+ from hub.utils.hf_tokens import apply_token_to_child_env++ with collecting_hub_token_rejections():+ call_with_anonymous_retry(_refused_with_a_token([]), OAUTH)+ env = {"HF_TOKEN": "operator"}+ apply_token_to_child_env(env, OAUTH)+ assert "HF_TOKEN" not in env+ assert env["HF_HUB_DISABLE_IMPLICIT_TOKEN"] == "1"+ env = {}+ apply_token_to_child_env(env, OAUTH)+ assert env["HF_TOKEN"] == OAUTH, "a new request asks with the token again"+++def test_the_pre_import_json_reader_shares_the_request_refusal(monkeypatch):+ import io+ import urllib.error++ from utils import transformers_version as tv++ sent = []++ def urlopen(request, timeout):+ auth = request.get_header("Authorization")+ sent.append(auth)+ if auth:+ raise urllib.error.HTTPError(request.full_url, 401, "Unauthorized", {}, None)+ return io.BytesIO(b'{"model_type": "llama"}')++ monkeypatch.setattr(tv, "_hf_urlopen", urlopen)+ with collecting_hub_token_rejections() as rejections:+ assert tv._hf_json("https://huggingface.co/a/resolve/main/config.json", OAUTH)+ assert tv._hf_json("https://huggingface.co/a/resolve/main/tokenizer_config.json", OAUTH)+ assert sent == [f"Bearer {OAUTH}", None, None]+ assert rejections.recovered+++def test_an_anonymous_missing_file_answer_is_kept():+ from huggingface_hub.utils import EntryNotFoundError++ def read(token):+ if token is False:+ raise EntryNotFoundError("404 Client Error: Entry Not Found")+ raise RepositoryNotFoundError(reason = "OAuth token verification failed")++ with collecting_hub_token_rejections() as rejections:+ with pytest.raises(EntryNotFoundError):+ call_with_anonymous_retry(read, OAUTH)+ assert rejections.recovered+++def test_a_later_load_in_the_same_worker_gets_its_token_environment_back(monkeypatch):+ import os++ from core.inference import worker++ monkeypatch.setenv("HF_TOKEN", "hf_" + "n" * 34)+ monkeypatch.delenv("HF_HUB_DISABLE_IMPLICIT_TOKEN", raising = False)+ monkeypatch.setattr(worker, "_TOKEN_ENV_BEFORE_ANONYMOUS_LOAD", None)+ config = {"hf_token": OAUTH, "model_name": "org/public"}+ with collecting_hub_token_rejections():+ call_with_anonymous_retry(_refused_with_a_token([]), OAUTH)+ worker._drop_a_rejected_token(config)+ assert "HF_TOKEN" not in os.environ+ assert os.environ["HF_HUB_DISABLE_IMPLICIT_TOKEN"] == "1"+ worker._restore_token_environment()+ assert os.environ["HF_TOKEN"] == "hf_" + "n" * 34+ assert "HF_HUB_DISABLE_IMPLICIT_TOKEN" not in os.environdiff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.pyindex 4413af1b1af..4383d301ce0 100644--- a/studio/backend/utils/hardware/hardware.py+++ b/studio/backend/utils/hardware/hardware.py@@ -5059,8 +5059,10 @@ def _get_hf_safetensors_total_params(return Nonefrom huggingface_hub import model_info as hf_model_info+ from hub.utils.hf_tokens import call_hub_with_anonymous_retry- info = hf_model_info(model_name, token = hf_token)+ # A refused token must not drop this to the text-tower-only config estimate.+ info = call_hub_with_anonymous_retry(hf_model_info, hf_token, model_name)safetensors = getattr(info, "safetensors", None)if isinstance(safetensors, dict):total = safetensors.get("total")diff --git a/studio/backend/utils/hf_probe.py b/studio/backend/utils/hf_probe.pyindex 501ad6043a3..a0223a35d57 100644--- a/studio/backend/utils/hf_probe.py+++ b/studio/backend/utils/hf_probe.py@@ -33,6 +33,8 @@ def hf_file_definitely_absent(try:from huggingface_hub import get_hf_file_metadata, hf_hub_urlfrom huggingface_hub.errors import EntryNotFoundError, LocalEntryNotFoundError++ from hub.utils.hf_tokens import call_with_anonymous_retryexcept Exception: # noqa: BLE001 - an unimportable hub is the caller's problem, not oursreturn False@@ -43,7 +45,8 @@ def hf_file_definitely_absent(repo_type = repo_type,revision = revision,)- get_hf_file_metadata(url, token = token)+ # A refused credential is not an answer about the file: ask once more without it.+ call_with_anonymous_retry(lambda sent: get_hf_file_metadata(url, token = sent), token)except LocalEntryNotFoundError:return Falseexcept EntryNotFoundError:diff --git a/studio/backend/utils/hf_xet_fallback.py b/studio/backend/utils/hf_xet_fallback.pyindex 00837ebeb1b..3688a08b828 100644--- a/studio/backend/utils/hf_xet_fallback.py+++ b/studio/backend/utils/hf_xet_fallback.py@@ -1004,19 +1004,25 @@ def hf_hub_download_with_xet_fallback(optional["stall_timeout"] = stall_timeoutif interval is not None:optional["interval"] = interval- return _shared_hf_hub_download_with_xet_fallback(- repo_id,- filename,+ from hub.utils.hf_tokens import call_with_anonymous_retry++ # The 401 comes on the metadata HEAD, before any byte is written.+ return call_with_anonymous_retry(+ lambda token: _shared_hf_hub_download_with_xet_fallback(+ repo_id,+ filename,+ token,+ cancel_event = cancel_event,+ repo_type = repo_type,+ revision = revision,+ **optional,+ grace_period = grace_period,+ on_status = on_status,+ force_download = force_download,+ cache_dir = cache_dir,+ prepare_for_http_fn = partial(_studio_prepare_for_http, cache_dir = cache_dir),+ ),token,- cancel_event = cancel_event,- repo_type = repo_type,- revision = revision,- **optional,- grace_period = grace_period,- on_status = on_status,- force_download = force_download,- cache_dir = cache_dir,- prepare_for_http_fn = partial(_studio_prepare_for_http, cache_dir = cache_dir),)diff --git a/studio/backend/utils/log_redaction.py b/studio/backend/utils/log_redaction.pyindex 85e5740c159..84f360f93eb 100644--- a/studio/backend/utils/log_redaction.py+++ b/studio/backend/utils/log_redaction.py@@ -101,7 +101,7 @@ _KEY_START = r"(?<![A-Za-z0-9])"_PATTERNS: tuple[tuple[re.Pattern[str], str], ...] = (# Hugging Face- (re.compile(r"\bhf_[A-Za-z0-9]{20,}"), "hf_" + REDACTED),+ (re.compile(r"\bhf_(?:oauth_[A-Za-z0-9._~+/=-]{20,}|[A-Za-z0-9]{20,})"), "hf_" + REDACTED),# OpenAI and other sk- keys (project, Anthropic, OpenRouter). Not a word boundary: that also fires after a hyphen, eating checkpoint-sk-9f8a... in a filename.(re.compile(r"(?<![A-Za-z0-9-])sk-(?:proj-|ant-api\d{2}-|or-v1-)?[A-Za-z0-9_-]{16,}"),diff --git a/studio/backend/utils/models/model_config.py b/studio/backend/utils/models/model_config.pyindex 083775073fe..c189176f737 100644--- a/studio/backend/utils/models/model_config.py+++ b/studio/backend/utils/models/model_config.py@@ -27,6 +27,10 @@ from hub.utils.hf_tokens import (HfTokenArg,apply_token_to_child_env,cache_reads_authorized,+ call_with_anonymous_retry,+ collecting_hub_token_rejections,+ HUB_TOKEN_REJECTED_ERROR,+ is_rejected_credential_error,is_anonymous,normalize_token,)@@ -862,7 +866,12 @@ def _raw_config_has_vision_config(is_cached = lambda: _config_json_already_cached(model_name, revision),):return None- config_path = Path(hf_hub_download(**download_kwargs))+ config_path = Path(+ call_with_anonymous_retry(+ lambda token: hf_hub_download(**{**download_kwargs, "token": token}),+ hf_token,+ )+ )config = json.loads(config_path.read_text(encoding = "utf-8-sig"))architectures = config.get("architectures") or []model_type = config.get("model_type")@@ -1201,7 +1210,9 @@ def _hub_model_info(# Shared, so whichever probe reads first fixes the bound the rest inherit."timeout": _HUB_MODEL_INFO_TIMEOUT if timeout is None else timeout,}- info = hf_model_info(repo_id, **kwargs)+ info = call_with_anonymous_retry(+ lambda token: hf_model_info(repo_id, **{**kwargs, "token": token}), hf_token+ )if scope is not None:scope[key] = info@@ -3395,7 +3406,12 @@ def _looks_like_gguf_repo(repo_id: str, gguf_variant: Optional[str] = None) -> b)-def _gguf_repo_unreadable_message(repo_id: str, error: Optional[Exception]) -> str:+def _gguf_repo_unreadable_message(+ repo_id: str,+ error: Optional[Exception],+ *,+ token_rejected: bool = False,+) -> str:if error is None:return (f"Could not load the GGUF repo '{repo_id}': Studio is offline and the repo is not "@@ -3408,6 +3424,13 @@ def _gguf_repo_unreadable_message(repo_id: str, error: Optional[Exception]) -> scause += f", HTTP {status}"if first_line:cause += f": {first_line}"+ if token_rejected:+ # Anonymous access could not answer either: private, gated or missing without it.+ return (+ f"Could not read the GGUF repo '{repo_id}' from Hugging Face ({cause}). "+ f"{HUB_TOKEN_REJECTED_ERROR} If Studio reaches the Hub through a proxy, mirror "+ "or HF_ENDPOINT, check that as well."+ )return (f"Could not read the GGUF repo '{repo_id}' from Hugging Face ({cause}). ""Unsloth needs the repo's file list to pick a GGUF file. Check the Hugging Face "@@ -3929,11 +3952,14 @@ def get_base_model_from_lora_identifier(last_exc = Nonefor _attempt in range(2): # one retry: a transient blip must not skip the basetry:- cfg_path = hf_hub_download(- identifier,- "adapter_config.json",- token = normalize_token(hf_token),- cache_dir = active_hf_hub_cache(),+ cfg_path = call_with_anonymous_retry(+ lambda token: hf_hub_download(+ identifier,+ "adapter_config.json",+ token = token,+ cache_dir = active_hf_hub_cache(),+ ),+ normalize_token(hf_token),)except (EntryNotFoundError, RepositoryNotFoundError):# No adapter_config.json -> not a resolvable LoRA; caller scans the identifier.@@ -4354,14 +4380,20 @@ class ModelConfig:detect_failures: List[Exception] = []failure_token = _gguf_remote_detect_failure.set(detect_failures)try:- gguf_filename = detect_gguf_model_remote(identifier, hf_token = hf_token)+ with collecting_hub_token_rejections() as token_rejections:+ gguf_filename = detect_gguf_model_remote(identifier, hf_token = hf_token)finally:_gguf_remote_detect_failure.reset(failure_token)# A failed listing is not "no GGUF"; a refused repo is never served from cache (#11551).if not gguf_filename and _looks_like_gguf_repo(identifier, gguf_variant):if detect_failures:raise GgufRepoUnreadableError(- _gguf_repo_unreadable_message(identifier, detect_failures[-1])+ _gguf_repo_unreadable_message(+ identifier,+ detect_failures[-1],+ token_rejected = token_rejections.refused+ and is_rejected_credential_error(detect_failures[-1], hf_token),+ )) from Noneif _env_offline():raise GgufRepoUnreadableError(_gguf_repo_unreadable_message(identifier, None))@@ -4397,7 +4429,9 @@ class ModelConfig:# absent; without one, let the load path resolve it.try:from huggingface_hub import list_repo_files- repo_files = list_repo_files(identifier, token = hf_token)+ repo_files = call_with_anonymous_retry(+ lambda token: list_repo_files(identifier, token = token), hf_token+ )except Exception:repo_files = Noneif repo_files and not _gguf_files_for_variant(repo_files, variant):@@ -4508,11 +4542,14 @@ class ModelConfig:try:from huggingface_hub import hf_hub_download- config_path = hf_hub_download(- identifier,- "adapter_config.json",- token = hf_token,- cache_dir = active_hf_hub_cache(),+ config_path = call_with_anonymous_retry(+ lambda token: hf_hub_download(+ identifier,+ "adapter_config.json",+ token = token,+ cache_dir = active_hf_hub_cache(),+ ),+ hf_token,)with open(config_path, "r", encoding = "utf-8-sig") as f:adapter_config = json.load(f)diff --git a/studio/backend/utils/security/consent.py b/studio/backend/utils/security/consent.pyindex b866c4d26da..3465fb756d5 100644--- a/studio/backend/utils/security/consent.py+++ b/studio/backend/utils/security/consent.py@@ -133,6 +133,9 @@ def _load_remote_code_configs(return configsfrom huggingface_hub import hf_hub_download+ from hub.utils.hf_tokens import anonymous_retrying++ hf_hub_download = anonymous_retrying(hf_hub_download)from huggingface_hub.utils import EntryNotFoundErrorfrom utils.hf_cache_settings import active_hf_hub_cachefrom utils.hf_probe import hf_file_definitely_absentdiff --git a/studio/backend/utils/security/file_security.py b/studio/backend/utils/security/file_security.pyindex a6035d04eda..6fac12fb165 100644--- a/studio/backend/utils/security/file_security.py+++ b/studio/backend/utils/security/file_security.py@@ -130,6 +130,8 @@ def _indexed_shard_paths(from huggingface_hub.utils import EntryNotFoundErrorfrom utils.hf_cache_settings import active_hf_hub_cachefrom utils.hf_probe import hf_file_definitely_absent++ from hub.utils.hf_tokens import call_hub_with_anonymous_retryexcept Exception:return None@@ -143,11 +145,12 @@ def _indexed_shard_paths():continuetry:- index_path = hf_hub_download(+ index_path = call_hub_with_anonymous_retry(+ hf_hub_download,+ hf_token or None,model_name,prefix + filename,revision = revision,- token = hf_token or None,cache_dir = active_hf_hub_cache(),)except EntryNotFoundError:@@ -245,15 +248,18 @@ def _fetch_security_status():"""``security_repo_status`` (a dict) or None if unavailable. Hub metadata only; retries once on a transient error, then returns None so the caller can apply its local-fallback policy. ``revision`` scopes the scan to a specific cached commit (else the default branch)."""from huggingface_hub import model_info as hf_model_info+ from hub.utils.hf_tokens import call_hub_with_anonymous_retrytoken_arg = hf_token if hf_token else Falselast_exc = Nonefor attempt, timeout in enumerate((_REQUEST_TIMEOUT, _RETRY_TIMEOUT)):try:- info = hf_model_info(+ # A refused token still gets the public repo's scan, as the worker's load will.+ info = call_hub_with_anonymous_retry(+ hf_model_info,+ token_arg,model_name,revision = revision,- token = token_arg,securityStatus = True,timeout = timeout,)diff --git a/studio/backend/utils/security/remote_code_scan.py b/studio/backend/utils/security/remote_code_scan.pyindex ccd94d55301..48a26d5664d 100644--- a/studio/backend/utils/security/remote_code_scan.py+++ b/studio/backend/utils/security/remote_code_scan.py@@ -464,6 +464,10 @@ def repo_remote_code_files(return filesfrom huggingface_hub import hf_hub_download, list_repo_files+ from hub.utils.hf_tokens import anonymous_retrying++ hf_hub_download = anonymous_retrying(hf_hub_download)+ list_repo_files = anonymous_retrying(list_repo_files)from huggingface_hub.utils import EntryNotFoundErrorfrom utils.hf_probe import hf_file_definitely_absent@@ -600,6 +604,9 @@ def external_auto_map_repos(return reposfrom huggingface_hub import hf_hub_download+ from hub.utils.hf_tokens import anonymous_retrying++ hf_hub_download = anonymous_retrying(hf_hub_download)from huggingface_hub.utils import EntryNotFoundErrorfrom utils.hf_probe import hf_file_definitely_absent@@ -633,6 +640,10 @@ def _add_external_refs(files: dict, refs, hf_token, model_name: str) -> bool:from pathlib import Pathfrom huggingface_hub import hf_hub_download, list_repo_files+ from hub.utils.hf_tokens import anonymous_retrying++ hf_hub_download = anonymous_retrying(hf_hub_download)+ list_repo_files = anonymous_retrying(list_repo_files)entries: dict = {}for repo, fn in refs:diff --git a/studio/backend/utils/transformers_version.py b/studio/backend/utils/transformers_version.pyindex 8a0c2ddd189..705edec0682 100644--- a/studio/backend/utils/transformers_version.py+++ b/studio/backend/utils/transformers_version.py@@ -142,6 +142,23 @@ def _hf_proxy_opener(url: str):return None+def _hf_json(url: str, hf_token: str | None):+ """GET a JSON file from the Hub, once more without the token if the Hub refuses it (#11551)."""+ import urllib.request++ from hub.utils.hf_tokens import call_with_anonymous_retry++ def read(token):+ headers = {"User-Agent": "unsloth-studio"}+ if isinstance(token, str) and token:+ headers["Authorization"] = f"Bearer {token}"+ with _hf_urlopen(urllib.request.Request(url, headers = headers), timeout = 10) as resp:+ return json.loads(resp.read().decode())++ # No token sends none here (not the ambient one).+ return call_with_anonymous_retry(read, hf_token or False)++def _hf_urlopen(req, timeout: int):"""``urlopen`` through the same proxy huggingface_hub would use for this request,with redirects that cannot carry the Authorization header off-origin."""@@ -780,13 +797,8 @@ def _remote_lora_base(model_name: str, hf_token: str | None = None) -> str | Nonimport urllib.requesturl = _hf_raw_url(model_name, "adapter_config.json")- headers = {"User-Agent": "unsloth-studio"}- if hf_token:- headers["Authorization"] = f"Bearer {hf_token}"try:- req = urllib.request.Request(url, headers = headers)- with _hf_urlopen(req, timeout = 10) as resp:- cfg = json.loads(resp.read().decode())+ cfg = _hf_json(url, hf_token)base = cfg.get("base_model_name_or_path")if base:logger.info("Resolved remote LoRA adapter '%s' → base model '%s'", model_name, base)@@ -847,13 +859,8 @@ def _check_tokenizer_config_needs_v5(model_name: str, hf_token: str | None = Nonimport urllib.requesturl = _hf_raw_url(model_name, "tokenizer_config.json")- headers = {"User-Agent": "unsloth-studio"}- if hf_token:- headers["Authorization"] = f"Bearer {hf_token}"try:- req = urllib.request.Request(url, headers = headers)- with _hf_urlopen(req, timeout = 10) as resp:- data = json.loads(resp.read().decode())+ data = _hf_json(url, hf_token)tokenizer_class = data.get("tokenizer_class", "")result = tokenizer_class in _TRANSFORMERS_5_TOKENIZER_CLASSESif result:@@ -1002,13 +1009,8 @@ def _load_config_json(model_name: str, hf_token: str | None = None) -> dict | Noimport urllib.requesturl = _hf_raw_url(model_name, "config.json")- headers = {"User-Agent": "unsloth-studio"}- if hf_token:- headers["Authorization"] = f"Bearer {hf_token}"try:- req = urllib.request.Request(url, headers = headers)- with _hf_urlopen(req, timeout = 10) as resp:- cfg = json.loads(resp.read().decode())+ cfg = _hf_json(url, hf_token)_config_json_cache[cache_key] = cfgreturn cfgexcept urllib.error.HTTPError as exc:diff --git a/studio/backend/utils/utils.py b/studio/backend/utils/utils.pyindex 4e22b32cd6e..e364806771e 100644--- a/studio/backend/utils/utils.py+++ b/studio/backend/utils/utils.py@@ -888,6 +888,23 @@ def without_hf_auth():os.environ.pop("HF_HUB_DISABLE_IMPLICIT_TOKEN", None)+def _is_repo_level_401(error: BaseException, response) -> bool:+ """A 401 the Hub gave for the repo, not the credential: anonymous reads of a private or+ missing repo get one too. Refusals of the token itself name it in X-Error-Message."""+ if type(error).__name__ != "RepositoryNotFoundError":+ return False+ from hub.utils.hf_tokens import hub_token_rejections++ rejections = hub_token_rejections()+ if rejections is not None and rejections.refused:+ return False+ try:+ reason = str(response.headers.get("X-Error-Message") or "").lower()+ except Exception:+ reason = ""+ return not any(marker in reason for marker in ("token", "credential"))++def is_hf_authentication_error(error: Exception) -> bool:"""Return whether an exception chain contains a definitive HF auth failure."""seen: set[int] = set()@@ -897,7 +914,11 @@ def is_hf_authentication_error(error: Exception) -> bool:response = getattr(current, "response", None)status = getattr(response, "status_code", None)try:- if status is not None and int(status) == 401:+ if (+ status is not None+ and int(status) == 401+ and not _is_repo_level_401(current, response)+ ):return Trueexcept (TypeError, ValueError):pass