Files
hermes-agent/pm/uv_cache_prune.py
BearHuddleston de8947f0da fix(pm): drop uv cache links the prune left without a target
Staging now ships uv's wheel links as links, but the prune removes wheel
entries only under wheels-v*/pypi and sdists-v*/pypi. URL, git and
named-index wheels sit a hash directory deeper, so when the lock drops
one of them the prune deletes its archive-v0 bucket and leaves the link
pointing at nothing. publish_prepared's link check then rejects the
payload. Before links were kept, staging copied such an entry as a
self-contained directory, so the same prune left nothing dangling.

uv.lock has three such sources today (kittentts by URL, misaki and
psutil from git). With the pinned uv 0.13.5, a wheel installed from a
direct URL and then dropped from the lock reproduces it: stage, prune,
then the link check fails on wheels-v7/url/<hash>/gone-dep/<version>.

Finish the prune by removing links with no target in the wheel and
sdist families. The sweep does not depend on the layout. It also
repairs CI's rolling cache, which the setup-pm post-job runs through the
same prune.

Reported in review by @unsupportedpastels, whose regression test this
extends with a link the lock still resolves.
2026-11-03 01:15:57 +00:00

86 lines
3.3 KiB
Python

"""Exact-to-lock pruning for uv caches.
The lock is the contract for anything downstream consumes wholesale: a
shipped bundle payload and a rolling CI cache snapshot both must not carry
wheels the project's uv.lock cannot resolve. Rolling caches accumulate
entries for superseded pins (prefix restores across dependency changes);
pruning to the lock keeps every consumer exact without discarding wheels
the current lock still needs (unlike ``uv cache prune --ci``).
"""
from __future__ import annotations
import os
import re
import shutil
from pathlib import Path
def _normalized(name: str) -> str:
"""PEP 503 name: a ``ruamel.yaml-*.dist-info`` bucket is the lock's ``ruamel-yaml``."""
return re.sub(r"[-_.]+", "-", name).lower()
def lock_package_names(source_repo: Path) -> set[str]:
"""Dist names the lock can resolve; the exactness contract for a cache."""
import tomllib
data = tomllib.loads((source_repo / "uv.lock").read_text(encoding="utf-8"))
return {_normalized(entry["name"]) for entry in data["package"]}
def _drop_dangling_links(cache: Path) -> int:
"""Delete wheel and sdist links whose ``archive-v0/`` bucket is gone.
URL, git and named-index entries sit a hash directory deeper than ``pypi``
and are not pruned by name, but their links point into buckets the prune
removed. A link with no target is never a cache hit, and a payload that
ships one fails ``publish_prepared``'s link check. ``os.walk`` lists a
dangling link among the files, so the walk never follows one.
"""
dropped = 0
for family_dir in (*cache.glob("wheels-v*"), *cache.glob("sdists-v*")):
for directory, _dirs, files in os.walk(family_dir):
for name in files:
entry = Path(directory, name)
if entry.is_symlink() and not entry.exists():
entry.unlink()
dropped += 1
return dropped
def prune_uv_cache_to_lock(cache: Path, source_repo: Path) -> int:
"""Delete cache entries for dists the lock cannot resolve.
Unidentifiable buckets (no dist-info) survive — pruning fails open for
unknown layouts, never for identifiable stale pins. Links left without a
target go too. Returns the pruned entry count for the build log.
"""
keep = lock_package_names(source_repo)
dist_info = re.compile(r"([A-Za-z0-9_.]+?)-\d[^-]*\.dist-info")
def dist_name(bucket: Path) -> str | None:
for marker_file in bucket.glob("*.dist-info"):
match = dist_info.match(marker_file.name)
if match:
return _normalized(match.group(1))
return None
pruned = 0
archive = cache / "archive-v0"
if archive.is_dir():
for bucket in archive.iterdir():
if not bucket.is_dir():
continue
name = dist_name(bucket)
if name is not None and name not in keep:
shutil.rmtree(bucket, ignore_errors=True)
pruned += 1
for family_dir in (*cache.glob("wheels-v*/pypi"), *cache.glob("sdists-v*/pypi")):
if not family_dir.is_dir():
continue
for entry in family_dir.iterdir():
if entry.is_dir() and _normalized(entry.name) not in keep:
shutil.rmtree(entry, ignore_errors=True)
pruned += 1
return pruned + _drop_dangling_links(cache)