Skip to content

The agent that grows with you

fix(pm): finish a partial uv cache seed instead of marking it complete

Akloenx123941d527a8ce8
diff --git a/pm/packages.py b/pm/packages.py
index 315adf504a2..84c32814b13 100644
--- a/pm/packages.py
+++ b/pm/packages.py
@@ -302,6 +302,20 @@ def _uv_lock_digest(path: Path) -> bytes:
_uv_lock_digest_cache: dict[Path, tuple] = {}
+def _copy_seed_file(src: str | os.PathLike, dst: str | os.PathLike) -> str | os.PathLike:
+ """Copy one payload cache file unless an earlier seed already copied it whole.
+
+ A copy cut short leaves the file in place with too few bytes, so a matching
+ size is what counts as done; a finished file is not rewritten.
+ """
+ try:
+ if os.stat(dst).st_size == os.stat(src).st_size:
+ return dst
+ except FileNotFoundError:
+ pass
+ return shutil.copy2(src, dst)
+
+
def uv_cache_dir() -> Path:
"""The hermes-owned uv cache: machine-scoped and shared (keyed by
content — two profiles reuse one cache), anchored to the DEFAULT
@@ -334,12 +348,12 @@ def uv_cache_dir() -> Path:
if entry.name == ".seeded":
continue
dest = seeded / entry.name
- if not dest.exists():
- (
- shutil.copytree(entry, dest)
- if entry.is_dir()
- else shutil.copy2(entry, dest)
- )
+ # Merge into whatever an earlier failed or killed seed left: skipping
+ # an existing directory would record that partial copy as complete.
+ if entry.is_dir():
+ shutil.copytree(entry, dest, dirs_exist_ok=True, copy_function=_copy_seed_file)
+ else:
+ _copy_seed_file(entry, dest)
except OSError as exc:
LOG.warning("uv cache seed incomplete, retrying on the next install: %s", exc)
else:
diff --git a/tests/pm/test_uv_cache_seed.py b/tests/pm/test_uv_cache_seed.py
index 2878917e2c6..8fd7214ab6f 100644
--- a/tests/pm/test_uv_cache_seed.py
+++ b/tests/pm/test_uv_cache_seed.py
@@ -6,6 +6,7 @@ every offline sync that needed a missing entry failed closed with "requested dat
in the cache".
"""
+import os
import shutil
import pytest
@@ -47,3 +48,40 @@ def test_partial_seed_is_retried_on_the_next_install(payload_cache, monkeypatch)
packages.uv_cache_dir()
assert (payload_cache / ".seeded").is_file()
assert (payload_cache / "archive-v0" / "bucket" / "module.py").read_text(encoding="utf-8") == "seeded = True\n"
+
+
+def test_retry_finishes_a_seed_that_died_inside_a_directory(payload_cache, monkeypatch):
+ """A copy that already created ``archive-v0/`` left the retry nothing to do: it skipped the
+ existing top-level directory and recorded completion over the hole, so uv later failed with
+ "Missing .dist-info directory" for the bucket the copy never finished."""
+ dist_info = paths.store_root().parent / "uv-cache" / "archive-v0" / "bucket" / "pkg-1.0.dist-info"
+ dist_info.mkdir()
+ (dist_info / "METADATA").write_text("Name: pkg\n", encoding="utf-8")
+ real_copytree = shutil.copytree
+ interrupted: list[str] = []
+
+ def dies_at_dist_info(src, dst, *args, **kwargs):
+ if os.fspath(src).endswith(".dist-info") and not interrupted:
+ interrupted.append(os.fspath(src))
+ raise OSError("copy interrupted")
+ return real_copytree(src, dst, *args, **kwargs)
+
+ monkeypatch.setattr(shutil, "copytree", dies_at_dist_info)
+ packages.uv_cache_dir()
+ bucket = payload_cache / "archive-v0" / "bucket"
+ assert (bucket / "module.py").is_file() and not (bucket / "pkg-1.0.dist-info").exists()
+ assert not (payload_cache / ".seeded").is_file()
+
+ packages.uv_cache_dir()
+ assert (bucket / "pkg-1.0.dist-info" / "METADATA").read_text(encoding="utf-8") == "Name: pkg\n"
+ assert (payload_cache / ".seeded").is_file()
+
+
+def test_retry_recopies_a_file_the_interrupted_seed_left_short(payload_cache):
+ short = payload_cache / "archive-v0" / "bucket" / "module.py"
+ short.parent.mkdir(parents=True)
+ short.write_text("seed", encoding="utf-8")
+
+ packages.uv_cache_dir()
+ assert short.read_text(encoding="utf-8") == "seeded = True\n"
+ assert (payload_cache / ".seeded").is_file()