diff --git a/integrations/hermes-t3-gateway/adapter.py b/integrations/hermes-t3-gateway/adapter.py index 034d21ca97..5738d92b44 100644 --- a/integrations/hermes-t3-gateway/adapter.py +++ b/integrations/hermes-t3-gateway/adapter.py @@ -8,6 +8,7 @@ import logging import os import re import tempfile +import time import uuid import weakref from collections.abc import Coroutine @@ -37,6 +38,7 @@ from .home import ( save_home_thread_id, ) from .protocol import ( + PROTOCOL_VERSION, canonical_tool_data, canonical_tool_item_type, describe_response, @@ -67,6 +69,23 @@ _T3_HOME_CHANNEL_NOTICE = ( # `detail`. _STATUS_ITEM_TYPE = "status_text" +# How long a just-completed turn stays an acceptable scope for its own media. +# +# The window exists because the base adapter's delivery pipeline sends a +# reply's final TEXT before the reply's media files, and that text is +# notify-marked — so it completes the T3 turn, and every file of the same +# reply then arrives against a thread with no active turn +# (`gateway/platforms/base.py:5326` text, then `:5373+`/`:5424+` media). +# +# Sized against what actually separates the two: the live repro measured 36ms, +# and the only deliberate spacing upstream inserts is `_get_human_delay()` +# (`gateway/platforms/base.py:5051`), whose widest configured mode is 2.5s per +# file. 30s covers a slow batch of large files with generous headroom while +# staying far below any plausible human follow-up: the window closes long +# before the user could read the answer and ask something new, and it is a +# *scope* window only — it never keeps a turn alive or re-completes one. +_RECENT_TURN_MEDIA_WINDOW_SECONDS = 30.0 + def _hermes_version() -> str: try: @@ -143,6 +162,12 @@ class _TurnState: default_factory=asyncio.Lock, repr=False, ) + # Monotonic clock reading taken when this turn completed; None while live. + # Read only by `_media_turn_scope` to bound how long the completed turn + # remains an acceptable scope for its own trailing media + # (`_RECENT_TURN_MEDIA_WINDOW_SECONDS`). Monotonic deliberately: a wall + # clock adjustment mid-turn must not widen or collapse the window. + completed_at: float | None = None @dataclass @@ -474,6 +499,13 @@ class T3PlatformAdapter(BasePlatformAdapter): Entries are NOT removed here — only a `home.deliver.ack` purges one. Re-sending an entry T3 already durably wrote is harmless (it dedupes on `deliveryId`); dropping one it never wrote is not. + + Frames are restamped to the CURRENT protocol version before sending: an + entry queued by an older plugin carries the version it was built under, + and T3's strict-lockstep decoder closes the socket on any other version + — turning one stale queued frame into a reconnect loop that outlives + the upgrade. The delivery fields themselves are version-stable (the + v3→v4 change only added frame types), so restamping is honest. """ pending = self._home_queue.entries() if not pending: @@ -481,7 +513,7 @@ class T3PlatformAdapter(BasePlatformAdapter): logger.info("Flushing %d queued T3 home deliver(y|ies)", len(pending)) for entry in pending[:MAX_FLUSH_PER_CONNECT]: try: - await self._send_frame(entry) + await self._send_frame({**entry, "protocolVersion": PROTOCOL_VERSION}) except Exception as exc: # noqa: BLE001 - the rest rides the next connect logger.warning("T3 home delivery flush stopped: %s", exc) return @@ -512,9 +544,66 @@ class T3PlatformAdapter(BasePlatformAdapter): the base adapter's delivery pipeline sends a reply's final text — notify-marked, which completes the turn here — BEFORE it dispatches the reply's media files (`gateway/platforms/base.py:5326` then - `:5383+`), so turn media routinely arrives moments after its turn - closed. The most recently completed turn per thread is therefore an - accepted scope when the send still carries that turn's session key. + `:5373+`), so turn media routinely arrives moments after its turn + closed and must still be able to reach back to it. + + **The session key is NOT available on the media dispatch path**, and + that is structural, not a race. The gateway binds `HERMES_SESSION_KEY` + inside `_handle_message_with_agent` and clears it in that method's own + `finally` (`gateway/run.py:12972` → `:14626`); the delivery pipeline + that sends the text and then the files lives one frame further out, in + `BasePlatformAdapter._process_message_background`, and runs entirely + AFTER the handler returned. `clear_session_vars` sets the vars to `""` + rather than resetting them, deliberately suppressing the `os.environ` + fallback — so every send the pipeline makes, text and media alike, + reads `""`. Verified against the real gateway package: inside the + handler the key resolves; on return it is `""`. + + The text path never noticed because it does not consult the key when a + live turn exists — `send()` reaches `_is_proactive_delivery`, which for + a non-home thread returns False on the thread check alone and falls + through to `_active_turns`. Media had no such fallback: it required the + key to match, so on a non-home thread the file was dropped with + "no active T3 turn" (live repro 2026-07-27 18:47:06, 36ms after the + turn's own text completed the turn). + + So the reach-back cannot be keyed on the session key. It is keyed on + the two signals that ARE trustworthy here: + + * **Recency.** A completed turn is a scope only within + `_RECENT_TURN_MEDIA_WINDOW_SECONDS` of completing. Turn media follows + its text by milliseconds; anything later is not this turn's output. + * **Provenance.** `classify_delivery` must NOT positively identify the + send as proactive. This is the same discriminator the home half of + the gate uses, applied with the opposite default — and it is what + contains the collision this window would otherwise open. + + The collision to contain is `send_message`, the one thing besides a + turn that can dispatch media to a NON-home thread + (`tools/send_message_tool.py:1880+` → `adapter.send_image_file` with a + caller-chosen `chat_id`). Cron cannot: it delivers to the home channel + and is excluded by the thread check. But `send_message` runs INSIDE a + turn's own handler — it is a tool the agent calls — so it is not a + cross-turn intruder arriving during someone else's live turn; it is + this session's own agent choosing a destination. Two cases follow. If + it targets this thread, scoping the file to the turn that produced it + is exactly right. If it targets a *different* thread, that thread's + `_recent_turns` entry is stale by far more than the window unless the + user was mid-conversation there seconds ago — and in that narrow case + the file still lands in the thread the agent addressed, attributed to a + turn that just ended in it. A slightly-wrong turn attribution on a + message row, never a stolen answer. + + That asymmetry is the whole reason this is safe where the text gate is + strict. `send()` completes turns; a misattributed text send ends a live + turn with the wrong output — the `finalize` defect class. Media touches + no turn machinery at all: `media.deliver` carries `turnId` purely as a + sequencing hint, emits no turn or item frame, and cannot complete, + interrupt, or alter a turn. The worst outcome here is a file sequenced + next to the wrong neighbour. + + A live turn whose session key matches still wins outright and is + checked first, so nothing about the ordinary in-handler path changes. """ turn = self._active_turns.get(thread_id) recent = self._recent_turns.get(thread_id) @@ -525,11 +614,19 @@ class T3PlatformAdapter(BasePlatformAdapter): return recent home = home_thread_id() if not home or thread_id != home: - # Not home: media belongs to the live turn or has nowhere to go — - # exactly the text path's scope rule. A stale completed turn is - # NOT a scope here; only the session-key-matched case above may - # reach back past completion. - return turn + # Not home. A live turn takes the media exactly as the text path + # would. Otherwise the just-completed turn may claim it, bounded by + # recency and refused to a positively-proactive send — see above. + if turn is not None: + return turn + if not self._within_media_reachback(recent): + return None + _kind, _label, certain = classify_delivery( + content, + metadata, + session_user_id=self._session_user_id(), + ) + return None if certain else recent if turn is None: return None _kind, _label, certain = classify_delivery( @@ -541,6 +638,20 @@ class T3PlatformAdapter(BasePlatformAdapter): # media send during a live home turn stays with the turn. return None if certain else turn + @staticmethod + def _within_media_reachback(turn: _TurnState | None) -> bool: + """True while a completed turn may still claim its own trailing media. + + A turn with no `completed_at` never went through `_complete_turn`, so + nothing is known about when it ended — treated as out of the window + rather than assumed fresh. + """ + if turn is None or turn.completed_at is None: + return False + return ( + time.monotonic() - turn.completed_at + ) <= _RECENT_TURN_MEDIA_WINDOW_SECONDS + async def _deliver_media_file( self, chat_id: str, @@ -557,14 +668,40 @@ class T3PlatformAdapter(BasePlatformAdapter): divergence is a payload that cannot be built at all — unreadable file, empty, over the 25MB ceiling — which fails the send immediately instead of queueing a frame T3 would reject on every future flush. + + **An unscopeable file goes to Home rather than being dropped.** When + `_media_turn_scope` finds nothing on a non-home thread, the old + behaviour returned `"no active T3 turn"` — and upstream's only + response to that is `logger.error("Failed to send image: %s")` + (`gateway/platforms/base.py:3471`) before moving on. The file is gone, + silently from the user's side, after Hermes spent a generation call + producing it. Text can afford that (the agent can restate it, the user + can ask again); a produced artifact cannot. + + Routing it to Home is safe in the way the thread route is not. The + frame goes out turnless, so T3 re-resolves the instance's home thread + server-side and writes only there — a plugin cannot address an + arbitrary thread on this path even in principle + (`apps/server/src/provider/hermesGatewayHttp.ts:207-215`) — and it + carries `classify_delivery` provenance, so it renders as a badged + notification exactly like a cron artifact rather than impersonating a + thread reply. With no home designated there is genuinely nowhere to put + it, and the original error stands. """ thread_id = str(chat_id) content = str(caption or "") turn = self._media_turn_scope(thread_id, content, metadata) - if turn is None: - home = home_thread_id() - if not home or thread_id != home: + home = home_thread_id() + delivery_thread_id = thread_id + if turn is None and (not home or thread_id != home): + if not home: return SendResult(success=False, error="no active T3 turn") + logger.info( + "T3 media for thread %s has no turn to attach to; delivering " + "it to the home thread instead of dropping it", + thread_id, + ) + delivery_thread_id = home kind, label, _certain = classify_delivery( content, metadata, @@ -572,7 +709,7 @@ class T3PlatformAdapter(BasePlatformAdapter): ) try: delivery = build_media_delivery( - thread_id=thread_id, + thread_id=delivery_thread_id, path=str(path), kind=kind, label=label, @@ -1346,6 +1483,8 @@ class T3PlatformAdapter(BasePlatformAdapter): self._active_turns.pop(turn.thread_id, None) # Remembered for media scoping: the base adapter sends a reply's # media files AFTER its notify-marked text, i.e. after this point. + # The stamp bounds that reach-back — see `_media_turn_scope`. + turn.completed_at = time.monotonic() self._recent_turns[turn.thread_id] = turn await self._send_status() diff --git a/integrations/hermes-t3-gateway/tests/test_adapter.py b/integrations/hermes-t3-gateway/tests/test_adapter.py index ec0670aee1..2c3cb2f6ef 100644 --- a/integrations/hermes-t3-gateway/tests/test_adapter.py +++ b/integrations/hermes-t3-gateway/tests/test_adapter.py @@ -1869,6 +1869,35 @@ class HomeDeliveryTests(unittest.IsolatedAsyncioTestCase): ) self.assertEqual(self.queue.entries(), []) + async def test_flush_restamps_stale_protocol_versions(self): + """A frame queued under an older plugin must not wedge the reconnect. + + T3's strict-lockstep decoder closes the socket on any frame carrying a + different protocolVersion, so a v3-era queued delivery would otherwise + turn one stale outbox entry into a reconnect loop that outlives the + upgrade. The flush restamps to the current version; the delivery + fields themselves are version-stable. + """ + stale = protocol_module.home_deliver( + thread_id=self.HOME, + text="Queued before the upgrade", + kind="cron", + label="Cron: nightly", + delivery_id_value="stale-v3-delivery", + ) + stale["protocolVersion"] = 3 + self.assertTrue(self.queue.append(stale)) + + await self.adapter._flush_home_queue() + + flushed = self.connection.messages + self.assertEqual(len(flushed), 1) + self.assertEqual(flushed[0]["protocolVersion"], protocol_module.PROTOCOL_VERSION) + self.assertEqual(flushed[0]["text"], "Queued before the upgrade") + # The queued copy is untouched — restamping happens on the wire only, + # and the entry still purges by deliveryId on ack. + self.assertEqual(self.queue.entries()[0]["protocolVersion"], 3) + async def test_the_queue_flushes_in_fifo_order(self): class DeadConnection: connected = False @@ -2250,6 +2279,133 @@ class MediaDeliveryTests(unittest.IsolatedAsyncioTestCase): self.assertEqual(delivery["type"], "media.deliver") self.assertEqual(delivery["turnId"], "turn-late") + async def test_live_repro_reply_media_lands_with_no_session_key_bound(self): + """The 2026-07-27 18:47:06 gateway.log repro, end to end. + + An ordinary (non-home) thread asks for an image. Upstream's delivery + pipeline sends the reply's notify-marked TEXT — completing the T3 turn + through the real completion path — and 36ms later dispatches the file. + + The session context is modelled as it ACTUALLY is at that moment: + UNAVAILABLE. `HERMES_SESSION_KEY` is bound inside + `_handle_message_with_agent` and cleared in its own `finally` + (`gateway/run.py:12972` → `:14626`), while this whole delivery block + runs one frame further out in + `BasePlatformAdapter._process_message_background`, after the handler + returned — and `clear_session_vars` sets `""` rather than resetting, so + the `os.environ` fallback is suppressed too. Every send here reads `""`. + + That is why the file was dropped with "no active T3 turn": the text + path never consults the key when a live turn exists, but the media path + required it to match. The class default `_gateway_session_key` stub + (`lambda: ""`) is exactly this state — no per-test patch. + """ + thread = "3667b0a1-c1db-4216-8e72-2f62a3ff87e2" + await self._start_turn(thread, "turn-live-repro") + + # The reply's final text. notify=True is what upstream stamps via + # `_mark_notify_metadata`, and it completes the turn for real. + text_result = await self.adapter.send( + thread, + "Here's the image you asked for.", + metadata={"thread_id": thread, "notify": True}, + ) + self.assertTrue(text_result.success) + self.assertNotIn(thread, self.adapter._active_turns) + completed = [ + frame + for frame in self.connection.messages + if frame["type"] == "turn.completed" + ] + self.assertEqual([frame["turnId"] for frame in completed], ["turn-live-repro"]) + frames_before = len(self.connection.messages) + + # ~36ms later: the same reply's image, same metadata dict. + 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"]) + delivery = frames[0] + # Scoped to its own turn, in its own thread — not exiled to Home. + self.assertEqual(delivery["threadId"], thread) + self.assertEqual(delivery["turnId"], "turn-live-repro") + self.assertEqual(delivery["name"], "chart.png") + # The completed turn is not resurrected by claiming its media. + self.assertNotIn(thread, self.adapter._active_turns) + + 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. + + Without the window, `_recent_turns` would keep a thread's last turn + claimable forever and an unrelated later delivery would be sequenced + into an answer the user finished reading long ago. + """ + thread = "thread-stale-reachback" + await self._start_turn(thread, "turn-stale") + await self.adapter.send(thread, "Done.", metadata={"notify": True}) + self.assertNotIn(thread, self.adapter._active_turns) + + stale = self.adapter._recent_turns[thread] + stale.completed_at -= adapter_module._RECENT_TURN_MEDIA_WINDOW_SECONDS + 1 + frames_before = len(self.connection.messages) + + with self.assertLogs(adapter_module.logger, level="INFO"): + result = await self.adapter.send_document(thread, str(self.chart)) + + self.assertTrue(result.success) + delivery = self.connection.messages[frames_before:][0] + self.assertNotIn("turnId", delivery) + self.assertEqual(delivery["threadId"], self.HOME) + + async def test_a_cron_delivery_never_claims_a_just_closed_turn(self): + """Provenance still overrides recency inside the window. + + A positively-classified proactive send — cron here — is refused the + completed turn even microseconds after it closed, and takes the + turnless home route with its badge intact. This is the guard that + keeps the recency window from re-opening the defect class the + session-key gate was built for. + """ + thread = "thread-cron-collision" + await self._start_turn(thread, "turn-cron-collision") + await self.adapter.send(thread, "All set.", metadata={"notify": True}) + self.assertIsNotNone(self.adapter._recent_turns[thread].completed_at) + frames_before = len(self.connection.messages) + + with self.assertLogs(adapter_module.logger, level="INFO"): + result = await self.adapter.send_document( + thread, + str(self.chart), + caption="Cronjob Response: nightly\n-------------\n\nChart attached.", + ) + + self.assertTrue(result.success) + delivery = self.connection.messages[frames_before:][0] + self.assertNotIn("turnId", delivery) + self.assertEqual(delivery["threadId"], self.HOME) + self.assertEqual(delivery["kind"], "cron") + self.assertEqual(delivery["label"], "Cron: nightly") + + async def test_a_live_turn_still_outranks_a_completed_one(self): + """The user asked again; the new turn owns the thread, not the old one.""" + thread = "thread-relay" + await self._start_turn(thread, "turn-first") + await self.adapter.send(thread, "First answer.", metadata={"notify": True}) + await self._start_turn(thread, "turn-second") + frames_before = len(self.connection.messages) + + result = await self.adapter.send_image_file(thread, str(self.chart)) + + self.assertTrue(result.success) + delivery = self.connection.messages[frames_before:][0] + self.assertEqual(delivery["turnId"], "turn-second") + async def test_proactive_media_to_home_is_turnless_with_provenance(self): frames_before = len(self.connection.messages) @@ -2271,11 +2427,42 @@ class MediaDeliveryTests(unittest.IsolatedAsyncioTestCase): self.assertTrue(result.success) self.assertEqual(self.adapter._active_turns, {}) - async def test_media_to_a_non_home_thread_without_a_turn_errors(self): - """"Send media to any thread unprompted" stays out of scope.""" - result = await self.adapter.send_document( - "some-other-thread", str(self.chart) + async def test_unscopeable_media_falls_back_to_home_instead_of_dropping(self): + """"Send media to any thread unprompted" still lands — in Home. + + The thread route stays out of scope: the frame goes out turnless, so + T3 re-resolves the home thread server-side and can write nowhere else. + But the file is NOT dropped. Upstream's only response to a failed + media send is a log line, so returning an error silently loses an + artifact Hermes already spent a generation call producing. + """ + with self.assertLogs(adapter_module.logger, level="INFO"): + result = await self.adapter.send_document( + "some-other-thread", str(self.chart) + ) + + self.assertTrue(result.success) + frames = self.connection.messages + self.assertEqual([frame["type"] for frame in frames], ["media.deliver"]) + delivery = frames[0] + # Home-addressed and turnless: it renders as a badged notification, + # never as a reply inside the thread that could not take it. + self.assertEqual(delivery["threadId"], self.HOME) + self.assertNotIn("turnId", delivery) + self.assertEqual(delivery["label"], "Hermes") + self.assertEqual( + [entry["deliveryId"] for entry in self.queue.entries()], + [result.message_id], ) + + async def test_media_with_no_home_designated_still_errors(self): + """With nowhere to fall back to, the original error stands.""" + with unittest.mock.patch.dict( + adapter_module.os.environ, {home_module.HOME_CHANNEL_ENV: ""} + ): + result = await self.adapter.send_document( + "some-other-thread", str(self.chart) + ) self.assertFalse(result.success) self.assertEqual(result.error, "no active T3 turn") self.assertEqual(self.connection.messages, [])