Skip to content

The agent that grows with you

fix(runtime): owner scope is multiplex-only; foreign LOCAL sources fail closed; primary = launch home

Tekniuma37550b96ff7
diff --git a/gateway/platforms/webhook.py b/gateway/platforms/webhook.py
index 3eebf6272fa..7bb6dc571a6 100644
--- a/gateway/platforms/webhook.py
+++ b/gateway/platforms/webhook.py
@@ -594,8 +594,9 @@ class WebhookAdapter(BasePlatformAdapter):
return _json_error("Admission unavailable; retry this delivery", 503)
if getattr(event, '_webhook_duplicate', False):
return web.json_response({"status": "duplicate", "delivery_id": delivery_id}, status=200)
- from gateway.session_authorities import authority_for_profile_id
- authority = authority_for_profile_id(self._message_handler.__self__, receipt.ref.profile_id)
+ from gateway.session_authorities import active_authority, authority_for_profile_id
+ runner = self._message_handler.__self__
+ authority = authority_for_profile_id(runner, receipt.ref.profile_id) or active_authority(runner)
task = asyncio.create_task(self._finalize_delivery(event, authority, receipt))
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.discard)
diff --git a/gateway/run_api.py b/gateway/run_api.py
index 63913c7d0dd..6abbbf736da 100644
--- a/gateway/run_api.py
+++ b/gateway/run_api.py
@@ -157,9 +157,8 @@ class GatewayRuntimeAPI:
# The ticket names the served profile; this connection binds to that home's authority.
scope['hermes.session_authority'] = authority
from tui_gateway.ws import handle_ws
- from gateway.run import _profile_runtime_scope
- from pathlib import Path
- with _profile_runtime_scope(Path(authority.profile_id), hydrate_secrets=False):
+ from gateway.session_authorities import owner_scope
+ with owner_scope(authority):
await handle_ws(ws, auth_identity={'user_id': grant['subject'], 'provider': 'local',
'profile_id': grant['profile_id'],
'instance_id': grant['instance_id'],
diff --git a/gateway/session_authorities.py b/gateway/session_authorities.py
index 8d47cd4247d..1f7351d8b2e 100644
--- a/gateway/session_authorities.py
+++ b/gateway/session_authorities.py
@@ -87,18 +87,12 @@ class SessionAuthorities:
def served_profile_name(home) -> str:
- """Canonical profile id of a served home (``default`` for the root, the directory name for
- ``profiles/<name>``); the shape-only ``parent == 'profiles'`` guess mislabels a custom
- HERMES_HOME outside ``profiles/`` as ``default`` and collides in rosters/relay selectors."""
+ """Canonical profile id of a served home: ``default`` for the root, the directory name for
+ ``profiles/<name>`` (via ``hermes_constants.profile_name_for_home``, the resolver main uses).
+ Any other custom HERMES_HOME is the process's own single profile and keeps the ``default``
+ label the CLI reports for it (``hermes -p`` never targets such a home by name)."""
from hermes_constants import profile_name_for_home
- name = profile_name_for_home(home)
- if name is not None:
- return name
- from hermes_cli.profiles import get_active_profile_name
- from hermes_constants import get_hermes_home
- if Path(home).resolve() == get_hermes_home().resolve():
- return get_active_profile_name() or 'default'
- return Path(home).name
+ return profile_name_for_home(home) or 'default'
def authority_for_home(runner, home):
@@ -134,13 +128,19 @@ def all_authorities(runner):
def owner_scope(authority, *, hydrate_secrets=False):
- """Runtime scope of the profile that owns *authority*.
+ """Runtime scope of the profile that owns *authority* — under multiplex only.
Owner-side handlers (RPC dispatch, cron submit, admitted execution) must read config, jobs
- and policy for the OWNING profile, never the launch profile's ambient scope. Test peers
- build authorities with symbolic ids ('fixture'); those keep the ambient scope.
+ and policy for the OWNING profile, never the launch profile's ambient scope. A
+ single-profile gateway keeps its ambient scope byte-for-byte: entering a scope there
+ would read the profile's config/.env for every handler, including bypass (--safe-mode)
+ launches that must never touch the profile YAML. Test peers with symbolic ids ('fixture')
+ likewise keep the ambient scope.
"""
from contextlib import nullcontext
+ runner = getattr(authority, 'runner', None)
+ if not getattr(getattr(runner, 'config', None), 'multiplex_profiles', False):
+ return nullcontext()
home = Path(str(authority.profile_id))
if not home.is_absolute() or not home.is_dir():
return nullcontext()
diff --git a/gateway/session_ingress_context.py b/gateway/session_ingress_context.py
index 89f648c63e4..4898e5350f3 100644
--- a/gateway/session_ingress_context.py
+++ b/gateway/session_ingress_context.py
@@ -96,8 +96,12 @@ def _binding(runner, source, profile):
if name in homes or canonical in homes.values():
raise RuntimeStoreError('profile_mismatch')
homes[name] = canonical
- primary = getattr(runner, '_primary_profile_name', None)
- if not primary:
+ # The primary is the reserved entry whose home IS the launch home (identity, not the
+ # display name: a custom HERMES_HOME reports 'custom' while its reservation says 'default').
+ from hermes_constants import get_process_hermes_home
+ launch = get_process_hermes_home().resolve()
+ primary = next((name for name, path in homes.items() if path == launch), None)
+ if primary is None:
raise RuntimeStoreError('profile_mismatch')
owner_name = profile or primary
if homes.get(owner_name) != home:
diff --git a/gateway/session_local.py b/gateway/session_local.py
index d87a8c16178..eeed4a0b295 100644
--- a/gateway/session_local.py
+++ b/gateway/session_local.py
@@ -67,12 +67,16 @@ def authorize_local_source(runner, source):
return None
# A forged profile must not miss this adapter and fall into a messaging
# allow-all policy. Local source identity is owned by this authority alone.
+ from gateway.session_authorities import all_authorities
+ owners = all_authorities(runner)
adapter = runner._adapters_for_profile(source.profile).get(Platform.LOCAL)
if not isinstance(adapter, LocalSessionAdapter):
- return None
- from gateway.session_authorities import authority_for_profile_id
- owner = authority_for_profile_id(runner, adapter.authority.profile_id)
- if owner is None or adapter.authority is not owner:
+ # A LOCAL source naming a profile this runtime does not serve must fail closed rather
+ # than fall into a messaging allow-all policy; legacy runners (no authority) stay None.
+ return False if owners else None
+ # The adapter's authority must be one THIS runtime owns (the launch authority or a served
+ # secondary's), never a stale/foreign object that happens to carry a LOCAL adapter.
+ if not any(adapter.authority is owner for owner in owners):
return False
return adapter.authorize_source(source)
diff --git a/tui_gateway/ws.py b/tui_gateway/ws.py
index a89b46e2114..b907476d90f 100644
--- a/tui_gateway/ws.py
+++ b/tui_gateway/ws.py
@@ -290,7 +290,7 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:
_disable_nagle(ws)
_log.info("ws accepted peer=%s", peer)
transport = WSTransport(ws, asyncio.get_running_loop(), peer=peer, auth_identity=auth_identity)
- authority = ws.scope.get('hermes.session_authority') or getattr(
+ authority = (getattr(ws, 'scope', None) or {}).get('hermes.session_authority') or getattr(
getattr(getattr(ws, 'app', None), 'state', None), 'session_authority', None)
if authority is not None:
from gateway.session_controls import AuthorityConnection