fix(gc): let worktree holds beat the forge skip, read the mount table and recheck docker before deletes
CI / security-codeql (push) Canceled after 0s
CI / test-unit (push) Canceled after 0s
CI / test-integration (push) Canceled after 0s
CI / test-env-virtual (push) Canceled after 0s
CI / test-env-nix (push) Canceled after 0s
CI / test-e2e (push) Canceled after 0s
CI / test-virgin-user (push) Canceled after 0s
CI / test-virgin-root (push) Canceled after 0s
CI / lint-shell (push) Canceled after 0s
CI / lint-python (push) Canceled after 0s
CI / lint-docker (push) Canceled after 0s
CI / quality (push) Canceled after 0s

CodeRabbit architecture review on PR 21: a forge-unavailable skip hid lock, busy, checkout and age holds from the risk plan; a mount at a scratch candidate or a same-device bind mount went unseen; containers and build cache were removed without a fresh check. Tree holds now win over the forge skip and the age check also runs under it, scratch items read /proc/self/mountinfo, and docker items are planned again before each delete.

PR: https://github.com/kevinveenbirkenbach/package-manager/pull/21
Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-12-01 21:38:08 +00:00
1 parent babc977cfe
commit 43a3f9f73d
9 files changed
+102 -75

No files matched your search

@@ -3,7 +3,6 @@ from __future__ import annotations
import os
import tempfile
import unittest
from types import SimpleNamespace
from unittest.mock import patch
from pkgmgr.actions.gc.report import walk as walk_module
@@ -109,28 +108,6 @@ class TestWalk(unittest.TestCase):
self.assertEqual(stats.size_bytes, 0)
def test_child_on_another_filesystem_is_counted_not_walked(self) -> None:
self._write("mnt/inner", b"x" * 8192)
mnt = os.path.join(self.root, "mnt")
fields = list(os.lstat(mnt))
fields[2] += 1
moved = os.stat_result(fields)
real_children = walk_module._children
def children(path, stats):
return [
SimpleNamespace(path=e.path, stat=lambda follow_symlinks: moved)
if e.path == mnt
else e
for e in real_children(path, stats)
]
with patch.object(walk_module, "_children", side_effect=children):
stats = walk(self.root)
self.assertEqual(stats.mounts, 1)
self.assertEqual(stats.size_bytes, allocated(self.root))
if __name__ == "__main__":
unittest.main()
@@ -11,6 +11,7 @@ from tests.unit.pkgmgr.actions.gc.risk.risk_fixture import (
DANGLING_ID,
OLD_CACHE,
RiskPlanFixture,
write,
)
@@ -114,25 +115,21 @@ class TestApplyLow(RiskPlanFixture, unittest.TestCase):
self.assertTrue(os.path.isdir(self.wt["done"]))
self.assertTrue(os.path.isfile(self.t["old-done.log"]))
def _owners_after_plan(self, content: str | None) -> tuple[int, dict]:
def _after_plan(self, change) -> tuple[int, dict]:
real_apply = plan._apply
def rewrite_then_apply(*args):
if content is None:
os.unlink(self.owners)
else:
with open(self.owners, "w") as fh:
fh.write(content)
def change_then_apply(*args):
change()
return real_apply(*args)
with patch.object(plan, "_apply", side_effect=rewrite_then_apply):
with patch.object(plan, "_apply", side_effect=change_then_apply):
code, printed, _ = self.run_gc(level="low", apply=True)
return code, printed
def test_owner_status_changed_after_plan_keeps_the_scratch(self) -> None:
running = json.dumps({self.t["old-stopped"]: "running"})
code, printed = self._owners_after_plan(running)
code, printed = self._after_plan(lambda: write(self.owners, running))
stopped = self.entry(printed, self.t["old-stopped"])
self.assertEqual(code, 0)
@@ -142,7 +139,7 @@ class TestApplyLow(RiskPlanFixture, unittest.TestCase):
self.assertTrue(os.path.isdir(self.t["old-stopped"]))
def test_owner_file_gone_after_plan_fails_and_keeps_the_scratch(self) -> None:
code, printed = self._owners_after_plan(None)
code, printed = self._after_plan(lambda: os.unlink(self.owners))
self.assertEqual(code, 1)
for name in ("old-stopped", "old-done.log"):
@@ -151,21 +148,47 @@ class TestApplyLow(RiskPlanFixture, unittest.TestCase):
)
self.assertTrue(os.path.lexists(self.t[name]), name)
def test_mount_below_scratch_is_never_low(self) -> None:
real_walk = tmp_items.walk
def test_docker_items_no_longer_low_after_plan_are_kept(self) -> None:
def nothing_low() -> None:
df = json.loads(self.docker.df)
for row in df["Containers"]:
row["LocalVolumes"] = "1"
for row in df["BuildCache"]:
row["InUse"] = "true"
self.docker.df = json.dumps(df)
self.docker.calls.clear()
def mounted(path, *args):
stats = real_walk(path, *args)
if path == self.t["old-stopped"]:
stats.mounts = 1
return stats
with patch.object(tmp_items, "walk", side_effect=mounted):
code, printed, _ = self.run_gc(level="low", apply=True)
code, printed = self._after_plan(nothing_low)
changed = {
e["name"]: e["result"]
for e in printed["items"]
if e["kind"] in ("build-cache", "container")
}
self.assertEqual(code, 0)
self.assertNotIn(self.t["old-stopped"], self.risks(printed))
self.assertTrue(os.path.isdir(self.t["old-stopped"]))
self.assertEqual(
changed,
dict.fromkeys((OLD_CACHE, BARE_CONTAINER), "skipped: changed since plan"),
)
self.assertEqual(self.docker.writes(), [["image", "rm", DANGLING_ID]])
def test_mount_at_or_below_scratch_is_never_low(self) -> None:
stopped = self.t["old-stopped"]
table = os.path.join(self.root, "mountinfo")
for point in (stopped, os.path.join(stopped, "deep"), None):
with self.subTest(point=point):
if point is None:
table_path = os.path.join(self.root, "absent")
else:
escaped = point.replace(" ", "\\040")
write(table, f"36 35 98:0 / {escaped} rw - ext4 /dev/x rw\n")
table_path = table
with patch.object(tmp_items, "MOUNTINFO", table_path):
code, printed, _ = self.run_gc(level="low", apply=True)
self.assertEqual(code, 0)
self.assertNotIn(stopped, self.risks(printed))
self.assertTrue(os.path.isdir(stopped))
if __name__ == "__main__":
@@ -12,6 +12,7 @@ from pkgmgr.actions.branch.gc_forge import ForgeFacts
from pkgmgr.actions.gc.risk import plan
from pkgmgr.cli.commands.worktree import handle_gc
from pkgmgr.cli.parser import create_parser
from tests.support.git_sandbox import git
from tests.unit.pkgmgr.actions.gc.risk.risk_fixture import (
BARE_CONTAINER,
BUSY_CACHE,
@@ -60,6 +61,14 @@ class TestRiskRules(RiskPlanFixture, unittest.TestCase):
self.assertEqual(done["pr"], "unknown")
self.assertEqual(done["skip"], "forge unavailable")
def test_forge_absence_never_hides_a_worktree_hold(self) -> None:
self.forge = ForgeFacts(unavailable="no GitHub token")
git(self.repo, "worktree", "lock", "--reason", "kept", self.wt["done"])
_, printed, _ = self.run_gc(level="high")
done = self.entry(printed, self.wt["done"])
self.assertEqual((done["risk"], done["skip"]), ("medium", "lock: kept"))
def test_open_pull_request_is_not_low(self) -> None:
self.forge = ForgeFacts(open_heads={"done": 9})
_, printed, _ = self.run_gc(level="high")