Skip to content

The agent that grows with you

Merge remote-tracking branch 'origin/main' into ethie/pm-clean

ethernet5a86fe3340ac
diff --cc hermes_constants.py
index 77f2977145f,bec5d17d969..aed0fd55e25
--- a/hermes_constants.py
+++ b/hermes_constants.py
@@@ -815,11 -1130,13 +819,15 @@@ def apply_secure_dir_policy(path, *, ho
win (#117347): managed installs are left exactly as the package manager / activation
script set them (#77579); in a container only an explicit ``HERMES_HOME_MODE`` is applied
(a bind-mounted data dir is often shared with sibling containers, #10757); elsewhere
- ``HERMES_HOME_MODE`` (e.g. ``0701``, ``2770``) overrides the mode. ``HERMES_UID`` /
- ``HERMES_GID`` ownership is applied when those env vars are set (#34107).
+ ``HERMES_HOME_MODE`` (e.g. ``0701``, ``2770``) is passed to the host's ``chmod``. Special
+ bits may be cleared by the host filesystem (macOS commonly clears setgid on directories
+ whose group the caller does not belong to). ``HERMES_UID`` / ``HERMES_GID`` ownership is
+ applied when those env vars are set (#34107).
+ ``home`` (keyword-only: both arguments are path-likes) names the home whose managed-mode
+ marker is read, for callers that already resolved it; without it the effective home is
+ consulted.
+
Import-safe twin of ``hermes_cli.config._secure_dir`` (which delegates here), so callers
outside the CLI package — like :func:`get_scratch_dir` — share one policy implementation.
"""
diff --cc tests/test_scratch_dir.py
index 46bfddf50d7,8f8a3ed1378..87f226b100e
--- a/tests/test_scratch_dir.py
+++ b/tests/test_scratch_dir.py
@@@ -70,20 -70,10 +70,21 @@@ class TestScratchDirPermissionPolicy
"""get_scratch_dir must honor the home permission policy instead of a blanket 0700:
an explicit HERMES_HOME_MODE and a managed/shared home win (#117347)."""
+ @staticmethod
+ def _native_mode_after_chmod(path, requested=0o2770):
+ """Return what this host/filesystem preserves from a real chmod request.
+
+ macOS may clear setgid when the directory group is not one of the caller's groups.
+ The portable contract is to request the operator's mode and preserve every bit the
+ host accepts, not to pretend all POSIX filesystems retain identical special bits.
+ """
+ os.chmod(path, requested)
+ return stat.S_IMODE(os.stat(path).st_mode)
+
def _isolate_env(self, monkeypatch, tmp_path):
- # HERMES_HOME must point somewhere without a .managed marker, or a marker file in the
- # real home would flip every case into "managed" (the marker is read via get_hermes_home).
+ # A known, marker-free effective home: each case below plants `.managed` in the home whose
+ # scratch dir it exercises (`get_scratch_dir(home)` reads the marker there), and the real
+ # home's own marker must not leak into callers that still resolve the effective home.
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "home"))
for var in ("HERMES_HOME_MODE", "HERMES_MANAGED", "HERMES_CONTAINER",
"HERMES_SKIP_CHMOD", "HERMES_UID", "HERMES_GID"):
@@@ -117,11 -104,11 +118,11 @@@
home.mkdir()
self._isolate_env(monkeypatch, tmp_path)
(home / ".managed").write_text("nixos", encoding="utf-8")
- pre = tmp_path / "cache" / "scratch"
+ pre = home / "cache" / "scratch"
pre.mkdir(parents=True)
- os.chmod(pre, 0o2770)
+ expected = self._native_mode_after_chmod(pre)
- scratch = get_scratch_dir(tmp_path, prune=False)
+ scratch = get_scratch_dir(home, prune=False)
- assert stat.S_IMODE(os.stat(scratch).st_mode) == 0o2770
+ assert stat.S_IMODE(os.stat(scratch).st_mode) == expected
def test_empty_managed_marker_counts_as_managed(self, tmp_path, monkeypatch):
# Legacy NixOS module wrote an empty marker; config.get_managed_system treats it as
@@@ -130,11 -117,11 +131,11 @@@
home.mkdir()
self._isolate_env(monkeypatch, tmp_path)
(home / ".managed").write_text("", encoding="utf-8")
- pre = tmp_path / "cache" / "scratch"
+ pre = home / "cache" / "scratch"
pre.mkdir(parents=True)
- os.chmod(pre, 0o2770)
+ expected = self._native_mode_after_chmod(pre)
- scratch = get_scratch_dir(tmp_path, prune=False)
+ scratch = get_scratch_dir(home, prune=False)
- assert stat.S_IMODE(os.stat(scratch).st_mode) == 0o2770
+ assert stat.S_IMODE(os.stat(scratch).st_mode) == expected
def test_unreadable_managed_marker_counts_as_managed(self, tmp_path, monkeypatch):
# A marker that exists but cannot be read (OSError -> "") still counts as managed.
@@@ -142,12 -129,27 +143,27 @@@
home.mkdir()
self._isolate_env(monkeypatch, tmp_path)
(home / ".managed").mkdir()
- pre = tmp_path / "cache" / "scratch"
+ pre = home / "cache" / "scratch"
pre.mkdir(parents=True)
- os.chmod(pre, 0o2770)
+ expected = self._native_mode_after_chmod(pre)
- scratch = get_scratch_dir(tmp_path, prune=False)
+ scratch = get_scratch_dir(home, prune=False)
- assert stat.S_IMODE(os.stat(scratch).st_mode) == 0o2770
+ assert stat.S_IMODE(os.stat(scratch).st_mode) == expected
+ def test_effective_home_marker_does_not_govern_another_homes_scratch(self, tmp_path, monkeypatch):
+ # The caller's home decides the policy. A boot caller (``export_scratch_tmp_env``) and
+ # ``hermes doctor`` have already resolved theirs, so the marker lookup must not fall back
+ # to ``get_hermes_home()``: for a sticky profile with HERMES_HOME unset that lookup warns
+ # about the default profile on every command, and it is the wrong home to judge by anyway.
+ selected = tmp_path / "home"
+ selected.mkdir()
+ self._isolate_env(monkeypatch, tmp_path)
+ (selected / ".managed").write_text("nixos", encoding="utf-8")
+ other = tmp_path / "other"
+ pre = other / "cache" / "scratch"
+ pre.mkdir(parents=True)
+ os.chmod(pre, 0o750)
+ assert stat.S_IMODE(os.stat(get_scratch_dir(other, prune=False)).st_mode) == 0o700
+
def test_canonical_container_signal_without_env_override_keeps_operator_mode(self, tmp_path, monkeypatch):
# Podman/containerd/K8s runtimes often don't export HERMES_CONTAINER; the canonical
# _detect_container breadth (not the narrower legacy signal set) must skip the chmod.