Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: reuse the size probe's repo listing for scoped download blob hashes (#15663)

Daniel Han3c8e1fd66aae
diff --git a/studio/backend/hub/services/download_lifecycle.py b/studio/backend/hub/services/download_lifecycle.py
index 7195e8569ef..a21331d2934 100644
--- a/studio/backend/hub/services/download_lifecycle.py
+++ b/studio/backend/hub/services/download_lifecycle.py
@@ -113,6 +113,35 @@ def _repo_siblings(repo_type: str, repo_id: str, hf_token: HfTokenArg) -> tuple:
return siblings
+def repo_siblings_fetched_since(
+ repo_type: str,
+ repo_id: str,
+ hf_token: Optional[str],
+ *,
+ allow_ambient_token: bool,
+ since: float,
+) -> Optional[tuple]:
+ """The size probe's listing if fetched at/after ``since`` with exactly the credential
+ ``model_info(token = hf_token)`` sends and non-empty, else None (caller lists again)."""
+ if hf_token is None:
+ if not allow_ambient_token:
+ return None # the memo listed anonymously; model_info(token = None) sends the ambient login
+ elif not (isinstance(hf_token, str) and hf_token and hf_token == hf_token.strip()):
+ return None # hf_token_arg strips or drops it, so the memo may have sent something else
+ key = (
+ str(repo_type),
+ repo_id.lower(),
+ hf_cache_scan.token_fingerprint(
+ hf_token_arg(hf_token, allow_ambient_token = allow_ambient_token)
+ ),
+ )
+ with _repo_siblings_lock:
+ cached = _REPO_SIBLINGS.get(key)
+ if cached is None or cached[1] < since or not cached[0]:
+ return None
+ return cached[0]
+
+
def largest_download_file_bytes(
repo_type: str,
repo_id: str,
diff --git a/studio/backend/hub/services/models/downloads.py b/studio/backend/hub/services/models/downloads.py
index d6166d292b4..53e977d2535 100644
--- a/studio/backend/hub/services/models/downloads.py
+++ b/studio/backend/hub/services/models/downloads.py
@@ -6,6 +6,7 @@
from __future__ import annotations
import asyncio
+import time
from pathlib import Path
from typing import Optional, Sequence, TYPE_CHECKING
@@ -58,15 +59,19 @@ def _scope_variant(scope_id: Optional[str]) -> Optional[str]:
def scoped_file_blob_hashes(
- repo_id: str, files: Sequence[str], hf_token: Optional[str]
+ repo_id: str,
+ files: Sequence[str],
+ hf_token: Optional[str],
+ *,
+ siblings: Optional[Sequence] = None,
) -> frozenset[str]:
"""Blob hashes for exactly ``files``, so a scoped job's progress, purge and peer protection cover its own files and nothing else in the repo."""
- from huggingface_hub import HfApi
-
wanted = set(files)
- info = HfApi().model_info(repo_id, files_metadata = True, token = hf_token)
+ if siblings is None:
+ from huggingface_hub import HfApi
+ siblings = HfApi().model_info(repo_id, files_metadata = True, token = hf_token).siblings
return blob_hashes_for_siblings(
- [s for s in info.siblings if getattr(s, "rfilename", None) in wanted]
+ [s for s in siblings if getattr(s, "rfilename", None) in wanted]
)
@@ -351,6 +356,7 @@ async def download_model_response(
key = _download_job_key(repo_id, variant)
_reject_if_load_owned(key)
# Size and Auto resolution may perform network probes, so keep both off the event loop.
+ sized_since = time.monotonic()
largest_file_bytes = await asyncio.to_thread(
download_lifecycle.largest_download_file_bytes,
"model",
@@ -381,7 +387,17 @@ async def download_model_response(
if scope_variant is not None:
# A scope owns exactly its own files: same set for purge and for progress.
variant_blob_hashes = await asyncio.to_thread(
- scoped_file_blob_hashes, repo_id, scoped_files, hf_token
+ scoped_file_blob_hashes,
+ repo_id,
+ scoped_files,
+ hf_token,
+ siblings = download_lifecycle.repo_siblings_fetched_since(
+ "model",
+ repo_id,
+ hf_token,
+ allow_ambient_token = allow_ambient_token,
+ since = sized_since,
+ ),
)
variant_progress_blob_hashes = variant_blob_hashes
else:
diff --git a/studio/backend/tests/test_scoped_download_reuses_size_listing.py b/studio/backend/tests/test_scoped_download_reuses_size_listing.py
new file mode 100644
index 00000000000..d71de45bd7d
--- /dev/null
+++ b/studio/backend/tests/test_scoped_download_reuses_size_listing.py
@@ -0,0 +1,201 @@
+# 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 scoped download start reuses the size probe's fresh, credential-equivalent listing; otherwise it lists again."""
+
+from __future__ import annotations
+
+import asyncio
+import sys
+import time
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
+if _BACKEND_DIR not in sys.path:
+ sys.path.insert(0, _BACKEND_DIR)
+
+from hub.schemas.downloads import DownloadModelRequest
+from hub.services import download_lifecycle
+from hub.services.models import downloads as dl
+
+REPO = "org/scoped-reuse-model"
+FILES = ["a.gguf", "b.json"]
+
+
+def _sib(
+ name,
+ sha,
+ size = 10,
+):
+ return SimpleNamespace(
+ rfilename = name,
+ size = size,
+ blob_id = f"blob-{sha}",
+ lfs = SimpleNamespace(sha256 = sha, size = size) if name.endswith(".gguf") else None,
+ )
+
+
+def hub(monkeypatch):
+ state = {"calls": [], "version": 1, "fail_repo_info": False}
+
+ def listing():
+ v = state["version"]
+ return [
+ _sib("a.gguf", f"sha-a-{v}"),
+ _sib("b.json", f"sha-b-{v}"),
+ _sib("c.gguf", f"sha-c-{v}"),
+ ]
+
+ class _Api:
+ def __init__(
+ self,
+ token = None,
+ **_,
+ ):
+ self.token = token
+
+ def repo_info(
+ self,
+ repo_id,
+ *,
+ repo_type = None,
+ files_metadata = False,
+ timeout = None,
+ **_,
+ ):
+ state["calls"].append(("repo_info", repo_id, self.token, files_metadata))
+ if state["fail_repo_info"]:
+ raise OSError("hub down")
+ return SimpleNamespace(siblings = listing())
+
+ def model_info(
+ self,
+ repo_id,
+ *,
+ files_metadata = False,
+ token = None,
+ **_,
+ ):
+ state["calls"].append(("model_info", repo_id, token, files_metadata))
+ return SimpleNamespace(siblings = listing())
+
+ import huggingface_hub
+
+ monkeypatch.setattr(huggingface_hub, "HfApi", _Api)
+ monkeypatch.setattr(download_lifecycle, "_REPO_SIBLINGS", {})
+ monkeypatch.setattr(dl, "_reject_if_load_in_flight", lambda repo_id: None)
+ monkeypatch.setattr(dl, "resolve_cached_repo_id_case", lambda repo, **k: repo)
+ monkeypatch.setattr(download_lifecycle, "launch_worker", lambda *a, **k: "running")
+
+ async def _no_gate(*a, **k):
+ return None
+
+ monkeypatch.setattr(dl, "_refuse_stale_scoped_download", _no_gate)
+ real = dl.scoped_file_blob_hashes
+ hashes = []
+
+ def spy(*a, **k):
+ out = real(*a, **k)
+ hashes.append(out)
+ return out
+
+ monkeypatch.setattr(dl, "scoped_file_blob_hashes", spy)
+
+ def run(hf_token = None, *, allow_ambient_token = True):
+ state["calls"].clear()
+ hashes.clear()
+ body = DownloadModelRequest(
+ repo_id = REPO, scope_id = "audio", files = list(FILES), use_xet = False
+ )
+ asyncio.run(
+ dl.download_model_response(body, hf_token, allow_ambient_token = allow_ambient_token)
+ )
+ key = dl._download_job_key(REPO, dl._scope_variant("audio"))
+ dl._registry.set_job(key, "complete")
+ return list(state["calls"]), hashes[0]
+
+ run.state = state
+ return run
+
+
+EXPECTED_V1 = frozenset({"sha-a-1", "blob-sha-b-1"})
+
+
[email protected]("token", [None, "hf_abc"])
+def test_one_listing_per_scoped_start(hub, token):
+ calls, hashes = hub(token)
+ assert [c[0] for c in calls] == ["repo_info"]
+ assert hashes == EXPECTED_V1
+
+
+ "token, ambient",
+ [(None, False), (" hf_abc ", True), ("", True), ("hf_abc ", False)],
+)
+def test_a_different_credential_lists_again(hub, token, ambient):
+ calls, hashes = hub(token, allow_ambient_token = ambient)
+ assert [c[0] for c in calls] == ["repo_info", "model_info"]
+ assert calls[1][2] == token
+ assert hashes == EXPECTED_V1
+
+
+def test_a_failed_size_probe_lists_again(hub):
+ hub.state["fail_repo_info"] = True
+ calls, hashes = hub("hf_abc")
+ assert [c[0] for c in calls] == ["repo_info", "model_info"]
+ assert hashes == EXPECTED_V1
+
+
+def test_a_ttl_memo_from_an_earlier_request_is_not_reused(hub):
+ hub("hf_abc") # fills the 60 s size memo with version 1
+ hub.state["version"] = 2 # the repo changed since
+ calls, hashes = hub("hf_abc")
+ assert [c[0] for c in calls] == ["model_info"]
+ assert hashes == frozenset({"sha-a-2", "blob-sha-b-2"})
+
+
+def test_a_failed_refresh_serving_the_old_listing_is_not_reused(hub, monkeypatch):
+ hub("hf_abc")
+ key = next(iter(download_lifecycle._REPO_SIBLINGS))
+ sibs, _ = download_lifecycle._REPO_SIBLINGS[key]
+ download_lifecycle._REPO_SIBLINGS[key] = (sibs, time.monotonic() - 3600)
+ hub.state["fail_repo_info"] = True
+ hub.state["version"] = 2
+ calls, hashes = hub("hf_abc")
+ assert [c[0] for c in calls] == ["repo_info", "model_info"]
+ assert hashes == frozenset({"sha-a-2", "blob-sha-b-2"})
+
+
+def test_an_empty_listing_is_not_reused():
+ download_lifecycle._REPO_SIBLINGS.clear()
+ since = time.monotonic()
+ key = ("model", REPO.lower(), download_lifecycle.hf_cache_scan.token_fingerprint(None))
+ download_lifecycle._REPO_SIBLINGS[key] = ((), since)
+ try:
+ assert (
+ download_lifecycle.repo_siblings_fetched_since(
+ "model", REPO, None, allow_ambient_token = True, since = since
+ )
+ is None
+ )
+ finally:
+ download_lifecycle._REPO_SIBLINGS.pop(key, None)
+
+
+def test_scoped_hashes_match_with_and_without_a_listing(monkeypatch):
+ import huggingface_hub
+
+ sibs = [_sib("a.gguf", "x"), _sib("b.json", "y"), _sib("c.gguf", "z")]
+
+ class _Api:
+ def model_info(self, *a, **k):
+ return SimpleNamespace(siblings = sibs)
+
+ monkeypatch.setattr(huggingface_hub, "HfApi", _Api)
+ fetched = dl.scoped_file_blob_hashes(REPO, FILES, None)
+ reused = dl.scoped_file_blob_hashes(REPO, FILES, None, siblings = tuple(sibs))
+ assert fetched == reused == frozenset({"x", "blob-y"})