fix(cron): scope cron approval context per session - #77022
Merged
kshitijk4poor merged 1 commit intoAug 2, 2026
Conversation
kshitijk4poor
enabled auto-merge (rebase)
August 2, 2026 18:47
This was referenced Aug 2, 2026
Replace the process-global HERMES_CRON_SESSION env var with a per-session ContextVar so a cron tick in the gateway process cannot leak into unrelated live gateway/API/TUI turns. The cron scheduler now sets the ContextVar inside the job's try/finally scope and resets it on cleanup. Gateway, API server, ACP adapter, and TUI gateway all pass cron_session='' to explicitly mark their sessions as non-cron, masking any stale process env. Co-authored-by: hinablue <hinablue@gmail.com> Closes NousResearch#37968
kshitijk4poor
force-pushed
the
fix/cron-session-isolation
branch
from
August 2, 2026 18:50
d10361d to
d9fb24d
Compare
Collaborator
Related: #58663 is the earlier ContextVar repair for the same cron-marker leak. This patch additionally clears non-cron entry-point context and env-snapshot exposure; maintainer decision needed on the preferred scope. |
Open
1 task
This was referenced Aug 3, 2026
19 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Cron approval state no longer leaks from a cron tick into unrelated gateway/API/TUI sessions running in the same process. The process-global
HERMES_CRON_SESSION=1env var is replaced with a per-session_CRON_SESSIONContextVar that is scoped to each cron job's try/finally lifecycle.Root cause:
cron/scheduler.pysetos.environ["HERMES_CRON_SESSION"] = "1"at the start of every job but never restored it. The gateway runs the cron ticker in-process as a thread, so after the first cron tick, all subsequent gateway sessions inherited the cron marker. Withapprovals.cron_mode: deny, gateway dangerous commands were silently blocked; withapprove, they ran without the expected user approval prompt.Changes
gateway/session_context.py: Added_CRON_SESSIONContextVar (tri-state:_UNSET= legacy env fallback,"1"= cron,""= explicit non-cron).set_session_vars()acceptscron_sessionparam;clear_session_vars()resets it.cron/scheduler.py: Replacedos.environ["HERMES_CRON_SESSION"] = "1"with_cron_session_var.set("1")inside the job's try/finally, with token reset in cleanup.gateway/run.py,gateway/platforms/api_server.py,acp_adapter/server.py,tui_gateway/server.py: All gateway entry points passcron_session=""to explicitly mark non-cron sessions, masking any stale process env.tools/approval.py:_is_cron_approval_context()reads the ContextVar viaget_session_env()(with env fallback for CLI/tests). All fourenv_var_enabled("HERMES_CRON_SESSION")callsites replaced.Validation
Salvaged from PR #43370 by @hinablue (the most thorough of 4 competing PRs). Also closes #37969, #43549.
Co-authored-by: hinablue hinablue@gmail.com
Closes #37968