Skip to content

The agent that grows with you

fix(profiles): anchor named-profile detection to real Hermes homes

Teknium35328345d5e3
diff --git a/hermes_constants.py b/hermes_constants.py
index 0e21ecd8090..9c549a82d33 100644
--- a/hermes_constants.py
+++ b/hermes_constants.py
@@ -232,19 +232,60 @@ def get_default_hermes_root() -> Path:
# erase the fact that the profile was deleted.
_DELETED_PROFILES_DIR = ".deleted"
+# Files whose presence marks a directory as a real Hermes home. A fresh home
+# always gains at least one of these on first use (config save, env backfill,
+# session DB), while arbitrary directories that merely contain a ``profiles``
+# path segment (e.g. ``/srv/profiles/buildcache``) do not.
+_HERMES_HOME_MARKERS = ("config.yaml", ".env", "state.db")
+
+
+def _is_hermes_profiles_root(profiles_dir: Path) -> bool:
+ """Return True when *profiles_dir* is a canonical ``<hermes-home>/profiles``.
+
+ Anchors named-profile recognition so it only fires for directories that
+ provably live under a Hermes home: the classic ``~/.hermes`` layout, a
+ root carrying Hermes-home marker files (Docker/custom ``HERMES_HOME``
+ like ``/opt/data``), a ``profiles/.deleted`` tombstone directory (only
+ ever created by ``hermes profile delete``), or the process's resolved
+ default Hermes root.
+ """
+ root = profiles_dir.parent
+ if root.name == ".hermes":
+ return True
+ try:
+ if (profiles_dir / _DELETED_PROFILES_DIR).is_dir():
+ return True
+ if any((root / marker).exists() for marker in _HERMES_HOME_MARKERS):
+ return True
+ except OSError:
+ pass
+ try:
+ return root.resolve(strict=False) == get_default_hermes_root().resolve(
+ strict=False
+ )
+ except OSError:
+ return False
+
def named_profile_home(path: str | Path) -> Path | None:
"""Return ``<root>/profiles/<name>`` when *path* is that home or under it.
A named profile home is only ``.../profiles/<id>`` where ``<id>`` does
- not start with ``.``. A default Hermes home whose path merely contains a
- ``profiles`` segment (e.g. ``/tmp/foo/profiles/notahome/.hermes``) is not
- a named profile. ``.../profiles/worker/logs`` still resolves to
- ``.../profiles/worker``.
+ not start with ``.`` AND the ``profiles`` directory's parent is a real
+ Hermes home (see :func:`_is_hermes_profiles_root`). A default Hermes home
+ whose path merely contains a ``profiles`` segment
+ (e.g. ``/tmp/foo/profiles/notahome/.hermes``) is not a named profile,
+ and neither is an unrelated custom home like
+ ``/srv/profiles/buildcache`` — those must keep mkdir-ing normally.
+ ``.../profiles/worker/logs`` still resolves to ``.../profiles/worker``.
"""
current = Path(path)
for candidate in (current, *current.parents):
- if candidate.parent.name == "profiles" and not candidate.name.startswith("."):
+ if (
+ candidate.parent.name == "profiles"
+ and not candidate.name.startswith(".")
+ and _is_hermes_profiles_root(candidate.parent)
+ ):
return candidate
# Stop at a default Hermes home so a coincidental ``profiles/``
# ancestor is not treated as a named-profile root.
diff --git a/tests/hermes_cli/test_deleted_profile_tombstone.py b/tests/hermes_cli/test_deleted_profile_tombstone.py
index b0fca479adc..fdabf90ae80 100644
--- a/tests/hermes_cli/test_deleted_profile_tombstone.py
+++ b/tests/hermes_cli/test_deleted_profile_tombstone.py
@@ -122,7 +122,9 @@ class TestDeletedProfileTombstone:
profile_dir = create_profile("worker", no_alias=True, no_skills=True)
_delete("worker")
profile_dir.mkdir(parents=True)
- (profile_env / ".hermes" / ".env").write_text("OPENROUTER_API_KEY=root-key\n")
+ (profile_env / ".hermes" / ".env").write_text(
+ "OPENROUTER_API_KEY=root-key\n", encoding="utf-8"
+ )
backfilled = backfill_profile_envs(quiet=True)
@@ -158,11 +160,53 @@ class TestDeletedProfileTombstone:
class TestNamedProfileHome:
def test_logs_under_named_profile_resolve_to_profile_home(self, tmp_path):
+ # tmp_path acts as a real Hermes home (Docker/custom layout): it
+ # carries a home marker file, so profiles/ under it is canonical.
+ (tmp_path / "config.yaml").write_text("{}\n", encoding="utf-8")
worker = tmp_path / "profiles" / "worker"
assert named_profile_home(worker / "logs") == worker
assert named_profile_home(worker) == worker
+ def test_dot_hermes_layout_resolves_without_markers(self, tmp_path):
+ worker = tmp_path / ".hermes" / "profiles" / "worker"
+ assert named_profile_home(worker / "logs") == worker
+ assert named_profile_home(worker) == worker
+
def test_default_home_with_profiles_in_path_is_not_named(self, tmp_path):
default_home = tmp_path / "foo" / "profiles" / "notahome" / ".hermes"
assert named_profile_home(default_home) is None
assert named_profile_home(default_home / "logs") is None
+
+ def test_unrelated_profiles_dir_is_not_named(self, tmp_path):
+ # Review point 1 regression: a custom home like
+ # /srv/profiles/buildcache must NOT be treated as a named profile —
+ # its parent is not a Hermes home, so logging must keep mkdir-ing.
+ custom_home = tmp_path / "srv" / "profiles" / "buildcache"
+ assert named_profile_home(custom_home) is None
+ assert named_profile_home(custom_home / "logs") is None
+
+ def test_unrelated_profiles_dir_still_mkdirs(self, tmp_path):
+ from hermes_constants import mkdir_under_hermes_home
+
+ custom_home = tmp_path / "srv" / "profiles" / "buildcache"
+ log_dir = mkdir_under_hermes_home(custom_home / "logs")
+ assert log_dir.is_dir()
+
+ def test_setup_logging_under_unrelated_profiles_dir_succeeds(self, tmp_path):
+ # End-to-end shape of the point-1 regression: setup_logging on a
+ # non-profile custom home whose path contains a 'profiles' segment
+ # must create the log dir instead of raising FileNotFoundError.
+ custom_home = tmp_path / "srv" / "profiles" / "buildcache"
+ custom_home.mkdir(parents=True)
+ log_dir = setup_logging(hermes_home=custom_home, force=True)
+ assert log_dir == custom_home / "logs"
+ assert log_dir.is_dir()
+
+ def test_tombstone_dir_marks_profiles_root(self, tmp_path):
+ # A profiles/.deleted directory is only ever created by
+ # `hermes profile delete` — its presence alone anchors recognition,
+ # so tombstones are honored even when root markers are missing.
+ profiles_dir = tmp_path / "opt" / "profiles"
+ (profiles_dir / ".deleted").mkdir(parents=True)
+ worker = profiles_dir / "worker"
+ assert named_profile_home(worker / "logs") == worker