Skip to content

The agent that grows with you

fix(gateway): read a raw api_server completion's notice policy in its served profile

yoniebans733c2c88d996
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py
index 0d60d897a83..8ef692d645d 100644
--- a/gateway/run_notifications.py
+++ b/gateway/run_notifications.py
@@ -1424,6 +1424,9 @@ class GatewayNotificationsMixin(GatewayNotificationOwnershipMixin):
from hermes_constants import get_hermes_home_override
source = self._build_process_event_source(evt)
if source is None or not getattr(source, "profile", None):
+ raw_sid = _raw_process_event_session_id(evt)
+ if raw_sid and getattr(getattr(self, "config", None), "multiplex_profiles", False):
+ return self._served_api_server_event_scope(evt, raw_sid)
# No routed profile: the launch profile's own completion. Bind ITS scope once the
# process multiplexes — unscoped, a fail-closed ledger read raises on a legitimate
# launch-profile event (no-op while single-profile).
@@ -1434,6 +1437,21 @@ class GatewayNotificationsMixin(GatewayNotificationOwnershipMixin):
return contextlib.nullcontext() # already inside this profile's scope
return _async_profile_runtime_scope(profile_home)
+ @contextlib.asynccontextmanager
+ async def _served_api_server_event_scope(self, evt: dict, raw_sid: str):
+ """Scope of the served profile whose own store holds a raw api_server session (its events name no
+ profile), else the launch profile's, as for any unrouted event."""
+ from gateway.run import _async_profile_runtime_scope
+ from tui_gateway.launch_profile_policy import async_launch_profile_scope_if_multiplexed
+ served = await asyncio.to_thread(self._served_api_server_wake_profile, evt, raw_sid)
+ if served:
+ source = SessionSource(platform=Platform.API_SERVER, chat_id=raw_sid, profile=served)
+ scope = _async_profile_runtime_scope(self._resolve_profile_home_for_source(source))
+ else:
+ scope = async_launch_profile_scope_if_multiplexed()
+ async with scope:
+ yield
+
async def _deliver_completion_notification(
self, synth_text: str, evt: dict, *, sibling_claims=(),
) -> Optional[bool]:
diff --git a/tests/gateway/test_multiplex_routing_authz.py b/tests/gateway/test_multiplex_routing_authz.py
index c1abfcbfd4a..ff1c5708240 100644
--- a/tests/gateway/test_multiplex_routing_authz.py
+++ b/tests/gateway/test_multiplex_routing_authz.py
@@ -151,3 +151,57 @@ def test_completion_preflight_runs_in_target_profile_scope(mux):
return unscoped, scoped
assert asyncio.run(_run()) == ("terminal", "deliver")
+
+
[email protected](("launch_policy", "served_policy"), [(False, True), (True, False)])
+def test_raw_api_server_child_notice_follows_the_served_profile_policy(
+ tmp_path, monkeypatch, private_db_probe_cleanup, launch_policy, served_policy,
+):
+ """A served profile's raw api_server session names no profile: the child-process notice policy
+ of the profile whose own store holds the session decides, not the launch profile's."""
+ import queue
+ from typing import cast
+
+ from gateway import run as run_module
+ from hermes_state import SessionDB
+
+ home = tmp_path / "home"
+ served = home / "profiles" / "builder"
+ served.mkdir(parents=True)
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ policy = "delegation:\n surface_child_process_notifications: {}\ndisplay:\n background_process_notifications: all\n"
+ (home / "config.yaml").write_text(policy.format(str(launch_policy).lower()))
+ (served / "config.yaml").write_text(policy.format(str(served_policy).lower()))
+ db = SessionDB(db_path=served / "state.db")
+ db.create_session(session_id="raw-session", source="api_server", profile_name="builder")
+ turns = []
+
+ async def api_turn(**kw):
+ turns.append((kw["session_id"], kw["profile"]))
+
+ evt = {"type": "watch_match", "session_id": "proc_abcd1234", "session_key": "raw-session",
+ "platform": "api_server", "chat_id": "raw-session", "owner_task_id": "sa-child",
+ "pattern": "READY", "command": "build", "output": "READY"}
+
+ async def _run():
+ with run_module._profile_runtime_scope(home), patch.object(
+ run_module, "_multiplex_profile_homes", return_value=[("builder", served)],
+ ):
+ runner = run_module.GatewayRunner(GatewayConfig(multiplex_profiles=True, sessions_dir=home / "sessions"))
+ runner.adapters[Platform.API_SERVER] = cast(BasePlatformAdapter, SimpleNamespace(
+ supports_async_delivery=False, run_internal_session_turn=api_turn))
+ runner._primary_profile_name = "default"
+ runner._profile_adapters = {"builder": {}}
+ completions = queue.Queue()
+ completions.put(evt)
+ try:
+ await runner._drain_watch_notifications(completions)
+ finally:
+ runner.session_store.close_all_db_handles()
+ runner.close_all_session_db_handles()
+
+ try:
+ asyncio.run(_run())
+ finally:
+ db.close()
+ assert turns == ([("raw-session", "builder")] if served_policy else [])