Skip to content

The agent that grows with you

fix(gateway): a /p/<profile>/ API session runs under the profile whose authority admitted it

teknium17c7e56db936b
diff --git a/gateway/session_api.py b/gateway/session_api.py
index 10469d8154f..490b8971df2 100644
--- a/gateway/session_api.py
+++ b/gateway/session_api.py
@@ -18,6 +18,24 @@ def declared_api_session(db, key):
return row[0] if row else None
+def _served_profile(authority):
+ """Name of the profile *authority* serves; None for the launch profile (and for bare runners
+ without a registry), which is what ``SessionSource.profile`` means on the wire."""
+ registry = getattr(authority.runner, 'session_authorities', None)
+ return registry.profile_name(authority) if registry is not None else None
+
+
+def _pin_api_identity(runner, source, *, transport_profile=None):
+ """Pin the shared-listener identity on an API source: transport = the launch profile whose
+ listener received ``/p/<name>/``, runtime = ``source.profile``. Returns the transport profile to
+ persist; None outside multiplexing, where the launch adapter is the only one by construction."""
+ from gateway.session_identity import restore_identity
+ if transport_profile is None:
+ transport_profile = getattr(runner, '_primary_profile_name', None) or 'default'
+ identity = restore_identity(source, runner=runner, transport_profile=transport_profile)
+ return identity.transport_profile if identity is not None else None
+
+
def bind_api_session(authority, session_id, *, hosted_dispatch=None, declared_key=None):
"""Only the authenticated API edge may reserve an API source; never public RPC."""
authority._require_admission_open()
@@ -36,12 +54,19 @@ def bind_api_session(authority, session_id, *, hosted_dispatch=None, declared_ke
if (authority.sessions[session_id].source.platform != Platform.API_SERVER
or authority.db.get_session(session_id)['source'] != storage_source):
raise RuntimeStoreError('permission_denied')
+ # An API session is the shared-listener row of the transport matrix: the launch profile's
+ # ``api_server`` adapter receives ``/p/<name>/`` and answers it, while the turn RUNS under the
+ # authority's own profile. Both ride the persisted source — ``source.profile`` places the runtime
+ # (unset = launch profile, so a secondary's session would otherwise execute under the default's
+ # scope) and ``transport_profile`` names the delivering bot after a restart.
source = SessionSource(platform=Platform.API_SERVER, chat_id=session_id,
- user_id='api', chat_type='dm')
+ user_id='api', chat_type='dm', profile=_served_profile(authority))
+ transport_profile = _pin_api_identity(authority.runner, source)
route = authority.runner.session_store._generate_session_key(source)
from gateway.session_lifecycle import _now
now = _now()
- entry = SessionEntry(route, session_id, now, now, origin=source, platform=Platform.API_SERVER)
+ entry = SessionEntry(route, session_id, now, now, origin=source, platform=Platform.API_SERVER,
+ transport_profile=transport_profile)
receipt = {'profile_id': authority.profile_id, 'session_id': session_id,
'route': route, 'entry': entry.to_dict()}
if declared_key:
@@ -116,6 +141,7 @@ def restore_api_session(authority, session_id):
raise RuntimeStoreError('profile_mismatch')
entry = SessionEntry.from_dict(receipt['entry'])
source = entry.origin
+ _pin_api_identity(authority.runner, source, transport_profile=entry.transport_profile)
row = authority.db.get_session(session_id)
if (receipt['session_id'] != session_id or entry.session_id != session_id
or source.platform != Platform.API_SERVER or source.chat_id != session_id
diff --git a/tests/gateway/test_api_binding_served_profile.py b/tests/gateway/test_api_binding_served_profile.py
new file mode 100644
index 00000000000..b6abf3e6801
--- /dev/null
+++ b/tests/gateway/test_api_binding_served_profile.py
@@ -0,0 +1,66 @@
+"""A ``/p/<profile>/`` API session runs under the profile whose authority admitted it.
+
+Under multiplex the launch profile's ``api_server`` listener receives every ``/p/<name>/`` request
+and the routed authority admits the turn — but the turn's runtime home comes from the identity
+pinned on the session's source. A source bound with no ``profile`` resolved as the launch profile,
+so a secondary's API turn executed with the default profile's config, model and credentials.
+"""
+from pathlib import Path
+
+import pytest
+
+from tests.gateway.test_session_authorities_multiplex import _reserve_homes, _runner
+
+
+async def test_api_binding_pins_runtime_to_the_admitting_profile(tmp_path, monkeypatch):
+ from gateway.run_runtime import initialize_gateway_runtime
+ from gateway.runtime_ownership import process_ownership
+ from gateway.session_api import bind_api_session, restore_api_session
+ from gateway.session_identity import identity_of
+ root, homes = _reserve_homes(tmp_path, monkeypatch)
+ process_ownership.reserve([home for _, home in homes])
+ runner = None
+ try:
+ runner = _runner(root, homes)
+ await initialize_gateway_runtime(runner)
+ registry = runner.session_authorities
+ alpha_home = homes[1][1]
+ alpha = registry.for_home(alpha_home)
+ launch = registry.launch
+ for authority in (alpha, launch):
+ authority.db.create_session('api-1', source='api_server')
+
+ ref = bind_api_session(alpha, 'api-1')
+ live = alpha.sessions[ref.session_id]
+ identity = identity_of(live.source)
+ assert live.source.profile == 'alpha'
+ assert identity is not None and identity.multiplexed
+ # Runs under alpha; the launch listener is the transport that received and answers it.
+ assert identity.runtime_home == alpha_home.resolve()
+ assert identity.transport_profile == 'default'
+ assert runner._resolve_profile_home_for_source(live.source) == alpha_home.resolve()
+ assert runner._profile_scope_key_for_source(live.source) == alpha_home.resolve()
+ assert live.route.startswith('agent:alpha:')
+ # The same id bound by the launch authority stays in the launch profile (no cross-talk).
+ launch_ref = bind_api_session(launch, 'api-1')
+ launch_source = launch.sessions[launch_ref.session_id].source
+ assert launch_source.profile is None
+ assert identity_of(launch_source).runtime_home == root.resolve()
+ assert launch.sessions[launch_ref.session_id].route.startswith('agent:main:')
+
+ # A restart restores the same placement from the durable receipt.
+ alpha.sessions.clear()
+ restored = restore_api_session(alpha, 'api-1')
+ assert restored == ref
+ restored_source = alpha.sessions['api-1'].source
+ assert restored_source.profile == 'alpha'
+ restored_identity = identity_of(restored_source)
+ assert restored_identity.runtime_home == alpha_home.resolve()
+ assert restored_identity.transport_profile == 'default'
+ assert Path(runner._resolve_profile_home_for_source(restored_source)) == alpha_home.resolve()
+ finally:
+ for authority in list(getattr(runner, 'session_authorities', None) or []):
+ authority.db.close()
+ for _, home in homes:
+ process_ownership.release(home)