The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(bundles): prune the shipped uv cache to the lock — the ship gate for rolling CI caches
ethief07d00ebb5da
diff --git a/scripts/bundles/native.py b/scripts/bundles/native.pyindex 55ca920b856..cd504a6ca64 100644--- a/scripts/bundles/native.py+++ b/scripts/bundles/native.py@@ -52,6 +52,54 @@ def _arch_guard(store_dir: Path) -> list[str]:+def _lock_package_names(source_repo: Path) -> set[str]:+ """Dist names the shipped lock can resolve; the ship contract for the cache."""+ import tomllib++ data = tomllib.loads((source_repo / "uv.lock").read_text(encoding="utf-8"))+ return {entry["name"].lower().replace("_", "-") for entry in data["package"]}+++def prune_uv_cache_to_lock(cache: Path, source_repo: Path) -> int:+ """Delete cache entries for dists the lock cannot resolve.++ The CI cache is a rolling snapshot restored by prefix after dependency+ changes, so it accumulates wheels for superseded pins. Shipping that+ sediment would bloat every bundle; the lock is the ship contract.+ Returns the pruned entry count for the build log.+ """+ import re++ 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 match.group(1).lower().replace("_", "-")+ 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 entry.name.lower().replace("_", "-") not in keep:+ shutil.rmtree(entry, ignore_errors=True)+ pruned += 1+ return pruned++def stage_uv_cache(source: Path, destination: Path) -> None:"""Ship everything a per-install venv rebuild resolves from offline.@@ -241,7 +289,8 @@ def _prepare_native(*, out: Path, ref: str, source: Path, cache: Path,if src_cache.is_dir():print(f" uv-cache: copying {src_cache} → payload...", flush=True)stage_uv_cache(src_cache, payload_cache)- print("✓ uv-cache (full — offline rebuilds resolve from shipped wheels)", flush=True)+ pruned = prune_uv_cache_to_lock(payload_cache, repo_dir)+ print(f"✓ uv-cache (lock-scoped: pruned {pruned} stale entries; offline rebuilds resolve from shipped wheels)", flush=True)else:raise InstallError("uv-cache", "runtime dependency cache is missing")diff --git a/tests/scripts/test_bundle_native.py b/tests/scripts/test_bundle_native.pyindex d2d5dda6903..0b88586e8e2 100644--- a/tests/scripts/test_bundle_native.py+++ b/tests/scripts/test_bundle_native.py@@ -310,6 +310,71 @@ def test_bundle_stages_git_tree_and_runs_native_children_before_manifest(tmp_patnative._stage_native(args)+def test_staged_cache_prunes_entries_the_lock_cannot_resolve(tmp_path, monkeypatch):+ """Ship gate: a rolled CI cache snapshot carries wheels for superseded pins;+ the staged payload cache must contain exactly what the shipped lock resolves."""+ lock = tmp_path / "repo"+ lock.mkdir()+ (lock / "uv.lock").write_text(+ "[[package]]\n"+ 'name = "keep-me"\n'+ 'version = "1.0.0"\n'+ 'source = { registry = "https://pypi.org/simple" }\n'+ "\n"+ "[[package]]\n"+ 'name = "also-keep"\n'+ 'version = "2.0.0"\n'+ 'source = { registry = "https://pypi.org/simple" }\n',+ encoding="utf-8",+ )+ cache = tmp_path / "cache"+ buckets = {+ "keep-me": ("keep_me-1.0.0.dist-info", True),+ "also-keep": ("also_keep-2.0.0.dist-info", True),+ "superseded-pin": ("superseded_pin-0.9.9.dist-info", False),+ "old-dependency": ("old_dependency-3.2.1.dist-info", False),+ }+ for bucket, (dist_info, _keep) in buckets.items():+ path = cache / "archive-v0" / bucket / dist_info+ path.parent.mkdir(parents=True)+ path.write_text("", encoding="utf-8")+ # an unversioned bucket (no dist-info) must survive — it is not identifiable+ unknown = cache / "archive-v0" / "unknownbucket"+ unknown.mkdir(parents=True)+ (unknown / "data.bin").write_bytes(b"x")+ pruned = native.prune_uv_cache_to_lock(cache, lock)+ assert pruned == 2+ assert (cache / "archive-v0" / "keep-me").is_dir()+ assert (cache / "archive-v0" / "also-keep").is_dir()+ assert not (cache / "archive-v0" / "superseded-pin").exists()+ assert not (cache / "archive-v0" / "old-dependency").exists()+ assert (unknown / "data.bin").exists()+++def test_staged_cache_prunes_wheel_index_entries_outside_the_lock(tmp_path):+ lock = tmp_path / "repo"+ lock.mkdir()+ (lock / "uv.lock").write_text(+ "[[package]]\n"+ 'name = "keep-me"\n'+ 'version = "1.0.0"\n'+ 'source = { registry = "https://pypi.org/simple" }\n',+ encoding="utf-8",+ )+ cache = tmp_path / "cache"+ for entry in ("keep-me", "superseded-pin"):+ path = cache / "wheels-v6" / "pypi" / entry+ path.mkdir(parents=True)+ (path / "metadata.msgpack").write_bytes(b"")+ sdist = cache / "sdists-v9" / "pypi" / "old-sdist-only"+ sdist.mkdir(parents=True)+ (sdist / "metadata.msgpack").write_bytes(b"")+ native.prune_uv_cache_to_lock(cache, lock)+ assert (cache / "wheels-v6" / "pypi" / "keep-me").is_dir()+ assert not (cache / "wheels-v6" / "pypi" / "superseded-pin").exists()+ assert not (cache / "sdists-v9" / "pypi" / "old-sdist-only").exists()++@pytest.mark.parametrize("pointer, shard", [("revision.http", ""), ("revision.rev", "build-settings")])def test_staged_cache_skips_build_inputs_before_copying(tmp_path, monkeypatch, pointer, shard):cache = tmp_path / "cache"