Skip to content

The agent that grows with you

fix(cron): deliver=origin from an api_server session reaches the home channel instead of failing silently

teknium1d5d653c2f0ea
diff --git a/cron/scheduler_delivery.py b/cron/scheduler_delivery.py
index 82c968a5322..8ecbde4ff05 100644
--- a/cron/scheduler_delivery.py
+++ b/cron/scheduler_delivery.py
@@ -35,6 +35,11 @@ _KNOWN_DELIVERY_PLATFORMS = frozenset({
"wecom", "wecom_callback", "weixin", "sms", "email", "webhook", "bluebubbles",
"qqbot", "yuanbao"})
+# Gateway platforms whose adapter declares ``supports_async_delivery = False`` (request/response
+# only, ``send()`` is a stub) — a cron report can never reach them, so they are never a
+# deliver=origin destination.
+_NON_PUSH_ORIGIN_PLATFORMS = frozenset({"api_server"})
+
# Platforms supporting a cron/notification home target -> env var used by gateway config.
_HOME_TARGET_ENV_VARS = {
"matrix": "MATRIX_HOME_ROOM",
@@ -89,6 +94,11 @@ def _resolve_origin(job: dict) -> Optional[dict]:
"""
origin = job.get("origin")
if isinstance(origin, dict) and origin.get("platform") and origin.get("chat_id"):
+ # Jobs stamped before non-push origins stopped being captured (#69304): the api_server
+ # adapter's send() is a stub, so honouring this origin fails every fire with
+ # last_status=ok. Treat it as missing so deliver=origin takes the home-channel fallback.
+ if str(origin["platform"]).lower() in _NON_PUSH_ORIGIN_PLATFORMS:
+ return None
return origin
return None
diff --git a/tests/cron/test_cron_non_push_origin.py b/tests/cron/test_cron_non_push_origin.py
new file mode 100644
index 00000000000..3bda1ba9c9d
--- /dev/null
+++ b/tests/cron/test_cron_non_push_origin.py
@@ -0,0 +1,44 @@
+"""Cron deliver=origin on a non-push surface (#69304).
+
+An api_server turn binds ``async_delivery=False``: its adapter's ``send()`` is a stub, so a
+job that captured ``origin.platform="api_server"`` ran fine (``last_status=ok``) while every
+fire recorded ``last_delivery_error`` and nothing reached the creator. Both legs are covered:
+a non-push session never stamps an origin (so the creation-time local-only notice fires), and
+a job already stamped with such an origin resolves to the configured home channel instead.
+"""
+
+from gateway.session_context import clear_session_vars, set_session_vars
+
+
+def test_non_push_session_stamps_no_origin_and_gets_creation_notice():
+ from tools.cronjob_job_args import _local_delivery_notice, _origin_from_env
+
+ tokens = set_session_vars(platform="api_server", chat_id="desk-1", session_key="desk-1",
+ async_delivery=False)
+ try:
+ origin = _origin_from_env()
+ notice = _local_delivery_notice({"id": "j", "deliver": "origin", "origin": origin}, None)
+ finally:
+ clear_session_vars(tokens)
+ assert origin is None
+ assert notice and "NOT be delivered" in notice
+
+ # Control: a push-capable platform keeps its origin.
+ tokens = set_session_vars(platform="telegram", chat_id="777", session_key="tg", async_delivery=True)
+ try:
+ assert _origin_from_env()["platform"] == "telegram"
+ finally:
+ clear_session_vars(tokens)
+
+
+def test_stamped_api_server_origin_falls_back_to_home_channel(monkeypatch):
+ from cron import scheduler_delivery as sd
+
+ monkeypatch.setenv("TELEGRAM_HOME_CHANNEL", "12345")
+ job = {"id": "old", "deliver": "origin", "origin": {"platform": "api_server", "chat_id": "desk-1"}}
+ targets = sd._resolve_delivery_targets(job)
+ assert [(t["platform"], t["chat_id"], t["_resolved_from"]) for t in targets] == [
+ ("telegram", "12345", "origin_fallback")]
+ # Control: a push-capable origin is delivered as written.
+ job["origin"] = {"platform": "discord", "chat_id": "42"}
+ assert sd._resolve_delivery_targets(job)[0]["platform"] == "discord"
diff --git a/tools/cronjob_job_args.py b/tools/cronjob_job_args.py
index 13534d5a638..4fcfe8caf8a 100644
--- a/tools/cronjob_job_args.py
+++ b/tools/cronjob_job_args.py
@@ -11,11 +11,16 @@ logger = logging.getLogger("tools.cronjob_tools")
def _origin_from_env() -> Optional[Dict[str, str]]:
- from gateway.session_context import get_session_env
+ from gateway.session_context import async_delivery_supported, get_session_env
origin_platform = get_session_env("HERMES_SESSION_PLATFORM")
origin_chat_id = get_session_env("HERMES_SESSION_CHAT_ID")
if not (origin_platform and origin_chat_id):
return None
+ # A non-push surface (api_server: request/response, ``send()`` is a stub) cannot receive a
+ # fire-time report, so an origin stamp would make ``deliver=origin`` fail silently on every
+ # fire (#69304). No origin => the home-channel fallback + creation-time notice apply.
+ if not async_delivery_supported():
+ return None
thread_id = get_session_env("HERMES_SESSION_THREAD_ID") or None
# Slack stamps every TOP-LEVEL message's own id as the session thread (a per-message
# KEY, not a location); persisting it would pin all future deliveries inside an
@@ -66,7 +71,7 @@ def _local_delivery_notice(job: Dict[str, Any], user_deliver: Optional[str]) ->
return (
"This is a local-only cron job: its output is saved (view it with "
"cronjob(action='list')) but will NOT be delivered back into this "
- "session — CLI/TUI sessions have no live-delivery channel. To be "
+ "session — CLI/TUI and stateless HTTP API sessions have no live-delivery channel. To be "
"notified when it runs, recreate or update the job with deliver set to "
"a gateway-connected platform, e.g. deliver='telegram' or deliver='all'.")