The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webhook): an explicit /p/<profile>/ wins over profile_routes
Tekniumaf338b875ff1
diff --git a/gateway/platforms/webhook.py b/gateway/platforms/webhook.pyindex 3bd608535c6..df967b28d45 100644--- a/gateway/platforms/webhook.py+++ b/gateway/platforms/webhook.py@@ -775,7 +775,11 @@ class WebhookAdapter(BasePlatformAdapter):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, Noneevent = 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_producerdiff --git a/gateway/session_ingress_context.py b/gateway/session_ingress_context.pyindex 63a7c599204..3370a970b9a 100644--- a/gateway/session_ingress_context.py+++ b/gateway/session_ingress_context.py@@ -90,6 +90,9 @@ def carry_callback(event, rewritten):bound_var.set((bound[0], rewritten, *bound[2:]))+_PREFIX_PRODUCERS = ('webhook', 'api_server', 'msgraph_webhook')++def _binding(runner, source, profile):registries = [(None, runner.adapters), *getattr(runner, '_profile_adapters', {}).items()]from gateway.config import Platform@@ -135,16 +138,17 @@ def _binding(runner, source, profile):source.platform.value, guild_id=source.scope_id, chat_id=source.chat_id,thread_id=source.thread_id, parent_chat_id=source.parent_chat_id,adapter_profile=profile, user_id=source.user_id)]- if matches:+ if source.profile and source.platform.value in _PREFIX_PRODUCERS:+ # ``/p/<profile>/`` producers address a profile explicitly on the HOST's listener; that+ # URL prefix names the profile and wins over ``profile_routes`` (webhook.py /+ # api_server.py stamp it; a bare producer source carries the routed name itself).+ runtime_profile = source.profile+ elif matches:rank = max(route.specificity for route in matches)best = [route for route in matches if route.specificity == rank]if len(best) != 1:raise RuntimeStoreError('admission_conflict')runtime_profile = best[0].profile- elif source.profile and source.profile in homes and source.platform.value in ('webhook', 'api_server', 'msgraph_webhook'):- # ``/p/<profile>/`` producers address a served profile explicitly on the HOST's- # listener; that URL prefix is the route (webhook.py / api_server.py stamp it).- runtime_profile = source.profileelse:runtime_profile = owner_nameif (source.profile or primary) != runtime_profile or runtime_profile not in homes:diff --git a/tests/gateway/test_parked_profile_ingress.py b/tests/gateway/test_parked_profile_ingress.pyindex fda5e4803e3..40bded6e94f 100644--- a/tests/gateway/test_parked_profile_ingress.py+++ b/tests/gateway/test_parked_profile_ingress.py@@ -260,6 +260,62 @@ async def test_a_coalesced_webhook_to_a_served_profile_prefix_ignores_a_parked_p_close(runner, homes)[email protected]('beta_parked', [True, False])[email protected]('coalesced', [True, False])+async def test_an_explicit_profile_prefix_wins_over_a_profile_route(tmp_path, monkeypatch, beta_parked, coalesced):+ """``/p/default/`` names the profile: a ``profile_routes`` rule sending the route's+ ``webhook:<route>`` user to beta must not re-home it. The delivery is admitted on default, never+ acknowledged and then dropped at flush as ``profile_mismatch``."""+ 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='events-to-beta', platform='webhook', profile='beta', user_id='webhook:events')])+ if not beta_parked:+ await _serve_beta_again(runner, beta)+ route = {'secret': _INSECURE_NO_AUTH, 'prompt': 'event {id}'}+ if coalesced:+ route['coalesce'] = {'key': 'id', 'window_seconds': 0.1}+ adapter = _webhook(runner, {'events': route})+ settled = lambda: not adapter._coalescer.pending and not adapter._background_tasks+ try:+ response = await adapter._handle_webhook(_webhook_request('delivery-1', {'id': 17}, profile='default'))+ assert response.status == 202, (response.status, response.text)+ await async_wait_until(lambda: _admissions(root) == 1, what='the /p/default/ delivery admitted on default')+ await async_wait_until(settled, what='the delivery dispatched')+ assert _admissions(beta) == 0+ finally:+ _cancel(adapter)+ _close(runner, homes)++[email protected]('coalesced', [True, False])+async def test_an_explicit_parked_profile_prefix_is_retried_even_when_a_route_points_elsewhere(+ tmp_path, monkeypatch, coalesced):+ """``/p/beta/`` with beta parked is a retryable 503, never a 202, even when a ``profile_routes``+ rule would send the route's user to the served default."""+ from gateway.platforms.webhook import _INSECURE_NO_AUTH+ from gateway.profile_routing import ProfileRoute+ homes = _homes(tmp_path, monkeypatch)+ root = homes[0][1].resolve()+ runner = await _runner_with_beta_parked(tmp_path, monkeypatch, homes, profile_routes=[+ ProfileRoute(name='events-to-default', platform='webhook', profile='default', user_id='webhook:events')])+ route = {'secret': _INSECURE_NO_AUTH, 'prompt': 'event {id}', 'profile': 'beta'}+ if coalesced:+ route['coalesce'] = {'key': 'id', 'window_seconds': 0.1}+ adapter = _webhook(runner, {'events': route})+ try:+ response = await adapter._handle_webhook(_webhook_request('delivery-1', {'id': 17}, profile='beta'))+ assert response.status == 503 and response.headers.get('Retry-After'), (response.status, response.text)+ assert _admissions(root) == 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(