Skip to content

The agent that grows with you

fix: make macOS shell and mode contracts portable

ethernet75a13386fb57
diff --git a/hermes_constants.py b/hermes_constants.py
index d6079f0f5f4..01143432613 100644
--- a/hermes_constants.py
+++ b/hermes_constants.py
@@ -810,8 +810,10 @@ def apply_secure_dir_policy(path) -> None:
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).
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 --git a/scripts/termux/build_builder_image.sh b/scripts/termux/build_builder_image.sh
index 901b9e4bff6..dedc0a56326 100755
--- a/scripts/termux/build_builder_image.sh
+++ b/scripts/termux/build_builder_image.sh
@@ -24,7 +24,8 @@ identity = sys.argv[1].encode() + b"\0" + pathlib.Path(sys.argv[2]).read_bytes()
print(hashlib.sha256(identity).hexdigest()[:12])
' "$BASE" "$DOCKERFILE")"
REGISTRY="ghcr.io"
-OWNER="${GITHUB_REPOSITORY_OWNER,,}"
+# macOS still ships Bash 3.2, which predates ${value,,} case conversion.
+OWNER="$(printf '%s' "$GITHUB_REPOSITORY_OWNER" | tr '[:upper:]' '[:lower:]')"
IMAGE="${REGISTRY}/${OWNER}/hermes-termux-builder:${SHORT}"
if docker manifest inspect "$IMAGE" >/dev/null 2>&1; then
diff --git a/tests/scripts/test_termux_build_driver.py b/tests/scripts/test_termux_build_driver.py
index c967431dcec..16fed411850 100644
--- a/tests/scripts/test_termux_build_driver.py
+++ b/tests/scripts/test_termux_build_driver.py
@@ -197,7 +197,7 @@ def test_builder_image_identity_covers_all_inputs(tmp_path, changed_input):
# Only inspect an already-published image; any build/push call is a failure.
command = 'docker() { [[ "$1 $2" == "manifest inspect" ]]; }; export -f docker; bash "$1"'
env = {**os.environ, "PATH": f"{tools}{os.pathsep}{os.environ['PATH']}",
- "GITHUB_REPOSITORY_OWNER": "fixture"}
+ "GITHUB_REPOSITORY_OWNER": "FiXtUrE"}
def image():
result = subprocess.run(["bash", "-c", command, "fixture", str(scripts / "build_builder_image.sh")],
@@ -206,6 +206,7 @@ def test_builder_image_identity_covers_all_inputs(tmp_path, changed_input):
return result.stdout.splitlines()[-1]
original = image()
+ assert original.startswith("ghcr.io/fixture/hermes-termux-builder:")
assert image() == original
changed = scripts / "termux-builder.Dockerfile" if changed_input == "Dockerfile" else repo / "pm/lock.json"
before = changed.read_bytes()
diff --git a/tests/test_scratch_dir.py b/tests/test_scratch_dir.py
index 0aa5ae93495..2c6bf95fa2c 100644
--- a/tests/test_scratch_dir.py
+++ b/tests/test_scratch_dir.py
@@ -63,6 +63,17 @@ 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).
@@ -76,20 +87,23 @@ class TestScratchDirPermissionPolicy:
scratch = get_scratch_dir(tmp_path, prune=False)
assert stat.S_IMODE(os.stat(scratch).st_mode) == 0o700
- def test_explicit_home_mode_retained_with_setgid(self, tmp_path, monkeypatch):
+ def test_explicit_home_mode_uses_native_chmod_semantics(self, tmp_path, monkeypatch):
self._isolate_env(monkeypatch, tmp_path)
+ probe = tmp_path / "mode-probe"
+ probe.mkdir()
+ expected = self._native_mode_after_chmod(probe)
monkeypatch.setenv("HERMES_HOME_MODE", "2770")
scratch = get_scratch_dir(tmp_path, prune=False)
- assert stat.S_IMODE(os.stat(scratch).st_mode) == 0o2770
+ assert stat.S_IMODE(os.stat(scratch).st_mode) == expected
def test_managed_env_leaves_preexisting_mode_untouched(self, tmp_path, monkeypatch):
self._isolate_env(monkeypatch, tmp_path)
monkeypatch.setenv("HERMES_MANAGED", "nixos")
pre = tmp_path / "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)
- assert stat.S_IMODE(os.stat(scratch).st_mode) == 0o2770
+ assert stat.S_IMODE(os.stat(scratch).st_mode) == expected
def test_managed_marker_file_leaves_preexisting_mode_untouched(self, tmp_path, monkeypatch):
home = tmp_path / "home"
@@ -98,9 +112,9 @@ class TestScratchDirPermissionPolicy:
(home / ".managed").write_text("nixos", encoding="utf-8")
pre = tmp_path / "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)
- 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
@@ -111,9 +125,9 @@ class TestScratchDirPermissionPolicy:
(home / ".managed").write_text("", encoding="utf-8")
pre = tmp_path / "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)
- 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.
@@ -123,9 +137,9 @@ class TestScratchDirPermissionPolicy:
(home / ".managed").mkdir()
pre = tmp_path / "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)
- assert stat.S_IMODE(os.stat(scratch).st_mode) == 0o2770
+ assert stat.S_IMODE(os.stat(scratch).st_mode) == expected
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
@@ -138,13 +152,16 @@ class TestScratchDirPermissionPolicy:
scratch = get_scratch_dir(tmp_path, prune=False)
assert stat.S_IMODE(os.stat(scratch).st_mode) == 0o750
- def test_repeated_calls_do_not_strip_setgid(self, tmp_path, monkeypatch):
+ def test_repeated_calls_do_not_strip_filesystem_supported_mode_bits(self, tmp_path, monkeypatch):
self._isolate_env(monkeypatch, tmp_path)
+ probe = tmp_path / "mode-probe"
+ probe.mkdir()
+ expected = self._native_mode_after_chmod(probe)
monkeypatch.setenv("HERMES_HOME_MODE", "2770")
first = get_scratch_dir(tmp_path, prune=False)
second = get_scratch_dir(tmp_path, prune=False)
assert first == second
- assert stat.S_IMODE(os.stat(second).st_mode) == 0o2770
+ assert stat.S_IMODE(os.stat(second).st_mode) == expected
def test_container_keeps_operator_mode_but_honors_explicit(self, tmp_path, monkeypatch):
self._isolate_env(monkeypatch, tmp_path)
@@ -153,8 +170,11 @@ class TestScratchDirPermissionPolicy:
pre.mkdir(parents=True)
os.chmod(pre, 0o750)
assert stat.S_IMODE(os.stat(get_scratch_dir(tmp_path, prune=False)).st_mode) == 0o750
+ probe = tmp_path / "mode-probe"
+ probe.mkdir()
+ expected = self._native_mode_after_chmod(probe)
monkeypatch.setenv("HERMES_HOME_MODE", "2770")
- assert stat.S_IMODE(os.stat(get_scratch_dir(tmp_path, prune=False)).st_mode) == 0o2770
+ assert stat.S_IMODE(os.stat(get_scratch_dir(tmp_path, prune=False)).st_mode) == expected
def test_hermes_uid_gid_applied_to_scratch(self, tmp_path, monkeypatch):
self._isolate_env(monkeypatch, tmp_path)