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: keep a resident GGUF's repo identity for Hub shared-blob cache files (#16086)

Daniel Han858a97c9e56d
diff --git a/studio/backend/hub/services/models/account_access.py b/studio/backend/hub/services/models/account_access.py
index 5174fe71fb5..f78fd604a2a 100644
--- a/studio/backend/hub/services/models/account_access.py
+++ b/studio/backend/hub/services/models/account_access.py
@@ -883,6 +883,29 @@ def _cached_repo(path: Path) -> tuple[str, str] | None:
return None
+def _is_own_shared_blob(path: Path, cached: tuple[str, str], resolved: Path) -> bool:
+ """Hub >= 1.32 stores Xet files once per cache: snapshot -> repo blob -> <cache>/blobs/<hash[:2]>/<hash>."""
+ hop, last_in_repo = path, None
+ for _ in range(8):
+ if not hop.is_symlink():
+ break
+ hop = Path(os.path.normpath(hop.parent.resolve() / os.readlink(hop)))
+ if _cached_repo(hop) != cached:
+ break
+ last_in_repo = hop
+ if last_in_repo is None or hop != resolved or not resolved.is_file() or hop.is_symlink():
+ return False
+ repo_blobs = last_in_repo.parent
+ repo_dir = repo_blobs.parent
+ return (
+ repo_blobs.name == "blobs"
+ and repo_dir.name == f"models--{cached[0].replace('/', '--')}"
+ and resolved.parent.parent == repo_dir.parent / "blobs"
+ and re.fullmatch(r"[0-9a-f]{64}", resolved.name) is not None
+ and resolved.parent.name == resolved.name[:2]
+ )
+
+
def cached_model_repo(reference: str) -> str | None:
"""Recover a resident's Hub identity only from a verified model-cache path."""
from utils.hf_cache_settings import known_hf_hub_caches
@@ -896,7 +919,7 @@ def cached_model_repo(reference: str) -> str | None:
if cached is None or cached[1] != "model":
return None
resolved = path.resolve()
- if _cached_repo(resolved) != cached:
+ if _cached_repo(resolved) != cached and not _is_own_shared_blob(path, cached, resolved):
return None
if Path(normalize_path(reference)).expanduser().resolve() != resolved:
return None
@@ -951,9 +974,10 @@ def _model_visible(reference: str, grants, repo_type: str, offline: bool) -> boo
if cached is not None:
# Snapshots point at their own repo's blobs; cross-repo links are refused.
actual = _cached_repo(resolved)
- return actual == cached and repo_visible(
- cached[0], cached[1], grants = grants, **access_options
+ own = actual == cached or (
+ cached[1] == "model" and _is_own_shared_blob(path, cached, resolved)
)
+ return own and repo_visible(cached[0], cached[1], grants = grants, **access_options)
except (OSError, RuntimeError, ValueError):
return False
return False
diff --git a/studio/backend/tests/test_resident_cache_shared_blob.py b/studio/backend/tests/test_resident_cache_shared_blob.py
new file mode 100644
index 00000000000..27751058ebd
--- /dev/null
+++ b/studio/backend/tests/test_resident_cache_shared_blob.py
@@ -0,0 +1,99 @@
+# 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 resident GGUF in Hub >= 1.32's cache-wide shared blob store keeps its repo identity."""
+
+import os
+
+import pytest
+
+from hub.services.models import account_access as access
+
+HASH = "ab" * 32
+ETAG = "cd" * 32
+
+
+def _cache(monkeypatch, tmp_path):
+ from utils import hf_cache_settings
+
+ cache = tmp_path / "hub"
+ (cache / "blobs" / "ab").mkdir(parents = True)
+ (cache / "blobs" / ".huggingface-shared-blobs").write_text("1")
+ (cache / "blobs" / "ab" / HASH).write_bytes(b"")
+ monkeypatch.setattr(hf_cache_settings, "known_hf_hub_caches", lambda: [cache])
+ return cache
+
+
+def _repo(cache, name, blob_target):
+ repo = cache / f"models--org--{name}"
+ (repo / "blobs").mkdir(parents = True)
+ (repo / "snapshots" / "revision").mkdir(parents = True)
+ (repo / "blobs" / ETAG).symlink_to(blob_target)
+ pointer = repo / "snapshots" / "revision" / "weights.gguf"
+ pointer.symlink_to(f"../../blobs/{ETAG}")
+ return pointer
+
+
[email protected](os.name == "nt", reason = "shared blobs need symlinks")
+def test_shared_blob_resident_keeps_its_repo(monkeypatch, tmp_path):
+ cache = _cache(monkeypatch, tmp_path)
+ pointer = _repo(cache, "private", f"../../blobs/ab/{HASH}")
+ assert pointer.resolve() == cache / "blobs" / "ab" / HASH
+ assert access.cached_model_repo(str(pointer)) == "org/private"
+
+
[email protected](os.name == "nt", reason = "shared blobs need symlinks")
+def test_a_pointer_into_another_repo_is_still_refused(monkeypatch, tmp_path):
+ cache = _cache(monkeypatch, tmp_path)
+ other = cache / "models--org--other" / "blobs"
+ other.mkdir(parents = True)
+ (other / "real").write_bytes(b"")
+ pointer = _repo(cache, "private", "../../models--org--other/blobs/real")
+ assert access.cached_model_repo(str(pointer)) is None
+
+
[email protected](os.name == "nt", reason = "shared blobs need symlinks")
+def test_a_shared_store_outside_the_known_cache_is_still_refused(monkeypatch, tmp_path):
+ from utils import hf_cache_settings
+
+ cache = _cache(monkeypatch, tmp_path)
+ pointer = _repo(cache, "private", f"../../blobs/ab/{HASH}")
+ monkeypatch.setattr(hf_cache_settings, "known_hf_hub_caches", lambda: [tmp_path / "elsewhere"])
+ assert access.cached_model_repo(str(pointer)) is None
+
+
[email protected](os.name == "nt", reason = "shared blobs need symlinks")
+def test_a_repo_blob_pointing_outside_the_shared_store_is_refused(monkeypatch, tmp_path):
+ cache = _cache(monkeypatch, tmp_path)
+ (tmp_path / "loose").write_bytes(b"")
+ pointer = _repo(cache, "private", "../../../loose")
+ assert access.cached_model_repo(str(pointer)) is None
+
+
[email protected](os.name == "nt", reason = "shared blobs need symlinks")
[email protected]("store_entry", ["missing", "directory", "not-a-hash"])
+def test_a_store_entry_huggingface_hub_would_not_use_is_refused(monkeypatch, tmp_path, store_entry):
+ cache = _cache(monkeypatch, tmp_path)
+ if store_entry == "missing":
+ target = f"../../blobs/ab/{'ef' * 32}"
+ elif store_entry == "directory":
+ (cache / "blobs" / "ab" / ("ab" * 31 + "ef")).mkdir()
+ target = f"../../blobs/ab/{'ab' * 31 + 'ef'}"
+ else:
+ (cache / "blobs" / "ab" / "weights.gguf").write_bytes(b"")
+ target = "../../blobs/ab/weights.gguf"
+ pointer = _repo(cache, "private", target)
+ assert access.cached_model_repo(str(pointer)) is None
+
+
[email protected](os.name == "nt", reason = "shared blobs need symlinks")
+def test_a_granted_shared_blob_resident_is_visible(monkeypatch, tmp_path):
+ cache = _cache(monkeypatch, tmp_path)
+ pointer = _repo(cache, "private", f"../../blobs/ab/{HASH}")
+ monkeypatch.setattr(access, "managed_account", lambda: True)
+ monkeypatch.setattr(access, "model_allowed", lambda repo_id: True)
+ monkeypatch.setattr(access, "repo_is_public", lambda *args, **kwargs: False)
+ granted = {access._grant_key("org/private", "model")}
+ assert access.model_visible(str(pointer), grants = granted)
+ assert not access.model_visible(str(pointer), grants = set())
+ assert not access.model_visible(str(pointer), grants = {access._grant_key("org/other", "model")})