hermes-agent/tests/agent/test_serves_routed_profile_pin.py
tancou c7c9c18ccf fix(profiles): pin the launch home so a mirrored HERMES_HOME cannot flip routed-profile decisions
Symptom: a host that serves several profiles from one process and mirrors
the active turn's profile into `os.environ["HERMES_HOME"]` for legacy
readers (Hermes WebUI does this on every chat turn, next to the
context-local override) makes every launch-home decision see the served
profile as the launch profile. Two profiles that both configure `atlassian`
with different credentials share whichever MCP connection came first: a
READ_ONLY_MODE=false profile ends up calling a read-only server
(nesquena/hermes-webui#7721). The same misjudgement leaves the launch
residue in the served profile's child env, seeds the launch profile's
bridged allow-all grant into the served profile's secret scope, and lets
the served profile's `terminal.*` config bridge into the shared process env.

Cause: four launch-home checks compare the task's override with
`get_process_hermes_home()`, which reads `HERMES_HOME` live:
`agent.secret_scope.serves_routed_profile` (keys the MCP ledger via
`_mcp_registry_scope`, #108352 / #111481, and the check_fn cache, #111151),
`agent.secret_scope._is_process_home`, `tools.environments.local._is_routed_home`
and `hermes_cli.env_loader._process_hermes_home`. Under the mirror the two
sides are equal for every turn.

Change: `hermes_constants.pin_process_hermes_home(path | None)` lets the
host record the home it serves as its own; `get_routing_process_hermes_home()`
returns the pin when set, else `get_process_hermes_home()`; the four checks
compare against it. The pin is deliberately NOT folded into
`get_process_hermes_home()`: `get_hermes_home()` falls back to it for tasks
carrying no override (MCP loop, spawners), and the host's mirror exists
precisely so those readers see the served profile. Only "is this task
routed / is this the launch home" changes. Unpinned, behaviour is
byte-for-byte the old one; hosts that never mutate `HERMES_HOME` need not
call it. `activate_multi_profile_hosting()` is not the seam for this: it
flips `get_secret` fail-closed process-wide and freezes the launch env,
which an embedding host cannot adopt as a bug fix.

Tests (2 invariants, parametrized over the four checks plus the MCP ledger
key; red on main, green here): pinned + mirrored env -> the served home is
routed and the launch home is not, the MCP key is `(home_key, name)`,
`get_process_hermes_home()` still follows the env var; never pinned or
pinned-then-cleared -> old semantics, including "a mirrored env var IS the
launch home". `tests/conftest.py` resets the pin per test so the
module-global cannot leak between files.

Live repro (WebUI + a stdio FastMCP server named `atlassian` in two
profiles, one gated by READ_ONLY_MODE): base -> one ledger key
`'atlassian'`, the write profile lists only the read-only tools; fixed ->
`(<read_home_key>, 'atlassian')` and `(<write_home_key>, 'atlassian')`,
each profile lists its own tools.

Docs: `gateway/AGENTS.md` § Profile scope (one launch-home identity) and the
isolation table in `website/docs/user-guide/multi-profile-gateways.md`.
Also maps the author e-mail under contributors/emails/ (attribution check).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-09-23 08:09:47 -07:00

82 lines
3.8 KiB
Python

"""A host that mirrors the served profile into HERMES_HOME must not flip launch-home identity.
Hermes WebUI serves several profiles from one process and, for legacy readers, mirrors the active
turn's profile into ``os.environ["HERMES_HOME"]`` while also installing the context-local override.
Every "is this task routed / is this the launch home" decision that compared the override with the
live env var then saw the served home as the launch home: MCP connections were keyed by bare name
(shared across profiles), the launch residue was never stripped from the served profile's child env,
the launch profile's bridged allow-all grant was seeded into the served profile's secret scope, and
the served profile's ``terminal.*`` config was bridged into the shared process env.
``hermes_constants.pin_process_hermes_home`` gives such hosts one stable anchor for all of them.
"""
from __future__ import annotations
from pathlib import Path
import pytest
import hermes_constants
from agent.secret_scope import _is_process_home, serves_routed_profile
from hermes_cli.env_loader import _process_hermes_home
from tools.environments.local import _is_routed_home
from tools.mcp_tool_scope import _server_key
def _under(home, fn):
"""Run *fn* with *home* installed as the task's Hermes-home override."""
token = hermes_constants.set_hermes_home_override(home)
try:
return fn()
finally:
hermes_constants.reset_hermes_home_override(token)
# name -> "does this decision treat *home* as a routed (non-launch) profile?"
ROUTED = {
"secret_scope.serves_routed_profile": lambda home: _under(home, serves_routed_profile),
"secret_scope._is_process_home": lambda home: not _is_process_home(home),
"environments.local._is_routed_home": lambda home: _is_routed_home(home),
"env_loader._process_hermes_home": lambda home: _process_hermes_home().resolve() != Path(home).resolve(),
"mcp_tool_scope._server_key": lambda home: _under(home, lambda: _server_key("atlassian")) != "atlassian",
}
@pytest.fixture
def homes(tmp_path, monkeypatch):
launch = tmp_path / "launch"
served = tmp_path / "profiles" / "served"
launch.mkdir()
served.mkdir(parents=True)
monkeypatch.setenv("HERMES_HOME", str(launch))
monkeypatch.setattr(hermes_constants, "_PINNED_PROCESS_HERMES_HOME", None)
return launch, served
@pytest.mark.parametrize("decision", sorted(ROUTED))
def test_pinned_launch_home_survives_a_mirrored_hermes_home(homes, monkeypatch, decision):
launch, served = homes
routed = ROUTED[decision]
hermes_constants.pin_process_hermes_home(launch)
monkeypatch.setenv("HERMES_HOME", str(served)) # the host's per-turn mirror
assert routed(served) is True
assert routed(launch) is False
# The served profile's MCP connection is its own, keyed by its home, not a bare shared name.
assert _under(served, lambda: _server_key("atlassian")) == (hermes_constants.hermes_home_key(served), "atlassian")
# Process-asset readers keep following the env var: only routing decisions use the pin.
assert hermes_constants.get_process_hermes_home() == served
assert hermes_constants.get_routing_process_hermes_home() == launch
@pytest.mark.parametrize("decision", sorted(ROUTED))
def test_unpinned_or_cleared_pin_keeps_hermes_home_semantics(homes, monkeypatch, decision):
launch, served = homes
routed = ROUTED[decision]
for _ in ("never pinned", "pinned then cleared"):
monkeypatch.setenv("HERMES_HOME", str(launch))
assert routed(served) is True
assert routed(launch) is False
monkeypatch.setenv("HERMES_HOME", str(served)) # mirrored: the env var IS the launch home
assert routed(served) is False
hermes_constants.pin_process_hermes_home(launch)
hermes_constants.pin_process_hermes_home(None)