fix: repair the resolved Hugging Face cache

This commit is contained in:
debpalash
2026-08-10 07:04:42 +00:00
parent 363f71feca
commit 7cc173d8be
4 changed files with 17 additions and 8 deletions
+5 -3
View File
@@ -238,9 +238,11 @@ def repair_repo_cache(repo_id: str, cache_dir: str | None = None) -> dict:
from huggingface_hub import snapshot_download
dl_kwargs: dict = {"repo_id": repo_id, "revision": revision}
if cache_dir:
dl_kwargs["cache_dir"] = cache_dir
dl_kwargs: dict = {
"repo_id": repo_id,
"revision": revision,
"cache_dir": cache_root,
}
endpoint = os.environ.get("HF_ENDPOINT")
if endpoint:
dl_kwargs["endpoint"] = endpoint
+7 -2
View File
@@ -1665,12 +1665,17 @@ def _repair_model_cache(checkpoint: str, *, force: bool = False) -> bool:
try:
from services.hf_cache_repair import hf_cache_home
from services.hf_revisions import installed_revision
revision = installed_revision(checkpoint, hf_cache_home())
cache_root = hf_cache_home()
revision = installed_revision(checkpoint, cache_root)
except (OSError, ValueError) as revision_err:
_last_repair_error = str(revision_err)
logger.warning("Refusing unpinned model repair for %s: %s", checkpoint, revision_err)
return False
dl_kwargs: dict = {"repo_id": checkpoint, "revision": revision}
dl_kwargs: dict = {
"repo_id": checkpoint,
"revision": revision,
"cache_dir": cache_root,
}
# Explicit endpoint (HF_ENDPOINT / pref) wins; otherwise the automatic
# endpoint selection's cached pick applies (services.endpoint_race).
try:
+2 -2
View File
@@ -148,11 +148,11 @@ def test_repair_removes_only_broken_and_redownloads(tmp_path, monkeypatch):
_symlink_or_skip(os.path.join("..", "..", "blobs", "MISSING"),
str(snap / "model.safetensors"))
calls = []
monkeypatch.setenv("HF_HUB_CACHE", str(cache))
monkeypatch.setattr(huggingface_hub, "snapshot_download",
lambda **k: calls.append(k))
summary = hf_cache_repair.repair_repo_cache("test/checkpoint",
cache_dir=str(cache))
summary = hf_cache_repair.repair_repo_cache("test/checkpoint")
assert summary["found"] == 1
assert summary["removed"] == 1
assert summary["restored"] is True
+3 -1
View File
@@ -191,7 +191,7 @@ def test_repair_skipped_in_offline_mode(model_manager, monkeypatch):
assert called == [] # no download attempted offline
def test_repair_invokes_snapshot_download(model_manager, monkeypatch):
def test_repair_invokes_snapshot_download(model_manager, monkeypatch, tmp_path):
"""Repair re-fetches the repo via snapshot_download (resume/fill missing)."""
calls = []
@@ -200,11 +200,13 @@ def test_repair_invokes_snapshot_download(model_manager, monkeypatch):
return "/cache/test/checkpoint"
import huggingface_hub
monkeypatch.setenv("HF_HUB_CACHE", str(tmp_path))
monkeypatch.setattr(huggingface_hub, "snapshot_download", fake_snapshot_download)
assert model_manager._repair_model_cache("test/checkpoint") is True
assert calls and calls[0]["repo_id"] == "test/checkpoint"
assert calls[0]["revision"] == "a" * 40
assert calls[0]["cache_dir"] == str(tmp_path)
def test_repair_returns_false_when_download_fails(model_manager, monkeypatch):