Skip to content

The agent that grows with you

fix(profiles): a profiles/ dir without an identity file is not a profile

teknium119e984ae2a61
diff --git a/hermes_cli/AGENTS.md b/hermes_cli/AGENTS.md
index 248f2de50dd..a65e01a2fdb 100644
--- a/hermes_cli/AGENTS.md
+++ b/hermes_cli/AGENTS.md
@@ -164,8 +164,11 @@ and TUI all go through it). Clones are built in `profiles/.<name>.staging-<pid>`
`_iter_named_profile_dirs` and the hot-serve rescan) and published by one `os.rename` after the strip;
symlinked `.env`/`config.yaml` are materialized first so a clone never writes through to its source. Multiplex
(`gateway.multiplex_profiles`) secret-scope rules: `gateway/AGENTS.md`. The served set is
-`profiles.py::profiles_to_serve(multiplex=True)` = default + every live (non-tombstoned) dir under
-`profiles/` — there is no allowlist (`gateway.multiplex_profile_allowlist` was retired in config v43).
+`profiles.py::profiles_to_serve(multiplex=True)` = default + every live dir under `profiles/` — live =
+carries an identity marker (`hermes_constants.named_profile_has_identity`: `config.yaml`/`.env`/`SOUL.md`/
+`profile.yaml`/`auth.json`/`state.db`) and is not tombstoned. A marker-less dir (cron/log side-effect
+shell, stray infrastructure dir) is never listed, served, ticked, `.env`-backfilled or resolvable via `-p`
+(#95188, #99392); `profile create` may replace it. There is no allowlist (`gateway.multiplex_profile_allowlist` was retired in config v43).
Enumeration is a pure read: never `mkdir` a profile home from a served path (`SessionDB`, logging,
cron all go through `mkdir_under_hermes_home` / `_ensure_cron_dir`, which refuse a deleted or
missing named profile, #94590). Process-global per-profile slots (MCP discovery in `mcp_startup.py`,
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.py
index 95434c8b0f8..2c851278d44 100644
--- a/hermes_cli/profiles.py
+++ b/hermes_cli/profiles.py
@@ -18,7 +18,8 @@ from typing import Dict, List, Optional, Tuple
from agent.skill_utils import is_excluded_skill_path
from hermes_cli.archive_safe import archive_root_dirs, make_targz, normalize_archive_parts, safe_extract_targz
from hermes_constants import (
- LOCAL_RUNTIME_ROOT_DIRS, clear_named_profile_deleted, mark_named_profile_deleted, named_profile_is_deleted,
+ LOCAL_RUNTIME_ROOT_DIRS, clear_named_profile_deleted, mark_named_profile_deleted, named_profile_has_identity,
+ named_profile_is_deleted, named_profile_is_live,
)
logger = logging.getLogger(__name__)
@@ -310,7 +311,7 @@ def profile_exists(name: str) -> bool:
return False
if canon == "default":
return True
- return profile_dir.is_dir() and not named_profile_is_deleted(profile_dir)
+ return named_profile_is_live(profile_dir)
def profile_matches_home(name: str, home: "Path | None" = None) -> bool:
@@ -330,7 +331,12 @@ def profile_matches_home(name: str, home: "Path | None" = None) -> bool:
def _iter_named_profile_dirs(*, live_only: bool = True) -> List[Path]:
- """Sorted named-profile dirs (valid ids, never ``default``); ``live_only`` skips tombstones."""
+ """Sorted named-profile dirs (valid ids, never ``default``); ``live_only`` skips tombstones.
+
+ A dir is a profile only when it carries an identity marker (``named_profile_has_identity``):
+ cron/logging side-effects and pre-tombstone ghost shells leave marker-less dirs that must
+ not be listed, served, ticked, or ``.env``-seeded — that seeding is what turned a ghost
+ shell into a "real" profile on the next ``hermes update`` (#95188, #94823, #99392)."""
profiles_root = _get_profiles_root()
if not profiles_root.is_dir():
return []
@@ -339,6 +345,7 @@ def _iter_named_profile_dirs(*, live_only: bool = True) -> List[Path]:
if entry.is_dir()
and entry.name != "default"
and _PROFILE_ID_RE.match(entry.name)
+ and named_profile_has_identity(entry)
and not (live_only and named_profile_is_deleted(entry))
]
@@ -937,11 +944,10 @@ def create_profile(
if canon == "default":
raise ValueError("Cannot create a profile named 'default' — it is the built-in profile (~/.hermes).")
profile_dir = get_profile_dir(canon)
- if profile_dir.exists() and named_profile_is_deleted(profile_dir):
- # Empty shells left by post-delete mkdir may be replaced. Identity files mean the
- # leftover is not a shell — fail closed, no rmtree.
- if (profile_dir / "config.yaml").exists() or (profile_dir / ".env").exists():
- raise _profile_exists_error(canon)
+ if profile_dir.exists() and not named_profile_has_identity(profile_dir):
+ # An identity-less shell (post-delete mkdir, pre-tombstone ghost) is invisible to
+ # ``profile list`` and may be replaced. Identity files mean the leftover is not a shell —
+ # fail closed, no rmtree.
shutil.rmtree(profile_dir)
if profile_dir.exists():
raise _profile_exists_error(canon)
@@ -1955,7 +1961,7 @@ def resolve_profile_env(profile_name: str) -> str:
if canon == "default":
return str(root)
profile_dir = root / "profiles" / canon
- if not profile_dir.is_dir() or named_profile_is_deleted(profile_dir):
+ if not named_profile_is_live(profile_dir):
raise _missing_profile_error(canon)
return str(profile_dir)
diff --git a/hermes_constants.py b/hermes_constants.py
index b903638a2db..032092c4735 100644
--- a/hermes_constants.py
+++ b/hermes_constants.py
@@ -265,6 +265,26 @@ def named_profile_is_deleted(profile_home: str | Path) -> bool:
return profile_tombstone_path(Path(profile_home)).exists()
+# A directory under profiles/ is a profile only when something identifies it as one.
+# Runtime side-effects (cron heartbeats, log rotation, caches) create dirs that carry
+# none of these; a pre-tombstone ghost shell or a stray infrastructure dir must never be
+# listed, served, ticked, or seeded with the default install's credentials.
+_PROFILE_IDENTITY_MARKERS = ("config.yaml", ".env", "SOUL.md", "profile.yaml", "auth.json", "state.db")
+
+
+def named_profile_has_identity(profile_home: str | Path) -> bool:
+ home = Path(profile_home)
+ return any((home / marker).is_file() for marker in _PROFILE_IDENTITY_MARKERS)
+
+
+def named_profile_is_live(profile_home: str | Path) -> bool:
+ """A resolvable named profile: an existing dir with identity that has not been deleted.
+ ``-p``/``--profile`` resolution and ``profile_exists`` share this so a stale ghost shell can
+ never be started as a backend (whose ``ensure_hermes_home`` would rebuild the full tree)."""
+ home = Path(profile_home)
+ return home.is_dir() and named_profile_has_identity(home) and not named_profile_is_deleted(home)
+
+
def mark_named_profile_deleted(profile_home: str | Path) -> None:
marker = profile_tombstone_path(Path(profile_home))
marker.parent.mkdir(parents=True, exist_ok=True)
diff --git a/tests/hermes_cli/test_deleted_profile_tombstone.py b/tests/hermes_cli/test_deleted_profile_tombstone.py
index e2b00a26f74..79228d204dd 100644
--- a/tests/hermes_cli/test_deleted_profile_tombstone.py
+++ b/tests/hermes_cli/test_deleted_profile_tombstone.py
@@ -188,6 +188,35 @@ class TestDeletedProfileTombstone:
assert "worker" not in backfilled
assert not (profile_dir / ".env").exists()
+ def test_marker_less_shell_is_not_a_profile(self, profile_env):
+ """A ``profiles/<name>`` dir with no identity file (a pre-tombstone ghost shell left by a
+ cron ticker, or a stray infrastructure dir) is not listed, served, or seeded with the
+ default install's ``.env`` — that seeding is what legitimised ghosts on ``hermes update``
+ (#95188 path D, #94823, #99392). ``profile create`` may take the name back."""
+ default_env = profile_env / ".hermes" / ".env"
+ default_env.write_text("OPENAI_API_KEY=sk-real\n", encoding="utf-8")
+ shell = profile_env / ".hermes" / "profiles" / "ghost"
+ (shell / "cron").mkdir(parents=True)
+ (shell / "cron" / "ticker_heartbeat").write_text("1\n", encoding="utf-8")
+ legacy = profile_env / ".hermes" / "profiles" / "legacy"
+ legacy.mkdir()
+ (legacy / "state.db").write_bytes(b"")
+
+ assert backfill_profile_envs(quiet=True) == ["legacy"]
+ assert not (shell / ".env").exists()
+ assert _named_homes(profile_env) == ["legacy"]
+ assert [name for name, _ in profiles_to_serve(True)] == ["default", "legacy"]
+ # ``hermes --profile ghost serve`` (a stale Desktop boot target) must not start a backend
+ # in the shell — its ensure_hermes_home() would rebuild the full profile tree.
+ assert not profile_exists("ghost")
+ with pytest.raises(FileNotFoundError):
+ resolve_profile_env("ghost")
+ assert Path(resolve_profile_env("legacy")) == legacy
+
+ recreated = create_profile("ghost", no_alias=True, no_skills=True)
+ assert recreated == shell and (shell / ".env").exists()
+ assert "ghost" in _named_homes(profile_env)
+
def test_create_after_delete_replaces_empty_shell(self, profile_env):
profile_dir = create_profile("worker", no_alias=True, no_skills=True)
_delete("worker")
diff --git a/tests/hermes_cli/test_desktop_cron_ticker_profiles.py b/tests/hermes_cli/test_desktop_cron_ticker_profiles.py
index e4b1f58bae0..839432abfd2 100644
--- a/tests/hermes_cli/test_desktop_cron_ticker_profiles.py
+++ b/tests/hermes_cli/test_desktop_cron_ticker_profiles.py
@@ -150,6 +150,7 @@ def test_desktop_ticker_serves_every_profile_and_yields_to_owning_gateway(monkey
root = tmp_path / ".hermes"
for name in ("worker", "guest", "solo"):
(root / "profiles" / name).mkdir(parents=True)
+ (root / "profiles" / name / "config.yaml").write_text("{}\n") # identity marker: served
(root / "config.yaml").write_text(yaml.safe_dump({"gateway": {"multiplex_profiles": True}}))
monkeypatch.setattr("hermes_constants.get_default_hermes_root", lambda: root)
monkeypatch.setattr(profiles_mod, "_get_default_hermes_home", lambda: root)
diff --git a/tests/hermes_cli/test_gateway_multiplex_status.py b/tests/hermes_cli/test_gateway_multiplex_status.py
index e0c3ae92251..386e5379fba 100644
--- a/tests/hermes_cli/test_gateway_multiplex_status.py
+++ b/tests/hermes_cli/test_gateway_multiplex_status.py
@@ -26,6 +26,8 @@ def _fake_multiplexer(monkeypatch, tmp_path, *, multiplex: bool, pid_file: bool
import gateway.status as status
(tmp_path / "profiles" / "beta").mkdir(parents=True)
+ # A profile dir needs an identity marker to be listed/served (bare dirs are side-effect shells).
+ (tmp_path / "profiles" / "beta" / "config.yaml").write_text("{}\n")
(tmp_path / "config.yaml").write_text(
f"gateway:\n multiplex_profiles: {'true' if multiplex else 'false'}\n"
)
diff --git a/tests/tui_gateway/test_hosted_room_service.py b/tests/tui_gateway/test_hosted_room_service.py
index 7ff4c95cc19..95b4fa11af5 100644
--- a/tests/tui_gateway/test_hosted_room_service.py
+++ b/tests/tui_gateway/test_hosted_room_service.py
@@ -2078,15 +2078,19 @@ def test_peer_recovery_replays_the_same_execution_generation(tmp_path: Path):
def test_local_profiles_skips_delete_tombstones_and_dot_dirs(tmp_path: Path):
- """`hermes profile delete` leaves ``profiles/.deleted/<name>``; neither the tombstone dir nor a
- tombstoned profile is a roster member (#106847: ``.deleted`` failed validate_roster every cycle)."""
+ """`hermes profile delete` leaves ``profiles/.deleted/<name>``; neither the tombstone dir, a
+ tombstoned profile, nor a marker-less cron shell is a roster member (#106847: ``.deleted``
+ failed validate_roster every cycle; #99392: side-effect dirs listed as bots)."""
from hermes_constants import mark_named_profile_deleted
profiles = tmp_path / "profiles"
(profiles / "ops").mkdir(parents=True)
+ (profiles / "ops" / "config.yaml").write_text("{}\n", encoding="utf-8")
(profiles / "gone").mkdir()
+ (profiles / "gone" / "config.yaml").write_text("{}\n", encoding="utf-8")
mark_named_profile_deleted(profiles / "gone")
assert (profiles / ".deleted").is_dir()
+ (profiles / "shell" / "cron").mkdir(parents=True)
service = HostedRoomService(_server(), db_path=tmp_path / "shared-state.db")
diff --git a/tui_gateway/hosted_room_service.py b/tui_gateway/hosted_room_service.py
index a5d7e131a85..8369ab1c929 100644
--- a/tui_gateway/hosted_room_service.py
+++ b/tui_gateway/hosted_room_service.py
@@ -124,15 +124,17 @@ class HostedRoomService:
return self.db_path.parent
def local_profiles(self) -> tuple[str, ...]:
- from hermes_constants import named_profile_is_deleted
+ from hermes_constants import named_profile_has_identity, named_profile_is_deleted
profiles, profiles_dir = {"default"}, self.root / "profiles"
if profiles_dir.is_dir():
# ``profiles/.deleted/`` is the tombstone dir `hermes profile delete` leaves behind, not a
# profile: feeding it to validate_roster failed plan_next_task on every cycle (#106847).
+ # Marker-less dirs (cron/log side-effect shells) are not profiles either.
profiles.update(
path.name for path in profiles_dir.iterdir()
- if path.is_dir() and not path.name.startswith(".") and not named_profile_is_deleted(path))
+ if path.is_dir() and not path.name.startswith(".")
+ and named_profile_has_identity(path) and not named_profile_is_deleted(path))
return tuple(sorted(profiles))
def bindings(self) -> tuple[HostedRoomBinding, ...]: