Skip to content

The agent that grows with you

fix(pm): read retention from the home that owns installs/ (not a symlink target's parent); final removal is a non-recursive rmdir

Tekniumec6704a584de
diff --git a/pm/install_states.py b/pm/install_states.py
index ef911788319..c62b1803881 100644
--- a/pm/install_states.py
+++ b/pm/install_states.py
@@ -92,7 +92,7 @@ def orphan_install_states(installs: Path) -> list[Path]:
if not installs.is_dir():
return []
data_root = installs.resolve().parent
- retention = _retention_seconds(data_root)
+ retention = _retention_seconds(installs.parent)
return [state for state in sorted(installs.iterdir()) if _is_orphan(state, data_root, retention)]
@@ -155,7 +155,7 @@ def collect_orphan_install_states(installs: Path) -> list[Path]:
if not installs.is_dir():
return []
data_root = installs.resolve().parent
- retention = _retention_seconds(data_root)
+ retention = _retention_seconds(installs.parent)
removed: list[Path] = []
for state in [s for s in sorted(installs.iterdir()) if _is_orphan(s, data_root, retention)]:
with _install_lock(state) as locked:
@@ -163,10 +163,16 @@ def collect_orphan_install_states(installs: Path) -> list[Path]:
logger.debug("orphan install state %s is held or back in use; skipped", state.name)
continue
emptied = _remove_contents(state)
+ if emptied: # still under the lock: only the record is left, so it goes last
+ shutil.rmtree(state / "inputs", ignore_errors=True)
if not emptied:
logger.debug("orphan install state %s only partly removed; retried next pass", state.name)
continue
- shutil.rmtree(state, ignore_errors=True)
+ # Non-recursive: a launch from a checkout recreated at the same path may have taken the
+ # lock the moment it was released and written here; its files make rmdir fail, not vanish.
+ with contextlib.suppress(OSError):
+ (state / ".install.lock").unlink()
+ state.rmdir()
if not state.exists():
removed.append(state)
return removed
diff --git a/tests/pm/test_install_states_gc.py b/tests/pm/test_install_states_gc.py
index 0382af4ac5b..040110de692 100644
--- a/tests/pm/test_install_states_gc.py
+++ b/tests/pm/test_install_states_gc.py
@@ -157,6 +157,21 @@ def test_retention_comes_from_the_store_owner_not_the_active_profile(tmp_path, r
assert state.exists() is not reclaimed
+def test_symlinked_installs_reads_retention_from_the_home_not_the_link_target(tmp_path):
+ """``installs/`` moved to another disk via a symlink: the opt-out in the home's config still
+ applies, and the link target's parent is not turned into a Hermes home."""
+ root = tmp_path / "home"
+ root.mkdir()
+ (root / "config.yaml").write_text("worktrees:\n install_state_retention_days: 0\n", encoding="utf-8")
+ target = tmp_path / "bigdisk" / "installs"
+ target.mkdir(parents=True)
+ (root / "installs").symlink_to(target, target_is_directory=True)
+ state = _state(root / "installs", "aaaa", tmp_path / "unmounted" / "checkout")
+ _age(state, 30)
+ assert collect_orphan_install_states(root / "installs") == []
+ assert state.exists() and sorted(p.name for p in target.parent.iterdir()) == ["installs"]
+
+
def test_state_used_while_another_orphan_is_removed_is_kept(tmp_path, monkeypatch):
"""Candidates are listed before any removal starts; a state launched (lease taken and
released) while an earlier, large orphan is still being removed is back in use."""