Skip to content

The agent that grows with you

fix(windows): an existing dir an elevated run left is repaired, not an error

Tekniumf92f834a00d6
diff --git a/hermes_dir_acl.py b/hermes_dir_acl.py
index f352160b670..d49537d5390 100644
--- a/hermes_dir_acl.py
+++ b/hermes_dir_acl.py
@@ -315,7 +315,8 @@ def make_user_dir(path: str | Path, mode: int = 0o777, *, parents: bool = False,
Off Windows or unelevated this is ``Path.mkdir`` and nothing else. Elevated, every directory
this call created (missing parents included) and the leaf, even when it already existed (the
repair case), get the user's inheriting full-control ACE once per process, except that a
- component that is a junction/symlink/reparse point stops the grants with one warning."""
+ component that is a junction/symlink/reparse point stops the grants with one warning. An
+ existing leaf without ``exist_ok`` is still granted before the ``FileExistsError``."""
target = Path(path)
if sys.platform != "win32" or not process_is_elevated():
target.mkdir(mode=mode, parents=parents, exist_ok=exist_ok)
@@ -325,18 +326,29 @@ def make_user_dir(path: str | Path, mode: int = 0o777, *, parents: bool = False,
while not probe.exists() and probe.parent != probe:
created.append(probe)
probe = probe.parent
- target.mkdir(mode=mode, parents=parents, exist_ok=exist_ok)
- to_grant = list(reversed(created or [target]))
+ try:
+ target.mkdir(mode=mode, parents=parents, exist_ok=exist_ok)
+ except FileExistsError:
+ # ``Path.mkdir`` contract: an existing leaf still raises without exist_ok. The directory an
+ # earlier elevated run left is repaired first, so the caller's error is never the reason a
+ # non-elevated Hermes stays locked out of it.
+ if target.is_dir() and not is_reparse_point(target):
+ _grant_dirs(target, [target])
+ raise
+ _grant_dirs(target, list(reversed(created or [target])))
+ return target
+
+
+def _grant_dirs(target: Path, to_grant: list[Path]) -> None:
# A junction/symlink planted at (or swapped in for) the leaf, a created component, or any
# existing ancestor inside the home: grant nothing, one warning (nothing past it is in the home).
for component in dict.fromkeys([*_components_below_home(target), *to_grant]):
if is_reparse_point(component):
_warn_refused(component, ReparsePointRefused("junction, symlink or other reparse point"))
- return target
+ return
for directory in to_grant:
if directory not in _granted and grant_user_access(directory):
_granted.add(directory)
- return target
def unusable_dir_remedy(path: str | Path) -> str:
diff --git a/tests/test_hermes_dir_acl.py b/tests/test_hermes_dir_acl.py
index cc828c9aaa2..3dae55ea734 100644
--- a/tests/test_hermes_dir_acl.py
+++ b/tests/test_hermes_dir_acl.py
@@ -38,6 +38,22 @@ def test_elevated_grants_every_created_dir_and_an_existing_leaf_once(tmp_path, m
assert granted[-1] == existing # the repair case: re-grant an existing leaf
+def test_elevated_existing_leaf_without_exist_ok_is_repaired_then_raises(tmp_path, monkeypatch):
+ """Path.mkdir contract stays (FileExistsError), but the left-behind dir is granted first."""
+ granted = []
+ monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
+ monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
+ monkeypatch.setattr(hermes_dir_acl, "grant_user_access", lambda p: granted.append(p) or True)
+ monkeypatch.setattr(hermes_dir_acl, "_granted", set())
+ left = tmp_path / "state"
+ left.mkdir()
+ with pytest.raises(FileExistsError):
+ hermes_dir_acl.make_user_dir(left)
+ assert granted == [left]
+ hermes_dir_acl.make_user_dir(left, parents=True, exist_ok=True)
+ assert granted == [left] # memoized: no second ACE
+
+
def test_a_failed_grant_never_fails_the_mkdir_and_is_retried(tmp_path, monkeypatch):
monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
diff --git a/tests/test_hermes_dir_acl_windows.py b/tests/test_hermes_dir_acl_windows.py
index ca45cd52001..3f149044ac1 100644
--- a/tests/test_hermes_dir_acl_windows.py
+++ b/tests/test_hermes_dir_acl_windows.py
@@ -142,8 +142,8 @@ def test_elevated_rerun_repairs_a_dir_an_earlier_elevated_run_left(reporters_par
left = reporters_parent / "state"
left.mkdir()
before = _child("print(json.dumps({'usable': usable(sys.argv[1])}))", str(left))
- make_user_dir(left)
- make_user_dir(left) # idempotent: no second ACE
+ make_user_dir(left, parents=True, exist_ok=True) # how every production caller re-enters
+ make_user_dir(left, parents=True, exist_ok=True) # idempotent: no second ACE
after = _child("print(json.dumps({'usable': usable(sys.argv[1])}))", str(left))
acl = subprocess.run(["icacls", str(left)], capture_output=True, text=True, check=True).stdout
assert before["usable"] is False and after["usable"] is True, acl