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: Make installed Ollama models available through the API (#10763)

Nilay128659085bc9
diff --git a/studio/backend/core/inference/local_model_resolver.py b/studio/backend/core/inference/local_model_resolver.py
index 9ffbdad2df2..a9ee3e4ec03 100644
--- a/studio/backend/core/inference/local_model_resolver.py
+++ b/studio/backend/core/inference/local_model_resolver.py
@@ -101,7 +101,11 @@ def _advertised_loader_id(info) -> Optional[str]:
"""The id to advertise for a scanned model: prefer a client-facing alias over
an absolute filesystem path so /v1/models and the override key never expose a
host path (the ./models and LM Studio scanners report the path as info.id)."""
+ from hub.services.models.ollama import is_ollama_manifest_ref
+
raw_id = getattr(info, "id", None)
+ if isinstance(raw_id, str) and is_ollama_manifest_ref(raw_id):
+ return getattr(info, "model_id", None)
if not raw_id or not _is_abs_path_id(raw_id):
return raw_id
for alt in (getattr(info, "model_id", None), getattr(info, "display_name", None)):
@@ -579,6 +583,19 @@ def _local_weights_entry(loader_id: str, info) -> Optional[_LocalGgufEntry]:
def _local_servable_entry(loader_id: str, info) -> Optional[_LocalGgufEntry]:
"""Entry for whichever backend can serve *info* from disk, GGUF first."""
+ from hub.services.models.ollama import is_ollama_manifest_ref, ollama_model_ref_files
+
+ raw_id = getattr(info, "id", None)
+ if isinstance(raw_id, str) and is_ollama_manifest_ref(raw_id):
+ if getattr(info, "source", None) != "ollama":
+ return None
+ # Raises when the tag's layers are gone or unsupported, withholding rather than advertising.
+ try:
+ ollama_model_ref_files(raw_id)
+ except (OSError, ValueError):
+ return None
+ # No quants: an Ollama tag names one file, so there is no ":<quant>" to pin.
+ return _LocalGgufEntry(loader_id, raw_id, ())
return _local_gguf_entry(loader_id, info) or _local_weights_entry(loader_id, info)
@@ -593,15 +610,32 @@ def local_servable_model(info) -> Optional[tuple[bool, tuple[str, ...]]]:
from pathlib import Path
path = getattr(info, "path", None)
- # Ollama-link entries come from a scanner _build_index intentionally skips (it creates symlinks on the request
- # path), so their advertised ids never resolve. Don't report them as servable, or /v1/models would list unswitchable
- # models.
+ # A link an earlier load materialized, rescanned: the manifest row already has those weights.
if isinstance(path, str) and any(
seg in (".studio_links", "ollama_links") for seg in Path(path).parts
):
return None
entry = _local_servable_entry(getattr(info, "id", "") or "", info)
- return (entry.is_gguf, entry.variants) if entry is not None else None
+ if entry is None:
+ return None
+ if not _advertises_this_ollama_row(info):
+ return None
+ return (entry.is_gguf, entry.variants)
+
+
+def _advertises_this_ollama_row(info) -> bool:
+ """Whether an Ollama row's catalog id is the one the resolver loads for it: two roots can hold
+ one tag. Asks the index, so it cannot be called from inside a scan."""
+ from hub.services.models.ollama import is_ollama_manifest_ref
+
+ raw_id = getattr(info, "id", None)
+ if not isinstance(raw_id, str) or not is_ollama_manifest_ref(raw_id):
+ return True
+ model_id = getattr(info, "model_id", None)
+ if not model_id:
+ return False
+ resolved = resolve_local_gguf(model_id)
+ return bool(resolved and resolved[0] == raw_id)
def local_load_dir(path: Optional[str]) -> Optional[str]:
@@ -626,9 +660,8 @@ def _build_index() -> dict[str, _LocalGgufEntry]:
Scans the same roots Unsloth's model picker lists (./models, the active plus
legacy/default HF caches, LM Studio and Hermes dirs, and user scan folders) so a named
- local model is never missed and silently served as the loaded one. Ollama's
- scanner is skipped: it creates symlinks as a side effect and this runs on the
- request path.
+ local model is never missed and silently served as the loaded one. The Ollama scan only reads
+ manifests: the ``.gguf`` link its blobs need is materialized by the load.
"""
# Lazy import: routes.models imports core.inference, so import at call time.
from pathlib import Path
@@ -636,6 +669,7 @@ def _build_index() -> dict[str, _LocalGgufEntry]:
_scan_models_dir,
_scan_hf_cache,
_scan_lmstudio_dir,
+ _scan_ollama_dir,
_resolve_hf_cache_dir,
_is_hidden_model,
)
@@ -703,6 +737,12 @@ def _build_index() -> dict[str, _LocalGgufEntry]:
found += scan_hermes_dir(hermes_dir)
except Exception as exc:
logger.debug("auto-switch: Hermes scan failed: %s", exc)
+ try:
+ from utils.paths import ollama_model_dirs
+ for ollama_dir in ollama_model_dirs():
+ found += _scan_ollama_dir(ollama_dir, materialize_links = False)
+ except Exception as exc:
+ logger.debug("auto-switch: Ollama scan failed: %s", exc)
try:
from storage.studio_db import list_scan_folders
@@ -711,7 +751,10 @@ def _build_index() -> dict[str, _LocalGgufEntry]:
try:
fp = Path(folder["path"])
custom_found += dedupe_custom_gguf_rows(
- _scan_models_dir(fp, limit = 200) + _scan_hf_once(fp) + _scan_lmstudio_dir(fp)
+ _scan_models_dir(fp, limit = 200)
+ + _scan_hf_once(fp)
+ + _scan_lmstudio_dir(fp)
+ + _scan_ollama_dir(fp, limit = 200, materialize_links = False)
)
except Exception as exc:
logger.debug("auto-switch: scan folder %r failed: %s", folder, exc)
@@ -982,7 +1025,7 @@ def resolve_local_gguf(
) -> Optional[tuple]:
"""Return ``(load_path, gguf_variant, loader_id)`` for a local match, else None.
- ``load_path`` is the concrete on-disk path to hand /load (so it never fetches a remote),
+ ``load_path`` is the local path or ``ollama-manifest:`` ref to hand /load (never a remote),
``loader_id`` is the advertised id used as the launch-override key, and ``gguf_variant`` is None
for a non-GGUF checkpoint, which has no quant to pin. ``requested`` is ``repo`` or
``repo:VARIANT``: an exact id match wins first (so ids containing a colon still resolve), else
diff --git a/studio/backend/hub/services/models/catalog_classification.py b/studio/backend/hub/services/models/catalog_classification.py
index 22c6c5a3445..5824e15e83d 100644
--- a/studio/backend/hub/services/models/catalog_classification.py
+++ b/studio/backend/hub/services/models/catalog_classification.py
@@ -40,12 +40,13 @@ _LOADABLE_MEDIA_GGUF_TASKS = frozenset({"text-to-image", _VIDEO_GEN_TASK})
_MAX_TASK_CLASSIFY_GGUFS = 64
_TASK_CLASSIFY_WALK_SECONDS = 0.75
_TASK_CLASSIFY_READ_SECONDS = 1.5
+# ":" separates an Ollama size tag ("qwen3-asr:0.6b"), where a filename would use "-".
_QWEN3_ASR_HINT = re.compile(
- r"(?<![a-z0-9])qwen3[-_. ]*asr[-_. ]*(?:0[._]6|1[._]7)b(?![a-z0-9])",
+ r"(?<![a-z0-9])qwen3[-_. ]*asr[-_. :]*(?:0[._]6|1[._]7)b(?![a-z0-9])",
re.IGNORECASE,
)
_ORPHEUS_GGUF_HINT = re.compile(
- r"(?<![a-z0-9])orpheus[-_. ]*3b(?![a-z0-9])",
+ r"(?<![a-z0-9])orpheus[-_. :]*3b(?![a-z0-9])",
re.IGNORECASE,
)
@@ -342,11 +343,8 @@ def _gguf_path_audio_type(
) -> Optional[str]:
model_path = Path(path)
try:
- paths = (
- [model_path]
- if model_path.suffix.lower() == ".gguf" and model_path.is_file()
- else _iter_gguf_paths(model_path)
- )
+ # No extension check: an Ollama model is a blob named by its digest.
+ paths = [model_path] if model_path.is_file() else _iter_gguf_paths(model_path)
for gguf_path in paths:
audio_type = _arch_to_audio_type(
_gguf_architecture(str(gguf_path)),
@@ -370,7 +368,7 @@ def _repo_gguf_audio_type(repo_info, selected: Optional[Path] = None) -> Optiona
def _gguf_path_task(path: str | Path, id_hints: tuple[Optional[str], ...] = ()) -> Optional[str]:
model_path = Path(path)
try:
- if model_path.suffix.lower() == ".gguf" and model_path.is_file():
+ if model_path.is_file():
hints = id_hints + (model_path.name,)
if not file_contents_available_locally(model_path):
return _unhydrated_gguf_task(hints)
diff --git a/studio/backend/hub/services/models/ollama.py b/studio/backend/hub/services/models/ollama.py
index 648c549b24f..67335e0a594 100644
--- a/studio/backend/hub/services/models/ollama.py
+++ b/studio/backend/hub/services/models/ollama.py
@@ -40,6 +40,7 @@ from hub.utils.paths import (
logger = get_logger(__name__)
_OLLAMA_MANIFEST_REF_PREFIX = "ollama-manifest:"
+_OLLAMA_LINK_DIR_NAMES = frozenset((".studio_links", "ollama_links"))
_OLLAMA_BLOB_NAME_CHARS = frozenset(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._+-"
)
@@ -83,7 +84,7 @@ _OLLAMA_ADMITTED_LAYER_MEDIA_TYPES = (
| _OLLAMA_IGNORABLE_LAYER_MEDIA_TYPES
)
-_OLLAMA_MATERIALIZE_LOCKS: dict[str, threading.Lock] = {}
+_OLLAMA_MATERIALIZE_LOCKS: dict[tuple, threading.Lock] = {}
_OLLAMA_MATERIALIZE_LOCKS_GUARD = threading.Lock()
@@ -160,9 +161,21 @@ def _contained_link_path(link_dir: Path, link_name: str) -> Optional[Path]:
return link_path
-def _ollama_links_dir(ollama_dir: Path) -> Optional[Path]:
- """Writable directory for Ollama ``.gguf`` symlinks. Prefers ``<ollama_dir>/.studio_links/`` next to the blobs; falls back to Unsloth's cache (read-only system installs), then the temp dir (sandboxed installs)."""
+def _ollama_links_roots(ollama_dir: Path) -> tuple[Path, ...]:
+ """Where *ollama_dir*'s ``.gguf`` links can live, best first: beside the blobs, then Unsloth's cache (read-only installs), then the temp dir (sandboxed installs)."""
+ # Hashed so two Ollama roots cannot collide. A cache path, not a security boundary.
+ try:
+ digest = hashlib.sha256(str(ollama_dir.resolve()).encode()).hexdigest()[:12]
+ except (OSError, RuntimeError):
+ digest = "default"
+ return (
+ ollama_dir / ".studio_links",
+ cache_root() / "ollama_links" / digest,
+ tmp_root() / "ollama_links" / digest,
+ )
+
+def _ollama_links_dir(ollama_dir: Path) -> Optional[Path]:
def _ensure_writable_dir(path: Path) -> Optional[Path]:
try:
path.mkdir(parents = True, exist_ok = True)
@@ -174,24 +187,9 @@ def _ollama_links_dir(ollama_dir: Path) -> Optional[Path]:
logger.debug("Ollama link dir %s is not writable: %s", path, e)
return None
- primary = ollama_dir / ".studio_links"
- if _ensure_writable_dir(primary) is not None:
- return primary
-
- # Namespace by a hash of the ollama_dir so two different Ollama roots do not collide. A cache path,
- # not a security boundary.
- try:
- digest = hashlib.sha256(str(ollama_dir.resolve()).encode()).hexdigest()[:12]
- except (OSError, RuntimeError):
- digest = "default"
-
- fallback = cache_root() / "ollama_links" / digest
- if _ensure_writable_dir(fallback) is not None:
- return fallback
-
- tmp_fallback = tmp_root() / "ollama_links" / digest
- if _ensure_writable_dir(tmp_fallback) is not None:
- return tmp_fallback
+ for candidate in _ollama_links_roots(ollama_dir):
+ if _ensure_writable_dir(candidate) is not None:
+ return candidate
logger.warning(
"Could not create a writable Ollama link directory for %s",
@@ -257,6 +255,21 @@ def _make_ollama_blob_link(link_dir: Path, link_name: str, target: Path) -> Opti
return None
+def _manifest_rel_path(tag_file: Path, manifests_root: Path) -> Optional[Path]:
+ try:
+ return tag_file.relative_to(manifests_root)
+ except ValueError:
+ # ``manifests`` can be a symlink: a reference carries the canonical path, a scan its own.
+ try:
+ return Path(os.path.realpath(tag_file)).relative_to(os.path.realpath(manifests_root))
+ except (OSError, ValueError):
+ return None
+
+
+def _manifest_stem_hash(rel: Path) -> str:
+ return hashlib.sha256(rel.as_posix().encode()).hexdigest()[:10]
+
+
def _ollama_model_info_from_manifest(
ollama_dir: Path,
tag_file: Path,
@@ -264,13 +277,14 @@ def _ollama_model_info_from_manifest(
materialize_links: bool = False,
links_root: Optional[Path] = None,
reject_unsupported_layers: bool = False,
+ existing_links_only: bool = False,
+ manifest: Optional[dict] = None,
) -> Optional[LocalModelInfo]:
manifests_root = ollama_dir / "manifests"
blobs_dir = ollama_dir / "blobs"
- try:
- rel = tag_file.relative_to(manifests_root)
- except ValueError:
+ rel = _manifest_rel_path(tag_file, manifests_root)
+ if rel is None:
return None
parts = rel.parts
if len(parts) < 3:
@@ -297,10 +311,11 @@ def _ollama_model_info_from_manifest(
logger.debug("Skipping %s (%s)", tag_file, message)
return None
- try:
- manifest = json.loads(tag_file.read_text(encoding = "utf-8-sig"))
- except (json.JSONDecodeError, OSError, UnicodeDecodeError) as e:
- return invalid_manifest(str(e))
+ if manifest is None:
+ try:
+ manifest = json.loads(tag_file.read_text(encoding = "utf-8-sig"))
+ except (json.JSONDecodeError, OSError, UnicodeDecodeError) as e:
+ return invalid_manifest(str(e))
if not isinstance(manifest, dict):
return invalid_manifest("top level must be a JSON object")
@@ -344,8 +359,7 @@ def _ollama_model_info_from_manifest(
model_blob: Optional[Path] = None
projector_blob: Optional[Path] = None
gguf_link_path: Optional[str] = None
- stem_hash = hashlib.sha256(rel.as_posix().encode()).hexdigest()[:10]
- model_link_dir = links_root / stem_hash if links_root is not None else None
+ model_link_dir = links_root / _manifest_stem_hash(rel) if links_root is not None else None
safe_name = repo_name.replace("/", "-")
for layer in layers:
@@ -370,7 +384,16 @@ def _ollama_model_info_from_manifest(
if model_blob is None:
return invalid_manifest("model blob is missing")
- if materialize_links:
+ if materialize_links and existing_links_only:
+ link = (
+ _contained_link_path(model_link_dir, f"{safe_name}-{tag}.gguf")
+ if model_link_dir is not None
+ else None
+ )
+ if link is None or not _safe_is_file(link):
+ return None
+ gguf_link_path = str(link)
+ elif materialize_links:
if model_link_dir is None:
return invalid_manifest("link directory is unavailable")
link_name = f"{safe_name}-{tag}.gguf"
@@ -508,12 +531,20 @@ def scan_ollama_dir(
if not _safe_is_file(tag_file):
continue
- info = _ollama_model_info_from_manifest(
- ollama_dir,
- tag_file,
- materialize_links = materialize_links,
- links_root = links_root,
- )
+ lock = _materialization_lock(tag_file, ollama_dir) if materialize_links else None
+ # A load holds this tag; its lease keeps the link it made, so report that, never block.
+ leased = lock is not None and not lock.acquire(blocking = False)
+ try:
+ info = _ollama_model_info_from_manifest(
+ ollama_dir,
+ tag_file,
+ materialize_links = materialize_links,
+ links_root = links_root,
+ existing_links_only = leased,
+ )
+ finally:
+ if lock is not None and not leased:
+ lock.release()
if info is None:
continue
found.append(info)
@@ -524,8 +555,7 @@ def scan_ollama_dir(
return found
-def _ollama_dir_for_manifest(tag_file: Path) -> Optional[Path]:
- """Return a discovered or registered Ollama root containing *tag_file*."""
+def _known_ollama_dirs() -> List[Path]:
known_dirs = list(ollama_model_dirs())
try:
from hub.storage.scan_folders import list_scan_folders
@@ -536,7 +566,36 @@ def _ollama_dir_for_manifest(tag_file: Path) -> Optional[Path]:
)
except Exception as e:
logger.debug("Could not load registered Ollama roots: %s", e)
- for ollama_dir in known_dirs:
+ return known_dirs
+
+
+def ollama_manifest_ref_for_path(model_path: str) -> Optional[str]:
+ """The manifest reference *model_path* names, or None when it names no Ollama tag."""
+ if is_ollama_manifest_ref(model_path):
+ return model_path
+ link = Path(model_path)
+ if not any(part in _OLLAMA_LINK_DIR_NAMES for part in link.parts):
+ return None
+ for ollama_dir in _known_ollama_dirs():
+ if not any(
+ path_is_same_or_child(link.parent, root) for root in _ollama_links_roots(ollama_dir)
+ ):
+ continue
+ manifests_root = ollama_dir / "manifests"
+ try:
+ for tag_file in manifests_root.rglob("*"):
+ if not _safe_is_file(tag_file):
+ continue
+ rel = _manifest_rel_path(tag_file, manifests_root)
+ if rel is not None and _manifest_stem_hash(rel) == link.parent.name:
+ return _ollama_manifest_ref(tag_file)
+ except OSError as e:
+ logger.debug("Could not walk Ollama manifests under %s: %s", manifests_root, e)
+ return None
+
+
+def _ollama_dir_for_manifest(tag_file: Path) -> Optional[Path]:
+ for ollama_dir in _known_ollama_dirs():
if path_is_same_or_child(tag_file, ollama_dir / "manifests"):
return ollama_dir
return None
@@ -559,8 +618,55 @@ def _validated_ollama_manifest_location(ref: str) -> tuple[Path, Path]:
return tag_file, canonical_ollama_dir
-def _materialization_lock(tag_file: Path) -> threading.Lock:
- key = os.path.normcase(str(tag_file))
+def _ollama_model_ref_info(ref: str) -> tuple[Path, dict, LocalModelInfo]:
+ """Ollama root, parsed manifest and model row for *ref*, from a single read: a pull between two
+ reads would pair one version's weights with another's projector. Raises on nothing loadable."""
+ tag_file, ollama_dir = _validated_ollama_manifest_location(ref)
+ try:
+ manifest = json.loads(tag_file.read_text(encoding = "utf-8-sig"))
+ except (json.JSONDecodeError, OSError, UnicodeDecodeError) as e:
+ raise ValueError(f"Could not read Ollama manifest: {e}") from e
+ # A manifest of JSON ``null`` is indistinguishable from passing none below, which re-reads.
+ if not isinstance(manifest, dict):
+ raise ValueError("Invalid Ollama manifest: top level must be a JSON object")
+ info = _ollama_model_info_from_manifest(
+ ollama_dir,
+ tag_file,
+ materialize_links = False,
+ reject_unsupported_layers = True,
+ manifest = manifest,
+ )
+ if info is None:
+ raise ValueError("Could not resolve Ollama model from manifest")
+ return ollama_dir, manifest, info
+
+
+def ollama_model_ref_public_id(ref: str) -> str:
+ """The ``ollama/<repo>:<tag>`` id clients see for *ref*, whose own form is internal."""
+ _, _, info = _ollama_model_ref_info(ref)
+ return info.model_id
+
+
+def ollama_model_ref_files(ref: str) -> tuple[str, Optional[str]]:
+ """``(weights blob, projector blob or None)`` for *ref*: the blobs, not the links a load makes."""
+ ollama_dir, manifest, info = _ollama_model_ref_info(ref)
+ projector = None
+ for layer in manifest.get("layers") or []:
+ if layer.get("mediaType") == "application/vnd.ollama.image.projector":
+ projector = _ollama_blob_path(ollama_dir / "blobs", layer.get("digest"))
+ return info.path, str(projector) if projector is not None else None
+
+
+def _materialization_lock(tag_file: Path, ollama_dir: Path) -> threading.Lock:
+ """Per-tag lock, keyed by the store's inode and the manifest's path inside it. Not by pathname:
+ one store reached through a symlink or spelled in another case would hand out two locks."""
+ try:
+ stat = os.stat(ollama_dir)
+ root: object = (stat.st_dev, stat.st_ino)
+ rel = os.path.relpath(os.path.realpath(str(tag_file)), os.path.realpath(str(ollama_dir)))
+ except (OSError, ValueError):
+ root, rel = None, os.path.realpath(str(tag_file))
+ key = (root, os.path.normcase(rel))
with _OLLAMA_MATERIALIZE_LOCKS_GUARD:
return _OLLAMA_MATERIALIZE_LOCKS.setdefault(key, threading.Lock())
@@ -585,14 +691,14 @@ def _materialize_ollama_model_ref_unlocked(tag_file: Path, ollama_dir: Path) ->
def materialize_ollama_model_ref(ref: str) -> str:
"""Resolve an Ollama ref while serializing updates to its model/projector pair."""
tag_file, ollama_dir = _validated_ollama_manifest_location(ref)
- with _materialization_lock(tag_file):
+ with _materialization_lock(tag_file, ollama_dir):
return _materialize_ollama_model_ref_unlocked(tag_file, ollama_dir)
def acquire_ollama_model_ref(ref: str) -> OllamaModelLease:
"""Materialize and keep the pair stable until the caller releases the lease."""
tag_file, ollama_dir = _validated_ollama_manifest_location(ref)
- lock = _materialization_lock(tag_file)
+ lock = _materialization_lock(tag_file, ollama_dir)
lock.acquire()
try:
return OllamaModelLease(_materialize_ollama_model_ref_unlocked(tag_file, ollama_dir), lock)
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index 955b6eb9906..92fa69e607a 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -701,6 +701,13 @@ class ValidateModelResponse(BaseModel):
valid: bool = Field(..., description = "Whether the model identifier looks valid")
message: str = Field(..., description = "Human-readable validation message")
identifier: Optional[str] = Field(None, description = "Resolved model identifier")
+ resident: bool = Field(
+ False,
+ description = (
+ "Whether the weights this identifier names are the ones already loaded. Decided "
+ "from the files, so an Ollama tag answers for whichever of its spellings loaded it."
+ ),
+ )
display_name: Optional[str] = Field(None, description = "Display name derived from identifier")
is_gguf: bool = Field(False, description = "Whether this is a GGUF model (llama.cpp)")
is_diffusion: bool = Field(
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 632b64d1743..cdf4ce8e4d4 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -84,6 +84,7 @@ from hub.services.models.ollama import (
acquire_ollama_model_ref,
is_ollama_manifest_ref,
materialize_ollama_model_ref,
+ ollama_model_ref_public_id,
)
from core.inference.audio_errors import (
AudioBackendUnsupportedError,
@@ -7353,6 +7354,25 @@ def _public_model_identifier(requested: str, resolved: str) -> str:
return requested if is_ollama_manifest_ref(requested) else resolved
+def _as_ollama_manifest_request(request):
+ """*request* with a materialized Ollama ``.gguf`` link rewritten to the tag that produced it."""
+ from hub.services.models.ollama import ollama_manifest_ref_for_path, ollama_model_ref_files
+
+ # An authorized request keeps its path: the tag names whatever its manifest names now, so a
+ # managed account would load weights its check never saw and a lease would lose its artifact.
+ if account_access.managed_account() or getattr(request, "native_path_lease", None):
+ return request
+ ref = ollama_manifest_ref_for_path(request.model_path)
+ if not ref or ref == request.model_path:
+ return request
+ try:
+ ollama_model_ref_files(ref)
+ except Exception:
+ # A tag whose manifest no longer reads is no improvement on a link that still resolves.
+ return request
+ return request.model_copy(update = {"model_path": ref})
+
+
async def _lease_ollama_model_ref(
request: LoadRequest | ValidateModelRequest, *, operation: str, stack: ExitStack
) -> Optional[str]:
@@ -7574,9 +7594,7 @@ async def _wait_for_model_switch_idle(
def _llama_public_model_id(llama_backend, fallback: Optional[str] = None) -> Optional[str]:
- """The id to report for the loaded GGUF in API responses: the advertised repo
- id from an auto-switch load, else the cleaned public id, never the on-disk
- .gguf path (see core.inference.model_ids.public_model_id)."""
+ """API-facing id for the loaded GGUF: a recorded advertised id, else the public id, never a path."""
return (
getattr(llama_backend, "_openai_advertised_id", None)
or public_model_id(getattr(llama_backend, "model_identifier", None))
@@ -7674,6 +7692,12 @@ def _target_is_vision(
# but the rule requires it regardless).
from utils.models.model_config import is_vision_model
try:
+ if is_ollama_manifest_ref(load_path):
+ from hub.services.models.ollama import ollama_model_ref_files
+ from utils.models.gguf_metadata import mmproj_accepts_image
+
+ _, projector = ollama_model_ref_files(load_path)
+ return projector is not None and (not need_image or mmproj_accepts_image(projector))
# Deliberately unguarded: the resolver only yields local paths, so this returns
# from the mmproj filesystem branch without touching the hub. A reachability
# probe here would add seconds per request and prevent nothing.
@@ -7783,6 +7807,9 @@ def _target_accepts_request_input(
def _resolve_target_gguf_file(load_path: str, gguf_variant: Optional[str]) -> Optional[str]:
from utils.models.model_config import _find_local_gguf_by_variant, detect_gguf_model
+ if is_ollama_manifest_ref(load_path):
+ from hub.services.models.ollama import ollama_model_ref_files
+ return ollama_model_ref_files(load_path)[0]
local_path = os.path.expanduser(load_path)
if gguf_variant and Path(local_path).is_dir():
return _find_local_gguf_by_variant(local_path, gguf_variant)
@@ -8496,10 +8523,20 @@ def _loaded_satisfies(requested: str) -> bool:
)
if candidate
]
- if not _matches_any(base, candidates):
+ identifier = getattr(llama_backend, "model_identifier", None)
+ if identifier and is_ollama_manifest_ref(identifier):
+ # A re-pull keeps a spelling and replaces its blobs: the tag vouches for itself only.
+ own = [name for name in (identifier, _ollama_public_id(identifier)) if name]
+ still_tagged = _resident_is_still_tagged(identifier, llama_backend)
+ if _matches_any(requested, own):
+ return still_tagged
+ if _matches_any(base, own):
+ # The tag itself is the whole name, so a quant suffix on top of it names nothing.
+ return still_tagged and not looks_like_quant(variant)
+ return _ollama_request_is_resident(requested, llama_backend)
+ if not _matches_any(requested, candidates) and not _matches_any(base, candidates):
return False
if not looks_like_quant(variant):
- # An Ollama-style tag (":latest", ":8b") names no file, so the repo is enough.
return True
return (getattr(llama_backend, "hf_variant", None) or "").lower() == variant.lower()
backend = get_inference_backend()
@@ -8624,13 +8661,69 @@ def _resident_quant_is(variant: Optional[str]) -> bool:
return bool(variant) and resident.lower() == variant.strip().lower()
+def _blob_identity(identity: Optional[tuple]) -> Optional[tuple]:
+ """A load identity reduced to (device, inode, size, mtime): link, hardlink and blob then match."""
+ return tuple(part[-4:] for part in identity) if identity else None
+
+
+def _ollama_public_id(ref: str) -> Optional[str]:
+ try:
+ return ollama_model_ref_public_id(ref)
+ except (OSError, ValueError):
+ return None
+
+
+def _ollama_source_identity(ref: str) -> Optional[tuple]:
+ """Blob identity of every file *ref*'s manifest names now, projector included, or None."""
+ from core.inference.llama_cpp import LlamaCppBackend
+ from hub.services.models.ollama import ollama_model_ref_files
+
+ try:
+ model_path, projector_path = ollama_model_ref_files(ref)
+ return _blob_identity(
+ LlamaCppBackend._gguf_load_source_identity(model_path, projector_path)
+ )
+ except (OSError, ValueError):
+ return None
+
+
+def _resident_is_still_tagged(ref: str, llama_backend) -> bool:
+ # The load identity survives a failed load, so it describes a resident model only while one is.
+ if not getattr(llama_backend, "is_loaded", False):
+ return False
+ source = _ollama_source_identity(ref)
+ loaded = _blob_identity(getattr(llama_backend, "_gguf_load_identity", None))
+ return bool(source and source == loaded)
+
+
+def _ollama_request_is_resident(requested: str, llama_backend) -> bool:
+ """Whether *requested*'s blobs are the resident ones. Reads the built index, never a scan."""
+ identifier = getattr(llama_backend, "model_identifier", None)
+ if not identifier or not is_ollama_manifest_ref(identifier):
+ return False
+ if is_ollama_manifest_ref(requested):
+ return _resident_is_still_tagged(requested, llama_backend)
+ from core.inference.local_model_resolver import resolve_local_gguf
+
+ try:
+ load_path = (resolve_local_gguf(requested, allow_scan = False) or (None,))[0]
+ except Exception as e:
+ logger.debug("inference.ollama_alias_resolve_failed: %s", e)
+ return False
+ return bool(
+ load_path
+ and is_ollama_manifest_ref(load_path)
+ and _resident_is_still_tagged(load_path, llama_backend)
+ )
+
+
def _resolves_to_resident(
load_path: Optional[str],
*,
llama_only: bool = False,
exact_only: bool = False,
) -> bool:
- """Whether a resolved on-disk path is what is already loaded.
+ """Whether a resolver result -- a path, or an Ollama manifest reference -- is already loaded.
``llama_only`` drops the Transformers backend: only llama.cpp carries a quant
identity, so a Transformers model active from a directory that also holds GGUF
@@ -8642,6 +8735,9 @@ def _resolves_to_resident(
return False
target = _norm_path(load_path)
llama_backend = get_llama_cpp_backend()
+ # A reference and the .gguf link the picker loaded share no spelling, so only the blobs decide.
+ if is_ollama_manifest_ref(load_path):
+ return _resident_is_still_tagged(load_path, llama_backend)
orchestrator_model = (
None if llama_only else getattr(get_inference_backend(), "active_model_name", None)
)
@@ -8693,6 +8789,28 @@ def _classify_and_probe_residency(
return is_gguf, _resolves_to_resident(load_path, llama_only = llama_only, exact_only = not is_gguf)
+def _validated_target_is_resident(
+ request: "ValidateModelRequest",
+ *,
+ model_identifier: str,
+ config,
+ is_gguf: bool,
+ native_grant_backed: bool,
+) -> bool:
+ """Whether the artifact this validation resolved -- not the spelling asked for -- is loaded.
+
+ A lease picks the file and a variant the quant, so ``model_path`` names neither.
+ """
+ if is_ollama_manifest_ref(request.model_path):
+ return _resolves_to_resident(request.model_path, llama_only = is_gguf)
+ if native_grant_backed or not is_gguf:
+ target = model_identifier
+ else:
+ from hub.utils.gguf import resolve_local_gguf_path
+ target = config.gguf_file or resolve_local_gguf_path(model_identifier, request.gguf_variant)
+ return _resolves_to_resident(target, llama_only = is_gguf, exact_only = True)
+
+
def _innermost_indexed_owner(path: str) -> Optional[str]:
"""Longest catalog-listed model path containing *path*, or None if none does."""
best = None
@@ -8789,12 +8907,18 @@ async def _reject_unservable_model(
return
downloaded = resolved is not None
# /v1/models may have advertised this id off its own scan while the index is cold.
- advertised = _advertised_local_path(base)
+ advertised_alias = requested_model
+ advertised = _advertised_local_path(requested_model)
+ if advertised is None:
+ advertised_alias = base
+ advertised = _advertised_local_path(base)
# classified from the advertised path itself, since a resolver miss left the pair above safe.
advertised_is_resident = (
advertised is not None
and (
- await asyncio.to_thread(_classify_and_probe_residency, advertised, base, quantified)
+ await asyncio.to_thread(
+ _classify_and_probe_residency, advertised, advertised_alias, quantified
+ )
)[1]
)
if advertised_is_resident and (not quantified or _resident_quant_is(variant)):
@@ -9233,6 +9357,11 @@ async def _maybe_auto_switch_model(
_, _requested_variant = split_model_ref(requested_model)
bare = not looks_like_quant(_requested_variant)
+ ollama_target = target_is_gguf and is_ollama_manifest_ref(target_id)
+ ollama_source_identity = (
+ await asyncio.to_thread(_ollama_source_identity, target_id) if ollama_target else None
+ )
+
def _already_serving() -> bool:
# Match against both the concrete load path and the advertised repo id,
# so a model loaded manually by repo id (identifier = repo id) and one
@@ -9258,7 +9387,12 @@ async def _maybe_auto_switch_model(
advertised = getattr(backend, "_openai_advertised_id", None)
if advertised:
loaded_keys.add(advertised.lower())
- if loaded_keys.isdisjoint({target_id.lower(), override_id.lower()}):
+ if ollama_target:
+ # The blobs, not the keys: an alias shares no spelling with the resident tag.
+ loaded_source = _blob_identity(getattr(backend, "_gguf_load_identity", None))
+ if not ollama_source_identity or loaded_source != ollama_source_identity:
+ return False
+ elif loaded_keys.isdisjoint({target_id.lower(), override_id.lower()}):
return False
loaded_companion_roots = tuple(
getattr(backend, "_openai_gguf_companion_roots", ()) or ()
@@ -9446,6 +9580,11 @@ async def _maybe_auto_switch_model(
# Hold the keep-warm gate across the swap so no new inference can
# start on the model while it is being torn down and replaced.
async with inference_lifecycle_gate():
+ # Re-read under the gate: the snapshot above predates the wait.
+ if ollama_target:
+ ollama_source_identity = await asyncio.to_thread(
+ _ollama_source_identity, target_id
+ )
if _already_serving():
if claim_resident:
_set_preview_resident(None)
@@ -14245,7 +14384,21 @@ def _names_the_resident_model(resident: Optional[str], model_path: str) -> bool:
client reads back reports the repo id it maps to. Both name the same model, and an unload
arriving under either has to find it.
"""
- return bool(resident) and model_id_matches(model_path, resident)
+ if not resident:
+ return False
+ if model_id_matches(model_path, resident):
+ return True
+ # A load rewrites a materialized Ollama link to the tag that made it, so the id the picker
+ # still holds names the same model under a spelling no string compare reaches.
+ return is_ollama_manifest_ref(resident) and _ollama_ref_for_link(model_path) == resident
+
+
+def _ollama_ref_for_link(model_path: str) -> Optional[str]:
+ from hub.services.models.ollama import ollama_manifest_ref_for_path
+ try:
+ return ollama_manifest_ref_for_path(model_path)
+ except (OSError, ValueError):
+ return None
def _names_the_loading_model(loading: str, model_path: str) -> bool:
@@ -14780,6 +14933,7 @@ async def _load_model_impl(
native_access_deferred = _defers_access_to_native_grant(request)
if account_access.managed_account() and not native_access_deferred:
await asyncio.to_thread(account_access.require_model_access, request.model_path)
+ request = await asyncio.to_thread(_as_ollama_manifest_request, request)
if account_access.managed_account():
request = request.model_copy(
update = {"hf_token": account_access.account_hf_token(request.hf_token)}
@@ -14889,6 +15043,11 @@ async def _load_model_impl(
# Keep the inventory ref public while loading the materialized artifact.
public_model_identifier = _public_model_identifier(request.model_path, model_identifier)
+ ollama_advertised_id = (
+ await asyncio.to_thread(ollama_model_ref_public_id, request.model_path)
+ if resolved_ollama_path is not None
+ else None
+ )
# Version switching is handled by the subprocess-based inference
# backend -- no ensure_transformers_version() needed here.
@@ -14938,8 +15097,8 @@ async def _load_model_impl(
# Case-sensitive filesystems keep two checkpoint paths that differ only by
# case distinct, so they must not dedup onto the already-loaded fast path
# (the intent match lowercases the identifier). Checked before the adopt so
- # a mismatch never adopts the caller's placement.
- _same_loaded_identifier(llama_backend.model_identifier, model_identifier)
+ # a mismatch never adopts the caller's placement. An Ollama load records the ref.
+ _same_loaded_identifier(llama_backend.model_identifier, public_model_identifier)
and tuple(getattr(llama_backend, "_openai_gguf_companion_roots", ()) or ())
== tuple(request._gguf_companion_roots)
and getattr(llama_backend, "_openai_gguf_companion_state", ())
@@ -14952,6 +15111,8 @@ async def _load_model_impl(
logger.info("Model already loaded (GGUF): %s, skipping reload", model_log_label)
# A no-op Unsloth load of a preview-owned checkpoint still claims it.
_set_preview_resident(None)
+ if ollama_advertised_id:
+ llama_backend._openai_advertised_id = ollama_advertised_id
account_access.join_resident("chat")
return _gguf_load_response(
llama_backend,
@@ -15575,9 +15736,8 @@ async def _load_model_impl(
llama_backend._openai_gguf_companion_roots = tuple(request._gguf_companion_roots)
llama_backend._openai_gguf_companion_state = gguf_companion_state
await asyncio.to_thread(note_model_loaded, llama_backend)
- # A plain load advertises its own identifier; auto-switch overwrites
- # this with the repo id right after _load_model_impl returns.
- llama_backend._openai_advertised_id = None
+ # None elsewhere: only an Ollama load has an identifier no client should be handed.
+ llama_backend._openai_advertised_id = ollama_advertised_id
# Audio detection moved into load_model under _serial_load_lock (#5642).
_gguf_audio = llama_backend._audio_type
@@ -16066,6 +16226,7 @@ async def validate_model(
native_access_deferred = _defers_access_to_native_grant(request)
if account_access.managed_account() and not native_access_deferred:
await asyncio.to_thread(account_access.require_model_access, request.model_path)
+ request = await asyncio.to_thread(_as_ollama_manifest_request, request)
from core.inference.llama_cpp import (
LlamaServerNotFoundError,
_hf_offline_if_unreachable_for,
@@ -16392,6 +16553,14 @@ async def validate_model(
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),
@@ -26974,7 +27143,8 @@ _OWNED_BY = "unsloth-studio"
def _openai_model_objects() -> list[dict]:
- """The model objects GET /v1/models exposes (one per loaded local backend).
+ """The model objects GET /v1/models exposes, one per loaded local backend still answering to
+ the id it is advertised under.
Shared by the LIST and RETRIEVE handlers so both report the same ids and
field shape.
@@ -26988,12 +27158,20 @@ def _openai_model_objects() -> list[dict]:
# Check GGUF backend
llama_backend = get_llama_cpp_backend()
- if llama_backend.is_loaded:
+ # Claiming a re-pulled tag would displace the scanned row holding its weights, and the id
+ # about to be published is not always the tag the load recorded: an alias is its own name.
+ _resident_identifier = getattr(llama_backend, "model_identifier", None) or ""
+ _stale_ollama_resident = (
+ llama_backend.is_loaded
+ and is_ollama_manifest_ref(_resident_identifier)
+ and not _loaded_satisfies(_llama_public_model_id(llama_backend))
+ )
+ if llama_backend.is_loaded and not _stale_ollama_resident:
# Advertise the repo id an auto-switch load recorded, not the concrete
# on-disk load path, so /v1/models never leaks a host path or lists a
# model twice (path plus repo id).
entry = {
- # Advertised repo id after an auto-switch load, else a clean public id,
+ # Advertised id recorded by an auto-switch or Ollama load, else a clean public id,
# never the absolute .gguf path (which leaks the host filesystem layout).
"id": _llama_public_model_id(llama_backend),
"object": "model",
@@ -27418,7 +27596,7 @@ def _stt_model_objects(created: int, catalog_at: Optional[float] = None) -> list
async def _cached_local_catalog() -> list:
- """Locally available models (models dir + HF caches + LM Studio + scan
+ """Locally available models (models dir + HF caches + LM Studio + Ollama + scan
folders), cached for a few seconds. Returns a list of LocalModelInfo.
The scan walks several directories and stats many files, so it runs in a
@@ -27440,8 +27618,12 @@ async def _cached_local_catalog() -> list:
return _account_catalog_cache()["models"]
try:
from routes.models import collect_local_models
+
+ # By manifest reference: this catalog resolves rows rather than opening their ids.
_account_catalog_cache()["models"] = await asyncio.to_thread(
- lambda: _classified_catalog(collect_local_models(Path("./models").resolve()))
+ lambda: _classified_catalog(
+ collect_local_models(Path("./models").resolve(), materialize_ollama_links = False)
+ )
)
except Exception as exc:
logger.debug("model catalog scan failed: %s", exc)
@@ -27570,7 +27752,16 @@ def _servable_catalog_scan(catalog, catalog_at: Optional[float]):
# The source path, not the resolved load dir: an HF repo's snapshot pointer can
# move within the catalog's lifetime, and a cached snapshot path would then
# report a freshly loaded model as unloaded. Residency resolves it per call.
- scanned.append((info, is_gguf, quants, getattr(info, "path", None)))
+ # An Ollama row's path is the blob a read-only scan found, and only its tag is judged.
+ row_id = getattr(info, "id", None)
+ scanned.append(
+ (
+ info,
+ is_gguf,
+ quants,
+ row_id if is_ollama_manifest_ref(row_id or "") else getattr(info, "path", None),
+ )
+ )
if catalog_at is not None:
# The generation read BEFORE the scan, not after: an invalidation that
# landed while the scan ran would otherwise be stamped in as if the scan
@@ -28267,18 +28458,24 @@ def _resident_absent(llama_backend) -> bool:
def _stashed_gguf_embeds() -> bool:
from core.inference.llama_keepwarm import get_last_unloaded_model
from core.inference.local_model_resolver import resolve_local_gguf
+ from hub.services.models.ollama import ollama_model_ref_files
last = get_last_unloaded_model()
if not last:
return False
target_id, variant = last[0], last[1]
try:
+ # The variant beside a manifest reference is its tag, so pinning it asks for no quant.
+ pin_variant = variant and not is_ollama_manifest_ref(target_id)
resolved = resolve_local_gguf(
- f"{target_id}:{variant}" if variant else target_id, allow_scan = False
+ f"{target_id}:{variant}" if pin_variant else target_id, allow_scan = False
)
path = resolved[0] if resolved else None
if not path:
return False
+ # Read the blob out rather than materializing a link: this only asks what was stashed.
+ if is_ollama_manifest_ref(path):
+ path = ollama_model_ref_files(path)[0]
probe = _probe_backend()
probe._read_gguf_metadata(path)
return bool(probe.is_embedding_gguf)
diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.py
index a0b0b660f1d..702c24ffc23 100644
--- a/studio/backend/routes/models.py
+++ b/studio/backend/routes/models.py
@@ -2,13 +2,11 @@
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import asyncio
-import hashlib
import json
import os
import shutil
import sys
import time
-import uuid
import weakref
from pathlib import Path
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Request
@@ -746,233 +744,22 @@ def _scan_lmstudio_dir(lm_dir: Path) -> List[LocalModelInfo]:
return found
-def _ollama_links_dir(ollama_dir: Path) -> Optional[Path]:
- """A writable directory for Ollama ``.gguf`` symlinks. Prefers ``<ollama_dir>/.studio_links/`` so
- links sit next to their blobs; falls back to a per-ollama-dir namespace under Unsloth's cache when
- the models dir is read-only (common for system installs)."""
- from utils.paths.storage_roots import cache_root
-
- primary = ollama_dir / ".studio_links"
- try:
- primary.mkdir(exist_ok = True)
- return primary
- except OSError as e:
- logger.debug(
- "Ollama dir %s not writable for .studio_links (%s); falling back to Unsloth cache",
- ollama_dir,
- e,
- )
-
- # Fallback: namespace by a hash of ollama_dir so two roots don't collide (cache path only).
- try:
- digest = hashlib.sha256(str(ollama_dir.resolve()).encode()).hexdigest()[:12]
- except OSError:
- digest = "default"
- fallback = cache_root() / "ollama_links" / digest
- try:
- fallback.mkdir(parents = True, exist_ok = True)
- return fallback
- except OSError as e:
- logger.warning(
- "Could not create Ollama symlink cache at %s: %s",
- fallback,
- e,
- )
- return None
-
-
-def _scan_ollama_dir(ollama_dir: Path, limit: Optional[int] = None) -> List[LocalModelInfo]:
- """Scan an Ollama models directory for downloaded models. Ollama uses a content-addressable layout
- (``manifests/<host>/<namespace>/<model>/<tag>`` + ``blobs/sha256-...``); ``rglob`` finds every
- layout depth. The ``application/vnd.ollama.image.model`` layer holds the GGUF weights and
- ``...image.projector`` is the vision adapter. Ollama blobs lack the ``.gguf`` extension the
- loading pipeline requires, so create ``.gguf``-named links to them, one subdir per model keyed
- by a short hash of the manifest path so ``detect_mmproj_file`` only sees that model's projector.
- Symlinks when possible, else hardlinks."""
- manifests_root = ollama_dir / "manifests"
- if not manifests_root.is_dir():
- return []
-
- found: List[LocalModelInfo] = []
- blobs_dir = ollama_dir / "blobs"
- links_root = _ollama_links_dir(ollama_dir)
- if links_root is None:
- logger.warning(
- "Skipping Ollama scan for %s: no writable location for .gguf links",
- ollama_dir,
- )
- return []
-
- def _make_link(link_dir: Path, link_name: str, target: Path) -> Optional[str]:
- """Create a .gguf-named link to an Ollama blob. Symlink, then hardlink; skips the model if neither
- works (a multi-GB copy in a sync request would block the backend). Idempotent."""
- link_dir.mkdir(parents = True, exist_ok = True)
- link_path = link_dir / link_name
- resolved = target.resolve()
-
- # Skip if the link already points at the same blob; size checks can reuse stale links.
- try:
- if link_path.exists() and os.path.samefile(str(link_path), str(resolved)):
- return str(link_path)
- except OSError as e:
- logger.debug("Error checking existing link %s: %s", link_path, e)
-
- tmp_path = link_dir / f".{link_name}.tmp-{uuid.uuid4().hex[:8]}"
- try:
- if tmp_path.is_symlink() or tmp_path.exists():
- tmp_path.unlink()
- try:
- tmp_path.symlink_to(resolved)
- except OSError:
- try:
- os.link(str(resolved), str(tmp_path))
- except OSError:
- logger.warning(
- "Could not create link for Ollama blob %s "
- "(symlinks and hardlinks both failed). "
- "Skipping model to avoid blocking the API.",
- target,
- )
- return None
- os.replace(str(tmp_path), str(link_path))
- return str(link_path)
- except OSError as e:
- logger.debug("Could not create Ollama link %s: %s", link_path, e)
- try:
- if tmp_path.is_symlink() or tmp_path.exists():
- tmp_path.unlink()
- except OSError as cleanup_err:
- logger.debug("Could not clean up tmp path %s: %s", tmp_path, cleanup_err)
- return None
-
- try:
- for tag_file in manifests_root.rglob("*"):
- if not tag_file.is_file():
- continue
-
- rel = tag_file.relative_to(manifests_root)
- parts = rel.parts
- if len(parts) < 3:
- continue
-
- host = parts[0]
- repo_parts = list(parts[1:-1])
- tag = parts[-1]
-
- if host == "registry.ollama.ai" and repo_parts and repo_parts[0] == "library":
- repo_name = "/".join(repo_parts[1:])
- elif host == "registry.ollama.ai":
- repo_name = "/".join(repo_parts)
- else:
- repo_name = "/".join([host] + repo_parts)
-
- if not repo_name:
- continue
-
- display = f"{repo_name}:{tag}"
-
- manifest_key = rel.as_posix()
- stem_hash = hashlib.sha256(manifest_key.encode()).hexdigest()[:10]
-
- try:
- manifest = json.loads(tag_file.read_text(encoding = "utf-8-sig"))
- except (json.JSONDecodeError, OSError, UnicodeDecodeError) as e:
- logger.debug(
- "Skipping unreadable/invalid Ollama manifest %s: %s",
- tag_file,
- e,
- )
- continue
- # rglob("*") hands us every file under manifests/, so a pruned pull, an editor backup, or any stray JSON can
- # be a list or a string; .get() on one raises AttributeError, which neither this loop's `except OSError`
- # nor the caller's catches, and one such file would 500 the whole picker.
- if not isinstance(manifest, dict):
- logger.debug("Skipping Ollama manifest %s: top level is not an object", tag_file)
- continue
-
- config = manifest.get("config")
- config_digest = config.get("digest", "") if isinstance(config, dict) else ""
- if not isinstance(config_digest, str):
- config_digest = ""
- model_type = ""
- file_type = ""
- if config_digest and blobs_dir.is_dir():
- config_blob = blobs_dir / config_digest.replace(":", "-")
- if config_blob.is_file():
- try:
- cfg = json.loads(config_blob.read_text(encoding = "utf-8-sig"))
- except (json.JSONDecodeError, OSError, UnicodeDecodeError) as e:
- logger.debug(
- "Could not parse Ollama config blob %s: %s",
- config_blob,
- e,
- )
- cfg = None
- if isinstance(cfg, dict):
- model_type = cfg.get("model_type", "")
- file_type = cfg.get("file_type", "")
-
- model_link_dir = links_root / stem_hash
-
- gguf_link_path: Optional[str] = None
- quant = f"-{file_type}" if file_type else ""
- safe_name = repo_name.replace("/", "-")
- layers = manifest.get("layers") or []
- if not isinstance(layers, list):
- logger.debug("Skipping Ollama manifest %s: layers is not a list", tag_file)
- continue
- for layer in layers:
- if not isinstance(layer, dict):
- continue
- media = layer.get("mediaType", "")
- digest = layer.get("digest", "")
- if not isinstance(digest, str) or not digest:
- continue
-
- if media == "application/vnd.ollama.image.model":
- candidate = blobs_dir / digest.replace(":", "-")
- if candidate.is_file():
- link_name = f"{safe_name}-{tag}{quant}.gguf"
- gguf_link_path = _make_link(model_link_dir, link_name, candidate)
-
- elif media == "application/vnd.ollama.image.projector":
- candidate = blobs_dir / digest.replace(":", "-")
- if candidate.is_file():
- mmproj_name = f"{safe_name}-{tag}-mmproj.gguf"
- _make_link(model_link_dir, mmproj_name, candidate)
-
- if not gguf_link_path:
- continue
-
- suffix = ""
- if model_type:
- suffix += f" ({model_type}"
- if file_type:
- suffix += f" {file_type}"
- suffix += ")"
+def _scan_ollama_dir(
+ ollama_dir: Path,
+ *,
+ limit: Optional[int] = None,
+ materialize_links: bool = True,
+) -> List[LocalModelInfo]:
+ """Ollama rows for the compat inventory, from the one scanner the Hub inventory also uses.
- try:
- updated_at = tag_file.stat().st_mtime
- except OSError:
- updated_at = None
-
- found.append(
- LocalModelInfo(
- id = gguf_link_path,
- model_id = f"ollama/{repo_name}:{tag}",
- display_name = display + suffix,
- path = gguf_link_path,
- # The frontend groups and labels these rows by this value (local-model-options.ts, pickers.tsx);
- # "custom" hid them in the generic folder section (#9986).
- source = "ollama",
- updated_at = updated_at,
- ),
- )
- if limit is not None and len(found) >= limit:
- return found
- except OSError as e:
- logger.warning("Error scanning Ollama directory %s: %s", ollama_dir, e)
- return found
+ This inventory's readers treat a row's id and path as filenames, so ``materialize_links``
+ defaults to the ``.gguf`` link; a caller that resolves the model itself passes False.
+ """
+ from hub.services.models.ollama import scan_ollama_dir
+ return [
+ LocalModelInfo.model_validate(row.model_dump())
+ for row in scan_ollama_dir(ollama_dir, limit = limit, materialize_links = materialize_links)
+ ]
def _scan_hermes_dir(hermes_dir: Path) -> List[LocalModelInfo]:
@@ -993,10 +780,12 @@ class _CompatLocalInventorySources(NamedTuple):
lm_dirs: tuple[Path, ...]
known_hf_caches: tuple[Path, ...]
hermes_dirs: tuple[Path, ...] = ()
+ ollama_dirs: tuple[Path, ...] = ()
def _compat_local_inventory_sources() -> _CompatLocalInventorySources:
from utils.paths import (
+ ollama_model_dirs,
hermes_model_dirs,
hf_default_cache_dir,
legacy_hf_cache_dir,
@@ -1010,6 +799,7 @@ def _compat_local_inventory_sources() -> _CompatLocalInventorySources:
tuple(lmstudio_model_dirs()),
tuple(known_hf_hub_caches()),
tuple(hermes_model_dirs()),
+ tuple(ollama_model_dirs()),
)
@@ -1018,13 +808,15 @@ def collect_local_models(
*,
custom_folders: Optional[list[dict]] = None,
sources: Optional[_CompatLocalInventorySources] = None,
+ materialize_ollama_links: bool = True,
) -> List[LocalModelInfo]:
- """Scan ``models_root``, the HF caches, LM Studio and Hermes dirs, and user scan
+ """Scan ``models_root``, the HF caches, LM Studio, Hermes and Ollama dirs, and user scan
folders, returning a deduplicated, hidden-filtered list of discovered local models.
Shared by ``GET /models/local`` (the model picker) and the OpenAI-compatible
catalog (``GET /v1/models``) so the UI and the API never drift. ``models_root``
- must already be validated/trusted by the caller.
+ must already be validated/trusted by the caller, and ``materialize_ollama_links`` is the one
+ thing the two do not share; see :func:`_scan_ollama_dir`.
"""
from storage.studio_db import list_scan_folders
from hub.utils import gguf as gguf_utils
@@ -1099,6 +891,12 @@ def collect_local_models(
except Exception as e:
logger.warning("Error scanning Hermes directory %s: %s", hermes_dir, e)
+ for ollama_dir in sources.ollama_dirs:
+ try:
+ local_models += _scan_ollama_dir(ollama_dir, materialize_links = materialize_ollama_links)
+ except Exception as e:
+ logger.warning("Error scanning Ollama directory %s: %s", ollama_dir, e)
+
# Scan user-added custom folders (per-folder cap).
_MAX_MODELS_PER_FOLDER = 200
hermes_identities = {_compat_inventory_path_identity(str(d)) for d in sources.hermes_dirs}
@@ -1160,6 +958,7 @@ def collect_local_models(
custom_models += _scan_ollama_dir(
folder_path,
limit = _MAX_MODELS_PER_FOLDER - len(custom_models),
+ materialize_links = materialize_ollama_links,
)
except OSError as e:
logger.warning("Skipping unreadable scan folder %s: %s", folder_path, e)
@@ -1343,7 +1142,7 @@ async def list_local_models(
),
current_subject: str = Depends(get_current_subject),
):
- """List local model candidates from the models dir, HF caches, LM Studio and Hermes dirs."""
+ """List local model candidates from the models dir, HF caches, LM Studio, Hermes, Ollama."""
# Resolve all scan directories up front.
sources = _compat_local_inventory_sources()
hf_cache_dir = sources.hf_cache_dir
diff --git a/studio/backend/tests/test_account_inference_isolation.py b/studio/backend/tests/test_account_inference_isolation.py
index e0708497ab8..b23f7c33016 100644
--- a/studio/backend/tests/test_account_inference_isolation.py
+++ b/studio/backend/tests/test_account_inference_isolation.py
@@ -397,7 +397,7 @@ def test_openai_catalog_and_advertised_paths_are_account_scoped(monkeypatch):
monkeypatch.setattr(
models,
"collect_local_models",
- lambda path: [SimpleNamespace(model_id = "same/name", path = current_account_id())],
+ lambda *_a, **_kw: [SimpleNamespace(model_id = "same/name", path = current_account_id())],
)
async def read(account):
@@ -418,7 +418,7 @@ def test_the_local_id_v1_models_publishes_is_usable_by_its_own_account(monkeypat
rows = [_row(OWNER), _row(ALICE), _row(BOB)]
monkeypatch.setattr(inference, "_classified_catalog", lambda listed: listed)
- monkeypatch.setattr(models, "collect_local_models", lambda path: rows)
+ monkeypatch.setattr(models, "collect_local_models", lambda *_a, **_kw: rows)
request = SimpleNamespace(
scope = {},
@@ -440,7 +440,7 @@ def test_the_local_id_v1_models_publishes_is_usable_by_its_own_account(monkeypat
assert run_as(account, inference._own_local_model_for_alias, "other-model") is None
monkeypatch.setattr(inference, "_managed_catalogs", {})
- monkeypatch.setattr(models, "collect_local_models", lambda path: [_row(OWNER)])
+ monkeypatch.setattr(models, "collect_local_models", lambda *_a, **_kw: [_row(OWNER)])
for account in (ALICE, BOB):
with pytest.raises(HTTPException) as refused:
asyncio.run(resolve(account))
diff --git a/studio/backend/tests/test_chat_load_during_training.py b/studio/backend/tests/test_chat_load_during_training.py
index 91a3e2dc557..25015777baf 100644
--- a/studio/backend/tests/test_chat_load_during_training.py
+++ b/studio/backend/tests/test_chat_load_during_training.py
@@ -59,6 +59,7 @@ def _model_cfg(**overrides):
"is_vision": False,
"path": None,
"base_model": None,
+ "gguf_file": None,
**overrides,
}
)
diff --git a/studio/backend/tests/test_hf_cache_dangling_refs.py b/studio/backend/tests/test_hf_cache_dangling_refs.py
index 77997ad886d..2bd75fe7197 100644
--- a/studio/backend/tests/test_hf_cache_dangling_refs.py
+++ b/studio/backend/tests/test_hf_cache_dangling_refs.py
@@ -1574,9 +1574,9 @@ _MTIME_READERS = {
"_scan_hf_cache",
"_scan_lmstudio_dir",
"_scan_models_dir",
- "_scan_ollama_dir",
}
),
+ "hub/services/models/ollama.py": frozenset({"_ollama_model_info_from_manifest"}),
}
diff --git a/studio/backend/tests/test_ollama_api_catalog.py b/studio/backend/tests/test_ollama_api_catalog.py
new file mode 100644
index 00000000000..ca677619db8
--- /dev/null
+++ b/studio/backend/tests/test_ollama_api_catalog.py
@@ -0,0 +1,333 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+
+import asyncio
+import json
+from pathlib import Path
+from types import SimpleNamespace
+from unittest.mock import patch
+
+import pytest
+
+from core.inference import local_model_resolver as resolver
+from hub.services.models import account_access, ollama
+from models.inference import LoadRequest
+from routes import inference as inf, models as models_route
+from storage import studio_db
+from utils import paths, hf_cache_settings
+from studio.backend.tests.test_legacy_ollama_source import _write_ollama_store
+from studio.backend.tests.test_openai_auto_switch import (
+ _FakeBackend,
+ _reset_keepwarm,
+ _run_hook,
+ _wired,
+)
+
+
+_NO_ORCHESTRATOR = SimpleNamespace(active_model_name = None, models = {})
+
+
+def _refusing_to_scan():
+ raise AssertionError("the alias lookup must not scan")
+
+
+def _retag(store: Path, tag: str, digest: str) -> None:
+ (store / "blobs" / f"sha256-{digest}").write_bytes(b"GGUF-not-really-" + digest[:4].encode())
+ layer = {"mediaType": "application/vnd.ollama.image.model", "digest": f"sha256:{digest}"}
+ (store / "manifests/registry.ollama.ai/library/llama3" / tag).write_text(
+ json.dumps({"config": {}, "layers": [layer]}), encoding = "utf-8"
+ )
+
+
+def store(tmp_path, monkeypatch):
+ root = tmp_path / "ollama"
+ _write_ollama_store(root)
+ empty = tmp_path / "empty"
+ empty.mkdir()
+ monkeypatch.chdir(tmp_path)
+ for module in (paths, ollama):
+ monkeypatch.setattr(module, "ollama_model_dirs", lambda: [root])
+ for name in ("lmstudio_model_dirs", "hermes_model_dirs"):
+ monkeypatch.setattr(paths, name, lambda: [])
+ for name in ("legacy_hf_cache_dir", "hf_default_cache_dir"):
+ monkeypatch.setattr(paths, name, lambda: empty)
+ monkeypatch.setattr(hf_cache_settings, "known_hf_hub_caches", lambda: [])
+ monkeypatch.setattr(models_route, "_resolve_hf_cache_dir", lambda: empty)
+ monkeypatch.setattr(studio_db, "list_scan_folders", lambda: [])
+ monkeypatch.setattr(inf, "_CATALOG_CACHE", {"at": 0.0, "models": []})
+ monkeypatch.setattr(inf, "_SERVABLE_SCAN_CACHE", {"entry": None})
+ monkeypatch.setattr(inf, "_peek_inference_backend", lambda: None)
+ for empty_index in ("_media_model_objects", "_stt_model_objects"):
+ monkeypatch.setattr(inf, empty_index, lambda *args: [])
+ resolver.invalidate_index()
+ _reset_keepwarm()
+ yield root
+ resolver.invalidate_index()
+ _reset_keepwarm()
+
+
+def aliased(store):
+ tags = store / "manifests/registry.ollama.ai/library/llama3"
+ (tags / "8b").write_bytes((tags / "latest").read_bytes())
+ return {r.model_id: r.id for r in models_route._scan_ollama_dir(store, materialize_links = False)}
+
+
+def serving(monkeypatch):
+ def _pin(backend):
+ monkeypatch.setattr(inf, "get_llama_cpp_backend", lambda: backend)
+ monkeypatch.setattr(inf, "get_inference_backend", lambda: _NO_ORCHESTRATOR)
+ return backend
+
+ return _pin
+
+
+def test_the_compat_inventory_still_hands_out_a_gguf_path(store):
+ """Its readers treat a row's id and path as filenames; a suffixless blob is not loadable."""
+ (row,) = models_route.collect_local_models(Path("models").resolve())
+ assert row.model_id == "ollama/llama3:latest"
+ assert row.id == row.path
+ assert row.path.endswith(".gguf")
+ assert Path(row.path).is_file()
+
+
+def _store_contents(store):
+ def _entry(p):
+ stat = p.lstat()
+ return str(p.relative_to(store)), stat.st_size, stat.st_mtime_ns, p.is_symlink()
+
+ return sorted(_entry(p) for p in store.rglob("*"))
+
+
+def test_catalog_and_resolver_are_read_only_and_share_the_public_id(store, monkeypatch):
+ before = _store_contents(store)
+ (row,) = models_route.collect_local_models(
+ Path("models").resolve(), materialize_ollama_links = False
+ )
+ assert row.model_id == "ollama/llama3:latest"
+ assert row.id.startswith("ollama-manifest:")
+ assert row.model_format == "gguf"
+ assert resolver.local_servable_model(row) == (True, ())
+ assert resolver.resolve_local_gguf(row.model_id) == (row.id, None, row.model_id)
+ assert _store_contents(store) == before
+ monkeypatch.setattr(studio_db, "list_scan_folders", lambda: [{"path": str(store)}])
+ rows = models_route.collect_local_models(
+ Path("models").resolve(), materialize_ollama_links = False
+ )
+ assert len(rows) == 1
+
+
+def test_a_tag_being_loaded_stays_in_the_inventory(store):
+ ref = models_route._scan_ollama_dir(store, materialize_links = False)[0].id
+ lease = ollama.acquire_ollama_model_ref(ref)
+ try:
+ rows = models_route._scan_ollama_dir(store, materialize_links = True)
+ finally:
+ lease.release()
+ assert [row.path for row in rows] == [lease.path]
+ assert Path(lease.path).is_file()
+
+
+def _resident(ref: str, *, advertised: str = "") -> _FakeBackend:
+ from core.inference.llama_cpp import LlamaCppBackend
+
+ materialized = ollama.materialize_ollama_model_ref(ref)
+ backend = _FakeBackend(ref, advertised_id = advertised or ollama.ollama_model_ref_public_id(ref))
+ backend.gguf_path = materialized
+ backend._is_audio, backend._audio_type = False, None
+ for field in ("context_length", "max_context_length", "native_context_length"):
+ setattr(backend, field, 4096)
+ backend._gguf_load_identity = LlamaCppBackend._gguf_load_source_identity(materialized)
+ return backend
+
+
+def test_a_repulled_tag_is_not_reported_loaded_while_it_cannot_be_answered(store, serving):
+ ref = models_route._scan_ollama_dir(store, materialize_links = False)[0].id
+ backend = serving(_resident(ref))
+ assert inf._loaded_satisfies("ollama/llama3:latest") is True
+ assert inf._loaded_satisfies("ollama/llama3:latest:Q8_0") is False
+ assert [m["id"] for m in inf._openai_model_objects()] == ["ollama/llama3:latest"]
+ # The load identity is recorded before the server answers and outlives a failed load.
+ backend.is_loaded = False
+ assert inf._resolves_to_resident(ref, llama_only = True) is False
+ backend.is_loaded = True
+ _retag(store, "latest", "c" * 64)
+ assert inf._loaded_satisfies("ollama/llama3:latest") is False
+ assert inf._openai_model_objects() == []
+ # A manifest mid-pull reads as nothing: withhold the row, do not fail the listing.
+ (store / "manifests/registry.ollama.ai/library/llama3/latest").write_text("truncated")
+ assert inf._loaded_satisfies("ollama/llama3:latest") is False
+ assert inf._openai_model_objects() == []
+
+
+def _rewritten(model_path: str) -> str:
+ return inf._as_ollama_manifest_request(LoadRequest(model_path = model_path)).model_path
+
+
+def test_a_link_is_loaded_as_the_tag_that_materialized_it(store, monkeypatch, serving):
+ """Its directory names the tag, so a selection stored under an older link name resolves too."""
+ ref = models_route._scan_ollama_dir(store, materialize_links = False)[0].id
+ link = Path(ollama.materialize_ollama_model_ref(ref))
+ data = json.loads((store / "manifests/registry.ollama.ai/library/llama3/latest").read_text())
+ for named in (link, link.with_name("llama3-latest-Q4_K_M.gguf")):
+ assert _rewritten(str(named)) == ref
+ assert _rewritten(str(store / "blobs")) == str(store / "blobs")
+ # An authorized request keeps its own path: the tag may name blobs it never judged.
+ monkeypatch.setattr(account_access, "managed_account", lambda: True)
+ assert _rewritten(str(link)) == str(link)
+ monkeypatch.setattr(account_access, "managed_account", lambda: False)
+ leased = LoadRequest(model_path = str(link), native_path_lease = "lease")
+ assert inf._as_ollama_manifest_request(leased).model_path == str(link)
+ # A tag that no longer reads leaves a link that still resolves alone.
+ manifest = store / "manifests/registry.ollama.ai/library/llama3/latest"
+ manifest.write_text("truncated")
+ assert _rewritten(str(link)) == str(link)
+ manifest.write_text(json.dumps(data))
+ serving(_resident(ref))
+ assert [(m["id"], m["loaded"]) for m in asyncio.run(inf._openai_catalog_objects())] == [
+ ("ollama/llama3:latest", True)
+ ]
+
+
+def test_the_switch_gate_follows_the_blobs_under_an_alias(store, aliased, monkeypatch):
+ """A tag is a mutable name: it answers for the resident model only while it still names it."""
+ alias = aliased["ollama/llama3:8b"]
+ backend = _resident(aliased["ollama/llama3:latest"])
+ _, recorder = _wired(monkeypatch, backend, (alias, None, "ollama/llama3:8b"))
+ loader = inf._load_model_impl
+
+ async def _load_and_record_identity(request, *args, **kwargs):
+ from core.inference.llama_cpp import LlamaCppBackend
+ await loader(request, *args, **kwargs)
+ backend._gguf_load_identity = LlamaCppBackend._gguf_load_source_identity(
+ ollama.materialize_ollama_model_ref(request.model_path)
+ )
+
+ monkeypatch.setattr(inf, "_load_model_impl", _load_and_record_identity)
+ _run_hook("ollama/llama3:8b")
+ assert recorder.calls == []
+ _retag(store, "8b", "c" * 64)
+ _run_hook("ollama/llama3:8b")
+ assert [call.model_path for call in recorder.calls] == [alias]
+
+
+def test_an_alias_answers_for_the_resident_blobs_until_its_own_tag_moves(
+ store, aliased, monkeypatch, serving
+):
+ """Adoption records a name, and a re-pull replaces the weights under a name it keeps."""
+ link = Path(ollama.materialize_ollama_model_ref(aliased["ollama/llama3:latest"]))
+ alias_link = Path(ollama.materialize_ollama_model_ref(aliased["ollama/llama3:8b"]))
+ # The advertised id a request adopting the resident model under another name leaves behind.
+ serving(_resident(aliased["ollama/llama3:latest"], advertised = "ollama/llama3:8b"))
+
+ def _satisfied() -> bool:
+ resolver.invalidate_index()
+ resolver.resolve_local_gguf("ollama/llama3:8b")
+ with monkeypatch.context() as warm_only:
+ # Scanning here would run ahead of the bounded cold-index wait its callers apply.
+ warm_only.setattr(resolver, "_index", _refusing_to_scan)
+ return inf._loaded_satisfies("ollama/llama3:8b")
+
+ assert _satisfied() is True
+ assert [m["id"] for m in inf._openai_model_objects()] == ["ollama/llama3:8b"]
+ # Whichever spelling the inventory gave a recipe: this link, an older link name, the alias.
+ for stored in (link, link.with_name("llama3-latest-Q4_K_M.gguf"), alias_link):
+ assert _validate(str(stored)).resident is True
+ _retag(store, "latest", "d" * 64)
+ assert _satisfied() is True
+ assert [m["id"] for m in inf._openai_model_objects()] == ["ollama/llama3:8b"]
+
+ # Re-pulled: the spelling is the same and the weights under it are not.
+ _retag(store, "8b", "c" * 64)
+ assert _satisfied() is False
+ assert inf._openai_model_objects() == []
+ assert _validate(str(alias_link)).resident is False
+
+
+def _validate(
+ model_path: str,
+ *,
+ variant = None,
+ gguf_file = ...,
+):
+ from models.inference import ValidateModelRequest
+
+ config = SimpleNamespace(
+ identifier = model_path,
+ display_name = Path(model_path).name,
+ is_gguf = True,
+ gguf_file = model_path if gguf_file is ... else gguf_file,
+ is_lora = False,
+ is_vision = False,
+ )
+ request = ValidateModelRequest(model_path = model_path, gguf_variant = variant)
+ with patch.object(inf.ModelConfig, "from_identifier", return_value = config):
+ return asyncio.run(inf.validate_model(request, current_subject = "t"))
+
+
+def test_validate_answers_for_the_artifact_it_resolved(store, monkeypatch, tmp_path, serving):
+ """The variant picks the quant and a lease picks the file, so the identifier decides neither."""
+ from hub.utils import gguf as gguf_utils
+
+ def quant(name):
+ return str(tmp_path / f"model-{name}.gguf")
+
+ def loaded(
+ identifier,
+ path,
+ variant = None,
+ ):
+ return SimpleNamespace(
+ is_loaded = True,
+ model_identifier = identifier,
+ _openai_advertised_id = None,
+ hf_variant = variant,
+ gguf_path = path,
+ )
+
+ monkeypatch.setattr(gguf_utils, "resolve_local_gguf_path", lambda _id, name: quant(name))
+ # By repo id, then out of the directory the quants share. Q8_0 is loaded both times.
+ for identifier, by_file in (("org/model-GGUF", False), (str(tmp_path), True)):
+ serving(loaded(identifier, quant("Q8_0"), variant = "Q8_0"))
+ for name, expected in (("Q8_0", True), ("Q4_K_M", False)):
+ asked = quant(name) if by_file else None
+ assert _validate(identifier, variant = name, gguf_file = asked).resident is expected
+
+ one, other = (tmp_path / name for name in ("loaded.gguf", "other.gguf"))
+ for path in (one, other):
+ path.write_bytes(b"GGUF")
+ serving(loaded(str(one), str(one)))
+ for granted, expected in ((other, False), (one, True)):
+ monkeypatch.setattr(
+ inf,
+ "_resolve_model_identifier_for_request",
+ lambda request, granted = granted, **kwargs: (str(granted), granted.name, True),
+ )
+ assert _validate(str(one)).resident is expected
+
+
+def test_a_symlinked_manifests_dir_still_resolves(tmp_path, monkeypatch):
+ """A reference carries the canonical path, a scan the spelling it walked."""
+ staging, elsewhere, root = (tmp_path / name for name in ("staging", "elsewhere", "ollama"))
+ _write_ollama_store(staging)
+ elsewhere.mkdir()
+ root.mkdir()
+ (staging / "manifests").rename(elsewhere / "manifests")
+ (staging / "blobs").rename(root / "blobs")
+ (root / "manifests").symlink_to(elsewhere / "manifests", target_is_directory = True)
+ for module in (paths, ollama):
+ monkeypatch.setattr(module, "ollama_model_dirs", lambda: [root])
+
+ (row,) = models_route._scan_ollama_dir(root, materialize_links = False)
+ assert ollama.ollama_model_ref_files(row.id)[0].startswith(str(root / "blobs"))
+
+
+def test_stop_loading_reaches_a_tag_the_load_renamed(store):
+ """Chat holds the link it picked; the load runs as the tag. Stop must still find it."""
+ ref = models_route._scan_ollama_dir(store, materialize_links = False)[0].id
+ link = models_route._scan_ollama_dir(store, materialize_links = True)[0].id
+ assert link != ref
+ assert inf._names_the_resident_model(ref, link)
+ assert inf._names_the_loading_model(ref, link)
+ assert not inf._names_the_resident_model(ref, str(store / "blobs" / "nothing.gguf"))
diff --git a/studio/backend/tests/test_ollama_manifest_shape_guard.py b/studio/backend/tests/test_ollama_manifest_shape_guard.py
index f8ebed3ab94..4e78634a522 100644
--- a/studio/backend/tests/test_ollama_manifest_shape_guard.py
+++ b/studio/backend/tests/test_ollama_manifest_shape_guard.py
@@ -29,7 +29,7 @@ def _manifest_dir(root, model = "foo"):
def _write_good_model(
root,
model = "good",
- blob = "sha256-abc123",
+ blob = "sha256:abc123",
):
"""A manifest whose model layer resolves to a real blob, i.e. one the scan must surface."""
blobs = root / "blobs"
diff --git a/studio/backend/tests/test_openai_catalog.py b/studio/backend/tests/test_openai_catalog.py
index dbd9c3948ea..6b0fdcf5334 100644
--- a/studio/backend/tests/test_openai_catalog.py
+++ b/studio/backend/tests/test_openai_catalog.py
@@ -206,7 +206,11 @@ def test_empty_and_errored_scans_are_cached(monkeypatch):
for outcome in ("empty", "error"):
calls = {"n": 0}
- def _scan(_root, _outcome = outcome):
+ def _scan(
+ _root,
+ _outcome = outcome,
+ **_kwargs,
+ ):
calls["n"] += 1
if _outcome == "error":
raise RuntimeError("scan blew up")
@@ -234,7 +238,7 @@ def test_catalog_ttl_starts_after_scan_completes(monkeypatch):
calls = {"n": 0}
- def _slow_scan(_root):
+ def _slow_scan(_root, **_kwargs):
calls["n"] += 1
clock["t"] += inf._CATALOG_TTL_S + 10 # the scan itself outlives the TTL
return [_Info("/m/A.gguf", "A")]
@@ -272,7 +276,7 @@ def test_cached_local_catalog_offloads_and_caches(monkeypatch):
# cached, so a burst of /v1/models calls does not re-scan or block.
calls = {"scan": 0, "threaded": 0}
- def _fake_collect(_root):
+ def _fake_collect(_root, **_kwargs):
calls["scan"] += 1
return [_Info("/data/models/A.gguf", "A")]
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.ts
index e8333b2d8dd..ed5ecd56694 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
@@ -33,7 +33,7 @@ import {
import { consumeNativePathToken } from "@/features/native-intents/api";
// eslint-disable-next-line no-restricted-imports -- Avoid the hub barrel's React and download-manager exports.
import {
- isOllamaLinkPath,
+ isOllamaModelId,
modelDisplayName,
} from "@/features/hub/lib/model-identity";
// eslint-disable-next-line no-restricted-imports -- Avoid the hub barrel's React and download-manager exports.
@@ -1226,7 +1226,7 @@ export function useChatModelRuntime() {
// token must not gate a local or cached-LoRA load. An Ollama row's id is an opaque
// `ollama-manifest:` reference rather than a path, so isLocalModelPath does not recognise it.
const mayReachHub =
- !isLocal && !isOllamaLinkPath(modelId) && nativePathToken == null;
+ !isLocal && !isOllamaModelId(modelId) && nativePathToken == null;
if (mayReachHub) {
const preparedToken = await prepareHfTokenForUse(hfToken);
if (!preparedToken.proceed) {
diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.ts
index 14ad50ca4ac..4576cf842af 100644
--- a/studio/frontend/src/features/chat/index.ts
+++ b/studio/frontend/src/features/chat/index.ts
@@ -26,6 +26,7 @@ export {
notifyChatHistoryUpdated,
removeScanFolder,
revealCachedModel,
+ validateModel,
type BrowseFoldersResponse,
type CachedGgufRepo,
type CachedModelRepo,
diff --git a/studio/frontend/src/features/chat/types/api.ts b/studio/frontend/src/features/chat/types/api.ts
index f3b1c5e2925..3966d82ac80 100644
--- a/studio/frontend/src/features/chat/types/api.ts
+++ b/studio/frontend/src/features/chat/types/api.ts
@@ -124,6 +124,8 @@ export interface ValidateModelResponse {
valid: boolean;
message: string;
identifier?: string | null;
+ /** Decided from the files, so an Ollama tag answers for whichever spelling loaded it. */
+ resident?: boolean;
display_name?: string | null;
is_gguf?: boolean;
is_diffusion?: boolean;
diff --git a/studio/frontend/src/features/hub/index.ts b/studio/frontend/src/features/hub/index.ts
index e445f79ad84..b3016d15f02 100644
--- a/studio/frontend/src/features/hub/index.ts
+++ b/studio/frontend/src/features/hub/index.ts
@@ -105,6 +105,7 @@ export { hubTokenHeader } from "./lib/hub-token-header";
export {
ggufVariantsMatch,
isOllamaLinkPath,
+ isOllamaModelId,
normalizeGgufVariantIdentity,
normalizeModelIdentity,
publicModelId,
diff --git a/studio/frontend/src/features/hub/lib/model-identity.ts b/studio/frontend/src/features/hub/lib/model-identity.ts
index 6d6545714a7..f94a9223734 100644
--- a/studio/frontend/src/features/hub/lib/model-identity.ts
+++ b/studio/frontend/src/features/hub/lib/model-identity.ts
@@ -193,26 +193,29 @@ export function residentModelIdMatches(
});
}
-// Ollama's blobs reach the picker through a symlink dir that local_model_resolver.py refuses
-// to index, so mirroring their settings would advertise a load that cannot happen. The
-// inventory's opaque `ollama-manifest:` references resolve to those same skipped links, so
-// they carry the same non-promise.
const OLLAMA_LINK_SEGMENTS = new Set([".studio_links", "ollama_links"]);
const OLLAMA_MANIFEST_REF_PREFIX = "ollama-manifest:";
+/** A `.gguf` link an earlier load materialized. The resolver refuses to index one, so mirroring
+ * settings onto it would advertise a load that cannot happen; a reference it does index. */
export function isOllamaLinkPath(modelId: string | null | undefined): boolean {
if (!modelId) {
return false;
}
- if (modelId.startsWith(OLLAMA_MANIFEST_REF_PREFIX)) {
- return true;
- }
return modelId
.replace(BACKSLASHES_RE, "/")
.split("/")
.some((segment) => OLLAMA_LINK_SEGMENTS.has(segment));
}
+/** Either spelling of an Ollama model: the inventory's reference, or a materialized link. */
+export function isOllamaModelId(modelId: string | null | undefined): boolean {
+ return (
+ Boolean(modelId?.startsWith(OLLAMA_MANIFEST_REF_PREFIX)) ||
+ isOllamaLinkPath(modelId)
+ );
+}
+
// A dropped or file-picked GGUF is the API's second unreachable identity: /status withholds the
// host path for a lease-backed load, so the browser keys settings by the bare file name, which
// _build_index (path and stem only) never uses. Anything loadable carries a separator.
diff --git a/studio/frontend/src/features/model-picker/api/migrate-model-overrides.ts b/studio/frontend/src/features/model-picker/api/migrate-model-overrides.ts
index d3e22ad9bee..25fe0aacf3d 100644
--- a/studio/frontend/src/features/model-picker/api/migrate-model-overrides.ts
+++ b/studio/frontend/src/features/model-picker/api/migrate-model-overrides.ts
@@ -7,6 +7,7 @@
import {
isNativeFileLabel,
isOllamaLinkPath,
+ isOllamaModelId,
normalizeGgufVariantIdentity,
normalizeModelIdentity,
splitQuantSuffix,
@@ -23,7 +24,8 @@ import {
toApiOverride,
} from "./model-overrides";
-const DONE_FLAG = "unsloth_model_overrides_backfilled_v1";
+// Bumped when the filter below started admitting Ollama tags, so a completed v1 pass reruns.
+const DONE_FLAG = "unsloth_model_overrides_backfilled_v2";
function alreadyRan(): boolean {
try {
@@ -78,12 +80,12 @@ export async function backfillModelOverrides(): Promise<void> {
return;
}
const local = listPerModelConfigs().filter(
- // A quant means GGUF, the only thing auto-switch resolves. A standalone .gguf is stored with a
- // null variant, so it needs the extra test. An Ollama blob sits behind a link dir the resolver
- // skips, and a bare file name is a dropped file's label, which the resolver never keys.
+ // A quant means GGUF, all auto-switch resolves; a .gguf and a reference have a null variant.
+ // A link sits in a dir the resolver skips, a bare file name is a label it never keys.
(entry) =>
(entry.ggufVariant != null ||
- entry.modelId.toLowerCase().endsWith(".gguf")) &&
+ entry.modelId.toLowerCase().endsWith(".gguf") ||
+ isOllamaModelId(entry.modelId)) &&
!isOllamaLinkPath(entry.modelId) &&
!isNativeFileLabel(entry.modelId) &&
!isDefaultConfig(entry.config),
diff --git a/studio/frontend/src/features/model-picker/components/model-config-page.tsx b/studio/frontend/src/features/model-picker/components/model-config-page.tsx
index 4abc35f76f6..3476469bac3 100644
--- a/studio/frontend/src/features/model-picker/components/model-config-page.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-config-page.tsx
@@ -2881,7 +2881,8 @@ export function ModelConfigPage({
}
// Mirror to the server so an API load gets these settings, not app defaults. Best-effort, and
// skipped when the localStorage write failed or the two would permanently disagree. Gated on
- // auto-switch reach, not GGUF-ness: the resolver skips Ollama, and a native-path lease is the same.
+ // auto-switch reach, not GGUF-ness: the resolver skips a materialized Ollama link, and a
+ // native-path lease is the same.
// A forget also drops the local records for every other spelling the server reports clearing.
if (
!saveFailed &&
diff --git a/studio/frontend/src/features/model-picker/components/sidebar-model-config.tsx b/studio/frontend/src/features/model-picker/components/sidebar-model-config.tsx
index b786da8f192..2877d4235b0 100644
--- a/studio/frontend/src/features/model-picker/components/sidebar-model-config.tsx
+++ b/studio/frontend/src/features/model-picker/components/sidebar-model-config.tsx
@@ -56,7 +56,7 @@ export function SidebarModelConfig({
displayName: ggufVariant ? `${leaf} · ${ggufVariant}` : leaf,
ggufVariant: settingsGgufVariant,
isGguf,
- // An Ollama blob sits behind a link dir the resolver skips, so do not mirror it.
+ // A materialized Ollama link sits in a dir the resolver skips, so do not mirror it.
apiLoadable: isGguf && !isOllamaLinkPath(modelId),
meta: {
source: "local",
diff --git a/studio/frontend/src/features/model-picker/model-config/model-config-handoff.ts b/studio/frontend/src/features/model-picker/model-config/model-config-handoff.ts
index beddfa8700e..0809f91caad 100644
--- a/studio/frontend/src/features/model-picker/model-config/model-config-handoff.ts
+++ b/studio/frontend/src/features/model-picker/model-config/model-config-handoff.ts
@@ -9,6 +9,7 @@ import type {
import {
ggufVariantsMatch,
isOllamaLinkPath,
+ isOllamaModelId,
isStandaloneGgufPath,
modelDisplayName,
residentModelIdMatches,
@@ -105,7 +106,7 @@ export function modelConfigTargetIsResident({
if (
target.isGguf &&
target.ggufVariant == null &&
- (isStandaloneGgufPath(target.id) || isOllamaLinkPath(target.id))
+ (isStandaloneGgufPath(target.id) || isOllamaModelId(target.id))
) {
return true;
}
diff --git a/studio/frontend/src/features/model-picker/model-config/model-identity.ts b/studio/frontend/src/features/model-picker/model-config/model-identity.ts
index b0ec746d845..0ddc8399d74 100644
--- a/studio/frontend/src/features/model-picker/model-config/model-identity.ts
+++ b/studio/frontend/src/features/model-picker/model-config/model-identity.ts
@@ -12,6 +12,7 @@ import { looksLikeLocalPath } from "@/lib/local-path";
export {
isNativeFileLabel,
isOllamaLinkPath,
+ isOllamaModelId,
isStandaloneGgufPath,
ggufVariantsMatch,
modelDisplayName,
diff --git a/studio/frontend/src/features/recipe-studio/hooks/use-recipe-executions.ts b/studio/frontend/src/features/recipe-studio/hooks/use-recipe-executions.ts
index f397f368334..02e770aa89c 100644
--- a/studio/frontend/src/features/recipe-studio/hooks/use-recipe-executions.ts
+++ b/studio/frontend/src/features/recipe-studio/hooks/use-recipe-executions.ts
@@ -1,13 +1,12 @@
// 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 { getInferenceStatus, loadModel } from "@/features/chat";
-import { unpinnedLoadContext } from "@/features/chat/presets/preset-policy";
import { usePlatformStore } from "@/config/env";
-import {
- DEFAULT_MAX_SEQ_LENGTH,
- isServedByMlx,
-} from "@/features/model-picker";
+import { getInferenceStatus, loadModel, validateModel } from "@/features/chat";
+import { unpinnedLoadContext } from "@/features/chat/presets/preset-policy";
+// eslint-disable-next-line no-restricted-imports -- Avoid the hub barrel's React and download-manager exports.
+import { isOllamaModelId } from "@/features/hub/lib/model-identity";
+import { DEFAULT_MAX_SEQ_LENGTH, isServedByMlx } from "@/features/model-picker";
import { createLoadingToastIcon, toast } from "@/lib/toast";
import { toastError } from "@/shared/toast";
import { useCallback, useEffect, useState } from "react";
@@ -74,10 +73,7 @@ type LocalModelSelection = {
target: string;
ggufVariant: string;
aliases: string[];
- /** The context that model's own load asked for, for a selection captured to be
- * restored. Absent for a recipe's own target, which pins nothing. */
requestedContextLength?: number | null;
- /** Whether MLX served it. Only there is a positive request above unambiguous. */
isMlx?: boolean;
};
@@ -226,13 +222,40 @@ function localSelectionMatchesActive(input: {
);
}
-/** A context request, read only where it means something.
- *
- * An unpinned MLX load sends 0, so a positive value there is a pin. llama.cpp is
- * ambiguous: a same-model reload echoes the resolved n_ctx while the control is still
- * Auto (see resolve-ctx-pin-seed.ts), so reading that as a pin would reload the model
- * on every run and then re-pin an Auto-sized one. 0 is Auto on both.
- */
+/** One blob has three spellings and can carry two tags, so a disagreement is put to the server. */
+async function localSelectionMatchesResident(input: {
+ target: string;
+ ggufVariant: string;
+ activeModel: string | null | undefined;
+ activeVariant: string;
+}): Promise<boolean> {
+ if (localSelectionMatchesActive(input)) {
+ return true;
+ }
+ if (!isOllamaModelId(input.target)) {
+ return false;
+ }
+ try {
+ const validated = await validateModel({
+ // biome-ignore lint/style/useNamingConvention: api schema
+ model_path: input.target,
+ // biome-ignore lint/style/useNamingConvention: api schema
+ hf_token: null,
+ // Only `resident` is read; these are what /validate assumes when a caller sends none.
+ // biome-ignore lint/style/useNamingConvention: api schema
+ max_seq_length: 0,
+ // biome-ignore lint/style/useNamingConvention: api schema
+ load_in_4bit: true,
+ // biome-ignore lint/style/useNamingConvention: api schema
+ is_lora: false,
+ });
+ return validated.resident === true;
+ } catch {
+ return false;
+ }
+}
+
+/** A context request, read only where it pins: llama.cpp echoes n_ctx while Auto, MLX does not. */
function contextIntent(
value: number | null | undefined,
isMlx: boolean | null | undefined,
@@ -240,32 +263,29 @@ function contextIntent(
return isMlx && typeof value === "number" && value > 0 ? value : null;
}
-async function isLocalModelAlreadyLoaded(
+export async function isLocalModelAlreadyLoaded(
selection: LocalModelSelection,
): Promise<boolean> {
const { target, ggufVariant, requestedContextLength } = selection;
try {
const status = await getInferenceStatus();
if (
- !localSelectionMatchesActive({
+ !(await localSelectionMatchesResident({
target,
ggufVariant,
activeModel: status.model_identifier ?? status.active_model,
activeVariant: status.gguf_variant?.trim() ?? "",
- })
+ }))
) {
return false;
}
- // Same checkpoint, different context intent is still a different load: a recipe that
- // asked for nothing must not inherit whatever window Chat pinned.
- // The resident backend decides, since both values describe the load that is running.
+ // A different context intent is a different load: asking for nothing inherits no pin.
const residentIsMlx = status.is_mlx ?? false;
return (
contextIntent(requestedContextLength, residentIsMlx) ===
contextIntent(status.requested_context_length, residentIsMlx)
);
} catch {
- // Fall through to load attempt; the backend will re-error if needed.
return false;
}
}
@@ -387,7 +407,7 @@ async function getRestorableActiveLocalModelSelection(): Promise<RestorableLocal
ggufVariant: status.gguf_variant?.trim() ?? "",
aliases: ["previous Chat model"],
requestedContextLength: status.requested_context_length ?? null,
- isMlx: status.is_mlx ?? false,
+ isMlx: status.is_mlx ?? false,
},
unrestorableLabel: null,
};
diff --git a/studio/frontend/tests/chat-load-hub-token-reach.test.ts b/studio/frontend/tests/chat-load-hub-token-reach.test.ts
index b88a6de3daf..df713d4cb10 100644
--- a/studio/frontend/tests/chat-load-hub-token-reach.test.ts
+++ b/studio/frontend/tests/chat-load-hub-token-reach.test.ts
@@ -11,11 +11,11 @@ import assert from "node:assert/strict";
import test from "node:test";
import { isLocalModelPath } from "../src/features/chat/utils/model-download-staging.ts";
-import { isOllamaLinkPath } from "../src/features/hub/lib/model-identity.ts";
+import { isOllamaModelId } from "../src/features/hub/lib/model-identity.ts";
// The predicate as the load path composes it; nativePathToken is the file-lease case.
function mayReachHub(modelId: string, nativePathToken: string | null): boolean {
- const servedFromDisk = isLocalModelPath(modelId) || isOllamaLinkPath(modelId);
+ const servedFromDisk = isLocalModelPath(modelId) || isOllamaModelId(modelId);
return !servedFromDisk && nativePathToken == null;
}
diff --git a/studio/frontend/tests/hub-progress-token-boundary.test.ts b/studio/frontend/tests/hub-progress-token-boundary.test.ts
index 7045f29d534..deb08b5ee46 100644
--- a/studio/frontend/tests/hub-progress-token-boundary.test.ts
+++ b/studio/frontend/tests/hub-progress-token-boundary.test.ts
@@ -115,7 +115,7 @@ test("a local load is not gated behind Hub token preparation", () => {
// An Ollama row is local too, but its id is an opaque reference rather than a path,
// so isLocalModelPath alone lets it through. chat-load-hub-token-reach.test.ts pins
// what the predicate itself classifies.
- assert.match(guarded, /!isOllamaLinkPath\(modelId\)/);
+ assert.match(guarded, /!isOllamaModelId\(modelId\)/);
assert.match(guarded, /nativePathToken\s*==\s*null/);
assert.match(guarded, /if\s*\(mayReachHub\)\s*\{[\s\S]*prepareHfTokenForUse\(hfToken\)/);
});
diff --git a/studio/frontend/tests/model-config-handoff.test.ts b/studio/frontend/tests/model-config-handoff.test.ts
index 0959882c210..e647283f59c 100644
--- a/studio/frontend/tests/model-config-handoff.test.ts
+++ b/studio/frontend/tests/model-config-handoff.test.ts
@@ -227,21 +227,22 @@ test("only local GGUF files may ignore a derived active variant", () => {
);
});
-test("an Ollama load identity never advertises API-loadable settings", () => {
+test("an Ollama load identity mirrors its settings only when the API can reach it", () => {
const id = "ollama-manifest:library/model:latest";
- const target = modelConfigTarget(
- id,
- {
+ const link = "/home/u/.ollama/.studio_links/ab12/model-latest.gguf";
+ const local = (loadId: string) =>
+ ({
source: "local",
isLora: false,
- loadId: id,
+ loadId,
isDownloaded: true,
isGguf: true,
- },
- "Llama 3.2",
- );
+ }) as const;
+ const target = modelConfigTarget(id, local(id), "Llama 3.2");
- assert.equal(target.apiLoadable, false);
+ // The reference is what /v1/models advertises; the link it materializes the resolver skips.
+ assert.equal(target.apiLoadable, true);
+ assert.equal(modelConfigTarget(link, local(link), "").apiLoadable, false);
assert.equal(target.configId, undefined);
assert.equal(target.displayName, "Llama 3.2");
assert.equal(
diff --git a/studio/frontend/tests/model-identity.test.ts b/studio/frontend/tests/model-identity.test.ts
index 6bf6e0de947..2cfedd534e6 100644
--- a/studio/frontend/tests/model-identity.test.ts
+++ b/studio/frontend/tests/model-identity.test.ts
@@ -6,6 +6,7 @@ import test from "node:test";
import {
isOllamaLinkPath,
+ isOllamaModelId,
isStandaloneGgufPath,
modelDisplayName,
modelIdsMatch,
@@ -210,6 +211,9 @@ test("Ollama link paths are recognised the way the resolver excludes them", () =
assert.equal(isOllamaLinkPath("/srv/models/Qwen3-8B-Q4_K_M.gguf"), false);
assert.equal(isOllamaLinkPath("unsloth/Qwen3-8B-GGUF"), false);
assert.equal(isOllamaLinkPath(null), false);
+ // A manifest reference is not one: the resolver indexes it and /v1/models advertises its tag.
+ assert.equal(isOllamaLinkPath("ollama-manifest:%2Fh%2Fllama3"), false);
+ assert.equal(isOllamaModelId("ollama-manifest:%2Fh%2Fllama3"), true);
});
// The backfill matches on the folded identity, unambiguous only because storage holds one record per model.
diff --git a/studio/frontend/tests/recipe-resident-selection.test.ts b/studio/frontend/tests/recipe-resident-selection.test.ts
new file mode 100644
index 00000000000..6eb8b1d2504
--- /dev/null
+++ b/studio/frontend/tests/recipe-resident-selection.test.ts
@@ -0,0 +1,87 @@
+// 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 stored spelling, the tag a load records, two tags on one blob: only the server relates them.
+
+import assert from "node:assert/strict";
+import test from "node:test";
+import { isOllamaModelId } from "../src/features/hub/lib/model-identity.ts";
+import { loadWithStubs } from "./helpers/module-stubs.ts";
+
+type Selection = { target: string; ggufVariant: string; aliases: string[] };
+
+const chat = {
+ status: {} as Record<string, unknown>,
+ validated: [] as string[],
+ resident: false,
+ getInferenceStatus: () => Promise.resolve(chat.status),
+ // biome-ignore lint/style/useNamingConvention: api schema
+ validateModel: (request: { model_path: string }) => {
+ chat.validated.push(request.model_path);
+ return Promise.resolve({ valid: true, resident: chat.resident });
+ },
+};
+
+// The gate is what this drives, so the hook's other dependencies are empty shells: reaching
+// one throws instead of passing quietly.
+const { isLocalModelAlreadyLoaded } = loadWithStubs<{
+ isLocalModelAlreadyLoaded: (selection: Selection) => Promise<boolean>;
+}>(
+ new URL(
+ "../src/features/recipe-studio/hooks/use-recipe-executions.ts",
+ import.meta.url,
+ ),
+ {
+ "@/features/chat": chat,
+ "@/features/hub/lib/model-identity": { isOllamaModelId },
+ "@/features/chat/presets/preset-policy": {},
+ "@/config/env": {},
+ "@/features/model-picker": {},
+ "@/lib/toast": {},
+ "@/shared/toast": {},
+ react: {},
+ "zustand/react/shallow": {},
+ "../api": {},
+ "../data/executions-db": {},
+ "../executions/execution-helpers": {},
+ "../executions/hydration": {},
+ "../executions/run-settings": {},
+ "../executions/runtime": {},
+ "../stores/recipe-executions": {},
+ "../executions/tracker": {},
+ },
+);
+
+const LINK = "/home/u/.ollama/.studio_links/ab12cd34ef/llama3-latest.gguf";
+const REF = "ollama-manifest:%2Fh%2F.ollama%2Fmanifests%2Fllama3%2Flatest";
+
+async function loaded(target: string, resident: boolean): Promise<boolean> {
+ // biome-ignore lint/style/useNamingConvention: api schema
+ chat.status = { model_identifier: REF, gguf_variant: "" };
+ chat.validated = [];
+ chat.resident = resident;
+ return await isLocalModelAlreadyLoaded({
+ target,
+ ggufVariant: "",
+ aliases: [],
+ });
+}
+
+test("a stored spelling is loaded when the server says its tag is resident", async () => {
+ for (const stored of [
+ LINK,
+ LINK.replace("llama3-latest.gguf", "llama3-latest-Q4_K_M.gguf"),
+ LINK.replace("llama3-latest", "llama3-8b"),
+ ]) {
+ assert.equal(await loaded(stored, true), true);
+ assert.deepEqual(chat.validated, [stored]);
+ assert.equal(await loaded(stored, false), false);
+ }
+});
+
+test("only a name that disagrees and is an Ollama one costs a request", async () => {
+ assert.equal(await loaded(REF, false), true);
+ assert.deepEqual(chat.validated, []);
+ assert.equal(await loaded("/models/mistral.gguf", true), false);
+ assert.deepEqual(chat.validated, []);
+});
diff --git a/studio/frontend/tests/server-tuning-upgrade-downgrade-compat.test.ts b/studio/frontend/tests/server-tuning-upgrade-downgrade-compat.test.ts
index 9f5275cdb86..830d02a6172 100644
--- a/studio/frontend/tests/server-tuning-upgrade-downgrade-compat.test.ts
+++ b/studio/frontend/tests/server-tuning-upgrade-downgrade-compat.test.ts
@@ -50,7 +50,7 @@ const { backfillModelOverrides } = await import(
const { setAuthFetchHandler } = await import("./helpers/store-stubs/auth.ts");
const STORAGE_KEY = "unsloth_model_configs";
-const BACKFILL_FLAG = "unsloth_model_overrides_backfilled_v1";
+const BACKFILL_FLAG = "unsloth_model_overrides_backfilled_v2";
const MODEL = "unsloth/Repo-GGUF";
const VARIANT = "Q4_K_M";
@@ -321,6 +321,29 @@ test("the one-time backfill now uploads a tuning-only config", async () => {
assert.equal(store.get(BACKFILL_FLAG), "1", "a completed pass must not run again");
});
+test("the backfill offers an Ollama tag's settings even after the v1 pass ran", async () => {
+ // The v1 filter dropped these, so the marker had to move with it, or an install past that pass
+ // would never mirror what it saved for an Ollama model.
+ store.clear();
+ store.set("unsloth_model_overrides_backfilled_v1", "1");
+ const ref = "ollama-manifest:%2Fh%2F.ollama%2Fmanifests%2Fllama3%2Flatest";
+ assert.ok(savePerModelConfig(ref, null, config({ cacheRam: -1 })));
+
+ const puts: unknown[] = [];
+ setAuthFetchHandler((_input, init) => {
+ if (init?.method === "PUT") {
+ puts.push(JSON.parse(String(init.body)).model_id);
+ }
+ return new Response(JSON.stringify({ overrides: {} }), { status: 200 });
+ });
+ try {
+ await backfillModelOverrides();
+ } finally {
+ setAuthFetchHandler(null);
+ }
+ assert.deepEqual(puts, [ref.toLowerCase()]);
+});
+
test("an all-default config is still filtered out of the backfill", async () => {
// The gate the case above walks through has to stay shut for everything else, or
// every model the user ever opened is mirrored on first launch.
diff --git a/tests/studio/test_recipe_context_intent.py b/tests/studio/test_recipe_context_intent.py
index b15957e5856..2f938b771d5 100644
--- a/tests/studio/test_recipe_context_intent.py
+++ b/tests/studio/test_recipe_context_intent.py
@@ -38,7 +38,7 @@ def _harness_source() -> str:
return "// @ts-nocheck\nexport " + slice_between(
read(RECIPES),
"function contextIntent(",
- "\nasync function isLocalModelAlreadyLoaded(",
+ "\nexport async function isLocalModelAlreadyLoaded(",
)