hermes-agent/tests/agent/test_secret_scope_pinned_launch_home.py
teknium1 bd970b0588 fix(profiles): route-only launch pin; profile delete names its own unit under multiplex
Builds on tancou's #119129 (cherry-picked above): the pin now lives in
get_routing_process_hermes_home() and only the four routed-profile DECISIONS read it.
get_process_hermes_home()/get_hermes_home() keep following HERMES_HOME, so an env-only
home switch in a multiplexed process resolves as before.

- set_multiplex_active(True) pins the launch home only when no host pin exists, and
  set_multiplex_active(False) releases only the pin it created itself. A transient toggle
  (gateway_migrate._multiplex_read_mode, cron external-worker restore) no longer drops an
  embedding host's explicit pin_process_hermes_home(launch).
- profiles._cleanup_gateway_service binds set_hermes_home_override(profile_dir) beside the
  env write. Under the previous head, DELETE /api/profiles/<x> from a multi-profile dashboard
  resolved get_service_name() against the pinned launch home -> bare `hermes-gateway`, and
  disabled/stopped/unlinked the HOST multiplexer's unit. Same path serves rename_profile.

Tests (red on the previous head): explicit pin survives True->False; env readers follow the
env while pinned; two-home delete removes hermes-gateway-victim and leaves hermes-gateway.
2026-09-23 08:09:47 -07:00

70 lines
3 KiB
Python

"""The launch home is pinned once the process serves several profiles (#119242).
``hermes_constants.pin_process_hermes_home`` (salvaged from #119129) gives the four routed-profile
decisions one stable launch-home identity; ``set_multiplex_active(True)`` pins it automatically for
a multiplexing gateway/dashboard, since neither calls the pin itself. Two contracts around that:
* ``get_process_hermes_home()`` / ``get_hermes_home()`` keep following ``HERMES_HOME`` while the
pin holds — a host's env mirror exists so override-less readers see the served profile, and an
env-only home switch (``profiles._cleanup_gateway_service``) must not resolve to the launch home.
* Deactivation releases only the pin activation itself created: an embedding host's explicit pin
survives the transient toggles in ``gateway_migrate._multiplex_read_mode`` and cron workers.
"""
from __future__ import annotations
import pytest
import hermes_constants
from agent.secret_scope import serves_routed_profile, set_multiplex_active
from hermes_constants import (
get_hermes_home,
get_process_hermes_home,
get_routing_process_hermes_home,
reset_hermes_home_override,
set_hermes_home_override,
)
@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, raising=False)
return launch, served
def test_multiplex_activation_pins_the_launch_home_but_env_readers_still_follow_the_env(homes, monkeypatch):
launch, served = homes
set_multiplex_active(True)
monkeypatch.setenv("HERMES_HOME", str(served)) # the host's per-turn mirror
assert get_routing_process_hermes_home() == launch
# Only routing DECISIONS are frozen; override-less readers see what the env names.
assert get_process_hermes_home() == served
assert get_hermes_home() == served
set_multiplex_active(False)
# The auto-pin is released with the mode: a standalone process follows the env again.
assert get_routing_process_hermes_home() == served
token = set_hermes_home_override(served)
try:
assert not serves_routed_profile()
finally:
reset_hermes_home_override(token)
def test_an_explicit_host_pin_survives_a_transient_multiplex_toggle(homes, monkeypatch):
"""#119242 contract: the embedding host pins once; ``_multiplex_read_mode`` / a cron worker
flipping the mode True→False in the same process must not drop it."""
launch, served = homes
hermes_constants.pin_process_hermes_home(launch)
monkeypatch.setenv("HERMES_HOME", str(served))
token = set_hermes_home_override(served)
try:
set_multiplex_active(True)
set_multiplex_active(False)
assert get_routing_process_hermes_home() == launch
assert serves_routed_profile()
finally:
reset_hermes_home_override(token)