Skip to content

The agent that grows with you

fix(constants): a boot reader that resolved its own home must not warn about the sticky profile

helingjun0e1c0a5a193c
diff --git a/hermes_constants.py b/hermes_constants.py
index 79ed5e16ec3..1dd846884d3 100644
--- a/hermes_constants.py
+++ b/hermes_constants.py
@@ -1051,13 +1051,17 @@ _IGNORED_MANAGED_VALUES = frozenset({"brew", "homebrew"})
_MANAGED_FALSE_VALUES = frozenset({"false", "0", "no", "off"})
-def get_managed_system() -> str | None:
+def get_managed_system(home: str | Path | None = None) -> str | None:
"""Return the package manager owning this install, if any.
Signals: HERMES_MANAGED env var (systemd service) or a ``.managed`` marker file in
HERMES_HOME (NixOS activation script — interactive shells don't see the service env).
- An unreadable or empty marker still counts as managed (the legacy NixOS shape)."""
+ An unreadable or empty marker still counts as managed (the legacy NixOS shape).
+
+ ``home`` names the home whose marker file is read, for callers that already resolved it
+ (:func:`apply_secure_dir_policy` at boot, before ``--profile`` re-homes the process).
+ Defaults to the effective home."""
marker = os.getenv("HERMES_MANAGED", "").strip().lower() or None
- managed_marker = get_hermes_home() / ".managed"
+ managed_marker = (Path(home) if home else get_hermes_home()) / ".managed"
if marker is None and managed_marker.exists():
try:
marker = managed_marker.read_text(encoding="utf-8", errors="replace").strip().lower()
@@ -1119,7 +1123,7 @@ def _chown_to_hermes_uid(path) -> None:
pass
-def apply_secure_dir_policy(path) -> None:
+def apply_secure_dir_policy(path, home: str | Path | None = None) -> None:
"""Apply the canonical Hermes home-directory permission policy to *path*.
Owner-only ``0700`` by default, but the operator's explicit and managed sharing choices
@@ -1129,10 +1133,13 @@ def apply_secure_dir_policy(path) -> None:
``HERMES_HOME_MODE`` (e.g. ``0701``, ``2770``) overrides the mode. ``HERMES_UID`` /
``HERMES_GID`` ownership is applied when those env vars are set (#34107).
+ ``home`` is the home the managed-mode marker is read from, 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.
"""
- if get_managed_system() is not None:
+ if get_managed_system(home) is not None:
return
explicit_mode = os.environ.get("HERMES_HOME_MODE", "").strip()
if _container_or_chmod_skipped() and not explicit_mode:
@@ -1165,7 +1172,9 @@ def get_scratch_dir(home: str | Path | None = None, *, prune: bool = True) -> Pa
try:
scratch.mkdir(parents=True, exist_ok=True)
if sys.platform != "win32":
- apply_secure_dir_policy(scratch)
+ # The caller's home decides the policy: re-reading the effective home here would
+ # warn about a profile the CLI has not switched to yet (boot scratch setup).
+ apply_secure_dir_policy(scratch, home=base)
except OSError:
pass
if prune:
diff --git a/tests/test_hermes_home_profile_warning.py b/tests/test_hermes_home_profile_warning.py
index 5ece51bc5c3..977010e0eea 100644
--- a/tests/test_hermes_home_profile_warning.py
+++ b/tests/test_hermes_home_profile_warning.py
@@ -92,3 +92,47 @@ class TestGetHermesHomeProfileWarning:
# Shouldn't crash; shouldn't warn either (can't tell what profile was intended)
assert "HERMES_HOME fallback" not in capsys.readouterr().err
+
+class TestBootReadersBeforeProfileOverride:
+ """Readers that run before the CLI applies the sticky ``active_profile`` must not warn.
+
+ ``hermes_bootstrap`` points ``TMPDIR`` at the scratch dir of the *process* home during
+ import, and ``main._apply_profile_override`` re-homes the process a few lines later. A
+ caller that already resolved its home must not send the policy back through
+ ``get_hermes_home()``: for a sticky-profile user with ``HERMES_HOME`` unset in a plain
+ shell that lookup falls back to the default profile and warns, on every ``hermes``
+ command, while nothing lands in the wrong place. Same fix the parser's ``_cfg_path()``
+ carries for the ``--no-config`` help string.
+ """
+
+ def test_scratch_export_uses_the_process_home_silently(
+ self, fresh_constants, tmp_path, capsys
+ ):
+ """Boot scratch setup: silent, and pointed at the home the bootstrap resolved."""
+ hermes_dir = tmp_path / ".hermes"
+ (hermes_dir / "profiles" / "coder").mkdir(parents=True)
+ (hermes_dir / "active_profile").write_text("coder\n")
+ capsys.readouterr() # drop anything the setup above printed
+
+ env = {"PATH": "/usr/bin:/bin"}
+ assert fresh_constants.apply_scratch_tmp_env(env) is True
+
+ assert env["TMPDIR"] == str(hermes_dir / "cache" / "scratch")
+ assert "HERMES_HOME fallback" not in capsys.readouterr().err
+
+ def test_explicit_home_scratch_dir_never_reads_the_effective_home(
+ self, fresh_constants, tmp_path, capsys
+ ):
+ """A caller that passes a home (`hermes doctor` for another profile) stays silent."""
+ hermes_dir = tmp_path / ".hermes"
+ profile_dir = hermes_dir / "profiles" / "coder"
+ profile_dir.mkdir(parents=True)
+ (hermes_dir / "active_profile").write_text("coder\n")
+ capsys.readouterr() # drop anything the setup above printed
+
+ scratch = fresh_constants.get_scratch_dir(profile_dir)
+
+ assert scratch == profile_dir / "cache" / "scratch"
+ assert scratch.is_dir()
+ assert "HERMES_HOME fallback" not in capsys.readouterr().err
+