The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test: refuse changing the developer's real installed Hermes desktop app
kshitijk4poor4c014e440766
diff --git a/tests/conftest.py b/tests/conftest.pyindex 3006008d8eb..0e53c897ab5 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -1389,6 +1389,14 @@ def _capture_real_hermes_root() -> list[Path]:_REAL_HERMES_ROOT_CANDIDATES = _capture_real_hermes_root()+# Captured before any test can patch sys.platform, HOME or XDG_*: a test that runs the real+# GUI uninstall or update swap would otherwise delete the developer's own Hermes app. Only the+# ones present (none on CI runners, so the guard costs nothing there), each literal and resolved.+from hermes_cli.gui_uninstall import packaged_gui_app_paths # noqa: E402++_REAL_INSTALLED_GUI_APPS = sorted({+ os.path.normcase(form) for app in packaged_gui_app_paths() if os.path.lexists(app)+ for form in (os.path.abspath(app), os.path.realpath(app))})@pytest.fixture(autouse=True)@@ -1402,7 +1410,7 @@ def _forbid_real_hermes_home_io(monkeypatch, request):returnfrom tests.home_io_guard import HomeIOGuard- HomeIOGuard(lambda: _REAL_HERMES_ROOT_CANDIDATES).install(monkeypatch)+ HomeIOGuard(lambda: _REAL_HERMES_ROOT_CANDIDATES, lambda: _REAL_INSTALLED_GUI_APPS).install(monkeypatch)@pytest.fixturediff --git a/tests/hermes_cli/test_real_home_tripwire.py b/tests/hermes_cli/test_real_home_tripwire.pyindex de1ae67d97d..c6d5d10fbbc 100644--- a/tests/hermes_cli/test_real_home_tripwire.py+++ b/tests/hermes_cli/test_real_home_tripwire.py@@ -82,6 +82,26 @@ def test_rename_cannot_overwrite_a_protected_destination(protected_home, tmp_patassert source.read_text(encoding="utf-8") == "external"+def test_installed_desktop_app_can_be_read_but_never_changed(tmp_path, monkeypatch):+ from tests import conftest++ app = tmp_path / "Applications" / "Hermes.app"+ (app / "Contents").mkdir(parents=True)+ plist = app / "Contents" / "Info.plist"+ plist.write_text("unchanged", encoding="utf-8")+ replacement = tmp_path / "Hermes.app.new"+ replacement.mkdir()+ (tmp_path / "linked").symlink_to(app.parent, target_is_directory=True)+ monkeypatch.setattr(conftest, "_REAL_INSTALLED_GUI_APPS", [app])+ assert plist.read_text(encoding="utf-8") == "unchanged"+ for change in (lambda: shutil.rmtree(app), plist.unlink, lambda: replacement.replace(app),+ lambda: plist.write_text("clobbered", encoding="utf-8"),+ lambda: shutil.rmtree(tmp_path / "linked" / "Hermes.app")):+ with pytest.raises(pytest.fail.Exception, match="REAL installed Hermes desktop app"):+ change()+ assert plist.read_text(encoding="utf-8") == "unchanged"++def test_unprotected_paths_and_open_descriptors_still_work(protected_home, tmp_path):target = tmp_path / "allowed" / "file.txt"target.parent.mkdir()diff --git a/tests/home_io_guard.py b/tests/home_io_guard.pyindex a047815ab4a..4c5d915fa91 100644--- a/tests/home_io_guard.py+++ b/tests/home_io_guard.py@@ -47,12 +47,16 @@ def _contains(path: str, prefix: str) -> bool:class HomeIOGuard:- def __init__(self, roots):+ def __init__(self, roots, installed_apps=lambda: ()):self.roots = roots+ # The machine's installed desktop app (``packaged_gui_app_paths()`` before any test ran):+ # deleting, replacing or writing into it is refused. Reads stay allowed: several update+ # paths only probe it.+ self.installed_apps = installed_appsself.checking = threading.local()self.directories: dict[int, Path] = {}- def check(self, value, *, dir_fd=None, metadata=False):+ def check(self, value, *, dir_fd=None, metadata=False, destructive=False):if value is None or isinstance(value, int) or getattr(self.checking, "active", False):returnself.checking.active = True@@ -76,6 +80,8 @@ class HomeIOGuard:# which is not I/O against the home.if metadata and (absolute == "/proc" or absolute.startswith("/proc" + os.sep)):return+ if destructive:+ self._refuse_installed_app_change(value, absolute)roots = tuple(_normcase(os.fspath(r)) for r in self.roots())# Resolving the root itself (get_default_hermes_root's relative_to# probe) reads no state; only its contents are guarded.@@ -113,6 +119,19 @@ class HomeIOGuard:finally:self.checking.active = False+ def _refuse_installed_app_change(self, value, absolute):+ apps = [_normcase(os.path.abspath(app)) for app in self.installed_apps()]+ if not apps:+ return+ paths = (absolute, _normcase(os.path.realpath(absolute)))+ for app in apps:+ if any(_within(path, app) for path in paths):+ import pytest # noqa: PLC0415+ # pytest.fail, not AssertionError: removal helpers catch Exception and would log+ # the refusal as a warning while the test passed.+ pytest.fail(f"TEST BUG: changing the REAL installed Hermes desktop app: {value}\n"+ "Stub hermes_cli.gui_uninstall.packaged_gui_app_paths to a temporary path.")+@staticmethoddef refuse(value):raise AssertionError(@@ -135,36 +154,41 @@ class HomeIOGuard:)def install(self, monkeypatch):- def wrap(module, name, parameters, *, metadata=False):+ def wrap(module, name, parameters, *, metadata=False, destructive=False):original = getattr(module, name)@wraps(original)def guarded(*args, **kwargs):for index, (parameter, descriptor) in enumerate(parameters):value = args[index] if index < len(args) else kwargs.get(parameter)- self.check(value, dir_fd=kwargs.get(descriptor) if descriptor else None, metadata=metadata)+ self.check(value, dir_fd=kwargs.get(descriptor) if descriptor else None, metadata=metadata,+ destructive=destructive(args, kwargs) if callable(destructive) else destructive)return original(*args, **kwargs)monkeypatch.setattr(module, name, guarded)+ def open_writes(args, kwargs):+ mode = args[1] if len(args) > 1 else kwargs.get("mode", "r")+ return any(flag in mode for flag in "wax+")+for module in (builtins, io):- wrap(module, "open", (("file", None),))+ wrap(module, "open", (("file", None),), destructive=open_writes)for name in ("mkdir", "unlink", "remove", "rmdir", "chmod", "utime"):- wrap(os, name, (("path", "dir_fd"),))+ wrap(os, name, (("path", "dir_fd"),), destructive=name != "mkdir")for name in ("stat", "lstat", "readlink", "access"):wrap(os, name, (("path", "dir_fd"),), metadata=True)for name in ("makedirs", "listdir", "scandir"):wrap(os, name, (("name" if name == "makedirs" else "path", None),))for name in ("rename", "replace"):- wrap(os, name, (("src", "src_dir_fd"), ("dst", "dst_dir_fd")))- wrap(shutil, "rmtree", (("path", "dir_fd"),))+ wrap(os, name, (("src", "src_dir_fd"), ("dst", "dst_dir_fd")), destructive=True)+ wrap(shutil, "rmtree", (("path", "dir_fd"),), destructive=True)wrap(sqlite3, "connect", (("database", None),))original_open, original_close = os.open, os.close@wraps(original_open)def guarded_open(path, flags, *args, **kwargs):- self.check(path, dir_fd=kwargs.get("dir_fd"))+ self.check(path, dir_fd=kwargs.get("dir_fd"), destructive=bool(flags & (os.O_WRONLY | os.O_RDWR | os.O_CREAT | os.O_TRUNC)))fd = original_open(path, flags, *args, **kwargs)candidate = Path(os.fsdecode(path))if kwargs.get("dir_fd") is not None and not candidate.is_absolute():