The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(update): the windows_home_acl step lives in hermes_cli/update_windows_acl.py
Tekniumada0d8031fb6
diff --git a/hermes_cli/update_cmd_maint.py b/hermes_cli/update_cmd_maint.pyindex 26e6b1db851..d865d00419c 100644--- a/hermes_cli/update_cmd_maint.py+++ b/hermes_cli/update_cmd_maint.py@@ -21,45 +21,6 @@ from datetime import UTClogger = logging.getLogger("hermes_cli.update_cmd")-def _repair_windows_home_acl(*, elevated: bool | None = None) -> None:- """Restore access for the filtered UAC token after an elevated update.-- An elevated process can create home-tree entries whose DACL only grants- Administrators and SYSTEM. Administrators is deny-only in the normal UAC- token, so grant the actual user SID explicitly on every entry under the home.-- The walk never descends into or grants through a junction, symlink or hard link- (``hermes_dir_acl.grant_tree_user_access``): ``icacls /T`` follows junctions, so a- same-user junction inside the home would have turned the elevated grant into full- control of e.g. ``C:\\Program Files\\X``.-- *elevated* is the host fact (default: probed; False on every host but an elevated Windows one).- """- from hermes_dir_acl import grant_tree_user_access, process_is_elevated- if not (process_is_elevated() if elevated is None else elevated):- # Only an elevated update leaves Administrators-only entries, and only an elevated token- # can rewrite them; a recursive grant over the whole home on every update costs minutes.- return- from hermes_constants import get_hermes_home-- home = get_hermes_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, elevated=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),- "y" if len(failed) == 1 else "ies", failed[0][0], failed[0][1])--def _prepare_updated_checkout(project_root: Path, *, desktop: bool) -> None:"""Historical updater hook: never complete inside the pre-swap interpreter."""from hermes_cli._old_updater import stop_for_relaunch@@ -1015,7 +976,8 @@ def _run_post_update_maintenance(followups.append((name, reason))from hermes_cli.update_cmd import _check_and_apply_config_migration, _m- owed_step("windows_home_acl", _repair_windows_home_acl)+ from hermes_cli.update_windows_acl import repair_windows_home_acl+ owed_step("windows_home_acl", repair_windows_home_acl)# macOS TCC: Desktop bundles are re-signed each update, so old grants can go stale# (toggle ON, yet macOS re-prompts with no Allow button). Tell users how to re-grant.# With the post-#73681 identifier-pinned DR, new grants survive rebuilds — but a grant made to a pre-fixdiff --git a/hermes_cli/update_windows_acl.py b/hermes_cli/update_windows_acl.pynew file mode 100644index 00000000000..b5278e456df--- /dev/null+++ b/hermes_cli/update_windows_acl.py@@ -0,0 +1,45 @@+"""``hermes update``'s owed ``windows_home_acl`` step: after an elevated update, give the same+user back access to the home tree that run may have left Administrators-only (#139870, #134889 W8).+Split from ``update_cmd_maint.py``."""+import logging++logger = logging.getLogger("hermes_cli.update_cmd")+++def repair_windows_home_acl(*, elevated: bool | None = None) -> None:+ """Restore access for the filtered UAC token after an elevated update.++ An elevated process can create home-tree entries whose DACL only grants+ Administrators and SYSTEM. Administrators is deny-only in the normal UAC+ token, so grant the actual user SID explicitly on every entry under the home.++ The walk never descends into or grants through a junction, symlink or hard link+ (``hermes_dir_acl.grant_tree_user_access``): ``icacls /T`` follows junctions, so a+ same-user junction inside the home would have turned the elevated grant into full+ control of e.g. ``C:\\Program Files\\X``.++ *elevated* is the host fact (default: probed; False on every host but an elevated Windows one).+ """+ from hermes_dir_acl import grant_tree_user_access, process_is_elevated+ if not (process_is_elevated() if elevated is None else elevated):+ # Only an elevated update leaves Administrators-only entries, and only an elevated token+ # can rewrite them; a recursive grant over the whole home on every update costs minutes.+ return+ from hermes_constants import get_hermes_home++ home = get_hermes_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, elevated=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),+ "y" if len(failed) == 1 else "ies", failed[0][0], failed[0][1])diff --git a/tests/hermes_cli/test_update_post_commit_followups.py b/tests/hermes_cli/test_update_post_commit_followups.pyindex 6d4112394dd..0d0d339844b 100644--- a/tests/hermes_cli/test_update_post_commit_followups.py+++ b/tests/hermes_cli/test_update_post_commit_followups.py@@ -133,7 +133,9 @@ def test_failed_config_migration_is_owed_and_later_maintenance_runs(tmp_path, mocalls = []monkeypatch.setenv("HERMES_HOME", str(tmp_path))- for name in ("_repair_windows_home_acl", "_verify_and_restore_state_dbs_post_update", "_invalidate_live_plugin_catalog_caches",+ from hermes_cli import update_windows_acl+ monkeypatch.setattr(update_windows_acl, "repair_windows_home_acl", lambda: calls.append("repair_windows_home_acl"))+ for name in ("_verify_and_restore_state_dbs_post_update", "_invalidate_live_plugin_catalog_caches","_print_bundled_skills_sync_report", "_sync_profiles_after_update"):monkeypatch.setattr(maint, name, lambda name=name: calls.append(name))monkeypatch.setattr("hermes_cli.gitlock.fetch_full_commit_graph", lambda *a, **kw: False)@@ -151,7 +153,7 @@ def test_failed_config_migration_is_owed_and_later_maintenance_runs(tmp_path, moassume_yes=True, gateway_mode=False, pre_update_snapshot_id=None,had_desktop_app_before_update=False, pre_update_version=None, followups=owed) is Trueassert [step for step, _ in owed] == ["config_migration"]- assert calls[0] == "_repair_windows_home_acl"+ assert calls[0] == "repair_windows_home_acl"assert calls[-2:] == ["verdict", "notices"]assert "⚠ Update follow-up 'config_migration'" in capsys.readouterr().outupdate_receipt.finalize_update_receipt("success")diff --git a/tests/hermes_cli/test_update_windows_home_acl.py b/tests/hermes_cli/test_update_windows_home_acl.pyindex fd76dab9440..2ad370c4a58 100644--- a/tests/hermes_cli/test_update_windows_home_acl.py+++ b/tests/hermes_cli/test_update_windows_home_acl.py@@ -6,7 +6,7 @@ import subprocessimport pytestimport hermes_dir_acl-from hermes_cli import update_cmd_maint as maint+from hermes_cli import update_windows_acl as maintdef _fake_elevated(monkeypatch, tmp_path, elevated=True):@@ -30,10 +30,10 @@ def test_repair_runs_only_from_an_elevated_update(monkeypatch, tmp_path):the recursive grant over the whole home is skipped there."""(tmp_path / "home").mkdir()granted, runs = _fake_elevated(monkeypatch, tmp_path, elevated=False)- maint._repair_windows_home_acl() # the production call: probed, unelevated- maint._repair_windows_home_acl(elevated=False)+ maint.repair_windows_home_acl() # the production call: probed, unelevated+ maint.repair_windows_home_acl(elevated=False)assert granted == [] and runs == []- maint._repair_windows_home_acl(elevated=True)+ maint.repair_windows_home_acl(elevated=True)assert granted == [str(tmp_path / "home")]@@ -46,7 +46,7 @@ def test_repair_never_recurses_through_a_link_inside_the_home(monkeypatch, tmp_p(outside / "bin").mkdir(parents=True)(home / "cache" / "images").symlink_to(outside, target_is_directory=True)granted, runs = _fake_elevated(monkeypatch, tmp_path)- maint._repair_windows_home_acl()+ maint.repair_windows_home_acl()assert not any(argv and argv[0] == "icacls" and "/T" in argv for argv in runs), runsassert granted == [str(home), str(home / "cache")]@@ -60,7 +60,7 @@ 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()+ maint.repair_windows_home_acl()def test_repair_walks_below_an_operator_linked_home(monkeypatch, tmp_path):@@ -75,7 +75,7 @@ def test_repair_walks_below_an_operator_linked_home(monkeypatch, tmp_path):(real / "jn").symlink_to(outside, target_is_directory=True)granted, _runs = _fake_elevated(monkeypatch, tmp_path)(tmp_path / "home").symlink_to(real, target_is_directory=True)- maint._repair_windows_home_acl()+ maint.repair_windows_home_acl()assert sorted(granted) == sorted([str(real), str(real / "state"), str(real / "state" / "s.json")])@@ -83,4 +83,4 @@ def test_repair_that_repaired_nothing_stays_owed(monkeypatch, tmp_path):_fake_elevated(monkeypatch, tmp_path)(tmp_path / "home").symlink_to(tmp_path / "gone", target_is_directory=True) # danglingwith pytest.raises(RuntimeError, match="nothing was repaired"):- maint._repair_windows_home_acl()+ maint.repair_windows_home_acl()diff --git a/tests/test_hermes_dir_acl_windows.py b/tests/test_hermes_dir_acl_windows.pyindex ce980563220..4189dc7600b 100644--- a/tests/test_hermes_dir_acl_windows.py+++ b/tests/test_hermes_dir_acl_windows.py@@ -296,7 +296,7 @@ def _dacls(paths):def test_elevated_grants_never_reach_a_junction_target_outside_the_home(home_with_junction_out):import hermes_dir_acl- from hermes_cli import update_cmd_maint+ from hermes_cli import update_windows_aclhome, outside, junction = home_with_junction_outassert hermes_dir_acl.is_reparse_point(junction)@@ -307,7 +307,7 @@ def test_elevated_grants_never_reach_a_junction_target_outside_the_home(home_withermes_dir_acl.make_user_dir(junction / "thumbs", parents=True, exist_ok=True) # through the junctionassert hermes_dir_acl.grant_user_access(junction) is Falsegranted, skipped, failed = hermes_dir_acl.grant_tree_user_access(home)- update_cmd_maint._repair_windows_home_acl() # the post-update repair that used icacls /T+ update_windows_acl.repair_windows_home_acl() # the post-update repair that used icacls /Tassert junction in skipped and not failed and granted >= 2assert _dacls(watched) == before, "an elevated grant followed the junction out of the home"# The real entries in the home still got the grant.