diff --git a/hermes_constants.py b/hermes_constants.py index 62d4052fb1..d3a7790a54 100644 --- a/hermes_constants.py +++ b/hermes_constants.py @@ -216,16 +216,22 @@ def profile_name_for_home(path: str | Path | None) -> str | None: return None current = Path(path).expanduser() try: - if current.resolve(strict=False) == get_default_hermes_root().resolve(strict=False): + default_root = get_default_hermes_root() + if current == default_root: + return "default" + if current.parent.name == "profiles": + named = named_profile_home(current) + if named is not None: + return named.name + if current.name and not current.name.startswith("."): + return current.name + if current.resolve(strict=False) == default_root.resolve(strict=False): return "default" named = named_profile_home(current) except (OSError, RuntimeError, ValueError): return None if named is not None: return named.name - # Session records already carry a validated profile home. Preserve the - # established basename behavior for isolated test/custom roots whose - # parent is the conventional profiles directory but lacks root markers. if current.parent.name == "profiles" and current.name and not current.name.startswith("."): return current.name return None diff --git a/tests/tui_gateway/test_profile_target_unavailable.py b/tests/tui_gateway/test_profile_target_unavailable.py index 08509f488a..1c8528d619 100644 --- a/tests/tui_gateway/test_profile_target_unavailable.py +++ b/tests/tui_gateway/test_profile_target_unavailable.py @@ -35,6 +35,30 @@ def test_explicit_profile_target_never_falls_back(tmp_path, monkeypatch): # A real resolution I/O failure must propagate, too (no predicate patch). profiles = home / "profiles" profiles.rename(home / "saved-profiles") - profiles.symlink_to("profiles") - with pytest.raises((OSError, RuntimeError)): - server._profile_home("worker") + try: + profiles.symlink_to("profiles") + except OSError: + pass + else: + with pytest.raises((OSError, RuntimeError)): + server._profile_home("worker") + + +def test_custom_root_basename_target_fails_closed_when_unavailable(tmp_path, monkeypatch): + """An explicit profile request matching a custom root basename fails closed.""" + from tui_gateway import server + + custom_home = tmp_path / "customer-data" + custom_home.mkdir() + (custom_home / "config.yaml").write_text("terminal:\n cwd: /custom\n") + monkeypatch.setattr(Path, "home", lambda: tmp_path) + monkeypatch.setenv("HERMES_HOME", str(custom_home)) + monkeypatch.setattr(server, "_hermes_home", custom_home) + + with pytest.raises(FileNotFoundError): + server._profile_home("customer-data") + + with pytest.raises(FileNotFoundError): + with server._profile_db({"profile": "customer-data"}): + pass + diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.py index fdc53a2530..feb039da16 100644 --- a/tui_gateway/methods_prompt.py +++ b/tui_gateway/methods_prompt.py @@ -218,11 +218,10 @@ def _legacy_group_fence_error(rid, session, params): hosted = probe_hosted_room(default_db_path(), room_id=room_id) peer = False if not hosted: - from hermes_constants import named_profile_home - session_profile_home = named_profile_home(str(session.get("profile_home") or "")) + from hermes_constants import profile_name_for_home peer = probe_peer_room_reservation( default_db_path(), room_id=room_id, target_profile=( - (session_profile_home.name if session_profile_home is not None else "") + profile_name_for_home(session.get("profile_home")) or str(params.get("profile") or "").strip() or str(_current_profile_name() or "default").strip())) except RoomProbeUnavailableError: diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.py index d75bdd4c26..f14eeb4013 100644 --- a/tui_gateway/methods_session.py +++ b/tui_gateway/methods_session.py @@ -536,7 +536,7 @@ def _resume_live_unpersisted(ctx: _Resume, live_sid: str, live: dict) -> dict: return _ok(ctx.rid, _attach_todo_state({ "session_id": live_sid, "stored_session_id": str(live.get("session_key") or ""), "message_count": len(history), "messages": ctx.messages(history), - "info": {"model": _resolve_model(), "lazy": True, "profile_name": ctx.profile or ""}}, live)) + "info": {"model": _resolve_model(), "lazy": True, "profile_name": profile_name_for_home(live.get("profile_home")) or _response_profile_name(ctx.profile)}}, live)) def _resume_adopt_stranded(ctx: _Resume) -> None: diff --git a/tui_gateway/server.py b/tui_gateway/server.py index 46a1e37992..50e3099c76 100644 --- a/tui_gateway/server.py +++ b/tui_gateway/server.py @@ -23,7 +23,7 @@ from typing import Any, Callable, NamedTuple, Optional # noqa: F401 (Callable: # namespace (method_ctx.bind_module) — deleting one breaks a handler at call time, not import time. from agent.secret_scope import build_profile_secret_scope, reset_secret_scope, set_secret_scope # noqa: F401 from hermes_constants import ( - get_default_hermes_root, get_hermes_home, get_hermes_home_override, profile_name_for_home, + get_hermes_home, get_hermes_home_override, profile_name_for_home, reset_hermes_home_override, set_hermes_home_override) from hermes_cli.env_loader import load_hermes_dotenv from utils import is_truthy_value @@ -452,17 +452,8 @@ def _canonical_profile_request(name: str) -> str: basenames are installation details, while explicit unknown profile names must continue to fail closed in ``_profile_home``. """ - folded = name.casefold() - if folded in {".hermes", "hermes"}: + if name.casefold() in {".hermes", "hermes"}: return "default" - with contextlib.suppress(OSError, RuntimeError, ValueError): - if folded == get_default_hermes_root().name.casefold(): - from hermes_cli import profiles as profiles_mod - # A custom Hermes root can share a name with a valid named profile. - # Prefer that real profile when it exists; otherwise this is the - # legacy basename of the default root. - if not Path(profiles_mod.get_profile_dir(name)).is_dir(): - return "default" return name @@ -487,8 +478,12 @@ def _profile_home(profile: str | None) -> Path | None: home = Path(profiles_mod.get_profile_dir(name)) if not home.is_dir(): raise FileNotFoundError(f"Profile '{name}' does not exist.") - if home.resolve() == Path(_hermes_home).resolve(): + launch = Path(_hermes_home) + if home == launch: return None # already the launch profile (no override needed) + if home.name == launch.name or home.is_symlink() or launch.is_symlink(): + if home.resolve() == launch.resolve(): + return None _served_profile_homes.add(home) # the change watcher must stat every served sibling store too return home