Skip to content

The agent that grows with you

fix(update): an operator-linked HERMES_HOME is repaired, and an empty repair stays owed

Teknium5c3331418707
diff --git a/hermes_cli/update_cmd_maint.py b/hermes_cli/update_cmd_maint.py
index ca385ba4b24..35a8c66b619 100644
--- a/hermes_cli/update_cmd_maint.py
+++ b/hermes_cli/update_cmd_maint.py
@@ -43,10 +43,17 @@ def _repair_windows_home_acl() -> None:
from hermes_constants import get_hermes_home
home = get_hermes_home()
- granted, _skipped, failed = grant_tree_user_access(home)
+ # The home itself is the operator's choice and may be a link: pin its real directory and walk
+ # below it (links beneath are still never followed).
+ granted, skipped, failed = grant_tree_user_access(home, pin_root=True)
if failed and not granted:
path, reason = failed[0]
raise RuntimeError(f"could not restore user access to {home}: {path}: {reason}")
+ if not granted:
+ # Nothing took (the root itself was skipped or unreadable): the step stays owed, never a
+ # silent success that leaves stranded state stranded.
+ why = f"{skipped[0]} is a junction, symlink or hard link" if skipped else "nothing was walkable"
+ raise RuntimeError(f"could not restore user access to {home}: nothing was repaired ({why})")
if failed:
# Like icacls /C: keep going, report the first few; the step stays owed only when nothing took.
logger.warning("Restoring user access under %s failed for %d entr%s, e.g. %s: %s", home, len(failed),
diff --git a/hermes_dir_acl.py b/hermes_dir_acl.py
index b97da0e8e3e..954ac4a7d0a 100644
--- a/hermes_dir_acl.py
+++ b/hermes_dir_acl.py
@@ -285,16 +285,20 @@ def iter_tree_without_reparse_points(root: str | Path, skipped: list[Path]) -> I
yield child, False
-def grant_tree_user_access(root: str | Path) -> tuple[int, list[Path], list[tuple[Path, str]]]:
+def grant_tree_user_access(root: str | Path, *, pin_root: bool = False) -> tuple[int, list[Path], list[tuple[Path, str]]]:
"""Recursive :func:`grant_user_access` (the elevated post-update repair): grant the token's user
full control on *root* and every directory and file under it, inheriting on directories.
+ With *pin_root*, *root* is the operator's own choice (``HERMES_HOME``) and may itself be a link:
+ it is resolved once and the walk runs below the real directory. A link BELOW it is still never
+ entered or granted through.
+
Returns ``(granted, skipped, failed)``: *skipped* are the reparse points (and hard-linked files)
that were neither granted nor entered, *failed* the ``(path, reason)`` grants that raised. A
no-op ``(0, [], [])`` off Windows or when not elevated."""
if sys.platform != "win32" or not process_is_elevated():
return 0, [], []
- root = Path(os.path.abspath(root))
+ root = Path(os.path.realpath(root) if pin_root else os.path.abspath(root))
real_root = os.path.realpath(root)
granted, skipped, failed = 0, [], []
for path, is_dir in iter_tree_without_reparse_points(root, skipped):
diff --git a/tests/hermes_cli/test_update_windows_home_acl.py b/tests/hermes_cli/test_update_windows_home_acl.py
index 428c62f02f7..77a588b8a65 100644
--- a/tests/hermes_cli/test_update_windows_home_acl.py
+++ b/tests/hermes_cli/test_update_windows_home_acl.py
@@ -63,3 +63,28 @@ def test_repair_stays_owed_when_nothing_could_be_granted(monkeypatch, tmp_path):
monkeypatch.setattr(hermes_dir_acl, "_grant_with_pywin32", denied)
with pytest.raises(RuntimeError, match="could not restore user access"):
maint._repair_windows_home_acl()
+
+
+def test_repair_walks_below_an_operator_linked_home(monkeypatch, tmp_path):
+ """Review-9 (d): with HERMES_HOME itself a link the tree repair returned (0, [home], []) and the
+ update step reported success having repaired nothing. The home is pinned to its real dir; a link
+ beneath it is still refused."""
+ real = tmp_path / "real-home"
+ (real / "state").mkdir(parents=True)
+ (real / "state" / "s.json").write_text("{}")
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ (real / "jn").symlink_to(outside, target_is_directory=True)
+ granted, _runs = _fake_elevated_windows(monkeypatch, tmp_path)
+ (tmp_path / "home").symlink_to(real, target_is_directory=True)
+ maint._repair_windows_home_acl()
+ assert sorted(granted) == sorted([str(real), str(real / "state"), str(real / "state" / "s.json")])
+
+
+def test_repair_that_repaired_nothing_stays_owed(monkeypatch, tmp_path):
+ import pytest
+
+ _fake_elevated_windows(monkeypatch, tmp_path)
+ (tmp_path / "home").symlink_to(tmp_path / "gone", target_is_directory=True) # dangling
+ with pytest.raises(RuntimeError, match="nothing was repaired"):
+ maint._repair_windows_home_acl()