From 3d0459539dffe3555691508ca741369d40022de4 Mon Sep 17 00:00:00 2001 From: Ben Davis <45952064+bmdavis419@users.noreply.github.com> Date: Mon, 27 Jul 2026 21:34:54 -0700 Subject: [PATCH] fix(hermes-plugin): an image-only reply left its turn running forever MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Live repro (2026-07-27 21:26): "send it one more time" produced a reply that was just the image — no text. Upstream notify-marks every send of a reply's final delivery batch, text and media alike, but only the text path honored the mark; with no text send there was nothing to complete the turn, and the thread sat "Working" until the liveness timeout. The media path now honors the same notify-completion contract, guarded to the still-live turn: in the common text-then-media ordering the text completes the turn first, and re-completing the recent-turn entry the media scopes to would emit a second terminal frame for a turn T3 already folded (its conflict gate rejects those). Two regression tests pin the pair: image-only completes exactly once, text-then-media still emits a single turn.completed. Co-Authored-By: Claude Fable 5 --- integrations/hermes-t3-gateway/adapter.py | 15 +++++ .../hermes-t3-gateway/tests/test_adapter.py | 56 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/integrations/hermes-t3-gateway/adapter.py b/integrations/hermes-t3-gateway/adapter.py index 5738d92b44..4ae11a9f79 100644 --- a/integrations/hermes-t3-gateway/adapter.py +++ b/integrations/hermes-t3-gateway/adapter.py @@ -731,6 +731,21 @@ class T3PlatformAdapter(BasePlatformAdapter): delivery_id_value, exc, ) + # The same notify-completion contract `send()` honors for text. The + # base adapter notify-marks every send of a reply's FINAL delivery + # batch (`_mark_notify_metadata`, `gateway/platforms/base.py:5220`) — + # text and media alike — and an image-only reply produces no text + # send at all, so this is the only place its turn can complete. + # Guarded to the still-live turn: the common text-then-media ordering + # completes the turn on the text, and re-completing a `_recent_turns` + # entry would emit a second `turn.completed` for a turn T3 already + # folded. + if ( + turn is not None + and bool((metadata or {}).get("notify")) + and self._active_turns.get(thread_id) is turn + ): + await self._complete_turn(turn) return SendResult(success=True, message_id=delivery_id_value) async def send_image_file( diff --git a/integrations/hermes-t3-gateway/tests/test_adapter.py b/integrations/hermes-t3-gateway/tests/test_adapter.py index 2c3cb2f6ef..c7489e2f3d 100644 --- a/integrations/hermes-t3-gateway/tests/test_adapter.py +++ b/integrations/hermes-t3-gateway/tests/test_adapter.py @@ -2339,6 +2339,62 @@ class MediaDeliveryTests(unittest.IsolatedAsyncioTestCase): # The completed turn is not resurrected by claiming its media. self.assertNotIn(thread, self.adapter._active_turns) + async def test_an_image_only_reply_completes_its_turn(self): + """Live repro 2026-07-27 21:26: "send it one more time" → image, no text. + + Upstream notify-marks every send of a reply's final delivery batch — + text AND media (`_mark_notify_metadata`, base.py:5220) — but a reply + that is only an image produces no text send, so the media send is the + only carrier of the completion signal. Without honoring it, the turn + sat "Working" until the two-minute liveness timeout. + """ + thread = "thread-image-only-reply" + await self._start_turn(thread, "turn-image-only") + frames_before = len(self.connection.messages) + + result = await self.adapter.send_image_file( + thread, + str(self.chart), + caption=None, + metadata={"thread_id": thread, "notify": True}, + ) + + self.assertTrue(result.success) + frames = self.connection.messages[frames_before:] + # `_complete_turn` also republishes connection.status; the contract + # here is the ORDER media -> completed, not the exact frame set. + types = [frame["type"] for frame in frames] + self.assertEqual(types[:2], ["media.deliver", "turn.completed"]) + self.assertEqual(frames[0]["turnId"], "turn-image-only") + self.assertEqual(frames[1]["turnId"], "turn-image-only") + self.assertNotIn(thread, self.adapter._active_turns) + + async def test_trailing_media_does_not_recomplete_a_closed_turn(self): + """The text-then-media ordering must emit exactly one turn.completed. + + The text completes the turn; the file's own notify mark must not + re-complete the `_recent_turns` entry it scopes to — T3 already + folded the turn, and a second terminal frame names a turn its + conflict gate would reject. + """ + thread = "thread-text-then-media" + await self._start_turn(thread, "turn-text-media") + await self.adapter.send( + thread, "Here it is.", metadata={"thread_id": thread, "notify": True} + ) + frames_before = len(self.connection.messages) + + result = await self.adapter.send_image_file( + thread, + str(self.chart), + caption=None, + metadata={"thread_id": thread, "notify": True}, + ) + + self.assertTrue(result.success) + frames = self.connection.messages[frames_before:] + self.assertEqual([frame["type"] for frame in frames], ["media.deliver"]) + async def test_media_long_after_a_turn_closed_does_not_claim_it(self): """Recency is what bounds the reach-back, so an old turn must not claim.