From 75609f02bef66e65e783fa4c46e21081afbc6867 Mon Sep 17 00:00:00 2001 From: Offending Commit Date: Sun, 22 Nov 2026 21:39:41 -0600 Subject: [PATCH] fix(media): attach a cron-skipped send to the job's reply, not a checkmark Inside a cron run, Hermes' send_message skips a send to the chat the job already auto-delivers to and answers success: true, skipped: true, reason cron_auto_delivery_duplicate_target. deliver_media read that as a delivery, armed the final-response suppression, and the job's real reply (with its MEDIA line) was replaced by a lone checkmark. The voice memo never left. deliver_media now treats any skipped send as not delivered. For the cron duplicate-target skip it defers the attachment into the job's final reply (the as_reply path), so the memo arrives with that reply. Every other skip returns success=False and leaves the reply alone. CRON_DUPLICATE_SKIP_REASON is exported and pinned to the host's _maybe_skip_cron_duplicate_send by the contract suite. --- AGENTS.md | 4 + README.md | 7 ++ hermes_plugin_kit/__init__.py | 36 ++++++++- .../hermes-plugins/references/plugin-kit.md | 4 + tests/test_hermes_contract.py | 20 +++++ tests/test_kit.py | 75 +++++++++++++++++++ 6 files changed, 145 insertions(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 481233a..2793b5a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -55,6 +55,10 @@ plugin. `transform_llm_output` hook and `clear_media_delivery_state` as `on_session_end`; they must not recreate those contracts or substitute OpenClaw response shapes. +- A host answer of `skipped: true` is never a delivery and must never arm the + ✅ suppression. The cron duplicate-target skip (`CRON_DUPLICATE_SKIP_REASON`, + pinned to the host by the contract suite) defers the attachment into the + cron job's final reply; every other skip returns `success=False`. - `deliver_media(..., as_reply=True)` to `origin` defers the attachment into the turn's final reply instead of sending it plus a ✅. It must stay opt-in: its `success` means "accepted", not "sent". `clear_media_delivery_state` diff --git a/README.md b/README.md index 46a483e..f2acb6b 100644 --- a/README.md +++ b/README.md @@ -798,6 +798,13 @@ Costs: the attachment arrives when the turn ends, not when the tool ran, and send (a durable receipt, for example) must not use `as_reply`. Explicit targets and `spoiler=True` media always send directly. +Cron runs take the same path without `as_reply`. Hermes skips a cron job's +`send_message` to the chat the job already auto-delivers to, answering +`skipped: true` with reason `CRON_DUPLICATE_SKIP_REASON`. `deliver_media` then +defers the attachment into the job's final reply and returns `deferred=True`, +so the memo arrives with that reply instead of being replaced by a ✅. Any other +skipped send returns `success=False`: nothing was delivered. + For non-media host calls, `invoke_host_tool` remains the lower-level seam. `invoke_host_tool` resolves the supported direct host handler and wraps the nested operation with Hermes `pre_tool_call` and `post_tool_call` hooks. A blocking hook diff --git a/hermes_plugin_kit/__init__.py b/hermes_plugin_kit/__init__.py index 4ef718e..894fc3e 100644 --- a/hermes_plugin_kit/__init__.py +++ b/hermes_plugin_kit/__init__.py @@ -97,6 +97,7 @@ def register(ctx): "deliver_media", "resolve_delivery_target", "MEDIA_DELIVERED_REPLY", + "CRON_DUPLICATE_SKIP_REASON", "strip_media_mirror_lines", "transform_media_delivery_output", "clear_media_delivery_state", @@ -1995,6 +1996,10 @@ def _mark_media_delivery_success(context: dict[str, Any]) -> None: # The turn's visible reply after the attachment itself was already sent. MEDIA_DELIVERED_REPLY = "✅" +# shortcut: Hermes' send_message skip reason for a cron send to the job's own +# auto-delivery target (tools/send_message_tool.py _maybe_skip_cron_duplicate_send); +# the contract suite pins it to the host. +CRON_DUPLICATE_SKIP_REASON = "cron_auto_delivery_duplicate_target" # Hermes' send_message records each media send as an assistant line so the model # remembers it (tools/send_message_tool.py _describe_media_for_mirror). Models copy @@ -2373,7 +2378,36 @@ def deliver_media( host_payload = json.loads(raw) except (TypeError, json.JSONDecodeError): host_payload = {"error": "send_message returned invalid JSON"} - success = _host_result_fields(raw)[0] == "success" + # A skipped send reports success but delivered nothing, so it must not arm the + # final-response suppression. Inside a cron run Hermes skips a send to the chat + # the job already replies to; the attachment then rides that reply instead. + skipped = isinstance(host_payload, dict) and host_payload.get("skipped") is True + if ( + skipped + and host_payload.get("reason") == CRON_DUPLICATE_SKIP_REASON + and _defer_media_to_reply(media, resolved, context) + ): + log.info( + "event=media_delivery_deferred target=%s media_type=%s path=%s reason=%s", + resolved.display, + media.media_type.value, + media.path, + CRON_DUPLICATE_SKIP_REASON, + ) + return MediaDeliveryResult( + success=True, + requested_target=( + resolved.requested + if resolved.requested == "origin" + else _display_delivery_target(resolved.requested) + ), + display_target=resolved.display, + media_type=media.media_type, + path=media.path, + host_result={"deferred": "final_reply"}, + deferred=True, + ) + success = _host_result_fields(raw)[0] == "success" and not skipped safe_result = _safe_media_host_result(host_payload, resolved) log_method = log.info if success else log.warning log_method( diff --git a/skills/hermes-plugins/references/plugin-kit.md b/skills/hermes-plugins/references/plugin-kit.md index 814d71d..1de9592 100644 --- a/skills/hermes-plugins/references/plugin-kit.md +++ b/skills/hermes-plugins/references/plugin-kit.md @@ -129,6 +129,10 @@ the same operations with `@tool` shadows the active-context-aware dispatch. `transform_media_delivery_output` registered as `transform_llm_output` and `clear_media_delivery_state` registered as `on_session_end`, or the gateway can emit duplicate final output. +- In a cron run, a send to the job's own auto-delivery chat comes back from + Hermes as `skipped`. `deliver_media` then returns `deferred=True` and the + attachment rides the job's final reply; do not retry the send or tell the + model to drop the `MEDIA:` line from its reply. - The kit follows the real Hermes contract tests. A fake context alone can hide signature drift. - Public source does not imply public package access. Wheels, sdists, and diff --git a/tests/test_hermes_contract.py b/tests/test_hermes_contract.py index f1be51a..4540c8b 100644 --- a/tests/test_hermes_contract.py +++ b/tests/test_hermes_contract.py @@ -696,6 +696,26 @@ def test_media_delivered_reply_is_not_a_hermes_silence_marker(self) -> None: self.assertTrue(is_intentional_silence_response("NO_REPLY")) self.assertFalse(is_intentional_silence_response(hpk.MEDIA_DELIVERED_REPLY)) + def test_cron_duplicate_skip_reason_matches_the_host(self) -> None: + """deliver_media defers a cron send Hermes skips; the reason it matches must be the host's.""" + from gateway import session_context # type: ignore + from tools import send_message_tool # type: ignore + + values = { + "HERMES_CRON_AUTO_DELIVER_PLATFORM": "telegram", + "HERMES_CRON_AUTO_DELIVER_CHAT_ID": "8670382527", + } + with patch.object( + session_context, + "get_session_env", + lambda name, default="": values.get(name, default), + ): + skip = send_message_tool._maybe_skip_cron_duplicate_send("telegram", "8670382527", None) + + self.assertTrue(skip["success"]) + self.assertTrue(skip["skipped"]) + self.assertEqual(skip["reason"], hpk.CRON_DUPLICATE_SKIP_REASON) + def test_kit_silence_markers_match_the_host(self) -> None: """A deferred-media reply drops the model's silence marker using the host's own set.""" from gateway.response_filters import LIVE_GATEWAY_SILENT_MARKERS # type: ignore diff --git a/tests/test_kit.py b/tests/test_kit.py index 59533e7..c24e8b7 100644 --- a/tests/test_kit.py +++ b/tests/test_kit.py @@ -1305,6 +1305,81 @@ def test_successful_delivery_does_not_suppress_another_session(self) -> None: ) ) + def _cron_duplicate_skip(self) -> str: + """The answer Hermes' send_message gives a cron send to the job's own chat.""" + return json.dumps( + { + "success": True, + "skipped": True, + "reason": hpk.CRON_DUPLICATE_SKIP_REASON, + "target": "telegram:8670382527", + "note": "Skipped send_message to telegram:8670382527.", + } + ) + + def test_cron_skipped_send_rides_the_final_reply_instead_of_a_checkmark(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) / "memo.ogg" + path.write_bytes(b"OggS" + b"\x00" * 16) + with patch.object(hpk, "invoke_host_tool", return_value=self._cron_duplicate_skip()): + result = hpk.deliver_media( + hpk.MediaPayload(path, hpk.MediaType.VOICE), + target="telegram", + session_id="session-1", + task_id="cron:job-1:run-1", + ) + + self.assertTrue(result.success) + self.assertTrue(result.deferred) + self.assertEqual(result.host_result, {"deferred": "final_reply"}) + self.assertEqual( + hpk.transform_media_delivery_output(response_text="NO_REPLY", session_id="session-1"), + f"[[audio_as_voice]]\nMEDIA:{path}", + ) + self.assertIsNone( + hpk.transform_media_delivery_output(response_text="next turn", session_id="session-1") + ) + + def test_cron_skipped_send_keeps_the_model_words_beside_the_memo(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) / "memo.ogg" + path.write_bytes(b"OggS" + b"\x00" * 16) + with patch.object(hpk, "invoke_host_tool", return_value=self._cron_duplicate_skip()): + hpk.deliver_media( + hpk.MediaPayload(path, hpk.MediaType.VOICE), + target="telegram", + session_id="session-1", + ) + + self.assertEqual( + hpk.transform_media_delivery_output( + response_text=f"Ask me for the rest.\n[[audio_as_voice]]\nMEDIA:{path}", + session_id="session-1", + ), + f"Ask me for the rest.\n[[audio_as_voice]]\nMEDIA:{path}", + ) + + def test_other_skipped_send_is_not_a_delivery(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) / "memo.ogg" + path.write_bytes(b"OggS" + b"\x00" * 16) + with patch.object( + hpk, + "invoke_host_tool", + return_value=json.dumps({"success": True, "skipped": True, "reason": "muted"}), + ): + result = hpk.deliver_media( + hpk.MediaPayload(path, hpk.MediaType.VOICE), + target="telegram:8670382527", + session_id="session-1", + ) + + self.assertFalse(result.success) + self.assertFalse(result.deferred) + self.assertIsNone( + hpk.transform_media_delivery_output(response_text="keep this", session_id="session-1") + ) + def test_failed_delivery_does_not_suppress_final_response(self) -> None: with tempfile.TemporaryDirectory() as tmp: path = Path(tmp) / "memo.ogg"