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.

Fix Studio hydrating synced GGUF files before selection (#9539)

Rafael Milewski8a68128d871d
diff --git a/studio/backend/hub/services/models/catalog_classification.py b/studio/backend/hub/services/models/catalog_classification.py
index 792838006b2..6220df48790 100644
--- a/studio/backend/hub/services/models/catalog_classification.py
+++ b/studio/backend/hub/services/models/catalog_classification.py
@@ -17,6 +17,7 @@ from hub.services.models.common import (
_local_path_can_chat,
)
from utils.gguf_archs import SPEECH_GGUF_ARCHS, is_speech_gguf_architecture
+from utils.paths.path_utils import file_contents_available_locally
_DIFFUSION_GGUF_ARCHS = frozenset({"flux", "flux2", "qwen_image", "qwenimage", "z_image", "zimage"})
@@ -63,6 +64,13 @@ def _is_h3_bundle_gguf_hint(hint: Optional[str]) -> bool:
def _gguf_architecture(path: str) -> Optional[str]:
+ # Every read inventory classification makes goes through here, so this is where "never open
+ # a cloud placeholder" holds: opening one recalls its whole payload. The task probes below
+ # ask the same question again because an unhydrated file needs a different ROUTE, not just a
+ # skipped read; the audio-type probe has no route to pick and relies on this alone. Load-time
+ # inspection calls read_gguf_architecture directly and still hydrates, on purpose.
+ if not file_contents_available_locally(path):
+ return None
from utils.models.gguf_metadata import read_gguf_architecture
return read_gguf_architecture(path)
@@ -90,6 +98,61 @@ def _video_family_buildable(family) -> bool:
return True
+def _hint_leaf(hint: str) -> str:
+ """The last path segment of *hint*, leaving a bare name or repo id untouched."""
+ return str(hint).strip().rstrip("/\\").rsplit("/", 1)[-1].rsplit("\\", 1)[-1]
+
+
+def _name_hint_media_task(
+ name_hints: tuple[Optional[str], ...], unmatched: Optional[str]
+) -> Optional[str]:
+ """Media task from name and path hints alone, for a GGUF whose header settles nothing.
+
+ Both callers are files whose architecture cannot answer the question: a denoiser that
+ declares a placeholder arch, and a cloud file we must not open. They differ only in what
+ an unrecognised name means, hence *unmatched*.
+ """
+ from core.inference.video_families import detect_video_family
+
+ for hint in name_hints:
+ family = detect_video_family(hint) if hint else None
+ if family is not None:
+ if not getattr(family, "is_moe", False) and _video_family_buildable(family):
+ return _VIDEO_GEN_TASK
+ return _UNSUPPORTED_DIFFUSION_TASK
+ from core.inference.diffusion_families import detect_family_for_pick
+
+ if any(detect_family_for_pick(hint) is not None for hint in name_hints if hint):
+ return (
+ "text-to-image" if _gguf_family_buildable(name_hints) else _UNSUPPORTED_DIFFUSION_TASK
+ )
+ return unmatched
+
+
+def _unhydrated_gguf_task(name_hints: tuple[Optional[str], ...]) -> Optional[str]:
+ """Task for a GGUF still held as a cloud placeholder, read from its name alone.
+
+ The pickers filter On Device rows on an exact task, so an unclassified denoiser drops out
+ of Images and Video -- the pages whose pick is what would hydrate it -- and lists in Chat
+ instead, where a background auto-load recalls it into llama.cpp. The name is the only
+ evidence available, and it is the same evidence _arch_to_task already routes a
+ placeholder-arch denoiser on.
+
+ Speech stays out on purpose. A row tagged automatic-speech-recognition is dropped from
+ every filesystem list, and a text-to-speech GGUF row whose codec is unknown fails Audio's
+ routing check closed, so guessing either from a name hides the model outright. Unknown
+ keeps it in Chat, which is where a GGUF with nothing but a name belongs.
+ """
+ if any(_is_h3_bundle_gguf_hint(hint) for hint in name_hints):
+ return _VIDEO_GEN_TASK
+ # Leaves only. A filesystem row hands over its whole path, and family detection matches a
+ # keyword in ANY segment of it, so a chat GGUF under .../FLUX.1-dev-GGUF/extra/ read as
+ # text-to-image. With an architecture that mismatch only picks the wrong family; here the
+ # name is the entire case, so an ancestor directory -- the user's shelf, not this model --
+ # must not decide it.
+ return _name_hint_media_task(tuple(_hint_leaf(hint) for hint in name_hints if hint), None)
+
+
def _arch_to_task(arch: Optional[str], name_hints: tuple[Optional[str], ...] = ()) -> Optional[str]:
if any(_is_h3_bundle_gguf_hint(hint) for hint in name_hints):
return _VIDEO_GEN_TASK
@@ -105,23 +168,7 @@ def _arch_to_task(arch: Optional[str], name_hints: tuple[Optional[str], ...] = (
):
return _SPEECH_TASK
if normalized in _PLACEHOLDER_DIFFUSION_GGUF_ARCHS:
- from core.inference.video_families import detect_video_family
-
- for hint in name_hints:
- family = detect_video_family(hint) if hint else None
- if family is not None:
- if not getattr(family, "is_moe", False) and _video_family_buildable(family):
- return _VIDEO_GEN_TASK
- return _UNSUPPORTED_DIFFUSION_TASK
- from core.inference.diffusion_families import detect_family_for_pick
-
- if any(detect_family_for_pick(hint) is not None for hint in name_hints if hint):
- return (
- "text-to-image"
- if _gguf_family_buildable(name_hints)
- else _UNSUPPORTED_DIFFUSION_TASK
- )
- return _UNSUPPORTED_DIFFUSION_TASK
+ return _name_hint_media_task(name_hints, _UNSUPPORTED_DIFFUSION_TASK)
if is_speech_gguf_architecture(normalized):
return _SPEECH_TASK
if normalized in _DIFFUSION_GGUF_ARCHS:
@@ -237,8 +284,14 @@ def _gguf_folder_task(
if index and time.monotonic() >= read_deadline:
complete = False
break
+ hints = id_hints + (path.name,)
try:
- task = _arch_to_task(_gguf_architecture(str(path)), name_hints = id_hints + (path.name,))
+ if file_contents_available_locally(path):
+ task = _arch_to_task(_gguf_architecture(str(path)), name_hints = hints)
+ else:
+ # Its header stays unread, so a name that says nothing leaves the candidate
+ # unclassified rather than voting text-generation for the whole folder.
+ task = _unhydrated_gguf_task(hints)
except Exception:
# Unread, so unranked: this file might have been the runnable sibling.
complete = False
@@ -304,9 +357,12 @@ def _gguf_path_task(path: str | Path, id_hints: tuple[Optional[str], ...] = ())
model_path = Path(path)
try:
if model_path.suffix.lower() == ".gguf" and model_path.is_file():
+ hints = id_hints + (model_path.name,)
+ if not file_contents_available_locally(model_path):
+ return _unhydrated_gguf_task(hints)
return _arch_to_task(
_gguf_architecture(str(model_path)),
- name_hints = id_hints + (model_path.name,),
+ name_hints = hints,
)
return _gguf_folder_task(model_path, id_hints)
except Exception:
diff --git a/studio/backend/hub/utils/gguf.py b/studio/backend/hub/utils/gguf.py
index a963022a6b5..123cf5f2a07 100644
--- a/studio/backend/hub/utils/gguf.py
+++ b/studio/backend/hub/utils/gguf.py
@@ -14,6 +14,7 @@ from typing import Optional, Sequence
from loggers import get_logger
from utils.paths.path_utils import (
drop_shadowed_appledouble_names as _drop_shadowed_appledouble_names,
+ file_contents_available_locally,
has_appledouble_magic,
is_appledouble_metadata,
)
@@ -1082,10 +1083,15 @@ def list_local_gguf_variants(
if is_imatrix_filename(file.name):
continue
if is_mmproj_filename(file.name):
- # An empty projector is an interrupted download; an audio-only one is not vision.
+ # Header metadata distinguishes vision projectors from audio-only ones. Read it
+ # only when Windows reports the file fully present; opening a cloud placeholder
+ # would recall the projector during discovery.
try:
+ info = file.stat()
has_vision = has_vision or (
- file.stat().st_size > 0 and mmproj_accepts_image(str(file))
+ info.st_size > 0
+ and file_contents_available_locally(file, info)
+ and mmproj_accepts_image(str(file))
)
except OSError:
pass
diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.py
index e4434546b64..77941fb8967 100644
--- a/studio/backend/routes/models.py
+++ b/studio/backend/routes/models.py
@@ -3568,6 +3568,7 @@ def _read_native_context_length(repo_id: str, is_local: bool) -> Optional[int]:
"""
try:
from utils.models.gguf_metadata import read_gguf_context_length
+ from utils.paths.path_utils import file_contents_available_locally
# Before cache discovery (also filesystem I/O): started after, a slow enumeration would hand the walk a fresh budget.
deadline = time.monotonic() + _NATIVE_CONTEXT_READ_TIMEOUT_SECONDS
@@ -3587,7 +3588,9 @@ def _read_native_context_length(repo_id: str, is_local: bool) -> Optional[int]:
if time.monotonic() >= deadline:
logger.debug("native context read for '%s' out of budget", repo_id)
return None
- if _is_mmproj_filename(f.name):
+ if _is_mmproj_filename(f.name) or not file_contents_available_locally(f):
+ # Opening a cloud placeholder recalls its data. It keeps its variant row,
+ # but has no context metadata until the file is hydrated.
continue
n = read_gguf_context_length(str(f))
if n:
diff --git a/studio/backend/tests/test_cached_gguf_routes.py b/studio/backend/tests/test_cached_gguf_routes.py
index 55642391b06..17066171136 100644
--- a/studio/backend/tests/test_cached_gguf_routes.py
+++ b/studio/backend/tests/test_cached_gguf_routes.py
@@ -2625,6 +2625,24 @@ def test_native_context_read_still_reports_a_length_within_budget(monkeypatch, t
assert models_route._read_native_context_length(str(tmp_path), is_local = True) == 8192
+def test_native_context_read_skips_online_only_file(monkeypatch, tmp_path):
+ """A cloud placeholder keeps its logical filename without being opened for metadata."""
+ gguf = tmp_path / "model-Q4_K_M.gguf"
+ gguf.write_bytes(b"placeholder")
+ monkeypatch.setattr(models_route, "_iter_gguf_paths", lambda root, deadline = None: iter([gguf]))
+ monkeypatch.setattr(
+ "utils.paths.path_utils.file_contents_available_locally",
+ lambda path, stat_result = None: False,
+ )
+
+ def forbidden(_path):
+ raise AssertionError("online-only GGUF was opened")
+
+ monkeypatch.setattr("utils.models.gguf_metadata.read_gguf_context_length", forbidden)
+
+ assert models_route._read_native_context_length(str(tmp_path), is_local = True) is None
+
+
def test_gguf_variants_ignore_big_endian_siblings(monkeypatch, tmp_path):
siblings = [
SimpleNamespace(rfilename = "model-Q4_K_M-be.gguf", size = 100),
diff --git a/studio/backend/tests/test_gguf_variants_local_resolution.py b/studio/backend/tests/test_gguf_variants_local_resolution.py
index 27e08585b2e..2dbdd826717 100644
--- a/studio/backend/tests/test_gguf_variants_local_resolution.py
+++ b/studio/backend/tests/test_gguf_variants_local_resolution.py
@@ -5,6 +5,7 @@
import asyncio
import os
+from pathlib import Path
import pytest
@@ -93,6 +94,32 @@ def test_an_audio_only_projector_does_not_flag_vision(in_tmp_cwd):
assert response.has_vision is False
+def test_online_only_projector_is_not_opened(in_tmp_cwd, monkeypatch):
+ """Projector discovery skips content only when metadata marks it unhydrated."""
+ from hub.utils import gguf
+
+ (in_tmp_cwd / "config.json").write_text("{}")
+ weight = in_tmp_cwd / "model-Q4_K_M.gguf"
+ weight.write_bytes(b"GGUF")
+ projector = in_tmp_cwd / "mmproj-F16.gguf"
+ projector.write_bytes(b"online-only placeholder")
+
+ monkeypatch.setattr(
+ gguf,
+ "file_contents_available_locally",
+ lambda path, stat_result = None: Path(path) != projector,
+ )
+
+ def forbidden(_path):
+ raise AssertionError("online-only projector was opened")
+
+ monkeypatch.setattr(gguf, "mmproj_accepts_image", forbidden)
+ response = _variants(os.fspath(weight))
+
+ assert [variant.quant for variant in response.variants] == ["Q4_K_M"]
+ assert response.has_vision is False
+
+
@pytest.mark.parametrize(
"relpath",
[
diff --git a/studio/backend/tests/test_local_model_format.py b/studio/backend/tests/test_local_model_format.py
index 7363043390d..5ccfe327fc1 100644
--- a/studio/backend/tests/test_local_model_format.py
+++ b/studio/backend/tests/test_local_model_format.py
@@ -273,6 +273,166 @@ def _local(
)
+def test_windows_cloud_recall_attributes_are_not_local():
+ from utils.paths.path_utils import file_contents_available_locally
+
+ # Synology Drive exposes an online-only GGUF as 0x400020 through Python's
+ # os.stat(), and as 0x401620 through directory enumeration. Keep the individual
+ # Windows recall flags too so another cloud provider cannot regress unnoticed.
+ for attributes in (
+ 0x00400020,
+ 0x00401620,
+ 0x00001000,
+ 0x00040000,
+ 0x00400000,
+ ):
+ assert not file_contents_available_locally(
+ "unused", types.SimpleNamespace(st_file_attributes = attributes)
+ )
+
+ # A hydrated Synology file remains a reparse point (0x420), and UNPINNED is
+ # user intent rather than proof that bytes are absent. Both must retain real
+ # architecture, context, and projector reads.
+ for attributes in (0x00000420, 0x00100000):
+ assert file_contents_available_locally(
+ "unused", types.SimpleNamespace(st_file_attributes = attributes)
+ )
+
+
+def test_local_gguf_task_reads_present_header(tmp_path, monkeypatch):
+ """Fully present files retain architecture-based task detection."""
+ from hub.services.models import catalog_classification as classification
+
+ gguf = _touch(tmp_path / "generic-Q4_K_M.gguf")
+ reads = []
+ monkeypatch.setattr(classification, "file_contents_available_locally", lambda _path: True)
+ monkeypatch.setattr(
+ classification,
+ "_gguf_architecture",
+ lambda path: reads.append(path) or "llama",
+ )
+
+ model = _local(
+ gguf,
+ model_format = "gguf",
+ display_name = "generic",
+ id = "generic-file-id",
+ )
+
+ assert models_route._local_model_task(model) == "text-generation"
+ assert reads == [str(gguf)]
+
+
+def test_local_gguf_task_skips_online_only_contents(tmp_path, monkeypatch):
+ """Cloud placeholders stay discoverable by name without opening their data."""
+ from hub.services.models import catalog_classification as classification
+
+ def forbidden(*_args, **_kwargs):
+ raise AssertionError("local GGUF listing touched placeholder contents")
+
+ monkeypatch.setattr(classification, "file_contents_available_locally", lambda _path: False)
+ monkeypatch.setattr(classification, "_gguf_architecture", forbidden)
+
+ gguf = _touch(tmp_path / "generic-Q4_K_M.gguf")
+ model = _local(
+ gguf,
+ model_format = "gguf",
+ display_name = "generic",
+ id = "generic-file-id",
+ )
+
+ assert models_route._local_model_task(model) is None
+
+
+def test_local_classification_never_opens_an_online_only_gguf(tmp_path, monkeypatch):
+ """The whole probe, not just the task half.
+
+ ``_local_model_classification`` falls through to the audio-type probe whenever the task
+ comes back None, which for a placeholder is every time, and that probe reads an
+ architecture of its own. Asserting on ``_local_model_task`` alone leaves the listing
+ hydrating exactly the files it stopped classifying, a folder row once per sibling."""
+ from hub.services.models import catalog_classification as classification
+ from utils.models import gguf_metadata
+
+ single = _touch(tmp_path / "single" / "generic-Q4_K_M.gguf")
+ folder = tmp_path / "generic-GGUF"
+ for quant in ("Q4_K_M", "Q8_0"):
+ _touch(folder / f"generic-{quant}.gguf")
+
+ opened: list[str] = []
+ monkeypatch.setattr(
+ classification, "file_contents_available_locally", lambda *_args, **_kwargs: False
+ )
+ monkeypatch.setattr(
+ gguf_metadata,
+ "read_gguf_architecture",
+ lambda path: opened.append(path) or "llama",
+ )
+
+ for path in (single, folder):
+ model = _local(path, model_format = "gguf", display_name = "generic", id = str(path))
+ assert classification._local_model_classification(model) == (None, None)
+ assert opened == []
+
+
+def test_an_unhydrated_denoiser_keeps_the_picker_that_would_hydrate_it(tmp_path, monkeypatch):
+ """Images and Video filter On Device rows on an exact task, so an unclassified denoiser
+ is not reachable from the one page whose pick would pull it down, and lists in Chat
+ instead. The filename carries the family, and it is read without opening the file."""
+ from hub.services.models import catalog_classification as classification
+
+ def forbidden(*_args, **_kwargs):
+ raise AssertionError("placeholder contents were read to classify it")
+
+ monkeypatch.setattr(
+ classification, "file_contents_available_locally", lambda *_args, **_kwargs: False
+ )
+ monkeypatch.setattr(classification, "_gguf_architecture", forbidden)
+
+ for name, expected in (
+ ("flux1-dev-Q4_K_M.gguf", "text-to-image"),
+ ("z-image-turbo-Q4_K_M.gguf", "text-to-image"),
+ ("ltx-video-2b-Q4_K_M.gguf", "text-to-video"),
+ # No family in the name: unknown, which keeps the row in Chat where a GGUF with
+ # nothing but a name belongs, rather than guessing it into a media page.
+ ("qwen3-4b-instruct-Q4_K_M.gguf", None),
+ ):
+ gguf = _touch(tmp_path / name)
+ model = _local(gguf, model_format = "gguf", display_name = name, id = name)
+ assert models_route._local_model_task(model) == expected, name
+
+
+def test_an_ancestor_directory_does_not_name_an_unhydrated_gguf(tmp_path, monkeypatch):
+ """A filesystem row's id is its whole path, and family detection matches a keyword in any
+ segment of it. With an architecture that mismatch only picks the wrong family; for a
+ placeholder the name is the entire case, so a shelf named after a family would file every
+ chat GGUF stored under it as an image or video model."""
+ from hub.services.models import catalog_classification as classification
+
+ def forbidden(*_args, **_kwargs):
+ raise AssertionError("placeholder contents were read to classify it")
+
+ monkeypatch.setattr(
+ classification, "file_contents_available_locally", lambda *_args, **_kwargs: False
+ )
+ monkeypatch.setattr(classification, "_gguf_architecture", forbidden)
+
+ for relative in (
+ "FLUX.1-dev-GGUF/extra/qwen3-4b/qwen3-Q4_K_M.gguf",
+ "ltx-2/qwen3-4b/qwen3-Q4_K_M.gguf",
+ ):
+ gguf = _touch(tmp_path / relative)
+ model = _local(gguf, model_format = "gguf", display_name = gguf.name, id = str(gguf))
+ assert models_route._local_model_task(model) is None, relative
+
+ # The control, and the shape a scanned GGUF folder actually takes: the row IS the
+ # directory, so its own leaf names it and the family survives.
+ folder = tmp_path / "FLUX.1-dev-GGUF"
+ _touch(folder / "diffusion_model-Q4_K_M.gguf")
+ row = _local(folder, model_format = "gguf", display_name = folder.name, id = str(folder))
+ assert models_route._local_model_task(row) == "text-to-image"
+
+
def test_local_task_tags_family_named_pipeline_dir(tmp_path):
# A local diffusers pipeline whose id resolves to a supported image family loads fine, so tag it and the Images picker keeps it.
d = tmp_path / "flux-pipeline"
diff --git a/studio/backend/utils/paths/path_utils.py b/studio/backend/utils/paths/path_utils.py
index 78d18b0667b..6115c81e86f 100644
--- a/studio/backend/utils/paths/path_utils.py
+++ b/studio/backend/utils/paths/path_utils.py
@@ -14,6 +14,28 @@ from loggers import get_logger
logger = get_logger(__name__)
+# Opening a cloud placeholder for data recalls it. These attributes are available through
+# ``stat_result.st_file_attributes`` on Windows without reading file contents.
+_WINDOWS_CONTENT_RECALL_ATTRIBUTES = (
+ 0x00001000 # FILE_ATTRIBUTE_OFFLINE
+ | 0x00040000 # FILE_ATTRIBUTE_RECALL_ON_OPEN
+ | 0x00400000 # FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS
+)
+
+
+def file_contents_available_locally(path, stat_result = None) -> bool:
+ """Whether opening *path* can read data without recalling a cloud placeholder.
+
+ Non-Windows files have no ``st_file_attributes`` and are treated as local. An
+ inaccessible path is not safe to open during inventory discovery.
+ """
+ try:
+ info = stat_result if stat_result is not None else os.stat(path)
+ except OSError:
+ return False
+ attributes = int(getattr(info, "st_file_attributes", 0) or 0)
+ return not bool(attributes & _WINDOWS_CONTENT_RECALL_ATTRIBUTES)
+
# ── macOS Finder metadata companions ───────────────────────────
# A volume without native xattrs (exFAT, FAT, most SMB and NFS) makes macOS keep a file's xattrs