Skip to content

The agent that grows with you

fix(webhook): resolve the coalesced edge check on admission's own source

Teknium041e063d2356
diff --git a/gateway/platforms/webhook.py b/gateway/platforms/webhook.py
index df967b28d45..eb1e07ef223 100644
--- a/gateway/platforms/webhook.py
+++ b/gateway/platforms/webhook.py
@@ -72,6 +72,12 @@ _REPO_RE = re.compile(r"[A-Za-z0-9._-]+/[A-Za-z0-9._-]+")
_GH_TOKEN_VARS = ("GH_TOKEN", "GITHUB_TOKEN")
+def _header_delivery_id(headers) -> str:
+ """The provider's delivery id (GitHub, Svix, Standard Webhooks, generic), else a fresh one."""
+ return headers.get("X-GitHub-Delivery", headers.get("svix-id", headers.get(
+ "webhook-id", headers.get("X-Request-ID", uuid.uuid4().hex))))
+
+
@dataclass(frozen=True)
class _WebhookDeliveryIdentity:
"""Collision-free identity for one provider delivery on one routed webhook route."""
@@ -657,9 +663,16 @@ class WebhookAdapter(BasePlatformAdapter):
route_name, route_config, profile, error_response = self._resolve_route(request)
if error_response is None:
raw_body, error_response = await self._read_authenticated_body(request, route_name, route_config)
- if error_response is None: # a parked profile is retried, before any delivery id is recorded
- from gateway.platforms.webhook_ingress import parked_profile_refusal
- error_response = parked_profile_refusal(self.gateway_runner, profile, route_name, route_config)
+ if error_response is not None:
+ return error_response
+ headers = request.headers
+ delivery_id = _header_delivery_id(headers)
+ # A parked profile is retried, before any delivery id is recorded. The check resolves
+ # ``profile_routes`` against the very source admission builds for this delivery.
+ from gateway.platforms.webhook_ingress import parked_profile_refusal
+ error_response = parked_profile_refusal(
+ self.gateway_runner, profile, route_config,
+ lambda: self._admission_source(route_name, profile, delivery_id))
if error_response is not None:
return error_response
# Rate limiting (after auth)
@@ -668,7 +681,6 @@ class WebhookAdapter(BasePlatformAdapter):
payload = self._parse_body(raw_body)
if payload is _UNPARSEABLE:
return _json_error("Cannot parse body", 400)
- headers = request.headers
event_type = (headers.get("X-GitHub-Event", "") or headers.get("X-GitLab-Event", "")
or payload.get("event_type", "") or payload.get("type", "") or "unknown")
allowed_events = route_config.get("events", [])
@@ -697,8 +709,6 @@ class WebhookAdapter(BasePlatformAdapter):
# cron_job routes: the job's own skills apply; the rendered prompt is only per-run context.
if (skills := route_config.get("skills", [])) and not route_config.get("cron_job"):
prompt = self._apply_skills(prompt, skills)
- delivery_id = headers.get("X-GitHub-Delivery", headers.get("svix-id", headers.get(
- "webhook-id", headers.get("X-Request-ID", uuid.uuid4().hex))))
now = time.time() # idempotency: skip duplicate deliveries (webhook retries)
delivery_identity = _WebhookDeliveryIdentity.from_parts(profile, route_name, delivery_id)
coalesce = route_config.get("coalesce")
@@ -755,6 +765,21 @@ class WebhookAdapter(BasePlatformAdapter):
task.add_done_callback(self._background_tasks.discard)
return task
+ def _admission_source(self, route_name: str, profile, delivery_id: str):
+ """The ``SessionSource`` admission builds for one delivery, ``profile_routes`` resolved on it.
+ The edge's coalesced-route check (``parked_profile_refusal``) asks this same source, so a
+ route rule matches there exactly when it matches at admission."""
+ identity = _WebhookDeliveryIdentity.from_parts(profile, route_name, delivery_id)
+ source = self.build_source(chat_id=identity.session_chat_id, chat_name=f"webhook/{route_name}",
+ chat_type="webhook", user_id=f"webhook:{route_name}", user_name=route_name)
+ if profile and isinstance(profile, str):
+ # The ``/p/<profile>/`` prefix names the profile and wins over ``profile_routes``; the
+ # edge already refused it (retryable 503) when that profile is not served. Drop what
+ # ``build_source`` resolved from the routes, or a rejected route would refuse it here.
+ source.profile = profile
+ source.profile_route_rejected, source._rejected_route_profile = False, None
+ return source
+
async def _admit_agent_run(self, payload: Any, prompt: str, delivery_id: str, now: float, *, route_config: dict,
route_name: str, profile, event_type: str) -> Optional[str]:
"""Record delivery info and admit the run through the durable producer (shared by the immediate
@@ -772,14 +797,7 @@ class WebhookAdapter(BasePlatformAdapter):
self._delivery_info_created[session_chat_id] = now
self._delivery_info_order.append((now, session_chat_id))
self._prune_delivery_info(now)
- source = self.build_source(chat_id=session_chat_id, chat_name=f"webhook/{route_name}", chat_type="webhook",
- user_id=f"webhook:{route_name}", user_name=route_name)
- if profile and isinstance(profile, str):
- # The ``/p/<profile>/`` prefix names the profile and wins over ``profile_routes``; the
- # edge already refused it (retryable 503) when that profile is not served. Drop what
- # ``build_source`` resolved from the routes, or a rejected route would refuse it here.
- source.profile = profile
- source.profile_route_rejected, source._rejected_route_profile = False, None
+ source = self._admission_source(route_name, profile, delivery_id)
event = MessageEvent(text=prompt, message_type=MessageType.TEXT, source=source, raw_message=payload,
message_id=delivery_id, timestamp=datetime.fromtimestamp(0, UTC))
from gateway.platforms.webhook_ingress import admit_producer
diff --git a/gateway/platforms/webhook_ingress.py b/gateway/platforms/webhook_ingress.py
index 406d85557de..f2dd6c16dc4 100644
--- a/gateway/platforms/webhook_ingress.py
+++ b/gateway/platforms/webhook_ingress.py
@@ -54,12 +54,14 @@ def _refuse_parked(runner, profile):
raise ParkedProfileRefusal(profile)
-def parked_profile_refusal(runner, profile, route_name, route_config):
+def parked_profile_refusal(runner, profile, route_config, admission_source):
"""A 503 for a webhook whose profile is not served right now, else None: a ``/p/<profile>/``
prefix that is parked or has no serving authority here, or a coalesced route that
``profile_routes`` sends to a profile admission would refuse (a coalesced delivery is acknowledged
before admission applies that route). A parked target is retryable (``Retry-After``); any other
- rejected target gets the answer the immediate route gives when admission refuses it. Answered
+ rejected target gets the answer the immediate route gives when admission refuses it.
+ ``admission_source`` returns the ``SessionSource`` admission builds for this delivery
+ (``WebhookAdapter._admission_source``), so ``profile_routes`` match here exactly as there. Answered
before the delivery id is recorded: coalesced, ``deliver_only`` and ``cron_job`` routes act before
(or without) an admission, so the in-memory id is their only replay guard, and a recorded id would
answer the provider's retry as a duplicate once the profile is served again."""
@@ -67,7 +69,7 @@ def parked_profile_refusal(runner, profile, route_name, route_config):
if _prefix_not_served(runner, profile):
error = f'Profile {profile} is not being served; retry this delivery'
else:
- refusal = _coalesced_route_refusal(runner, profile, route_name, route_config)
+ refusal = _coalesced_route_refusal(runner, profile, route_config, admission_source)
if refusal is None:
return None
if refusal == 'refused':
@@ -93,31 +95,23 @@ def _prefix_not_served(runner, profile):
return authority is None or getattr(authority, 'retiring', False) is True
-def _coalesced_route_refusal(runner, profile, route_name, route_config):
+def _coalesced_route_refusal(runner, profile, route_config, admission_source):
"""How admission would refuse a coalesced bare-route delivery that ``profile_routes`` sends away:
``'parked'`` (retryable), ``'refused'`` (a profile neither served here nor parked: standalone,
- unknown, or without a live authority), else None. 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."""
+ unknown, or without a live authority), else None. Reads the profile the admission's own source
+ resolved (``build_source`` asks ``GatewayRunner._profile_name_for_source`` on the same chat_id,
+ user_id and names), so this edge and admission give one answer: an explicit ``/p/<profile>/``
+ wins over ``profile_routes`` there, and routes apply only under multiplexing."""
if profile or not isinstance(route_config.get('coalesce'), dict):
return None
- resolve = getattr(runner, '_profile_name_for_source', None)
- if not callable(resolve):
- return None
- from gateway.config import Platform
- 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``).
- 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:
+ source = admission_source()
+ if getattr(source, 'profile_route_rejected', False):
# 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.
- return 'parked' if exc.profile and profile_parked(runner, exc.profile) else 'refused'
+ rejected = getattr(source, '_rejected_route_profile', None)
+ return 'parked' if rejected and profile_parked(runner, rejected) else 'refused'
+ routed = source.profile
if not routed:
return None
if profile_parked(runner, routed):
diff --git a/tests/gateway/test_parked_profile_ingress.py b/tests/gateway/test_parked_profile_ingress.py
index 876d6ead67f..aecff3fef73 100644
--- a/tests/gateway/test_parked_profile_ingress.py
+++ b/tests/gateway/test_parked_profile_ingress.py
@@ -355,6 +355,38 @@ async def test_a_coalesced_bare_route_to_an_unserved_profile_is_refused_like_the
_close(runner, homes)
+async def test_a_route_rule_admission_never_matches_does_not_refuse_the_coalesced_route(tmp_path, monkeypatch):
+ """Admission's source carries ``chat_id=webhook:v2:<token>`` (per delivery), not
+ ``webhook:<route>``: a ``profile_routes`` rule keyed on ``chat_id: webhook:<route>`` never matches
+ there, so the delivery is admitted on default. The coalesced route's edge check must ask that
+ same source and answer what the immediate route answers (202), not a 503 forever."""
+ from gateway.platforms.webhook import _INSECURE_NO_AUTH
+ from gateway.profile_routing import ProfileRoute
+ homes = _homes(tmp_path, monkeypatch)
+ root, beta = homes[0][1].resolve(), homes[1][1].resolve()
+ runner = await _runner_with_beta_parked(tmp_path, monkeypatch, homes, profile_routes=[
+ ProfileRoute(name=f'{route}-to-ghost', platform='webhook', profile='ghost', chat_id=f'webhook:{route}')
+ for route in ('events', 'events_now')])
+ await _serve_beta_again(runner, beta)
+ adapter = _webhook(runner, {
+ 'events': {'secret': _INSECURE_NO_AUTH, 'prompt': 'event {id}', 'coalesce': {'key': 'id', 'window_seconds': 0.05}},
+ 'events_now': {'secret': _INSECURE_NO_AUTH, 'prompt': 'event {id}'}})
+ settled = lambda: not adapter._coalescer.pending and not adapter._background_tasks
+ try:
+ immediate = await adapter._handle_webhook(_webhook_request('delivery-1', {'id': 17}, 'events_now', None))
+ coalesced = await adapter._handle_webhook(_webhook_request('delivery-2', {'id': 18}, 'events', None))
+ assert immediate.status == 202, (immediate.status, immediate.text)
+ assert coalesced.status == 202 and json.loads(coalesced.text)['status'] == 'coalesced', \
+ (coalesced.status, coalesced.text)
+ await async_wait_until(settled, what='the coalesced batch dispatched')
+ await async_wait_until(lambda: _admissions(root) == 2, what='both deliveries admitted on default')
+ assert _admissions(beta) == 0
+ 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(