fix: isolate cron approval context - #36004
Conversation
|
Competing with #31184 — same core fix (move HERMES_CRON_SESSION from os.environ to contextvars). This PR also adds gateway session hygiene and broader regression test coverage. |
teknium1
left a comment
There was a problem hiding this comment.
Thanks for isolating a real process-global approval leak. Current main still writes HERMES_CRON_SESSION globally at cron/scheduler.py:2688, and the approval layer still consumes it before gateway classification at tools/approval.py:194 and in the execute-code guard at tools/approval.py:3019.
Problems
- This branch is based on
1044d9f25d63b48c51fe40af0a4cfeea3b6de516; current main has substantially changed all touched files, including the session-context lifecycle and approval implementation. This needs manual salvage rather than a clean cherry-pick. - The regression coverage does not exercise the scheduler's real
contextvars.copy_context()worker handoff atcron/scheduler.py:3138-3139, where the cron marker must survive intoagent.run_conversation.
Suggested changes
- During salvage, add a scheduler-worker regression proving that cron retains
cron_modein the copied worker context while a concurrent gateway context is not classified as cron.
This is an automated hermes-sweeper review.
| @@ -1430,6 +1425,7 @@ def _run_job_impl(job: dict) -> tuple[bool, str, str, Optional[str]]: | |||
| platform="", | |||
There was a problem hiding this comment.
Please add a regression through the scheduler's copied worker context as well: current main submits contextvars.copy_context().run(agent.run_conversation, prompt) at cron/scheduler.py:3138-3139, so the cron marker must survive that hop without appearing in a concurrent live gateway context.
Summary
os.environHERMES_CRON_SESSIONcron_modebehaviorTest plan
python -m pytest tests/gateway/test_session_env.py tests/tools/test_cron_approval_mode.py tests/tools/test_execute_code_approval_cluster.py tests/tools/test_approval.py::TestApprovalTimeoutIsNotConsent tests/cron/test_cron_script.py tests/cron/test_scheduler.py -qContext
In a long-lived gateway process, cron scheduler ticks could leave
HERMES_CRON_SESSION=1in process-global environment. Later live Discord/Telegram sessions in the same process could then be misclassified as cron and get cron-only approval behavior, e.g.execute_codebeing blocked with “Cron jobs run without a user present...” even though a live gateway user was present. This keeps the cron safety default intact while preventing that marker from leaking across sessions.