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 the local model cache locally, and keep host paths off the API (#11026)
Daniel Han5fb083d40b2a
diff --git a/scripts/verify_import_hoist.py b/scripts/verify_import_hoist.pyindex 6a835a4a857..1d0c8b92936 100644--- a/scripts/verify_import_hoist.py+++ b/scripts/verify_import_hoist.py@@ -271,6 +271,11 @@ class _Builder(ast.NodeVisitor):# AugAssign target is also a loadif isinstance(node, ast.AugAssign):self._record_loads(t, scope)+ else:+ # A subscript or attribute target LOADS everything but the outermost+ # binding: `overrides[dependency] = fn` reads both names.+ if not isinstance(t, ast.Name):+ self._record_loads(t, scope)returnif isinstance(node, (ast.For, ast.AsyncFor)):self._visit_expr(node.iter, scope)@@ -771,6 +776,22 @@ _SELF_TESTS = {" return x\n",None,),+ # `app.dependency_overrides[dep] = lambda: ...` is how FastAPI route tests are written.+ "a_subscript_key_on_the_left_hand_side_is_a_use": (+ "app = {}\n",+ "from .deps import dependency\napp = {}\napp[dependency] = 1\n",+ None,+ ),+ "a_subscripted_object_on_the_left_hand_side_is_a_use": (+ "def f(k, v):\n return k, v\n",+ "from .deps import registry\ndef f(k, v):\n registry[k] = v\n",+ None,+ ),+ "an_attribute_target_loads_the_object": (+ "def f(v):\n return v\n",+ "from .deps import settings\ndef f(v):\n settings.value = v\n",+ None,+ ),# The other direction: Literal['T'] is a VALUE, so it must NOT credit an import T."a_literal_value_is_not_a_use_of_that_name": ("from typing import TYPE_CHECKING, Literal\ndef f(x) -> Literal['a']:\n return x\n",diff --git a/studio/backend/hub/routes/inventory.py b/studio/backend/hub/routes/inventory.pyindex bd2fd37eaa3..fa41d2c6403 100644--- a/studio/backend/hub/routes/inventory.py+++ b/studio/backend/hub/routes/inventory.py@@ -7,10 +7,20 @@ from __future__ import annotationsfrom typing import Optional-from fastapi import APIRouter, Body, Depends, Query+from fastapi import APIRouter, Body, Depends, HTTPException, Query-from auth.authentication import allow_ambient_hf_token, get_current_subject+from auth.authentication import (+ allow_ambient_hf_token,+ authenticated_via_api_key,+ get_current_subject,+)from hub.dependencies import get_hf_token, get_request_hf_token+from hub.utils.host_paths import (+ redact_host_paths,+ redact_inventory_error_detail,+ redact_inventory_host_paths,+ resolve_host_path_reference,+)from hub.schemas.downloads import (ActiveDownloadsResponse,CancelDownloadResponse,@@ -55,22 +65,57 @@ async def list_local_models(default = "./models", description = "Directory to scan for local model folders"),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- return await local_inventory.list_local_models_response(models_dir)+ # Inside the try: an exception raised while evaluating an argument never reaches the+ # function it was being passed to, so its detail goes out unredacted.+ try:+ payload = await local_inventory.list_local_models_response(models_dir)+ except HTTPException as error:+ raise HTTPException(+ status_code = error.status_code,+ detail = redact_inventory_error_detail(error.detail, via_api_key = via_api_key),+ headers = error.headers,+ ) from error+ return redact_inventory_host_paths(payload, via_api_key = via_api_key)# Plain def, not async: synchronous SQLite and filesystem work runs in FastAPI's thread pool instead# of blocking the event loop.@router.get("/scan-folders", response_model = ScanFoldersResponse)-def get_scan_folders(current_subject: str = Depends(get_current_subject)):- return local_inventory.get_scan_folders_response()+def get_scan_folders(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):+ try:+ payload = local_inventory.get_scan_folders_response()+ except HTTPException as error:+ raise HTTPException(+ status_code = error.status_code,+ detail = redact_inventory_error_detail(error.detail, via_api_key = via_api_key),+ headers = error.headers,+ ) from error+ return redact_inventory_host_paths(payload, via_api_key = via_api_key)@router.post("/scan-folders", response_model = ScanFolderInfo, status_code = 201)def add_scan_folder_endpoint(- body: AddScanFolderRequest, current_subject: str = Depends(get_current_subject)+ body: AddScanFolderRequest,+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- return local_inventory.add_scan_folder_response(body.path)+ # Redacted even though the caller named this path, so a NORMALISED path (symlinks resolved,+ # a relative one anchored) cannot answer for the host. Inside the try, since an exception+ # raised while evaluating an argument never reaches the function it was passed to.+ try:+ payload = local_inventory.add_scan_folder_response(body.path)+ except HTTPException as error:+ raise HTTPException(+ status_code = error.status_code,+ detail = redact_inventory_error_detail(error.detail, via_api_key = via_api_key),+ headers = error.headers,+ ) from error+ return redact_inventory_host_paths(payload, via_api_key = via_api_key)@router.delete("/scan-folders/{folder_id}", response_model = RemoveScanFolderResponse)@@ -81,8 +126,21 @@ def remove_scan_folder_endpoint(@router.get("/models-folder", response_model = ModelsFolderResponse)-def get_models_folder(current_subject: str = Depends(get_current_subject)):- return local_inventory.get_models_folder_response()+def get_models_folder(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):+ # Inside the try, not the redactor's argument list: this route RAISES with the cache path+ # in the detail.+ try:+ payload = local_inventory.get_models_folder_response()+ except HTTPException as error:+ raise HTTPException(+ status_code = error.status_code,+ detail = redact_inventory_error_detail(error.detail, via_api_key = via_api_key),+ headers = error.headers,+ ) from error+ return redact_inventory_host_paths(payload, via_api_key = via_api_key)@router.get("/gguf-variants", response_model = GgufVariantsResponse)@@ -95,13 +153,22 @@ async def get_gguf_variants(local_path: Optional[str] = Query(None),hf_token: HfTokenArg = Depends(get_request_hf_token),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- return await gguf_variants.get_gguf_variants_response(- repo_id,- prefer_local_cache = prefer_local_cache,- offline = offline,- local_path = local_path,- hf_token = hf_token,+ # Both identifiers can come back as handles: an API-key caller is handed `ref:` in place of+ # every host path, and that reference is the only name it has for a custom local GGUF or a+ # copy in a secondary root. Unresolved, the lookup misses or is answered out of the ACTIVE+ # cache, a different file. Redacted on the way out because a local listing copies its input+ # into `repo_id`, which would hand back the path the inventory took trouble to hide.+ return redact_host_paths(+ await gguf_variants.get_gguf_variants_response(+ resolve_host_path_reference(repo_id) or repo_id,+ prefer_local_cache = prefer_local_cache,+ offline = offline,+ local_path = resolve_host_path_reference(local_path) or local_path,+ hf_token = hf_token,+ ),+ via_api_key = via_api_key,)@@ -131,8 +198,14 @@ async def get_download_status(repo_id: str = Query(..., description = "HuggingFace repo ID"),gguf_variant: str = Query("", description = "Quantization variant (empty for safetensors)"),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- return await downloads.get_download_status_response(repo_id, gguf_variant)+ # `error` is the worker's own stderr, kept after `scrub_secrets` only, so polling a failed+ # download reads the host layout from a route with no path field at all.+ return redact_host_paths(+ await downloads.get_download_status_response(repo_id, gguf_variant),+ via_api_key = via_api_key,+ )@router.get("/active-downloads", response_model = ActiveDownloadsResponse)@@ -149,11 +222,15 @@ async def get_model_transport_status(gguf_variant: str = Query("", description = "Quantization variant (empty for safetensors)"),hf_token: HfTokenArg = Depends(get_request_hf_token),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- return await downloads.get_model_transport_status_response(- repo_id,- gguf_variant,- hf_token,+ return redact_host_paths(+ await downloads.get_model_transport_status_response(+ repo_id,+ gguf_variant,+ hf_token,+ ),+ via_api_key = via_api_key,)@@ -168,12 +245,16 @@ async def get_gguf_download_progress(expected_bytes: int = Query(0, description = "Expected total download size in bytes"),hf_token: HfTokenArg = Depends(get_request_hf_token),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- return await downloads.get_gguf_download_progress_response(- repo_id,- variant = variant,- expected_bytes = expected_bytes,- hf_token = hf_token,+ return redact_host_paths(+ await downloads.get_gguf_download_progress_response(+ repo_id,+ variant = variant,+ expected_bytes = expected_bytes,+ hf_token = hf_token,+ ),+ via_api_key = via_api_key,)@@ -183,11 +264,15 @@ async def get_download_progress(expected_bytes: int = Query(0, description = "Expected total download size in bytes"),hf_token: HfTokenArg = Depends(get_request_hf_token),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- return await downloads.get_download_progress_response(- repo_id,- expected_bytes = expected_bytes,- hf_token = hf_token,+ return redact_host_paths(+ await downloads.get_download_progress_response(+ repo_id,+ expected_bytes = expected_bytes,+ hf_token = hf_token,+ ),+ via_api_key = via_api_key,)@@ -195,26 +280,38 @@ async def get_download_progress(async def list_cached_gguf(hf_token: HfTokenArg = Depends(get_request_hf_token),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- return await cache_inventory.list_cached_gguf_response(hf_token)+ return redact_host_paths(+ await cache_inventory.list_cached_gguf_response(hf_token), via_api_key = via_api_key+ )@router.get("/cached-models", response_model = CachedModelsResponse)async def list_cached_models(hf_token: HfTokenArg = Depends(get_request_hf_token),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- return await cache_inventory.list_cached_models_response(hf_token)+ return redact_host_paths(+ await cache_inventory.list_cached_models_response(hf_token), via_api_key = via_api_key+ )@router.get("/hidden-models", response_model = HiddenModelsResponse)-async def list_hidden_models(current_subject: str = Depends(get_current_subject)):+async def list_hidden_models(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):import asynciofrom routes.models import hidden_model_matchersneedles, exact_ids, exact_paths = await asyncio.to_thread(hidden_model_matchers)- return HiddenModelsResponse(needles = needles, exact_ids = exact_ids, exact_paths = exact_paths)+ return redact_host_paths(+ HiddenModelsResponse(needles = needles, exact_ids = exact_ids, exact_paths = exact_paths),+ via_api_key = via_api_key,+ )@router.post("/delete-impact", response_model = DeleteImpactResponse)@@ -232,10 +329,15 @@ async def delete_impact(@router.get("/orphan-companions", response_model = OrphanCompanionsResponse)-async def orphan_companions(current_subject: str = Depends(get_current_subject)):+async def orphan_companions(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):"""Cached companion assets no installed model needs. Listing only; removal goes throughthe ordinary guarded delete."""- return await companion_cleanup.orphan_companions_response()+ return redact_host_paths(+ await companion_cleanup.orphan_companions_response(), via_api_key = via_api_key+ )@router.delete(@@ -252,6 +354,12 @@ async def delete_cached_model(hf_token: HfTokenArg = Depends(get_request_hf_token),current_subject: str = Depends(get_current_subject),):+ # `cache_ref` is the only identifier an API-key caller HAS for a specific copy; omitting it+ # silently acts on the active root.return await deletion.delete_cached_model_response(- repo_id, variant, hf_token, cache_path, only_if_orphan+ repo_id,+ variant,+ hf_token,+ resolve_host_path_reference(cache_path) or cache_path,+ only_if_orphan,)diff --git a/studio/backend/hub/schemas/downloads.py b/studio/backend/hub/schemas/downloads.pyindex 93d4ae105d7..ae6933083b5 100644--- a/studio/backend/hub/schemas/downloads.py+++ b/studio/backend/hub/schemas/downloads.py@@ -156,6 +156,9 @@ class DownloadProgressResponse(BaseModel):expected_bytes: intprogress: floatcache_path: Optional[str] = None+ # Opaque stand-in for ``cache_path``. A redacted reading is an empty string, never null,+ # so the discriminator above survives.+ cache_ref: Optional[str] = Nonecache_measured: bool = Field(True,description = (diff --git a/studio/backend/hub/schemas/inventory.py b/studio/backend/hub/schemas/inventory.pyindex 5f88a54afe9..d136fe03ae6 100644--- a/studio/backend/hub/schemas/inventory.py+++ b/studio/backend/hub/schemas/inventory.py@@ -250,6 +250,8 @@ class CachedRepoBase(BaseModel):repo_id: strsize_bytes: int = 0cache_path: Optional[str] = None+ # Opaque stand-in for ``cache_path``, stable for the server's life and not reversible.+ cache_ref: Optional[str] = Nonelast_modified: Optional[float] = Nonepartial: bool = Falsepartial_transport: Optional[str] = None@@ -383,6 +385,8 @@ class OrphanCompanionInfo(BaseModel):repo_id: strsize_bytes: int = 0cache_path: Optional[str] = None+ # Opaque stand-in for ``cache_path``, stable for the server's life and not reversible.+ cache_ref: Optional[str] = Noneclass OrphanCompanionsResponse(BaseModel):diff --git a/studio/backend/hub/services/datasets/formatting.py b/studio/backend/hub/services/datasets/formatting.pyindex 4be463b7c7b..e76abd6931b 100644--- a/studio/backend/hub/services/datasets/formatting.py+++ b/studio/backend/hub/services/datasets/formatting.py@@ -49,7 +49,7 @@ from hub.utils.paths import (normalize_path,resolve_dataset_path,)-from hub.utils.hf_tokens import cached_read_refused+from hub.utils.hf_tokens import cached_read_refused, recording_a_request_token_fetchfrom utils.datasets.audio_decode import ensure_audio_decodingfrom utils.paths.path_utils import drop_shadowed_appledouble_names@@ -477,8 +477,16 @@ def check_format_response("token": hf_token,}- streamed_ds = load_dataset(**load_kwargs)- rows = list(islice(streamed_ds, PREVIEW_SIZE))+ # Recorded against the call that can materialise rows, not the+ # listing above it: a preview writes into the datasets cache under+ # what may be a one-off token, and unrecorded a later tokenless+ # caller reads "none needed one". Recording before `list_repo_files`+ # left a record for a fetch a 404 or outage never made.+ with recording_a_request_token_fetch(+ hf_token, request.dataset_name, "dataset"+ ):+ streamed_ds = load_dataset(**load_kwargs)+ rows = list(islice(streamed_ds, PREVIEW_SIZE))if rows:preview_slice = Dataset.from_list(rows)except Exception as e:@@ -500,9 +508,12 @@ def check_format_response(if request.subset:load_kwargs["name"] = request.subset- streamed_ds = load_dataset(**load_kwargs)+ # Tier 2 reaches the network on its own, whether or not tier 1 ran, and+ # takes its record back if it fails having cached nothing.+ with recording_a_request_token_fetch(hf_token, request.dataset_name, "dataset"):+ streamed_ds = load_dataset(**load_kwargs)- rows = list(islice(streamed_ds, PREVIEW_SIZE))+ rows = list(islice(streamed_ds, PREVIEW_SIZE))if not rows:raise HTTPException(status_code = 400,diff --git a/studio/backend/hub/services/download_lifecycle.py b/studio/backend/hub/services/download_lifecycle.pyindex 46ebd4195df..b654dca18ee 100644--- a/studio/backend/hub/services/download_lifecycle.py+++ b/studio/backend/hub/services/download_lifecycle.py@@ -1404,6 +1404,12 @@ def launch_worker(else None),)+ # The one-off `X-Unsloth-HF-Token` is kept nowhere, so a repo fetched with it can sit in the+ # cache of a host whose credential set is empty. Recorded HERE, after the in-flight check and+ # the registry claim, so a rejected request cannot mark a repo it never fetched.+ from hub.utils import hf_tokens as _hf_tokens++ _hf_tokens.note_repo_fetched_with_a_request_token(hf_token, repo_id, repo_type)try:proc = spawn()except Exception as e:diff --git a/studio/backend/hub/services/models/cache_inventory.py b/studio/backend/hub/services/models/cache_inventory.pyindex e9ea9a8e2e2..79d7e402711 100644--- a/studio/backend/hub/services/models/cache_inventory.py+++ b/studio/backend/hub/services/models/cache_inventory.py@@ -20,6 +20,7 @@ from hub.schemas.inventory import ModelFormatfrom hub.utils import inventory_scan as hf_cache_scanfrom hub.utils import download_manifest, download_registryfrom hub.utils.hf_cache_state import snapshot_selection_key+from hub.utils.host_paths import scrub_pathsfrom hub.utils.snapshot_filters import (snapshot_download_blob_hashes,snapshot_download_size,@@ -512,7 +513,7 @@ def _scan_cached_gguf()except Exception as e:# The index is built once for the whole scan and outside the per-repository try, so one undecodable cache directory name, hashed for the repo key, answered 500 with every valid row hidden.- logger.warning("Could not build shared cached-GGUF state index: %s", e)+ logger.warning("Could not build shared cached-GGUF state index: %s", scrub_paths(e))variant_states = Noneseen_lower: dict[str, dict] = {}@@ -624,7 +625,7 @@ def _scan_cached_gguf(existing["last_modified"] = last_modifiedexcept Exception as e:repo_label = getattr(repo_info, "repo_id", "<unknown>")- logger.warning(f"Skipping cached GGUF repo {repo_label}: {e}")+ logger.warning("Skipping cached GGUF repo %s: %s", repo_label, scrub_paths(e))continuereturn sorted(seen_lower.values(), key = lambda c: c["repo_id"])@@ -975,7 +976,7 @@ def _scan_cached_models(active_hub_cache = active_hub_cache,)except Exception as e:- logger.warning("Could not build shared cached-model state index: %s", e)+ logger.warning("Could not build shared cached-model state index: %s", scrub_paths(e))variant_states = Noneseen_lower: dict[str, dict] = {}@@ -1137,7 +1138,7 @@ def _scan_cached_models(existing["last_modified"] = last_modifiedexcept Exception as e:repo_label = getattr(repo_info, "repo_id", "<unknown>")- logger.warning(f"Skipping cached model repo {repo_label}: {e}")+ logger.warning("Skipping cached model repo %s: %s", repo_label, scrub_paths(e))continuecached = sorted(seen_lower.values(), key = lambda c: c["repo_id"])logger.info(diff --git a/studio/backend/hub/services/models/local_inventory.py b/studio/backend/hub/services/models/local_inventory.pyindex fb9919ce6fb..0ca2e4a77ca 100644--- a/studio/backend/hub/services/models/local_inventory.py+++ b/studio/backend/hub/services/models/local_inventory.py@@ -20,6 +20,7 @@ from hub.storage.scan_folders import (remove_scan_folder,)from hub.utils import download_manifest, gguf, inventory_scan as hf_cache_scan+from hub.utils.host_paths import scrub_paths, short_path_for_logfrom hub.utils.paths import (hermes_model_dirs,hf_default_cache_dir,@@ -320,7 +321,11 @@ def _scan_hf_cache(or (cache_dir if active_cache else _resolve_hf_cache_dir()),)except Exception as e:- logger.warning("Could not build Hub-state index for %s: %s", cache_dir, e)+ logger.warning(+ "Could not build Hub-state index for %s: %s",+ short_path_for_log(cache_dir),+ scrub_paths(e),+ )variant_states = Nonefound: list[LocalModelInfo] = []@@ -636,7 +641,9 @@ async def _scan_source(label: str, scanner, path: Path) -> List[LocalModelInfo]:try:return await asyncio.to_thread(scanner, path)except Exception as e:- logger.warning("Skipping %s scan for %s: %s", label, path, e)+ logger.warning(+ "Skipping %s scan for %s: %s", label, short_path_for_log(path), scrub_paths(e)+ )return []@@ -703,7 +710,7 @@ async def _collect_models_from_default_sources(active_hub_cache = hf_cache_dir,)except Exception as e:- logger.warning("Could not build shared Hub-state index: %s", e)+ logger.warning("Could not build shared Hub-state index: %s", scrub_paths(e))variant_states = Nonefor label, cache_dir, active_cache, discovered in discovered_sources:local_models += await _scan_source(@@ -751,7 +758,11 @@ async def _collect_models_from_default_sources(if _inventory_physical_identity(model.path) not in staged]except Exception as e:- logger.warning("Skipping unreadable scan folder %s: %s", folder_path, e)+ logger.warning(+ "Skipping unreadable scan folder %s: %s",+ short_path_for_log(folder_path),+ scrub_paths(e),+ )# Only an OS failure is something the user can fix, so only that is shown.if isinstance(e, OSError):record_scan_failure(row_path, e)@@ -853,7 +864,7 @@ async def _load_custom_folders() -> list[dict]:try:return await asyncio.to_thread(list_scan_folders)except Exception as e:- logger.warning("Could not load custom scan folders: %s", e)+ logger.warning("Could not load custom scan folders: %s", scrub_paths(e))return []@@ -966,7 +977,7 @@ async def _scan_local_models_response(models = models,)except Exception as e:- logger.error(f"Error listing local models: {e}", exc_info = True)+ logger.error("Error listing local models: %s", scrub_paths(e), exc_info = True)raise _http_error(status_code = 500,detail = f"Failed to list local models: {str(e)}",@@ -1001,7 +1012,7 @@ async def list_local_models_response(models_dir: str = "./models") -> LocalModel)return response.model_copy(update = {"models": models})except Exception as e: # noqa: BLE001 -- classification never breaks the listing- logger.warning("Could not classify local model tasks: %s", e)+ logger.warning("Could not classify local model tasks: %s", scrub_paths(e))return responseasync def scan_and_classify(@@ -1079,9 +1090,11 @@ def add_scan_folder_response(path: str) -> dict:try:folder, inserted = add_scan_folder_with_status(_coerce_scan_folder_path(path))except ValueError as e:- logger.warning("Scan folder rejected: %s (path=%s)", e, path)+ logger.warning(+ "Scan folder rejected: %s (path=%s)", scrub_paths(e), short_path_for_log(path)+ )raise _http_error(status_code = 400, detail = str(e))- logger.info("Scan folder added: %s", folder.get("path"))+ logger.info("Scan folder added: %s", short_path_for_log(folder.get("path")))if inserted:from core.inference.local_model_resolver import invalidate_index, warm_index_sooninvalidate_index()diff --git a/studio/backend/hub/utils/hf_tokens.py b/studio/backend/hub/utils/hf_tokens.pyindex d9fda750abd..ee2fd909ada 100644--- a/studio/backend/hub/utils/hf_tokens.py+++ b/studio/backend/hub/utils/hf_tokens.py@@ -6,9 +6,14 @@from __future__ import annotationsimport hashlib+import hmac+import loggingimport threadingimport time-from typing import Literal, MutableMapping, Optional, Union+from contextlib import contextmanager+from typing import Iterable, Literal, MutableMapping, Optional, Union++logger = logging.getLogger(__name__)HfTokenArg = Optional[Union[str, Literal[False]]]@@ -109,12 +114,53 @@ _REPO_ACCESS_TTL_S = 60.0# Says nothing about the credential. MUST exceed the probe timeout or every caller re-stalls._REPO_ACCESS_UNREACHABLE_TTL_S = 30.0_REPO_ACCESS_CACHE_MAX = 1024-_repo_access_cache: dict[tuple[str, str, str], tuple[float, bool]] = {}+# ``None`` is a THIRD value, not a miss: "the Hub could not be asked", never a denial.+_repo_access_cache: dict[tuple[str, str, str], tuple[float, Optional[bool]]] = {}+_CACHE_MISS = object()_repo_access_lock = threading.Lock()# One probe per key: the probe runs outside _repo_access_lock, so a cold key would# otherwise open a connection per caller._repo_access_inflight: dict[tuple[str, str, str], threading.Lock] = {}+# An answered NO never expires and its eviction is remembered, since 429/5xx are "unaskable"+# yet reachable: a refused caller is otherwise one outage from access.+_DENIAL_MEMORY_MAX = 8192+_denied_repo_access: dict[tuple[str, str, str], float] = {}+_denial_memory_is_complete = True+++def _remember_denial(key: tuple[str, str, str], now: float) -> None:+ global _denial_memory_is_complete+ with _repo_access_lock:+ _denied_repo_access.pop(key, None)+ while len(_denied_repo_access) >= _DENIAL_MEMORY_MAX:+ _denied_repo_access.pop(next(iter(_denied_repo_access)), None)+ _denial_memory_is_complete = False+ _denied_repo_access[key] = now+++def _denial_memory_lost_an_entry() -> bool:+ with _repo_access_lock:+ return not _denial_memory_is_complete+++def _forget_denial(key: tuple[str, str, str]) -> None:+ with _repo_access_lock:+ _denied_repo_access.pop(key, None)+++def _denial_is_remembered(key: tuple[str, str, str]) -> bool:+ with _repo_access_lock:+ return key in _denied_repo_access+++def _with_remembered_denial(key: tuple[str, str, str], verdict: Optional[bool]) -> Optional[bool]:+ """ "Could not ask" reads as the last answer if that was no; once any refusal is evicted, no+ key may claim it was never refused."""+ if verdict is None and (_denial_is_remembered(key) or _denial_memory_lost_an_entry()):+ return False+ return verdict+class _ProbeTimedOut(Exception):"""Raised when /auth-check could not be asked at all, rather than answering."""@@ -137,6 +183,20 @@ _UNREACHABLE_EXC_NAMES = frozenset(_UNREACHABLE_PACKAGES = frozenset({"requests", "httpx", "urllib3"})+# Matched by name, not imported: a version that moves one must not turn a denial into "could not ask".+_DENIAL_EXC_NAMES = frozenset(+ {+ "RepositoryNotFoundError",+ "GatedRepoError",+ "DisabledRepoError",+ "RevisionNotFoundError",+ }+)+# Answers; anything else failed to answer. Not 404: a bare 401 covers private AND missing repos,+# so a 404 means the endpoint has no /auth-check route.+_DENIAL_STATUSES = frozenset({401, 403, 410, 451})++def _is_probe_timeout(exc: BaseException) -> bool:"""Could not ask, as opposed to asked and told no. Named for its original narrow case."""for cls in type(exc).__mro__:@@ -154,10 +214,14 @@ def _is_probe_timeout(exc: BaseException) -> bool:def reset_repo_access_cache() -> None:- """Drop memoized Hub access answers. Tests only."""+ global _denial_memory_is_completewith _repo_access_lock:_repo_access_cache.clear()_repo_access_inflight.clear()+ _denied_repo_access.clear()+ _denial_memory_is_complete = True+ _noted_credential_identities.clear()+ _unrecorded_fetches.clear()def cache_reads_authorized(@@ -169,17 +233,11 @@ def cache_reads_authorized() -> bool:"""Whether this caller may read the host Hub disk cache for *repo_id*.- ``is_anonymous`` authenticates the caller CLASS, not the credential, so any token-shaped- string leaves the sentinel and takes the disk fast paths. ``repo_info`` cannot replace- the probe: gated public metadata still returns for an invalid token.-- ``True`` does not mean the token is valid. /auth-check answers "is this repo reachable",- which a public repo answers 200 for any string; it discriminates on private and gated- repos, which is where the cached reads are.-- Offline, an explicit token is denied without a memoized probe (fail closed) while ambient- ``None`` still reads. ``offline`` is the CALLER's own flag: without it a cache-only- request put its token on the wire and could stall for the probe timeout first.+ Three outcomes, not two: a Hub that ANSWERED no denies, one that could not be ASKED resolves+ against the disk (``_resolve_unaskable``), or a local question depends on reaching+ huggingface.co. ``True`` is not "the token is valid": /auth-check answers 200 for any string+ on a public repo and discriminates on the private and gated ones, which is where cached reads+ are. ``repo_info`` cannot replace it, since gated public metadata returns for an invalid token."""if is_anonymous(hf_token):return False@@ -200,7 +258,10 @@ def cache_reads_authorized(# Unvalidated in the URL, so probing puts the caller's path on the wire with their# bearer token, for an answer that can only be no.return False- return _explicit_token_reaches_repo(repo, hf_token, repo_type, offline = offline)+ verdict = _explicit_token_reaches_repo(repo, hf_token, repo_type, offline = offline)+ if verdict is None:+ return _resolve_unaskable(repo, repo_type, token = hf_token)+ return verdictdef public_cache_read_authorized(@@ -211,14 +272,16 @@ def public_cache_read_authorized() -> bool:"""Whether serving *repo_id* from the cache to a caller with NO credential leaks anything.- The sentinel can never authorize itself, which is right for a private repo and wrong for- a public one. An unauthenticated /auth-check is exactly that difference. Fail closed when- it cannot be asked.+ The sentinel never authorizes itself, right for a private repo and wrong for a public one; an+ unauthenticated /auth-check is that difference. Unaskable resolves against the disk."""repo = (repo_id or "").strip()if not repo or _is_local_path(repo):return False- return _explicit_token_reaches_repo(repo, None, repo_type, offline = offline)+ verdict = _explicit_token_reaches_repo(repo, None, repo_type, offline = offline)+ if verdict is None:+ return _resolve_unaskable(repo, repo_type, token = None)+ return verdictdef cached_read_refused(@@ -231,16 +294,10 @@ def cached_read_refused() -> bool:"""Refuse a read only where the operator's disk could answer it AND this caller may not.- An uncached repo has nothing to leak: refusing it protects nothing and costs a legitimate- caller its answer whenever the probe is merely unavailable rather than negative (a mirror- without the undocumented /auth-check, one transient failure). ``is_cached`` is asked FIRST- so nothing on disk means no probe, and must fail closed or the guard's own failure opens- the path it guards. Each reader passes its own predicate: a file at a revision, a dataset- in either cache, a repo dir.-- "May not read it" is not "cannot authorize itself", which is where the sentinel sits: a- public repo is one it was always entitled to read. Asked here so every reader shares the- rule; the dataset preview used to be the only one asking.+ ``is_cached`` is asked FIRST, so an uncached repo is never probed: refusing one protects+ nothing and costs a legitimate caller its answer whenever the probe is merely unavailable.+ It must fail closed, or the guard's own failure opens the path it guards. "May not read it"+ is not "cannot authorize itself", which is where the sentinel sits."""if not is_cached():return False@@ -261,6 +318,484 @@ def _is_local_path(repo_id: str) -> bool:return False+def _env_hf_token() -> "Optional[str]":+ import os+ for key in _HF_TOKEN_ENV_KEYS:+ if key == "HF_OIDC_RESOURCE":+ # Names a token rather than holding one, so it cannot be compared to a caller's.+ continue+ value = os.environ.get(key)+ if isinstance(value, str) and value.strip():+ return value.strip()+ return None+++def _ambient_hf_token() -> "tuple[bool, Optional[str]]":+ """``(False, None)`` is "could not answer"; reading it as "no credential" is a fail-open."""+ get_token = None+ try:+ from huggingface_hub import get_token as _get_token+ get_token = _get_token+ except Exception:+ get_token = None+ if get_token is not None:+ try:+ token = get_token()+ except Exception:+ # It could not answer, but the environment still can, and a credential found+ # there is knowledge rather than a guess.+ env_token = _env_hf_token()+ return (True, env_token) if env_token else (False, None)+ if isinstance(token, str) and token.strip():+ return (True, token.strip())+ # An empty answer is NOT "this host holds nothing": `get_token` reads HF_TOKEN,+ # HUGGING_FACE_HUB_TOKEN, the OIDC exchange and the token file, and nothing else. A+ # credential sitting in an alias this module already honours (HF_HUB_TOKEN,+ # HUGGINGFACE_HUB_TOKEN, HUGGINGFACEHUB_API_TOKEN) read as credentialless, which+ # authorizes a tokenless caller against a cache that credential may have filled.+ return (True, _env_hf_token())+ # No reader at all: the environment is the only thing left to ask, and silence there is+ # "could not answer" rather than "nothing".+ env_token = _env_hf_token()+ return (True, env_token) if env_token else (False, None)+++def _saved_studio_hf_token() -> "tuple[bool, Optional[str]]":+ """Never written to the token file, so ``_ambient_hf_token`` cannot see it; unreadable is+ not "none"."""+ try:+ from storage import credential_secrets+ except Exception:+ return (False, None)+ try:+ token = credential_secrets.get_hf_token()+ except Exception:+ return (False, None)+ if isinstance(token, str) and token.strip():+ return (True, token.strip())+ # `get_secret` returns None for an absent row AND an undecryptable one; conflating is fail-open.+ try:+ stored = credential_secrets.hf_token_row_exists()+ except Exception:+ return (False, None)+ if stored:+ return (False, None)+ return (True, None)+++def _host_hf_credentials() -> "tuple[bool, tuple]":+ """``known`` is the AND of both stores, so an unreadable one authorizes nobody."""+ ambient_known, ambient = _ambient_hf_token()+ saved_known, saved = _saved_studio_hf_token()+ if not (ambient_known and saved_known):+ return (False, ())+ return (True, tuple(value for value in (ambient, saved) if value))+++# A one-off `X-Unsloth-HF-Token` is stored nowhere, so an empty credential set would otherwise+# read as "everything here was public". Such fetches are recorded per repo instead.+_REQUEST_TOKEN_REPOS_SETTING_KEY = "hub_repos_fetched_with_a_request_token"+++def _request_token_repo_key(repo_id: str, repo_type: Optional[str]) -> str:+ """Case-folded: the authorization cache and `iter_repo_cache_dirs` compare ids that way."""+ return f"{(repo_type or 'model').strip().lower()}:{repo_id.strip().lower()}"+++def _as_owner(call, *args, **kwargs):+ from utils.account_context import OWNER, is_owner_context, run_as+ if is_owner_context():+ return call(*args, **kwargs)+ return run_as(OWNER, call, *args, **kwargs)+++def note_repo_fetched_with_a_request_token(+ token: HfTokenArg,+ repo_id: str,+ repo_type: Optional[str] = "model",+) -> "Optional[dict]":+ """Record that *repo_id* was fetched under a credential, and WHICH one. Never raises.++ Over-broad on purpose: an extra record withholds bytes the caller can still fetch, a missing+ one leaks a private repo. ``by`` is ``None`` when the fetch cannot be attributed to exactly+ one credential, which authorizes nobody.++ Returns the entry it wrote, for a caller that may have to take it back; None when it wrote+ nothing, or when the entry that stands is an earlier writer's.+ """+ if is_anonymous(token) or not repo_id:+ return None+ if token is not None and (not isinstance(token, str) or not token):+ return None+ try:+ fetched_by: Optional[str] = None+ if token is None:+ known, host_tokens = _host_hf_credentials()+ if known and not host_tokens:+ return None+ if known and len(set(host_tokens)) == 1:+ fetched_by = _credential_identity(host_tokens[0])+ else:+ fetched_by = _credential_identity(token)+ from storage.studio_db import upsert_app_setting_map_entry++ key = _request_token_repo_key(repo_id, repo_type)+ recorded = _recorded_request_token_repos()+ if isinstance(recorded, dict) and key not in recorded:+ # Bounded: the repo id is caller-supplied. A miss in a FULL map means "cannot say".+ # Advisory only, since the map can gain an entry between this read and the write;+ # overshooting the bound by a handful of entries is harmless, refusing to record a+ # fetch is not.+ if len(recorded) >= _REQUEST_TOKEN_REPOS_MAX:+ logger.debug("the request-token provenance map is full; not recording %s", key)+ return None+ # The first record stands, and a second credential claiming what the first filled+ # collapses the attribution. Decided INSIDE the write's transaction: two requests with+ # different credentials for the same uncached repo both read "absent" otherwise, and the+ # last writer then stores its own identity where the truth is "two of them could have".+ entry = {"at": time.time(), "by": fetched_by}+ stored = _as_owner(+ upsert_app_setting_map_entry,+ _REQUEST_TOKEN_REPOS_SETTING_KEY,+ key,+ entry,+ keep_first_writer = True,+ ambiguous_field = "by",+ )+ # Returned so the caller can take it back if the fetch it was written for turns out to+ # have moved nothing; only when the stored entry IS ours, since an earlier writer's+ # record is not this call's to remove.+ if isinstance(stored, dict) and stored.get(key) == entry:+ return entry+ except Exception: # noqa: BLE001 -- a download must never fail on its own bookkeeping+ logger.debug("could not record the credential a download used", exc_info = True)+ # The read side must not take this missing record for the absence an unfetched repo leaves.+ try:+ _unrecorded_fetches.add(_request_token_repo_key(repo_id, repo_type))+ except Exception: # noqa: BLE001 -- bookkeeping about bookkeeping, still never raises+ logger.debug("could not note the provenance write that failed", exc_info = True)+ return None+++def forget_a_fetch_that_moved_nothing(+ record: "Optional[dict]",+ repo_id: str,+ repo_type: Optional[str] = "model",+) -> None:+ """Take back ``record`` when the call it was written for failed leaving NOTHING on disk.++ Written before the call rather than after it on purpose: a fetch that dies half way has+ still filled the cache, and an unrecorded repo reads later as "nobody needed a credential+ for this". The cost is the other direction -- a 404, a rejected token or an outage leaves a+ record for a fetch that never happened, the first-writer rule keeps it, and a repo a later+ anonymous download fills is then withheld from the tokenless offline caller. So the record+ is taken back only where the disk says the call really did move nothing, and only while it+ is still the record this call wrote.+ """+ if not record or not repo_id:+ return+ try:+ if _repo_present_on_disk(repo_id, repo_type or "model"):+ return+ from storage.studio_db import upsert_app_setting_map_entry++ key = _request_token_repo_key(repo_id, repo_type)+ _as_owner(+ upsert_app_setting_map_entry,+ _REQUEST_TOKEN_REPOS_SETTING_KEY,+ key,+ None,+ delete_if_entry_equals = record,+ )+ _unrecorded_fetches.discard(key)+ except Exception: # noqa: BLE001 -- bookkeeping, and the over-broad record is the safe side+ logger.debug("could not take back a provenance record", exc_info = True)+++@contextmanager+def recording_a_request_token_fetch(+ token: HfTokenArg,+ repo_id: str,+ repo_type: Optional[str] = "model",+):+ """Record the fetch around the call that performs it, and take the record back if that call+ raised without leaving anything on disk. See ``forget_a_fetch_that_moved_nothing``."""+ record = note_repo_fetched_with_a_request_token(token, repo_id, repo_type)+ try:+ yield+ except BaseException:+ forget_a_fetch_that_moved_nothing(record, repo_id, repo_type)+ raise+++_REQUEST_TOKEN_REPOS_MAX = 4096++# Provenance writes KNOWN to have failed; a durable marker needs the write that just failed.+# Never evicted, since at the bound the whole set reads as "cannot say".+_UNRECORDED_FETCHES_MAX = 4096+_unrecorded_fetches: "set[str]" = set()+++def _provenance_record_is_missing(repo_id: Optional[str], repo_type: Optional[str]) -> bool:+ """A write FAILED, as opposed to nothing having fetched it. Only one of them authorizes."""+ if not repo_id:+ return False+ if len(_unrecorded_fetches) >= _UNRECORDED_FETCHES_MAX:+ return True+ return _request_token_repo_key(repo_id, repo_type) in _unrecorded_fetches+++def _recorded_request_token_repos() -> "Optional[dict]":+ try:+ from storage.studio_db import get_app_setting+ recorded = _as_owner(get_app_setting, _REQUEST_TOKEN_REPOS_SETTING_KEY, None)+ except Exception: # noqa: BLE001+ return None+ if recorded is None:+ return {}+ return recorded if isinstance(recorded, dict) else None+++# Credentials this host has EVER held, as one-way digests. Without it, removing one lets a+# tokenless caller inherit its downloads and rotating one lets the successor inherit them.+_HOST_CREDENTIAL_IDENTITIES_SETTING_KEY = "hub_hf_credential_identities_seen"+_HOST_CREDENTIAL_IDENTITIES_MAX = 64+_noted_credential_identities: "set[str]" = set()+++def _credential_identity(token: str) -> str:+ """One-way and domain-separated: the ledger must never be readable back into a credential."""+ return hashlib.sha256(+ b"unsloth-hf-credential-identity:" + token.encode("utf-8", "surrogatepass")+ ).hexdigest()+++def _host_credential_identities() -> "Optional[dict]":+ """``{}`` when nothing was recorded, ``None`` when unreadable. Unreadable is not "empty"."""+ try:+ from storage.studio_db import get_app_setting+ seen = _as_owner(get_app_setting, _HOST_CREDENTIAL_IDENTITIES_SETTING_KEY, None)+ except Exception: # noqa: BLE001+ return None+ if seen is None:+ return {}+ return seen if isinstance(seen, dict) else None+++def _note_host_credential_identities(tokens: Iterable[str]) -> None:+ """The memo is set only once the identity is KNOWN to be in the ledger: a missing entry+ authorizes MORE, not less."""+ for token in tokens:+ if not isinstance(token, str) or not token:+ continue+ identity = _credential_identity(token)+ if identity in _noted_credential_identities:+ continue+ try:+ from storage.studio_db import upsert_app_setting_map_entry++ seen = _host_credential_identities()+ if seen is None:+ continue+ if identity in seen:+ _noted_credential_identities.add(identity)+ continue+ if len(seen) >= _HOST_CREDENTIAL_IDENTITIES_MAX:+ continue+ _as_owner(+ upsert_app_setting_map_entry,+ _HOST_CREDENTIAL_IDENTITIES_SETTING_KEY,+ identity,+ {"at": time.time()},+ )+ _noted_credential_identities.add(identity)+ except Exception: # noqa: BLE001+ continue+++# A credential this host held but could not read back. Its digest cannot be computed, and+# "could not decrypt it" must not read as "there was never one": the ledger takes this instead,+# which is an identity no caller can ever match, so the fallback refuses.+_UNREADABLE_CREDENTIAL_IDENTITY = "a-credential-this-host-could-not-read"+++def note_host_credential_identity(+ token: Optional[str], *, a_credential_was_held: bool = False+) -> None:+ """Enter a credential this host HELD in the ledger, while Studio still has it to hash.++ Called from the save and the delete paths, not only from the authorization gate that reads+ the ledger: on an upgraded install the ledger starts empty, and an operator who replaced or+ cleared the saved token before any unaskable probe ever ran left no trace of the old one at+ all. A later outage then found "this host has never held a credential" and handed a+ tokenless caller everything that credential had downloaded.+ """+ if isinstance(token, str) and token:+ _note_host_credential_identities((token,))+ elif a_credential_was_held:+ _note_host_credential_identities((_UNREADABLE_CREDENTIAL_IDENTITY,))+++def _no_other_credential_ever_held(tokens: Iterable[str]) -> Optional[bool]:+ """``None`` when unreadable; a ledger predating a rotation cannot report what it never saw."""+ seen = _host_credential_identities()+ if seen is None:+ return None+ mine = {_credential_identity(token) for token in tokens if isinstance(token, str) and token}+ return not (set(seen) - mine)+++def _repo_was_fetched_with_a_request_token(+ repo_id: Optional[str], repo_type: Optional[str]+) -> Optional[bool]:+ if not repo_id:+ return None+ recorded = _recorded_request_token_repos()+ if recorded is None:+ return None+ if _request_token_repo_key(repo_id, repo_type) in recorded:+ return True+ if len(recorded) >= _REQUEST_TOKEN_REPOS_MAX:+ # Past the cap, "not in it" no longer means "not fetched with one". Cannot say.+ return None+ if _provenance_record_is_missing(repo_id, repo_type):+ return None+ return False+++def _repo_fetched_by_this_credential(+ token: str, repo_id: Optional[str], repo_type: Optional[str]+) -> bool:+ """``True`` also when there is NO record, the case a cache older than the record exists for."""+ if not repo_id:+ return False+ recorded = _recorded_request_token_repos()+ if recorded is None:+ return False+ entry = recorded.get(_request_token_repo_key(repo_id, repo_type))+ if entry is None:+ if _provenance_record_is_missing(repo_id, repo_type):+ return False+ return len(recorded) < _REQUEST_TOKEN_REPOS_MAX+ if not isinstance(entry, dict):+ return False+ recorded_by = entry.get("by")+ if not isinstance(recorded_by, str) or not recorded_by:+ return False+ return hmac.compare_digest(recorded_by, _credential_identity(token))+++def _caller_populated_the_cache(+ token: Optional[str],+ *,+ repo_id: Optional[str] = None,+ repo_type: Optional[str] = None,+) -> bool:+ """The whole safety of the unaskable fallback: disk presence is a fact about the OPERATOR, so+ resolving an unanswerable probe against it for ANY caller hands a second principal the+ operator's private downloads. A cache filled before the record existed reads as "never+ recorded", which authorizes. Compared without short-circuiting, so timing reports nothing."""+ known, host_tokens = _host_hf_credentials()+ if not known:+ return False+ _note_host_credential_identities(host_tokens)+ if token is None:+ if host_tokens:+ return False+ # Held none and never has; a credential since given up must not be inherited tokenless.+ if _no_other_credential_ever_held(()) is not True:+ return False+ return _repo_was_fetched_with_a_request_token(repo_id, repo_type) is False+ if not isinstance(token, str) or not token or not host_tokens:+ return False+ if len({held for held in host_tokens}) > 1:+ # Two DIFFERENT credentials on one host: either could have filled the cache.+ return False+ if _no_other_credential_ever_held(host_tokens) is not True:+ # Holding it now is not having filled the cache with it; otherwise a rotation inherits.+ return False+ matched = False+ for held in host_tokens:+ # Encoded: ``compare_digest`` raises TypeError on a non-ASCII str, and both are user text.+ if hmac.compare_digest(+ token.encode("utf-8", "surrogatepass"), held.encode("utf-8", "surrogatepass")+ ):+ matched = True+ if not matched:+ return False+ # Only the record separates the host credential from a one-off token it does not hold.+ return _repo_fetched_by_this_credential(token, repo_id, repo_type)+++def _resolve_unaskable(repo_id: str, repo_type: str, *, token: Optional[str]) -> bool:+ """The local fact decides, but only for a caller it is about; not on disk -> refused."""+ if not _caller_populated_the_cache(token, repo_id = repo_id, repo_type = repo_type):+ return False+ return _repo_present_on_disk(repo_id, repo_type)+++def _cache_provenance_is_establishable(repo_id: Optional[str], repo_type: Optional[str]) -> bool:+ """The readers below report not knowing as a plain refusal, misleading a caller weighing one."""+ known, _held = _host_hf_credentials()+ if not known:+ return False+ if _recorded_request_token_repos() is None:+ return False+ return not _provenance_record_is_missing(repo_id, repo_type)+++def _denial_can_be_overturned(repo_id: str, repo_type: str, token: Optional[str]) -> bool:+ """Whether remembering this denial protects anything: only where ``_resolve_unaskable`` would+ otherwise say yes. Slots are the point, since the key carries a CALLER-SUPPLIED repo id and+ 8192 denials for nonexistent repos force an eviction that refuses every unaskable probe+ process-wide. Unanswerable means remember: not remembering is what loses safety."""+ # BEFORE the rule below, which answers a plain no for a fact it could not establish: a+ # denial dropped during an unreadable store is authorized by the next outage.+ if not _cache_provenance_is_establishable(repo_id, repo_type):+ return True+ try:+ if not _caller_populated_the_cache(token, repo_id = repo_id, repo_type = repo_type):+ return False+ except Exception: # noqa: BLE001 -- could not establish it; remember, do not discard+ return True+ try:+ return _repo_present_on_disk(repo_id, repo_type)+ except Exception: # noqa: BLE001 -- same direction+ return True+++def _repo_present_on_disk(repo_id: str, repo_type: str) -> bool:+ """Not memoized: a completing download must take effect at once. A dataset also counts when+ only the ``datasets`` PREPARED cache holds it. Asked only after a readable listing FOUND a+ cache dir, since ``repo_cache_has_usable_snapshot`` answers True for an unenumerable root."""+ try:+ from hub.utils.hf_cache_state import (+ iter_repo_cache_dirs,+ repo_cache_has_usable_snapshot,+ )+ except Exception:+ return False+ # Independent evidence: a dataset can have a good prepared cache and an unusable hub dir.+ try:+ if any(True for _dir in iter_repo_cache_dirs(repo_type, repo_id)):+ if bool(repo_cache_has_usable_snapshot(repo_type, repo_id)):+ return True+ except Exception:+ import logging+ logging.getLogger(__name__).debug(+ "Could not check the local cache for '%s'", repo_id, exc_info = True+ )+ if repo_type != "dataset":+ return False+ if repo_type != "dataset":+ return False+ try:+ from hub.utils.dataset_cache import latest_processed_dataset_cache_path+ return latest_processed_dataset_cache_path(repo_id) is not None+ except Exception:+ return False++def _hub_offline() -> bool:try:from utils.utils import hf_env_offline@@ -274,11 +809,12 @@ def _hub_offline() -> bool:return False-def _cached_repo_access(key: tuple[str, str, str], now: float) -> Optional[bool]:+def _cached_repo_access(key: tuple[str, str, str], now: float):+ """``None`` is a verdict of its own ("could not be asked"), so a miss needs its own sentinel."""cached = _repo_access_cache.get(key)if cached is not None and cached[0] > now:return cached[1]- return None+ return _CACHE_MISSdef _explicit_token_reaches_repo(@@ -286,7 +822,7 @@ def _explicit_token_reaches_repo(token: Optional[str],repo_type: str,offline: bool = False,-) -> bool:+) -> Optional[bool]:# None asks the public question, under its own key: a public repo answers 200 for every# token, so a shared key would let any string claim that verdict.key = (@@ -295,32 +831,43 @@ def _explicit_token_reaches_repo(hashlib.sha256(token.encode()).hexdigest()[:16] if token else "anonymous",)cached = _cached_repo_access(key, time.monotonic())- if cached is not None:- return cached+ if cached is not _CACHE_MISS:+ return _with_remembered_denial(key, cached) # type: ignore[arg-type]if offline or _hub_offline():- return False+ # Not memoized: a memo would outlive the moment the network comes back.+ return _with_remembered_denial(key, None)with _inflight_lock(key):cached = _cached_repo_access(key, time.monotonic())- if cached is not None:- return cached- started = time.monotonic()+ if cached is not _CACHE_MISS:+ return _with_remembered_denial(key, cached) # type: ignore[arg-type]try:allowed = _probe_repo_access(repo_id, token, repo_type)- timed_out = Falseexcept _ProbeTimedOut:- allowed = False- timed_out = True+ allowed = None+ except Exception:+ # The gate's own failure is a failure to ASK, not a denial.+ import logging+ logging.getLogger(__name__).debug(+ "Repo access probe for '%s' raised", repo_id, exc_info = True+ )+ allowed = None# AFTER the probe: `start + TTL` memoizes an expired entry when the Hub stalls.finished = time.monotonic()- if not timed_out:- timed_out = not allowed and (finished - started) >= _REPO_ACCESS_PROBE_TIMEOUT_S- expiry = finished + (_REPO_ACCESS_UNREACHABLE_TTL_S if timed_out else _REPO_ACCESS_TTL_S)+ # No elapsed-time rewrite: the budget covers the cold import, so slow is not denied.+ expiry = finished + (+ _REPO_ACCESS_UNREACHABLE_TTL_S if allowed is None else _REPO_ACCESS_TTL_S+ )with _repo_access_lock:if len(_repo_access_cache) >= _REPO_ACCESS_CACHE_MAX:_evict_repo_access_locked()_repo_access_cache[key] = (expiry, allowed)- return allowed+ if allowed is False:+ if _denial_can_be_overturned(repo_id, repo_type, token):+ _remember_denial(key, finished)+ elif allowed is True:+ _forget_denial(key)+ return _with_remembered_denial(key, allowed)def _evict_repo_access_locked() -> None:@@ -385,7 +932,38 @@ def _same_probe_target(answered: str, asked: str) -> bool:return False-def _probe_repo_access(repo_id: str, token: Optional[str], repo_type: str) -> bool:+def _probe_answer_from_exception(exc: BaseException, response = None) -> Optional[bool]:+ """A blanket ``except -> False`` cannot tell an outage from a rejected credential."""+ if _is_probe_timeout(exc):+ return None+ for cls in type(exc).__mro__:+ if cls.__name__ in _DENIAL_EXC_NAMES:+ return False+ status = getattr(response, "status_code", None)+ if not isinstance(status, int):+ # hub attaches the response to HfHubHTTPError; a plain httpx error carries it too.+ status = getattr(getattr(exc, "response", None), "status_code", None)+ if isinstance(status, int):+ if status in _DENIAL_STATUSES:+ return False+ if status == 404 and _has_hf_error_code(response):+ # HF's own "no such repo for you"; a mirror with no /auth-check route sends a bare 404.+ return False+ return None+++def _has_hf_error_code(response) -> bool:+ try:+ headers = getattr(response, "headers", None)+ if headers is None:+ return False+ return bool(headers.get("X-Error-Code"))+ except Exception:+ return False+++def _probe_repo_access(repo_id: str, token: Optional[str], repo_type: str) -> Optional[bool]:+ response = Nonetry:from huggingface_hub import constantsfrom huggingface_hub.utils import build_hf_headers, get_session, hf_raise_for_status@@ -408,24 +986,25 @@ def _probe_repo_access(repo_id: str, token: Optional[str], repo_type: str) -> boheaders = build_hf_headers(token = token if token else False),timeout = _REPO_ACCESS_PROBE_TIMEOUT_S,)- hf_raise_for_status(response)- # hf_raise_for_status passes 3xx: without this a bare 307 reads as authorized.- if 300 <= getattr(response, "status_code", 0) < 400:- return False+ # hf_raise_for_status passes 3xx, so a bare 307 reads as authorized. Not a denial either.+ if 300 <= (getattr(response, "status_code", 0) or 0) < 400:+ return None# And get_session DOES follow them (httpx.Client(follow_redirects=True)), so the# check above never fires: a redirect to a login page returns an approving 200 for# somewhere else. Only the repo we asked about may answer for it.if getattr(response, "history", None):- return False+ return None# Belt and braces for a client that does not record history. Compared on parts, not# as strings: httpx canonicalises response.url (lower-cases the host, drops an# explicit :443) while the configured HF_ENDPOINT keeps its spelling, so a raw# comparison denied every mirror written as HF-MIRROR.example or with the port.final_url = getattr(response, "url", None)if final_url is not None and not _same_probe_target(str(final_url), path):- return False+ return None+ # Last, so the checks above see the response rather than an exception built from it.+ hf_raise_for_status(response)return Trueexcept Exception as exc:if _is_probe_timeout(exc):raise _ProbeTimedOut from exc- return False+ return _probe_answer_from_exception(exc, response)diff --git a/studio/backend/hub/utils/host_paths.py b/studio/backend/hub/utils/host_paths.pynew file mode 100644index 00000000000..eb356990bfe--- /dev/null+++ b/studio/backend/hub/utils/host_paths.py@@ -0,0 +1,636 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Keep the host's filesystem layout inside the host. For an ``sk-unsloth`` API key every host+path in an inventory payload is replaced by an opaque reference, stable for the life of the+server so a client can still group rows. Output encoding, not an authorization decision.+"""++from __future__ import annotations++import hmac+import os+import re+import secrets+import threading+import time+from collections import OrderedDict+from contextvars import ContextVar+from functools import lru_cache+from hashlib import sha256+from typing import Any, Iterable, Mapping, Optional++# One list, so a new route cannot introduce a path field the drift test misses.+HOST_PATH_SCALAR_FIELDS = frozenset(+ {+ "cache_path",+ "load_cache_path",+ "hf_cache_dir",+ "models_dir",+ "repo_path",+ "snapshot_path",+ "local_path",+ "model_local_path",+ "dataset_local_path",+ "dataset_path",+ "tensorboard_dir",+ }+)++# Referenced, not blanked: blanking left `can_resume` true beside nothing to resume with.+# The two snapshot pins are here for the same reason one layer down. Resume replays the run's+# config, and a blanked pin is FALSY, so the preflight stops pinning and re-resolves the newest+# cached revision instead: the checkpoint then continues against different base weights, with no+# warning, because a pin that was never asked for cannot go missing.+HOST_PATH_HANDLE_FIELDS = frozenset(+ {+ "output_dir",+ "checkpoint_path",+ "resume_from_checkpoint",+ "model_snapshot_path",+ "dataset_snapshot_path",+ }+)++# Scrubbed, not blanked: the only account of WHY a run failed.+HOST_PATH_TEXT_FIELDS = frozenset({"error_message", "error", "detail", "message"})++# The same text, one per entry. A run's warnings quote the file they are about ("missing+# <path>/tokenizer.json"), and the singular fields above do not reach a list.+HOST_PATH_TEXT_LIST_FIELDS = frozenset({"warnings", "errors", "details_lines"})++# Emptied, not referenced: scan ROOTS carry layout and nothing actionable.+HOST_PATH_LIST_FIELDS = frozenset(+ {+ "exact_paths",+ "lmstudio_dirs",+ "ollama_dirs",+ "hermes_dirs",+ "scanned_dirs",+ "output_dirs",+ "dataset_paths",+ }+)++# Referenced entry by entry: emptying leaves a resumable checkpoint with no dataset to resume on.+HOST_PATH_HANDLE_LIST_FIELDS = frozenset({"local_datasets", "local_eval_datasets"})++# A path on the inventory objects and a repo id elsewhere, so never redacted by field name alone.+HOST_PATH_AMBIGUOUS_FIELD = "path"++# Decided by a SIBLING: a repo id for most adapters, a path for a locally trained one.+HOST_PATH_CONDITIONAL_FIELD = "base_model"+HOST_PATH_CONDITIONAL_SOURCE_FIELD = "base_model_source"+HOST_PATH_CONDITIONAL_SOURCE_LOCAL = "local"+++def _conditional_path_is_local(payload: Mapping) -> bool:+ return payload.get(HOST_PATH_CONDITIONAL_SOURCE_FIELD) == HOST_PATH_CONDITIONAL_SOURCE_LOCAL+++# A row whose IDENTITY is a path: blanking `path` hides nothing while `id`, `load_id` and+# `inventory_id` spell it out. The rest are decided by VALUE, not field name.+HOST_PATH_IDENTITY_FIELDS = (+ "id",+ "load_id",+ "repo_id",+ "model_name",+ "active_model",+ "model_identifier",+ "dataset_name",+ "base_repo",+)+HOST_PATH_IDENTITY_LIST_FIELDS = ("loaded", "loading")+# The subset a LOCAL row is named by, referenced on source alone; the others only on value.+HOST_PATH_ROW_IDENTITY_FIELDS = ("id", "load_id")+HOST_PATH_ENCODED_IDENTITY_FIELD = "inventory_id"+HOST_PATH_ROW_SOURCE_FIELD = "source"+# `hf_cache` is absent: those rows are named by repo id, and otherwise value decides.+HOST_PATH_LOCAL_SOURCES = frozenset({"models_dir", "lmstudio", "ollama", "hermes", "custom"})+++def _row_identity_is_a_path(payload: Mapping) -> bool:+ return payload.get(HOST_PATH_ROW_SOURCE_FIELD) in HOST_PATH_LOCAL_SOURCES+++def _identity_value_is_a_path(value: Any) -> bool:+ """Whatever the row's source says: a copy outside the active cache pins a row to a path."""+ return isinstance(value, str) and bool(value) and _looks_absolute(value)+++def _referenced_identity(value: Any) -> Any:+ if not isinstance(value, str) or not value:+ return value+ return cache_reference(value) or ""+++def _referenced_inventory_id(value: Any) -> Any:+ """`<source>:<format>:<identity>[:<variant>]`; the shape is kept because clients split on it."""+ if not isinstance(value, str) or not value:+ return value+ parts = value.split(":")+ if len(parts) < 3:+ return _referenced_identity(value)+ parts[2] = (cache_reference(parts[2]) or "").removeprefix(_REFERENCE_PREFIX)+ return ":".join(parts)+++# Sibling written beside a redacted scalar, so a client keeps the identity the path gave it.+CACHE_REFERENCE_FIELD = "cache_ref"++_REFERENCE_PREFIX = "ref:"++# Per-process, so a reference cannot be brute-forced from guessed home directories.+_REFERENCE_KEY = secrets.token_bytes(32)+++def host_paths_visible(via_api_key: Any) -> bool:+ """Non-bool means no HTTP request: an in-process call gets a truthy unresolved ``Depends``."""+ if not isinstance(via_api_key, bool):+ return True+ return not via_api_key+++_REFERENCE_LIMIT = 8192+# Evicted by AGE: with no row limit, count-based eviction drops entries of the response being+# built RIGHT NOW. The ceiling still bounds the table.+_REFERENCE_PIN_SECONDS = 120.0+_REFERENCE_CEILING = 65536+_reference_paths: "OrderedDict[str, tuple[str, float]]" = OrderedDict()+_reference_lock = threading.Lock()+++def cache_reference(value: Any) -> Optional[str]:+ text = _as_text(value)+ if not text:+ return None+ digest = hmac.new(_REFERENCE_KEY, os.fsencode(text), sha256).hexdigest()+ reference = f"{_REFERENCE_PREFIX}{digest[:32]}"+ now = time.monotonic()+ with _reference_lock:+ _reference_paths[reference] = (text, now)+ _reference_paths.move_to_end(reference)+ while len(_reference_paths) > _REFERENCE_LIMIT:+ oldest, (_path, issued) = next(iter(_reference_paths.items()))+ if (+ now - issued < _REFERENCE_PIN_SECONDS+ and len(_reference_paths) <= _REFERENCE_CEILING+ ):+ break+ _reference_paths.pop(oldest, None)+ return reference+++# Per REQUEST, so one caller's substitutions cannot reach another's response.+_request_handles: "ContextVar[Optional[dict[str, str]]]" = ContextVar(+ "unsloth_request_inventory_handles", default = None+)+++def note_resolved_handle(handle: str, path: str) -> None:+ if not isinstance(handle, str) or not isinstance(path, str) or not handle or not path:+ return+ if handle == path:+ return+ known = _request_handles.get()+ if known is None:+ known = {}+ _request_handles.set(known)+ known[path] = handle+++def restore_inventory_handles(payload: Any) -> Any:+ """A substring swap, not a field list: the path also appears in labels and error details."""+ known = _request_handles.get()+ if not known:+ return payload+ return _restore(payload, known)+++def _restore(payload: Any, known: "dict[str, str]") -> Any:+ dumped = _dump_model(payload)+ if dumped is not None:+ return _restore(dumped, known)+ if isinstance(payload, Mapping):+ return {key: _restore(value, known) for key, value in payload.items()}+ if isinstance(payload, (list, tuple)):+ restored = [_restore(item, known) for item in payload]+ if not isinstance(payload, tuple):+ return restored+ # A NamedTuple takes its fields one by one and a plain tuple the iterable; deciding by+ # try/except turns the plain `("abc",)` into `("a", "b", "c")` without raising.+ if hasattr(payload, "_fields"):+ try:+ return type(payload)(*restored)+ except Exception: # noqa: BLE001 -- a subclass with its own constructor+ return tuple(restored)+ try:+ return type(payload)(restored)+ except Exception: # noqa: BLE001 -- same+ return tuple(restored)+ if isinstance(payload, str):+ text = payload+ # Longest first, so a path that is a prefix of another does not claim its text.+ for path in sorted(known, key = len, reverse = True):+ if path in text:+ text = _swap_at_boundaries(text, path, known[path])+ return text+ return payload+++# A plain substring swap turns a sibling (`/srv/models/foo-private` beside a resolved+# `/srv/models/foo`) into `ref:<digest>-private`, which resolves to nothing and no longer reads+# as a path, so the layout rides out in it. The component must END at the boundary, so a letter,+# digit, `-`, `_`, `.` or space means a DIFFERENT path; declining is safe, since an unswapped+# path is still removed on the way out.+_HANDLE_LEFT_BOUNDARY = r"(?<![\w:/.\\])"+_HANDLE_RIGHT_BOUNDARY = r"(?![^\s\\/\n\":;,=])"+++@lru_cache(maxsize = 4096)+def _boundary_pattern(path: str) -> "re.Pattern[str]":+ return re.compile(_HANDLE_LEFT_BOUNDARY + re.escape(path) + _HANDLE_RIGHT_BOUNDARY)+++def _swap_at_boundaries(text: str, path: str, handle: str) -> str:+ try:+ pattern = _boundary_pattern(path)+ except Exception: # noqa: BLE001 -- an uncompilable path must not fail the response+ return text+ return pattern.sub(lambda _match: handle, text)+++def resolve_host_path_reference(value: Any) -> Optional[str]:+ """Not an authorization decision: callers stay subject to the checks a named path gets."""+ text = _as_text(value)+ if not text or not text.startswith(_REFERENCE_PREFIX):+ return None+ with _reference_lock:+ entry = _reference_paths.get(text)+ return entry[0] if entry else None+++def redact_host_paths(+ payload: Any,+ *,+ via_api_key: bool,+ echo: Iterable[Any] = (),+) -> Any:+ """``echo`` holds values THIS CALLER supplied, returned as written; see ``_echoed``."""+ if host_paths_visible(via_api_key):+ return payload+ return _redact(payload, redact_ambiguous_path = False, echo = _echo_set(echo))+++def redact_inventory_host_paths(+ payload: Any,+ *,+ via_api_key: bool,+ echo: Iterable[Any] = (),+) -> Any:+ if host_paths_visible(via_api_key):+ return payload+ return _redact(payload, redact_ambiguous_path = True, echo = _echo_set(echo))+++def _echo_set(echo: Iterable[Any]) -> frozenset:+ return frozenset(text for text in (_as_text(value) for value in echo) if text)+++def response_leaks_host_path(+ payload: Any,+ roots: Iterable[Any] = (),+ *,+ ignore: Iterable[str] = (),+) -> Optional[str]:+ """For tests and the drift gate. ``ignore`` makes keeping a path explicit at each call."""+ needles = [text for text in (_as_text(root) for root in roots) if text]+ return _find_leak(payload, needles, ambiguous_is_path = True, ignore = frozenset(ignore))+++def _echoed(value: Any, echo: frozenset) -> bool:+ """A reference cannot stand in for a path the caller NAMED: the HMAC is process-stable, so+ referencing caller-chosen input is a confirmation oracle."""+ text = _as_text(value)+ return bool(text) and text in echo+++def _redact(+ payload: Any,+ *,+ redact_ambiguous_path: bool,+ echo: frozenset = frozenset(),+) -> Any:+ dumped = _dump_model(payload)+ if dumped is not None:+ return _redact(dumped, redact_ambiguous_path = redact_ambiguous_path, echo = echo)+ if isinstance(payload, Mapping):+ out: dict[Any, Any] = {}+ reference: Optional[str] = None+ # Read before the walk: the sibling that decides it may come after it in the dump.+ base_model_is_local = _conditional_path_is_local(payload)+ identity_is_a_path = redact_ambiguous_path and _row_identity_is_a_path(payload)+ for key, value in payload.items():+ if key in HOST_PATH_IDENTITY_FIELDS and (+ (identity_is_a_path and key in HOST_PATH_ROW_IDENTITY_FIELDS)+ or _identity_value_is_a_path(value)+ ):+ out[key] = value if _echoed(value, echo) else _referenced_identity(value)+ continue+ if key in HOST_PATH_IDENTITY_LIST_FIELDS and isinstance(value, (list, tuple)):+ out[key] = [+ _referenced_identity(item)+ if _identity_value_is_a_path(item) and not _echoed(item, echo)+ else item+ for item in value+ ]+ continue+ if identity_is_a_path and key == HOST_PATH_ENCODED_IDENTITY_FIELD:+ out[key] = value if _echoed(value, echo) else _referenced_inventory_id(value)+ continue+ if (+ key == HOST_PATH_CONDITIONAL_FIELD+ and not base_model_is_local+ and _identity_value_is_a_path(value)+ ):+ out[key] = value if _echoed(value, echo) else _referenced_identity(value)+ continue+ if key in HOST_PATH_HANDLE_FIELDS:+ # Only where the value really is a path: a relative output dir names no layout.+ out[key] = (+ _referenced_identity(value)+ if _identity_value_is_a_path(value) and not _echoed(value, echo)+ else value+ )+ continue+ if (+ key in HOST_PATH_SCALAR_FIELDS+ or (redact_ambiguous_path and key == HOST_PATH_AMBIGUOUS_FIELD)+ or (base_model_is_local and key == HOST_PATH_CONDITIONAL_FIELD)+ ):+ # Only the row-level cache dir: one per scan root is layout again.+ if (+ key in {"cache_path", "repo_path"}+ and reference is None+ and not _echoed(value, echo)+ ):+ reference = cache_reference(value)+ out[key] = None if value is None else ""+ continue+ if key in HOST_PATH_HANDLE_LIST_FIELDS and isinstance(value, (list, tuple)):+ out[key] = [+ _referenced_identity(item)+ if _identity_value_is_a_path(item) and not _echoed(item, echo)+ else item+ for item in value+ ]+ continue+ if key in HOST_PATH_LIST_FIELDS:+ out[key] = []+ continue+ if key in HOST_PATH_TEXT_FIELDS and isinstance(value, str):+ out[key] = redact_paths_in_text(value)+ continue+ if key in HOST_PATH_TEXT_LIST_FIELDS and isinstance(value, (list, tuple)):+ out[key] = [+ redact_paths_in_text(item)+ if isinstance(item, str)+ else _redact(item, redact_ambiguous_path = redact_ambiguous_path, echo = echo)+ for item in value+ ]+ continue+ out[key] = _redact(value, redact_ambiguous_path = redact_ambiguous_path, echo = echo)+ # After the walk: the field is declared on the models, so the dump's None would win.+ if reference is not None and not out.get(CACHE_REFERENCE_FIELD):+ out[CACHE_REFERENCE_FIELD] = reference+ return out+ if isinstance(payload, (list, tuple)):+ redacted = [+ _redact(item, redact_ambiguous_path = redact_ambiguous_path, echo = echo)+ for item in payload+ ]+ if not isinstance(payload, tuple):+ return redacted+ # A NamedTuple's constructor takes the fields one by one, so a list rebuild raises.+ if hasattr(payload, "_fields"):+ try:+ return type(payload)(*redacted)+ except Exception:+ return tuple(redacted)+ return type(payload)(redacted)+ return payload+++def _find_leak(+ payload: Any,+ needles: list[str],+ *,+ ambiguous_is_path: bool,+ ignore: frozenset[str] = frozenset(),+) -> Optional[str]:+ dumped = _dump_model(payload)+ if dumped is not None:+ return _find_leak(dumped, needles, ambiguous_is_path = ambiguous_is_path, ignore = ignore)+ if isinstance(payload, Mapping):+ base_model_is_local = _conditional_path_is_local(payload)+ for key, value in payload.items():+ if key in ignore:+ continue+ text = _as_text(value)+ is_path_field = (+ key in HOST_PATH_SCALAR_FIELDS+ or key in HOST_PATH_HANDLE_FIELDS+ or (ambiguous_is_path and key == HOST_PATH_AMBIGUOUS_FIELD)+ or (base_model_is_local and key == HOST_PATH_CONDITIONAL_FIELD)+ )+ if is_path_field and text and _looks_absolute(text):+ return f"{key}={text}"+ if key in HOST_PATH_IDENTITY_FIELDS and _identity_value_is_a_path(text):+ return f"{key}={text}"+ if (key in HOST_PATH_LIST_FIELDS or key in HOST_PATH_HANDLE_LIST_FIELDS) and isinstance(+ value, (list, tuple)+ ):+ for item in value:+ item_text = _as_text(item)+ if item_text and _looks_absolute(item_text):+ return f"{key}[]={item_text}"+ if text and any(needle in text for needle in needles):+ return f"{key}={text}"+ found = _find_leak(value, needles, ambiguous_is_path = ambiguous_is_path, ignore = ignore)+ if found is not None:+ return found+ return None+ if isinstance(payload, (list, tuple)):+ for item in payload:+ found = _find_leak(item, needles, ambiguous_is_path = ambiguous_is_path, ignore = ignore)+ if found is not None:+ return found+ return None+ text = _as_text(payload)+ if text and any(needle in text for needle in needles):+ return text+ return None+++def short_path_for_log(value: Any) -> str:+ text = _as_text(value)+ if not text:+ return ""+ # A RELATIVE path is returned as written: `.../models/repo` invents a parent it lacks.+ if not _is_absolute_for_log(text):+ return text+ parts = [part for part in text.replace("\\", "/").split("/") if part]+ if len(parts) <= 2:+ # Rebuilding turned `/srv/cache` into `srv/cache`.+ return text+ return ".../" + "/".join(parts[-2:])+++def _is_absolute_for_log(text: str) -> bool:+ """Judged as WRITTEN, not against this host's OS: a Windows path read on Linux is still one."""+ return bool(text.startswith(("/", "\\")) or re.match(r"^[A-Za-z]:[\\/]", text))+++# Quotes, whitespace, punctuation and `=` terminate the run, so a two-path line is not swallowed+# whole. Spaces are allowed INSIDE a component.+_PATH_COMPONENT = r"[^\s\\/\n\":;,=](?:[^\\/\n\":;,=]*[^\s\\/\n\":;,=])?"++_ABSOLUTE_PATH_RE = re.compile(+ # Not preceded by a word character, colon or slash, so a URL and a ratio like "3/4" survive.+ # Components are a DENYLIST of terminators, or `client(acme)` stops the run at the `(`.+ r"(?<![\w:/.])(?:\\\\[^\\/\s]+[\\/]|[A-Za-z]:[\\/]|/)"+ r"(?:" + _PATH_COMPONENT + r"[\\/])*" + _PATH_COMPONENT+)+++# A path written as a URI. The lookbehind above refuses the leading slash of `file:///home/...`+# because it follows `:` and `/`, which is exactly what keeps `https://huggingface.co/...` intact,+# so the filesystem schemes are matched separately rather than by loosening it. The optional drive+# letter is the Windows form `file:///C:/Users/...`, whose `:` is a component terminator.+_FILE_URI_RE = re.compile(+ r"(?<![\w.-])(?:file|filesystem):/{2,3}(?:localhost)?/?(?:[A-Za-z]:[\\/])?"+ r"(?:" + _PATH_COMPONENT + r"[\\/])*" + _PATH_COMPONENT+)+++# Only the scheme and its authority slashes: the path's own leading slash stays, or what is left+# reads as relative and `short_path_for_log` returns it whole.+_FILE_URI_PREFIX_RE = re.compile(r"^(?:file|filesystem)://(?:localhost)?")+++def scrub_paths(text: Any) -> str:+ """Shorten every absolute path inside a log message. Not a security control: the log is local."""+ # str(), not _as_text: the usual argument is an exception, whose message is the whole point.+ message = text if isinstance(text, str) else ("" if text is None else str(text))+ if not message:+ return ""+ # The scheme comes off first: `short_path_for_log` judges "absolute" on the first character,+ # and `file:` is not one, so it would hand the whole URI back unshortened.+ message = _FILE_URI_RE.sub(+ lambda match: short_path_for_log(_FILE_URI_PREFIX_RE.sub("", match.group(0))), message+ )+ return _ABSOLUTE_PATH_RE.sub(lambda match: short_path_for_log(match.group(0)), message)+++_REDACTED_PATH = "<path>"++# The leftover when a directory name contains a terminator (`Acme, Inc` ends the run early): a+# terminator plus text carrying one more separator is replaced too, that last condition being+# what keeps the terminators terminating. The set must match `_PATH_COMPONENT`'s, `=` included,+# or `/srv/cache/foo=bar/config.json` publishes `<path>=bar/config.json`.+_REDACTED_TAIL_TEXT = r"[^\s\\/\":;,=][^\\/\":;,=]*"+_REDACTED_TAIL_RE = re.compile(+ re.escape(_REDACTED_PATH)+ + r"(?:(?:[:;,=][ ]?"+ + _REDACTED_TAIL_TEXT+ + r")+"+ + r"(?:[\\/]"+ + _REDACTED_TAIL_TEXT+ + r")+)+"+)+++def redact_paths_in_text(text: Any) -> str:+ """Removed, not shortened: the component ``scrub_paths`` keeps is layout once text leaves."""+ message = text if isinstance(text, str) else ("" if text is None else str(text))+ if not message:+ return ""+ message = _FILE_URI_RE.sub(_REDACTED_PATH, message)+ return _REDACTED_TAIL_RE.sub(_REDACTED_PATH, _ABSOLUTE_PATH_RE.sub(_REDACTED_PATH, message))+++def redact_inventory_error_detail(detail: Any, *, via_api_key: bool) -> Any:+ """The response redactors only see a payload that was BUILT; a raising route comes here."""+ if host_paths_visible(via_api_key):+ return detail+ if isinstance(detail, str):+ return redact_paths_in_text(detail)+ if isinstance(detail, Mapping):+ return {+ key: redact_inventory_error_detail(value, via_api_key = via_api_key)+ for key, value in detail.items()+ }+ if isinstance(detail, (list, tuple)):+ redacted = [+ redact_inventory_error_detail(value, via_api_key = via_api_key) for value in detail+ ]+ if not isinstance(detail, tuple):+ return redacted+ # A NamedTuple takes its fields one by one; handing it the list raises TypeError, and+ # this runs while a route is already raising, so the caller gets a 500 not the refusal.+ if hasattr(detail, "_fields"):+ try:+ return type(detail)(*redacted)+ except Exception: # noqa: BLE001 -- a subclass with its own constructor+ return tuple(redacted)+ try:+ return type(detail)(redacted)+ except Exception: # noqa: BLE001 -- same+ return tuple(redacted)+ return detail+++def redact_load_progress(progress: Any, *, via_api_key: bool) -> Any:+ """A worker-thread failure stores ``str(exc)``, naming the path a ``ref:`` load resolved to,+ answered on a LATER request whose handle map is empty."""+ if not isinstance(progress, Mapping):+ return progress+ if host_paths_visible(via_api_key) or not progress.get("error"):+ return dict(progress)+ return {**progress, "error": redact_paths_in_text(progress["error"])}+++def raised_inventory_detail(detail: Any, *, via_api_key: bool) -> Any:+ """For a payload that was RAISED, since the route's wrappers never run. Restore first so a+ path the caller owns comes back as its handle, then redact what is left."""+ return redact_inventory_error_detail(restore_inventory_handles(detail), via_api_key = via_api_key)+++def _dump_model(payload: Any) -> Optional[dict]:+ """A pydantic response object as a plain dict, or ``None``. Duck-typed: imported beneath."""+ dump = getattr(payload, "model_dump", None)+ if dump is None or isinstance(payload, type) or isinstance(payload, Mapping):+ return None+ try:+ dumped = dump()+ except Exception:+ return None+ return dumped if isinstance(dumped, dict) else None+++def _looks_absolute(text: str) -> bool:+ if text.startswith(_REFERENCE_PREFIX):+ return False+ if text.startswith("/") or text.startswith("\\\\"):+ return True+ return len(text) > 2 and text[1] == ":" and text[2] in "\\/"+++def _as_text(value: Any) -> str:+ if value is None or isinstance(value, bool):+ return ""+ if isinstance(value, str):+ return value+ if isinstance(value, os.PathLike):+ return os.fspath(value)+ return ""diff --git a/studio/backend/hub/utils/inventory_scan.py b/studio/backend/hub/utils/inventory_scan.pyindex b32ee7da6b4..a63fcb16d00 100644--- a/studio/backend/hub/utils/inventory_scan.py+++ b/studio/backend/hub/utils/inventory_scan.py@@ -34,6 +34,7 @@ from hub.utils.hf_tokens import (is_anonymous,qualify_cache_identity,)+from hub.utils.host_paths import scrub_paths, short_path_for_logfrom hub.utils.state_dir import RepoTypefrom hub.utils.hf_cache_state import (@@ -388,7 +389,7 @@ def _with_repos_dropped_by_scan()except (AttributeError, TypeError, ValueError) as exc:# A scan shape we cannot rebuild is left untouched rather than dropped.- logger.debug("Could not attach recovered HF cache repos: %s", exc)+ logger.debug("Could not attach recovered HF cache repos: %s", scrub_paths(exc))return scan@@ -420,7 +421,9 @@ def _compute_all_hf_cache_scans() -> list:if recovered.repos:scans.append(recovered)except Exception as exc:- logger.warning("Could not scan HF cache %s: %s", cache_root, exc)+ logger.warning(+ "Could not scan HF cache %s: %s", short_path_for_log(cache_root), scrub_paths(exc)+ )return scansdiff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.pyindex 8061cadb66b..599f1442179 100644--- a/studio/backend/models/inference.py+++ b/studio/backend/models/inference.py@@ -34,6 +34,23 @@ from picker.schemas import MAX_CHAT_TEMPLATE_BYTESfrom utils.reasoning_budget import validate_reasoning_budget_message+def resolve_inventory_handle(value: str) -> str:+ """Turn a `ref:...` inventory identity back into the path it stands for. A reference this+ process did not issue is left as it arrived. Decides nothing about authorization."""+ if not isinstance(value, str) or not value.startswith("ref:"):+ return value+ try:+ from hub.utils.host_paths import note_resolved_handle, resolve_host_path_reference+ except Exception: # noqa: BLE001 -- a resolver that cannot import must not fail loads+ return value+ resolved = resolve_host_path_reference(value)+ if not resolved:+ return value+ # Remembered for this request so the ANSWER carries the handle, not the path.+ note_resolved_handle(value, resolved)+ return resolved++class LoadRequest(BaseModel):"""Request to load a model for inference"""@@ -82,6 +99,8 @@ class LoadRequest(BaseModel):description = "Custom Jinja2 chat template to use instead of the model's default",)+ _resolve_the_handle = field_validator("model_path")(resolve_inventory_handle)+@field_validator("chat_template_override")@classmethoddef normalize_blank_chat_template_override(cls, value: Optional[str]) -> Optional[str]:@@ -412,6 +431,9 @@ class UnloadRequest(BaseModel):"unload takes away the llama-server they are decoding on."),)+ # The resident model is keyed on the path, so an unresolved handle matches nothing and+ # reports success while the model keeps its GPU.+ _resolve_the_handle = field_validator("model_path")(resolve_inventory_handle)class SearchImagesLookupRequest(BaseModel):@@ -468,6 +490,8 @@ class ValidateModelRequest(BaseModel):"""Check whether an identifier resolves to a ModelConfig; does NOT load weights."""model_path: str = Field(..., description = "Model identifier or local path")+ # The same inventory handle the picker was shown; see `resolve_inventory_handle`.+ _resolve_the_handle = field_validator("model_path")(resolve_inventory_handle)native_path_lease: Optional[str] = Field(None, description = "Frontend-visible signed native path grant")@@ -673,6 +697,12 @@ class TransformersUpgradeCheckRequest(BaseModel):"installing would strand that checkpoint's exact 4-bit resume.",)+ # This route SWALLOWS a failed lookup and answers "no upgrade needed", so training starts+ # and dies at model load in the worker.+ _resolve_the_handle = field_validator(+ "model_name", "model_local_path", "model_snapshot_path", "model_snapshot_repo_id"+ )(resolve_inventory_handle)+class TransformersUpgradeCheckResponse(BaseModel):"""Upgrade + quantization preflight for a load that does not run /validate.@@ -873,6 +903,9 @@ class EstimateMemoryRequest(BaseModel):_no_booleans = field_validator("n_batch", "n_ubatch", "ctx_checkpoints", "n_ctx", mode = "before")(LoadRequest._no_booleans.__func__)+ # Unresolved, the reference reads as a Hub id and the panel is told the estimate is+ # unavailable for a row it was offered.+ _resolve_the_handle = field_validator("model_path")(resolve_inventory_handle)class EstimateMemoryResponse(BaseModel):@@ -3676,6 +3709,8 @@ class DiffusionLoadRequest(BaseModel):"""Request to load a local diffusion (text-to-image) checkpoint."""model_path: str = Field(..., description = "Diffusion repo id or local path")+ # The same inventory handle the picker was shown; see `resolve_inventory_handle`.+ _resolve_the_handle = field_validator("model_path")(resolve_inventory_handle)gguf_filename: Optional[str] = Field(None,description = "The chosen single-file checkpoint (GGUF or safetensors) inside "@@ -3692,6 +3727,8 @@ class DiffusionLoadRequest(BaseModel):base_repo: Optional[str] = Field(None, description = "Companion diffusers repo for VAE/text-encoders (default: family base)")+ # Referenced out, so resolved back in, or a caller handed a `ref:` base cannot load it.+ _resolve_the_base_handle = field_validator("base_repo")(resolve_inventory_handle)family_override: Optional[str] = Field(None, description = "Force a family when it can't be inferred from the repo id")@@ -4592,6 +4629,8 @@ class VideoLoadRequest(BaseModel):"""Request to load a local text-to-video checkpoint."""model_path: str = Field(..., description = "Video repo id or local path")+ # The same inventory handle the picker was shown; see `resolve_inventory_handle`.+ _resolve_the_handle = field_validator("model_path")(resolve_inventory_handle)gguf_filename: Optional[str] = Field(None,description = "The chosen single-file checkpoint (GGUF or safetensors) inside "@@ -4609,6 +4648,8 @@ class VideoLoadRequest(BaseModel):None,description = "Companion diffusers repo for VAE/text-encoders (default: family base)",)+ # As on the diffusion request above: referenced out, so resolved back in.+ _resolve_the_base_handle = field_validator("base_repo")(resolve_inventory_handle)family_override: Optional[str] = Field(None, description = "Force a family when it can't be inferred from the repo id")diff --git a/studio/backend/models/training.py b/studio/backend/models/training.pyindex e6c3e8202d1..099bd698af7 100644--- a/studio/backend/models/training.py+++ b/studio/backend/models/training.py@@ -97,12 +97,30 @@ def _parse_lr(v: Any) -> float:return lr+def _resolve_inventory_handles(values):+ """Resume replays a referenced history payload, so the entries come back as handles."""+ if not isinstance(values, (list, tuple)):+ return values+ return [_resolve_inventory_handle(item) if isinstance(item, str) else item for item in values]+++def _resolve_inventory_handle(value: str) -> str:+ """Lazy: `models.inference` is large and the CLI imports this module without needing it."""+ try:+ from models.inference import resolve_inventory_handle+ except Exception: # noqa: BLE001 -- a resolver that cannot import must not fail a run+ return value+ return resolve_inventory_handle(value)++class TrainingStartRequest(BaseModel):"""Request schema for starting training"""model_name: str = Field(..., description = "Model identifier (e.g., 'unsloth/llama-3-8b-bnb-4bit')")+ # The same identity the picker was shown; see `resolve_inventory_handle`.+ _resolve_the_handle = field_validator("model_name")(_resolve_inventory_handle)project_name: Optional[str] = Field(None,max_length = 80,@@ -170,6 +188,10 @@ class TrainingStartRequest(BaseModel):local_eval_datasets: List[str] = Field(default_factory = list, description = "List of local eval dataset paths")+ # The history detail references these, and Resume replays that payload.+ _resolve_the_dataset_handles = field_validator("local_datasets", "local_eval_datasets")(+ _resolve_inventory_handles+ )format_type: str = Field(..., description = "Dataset format type")subset: Optional[str] = Nonetrain_split: Optional[str] = Field("train", description = "Training split name")@@ -278,7 +300,9 @@ class TrainingStartRequest(BaseModel):def _check_cache_local_path(cls, v: Optional[str]) -> Optional[str]:if v is None:return v- v = v.strip()+ # Resolved FIRST, so the checks below run on the path rather than on its handle: the+ # snapshot pins come back referenced, and Resume replays this payload verbatim.+ v = _resolve_inventory_handle(v.strip())if not v:return Noneif len(v) > 4096:@@ -570,6 +594,10 @@ class TrainingStartRequest(BaseModel):resume_from_checkpoint: Optional[str] = Field(None, description = "Saved training output directory to resume from")+ # The history detail hands out a handle for the resumable directory; Resume sends it back.+ _resolve_the_resume_handle = field_validator("resume_from_checkpoint")(+ _resolve_inventory_handle+ )gpu_ids: Optional[List[int]] = Field(None,@@ -821,8 +849,12 @@ class DiffusionTrainingStartRequest(BaseModel):model_config = ConfigDict(protected_namespaces = ())base_model: str = Field(..., description = "HF repo id or local path to a trainable base")+ # Unresolved, family detection and `_assert_trusted_base_model` read `ref:...` as a Hub id.+ _resolve_the_base_handle = field_validator("base_model")(_resolve_inventory_handle)data_dir: str = Field(..., description = "Folder of training images (+ captions)")output_dir: str = Field(..., description = "Directory to write the LoRA .safetensors into")+ # Resume replays the stored config, whose `output_dir` answers as a handle.+ _resolve_the_output_handle = field_validator("output_dir")(_resolve_inventory_handle)model_family: Optional[str] = Field(None,description = "Explicit trainer family (sdxl / flux.1 / ...); omitted = detect from base_model",@@ -966,6 +998,10 @@ class DiffusionTrainingStartRequest(BaseModel):"checkpoint at step 11 with train_steps=500 trains steps 12..500."),)+ # Diffusion Resume replays `checkpoint_path` or the output dir; both answer as references.+ _resolve_the_resume_handle = field_validator("resume_from_checkpoint")(+ _resolve_inventory_handle+ )resumed_from_job_id: Optional[str] = Field(None,pattern = r"^[0-9a-f]{32}$",diff --git a/studio/backend/picker/service.py b/studio/backend/picker/service.pyindex 9a901e7b620..5b3ae113b04 100644--- a/studio/backend/picker/service.py+++ b/studio/backend/picker/service.py@@ -7,10 +7,15 @@ import jsonimport loggingimport osimport re+from contextlib import nullcontextfrom pathlib import Pathfrom typing import Optional-from hub.utils.hf_tokens import cache_reads_authorized, cached_read_refused+from hub.utils.hf_tokens import (+ cache_reads_authorized,+ cached_read_refused,+ recording_a_request_token_fetch,+)from hub.services.models.folder_browser import (_build_browse_allowlist,_is_path_inside_allowlist,@@ -441,16 +446,42 @@ def read_default_chat_template(size = getattr(matched[0], "size", None)return not (isinstance(size, int) and size > MAX_TEMPLATE_METADATA_BYTES)+ def _this_file_was_already_here(rel: str) -> bool:+ """``_this_file_is_cached`` asks the same question and answers True when it cannot+ tell, which is right for a gate and wrong here: not knowing must RECORD, since an+ unrecorded credentialed fetch is what hands a private repo to a tokenless caller."""+ try:+ from huggingface_hub import try_to_load_from_cache+ return isinstance(+ try_to_load_from_cache(+ repo_id = resolved, filename = rel, cache_dir = active_hf_hub_cache()+ ),+ str,+ )+ except Exception: # noqa: BLE001 -- cannot tell, so record+ return False+def _download_text(rel: str) -> Optional[str]:if not _remote_worth_downloading(rel):return Nonetry:- path = hf_hub_download(- resolved,- rel,- token = hf_token,- cache_dir = active_hf_hub_cache(),+ # Lands files in the hub cache under what may be a one-off token, but only when+ # it really fetches: hf_hub_download returns a cached file without asking the+ # Hub, and recording that withholds a repo the cache may have held anonymously.+ # Around the call, not before: a half-dead download has still written, while a+ # 404 leaves nothing and the context manager takes that record back.+ recording = (+ recording_a_request_token_fetch(hf_token, resolved, "model")+ if not _this_file_was_already_here(rel)+ else nullcontext())+ with recording:+ path = hf_hub_download(+ resolved,+ rel,+ token = hf_token,+ cache_dir = active_hf_hub_cache(),+ )return _read_bounded_text(Path(path), MAX_TEMPLATE_METADATA_BYTES)except Exception:return Nonediff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 067d24148d3..e3750202ffb 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -2936,6 +2936,16 @@ def _deferred_error_body(status_code: int, detail) -> bytes:return json.dumps(body).encode()+def _handle_restored_http_exception(exc: HTTPException) -> HTTPException:+ """A new exception rather than a mutated one, so status code and headers stay as raised."""+ from hub.utils.host_paths import restore_inventory_handles++ detail = restore_inventory_handles(exc.detail)+ if detail == exc.detail:+ return exc+ return HTTPException(status_code = exc.status_code, detail = detail, headers = exc.headers)++async def _tunnel_safe_json(coro, *, label: str):"""Await ``coro``, padding the response body if it outruns the tunnel timer.@@ -2948,6 +2958,8 @@ async def _tunnel_safe_json(coro, *, label: str):A client disconnect does not cancel the work: the model stays resident, asit does today."""+ from hub.utils.host_paths import restore_inventory_handles+task = asyncio.ensure_future(coro)# A client that disconnects mid-pad leaves nobody to await the task, and an# unretrieved exception logs "Task exception was never retrieved". Retrieving@@ -2955,7 +2967,11 @@ async def _tunnel_safe_json(coro, *, label: str):task.add_done_callback(lambda t: t.cancelled() or t.exception())done, _ = await asyncio.wait({task}, timeout = _TUNNEL_KEEPALIVE_AFTER_S)if done:- return task.result() # re-raises exactly as an un-wrapped await would+ # Here rather than at each `return LoadResponse`: the one funnel every answer passes.+ try:+ return restore_inventory_handles(task.result())+ except HTTPException as exc:+ raise _handle_restored_http_exception(exc) from Nonelogger.info(f"{label} exceeded {_TUNNEL_KEEPALIVE_AFTER_S:.0f}s; "@@ -2972,12 +2988,16 @@ async def _tunnel_safe_json(coro, *, label: str):payload = task.result()except HTTPException as exc:logger.info(f"{label} failed with {exc.status_code} after the response committed")- yield _deferred_error_body(exc.status_code, exc.detail)+ yield _deferred_error_body(exc.status_code, restore_inventory_handles(exc.detail))except Exception as exc:logger.exception(f"{label} failed after the response was committed")- yield _deferred_error_body(500, f"{type(exc).__name__}: {exc}")+ # A filesystem failure quotes the path it failed on, never shown to a caller+ # that started from an inventory reference.+ yield _deferred_error_body(+ 500, restore_inventory_handles(f"{type(exc).__name__}: {exc}")+ )else:- yield json.dumps(jsonable_encoder(payload)).encode()+ yield json.dumps(jsonable_encoder(restore_inventory_handles(payload))).encode()returnreturn _SameTaskStreamingResponse(@@ -3439,7 +3459,11 @@ from core.inference.anthropic_compat import (AnthropicPassthroughEmitter,)from auth import policy as auth_policy, storage as auth_storage-from auth.authentication import API_KEY_PREFIX, get_current_subject+from auth.authentication import (+ API_KEY_PREFIX,+ authenticated_via_api_key,+ get_current_subject,+)from state import active_generations@@ -15446,6 +15470,97 @@ async def load_model()+def _repo_is_in_the_hub_cache(model_ref) -> Optional[bool]:+ """``None`` when the question does not arise or cannot be answered."""+ try:+ from hub.utils import hf_tokens as _hf_tokens++ repo = (model_ref or "").strip()+ if not repo or "/" not in repo or repo.startswith((".", "/", "~")) or ":" in repo[:3]:+ return None+ return bool(_hf_tokens._repo_present_on_disk(repo, "model"))+ except Exception: # noqa: BLE001 -- a load never fails on its own bookkeeping+ return None+++def _hub_cache_footprint(model_ref) -> Optional[tuple]:+ """Only ``blobs/`` counts: ``refs/`` and ``.no_exist`` are written by a cache HIT too."""+ try:+ from hub.utils.hf_cache_state import iter_repo_cache_dirs++ repo = (model_ref or "").strip()+ if not repo or "/" not in repo or repo.startswith((".", "/", "~")) or ":" in repo[:3]:+ return None+ count = total = 0+ errors: list = []+ for repo_dir in iter_repo_cache_dirs("model", repo, scan_errors = errors):+ for blob in (repo_dir / "blobs").iterdir():+ if blob.is_symlink() or not blob.is_file():+ continue+ count += 1+ total += blob.stat().st_size+ # A root that could not be listed makes the comparison meaningless rather than equal.+ return None if errors else (count, total)+ except FileNotFoundError:+ return (0, 0)+ except Exception: # noqa: BLE001 -- a load never fails on its own bookkeeping+ return None+++def _load_fetched_bytes(model_ref, cached_before, footprint_before) -> bool:+ """Only usable where the caller AWAITED the fetch; the media loads decide at entry instead.+ A wrong record withholds a public cached copy permanently, so a reading that could not be+ taken is not evidence, and GROWTH is the test, not difference (a SHRUNK cache was pruned)."""+ if cached_before is False and _repo_is_in_the_hub_cache(model_ref):+ return True+ if cached_before is not True or footprint_before is None:+ return False+ footprint_after = _hub_cache_footprint(model_ref)+ if footprint_after is None:+ return False+ # Per component, so a prune racing an arrival still reads as a fetch.+ return any(after > before for after, before in zip(footprint_after, footprint_before))+++def _lora_base_already_in_the_hub_cache(model_ref) -> Optional[str]:+ """Resolvable only once the adapter itself is cached; otherwise this load brings both in."""+ try:+ from utils.transformers_version import _adapter_base_from_hf_cache+ base = _adapter_base_from_hf_cache(model_ref) if model_ref else None+ except Exception: # noqa: BLE001 -- a load never fails on its own bookkeeping+ return None+ return base if base and _repo_is_in_the_hub_cache(base) else None+++def _note_lora_base_fetched_with_a_request_token(+ model_ref,+ hf_token,+ already_cached = None,+) -> None:+ """Read from the hub cache, not the network: an unresolvable base has no record, so REFUSES."""+ try:+ from utils.transformers_version import _adapter_base_from_hf_cache+ base = _adapter_base_from_hf_cache(model_ref) if model_ref else None+ except Exception: # noqa: BLE001 -- a load never fails on its own bookkeeping+ return+ # A base ALREADY here was not fetched by this load; recording it withholds a public model.+ if base and base != model_ref and base != already_cached and _repo_is_in_the_hub_cache(base):+ _note_load_fetched_with_a_request_token(base, hf_token)+++def _note_load_fetched_with_a_request_token(model_ref, hf_token) -> None:+ """A load auto-downloads through its own loader, not the download lifecycle. Never local."""+ try:+ from hub.utils import hf_tokens as _hf_tokens++ repo = (model_ref or "").strip()+ if not repo or "/" not in repo or repo.startswith((".", "/", "~")) or ":" in repo[:3]:+ return+ _hf_tokens.note_repo_fetched_with_a_request_token(hf_token, repo, "model")+ except Exception: # noqa: BLE001 -- a load never fails on its own bookkeeping+ pass++async def load_model_gated(request: LoadRequest,fastapi_request: Request,@@ -15570,6 +15685,12 @@ async def _load_model_impl(request = request.model_copy(update = {"hf_token": account_access.account_hf_token(request.hf_token)})+ # A one-off `X-Unsloth-HF-Token` on a LOAD pulls the repo into the same cache and is kept+ # nowhere, so provenance is recorded in the finally below. Presence alone is not the test:+ # an existing repo can still have blobs pulled.+ _cached_before_this_load = _repo_is_in_the_hub_cache(request.model_path)+ _cache_footprint_before = _hub_cache_footprint(request.model_path)+ _lora_base_before_this_load = _lora_base_already_in_the_hub_cache(request.model_path)from core.inference.llama_cpp import LlamaServerNotFoundErrordef _raise_if_scoped_load_cancelled() -> None:@@ -16727,6 +16848,15 @@ async def _load_model_impl(raise HTTPException(status_code = 500, detail = f"Failed to load model: {msg}")finally:gguf_load_stack.close()+ # Runs for a load that failed part way too: the bytes it pulled are cached anyway.+ if _load_fetched_bytes(+ request.model_path, _cached_before_this_load, _cache_footprint_before+ ):+ _note_load_fetched_with_a_request_token(request.model_path, request.hf_token)+ # The LoRA base, unknown until the adapter's config is on disk.+ _note_lora_base_fetched_with_a_request_token(+ request.model_path, request.hf_token, already_cached = _lora_base_before_this_load+ )# Catch-all: an error or cancelled load would otherwise leave the row "loading".api_monitor.fail_open(_load_event, "Load did not complete")@@ -16870,6 +17000,9 @@ async def validate_model(Checks that ModelConfig.from_identifier() can resolve model_path, but doesNOT load model weights into GPU memory."""+ # What goes out is the string that came in, not the path this request resolved it to.+ from hub.utils.host_paths import restore_inventory_handles+if account_access.managed_account():request = request.model_copy(update = {"hf_token": account_access.account_hf_token(request.hf_token)}@@ -17205,45 +17338,49 @@ async def validate_model(except Exception as e:logger.debug("Header probe failed for %s: %s", model_log_label, e)- return ValidateModelResponse(- valid = True,- message = "Model identifier is valid.",- identifier = model_log_label if native_grant_backed else config.identifier,- resident = await asyncio.to_thread(- _validated_target_is_resident,- request,- model_identifier = model_identifier,- config = config,+ return restore_inventory_handles(+ ValidateModelResponse(+ valid = True,+ message = "Model identifier is valid.",+ identifier = model_log_label if native_grant_backed else config.identifier,+ resident = await asyncio.to_thread(+ _validated_target_is_resident,+ request,+ model_identifier = model_identifier,+ config = config,+ is_gguf = is_gguf,+ native_grant_backed = native_grant_backed,+ ),+ display_name = model_log_label+ if native_grant_backed+ else getattr(config, "display_name", config.identifier),is_gguf = is_gguf,- native_grant_backed = native_grant_backed,- ),- display_name = model_log_label- if native_grant_backed- else getattr(config, "display_name", config.identifier),- is_gguf = is_gguf,- is_diffusion = is_gguf and placement.diffusion_kind is True,- # An unavailable header is inconclusive, not proof of an ordinary GGUF.- diffusion_unknown = is_gguf and placement.diffusion_kind is None,- is_lora = getattr(config, "is_lora", False),- is_vision = getattr(config, "is_vision", False),- requires_trust_remote_code = requires_trust_remote_code,- requires_security_review = requires_security_review,- context_length = context_length,- layer_count = layer_count,- moe_layer_count = moe_layer_count,- chat_template = chat_template,- requires_transformers_upgrade = transformers_upgrade is not None,- transformers_upgrade = transformers_upgrade,+ is_diffusion = is_gguf and placement.diffusion_kind is True,+ # An unavailable header is inconclusive, not proof of an ordinary GGUF.+ diffusion_unknown = is_gguf and placement.diffusion_kind is None,+ is_lora = getattr(config, "is_lora", False),+ is_vision = getattr(config, "is_vision", False),+ requires_trust_remote_code = requires_trust_remote_code,+ requires_security_review = requires_security_review,+ context_length = context_length,+ layer_count = layer_count,+ moe_layer_count = moe_layer_count,+ chat_template = chat_template,+ requires_transformers_upgrade = transformers_upgrade is not None,+ transformers_upgrade = transformers_upgrade,+ ))- except HTTPException:- raise+ except HTTPException as http_error:+ # The `model_log_label` in the detail is the RESOLVED path.+ raise _handle_restored_http_exception(http_error) from http_errorexcept LlamaServerNotFoundError as e:# Missing GGUF runtime: 400 with the install message, not a generic "Invalid model".logger.warning("GGUF runtime missing while validating '%s': %s", request.model_path, e)- raise HTTPException(status_code = 400, detail = str(e))+ raise HTTPException(status_code = 400, detail = restore_inventory_handles(str(e)))except Exception as e:- redacted_msg = redact_native_paths(str(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 is_hf_authentication_error(e):raise HTTPException(status_code = 400,@@ -17501,8 +17638,10 @@ async def check_transformers_upgrade_route(install_breaks_exact_resume = await asyncio.to_thread(_install_breaks_exact_resume, request.resume_run_id)+ from hub.utils.host_paths import restore_inventory_handles+return TransformersUpgradeCheckResponse(- model_name = model_name,+ model_name = restore_inventory_handles(model_name),requires_transformers_upgrade = transformers_upgrade is not None,transformers_upgrade = transformers_upgrade,# Already booleans: False, or the preflight's own bool result. Re-wrapping the@@ -18737,7 +18876,28 @@ async def get_llama_flags(@router.get("/status", response_model = InferenceStatusResponse)-async def get_status(current_subject: str = Depends(get_current_subject)):+async def inference_status(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):+ """`GET /api/inference/status`, redacted the way the image and video status routes are.++ A chat or GGUF row loaded from an inventory reference RETAINS the resolved absolute path+ as the resident model identity, and it comes back out here through `active_model`,+ `model_identifier` and `loaded`. This route answers long after the request that resolved+ the reference has ended, so there is no handle in the request context to put back and+ `restore_inventory_handles` cannot help: the redactor hands out the same opaque reference+ the caller sent instead. Without it, an API-key caller recovered the path the inventory+ redaction exists to hide by loading the row and then polling status.+ """+ from hub.utils.host_paths import redact_host_paths+ return redact_host_paths(+ await get_status(current_subject = current_subject),+ via_api_key = via_api_key,+ )+++async def get_status(current_subject: str):"""Get current inference backend status.Reports whichever backend (Unsloth or llama-server) is active.@@ -38429,9 +38589,29 @@ def _assert_native_precision_unset(@studio_router.post("/images/load", response_model = DiffusionStatusResponse)@account_access.gpu_busy_routeasync def load_diffusion_model(- request: DiffusionLoadRequest, current_subject: str = Depends(get_current_subject)+ request: DiffusionLoadRequest,+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- return await load_diffusion_model_gated(request, current_subject, user_initiated = True)+ # The status describes whatever is resident, which on a second load is the PREVIOUS model,+ # resolved by an earlier request this context has no handle for. In the route, not the gated+ # body, whose internal callers serve no API-key request.+ from hub.utils.host_paths import (+ raised_inventory_detail,+ redact_host_paths,+ restore_inventory_handles,+ )+ try:+ loaded = await load_diffusion_model_gated(request, current_subject, user_initiated = True)+ except HTTPException as exc:+ # A load that RAISES skips both wrappers, and the inner loader redacted native paths+ # only, which do not know inventory handles.+ exc.detail = raised_inventory_detail(exc.detail, via_api_key = via_api_key)+ raise+ return redact_host_paths(+ restore_inventory_handles(loaded),+ via_api_key = via_api_key,+ )async def load_diffusion_model_gated(@@ -38457,6 +38637,14 @@ async def load_diffusion_model_gated(request = request.model_copy(update = {"hf_token": account_access.account_hf_token(request.hf_token)})+ # Tested at ENTRY because `begin_load` returns before the worker moves a byte, so the only+ # fact available is that a repo ALREADY cached is not one this load will fetch. The WRITE is+ # deferred to the launch below, since the validation in between 400s without moving a byte.+ _media_repos_to_record = [+ ref+ for ref in (request.model_path, request.base_repo)+ if ref and _repo_is_in_the_hub_cache(ref) is not True+ ]from core.inference.diffusion import (get_diffusion_backend,resolve_local_single_file,@@ -38623,6 +38811,11 @@ async def load_diffusion_model_gated()def _start_engine_load():+ # Recorded HERE for the reason the video route gives: inside the admitted callback,+ # past the busy guard, the arbiter and the retirement check, and still before the+ # worker is handed the credential.+ for _ref in _media_repos_to_record:+ _note_load_fetched_with_a_request_token(_ref, request.hf_token)# Kicks the slow load onto a background thread and returns at once (the client polls images/load-progress).return engine.begin_load(request.model_path,@@ -39231,7 +39424,10 @@ async def clear_gallery_transcripts(current_subject: str = Depends(get_current_s@studio_router.post("/images/unload", response_model = DiffusionStatusResponse)@account_access.gpu_busy_route-async def unload_diffusion_model(current_subject: str = Depends(get_current_subject)):+async def unload_diffusion_model(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):account_access.require_resident_control("diffusion")from core.inference.diffusion_engine_router import annotate_status, get_active_diffusion_enginefrom core.inference.gpu_arbiter import release_if, DIFFUSION@@ -39252,19 +39448,30 @@ async def unload_diffusion_model(current_subject: str = Depends(get_current_subjDIFFUSION,lambda: not engine.loading_repo_ids() and not engine.is_loaded,)- return DiffusionStatusResponse(**annotate_status(status_dict))+ # An unload answers with the state it left behind, which still names the model it dropped.+ from hub.utils.host_paths import redact_host_paths, restore_inventory_handles++ return redact_host_paths(+ restore_inventory_handles(DiffusionStatusResponse(**annotate_status(status_dict))),+ via_api_key = via_api_key,+ )@studio_router.get("/images/status", response_model = DiffusionStatusResponse)-async def diffusion_status(current_subject: str = Depends(get_current_subject)):+async def diffusion_status(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):if account_access.resident_hidden("diffusion"):return account_access.hidden_resident_response()from core.inference.diffusion_engine_router import active_status+ from hub.utils.host_paths import redact_host_pathsstatus_dict = active_status()if account_access.resident_hidden("diffusion", status_dict.get("repo_id")):return account_access.hidden_resident_response()- return DiffusionStatusResponse(**status_dict)+ # Answered long after the resolving request ended, so no handle is in context.+ return redact_host_paths(DiffusionStatusResponse(**status_dict), via_api_key = via_api_key)@studio_router.get("/images/info", response_model = DiffusionInferenceInfoResponse)@@ -39278,7 +39485,10 @@ async def diffusion_inference_info(current_subject: str = Depends(get_current_su@studio_router.get("/images/load-progress", response_model = DiffusionLoadProgressResponse)-async def diffusion_load_progress(current_subject: str = Depends(get_current_subject)):+async def diffusion_load_progress(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):if account_access.resident_hidden("diffusion"):return account_access.hidden_resident_response()from core.inference.diffusion_engine_router import get_active_diffusion_engine@@ -39289,7 +39499,9 @@ async def diffusion_load_progress(current_subject: str = Depends(get_current_subreturn account_access.hidden_resident_response()progress = get_active_diffusion_engine().load_progress()log_media_load_progress("image", progress.get("phase"), progress.get("fraction"))- return DiffusionLoadProgressResponse(**progress)+ from hub.utils.host_paths import redact_load_progress++ return DiffusionLoadProgressResponse(**redact_load_progress(progress, via_api_key = via_api_key))@studio_router.get("/images/generate-progress", response_model = DiffusionGenerateProgressResponse)diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.pyindex 37f86499bc6..b67837b78d5 100644--- a/studio/backend/routes/models.py+++ b/studio/backend/routes/models.py@@ -179,7 +179,11 @@ backend_path = Path(__file__).parent.parent.parentif str(backend_path) not in sys.path:sys.path.insert(0, str(backend_path))-from auth.authentication import allow_ambient_hf_token, get_current_subject+from auth.authentication import (+ allow_ambient_hf_token,+ authenticated_via_api_key,+ get_current_subject,+)from hub.dependencies import get_hf_token, get_request_hf_tokenfrom hub.utils.hf_tokens import (HfTokenArg,@@ -189,10 +193,26 @@ from hub.utils.hf_tokens import (is_anonymous,normalize_token,)+from hub.utils.host_paths import (+ redact_host_paths,+ redact_inventory_host_paths,+ resolve_host_path_reference,+ scrub_paths,+)from utils.utils import anonymous_and_offline-_UNAUTHORIZED_OFFLINE = "This request cannot be authorized without network access."+# Says both halves, or operators go looking for a credential problem that is not there.+_UNAUTHORIZED_OFFLINE = (+ "This request cannot be authorized without network access, and this repository is not in "+ "the local cache."+)++# "unauthorized" alone reads as a broken credential, which a repo refusing this caller is not.+_UNAUTHORIZED_CACHED_MODEL = (+ "This model is cached on this host, but this repository does not authorize this caller to "+ "read it."+)def _resolve_hub_token(header_token: HfTokenArg, query_token: Optional[str]) -> HfTokenArg:@@ -1141,8 +1161,13 @@ async def list_local_models(default = "./models", description = "Directory to scan for local model folders"),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- """List local model candidates from the models dir, HF caches, LM Studio, Hermes, Ollama."""+ """List local model candidates from the models dir, HF caches, LM Studio, Hermes, Ollama.++ Redacted as ``/api/hub/local`` is: this router mirrors it over the same scan roots, so+ leaving it alone recovers the layout that route hides.+ """# Resolve all scan directories up front.sources = _compat_local_inventory_sources()hf_cache_dir = sources.hf_cache_dir@@ -1180,12 +1205,15 @@ async def list_local_models(models = await _shared_compat_local_inventory_scan(models_root, sources)if account_access.managed_account():models = await asyncio.to_thread(account_access.filter_model_rows, models)- return LocalModelListResponse(- models_dir = str(models_root),- hf_cache_dir = str(hf_cache_dir),- lmstudio_dirs = [str(d) for d in lm_dirs],- hermes_dirs = [str(d) for d in sources.hermes_dirs],- models = models,+ return redact_inventory_host_paths(+ LocalModelListResponse(+ models_dir = str(models_root),+ hf_cache_dir = str(hf_cache_dir),+ lmstudio_dirs = [str(d) for d in lm_dirs],+ hermes_dirs = [str(d) for d in sources.hermes_dirs],+ models = models,+ ),+ via_api_key = via_api_key,)except Exception as e:raise log_and_http_error(@@ -1198,21 +1226,32 @@ async def list_local_models(@router.get("/scan-folders")-async def get_scan_folders(current_subject: str = Depends(get_current_subject)):- """List all registered custom model scan folders."""+async def get_scan_folders(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):+ """List all registered custom model scan folders. Redacted like ``/api/hub/scan-folders``."""from storage.studio_db import list_scan_foldersfolders = list_scan_folders()# Opening the dialog is how a fixed folder clears, so recheck the bad ones.await asyncio.to_thread(refresh_failed_scan_folders, folders)- return {"folders": annotate_scan_folders(folders)}+ return redact_inventory_host_paths(+ {"folders": annotate_scan_folders(folders)}, via_api_key = via_api_key+ )@router.post("/scan-folders", response_model = ScanFolderInfo, status_code = 201)async def add_scan_folder_endpoint(- body: AddScanFolderRequest, current_subject: str = Depends(get_current_subject)+ body: AddScanFolderRequest,+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- """Register a new directory to scan for local models."""+ """Register a new directory to scan for local models. Redacted like the GET above.++ The listings hide the normalized absolute path; submitting ``.`` here and reading the answer+ recovered the server's working directory.+ """if account_access.managed_account():body = body.model_copy(update = {"path": account_access.private_directory(body.path, "")})from storage.studio_db import add_scan_folder_with_status@@ -1228,7 +1267,7 @@ async def add_scan_folder_endpoint(from core.inference.local_model_resolver import invalidate_index, warm_index_soonawait asyncio.to_thread(invalidate_index)warm_index_soon()- return folder+ return redact_inventory_host_paths(folder, via_api_key = via_api_key)@router.delete("/scan-folders/{folder_id}")@@ -2204,8 +2243,16 @@ async def get_model_config(header_hf_token: Optional[str] = Depends(get_hf_token),allow_ambient_token: bool = Depends(allow_ambient_hf_token),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):"""Get configuration for a specific model (wraps load_model_defaults)."""+ # An API-key caller is shown a filesystem-backed row under an opaque `ref:` handle and hands+ # it back here, where it would otherwise read as a Hugging Face id.+ from models.inference import resolve_inventory_handle++ model_name = resolve_inventory_handle(model_name)+ if local_path:+ local_path = resolve_inventory_handle(local_path)if local_path:if account_access.managed_account():await asyncio.to_thread(account_access.require_model_access, local_path)@@ -2233,7 +2280,9 @@ async def get_model_config():# Inside the context, not before: the guard forces offline itself when the hub# is unreachable, and every probe below then resolves from disk.- if anonymous_and_offline(hf_token) and not is_local_path(model_name):+ if not is_local_path(model_name) and anonymous_and_offline(+ hf_token, repo_id = canonical_model_repo_id(model_name)+ ):raise HTTPException(status_code = 404, detail = _UNAUTHORIZED_OFFLINE)if not is_local_path(model_name):resolved = resolve_cached_repo_id_case(model_name)@@ -2334,7 +2383,15 @@ async def get_model_config(try:# Off the loop: the guard blocks on DNS + HEAD + TCP, stalling every other request.- return await asyncio.to_thread(_resolve, model_name)+ # Restore puts back the handle the CALLER sent; redaction covers a second path they+ # never named (a LoRA's `base_model_name_or_path`). Referencing `echo`, the caller's own+ # identifier, would make this route an oracle confirming their other references.+ from hub.utils.host_paths import redact_host_paths, restore_inventory_handles+ return redact_host_paths(+ restore_inventory_handles(await asyncio.to_thread(_resolve, model_name)),+ via_api_key = via_api_key,+ echo = (model_name,),+ )except HTTPException:raise@@ -2381,6 +2438,17 @@ async def scan_model_remote_code(POST (not GET) so the ``hf_token`` for gated repos travels in the body andnever lands in a URL, browser history, or access log."""+ # Before the access checks, so they run on the resolved path.+ from models.inference import resolve_inventory_handle++ model_name = resolve_inventory_handle(model_name)+ model_local_path = resolve_inventory_handle(model_local_path) if model_local_path else None+ model_snapshot_path = (+ resolve_inventory_handle(model_snapshot_path) if model_snapshot_path else None+ )+ model_snapshot_repo_id = (+ resolve_inventory_handle(model_snapshot_repo_id) if model_snapshot_repo_id else None+ )if account_access.managed_account():for ref in (model_name, model_local_path, model_snapshot_path, model_snapshot_repo_id):if isinstance(ref, str) and ref:@@ -2391,7 +2459,9 @@ async def scan_model_remote_code(hf_token = hf_token_arg(hf_token, allow_ambient_token = allow_ambient_token)# Offline the scanner's hf_hub_download calls resolve config.json and the repo's# Python out of the cache, and the response carries source snippets.- if anonymous_and_offline(hf_token) and not is_local_path(model_name):+ if not is_local_path(model_name) and anonymous_and_offline(+ hf_token, repo_id = canonical_model_repo_id(model_name)+ ):raise HTTPException(status_code = 404, detail = _UNAUTHORIZED_OFFLINE)try:from utils.security import (@@ -2443,7 +2513,7 @@ async def scan_model_remote_code():raise HTTPException(status_code = 404,- detail = "This model is not available to an unauthorized caller.",+ detail = _UNAUTHORIZED_CACHED_MODEL,)scan_target = model_nameexact_snapshot_path = (@@ -2533,7 +2603,7 @@ async def scan_model_remote_code():raise HTTPException(status_code = 404,- detail = "This model is not available to an unauthorized caller.",+ detail = _UNAUTHORIZED_CACHED_MODEL,)if _target not in consent_load_subdirs:security_targets.append(_target)@@ -2585,7 +2655,7 @@ async def scan_model_remote_code():raise HTTPException(status_code = 404,- detail = "This model is not available to an unauthorized caller.",+ detail = _UNAUTHORIZED_CACHED_MODEL,)external_refs.append(_ext)_mark_scan_created(_ext)@@ -2633,7 +2703,10 @@ async def scan_model_remote_code(payload["approvable"] = Falsepayload["requires_trust_remote_code"] = Truepayload["error_kind"] = "malware_blocked"- return payload+ # The findings quote paths inside the model directory.+ from hub.utils.host_paths import restore_inventory_handles++ return restore_inventory_handles(payload)except HTTPException:raiseexcept Exception as e:@@ -4400,8 +4473,13 @@ async def get_gguf_variants(hf_token: Optional[str] = Query(None, description = "HuggingFace token for private repos"),hf_token_header: HfTokenArg = Depends(get_request_hf_token),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):"""List GGUF quantization variants for a HF repo or local directory."""+ # Resolved before the access check, not after: a handle matches no allowlist entry, and+ # it is the only name an API-key caller has for a local GGUF (see the /hub twin).+ repo_id = resolve_host_path_reference(repo_id) or repo_id+ local_path = resolve_host_path_reference(local_path) or local_pathif account_access.managed_account():await asyncio.to_thread(account_access.require_model_access, repo_id)try:@@ -4429,42 +4507,47 @@ async def get_gguf_variants(context_model = Nonelocal = context_model is not None and is_local_path(context_model)- return GgufVariantsResponse(- repo_id = response.repo_id,- variants = [- GgufVariantDetail(- filename = v.filename,- quant = v.quant,- # A path-qualified key is not a label a picker can show; without this- # the row reads as its whole relative path.- display_label = getattr(v, "display_label", None),- size_bytes = v.size_bytes,- shard_count = int(getattr(v, "shard_count", 0) or 0),- download_size_bytes = int(- getattr(v, "download_size_bytes", v.size_bytes) or v.size_bytes- ),- pending_drafter_filename = getattr(v, "pending_drafter_filename", None),- pending_drafter_size_bytes = int(- getattr(v, "pending_drafter_size_bytes", 0) or 0- ),- downloaded = bool(v.downloaded),- update_available = bool(getattr(v, "update_available", False)),- partial = bool(getattr(v, "partial", False)),- cleanable = bool(getattr(v, "cleanable", False)),- )- for v in response.variants- ],- has_vision = response.has_vision,- default_variant = response.default_variant,- context_length = (- await _read_native_context_length_bounded(context_model, local)- if context_model is not None- else None+ # See the /hub twin: the identifier is resolved on the way in, so it has to be+ # referenced again on the way out.+ return redact_host_paths(+ GgufVariantsResponse(+ repo_id = response.repo_id,+ variants = [+ GgufVariantDetail(+ filename = v.filename,+ quant = v.quant,+ # A path-qualified key is not a label a picker can show; without this+ # the row reads as its whole relative path.+ display_label = getattr(v, "display_label", None),+ size_bytes = v.size_bytes,+ shard_count = int(getattr(v, "shard_count", 0) or 0),+ download_size_bytes = int(+ getattr(v, "download_size_bytes", v.size_bytes) or v.size_bytes+ ),+ pending_drafter_filename = getattr(v, "pending_drafter_filename", None),+ pending_drafter_size_bytes = int(+ getattr(v, "pending_drafter_size_bytes", 0) or 0+ ),+ downloaded = bool(v.downloaded),+ update_available = bool(getattr(v, "update_available", False)),+ partial = bool(getattr(v, "partial", False)),+ cleanable = bool(getattr(v, "cleanable", False)),+ )+ for v in response.variants+ ],+ has_vision = response.has_vision,+ default_variant = response.default_variant,+ context_length = (+ await _read_native_context_length_bounded(context_model, local)+ if context_model is not None+ else None+ ),+ resolved_locally = bool(getattr(response, "resolved_locally", False)),+ dependencies_resolved = bool(getattr(response, "dependencies_resolved", False)),+ loadable_variants = getattr(response, "loadable_variants", None),+ loadable = getattr(response, "loadable", None),),- resolved_locally = bool(getattr(response, "resolved_locally", False)),- dependencies_resolved = bool(getattr(response, "dependencies_resolved", False)),- loadable_variants = getattr(response, "loadable_variants", None),- loadable = getattr(response, "loadable", None),+ via_api_key = via_api_key,)except HTTPException:raise@@ -4483,14 +4566,18 @@ async def get_gguf_download_progress(expected_bytes: int = Query(0, description = "Expected total download size in bytes"),hf_token: HfTokenArg = Depends(get_request_hf_token),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):"""Compatibility route backed by the shared multi-cache progress service."""from hub.services.models import downloads- return await downloads.get_gguf_download_progress_response(- repo_id,- variant = variant,- expected_bytes = expected_bytes,- hf_token = hf_token,+ return redact_host_paths(+ await downloads.get_gguf_download_progress_response(+ repo_id,+ variant = variant,+ expected_bytes = expected_bytes,+ hf_token = hf_token,+ ),+ via_api_key = via_api_key,)@@ -4507,10 +4594,15 @@ async def get_download_progress(repo_id: str = Query(..., description = "HuggingFace repo ID"),hf_token: HfTokenArg = Depends(get_request_hf_token),current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- """Compatibility route backed by the shared multi-cache progress service."""+ """Compatibility route backed by the shared multi-cache progress service. The payload names+ the cache directory it measured, so it takes the caller class like its ``/api/hub`` twin."""from hub.services.models import downloads- return await downloads.get_download_progress_response(repo_id, hf_token = hf_token)+ return redact_host_paths(+ await downloads.get_download_progress_response(repo_id, hf_token = hf_token),+ via_api_key = via_api_key,+ )def _repo_in_any_hf_cache(model_name: str) -> bool:@@ -4856,13 +4948,18 @@ def _preferred_gguf_copy(@router.get("/cached-gguf")-async def list_cached_gguf(current_subject: str = Depends(get_current_subject)):+async def list_cached_gguf(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):"""List GGUF repos downloaded to HF cache, legacy Unsloth cache, and HF default cache."""try:# Off the loop: the filter can probe the Hub per ungranted repo.- return {"cached": await asyncio.to_thread(cached_gguf_rows)}+ return redact_host_paths(+ {"cached": await asyncio.to_thread(cached_gguf_rows)}, via_api_key = via_api_key+ )except Exception as e:- logger.error(f"Error listing cached GGUF repos: {e}", exc_info = True)+ logger.error("Error listing cached GGUF repos: %s", scrub_paths(e), exc_info = True)return {"cached": []}@@ -4963,13 +5060,16 @@ def _cached_repo_partial(async def list_cached_models(current_subject: str = Depends(get_current_subject),hf_token: HfTokenArg = Depends(get_request_hf_token),+ via_api_key: bool = Depends(authenticated_via_api_key),):"""List non-GGUF model repos downloaded to HF cache, legacy Unsloth cache, and HF default cache."""try:# Off the loop: the filter can probe the Hub per ungranted repo.- return {"cached": await asyncio.to_thread(cached_model_rows)}+ return redact_host_paths(+ {"cached": await asyncio.to_thread(cached_model_rows)}, via_api_key = via_api_key+ )except Exception as e:- logger.error(f"Error listing cached models: {e}", exc_info = True)+ logger.error("Error listing cached models: %s", scrub_paths(e), exc_info = True)return {"cached": []}@@ -5237,7 +5337,11 @@ async def delete_cached_model(account_access.require_installation_owner()from hub.services.models import deletion- return await deletion.delete_cached_model_response(repo_id, variant, hf_token, cache_path)+ # The reference is the only identifier an API-key caller has for one copy; omitting it acts+ # on the active root instead.+ return await deletion.delete_cached_model_response(+ repo_id, variant, hf_token, resolve_host_path_reference(cache_path) or cache_path+ )def _resolve_cached_model_path(repo_id: str, variant: Optional[str]) -> Path:diff --git a/studio/backend/routes/training.py b/studio/backend/routes/training.pyindex 6dcb39e493b..167804cde40 100644--- a/studio/backend/routes/training.py+++ b/studio/backend/routes/training.py@@ -86,6 +86,7 @@ except ImportError:from utils.paths import is_local_path, normalize_path, resolve_dataset_pathfrom auth.authentication import authenticated_via_api_key, get_current_subject+from hub.utils.host_paths import redact_host_pathsfrom hub.utils.hf_tokens import HfTokenArg, cached_read_refused, hf_token_argfrom utils.utils import (@@ -2301,9 +2302,16 @@ def _build_training_status(@router.get("/status")-async def get_training_status(current_subject: str = Depends(get_current_subject)):+async def get_training_status(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):"""Get the current training status.++ Redacted for an API-key caller like every other route that can name a host path: the+ worker's own progress line quotes the model it is loading, and for a local model that is+ an absolute path, which would hand back what the inventory took the trouble to hide."""try:backend = get_training_backend()@@ -2315,7 +2323,7 @@ async def get_training_status(current_subject: str = Depends(get_current_subjectcontinuestatus = _build_training_status(backend, identity, is_active)if _training_status_identity(backend) == identity:- return status+ return redact_host_paths(status, via_api_key = via_api_key)raise HTTPException(status_code = 409, detail = "Training state changed during status read")except HTTPException:raisediff --git a/studio/backend/routes/training_history.py b/studio/backend/routes/training_history.pyindex 3327030a909..02e04a3eff8 100644--- a/studio/backend/routes/training_history.py+++ b/studio/backend/routes/training_history.py@@ -13,7 +13,11 @@ from typing import Literal, Optional, Unionfrom fastapi import APIRouter, Depends, HTTPException, Queryfrom loggers import get_logger-from auth.authentication import authenticated_without_credential, get_current_subject+from auth.authentication import (+ authenticated_via_api_key,+ authenticated_without_credential,+ get_current_subject,+)from core.training.resume import artifacts_present, can_resume_runfrom utils.training_runs import drop_non_finitefrom models import (@@ -271,6 +275,7 @@ async def list_training_runs(offset: int = Query(0, ge = 0),current_subject: str = Depends(get_current_subject),no_credential: bool = Depends(authenticated_without_credential),+ via_api_key: bool = Depends(authenticated_via_api_key),):"""List training runs, newest first."""result = list_runs(limit = limit, offset = offset)@@ -280,9 +285,13 @@ async def list_training_runs(result["runs"],sharing_on,)- return TrainingRunListResponse(- runs = runs,- total = result["total"],+ # A run persists the resolved path as `model_name`, and this route answers days later with+ # no handle left in context.+ from hub.utils.host_paths import redact_host_paths++ return redact_host_paths(+ TrainingRunListResponse(runs = runs, total = result["total"]),+ via_api_key = via_api_key,)@@ -291,6 +300,7 @@ async def get_training_run_detail(run_id: str,current_subject: str = Depends(get_current_subject),no_credential: bool = Depends(authenticated_without_credential),+ via_api_key: bool = Depends(authenticated_via_api_key),):"""Get a single training run with full config and metrics."""run = get_run(run_id)@@ -311,10 +321,17 @@ async def get_training_run_detail(run,get_preview_sharing_enabled() and not no_credential,)- return TrainingRunDetailResponse(- run = summary,- config = config,- metrics = TrainingRunMetrics(**metrics_data),+ # The same persisted path, in the summary as `model_name` and again inside `config_json`,+ # so the WHOLE response goes through.+ from hub.utils.host_paths import redact_host_paths++ return redact_host_paths(+ TrainingRunDetailResponse(+ run = summary,+ config = config,+ metrics = TrainingRunMetrics(**metrics_data),+ ),+ via_api_key = via_api_key,)@@ -324,6 +341,7 @@ async def update_training_run(payload: TrainingRunUpdateRequest,current_subject: str = Depends(get_current_subject),no_credential: bool = Depends(authenticated_without_credential),+ via_api_key: bool = Depends(authenticated_via_api_key),):"""Update mutable fields on a training run (currently only display_name)."""run = get_run(run_id)@@ -339,11 +357,15 @@ async def update_training_run(refreshed = get_run(run_id)if refreshed is None:raise HTTPException(status_code = 404, detail = f"Run {run_id} not found")- return await asyncio.to_thread(+ summary = await asyncio.to_thread(_summary_from_row,refreshed,get_preview_sharing_enabled() and not no_credential,)+ # Renaming answers with the run, so a no-op PATCH would recover the path.+ from hub.utils.host_paths import redact_host_paths++ return redact_host_paths(summary, via_api_key = via_api_key)@router.delete("/runs/{run_id}", response_model = TrainingRunDeleteResponse)diff --git a/studio/backend/routes/video.py b/studio/backend/routes/video.pyindex fefa454d5a3..535afbf7569 100644--- a/studio/backend/routes/video.py+++ b/studio/backend/routes/video.py@@ -32,7 +32,11 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Request, Responsefrom pydantic import ValidationErrorfrom starlette.datastructures import UploadFile-from auth.authentication import get_current_subject, request_admitted_without_credential+from auth.authentication import (+ authenticated_via_api_key,+ get_current_subject,+ request_admitted_without_credential,+)from core.inference.model_ids import public_model_idfrom hub.dependencies import get_hf_tokenfrom hub.services.models import account_access@@ -228,9 +232,29 @@ async def video_download_plan(@router.post("/video/load", response_model = VideoStatusResponse)@account_access.gpu_busy_routeasync def load_video_model(- request: VideoLoadRequest, current_subject: str = Depends(get_current_subject)+ request: VideoLoadRequest,+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),):- return await load_video_model_gated(request, current_subject, user_initiated = True)+ # The status describes whatever is resident, which on a second load is the PREVIOUS model,+ # resolved by an earlier request this context has no handle for. In the route rather than the+ # gated body, whose internal callers serve no API-key request.+ from hub.utils.host_paths import (+ raised_inventory_detail,+ redact_host_paths,+ restore_inventory_handles,+ )+ try:+ loaded = await load_video_model_gated(request, current_subject, user_initiated = True)+ except HTTPException as exc:+ # A load that RAISES skips both wrappers below, and the inner loader redacted only+ # native paths, which do not know inventory handles.+ exc.detail = raised_inventory_detail(exc.detail, via_api_key = via_api_key)+ raise+ return redact_host_paths(+ restore_inventory_handles(loaded),+ via_api_key = via_api_key,+ )async def load_video_model_gated(@@ -254,6 +278,19 @@ async def load_video_model_gated(request = request.model_copy(update = {"hf_token": account_access.account_hf_token(request.hf_token)})+ # Same as the image load: tested at entry, because `begin_load` returns before the worker+ # moves a byte, and written at the launch below, because the validation in between 400s+ # without starting one and the record would be permanent.+ from routes.inference import (+ _note_load_fetched_with_a_request_token,+ _repo_is_in_the_hub_cache,+ )++ _media_repos_to_record = [+ ref+ for ref in (request.model_path, request.base_repo)+ if ref and _repo_is_in_the_hub_cache(ref) is not True+ ]from core.inference.diffusion import resolve_local_single_filefrom core.inference.diffusion_device import (resolve_diffusion_device_target,@@ -334,6 +371,12 @@ async def load_video_model_gated(device = await asyncio.to_thread(lambda: resolve_diffusion_device_target().device)def _start_load():+ # Recorded HERE, inside the admitted callback: every cheap refusal (the busy guard+ # above, the arbiter, the retirement check inside admit_media_load) has already let+ # this load through, and the worker below has not been handed the credential yet. A+ # record left by a load that was refused withholds a repo nobody fetched.+ for _ref in _media_repos_to_record:+ _note_load_fetched_with_a_request_token(_ref, request.hf_token)# Kicks the (slow) load onto a background thread and returns at once; begin_load itself validates# network-free.return backend.begin_load(@@ -405,7 +448,10 @@ async def load_video_model_gated(@router.get("/video/load-progress", response_model = VideoLoadProgressResponse)-async def video_load_progress(current_subject: str = Depends(get_current_subject)):+async def video_load_progress(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):if account_access.resident_hidden("video"):return account_access.hidden_resident_response()from core.inference.video import get_video_backend@@ -417,7 +463,9 @@ async def video_load_progress(current_subject: str = Depends(get_current_subjectprogress = get_video_backend().load_progress()fraction = byte_fraction(progress.get("downloaded_bytes"), progress.get("expected_bytes"))log_media_load_progress("video", progress.get("phase"), fraction)- return VideoLoadProgressResponse(**progress)+ from hub.utils.host_paths import redact_load_progress++ return VideoLoadProgressResponse(**redact_load_progress(progress, via_api_key = via_api_key))_generation_account: Optional[str] = None@@ -674,20 +722,29 @@ async def cancel_video_generation(current_subject: str = Depends(get_current_sub@router.get("/video/status", response_model = VideoStatusResponse)-async def video_status(current_subject: str = Depends(get_current_subject)):+async def video_status(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):if account_access.resident_hidden("video"):return account_access.hidden_resident_response()from core.inference.video import get_video_backend+ from hub.utils.host_paths import redact_host_pathsstatus_dict = get_video_backend().status()if account_access.resident_hidden("video", status_dict.get("repo_id")):return account_access.hidden_resident_response()- return VideoStatusResponse(**status_dict)+ # This route answers long after the request that resolved the reference ended, so there is+ # no handle in context to put back.+ return redact_host_paths(VideoStatusResponse(**status_dict), via_api_key = via_api_key)@router.post("/video/unload", response_model = VideoStatusResponse)@account_access.gpu_busy_route-async def unload_video_model(current_subject: str = Depends(get_current_subject)):+async def unload_video_model(+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):account_access.require_resident_control("video")from core.inference.gpu_arbiter import VIDEO, release_iffrom core.inference.video import get_video_backend@@ -706,7 +763,13 @@ async def unload_video_model(current_subject: str = Depends(get_current_subject)VIDEO,lambda: not backend.loading_repo_ids() and not backend.status()["loaded"],)- return VideoStatusResponse(**status_dict)+ # An unload answers with the state it left behind, which still names the model it dropped.+ from hub.utils.host_paths import redact_host_paths, restore_inventory_handles++ return redact_host_paths(+ restore_inventory_handles(VideoStatusResponse(**status_dict)),+ via_api_key = via_api_key,+ )@router.get("/video/gallery", response_model = VideoGalleryListResponse)diff --git a/studio/backend/storage/credential_secrets.py b/studio/backend/storage/credential_secrets.pyindex fa0e43d3500..d80efcf0c34 100644--- a/studio/backend/storage/credential_secrets.py+++ b/studio/backend/storage/credential_secrets.py@@ -200,6 +200,25 @@ def has_secret(credential_kind: str, scope_id: str) -> bool:return get_secret(credential_kind, scope_id) is not None+def secret_row_exists(credential_kind: str, scope_id: str) -> bool:+ """Whether a credential is STORED, readable or not. `get_secret` and `has_secret` answer+ None for an absent row AND an undecryptable one, which callers that AUTHORIZE on the absence+ of a credential must tell apart."""+ conn = get_connection()+ try:+ row = conn.execute(+ """+ SELECT 1+ FROM credential_secrets+ WHERE credential_kind = ? AND scope_id = ?+ """,+ (credential_kind, scope_id),+ ).fetchone()+ finally:+ conn.close()+ return row is not None++def delete_secret(credential_kind: str,scope_id: str,@@ -226,15 +245,41 @@ def get_hf_token() -> Optional[str]:return get_secret(HF_TOKEN_KIND, HF_TOKEN_SCOPE)+def hf_token_row_exists() -> bool:+ """Whether an HF token is saved, readable or not. See `secret_row_exists`."""+ return secret_row_exists(HF_TOKEN_KIND, HF_TOKEN_SCOPE)+++def _note_a_credential_this_host_held() -> None:+ """Record the identity of the token being replaced or removed, while it is still here.++ The ledger of credentials this host has EVER held is what stops a tokenless caller+ inheriting the downloads of one that has since been removed, and it used to be written only+ where an authorization probe happened to read it. An operator who cleared the token first+ left nothing behind. A row that exists but cannot be decrypted records the sentinel, since+ an unreadable credential is still a credential this host held.+ """+ try:+ from hub.utils.hf_tokens import note_host_credential_identity+ note_host_credential_identity(get_hf_token(), a_credential_was_held = hf_token_row_exists())+ except Exception: # noqa: BLE001 -- bookkeeping must never fail a settings write+ pass++def save_hf_token(token: str) -> None:+ _note_a_credential_this_host_held()upsert_secret(HF_TOKEN_KIND, HF_TOKEN_SCOPE, token)+ _note_a_credential_this_host_held()def save_hf_token_if_absent(token: str) -> bool:- return insert_secret_if_absent(HF_TOKEN_KIND, HF_TOKEN_SCOPE, token)+ inserted = insert_secret_if_absent(HF_TOKEN_KIND, HF_TOKEN_SCOPE, token)+ _note_a_credential_this_host_held()+ return inserteddef delete_hf_token() -> bool:+ _note_a_credential_this_host_held()return delete_secret(HF_TOKEN_KIND, HF_TOKEN_SCOPE)diff --git a/studio/backend/storage/studio_db.py b/studio/backend/storage/studio_db.pyindex 5b5e95ae672..406f25c7002 100644--- a/studio/backend/storage/studio_db.py+++ b/studio/backend/storage/studio_db.py@@ -4475,6 +4475,9 @@ def upsert_app_setting_map_entry(*,fill_absent_fields: bool = False,coupled_fields: tuple[tuple[str, ...], ...] = (),+ keep_first_writer: bool = False,+ ambiguous_field: str | None = None,+ delete_if_entry_equals: dict[str, Any] | None = None,) -> dict[str, Any]:"""Set (or delete, when entry_value is falsy) one sub-entry of a dict-valued app setting,atomically under BEGIN IMMEDIATE so concurrent writers to other sub-entries cannot drop each@@ -4488,7 +4491,18 @@ def upsert_app_setting_map_entry(filling would take a qualifier from this browser and leave the value it qualifies as the serverwrote it: a stored ``gpu_ids`` in one index space, relabelled with the other space's``gpu_index_kind``, points at a different GPU while looking stored. A group any part of which is- held is skipped whole."""+ held is skipped whole.++ ``keep_first_writer`` leaves an entry that already exists as it is, and with+ ``ambiguous_field`` also collapses that one field to None when the stored value differs from+ the incoming one. The comparison happens inside this transaction on purpose: a caller that+ read the map first and decided outside it loses the race it is there to detect, since two+ writers can both read "absent" and then each write its own value, and the last one wins with+ an attribution that is no longer true.++ ``delete_if_entry_equals`` removes the entry only when it is still exactly the one the+ caller wrote, which is how a writer takes back a record for a call that then failed without+ taking back a later writer's."""conn = get_connection()try:conn.execute("BEGIN IMMEDIATE")@@ -4514,9 +4528,23 @@ def upsert_app_setting_map_entry(current[entry_key] = mergedelse:current[entry_key] = entry_value+ elif keep_first_writer and entry_value and entry_key in current:+ stored = current[entry_key]+ if not isinstance(stored, dict) or ambiguous_field is None:+ conn.rollback()+ return current+ if stored.get(ambiguous_field) in (None, entry_value.get(ambiguous_field)):+ conn.rollback()+ return current+ current[entry_key] = {**stored, ambiguous_field: None}elif entry_value:current[entry_key] = entry_valueelse:+ if delete_if_entry_equals is not None and (+ current.get(entry_key) != delete_if_entry_equals+ ):+ conn.rollback()+ return currentcurrent.pop(entry_key, None)now = datetime.now(timezone.utc).isoformat()conn.execute(diff --git a/studio/backend/tests/conftest.py b/studio/backend/tests/conftest.pyindex 47e16e642dc..dd5dce81124 100644--- a/studio/backend/tests/conftest.py+++ b/studio/backend/tests/conftest.py@@ -1115,6 +1115,26 @@ def _process_shutdown_latch_is_clear():_reopen()[email protected](autouse = True)+def _no_leaked_inventory_handles():+ """Start every test with an empty per-request handle table.++ The ContextVar a REQUEST owns is never reset between tests, so one test that resolves a+ handle leaves the table populated for every test after it in the same worker, and a route+ that answers a response MODEL then answers a restored dict instead.+ """+ try:+ from hub.utils import host_paths+ except Exception: # optional deps absent on some CI legs+ yield+ return+ token = host_paths._request_handles.set(None)+ try:+ yield+ finally:+ host_paths._request_handles.reset(token)++@pytest.fixture(autouse = True)def _drop_the_settings_memo_between_tests():"""Stop one test's app settings answering another test's read.diff --git a/studio/backend/tests/multi_account/test_chat_load_status.py b/studio/backend/tests/multi_account/test_chat_load_status.pyindex e8e477a8540..c22f78d4b2b 100644--- a/studio/backend/tests/multi_account/test_chat_load_status.py+++ b/studio/backend/tests/multi_account/test_chat_load_status.py@@ -13,7 +13,7 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "studio/backend"))import pytestfrom fastapi import FastAPI, HTTPExceptionfrom fastapi.testclient import TestClient-from auth.authentication import get_current_subject+from auth.authentication import authenticated_via_api_key, get_current_subjectfrom core.inference import gpu_arbiterfrom hub.services.models import account_access as accessfrom routes import inference@@ -31,6 +31,7 @@ def client_for(account):reset_account(token)app.dependency_overrides[get_current_subject] = subject+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falseapp.include_router(inference.router, prefix = "/api/inference")return TestClient(app)diff --git a/studio/backend/tests/multi_account/test_chat_shared_resident.py b/studio/backend/tests/multi_account/test_chat_shared_resident.pyindex e22e284ff4f..6e54133c97c 100644--- a/studio/backend/tests/multi_account/test_chat_shared_resident.py+++ b/studio/backend/tests/multi_account/test_chat_shared_resident.py@@ -16,7 +16,7 @@ import pytestfrom fastapi import FastAPIfrom fastapi.testclient import TestClientfrom auth import policy-from auth.authentication import get_current_subject+from auth.authentication import authenticated_via_api_key, get_current_subjectfrom core.inference import gpu_arbiterfrom hub.services.models import account_access as accessfrom models.inference import LoadRequest@@ -40,6 +40,7 @@ def client_for(account):reset_account(token)app.dependency_overrides[get_current_subject] = subject+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falseapp.include_router(inference.router, prefix = "/api/inference")return TestClient(app)diff --git a/studio/backend/tests/multi_account/test_chat_unload_ownership.py b/studio/backend/tests/multi_account/test_chat_unload_ownership.pyindex 2635a75b66a..a81019baf1a 100644--- a/studio/backend/tests/multi_account/test_chat_unload_ownership.py+++ b/studio/backend/tests/multi_account/test_chat_unload_ownership.py@@ -14,7 +14,7 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "studio/backend"))import pytestfrom fastapi import FastAPIfrom fastapi.testclient import TestClient-from auth.authentication import get_current_subject+from auth.authentication import authenticated_via_api_key, get_current_subjectfrom core.inference import gpu_arbiter, llama_keepwarmfrom hub.services.models import account_access as accessfrom routes import inference@@ -34,6 +34,7 @@ def client_for(account):reset_account(token)app.dependency_overrides[get_current_subject] = subject+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falseapp.include_router(inference.router, prefix = "/api/inference")return TestClient(app)diff --git a/studio/backend/tests/multi_account/test_media_unload_handoff.py b/studio/backend/tests/multi_account/test_media_unload_handoff.pyindex 705a144d712..dbbe21ce205 100644--- a/studio/backend/tests/multi_account/test_media_unload_handoff.py+++ b/studio/backend/tests/multi_account/test_media_unload_handoff.py@@ -17,7 +17,7 @@ from fastapi import FastAPIfrom fastapi.testclient import TestClientfrom auth import policy-from auth.authentication import get_current_subject+from auth.authentication import authenticated_via_api_key, get_current_subjectfrom core.inference import diffusion_engine_router, gpu_arbiterfrom core.inference.diffusion import DiffusionBackendfrom core.inference.diffusion_families import DIFFUSION_CANCELLED_MSG@@ -46,6 +46,7 @@ def client_for(account):reset_account(token)app.dependency_overrides[get_current_subject] = subject+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falseapp.include_router(inference.studio_router, prefix = "/api/inference")app.include_router(video.router, prefix = "/api/inference")return TestClient(app)diff --git a/studio/backend/tests/test_account_inference_isolation.py b/studio/backend/tests/test_account_inference_isolation.pyindex b23f7c33016..f9e6b1fd8bf 100644--- a/studio/backend/tests/test_account_inference_isolation.py+++ b/studio/backend/tests/test_account_inference_isolation.py@@ -12,7 +12,11 @@ from fastapi import FastAPI, HTTPExceptionfrom fastapi.testclient import TestClientfrom auth import policy-from auth.authentication import allow_ambient_hf_token, get_current_subject+from auth.authentication import (+ allow_ambient_hf_token,+ authenticated_via_api_key,+ get_current_subject,+)from core.inference import gpu_arbiterfrom hub.services.models import account_access as accessfrom models.inference import LoadRequest, UnloadRequest@@ -61,6 +65,9 @@ def client_for(account):app.dependency_overrides[get_current_subject] = subjectapp.dependency_overrides[allow_ambient_hf_token] = lambda: False+ # A browser session: the isolation these tests check is between ACCOUNTS, not between+ # caller classes.+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falseapp.include_router(inference.router, prefix = "/api/inference")app.include_router(inference.studio_router, prefix = "/api/inference")app.include_router(video.router, prefix = "/api/inference")diff --git a/studio/backend/tests/test_account_media_isolation.py b/studio/backend/tests/test_account_media_isolation.pyindex 5ffdcfa48ce..c9966c83550 100644--- a/studio/backend/tests/test_account_media_isolation.py+++ b/studio/backend/tests/test_account_media_isolation.py@@ -12,7 +12,7 @@ from fastapi.testclient import TestClientfrom PIL import Imagefrom auth import policy, storage as auth_storage-from auth.authentication import get_current_subject+from auth.authentication import authenticated_via_api_key, get_current_subjectfrom core.inference import audio_gallery, image_gallery, search_images, video_galleryfrom routes import inference, videofrom utils.account_context import OWNER, AccountContext, bind_account, reset_account, run_as@@ -78,6 +78,7 @@ def _client(account):reset_account(token)app.dependency_overrides[get_current_subject] = subject+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falseapp.include_router(inference.studio_router, prefix = "/api/inference")app.include_router(video.router, prefix = "/api/inference")app.include_router(video.openai_router, prefix = "/v1")diff --git a/studio/backend/tests/test_cache_inventory_path_boundary.py b/studio/backend/tests/test_cache_inventory_path_boundary.pynew file mode 100644index 00000000000..0c245432337--- /dev/null+++ b/studio/backend/tests/test_cache_inventory_path_boundary.py@@ -0,0 +1,1789 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""An API key gets the local inventory listing but not the host paths; a browser session still+gets them. Assertions are on the SERIALISED body: a unit test on the helper alone would pass+with the helper wired to nothing.+"""++import ast+import inspect+import json+import re+import time+from pathlib import Path++import pytest+from fastapi import FastAPI, HTTPException+from fastapi.testclient import TestClient++from auth.authentication import authenticated_via_api_key, get_current_subject+from hub.routes import inventory as inventory_routes+from hub.schemas.inventory import (+ OrphanCompanionInfo,+ LocalModelListResponse,+ ModelsFolderResponse,+ OrphanCompanionsResponse,+ ScanFolderInfo,+ ScanFoldersResponse,+)+from hub.services.models import cache_inventory, companion_cleanup, local_inventory+from hub.utils import host_paths+from hub.utils.host_paths import (+ cache_reference,+ redact_host_paths,+ redact_inventory_host_paths,+ redact_paths_in_text,+ response_leaks_host_path,+ scrub_paths,+ short_path_for_log,+)+from routes import models as models_routes++# Empty: no field is exempt from redaction any more, including ``load_id``.+LOAD_HANDLE: "tuple[str, ...]" = ()++# A root that cannot exist by accident, so finding it in a body is proof and not a coincidence.+HOST_ROOT = "/home/operator-7f3c/.cache/huggingface/hub"+# A constructed path comes back with backslashes on Windows, so a POSIX literal is a leak+# check that can never fire.+HOST_ROOT_NATIVE = str(Path(HOST_ROOT))+REPO_DIR = f"{HOST_ROOT}/models--unsloth--Llama-3.2-1B-Instruct"+++def _client(router, prefix: str, *, via_api_key: bool) -> TestClient:+ app = FastAPI()+ app.include_router(router, prefix = prefix)+ app.dependency_overrides[get_current_subject] = lambda: "alice"+ app.dependency_overrides[authenticated_via_api_key] = lambda: via_api_key+ return TestClient(app, raise_server_exceptions = False)+++def _hub(via_api_key: bool) -> TestClient:+ return _client(inventory_routes.router, "/api/hub", via_api_key = via_api_key)+++def _models(via_api_key: bool) -> TestClient:+ return _client(models_routes.router, "/api/models", via_api_key = via_api_key)+++def _cached_row(**extra) -> dict:+ row = {+ "repo_id": "unsloth/Llama-3.2-1B-Instruct",+ "size_bytes": 2471234567,+ "cache_path": REPO_DIR,+ "partial": False,+ "model_format": "safetensors",+ "runtime": "transformers",+ "load_id": "unsloth/Llama-3.2-1B-Instruct",+ }+ row.update(extra)+ return row+++def jsonable(payload):+ from fastapi.encoders import jsonable_encoder+ return jsonable_encoder(payload)+++def test_a_reference_is_opaque_stable_and_not_the_path():+ first = cache_reference(REPO_DIR)+ assert first is not None+ assert first == cache_reference(REPO_DIR)+ assert first != cache_reference(REPO_DIR + "x")+ assert REPO_DIR not in first+ assert "operator-7f3c" not in first+ assert cache_reference("") is None+ assert cache_reference(None) is None+++def test_a_ui_session_payload_is_returned_untouched():+ payload = {"cached": [_cached_row()]}+ assert redact_host_paths(payload, via_api_key = False) is payload+ assert redact_inventory_host_paths(payload, via_api_key = False) is payload++ # BOUNDARY. A browser session keeps every path and gains nothing beside it.+ row = {"repo_id": "acme/model", "cache_path": "/home/op/.cache/huggingface/hub/x"}+ assert redact_host_paths(row, via_api_key = False) is row+ assert host_paths.CACHE_REFERENCE_FIELD not in row+++def test_redaction_keeps_every_non_path_field():+ payload = {"cached": [_cached_row()], "scan_confirmed": True}+ out = redact_host_paths(payload, via_api_key = True)+ row = out["cached"][0]+ assert row["repo_id"] == "unsloth/Llama-3.2-1B-Instruct"+ assert row["size_bytes"] == 2471234567+ assert row["model_format"] == "safetensors"+ assert out["scan_confirmed"] is True+ assert row["cache_path"] == ""+ assert row["cache_ref"] == cache_reference(REPO_DIR)+++# `cache_ref` is the "is this cached" discriminator `cache_path` truthiness no longer can be.+++def test_a_redacted_row_is_still_distinguishable_from_one_with_no_path_at_all():+ cached = {"repo_id": "acme/model", "cache_path": "/home/op/.cache/huggingface/hub/x"}+ uncached = {"repo_id": "acme/other", "cache_path": None}++ red_cached = host_paths.redact_host_paths(cached, via_api_key = True)+ red_uncached = host_paths.redact_host_paths(uncached, via_api_key = True)++ assert red_cached["cache_path"] == ""+ assert red_uncached["cache_path"] is None+ assert red_cached[host_paths.CACHE_REFERENCE_FIELD].startswith("ref:")+ assert (+ host_paths.CACHE_REFERENCE_FIELD not in red_uncached+ ), "a row with no path must not gain a reference, or the discriminator says nothing"+++def test_the_ambiguous_path_field_is_only_redacted_where_it_is_a_path():+ payload = {"path": REPO_DIR}+ assert redact_host_paths(payload, via_api_key = True)["path"] == REPO_DIR+ assert redact_inventory_host_paths(payload, via_api_key = True)["path"] == ""+++def test_scan_root_lists_are_emptied_not_referenced():+ payload = {"lmstudio_dirs": [f"{HOST_ROOT}/lm"], "exact_paths": [REPO_DIR]}+ out = redact_host_paths(payload, via_api_key = True)+ assert out["lmstudio_dirs"] == []+ assert out["exact_paths"] == []+++# `base_model` is a host path for one source and a repo id for the next; the sibling decides.+def _adapter(identifier, base_model, source) -> dict:+ return {"models": [{"id": identifier, "base_model": base_model, "base_model_source": source}]}+++_LOCAL_ADAPTER = _adapter("my-lora", "/home/op/models/Llama-3.1-8B", "local")+_HUB_ADAPTER = _adapter("other-lora", "meta-llama/Llama-3.1-8B", "huggingface")++# Every route test below trusts the leak detector, so it is pinned on both answers.+_DEFAULT_ROOTS = object()+++ ("payload", "roots", "leaks"),+ [+ ({"cached": [_cached_row()]}, [HOST_ROOT], True),+ (redact_host_paths({"cached": [_cached_row()]}, via_api_key = True), [HOST_ROOT], False),+ ({"detail": f"could not read {REPO_DIR}"}, [HOST_ROOT], True),+ (_LOCAL_ADAPTER, ["/home/op"], True),+ (_HUB_ADAPTER, ["/home/op"], False),+ ({"load_id": f"{HOST_ROOT}/snapshots/abc"}, _DEFAULT_ROOTS, True),+ ({"load_id": "unsloth/Llama-3.2-1B-Instruct"}, _DEFAULT_ROOTS, False),+ ({"load_id": "ref:0123456789abcdef"}, _DEFAULT_ROOTS, False),+ ],+)+def test_the_leak_detector_finds_what_it_is_for(payload, roots, leaks):+ found = (+ response_leaks_host_path(payload)+ if roots is _DEFAULT_ROOTS+ else response_leaks_host_path(payload, roots)+ )+ assert (found is not None) is leaks, found+++ "message, expected",+ [+ (+ f"Skipping {REPO_DIR}: Permission denied",+ "Skipping .../hub/models--unsloth--Llama-3.2-1B-Instruct: Permission denied",+ ),+ (+ "ratio 3/4 and https://huggingface.co/api/models",+ "ratio 3/4 and https://huggingface.co/api/models",+ ),+ ("nothing to see", "nothing to see"),+ (OSError(f"cannot open {REPO_DIR}/blobs/abc"), "cannot open .../blobs/abc"),+ (None, ""),+ (+ r"Skipping \\fileserver\models\hub\models--acme--x: denied",+ "Skipping .../hub/models--acme--x: denied",+ ),+ (r"open \\srv\share\acme\config.json failed", "open .../acme/config.json failed"),+ ("ratio 3/4", "ratio 3/4"),+ ("https://huggingface.co/api/models/a/b", "https://huggingface.co/api/models/a/b"),+ ("Skipping ./models/repo: denied", "Skipping ./models/repo: denied"),+ ("Skipping ../models/repo: denied", "Skipping ../models/repo: denied"),+ ("Skipping ../../a/b/c: denied", "Skipping ../../a/b/c: denied"),+ ("Skipping models/team/repo: denied", "Skipping models/team/repo: denied"),+ # A two-component path keeps its root: rebuilding turned `/srv/cache` into `srv/cache`.+ ("cache root /srv/cache is unreadable", "cache root /srv/cache is unreadable"),+ (+ "Failed /home/op/.cache/huggingface/hub/models--acme--x: EACCES",+ "Failed .../hub/models--acme--x: EACCES",+ ),+ # The label between two paths is not swallowed, nor does a space break the run.+ (+ "Scan folder rejected: /home/jane.doe/.cache/huggingface/hub (path=/home/jane.doe/x)",+ "Scan folder rejected: .../huggingface/hub (path=.../jane.doe/x)",+ ),+ ("Skipping /srv/client(acme)/models: denied", "Skipping .../client(acme)/models: denied"),+ (+ "Skipping /srv/Program Files/models--x: denied",+ "Skipping .../Program Files/models--x: denied",+ ),+ # A path written as a URI. The rule that keeps `https://` intact refuses the leading+ # slash here too, so these are matched by their scheme instead.+ (+ f"cannot open file://{REPO_DIR}/blobs/abc",+ "cannot open .../blobs/abc",+ ),+ (+ "cannot open file:///C:/Users/operator/models--x/config.json",+ "cannot open .../models--x/config.json",+ ),+ ],+)+def test_log_messages_lose_the_layout_and_keep_the_meaning(message, expected):+ assert scrub_paths(message) == expected+++ "message",+ [+ f"OSError: cannot read file://{REPO_DIR}/blobs/abc",+ f"file://localhost{REPO_DIR}/config.json missing",+ "load failed for file:///C:/Users/operator/models--x/model.gguf",+ ],+)+def test_a_path_written_as_a_uri_is_redacted_like_any_other(message):+ """An exception that formats its filename as a URI went out whole: the lookbehind that+ protects `https://` refuses the URI's own first slash, so nothing matched and the retained+ error text an API-key caller reads carried the host path."""+ redacted = redact_paths_in_text(message)+ assert HOST_ROOT not in redacted and "Users/operator" not in redacted, redacted+ assert "<path>" in redacted++ # BOUNDARY. Web URLs, and a bare scheme with no path, are left alone.+ for kept in (+ "see https://huggingface.co/api/models and http://localhost:8000/v1",+ "file:// nothing here",+ "ratio 3/4",+ ):+ assert redact_paths_in_text(kept) == kept+++ "path, expected",+ [+ (Path(REPO_DIR), ".../hub/models--unsloth--Llama-3.2-1B-Instruct"),+ ("./models/repo", "./models/repo"),+ ("../models/repo", "../models/repo"),+ ],+)+def test_short_path_for_log_keeps_the_tail_and_relative_paths_as_written(path, expected):+ assert short_path_for_log(path) == expected+++ "scrub, text",+ [+ (scrub_paths, ("word " * 20_000) + "x"),+ (redact_paths_in_text, "/srv/models" + ", filler" * 4000),+ ],+ ids = ("a-long-line-with-no-path", "the-redacted-tail-pass"),+)+def test_the_text_passes_stay_linear(scrub, text):+ started = time.monotonic()+ scrub(text)+ assert time.monotonic() - started < 1.0+++# The directory name goes WHOLE: stopping the run at punctuation inside it publishes the rest+# of the layout. `_PATH_COMPONENT` ends at `=`, so the tail rule treats `=` like `:`, `;`, `,`.+++ "message, expected",+ [+ ("Failed /srv/client(acme)/models", "Failed <path>"),+ ("cannot read /home/o'connor/models", "cannot read <path>"),+ (r"C:\cache\Models (private)", "<path>"),+ ("open /mnt/data[1]/models denied", "open <path>"),+ ("load /home/operator/Acme, Inc/private/model.bin failed", "load <path>"),+ ("read /srv/Acme, Inc, Ltd/private/model.bin", "read <path>"),+ (r"C:\Users\operator\Acme, Inc\private\model.bin", "<path>"),+ ("read /srv/models; Acme; Inc/private/model.bin", "read <path>"),+ (f"Error at {HOST_ROOT}/foo=bar/model/config.json", "Error at <path>"),+ (f"failed: {HOST_ROOT}/models--a--b=v2/snapshots/dead/config.json", "failed: <path>"),+ ("Skipping /a/b: Permission denied", "Skipping <path>: Permission denied"),+ ('opened "/a/b" already', 'opened "<path>" already'),+ ("tried /a/b, /c/d", "tried <path>, <path>"),+ ("Skipping /a/b: denied, see /etc/fstab", "Skipping <path>: denied, see <path>"),+ # BOUNDARY: prose, an `=` outside a path, and a URL are not host paths.+ ("3/4 of the shards", "3/4 of the shards"),+ ("and/or the projector", "and/or the projector"),+ ("no paths here", "no paths here"),+ ("ratio 3/4 and rate=0.5 are fine", "ratio 3/4 and rate=0.5 are fine"),+ (+ "see https://huggingface.co/acme/model for details",+ "see https://huggingface.co/acme/model for details",+ ),+ ],+)+def test_a_directory_name_is_removed_whole_and_prose_is_left_alone(message, expected):+ cleaned = redact_paths_in_text(message)+ assert cleaned == expected, (message, cleaned)+++def _cached_inventory(monkeypatch):+ async def _models_response(hf_token = None):+ return {"cached": [_cached_row()], "scan_confirmed": True}++ async def _gguf_response(hf_token = None):+ return {+ "cached": [+ _cached_row(+ repo_id = "unsloth/gemma-3-270m-it-GGUF",+ model_format = "gguf",+ cache_path = f"{HOST_ROOT}/models--unsloth--gemma-3-270m-it-GGUF",+ load_id = f"{HOST_ROOT}/models--unsloth--gemma-3-270m-it-GGUF/snapshots/abc",+ )+ ],+ "scan_confirmed": True,+ }++ monkeypatch.setattr(cache_inventory, "list_cached_models_response", _models_response)+ monkeypatch.setattr(cache_inventory, "list_cached_gguf_response", _gguf_response)+ monkeypatch.setattr(models_routes, "cached_model_rows", lambda *a, **k: [_cached_row()])+ monkeypatch.setattr(models_routes, "cached_gguf_rows", lambda *a, **k: [_cached_row()])+++ ("client", "route", "row_fields"),+ [+ (_hub, "/api/hub/cached-models", True),+ (_hub, "/api/hub/cached-gguf", True),+ # /api/models is the OpenAI-compatible mirror of /api/hub, reachable with the same key,+ # and it answers a shape of its own: the leak check is what both have in common.+ (_models, "/api/models/cached-models", False),+ (_models, "/api/models/cached-gguf", False),+ ],+ ids = ("hub-models", "hub-gguf", "compat-models", "compat-gguf"),+)+def test_the_cache_is_enumerable_without_the_layout_it_sits_in(+ _cached_inventory, client, route, row_fields+):+ answered = client(via_api_key = True).get(route)+ assert answered.status_code == 200+ payload = answered.json()+ assert [row["repo_id"] for row in payload["cached"]], "the listing must still answer"+ assert response_leaks_host_path(payload, [HOST_ROOT], ignore = LOAD_HANDLE) is None+ if row_fields:+ for row in payload["cached"]:+ assert row["cache_path"] == ""+ assert row["cache_ref"].startswith("ref:")+ assert HOST_ROOT not in json.dumps(row), row+ assert not row["repo_id"].startswith("ref:")++ # BOUNDARY. A browser session still sees its own machine.+ ui = client(via_api_key = False).get(route).json()+ assert ui["cached"]+ if row_fields:+ assert all(row["cache_path"].startswith(HOST_ROOT) for row in ui["cached"])+++def test_the_models_folder_is_not_disclosed(monkeypatch):+ monkeypatch.setattr(+ local_inventory, "get_models_folder_response", lambda: ModelsFolderResponse(path = HOST_ROOT)+ )+ assert _hub(via_api_key = True).get("/api/hub/models-folder").json()["path"] == ""+ assert _hub(via_api_key = False).get("/api/hub/models-folder").json()["path"] == HOST_ROOT+++# A raised detail is walked like a payload: the message survives, the layout does not.+++ ("raised", "secret", "kept", "message", "ui_sees_it"),+ [+ (+ f"Failed to create models folder: {HOST_ROOT}/hub: Permission denied",+ HOST_ROOT,+ ("Permission denied", "Failed to create models folder"),+ None,+ True,+ ),+ *[+ (+ f"Models folder path is not a directory: {root}",+ root,+ ("not a directory",),+ None,+ False,+ )+ for root in ("/tmp", "/cache", "C:\\cache")+ ],+ (+ {"message": "bad folder", "path": f"{HOST_ROOT}/hub"},+ HOST_ROOT,+ (),+ "bad folder",+ False,+ ),+ ],+)+def test_the_models_folder_error_is_not_disclosed_either(+ monkeypatch, raised, secret, kept, message, ui_sees_it+):+ def _raises():+ raise HTTPException(status_code = 500, detail = raised)++ monkeypatch.setattr(local_inventory, "get_models_folder_response", _raises)++ response = _hub(via_api_key = True).get("/api/hub/models-folder")+ assert response.status_code == 500+ detail = response.json()["detail"]+ assert secret not in str(detail), detail+ for fragment in kept:+ assert fragment in str(detail), detail+ if message is not None:+ assert detail["message"] == message, detail++ if ui_sees_it:+ session = _hub(via_api_key = False).get("/api/hub/models-folder")+ assert session.status_code == 500+ assert HOST_ROOT in session.json()["detail"]+++def test_the_hidden_model_matchers_keep_their_ids_and_drop_their_paths(monkeypatch):+ monkeypatch.setattr(+ models_routes,+ "hidden_model_matchers",+ lambda: (["needle"], ["org/repo"], [REPO_DIR]),+ )+ payload = _hub(via_api_key = True).get("/api/hub/hidden-models").json()+ assert payload["needles"] == ["needle"]+ assert payload["exact_ids"] == ["org/repo"]+ assert payload["exact_paths"] == []+ ui = _hub(via_api_key = False).get("/api/hub/hidden-models").json()+ assert ui["exact_paths"] == [REPO_DIR]+++def test_orphan_companions_keep_their_repo_ids(monkeypatch):+ async def _response():+ return OrphanCompanionsResponse(+ companions = [+ OrphanCompanionInfo(+ repo_id = "org/vae",+ size_bytes = 12,+ cache_path = f"{HOST_ROOT}/models--org--vae",+ )+ ],+ total_bytes = 12,+ )++ monkeypatch.setattr(companion_cleanup, "orphan_companions_response", _response)+ payload = _hub(via_api_key = True).get("/api/hub/orphan-companions").json()+ assert payload["companions"][0]["repo_id"] == "org/vae"+ assert payload["companions"][0]["cache_ref"].startswith("ref:")+ assert response_leaks_host_path(payload, [HOST_ROOT]) is None+++ ("client", "route", "reader", "answers_with_a_reference"),+ [+ (_hub, "/api/hub/download-progress", "get_download_progress_response", True),+ (_models, "/api/models/download-progress", "get_download_progress_response", False),+ (+ _models,+ "/api/models/gguf-download-progress",+ "get_gguf_download_progress_response",+ False,+ ),+ ],+ ids = ("hub", "compat", "compat-gguf"),+)+def test_download_progress_hides_the_cache_dir_it_measured(+ monkeypatch, client, route, reader, answers_with_a_reference+):+ """The measurement is taken FROM the cache directory, so both compat aliases must drop it+ on the way out too."""++ async def _progress(repo_id, **_kwargs):+ return {+ "repo_id": repo_id,+ "downloaded_bytes": 10,+ "expected_bytes": 100,+ "progress": 0.1,+ "complete": False,+ "cache_path": REPO_DIR,+ }++ from hub.services.models import downloads++ monkeypatch.setattr(downloads, reader, _progress)+ payload = client(via_api_key = True).get(route, params = {"repo_id": "org/repo"}).json()+ assert payload["cache_path"] == ""+ assert response_leaks_host_path(payload, [HOST_ROOT]) is None+ if answers_with_a_reference:+ assert payload["cache_ref"].startswith("ref:")+++# Hub inventory routes that answer no host path. Anything else must take the caller class.+_ROUTES_WITHOUT_HOST_PATHS = {+ "remove_scan_folder_endpoint": "status and id only",+ "get_gguf_variants": "filenames and quant labels, no directory",+ "download_model": "job id and status",+ "cancel_download_model": "job id and status",+ "get_active_downloads": "repo ids and byte counts",+ "delete_impact": "repo ids, byte counts and blockers",+ "delete_cached_model": "status, repo id and variant",+}+++def _route_arguments(module, *, router_decorated = False) -> dict:+ """With `router_decorated`, only top-level functions carrying a `@router.<method>(...)`."""+ tree = ast.parse(Path(module.__file__).read_text(encoding = "utf-8"))+ found = {}+ for node in tree.body if router_decorated else ast.walk(tree):+ if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):+ continue+ if router_decorated and not any(+ isinstance(dec, ast.Call)+ and isinstance(dec.func, ast.Attribute)+ and isinstance(dec.func.value, ast.Name)+ and dec.func.value.id == "router"+ for dec in node.decorator_list+ ):+ continue+ found[node.name] = {arg.arg for arg in node.args.args + node.args.kwonlyargs}+ return found+++def test_every_inventory_route_that_could_answer_a_path_takes_the_caller_class():+ routes = _route_arguments(inventory_routes, router_decorated = True)+ missing = [+ name+ for name, args in routes.items()+ if "via_api_key" not in args and name not in _ROUTES_WITHOUT_HOST_PATHS+ ]+ assert not missing, (+ "these inventory routes neither take via_api_key nor are recorded as path-free: "+ f"{missing}. Add the dependency and redact, or list the route with its reason."+ )+++def test_the_path_field_lists_cover_the_inventory_schemas():+ from hub.schemas import inventory as inventory_schemas++ known = (+ host_paths.HOST_PATH_SCALAR_FIELDS+ | host_paths.HOST_PATH_LIST_FIELDS+ | {host_paths.HOST_PATH_AMBIGUOUS_FIELD}+ )+ suspicious = []+ for name in dir(inventory_schemas):+ model = getattr(inventory_schemas, name)+ fields = getattr(model, "model_fields", None)+ if not isinstance(fields, dict):+ continue+ for field in fields:+ if field in known:+ continue+ if field.endswith(("_path", "_paths", "_dir", "_dirs")) or field in {"path", "paths"}:+ suspicious.append(f"{name}.{field}")+ assert not suspicious, (+ "path-shaped response fields that redaction does not know about: " + repr(suspicious)+ )+++def test_a_local_adapter_base_model_is_redacted_but_a_repo_id_is_kept():+ payload = {+ "models": [+ {**row, "path": f"/home/op/models/{row['id']}"}+ for row in _LOCAL_ADAPTER["models"] + _HUB_ADAPTER["models"]+ ]+ }++ via_key = redact_inventory_host_paths(payload, via_api_key = True)["models"]+ assert via_key[0]["base_model"] == "", "a local base-model PATH must not reach an API key"+ assert via_key[0]["base_model_source"] == "local", "the source itself is not a path"+ assert (+ via_key[1]["base_model"] == "meta-llama/Llama-3.1-8B"+ ), "a Hub repo id is what the caller asked for and must survive redaction"++ session = redact_inventory_host_paths(payload, via_api_key = False)["models"]+ assert session[0]["base_model"] == "/home/op/models/Llama-3.1-8B"+++def _stub_hub_scan_folders(monkeypatch):+ monkeypatch.setattr(+ local_inventory,+ "get_scan_folders_response",+ lambda: ScanFoldersResponse(+ folders = [ScanFolderInfo(id = 1, path = f"{HOST_ROOT}/extra", created_at = "2026-09-01")]+ ),+ )+++def _stub_compat_scan_folders(monkeypatch):+ monkeypatch.setattr(+ models_routes,+ "annotate_scan_folders",+ lambda folders: [{"id": 1, "path": f"{HOST_ROOT}/extra", "created_at": "2026-09-01"}],+ )+ monkeypatch.setattr(models_routes, "refresh_failed_scan_folders", lambda folders: None)+ monkeypatch.setattr("storage.studio_db.list_scan_folders", lambda: [{"id": 1}])+++ ("install", "client", "route"),+ [+ (_stub_hub_scan_folders, _hub, "/api/hub/scan-folders"),+ (_stub_compat_scan_folders, _models, "/api/models/scan-folders"),+ ],+ ids = ("hub", "compat"),+)+def test_a_scan_folder_is_listed_by_id_and_not_by_where_it_points(+ monkeypatch, install, client, route+):+ install(monkeypatch)+ payload = client(via_api_key = True).get(route).json()+ assert payload["folders"], "the folder is still listed, by id"+ assert response_leaks_host_path(payload, [HOST_ROOT]) is None++ # BOUNDARY. The folder a browser session added is shown where it added it.+ ui = client(via_api_key = False).get(route).json()+ assert ui["folders"][0]["path"] == f"{HOST_ROOT}/extra"+++def test_the_compat_local_scan_is_not_disclosed(monkeypatch):+ async def _scan(models_root, sources):+ return []++ monkeypatch.setattr(models_routes, "_shared_compat_local_inventory_scan", _scan)+ payload = (+ _models(via_api_key = True).get("/api/models/local", params = {"models_dir": "./models"}).json()+ )+ assert payload["hf_cache_dir"] == ""+ assert payload["lmstudio_dirs"] == []+ assert payload["models_dir"] == ""+++_COMPAT_INVENTORY_ROUTES = (+ "list_local_models",+ "get_scan_folders",+ "get_download_progress",+ "get_gguf_download_progress",+ "list_cached_gguf",+ "list_cached_models",+)+++def test_every_compat_mirror_of_an_inventory_route_takes_the_caller_class():+ found = _route_arguments(models_routes)+ missing_routes = [name for name in _COMPAT_INVENTORY_ROUTES if name not in found]+ assert not missing_routes, f"these routes were renamed or removed: {missing_routes}"+ without = [name for name in _COMPAT_INVENTORY_ROUTES if "via_api_key" not in found[name]]+ assert not without, (+ "these compatibility routes answer inventory data without taking the caller class: "+ f"{without}"+ )+++def _local_row():+ from hub.services.models.common import _local_model_info+ load_path = Path(HOST_ROOT) / "my models" / "Llama-3.2-1B"+ return _local_model_info(+ scan_path = load_path,+ load_path = load_path,+ source = "models_dir",+ model_format = "safetensors",+ )+++def _local_inventory(monkeypatch):+ async def _response(models_dir = "./models"):+ return LocalModelListResponse(+ models_dir = f"{HOST_ROOT}/models",+ hf_cache_dir = HOST_ROOT,+ lmstudio_dirs = [f"{HOST_ROOT}/lmstudio"],+ ollama_dirs = [],+ hermes_dirs = [],+ models = [_local_row()],+ )++ monkeypatch.setattr(local_inventory, "list_local_models_response", _response)+++def test_a_filesystem_backed_row_is_not_named_by_its_path(_local_inventory):+ from models.inference import LoadRequest++ payload = _hub(via_api_key = True).get("/api/hub/local").json()+ row = payload["models"][0]+ assert row["path"] == ""+ assert row["id"].startswith("ref:"), row["id"]+ assert row["load_id"].startswith("ref:"), row["load_id"]+ assert row["inventory_id"].startswith("models_dir:safetensors:"), row["inventory_id"]+ assert payload["lmstudio_dirs"] == []+ assert HOST_ROOT not in json.dumps(payload), payload+ # Both spellings, or the leak check is vacuous wherever the row carries the other one.+ assert HOST_ROOT_NATIVE not in json.dumps(payload), payload+ assert "my%20models" not in json.dumps(payload), payload+ assert response_leaks_host_path(payload, [HOST_ROOT, HOST_ROOT_NATIVE]) is None++ assert LoadRequest(model_path = row["load_id"]).model_path == str(+ Path(HOST_ROOT) / "my models" / "Llama-3.2-1B"+ )++ # BOUNDARY.+ ui = _hub(via_api_key = False).get("/api/hub/local").json()+ assert ui["models"][0]["load_id"].startswith(HOST_ROOT_NATIVE)+ assert ui["hf_cache_dir"] == HOST_ROOT+++def _stub_rejected_scan_folder(monkeypatch):+ def _raises(path):+ raise HTTPException(+ status_code = 400,+ detail = f"Path is not readable: [Errno 36] File name too long: '{REPO_DIR}'",+ )++ monkeypatch.setattr(local_inventory, "add_scan_folder_response", _raises)+++def _stub_failing_local_scan(monkeypatch):+ async def _raises(models_dir = "./models"):+ raise HTTPException(+ status_code = 500,+ detail = f"Failed to list local models: unable to open database file {HOST_ROOT}/studio.db",+ )++ monkeypatch.setattr(local_inventory, "list_local_models_response", _raises)+++ ("install", "send", "status", "kept"),+ [+ (+ _stub_rejected_scan_folder,+ lambda api_key: _hub(via_api_key = api_key).post(+ "/api/hub/scan-folders", json = {"path": "./models"}+ ),+ 400,+ "File name too long",+ ),+ (+ _stub_failing_local_scan,+ lambda api_key: _hub(via_api_key = api_key).get("/api/hub/local"),+ 500,+ "unable to open database file",+ ),+ ],+ ids = ("a-rejected-scan-folder", "a-failing-local-scan"),+)+def test_a_raised_detail_keeps_the_reason_and_loses_the_layout(+ monkeypatch, install, send, status, kept+):+ """The error text is the only account of WHY, so it is scrubbed rather than blanked."""+ install(monkeypatch)+ response = send(True)+ assert response.status_code == status+ detail = str(response.json()["detail"])+ assert HOST_ROOT not in detail, detail+ assert REPO_DIR not in detail, detail+ assert kept in detail, detail++ # BOUNDARY. A browser session is told exactly which path failed.+ assert HOST_ROOT in str(send(False).json()["detail"])+++def test_a_reference_table_that_fills_up_drops_the_oldest(monkeypatch):+ """Evicted by AGE, not by count, or a listing longer than the table breaks its own rows."""+ clock = {"now": 0.0}+ # Process-global: other tests' entries carry real timestamps this fake clock cannot age out.+ host_paths._reference_paths.clear()+ monkeypatch.setattr(host_paths.time, "monotonic", lambda: clock["now"])+ monkeypatch.setattr(host_paths, "_REFERENCE_LIMIT", 4)+ first = host_paths.cache_reference("/host/first")+ for index in range(6):+ host_paths.cache_reference(f"/host/filler-{index}")+ assert host_paths.resolve_host_path_reference(first) == "/host/first"+ clock["now"] = host_paths._REFERENCE_PIN_SECONDS + 1.0+ for index in range(6):+ host_paths.cache_reference(f"/host/later-{index}")+ assert host_paths.resolve_host_path_reference(first) is None+ newest = host_paths.cache_reference("/host/newest")+ assert host_paths.resolve_host_path_reference(newest) == "/host/newest"+++def _request_model(name: str):+ from models import inference, training+ return getattr(inference, name, None) or getattr(training, name)+++ ("model_name", "fields", "extra"),+ [+ ("LoadRequest", ("model_path",), {}),+ ("ValidateModelRequest", ("model_path",), {}),+ ("DiffusionLoadRequest", ("model_path",), {}),+ ("VideoLoadRequest", ("model_path",), {}),+ ("UnloadRequest", ("model_path",), {}),+ ("EstimateMemoryRequest", ("model_path",), {}),+ (+ "TransformersUpgradeCheckRequest",+ (+ "model_name",+ "model_local_path",+ "model_snapshot_path",+ "model_snapshot_repo_id",+ ),+ {},+ ),+ (+ "TrainingStartRequest",+ # The snapshot pins too: Resume replays the run's config, and a pin it cannot+ # resolve is a pin it stops asking for, which re-resolves the newest cached+ # revision and continues the checkpoint against different base weights.+ (+ "model_name",+ "resume_from_checkpoint",+ "model_snapshot_path",+ "dataset_snapshot_path",+ ),+ {"training_type": "LoRA/QLoRA", "format_type": "chat"},+ ),+ (+ "DiffusionTrainingStartRequest",+ ("base_model", "output_dir", "resume_from_checkpoint"),+ {"data_dir": "/data/images"},+ ),+ ],+)+def test_every_request_that_consumes_an_inventory_identity_resolves_the_handle(+ model_name, fields, extra+):+ request_model = _request_model(model_name)+ path = f"{HOST_ROOT}/my models/Llama-3.2-1B"+ reference = host_paths.cache_reference(path)++ resolved = request_model(**{field: reference for field in fields}, **extra)+ for field in fields:+ assert getattr(resolved, field) == path, (model_name, field)++ # An unissued reference is left as written, so it fails like an unknown model.+ for written in ("unsloth/Llama-3.2-1B", "ref:" + "0" * 32):+ echoed = request_model(**{field: written for field in fields}, **extra)+ for field in fields:+ assert getattr(echoed, field) == written, (model_name, field, written)+++ ("client", "route"),+ [(_hub, "/api/hub/delete-cached"), (_models, "/api/models/delete-cached")],+ ids = ("hub", "compat"),+)+def test_a_cache_reference_can_delete_the_copy_it_names(monkeypatch, client, route):+ seen = {}++ async def _delete(+ repo_id,+ variant,+ hf_token,+ cache_path,+ only_if_orphan = None,+ ):+ seen.update(repo_id = repo_id, variant = variant, cache_path = cache_path)+ return {"status": "deleted", "repo_id": repo_id, "variant": variant}++ from hub.services.models import account_access, deletion++ monkeypatch.setattr(deletion, "delete_cached_model_response", _delete)+ monkeypatch.setattr(account_access, "require_installation_owner", lambda: None)+ reference = host_paths.cache_reference(REPO_DIR)+ assert reference != REPO_DIR++ def _sent(via_api_key, body):+ seen.clear()+ answered = client(via_api_key = via_api_key).request("DELETE", route, json = body)+ assert answered.status_code == 200, answered.text+ return answered++ answered = _sent(True, {"repo_id": "unsloth/Llama-3.2-1B", "cache_path": reference})+ assert answered.json()["status"] == "deleted"+ assert seen["cache_path"] == REPO_DIR++ # An absent `cache_path` reaches the service as None, not as some resolved root.+ _sent(True, {"repo_id": "unsloth/Llama-3.2-1B-Instruct", "variant": None})+ assert seen["cache_path"] is None++ # BOUNDARY. A browser session names the path itself, and it arrives as written.+ _sent(False, {"repo_id": "unsloth/Llama-3.2-1B", "cache_path": REPO_DIR})+ assert seen["cache_path"] == REPO_DIR+++ ("client", "route"),+ [(_hub, "/api/hub/gguf-variants"), (_models, "/api/models/gguf-variants")],+ ids = ("hub", "compat"),+)+def test_a_cache_reference_lists_the_quants_of_the_copy_it_names(monkeypatch, client, route):+ """The inventory hands an API-key caller a handle in place of every host path, so a handle+ is the only name it HAS for a custom local GGUF or for a copy in a secondary root. Passed+ back unresolved, the lookup either misses or is answered out of the ACTIVE cache, which is+ a different file."""+ seen = {}++ from hub.schemas.inventory import GgufVariantsResponse+ from hub.services.models import account_access, gguf_variants as hub_gguf_variants++ async def _variants(+ repo_id,+ prefer_local_cache = False,+ offline = False,+ local_path = None,+ hf_token = None,+ ):+ seen.update(repo_id = repo_id, local_path = local_path)+ return GgufVariantsResponse(repo_id = repo_id, variants = [])++ async def _answer(repo_id, **kwargs):+ return hub_gguf_variants.VariantsAnswer(await _variants(repo_id, **kwargs), None, True)++ monkeypatch.setattr(hub_gguf_variants, "get_gguf_variants_response", _variants)+ monkeypatch.setattr(hub_gguf_variants, "get_gguf_variants_answer", _answer)+ monkeypatch.setattr(hub_gguf_variants, "pinned_snapshot_for_request", lambda *_a, **_k: None)+ monkeypatch.setattr(account_access, "managed_account", lambda: False)+ reference = host_paths.cache_reference(REPO_DIR)+ assert reference != REPO_DIR++ answered = client(via_api_key = True).get(f"{route}?repo_id={reference}&local_path={reference}")+ assert answered.status_code == 200, answered.text+ assert seen["repo_id"] == REPO_DIR+ assert seen["local_path"] == REPO_DIR+ # And the answer does not hand the path back: a local listing copies its input into+ # `repo_id`, so resolving on the way in has to be matched by referencing on the way out.+ assert REPO_DIR not in answered.text, answered.text+ assert answered.json()["repo_id"] == reference++ # BOUNDARY. A browser session names the repo itself and it arrives as written.+ seen.clear()+ answered = client(via_api_key = False).get(f"{route}?repo_id=unsloth/Llama-3.2-1B-GGUF")+ assert answered.status_code == 200, answered.text+ assert seen["repo_id"] == "unsloth/Llama-3.2-1B-GGUF"+ assert seen["local_path"] is None+++def test_a_cache_row_pinned_to_a_snapshot_is_referenced_too(monkeypatch):+ pinned = f"{HOST_ROOT}/models--unsloth--Llama-3.2-1B-Instruct/snapshots/deadbeef"++ async def _models_response(hf_token = None):+ return {"cached": [_cached_row(load_id = pinned)], "scan_confirmed": True}++ monkeypatch.setattr(cache_inventory, "list_cached_models_response", _models_response)+ payload = _hub(via_api_key = True).get("/api/hub/cached-models").json()+ row = payload["cached"][0]+ assert row["repo_id"] == "unsloth/Llama-3.2-1B-Instruct"+ assert row["load_id"].startswith("ref:"), row["load_id"]+ assert HOST_ROOT not in json.dumps(payload), payload+ assert response_leaks_host_path(payload, [HOST_ROOT]) is None+ assert host_paths.resolve_host_path_reference(row["load_id"]) == pinned++ ui = _hub(via_api_key = False).get("/api/hub/cached-models").json()+ assert ui["cached"][0]["load_id"] == pinned+++def test_the_compat_scan_folder_add_does_not_answer_with_the_path(monkeypatch):+ created = {"id": 7, "path": f"{HOST_ROOT}/extra", "created_at": "2026-09-01"}+ monkeypatch.setattr(+ "storage.studio_db.add_scan_folder_with_status", lambda path: (created, False)+ )++ payload = _models(via_api_key = True).post("/api/models/scan-folders", json = {"path": "."})+ assert payload.status_code == 201, payload.text+ body = payload.json()+ assert body["id"] == 7, "the folder is still identified"+ assert response_leaks_host_path(body, [HOST_ROOT]) is None++ ui = _models(via_api_key = False).post("/api/models/scan-folders", json = {"path": "."})+ assert ui.json()["path"] == f"{HOST_ROOT}/extra"+++def test_the_handle_a_caller_sent_is_the_handle_it_gets_back():+ from models.inference import LoadRequest++ path = f"{HOST_ROOT}/my models/Llama-3.2-1B"+ reference = host_paths.cache_reference(path)+ assert LoadRequest(model_path = reference).model_path == path++ answer = {+ "status": "loaded",+ "model": path,+ "display_name": path,+ "inference": {"identifier": f"{path}/snapshots/abc"},+ "warnings": [f"could not read {path}/config.json"],+ "unrelated": "unsloth/Llama-3.2-1B",+ }+ restored = host_paths.restore_inventory_handles(answer)+ assert HOST_ROOT not in json.dumps(restored), restored+ assert restored["model"] == reference+ assert restored["display_name"] == reference+ assert restored["inference"]["identifier"] == f"{reference}/snapshots/abc"+ assert restored["warnings"] == [f"could not read {reference}/config.json"]+ assert restored["unrelated"] == "unsloth/Llama-3.2-1B"++ # A request that named a path DIRECTLY has no handle of the caller's to give back.+ named = {"model": f"{HOST_ROOT}/models/Llama-3.2-1B"}+ assert host_paths.restore_inventory_handles(named) == named+++def test_a_tuple_keeps_its_shape_through_both_walks():+ """A NamedTuple is rebuilt field by field, a plain tuple from the iterable. Deciding that by+ try/except is WRONG: `tuple(*["abc"])` raises nothing and spells the string out."""+ from collections import namedtuple++ Row = namedtuple("Row", "first second")+ One = namedtuple("One", "only")++ path = f"{HOST_ROOT}/models/Llama-3.2-1B"+ token = host_paths._request_handles.set({path: host_paths.cache_reference(path)})+ try:+ assert host_paths.restore_inventory_handles(("abc",)) == ("abc",)+ assert host_paths.restore_inventory_handles(("ab", "cd")) == ("ab", "cd")+ assert host_paths.restore_inventory_handles(One("abc")) == One("abc")+ assert host_paths.restore_inventory_handles(Row("ab", "cd")) == Row("ab", "cd")+ assert host_paths.restore_inventory_handles((path,)) == (host_paths.cache_reference(path),)+ finally:+ host_paths._request_handles.reset(token)++ # This walk runs while a route is already raising: a TypeError here replaces the refusal+ # the caller was being told about with a 500.+ detail = host_paths.raised_inventory_detail(+ Row(f"{HOST_ROOT}/a", f"{HOST_ROOT}/b"), via_api_key = True+ )+ assert isinstance(detail, Row), detail+ assert response_leaks_host_path(detail, [HOST_ROOT]) is None, detail+ assert host_paths.redact_inventory_error_detail(("abc",), via_api_key = True) == ("abc",)+ # The payload walk rebuilds one rather than raising on it.+ assert redact_host_paths({"rows": [Row("acme/x", 5)]}, via_api_key = True)["rows"][0] == Row(+ "acme/x", 5+ )+ assert host_paths.raised_inventory_detail(Row("ab", "cd"), via_api_key = False) == Row(+ "ab", "cd"+ ), "a UI session's detail was rebuilt into something else"+++def test_a_sibling_that_merely_starts_with_the_resolved_path_is_not_a_handle():+ """A substring swap turned `<root>/models/Llama-3.2-1B-private` into `ref:<digest>-private`:+ a handle resolving to nothing, and a value the redactor no longer reads as a path, so the+ rest of the layout rode out in it."""+ path = f"{HOST_ROOT}/models/Llama-3.2-1B"+ sibling = f"{path}-private"+ reference = host_paths.cache_reference(path)++ token = host_paths._request_handles.set({path: reference})+ try:+ restored = host_paths.restore_inventory_handles(+ {"active_model": sibling, "model_name": path, "detail": f"{sibling} is not loaded"}+ )+ assert restored["model_name"] == reference, "the path that WAS resolved still comes back"+ assert restored["active_model"] == sibling, restored+ assert restored["detail"] == f"{sibling} is not loaded", restored+ assert host_paths.resolve_host_path_reference(restored["active_model"]) is None++ answered = host_paths.redact_host_paths(restored, via_api_key = True)+ assert response_leaks_host_path(answered, [HOST_ROOT]) is None, answered+ assert host_paths.redact_host_paths(restored, via_api_key = False) == restored+ finally:+ host_paths._request_handles.reset(token)+++def _source(target: str) -> str:+ from routes import inference as inference_routes+ from routes import training_history as training_routes+ from routes import video as video_routes++ module, _, attribute = target.partition(".")+ holder = {+ "inference": inference_routes,+ "video": video_routes,+ "training": training_routes,+ "models": models_routes,+ }[module]+ return inspect.getsource(getattr(holder, attribute) if attribute else holder)+++# Wiring, invisible in a payload test because the payload is never built. A target marked+# `squeeze` is matched whitespace-insensitively, so a re-wrap is not read as a removal.+_WIRING = {+ ("inference", "squeeze"): (+ "restore_inventory_handles(task.result())",+ "_handle_restored_http_exception(exc)",+ "restore_inventory_handles(exc.detail)",+ "restore_inventory_handles(ValidateModelResponse(",+ "jsonable_encoder(restore_inventory_handles(payload))",+ "restore_inventory_handles(redact_native_paths(str(e)))",+ "raise _handle_restored_http_exception(http_error) from http_error",+ "detail = restore_inventory_handles(str(e))",+ ),+ ("inference", "exact"): (+ "model_name = restore_inventory_handles(model_name),",+ "redact_host_paths(DiffusionStatusResponse(",+ "authenticated_via_api_key",+ ),+ ("video", "exact"): ("redact_host_paths(VideoStatusResponse(", "authenticated_via_api_key"),+ ("training", "exact"): (+ 'TrainingRunListResponse(runs = runs, total = result["total"]),',+ "authenticated_via_api_key",+ ),+ ("models.get_model_config", "exact"): (+ "via_api_key: bool = Depends(authenticated_via_api_key)",+ "redact_host_paths(",+ "restore_inventory_handles(await asyncio.to_thread(_resolve, model_name))",+ "echo = (model_name,)",+ "resolve_inventory_handle(",+ ),+ ("models.scan_model_remote_code", "exact"): (+ "resolve_inventory_handle(",+ "restore_inventory_handles(",+ ),+ ("inference.load_diffusion_model", "exact"): (+ "except HTTPException",+ "raised_inventory_detail",+ ),+ ("video.load_video_model", "exact"): ("except HTTPException", "raised_inventory_detail"),+ ("inference.diffusion_load_progress", "exact"): (+ "authenticated_via_api_key",+ "redact_load_progress",+ ),+ ("video.video_load_progress", "exact"): ("authenticated_via_api_key", "redact_load_progress"),+}++[email protected](("target", "match"), list(_WIRING))+def test_the_routes_are_wired_to_the_redaction_they_depend_on(target, match):+ source = _source(target)+ squeeze = (lambda text: re.sub(r"\s+", "", text)) if match == "squeeze" else (lambda text: text)+ for needle in _WIRING[(target, match)]:+ assert squeeze(needle) in squeeze(source), (target, needle)+++def test_a_refusal_names_the_handle_the_caller_sent(monkeypatch):+ from routes import inference as inference_routes+ from models.inference import LoadRequest++ path = f"{HOST_ROOT}/my models/Broken-1B"+ reference = host_paths.cache_reference(path)+ assert LoadRequest(model_path = reference).model_path == path++ raised = HTTPException(status_code = 400, detail = f"Invalid model identifier: {path}")+ restored = inference_routes._handle_restored_http_exception(raised)+ assert restored.status_code == 400+ assert restored.detail == f"Invalid model identifier: {reference}"+ assert HOST_ROOT not in restored.detail++ untouched = HTTPException(status_code = 409, detail = "A model is already loading")+ assert inference_routes._handle_restored_http_exception(untouched) is untouched+++# Referenced rather than blanked, because nothing else in these answers names the model.+# REF means "resolves back to the row's path", KEPT "came back as it was".+REF = object()+KEPT = object()++_MODEL_PATH = f"{HOST_ROOT}/my models/Llama-3.2-1B"+_DATASET_PATH = "/home/operator/datasets/customer-transcripts.jsonl"+++def _assert_redacted(+ row,+ expect,+ *,+ inventory = False,+ kept_text = (),+ roots = (HOST_ROOT,),+):+ redact = redact_inventory_host_paths if inventory else redact_host_paths+ redacted = redact(row, via_api_key = True)+ body = json.dumps(redacted)+ for root in roots:+ assert root not in body, redacted+ for field, want in expect.items():+ if want is REF:+ assert host_paths.resolve_host_path_reference(redacted[field]) == row[field], field+ elif want is KEPT:+ assert redacted[field] == row[field], field+ else:+ assert redacted[field] == want, field+ for fragment in kept_text:+ assert fragment in body, redacted+ # BOUNDARY.+ assert redact(row, via_api_key = False) is row+ return redacted+++ ("row", "expect", "kwargs"),+ [+ (+ {"loaded": True, "repo_id": _MODEL_PATH, "device": "cuda"},+ {"repo_id": REF, "device": KEPT},+ {},+ ),+ (+ {"run_id": "abc", "model_name": _MODEL_PATH, "status": "completed"},+ {"model_name": REF, "status": KEPT},+ {},+ ),+ (+ {"repo_id": "unsloth/Llama-3.2-1B", "model_name": "unsloth/Llama-3.2-1B"},+ {"repo_id": KEPT, "model_name": KEPT},+ {},+ ),+ (+ {"id": "ref:whatever", "is_lora": True, "base_model": _MODEL_PATH},+ {"base_model": REF, "is_lora": KEPT},+ {},+ ),+ ({"base_model": "unsloth/Llama-3.2-1B"}, {"base_model": KEPT}, {}),+ (+ {+ "id": "run-1",+ "dataset_name": _DATASET_PATH,+ "local_datasets": [_DATASET_PATH],+ "model_name": "unsloth/Llama-3.2-1B",+ },+ {"dataset_name": REF, "model_name": KEPT},+ {"roots": ("/home/operator",)},+ ),+ ({"id": "run-2", "dataset_name": "unsloth/Radiology-mini"}, {"dataset_name": KEPT}, {}),+ (+ {+ "error_message": f"FileNotFoundError: no such file: {HOST_ROOT}/data/train.jsonl",+ "status": "error",+ },+ {"status": KEPT},+ {"kept_text": ("FileNotFoundError",)},+ ),+ (+ {+ "source": "models_dir",+ "id": f"{HOST_ROOT}/models/Llama-3.2-1B",+ "load_id": f"{HOST_ROOT}/models/Llama-3.2-1B",+ "repo_id": "unsloth/Llama-3.2-1B",+ },+ {"id": REF, "load_id": REF, "repo_id": KEPT},+ {"inventory": True},+ ),+ ],+ ids = (+ "loaded-status",+ "training-run",+ "hub-row",+ "lora-base",+ "hub-base",+ "local-dataset-name",+ "hub-dataset-name",+ "failure-message",+ "local-row",+ ),+)+def test_a_path_that_outlives_its_request_is_still_referenced(row, expect, kwargs):+ _assert_redacted(row, expect, **kwargs)+++def test_a_second_load_does_not_hand_back_the_resident_path(monkeypatch):+ import asyncio++ from models.inference import DiffusionStatusResponse+ from routes import inference as inference_routes++ resident = f"{HOST_ROOT}/my models/previous-image-model"+ resident_reference = host_paths.cache_reference(resident)++ async def _gated(request, subject, **kwargs):+ return DiffusionStatusResponse(+ **{"loaded": True, "repo_id": resident, "model_path": resident}+ )++ class _Request:+ model_path = "ref:whatever"+ base_repo = None++ monkeypatch.setattr(inference_routes, "load_diffusion_model_gated", _gated)++ def _loaded(subject, via_api_key):+ return json.dumps(+ jsonable(+ asyncio.run(+ inference_routes.load_diffusion_model(+ _Request(), current_subject = subject, via_api_key = via_api_key+ )+ )+ )+ )++ body = _loaded("api", True)+ assert HOST_ROOT not in body, body+ assert resident_reference in body, body+ assert host_paths.resolve_host_path_reference(resident_reference) == resident++ # BOUNDARY.+ assert resident in _loaded("browser", False)+++def test_every_route_that_answers_with_a_persisted_record_redacts():+ from routes import inference as inference_routes+ from routes import training_history as training_routes+ from routes import video as video_routes+ from routes import training as live_training_routes++ expected = {+ inference_routes: (+ "load_diffusion_model",+ "unload_diffusion_model",+ "diffusion_status",+ ),+ video_routes: ("load_video_model", "unload_video_model", "video_status"),+ training_routes: (+ "list_training_runs",+ "get_training_run_detail",+ "update_training_run",+ ),+ # The LIVE run, not only its history: the worker's own progress line quotes the model+ # it is loading, and for a local model that is an absolute path.+ live_training_routes: ("get_training_status",),+ }+ for module, names in expected.items():+ tree = ast.parse(inspect.getsource(module))+ found = {+ node.name: node+ for node in ast.walk(tree)+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))+ }+ for name in names:+ assert name in found, (module.__name__, name)+ body = ast.unparse(found[name])+ assert "redact_host_paths(" in body, (module.__name__, name)+ assert "authenticated_via_api_key" in body, (module.__name__, name)+++def test_a_live_training_status_does_not_quote_the_model_path():+ """Every field a worker writes free text into, not just the one that named the model: the+ warnings list quotes the file it is about, and the singular text fields do not reach it."""+ path = f"{HOST_ROOT}/my models/trained-from"+ status = {+ "job_id": "run-1",+ "phase": "training",+ "is_training_running": True,+ "message": f"Loading {path}...",+ "error": f"could not read {path}/config.json",+ "warnings": [f"missing {path}/tokenizer.json"],+ "details": {"model_name": path, "output_dir": f"{path}/out"},+ }+ redacted = host_paths.redact_host_paths(status, via_api_key = True)+ body = json.dumps(redacted)+ assert HOST_ROOT not in body, body+ assert host_paths.response_leaks_host_path(redacted, [HOST_ROOT]) is None, redacted+ assert redacted["job_id"] == "run-1" and redacted["phase"] == "training"+ assert host_paths.resolve_host_path_reference(redacted["details"]["model_name"]) == path++ # BOUNDARY. A browser session keeps every one of them.+ assert host_paths.redact_host_paths(status, via_api_key = False) is status+++def test_opening_or_renaming_a_run_does_not_hand_back_the_path(monkeypatch):+ import asyncio++ from routes import training_history as training_routes++ path = f"{HOST_ROOT}/my models/trained-from"+ reference = host_paths.cache_reference(path)+ row = {+ "id": "run-1",+ "run_id": "run-1",+ "model_name": path,+ "dataset_name": "some/dataset",+ "started_at": "2026-01-01T00:00:00Z",+ "status": "completed",+ "config_json": json.dumps({"model_name": path}),+ "output_dir": None,+ "display_name": None,+ }++ monkeypatch.setattr(training_routes, "get_run", lambda run_id: dict(row))+ monkeypatch.setattr(training_routes, "get_run_metrics", lambda run_id: {})+ monkeypatch.setattr(training_routes, "get_preview_sharing_enabled", lambda: False)++ async def _opened():+ return await training_routes.get_training_run_detail(+ "run-1", current_subject = "api", no_credential = False, via_api_key = True+ )++ async def _renamed():+ return await training_routes.update_training_run(+ "run-1",+ training_routes.TrainingRunUpdateRequest(),+ current_subject = "api",+ no_credential = False,+ via_api_key = True,+ )++ for answer in (_opened, _renamed):+ body = json.dumps(jsonable(asyncio.run(answer())))+ assert HOST_ROOT not in body, body+ assert reference in body, body+++def test_a_prepared_dataset_cache_still_counts_when_the_hub_copy_is_unusable(monkeypatch):+ import sys+ import types++ from hub.utils import hf_tokens++ cache_state = types.ModuleType("hub.utils.hf_cache_state")+ cache_state.iter_repo_cache_dirs = lambda repo_type, repo_id: iter(["a-directory"])+ cache_state.repo_cache_has_usable_snapshot = lambda repo_type, repo_id: False+ dataset_cache = types.ModuleType("hub.utils.dataset_cache")+ dataset_cache.latest_processed_dataset_cache_path = lambda repo_id: "/prepared/here"++ monkeypatch.setitem(sys.modules, "hub.utils.hf_cache_state", cache_state)+ monkeypatch.setitem(sys.modules, "hub.utils.dataset_cache", dataset_cache)++ assert hf_tokens._repo_present_on_disk("owner/ds", "dataset") is True+ assert hf_tokens._repo_present_on_disk("owner/model", "model") is False+ dataset_cache.latest_processed_dataset_cache_path = lambda repo_id: None+ assert hf_tokens._repo_present_on_disk("owner/ds", "dataset") is False++ def _raise(repo_type, repo_id):+ raise OSError("unreadable cache root")++ cache_state.iter_repo_cache_dirs = _raise+ dataset_cache.latest_processed_dataset_cache_path = lambda repo_id: "/prepared/here"+ assert hf_tokens._repo_present_on_disk("owner/ds", "dataset") is True+ assert hf_tokens._repo_present_on_disk("owner/model", "model") is False+++def test_the_metadata_route_resolves_and_answers_with_the_handle():+ path = f"{HOST_ROOT}/my models/Local-Model"+ reference = host_paths.cache_reference(path)++ from models.inference import resolve_inventory_handle++ token = host_paths._request_handles.set(None)+ try:+ assert resolve_inventory_handle(reference) == path+ answered = {"model": path, "detail": f"could not read {path}/config.json"}+ restored = host_paths.restore_inventory_handles(answered)+ assert restored["model"] == reference+ assert HOST_ROOT not in json.dumps(restored), restored+ assert resolve_inventory_handle("ref:not-one-of-ours") == "ref:not-one-of-ours"+ assert resolve_inventory_handle("unsloth/Llama-3.2-1B") == "unsloth/Llama-3.2-1B"+ finally:+ host_paths._request_handles.reset(token)+++def test_a_training_run_does_not_carry_the_output_layout():+ row = {+ "id": "run-1",+ "model_name": f"{HOST_ROOT}/my models/base",+ "output_dir": f"{HOST_ROOT}/outputs/run-1",+ "model_local_path": f"{HOST_ROOT}/cache/base",+ "model_snapshot_path": f"{HOST_ROOT}/cache/base/snapshots/abc",+ "dataset_local_path": f"{HOST_ROOT}/data/set",+ "dataset_snapshot_path": f"{HOST_ROOT}/data/set/snapshots/abc",+ "checkpoint_path": f"{HOST_ROOT}/outputs/run-1/checkpoint-10",+ "resume_from_checkpoint": f"{HOST_ROOT}/outputs/run-1/checkpoint-10",+ "output_dirs": [f"{HOST_ROOT}/outputs/run-1", f"{HOST_ROOT}/outputs/run-2"],+ "dataset_name": "acme/dataset",+ }+ # Resume fields are referenced, not blanked, or `can_resume` is true beside no identifier.+ _assert_redacted(+ row,+ {+ "output_dirs": [],+ "model_name": REF,+ "output_dir": REF,+ "checkpoint_path": REF,+ "resume_from_checkpoint": REF,+ # Referenced, not blanked: a blanked pin is falsy, and Resume then silently+ # continues from whatever revision is newest in the cache.+ "model_snapshot_path": REF,+ "dataset_snapshot_path": REF,+ "model_local_path": "",+ "dataset_local_path": "",+ "dataset_name": KEPT,+ },+ )+++def test_the_deferred_five_hundred_restores_the_handle_too():+ from routes import inference as inference_routes++ body = inspect.getsource(inference_routes._tunnel_safe_json)+ generic = body.index("failed after the response was committed")+ # Stop at the success branch, or its restoration would satisfy this assertion.+ tail = body[generic : body.index("else:", generic)]+ assert "_deferred_error_body(" in tail, tail+ assert "restore_inventory_handles(" in tail, tail++ path = f"{HOST_ROOT}/my models/Local-Model"+ reference = host_paths.cache_reference(path)+ token = host_paths._request_handles.set({path: reference})+ try:+ restored = host_paths.restore_inventory_handles(+ f"OSError: [Errno 13] Permission denied: '{path}/model.safetensors'"+ )+ assert HOST_ROOT not in restored, restored+ assert reference in restored, restored+ finally:+ host_paths._request_handles.reset(token)+++def test_the_persisted_training_request_keys_are_the_ones_redacted():+ from models.training import TrainingStartRequest++ declared = set(TrainingStartRequest.model_fields)+ covered = (+ host_paths.HOST_PATH_SCALAR_FIELDS+ | host_paths.HOST_PATH_LIST_FIELDS+ | host_paths.HOST_PATH_HANDLE_LIST_FIELDS+ )+ for field in ("local_datasets", "local_eval_datasets", "model_local_path"):+ assert field in declared, field+ assert field in covered, field++ config = {+ "local_datasets": [f"{HOST_ROOT}/data/train.jsonl"],+ "local_eval_datasets": [f"{HOST_ROOT}/data/eval.jsonl"],+ "tensorboard_dir": f"{HOST_ROOT}/outputs/run-1/runs",+ "learning_rate": 0.0002,+ }+ redacted = host_paths.redact_host_paths({"config": config}, via_api_key = True)+ assert HOST_ROOT not in json.dumps(redacted), redacted+ assert redacted["config"]["learning_rate"] == 0.0002+++def test_the_chat_status_does_not_hand_back_the_path_the_load_resolved(monkeypatch):+ import asyncio++ from models.inference import InferenceStatusResponse+ from routes import inference as inference_routes++ async def _payload(current_subject: str):+ return InferenceStatusResponse(+ active_model = REPO_DIR,+ model_identifier = REPO_DIR,+ is_gguf = True,+ is_local_model = True,+ loaded = [REPO_DIR],+ )++ monkeypatch.setattr(inference_routes, "get_status", _payload)+ answered = asyncio.run(+ inference_routes.inference_status(current_subject = "alice", via_api_key = True)+ )+ body = json.dumps(json.loads(InferenceStatusResponse(**dict(answered)).model_dump_json()))+ assert HOST_ROOT not in body, body+ assert REPO_DIR not in body, body++ ui = asyncio.run(inference_routes.inference_status(current_subject = "alice", via_api_key = False))+ assert ui.model_identifier == REPO_DIR+++def test_a_resumable_run_can_still_be_resumed_by_an_api_key_caller():+ from models.training import TrainingStartRequest++ output_dir = f"{HOST_ROOT}/outputs/run-1"+ detail = host_paths.redact_host_paths(+ {+ "id": "run-1",+ "can_resume": True,+ "output_dir": output_dir,+ "checkpoint_path": None,+ "local_datasets": [f"{HOST_ROOT}/datasets/train.jsonl"],+ "local_eval_datasets": [f"{HOST_ROOT}/datasets/eval.jsonl", "acme/hub-eval"],+ },+ via_api_key = True,+ )+ assert detail["can_resume"] is True+ handle = detail["output_dir"]+ assert handle.startswith("ref:"), detail+ assert HOST_ROOT not in json.dumps(detail), detail+ assert all(entry.startswith("ref:") for entry in detail["local_datasets"])+ assert detail["local_eval_datasets"][1] == "acme/hub-eval"++ replayed = TrainingStartRequest(+ model_name = "unsloth/Llama-3.2-1B",+ training_type = "LoRA/QLoRA",+ format_type = "alpaca",+ local_datasets = detail["local_datasets"],+ local_eval_datasets = detail["local_eval_datasets"],+ resume_from_checkpoint = handle,+ )+ assert replayed.resume_from_checkpoint == output_dir+ assert replayed.local_datasets == [f"{HOST_ROOT}/datasets/train.jsonl"]+ assert replayed.local_eval_datasets == [f"{HOST_ROOT}/datasets/eval.jsonl", "acme/hub-eval"]++ # A handle that was never issued resolves to nothing rather than somebody else's data.+ forged = "ref:" + "0" * 32+ unresumable = TrainingStartRequest(+ model_name = "unsloth/Llama-3.2-1B",+ training_type = "LoRA/QLoRA",+ format_type = "alpaca",+ local_datasets = [forged],+ )+ assert unresumable.local_datasets == [forged], "a forged handle resolved to a host path"+ assert unresumable.resume_from_checkpoint is None+++def test_a_listing_bigger_than_the_reference_table_still_resolves_its_own_rows():+ host_paths._reference_paths.clear()+ issued = [+ host_paths.cache_reference(f"/srv/models/row-{index}.gguf")+ for index in range(host_paths._REFERENCE_LIMIT + 2000)+ ]+ unresolved = [+ reference+ for reference in issued+ if host_paths.resolve_host_path_reference(reference) is None+ ]+ assert unresolved == [], len(unresolved)+++def test_the_table_is_still_bounded_at_its_ceiling(monkeypatch):+ host_paths._reference_paths.clear()+ monkeypatch.setattr(host_paths, "_REFERENCE_CEILING", host_paths._REFERENCE_LIMIT + 10)+ for index in range(host_paths._REFERENCE_LIMIT + 500):+ host_paths.cache_reference(f"/srv/ceiling/row-{index}.gguf")+ assert len(host_paths._reference_paths) <= host_paths._REFERENCE_CEILING + 1+++def test_a_failed_downloads_error_does_not_carry_the_cache_path(monkeypatch):+ from hub.schemas.downloads import DownloadJobStatus+ from hub.services.models import downloads as downloads_service++ async def _status(_repo_id, _variant = ""):+ return DownloadJobStatus(+ repo_id = "acme/model",+ state = "error",+ error = (+ "OSError: [Errno 28] No space left on device: "+ f"'{HOST_ROOT}/hub/models--acme--model/blobs/deadbeef.incomplete'"+ ),+ )++ monkeypatch.setattr(downloads_service, "get_download_status_response", _status)+ answered = _hub(True).get("/api/hub/download-status", params = {"repo_id": "acme/model"})+ assert answered.status_code == 200, answered.text+ body = answered.json()+ assert HOST_ROOT not in answered.text, body+ assert "No space left on device" in body["error"], body++ owner = _hub(False).get("/api/hub/download-status", params = {"repo_id": "acme/model"})+ assert HOST_ROOT in owner.text+++def test_a_caller_named_path_is_echoed_not_referenced():+ """Referencing an identifier the caller CHOSE turns the route into an online oracle; the+ per-process HMAC key defeats an offline attack and does nothing about this."""+ victim = f"{HOST_ROOT}/models--acme--private/snapshots/deadbeef"+ held = cache_reference(victim)++ def answer(caller_supplied):+ return redact_host_paths(+ {"id": caller_supplied, "model_name": caller_supplied, "config": {}},+ via_api_key = True,+ echo = (caller_supplied,),+ )++ for guess in (f"{HOST_ROOT}/models--acme--private/snapshots/beef", victim):+ out = answer(guess)+ assert out["id"] == guess, "the caller's own identifier came back altered"+ assert out["id"] != held, "the reference function answered for a caller-chosen string"++ assert redact_host_paths({"id": victim}, via_api_key = True)["id"] == held++ out = redact_host_paths(+ {"id": "acme/lora", "base_model": victim}, via_api_key = True, echo = ("acme/lora",)+ )+ assert out["base_model"] == held+ assert response_leaks_host_path(out, [HOST_ROOT]) is None+++# `base_repo` is usually a Hub id, but the local form is a host path, and the STATUS routes+# answer it to whoever polls rather than to the caller who supplied it.++BASE_DIR = f"{HOST_ROOT}/flux-base"+++def _media_status(cls):+ from models.inference import DiffusionStatusResponse, VideoStatusResponse+ return {"diffusion": DiffusionStatusResponse, "video": VideoStatusResponse}[cls]+++def _base_repo(answer):+ return answer.get("base_repo") if isinstance(answer, dict) else answer.base_repo++[email protected]("kind", ["diffusion", "video"])+def test_a_local_companion_base_is_referenced_not_returned(kind):+ """A row advertised as actionable has to be actionable: the handle the status route hands+ out must resolve back to the path on the load request that takes it."""+ from models.inference import DiffusionLoadRequest, VideoLoadRequest++ response = _media_status(kind)(loaded = True, repo_id = "unsloth/x", base_repo = BASE_DIR)++ api = redact_host_paths(response, via_api_key = True)+ handle = _base_repo(api)+ assert handle.startswith("ref:"), handle+ assert response_leaks_host_path(api, [HOST_ROOT, HOST_ROOT_NATIVE]) is None++ # BOUNDARY.+ assert _base_repo(redact_host_paths(response, via_api_key = False)) == BASE_DIR++ request_cls = {"diffusion": DiffusionLoadRequest, "video": VideoLoadRequest}[kind]+ assert request_cls(model_path = "unsloth/x", base_repo = handle).base_repo == BASE_DIR++[email protected]("kind", ["diffusion", "video"])+def test_a_hub_companion_base_is_not_a_path_and_is_left_alone(kind):+ """BOUNDARY, and the reason this field is decided by VALUE: referencing the ordinary repo+ id would make the field useless to every caller for nothing."""+ response = _media_status(kind)(+ loaded = True, repo_id = "unsloth/x", base_repo = "black-forest-labs/FLUX.2-klein-4B"+ )++ api = redact_host_paths(response, via_api_key = True)+ assert _base_repo(api) == "black-forest-labs/FLUX.2-klein-4B"+++def test_a_media_load_that_raises_redacts_the_path_it_resolved():+ """A load that RAISES skips the route's restore+redact wrappers, and the inner loader only+ redacted NATIVE paths, which do not know inventory handles."""+ from hub.utils.host_paths import raised_inventory_detail++ detail = f"[Errno 2] No such file or directory: '{REPO_DIR}/model.safetensors'"++ assert (+ response_leaks_host_path(+ {"detail": raised_inventory_detail(detail, via_api_key = True)},+ [HOST_ROOT, HOST_ROOT_NATIVE],+ )+ is None+ )+ # BOUNDARY.+ assert raised_inventory_detail(detail, via_api_key = False) == detail+++def test_a_media_load_progress_error_does_not_publish_the_resolved_path():+ """The worker stores `str(exc)`, and the progress routes answer with it on a LATER request,+ where the load's handle map is gone: the path has to be removed, not referenced."""+ from hub.utils.host_paths import redact_load_progress++ progress = {"phase": "error", "error": f"could not read {REPO_DIR}/model.safetensors"}++ redacted = redact_load_progress(progress, via_api_key = True)+ assert response_leaks_host_path(redacted, [HOST_ROOT, HOST_ROOT_NATIVE]) is None+ assert redacted["phase"] == "error", "the phase the client polls for was dropped"+ # BOUNDARY.+ assert redact_load_progress(progress, via_api_key = False) == progress+ assert redact_load_progress({"phase": "ready"}, via_api_key = True) == {"phase": "ready"}diff --git a/studio/backend/tests/test_diffusion_routes.py b/studio/backend/tests/test_diffusion_routes.pyindex efe468d1448..1db6196d001 100644--- a/studio/backend/tests/test_diffusion_routes.py+++ b/studio/backend/tests/test_diffusion_routes.py@@ -36,7 +36,7 @@ from fastapi.testclient import TestClientimport core.inference.diffusion as diffusion_moduleimport core.inference.gpu_arbiter as gpu_arbiterimport core.inference.image_gallery as gallery_module-from auth.authentication import get_current_subject+from auth.authentication import authenticated_via_api_key, get_current_subjectimport routes.inference as inference_routesfrom routes.inference import studio_router@@ -275,6 +275,8 @@ def client(monkeypatch, tmp_path):app = FastAPI()app.include_router(studio_router, prefix = "/api/inference")app.dependency_overrides[get_current_subject] = lambda: "test-user"+ # A browser session: the status routes redact host paths for an API-key caller.+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falsereturn TestClient(app)@@ -842,6 +844,8 @@ def test_a_cpu_mispredicted_engine_is_still_preflighted(monkeypatch):app = FastAPI()app.include_router(studio_router, prefix = "/api/inference")app.dependency_overrides[get_current_subject] = lambda: "test-user"+ # A browser session: the status routes redact host paths for an API-key caller.+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falselocal = TestClient(app)resp = local.post(@@ -914,6 +918,8 @@ def test_gated_pick_on_an_engine_switch_keeps_the_previous_model(monkeypatch, deapp = FastAPI()app.include_router(studio_router, prefix = "/api/inference")app.dependency_overrides[get_current_subject] = lambda: "test-user"+ # A browser session: the status routes redact host paths for an API-key caller.+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falselocal = TestClient(app)resp = local.post(@@ -1183,6 +1189,8 @@ def test_load_routes_to_sd_cpp_on_cpu(monkeypatch, tmp_path):app = FastAPI()app.include_router(studio_router, prefix = "/api/inference")app.dependency_overrides[get_current_subject] = lambda: "test-user"+ # A browser session: the status routes redact host paths for an API-key caller.+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falseclient = TestClient(app)resp = _post_load(client, model_path = "unsloth/Z-Image-Turbo-GGUF", gguf_filename = "z.gguf")diff --git a/studio/backend/tests/test_export_absolute_paths.py b/studio/backend/tests/test_export_absolute_paths.pyindex 71de56542fa..7d54c467ae4 100644--- a/studio/backend/tests/test_export_absolute_paths.py+++ b/studio/backend/tests/test_export_absolute_paths.py@@ -101,6 +101,8 @@ def _install_lightweight_backend_stubs(monkeypatch):# routes/models.py imports this alongside get_current_subject; a stub missing it# fails the import with "unknown location", which reads like a path problem.auth_mod.allow_ambient_hf_token = lambda: True+ # Same reason: the cached-model routes take the caller class.+ auth_mod.authenticated_via_api_key = lambda: Falsemonkeypatch.setitem(sys.modules, "auth", auth_pkg)monkeypatch.setitem(sys.modules, "auth.authentication", auth_mod)diff --git a/studio/backend/tests/test_hub_token_caller_identity.py b/studio/backend/tests/test_hub_token_caller_identity.pyindex a888de1aa96..38051b6af91 100644--- a/studio/backend/tests/test_hub_token_caller_identity.py+++ b/studio/backend/tests/test_hub_token_caller_identity.py@@ -517,9 +517,10 @@ def test_an_unreachable_probe_takes_the_short_ttl_and_spans_the_next_request(monassert probes["n"] == 1, "the unreachable answer did not survive to the next request"# Held under the SHORT ttl, not the denial one, and the constants must stay ordered so- # the memo cannot expire before the next request reaches it.+ # Memoized as UNKNOWN rather than as False: a stored denial the Hub never gave was handed to+ # every caller in the window. None re-resolves locally on each call inside it.(expiry, allowed) = next(iter(hf_tokens._repo_access_cache.values()))- assert allowed is False+ assert allowed is Noneassert expiry - time.monotonic() <= hf_tokens._REPO_ACCESS_UNREACHABLE_TTL_Sassert (hf_tokens._REPO_ACCESS_PROBE_TIMEOUT_S@@ -2557,7 +2558,11 @@ def test_the_scan_predicate_covers_every_config_the_scanner_reads(monkeypatch, cmonkeypatch.setattr("huggingface_hub.try_to_load_from_cache", _lookup)assert _scan_refused("acme/private") == 404- assert all(root == "/studio/cache" for _n, root in seen), "asked the wrong cache root"+ # Compared as paths, not as strings: on Windows the round trip through str() spells it+ # "\\studio\\cache". The claim is which root was asked about.+ assert all(+ Path(root) == Path("/studio/cache") for _n, root in seen+ ), "asked the wrong cache root"def test_the_template_predicate_asks_the_active_cache(monkeypatch):@@ -2594,7 +2599,9 @@ def test_the_template_predicate_asks_the_active_cache(monkeypatch):)assert picker_service.read_default_chat_template("org/private", "hf_dummy") is None- assert roots and all(r == "/studio/cache" for r in roots), "asked the wrong cache root"+ assert roots and all(+ Path(r) == Path("/studio/cache") for r in roots+ ), "asked the wrong cache root"assert not downloads, "a template cached in the active root was served to a denied caller"@@ -2622,3 +2629,35 @@ def test_a_local_only_config_read_stays_off_the_wire(monkeypatch):)assert probes["n"] == 0+++def test_a_slow_denial_is_still_a_denial(monkeypatch):+ """Elapsed time is not evidence about what the Hub said.++ Giving up is recognised by CLASS and by STATUS, and the budget covers the cold+ huggingface_hub import and a distant mirror as well as the request, so a definitive 401 can+ easily take all of it. Rewriting that to None discarded the refusal and left no remembered+ denial.+ """+ monkeypatch.setattr(hf_tokens, "_REPO_ACCESS_PROBE_TIMEOUT_S", 0.05)+ calls = _counting_probe(monkeypatch, False, offline = False, delay = 0.2)++ assert hf_tokens._explicit_token_reaches_repo("org/private", "hf_revoked", "model") is False+ assert calls["n"] == 1+ # Memoized as the denial it was, under the denial TTL rather than the short unreachable one,+ # and remembered, which is what survives a later unaskable Hub.+ (expiry, allowed) = next(iter(hf_tokens._repo_access_cache.values()))+ assert allowed is False+ assert expiry - time.monotonic() > hf_tokens._REPO_ACCESS_UNREACHABLE_TTL_S++ # And the refusal outlives an unaskable probe afterwards, the path the disclosure went+ # through. Asked of the AUTHORIZATION, not of the probe's tri-state: a denial is remembered+ # only where the disk fallback could overturn it, which needs a credential this host HOLDS+ # and the repo on this disk, else a caller naming absent repos fills the table and costs+ # everyone the offline fallback. `hf_revoked` is neither, so the probe reports "could not+ # ask" and `_resolve_unaskable` refuses; the caller-visible answer is the same no. The+ # remembering itself is pinned by test_offline_local_cache_discovery.py::+ # test_nothing_but_the_hub_overturns_a_hub_that_answered_no.+ hf_tokens._repo_access_cache.clear()+ _counting_probe(monkeypatch, None, offline = False)+ assert hf_tokens.cache_reads_authorized("hf_revoked", repo_id = "org/private") is Falsediff --git a/studio/backend/tests/test_media_auto_switch.py b/studio/backend/tests/test_media_auto_switch.pyindex 91a2a1cab2b..41aeaeed4ce 100644--- a/studio/backend/tests/test_media_auto_switch.py+++ b/studio/backend/tests/test_media_auto_switch.py@@ -30,7 +30,7 @@ import core.inference.media_switch_backends as backendsimport core.inference.media_switch_errors as errorsimport routes.models as models_routeimport utils.openai_auto_switch_settings as settings-from auth.authentication import get_current_subject+from auth.authentication import authenticated_via_api_key, get_current_subjectfrom core.inference.openai_auto_download import preferred_quantfrom utils.api_errors import install_api_error_handlersfrom routes.video import router as video_router@@ -428,6 +428,9 @@ def _client(router, prefix):install_api_error_handlers(app)app.include_router(router, prefix = prefix)app.dependency_overrides[get_current_subject] = lambda: "test-user"+ # The caller-class dependency resolves the same bearer scheme, so leaving it live makes a+ # bare app 401 before the route is reached. An operator session is what this file tests.+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falsereturn TestClient(app)diff --git a/studio/backend/tests/test_offline_local_cache_discovery.py b/studio/backend/tests/test_offline_local_cache_discovery.pynew file mode 100644index 00000000000..7977f1317cf--- /dev/null+++ b/studio/backend/tests/test_offline_local_cache_discovery.py@@ -0,0 +1,1760 @@+# 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 Hub that cannot be ASKED is not a Hub that said no (#10264 put a live /auth-check in front+of every disk-cache read). Only the list of cache ROOTS is redirected, so presence walks are real.+"""++from __future__ import annotations++import sys+from pathlib import Path+from types import SimpleNamespace++import pytest+import requests++sys.path.insert(0, str(Path(__file__).resolve().parent.parent))++import picker.service as picker_service+from hub.utils import dataset_cache, hf_cache_state, hf_tokens+from hub.utils.hf_tokens import (+ AmbientAuthorizedToken,+ cache_reads_authorized,+ cached_read_refused,+ public_cache_read_authorized,+ reset_repo_access_cache,+)+from routes import inference as inference_routes+from utils import transformers_version+from utils.models import model_config as model_config_module++# Captured before the autouse fixture replaces it, so the test ABOUT the reader reaches it.+_REAL_AMBIENT_HF_TOKEN = hf_tokens._ambient_hf_token+_REAL_SAVED_STUDIO_HF_TOKEN = hf_tokens._saved_studio_hf_token+_REAL_HOST_CREDENTIAL_IDENTITIES = hf_tokens._host_credential_identities++ON_DISK = "acme/downloaded-model"+ABSENT = "acme/never-downloaded"+OPERATOR_TOKEN = "hf_operator_ambient_token"+# Saved through Studio Settings: `get_token()` never returns it; the UI replays it per request.+STUDIO_UI_TOKEN = "hf_saved_in_studio_settings"+FOREIGN_TOKEN = "hf_some_other_callers_token"+HOST_CREDENTIAL = "hf_the_operators_own_credential"+ONE_OFF = "hf_a_one_off_someone_else_sent"+ROTATED_AWAY = "hf_the_credential_this_host_used_to_hold"+REVISION = "a" * 40++# Asks through a different entry point than a credentialed caller; `_reads`/`_refused` route it.+ANON = SimpleNamespace(caller = "no credential at all")+++class _ProbeUnreachable(Exception):+ """Stands in for a refused connection: not an answer, so the fallback applies."""++[email protected](autouse = True)+def _isolate_repo_access_cache():+ reset_repo_access_cache()+ yield+ reset_repo_access_cache()++[email protected](autouse = True)+def _no_credential_ledger(monkeypatch):+ """An install whose ledger predates it, which is what most hosts are on upgrade."""+ monkeypatch.setattr(hf_tokens, "_host_credential_identities", lambda: {})++[email protected](autouse = True)+def _host_credential(monkeypatch):+ monkeypatch.setattr(hf_tokens, "_ambient_hf_token", lambda: (True, OPERATOR_TOKEN))++[email protected](autouse = True)+def _saved_studio_credential(monkeypatch):+ monkeypatch.setattr(hf_tokens, "_saved_studio_hf_token", lambda: (True, None))+++_HF_STUBS = {+ "ambient": "_ambient_hf_token",+ "saved": "_saved_studio_hf_token",+ "credentials": "_host_hf_credentials",+ "identities": "_host_credential_identities",+ "recorded": "_recorded_request_token_repos",+ "present": "_repo_present_on_disk",+ "no_other_held": "_no_other_credential_ever_held",+ "filled_by_caller": "_caller_populated_the_cache",+ "fetched_with_request_token": "_repo_was_fetched_with_a_request_token",+ "unrecorded": "_unrecorded_fetches",+ "as_owner": "_as_owner",+ "denial_max": "_DENIAL_MEMORY_MAX",+ "noted_identities": "_noted_credential_identities",+}+_HF_PLAIN = ("unrecorded", "denial_max", "noted_identities")+++def _hf_state(monkeypatch, **state) -> None:+ """A reader's argument is the VALUE it should answer with, or a callable installed as it is."""+ for key, value in state.items():+ plain = key in _HF_PLAIN or callable(value)+ stub = value if plain else (lambda *_a, _v = value, **_k: _v)+ monkeypatch.setattr(hf_tokens, _HF_STUBS[key], stub)+++def _no_host_credential(monkeypatch):+ _hf_state(monkeypatch, ambient = (True, None))+++def _reads(caller, repo, **kw) -> bool:+ """The same question through the two entry points a credentialed and an ANON caller use."""+ if caller is ANON:+ return public_cache_read_authorized(repo_id = repo, **kw)+ return cache_reads_authorized(caller, repo_id = repo, **kw)+++def _refused(caller, repo, **kw) -> bool:+ """The mirror of `_reads` that the route guards actually call."""+ caller = False if caller is ANON else caller+ return cached_read_refused(caller, repo_id = repo, is_cached = lambda: True, **kw)+++def _key(repo: str):+ return hf_tokens._request_token_repo_key(repo, "model")+++def _filled_by(token, repo: str) -> bool:+ return hf_tokens._caller_populated_the_cache(token, repo_id = repo, repo_type = "model")+++def _noted(token, repo: str) -> None:+ hf_tokens.note_repo_fetched_with_a_request_token(token, repo, "model")+++def _fetched_with_a_token(repo: str):+ return hf_tokens._repo_was_fetched_with_a_request_token(repo, "model")+++def _materialize_repo(root: Path, repo_id: str) -> Path:+ repo_dir = root / f"models--{repo_id.replace('/', '--')}"+ snapshot = repo_dir / "snapshots" / REVISION+ snapshot.mkdir(parents = True, exist_ok = True)+ (repo_dir / "refs").mkdir(parents = True, exist_ok = True)+ (repo_dir / "refs" / "main").write_text(REVISION, encoding = "utf-8")+ (snapshot / "config.json").write_text('{"model_type": "llama"}', encoding = "utf-8")+ (snapshot / "model.safetensors").write_text("x", encoding = "utf-8")+ return snapshot+++def _cached_repo_dir(monkeypatch, tmp_path: Path, repo: str, **blobs: int) -> Path:+ """A real cache directory for `repo` holding `blobs` (name -> size); the reader walks it."""+ repo_dir = tmp_path / f"models--{repo.replace('/', '--')}"+ (repo_dir / "blobs").mkdir(parents = True)+ for name, size in blobs.items():+ (repo_dir / "blobs" / name).write_bytes(b"x" * size)+ monkeypatch.setattr(+ hf_cache_state,+ "iter_repo_cache_dirs",+ lambda repo_type, repo_id, **kw: iter([repo_dir]),+ )+ return repo_dir+++def _cache_root(monkeypatch, tmp_path: Path) -> Path:+ root = tmp_path / "hub"+ root.mkdir(parents = True, exist_ok = True)+ monkeypatch.setattr(hf_cache_state, "hf_cache_roots", lambda scan_errors = None: [root])+ return root+++def cache_root(monkeypatch, tmp_path) -> Path:+ """Only the ROOT LIST is redirected, so every presence walk under it runs for real."""+ return _cache_root(monkeypatch, tmp_path)+++def on_disk(cache_root) -> Path:+ """`cache_root` with ON_DISK fully materialised."""+ _materialize_repo(cache_root, ON_DISK)+ return cache_root+++def recorded_fetches(monkeypatch) -> list[tuple[str, str]]:+ """(repo, token) pairs: the token too, since the right repo against the wrong caller leaks."""+ seen: list[tuple[str, str]] = []+ monkeypatch.setattr(+ inference_routes,+ "_note_load_fetched_with_a_request_token",+ lambda ref, token: seen.append((ref, token)),+ )+ return seen+++def writes(monkeypatch) -> list:+ """Arguments captured whole: the right repo under the wrong key or arity is still bad."""+ seen: list = []+ _hf_state(+ monkeypatch,+ credentials = (True, ("hf_theoperators",)),+ as_owner = lambda call, *a, **k: seen.append(a),+ )+ return seen+++class _LiveProvenance:+ """Read-only view of the provenance map as the writer actually left it."""++ def __getitem__(self, key):+ return (hf_tokens._recorded_request_token_repos() or {})[key]++ def get(+ self,+ key,+ default = None,+ ):+ return (hf_tokens._recorded_request_token_repos() or {}).get(key, default)++ def __contains__(self, key):+ return key in (hf_tokens._recorded_request_token_repos() or {})++ def __len__(self):+ return len(hf_tokens._recorded_request_token_repos() or {})+++def provenance(monkeypatch) -> _LiveProvenance:+ """The REAL setting store, under this test's own studio home (conftest isolates it).++ Not an in-memory stand-in any more: the first-writer rule and the collapse to an ambiguous+ `by` now happen inside the write's own transaction, because two requests holding different+ credentials for the same uncached repo otherwise both read "absent" and the second stores+ its own identity over the first. A fake `as_owner` that just assigns would be the test+ re-implementing the thing under test, and would pass however the race goes."""+ _hf_state(+ monkeypatch,+ credentials = (True, (HOST_CREDENTIAL,)),+ no_other_held = True,+ present = True,+ )+ return _LiveProvenance()+++def _counting_probe(+ monkeypatch,+ verdict,+ *,+ offline: bool = False,+) -> dict:+ calls = {"n": 0}++ def _probe(*_a, **_k):+ calls["n"] += 1+ if isinstance(verdict, BaseException):+ raise verdict+ return verdict++ monkeypatch.setattr(hf_tokens, "_probe_repo_access", _probe)+ monkeypatch.setattr(hf_tokens, "_hub_offline", lambda: offline)+ return calls+++def _probe_against(monkeypatch, response_factory):+ class _Session:+ def get(self, url, **_k):+ return response_factory(url)++ monkeypatch.setattr("huggingface_hub.utils.get_session", lambda: _Session())+ monkeypatch.setattr(hf_tokens, "_hub_offline", lambda: False)+++def _response(status: int, error_code: str | None, url: str | None):+ headers = {"X-Error-Code": error_code} if error_code else {}+ return SimpleNamespace(+ status_code = status,+ headers = headers,+ url = url,+ raise_for_status = _raiser(status, url),+ )+++def _raiser(status: int, url):+ def _raise():+ if status < 400:+ return+ import httpx++ request = httpx.Request("GET", url or "http://mirror.invalid/x")+ raise httpx.HTTPStatusError(+ f"{status}",+ request = request,+ response = httpx.Response(status, request = request),+ )++ return _raise+++UNASKABLE = [+ ("env-offline", True, True, {}),+ ("caller-offline", True, False, {"offline": True}),+ ("unreachable", requests.exceptions.ConnectionError("refused"), False, {}),+ ("timeout", requests.exceptions.ReadTimeout("slow"), False, {}),+]+++ ("verdict", "hub_offline", "call_kw"),+ [case[1:] for case in UNASKABLE],+ ids = [case[0] for case in UNASKABLE],+)+def test_an_unaskable_hub_resolves_against_the_disk_and_only_for_its_own_caller(+ monkeypatch, on_disk, verdict, hub_offline, call_kw+):+ probes = _counting_probe(monkeypatch, verdict, offline = hub_offline)++ assert _reads(OPERATOR_TOKEN, ON_DISK, **call_kw) is True+ if hub_offline or call_kw.get("offline"):+ assert probes["n"] == 0, "the offline answer went to the network"++ # BOUNDARY. On this disk is a fact about the OPERATOR, not about whoever is asking.+ assert _reads(FOREIGN_TOKEN, ON_DISK, **call_kw) is False+ assert _refused(FOREIGN_TOKEN, ON_DISK, **call_kw) is True++ # BOUNDARY. Not on this disk answers nothing, so there is nothing to fall back to.+ assert _reads(OPERATOR_TOKEN, ABSENT, **call_kw) is False+ assert _reads(ANON, ABSENT, **call_kw) is False+++def test_an_interrupted_download_is_not_a_repo_on_disk(monkeypatch, tmp_path):+ """BOUNDARY. A cancelled download leaves an empty revision, which answers nothing."""+ root = _cache_root(monkeypatch, tmp_path)+ (root / f"models--{ON_DISK.replace('/', '--')}" / "snapshots" / REVISION).mkdir(parents = True)+ _counting_probe(monkeypatch, True, offline = True)++ assert _reads(OPERATOR_TOKEN, ON_DISK) is False+++# The two credential stores, as the `(readable, token)` pair each reader returns.+HOST_STORES = {+ "ambient": ((True, OPERATOR_TOKEN), (True, None)),+ "saved-in-studio": ((True, None), (True, STUDIO_UI_TOKEN)),+ "both-the-same": ((True, OPERATOR_TOKEN), (True, OPERATOR_TOKEN)),+ "two-different": ((True, OPERATOR_TOKEN), (True, STUDIO_UI_TOKEN)),+ "none": ((True, None), (True, None)),+ "unreadable-saved": ((True, OPERATOR_TOKEN), (False, None)),+ "unreadable-saved-only": ((True, None), (False, None)),+}+++ ("stores", "caller", "authorized"),+ [+ ("ambient", OPERATOR_TOKEN, True),+ ("ambient", FOREIGN_TOKEN, False),+ ("ambient", ANON, False),+ # The UI's ambient marker and a bare None are the host itself asking, never probe-gated.+ ("ambient", AmbientAuthorizedToken("hf_ui"), True),+ ("ambient", None, True),+ ("none", ANON, True),+ ("saved-in-studio", STUDIO_UI_TOKEN, True),+ ("saved-in-studio", FOREIGN_TOKEN, False),+ ("saved-in-studio", ANON, False),+ ("both-the-same", OPERATOR_TOKEN, True),+ ("both-the-same", FOREIGN_TOKEN, False),+ # BOUNDARY. Either of two DIFFERENT host credentials could have filled this cache.+ ("two-different", OPERATOR_TOKEN, False),+ ("two-different", STUDIO_UI_TOKEN, False),+ # BOUNDARY. "Cannot be read" is not "empty", so an unreadable store authorizes nobody.+ ("unreadable-saved", OPERATOR_TOKEN, False),+ ("unreadable-saved", ANON, False),+ ("unreadable-saved-only", ANON, False),+ ("unreadable-saved-only", OPERATOR_TOKEN, False),+ ],+)+def test_offline_only_the_caller_whose_credential_filled_the_cache_may_read_it(+ monkeypatch, on_disk, stores, caller, authorized+):+ ambient, saved = HOST_STORES[stores]+ _hf_state(monkeypatch, ambient = ambient, saved = saved)+ probes = _counting_probe(monkeypatch, True, offline = True)++ assert _reads(caller, ON_DISK) is authorized+ assert probes["n"] == 0, "the offline answer went to the network"+ if caller is ANON or isinstance(caller, str):+ assert _refused(caller, ON_DISK) is not authorized+++def test_an_unreachable_probe_is_memoized_as_unknown_not_as_a_denial(monkeypatch, cache_root):+ probes = _counting_probe(+ monkeypatch, requests.exceptions.ConnectionError("refused"), offline = False+ )++ assert _reads(OPERATOR_TOKEN, ON_DISK) is False+ assert [v for _expiry, v in hf_tokens._repo_access_cache.values()] == [None]++ # The repo arrives only now, so the second call proves the memo was reconsulted, not reprobed.+ _materialize_repo(cache_root, ON_DISK)+ assert _reads(OPERATOR_TOKEN, ON_DISK) is True+ assert probes["n"] == 1, "the memo did not spare the second caller the dead round trip"+++ ("overturn", "still_refused"),+ [+ ("nothing", True),+ # The verdict cache is what expires; the denial is what does not.+ ("an-outage", True),+ # An expiry made the boundary defeatable with the one input the caller controls.+ ("the-clock", True),+ ("the-hub-saying-yes", False),+ ],+)+def test_nothing_but_the_hub_overturns_a_hub_that_answered_no(+ monkeypatch, on_disk, overturn, still_refused+):+ _counting_probe(monkeypatch, False, offline = False)+ assert _reads(OPERATOR_TOKEN, ON_DISK) is False+ assert _refused(OPERATOR_TOKEN, ON_DISK) is True+ assert len(hf_tokens._denied_repo_access) == 1++ if overturn == "nothing":+ reset_repo_access_cache()+ assert not hf_tokens._denied_repo_access, "the reset left the denial behind"+ return++ if overturn == "the-hub-saying-yes":+ hf_tokens._repo_access_cache.clear()+ _counting_probe(monkeypatch, True, offline = False)+ assert _reads(OPERATOR_TOKEN, ON_DISK) is True++ if overturn == "the-clock":+ later = hf_tokens.time.monotonic() + 86_400.0+ monkeypatch.setattr(hf_tokens, "time", SimpleNamespace(monotonic = lambda: later))+ else:+ hf_tokens._repo_access_cache.clear()++ _counting_probe(monkeypatch, _ProbeUnreachable(), offline = False)+ assert _reads(OPERATOR_TOKEN, ON_DISK) is not still_refused, "an outage overturned the Hub"+ if still_refused:+ assert _refused(OPERATOR_TOKEN, ON_DISK) is True+++def test_a_repo_fetched_with_a_one_off_token_is_not_served_to_a_tokenless_caller(+ monkeypatch, on_disk+):+ _counting_probe(monkeypatch, requests.exceptions.ConnectionError("refused"), offline = False)+ recorded: dict = {}+ _hf_state(+ monkeypatch,+ credentials = (True, ()),+ fetched_with_request_token = lambda repo, kind: recorded.get(_key(repo), False),+ )+ assert _reads(ANON, ON_DISK) is True++ recorded[_key(ON_DISK)] = True+ hf_tokens._repo_access_cache.clear()+ assert _reads(ANON, ON_DISK) is False, "a one-off token's repo went to a tokenless caller"+ _materialize_repo(on_disk, "acme/other")+ hf_tokens._repo_access_cache.clear()+ assert _reads(ANON, "acme/other") is True++ _hf_state(monkeypatch, fetched_with_request_token = None)+ hf_tokens._repo_access_cache.clear()+ assert _reads(ANON, "acme/other") is False+++def test_the_provenance_key_is_case_folded_like_every_other_repo_lookup():+ assert _key("Org/Private") == hf_tokens._request_token_repo_key("org/private", "MODEL")+ assert hf_tokens._request_token_repo_key("org/private", "dataset") != _key("org/private")+++def _source(target) -> str:+ import inspect+ return inspect.getsource(target)+++def _fetch_sites():+ """Where a fetch under a request token is recorded, and what has to be true of each site.++ Asserted against the code rather than by driving every route, because the thing being+ pinned is ORDER: the record has to exist before the bytes can move and after anything+ cheap can still refuse, and no test that only drives the happy path can see that.+ """+ from hub.services import download_lifecycle+ from hub.services.datasets import formatting+ from hub.services.models import downloads+ from picker import service as picker_module+ from routes import video as video_routes++ text_load = _source(inference_routes._load_model_impl)+ media_loads = {+ "diffusion-load": _source(inference_routes.load_diffusion_model_gated),+ "video-load": _source(video_routes.load_video_model_gated),+ }+ preview = _source(formatting.check_format_response)+ picker = _source(picker_module)+ config_read = _source(model_config_module)+ return {+ "text-load": text_load,+ **media_loads,+ "chat-route": _source(inference_routes.load_model_gated),+ "download-service": _source(downloads),+ "download-lifecycle": _source(download_lifecycle),+ "dataset-preview": preview,+ "picker-template": picker,+ "config-read": config_read,+ }+++_SITES = _fetch_sites()++# A text load compares the cache before and after, since its fetch has happened by then; a media+# route cannot, because `begin_load` returns before the worker moves a byte.+_PRESENT = [+ (+ "text-load",+ "_note_load_fetched_with_a_request_token(request.model_path",+ "_load_fetched_bytes(",+ ),+ (+ "diffusion-load",+ "_repo_is_in_the_hub_cache(ref) is not True",+ "_note_load_fetched_with_a_request_token(_ref",+ ),+ (+ "video-load",+ "_repo_is_in_the_hub_cache(ref) is not True",+ "_note_load_fetched_with_a_request_token(_ref",+ ),+ # Each of these WRAPS its fetch rather than writing beside it: the record has to exist+ # before the call, because a download that dies half way has still filled the cache, and it+ # has to be taken back when the call raised having left nothing.+ ("download-lifecycle", "note_repo_fetched_with_a_request_token(hf_token, repo_id, repo_type)"),+ (+ "dataset-preview",+ "recording_a_request_token_fetch(\n hf_token, request.dataset_name",+ ),+ ("picker-template", "recording_a_request_token_fetch(hf_token, resolved"),+ ("config-read", "recording_a_request_token_fetch(token, model_name"),+ # And only where the call can actually fetch: both of these resolve an already-cached file+ # without asking the Hub, and recording that marks a repo the cache may have held+ # anonymously, which withholds it from the tokenless offline caller.+ ("config-read", "not _config_json_already_cached(model_name, revision)"),+ ("picker-template", "if not _this_file_was_already_here(rel)"),+]++_ABSENT = [+ (+ "chat-route",+ "_note_load_fetched_with_a_request_token",+ "the route records again, before the load is admitted",+ ),+ (+ "download-service",+ "note_repo_fetched_with_a_request_token",+ "the download service records what its caller already did",+ ),+ (+ "diffusion-load",+ "_load_fetched_bytes",+ "a media route cannot compare before and after: its fetch has not happened yet",+ ),+ (+ "video-load",+ "_load_fetched_bytes",+ "a media route cannot compare before and after: its fetch has not happened yet",+ ),+]++# Read as "this marker must appear before the next one".+_ORDERED = [+ (+ "diffusion-load",+ "the media record lands after the 400-able validation and before the launch",+ [+ "_repo_is_in_the_hub_cache(ref) is not True",+ "validate_load_request",+ "_note_load_fetched_with_a_request_token(_ref",+ "status_dict = await asyncio.to_thread(",+ ],+ ),+ (+ "video-load",+ "the media record lands after the 400-able validation and before the launch",+ [+ "_repo_is_in_the_hub_cache(ref) is not True",+ "validate_load_request",+ "_note_load_fetched_with_a_request_token(_ref",+ "status_dict = await asyncio.to_thread(",+ ],+ ),+ # INSIDE the admitted start callback, not on the way to it: the busy guard 409s, the arbiter+ # refuses and the retirement check rejects a tombstoned account, none of them starting a+ # worker, and a record any of them leaves behind withholds a repo nobody fetched.+ (+ "diffusion-load",+ "the media record is inside the callback the load is admitted through",+ ["def _start_", "_note_load_fetched_with_a_request_token(_ref", ".begin_load("],+ ),+ (+ "video-load",+ "the media record is inside the callback the load is admitted through",+ ["def _start_", "_note_load_fetched_with_a_request_token(_ref", ".begin_load("],+ ),+ (+ "download-lifecycle",+ "the fetch was recorded only after making it",+ ["note_repo_fetched_with_a_request_token", "proc = spawn()"],+ ),+ (+ "picker-template",+ "the fetch was recorded only after making it",+ ["recording_a_request_token_fetch", "path = hf_hub_download("],+ ),+ (+ "config-read",+ "the fetch was recorded only after making it",+ ["recording_a_request_token_fetch", "AutoConfig.from_pretrained("],+ ),+ # A cache-only preview reads the cache or 404s without a round trip, and `list_repo_files`+ # caches nothing, so a 404 there used to leave a record for a fetch that never happened.+ (+ "dataset-preview",+ "a cache-only preview records a fetch it never made",+ ["_LOCAL_CACHE_MISS_ERROR_CODE", "recording_a_request_token_fetch"],+ ),+ (+ "dataset-preview",+ "a failed listing records a fetch that never started",+ ["list_repo_files", "recording_a_request_token_fetch", "load_dataset(**load_kwargs)"],+ ),+]+++ ("site", "markers"),+ [(site, markers) for site, *markers in _PRESENT],+ ids = [f"{site}:{markers[0][:38]}" for site, *markers in _PRESENT],+)+def test_a_fetch_site_records_what_it_is_supposed_to(site, markers):+ for marker in markers:+ assert marker in _SITES[site], (site, marker)+++ ("site", "marker", "why"),+ _ABSENT,+ ids = [f"{site}:{marker[:38]}" for site, marker, _why in _ABSENT],+)+def test_a_site_that_must_not_record_does_not(site, marker, why):+ assert marker not in _SITES[site], why+++ ("site", "why", "markers"),+ _ORDERED,+ ids = [f"{site}:{why[:44]}" for site, why, _markers in _ORDERED],+)+def test_the_record_sits_between_the_last_refusal_and_the_first_byte(site, why, markers):+ source = _SITES[site]+ found = [source.index(marker) for marker in markers]+ assert found == sorted(found), (why, dict(zip(markers, found)))+++def test_a_text_load_records_in_its_finally_and_the_preview_records_at_both_tiers():+ """The two contracts that are counts rather than order: the text load's record is in the+ `finally`, so a load that failed part way still records the bytes it pulled, and the preview+ has one per download tier, since tier 2 reaches the network whether or not tier 1 ran."""+ text_load = _SITES["text-load"]+ assert text_load.rindex("finally:") < text_load.index("_note_load_fetched_with_a_request_token")+ assert _SITES["dataset-preview"].count("recording_a_request_token_fetch") == 2+++def live_ledger(monkeypatch):+ """The REAL ledger under this test's own studio home, since what is under test is whether+ anything writes to it at all."""+ monkeypatch.setattr(hf_tokens, "_host_credential_identities", _REAL_HOST_CREDENTIAL_IDENTITIES)+ hf_tokens._noted_credential_identities.clear()+ yield lambda: _REAL_HOST_CREDENTIAL_IDENTITIES() or {}+ hf_tokens._noted_credential_identities.clear()+++def test_a_credential_is_entered_in_the_ledger_when_it_is_saved_and_when_it_goes(live_ledger):+ """On an upgraded install the ledger starts empty and used to be written only where an+ unaskable probe happened to read it, so an operator who replaced or cleared the saved token+ first left no trace of the old one."""+ from storage import credential_secrets++ assert live_ledger() == {}++ credential_secrets.save_hf_token(HOST_CREDENTIAL)+ assert hf_tokens._credential_identity(HOST_CREDENTIAL) in live_ledger()++ # Replaced: BOTH are in it afterwards, or the successor inherits the predecessor's downloads.+ credential_secrets.save_hf_token(ROTATED_AWAY)+ assert {+ hf_tokens._credential_identity(HOST_CREDENTIAL),+ hf_tokens._credential_identity(ROTATED_AWAY),+ } <= set(live_ledger())++ credential_secrets.delete_hf_token()+ assert hf_tokens._credential_identity(ROTATED_AWAY) in live_ledger()+++def test_a_credential_that_cannot_be_decrypted_is_still_a_credential_this_host_held(+ monkeypatch, live_ledger+):+ from storage import credential_secrets++ monkeypatch.setattr(credential_secrets, "get_hf_token", lambda: None)+ monkeypatch.setattr(credential_secrets, "hf_token_row_exists", lambda: True)++ credential_secrets.delete_hf_token()++ ledger = live_ledger()+ assert ledger, "an unreadable credential left the ledger empty, which reads as 'never held'"+ assert hf_tokens._no_other_credential_ever_held(()) is False+++def test_a_tokenless_caller_does_not_inherit_a_credential_that_has_since_been_removed(+ monkeypatch, tmp_path, live_ledger+):+ """The whole point of the ledger, through the gate that consults it: the repo is on disk,+ nothing recorded WHICH credential fetched it (a cache older than the record), and the host+ holds nothing now. Whether that cache is the operator's own or a removed credential's is+ exactly what the ledger answers."""+ from storage import credential_secrets++ _no_host_credential(monkeypatch)+ _hf_state(monkeypatch, recorded = {}, present = True)+ root = _cache_root(monkeypatch, tmp_path)+ _materialize_repo(root, ON_DISK)++ # Nothing was ever held: the tokenless caller may read its own cache.+ assert _filled_by(None, ON_DISK) is True++ # The operator had a token and cleared it, with no probe in between.+ credential_secrets.save_hf_token(HOST_CREDENTIAL)+ credential_secrets.delete_hf_token()++ assert _filled_by(None, ON_DISK) is False, "a removed credential's downloads were inherited"+ assert hf_tokens._resolve_unaskable(ON_DISK, "model", token = None) is False+++def test_every_credentialed_fetch_is_recorded_not_only_a_foreign_one(monkeypatch, writes):+ def written():+ return [call for call in writes if len(call) == 3]++ _noted(False, "acme/public")+ assert written() == [], "an anonymous fetch says nothing about anybody"++ _noted("hf_theoperators", "acme/saved")+ _noted(None, "acme/ambient")+ assert [call[1] for call in written()] == [_key("acme/saved"), _key("acme/ambient")]++ _noted("hf_someoneelses", "acme/private")+ assert written()[-1][1] == _key("acme/private")++ writes.clear()+ _hf_state(monkeypatch, credentials = (True, ()))+ _noted(None, "acme/public2")+ assert written() == []+++def test_a_deleted_credential_does_not_reopen_the_cache(monkeypatch):+ _hf_state(+ monkeypatch,+ ambient = (True, None),+ saved = (True, None),+ present = True,+ recorded = {_key("acme/private"): {"at": 0}},+ )+ hf_tokens.reset_repo_access_cache()++ assert not _reads(ANON, "acme/private", offline = True)+ assert _reads(ANON, "acme/public", offline = True)+++def test_a_repo_that_was_never_refused_still_resolves_against_the_disk(monkeypatch, on_disk):+ _materialize_repo(on_disk, "acme/other")+ _counting_probe(monkeypatch, False, offline = False)+ assert _reads(OPERATOR_TOKEN, ON_DISK) is False++ hf_tokens._repo_access_cache.clear()+ _counting_probe(monkeypatch, requests.exceptions.ConnectionError("refused"), offline = False)+ assert _reads(OPERATOR_TOKEN, "acme/other") is True+ assert _reads(FOREIGN_TOKEN, ON_DISK) is False, "a second principal inherited that cache"+++def test_a_refusal_the_table_had_to_forget_closes_the_outage_path(monkeypatch, on_disk):+ _hf_state(monkeypatch, denial_max = 4)+ _counting_probe(monkeypatch, False, offline = False)+ assert _reads(OPERATOR_TOKEN, ON_DISK) is False+ assert not hf_tokens._denial_memory_lost_an_entry()++ # The filler must EARN a slot: on this disk and refused to a credential this host holds.+ for index in range(8):+ filler = f"acme/filler-{index}"+ _materialize_repo(on_disk, filler)+ hf_tokens._repo_access_cache.clear()+ assert _reads(OPERATOR_TOKEN, filler) is False+ assert hf_tokens._denial_memory_lost_an_entry(), "the table dropped nothing"++ hf_tokens._repo_access_cache.clear()+ _counting_probe(monkeypatch, requests.exceptions.ConnectionError("refused"), offline = False)+ assert _reads(OPERATOR_TOKEN, ON_DISK) is False, "a forgotten refusal read as never refused"+++def test_forgetting_one_refusal_is_not_a_reason_to_refuse_an_answered_hub(monkeypatch, on_disk):+ _hf_state(monkeypatch, denial_max = 1)+ hf_tokens._remember_denial(("acme/one", "model", "hash"), 1.0)+ hf_tokens._remember_denial(("acme/two", "model", "hash"), 2.0)+ assert hf_tokens._denial_memory_lost_an_entry()++ _counting_probe(monkeypatch, True, offline = False)+ assert _reads(OPERATOR_TOKEN, ON_DISK) is True++ reset_repo_access_cache()+ assert (+ not hf_tokens._denial_memory_lost_an_entry()+ ), "the reset left the fail-closed marker set for every following test"+++def test_a_refusal_still_being_re_asked_is_not_the_one_dropped(monkeypatch):+ _hf_state(monkeypatch, denial_max = 3)+ keys = [(f"acme/{index}", "model", "hash") for index in range(3)]+ for at, key in enumerate(keys):+ hf_tokens._remember_denial(key, float(at))+ hf_tokens._remember_denial(keys[0], 9.0)+ hf_tokens._remember_denial(("acme/new", "model", "hash"), 10.0)+ assert hf_tokens._denial_is_remembered(keys[0])+ assert not hf_tokens._denial_is_remembered(keys[1])+++def test_the_ownership_check_reads_the_hosts_token_every_time(monkeypatch, on_disk):+ _counting_probe(monkeypatch, True, offline = True)++ assert _reads(OPERATOR_TOKEN, ON_DISK) is True+ _hf_state(monkeypatch, ambient = (True, "hf_rotated"))+ assert _reads(OPERATOR_TOKEN, ON_DISK) is False+ assert _reads("hf_rotated", ON_DISK) is True+++ ("reader", "target", "raw", "stripped", "failure"),+ [+ (+ "ambient",+ "huggingface_hub.get_token",+ " hf_from_hub ",+ "hf_from_hub",+ OSError("token file unreadable"),+ ),+ (+ "saved",+ "storage.credential_secrets.get_hf_token",+ " hf_from_store ",+ "hf_from_store",+ RuntimeError("credential database is locked"),+ ),+ ],+ ids = ("the-hosts-own-token", "the-studio-credential-store"),+)+def test_a_credential_reader_separates_an_absent_credential_from_an_unreadable_one(+ monkeypatch, reader, target, raw, stripped, failure+):+ read = _REAL_AMBIENT_HF_TOKEN if reader == "ambient" else _REAL_SAVED_STUDIO_HF_TOKEN+ if reader == "ambient":+ # The env fallback must not answer in place of the Hub's own store.+ for key in hf_tokens._HF_TOKEN_ENV_KEYS:+ monkeypatch.delenv(key, raising = False)++ monkeypatch.setattr(target, lambda: raw)+ assert read() == (True, stripped)++ monkeypatch.setattr(target, lambda: None)+ assert read() == (True, None)++ # Asked and NOT answered is the third outcome; collapsing it into "no credential" fails open.+ def _raises():+ raise failure++ monkeypatch.setattr(target, _raises)+ assert read() == (False, None)+ assert hf_tokens._caller_populated_the_cache(None) is False+ assert hf_tokens._caller_populated_the_cache("hf_anything") is False+++ "alias",+ ["HF_HUB_TOKEN", "HUGGINGFACE_HUB_TOKEN", "HUGGINGFACEHUB_API_TOKEN"],+)+def test_a_credential_held_only_under_a_legacy_alias_still_counts(monkeypatch, alias):+ """`get_token` reads HF_TOKEN, HUGGING_FACE_HUB_TOKEN, the OIDC exchange and the token file,+ and nothing else. The aliases this module elsewhere treats as credentials (it strips all of+ them from a download subprocess) therefore came back as "this host holds nothing", which+ authorizes a tokenless caller against a cache that credential may have filled."""+ for key in hf_tokens._HF_TOKEN_ENV_KEYS:+ monkeypatch.delenv(key, raising = False)+ monkeypatch.setattr("huggingface_hub.get_token", lambda: None)++ assert _REAL_AMBIENT_HF_TOKEN() == (True, None)++ monkeypatch.setenv(alias, f" {HOST_CREDENTIAL} ")+ assert _REAL_AMBIENT_HF_TOKEN() == (True, HOST_CREDENTIAL)++ # And when the reader itself could not answer, the alias is still knowledge.+ def _raises():+ raise OSError("token file unreadable")++ monkeypatch.setattr("huggingface_hub.get_token", _raises)+ assert _REAL_AMBIENT_HF_TOKEN() == (True, HOST_CREDENTIAL)+++def test_an_unreadable_saved_credential_is_unknown_rather_than_absent(monkeypatch):+ """`get_secret` answers None for an absent row AND an undecryptable one; the row EXISTING+ separates them, readable without decrypting anything."""+ from storage import credential_secrets++ read = _REAL_SAVED_STUDIO_HF_TOKEN+ monkeypatch.setattr(credential_secrets, "get_hf_token", lambda: None)++ monkeypatch.setattr(credential_secrets, "hf_token_row_exists", lambda: True)+ assert read() == (False, None), "an unreadable saved credential must not read as absent"++ monkeypatch.setattr(credential_secrets, "hf_token_row_exists", lambda: False)+ assert read() == (True, None), "a host with nothing saved is still a real answer"++ def _raises():+ raise RuntimeError("credential database is locked")++ monkeypatch.setattr(credential_secrets, "hf_token_row_exists", _raises)+ assert read() == (False, None)+++def test_the_row_check_does_not_need_to_decrypt(monkeypatch):+ from pathlib import Path as _Path++ from storage import credential_secrets++ source = _Path(credential_secrets.__file__).read_text(encoding = "utf-8")+ body = source.split("def secret_row_exists(", 1)[1].split("\ndef ", 1)[0]+ # Strip comments and the docstring, whose prose would otherwise be read as code.+ code = "\n".join(line for line in body.splitlines() if not line.lstrip().startswith("#"))+ code = code.split('"""')[0] + "".join(code.split('"""')[2:])+ assert "get_secret" not in code, "the row check goes back through the decrypting reader"+ assert "SELECT 1" in code+++ ("status", "error_code", "landed_on", "expected"),+ [+ (200, None, None, True),+ (401, None, None, False),+ (403, None, None, False),+ (404, "RepoNotFound", None, False),+ (410, None, None, False),+ # Could not be asked: none of these is a statement about this caller.+ (404, None, None, None), # an HF_ENDPOINT mirror with no /auth-check route+ (405, None, None, None), # a mirror that rejects the method+ (429, None, None, None), # rate limited+ (500, None, None, None), # outage+ (502, None, None, None), # gateway in front of the Hub+ (200, None, "https://huggingface.co/login", None), # a redirect answers about elsewhere+ ],+)+def test_the_probe_separates_an_answer_from_a_failure_to_answer(+ monkeypatch, status, error_code, landed_on, expected+):+ _probe_against(monkeypatch, lambda url: _response(status, error_code, landed_on or url))++ assert hf_tokens._probe_repo_access(ON_DISK, OPERATOR_TOKEN, "model") is expected+++def test_a_probe_timeout_is_not_an_answer(monkeypatch):+ def _stall(*_a, **_k):+ raise requests.exceptions.Timeout("stalled")++ class _Session:+ get = staticmethod(_stall)++ monkeypatch.setattr("huggingface_hub.utils.get_session", lambda: _Session())++ with pytest.raises(hf_tokens._ProbeTimedOut):+ hf_tokens._probe_repo_access(ON_DISK, OPERATOR_TOKEN, "model")+++def test_the_presence_check_never_reaches_the_network(monkeypatch, on_disk):+ def _forbidden(*_a, **_k):+ raise AssertionError("the local presence check opened a connection")++ monkeypatch.setattr("huggingface_hub.utils.get_session", _forbidden)++ assert hf_tokens._repo_present_on_disk(ON_DISK, "model") is True+ assert hf_tokens._repo_present_on_disk(ABSENT, "model") is False+++def test_a_dataset_counts_when_only_the_prepared_cache_holds_it(monkeypatch, tmp_path):+ _cache_root(monkeypatch, tmp_path)+ prepared = tmp_path / "prepared"+ prepared.mkdir()+ monkeypatch.setattr(+ dataset_cache,+ "latest_processed_dataset_cache_path",+ lambda repo_id: prepared if repo_id == "acme/ds" else None,+ )++ assert hf_tokens._repo_present_on_disk("acme/ds", "dataset") is True+ assert hf_tokens._repo_present_on_disk("acme/other", "dataset") is False+++def test_an_unreadable_cache_does_not_authorize_anything(monkeypatch, tmp_path):+ _cache_root(monkeypatch, tmp_path)+ monkeypatch.setattr(+ hf_cache_state,+ "repo_cache_has_usable_snapshot",+ lambda *_a, **_k: (_ for _ in ()).throw(OSError("EIO")),+ )+ _counting_probe(monkeypatch, True, offline = True)++ assert hf_tokens._repo_present_on_disk(ON_DISK, "model") is False+ assert _reads(OPERATOR_TOKEN, ON_DISK) is False+++def test_one_unreadable_cache_root_does_not_make_every_repo_present(monkeypatch, tmp_path):+ """``repo_cache_has_usable_snapshot`` answers True for an unenumerable root."""++ def _roots(scan_errors = None):+ if scan_errors is not None:+ scan_errors.append(OSError("EACCES"))+ return []++ monkeypatch.setattr(hf_cache_state, "hf_cache_roots", _roots)+ assert hf_cache_state.repo_cache_has_usable_snapshot("model", ON_DISK) is True+ assert hf_tokens._repo_present_on_disk(ON_DISK, "model") is False++ _counting_probe(monkeypatch, True, offline = True)+ assert _reads(OPERATOR_TOKEN, ON_DISK) is False+++def test_tier_detection_reads_a_cached_config_offline(monkeypatch, on_disk):+ monkeypatch.setattr(+ transformers_version,+ "get_hf_cache_paths",+ lambda: SimpleNamespace(hub_cache = on_disk),+ )+ monkeypatch.setattr(transformers_version, "_env_offline", lambda: True)+ _counting_probe(monkeypatch, True, offline = True)+ transformers_version._config_json_cache.clear()++ assert transformers_version._load_config_json(ON_DISK, OPERATOR_TOKEN) == {+ "model_type": "llama"+ }+++def test_the_capability_probes_cache_only_gate_allows_a_repo_on_disk(monkeypatch, on_disk):+ _counting_probe(monkeypatch, True, offline = True)+ refused = model_config_module._offline_cache_read_refused++ assert refused(OPERATOR_TOKEN, ON_DISK, ON_DISK, True) is False+ assert refused(OPERATOR_TOKEN, ABSENT, ABSENT, True) is True+++ ("hub_says", "offline", "template"),+ [(True, True, "{{ messages }}"), (False, False, None)],+ ids = ("serves-what-it-cannot-ask-about", "withholds-what-the-hub-refused"),+)+def test_the_picker_serves_a_cached_chat_template_offline(+ monkeypatch, cache_root, hub_says, offline, template+):+ snapshot = _materialize_repo(cache_root, ON_DISK)+ (snapshot / "chat_template.jinja").write_text("{{ messages }}", encoding = "utf-8")+ monkeypatch.setattr(picker_service, "hf_env_offline", lambda: True)+ monkeypatch.setattr(picker_service, "resolve_cached_repo_id_case", lambda name: name)+ monkeypatch.setattr(+ picker_service, "iter_snapshots_preferring_whole", lambda *_a, **_k: iter([snapshot])+ )+ _counting_probe(monkeypatch, hub_says, offline = offline)++ assert picker_service.read_default_chat_template(ON_DISK, OPERATOR_TOKEN) == template+++def _stub_autoconfig(monkeypatch) -> dict:+ """Imported inside the function under test, so the stand-in goes on the package."""+ import transformers++ seen = {"n": 0}++ class _AutoConfig:+ @staticmethod+ def from_pretrained(name, *_a, **kwargs):+ seen["n"] += 1+ seen["name"] = name+ seen["local_files_only"] = kwargs.get("local_files_only")+ return SimpleNamespace(model_type = "llama")++ monkeypatch.setattr(transformers, "AutoConfig", _AutoConfig)+ return seen+++def test_an_anonymous_cache_only_config_read_serves_a_repo_on_disk(monkeypatch, on_disk):+ """The sentinel's branch refused EVERY cache-only read, on the one host where the cache is+ the only answer there is."""+ _no_host_credential(monkeypatch)+ monkeypatch.setattr(model_config_module, "_config_json_already_cached", lambda *_a, **_k: True)+ probes = _counting_probe(monkeypatch, True, offline = True)+ seen = _stub_autoconfig(monkeypatch)++ config = model_config_module.load_model_config(ON_DISK, token = False, local_files_only = True)++ assert config is not None+ assert seen["name"] == ON_DISK and seen["local_files_only"] is True+ assert probes["n"] == 0, "a cache-only read went to the network"+++ "already_cached, expected",+ [(True, []), (False, [("hf_a_one_off", ON_DISK, "model")])],+ ids = ["a cache hit records nothing", "a real fetch is recorded"],+)+def test_a_credentialed_config_read_records_only_what_it_could_fetch(+ monkeypatch, already_cached, expected+):+ """AutoConfig resolves an already-cached config without asking the Hub. Recording that marks a+ repo the cache may have held anonymously, which then withholds it from the tokenless offline+ caller this path exists for."""+ recorded: list = []+ # Patched on hf_tokens, not on the caller: the call goes through+ # `recording_a_request_token_fetch`, so this drives the real context manager.+ monkeypatch.setattr(+ hf_tokens,+ "note_repo_fetched_with_a_request_token",+ lambda token, repo, kind: recorded.append((token, repo, kind)),+ )+ monkeypatch.setattr(+ model_config_module, "_config_json_already_cached", lambda *_a, **_k: already_cached+ )+ # The Hub says this caller may reach the repo, so the gate above is not what is under test.+ _counting_probe(monkeypatch, True)+ _stub_autoconfig(monkeypatch)++ model_config_module.load_model_config(ON_DISK, token = "hf_a_one_off")++ assert recorded == expected+++ "prefer_local_cache, expected",+ [(True, []), (False, [("hf_a_one_off", "acme/ds", "dataset")])],+ ids = ["a cache-only preview records nothing", "a preview that may fetch is recorded"],+)+def test_a_dataset_preview_records_only_where_it_could_fetch(+ monkeypatch, prefer_local_cache, expected+):+ """`prefer_local_cache` reads the datasets cache or 404s, never the network. Recording it+ relabels a dataset the cache may have held anonymously, and the tokenless offline caller+ that path exists for is then refused."""+ import contextlib++ from hub.services.datasets import formatting+ from hub.schemas.datasets import CheckFormatRequest++ recorded: list = []+ monkeypatch.setattr(+ hf_tokens,+ "note_repo_fetched_with_a_request_token",+ lambda token, repo, kind: recorded.append((token, repo, kind)),+ )+ monkeypatch.setattr(formatting, "refuse_unauthorized_dataset_preview", lambda *_a, **_k: None)++ class _NoHub:+ def list_repo_files(self, *_a, **_k):+ raise RuntimeError("no network in this test")++ monkeypatch.setattr("huggingface_hub.HfApi", lambda *_a, **_k: _NoHub())+ monkeypatch.setattr(+ "datasets.load_dataset",+ lambda *_a, **_k: (_ for _ in ()).throw(RuntimeError("no network in this test")),+ )++ # Whatever the branch does after recording is another test's subject: the network is+ # stubbed off, so both legs end in an error and only the ledger is read here.+ with contextlib.suppress(Exception):+ formatting.check_format_response(+ CheckFormatRequest(dataset_name = "acme/ds", prefer_local_cache = prefer_local_cache),+ "hf_a_one_off",+ )++ assert recorded == expected+++def _offline_route_guard(monkeypatch, tmp_path, repo_id, present, host_token):+ import utils.utils as utils_module++ if host_token is None:+ _no_host_credential(monkeypatch)+ root = _cache_root(monkeypatch, tmp_path)+ if present:+ _materialize_repo(root, repo_id)+ monkeypatch.setattr(utils_module, "hf_env_offline", lambda: True)+ monkeypatch.setattr(hf_tokens, "_hub_offline", lambda: True)+ return utils_module.anonymous_and_offline(False, repo_id = repo_id)+++ ("repo_id", "present", "host_token", "refused"),+ [+ (ON_DISK, True, None, False),+ (ABSENT, False, None, True),+ # BOUNDARY. A host that HOLDS a credential could have filled this cache with it.+ (ON_DISK, True, OPERATOR_TOKEN, True),+ ],+ ids = ("on-disk", "not-on-disk", "host-holds-a-token"),+)+def test_the_route_entry_guard_serves_only_what_the_anonymous_caller_may_have(+ monkeypatch, tmp_path, repo_id, present, host_token, refused+):+ guard = _offline_route_guard(monkeypatch, tmp_path, repo_id, present, host_token)+ assert guard is refused+++def test_the_route_entry_guard_keeps_its_blanket_answer_without_a_repo_id(monkeypatch):+ import utils.utils as utils_module++ monkeypatch.setattr(utils_module, "hf_env_offline", lambda: True)+ assert utils_module.anonymous_and_offline(False) is True+ assert utils_module.anonymous_and_offline(None) is False+++def test_the_route_entry_guard_leaves_online_behaviour_exactly_as_it_was(monkeypatch, on_disk):+ import utils.utils as utils_module++ monkeypatch.setattr(utils_module, "hf_env_offline", lambda: False)+ probes = _counting_probe(monkeypatch, False)+ assert utils_module.anonymous_and_offline(False, repo_id = ON_DISK) is False+ assert probes["n"] == 0+++def test_a_repo_already_recorded_keeps_the_record_the_first_writer_left(provenance):+ """Asserted on the STORED value rather than on whether a write was attempted: the first-writer+ rule lives inside the transaction now, so a second call reaching the store is expected and it+ is what the store does with it that matters."""+ _noted(ONE_OFF, "acme/private")+ first = provenance[_key("acme/private")]+ assert first["by"] == hf_tokens._credential_identity(ONE_OFF)++ _noted(ONE_OFF, "acme/private")+ assert provenance[_key("acme/private")] == first, "the same credential rewrote its own record"++ # A DIFFERENT credential does not take it over; it makes the record say "cannot attribute".+ _noted("hf_someoneelses", "acme/private")+ again = provenance[_key("acme/private")]+ assert again["by"] is None+ assert again["at"] == first["at"], "the collapse rewrote the timestamp of the first fetch"++ _noted(ONE_OFF, "acme/other")+ assert provenance[_key("acme/other")]["by"] == hf_tokens._credential_identity(ONE_OFF)+++def test_the_provenance_map_is_bounded_and_says_so_when_it_is_full(monkeypatch, writes):+ full = {+ f"model:acme/repo-{index}": {"at": 1.0}+ for index in range(hf_tokens._REQUEST_TOKEN_REPOS_MAX)+ }+ _hf_state(monkeypatch, recorded = full)++ _noted("hf_someoneelses", "acme/new")+ assert writes == [], "the map kept growing on a caller-supplied repo id"+ assert _fetched_with_a_token("acme/new") is None+ assert _fetched_with_a_token("acme/repo-0") is True+ _hf_state(monkeypatch, credentials = (True, ()))+ assert _filled_by(None, "acme/new") is False+++def test_a_map_below_the_cap_still_answers_no_for_a_repo_it_does_not_hold(monkeypatch):+ _hf_state(monkeypatch, recorded = {})+ assert _fetched_with_a_token("acme/other") is False+++def test_a_credential_does_not_inherit_a_repo_another_credential_fetched(provenance):+ """A one-off X-Unsloth-HF-Token reaches private repos the host credential cannot, and its+ bytes land in the same cache, so holding the host credential now is not having filled it."""+ _noted(ONE_OFF, "acme/theirs")+ _noted(None, "acme/ours")++ assert _filled_by(HOST_CREDENTIAL, "acme/theirs") is False, "the host inherited a foreign repo"+ assert hf_tokens._resolve_unaskable("acme/theirs", "model", token = HOST_CREDENTIAL) is False++ # A cache older than the record is the tokenless offline install on first upgrade.+ assert _filled_by(HOST_CREDENTIAL, "acme/ours") is True+ assert _filled_by(HOST_CREDENTIAL, "acme/never-seen") is True+ assert _filled_by(ONE_OFF, "acme/theirs") is False+ assert provenance[_key("acme/theirs")]["by"] == hf_tokens._credential_identity(ONE_OFF)+++def test_a_repo_two_different_credentials_fetched_belongs_to_neither(provenance):+ _noted(None, "acme/shared")+ _noted(ONE_OFF, "acme/shared")++ assert provenance[_key("acme/shared")]["by"] is None+ assert (+ _filled_by(HOST_CREDENTIAL, "acme/shared") is False+ ), "a repo more than one credential fetched was claimed by one of them"+ assert _fetched_with_a_token("acme/shared") is True+++ ("landed", "kept"),+ [(False, False), (True, True)],+ ids = ("nothing landed, so the record goes", "bytes landed, so the record stands"),+)+def test_a_fetch_that_raised_keeps_its_record_only_if_it_wrote_something(+ monkeypatch, provenance, landed, kept+):+ """The write has to happen BEFORE the call, since a download that dies half way has still+ filled the cache and an unrecorded repo reads later as "nobody needed a credential". The+ cost is a 404 or an outage leaving a record for a fetch that never happened, which the+ first-writer rule then keeps, so the record is taken back exactly when the disk says the+ call moved nothing."""+ _hf_state(monkeypatch, present = landed)++ with pytest.raises(RuntimeError):+ with hf_tokens.recording_a_request_token_fetch(ONE_OFF, "acme/attempted", "model"):+ assert _key("acme/attempted") in provenance, "the record must exist during the call"+ raise RuntimeError("404 from the hub")++ assert (_key("acme/attempted") in provenance) is kept+++def test_a_take_back_leaves_another_writers_record_alone(monkeypatch, provenance):+ """Only while it is still the record this call wrote: a second credential collapsing the+ attribution replaces the entry, and taking that one back would drop somebody else's."""+ _hf_state(monkeypatch, present = False)++ record = hf_tokens.note_repo_fetched_with_a_request_token(ONE_OFF, "acme/shared", "model")+ _noted(None, "acme/shared")+ assert provenance[_key("acme/shared")]["by"] is None++ hf_tokens.forget_a_fetch_that_moved_nothing(record, "acme/shared", "model")+ assert _key("acme/shared") in provenance, "a collapsed record was removed by the first writer"+++def test_a_provenance_write_that_failed_is_not_read_as_nothing_to_record(monkeypatch):+ """A failed write leaves the same absence a repo nobody ever fetched leaves, and only one of+ the two authorizes."""++ def _raises(*_a, **_k):+ raise RuntimeError("database is locked")++ _hf_state(+ monkeypatch,+ unrecorded = set(),+ credentials = (True, ()),+ no_other_held = True,+ recorded = {},+ as_owner = _raises,+ )+ _noted("hf_a_one_off", "acme/private")++ assert _fetched_with_a_token("acme/private") is None+ assert (+ _filled_by(None, "acme/private") is False+ ), "a repo whose provenance write failed was read as one nothing ever fetched"+ # Only that repo: a locked database must not cost the whole host its own cache.+ assert _filled_by(None, "acme/other") is True+++def test_a_denial_is_remembered_when_the_facts_that_could_overturn_it_cannot_be_read(monkeypatch):+ """The rule below the memory answers a plain no for a fact it could not establish, because+ its callees swallow their own failures; a denial dropped in that window let the next outage+ authorize the caller the Hub had refused."""+ overturnable = hf_tokens._denial_can_be_overturned+ _hf_state(monkeypatch, present = True, unrecorded = set())++ _hf_state(monkeypatch, credentials = (False, ()))+ assert overturnable("acme/private", "model", HOST_CREDENTIAL) is True++ _hf_state(monkeypatch, credentials = (True, (HOST_CREDENTIAL,)), recorded = None)+ assert overturnable("acme/private", "model", HOST_CREDENTIAL) is True++ # BOUNDARY. The fallback could never authorize this caller, so no slot is spent.+ _hf_state(monkeypatch, recorded = {}, filled_by_caller = False)+ assert overturnable("acme/private", "model", HOST_CREDENTIAL) is False+++def test_a_ledger_write_that_failed_is_retried_rather_than_remembered(monkeypatch):+ """A shorter ledger reads as "this host never held anything else", which authorizes more."""+ identity = hf_tokens._credential_identity(HOST_CREDENTIAL)+ ledger: dict = {}+ attempts = {"n": 0}++ def _write(call, _key_unused, entry, value):+ attempts["n"] += 1+ if attempts["n"] == 1:+ raise RuntimeError("database is locked")+ ledger[entry] = value++ _hf_state(+ monkeypatch,+ noted_identities = set(),+ identities = lambda: dict(ledger),+ as_owner = _write,+ )++ hf_tokens._note_host_credential_identities([HOST_CREDENTIAL])+ assert ledger == {}, "the write did not fail"+ hf_tokens._note_host_credential_identities([HOST_CREDENTIAL])+ assert attempts["n"] == 2, "a failed ledger write was never retried"+ assert identity in ledger+ hf_tokens._note_host_credential_identities([HOST_CREDENTIAL])+ assert attempts["n"] == 2, "a ledger entry already present was rewritten"+++def test_an_unreadable_ledger_is_not_remembered_as_written(monkeypatch):+ _hf_state(+ monkeypatch,+ noted_identities = set(),+ identities = None,+ as_owner = lambda *a, **k: pytest.fail("wrote into a ledger it cannot read"),+ )+ hf_tokens._note_host_credential_identities([HOST_CREDENTIAL])+ assert hf_tokens._noted_credential_identities == set()+++def test_the_ledger_records_an_identity_and_never_the_credential(monkeypatch):+ written: dict = {}+ _hf_state(+ monkeypatch,+ identities = lambda: dict(written),+ as_owner = lambda call, _key_unused, entry, value: written.setdefault(entry, value),+ )+ hf_tokens.reset_repo_access_cache()++ hf_tokens._note_host_credential_identities((OPERATOR_TOKEN,))+ assert list(written) == [hf_tokens._credential_identity(OPERATOR_TOKEN)]+ assert OPERATOR_TOKEN not in repr(written)+ assert hf_tokens._credential_identity(OPERATOR_TOKEN) != hf_tokens._credential_identity(+ ROTATED_AWAY+ )++ # Written once: this runs on the authorization path, not on a settings save.+ written.clear()+ hf_tokens._note_host_credential_identities((OPERATOR_TOKEN,))+ assert written == {}, "the ledger was rewritten on every check"+++def _ledger(monkeypatch, *tokens: str) -> None:+ _hf_state(+ monkeypatch,+ identities = {hf_tokens._credential_identity(token): {"at": 1.0} for token in tokens},+ )+++ ("held", "caller", "authorized"),+ [+ # After a rotation the new credential must not be handed the old one's downloads.+ ((ROTATED_AWAY, OPERATOR_TOKEN), OPERATOR_TOKEN, False),+ # BOUNDARY: a host that never rotated is unchanged, which is nearly every host.+ ((OPERATOR_TOKEN,), OPERATOR_TOKEN, True),+ # Deleting it leaves its private repos on disk, so holding none stops meaning none+ # was needed.+ ((ROTATED_AWAY,), ANON, False),+ ((), ANON, True),+ # Cannot be established is not "empty".+ (None, OPERATOR_TOKEN, False),+ (None, ANON, False),+ ],+)+def test_an_outage_serves_the_cache_only_to_a_credential_that_really_filled_it(+ monkeypatch, on_disk, held, caller, authorized+):+ _counting_probe(monkeypatch, requests.exceptions.ConnectionError("refused"), offline = False)+ if caller is ANON:+ _hf_state(monkeypatch, ambient = (True, None), recorded = {})+ if held is None:+ _hf_state(monkeypatch, identities = None)+ else:+ _ledger(monkeypatch, *held)++ assert _reads(caller, ON_DISK) is authorized+++def test_a_non_ascii_credential_is_compared_not_crashed_on(monkeypatch, on_disk):+ """`hmac.compare_digest` refuses a str with any non-ASCII character, and both operands are+ text somebody else chose, so one high byte turned authorization into a 500."""+ _counting_probe(monkeypatch, _ProbeUnreachable(), offline = False)++ # Starlette latin-1 decodes the caller's header byte 0xE9 to U+00E9.+ assert _reads("hf_é_not_the_hosts", ON_DISK) is False++ _hf_state(monkeypatch, ambient = (True, "hf_opérateur"))+ assert _reads("hf_opérateur", ON_DISK) is True+++ ("base", "in_cache", "already_cached", "recorded"),+ [+ ("acme/private-base", True, None, [("acme/private-base", "hf_someoneelses")]),+ (None, True, None, []),+ ("acme/never-pulled", False, None, []),+ (OSError(13, "denied"), True, None, []),+ ("acme/public-base", True, "acme/public-base", []),+ ],+ ids = ("pulled", "unresolved", "not-in-the-cache", "cache-unreadable", "already-cached"),+)+def test_only_a_lora_base_this_load_really_pulled_is_recorded(+ monkeypatch, recorded_fetches, base, in_cache, already_cached, recorded+):+ def _resolve(_repo):+ if isinstance(base, BaseException):+ raise base+ return base++ monkeypatch.setattr(transformers_version, "_adapter_base_from_hf_cache", _resolve)+ monkeypatch.setattr(inference_routes, "_repo_is_in_the_hub_cache", lambda ref: in_cache)++ inference_routes._note_lora_base_fetched_with_a_request_token(+ "acme/adapter", "hf_someoneelses", already_cached = already_cached+ )++ assert recorded_fetches == recorded+++def test_the_base_a_load_started_with_is_the_one_read_before_it(monkeypatch):+ monkeypatch.setattr(+ transformers_version, "_adapter_base_from_hf_cache", lambda repo: "acme/public-base"+ )+ monkeypatch.setattr(inference_routes, "_repo_is_in_the_hub_cache", lambda ref: True)++ assert (+ inference_routes._lora_base_already_in_the_hub_cache("acme/adapter") == "acme/public-base"+ )+++def test_the_load_reads_the_base_after_the_fetch_not_before_it():+ import inspect++ impl = inspect.getsource(inference_routes._load_model_impl)+ assert "_note_lora_base_fetched_with_a_request_token(" in impl+ assert impl.rindex("finally:") < impl.index("_note_lora_base_fetched_with_a_request_token(")+ assert "already_cached = _lora_base_before_this_load" in impl+ assert impl.index("_lora_base_before_this_load = ") < impl.rindex("finally:")+++def test_only_a_repo_this_load_actually_pulled_is_recorded(recorded_fetches):+ def _record(before, after):+ recorded_fetches.clear()+ if before is False and after:+ inference_routes._note_load_fetched_with_a_request_token("acme/private", "hf_x")+ return [ref for ref, _token in recorded_fetches]++ assert _record(False, True) == ["acme/private"], "a repo this load pulled was not recorded"+ assert _record(True, True) == [], "a repo that was already cached was recorded as fetched"+ assert _record(False, False) == [], "a load that fetched nothing recorded a fetch"+ assert _record(None, True) == [], "an unanswerable reading recorded a fetch"++[email protected]("route", ("image", "video"))+def test_a_media_load_refused_by_validation_records_no_fetch(monkeypatch, recorded_fetches, route):+ """The record is permanent, so recording a fetch that never happened would withhold the repo+ from a later tokenless offline read of a copy downloaded anonymously."""+ import asyncio++ from fastapi import HTTPException++ from hub.services.models import account_access++ monkeypatch.setattr(account_access, "managed_account", lambda: False)+ monkeypatch.setattr(account_access, "require_idle_other_accounts", lambda: None)+ monkeypatch.setattr(inference_routes, "_repo_is_in_the_hub_cache", lambda ref: False)++ def _refuse(*_a, **_k):+ raise ValueError("unsupported model_kind")++ if route == "image":+ from core.inference import diffusion as _diffusion+ from models.inference import DiffusionLoadRequest++ monkeypatch.setattr(_diffusion, "resolve_model_kind", _refuse)+ request = DiffusionLoadRequest(model_path = "acme/private-image")+ load = inference_routes.load_diffusion_model_gated+ else:+ from core.inference import video as _video+ from models.inference import VideoLoadRequest+ from routes import video as video_routes++ monkeypatch.setattr(_video, "resolve_video_model_kind", _refuse)+ request = VideoLoadRequest(model_path = "acme/private-video")+ load = video_routes.load_video_model_gated++ request.hf_token = "hf_a_one_off_request_token"+ with pytest.raises(HTTPException) as refusal:+ asyncio.run(load(request, "owner", user_initiated = True))+ assert refusal.value.status_code == 400+ assert recorded_fetches == [], "a load refused before any worker started recorded a fetch"+++def test_a_media_load_records_at_entry_because_its_fetch_is_on_a_worker_thread():+ import inspect++ from core.inference import diffusion, video as video_core+ for begin in (diffusion.DiffusionBackend.begin_load, video_core.VideoBackend.begin_load):+ doc = inspect.getdoc(begin) or ""+ assert "Returns at once" in doc or "daemon thread" in doc, (+ f"{begin.__qualname__} no longer hands off; the media routes could compare "+ "before and after like the text load does"+ )+++def test_the_presence_probe_answers_none_for_anything_that_is_not_a_repo(monkeypatch):+ for local in ("/srv/models/model.gguf", "./model.gguf", "~/model.gguf", "C:/models", ""):+ assert inference_routes._repo_is_in_the_hub_cache(local) is None, local++ _hf_state(monkeypatch, present = True)+ assert inference_routes._repo_is_in_the_hub_cache("acme/model") is True++ def _raises(_repo, _kind):+ raise OSError(13, "denied")++ _hf_state(monkeypatch, present = _raises)+ assert inference_routes._repo_is_in_the_hub_cache("acme/model") is None+++def test_only_real_growth_in_the_hub_cache_counts_as_a_fetch(monkeypatch, tmp_path):+ blobs = _cached_repo_dir(monkeypatch, tmp_path, "acme/private", a = 10) / "blobs"+ monkeypatch.setattr(inference_routes, "_repo_is_in_the_hub_cache", lambda ref: True)+ fetched = inference_routes._load_fetched_bytes++ before = inference_routes._hub_cache_footprint("acme/private")+ assert before == (1, 10)+ assert not fetched("acme/private", True, before), "a pure cache hit was recorded as a fetch"++ (blobs / "b").write_bytes(b"y" * 4096)+ assert fetched("acme/private", True, before), "a refetch of an existing repo was lost"+ assert not fetched("acme/private", True, None), "an unreadable before-reading guessed"+ assert fetched("acme/private", False, None)++ # A prune is not a fetch, and neither is an after-reading that could not be taken.+ after_the_pull = inference_routes._hub_cache_footprint("acme/private")+ (blobs / "b").unlink()+ assert not fetched("acme/private", True, after_the_pull)+ monkeypatch.setattr(inference_routes, "_hub_cache_footprint", lambda ref: None)+ assert not fetched("acme/private", True, before), "an unreadable after-reading counted"++ # Either component: a new blob moves the count, an appended partial moves only the bytes.+ monkeypatch.setattr(inference_routes, "_hub_cache_footprint", lambda ref: (2, 10))+ assert fetched("acme/private", True, before)+ monkeypatch.setattr(inference_routes, "_hub_cache_footprint", lambda ref: (1, 9000))+ assert fetched("acme/private", True, before)+++def test_refs_and_no_exist_markers_are_not_read_as_a_fetch(monkeypatch, tmp_path):+ repo_dir = _cached_repo_dir(monkeypatch, tmp_path, "acme/public", a = 32)+ before = inference_routes._hub_cache_footprint("acme/public")++ (repo_dir / "refs").mkdir()+ (repo_dir / "refs" / "main").write_text("deadbeef")+ (repo_dir / ".no_exist" / "deadbeef").mkdir(parents = True)+ (repo_dir / ".no_exist" / "deadbeef" / "adapter_config.json").write_text("")+ assert inference_routes._hub_cache_footprint("acme/public") == before+++# The slots are keyed partly on caller-supplied input, so what may occupy one decides+# whether a caller can exhaust them.+++ ("caller_for", "repo_for"),+ [+ (lambda i: OPERATOR_TOKEN, lambda i: f"attacker/absent-{i}"),+ (lambda i: f"hf_stranger_{i}", lambda i: ON_DISK),+ ],+ ids = ("a-repo-not-on-disk", "a-credential-this-host-never-held"),+)+def test_a_denial_the_disk_could_never_overturn_does_not_occupy_a_slot(+ monkeypatch, on_disk, caller_for, repo_for+):+ _counting_probe(monkeypatch, False)++ for i in range(64):+ assert _reads(caller_for(i), repo_for(i)) is False++ assert len(hf_tokens._denied_repo_access) == 0+ assert hf_tokens._denial_memory_lost_an_entry() is False+++def test_a_flood_does_not_take_the_offline_fallback_away_from_everyone_else(monkeypatch, on_disk):+ """The defect this closes: one evicted entry set `_denial_memory_is_complete` False for the+ life of the process, after which every unaskable probe read as a refusal."""+ _counting_probe(monkeypatch, False)++ for i in range(hf_tokens._DENIAL_MEMORY_MAX + 16):+ _reads(f"hf_stranger_{i}", f"attacker/absent-{i}")++ assert hf_tokens._denial_memory_lost_an_entry() is False+ reset_repo_access_cache()+ _counting_probe(monkeypatch, _ProbeUnreachable(), offline = False)+ assert _reads(OPERATOR_TOKEN, ON_DISK) is True+++# `load_model_config`'s anonymous branch closes a read the Hub did not say yes to. Silence is+# not a yes EITHER: a Hub that cannot be asked leaves the metadata request failing too, and+# transformers then serves the cached config.json. What keeps a downloaded PUBLIC model from+# disappearing on a DNS blip is the provenance rule underneath, not the reachability of the+# probe: nothing is refused unless a credential could have filled that cache.+++def _anonymous_config_read(monkeypatch, tmp_path, probe, env_offline):+ root = _cache_root(monkeypatch, tmp_path)+ _materialize_repo(root, ON_DISK)+ _counting_probe(monkeypatch, probe, offline = False)+ monkeypatch.setattr(model_config_module, "_env_offline", lambda: env_offline)+ monkeypatch.setattr(model_config_module, "_config_json_already_cached", lambda *_a, **_k: True)+ return _stub_autoconfig(monkeypatch)+++ ("host_holds_a_credential", "probe", "env_offline", "served"),+ [+ # Unaskable and online is the same position as unaskable and offline: the credential+ # this host holds could have filled that cache, and the Hub is not there to say.+ (True, _ProbeUnreachable(), False, False),+ # BOUNDARY. The DNS blip that must NOT take a downloaded model away: no credential on+ # this host, so nothing could have fetched it under one.+ (False, _ProbeUnreachable(), False, True),+ # BOUNDARY. The reverse hole #10264 left open: an answered no must stay refused.+ (True, False, False, False),+ # BOUNDARY. Declared offline is narrowed on purpose: either could have filled the cache.+ (True, _ProbeUnreachable(), True, False),+ (False, _ProbeUnreachable(), True, True),+ ],+ ids = (+ "unaskable-online",+ "unaskable-online-no-credential",+ "an-answered-no",+ "declared-offline",+ "offline-no-credential",+ ),+)+def test_an_anonymous_cached_config_read_turns_only_on_an_answered_no(+ monkeypatch, tmp_path, host_holds_a_credential, probe, env_offline, served+):+ if not host_holds_a_credential:+ _no_host_credential(monkeypatch)+ reads = _anonymous_config_read(monkeypatch, tmp_path, probe, env_offline)++ if served:+ model_config_module.load_model_config(ON_DISK, use_auth = False, token = False)+ assert reads["n"] == 1+ else:+ with pytest.raises(OSError, match = "not available to an unauthorized caller"):+ model_config_module.load_model_config(ON_DISK, use_auth = False, token = False)+ assert reads["n"] == 0++ if probe is False:+ assert _refused(ANON, ON_DISK) is Truediff --git a/studio/backend/tests/test_openai_auto_switch.py b/studio/backend/tests/test_openai_auto_switch.pyindex 5032a61c314..e192fcbe9dd 100644--- a/studio/backend/tests/test_openai_auto_switch.py+++ b/studio/backend/tests/test_openai_auto_switch.py@@ -8511,6 +8511,61 @@ def test_map_entry_fill_reads_and_writes_in_one_transaction(tmp_path, monkeypatcassert db.get_app_setting(key) == {"a": {"v": 9}}+def test_a_first_writer_entry_collapses_a_conflicting_claim_inside_the_write(tmp_path, monkeypatch):+ """The credential-provenance rule, decided where the race is. A caller that reads the map,+ sees nothing, and then writes loses to a second caller doing the same with a different+ identity: both see "absent" and the last one stores its own claim over the first. So the+ comparison belongs inside this transaction."""+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))+ monkeypatch.setattr(db, "_schema_ready", set())++ key = "test_map_entry_first_writer"+ first = {"at": 100.0, "by": "identity-a"}+ assert db.upsert_app_setting_map_entry(+ key, "repo", first, keep_first_writer = True, ambiguous_field = "by"+ ) == {"repo": first}++ # The same identity writing again changes nothing, timestamp included.+ db.upsert_app_setting_map_entry(+ key,+ "repo",+ {"at": 200.0, "by": "identity-a"},+ keep_first_writer = True,+ ambiguous_field = "by",+ )+ assert db.get_app_setting(key) == {"repo": first}++ # A different one cannot take it over, and cannot be taken over in turn.+ db.upsert_app_setting_map_entry(+ key,+ "repo",+ {"at": 300.0, "by": "identity-b"},+ keep_first_writer = True,+ ambiguous_field = "by",+ )+ assert db.get_app_setting(key) == {"repo": {"at": 100.0, "by": None}}+ db.upsert_app_setting_map_entry(+ key,+ "repo",+ {"at": 400.0, "by": "identity-a"},+ keep_first_writer = True,+ ambiguous_field = "by",+ )+ assert db.get_app_setting(key) == {"repo": {"at": 100.0, "by": None}}++ # An absent entry is still created, and the ordinary write still replaces.+ db.upsert_app_setting_map_entry(+ key,+ "other",+ {"at": 500.0, "by": "identity-b"},+ keep_first_writer = True,+ ambiguous_field = "by",+ )+ assert db.get_app_setting(key)["other"] == {"at": 500.0, "by": "identity-b"}+ db.upsert_app_setting_map_entry(key, "other", {"at": 600.0, "by": "identity-c"})+ assert db.get_app_setting(key)["other"] == {"at": 600.0, "by": "identity-c"}++def test_a_fill_never_relabels_a_stored_gpu_pin_with_this_browser_s_index_space(tmp_path, monkeypatch):diff --git a/studio/backend/tests/test_saved_image_metadata_precision_contract.py b/studio/backend/tests/test_saved_image_metadata_precision_contract.pyindex d4bb6bac293..3db44ec6b14 100644--- a/studio/backend/tests/test_saved_image_metadata_precision_contract.py+++ b/studio/backend/tests/test_saved_image_metadata_precision_contract.py@@ -43,7 +43,7 @@ from fastapi.testclient import TestClientimport core.inference.diffusion as diffusion_moduleimport core.inference.gpu_arbiter as gpu_arbiterimport core.inference.image_gallery as gallery-from auth.authentication import get_current_subject+from auth.authentication import authenticated_via_api_key, get_current_subjectfrom core.inference.diffusion import DiffusionBackendfrom routes.inference import router as openai_router, studio_router@@ -455,6 +455,7 @@ def engaged_client(monkeypatch, tmp_path):# production. Both persistence paths reach the same gallery, so both are exercised here.app.include_router(openai_router, prefix = "/v1")app.dependency_overrides[get_current_subject] = lambda: "test-user"+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falsereturn TestClient(app), backend, saveddiff --git a/studio/backend/tests/test_video_routes.py b/studio/backend/tests/test_video_routes.pyindex 556cb4c0f27..3a833892133 100644--- a/studio/backend/tests/test_video_routes.py+++ b/studio/backend/tests/test_video_routes.py@@ -22,7 +22,7 @@ from fastapi.testclient import TestClientimport core.inference.gpu_arbiter as gpu_arbiterimport core.inference.video as video_moduleimport core.inference.video_gallery as gallery_module-from auth.authentication import get_current_subject+from auth.authentication import authenticated_via_api_key, get_current_subjectfrom core.inference.video_families import (VIDEO_CANCELLED_MSG,VIDEO_GENERATION_BUSY_MSG,@@ -292,6 +292,8 @@ def client(monkeypatch, tmp_path):app = FastAPI()app.include_router(video_router, prefix = "/api/inference")app.dependency_overrides[get_current_subject] = lambda: "test-user"+ # A browser session: the status routes redact host paths for an API-key caller.+ app.dependency_overrides[authenticated_via_api_key] = lambda: Falsereturn TestClient(app)diff --git a/studio/backend/utils/models/model_config.py b/studio/backend/utils/models/model_config.pyindex 90ef337a448..ccc342fe66f 100644--- a/studio/backend/utils/models/model_config.py+++ b/studio/backend/utils/models/model_config.py@@ -17,7 +17,10 @@ from utils.paths import (resolve_output_dir,resolve_export_dir,)+from contextlib import nullcontext+from hub.utils.hf_tokens import (+ recording_a_request_token_fetch,ANONYMOUS_CACHE_IDENTITY,cached_read_refused,qualify_cache_identity,@@ -563,9 +566,20 @@ def load_model_config(# `False` is falsy: without this it falls past both branches to the ambient call.# Passed as the sentinel rather than via without_hf_auth(), which mutates HF_TOKEN# process-wide and would strip a concurrent download's credential.- # token=False denies auth, not the cache, so offline it would read a cached private- # config.json anyway; with no network this caller gets nothing instead.- if local_files_only or _env_offline():+ # token=False denies auth, not the cache: AutoConfig serves a cached config.json without+ # consulting the credential, so the read is gated here. One condition, not two: an+ # "online, so only an ANSWERED refusal refuses" clause used to sit beside it, and it+ # discarded the unaskable verdict this gate exists for -- a Hub that cannot be asked+ # fails the metadata request too, and transformers then serves the private config.json+ # off disk. Public repos are not the cost: `cached_read_refused` refuses only where a+ # credential could have filled that cache in the first place.+ if not is_local_path(model_name) and cached_read_refused(+ token,+ repo_id = model_name,+ is_cached = lambda: _config_json_already_cached(model_name, revision),+ # The caller's own cache-only contract, as in the explicit-token gate below.+ offline = bool(local_files_only),+ ):raise OSError(f"config.json for {model_name} is not available to an unauthorized caller")@@ -595,14 +609,26 @@ def load_model_config(raise OSError(f"config.json for {model_name} is not available to an unauthorized caller")if token:- return AutoConfig.from_pretrained(- model_name,- trust_remote_code = trust_remote_code,- token = token,- local_files_only = local_files_only,- cache_dir = active_hf_hub_cache(),- **revision_kwargs,+ # config.json lands in the hub cache under what may be a one-off token; unrecorded it+ # reads later as "nothing here needed one". Only when this call can actually fetch:+ # recording an already-cached resolve would mark a repo the cache may have held+ # anonymously, withholding it from the tokenless offline caller this path exists for.+ # Written BEFORE the call, since a fetch that dies half way has still filled the cache,+ # and taken back by the context manager when the call raised leaving nothing on disk.+ recording = (+ recording_a_request_token_fetch(token, model_name, "model")+ if not local_files_only and not _config_json_already_cached(model_name, revision)+ else nullcontext())+ with recording:+ return AutoConfig.from_pretrained(+ model_name,+ trust_remote_code = trust_remote_code,+ token = token,+ local_files_only = local_files_only,+ cache_dir = active_hf_hub_cache(),+ **revision_kwargs,+ )if not use_auth:# No auth, for public model checks@@ -2547,7 +2573,7 @@ def detect_gguf_model(path: str, model_root: Optional[str] = None) -> Optional[sis_dir = False # stat() unavailable in the lock windowif not is_dir:return str(_local_gguf_load_path(p))- # Directory named "*.gguf": fall through to the dir scan below.+ # Directory named "*.gguf": fall through to the dir scan below.# Case 2: directory containing .gguf files (skip mmproj / MTP drafter)if p.is_dir():diff --git a/studio/backend/utils/utils.py b/studio/backend/utils/utils.pyindex cf15409f5bc..fb9872b63e5 100644--- a/studio/backend/utils/utils.py+++ b/studio/backend/utils/utils.py@@ -39,10 +39,32 @@ def hf_env_offline() -> bool:return False-def anonymous_and_offline(hf_token) -> bool:- """The one condition under which a Hub-reaching request can only be answered by disk. ``token=False`` denies authentication, not the cache: offline, huggingface_hub and datasets both resolve a previously downloaded private repo without ever authorizing, and a caller holding the anonymous sentinel has no network to establish access over, so every downstream read is a disk read it never earned. Guarding this at the route entry rather than at each call site is deliberate: the per-site version was fixed six times (the snapshot walk, the config probes, the embedding marker, the GGUF listing, the preview slices, AutoConfig) and each fix only moved the boundary to the next reader, so this states the rule once, before any of them run."""- from hub.utils.hf_tokens import is_anonymous- return is_anonymous(hf_token) and hf_env_offline()+def anonymous_and_offline(hf_token, *, repo_id: Optional[str] = None) -> bool:+ """The one condition under which a Hub-reaching request can only be answered by disk.++ ``token=False`` denies authentication, not the cache: offline, huggingface_hub and datasets+ both resolve a previously downloaded private repo without ever authorizing. Guarded at the+ route entry, since each per-call-site fix only moved the boundary to the next reader. Given+ a repo id the shared cached-read rule applies rather than a blanket refusal: a PUBLIC repo+ was always readable, and one not on the disk has nothing to leak.+ """+ from hub.utils.hf_tokens import cached_read_refused, is_anonymous++ if not is_anonymous(hf_token):+ return False+ if not hf_env_offline():+ # The per-reader gates answer the online question; this guard is the offline one.+ return False+ if repo_id is None:+ return True+ # is_cached is True because these routes read whatever the cache holds; only authorization+ # is left, and offline that resolves against the disk.+ return cached_read_refused(+ hf_token,+ repo_id = repo_id,+ is_cached = lambda: True,+ offline = True,+ )def canonical_model_repo_id(model_name: str) -> str:diff --git a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.tsindex 3b79aaea1da..c462da12b46 100644--- a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts+++ b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts@@ -1,6 +1,11 @@// SPDX-License-Identifier: AGPL-3.0-only// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0+import {+ CACHE_MISS_DOWNLOAD_DESCRIPTION,+ EMPTY_CACHE_MISS_WATCH,+ watchCacheMissDownload,+} from "../lib/cache-miss-download";import { mlxRuntimeStateFrom } from "../lib/mlx-runtime-state";import {type ServerTuningValues,@@ -2395,6 +2400,22 @@ export function useChatModelRuntime() {let downloadComplete = isDownloaded || isCachedLora;+ // A load that believes the weights are cached can still turn into a download (#9094): the+ // backend re-fetches a blob it judged unsafe to resume. MOVEMENT is the only proof accepted,+ // since bytes below the expected total is the ordinary state of a partial revision.+ const watchForCacheMiss =+ isDownloaded && !isLocal && nativePathToken == null && !isOllamaModelId(modelId);+ const cacheMissDescription = [+ currentCheckpoint ? "Switching models." : null,+ extraLoadingDescription ?? null,+ CACHE_MISS_DOWNLOAD_DESCRIPTION,+ ]+ .filter(Boolean)+ .join(" ");+ let activeLoadingDescription = loadingDescription;+ let cacheMissWatch = EMPTY_CACHE_MISS_WATCH;+ let cacheMissDownload = false;+const pollDownload = async () => {if (abortCtrl.signal.aborted || !loadingModelRef.current) {if (progressInterval) clearInterval(progressInterval);@@ -2439,7 +2460,7 @@ export function useChatModelRuntime() {...modelLoadToastOptions(renderLoadDescription("Downloading model…",- loadingDescription,+ activeLoadingDescription,pct,progressLabel,),@@ -2455,13 +2476,28 @@ export function useChatModelRuntime() {const est = estimate(dlSamples, prog.downloaded_bytes, 0);const rateSuffix =est.stable ? ` • ${formatRate(est.rate)}` : "";+ const unknownTotalLabel = `${dlGb.toFixed(1)} GB downloaded${rateSuffix}`;// Inline-status-only state; skip the chat-page re-render unless it is shown.if (loadToastDismissedRef.current) {setLoadProgress({percent: null,- label: `${dlGb.toFixed(1)} GB downloaded${rateSuffix}`,+ label: unknownTotalLabel,phase: "downloading",});+ } else {+ // The toast is UP and would otherwise say "Loading cached model into+ // memory" for the whole download. A missing total is supported, not an error.+ toast(null, {+ id: toastId,+ ...modelLoadToastOptions(+ renderLoadDescription(+ "Downloading model…",+ activeLoadingDescription,+ null,+ unknownTotalLabel,+ ),+ ),+ });}} else if (prog.progress >= 1 && hasShownProgress) {downloadComplete = true;@@ -2543,12 +2579,44 @@ export function useChatModelRuntime() {}};+ /** Whether this "cached" load has quietly become a download. */+ const cacheMissDownloadStarted = async (): Promise<boolean> => {+ try {+ const reading = await getDownloadProgress(modelId, hfToken);+ // Re-read AFTER the await, as pollDownload does: the load can finish or be+ // cancelled in flight, and `finally` then calls resetLoadingUi().+ if (abortCtrl.signal.aborted || !loadingModelRef.current) return false;+ const verdict = watchCacheMissDownload(cacheMissWatch, reading);+ cacheMissWatch = verdict.watch;+ if (!verdict.started) return false;+ cacheMissDownload = true;+ // pollDownload's completion branch is gated on it; leaving it false leaves+ // `downloadComplete` false forever and suppresses later progress.+ hasShownProgress = true;+ downloadComplete = false;+ activeLoadingDescription = cacheMissDescription;+ setLoadProgress({+ percent: verdict.percent,+ label: "Downloading the rest of the model",+ phase: "downloading",+ });+ return true;+ } catch {+ // Ignore polling errors; the next poll asks again.+ return false;+ }+ };+const pollProgress = async () => {if (!downloadComplete) {await pollDownload();- } else {- await pollLoad();+ return;+ }+ if (watchForCacheMiss && !cacheMissDownload && (await cacheMissDownloadStarted())) {+ await pollDownload();+ return;}+ await pollLoad();};let hasShownProgress = false;diff --git a/studio/frontend/src/features/chat/lib/cache-miss-download.ts b/studio/frontend/src/features/chat/lib/cache-miss-download.tsnew file mode 100644index 00000000000..55bf631939a--- /dev/null+++ b/studio/frontend/src/features/chat/lib/cache-miss-download.ts@@ -0,0 +1,59 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++/**+ * Telling "loading a cached model" apart from "downloading it again" (#9094). Only a count that+ * GREW between two readings is a transfer: below the expected total is an ordinary partial+ * revision, and an unmeasurable cache leaves the watch alone rather than forfeiting it.+ */++/** Previous reading's bytes; null when there is nothing to compare against. */+export interface CacheMissWatch {+ readonly bytes: number | null;+}++export interface CacheMissReading {+ readonly downloaded_bytes?: number | null;+ readonly expected_bytes?: number | null;+ readonly progress?: number | null;+ readonly cache_measured?: boolean | null;+}++export const EMPTY_CACHE_MISS_WATCH: CacheMissWatch = { bytes: null };++export const CACHE_MISS_DOWNLOAD_DESCRIPTION =+ "The cached copy is incomplete, so the missing files are downloading from Hugging Face.";++export interface CacheMissVerdict {+ readonly started: boolean;+ readonly watch: CacheMissWatch;+ /** Whole percent, or null when the total is unknown. */+ readonly percent: number | null;+}++export function watchCacheMissDownload(+ watch: CacheMissWatch,+ reading: CacheMissReading | null | undefined,+): CacheMissVerdict {+ if (!reading || reading.cache_measured === false) {+ return { started: false, watch, percent: null };+ }+ const progress = reading.progress ?? 0;+ if (progress >= 1) {+ return { started: false, watch: EMPTY_CACHE_MISS_WATCH, percent: null };+ }+ const bytes = reading.downloaded_bytes ?? 0;+ if (!Number.isFinite(bytes) || bytes < 0) {+ return { started: false, watch, percent: null };+ }+ const previous = watch.bytes;+ if (previous != null && bytes > previous) {+ const expected = reading.expected_bytes ?? 0;+ return {+ started: true,+ watch: { bytes },+ percent: expected > 0 ? Math.round(progress * 100) : null,+ };+ }+ return { started: false, watch: { bytes }, percent: null };+}diff --git a/studio/frontend/tests/chat-cache-miss-download-label.test.ts b/studio/frontend/tests/chat-cache-miss-download-label.test.tsnew file mode 100644index 00000000000..fb55f0de274--- /dev/null+++ b/studio/frontend/tests/chat-cache-miss-download-label.test.ts@@ -0,0 +1,179 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// #9094: the toast read "Loading cached model into memory." while bytes arrived from Hugging+// Face. Asserted on the decision, not the source text, which would pass with nothing wired up.++import assert from "node:assert/strict";+import test from "node:test";++import {+ CACHE_MISS_DOWNLOAD_DESCRIPTION,+ EMPTY_CACHE_MISS_WATCH,+ watchCacheMissDownload,+} from "../src/features/chat/lib/cache-miss-download.ts";+import { readText } from "./helpers/kit.ts";++const partial = (bytes: number) => ({+ downloaded_bytes: bytes,+ expected_bytes: 1_000,+ progress: bytes / 1_000,+ cache_measured: true,+});++test("bytes that grow between two readings are a download", () => {+ const first = watchCacheMissDownload(EMPTY_CACHE_MISS_WATCH, partial(100));+ assert.equal(first.started, false, "one reading cannot show movement");++ const second = watchCacheMissDownload(first.watch, partial(400));+ assert.equal(second.started, true);+ assert.equal(second.percent, 40);+});++test("an incomplete cache that is not moving is a load, not a download", () => {+// The state of every repo whose last download was cancelled; relabelling here would cry+// download on every load.+ let watch = EMPTY_CACHE_MISS_WATCH;+ for (let poll = 0; poll < 5; poll += 1) {+ const verdict = watchCacheMissDownload(watch, partial(250));+ assert.equal(verdict.started, false);+ watch = verdict.watch;+ }+});++test("a complete cache resets the watch instead of arming it", () => {+ const complete = {+ downloaded_bytes: 1_000,+ expected_bytes: 1_000,+ progress: 1,+ cache_measured: true,+ };+ const armed = watchCacheMissDownload(EMPTY_CACHE_MISS_WATCH, partial(100));+ const reset = watchCacheMissDownload(armed.watch, complete);+ assert.equal(reset.started, false);+ assert.equal(reset.watch.bytes, null);+ assert.equal(watchCacheMissDownload(reset.watch, partial(900)).started, false);+});++test("an unmeasurable cache leaves the comparison alone", () => {+ const armed = watchCacheMissDownload(EMPTY_CACHE_MISS_WATCH, partial(100));+ const unreadable = watchCacheMissDownload(armed.watch, {+ downloaded_bytes: 0,+ expected_bytes: 0,+ progress: 0,+ cache_measured: false,+ });+ assert.equal(unreadable.started, false);+ assert.equal(unreadable.watch.bytes, 100, "the reading was not evidence, so it is not kept");+ assert.equal(watchCacheMissDownload(unreadable.watch, partial(300)).started, true);+});++test("a missing or malformed reading is never a download", () => {+ for (const reading of [null, undefined, {}, { downloaded_bytes: Number.NaN, progress: 0 }]) {+ assert.equal(+ watchCacheMissDownload({ bytes: 1 }, reading as never).started,+ false,+ );+ }+});++test("the phase has words of its own, naming the reason", () => {+ assert.match(CACHE_MISS_DOWNLOAD_DESCRIPTION, /downloading from Hugging Face/);+ assert.doesNotMatch(CACHE_MISS_DOWNLOAD_DESCRIPTION, /cached model into memory/);+});++test("a total the backend could not establish reports bytes rather than a wrong percent", () => {+ const first = watchCacheMissDownload(EMPTY_CACHE_MISS_WATCH, {+ downloaded_bytes: 10,+ expected_bytes: 0,+ progress: 0,+ cache_measured: true,+ });+ const second = watchCacheMissDownload(first.watch, {+ downloaded_bytes: 20,+ expected_bytes: 0,+ progress: 0,+ cache_measured: true,+ });+ assert.equal(second.started, true);+ assert.equal(second.percent, null);+});++test("the load hook actually consults the watch, and on the cached branch", () => {+// Only worth anything if the poll loop asks it: a cached load polls the mmap phase and nothing+// else on main, which is how #9094 stayed invisible.+ const hook = readText("../src/features/chat/hooks/use-chat-model-runtime.ts");+ assert.match(hook, /watchCacheMissDownload\(/);+ assert.match(hook, /watchForCacheMiss && !cacheMissDownload && \(await cacheMissDownloadStarted\(\)\)/);+ assert.match(hook, /downloadComplete = false;\n\s+activeLoadingDescription = cacheMissDescription;/);+// Local paths, Ollama manifests and cached LoRAs never reach the Hub, so they are not polled.+ assert.match(+ hook,+ /const watchForCacheMiss =\n\s+isDownloaded && !isLocal && nativePathToken == null && !isOllamaModelId\(modelId\);/,+ );+});++test("an unknown total still updates the toast the user is looking at", () => {+ // This branch's state write is read only by the inline status shown AFTER the toast is+ // dismissed, so with the toast up the words stay on whatever the previous phase set.+ const hook = readText("../src/features/chat/hooks/use-chat-model-runtime.ts");+ const start = hook.indexOf("prog.expected_bytes === 0 &&");+ assert.ok(start > 0, "the unknown-total branch moved");+ const end = hook.indexOf("} else if (prog.progress >= 1 && hasShownProgress)", start);+ assert.ok(end > start, "the branch after the unknown-total one moved");+ const branch = hook.slice(start, end);++ // Dismissed: only the inline status is written, so the page is not re-rendered per poll.+ assert.match(branch, /if \(loadToastDismissedRef\.current\) \{[\s\S]*setLoadProgress\(/);+ // Visible: the toast is written directly, as both neighbouring branches do.+ assert.match(branch, /\} else \{[\s\S]*toast\(null, \{[\s\S]*renderLoadDescription\(/);+ assert.match(branch, /"Downloading model…"/);+ // No percentage: there is no total to compute one from.+ assert.match(branch, /renderLoadDescription\(\s*"Downloading model…",[\s\S]*?null,/);+});++test("detecting the download counts as having shown progress", () => {+ // pollDownload's completion branch is gated on hasShownProgress, so a tail finishing between+ // the watcher request and the pollDownload behind it reaches a poller that ignores+ // `progress >= 1`, and the UI sticks in the downloading phase for the rest of the load.+ const hook = readText("../src/features/chat/hooks/use-chat-model-runtime.ts");+ const start = hook.indexOf("const cacheMissDownloadStarted");+ assert.ok(start > 0, "the watcher moved");+ const end = hook.indexOf("const pollProgress", start);+ assert.ok(end > start);+ const branch = hook.slice(start, end);+ assert.match(branch, /if \(!verdict\.started\) return false;/);+ assert.match(branch, /cacheMissDownload = true;[\s\S]*hasShownProgress = true;/);+ // And it is set only once the watch has actually reported movement, so a first reading cannot+ // arm it.+ const armed = branch.indexOf("hasShownProgress = true;");+ assert.ok(armed > branch.indexOf("if (!verdict.started) return false;"));+});++test("the cache-miss watcher re-reads the load after its own await", () => {+ // A load that completes or is cancelled mid-request runs `finally`, which calls+ // resetLoadingUi(); a callback writing afterwards left the inline status still downloading.+ const hook = readText("../src/features/chat/hooks/use-chat-model-runtime.ts");+ const start = hook.indexOf("const cacheMissDownloadStarted");+ assert.ok(start > 0, "the watcher moved");+ const end = hook.indexOf("const pollProgress", start);+ assert.ok(end > start);+ const branch = hook.slice(start, end);++ const awaited = branch.indexOf("await getDownloadProgress(");+ assert.ok(awaited > 0, "the watcher no longer reads download progress");+ const recheck = branch.indexOf(+ "if (abortCtrl.signal.aborted || !loadingModelRef.current) return false;",+ );+ assert.ok(recheck > awaited, "the load is not re-read after the await");+ // Before anything is mutated: the watch, the flags and the progress write all follow it.+ for (const mutation of [+ "cacheMissWatch = verdict.watch;",+ "cacheMissDownload = true;",+ "hasShownProgress = true;",+ "setLoadProgress({",+ ]) {+ const at = branch.indexOf(mutation);+ assert.ok(at > recheck, `${mutation} runs before the re-read`);+ }+});