Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: deleting a GGUF row no longer deletes other builds at the same quant (#13368)
alkinun0b40c8150067
diff --git a/studio/backend/hub/routes/inventory.py b/studio/backend/hub/routes/inventory.pyindex ca301f4ce1b..939581b4dc5 100644--- a/studio/backend/hub/routes/inventory.py+++ b/studio/backend/hub/routes/inventory.py@@ -321,21 +321,19 @@ async def list_hidden_models(async def delete_impact(repo_id: str = Body(...),variant: Optional[str] = Body(None),- # The copy the listing advertised, so the preview describes the delete that will follow.cache_path: Optional[str] = Body(None),+ hf_token: HfTokenArg = Depends(get_request_hf_token),current_subject: str = Depends(get_current_subject),via_api_key: bool = Depends(authenticated_via_api_key),):- """Preview a delete: bytes reclaimed, shared assets retained, and anything blocking it.-- POST rather than GET because a repo id is a path-shaped value and this reads no cache of its- own; it is a pure query and mutates nothing.- """- # The preview names the folder it would delete, so it takes the same host-path boundary as- # the inventory routes; an API-key caller gets the opaque reference instead.+ """preview a delete without mutation; POST avoids path-shaped repo IDs."""+ # use inventory host-path redaction so API-key callers receive only opaque referencesreturn redact_host_paths(await companion_cleanup.delete_impact_response(- repo_id, variant, resolve_host_path_reference(cache_path) or cache_path+ repo_id,+ variant,+ resolve_host_path_reference(cache_path) or cache_path,+ hf_token,),via_api_key = via_api_key,)@@ -346,8 +344,7 @@ async def orphan_companions(current_subject: str = Depends(get_current_subject),via_api_key: bool = Depends(authenticated_via_api_key),):- """Cached companion assets no installed model needs. Listing only; removal goes through- the ordinary guarded delete."""+ """list unused companion assets; removal still uses the guarded delete."""return redact_host_paths(await companion_cleanup.orphan_companions_response(), via_api_key = via_api_key)diff --git a/studio/backend/hub/services/models/companion_cleanup.py b/studio/backend/hub/services/models/companion_cleanup.pyindex 30fd484dd48..ee8cbd99724 100644--- a/studio/backend/hub/services/models/companion_cleanup.py+++ b/studio/backend/hub/services/models/companion_cleanup.py@@ -1,12 +1,7 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0-"""Delete preflight and orphaned-companion cleanup for image-model assets.--Three answers live here, all derived from the cache scan at call time (see ``hub.utils.companion_assets`` for why nothing is counted): :func:`delete_impact_response` (what a pending delete reclaims and what it leaves behind), :func:`companion_dependents` (who still needs a companion base, used as a delete guard) and :func:`orphan_companions_response` (companion bases no installed model needs any more).--Sizes are real on-disk blob bytes from the HF cache scan, deduped per blob, not Hub metadata: the number in a delete dialog has to be the number the disk gives back.-"""+"""delete previews and orphan cleanup report deduplicated HF cache blob bytes, not Hub metadata."""from __future__ import annotations@@ -18,20 +13,20 @@ from typing import Optionalfrom fastapi import HTTPExceptionfrom loggers import get_logger-from hub.utils import companion_assets+from hub.utils import companion_assets, download_registryfrom hub.utils.gguf import gguf_variant_keyfrom hub.services.models import cache_inventoryfrom hub.services.models.common import _is_main_gguf_filenamefrom hub.utils.paths import is_valid_gguf_variant as _is_valid_gguf_variantfrom hub.utils.paths import is_valid_repo_id as _is_valid_repo_id-from utils.paths.path_utils import is_appledouble_metadatalogger = get_logger(__name__)def _repo_blob_bytes(repo_info, *, only = None) -> int:- """On-disk bytes of *repo_info*, deduped by blob so a file shared across revisions counts once. ``only`` is an optional predicate on the snapshot-relative file name."""+ """return deduplicated bytes; filtered blobs count only if every snapshot reference matches."""unique: dict[str, int] = {}+ retained: set[str] = set()for revision in getattr(repo_info, "revisions", ()) or ():rev_id = getattr(revision, "commit_hash", None) or str(id(revision))snapshot = getattr(revision, "snapshot_path", None)@@ -43,20 +38,21 @@ def _repo_blob_bytes(repo_info, *, only = None) -> int:name = Path(path).relative_to(Path(snapshot)).as_posix()except ValueError:pass- if only is not None and not only(name):- continuefrom hub.services.models.cache_inventory import _blob_key- unique[_blob_key(f, f"{rev_id}:{name}")] = int(getattr(f, "size_on_disk", 0) or 0)- return sum(unique.values())+ key = _blob_key(f, f"{rev_id}:{name}")+ if only is not None and not only(name, Path(path) if path else None):+ retained.add(key)+ continue+ unique[key] = int(getattr(f, "size_on_disk", 0) or 0)+ return sum(size for key, size in unique.items() if key not in retained)-# One definition, so the orphan listing and the delete preview cannot disagree about which cached repos are leftovers (see companion_assets.repo_holds_denoiser)._repo_holds_denoiser = companion_assets.repo_holds_denoiserdef _account_scans() -> list:- """A managed caller previews only the repos its grants cover; other accounts' downloads stay unseen."""+ """managed callers see only repositories covered by their grants."""scans = cache_inventory.all_hf_cache_scans()access = cache_inventory._account_access()if not access.managed_account():@@ -82,50 +78,71 @@ def _repos_by_id(cache_scans) -> dict[str, list]:return out-def _variant_keys(repo_info, variant: str) -> set[str]:- """The variant keys *variant* names in *repo_info*, from the destructive path's own resolver. The inventory and the delete both identify a row by ``gguf_variant_key``, which for a path-qualified checkpoint (``distilled/ltx-2.3-22b-distilled-Q6_K``) is not the bare quant label. Comparing labels here made the preview miss the file entirely: 0 B reclaimed, and the last checkpoint of a repo read as if a sibling survived, so its companions were described as retained rather than freed."""+def _variant_keys(+ repo_info,+ variant: str,+ preferred_path: Optional[str] = None,+) -> set[str]:+ """use deletion keys because path-qualified checkpoints do not match bare quant labels."""from hub.services.models.deletion import _variant_keys_to_delete- return {key.lower() for key in _variant_keys_to_delete(repo_info, variant)}---def _variant_bytes(repo_info, variant: str) -> int:- from hub.services.models.deletion import _audio_cpp_package_scope-- wanted = _variant_keys(repo_info, variant)- own, protected = _audio_cpp_package_scope(repo_info, variant)+ return {key.lower() for key in _variant_keys_to_delete(repo_info, variant, preferred_path)}+++def _variant_bytes(+ repo_info,+ variant: str,+ preferred_path: Optional[str] = None,+) -> int:+ from hub.services.models.deletion import (+ _audio_cpp_package_scope,+ _main_gguf_variant_keys,+ _main_key,+ )- def _matches(name: str) -> bool:- if name in protected:- return False- return name in own or (- _is_main_gguf_filename(name) and gguf_variant_key(name).lower() in wanted+ wanted = _variant_keys(repo_info, variant, preferred_path)+ main_keys = _main_gguf_variant_keys(repo_info, preferred_path)+ package_files, protected = _audio_cpp_package_scope(repo_info, variant)++ def _matches(name: str, path: Optional[Path]) -> bool:+ return (+ path is not None+ and name not in protected+ and (+ name in package_files+ or (_is_main_gguf_filename(name) and _main_key(path, main_keys) in wanted)+ ))return _repo_blob_bytes(repo_info, only = _matches)-def _remaining_main_gguf_variants(repo_info, *, excluding: Optional[str] = None) -> set[str]:- skip = _variant_keys(repo_info, excluding) if excluding else set()- found: set[str] = set()- for revision in getattr(repo_info, "revisions", ()) or ():- snapshot = getattr(revision, "snapshot_path", None)- for f in getattr(revision, "files", ()) or ():- name = str(getattr(f, "file_name", "") or "")- path = getattr(f, "file_path", None)- if path and snapshot:- try:- name = Path(path).relative_to(Path(snapshot)).as_posix()- except ValueError:- pass- if not _is_main_gguf_filename(name):- continue- # The delete this previews ignores proven metadata, so counting it here would report a checkpoint as surviving that the deletion itself does not see.- if path and is_appledouble_metadata(Path(path)):- continue- key = gguf_variant_key(name).lower()- if key and key not in skip:- found.add(key)- return found+def _remaining_main_gguf_variants(+ repo_info,+ *,+ excluding: Optional[str] = None,+ preferred_path: Optional[str] = None,+) -> set[str]:+ from hub.services.models.deletion import (+ _audio_cpp_package_scope,+ _main_gguf_variant_keys,+ _repo_file_matches,+ )++ skip = _variant_keys(repo_info, excluding, preferred_path) if excluding else set()+ package_files, protected = (+ _audio_cpp_package_scope(repo_info, excluding) if excluding else (frozenset(), frozenset())+ )+ removed_package_paths = {+ snap+ for snap, _blob, name in _repo_file_matches(repo_info, lambda name: name in package_files)+ if name not in protected+ }+ # deletion keys keep sibling builds with the same quant distinct+ return {+ key.lower()+ for path, key in _main_gguf_variant_keys(repo_info, preferred_path).items()+ if path not in removed_package_paths and key and key.lower() not in skip+ }def companion_dependents(@@ -134,14 +151,14 @@ def companion_dependents(*,ignore_repo_ids = (),) -> list[str]:- """Installed checkpoints that would still need *base_repo_id* after ignoring *ignore_repo_ids*, sorted for a stable message. Empty means the base is safe to remove."""+ """return sorted checkpoints needing *base_repo_id*; an empty result makes it removable."""scans = cache_scans if cache_scans is not None else cache_inventory.all_hf_cache_scans()required = companion_assets.required_companion_bases(scans, ignore_repo_ids = ignore_repo_ids)return sorted(required.get((base_repo_id or "").strip().lower(), set()))def _variant_is_a_required_companion_asset(repo_id: str, variant: str) -> bool:- """The deletion guard's predicate, shared so the preview and the refusal cannot disagree."""+ """share the deletion guard so preview and refusal agree."""from hub.services.models.deletion import _variant_is_a_required_companion_asset as _implreturn _impl(repo_id, variant)@@ -150,6 +167,7 @@ def _delete_impact_blocking(repo_id: str,variant: Optional[str],cache_path: Optional[str] = None,+ preferred_path: Optional[str] = None,) -> dict:scans = _account_scans()by_id = _repos_by_id(scans)@@ -157,7 +175,7 @@ def _delete_impact_blocking(all_copies = by_id.get(key, [])from hub.utils.gguf_sources import cached_gguf_action_path- cache_path = cached_gguf_action_path(repo_id, variant, cache_path)+ cache_path = cached_gguf_action_path(repo_id, variant, cache_path, preferred_path)repos = all_copiessurviving = []if cache_path:@@ -176,21 +194,25 @@ def _delete_impact_blocking(reclaimed = 0for repo_info in repos:- reclaimed += _variant_bytes(repo_info, variant) if variant else _repo_blob_bytes(repo_info)+ reclaimed += (+ _variant_bytes(repo_info, variant, preferred_path)+ if variant+ else _repo_blob_bytes(repo_info)+ )- # Would this delete leave the repo with no runnable checkpoint? Only then can its companions become reclaimable;- # while a sibling quant survives they stay in use.+ # companions become reclaimable only when no runnable checkpoint survivesremoves_last_checkpoint = not any(_repo_holds_denoiser(repo) for repo in surviving)if variant:for repo_info in repos:- if _remaining_main_gguf_variants(repo_info, excluding = variant):+ if _remaining_main_gguf_variants(+ repo_info, excluding = variant, preferred_path = preferred_path+ ):removes_last_checkpoint = Falsebreakignore = [repo_id] if removes_last_checkpoint else []required_after = companion_assets.required_companion_bases(scans, ignore_repo_ids = ignore)- # Companion bases THIS pick uses, from the same derivation the loader's resolver feeds.own_bases = companion_assets.required_companion_bases([_SingleRepoScan(repos)] if repos else [],)@@ -230,7 +252,7 @@ def _delete_impact_blocking(class _SingleRepoScan:- """Adapter presenting a fixed repo list with the attribute the derivation reads."""+ """present a fixed repo list through the scan interface."""def __init__(self, repos):self.repos = repos@@ -310,8 +332,9 @@ async def delete_impact_response(repo_id: str,variant: Optional[str] = None,cache_path: Optional[str] = None,+ hf_token: Optional[str] = None,) -> dict:- """What a delete of *repo_id* (/*variant*) would reclaim, retain, and be blocked by."""+ """preview bytes, retained assets, and blockers for deleting *repo_id* or its *variant*."""from core.inference.audio_cpp_models import cached_folder_targetfolder = await asyncio.to_thread(cached_folder_target, repo_id, (variant or "").strip() or None)@@ -327,7 +350,23 @@ async def delete_impact_response(variant = (variant or "").strip() or Noneif variant is not None and not _is_valid_gguf_variant(variant):raise HTTPException(status_code = 400, detail = f"Invalid gguf_variant: {variant!r}")- return await asyncio.to_thread(_delete_impact_blocking, repo_id, variant, cache_path)+ preferred_path = None+ if variant is not None:+ try:+ from hub.services.models import gguf_variants+ preferred_path = await gguf_variants.cached_gguf_variant_source_path(+ repo_id, variant, hf_token+ )+ except Exception as exc: # noqa: BLE001 -- local readiness is the offline fallback+ logger.debug(+ "Could not resolve the scoped GGUF source for %s [%s]: %s",+ repo_id,+ variant,+ download_registry.scrub_secrets(str(exc), hf_token = hf_token),+ )+ return await asyncio.to_thread(+ _delete_impact_blocking, repo_id, variant, cache_path, preferred_path+ )def _orphan_companions_blocking() -> dict:@@ -341,7 +380,7 @@ def _orphan_companions_blocking() -> dict:if required.get(base_key):continuerepos = by_id[base_key]- # A repo holding a runnable denoiser is a model the user installed: a companion fetch takes everything BUT transformer/, while a pipeline pick takes it, so its presence answers whether the user asked for this repo. Per COPY, since a delete is scoped to one cache root.+ # exclude user-installed copies with denoisers; companion fetches omit transformer/ and orphan rows are per rootrepos = [r for r in repos if not _repo_holds_denoiser(r)]if not repos:continuediff --git a/studio/backend/hub/services/models/deletion.py b/studio/backend/hub/services/models/deletion.pyindex 506010f51cc..363b5371b2d 100644--- a/studio/backend/hub/services/models/deletion.py+++ b/studio/backend/hub/services/models/deletion.py@@ -32,7 +32,11 @@ from hub.utils import inventory_scan as hf_cache_scanfrom hub.utils.gguf import (bare_quant_alias,extract_quant_token,+ gguf_listed_checkpoints,+ group_gguf_variant_files,+ gguf_variant_family,gguf_variant_key,+ gguf_variant_keys_by_checkpoint,is_qualified_gguf_variant_key,quant_token_with_bpw,remove_appledouble_sidecar,@@ -44,6 +48,7 @@ from hub.utils.hf_cache_state import (purge_partial_repo,purge_repo_cache_dirs,resolve_delete_target_root,+ snapshot_selection_key,)from hub.utils.paths import (is_valid_gguf_variant as _is_valid_gguf_variant,@@ -120,7 +125,7 @@ def _path_exists_or_symlink(path: Path) -> bool:def _unlink_snapshot_entry(snap: Path) -> int:- """Unlink one snapshot entry, plus any AppleDouble sidecar beside it. Returns the entries removed, which never counts the sidecar: it is metadata about a file the caller asked to remove, not a second file."""+ """unlink a snapshot entry and its AppleDouble sidecar, counting only the entry."""removed = 0if _path_exists_or_symlink(snap):snap.unlink()@@ -129,34 +134,40 @@ def _unlink_snapshot_entry(snap: Path) -> int:return removed-def _repo_file_matches(target_repo, predicate) -> list[tuple[Path, Optional[Path], str]]:- """Files whose snapshot-relative path satisfies *predicate*. Relative, not the bare ``file_name``: huggingface_hub sets that to ``file_path.name`` (and our own recovery scan to ``entry.name``), so a companion in ``dspark/`` or ``MTP/`` arrived here indistinguishable from a root file. Every predicate below keys on the directory for at least one supported layout, and the quant labels they extract are unchanged by the prefix."""+def _revision_file_matches(revision, predicate) -> list[tuple[Path, Optional[Path], str]]:matches: list[tuple[Path, Optional[Path], str]] = []- for rev in getattr(target_repo, "revisions", ()):- snapshot = getattr(rev, "snapshot_path", None)- for f in getattr(rev, "files", ()):- name = str(getattr(f, "file_name", ""))- file_path = getattr(f, "file_path", None)- if snapshot and file_path:- try:- name = Path(file_path).relative_to(Path(snapshot)).as_posix()- except ValueError:- pass- if not predicate(name):- continue- if not file_path:- continue- # Every predicate here keys on the name, which a sidecar answers exactly as its neighbour does. Proven metadata only: anything else carrying this key is a file to delete.- if is_appledouble_metadata(Path(file_path)):- continue- blob_path = getattr(f, "blob_path", None)- matches.append(- (- Path(file_path),- Path(blob_path) if blob_path else None,- name,- )+ snapshot = getattr(revision, "snapshot_path", None)+ for f in getattr(revision, "files", ()):+ name = str(getattr(f, "file_name", ""))+ file_path = getattr(f, "file_path", None)+ if snapshot and file_path:+ try:+ name = Path(file_path).relative_to(Path(snapshot)).as_posix()+ except ValueError:+ pass+ if not predicate(name):+ continue+ if not file_path:+ continue+ # skip only proven AppleDouble metadata; any other matching name is a deletion target.+ if is_appledouble_metadata(Path(file_path)):+ continue+ blob_path = getattr(f, "blob_path", None)+ matches.append(+ (+ Path(file_path),+ Path(blob_path) if blob_path else None,+ name,)+ )+ return matches+++def _repo_file_matches(target_repo, predicate) -> list[tuple[Path, Optional[Path], str]]:+ """match relative paths because bare names lose layouts such as ``dspark/`` and ``MTP/``."""+ matches: list[tuple[Path, Optional[Path], str]] = []+ for revision in getattr(target_repo, "revisions", ()):+ matches.extend(_revision_file_matches(revision, predicate))return matches@@ -171,8 +182,8 @@ def _has_remaining_main_gguf(target_repo) -> bool:def _remove_empty_variant_dirs(target_repos: list, variant: str) -> tuple[int, list[str]]:- """Remove now-empty ``snapshots/<rev>/<quant>/`` folders for *variant* (the quant label names the folder); only empty dirs go, so siblings are safe. Returns (count removed, removal failures other than a concurrent refill)."""- # A qualified key names its own folder and must not reach for a <quant>/ dir it does not own; qualified means a path (distilled/...-Q6_K), an H3 root stem, or a bpw modifier (IQ4_XS-3.53bpw, whose token-only IQ4_XS/ folder is a different build's).+ """remove empty variant folders and return their count plus non-concurrent failures."""+ # qualified keys own only their exact folder because token-only folders can be another build.qualified = (is_qualified_gguf_variant_key(variant)or (quant_token_with_bpw(variant) or "").lower() == variant.lower()@@ -214,13 +225,15 @@ def _remove_empty_variant_dirs(target_repos: list, variant: str) -> tuple[int, lsub.rmdir()removed += 1except OSError as e:- # A concurrent download refilling the dir (ENOTEMPTY) is not a failure; a read-only cache or locked dir is, so surface it.+ # ignore ENOTEMPTY from concurrent refills; surface locked or read-only directories.if e.errno != errno.ENOTEMPTY:failures.append(f"{sub.name}: {e}")return removed, failures-def _remove_empty_snapshot_dirs(target_repos: list) -> tuple[int, list[str]]:+def _remove_empty_snapshot_dirs(+ target_repos: list, protected_revisions: frozenset[str] = frozenset()+) -> tuple[int, list[str]]:removed = 0failures: list[str] = []for target_repo in target_repos:@@ -235,6 +248,8 @@ def _remove_empty_snapshot_dirs(target_repos: list) -> tuple[int, list[str]]:except OSError:continuefor snap in snap_dirs:+ if snap.name in protected_revisions:+ continuetry:snap.rmdir()removed += 1@@ -244,30 +259,90 @@ def _remove_empty_snapshot_dirs(target_repos: list) -> tuple[int, list[str]]:return removed, failures-def _variant_keys_to_delete(target_repo, variant: str) -> set[str]:- """The variant keys in *target_repo* that *variant* names, lowercased. Its own key, always, plus the unambiguous bare-quant alias the download side already admits (``gguf_plan.plan_for_variant``): a repo filing its sole Q4_K_M under a shared container (``weights/model-Q4_K_M.gguf``) qualifies that key, because the key is a pure function of the path and cannot know the directory disambiguates nothing, so every stored pin and every explicit ``repo:Q4_K_M`` names it by quant alone, and admitting the alias for the download and not for the delete answered "not found" and left the weights on disk. Only when it is unambiguous, exactly as the download side decides it: a repo that really does hold several checkpoints at one quant gets no fallback, because there the bare name genuinely does not name one of them and deleting the wrong one is unrecoverable."""+def _revision_selection_key(revision) -> tuple[float, str]:+ snapshot = getattr(revision, "snapshot_path", None)+ return snapshot_selection_key(Path(snapshot)) if snapshot else (0.0, "")+++def _rank_complete_snapshots(+ repo_id: str, revisions: list, complete: list[set[str]]+) -> list[set[str]]:+ from hub.utils.gguf_sources import _prefer_duplicate++ selected: dict[str, tuple[int, Path]] = {}+ for index, (revision, variants) in enumerate(zip(revisions, complete)):+ snapshot_path = getattr(revision, "snapshot_path", None)+ if not snapshot_path:+ continue+ snapshot = Path(snapshot_path)+ for variant in variants:+ key = variant.lower()+ previous = selected.get(key)+ if previous is None or _prefer_duplicate(repo_id, variant, previous[1], snapshot):+ selected[key] = (index, snapshot)+ return [+ {variant for variant in variants if selected[variant.lower()][0] == index}+ for index, variants in enumerate(complete)+ ]+++def _main_gguf_variant_keys(target_repo, preferred_path: Optional[str] = None) -> dict[Path, str]:+ """use the lister's cross-snapshot readiness ranking so unlisted builds cannot claim the row."""+ revisions = sorted(+ getattr(target_repo, "revisions", ()), key = _revision_selection_key, reverse = True+ )+ matches = [_revision_file_matches(revision, _is_main_gguf_filename) for revision in revisions]+ names = [[name for _snap, _blob, name in revision] for revision in matches]+ complete = []+ for revision, revision_names in zip(revisions, names):+ snapshot = getattr(revision, "snapshot_path", None)+ snapshot_complete = (+ hf_cache_scan.complete_snapshot_variants(str(snapshot)) if snapshot else set()+ )+ listed_rows = group_gguf_variant_files((name, 0) for name in revision_names)+ complete.append({key for key in listed_rows if key in snapshot_complete})+ repo_id = str(getattr(target_repo, "repo_id", ""))+ if repo_id:+ complete = _rank_complete_snapshots(repo_id, revisions, complete)+ listed = gguf_listed_checkpoints(names, complete)+ if preferred_path:+ wanted_path = Path(preferred_path)+ preferred_name = next(+ (name for revision in matches for snap, _blob, name in revision if snap == wanted_path),+ None,+ )+ if preferred_name is not None:+ listed.update(gguf_listed_checkpoints(((preferred_name,),)))+ keys: dict[Path, str] = {}+ for revision, revision_names in zip(matches, names):+ revision_keys = gguf_variant_keys_by_checkpoint(revision_names, listed)+ keys.update({snap: revision_keys[name] for snap, _blob, name in revision})+ return keys+++def _main_key(snap: Path, main_keys: dict[Path, str]) -> str:+ return (main_keys.get(snap) or "").lower()+++def _variant_keys_to_delete(+ target_repo,+ variant: str,+ preferred_path: Optional[str] = None,+) -> set[str]:+ """return lowercase keys, accepting a bare-quant alias only for one path-qualified key."""wanted = (variant or "").strip().lower()if not wanted or "/" in wanted:return {wanted}- keys = {- gguf_variant_key(name).lower()- for _snap, _blob, name in _repo_file_matches(target_repo, _is_main_gguf_filename)- }+ keys = {key.lower() for key in _main_gguf_variant_keys(target_repo, preferred_path).values()}if wanted in keys:return {wanted}- # PATH-qualified keys only, not is_qualified_gguf_variant_key: an H3 root stem's bare quant names both partitions, so it must not delete either.+ # use path-qualified keys, not is_qualified_gguf_variant_key; H3 bare quants span partitions.aliased = {key for key in keys if "/" in key and bare_quant_alias(key).lower() == wanted}return aliased if len(aliased) == 1 else {wanted}def _audio_cpp_package_scope(target_repo, variant: str) -> tuple[frozenset[str], frozenset[str]]:- """``(own files, protected files)`` of an audio.cpp package mix being deleted.-- A package mix (MiniMax Music 3, YuE2) is several GGUFs plus configs, some of them shared with- other mixes, so the quant key alone neither finds all of its files nor knows which a sibling- still needs. Own files are the mix's whole file list; protected files are every file of the- other mixes still fully on disk. Both empty for any other repo.- """+ """return audio.cpp mix files and files protected by complete sibling mixes, else empty sets."""try:from core.inference.audio_cpp_models import package_variant_filesexcept Exception: # noqa: BLE001 - no audio.cpp support, no package layout@@ -316,28 +391,35 @@ def _delete_gguf_variant_from_repos(*,sibling_active: bool = False,root: Optional[Path] = None,+ preferred_path: Optional[str] = None,) -> dict:failures: list[str] = []removed_snapshots = 0deleted_bytes = 0deleted_blobs = 0completed_hashes: set[str] = set()+ selected_families: set[str] = set()for target_repo in target_repos:repo_dir = Path(target_repo.repo_path) if getattr(target_repo, "repo_path", None) else None- wanted_keys = _variant_keys_to_delete(target_repo, variant)+ wanted_keys = _variant_keys_to_delete(target_repo, variant, preferred_path)+ main_keys = _main_gguf_variant_keys(target_repo, preferred_path)package_files, protected = _audio_cpp_package_scope(target_repo, variant)matched = [matchfor match in _repo_file_matches(target_repo,- lambda name, keys = wanted_keys: name in package_files- or (_is_main_gguf_filename(name) and gguf_variant_key(name).lower() in keys),+ lambda name: name in package_files or _is_main_gguf_filename(name),)- # A component another downloaded mix still loads (MiniMax's Q4_0 and Q8_0 share one- # depth decoder) stays.+ if match[2] in package_files or _main_key(match[0], main_keys) in wanted_keys+ # keep components used by another downloaded mix, including MiniMax's shared decoder.if match[2] not in protected]+ selected_families.update(+ gguf_variant_family(name).lower()+ for _snap, _blob, name in matched+ if _is_main_gguf_filename(name)+ )for snap, _blob, name in matched:try:@@ -349,7 +431,7 @@ def _delete_gguf_variant_from_repos(if matched and not sibling_active and not _has_remaining_main_gguf(target_repo):companion_matches = _repo_file_matches(target_repo,- # No main GGUF is left, so the companions cannot be launched; reclaim them with the last variant.+ # no main GGUF remains, so its companions cannot be launched.lambda name: _is_gguf_filename(name)and (_is_mmproj_filename(name)@@ -400,14 +482,40 @@ def _delete_gguf_variant_from_repos(),)- incomplete_result = gguf_variants.delete_variant_incomplete_blobs_result(- repo_id,- variant,- hf_token,- extra_hashes = frozenset(completed_hashes),- companions = not sibling_active,- root = root,+ cleanup_variant: Optional[str] = variant+ protected_revisions: set[str] = set()+ manifest = download_manifest.read_manifest("model", repo_id, variant, hub_cache = root)+ manifest_families = (+ {+ gguf_variant_family(expected.path).lower()+ for expected in manifest.expected_files+ if _is_main_gguf_filename(expected.path)+ }+ if manifest is not None+ else set())+ # bare-quant state is safe to purge only when its readable manifest identifies this checkpoint.+ if selected_families and not (+ manifest_families and manifest_families.issubset(selected_families)+ ):+ exact_family = next(iter(selected_families)) if len(selected_families) == 1 else None+ cleanup_variant = (+ exact_family if exact_family is not None and exact_family != variant.lower() else None+ )+ if manifest is not None and manifest.commit_hash:+ protected_revisions.add(manifest.commit_hash)++ if cleanup_variant is None:+ incomplete_result = gguf_variants.VariantIncompleteDeleteResult(deleted = 0, unresolved = False)+ else:+ incomplete_result = gguf_variants.delete_variant_incomplete_blobs_result(+ repo_id,+ cleanup_variant,+ hf_token,+ extra_hashes = frozenset(completed_hashes),+ companions = not sibling_active,+ root = root,+ )if incomplete_result.unresolved:raise HTTPException(status_code = 409,@@ -418,9 +526,19 @@ def _delete_gguf_variant_from_repos(),)- state_purged = download_manifest.purge_state("model", repo_id, variant, hub_cache = root)- removed_dirs, dir_failures = _remove_empty_variant_dirs(target_repos, variant)- removed_snap_dirs, snap_dir_failures = _remove_empty_snapshot_dirs(target_repos)+ state_purged = (+ download_manifest.purge_state("model", repo_id, cleanup_variant, hub_cache = root)+ if cleanup_variant is not None+ else False+ )+ removed_dirs, dir_failures = (+ _remove_empty_variant_dirs(target_repos, cleanup_variant)+ if cleanup_variant is not None+ else (0, [])+ )+ removed_snap_dirs, snap_dir_failures = _remove_empty_snapshot_dirs(+ target_repos, frozenset(protected_revisions)+ )removed_dirs += removed_snap_dirsdir_failures.extend(snap_dir_failures)if dir_failures:@@ -549,13 +667,15 @@ def reclaim_replaced_gguf_variant(for target_repo in target_repos:repo_dir = Path(target_repo.repo_path) if getattr(target_repo, "repo_path", None) else Nonestale_matches: list[tuple[Path, Optional[Path], str]] = []- matches = _repo_file_matches(- target_repo,- lambda name: _is_main_gguf_filename(name)- and gguf_variant_key(name).lower() == variant_key,- )+ # hashes from another build at this quant would look stale for this build.+ main_keys = _main_gguf_variant_keys(target_repo)+ matches = [+ match+ for match in _repo_file_matches(target_repo, _is_main_gguf_filename)+ if _main_key(match[0], main_keys) == variant_key+ ]for snap, blob, name in matches:- # Prune only a file we can identify as a real, stale cache blob: a no-symlink snapshot file has no identifiable blob hash, so keep it.+ # prune confirmed stale blobs only; non-symlink snapshot files lack identifiable hashes.blob_hash = (_blob_hash_from_path(blob)if cache_inventory._is_real_cache_blob(blob, repo_dir)@@ -1018,7 +1138,7 @@ async def delete_cached_model_response()raise HTTPException(status_code = 400, detail = detail)try:- # Re-derived now the scope is reserved, as only_if_orphan re-derives its own answer below: the first read ran before the reservation existed, so a load starting in between published its claim too late, and begin_delete misses it too.+ # recheck after reserving the scope so a concurrent load claim is visible.try:blocks_detail = await asyncio.to_thread(_load_state_blocks_delete)except Exception as e:@@ -1032,6 +1152,19 @@ async def delete_cached_model_response(status_code = 400,detail = blocks_detail,)+ preferred_path = None+ if variant is not None:+ try:+ preferred_path = await gguf_variants.cached_gguf_variant_source_path(+ repo_id, variant, hf_token+ )+ except Exception as exc: # noqa: BLE001 -- local readiness is the offline fallback+ logger.debug(+ "Could not resolve the scoped GGUF source for %s [%s]: %s",+ repo_id,+ variant,+ download_registry.scrub_secrets(str(exc), hf_token = hf_token),+ )result = await asyncio.to_thread(_delete_cached_model_blocking,repo_id,@@ -1039,14 +1172,13 @@ async def delete_cached_model_response(hf_token,cache_path,only_if_orphan = only_if_orphan,+ preferred_path = preferred_path,)- # The audio.cpp link farm hardlinks the deleted blobs; without this they keep their disk space.- # Any GGUF repo can be an audio.cpp model, and pruning an absent or current farm is a no-op.+ # prune stale audio.cpp hardlinks that retain deleted blobs; current farms are unchanged.from core.inference.audio_cpp_files import prune_link_farmfrom hub.utils.hf_cache_state import hf_cache_roots- # Every remembered root, not one recomputed from cache_path: an omitted path deletes from- # the sole owning cache, which need not be the active one. Pruning drops only stale entries.+ # prune every remembered root because the owning cache may not be active.def _prune_all() -> None:for root in hf_cache_roots():prune_link_farm(root)@@ -1111,12 +1243,12 @@ def _delete_cached_model_blocking(cache_path: Optional[str] = None,*,only_if_orphan: bool = False,+ preferred_path: Optional[str] = None,) -> dict:from hub.utils.gguf_sources import cached_gguf_action_path- cache_path = cached_gguf_action_path(repo_id, variant, cache_path)- # Free up space's list can be minutes old, and a background download finishing turns that orphan- # into an installed checkpoint neither guard below catches.+ cache_path = cached_gguf_action_path(repo_id, variant, cache_path, preferred_path)+ # recheck orphan status because a download can finish after the free-space listing.if only_if_orphan:from hub.services.models import companion_cleanupfrom hub.utils import companion_assets@@ -1125,7 +1257,7 @@ def _delete_cached_model_blocking(copies = companion_cleanup._repos_by_id(cache_inventory.all_hf_cache_scans()).get(repo_id.strip().lower(), [])- # Only the copy being removed: the orphan listing emits one row per cache root, so a full-pipeline copy in another remembered cache must not veto removing the companion-only copy listed.+ # check only this root; a full copy elsewhere must not block deleting this orphan row.if cache_path:wanted = Path(cache_path)copies = [@@ -1260,6 +1392,7 @@ def _delete_cached_model_blocking(hf_token,sibling_active = sibling_active,root = target_root,+ preferred_path = preferred_path,)deleted_revisions = Falsediff --git a/studio/backend/hub/services/models/gguf_variants.py b/studio/backend/hub/services/models/gguf_variants.pyindex 36dd30ba042..b9c8ba94208 100644--- a/studio/backend/hub/services/models/gguf_variants.py+++ b/studio/backend/hub/services/models/gguf_variants.py@@ -2534,7 +2534,7 @@ async def get_gguf_variants_response(hf_token: Optional[str] = None,include_cache_locations: bool = False,) -> GgufVariantsResponse:- """The listing alone, for callers that do not read metadata off the same copy."""+ """return the listing without same-copy metadata."""answer = await get_gguf_variants_answer(repo_id,prefer_local_cache = prefer_local_cache,@@ -2544,3 +2544,17 @@ async def get_gguf_variants_response(include_cache_locations = include_cache_locations,)return answer.response+++async def cached_gguf_variant_source_path(+ repo_id: str,+ variant: str,+ hf_token: Optional[str] = None,+) -> Optional[str]:+ """return the exact cached file selected after online readiness checks."""+ answer = await get_gguf_variants_answer(+ repo_id,+ hf_token = hf_token,+ include_cache_locations = True,+ )+ return (answer.variant_context_sources or {}).get((variant or "").strip().lower())diff --git a/studio/backend/hub/utils/gguf.py b/studio/backend/hub/utils/gguf.pyindex 4fe38ccefd1..7f1bdd57459 100644--- a/studio/backend/hub/utils/gguf.py+++ b/studio/backend/hub/utils/gguf.py@@ -496,8 +496,88 @@ def gguf_checkpoint_family(filename: str) -> Optional[str]:return family.strip("-_. /\t\r\n") or None+def _fold_name(text: str, *, drop_quant: bool) -> str:+ token = quant_token_with_bpw(text) if drop_quant else None+ if token is not None:+ text = re.sub(re.escape(token), "", text, count = 1, flags = re.IGNORECASE)+ return re.sub(r"[-_.\s]+", "-", text).strip("-").casefold()+++def _checkpoint_identity(filename: str) -> Optional[str]:+ """keep a quant directory's non-quant words unless they repeat the file's name."""+ parts = gguf_variant_family(filename.replace("\\", "/")).split("/")+ stem = _fold_name(parts.pop(), drop_quant = True)+ segments = []+ for segment in filter(None, parts):+ quant_dir = _is_quant_directory(segment)+ folded = _fold_name(segment, drop_quant = quant_dir)+ if quant_dir and folded in ("", stem):+ continue+ segments.append(folded)+ return "/".join((*segments, stem)).strip("/") or None+++def _paths_by_variant_key(paths: Iterable[str]) -> dict[str, list[str]]:+ by_key: dict[str, list[str]] = {}+ for path in paths:+ by_key.setdefault(gguf_variant_key(path).lower(), []).append(path)+ for members in by_key.values():+ members.sort(key = lambda path: path.replace("\\", "/"))+ return by_key+++def _loader_visible_members(members: Sequence[str]) -> list[str]:+ from utils.models.model_config import _extract_quant_label as _loader_quant+ return [path for path in members if not is_big_endian_gguf_path(path, _loader_quant(path))]+++def _listed_identity(members: Sequence[str]) -> Optional[str]:+ """prefer the first lister-visible file over hidden big-endian members."""+ selectable = _loader_visible_members(members)+ return _checkpoint_identity((selectable or members)[0])+++def gguf_listed_checkpoints(+ snapshots: Iterable[Iterable[str]], completed: Optional[Iterable[Iterable[str]]] = None+) -> dict[str, Optional[str]]:+ groups = [_paths_by_variant_key(paths) for paths in snapshots]+ whole = [set(keys) for keys in completed] if completed is not None else []+ if len(whole) < len(groups):+ whole.extend(set() for _ in range(len(groups) - len(whole)))++ listed: dict[str, Optional[str]] = {}+ complete_keys: set[str] = set()+ for index, snapshot in enumerate(groups):+ for key, members in snapshot.items():+ selectable = _loader_visible_members(members)+ if not selectable:+ continue+ complete = gguf_variant_key(selectable[0]) in whole[index]+ if key not in listed or (complete and key not in complete_keys):+ listed[key] = _checkpoint_identity(selectable[0])+ if complete:+ complete_keys.add(key)+ return listed+++def gguf_variant_keys_by_checkpoint(+ paths: Iterable[str], listed: Optional[dict[str, Optional[str]]] = None+) -> dict[str, str]:+ """separate checkpoints sharing a key; listed preserves the row's cross-snapshot build."""+ keys: dict[str, str] = {}+ for key, members in _paths_by_variant_key(paths).items():+ identity_listed = listed[key] if listed and key in listed else _listed_identity(members)+ for path in members:+ normalized = path.replace("\\", "/")+ if _checkpoint_identity(path) == identity_listed:+ keys[path] = gguf_variant_key(normalized)+ else:+ keys[path] = gguf_variant_family(normalized)+ return keys++def local_path_physical_identity(raw_path: str) -> str:- """Identity for an existing local path without folding valid filename characters."""+ """identify an existing local path without folding valid filename characters."""try:path = os.path.realpath(os.path.expanduser(raw_path))stat = os.stat(path)@@ -509,7 +589,7 @@ def local_path_physical_identity(raw_path: str) -> str:def should_group_local_gguf_files(filenames: Sequence[str]) -> bool:- """Whether filenames are variants of one selectable checkpoint."""+ """whether filenames are variants of one selectable checkpoint."""if len(filenames) == 1:return True@@ -910,7 +990,6 @@ def _apply_gguf_display_labels(variants: list[GgufVariantInfo], *repo_ids: objecif token is None:variant.display_label = f"GGUF · {variant.filename}" if ambiguous else "GGUF"elif variant.quant.lower() != (_plain_key(variant) or "").lower():- # A key qualified by path: show the quant, plus what distinguishes it.collides = scopes.get(_variant_scope_label(variant.filename).lower(), 0) > 1variant.display_label = (f"{token} · {_variant_scope_label(variant.filename, with_stem = collides)}"@@ -921,16 +1000,17 @@ def _apply_gguf_display_labels(variants: list[GgufVariantInfo], *repo_ids: objecdef group_gguf_variant_files(entries) -> dict[str, tuple[str, int]]:- """``variant key -> (first filename, summed size of ONE shard set)``: a quant shipped twice (``QwQ-32B-BF16-*`` beside ``QwQ-32B.BF16-*``, or whole beside split) keeps only the set holding the first file, the one the loader opens."""+ """map each variant key to the loader's first shard set and its total bytes."""families: dict[str, dict[tuple[str, int], list[tuple[str, int]]]] = {}for path, size in entries:- families.setdefault(gguf_variant_key(path), {}).setdefault(gguf_shard_set(path), []).append(- (path, int(size or 0))- )+ families.setdefault(gguf_variant_key(path).casefold(), {}).setdefault(+ gguf_shard_set(path), []+ ).append((path, int(size or 0)))grouped: dict[str, tuple[str, int]] = {}- for key, by_family in families.items():+ for by_family in families.values():chosen = min(by_family.values(), key = lambda members: min(path for path, _ in members))- grouped[key] = (min(path for path, _ in chosen), sum(size for _, size in chosen))+ first = min(path for path, _ in chosen)+ grouped[gguf_variant_key(first)] = (first, sum(size for _, size in chosen))return groupeddiff --git a/studio/backend/hub/utils/gguf_sources.py b/studio/backend/hub/utils/gguf_sources.pyindex e30e0350dba..37c236c7c09 100644--- a/studio/backend/hub/utils/gguf_sources.py+++ b/studio/backend/hub/utils/gguf_sources.py@@ -153,9 +153,7 @@ def cached_gguf_sources(repo_id: str, *, scoped_ready = None) -> dict[str, Cachesources[key] = CachedGgufSource(variant, snapshot, has_vision)partials.pop(key, None)continue- # An interrupted split quant is listed but not complete. Keep it as a fallback- # instead of dropping it, so the merge can still show the row, mark it partial- # and point a resume at the folder that holds it.+ # keep interrupted split quants as partial resume sourcespartials.setdefault(key, CachedGgufSource(variant, snapshot, has_vision, incomplete = True))@@ -168,9 +166,30 @@ def cached_gguf_action_path(repo_id: str,variant: str | None,cache_path: str | None = None,+ preferred_path: str | None = None,) -> str | None:- """An explicit folder wins; logical quant actions use the same source as the listing."""+ """use an explicit folder first; otherwise use the source selected by the listing."""if cache_path or not variant:return cache_path+ if preferred_path:+ expected = f"models--{repo_id.replace('/', '--')}".lower()+ try:+ selected = Path(preferred_path).expanduser()+ except (OSError, RuntimeError, ValueError):+ selected = None+ repo_dir = (+ next(+ (+ candidate+ for candidate in (selected, *selected.parents)+ if candidate.name.lower() == expected+ ),+ None,+ )+ if selected is not None+ else None+ )+ if repo_dir is not None:+ return str(repo_dir)source = cached_gguf_sources(repo_id).get(variant.lower())return source.cache_path if source else Nonediff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.pyindex 947659a43ca..3a2729a4434 100644--- a/studio/backend/routes/models.py+++ b/studio/backend/routes/models.py@@ -3381,30 +3381,30 @@ def _delete_gguf_variant_files(root: Path, variant: str) -> tuple[int, int]:deleted_bytes = 0from hub.utils.gguf import remove_appledouble_sidecar+ from hub.utils.gguf import gguf_variant_keys_by_checkpoint++ weights: list[tuple[Path, str]] = []for path in root.rglob("*"):if not path.is_file() or not _is_main_gguf_filename(path.name):continue- # Counted as a model if left in, so the reported count would follow the walk order; it still goes- # below, as metadata of the file it belongs to. Proven metadata only.+ # proven AppleDouble metadata is removed with its weight, not counted as a model.if is_appledouble_metadata(path):continue- # Keyed on the path, not the basename: a repo holding several checkpoints at- # one quant would otherwise delete every one of them for a single row.- from utils.models.model_config import _gguf_variant_key-try:relative = path.relative_to(root).as_posix()except ValueError:relative = path.name- if _gguf_variant_key(relative).lower() != variant.lower():+ weights.append((path, relative))+ # checkpoint-qualified keys isolate sibling builds that share a quant.+ keys = gguf_variant_keys_by_checkpoint(relative for _path, relative in weights)+ for path, relative in weights:+ if keys[relative].lower() != variant.lower():continuetry:deleted_bytes += path.stat().st_sizeexcept OSError:passpath.unlink()- # Skipped by the walk above, so this is its only chance to be reclaimed. Its bytes count- # toward what was freed even though it is not counted as a model.deleted_bytes += remove_appledouble_sidecar(path)deleted_count += 1return deleted_count, deleted_bytes@@ -3418,11 +3418,7 @@ async def delete_finetuned_model(gguf_variant: Optional[str] = Body(None),current_subject: str = Depends(get_current_subject),):- """Delete an Unsloth-trained or exported model from disk.-- Only paths under Unsloth's outputs/exports roots are accepted.- Exported GGUF entries can delete one quant variant at a time.- """+ """delete an Unsloth output, or one quant from an exported GGUF, within approved roots."""if source not in {"training", "exported"}:raise HTTPException(status_code = 400,diff --git a/studio/backend/tests/test_gguf_delete_keeps_sibling_builds.py b/studio/backend/tests/test_gguf_delete_keeps_sibling_builds.pynew file mode 100644index 00000000000..d09e0e376c7--- /dev/null+++ b/studio/backend/tests/test_gguf_delete_keeps_sibling_builds.py@@ -0,0 +1,639 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""GGUF listings expose one build per quant (#13327), but deletion must preserve cached siblings."""++from __future__ import annotations++from pathlib import Path+from types import SimpleNamespace++from hub.utils.gguf import gguf_variant_keys_by_checkpoint, list_local_gguf_variants++PREFIX = "Huihui-Qwen3.8-27B-abliterated-"+LISTED = f"{PREFIX}GSQ-RCO-IQ3_S-mtp.gguf"+SWIFT = f"{PREFIX}Swift-1.5-GSQ-RCO-IQ3_S-mtp.gguf"+UD_DW = f"{PREFIX}UD-DW-IQ3_S.gguf"+BUILDS = [LISTED, SWIFT, UD_DW]+++def _cache_repo(tmp_path: Path, names: list[str]):+ """model an HF cache whose snapshot symlinks match the deletion scan's input."""+ repo_dir = tmp_path / "models--huihui-ai--Model-GGUF"+ snap = repo_dir / "snapshots" / "rev1"+ blobs = repo_dir / "blobs"+ snap.mkdir(parents = True)+ blobs.mkdir(parents = True)+ files = []+ for index, name in enumerate(names):+ blob = blobs / f"sha{index}"+ blob.write_bytes(b"x" * 64)+ link = snap / name+ link.parent.mkdir(parents = True, exist_ok = True)+ link.symlink_to(blob)+ files.append(+ SimpleNamespace(+ file_name = name,+ file_path = str(link),+ blob_path = str(blob),+ size_on_disk = 64,+ )+ )+ return SimpleNamespace(+ repo_id = "huihui-ai/Model-GGUF",+ repo_type = "model",+ repo_path = repo_dir,+ revisions = [SimpleNamespace(files = files, snapshot_path = str(snap))],+ ), snap+++def test_only_a_different_build_leaves_the_key():+ keys = gguf_variant_keys_by_checkpoint(+ [*BUILDS, "QwQ-32B-BF16.gguf", "QwQ-32B.BF16.gguf", "model-Q6_K-00001-of-00002.gguf"]+ )+ assert keys[LISTED] == "IQ3_S"+ assert keys[SWIFT] == SWIFT[: -len(".gguf")]+ assert keys[UD_DW] == UD_DW[: -len(".gguf")]+ # alternate spellings and split shards belong to the same checkpoint.+ assert keys["QwQ-32B-BF16.gguf"] == keys["QwQ-32B.BF16.gguf"] == "BF16"+ assert keys["model-Q6_K-00001-of-00002.gguf"] == "Q6_K"+++def test_the_listing_still_offers_one_build_per_quant(tmp_path):+ snapshot = tmp_path / "snap"+ snapshot.mkdir()+ for name in BUILDS:+ (snapshot / name).write_bytes(b"x" * 64)+ (snapshot / "config.json").write_text("{}")+ variants, _ = list_local_gguf_variants(str(snapshot))+ assert [(v.quant, v.filename) for v in variants] == [("IQ3_S", LISTED)]+++def test_quant_key_case_exposes_and_deletes_the_same_build(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos+ from hub.utils.gguf import group_gguf_variant_files+ from utils.models.model_config import _group_gguf_variant_files++ alpha = "alpha-Q4_K_M.gguf"+ beta = "beta-q4_k_m.gguf"+ snapshot = tmp_path / "snapshot"+ snapshot.mkdir()+ for name in (alpha, beta):+ (snapshot / name).write_bytes(b"x" * 64)+ (snapshot / "config.json").write_text("{}")++ variants, _ = list_local_gguf_variants(str(snapshot))+ assert [(variant.quant, variant.filename) for variant in variants] == [("Q4_K_M", alpha)]+ hub_grouped = group_gguf_variant_files([(alpha, 64), (beta, 128)])+ inference_grouped = _group_gguf_variant_files([(alpha, "Q4_K_M", 64), (beta, "q4_k_m", 128)])+ assert inference_grouped == hub_grouped == {"Q4_K_M": (alpha, 64)}++ repo, cache_snapshot = _cache_repo(tmp_path / "cache", [alpha, beta])+ _delete_gguf_variant_from_repos(repo.repo_id, variants[0].quant, [repo], None, root = tmp_path)+ assert not (cache_snapshot / alpha).is_symlink()+ assert (cache_snapshot / beta).is_symlink()+++def test_deleting_the_row_keeps_the_other_builds(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos++ repo, snap = _cache_repo(tmp_path, BUILDS)+ _delete_gguf_variant_from_repos(repo.repo_id, "IQ3_S", [repo], None, root = tmp_path)+ assert not (snap / LISTED).is_symlink()+ assert (snap / SWIFT).is_symlink()+ assert (snap / UD_DW).is_symlink()+ assert (repo.repo_path / "blobs" / "sha1").exists()+++def test_copies_of_the_listed_build_still_go_with_it(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos++ repo, snap = _cache_repo(tmp_path, ["Model-Q4_K_M.gguf", "Model.Q4_K_M.gguf"])+ _delete_gguf_variant_from_repos(repo.repo_id, "Q4_K_M", [repo], None, root = tmp_path)+ assert not (snap / "Model-Q4_K_M.gguf").is_symlink()+ assert not (snap / "Model.Q4_K_M.gguf").is_symlink()+++def test_an_unnamed_checkpoint_is_not_a_wildcard_for_named_builds(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos++ names = ["Q4_K_M.gguf", "model-a-Q4_K_M.gguf", "model-b-Q4_K_M.gguf"]+ repo, snap = _cache_repo(tmp_path, names)+ _delete_gguf_variant_from_repos(repo.repo_id, "Q4_K_M", [repo], None, root = tmp_path)+ assert not (snap / names[0]).is_symlink()+ assert (snap / names[1]).is_symlink()+ assert (snap / names[2]).is_symlink()+++def test_hidden_big_endian_file_cannot_take_the_visible_rows_key(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos+ for index, hidden in enumerate(("model-Q4_K_M-be.gguf", "0-F16-be-checkpoint-Q4_K_M.gguf")):+ root = tmp_path / str(index)+ visible = "model-Q4_K_M.gguf"+ repo, snap = _cache_repo(root, [hidden, visible])+ _delete_gguf_variant_from_repos(repo.repo_id, "Q4_K_M", [repo], None, root = root)+ assert (snap / hidden).is_symlink()+ assert not (snap / visible).is_symlink()+++def _revisioned_repo(tmp_path: Path, names_by_revision):+ """treat the final HF cache snapshot as newest."""+ import os++ repo_dir = tmp_path / "models--org--Model-GGUF"+ blobs = repo_dir / "blobs"+ blobs.mkdir(parents = True)+ revisions = []+ for age, (revision_name, names) in enumerate(names_by_revision):+ snapshot = repo_dir / "snapshots" / revision_name+ snapshot.mkdir(parents = True)+ files = []+ for name in names:+ blob = blobs / f"{revision_name}-{name}"+ blob.write_bytes(b"x" * 64)+ link = snapshot / name+ link.symlink_to(blob)+ files.append(+ SimpleNamespace(+ file_name = name,+ file_path = str(link),+ blob_path = str(blob),+ size_on_disk = 64,+ )+ )+ os.utime(snapshot, (1_000_000 + age, 1_000_000 + age))+ revisions.append(SimpleNamespace(files = files, snapshot_path = str(snapshot)))+ return SimpleNamespace(+ repo_id = "org/Model-GGUF",+ repo_type = "model",+ repo_path = repo_dir,+ revisions = revisions,+ ), repo_dir / "snapshots"+++def test_the_newest_snapshot_names_the_rows_build(tmp_path):+ from hub.services.models.companion_cleanup import (+ _remaining_main_gguf_variants,+ _variant_bytes,+ )+ from hub.services.models.deletion import _delete_gguf_variant_from_repos++ repo, snapshots = _revisioned_repo(+ tmp_path,+ (+ ("old", ["alpha-Q4_K_M.gguf", "beta-Q4_K_M.gguf", "zeta-Q4_K_M.gguf"]),+ ("new", ["beta-Q4_K_M.gguf", "gamma-Q4_K_M.gguf"]),+ ),+ )+ assert _variant_bytes(repo, "Q4_K_M") == 128+ assert _remaining_main_gguf_variants(repo, excluding = "Q4_K_M") == {+ "alpha-q4_k_m",+ "zeta-q4_k_m",+ "gamma-q4_k_m",+ }+ _delete_gguf_variant_from_repos(repo.repo_id, "Q4_K_M", [repo], None, root = tmp_path)+ assert not (snapshots / "new/beta-Q4_K_M.gguf").is_symlink()+ assert not (snapshots / "old/beta-Q4_K_M.gguf").is_symlink()+ assert (snapshots / "old/alpha-Q4_K_M.gguf").is_symlink()+ assert (snapshots / "old/zeta-Q4_K_M.gguf").is_symlink()+ assert (snapshots / "new/gamma-Q4_K_M.gguf").is_symlink()+++def test_a_build_alone_in_an_older_snapshot_is_not_the_rows(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos++ repo, snapshots = _revisioned_repo(tmp_path, (("old", [SWIFT]), ("new", [LISTED])))+ _delete_gguf_variant_from_repos(repo.repo_id, "IQ3_S", [repo], None, root = tmp_path)+ assert not (snapshots / "new" / LISTED).is_symlink()+ assert (snapshots / "old" / SWIFT).is_symlink()+++def test_an_incomplete_newer_build_does_not_replace_the_complete_rows_build(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos+ from hub.utils.gguf import select_gguf_cache_snapshot_for_repo_dir++ old = "old-Q4_K_M.gguf"+ partial = "new-Q4_K_M-00001-of-00002.gguf"+ repo, snapshots = _revisioned_repo(tmp_path, (("old", [old]), ("new", [partial])))++ selected = select_gguf_cache_snapshot_for_repo_dir(repo.repo_path)+ assert selected is not None and selected[3] == snapshots / "old"+ _delete_gguf_variant_from_repos(repo.repo_id, "Q4_K_M", [repo], None, root = tmp_path)+ assert not (snapshots / "old" / old).is_symlink()+ assert (snapshots / "new" / partial).is_symlink()+++def test_a_hidden_only_newer_snapshot_does_not_replace_the_visible_rows_build(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos++ partial = "model-Q4_K_M-00001-of-00002.gguf"+ hidden = "model-Q4_K_M-be.gguf"+ repo, snapshots = _revisioned_repo(tmp_path, (("old", [partial]), ("new", [hidden])))++ _delete_gguf_variant_from_repos(repo.repo_id, "Q4_K_M", [repo], None, root = tmp_path)+ assert not (snapshots / "old" / partial).is_symlink()+ assert (snapshots / "new" / hidden).is_symlink()+++def test_each_rows_build_is_selected_independently_across_snapshots(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos++ alpha = "alpha-Q4_K_M-00001-of-00002.gguf"+ beta = "beta-Q4_K_M-00001-of-00002.gguf"+ repo, snapshots = _revisioned_repo(+ tmp_path,+ (("old", [alpha, "model-Q6_K.gguf"]), ("new", [beta])),+ )++ _delete_gguf_variant_from_repos(repo.repo_id, "Q4_K_M", [repo], None, root = tmp_path)+ assert not (snapshots / "new" / beta).is_symlink()+ assert (snapshots / "old" / alpha).is_symlink()+ assert (snapshots / "old/model-Q6_K.gguf").is_symlink()+++def test_readiness_ranking_selects_the_healthy_rows_build(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos+ from hub.utils import download_manifest+ from hub.utils.gguf_sources import cached_gguf_manifest_complete++ alpha = "alpha-Q4_K_M.gguf"+ beta = "beta-Q4_K_M.gguf"+ old_revision = "a" * 40+ new_revision = "b" * 40+ repo, snapshots = _revisioned_repo(tmp_path, ((old_revision, [alpha]), (new_revision, [beta])))+ assert download_manifest.write_manifest(+ "model",+ repo.repo_id,+ "Q4_K_M",+ [+ download_manifest.ExpectedFile(beta, 64),+ download_manifest.ExpectedFile("mmproj-F16.gguf", 128),+ ],+ hub_cache = tmp_path,+ commit_hash = new_revision,+ metadata_derived = True,+ )+ assert not cached_gguf_manifest_complete(repo.repo_id, "Q4_K_M", snapshots / new_revision)+ assert cached_gguf_manifest_complete(repo.repo_id, "Q4_K_M", snapshots / old_revision)++ _delete_gguf_variant_from_repos(repo.repo_id, "Q4_K_M", [repo], None, root = tmp_path)+ assert not (snapshots / old_revision / alpha).is_symlink()+ assert (snapshots / new_revision / beta).is_symlink()+++def test_case_folded_sibling_cannot_lend_completeness_to_the_listed_build(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos++ partial = "alpha-Q4_K_M-00001-of-00002.gguf"+ case_sibling = "beta-q4_k_m.gguf"+ selected = "gamma-Q4_K_M.gguf"+ repo, snapshots = _revisioned_repo(+ tmp_path,+ (("old", [selected]), ("new", [partial, case_sibling])),+ )++ _delete_gguf_variant_from_repos(repo.repo_id, "Q4_K_M", [repo], None, root = tmp_path)++ assert not (snapshots / "old" / selected).is_symlink()+ assert (snapshots / "new" / partial).is_symlink()+ assert (snapshots / "new" / case_sibling).is_symlink()+++def test_scoped_source_path_selects_the_rows_build(tmp_path):+ from hub.services.models.companion_cleanup import _variant_bytes+ from hub.services.models.deletion import _delete_gguf_variant_from_repos+ from hub.utils.gguf_sources import cached_gguf_action_path++ alpha = "alpha-Q4_K_M.gguf"+ beta = "beta-Q4_K_M.gguf"+ repo, snapshots = _revisioned_repo(tmp_path, (("old", [alpha]), ("new", [beta])))+ beta_entry = repo.revisions[1].files[0]+ Path(beta_entry.blob_path).write_bytes(b"x" * 128)+ beta_entry.size_on_disk = 128+ preferred_path = str(snapshots / "old" / alpha)++ assert cached_gguf_action_path(repo.repo_id, "Q4_K_M", preferred_path = preferred_path) == str(+ repo.repo_path+ )+ assert _variant_bytes(repo, "Q4_K_M") == 128+ assert _variant_bytes(repo, "Q4_K_M", preferred_path) == 64+ _delete_gguf_variant_from_repos(+ repo.repo_id,+ "Q4_K_M",+ [repo],+ None,+ root = tmp_path,+ preferred_path = preferred_path,+ )+ assert not (snapshots / "old" / alpha).is_symlink()+ assert (snapshots / "new" / beta).is_symlink()+++def test_scoped_source_path_keeps_repo_ancestor_for_shared_blob(tmp_path):+ from hub.utils.gguf_sources import cached_gguf_action_path++ repo_dir = tmp_path / "hub" / "models--org--Model-GGUF"+ source = repo_dir / "snapshots" / "rev" / "model-Q4_K_M.gguf"+ shared = tmp_path / "xet" / "blobs" / "shared"+ source.parent.mkdir(parents = True)+ shared.parent.mkdir(parents = True)+ shared.write_bytes(b"x")+ source.symlink_to(shared)++ assert cached_gguf_action_path("org/Model-GGUF", "Q4_K_M", preferred_path = str(source)) == str(+ repo_dir+ )+++def test_deleting_selected_build_preserves_sibling_partial_state(tmp_path, monkeypatch):+ from hub.services.models import gguf_variants+ from hub.services.models.deletion import _delete_gguf_variant_from_repos+ from hub.utils import download_manifest+ from utils import hf_cache_settings++ alpha = "alpha-Q4_K_M.gguf"+ beta = "beta-Q4_K_M.gguf"+ repo, snapshots = _revisioned_repo(tmp_path, (("old", [alpha]), ("new", [beta])))+ monkeypatch.setattr(+ hf_cache_settings,+ "get_hf_cache_paths",+ lambda: SimpleNamespace(hub_cache = tmp_path),+ )+ monkeypatch.setattr(gguf_variants, "_fetch_gguf_variant_requirements", lambda *a, **k: {})+ partial_hash = "f" * 64+ incomplete = repo.repo_path / "blobs" / f"{partial_hash}.incomplete"+ incomplete.write_bytes(b"partial")+ empty_beta_dir = snapshots / "new" / "Q4_K_M"+ empty_beta_dir.mkdir()+ assert download_manifest.write_manifest(+ "model",+ repo.repo_id,+ "Q4_K_M",+ [download_manifest.ExpectedFile(beta, 128, partial_hash)],+ hub_cache = tmp_path,+ commit_hash = "new",+ metadata_derived = True,+ )+ assert download_manifest.write_cancel_marker(+ "model", repo.repo_id, "Q4_K_M", "http", hub_cache = tmp_path+ )++ _delete_gguf_variant_from_repos(+ repo.repo_id,+ "Q4_K_M",+ [repo],+ None,+ root = tmp_path,+ preferred_path = str(snapshots / "old" / alpha),+ )++ assert not (snapshots / "old" / alpha).is_symlink()+ assert (snapshots / "new" / beta).is_symlink()+ assert incomplete.exists()+ assert empty_beta_dir.is_dir()+ assert (+ download_manifest.read_manifest("model", repo.repo_id, "Q4_K_M", hub_cache = tmp_path)+ is not None+ )+ assert download_manifest.has_cancel_marker("model", repo.repo_id, "Q4_K_M", hub_cache = tmp_path)+++def test_deleting_quant_only_build_preserves_named_sibling_partial(tmp_path, monkeypatch):+ from hub.services.models import gguf_variants+ from hub.services.models.deletion import _delete_gguf_variant_from_repos+ from hub.utils import download_manifest+ from utils import hf_cache_settings++ selected = "Q4_K_M.gguf"+ sibling = "alpha-Q4_K_M.gguf"+ repo, snapshots = _revisioned_repo(tmp_path, (("healthy", [selected]),))+ monkeypatch.setattr(+ hf_cache_settings,+ "get_hf_cache_paths",+ lambda: SimpleNamespace(hub_cache = tmp_path),+ )+ monkeypatch.setattr(gguf_variants, "_fetch_gguf_variant_requirements", lambda *a, **k: {})+ partial_hash = "f" * 64+ incomplete = repo.repo_path / "blobs" / f"{partial_hash}.incomplete"+ incomplete.write_bytes(b"partial")+ empty_sibling_dir = snapshots / "healthy" / "Q4_K_M"+ empty_sibling_dir.mkdir()+ assert download_manifest.write_manifest(+ "model",+ repo.repo_id,+ "Q4_K_M",+ [download_manifest.ExpectedFile(sibling, 128, partial_hash)],+ hub_cache = tmp_path,+ commit_hash = "healthy",+ metadata_derived = True,+ )+ assert download_manifest.write_cancel_marker(+ "model", repo.repo_id, "Q4_K_M", "http", hub_cache = tmp_path+ )++ _delete_gguf_variant_from_repos(repo.repo_id, "Q4_K_M", [repo], None, root = tmp_path)++ assert not (snapshots / "healthy" / selected).is_symlink()+ assert incomplete.exists()+ assert empty_sibling_dir.is_dir()+ assert (+ download_manifest.read_manifest("model", repo.repo_id, "Q4_K_M", hub_cache = tmp_path)+ is not None+ )+ assert download_manifest.has_cancel_marker("model", repo.repo_id, "Q4_K_M", hub_cache = tmp_path)+++def test_deleting_selected_build_preserves_unmanifested_sibling_partial(tmp_path, monkeypatch):+ from hub.services.models import gguf_variants+ from hub.services.models.deletion import _delete_gguf_variant_from_repos+ from hub.utils import download_manifest+ from utils import hf_cache_settings++ beta = "beta-Q4_K_M.gguf"+ repo, snapshots = _revisioned_repo(tmp_path, (("healthy", [beta]),))+ monkeypatch.setattr(+ hf_cache_settings,+ "get_hf_cache_paths",+ lambda: SimpleNamespace(hub_cache = tmp_path),+ )+ partial_hash = "f" * 64+ incomplete = repo.repo_path / "blobs" / f"{partial_hash}.incomplete"+ incomplete.write_bytes(b"partial")+ empty_sibling_dir = snapshots / "healthy" / "Q4_K_M"+ empty_sibling_dir.mkdir()+ assert download_manifest.write_cancel_marker(+ "model", repo.repo_id, "Q4_K_M", "http", hub_cache = tmp_path+ )+ monkeypatch.setattr(+ gguf_variants,+ "gguf_variant_blob_hashes",+ lambda _repo_id, candidate, *_args, **_kwargs: (+ frozenset({partial_hash}) if candidate == "Q4_K_M" else frozenset()+ ),+ )++ _delete_gguf_variant_from_repos(+ repo.repo_id,+ "Q4_K_M",+ [repo],+ None,+ root = tmp_path,+ )++ assert not (snapshots / "healthy" / beta).is_symlink()+ assert incomplete.exists()+ assert empty_sibling_dir.is_dir()+ assert download_manifest.has_cancel_marker("model", repo.repo_id, "Q4_K_M", hub_cache = tmp_path)+++def test_delete_preview_counts_the_whole_audio_package(tmp_path):+ from core.inference.audio_cpp_models import FAMILIES+ from hub.services.models.companion_cleanup import (+ _remaining_main_gguf_variants,+ _variant_bytes,+ )++ package = next(+ variant for variant in FAMILIES["minimax_music3"].package if variant.key == "Q8_0"+ )+ repo, _snap = _cache_repo(tmp_path, list(package.files))+ assert _variant_bytes(repo, "Q8_0") == 64 * len(package.files)+ assert _remaining_main_gguf_variants(repo, excluding = "Q8_0") == set()+++def test_the_delete_preview_counts_only_the_listed_build(tmp_path):+ from hub.services.models.companion_cleanup import (+ _remaining_main_gguf_variants,+ _variant_bytes,+ )++ repo, _snap = _cache_repo(tmp_path, BUILDS)+ assert _variant_bytes(repo, "IQ3_S") == 64+ # surviving builds retain their companions, so those bytes are not reclaimable.+ assert _remaining_main_gguf_variants(repo, excluding = "IQ3_S") == {+ SWIFT[: -len(".gguf")].lower(),+ UD_DW[: -len(".gguf")].lower(),+ }+++def test_the_delete_preview_excludes_a_blob_retained_by_a_sibling_build(tmp_path):+ from hub.services.models.companion_cleanup import _variant_bytes+ from hub.services.models.deletion import _delete_gguf_variant_from_repos++ repo, snap = _cache_repo(tmp_path, [LISTED, SWIFT])+ shared_blob = repo.repo_path / "blobs" / "sha0"+ (snap / SWIFT).unlink()+ (snap / SWIFT).symlink_to(shared_blob)+ repo.revisions[0].files[1].blob_path = str(shared_blob)++ assert _variant_bytes(repo, "IQ3_S") == 0+ _delete_gguf_variant_from_repos(repo.repo_id, "IQ3_S", [repo], None, root = tmp_path)+ assert shared_blob.exists()+++def test_deleting_from_a_local_folder_keeps_the_other_builds(tmp_path):+ from routes.models import _delete_gguf_variant_files++ for name in BUILDS:+ (tmp_path / name).write_bytes(b"x" * 64)+ assert _delete_gguf_variant_files(tmp_path, "IQ3_S")[0] == 1+ assert not (tmp_path / LISTED).exists()+ assert (tmp_path / SWIFT).exists()+ assert (tmp_path / UD_DW).exists()+++def test_reclaiming_an_update_leaves_the_other_builds(monkeypatch, tmp_path):+ from hub.services.models import cache_inventory as CI+ from hub.services.models import deletion as D++ repo_path = tmp_path / "models--huihui-ai--Model-GGUF"+ snap = repo_path / "snapshots" / ("b" * 40)+ entries = []+ for name, sha in ((LISTED, "NEWsha"), (SWIFT, "SWIFTsha")):+ link = snap / name+ blob = repo_path / "blobs" / sha+ for path in (link, blob):+ path.parent.mkdir(parents = True, exist_ok = True)+ path.write_bytes(sha.encode())+ entries.append(SimpleNamespace(file_name = name, file_path = str(link), blob_path = str(blob)))+ repo_info = SimpleNamespace(+ repo_id = "huihui-ai/Model-GGUF",+ repo_type = "model",+ repo_path = repo_path,+ revisions = [SimpleNamespace(files = entries)],+ )+ monkeypatch.setattr(CI, "all_hf_cache_scans", lambda: [SimpleNamespace(repos = [repo_info])])+ monkeypatch.setattr(CI, "invalidate_hf_cache_scans", lambda: None)++ D.reclaim_replaced_gguf_variant(+ repo_info.repo_id, "IQ3_S", frozenset({"NEWsha"}), hub_cache = tmp_path+ )+ assert (snap / SWIFT).exists()+ assert (repo_path / "blobs" / "SWIFTsha").exists()+++def test_reclaiming_an_update_keeps_a_build_alone_in_an_older_snapshot(monkeypatch, tmp_path):+ import os++ from hub.services.models import cache_inventory as CI+ from hub.services.models import deletion as D++ repo_path = tmp_path / "models--huihui-ai--Model-GGUF"+ revisions = []+ layout = (+ ("a" * 40, LISTED, "OLDsha"),+ ("c" * 40, SWIFT, "SWIFTsha"),+ ("b" * 40, LISTED, "NEWsha"),+ )+ for age, (rev, name, sha) in enumerate(layout):+ snap = repo_path / "snapshots" / rev+ link = snap / name+ blob = repo_path / "blobs" / sha+ for path in (link, blob):+ path.parent.mkdir(parents = True, exist_ok = True)+ path.write_bytes(sha.encode())+ os.utime(snap, (1_000_000 + age, 1_000_000 + age))+ revisions.append(+ SimpleNamespace(+ files = [SimpleNamespace(file_name = name, file_path = str(link), blob_path = str(blob))],+ snapshot_path = str(snap),+ )+ )+ repo_info = SimpleNamespace(+ repo_id = "huihui-ai/Model-GGUF",+ repo_type = "model",+ repo_path = repo_path,+ revisions = revisions,+ )+ monkeypatch.setattr(CI, "all_hf_cache_scans", lambda: [SimpleNamespace(repos = [repo_info])])+ monkeypatch.setattr(CI, "invalidate_hf_cache_scans", lambda: None)++ D.reclaim_replaced_gguf_variant(+ repo_info.repo_id, "IQ3_S", frozenset({"NEWsha"}), hub_cache = tmp_path+ )+ assert not (repo_path / "blobs" / "OLDsha").exists()+ assert (repo_path / "snapshots" / ("c" * 40) / SWIFT).exists()+ assert (repo_path / "blobs" / "SWIFTsha").exists()+ assert (repo_path / "blobs" / "NEWsha").exists()+++def test_builds_under_quant_named_directories_stay_apart(tmp_path):+ from hub.services.models.deletion import _delete_gguf_variant_from_repos++ keys = gguf_variant_keys_by_checkpoint(+ [+ "alpha-Q4_K_M/model.gguf",+ "beta-Q4_K_M/model.gguf",+ "Q6_K/model-Q6_K-00001-of-00002.gguf",+ "model-Q6_K/model-Q6_K-00001-of-00002.gguf",+ ]+ )+ assert keys["alpha-Q4_K_M/model.gguf"] == "Q4_K_M"+ assert keys["beta-Q4_K_M/model.gguf"] == "beta-Q4_K_M/model"+ # a directory repeating the file's own name is still a copy of the build.+ assert keys["model-Q6_K/model-Q6_K-00001-of-00002.gguf"] == "Q6_K"++ repo, snap = _cache_repo(tmp_path, ["alpha-Q4_K_M/model.gguf", "beta-Q4_K_M/model.gguf"])+ _delete_gguf_variant_from_repos(repo.repo_id, "Q4_K_M", [repo], None, root = tmp_path)+ assert not (snap / "alpha-Q4_K_M/model.gguf").is_symlink()+ assert (snap / "beta-Q4_K_M/model.gguf").is_symlink()diff --git a/studio/backend/utils/models/model_config.py b/studio/backend/utils/models/model_config.pyindex 6c5c7d5358d..acb8ca401b7 100644--- a/studio/backend/utils/models/model_config.py+++ b/studio/backend/utils/models/model_config.py@@ -3207,34 +3207,29 @@ def list_gguf_variants())- # Largest first; pinning and OOM demotion happen client-side where VRAM info exists.+ # largest first; pinning and OOM demotion happen client-side with VRAM data.variants.sort(key = lambda v: -v.size_bytes)return variants, has_visiondef _group_gguf_variant_files(entries: list[tuple[str, str, int]]) -> dict[str, tuple[str, int]]:- """MIRROR of ``hub.utils.gguf.group_gguf_variant_files`` over ``(name, quant, size)`` triples; ``routes/inference.py`` bills this size to the VRAM guard."""- families: dict[str, dict[tuple[str, int], list[tuple[str, int]]]] = {}+ """mirror ``hub.utils.gguf.group_gguf_variant_files`` for consistent VRAM accounting."""+ families: dict[str, dict[tuple[str, int], list[tuple[str, str, int]]]] = {}for name, quant, size in entries:- families.setdefault(quant, {}).setdefault(_gguf_shard_set(name), []).append(- (name, int(size or 0))+ families.setdefault(quant.casefold(), {}).setdefault(_gguf_shard_set(name), []).append(+ (name, quant, int(size or 0)))grouped: dict[str, tuple[str, int]] = {}- for quant, by_family in families.items():- chosen = min(by_family.values(), key = lambda members: min(n for n, _ in members))- grouped[quant] = (min(n for n, _ in chosen), sum(s for _, s in chosen))+ for by_family in families.values():+ chosen = min(by_family.values(), key = lambda members: min(n for n, _q, _s in members))+ first_name, first_quant, _ = min(chosen, key = lambda member: member[0])+ grouped[first_quant] = (first_name, sum(size for _name, _quant, size in chosen))return groupeddef _resolve_gguf_dir(p: Path) -> Optional[Path]:- """Resolve a path to the directory containing GGUF variants.-- Directory *p* returns directly. A ``.gguf`` file whose parent dir has- model metadata (``config.json`` or ``adapter_config.json``) returns the- parent -- all GGUFs there belong to the same model. Returns ``None`` for- loose standalone GGUFs (no config) to avoid cross-wiring unrelated models.- """+ """resolve a GGUF directory; reject loose files without metadata to avoid mixing models."""if p.is_dir():return pif p.is_file() and p.suffix.lower() == ".gguf":diff --git a/studio/frontend/src/features/hub/catalog/delete-impact.tsx b/studio/frontend/src/features/hub/catalog/delete-impact.tsxindex cc17f5eea6a..e871e16de2d 100644--- a/studio/frontend/src/features/hub/catalog/delete-impact.tsx+++ b/studio/frontend/src/features/hub/catalog/delete-impact.tsx@@ -4,36 +4,35 @@import { useEffect, useState } from "react";import { formatBytes } from "@/features/hub/lib/format";import { fetchDeleteImpact, type DeleteImpact } from "../inventory";+import { useHfTokenStore } from "../stores/hf-token-store";-/**- * Load the delete preview for a confirm dialog while it is open.- *- * An image GGUF is a small checkpoint plus a much larger companion base repo (text encoders,- * VAE, tokenizer) shared by every quant of its family, so "removes it from disk" was never the- * whole story: it could free 2.6 GB and silently leave 8.2 GB behind. Returns `null` until the- * preview lands and if it fails, so the dialog opens either way.- */+/** preview reclaimed and shared companion bytes; return `null` so confirmation stays available. */export function useDeleteImpact(open: boolean,repoId: string,variant?: string | null,- /** The copy the delete targets, so the preview measures that one and not another duplicate. */cachePath?: string | null,): DeleteImpact | null {const [impact, setImpact] = useState<DeleteImpact | null>(null);+ const hfToken = useHfTokenStore((state) => state.token);useEffect(() => {if (!open) {setImpact(null);return;}let cancelled = false;- void fetchDeleteImpact(repoId, variant ?? undefined, cachePath ?? undefined).then((result) => {+ void fetchDeleteImpact(+ repoId,+ variant ?? undefined,+ cachePath ?? undefined,+ hfToken || undefined,+ ).then((result) => {if (!cancelled) setImpact(result);});return () => {cancelled = true;};- }, [open, repoId, variant, cachePath]);+ }, [open, repoId, variant, cachePath, hfToken]);return impact;}@@ -42,12 +41,7 @@ function joinNames(names: string[]): string {return `${names.slice(0, 2).join(", ")} and ${names.length - 2} more`;}-/**- * The truthful half of a delete confirmation: what comes back, and what does not.- *- * Deliberately says the retained number out loud even when it dwarfs the reclaimed one, and- * says nothing at all rather than guess when the preview is unavailable.- */+/** report reclaimed and retained bytes, but render nothing when the preview is unavailable. */export function DeleteImpactSummary({ impact }: { impact: DeleteImpact | null }) {if (!impact) return null;if (impact.blocked_by.length > 0) {diff --git a/studio/frontend/src/features/hub/inventory/api.ts b/studio/frontend/src/features/hub/inventory/api.tsindex 54c6939bb7d..63078feab06 100644--- a/studio/frontend/src/features/hub/inventory/api.ts+++ b/studio/frontend/src/features/hub/inventory/api.ts@@ -362,17 +362,17 @@ export interface DeleteImpact {blocked_by: string[];}-/** What a delete would actually reclaim and leave behind. Never throws: the confirm dialog- * still has to open if this preview is unavailable, it just falls back to the plain wording. */+/** return the delete preview or `null` so confirmation still opens when unavailable. */export async function fetchDeleteImpact(repoId: string,variant?: string | null,cachePath?: string | null,+ hfToken?: string | null,): Promise<DeleteImpact | null> {try {const response = await authFetch("/api/hub/delete-impact", {method: "POST",- headers: { "Content-Type": "application/json" },+ headers: { "Content-Type": "application/json", ...hubTokenHeader(hfToken) },body: JSON.stringify(variant? { repo_id: repoId, variant, ...(cachePath ? { cache_path: cachePath } : {}) }diff --git a/studio/frontend/tests/gguf-delete-cache-reference.test.ts b/studio/frontend/tests/gguf-delete-cache-reference.test.tsindex 7a7d624d05f..f384de63456 100644--- a/studio/frontend/tests/gguf-delete-cache-reference.test.ts+++ b/studio/frontend/tests/gguf-delete-cache-reference.test.ts@@ -78,14 +78,21 @@ test("the delete preview accepts the copy so it measures that one", () => {);const body = fn.slice(0, fn.indexOf("\n}"));assert.match(body, /cachePath\?: string \| null,/, "the preview must accept a copy");+ assert.match(body, /hfToken\?: string \| null,/, "the preview must accept the request token");assert.ok(body.includes("cachePath ? { cache_path: cachePath } : {}"),"the preview must send the copy it was given",);+ assert.ok(+ body.includes("...hubTokenHeader(hfToken)"),+ "the preview must authenticate the same private or gated request as deletion",+ );assert.ok(impact.text.includes("cachePath?: string | null,") &&- impact.text.includes("fetchDeleteImpact(repoId, variant ?? undefined, cachePath ?? undefined)"),- "the hook must forward the copy and refetch when it changes",+ impact.text.includes("useHfTokenStore((state) => state.token)") &&+ impact.text.includes("hfToken || undefined,") &&+ impact.text.includes("[open, repoId, variant, cachePath, hfToken]"),+ "the hook must forward the copy and token and refetch when either changes",);const menu = readSource("../src/features/model-picker/components/model-selector/model-row-menu.tsx");assert.match(