Skip to content

[Bug]: Cronjob 'deliver: origin' doesn't preserve Telegram thread context #1219

@nova328

Description

@nova328

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

  1. Start a conversation in a Telegram thread (e.g., thread 23 in a group)
  2. Create a recurring cronjob: schedule_cronjob(prompt='test', schedule='every 1m', deliver='origin')
  3. Wait for the job to run
  4. 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:

  1. 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
  1. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions