Skip to content

The agent that grows with you

refactor(gateway): one parked-scope authority check and one parked 503

Tekniumc13414bd98cd
diff --git a/gateway/platforms/api_server_response_admissions.py b/gateway/platforms/api_server_response_admissions.py
index 6e606f677a2..6ed96e611bd 100644
--- a/gateway/platforms/api_server_response_admissions.py
+++ b/gateway/platforms/api_server_response_admissions.py
@@ -30,15 +30,10 @@ def find_admission(adapter, durable_key, request_id):
The compact pointer survives response-cache eviction and transcript retirement. A process
loss between the authority commit and pointer commit is repaired from that exact request id.
"""
- from gateway.session_authorities import active_authority
+ from gateway.session_authorities import require_active_authority
from gateway.session_contract import SessionRef
from hermes_state_runtime import get_session_admission, _row
- authority = active_authority(adapter.gateway_runner)
- if authority is None:
- from gateway.run_runtime import refuse_parked_scope
- refuse_parked_scope(adapter.gateway_runner) # retryable while its profile is parked
- if authority is None or adapter._ensure_session_db() is not authority.db:
- raise RuntimeStoreError('profile_mismatch')
+ authority = require_active_authority(adapter.gateway_runner, lambda: adapter._ensure_session_db())
store = adapter._current_response_store()
admission_id = store.request_admission(durable_key[0])
if admission_id:
diff --git a/gateway/platforms/shared_ingress.py b/gateway/platforms/shared_ingress.py
index 322552c6c19..8aa4c9babd2 100644
--- a/gateway/platforms/shared_ingress.py
+++ b/gateway/platforms/shared_ingress.py
@@ -129,8 +129,9 @@ async def dispatch_profile_ingress(
from gateway.run_runtime import profile_parked
if not candidates and profile_parked(runner, profile):
# Its adapters come back with it (a rescan or restart): the platform retries a 503.
+ from gateway.parked_route_refusal import ParkedRouteRefused
raise web.HTTPServiceUnavailable(text="Profile temporarily unavailable; retry",
- headers={"Retry-After": "30"})
+ headers=ParkedRouteRefused.headers)
if not profile or not candidates:
raise web.HTTPNotFound(text="Unknown or unconfigured profile")
rel_url = request.rel_url.with_path("/" + tail.lstrip("/"), keep_query=True, keep_fragment=True)
diff --git a/gateway/platforms/webhook_ingress.py b/gateway/platforms/webhook_ingress.py
index ecdb2056d52..602aa18e8f9 100644
--- a/gateway/platforms/webhook_ingress.py
+++ b/gateway/platforms/webhook_ingress.py
@@ -69,7 +69,8 @@ def parked_profile_refusal(runner, profile, route_name, route_config):
else:
return None
from aiohttp import web
- return web.json_response({'error': error}, status=503, headers={'Retry-After': '30'})
+ from gateway.parked_route_refusal import ParkedRouteRefused
+ return web.json_response({'error': error}, status=ParkedRouteRefused.status, headers=ParkedRouteRefused.headers)
def _prefix_not_served(runner, profile):
diff --git a/gateway/session_api_turn.py b/gateway/session_api_turn.py
index 19b5e503231..7727ba385ac 100644
--- a/gateway/session_api_turn.py
+++ b/gateway/session_api_turn.py
@@ -159,12 +159,8 @@ def admit_api_turn(adapter, **kwargs):
async def admit_api_turn_async(adapter, steps=None, **kwargs):
"""``admit_api_turn`` (or *steps*, a generator ending in ``admit_api_steps``) with every SQLite
write off the owner loop, in admission order."""
- from gateway.run_runtime import refuse_parked_scope
- from gateway.session_authorities import active_authority
- authority = active_authority(adapter.gateway_runner)
- if authority is None:
- refuse_parked_scope(adapter.gateway_runner) # retryable while its profile is parked
- raise RuntimeStoreError('profile_mismatch')
+ from gateway.session_authorities import require_active_authority
+ authority = require_active_authority(adapter.gateway_runner)
return await run_steps_off_loop(authority, steps or admit_api_steps(adapter, **kwargs), orphaned=schedule_orphan)
@@ -177,13 +173,8 @@ def schedule_orphan(admitted):
def admit_api_steps(adapter, **kwargs):
# ``/p/<profile>/`` middleware scoped this request; the routed home's authority admits it.
- from gateway.run_runtime import refuse_parked_scope
- from gateway.session_authorities import active_authority
- authority = active_authority(adapter.gateway_runner)
- if authority is None:
- refuse_parked_scope(adapter.gateway_runner) # retryable while its profile is parked
- if authority is None or adapter._ensure_session_db() is not authority.db:
- raise RuntimeStoreError('profile_mismatch')
+ from gateway.session_authorities import require_active_authority
+ authority = require_active_authority(adapter.gateway_runner, lambda: adapter._ensure_session_db())
sid = kwargs.get('session_id') or uuid.uuid4().hex
declared_key = kwargs.get('gateway_session_key') if kwargs.get('bind_declared_conversation') else None
if declared_key:
diff --git a/gateway/session_authorities.py b/gateway/session_authorities.py
index bb52b1f2357..0f63420d5b8 100644
--- a/gateway/session_authorities.py
+++ b/gateway/session_authorities.py
@@ -184,6 +184,21 @@ def active_authority(runner):
return registry.active()
+def require_active_authority(runner, store=None):
+ """The active scope's authority, or a refusal: retryable (``runtime_draining``) while its
+ profile is parked, else ``profile_mismatch``. With *store* (a callable returning the caller's
+ SessionDB, called only once an authority exists), that store must be the authority's: an API
+ adapter bound to another profile's SessionDB never admits here."""
+ authority = active_authority(runner)
+ if authority is None:
+ from gateway.run_runtime import refuse_parked_scope
+ refuse_parked_scope(runner) # retryable while its profile is parked
+ if authority is None or (store is not None and store() is not authority.db):
+ from hermes_state_runtime import RuntimeStoreError
+ raise RuntimeStoreError('profile_mismatch')
+ return authority
+
+
def all_authorities(runner):
registry = getattr(runner, 'session_authorities', None)
if registry is not None: