Skip to content

The agent that grows with you

fix(gateway): answer /retry's admission refusals before the transcript rewrite

liuhao1024c4199a773a99
diff --git a/gateway/slash_commands_session.py b/gateway/slash_commands_session.py
index 6156bd33d13..fab0d337a4e 100644
--- a/gateway/slash_commands_session.py
+++ b/gateway/slash_commands_session.py
@@ -408,6 +408,12 @@ class GatewaySessionCommandsMixin:
handoff, _ = split_user_originated_turn(history[last_user_idx])
except ValueError as exc:
return t("gateway.retry.unsafe", error=exc)
+ # Answer the refusals the re-sent turn would hit BEFORE the transcript rewrite: /retry
+ # passes the post-command gates as a canonical command, the plain-text re-send does not,
+ # and a refusal after the rewrite leaves the transcript shorter and loses the user's turn.
+ readmission_reply = self._retry_readmission_reply(source, last_user_msg)
+ if readmission_reply is not None:
+ return readmission_reply
if handoff is not None:
# Composite carrier (one row = retained summary + live ask): the carrier-aware rewind
@@ -438,6 +444,22 @@ class GatewaySessionCommandsMixin:
carry_callback(event, turn)
return await self._handle_message(turn)
+ def _retry_readmission_reply(self, source: SessionSource, resend_text: str) -> Optional[str]:
+ """Refusal the /retry re-send would meet in ``_handle_message``, if any — checked before the
+ transcript rewrite so a refused /retry leaves the transcript untouched. Mirrors the gates
+ the re-sent plain-text turn passes after canonical command dispatch: the emergency-stop
+ pause (canonical commands pass it, plain turns do not), the external-drain new-turn refusal
+ and the active-session cap. Not race-free — a drain can engage or the cap can fill between
+ this check and the re-send — but every refusal known up front is answered with the
+ transcript intact."""
+ resend = MessageEvent(text=resend_text, message_type=MessageType.TEXT, source=source)
+ paused_notice = self._hm_estop_gate(resend, source, False)
+ if paused_notice is not None:
+ return paused_notice
+ if getattr(self, "_external_drain_active", False):
+ return t("gateway.busy.draining_maintenance")
+ return self._active_session_limit_message(self._session_key_for_source(source))
+
def _record_model_friction(self, signal: str, source, session_id: str, turns: int = 1) -> None:
"""Slash dispatch does not install the routed profile's scope, so a multiplexed runner
names the owning home explicitly."""
diff --git a/tests/gateway/test_retry_replacement.py b/tests/gateway/test_retry_replacement.py
index ca5bcf10500..bfb3f43c0d5 100644
--- a/tests/gateway/test_retry_replacement.py
+++ b/tests/gateway/test_retry_replacement.py
@@ -12,6 +12,7 @@ from agent.context_compressor import (
SUMMARY_PREFIX,
_SUMMARY_END_MARKER,
)
+from agent.i18n import t
from gateway.config import GatewayConfig
from gateway.platforms.event import MessageEvent, MessageType
from gateway.run import GatewayRunner
@@ -613,3 +614,117 @@ async def test_gateway_retry_preserves_archived_compaction_rows_when_probe_fails
"first question",
"retry me",
]
+
+
+def _paused_runner_with_exchange(tmp_path, monkeypatch, session_id, rows):
+ """Bare runner whose session holds one plain user/assistant exchange; returns
+ ``(gw, store, session_entry)`` with the transcript snapshot taken before /retry."""
+ import hermes_state
+ monkeypatch.setattr(hermes_state, "DEFAULT_DB_PATH", tmp_path / "state.db")
+
+ config = GatewayConfig()
+ store = SessionStore(sessions_dir=tmp_path, config=config)
+ store._db.create_session(session_id=session_id, source="test")
+ for role, content in rows:
+ store._db.append_message(session_id, role, content)
+
+ gw = GatewayRunner.__new__(GatewayRunner)
+ gw.config = config
+ gw.session_store = store
+ session_entry = MagicMock(session_id=session_id)
+ session_entry.last_prompt_tokens = 111
+ gw.session_store.get_or_create_session = MagicMock(return_value=session_entry)
+ gw._handle_message = AsyncMock()
+ return gw, store, session_entry
+
+
+async def test_gateway_retry_paused_refusal_leaves_transcript_unchanged(tmp_path, monkeypatch):
+ """The estop pause refuses the re-sent plain-text turn (the /retry command itself passes the
+ gate as a canonical command); /retry must answer it BEFORE the rewrite or the transcript
+ ends up shorter and the user's turn is lost."""
+ gw, store, session_entry = _paused_runner_with_exchange(
+ tmp_path, monkeypatch, "retry-paused-session",
+ [("user", "retry me"), ("assistant", "old answer")],
+ )
+ before = store._db.get_messages("retry-paused-session", include_inactive=True)
+ paused_notice = "⏸️ Hermes is paused. New work is on hold; run `hermes resume`."
+ monkeypatch.setattr("agent.estop.paused_reply", lambda: paused_notice)
+
+ result = await gw._handle_retry_command(
+ MessageEvent(text="/retry", message_type=MessageType.TEXT, source=MagicMock())
+ )
+
+ assert result == paused_notice
+ assert store._db.get_messages("retry-paused-session", include_inactive=True) == before
+ gw._handle_message.assert_not_awaited()
+ assert session_entry.last_prompt_tokens == 111
+
+
+async def test_gateway_retry_paused_refusal_leaves_carrier_rewind_untouched(
+ tmp_path, monkeypatch
+):
+ """Same pause pre-check on the composite-carrier path: the carrier-aware rewind must not run
+ either, or the retained scaffold is swapped for the pure scaffold with the ask gone."""
+ gw, store, session_entry = _paused_runner_with_exchange(
+ tmp_path, monkeypatch, "retry-paused-carrier-session",
+ [("user", _composite_carrier()["content"]), ("assistant", "failed answer")],
+ )
+ before = store._db.get_messages("retry-paused-carrier-session", include_inactive=True)
+ paused_notice = "⏸️ Hermes is paused. New work is on hold; run `hermes resume`."
+ monkeypatch.setattr("agent.estop.paused_reply", lambda: paused_notice)
+
+ result = await gw._handle_retry_command(
+ MessageEvent(text="/retry", message_type=MessageType.TEXT, source=MagicMock())
+ )
+
+ assert result == paused_notice
+ assert store._db.get_messages("retry-paused-carrier-session", include_inactive=True) == before
+ gw._handle_message.assert_not_awaited()
+ assert session_entry.last_prompt_tokens == 111
+
+
+async def test_gateway_retry_draining_refusal_leaves_transcript_unchanged(tmp_path, monkeypatch):
+ """An externally drained gateway refuses NEW turns after canonical command dispatch, so the
+ /retry re-send is refused; answering that refusal before the rewrite keeps the exchange."""
+ gw, store, session_entry = _paused_runner_with_exchange(
+ tmp_path, monkeypatch, "retry-draining-session",
+ [("user", "retry me"), ("assistant", "old answer")],
+ )
+ before = store._db.get_messages("retry-draining-session", include_inactive=True)
+ gw._external_drain_active = True
+
+ result = await gw._handle_retry_command(
+ MessageEvent(text="/retry", message_type=MessageType.TEXT, source=MagicMock())
+ )
+
+ assert result == t("gateway.busy.draining_maintenance")
+ assert store._db.get_messages("retry-draining-session", include_inactive=True) == before
+ gw._handle_message.assert_not_awaited()
+ assert session_entry.last_prompt_tokens == 111
+
+
+async def test_gateway_retry_session_cap_refusal_leaves_transcript_unchanged(
+ tmp_path, monkeypatch
+):
+ """max_concurrent_sessions rejects the re-sent turn's slot claim; the same cap message must
+ be answerable before the rewrite so the exchange survives for a later /retry."""
+ gw, store, session_entry = _paused_runner_with_exchange(
+ tmp_path, monkeypatch, "retry-cap-session",
+ [("user", "retry me"), ("assistant", "old answer")],
+ )
+ before = store._db.get_messages("retry-cap-session", include_inactive=True)
+ limit_message = "⏸️ Active session limit reached (4/4)."
+ gw._active_session_limit_message = MagicMock(return_value=limit_message)
+
+ result = await gw._handle_retry_command(
+ MessageEvent(text="/retry", message_type=MessageType.TEXT, source=MagicMock())
+ )
+
+ assert result == limit_message
+ assert store._db.get_messages("retry-cap-session", include_inactive=True) == before
+ gw._handle_message.assert_not_awaited()
+ assert session_entry.last_prompt_tokens == 111