Skip to content

The agent that grows with you

fix(webhook): don't 503 a coalesced delivery to a served /p/<profile>/

Tekniumbbb0bf4f42fb
diff --git a/gateway/platforms/webhook_ingress.py b/gateway/platforms/webhook_ingress.py
index eb55166abde..ecdb2056d52 100644
--- a/gateway/platforms/webhook_ingress.py
+++ b/gateway/platforms/webhook_ingress.py
@@ -64,7 +64,7 @@ def parked_profile_refusal(runner, profile, route_name, route_config):
once the profile is served again."""
if _prefix_not_served(runner, profile):
error = f'Profile {profile} is not being served; retry this delivery'
- elif _coalesced_to_parked(runner, route_name, route_config):
+ elif _coalesced_to_parked(runner, profile, route_name, route_config):
error = 'The routed profile is not being served right now; retry this delivery'
else:
return None
@@ -87,16 +87,30 @@ def _prefix_not_served(runner, profile):
return authority is None or getattr(authority, 'retiring', False) is True
-def _coalesced_to_parked(runner, route_name, route_config):
- routes = getattr(getattr(runner, 'config', None), 'profile_routes', None)
- if not isinstance(route_config.get('coalesce'), dict) or not routes:
+def _coalesced_to_parked(runner, profile, route_name, route_config):
+ """A coalesced bare-route delivery that ``profile_routes`` sends to a parked profile. Asks the
+ admission's own resolver (``GatewayRunner._profile_name_for_source``) so this edge and admission
+ give one answer: an explicit ``/p/<profile>/`` wins over ``profile_routes`` there (the source's
+ ``profile`` is set), and routes apply only under multiplexing."""
+ if profile or not isinstance(route_config.get('coalesce'), dict):
+ return False
+ resolve = getattr(runner, '_profile_name_for_source', None)
+ if not callable(resolve):
return False
from gateway.config import Platform
- from gateway.profile_routing import match_profile_route
+ from gateway.profile_routing import ProfileRouteRejected
from gateway.run_runtime import profile_parked
+ from gateway.session import SessionSource
# The source the admission builds carries ``user_id=webhook:<route>`` (``_admit_agent_run``).
- route = match_profile_route(routes, platform=Platform.WEBHOOK.value, user_id=f'webhook:{route_name}')
- return route is not None and profile_parked(runner, route.profile)
+ source = SessionSource(platform=Platform.WEBHOOK, chat_id=f'webhook:{route_name}', chat_type='webhook',
+ user_id=f'webhook:{route_name}')
+ try:
+ routed = resolve(source)
+ except ProfileRouteRejected as exc:
+ # A parked target leaves the served set, so the resolver rejects it; admission retries
+ # that one (``_rejected_route_profile``) and refuses any other rejection for good.
+ routed = exc.profile
+ return bool(routed) and profile_parked(runner, routed)
async def admit_producer(adapter, event):
diff --git a/tests/gateway/test_parked_profile_ingress.py b/tests/gateway/test_parked_profile_ingress.py
index 96616f8a9ea..49c54e95679 100644
--- a/tests/gateway/test_parked_profile_ingress.py
+++ b/tests/gateway/test_parked_profile_ingress.py
@@ -182,12 +182,12 @@ async def test_a_relay_frame_for_a_parked_profile_is_refused_to_its_sender_not_h
_close(runner, homes)
-def _webhook_request(delivery_id, body, route='events'):
+def _webhook_request(delivery_id, body, route='events', profile='beta'):
raw = json.dumps(body).encode()
request = MagicMock()
request.headers = {'X-GitHub-Delivery': delivery_id}
request.content_length = len(raw)
- request.match_info = {'route_name': route, 'profile': 'beta'}
+ request.match_info = {'route_name': route, 'profile': profile}
request.method = 'POST'
async def read():
@@ -234,6 +234,29 @@ async def test_a_coalesced_webhook_for_a_parked_profile_is_retried_not_lost(tmp_
_close(runner, homes)
+async def test_a_coalesced_webhook_to_a_served_profile_prefix_ignores_a_parked_profile_route(tmp_path, monkeypatch):
+ """Admission keeps an explicit ``/p/<profile>/`` (``source.profile`` wins; ``profile_routes``
+ applies only when it is unset), so a ``profile_routes`` rule that sends the bare route's
+ ``webhook:<route>`` user to parked beta must not 503 the same route delivered to a served
+ ``/p/default/``. The bare delivery is still the one the rule routes to beta, so it is retried."""
+ from gateway.platforms.webhook import _INSECURE_NO_AUTH
+ from gateway.profile_routing import ProfileRoute
+ homes = _homes(tmp_path, monkeypatch)
+ runner = await _runner_with_beta_parked(tmp_path, monkeypatch, homes, profile_routes=[
+ ProfileRoute(name='events-to-beta', platform='webhook', profile='beta', user_id='webhook:events')])
+ adapter = _webhook(runner, {'events': {'secret': _INSECURE_NO_AUTH, 'prompt': 'event {id}',
+ 'coalesce': {'key': 'id', 'window_seconds': 30}}})
+ try:
+ served = await adapter._handle_webhook(_webhook_request('delivery-1', {'id': 17}, profile='default'))
+ assert served.status == 202 and json.loads(served.text)['status'] == 'coalesced', (served.status, served.text)
+ bare = await adapter._handle_webhook(_webhook_request('delivery-2', {'id': 18}, profile=None))
+ assert bare.status == 503 and bare.headers.get('Retry-After'), (bare.status, bare.text)
+ finally:
+ _cancel(adapter)
+ _close(runner, homes)
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize('mode', ['deliver_only', 'cron_job'])
async def test_a_non_durable_webhook_for_a_parked_profile_is_refused_before_its_id_is_recorded(