fix: preserve thread context for cronjob deliver=origin

When a cronjob is created from within a Telegram or Slack thread,
deliver=origin was posting to the parent channel instead of the thread.

Root cause: the gateway never set HERMES_SESSION_THREAD_ID in the
session environment, so cronjob_tools.py could not capture thread_id
into the job's origin metadata — even though the scheduler already
reads origin.get('thread_id').

Fix:
- gateway/run.py: set HERMES_SESSION_THREAD_ID when thread_id is
  present on the session context, and clear it in _clear_session_env
- tools/cronjob_tools.py: read HERMES_SESSION_THREAD_ID into origin

Closes #1219
This commit is contained in:
teyrebaz33 2026-03-14 06:06:44 +03:00 committed by teknium1
parent 77bfa252b9
commit 20f381cfb6
4 changed files with 79 additions and 1 deletions

View file

@ -153,6 +153,36 @@ class TestScheduleCronjob:
assert job["provider"] == "custom"
assert job["base_url"] == "http://127.0.0.1:4000/v1"
def test_thread_id_captured_in_origin(self, monkeypatch):
monkeypatch.setenv("HERMES_SESSION_PLATFORM", "telegram")
monkeypatch.setenv("HERMES_SESSION_CHAT_ID", "123456")
monkeypatch.setenv("HERMES_SESSION_THREAD_ID", "42")
import cron.jobs as _jobs
created = json.loads(schedule_cronjob(
prompt="Thread test",
schedule="every 1h",
deliver="origin",
))
assert created["success"] is True
job_id = created["job_id"]
job = _jobs.get_job(job_id)
assert job["origin"]["thread_id"] == "42"
def test_thread_id_absent_when_not_set(self, monkeypatch):
monkeypatch.setenv("HERMES_SESSION_PLATFORM", "telegram")
monkeypatch.setenv("HERMES_SESSION_CHAT_ID", "123456")
monkeypatch.delenv("HERMES_SESSION_THREAD_ID", raising=False)
import cron.jobs as _jobs
created = json.loads(schedule_cronjob(
prompt="No thread test",
schedule="every 1h",
deliver="origin",
))
assert created["success"] is True
job_id = created["job_id"]
job = _jobs.get_job(job_id)
assert job["origin"].get("thread_id") is None
# =========================================================================
# list_cronjobs