Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 35 additions & 1 deletion hermes_plugin_kit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions skills/hermes-plugins/references/plugin-kit.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/test_hermes_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 75 additions & 0 deletions tests/test_kit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down