Skip to content

The agent that grows with you

fix(gateway): run admitted turns through the startup-restore gate

unsupportedpastelsab819a439f67
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.py
index 60e21baf5d9..7ac9686a123 100644
--- a/gateway/run_inbound.py
+++ b/gateway/run_inbound.py
@@ -179,10 +179,14 @@ class GatewayInboundMixin(GatewayInboundHooksMixin, GatewayPluginInjectionMixin)
logger.info("Dropping Slack message from configured ignored channel %s", _chat_id)
return None
+ # An admission the authority is executing is already ordered by its durable FIFO; parking it
+ # here would settle the row with no turn and replay the input outside it (GW-2).
+ from gateway.session_ingress import executing_admission
if (
getattr(self, "_startup_restore_in_progress", False)
and not is_internal
and not getattr(event, "_hermes_startup_restore_replay", False)
+ and not executing_admission.get()
):
self._queue_startup_restore_event(event)
return None
diff --git a/tests/gateway/test_webhook_startup_restore_admission.py b/tests/gateway/test_webhook_startup_restore_admission.py
new file mode 100644
index 00000000000..72fad278d20
--- /dev/null
+++ b/tests/gateway/test_webhook_startup_restore_admission.py
@@ -0,0 +1,60 @@
+"""GW-2: an admission the authority is executing is never parked by the startup-restore gate.
+
+A webhook POST that lands while startup auto-resume runs is committed to the durable FIFO. If the
+gate then parked its execution, the row settled ``terminal`` with no turn, the webhook finalizer
+ended the session in state.db, and the gate's later replay ran the input a second time outside its
+admission (``routing key ... is ended in state.db but still live``, reply ``not_found``).
+"""
+import hashlib
+import hmac
+import json
+
+import pytest
+from aiohttp import web
+from aiohttp.test_utils import TestClient, TestServer
+
+from gateway.config import PlatformConfig
+from gateway.platforms.webhook import WebhookAdapter
+from tests.gateway.fixtures.webhook_route_authority import mount_authority
+
+
+async def test_admitted_webhook_runs_through_startup_restore_gate(monkeypatch):
+ from gateway.session_contract import SessionRef
+ from gateway.session_ingress import execute_admission
+ from hermes_state_runtime import get_session_admission
+
+ config = PlatformConfig(enabled=True, extra={
+ 'secret': 'owned-secret', 'routes': {'fixture': {'prompt': '{text}', 'deliver': 'log'}}})
+ adapter = WebhookAdapter(config)
+ app = web.Application()
+ mount_authority(app, adapter)
+ app.router.add_post('/webhooks/{route_name}', adapter._handle_webhook)
+ async with TestClient(TestServer(app)) as client:
+ runner = adapter._message_handler.__self__
+ # Boot auto-resume is still running: the inbound gate is closed.
+ runner._startup_restore_in_progress = True
+ runner._startup_restore_queue = []
+ body = json.dumps({'text': 'during restore'}).encode()
+ headers = {'X-GitHub-Delivery': 'restore-one', 'X-Hub-Signature-256':
+ 'sha256=' + hmac.new(b'owned-secret', body, hashlib.sha256).hexdigest()}
+ response = await client.post('/webhooks/fixture', data=body, headers=headers)
+ assert response.status == 202, await response.text()
+ authority = runner.session_authority
+ admission_id = authority.db._read_one('SELECT admission_id FROM session_admissions')['admission_id']
+ row = get_session_admission(authority.db, admission_id=admission_id)
+
+ turns = []
+
+ async def turn_ran(event, source, _key):
+ # First seam past the gate. This pins the gate decision only: claim, settlement,
+ # webhook finalisation and delivery are not exercised here.
+ turns.append(event.text)
+ return 'answered'
+
+ monkeypatch.setattr(runner, '_hm_pending_reply_intercepts', turn_ran)
+ result = await execute_admission(authority, SessionRef(authority.profile_id, row['target_session_id']), row)
+
+ assert runner._startup_restore_queue == [], 'the gate parked an admission it does not own'
+ assert turns == ['during restore']
+ assert result == 'answered'