Skip to content

The agent that grows with you

fix(pm): gc collects abandoned .staging-* scratch dirs

ethernetfc140f2598f6
diff --git a/pm/cli.py b/pm/cli.py
index dbd446997c6..7faaf6f5848 100644
--- a/pm/cli.py
+++ b/pm/cli.py
@@ -264,7 +264,13 @@ def _gc_store(store, facts) -> tuple[int, int]:
keep = facts.entries_in_use()
collect_partials(partials_dir)
for item in sorted(store.root.iterdir()):
- if not item.is_dir() or item.name.startswith("."):
+ if not item.is_dir():
+ continue
+ # Scratch dirs are created and removed under this same lock, so any
+ # that remain belong to a killed installer. Other dot-dirs stay:
+ # .previous-* is the restore point the next install of that entry
+ # consumes, and it is only safe to drop after that verification.
+ if item.name.startswith(".") and not item.name.startswith(".staging-"):
continue
if item.name in keep:
continue
diff --git a/tests/pm/test_pm_core.py b/tests/pm/test_pm_core.py
index f7c9f77efa9..1335ea14d79 100644
--- a/tests/pm/test_pm_core.py
+++ b/tests/pm/test_pm_core.py
@@ -401,8 +401,13 @@ def test_gc_keeps_used_removes_orphans(pm_env):
ensure("faketool", base_env={})
orphan = runtime / "orphan-9.9-nowhere"
orphan.mkdir()
+ # A killed installer's scratch dir; its restore point must survive gc.
+ (runtime / ".staging-abandoned" / "tree").mkdir(parents=True)
+ (runtime / ".previous-faketool-1.0").mkdir()
cmd_gc(None)
assert not orphan.exists()
+ assert not (runtime / ".staging-abandoned").exists()
+ assert (runtime / ".previous-faketool-1.0").is_dir()
assert any(p.name.startswith("faketool-1.0") for p in runtime.iterdir())