Skip to content

The agent that grows with you

fix: preserve webhook duplicate responses with real route admission fixtures

Teknium253f9b5e4c5f
diff --git a/gateway/platforms/webhook.py b/gateway/platforms/webhook.py
index 0c31e1be8d7..ec73898c956 100644
--- a/gateway/platforms/webhook.py
+++ b/gateway/platforms/webhook.py
@@ -581,6 +581,8 @@ class WebhookAdapter(BasePlatformAdapter):
except Exception:
logger.exception("[webhook] Durable admission failed for %s", delivery_id)
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)
authority = self._message_handler.__self__.session_authority
task = asyncio.create_task(self._finalize_delivery(event, authority, receipt))
self._background_tasks.add(task)
diff --git a/gateway/platforms/webhook_ingress.py b/gateway/platforms/webhook_ingress.py
index 9e98b8fa1ac..a1bd31156f3 100644
--- a/gateway/platforms/webhook_ingress.py
+++ b/gateway/platforms/webhook_ingress.py
@@ -56,6 +56,7 @@ async def _webhook_retry(authority, event):
row = get_session_admission(authority.db, admission_id=rows[0]['admission_id'])
if len(rows) != 1 or row['payload'] != payload:
raise RuntimeStoreError('admission_conflict')
+ event._webhook_duplicate = True
return authority._receipt(row)
diff --git a/tests/gateway/fixtures/webhook_route_authority.py b/tests/gateway/fixtures/webhook_route_authority.py
new file mode 100644
index 00000000000..60fe9d97307
--- /dev/null
+++ b/tests/gateway/fixtures/webhook_route_authority.py
@@ -0,0 +1,43 @@
+"""Real durable admission for route tests; only execution is held at scheduling."""
+import asyncio
+from contextlib import suppress
+
+
+def mount_authority(app, adapter):
+ authority = None
+ async def start(app):
+ nonlocal authority
+ from gateway.config import Platform
+ from gateway.run import GatewayRunner
+ from gateway.session_authority import initialize_session_authority
+ from gateway.session import SessionStore
+ from hermes_constants import get_hermes_home
+ runner = GatewayRunner()
+ runner.session_store = SessionStore(sessions_dir=get_hermes_home() / "sessions", config=runner.config)
+ from hermes_state import SessionDB
+ runner.session_store._db = SessionDB(db_path=get_hermes_home() / "state.db")
+ runner._session_db = runner.session_store._db
+ authority = await initialize_session_authority(runner, profile_id='default', instance_id='route-test')
+ runner.adapters[Platform.WEBHOOK] = adapter
+ runner._wire_adapter_handlers(adapter)
+ # These tests inspect routing/rendering, not inference. Leave the actual
+ # committed FIFO untouched; execution is covered by test_webhook_authority.
+ authority._schedule = lambda ref: None
+ original = authority.admit_native
+ async def capture(event):
+ receipt = await original(event)
+ observer = adapter.__dict__.get('handle_message')
+ if observer is not None:
+ await observer(event)
+ return receipt
+ authority.admit_native = capture
+
+ async def cleanup(app):
+ for task in list(adapter._background_tasks):
+ task.cancel()
+ with suppress(asyncio.CancelledError):
+ await task
+ authority.db.close()
+
+ app.on_startup.append(start)
+ app.on_cleanup.append(cleanup)
diff --git a/tests/gateway/test_webhook_adapter.py b/tests/gateway/test_webhook_adapter.py
index 7aee89d9b41..1dcd7854750 100644
--- a/tests/gateway/test_webhook_adapter.py
+++ b/tests/gateway/test_webhook_adapter.py
@@ -72,6 +72,8 @@ def _create_app(adapter: WebhookAdapter) -> web.Application:
"""Build the aiohttp Application from the adapter (without starting a full server)."""
# Mirror connect(): client_max_size enforces the cap on chunked bodies.
app = web.Application(client_max_size=adapter._max_body_bytes)
+ from tests.gateway.fixtures.webhook_route_authority import mount_authority
+ mount_authority(app, adapter)
app.router.add_get("/health", adapter._handle_health)
app.router.add_post("/webhooks/{route_name}", adapter._handle_webhook)
return app
@@ -969,6 +971,7 @@ class TestMultiplexProfileWebhookAuthentication:
def _configure_profiles(adapter, tmp_path, monkeypatch):
runner = MagicMock()
runner.config.multiplex_profiles = True
+ runner._profile_name_for_source.return_value = None
adapter.gateway_runner = runner
monkeypatch.setattr(
"hermes_cli.profiles.profiles_to_serve",
@@ -1077,10 +1080,14 @@ class TestMultiplexProfileWebhookAuthentication:
self._configure_profiles(adapter, tmp_path, monkeypatch)
seen = []
- async def _capture(event):
- seen.append(event)
+ async def _capture(request, route_config, route_name, profile, payload, prompt,
+ event_type, delivery_id, now):
+ seen.append((profile, prompt))
+ return web.Response(status=202)
- adapter.handle_message = _capture
+ # Rendering-only boundary; the authority correctly refuses an unserved
+ # secondary home (covered independently by the native profile tests).
+ adapter._dispatch_agent_run = _capture
body = b'{"action":"opened"}'
headers = {
"Content-Type": "application/json",
@@ -1095,8 +1102,8 @@ class TestMultiplexProfileWebhookAuthentication:
assert resp.status == 202
await asyncio.sleep(0.05)
assert len(seen) == 1
- assert seen[0].source.profile == "worker"
- assert "Body of worker-only." in seen[0].text
+ assert seen[0][0] == "worker"
+ assert "Body of worker-only." in seen[0][1]
def test_route_profile_validation_fails_closed():
diff --git a/tests/gateway/test_webhook_integration.py b/tests/gateway/test_webhook_integration.py
index 2fae386b6a9..34a037aab18 100644
--- a/tests/gateway/test_webhook_integration.py
+++ b/tests/gateway/test_webhook_integration.py
@@ -42,6 +42,8 @@ def _make_adapter(routes, **extra_kw) -> WebhookAdapter:
def _create_app(adapter: WebhookAdapter) -> web.Application:
"""Build the aiohttp Application from the adapter."""
app = web.Application()
+ from tests.gateway.fixtures.webhook_route_authority import mount_authority
+ mount_authority(app, adapter)
app.router.add_get("/health", adapter._handle_health)
app.router.add_post("/webhooks/{route_name}", adapter._handle_webhook)
return app
@@ -232,6 +234,7 @@ class TestCrossPlatformDelivery:
mock_tg_adapter.send = AsyncMock(return_value=SendResult(success=True))
mock_runner = MagicMock()
+ mock_runner._profile_name_for_source.return_value = None
mock_runner.adapters = {Platform.TELEGRAM: mock_tg_adapter}
mock_runner.config = GatewayConfig(
platforms={Platform.TELEGRAM: PlatformConfig(enabled=True, token="fake")}
diff --git a/tests/gateway/test_webhook_signature_rate_limit.py b/tests/gateway/test_webhook_signature_rate_limit.py
index 80c7b6e3a52..d31e04bab63 100644
--- a/tests/gateway/test_webhook_signature_rate_limit.py
+++ b/tests/gateway/test_webhook_signature_rate_limit.py
@@ -41,6 +41,8 @@ def _make_adapter(routes, rate_limit=5, **extra_kw) -> WebhookAdapter:
def _create_app(adapter: WebhookAdapter) -> web.Application:
"""Build the aiohttp Application from the adapter."""
app = web.Application()
+ from tests.gateway.fixtures.webhook_route_authority import mount_authority
+ mount_authority(app, adapter)
app.router.add_get("/health", adapter._handle_health)
app.router.add_post("/webhooks/{route_name}", adapter._handle_webhook)
return app