diff --git a/gateway/session_ingress.py b/gateway/session_ingress.py index daba939660..ddc72b0913 100644 --- a/gateway/session_ingress.py +++ b/gateway/session_ingress.py @@ -57,6 +57,12 @@ async def admit_message(authority, event): named = f'{exc.reason}: {exc.field}' if exc.field in _CONTENT_CLAUSES else exc.reason return (f'⚠️ Hermes refused this message ({named}), so it was not saved ' 'or answered. Ask the operator to check gateway.log.') + runner = authority.runner + if exc.reason == 'runtime_draining' and runner._draining: + # A restart's after-turn wait or a stop refused it before commit: the same notice the + # in-process path gives, not a turn error that offers a /retry into the drain (#152410). + from agent.i18n import t + return t('gateway.busy.drain_rejected_new_work', action=runner._status_action_gerund()) if exc.reason != 'queue_full': raise # Nothing was committed (main's busy-queue cap): say so instead of a generic turn error. diff --git a/tests/gateway/test_restart_wait_messaging_drain_notice.py b/tests/gateway/test_restart_wait_messaging_drain_notice.py new file mode 100644 index 0000000000..1c39908653 --- /dev/null +++ b/tests/gateway/test_restart_wait_messaging_drain_notice.py @@ -0,0 +1,29 @@ +"""A chat message refused by a restart's after-turn wait is answered with the drain notice. + +While ``request_restart`` holds the gateway in ``draining`` for an in-flight turn (up to +``agent.restart_after_turn_timeout``), the session authority's admission gate refuses every new +messaging input with ``runtime_draining`` (#152410). That refusal must reach the sender as the same +"Gateway is restarting" notice the in-process path gives, not escape ``_handle_message`` as an +exception the adapter turns into the generic "Something went wrong… /retry" turn error. Real +GatewayRunner, DiscordAdapter and SessionAuthority over SQLite. +""" + +import pytest + +from tests.gateway.test_wake_during_restart_drain_authority import admissions, authority_route + + +@pytest.mark.asyncio +async def test_message_during_restart_wait_gets_the_drain_notice_not_a_turn_error(monkeypatch): + from agent.i18n import t + from gateway.platforms.event import MessageEvent + + executed = [] + runner, _adapter, authority, source = await authority_route(monkeypatch, executed) + runner._restart_requested = True + runner._draining = True # request_restart: refusing new turns while the in-flight one finishes + + reply = await runner._handle_message(MessageEvent(text="are you there?", source=source, message_id="B")) + + assert reply == t("gateway.busy.drain_rejected_new_work", action=runner._status_action_gerund()) + assert executed == [] and admissions(runner, authority, source) == [] # nothing half-admitted