The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui_gateway): dispatch agent_loop_stopped in the session's profile scope (#125063, salvage #125073)
Yuan Li51b91997a59a
diff --git a/tests/tui_gateway/test_interrupt_agent_loop_stopped_hook.py b/tests/tui_gateway/test_interrupt_agent_loop_stopped_hook.pyindex d4a3e9f57a2..02af3a1dfe0 100644--- a/tests/tui_gateway/test_interrupt_agent_loop_stopped_hook.py+++ b/tests/tui_gateway/test_interrupt_agent_loop_stopped_hook.py@@ -68,3 +68,39 @@ def test_hook_failure_does_not_break_interrupt(mock_invoke_hook):# The cancel flag was still set despite the hook blowing up.assert session["_turn_cancel_requested"] is True+++def test_interrupt_hook_observer_sees_the_sessions_profile_a_b_a(tmp_path, monkeypatch):+ """Off-turn dispatch (RPC / reaper) binds the SESSION's profile around ``agent_loop_stopped``, so an+ observer's ``get_hermes_home()`` names that profile — launch → secondary → launch under multiplexing+ (#125063), and no binding leaks past the dispatch."""+ from agent.secret_scope import set_multiplex_active+ from hermes_constants import get_hermes_home, get_hermes_home_override+ from tui_gateway import launch_profile_policy as lpp, server++ launch = tmp_path / "hermes_home"+ routed = launch / "profiles" / "s6probe-b"+ routed.mkdir(parents=True)+ for home in (launch, routed):+ (home / ".env").write_text("", encoding="utf-8")+ monkeypatch.setenv("HERMES_HOME", str(launch))+ monkeypatch.setattr(server, "_hermes_home", launch)+ monkeypatch.setattr(lpp, "_snapshot", None)+ seen = []++ def observer(name, **_kw):+ if name == "agent_loop_stopped":+ seen.append(get_hermes_home())++ set_multiplex_active(True)+ try:+ for profile_home in (None, routed, None):+ session = _make_session(running=True)+ session["profile_home"] = str(profile_home) if profile_home else None+ with patch("hermes_cli.plugins.invoke_hook", side_effect=observer), patch.object(server, "_clear_pending"):+ server._interrupt_session_turn("s1", session)+ assert get_hermes_home_override() is None+ finally:+ set_multiplex_active(False)++ assert seen == [launch, routed, launch]diff --git a/tui_gateway/session_lifecycle.py b/tui_gateway/session_lifecycle.pyindex 8ed652e7eca..366bdc95761 100644--- a/tui_gateway/session_lifecycle.py+++ b/tui_gateway/session_lifecycle.py@@ -655,12 +655,16 @@ def _interrupt_session_turn(sid: str, session: dict, *, request_id: str | None =# Sibling of gateway/run_agent_cache.py::_interrupt_and_clear_session: a user-initiated stop of a# live TUI/desktop turn is the same "loop is gone" event for plugins holding per-turn external# resources. Observer-only; dispatch failures never break the interrupt.+ # Every caller (session.interrupt RPC, orphan/idle reapers, lease takeover) is off-turn, so bind the+ # SESSION's profile as _finalize_session does or an observer's get_hermes_home() names the launch+ # profile (#125063). hydrate_secrets=False: observer-only, /stop must stay fast.try:from hermes_cli.plugins import invoke_hook as _invoke_hook- _invoke_hook(- "agent_loop_stopped", session_key=session.get("session_key", ""), platform="tui",- reason="user_stop", invalidation_reason="session_interrupt",- )+ with _session_profile_runtime_scope(session, hydrate_secrets=False):+ _invoke_hook(+ "agent_loop_stopped", session_key=session.get("session_key", ""), platform="tui",+ reason="user_stop", invalidation_reason="session_interrupt",+ )except Exception:logger.debug("agent_loop_stopped hook dispatch failed", exc_info=True)if not use_compute_host: