diff --git a/docker/Dockerfile b/docker/Dockerfile index dd007440b81..bfc2ec511d7 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -159,7 +159,7 @@ RUN curl --proto '=https' --tlsv1.2 --retry 3 --retry-delay 2 -sSf https://sh.ru # Install uv and python outside /root so an unprivileged runtime UID (for example # 65532) can execute them: /root is not traversable by other users, and the project # venv's python is a symlink into uv's managed-Python directory. -ARG UV_VERSION=0.11.28 +ARG UV_VERSION=0.13.6 ARG PYTHON_VERSION=3.13.14 ENV UV_PYTHON_INSTALL_DIR=/opt/uv/python # One uv cache for the build and for every runtime identity. It holds every wheel the venvs are @@ -271,6 +271,8 @@ COPY --from=nemo-rl nemo_rl/__init__.py nemo_rl/package_info.py ./nemo_rl/ COPY --from=nemo-rl nemo_rl/distributed/actor_environments.py ./nemo_rl/distributed/ COPY --from=nemo-rl tools/build-custom-vllm.sh ./tools/build-custom-vllm.sh COPY --from=nemo-rl tools/build-custom-flashinfer.sh ./tools/build-custom-flashinfer.sh +# Part of this recipe, not of the selected source: an older NRL_GIT_REF has no such script. +COPY docker/prune_uv_cache.py /usr/local/bin/prune_uv_cache.py COPY --from=nemo-rl --link research/ ./research/ COPY --from=nemo-rl --link \ --exclude=TensorRT-LLM-workspace \ @@ -290,7 +292,7 @@ RUN --mount=type=ssh \ # downloads persist for the next build. This is the first uv-writing RUN in # the stage, so it force-copies the whole store in and prunes on sync-back # (see docker/uv_cache_mount.sh). Every later RUN in this stage that also -# invokes uv (TRT-LLM build, Dynamo install, worker-venv prefetch phase 2, +# invokes uv (TRT-LLM build, worker-venv prefetch phase 2, # Gym prefetch) sources the same helper but only tops up what it adds, # instead of re-shipping the whole store again in its own layer. # Whether the materialized copy also ships in the final image (for the @@ -301,6 +303,17 @@ uv_cache_restore force trap 'uv_cache_persist prune' EXIT uv venv --seed +# Drop what the restored cache carried in that uv.lock no longer pins -- versions from +# older locks, which neither the seed export's `uv cache prune --ci` (it keeps every +# source-built version) nor the durable cache mount ever evict (see +# docker/prune_uv_cache.py). It runs before any sync, so it only ever judges restored +# content: build-isolation deps the syncs below resolve outside uv.lock (setuptools, a +# build-time torch, ...) are kept. It runs in this RUN because deleting files in a later +# layer only hides them, and the image would still ship every byte; and the +# `uv_cache_persist prune` trap above mirrors the result back into the cache mount. +du -sh "${UV_CACHE_DIR}" +"${UV_PROJECT_ENVIRONMENT}/bin/python" /usr/local/bin/prune_uv_cache.py --lock uv.lock --cache-dir "${UV_CACHE_DIR}" +du -sh "${UV_CACHE_DIR}" # The custom build scripts will alter the pyproject.toml and uv.lock if [[ -n "${BUILD_CUSTOM_VLLM:-}" ]]; then UV_LINK_MODE=hardlink bash tools/build-custom-vllm.sh ${BUILD_CUSTOM_VLLM_URL:-} ${BUILD_CUSTOM_VLLM_REF:-} ${BUILD_CUSTOM_VLLM_PRECOMPILED_WHEEL_LOCATION:-} @@ -321,7 +334,6 @@ apt-get update # DeepEP's multinode link needs the exact filename libnvidia-ml.so.1; the CUDA base # only ships stubs/libnvidia-ml.so. Purged again after the syncs below. apt-get install -y --no-install-recommends libnvidia-ml-dev -uv cache clean deep-ep # Everything is hardlinked against the uv cache; this layer holds exactly one real copy of every wheel. UV_LINK_MODE=hardlink uv sync --frozen --no-install-project if [[ -z "${SKIP_VLLM_BUILD:-}" ]]; then @@ -680,21 +692,17 @@ COPY --from=nemo-rl --exclude=pyproject.toml --exclude=uv.lock . /opt/nemo-rl # Keep Dynamo's Python 3.12 dependency graph isolated from NeMo-RL's normal # Ray/vLLM environments. Default image builds do not create this environment. -RUN --mount=from=uv-cache-helper,src=/,dst=/tmp/uv-cache-helper \ - --mount=type=cache,id=uv-cache-${TARGETARCH}-${BASE_IMAGE}-${UV_VERSION}-${HYBRID_EP_MULTINODE},from=uv-cache-seed,source=.,target=/root/.uv-cache-store,sharing=locked <<"EOF" bash -exu -o pipefail +RUN <<"EOF" bash -exu -o pipefail if [[ "${BUILD_DYNAMO:-0}" != "1" ]]; then echo "BUILD_DYNAMO is not 1; skipping the managed Dynamo runtime" exit 0 fi -# Same materialize/sync-back/strip dance as the other uv-writing RUNs -- -# docker/dynamo/install.sh calls uv sync with no explicit UV_CACHE_DIR override, so -# it uses the image-wide UV_CACHE_DIR (set in base), which must be a plain -# same-filesystem directory for hardlink installs. Already exists from the -# hermetic stage unless STRIP_UV_CACHE removed it there. -source /tmp/uv-cache-helper/uv_cache_mount.sh -uv_cache_restore -trap 'uv_cache_persist' EXIT -bash docker/dynamo/install.sh +# Dynamo syncs its own lock (Python 3.12) into a --link-mode copy venv that nothing re-syncs at +# runtime, so its wheels are never needed from the shared cache. A throwaway cache keeps them +# out of this layer instead of shipping them twice, and out of the durable cache mount, so this +# RUN needs neither the mount nor the uv_cache_mount.sh restore/persist. +UV_CACHE_DIR=/tmp/dynamo-uv-cache bash docker/dynamo/install.sh +rm -rf /tmp/dynamo-uv-cache EOF # Unshallow the repo to get the full history (in the case it was from the scratch layer). # Potentially not necessary if the repo is passed in as a complete repository (w/ full git history), @@ -862,7 +870,7 @@ echo "Exporting uv cache for build ${UV_CACHE_EXPORT_NONCE:-local}" # RUN command, leaving this builder-local mount empty. Restore the seed only in # that case; unconditionally merging it would resurrect wheels uv evicted. if [[ -z "$(find /root/.uv-cache-store -mindepth 1 -print -quit)" ]]; then - rsync -a /tmp/uv-cache-seed/ /root/.uv-cache-store/ + rsync -aH /tmp/uv-cache-seed/ /root/.uv-cache-store/ fi mkdir -p /tmp/uv-cache-export cp -a /root/.uv-cache-store/. /tmp/uv-cache-export/ diff --git a/docker/prune_uv_cache.py b/docker/prune_uv_cache.py new file mode 100644 index 00000000000..c448937d7c0 --- /dev/null +++ b/docker/prune_uv_cache.py @@ -0,0 +1,287 @@ +#!/usr/bin/env python3 +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Prune uv's cache down to what uv.lock still pins. + +uv.lock pins one version (or one git commit) per package across the WHOLE +project -- every extra, installed in this build or not. uv's cache is +append-only: `uv sync` downloads what a build needs and never removes a version +an older lockfile pinned. The CI seed persists the cache across builds for as +long as BASE_IMAGE/UV_VERSION/HYBRID_EP_MULTINODE are unchanged (see the +dependency-install RUN in docker/Dockerfile), so every dependency bump would +otherwise leave its predecessor in the seed and in every shipped image. +`uv cache prune` does not help: it removes stale cache-format buckets, cached +environments and unlinked archives, never a valid entry for an older version. + +Removed, matched against uv.lock (layout per uv's crates/uv-cache/src/wheel.rs): + wheels-v*/{pypi,index/}//-[.http|.msgpack] + sdists-v*/{pypi,index/}//[...] + kept iff (, ) is pinned by uv.lock. + sdists-v*/git// + git-v*/checkouts//[.ok] + kept iff the commit prefixes a git commit uv.lock pins. + archive-v0/ + kept iff any link left under wheels-v*/ or sdists-v*/ resolves to it -- + the same reachability scan uv's own `uv cache prune` uses, so archives + behind entries this script never touches (url/, path/, editable/) stay. + files-v*// + kept iff something still hardlinks it (link count > 1), as uv's own + `uv cache prune` decides. + +Never removed: url/, path/, editable/ trees (keyed by URL digest, no version to +match), git-v*/db, and every other bucket. That keeps it safe to run before +shipping UV_CACHE_DIR: it cannot remove anything offline +`uv run --extra ` needs, because that is itself "what uv.lock pins". +""" + +from __future__ import annotations + +import argparse +import os +import re +import shutil +import sys +import tomllib +from pathlib import Path + +# PEP 503: uv.lock and the cache's directory names may differ in case/separator. +_NORMALIZE_RE = re.compile(r"[-_.]+") +_GIT_SHA_RE = re.compile(r"#([0-9a-f]{40})$") +_METADATA_SUFFIXES = (".http", ".msgpack", ".lock", ".rev") +# uv's archive-reference scan skips these; they are never links into archive-v0. +_SCAN_SKIP_SUFFIXES = (".lock", ".whl", ".http", ".rev", ".msgpack") + + +def normalize(name: str) -> str: + return _NORMALIZE_RE.sub("-", name).lower() + + +def load_lock(lock_path: Path) -> tuple[set[tuple[str, str]], set[str]]: + """Returns ({(normalized name, version)}, {full git commit SHA}).""" + with lock_path.open("rb") as f: + packages = tomllib.load(f).get("package", []) + versions = set() + for p in packages: + if "name" in p and "version" in p: + name, version = normalize(p["name"]), p["version"] + versions.add((name, version)) + # uv's WheelFilename::cache_key truncates long versions to 47 chars. + versions.add((name, version[:47].rstrip(".+"))) + shas = set() + for p in packages: + git = p.get("source", {}).get("git") + if git and (m := _GIT_SHA_RE.search(git)): + shas.add(m.group(1)) + return versions, shas + + +def entry_version(entry_name: str) -> str | None: + """`-[.http]` or `` -> ``; None if not a version.""" + for suffix in _METADATA_SUFFIXES: + if entry_name.endswith(suffix): + entry_name = entry_name[: -len(suffix)] + break + version = entry_name.split("-", 1)[0] + # PEP 440 versions (including epochs) start with a digit. Anything else is + # not ours to judge, so it is kept. + return version if version[:1].isdigit() else None + + +def _registry_roots(cache_dir: Path) -> list[Path]: + roots = [] + for bucket in ("wheels-v*", "sdists-v*"): + for bucket_dir in cache_dir.glob(bucket): + roots.extend(p for p in [bucket_dir / "pypi"] if p.is_dir()) + index = bucket_dir / "index" + if index.is_dir(): + roots.extend(p for p in index.iterdir() if p.is_dir()) + return roots + + +def _sha_kept(prefix: str, shas: set[str]) -> bool: + # Unparseable names are kept: only a hex commit prefix can be matched. + if len(prefix) < 7 or not all(c in "0123456789abcdef" for c in prefix): + return True + return any(sha.startswith(prefix) for sha in shas) + + +def plan_removals( + cache_dir: Path, versions: set[tuple[str, str]], shas: set[str] +) -> list[Path]: + removals = [] + for root in _registry_roots(cache_dir): + for name_dir in root.iterdir(): + if not name_dir.is_dir() or name_dir.is_symlink(): + continue + name = normalize(name_dir.name) + for entry in name_dir.iterdir(): + if root.relative_to(cache_dir).parts[0].startswith("sdists-v") and ( + entry.is_dir() and not entry.is_symlink() + ): + # An sdist version directory is the bare version, compared verbatim: + # stripping a metadata suffix would change a version like `1.0+foo.http`. + version = entry.name if entry.name[:1].isdigit() else None + else: + version = entry_version(entry.name) + if version is not None and (name, version) not in versions: + removals.append(entry) + commit_dirs = [ + *cache_dir.glob("sdists-v*/git/*/*"), + *cache_dir.glob("git-v*/checkouts/*/*"), + ] + for commit_dir in commit_dirs: + if commit_dir.is_dir() and not _sha_kept(commit_dir.name, shas): + removals.append(commit_dir) + # uv >= 0.12 marks a finished checkout with a sibling `.ok` file. + ready = commit_dir.with_name(f"{commit_dir.name}.ok") + if ready.is_file(): + removals.append(ready) + return removals + + +def _reachable_archives(cache_dir: Path, removed: set[Path]) -> set[str]: + archive_root = (cache_dir / "archive-v0").resolve() + registry_roots = set(_registry_roots(cache_dir)) + reachable = set() + for bucket in [*cache_dir.glob("wheels-v*"), *cache_dir.glob("sdists-v*")]: + for dirpath, dirnames, filenames in os.walk(bucket): + here = Path(dirpath) + # Skip the unpacked source trees inside sdist builds (`/src`), but not + # a package that happens to be named `src`. + dirnames[:] = [ + d + for d in dirnames + if here / d not in removed + and ( + d != "src" + or bucket.name.startswith("wheels-v") + or here in registry_roots + ) + ] + # os.walk lists directory symlinks under dirnames without following them. + for name in [*filenames, *dirnames]: + link = here / name + if link in removed or name.endswith(_SCAN_SKIP_SUFFIXES): + continue + if link.is_symlink(): + target = link.resolve() + if target.parent == archive_root: + reachable.add(target.name) + return reachable + + +def _remove_unlinked_files(cache_dir: Path, *, verbose: bool) -> int: + """Drop content-addressed file objects no archive hardlinks any more. + + With uv's `content-addressed-cache` preview feature (on under `preview = true`), + every file in archive-v0// is a hardlink to files-v0//, so + removing an archive frees nothing until its objects go too. An object with a + link count of 1 has no archive (and no hardlinked venv) left -- the same rule + as uv's own `uv cache prune` (Cache::prune_archive_files). + """ + removed = 0 + for obj in cache_dir.glob("files-v*/*/*"): + if not obj.is_symlink() and obj.is_file() and obj.stat().st_nlink == 1: + if verbose: + print(f"remove: {obj}") + obj.unlink() + removed += 1 + for shard in cache_dir.glob("files-v*/*"): + if shard.is_dir() and not shard.is_symlink() and not any(shard.iterdir()): + shard.rmdir() + return removed + + +def prune( + cache_dir: Path, + versions: set[tuple[str, str]], + shas: set[str], + *, + dry_run: bool, + verbose: bool = False, +) -> tuple[int, int]: + """Returns (entries_removed, archives_removed).""" + removals = plan_removals(cache_dir, versions, shas) + reachable = _reachable_archives(cache_dir, set(removals)) + archive_root = cache_dir / "archive-v0" + orphaned = ( + [a for a in archive_root.iterdir() if a.name not in reachable] + if archive_root.is_dir() + else [] + ) + for path in [*removals, *orphaned]: + if verbose: + print(f"{'would remove' if dry_run else 'remove'}: {path}") + if dry_run: + continue + if path.is_symlink() or path.is_file(): + path.unlink() + else: + shutil.rmtree(path) + if not dry_run: + _remove_unlinked_files(cache_dir, verbose=verbose) + return len(removals), len(orphaned) + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--lock", type=Path, required=True, help="Path to uv.lock") + parser.add_argument( + "--cache-dir", type=Path, required=True, help="UV_CACHE_DIR to prune" + ) + parser.add_argument( + "--dry-run", + action="store_true", + help="Report what would be removed without removing it", + ) + parser.add_argument( + "--verbose", action="store_true", help="Print every removed path" + ) + # Sanity floor: a near-empty keep set (e.g. a uv.lock format change this + # script does not understand) must not turn into deleting the whole cache. + parser.add_argument("--min-keep", type=int, default=100) + args = parser.parse_args() + + if not args.lock.is_file(): + print(f"error: {args.lock} does not exist", file=sys.stderr) + return 1 + if not args.cache_dir.is_dir(): + print(f"error: {args.cache_dir} does not exist", file=sys.stderr) + return 1 + + versions, shas = load_lock(args.lock) + if len(versions) < args.min_keep: + print( + f"error: only parsed {len(versions)} pinned packages from {args.lock} " + f"(expected at least {args.min_keep}) -- refusing to prune, " + "uv.lock's format may have changed in a way this script doesn't understand", + file=sys.stderr, + ) + return 1 + + entries_removed, archives_removed = prune( + args.cache_dir, versions, shas, dry_run=args.dry_run, verbose=args.verbose + ) + verb = "Would remove" if args.dry_run else "Removed" + print( + f"{verb} {entries_removed} stale cache entries and {archives_removed} " + f"orphaned archives (kept {len(versions)} packages and {len(shas)} git " + f"commits pinned in {args.lock})" + ) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/docker/uv_cache_mount.sh b/docker/uv_cache_mount.sh index cb4ad9eb1b2..c151298bee4 100644 --- a/docker/uv_cache_mount.sh +++ b/docker/uv_cache_mount.sh @@ -37,14 +37,14 @@ uv_cache_restore() { if [ "${1:-}" = "force" ] || [ ! -d "${UV_CACHE_DIR}" ]; then mkdir -p "${UV_CACHE_DIR}" - rsync -a /root/.uv-cache-store/ "${UV_CACHE_DIR}/" + rsync -aH /root/.uv-cache-store/ "${UV_CACHE_DIR}/" fi } # Sync new downloads back to the durable store and, if STRIP_UV_CACHE is set, # delete the materialized copy before this RUN's layer commits. Pass "prune" -# only on the first uv-writing RUN in a stage, so cleanup performed there (uv -# cache clean, the aiohttp/yarl removal) is reflected in the store. Later +# only on the first uv-writing RUN in a stage, so cleanup performed there (the +# lockfile prune, the aiohttp/yarl removal) is reflected in the store. Later # RUNs must NOT prune: the store can hold entries this RUN never copied in, # and --delete there would discard them from the next build's seed. # @@ -54,9 +54,9 @@ uv_cache_restore() { # should degrade the next build's cache hit rate, not fail this build. uv_cache_persist() { if [ "${1:-}" = "prune" ]; then - rsync -a --delete "${UV_CACHE_DIR}/" /root/.uv-cache-store/ 2>/dev/null || true + rsync -aH --delete "${UV_CACHE_DIR}/" /root/.uv-cache-store/ 2>/dev/null || true else - rsync -a "${UV_CACHE_DIR}/" /root/.uv-cache-store/ 2>/dev/null || true + rsync -aH "${UV_CACHE_DIR}/" /root/.uv-cache-store/ 2>/dev/null || true fi if [ -n "${STRIP_UV_CACHE:-}" ]; then rm -rf "${UV_CACHE_DIR}"; fi } diff --git a/docs/docker.md b/docs/docker.md index f99ff7c2db9..4835ed5067b 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -33,6 +33,12 @@ docker buildx build --build-context nemo-rl=. -f docker/Dockerfile \ > [!NOTE] > The `--tag /nemo-rl:latest --push` flags are not necessary if you just want to build locally. +## uv Cache Pruning + +The release image ships `uv`'s download cache (`/opt/nemo_rl_cache/uv`) so an unprivileged runtime user can later run `uv run --extra ` fully offline. Each build starts that cache from a BuildKit cache mount (seeded from the `uv-cache` registry image), and `uv` only ever adds to it: it never removes a version a prior `uv.lock` pinned and the current one doesn't. The seed export's `uv cache prune --ci` drops prebuilt wheels but keeps every version ever built from source, and a long-lived builder's cache mount keeps everything. + +Right after that cache is restored, before any `uv sync`, `docker/prune_uv_cache.py` removes exactly those superseded entries: index wheels and source builds whose `(package, version)` the current `uv.lock` no longer pins, and git builds/checkouts for commits it no longer pins, across every extra (installed in this build or not). Running first means it only judges restored content, so the build-isolation dependencies the syncs resolve outside `uv.lock` (setuptools, a build-time torch, ...) are kept. It runs in the same layer the syncs fill, because deleting files in a later layer only hides them and the image still ships them, and the result is synced back into the cache mount. It never removes a wheel the offline-install promise depends on, since that promise is itself "every wheel `uv.lock` could need." This runs automatically on every build; there is no flag to disable it. + ## Skipping vLLM or SGLang Dependencies If you don't need vLLM, SGLang, or TRT-LLM support, you can skip building those dependencies to reduce build time and image size. Use the `SKIP_VLLM_BUILD`, `SKIP_SGLANG_BUILD`, and/or `SKIP_TRTLLM_BUILD` build arguments: diff --git a/pyrefly.toml b/pyrefly.toml index cdbe67f7da2..91e8d0348fb 100644 --- a/pyrefly.toml +++ b/pyrefly.toml @@ -25,6 +25,7 @@ replace-imports-with-any = [ "zstandard.*", ] project-includes = [ + "docker/prune_uv_cache.py", "nemo_rl/environments/call_selection.py", "nemo_rl/algorithms/single_controller_utils/logical_advantage.py", "nemo_rl/data/captured_media.py", diff --git a/tests/unit/tools/test_prune_uv_cache.py b/tests/unit/tools/test_prune_uv_cache.py new file mode 100644 index 00000000000..7190c57908a --- /dev/null +++ b/tests/unit/tools/test_prune_uv_cache.py @@ -0,0 +1,358 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import importlib.util +import os +import sys +from pathlib import Path + +import pytest + +REPO_ROOT = Path(__file__).parents[3] + +# docker/ is not a package, so import the script by path. +_SPEC = importlib.util.spec_from_file_location( + "prune_uv_cache", REPO_ROOT / "docker" / "prune_uv_cache.py" +) +prune_uv_cache = importlib.util.module_from_spec(_SPEC) +sys.modules["prune_uv_cache"] = prune_uv_cache +_SPEC.loader.exec_module(prune_uv_cache) + +SHA_KEPT = "17cfb817bccec3a9c247013360cc550c2bac441e" # pragma: allowlist secret +SHA_STALE = "0123456789abcdef0123456789abcdef01234567" # pragma: allowlist secret +TAG = "cp313-cp313-linux_x86_64" + + +def _archive(cache: Path, archive_id: str) -> Path: + archive = cache / "archive-v0" / archive_id + archive.mkdir(parents=True, exist_ok=True) + (archive / "payload.txt").write_text(archive_id) + return archive + + +def _link(link: Path, archive: Path) -> None: + # uv writes relative links into archive-v0. + link.parent.mkdir(parents=True, exist_ok=True) + link.symlink_to(os.path.relpath(archive, link.parent), target_is_directory=True) + + +def _wheel(root: Path, name: str, entry: str, archive_id: str) -> None: + cache = root + while not (cache / "archive-v0").exists() and cache.name != "cache": + cache = cache.parent + _link(root / name / entry, _archive(cache, archive_id)) + (root / name / f"{entry}.http").write_text("cache-control") + + +def _names(path: Path) -> set[str]: + return {p.name for p in path.iterdir()} if path.exists() else set() + + +@pytest.fixture +def cache(tmp_path: Path) -> Path: + c = tmp_path / "cache" + (c / "archive-v0").mkdir(parents=True) + return c + + +def test_normalize_is_pep503() -> None: + assert prune_uv_cache.normalize("Foo_Bar.Baz") == "foo-bar-baz" + assert prune_uv_cache.normalize("foo--bar") == "foo-bar" + assert prune_uv_cache.normalize("UPPER") == "upper" + + +def test_load_lock_reads_versions_and_git_commits(tmp_path: Path) -> None: + long_version = "1.0.0+" + "a" * 60 + lock = tmp_path / "uv.lock" + lock.write_text( + f""" +version = 1 + +[[package]] +name = "Orjson" +version = "3.12.0" +source = {{ registry = "https://pypi.org/simple" }} + +[[package]] +name = "deep-ep" +version = "1.2.1" +source = {{ git = "https://github.com/deepseek-ai/DeepEP.git?rev={SHA_KEPT}#{SHA_KEPT}" }} + +[[package]] +name = "long" +version = "{long_version}" +source = {{ registry = "https://pypi.org/simple" }} + +[[package]] +name = "nemo-rl" +source = {{ editable = "." }} +""" + ) + versions, shas = prune_uv_cache.load_lock(lock) + assert ("orjson", "3.12.0") in versions + assert ("deep-ep", "1.2.1") in versions + assert ("long", long_version) in versions + # uv truncates long versions in wheel cache keys; the truncated form is kept too. + assert ("long", long_version[:47]) in versions + assert shas == {SHA_KEPT} + + +@pytest.mark.parametrize( + "entry, expected", + [ + (f"3.12.0-{TAG}", "3.12.0"), + (f"3.12.0-{TAG}.http", "3.12.0"), + (f"3.12.0-{TAG}.msgpack", "3.12.0"), + (f"2.9.0+cu130-{TAG}", "2.9.0+cu130"), + ("1!2.0-py3-none-any", "1!2.0"), + ("1.0.1", "1.0.1"), + ("metadata.msgpack", None), + (".lock", None), + ], +) +def test_entry_version(entry: str, expected: str | None) -> None: + assert prune_uv_cache.entry_version(entry) == expected + + +def test_prunes_stale_pypi_wheels_and_their_archives(cache: Path) -> None: + pypi = cache / "wheels-v7" / "pypi" + _wheel(pypi, "orjson", f"3.11.3-{TAG}", "old-1") + _wheel(pypi, "orjson", f"3.11.9-{TAG}", "old-2") + _wheel(pypi, "orjson", f"3.12.0-{TAG}", "kept") + + removed, archives = prune_uv_cache.prune( + cache, {("orjson", "3.12.0")}, set(), dry_run=False + ) + + assert (removed, archives) == (4, 2) + assert _names(pypi / "orjson") == {f"3.12.0-{TAG}", f"3.12.0-{TAG}.http"} + assert _names(cache / "archive-v0") == {"kept"} + assert (cache / "archive-v0" / "kept" / "payload.txt").read_text() == "kept" + + +def test_does_not_confuse_version_prefixes(cache: Path) -> None: + # Removing 1.0 must not touch 1.0.1's files (a prefix glob would). + pypi = cache / "wheels-v7" / "pypi" + _wheel(pypi, "pkg", "1.0-py3-none-any", "stale") + _wheel(pypi, "pkg", "1.0.1-py3-none-any", "kept") + + prune_uv_cache.prune(cache, {("pkg", "1.0.1")}, set(), dry_run=False) + + assert _names(pypi / "pkg") == { + "1.0.1-py3-none-any", + "1.0.1-py3-none-any.http", + } + assert _names(cache / "archive-v0") == {"kept"} + + +def test_prunes_alternative_index_trees(cache: Path) -> None: + # torch & co. come from download.pytorch.org: wheels-v7/index//... + index = cache / "wheels-v7" / "index" / "d1g3st" + _wheel(index, "torch", f"2.8.0+cu130-{TAG}", "torch-old") + _wheel(index, "torch", f"2.9.0+cu130-{TAG}", "torch-kept") + + prune_uv_cache.prune(cache, {("torch", "2.9.0+cu130")}, set(), dry_run=False) + + assert _names(index / "torch") == { + f"2.9.0+cu130-{TAG}", + f"2.9.0+cu130-{TAG}.http", + } + assert _names(cache / "archive-v0") == {"torch-kept"} + + +def test_keeps_archives_referenced_by_untouched_trees(cache: Path) -> None: + # url/ and editable/ entries carry no version to match, so they are left + # alone -- and so must the archives they point at. + wheels = cache / "wheels-v7" + _link(wheels / "url" / "abc" / "vllm" / f"0.29.0-{TAG}", _archive(cache, "url")) + _link(wheels / "editable" / "def" / "proj" / "0.1.0", _archive(cache, "edit")) + + removed, archives = prune_uv_cache.prune(cache, set(), set(), dry_run=False) + + assert (removed, archives) == (0, 0) + assert _names(cache / "archive-v0") == {"url", "edit"} + + +def test_prunes_stale_sdist_versions_keeping_built_wheel_archives( + cache: Path, +) -> None: + sdists = cache / "sdists-v10" / "pypi" / "flash-attn" + _link(sdists / "2.7.0" / "rev1" / "wheel", _archive(cache, "built-old")) + _link(sdists / "2.8.3" / "rev1" / "wheel", _archive(cache, "built-kept")) + (sdists / "2.8.3" / "rev1" / "src").mkdir() + + prune_uv_cache.prune(cache, {("flash-attn", "2.8.3")}, set(), dry_run=False) + + assert _names(sdists) == {"2.8.3"} + assert _names(cache / "archive-v0") == {"built-kept"} + + +def test_prunes_git_builds_and_checkouts_by_commit(cache: Path) -> None: + # uv >= 0.13: sdists-v10/git//<16-hex commit>// + # holds the built wheel, its unzipped link and metadata.msgpack (astral-sh/uv#22404). + git_builds = cache / "sdists-v10" / "git" / "repo" + for sha, archive in ((SHA_KEPT, "git-kept"), (SHA_STALE, "git-old")): + shard = git_builds / sha[:16] / "b1nf0d1g3st" + _link(shard / "pkg-1.0-py3-none-any", _archive(cache, archive)) + (shard / "metadata.msgpack").write_text("metadata") + # git-v1/checkouts//<7-hex commit>/ plus its `<7-hex commit>.ok` marker. + checkouts = cache / "git-v1" / "checkouts" / "repo" + for sha in (SHA_KEPT, SHA_STALE): + (checkouts / sha[:7]).mkdir(parents=True, exist_ok=True) + (checkouts / f"{sha[:7]}.ok").touch() + (checkouts / "not-a-sha").mkdir() + (cache / "git-v1" / "db" / "repo").mkdir(parents=True) + + removed, archives = prune_uv_cache.prune(cache, set(), {SHA_KEPT}, dry_run=False) + + assert (removed, archives) == (3, 1) + assert _names(git_builds) == {SHA_KEPT[:16]} + assert _names(git_builds / SHA_KEPT[:16] / "b1nf0d1g3st") == { + "pkg-1.0-py3-none-any", + "metadata.msgpack", + } + assert _names(checkouts) == {SHA_KEPT[:7], f"{SHA_KEPT[:7]}.ok", "not-a-sha"} + assert (cache / "git-v1" / "db" / "repo").is_dir() + assert _names(cache / "archive-v0") == {"git-kept"} + + +def test_removes_content_addressed_files_no_archive_links(cache: Path) -> None: + # uv's content-addressed-cache preview (on under `preview = true`) hardlinks every + # archive file to files-v0//; dropping the archive alone frees nothing. + pypi = cache / "wheels-v7" / "pypi" + _wheel(pypi, "orjson", f"3.11.3-{TAG}", "old") + _wheel(pypi, "orjson", f"3.12.0-{TAG}", "kept") + files = cache / "files-v0" + for archive in ("old", "kept"): + obj = files / archive[:2] / f"{archive}-digest" + obj.parent.mkdir(parents=True, exist_ok=True) + os.link(cache / "archive-v0" / archive / "payload.txt", obj) + # An object restored without its hardlink (e.g. `rsync -a` without -H) is unlinked too. + (files / "zz").mkdir() + (files / "zz" / "split-copy").write_text("x") + + assert prune_uv_cache.prune( + cache, {("orjson", "3.12.0")}, set(), dry_run=False + ) == (2, 1) + + assert _names(files) == {"ke"} + assert _names(files / "ke") == {"kept-digest"} + assert (cache / "archive-v0" / "kept" / "payload.txt").stat().st_nlink == 2 + + +def test_dry_run_reports_same_counts_without_removing(cache: Path) -> None: + pypi = cache / "wheels-v7" / "pypi" + _wheel(pypi, "orjson", f"3.11.3-{TAG}", "old") + _wheel(pypi, "orjson", f"3.12.0-{TAG}", "kept") + keep = {("orjson", "3.12.0")} + + assert prune_uv_cache.prune(cache, keep, set(), dry_run=True) == (2, 1) + assert len(_names(pypi / "orjson")) == 4 + assert _names(cache / "archive-v0") == {"old", "kept"} + assert prune_uv_cache.prune(cache, keep, set(), dry_run=False) == (2, 1) + + +def test_second_run_is_a_no_op(cache: Path) -> None: + pypi = cache / "wheels-v7" / "pypi" + _wheel(pypi, "orjson", f"3.11.3-{TAG}", "old") + _wheel(pypi, "orjson", f"3.12.0-{TAG}", "kept") + keep = {("orjson", "3.12.0")} + + prune_uv_cache.prune(cache, keep, set(), dry_run=False) + assert prune_uv_cache.prune(cache, keep, set(), dry_run=False) == (0, 0) + + +def test_dockerfile_prunes_the_seed_before_any_sync() -> None: + # Prune restored content in its importing layer, before syncs add build dependencies. + lines = (REPO_ROOT / "docker" / "Dockerfile").read_text().splitlines() + code = [(i, line) for i, line in enumerate(lines) if not line.startswith("#")] + + copy_script = next( + i for i, l in code if l.startswith("COPY") and "prune_uv_cache.py" in l + ) + prunes = [ + i for i, l in code if "prune_uv_cache.py" in l and not l.startswith("COPY") + ] + assert len(prunes) == 1, "prune must run exactly once, in the dependency RUN" + prune = prunes[0] + run_start = max(i for i, l in code if i < prune and l.startswith("RUN")) + run_end = next(i for i, l in code if i > prune and l == "EOF") + restore = next( + i for i, l in code if i > run_start and l.startswith("uv_cache_restore") + ) + persist = next(i for i, l in code if i > run_start and "uv_cache_persist" in l) + venv = next(i for i, l in code if i > run_start and l == "uv venv --seed") + first_sync = next(i for i, l in code if i > run_start and "uv sync" in l) + + # The script comes from the recipe's own context: an older NRL_GIT_REF lacks it. + assert "--from=" not in lines[copy_script] + assert copy_script < run_start + assert lines[restore] == "uv_cache_restore force" + assert "uv_cache_persist prune" in lines[persist] + assert run_start < restore < venv < prune < first_sync < run_end + assert persist < run_end + + # Dynamo installs with a throwaway cache, so its RUN needs no shared cache mount. + dynamo = next(i for i, l in code if "docker/dynamo/install.sh" in l and "bash" in l) + dynamo_run = max(i for i, l in code if i < dynamo and l.startswith("RUN")) + assert "uv-cache" not in "\n".join(lines[dynamo_run:dynamo]) + assert lines[dynamo].startswith("UV_CACHE_DIR=/tmp/") + assert lines[dynamo + 1].startswith("rm -rf /tmp/") + + +@pytest.mark.parametrize("suffix", [".http", ".msgpack", ".lock", ".rev"]) +def test_compares_sdist_version_directories_verbatim(cache: Path, suffix: str) -> None: + version = f"1.0+foo{suffix}" + sdists = cache / "sdists-v10" / "pypi" / "pkg" + _link(sdists / version / "rev" / "wheel", _archive(cache, "built")) + + prune_uv_cache.prune(cache, {("pkg", version)}, set(), dry_run=False) + assert _names(sdists) == {version} + assert _names(cache / "archive-v0") == {"built"} + + # Pinning only the shorter version must not keep the longer one. + prune_uv_cache.prune(cache, {("pkg", "1.0+foo")}, set(), dry_run=False) + assert _names(sdists) == set() + assert _names(cache / "archive-v0") == set() + + +def test_keeps_archives_of_a_package_named_src(cache: Path) -> None: + pypi = cache / "wheels-v7" / "pypi" + _wheel(pypi, "src", "1.0-py3-none-any", "wheel-kept") + _wheel(pypi, "src", "0.9-py3-none-any", "wheel-old") + sdists = cache / "sdists-v10" / "pypi" + _link(sdists / "src" / "1.0" / "rev" / "wheel", _archive(cache, "sdist-kept")) + _link(sdists / "src" / "0.9" / "rev" / "wheel", _archive(cache, "sdist-old")) + # A link inside an unpacked source tree does not keep its target alive. + _link(sdists / "src" / "1.0" / "rev" / "src" / "x", _archive(cache, "in-src")) + + prune_uv_cache.prune(cache, {("src", "1.0")}, set(), dry_run=False) + + assert _names(pypi / "src") == {"1.0-py3-none-any", "1.0-py3-none-any.http"} + assert _names(sdists / "src") == {"1.0"} + assert _names(cache / "archive-v0") == {"wheel-kept", "sdist-kept"} + + +def test_main_refuses_a_suspiciously_small_lock( + cache: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + _wheel(cache / "wheels-v7" / "pypi", "orjson", f"3.11.3-{TAG}", "old") + lock = tmp_path / "uv.lock" + lock.write_text('version = 1\n[[package]]\nname = "a"\nversion = "1"\n') + monkeypatch.setattr( + sys, "argv", ["prune", "--lock", str(lock), "--cache-dir", str(cache)] + ) + + assert prune_uv_cache.main() == 1 + assert _names(cache / "archive-v0") == {"old"}