Skip to content

The agent that grows with you

fix(disk-cleanup): accept macOS temp aliases

Benjamin Brumbaugh0ba4992c52e9
diff --git a/plugins/disk-cleanup/disk_cleanup.py b/plugins/disk-cleanup/disk_cleanup.py
index 5bd50034cce..c8cd1e0e40f 100755
--- a/plugins/disk-cleanup/disk_cleanup.py
+++ b/plugins/disk-cleanup/disk_cleanup.py
@@ -30,12 +30,28 @@ def _state_file(name: str) -> Path:
def is_safe_path(path: Path) -> bool:
- """Accept only paths under HERMES_HOME or ``/tmp/hermes-*`` (rejects /mnt/c etc.)."""
- with contextlib.suppress(ValueError, OSError):
- path.resolve().relative_to(get_hermes_home())
+ """Accept only paths under HERMES_HOME or ``/tmp/hermes-*``.
+
+ Rejects Windows mounts (``/mnt/c`` etc.) and any system directory.
+ """
+ hermes_home = get_hermes_home()
+ try:
+ resolved = path.resolve()
+ except OSError:
+ return False
+ try:
+ resolved.relative_to(hermes_home)
return True
- parts = path.parts
- return len(parts) >= 3 and parts[1] == "tmp" and parts[2].startswith("hermes-")
+ except ValueError:
+ pass
+
+ # Allow /tmp/hermes-* explicitly. Compare resolved roots rather than raw
+ # path parts because macOS resolves /tmp to /private/tmp.
+ try:
+ relative_tmp = resolved.relative_to(Path("/tmp").resolve())
+ except (ValueError, OSError):
+ return False
+ return bool(relative_tmp.parts and relative_tmp.parts[0].startswith("hermes-"))
def _log(message: str) -> None:
diff --git a/tests/plugins/test_disk_cleanup_plugin.py b/tests/plugins/test_disk_cleanup_plugin.py
index 18a58b7d7a3..e55d8338e94 100644
--- a/tests/plugins/test_disk_cleanup_plugin.py
+++ b/tests/plugins/test_disk_cleanup_plugin.py
@@ -16,7 +16,9 @@ import importlib
import itertools
import json
import os
+import shutil
import sys
+import tempfile
from datetime import datetime, timedelta, timezone
from pathlib import Path
@@ -98,6 +100,15 @@ class TestIsSafePath:
p.write_text("x")
assert dg.is_safe_path(p) is True
+ @pytest.mark.macos_only
+ def test_track_accepts_tmp_hermes_path_after_platform_resolution(self, _isolate_env):
+ dg = _load_lib()
+ temporary = Path(tempfile.mkdtemp(prefix="hermes-disk-cleanup-", dir="/tmp"))
+ try:
+ assert dg.track(str(temporary), "temp", silent=True) is True
+ finally:
+ shutil.rmtree(temporary)
+
def test_rejects_outside_hermes_home(self, _isolate_env):
dg = _load_lib()
assert dg.is_safe_path(Path("/etc/passwd")) is False