Skip to content

The agent that grows with you

fix(bot-mode): teammate roster and every profiles/ enumerator use the profile identity predicate (#99392)

teknium185d028873770
diff --git a/hermes_cli/active_sessions.py b/hermes_cli/active_sessions.py
index 4247b3f3440..12b1d37ea1d 100644
--- a/hermes_cli/active_sessions.py
+++ b/hermes_cli/active_sessions.py
@@ -19,7 +19,7 @@ from dataclasses import dataclass
from pathlib import Path
from typing import Any, Iterator, Optional
-from hermes_constants import get_default_hermes_root, get_hermes_home
+from hermes_constants import get_default_hermes_root, get_hermes_home, named_profile_is_live
from utils import atomic_json_write
logger = logging.getLogger(__name__)
@@ -644,8 +644,7 @@ def release_orphaned_leases(live_lease_ids: set[str]) -> int:
root = get_default_hermes_root()
homes = [root]
try:
- homes.extend(p for p in (root / "profiles").iterdir()
- if p.is_dir() and not p.name.startswith("."))
+ homes.extend(p for p in (root / "profiles").iterdir() if named_profile_is_live(p))
except OSError:
pass
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index 8a81c187acb..85038cabc33 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -513,13 +513,13 @@ def _resolve_sudo_user_profile_env(name: str) -> str | None:
"""
if name == "default":
return None
- from hermes_constants import sudo_invoker_default_home
+ from hermes_constants import named_profile_is_live, sudo_invoker_default_home
sudo_home = sudo_invoker_default_home()
if sudo_home is None:
return None
candidate = sudo_home / "profiles" / name
- return str(candidate) if candidate.is_dir() else None
+ return str(candidate) if named_profile_is_live(candidate) else None
def _under_gateway_supervisor(argv: list) -> bool:
diff --git a/tests/hermes_cli/test_active_sessions.py b/tests/hermes_cli/test_active_sessions.py
index 0fe223101fc..949f6976b4c 100644
--- a/tests/hermes_cli/test_active_sessions.py
+++ b/tests/hermes_cli/test_active_sessions.py
@@ -190,6 +190,7 @@ def test_release_orphaned_leases_sweeps_profile_runtime_registries(
root = tmp_path / "hermes"
profile = root / "profiles" / "worker"
profile.mkdir(parents=True)
+ (profile / "config.yaml").write_text("{}\n") # identity marker: a bare dir is not a profile
monkeypatch.setenv("HERMES_HOME", str(root))
root_lease, root_error = active_sessions.try_acquire_active_session(
@@ -241,6 +242,7 @@ def test_release_under_profile_home_override_targets_acquisition_registry(
root = tmp_path / "hermes"
profile = root / "profiles" / "worker"
profile.mkdir(parents=True)
+ (profile / "config.yaml").write_text("{}\n") # identity marker: a bare dir is not a profile
monkeypatch.setenv("HERMES_HOME", str(root))
lease, error = active_sessions.try_acquire_active_session(
@@ -278,6 +280,7 @@ def test_transfer_under_profile_home_override_targets_acquisition_registry(
root = tmp_path / "hermes"
profile = root / "profiles" / "worker"
profile.mkdir(parents=True)
+ (profile / "config.yaml").write_text("{}\n") # identity marker: a bare dir is not a profile
monkeypatch.setenv("HERMES_HOME", str(root))
lease, error = active_sessions.try_acquire_active_session(
diff --git a/tests/hermes_cli/test_apply_profile_override.py b/tests/hermes_cli/test_apply_profile_override.py
index a981bda518b..74f1add671f 100644
--- a/tests/hermes_cli/test_apply_profile_override.py
+++ b/tests/hermes_cli/test_apply_profile_override.py
@@ -106,6 +106,7 @@ class TestApplyProfileOverrideHermesHomeGuard:
user_home = tmp_path / "home" / "hermes"
profile_dir = user_home / ".hermes" / "profiles" / "elias"
profile_dir.mkdir(parents=True, exist_ok=True)
+ (profile_dir / "config.yaml").write_text("{}\n") # identity marker: a bare dir does not resolve
(root_home / ".hermes").mkdir(parents=True, exist_ok=True)
monkeypatch.setattr(Path, "home", lambda: root_home)
@@ -118,11 +119,14 @@ class TestApplyProfileOverrideHermesHomeGuard:
monkeypatch.setattr(pwd, "getpwnam", lambda name: SimpleNamespace(pw_dir=str(user_home)))
- from hermes_cli.main import _apply_profile_override
+ from hermes_cli.main import _apply_profile_override, _resolve_sudo_user_profile_env
_apply_profile_override()
assert os.environ.get("HERMES_HOME") == str(profile_dir)
assert sys.argv == ["hermes", "gateway", "install", "--system"]
+ # Same identity gate as ``-p`` without sudo: a marker-less shell is not a profile.
+ (user_home / ".hermes" / "profiles" / "ghost" / "cron").mkdir(parents=True)
+ assert _resolve_sudo_user_profile_env("ghost") is None
diff --git a/tests/tools/test_bot_mode_probe.py b/tests/tools/test_bot_mode_probe.py
index 7d0a287cc27..3174baf757b 100644
--- a/tests/tools/test_bot_mode_probe.py
+++ b/tests/tools/test_bot_mode_probe.py
@@ -34,6 +34,25 @@ def _make_bot_profile(root, name, *, managed=True, soul=None):
return d
+def test_roster_excludes_infra_dirs_and_tombstones(tmp_path):
+ """The teammate roster applies the same identity predicate as ``profile list``: bare
+ infrastructure dirs (``@sessions``, ``@logs``) and deleted profiles are not teammates (#99392)."""
+ from hermes_constants import mark_named_profile_deleted
+
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ _make_bot_profile(home, "researcher", managed=True)
+ for stray in ("sessions", "logs"):
+ (home / "profiles" / stray / "cron").mkdir(parents=True)
+ ghost = _make_bot_profile(home, "ghost", managed=True)
+ mark_named_profile_deleted(ghost)
+
+ assert [name for name, _ in bot_mode_probe._roster(home)] == ["default", "researcher"]
+ section = bot_mode_probe.get_bot_mode_protocol_section(home)
+ assert "`@researcher`" in section
+ assert not any(f"`@{s}`" in section for s in ("sessions", "logs", "ghost", ".deleted"))
+
+
def test_silent_when_no_profile_is_bot_managed(tmp_path):
home = tmp_path / ".hermes"
home.mkdir()
diff --git a/tests/tui_gateway/test_bot_relay_methods.py b/tests/tui_gateway/test_bot_relay_methods.py
index 3d96bff4012..9dd42049ebe 100644
--- a/tests/tui_gateway/test_bot_relay_methods.py
+++ b/tests/tui_gateway/test_bot_relay_methods.py
@@ -25,6 +25,7 @@ from tools import bot_relay
def home(tmp_path, monkeypatch):
h = tmp_path / ".hermes"
(h / "profiles" / "ops").mkdir(parents=True)
+ (h / "profiles" / "ops" / "config.yaml").write_text("{}\n") # identity marker: a bare dir is no target
monkeypatch.setenv("HERMES_HOME", str(h))
return h
@@ -96,10 +97,12 @@ def test_deliver_validates_profile_and_runs_transport(home, monkeypatch):
_result(srv._methods["bot_relay.deliver"](2, {"profile": "hermes", "message": "x"}))
assert calls["argv"][1:3] == ["-p", "default"]
- # unknown profile refuses without spawning
+ # unknown profile refuses without spawning; so does a bare infra dir under profiles/ (#99392)
calls.clear()
- err = srv._methods["bot_relay.deliver"](3, {"profile": "ghost", "message": "x"})
- assert "error" in err and "ghost" in err["error"]["message"]
+ (home / "profiles" / "sessions" / "cron").mkdir(parents=True)
+ for target in ("ghost", "sessions"):
+ err = srv._methods["bot_relay.deliver"](3, {"profile": target, "message": "x"})
+ assert "error" in err and target in err["error"]["message"]
assert not calls
diff --git a/tests/tui_gateway/test_change_watcher.py b/tests/tui_gateway/test_change_watcher.py
index 1e0d297072d..a651079bb19 100644
--- a/tests/tui_gateway/test_change_watcher.py
+++ b/tests/tui_gateway/test_change_watcher.py
@@ -116,6 +116,7 @@ def test_pairing_signal_follows_a_profile_store(watcher_home):
home, events = watcher_home
store = home / "profiles" / "work" / "platforms" / "pairing"
store.mkdir(parents=True)
+ (home / "profiles" / "work" / "config.yaml").write_text("{}\n") # identity marker: a bare dir is not a profile
server._broadcast_watched_changes(now=0.0)
(store / "telegram-approved.json").write_text('{"u1": {"user_id": "u1"}}')
diff --git a/tools/bot_mode_probe.py b/tools/bot_mode_probe.py
index 2fbd9af4613..dec93640dbf 100644
--- a/tools/bot_mode_probe.py
+++ b/tools/bot_mode_probe.py
@@ -70,9 +70,15 @@ def _handle(name: str) -> str:
def _roster(root: Path) -> list[tuple[str, Path]]:
- """(name, dir) for the default profile + every named profile, sorted."""
+ """(name, dir) for the default profile + every live named profile, sorted. Same identity
+ predicate as ``profile list``: infra dirs (``sessions/``, ``logs/``) and tombstones are not
+ teammates (#99392)."""
+ from hermes_constants import named_profile_is_live
+
profiles = root / "profiles"
- named = _swallow(lambda: [(c.name, c) for c in sorted(profiles.iterdir()) if c.is_dir()] if profiles.is_dir() else [], [])
+ named = _swallow(
+ lambda: [(c.name, c) for c in sorted(profiles.iterdir()) if named_profile_is_live(c)] if profiles.is_dir() else [],
+ [])
return [("default", root), *named]
diff --git a/tui_gateway/change_watcher.py b/tui_gateway/change_watcher.py
index 8eadec3e454..84f50f4af75 100644
--- a/tui_gateway/change_watcher.py
+++ b/tui_gateway/change_watcher.py
@@ -135,9 +135,12 @@ def _pairing_sig():
shared signal (a pairing request moves nothing in gateway_state.json)."""
home = _watcher_home()
roots = [home / "pairing", home / "platforms" / "pairing"]
+ from hermes_constants import named_profile_is_live
+
with contextlib.suppress(OSError):
for profile_dir in (home / "profiles").iterdir():
- roots += [profile_dir / "pairing", profile_dir / "platforms" / "pairing"]
+ if named_profile_is_live(profile_dir):
+ roots += [profile_dir / "pairing", profile_dir / "platforms" / "pairing"]
entries = []
for root in roots:
with contextlib.suppress(OSError):
diff --git a/tui_gateway/methods_bot_relay.py b/tui_gateway/methods_bot_relay.py
index 082e891c802..a5eacf40b12 100644
--- a/tui_gateway/methods_bot_relay.py
+++ b/tui_gateway/methods_bot_relay.py
@@ -72,9 +72,8 @@ def _(rid, params: dict, _root=_relay_root, _run=_run_delivery) -> dict:
if len(message) > MESSAGE_MAX_CHARS + 200: # + attribution headroom
return _err(rid, 4091, "message too long")
root = _root()
- known = {"default"}
- if (root / "profiles").is_dir():
- known.update(c.name for c in (root / "profiles").iterdir() if c.is_dir())
+ from tools.bot_mode_probe import _roster
+ known = {name for name, _ in _roster(root)}
resolved = "default" if profile.lower() == "hermes" else profile
if resolved not in known:
return _err(rid, 4092, f"no profile '{profile}' on this gateway")