Skip to content

The agent that grows with you

fix: python -m hermes_cli.main --profile default no longer lands in the sticky profile

Teknium2bd5261d8a3c
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index f682f57e94b..03f6c60555f 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -662,6 +662,10 @@ def _apply_profile_override() -> None:
sys.argv = sys.argv[:start] + sys.argv[start + consume :]
+if __name__ == "__main__" and getattr(sys.modules.get("__main__"), "__file__", None) == __file__:
+ # `python -m`: a later `import hermes_cli.main` reuses THIS module; a second copy would re-run the
+ # override on the stripped argv, and `--profile default` would fall to the sticky profile.
+ sys.modules.setdefault("hermes_cli.main", sys.modules["__main__"])
_apply_profile_override()
# ``-p``/active_profile re-homed the process after hermes_bootstrap ran: re-point the temp vars
# at THIS home's scratch dir (a user-set TMPDIR is still left alone).
diff --git a/tests/hermes_cli/test_apply_profile_override.py b/tests/hermes_cli/test_apply_profile_override.py
index 5ab10c00a30..4f44bd15a0a 100644
--- a/tests/hermes_cli/test_apply_profile_override.py
+++ b/tests/hermes_cli/test_apply_profile_override.py
@@ -396,3 +396,22 @@ class TestS6ContainerGatewayRun:
tmp_path, monkeypatch, hermes_home=str(root), active_profile="coder", argv=argv,
)
assert result == str(root / "profiles" / "coder"), argv
+
+
+def test_module_entry_keeps_an_explicit_default_profile_over_the_sticky_one(tmp_path):
+ """`python -m hermes_cli.main --profile default` (how Hermes spawns its own CLI) stays on the root
+ home while active_profile names another profile: a second import of the module must not re-read
+ the stripped argv and fall to the sticky profile."""
+ import subprocess
+ root = tmp_path / ".hermes"
+ (root / "profiles" / "work").mkdir(parents=True)
+ (root / "profiles" / "work" / "config.yaml").write_text("{}\n", encoding="utf-8")
+ (root / "active_profile").write_text("work\n", encoding="utf-8")
+ env = {k: v for k, v in os.environ.items() if not k.startswith("HERMES_")}
+ env.update(HOME=str(tmp_path), USERPROFILE=str(tmp_path), HERMES_HOME=str(root),
+ PYTHONPATH=str(Path(__file__).resolve().parents[2]))
+
+ out = subprocess.run([sys.executable, "-m", "hermes_cli.main", "--profile", "default", "config", "path"],
+ env=env, capture_output=True, text=True, timeout=120, check=True).stdout
+
+ assert Path(out.strip().splitlines()[-1]).resolve() == (root / "config.yaml").resolve()