Skip to content

The agent that grows with you

fix(pm): prune fetch-* cache and stale entries during bundle, test gc

ethernet3de5990bd029
diff --git a/pm/cli.py b/pm/cli.py
index 5a1ea4e2c6f..710b7a732ee 100644
--- a/pm/cli.py
+++ b/pm/cli.py
@@ -309,13 +309,20 @@ def cmd_develop(args) -> int:
return subprocess.call([shell], env=env, cwd=paths.repo_root())
-def cmd_gc(args) -> int:
+def _gc_store(store, facts) -> tuple[int, int]:
+ """The sweep core shared by `pm gc` and `pm bundle`.
+
+ Removes every store entry nothing references: fetch-<sha> download-cache
+ dirs (the raw archives — needed only at install time, dead weight in a
+ staged payload or a CI cache), orphaned package versions from an older
+ lock, and expired partials. Keeps live package entries (recorded in
+ facts) and partials an in-flight download still owns. Returns
+ (removed, kept).
+ """
from pm.downloader import gc_protected_names
- facts = _facts()
- store = _store()
if not store.root.is_dir():
- return 0
+ return (0, 0)
removed = 0
with store.install_lock():
facts.reload()
@@ -344,7 +351,14 @@ def cmd_gc(args) -> int:
print(f"removing {item.name}")
shutil.rmtree(item, ignore_errors=True)
removed += 1
- print(f"gc: removed {removed}, kept {len(keep)}")
+ return (removed, len(keep))
+
+
+def cmd_gc(args) -> int:
+ facts = _facts()
+ store = _store()
+ removed, kept = _gc_store(store, facts)
+ print(f"gc: removed {removed}, kept {kept}")
return 0
@@ -429,6 +443,14 @@ def cmd_bundle(args) -> int:
)
_drop_unloadable_runtime_files(store_dir)
+ # Prune the staged store BEFORE the venv sync and packaging: drop the
+ # fetch-<sha> download-cache archives (needed only at install time — dead
+ # weight in the shipped payload AND in the CI cache that restores this
+ # dir) and any orphaned package versions left over from an older lock
+ # the cache carried in. A lean staged store = a lean CI cache.
+ removed, kept = _gc_store(_store(), _facts())
+ print(f"✓ gc: pruned {removed} fetch/stale entries, kept {kept}")
+
uv_bin, env = pm_uv()
if uv_bin is None:
print("✗ venv: uv did not stage")
diff --git a/tests/pm/test_pm_core.py b/tests/pm/test_pm_core.py
index f4a42c02ff8..dfa893f42b1 100644
--- a/tests/pm/test_pm_core.py
+++ b/tests/pm/test_pm_core.py
@@ -424,6 +424,27 @@ def test_gc_keeps_used_removes_orphans(pm_env):
assert any(p.name.startswith("faketool-1.0") for p in runtime.iterdir())
+def test_gc_removes_fetch_cache_archives(pm_env):
+ """The fetch-<sha> download-cache dirs are install-time only — gc must
+ drop them so a staged payload (and the CI cache that stores it) doesn't
+ carry the raw archives. The live package entry survives."""
+ from pm.cli import cmd_gc
+ from pm.ensure import ensure
+
+ _, runtime, *_ = pm_env
+ ensure("faketool", base_env={})
+
+ # install() fetched via store.fetch → a fetch-<sha> archive cache dir.
+ fetches = [p for p in runtime.iterdir() if p.name.startswith("fetch-")]
+ assert fetches, "install should have left fetch-<sha> download-cache dirs"
+
+ cmd_gc(None)
+ assert not [p for p in runtime.iterdir() if p.name.startswith("fetch-")], \
+ "gc must remove the fetch-<sha> archive cache"
+ assert any(p.name.startswith("faketool-1.0") for p in runtime.iterdir()), \
+ "the live package entry survives gc"
+
+
def test_env_for_never_installs(pm_env):
from pm.ensure import env_for