Skip to content

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

fix(studio): keep scoped download progress tied to current files (#12390)

Lee Jackson543acd5bd6b5
diff --git a/studio/backend/hub/services/models/downloads.py b/studio/backend/hub/services/models/downloads.py
index 08d24cc21e4..ec77bc73578 100644
--- a/studio/backend/hub/services/models/downloads.py
+++ b/studio/backend/hub/services/models/downloads.py
@@ -687,9 +687,22 @@ async def get_gguf_download_progress_response(
force_active = job.state in {"running", "cancelling"},
active_root = Path(hub_cache) if hub_cache else None,
)
+ if getattr(job_metadata, "scoped_files", ()) and (
+ manifest is None
+ or not snapshot_progress.manifest_matches_download(manifest, job_metadata)
+ ):
+ # Until this job's worker publishes a manifest, an older scope's must not supply bytes or completion.
+ return (
+ 0,
+ frozenset(getattr(job_metadata, "progress_blob_hashes", ()) or ()),
+ )
if manifest is not None:
+ total = sum(max(0, int(file.size or 0)) for file in manifest.expected_files)
+ if not progress_variant.startswith(_SCOPE_PREFIX):
+ # Offline fallback: an older local revision's manifest must not shrink the caller's estimate.
+ total = max(total, expected_total)
return (
- sum(max(0, int(file.size or 0)) for file in manifest.expected_files),
+ total,
frozenset(file.sha256 for file in manifest.expected_files if file.sha256),
)
if verdict == "refused":
diff --git a/studio/backend/hub/services/snapshot_progress.py b/studio/backend/hub/services/snapshot_progress.py
index 38e035cf23e..130e5753a5b 100644
--- a/studio/backend/hub/services/snapshot_progress.py
+++ b/studio/backend/hub/services/snapshot_progress.py
@@ -343,6 +343,22 @@ def _snapshot_resolves_to(
return True
+def manifest_matches_download(
+ manifest: Optional[download_manifest.Manifest],
+ metadata: Optional[download_registry.DownloadMetadata],
+) -> bool:
+ """A reused scope must not read a previous file set or revision's manifest."""
+ files = frozenset(getattr(metadata, "scoped_files", ()) or ())
+ if manifest is None or not files:
+ return True
+ if frozenset(file.path for file in manifest.expected_files) != files:
+ return False
+ hashes = frozenset(getattr(metadata, "progress_blob_hashes", ()) or ())
+ return not hashes or hashes == frozenset(
+ file.sha256 for file in manifest.expected_files if file.sha256
+ )
+
+
def compute_snapshot_progress(
*,
repo_type: RepoType,
@@ -373,10 +389,18 @@ def compute_snapshot_progress(
active_root = Path(metadata_hub_cache) if metadata_hub_cache else None
expected_total = max(expected_bytes, 0)
- # Always resolve the revision's blob hashes so stale blobs from a superseded revision cannot inflate the count; they degrade to empty (count-all) only when metadata is unavailable (e.g. offline). Take the larger total so a low caller hint cannot cap the bar.
+ # Resolve revision hashes so superseded blobs cannot inflate the count.
+ # Unavailable metadata falls back to the caller's estimate.
meta_total, expected_hashes = metadata_resolver(repo_id, hf_token)
meta_total = max(0, meta_total)
- expected_total = max(expected_total, meta_total)
+ # A resolved variant's total is exact: max() with a hint left over from an earlier scoped pick never shrinks.
+ expected_total = (
+ meta_total if variant is not None and meta_total > 0 else max(expected_total, meta_total)
+ )
+
+ scoped_files = frozenset(getattr(metadata, "scoped_files", ()) or ())
+ if variant is not None and scoped_files:
+ variant_file_matcher = lambda path, **_kwargs: path in scoped_files
# Without resolved hashes a variant must not count unscoped blobs, since sibling quants share one blobs/ dir; a no-variant snapshot owns the whole dir and counts unscoped.
count_unscoped = variant is None
@@ -467,14 +491,16 @@ def compute_snapshot_progress(
snapshot_dirs: "_Lazy[list[Path]]" = _Lazy(
lambda entry = entry: _retained_snapshot_dirs(entry)
)
- entry_manifest: "_Lazy[Optional[download_manifest.Manifest]]" = _Lazy(
+ raw_manifest: "_Lazy[Optional[download_manifest.Manifest]]" = _Lazy(
lambda entry = entry: download_manifest.read_manifest(
- repo_type,
- repo_id,
- variant,
- hub_cache = entry.parent,
+ repo_type, repo_id, variant, hub_cache = entry.parent
)
)
+ entry_manifest: "_Lazy[Optional[download_manifest.Manifest]]" = _Lazy(
+ lambda raw_manifest = raw_manifest: raw_manifest.get()
+ if manifest_matches_download(raw_manifest.get(), metadata)
+ else None
+ )
if variant is not None:
# The best reading across every retained snapshot, since the variant can live in an older revision, and because huggingface_hub 1.18's Windows copy layout can move a completed file straight into the snapshot and leave a blob-only tally at zero.
manifest = entry_manifest.get()
@@ -487,7 +513,12 @@ def compute_snapshot_progress(
frozenset(partial_bytes),
)
for snap in snapshot_dirs.get()
- if not expected_hashes or _snapshot_resolves_to(manifest, snap, expected_hashes)
+ # A rejected manifest means an old pick: its same-named snapshot files are not ours.
+ if manifest_matches_download(raw_manifest.get(), metadata)
+ and (
+ not expected_hashes
+ or _snapshot_resolves_to(manifest, snap, expected_hashes)
+ )
),
default = 0,
)
diff --git a/studio/backend/hub/tests/test_scoped_progress_identity.py b/studio/backend/hub/tests/test_scoped_progress_identity.py
new file mode 100644
index 00000000000..04e3d67457f
--- /dev/null
+++ b/studio/backend/hub/tests/test_scoped_progress_identity.py
@@ -0,0 +1,218 @@
+# 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 asyncio
+from types import SimpleNamespace
+
+import pytest
+
+from hub.services import snapshot_progress
+from hub.services.models import downloads
+from hub.utils import download_manifest
+
+
+def scoped_progress(monkeypatch, tmp_path):
+ entry = tmp_path / "models--Org--Model"
+ blobs = entry / "blobs"
+ blobs.mkdir(parents = True)
+ snapshot = entry / "snapshots" / "revision"
+ snapshot.mkdir(parents = True)
+ state = SimpleNamespace(manifest = None, job_state = "running")
+ metadata = SimpleNamespace(
+ scoped_files = ("new.gguf",),
+ progress_blob_hashes = frozenset({"new"}),
+ completed_baseline_bytes = 0,
+ hub_cache = str(tmp_path),
+ )
+ monkeypatch.setattr(downloads.account_access, "managed_account", lambda: False)
+ monkeypatch.setattr(
+ downloads,
+ "_registry",
+ SimpleNamespace(
+ get_job = lambda _key: SimpleNamespace(state = state.job_state),
+ get_job_metadata = lambda _key: metadata,
+ ),
+ )
+ for module in (downloads, snapshot_progress):
+ monkeypatch.setattr(module, "preferred_repo_cache_dirs", lambda *a, **k: [entry])
+ monkeypatch.setattr(download_manifest, "read_manifest", lambda *a, **k: state.manifest)
+ monkeypatch.setattr(download_manifest, "has_cancel_marker", lambda *a, **k: False)
+ monkeypatch.setattr(downloads.gguf_variants, "gguf_variant_requirements", lambda *a, **k: None)
+ monkeypatch.setattr(
+ downloads.gguf_variants, "gguf_variant_blob_hashes", lambda *a, **k: frozenset()
+ )
+
+ def manifest(
+ size,
+ path = "new.gguf",
+ sha = "new",
+ ):
+ state.manifest = download_manifest.Manifest(
+ repo_type = "model",
+ repo_id = "Org/Model",
+ variant = "@diffusion",
+ started_at = "",
+ expected_files = (download_manifest.ExpectedFile(path = path, size = size, sha256 = sha),),
+ )
+
+ def poll(hint, variant = "@diffusion"):
+ return asyncio.run(
+ downloads.get_gguf_download_progress_response(
+ "Org/Model",
+ variant = variant,
+ expected_bytes = hint,
+ )
+ )
+
+ return SimpleNamespace(
+ blobs = blobs, snapshot = snapshot, state = state, metadata = metadata, manifest = manifest, poll = poll
+ )
+
+
[email protected]("scope", ["@diffusion", "@hub-assets"])
[email protected]("old_size,new_size", [(670, 627), (450, 627), (627, 627)])
+def test_scope_file_change_rejects_old_manifest(scoped_progress, scope, old_size, new_size):
+ case = scoped_progress
+ case.manifest(old_size, "old.gguf", "old")
+ (case.blobs / "old").write_bytes(b"o" * old_size)
+ (case.snapshot / "old.gguf").write_bytes(b"o" * old_size)
+ (case.blobs / "new.incomplete").write_bytes(b"n" * 20)
+ first = case.poll(new_size, scope)
+ assert first["expected_bytes"] == new_size
+ assert first["downloaded_bytes"] == 20
+ assert first["complete_on_disk"] is False
+ case.manifest(new_size)
+ second = case.poll(first["expected_bytes"], scope)
+ assert second["expected_bytes"] == new_size
+ assert second["downloaded_bytes"] == 20
+
+
[email protected]("job_state", ["running", "cancelling", "idle"])
+def test_scoped_metadata_corrects_stale_hint(scoped_progress, job_state):
+ case = scoped_progress
+ case.state.job_state = job_state
+ case.manifest(627)
+ (case.blobs / "new.incomplete").write_bytes(b"n" * 20)
+ assert case.poll(670)["expected_bytes"] == 627
+
+
+def test_scoped_retry_keeps_current_file_progress(scoped_progress):
+ case = scoped_progress
+ case.manifest(627)
+ (case.blobs / "new.incomplete").write_bytes(b"n" * 20)
+ result = case.poll(627)
+ assert result["expected_bytes"] == 627
+ assert result["downloaded_bytes"] == 20
+
+
+def test_same_filename_new_revision_rejects_old_hash(scoped_progress):
+ case = scoped_progress
+ case.manifest(670, sha = "old")
+ (case.snapshot / "new.gguf").write_bytes(b"o" * 670)
+ (case.blobs / "old").write_bytes(b"o" * 670)
+ (case.blobs / "new.incomplete").write_bytes(b"n" * 20)
+ result = case.poll(627)
+ assert result["expected_bytes"] == 627
+ assert result["downloaded_bytes"] == 20
+ assert result["complete_on_disk"] is False
+
+
+def test_missing_manifest_uses_current_hashes_and_hint(scoped_progress):
+ case = scoped_progress
+ (case.blobs / "new.incomplete").write_bytes(b"n" * 20)
+ (case.blobs / "old").write_bytes(b"o" * 670)
+ result = case.poll(627)
+ assert result["expected_bytes"] == 627
+ assert result["downloaded_bytes"] == 20
+ assert result["complete_on_disk"] is False
+
+
+def test_unknown_hashes_do_not_count_old_scope_snapshot(scoped_progress):
+ case = scoped_progress
+ case.metadata.progress_blob_hashes = frozenset()
+ case.manifest(670, "old.gguf", "old")
+ (case.snapshot / "old.gguf").write_bytes(b"o" * 670)
+ result = case.poll(627)
+ assert result["expected_bytes"] == 627
+ assert result["downloaded_bytes"] == 0
+ assert result["complete_on_disk"] is False
+
+
+def test_current_scope_can_complete_after_total_shrinks(scoped_progress):
+ case = scoped_progress
+ case.manifest(627)
+ (case.blobs / "new").write_bytes(b"n" * 627)
+ (case.snapshot / "new.gguf").write_bytes(b"n" * 627)
+ result = case.poll(670)
+ assert result["expected_bytes"] == 627
+ assert result["downloaded_bytes"] == 627
+ assert result["complete_on_disk"] is True
+ assert result["progress"] == 1
+
+
+def test_restored_scope_without_registry_metadata_uses_manifest(scoped_progress):
+ case = scoped_progress
+ case.metadata.scoped_files = ()
+ case.metadata.progress_blob_hashes = frozenset()
+ case.manifest(627)
+ (case.blobs / "new.incomplete").write_bytes(b"n" * 20)
+ result = case.poll(670)
+ assert result["expected_bytes"] == 627
+ assert result["downloaded_bytes"] == 20
+
+
+def test_regular_gguf_total_includes_companions(monkeypatch, scoped_progress):
+ case = scoped_progress
+ case.metadata.scoped_files = ()
+ monkeypatch.setattr(
+ downloads.gguf_variants,
+ "gguf_variant_requirements",
+ lambda *a, **k: SimpleNamespace(
+ download_size_bytes = 657,
+ required_hashes = frozenset({"new", "companion"}),
+ expected_files = (),
+ ),
+ )
+ (case.blobs / "new.incomplete").write_bytes(b"n" * 20)
+ (case.blobs / "companion").write_bytes(b"c" * 30)
+ result = case.poll(700, "Q6_K")
+ assert result["expected_bytes"] == 657
+ assert result["downloaded_bytes"] == 50
+
+
+def test_unresolved_variant_keeps_size_hint(scoped_progress):
+ case = scoped_progress
+ case.metadata.scoped_files = ()
+ case.metadata.progress_blob_hashes = frozenset()
+ result = case.poll(627, "Q6_K")
+ assert result["expected_bytes"] == 627
+ assert result["complete_on_disk"] is False
+
+
+def test_full_snapshot_retains_conservative_total(scoped_progress):
+ case = scoped_progress
+ case.metadata.scoped_files = ()
+ result = snapshot_progress.compute_snapshot_progress(
+ repo_type = "model",
+ repo_id = "Org/Model",
+ job_key = "Org/Model",
+ expected_bytes = 670,
+ hf_token = None,
+ registry = downloads._registry,
+ metadata_resolver = lambda *a: (627, frozenset({"new"})),
+ )
+ assert result["expected_bytes"] == 670
+ assert result["complete_on_disk"] is False
+
+
+def test_offline_variant_manifest_does_not_shrink_hint(scoped_progress):
+ case = scoped_progress
+ case.metadata.scoped_files = ()
+ case.metadata.progress_blob_hashes = frozenset()
+ case.manifest(627, "model-Q6_K.gguf", "old")
+ (case.blobs / "old").write_bytes(b"o" * 627)
+ (case.snapshot / "model-Q6_K.gguf").write_bytes(b"o" * 627)
+ result = case.poll(670, "Q6_K")
+ assert result["expected_bytes"] == 670
+ assert result["complete_on_disk"] is False