The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(relay): prompt-lifecycle acks are interim sends — the approval ack was sealing the turn's own draft stream
Victor Kyriazakos57fe01e367f8
diff --git a/gateway/relay/adapter.py b/gateway/relay/adapter.pyindex 7536062ae42..403f1f58f53 100644--- a/gateway/relay/adapter.py+++ b/gateway/relay/adapter.py@@ -107,6 +107,10 @@ class RelayAdapter(BasePlatformAdapter):# per-chat keying collided three concurrent turns: merged task# cards, clobbered seal state, 3x duplicate finals).self._open_draft_by_chat: Dict[str, int] = {}+ # Draft keys whose post-seal tombstone swallow has been logged once+ # (observability for the hijacked-live-stream class; bounded FIFO+ # like the sibling caches).+ self._tombstone_swallow_logged: Dict[str, int] = {}# chat_id -> draft_id of the most recently SEALED stream (gateway# mirror of the connector's sealed-key tombstone): post-seal# straggler frames must neither re-arm interception nor re-open a@@ -465,6 +469,15 @@ class RelayAdapter(BasePlatformAdapter):k for k in self._open_draft_by_chat if k.startswith(prefix)]if len(candidates) == 1:+ # Absorbing a send into a stream is a significant, previously+ # silent decision — the wrong caller matching here is exactly+ # the prompt-ack-seals-own-stream bug (rc.4 live finding). Say+ # it out loud so the next mismatch is a grep, not a hunt.+ logger.info(+ "relay: absorbing identity-less send into the single open "+ "stream %s (single-open-stream fallback)",+ candidates[0],+ )return candidates[0]return None@@ -494,7 +507,24 @@ class RelayAdapter(BasePlatformAdapter):chat_key = self._draft_key(str(chat_id), metadata)if self._sealed_draft_by_chat.get(chat_key) == draft_id:# Post-seal straggler: its content is already in the sealed- # message; report success, send nothing, arm nothing.+ # message; report success, send nothing, arm nothing. Log the+ # FIRST swallow per key at WARNING — one straggler is the+ # normal race this tombstone exists for, but a hijacked live+ # stream (something else sealed this draft mid-flight) shows+ # up as a burst of swallows, and silence here cost a full+ # forensic hunt (rc.4: prompt ack sealed the turn's own draft+ # and every later append vanished without a line).+ if chat_key not in self._tombstone_swallow_logged:+ self._tombstone_swallow_logged[chat_key] = draft_id+ self._evict_oldest(self._tombstone_swallow_logged)+ logger.warning(+ "relay: draft frame for %s swallowed by post-seal "+ "tombstone (draft_id=%s) — expected for a straggler; "+ "a live stream freezing NOW means something sealed it "+ "mid-flight",+ chat_key,+ draft_id,+ )return SendResult(success=True)# Arm seal-interception ONLY for stream-is-the-message chats# (review B4, per-chat in r2 finding 2): on a Telegram-shaped@@ -3010,8 +3040,21 @@ class RelayAdapter(BasePlatformAdapter):logger.debug("relay expired-prompt notice failed", exc_info=True)def _prompt_reply_metadata(self, event) -> Dict[str, Any]:- """Thread/topic metadata so prompt acks land where the prompt lives."""- meta: Dict[str, Any] = {}+ """Thread/topic metadata so prompt acks land where the prompt lives.++ Marked as an INTERIM send (live finding, rc.4 staging): prompt+ lifecycle acks ("✅ Approved once", slash-confirm acks, expiry+ notices) are system messages that fire while the approval turn's+ OWN draft stream is open. Without the interim marker they carry+ only placement metadata — no per-turn identity — so send()'s+ single-open-stream fallback matched them to the live draft and+ sealed it with the ack text. Every later append then died on the+ post-seal tombstone (silent by design), freezing the visible+ stream mid-word, and the real turn-final fell through to a plain+ send: the observed 100%-reproducible stuck-draft + duplicate-final+ on approval turns. Interim sends bypass draft matching entirely.+ """+ meta: Dict[str, Any] = {"_interim_send": True}thread_id = getattr(event.source, "thread_id", None)if thread_id:meta["thread_id"] = str(thread_id)diff --git a/tests/relay/test_relay_prompt_ack_stream_isolation.py b/tests/relay/test_relay_prompt_ack_stream_isolation.pynew file mode 100644index 00000000000..d18971855ef--- /dev/null+++ b/tests/relay/test_relay_prompt_ack_stream_isolation.py@@ -0,0 +1,154 @@+"""Prompt-ack sends must never seal an open draft stream.++Live finding (rc.4 staging, 100% reproducible on approval turns): the+"✅ Approved once" acknowledgement the adapter sends after resolving an+exec-approval prompt_response carried only placement metadata (thread_id)+— no per-turn message identity and no interim marker. send()'s+single-open-stream fallback (review B2: "a chat with exactly one open+stream absorbs a turn-final arriving without identity") therefore matched+the approval turn's OWN live draft and sealed it with the ack text:++ * every subsequent append hit the post-seal tombstone (silently+ swallowed by design — built for millisecond stragglers), freezing the+ visible draft mid-word with zero log lines;+ * the turn-final then found no open draft and fell through to a plain+ send — the duplicate (fallback) message the user saw. Also silent:+ no suppression line, no seal-failed warning.++The fix marks every prompt-lifecycle system send (approval ack, slash-+confirm ack, expiry notice) as an interim send, which bypasses draft+matching entirely. These tests pin the whole class, plus the regression+contract that real turn-finals still absorb into their stream.+"""++import json++import pytest++from gateway.config import PlatformConfig+from gateway.relay.adapter import RelayAdapter+from gateway.relay.descriptor import CapabilityDescriptor+++def _descriptor(**overrides):+ base = dict(+ contract_version=1,+ platform="slack",+ label="Slack",+ max_message_length=4000,+ supports_draft_streaming=True,+ supports_edit=True,+ supports_threads=True,+ markdown_dialect="mrkdwn",+ len_unit="chars",+ supported_ops=("send", "edit", "draft", "prompt"),+ )+ base.update(overrides)+ return CapabilityDescriptor(**base)+++class FakeTransport:+ def __init__(self):+ self.frames = []+ self._identities = [("slack", "hermes")]++ def descriptor_for_platform(self, platform):+ return None++ async def send_outbound(self, frame, platform=None):+ self.frames.append((frame, platform))+ return {"success": True, "message_id": "1.2"}+++class FakeSource:+ chat_id = "D01"+ thread_id = "111.222"+++class FakeEvent:+ def __init__(self, prompt_id, option_id):+ self.prompt_response = {"prompt_id": prompt_id, "option_id": option_id}+ self.source = FakeSource()+++def _adapter():+ config = PlatformConfig(enabled=True, extra={})+ return RelayAdapter(config, _descriptor(), transport=FakeTransport())+++async def _open_turn_draft(a, chat_id="D01", draft_id=7):+ """Open a live draft the way a streaming turn does."""+ await a.send_draft(chat_id, draft_id, "streaming partial…")+ key = a._draft_key(chat_id, None)+ candidates = [k for k in a._open_draft_by_chat if k.startswith(f"{chat_id}:")]+ assert candidates, "test setup: draft did not arm interception"+ return candidates[0]+++class TestPromptAckDoesNotSealDraft:+ @pytest.mark.asyncio+ async def test_approval_ack_leaves_open_draft_untouched(self):+ """The exact live failure: exec-approval tap resolves while the+ turn's draft stream is open; the ack must go out as its own plain+ send and the draft must STILL be armed afterwards."""+ a = _adapter()+ draft_key = await _open_turn_draft(a)++ prompt_id = a._mint_prompt(+ "exec_approval", {"session_key": "sess-1", "chat_id": "D01"}+ )+ # resolve_gateway_approval is imported inside the handler; a session+ # with no waiting entry returns 0 -> "expired" label. Either label+ # shape exercises the same send path.+ consumed = await a._consume_prompt_response(+ FakeEvent(prompt_id, "once")+ )+ assert consumed is True++ # The draft interception must still be armed for the real turn-final.+ assert draft_key in a._open_draft_by_chat, (+ "prompt ack sealed the open draft — the live stuck-stream/"+ "duplicate-final bug"+ )+ # The ack egressed as a plain send op, not a draft seal.+ ack_frames = [+ f for f, _ in a._transport.frames if f["op"] == "send"+ ]+ assert ack_frames, "ack was not sent at all"+ seal_frames = [+ f+ for f, _ in a._transport.frames+ if f["op"] == "draft" and f.get("final") is True+ ]+ assert not seal_frames, "ack egressed as a draft seal"++ @pytest.mark.asyncio+ async def test_expired_prompt_notice_leaves_open_draft_untouched(self):+ """Same class: the expiry notice for an unknown prompt id must not+ absorb into an open stream either."""+ a = _adapter()+ draft_key = await _open_turn_draft(a)+ consumed = await a._consume_prompt_response(+ FakeEvent("prompt-never-minted", "once")+ )+ assert consumed is True+ assert draft_key in a._open_draft_by_chat++ @pytest.mark.asyncio+ async def test_turn_final_still_absorbs_into_single_open_stream(self):+ """Regression control (review B2 contract): a REAL turn-final+ without identity metadata, in a chat with exactly one open stream,+ must still seal that stream."""+ a = _adapter()+ draft_key = await _open_turn_draft(a)+ result = await a.send("D01", "the full final answer")+ assert result.success+ assert draft_key not in a._open_draft_by_chat, (+ "turn-final no longer absorbs into its stream — B2 fallback broken"+ )+ seal_frames = [+ f+ for f, _ in a._transport.frames+ if f["op"] == "draft" and f.get("final") is True+ ]+ assert len(seal_frames) == 1