fix(cron): avoid delivery origin as sender identity

This commit is contained in:
Ofer LaOr 2026-05-09 06:58:11 +00:00 committed by kshitij
parent a7e7921dbc
commit dbafa083b5

View file

@ -1204,10 +1204,31 @@ def run_job(job: dict) -> tuple[bool, str, str, Optional[str]]:
# don't clobber each other's targets (os.environ is process-global).
from gateway.session_context import set_session_vars, clear_session_vars, _VAR_MAP
# Cron execution is an internal scheduler context, not a live inbound
# gateway message. Do not seed HERMES_SESSION_* contextvars from the
# stored ``origin`` (which is delivery routing metadata, not a sender
# identity). Several tool consumers branch on these vars during job
# execution and would otherwise behave as if a real user from the
# origin chat was driving the agent:
# - tools/terminal_tool.py: background-process notification routing
# (notify_on_complete / watch_patterns) reads HERMES_SESSION_PLATFORM
# and HERMES_SESSION_CHAT_ID to populate watcher_platform / chat_id,
# which would route completion notifications to the origin chat
# instead of via HERMES_CRON_AUTO_DELIVER_* below.
# - tools/tts_tool.py: picks Opus vs MP3 based on
# HERMES_SESSION_PLATFORM == "telegram".
# - tools/skills_tool.py + agent/prompt_builder.py: per-platform
# skill-disable lists and the system-prompt cache key both consume
# HERMES_SESSION_PLATFORM.
# - tools/send_message_tool.py: mirror source labelling and the
# send_message gate read HERMES_SESSION_PLATFORM.
# Cron output delivery itself reads job["origin"] directly via
# _resolve_origin(job) and the HERMES_CRON_AUTO_DELIVER_* vars set
# below, so clearing HERMES_SESSION_* here does not affect delivery.
_ctx_tokens = set_session_vars(
platform=origin["platform"] if origin else "",
chat_id=str(origin["chat_id"]) if origin else "",
chat_name=origin.get("chat_name", "") if origin else "",
platform="",
chat_id="",
chat_name="",
)
_cron_delivery_vars = (
"HERMES_CRON_AUTO_DELIVER_PLATFORM",