-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[Bug]: Cronjob 'deliver: origin' doesn't preserve Telegram thread context #1219
Description
Bug Description
When creating a cronjob with deliver: 'origin' from within a Telegram thread (or Slack thread), the job delivers to the parent channel instead of the original thread.
Root cause: The gateway doesn't set HERMES_SESSION_THREAD_ID in the environment, so cronjobs can't capture thread context even though the scheduler supports it.
Steps to Reproduce
- Start a conversation in a Telegram thread (e.g., thread 23 in a group)
- Create a recurring cronjob:
schedule_cronjob(prompt='test', schedule='every 1m', deliver='origin') - Wait for the job to run
- Observe: Message appears in parent channel, not the thread
Expected Behavior
Cronjob messages should deliver to the same thread where the job was created.
Actual Behavior
Messages deliver to the parent channel, losing thread context.
Technical Details
The scheduler (cron/scheduler.py line 81) already extracts and uses thread_id from job origin metadata:
thread_id = origin.get('thread_id')But the job creation (tools/cronjob_tools.py) never captures it because the gateway doesn't set the environment variable.
Fix
Two files patched:
- gateway/run.py - Set HERMES_SESSION_THREAD_ID in session environment:
def _set_session_env(self, context: SessionContext) -> None:
os.environ['HERMES_SESSION_PLATFORM'] = context.source.platform.value
os.environ['HERMES_SESSION_CHAT_ID'] = context.source.chat_id
if context.source.chat_name:
os.environ['HERMES_SESSION_CHAT_NAME'] = context.source.chat_name
if context.source.thread_id: # ← ADD THIS
os.environ['HERMES_SESSION_THREAD_ID'] = str(context.source.thread_id) # ← ADD THIS- tools/cronjob_tools.py - Capture thread_id from environment:
origin = {
'platform': origin_platform,
'chat_id': origin_chat_id,
'chat_name': os.getenv('HERMES_SESSION_CHAT_NAME'),
'thread_id': os.getenv('HERMES_SESSION_THREAD_ID'), # ← ADD THIS
}Also update _clear_session_env() to clear HERMES_SESSION_THREAD_ID.
Impact
All users who want recurring automated messages to stay within threads (Telegram/Slack) are affected. This is critical for:
- Thread-specific reminders
- Contextual follow-ups
- Organized group conversations
Testing
Verified fix works for:
- One-shot cronjobs (already worked)
- Recurring cronjobs (was broken, now fixed)
- Both auto-delete and permanent jobs
Files Changed
gateway/run.py(2 lines added)tools/cronjob_tools.py(1 line added, 1 line modified)
Total: 3 lines changed, minimal risk.
Reporter: Joel deTeves (via Nova Prime)
Date: 2026-03-14
Status: Fixed locally, ready for PR