Skip to content

The agent that grows with you

fix: post_gateway_admission fires once at ingress under the session authority

Tekniumf45a26cfd861
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.py
index cd61d0f09ad..35abb79276f 100644
--- a/gateway/run_inbound.py
+++ b/gateway/run_inbound.py
@@ -1360,6 +1360,12 @@ class GatewayInboundMixin(GatewayPluginInjectionMixin):
# its whole request timeout.
if self._is_session_running(_quick_key) and await self._hm_busy_preempt(event, source, _quick_key):
return None
+ # Ingress-side, once per inbound message (#129958): a consumed message is never
+ # admitted, and a committed row never re-fires it at execution or restart replay.
+ from gateway.run_inbound_consumer import run_post_admission_hook
+ _consumed, _consumer_reply = await run_post_admission_hook(self, event, source, _quick_key)
+ if _consumed:
+ return _consumer_reply
return await admit_message(authority, event)
if (authority is None and not is_internal and not event.get_command()
and getattr(self, 'session_authority', None) is not None):
@@ -1415,7 +1421,10 @@ class GatewayInboundMixin(GatewayPluginInjectionMixin):
_run_generation = self._begin_session_run_generation(_quick_key)
try:
- if not is_internal: # fail-open plugin consume, inside the claimed slot (#129958)
+ # Fail-open plugin consume inside the claimed slot (#129958). An admitted row already
+ # ran it at ingress, before its commit: execution never offers the same message twice.
+ from gateway.session_ingress import executing_admission
+ if not is_internal and not executing_admission.get():
from gateway.run_inbound_consumer import run_post_admission_hook
_consumed, _consumer_reply = await run_post_admission_hook(self, event, source, _quick_key)
if _consumed:
diff --git a/tests/gateway/test_post_gateway_admission.py b/tests/gateway/test_post_gateway_admission.py
index fe945e5ed15..0febb479745 100644
--- a/tests/gateway/test_post_gateway_admission.py
+++ b/tests/gateway/test_post_gateway_admission.py
@@ -122,3 +122,35 @@ async def test_consumer_fires_in_the_routed_profile_scope_only(tmp_path):
assert fired == [("beta", routed)]
runner._handle_message_with_agent.assert_not_awaited()
+
+
+async def test_under_an_authority_the_hook_fires_once_at_ingress_never_at_execution(monkeypatch):
+ """A consumed message is never committed, and an admitted row's execution (first run or
+ restart replay) never offers the same message to the plugin again."""
+ import gateway.session_authorities as authorities
+ import gateway.session_ingress as ingress
+
+ fired = []
+ _register(lambda **kwargs: fired.append(kwargs["text"]) or (
+ {"action": "handled", "reply": "consumed"} if kwargs["text"] == "eat me" else None))
+ runner = _runner()
+ admitted = []
+
+ async def _admit(_authority, event):
+ admitted.append(event.text)
+ return "admitted"
+
+ monkeypatch.setattr(authorities, "active_authority", lambda _runner: object())
+ monkeypatch.setattr(ingress, "admit_message", _admit)
+
+ assert await runner._handle_message(_event("eat me")) == "consumed"
+ assert await runner._handle_message(_event("keep me")) == "admitted"
+ assert admitted == ["keep me"] and fired == ["eat me", "keep me"]
+
+ token = ingress.executing_admission.set(True)
+ try:
+ assert await runner._handle_message(_event("keep me")) == "agent-ok"
+ finally:
+ ingress.executing_admission.reset(token)
+ assert fired == ["eat me", "keep me"]