Fix cron session context isolation - #43370
Conversation
Verification ReviewReviewed by: automated PR review cron Summary: Correctly migrates Observations:
LGTM. |
|
Verification Review — reviewed diff, no issues found. The ContextVar migration from process-global
Clean implementation. The process-global → ContextVar migration pattern matches the existing delivery-target ContextVars already in use. |
|
Verification: Cron session isolation via ContextVar — looks clean. The migration from process-global Test coverage is solid — One note: the ACP adapter ( |
egilewski
left a comment
There was a problem hiding this comment.
Recommendation: approve / looks mergeable.
I reviewed this security fix against current GitHub main 4cecb1a13a87da169a82279bfd1f1db19eb732da, PR base 5a4297a11a83c38ac24eec7df0e4e41d6b3dbb9f, and PR head d7faa3604b846aee4daf20f28720cb5ec70fb2f0.
Validation:
git rev-list --left-right --count 4cecb1a13a87da169a82279bfd1f1db19eb732da...d7faa3604b846aee4daf20f28720cb5ec70fb2f0:44 2; the PR is behind current main but only by upstream commits.git merge-tree --write-tree 4cecb1a13a87da169a82279bfd1f1db19eb732da d7faa3604b846aee4daf20f28720cb5ec70fb2f0: passed, wrote tree655c7001f005220cd601117f33589324111e0abb.git diff --check 4cecb1a13a87da169a82279bfd1f1db19eb732da...d7faa3604b846aee4daf20f28720cb5ec70fb2f0: passed with no whitespace/conflict-marker findings.- Synthetic approval-guard probe on current main with leaked
HERMES_CRON_SESSION=1plus a normal gateway session: reproduced the bug, returningapproved=False,outcome=blocked. - The same probe on PR head with the gateway session binding
cron_session="": passed, returningapproved=Trueanduser_approved=True. /home/mac/hermes-agent/.venv/bin/python -B -m pytest -q tests/cron/test_scheduler_cron_session_isolation.py tests/tools/test_execute_code_approval_cluster.py tests/tools/test_cron_approval_mode.py tests/gateway/test_session_env.py -p no:cacheprovider:59 passed in 2.46s.coderabbit review --plain --base upstream/main --type committed: completed with no findings.
Finding:
The patch preserves the security boundary I expected: cron jobs still hit approvals.cron_mode through scoped context, while normal gateway/API/ACP/TUI turns explicitly mask a stale process-global cron marker before execute-code approval routing. I did not find a blocker in the reviewed scope.
Signed: GPT-5.5-xhigh in Codex
|
Thanks for tracing this to process-global state. The premise still holds on current main: Suggested changes
Automated hermes-sweeper review. |
f390e6d to
571961c
Compare
|
Refreshed this PR onto current The historical snapshot was replaced with a current-architecture port:
Fresh verification on commit
The PR is now one commit ahead of current main and GitHub reports it as mergeable. |
Related to #58663: both scope the cron marker per execution context, while this head additionally masks a stale legacy environment marker at gateway, API, ACP, and TUI entrypoints. Reviewers can consolidate the overlapping fixes. |
Related to #58663: both address the process-global cron approval marker. This refreshed version additionally masks stale cron state in non-cron gateway/API/ACP/TUI bindings, so the implementations should be compared rather than treated as duplicates. |
Restore the cron ContextVar token after each job, update current-main approval test seams, and keep ordered cron/approval suites context-isolated.
571961c to
a7322d5
Compare
|
suggesting changes
The separate Security evidence:
Signed: GPT-5.6-sol-xhigh in Codex |
|
Merged via #77022. Your commits were salvaged onto current main with authorship preserved in the Co-authored-by trailer. Your PR had the most thorough approach of the four competing PRs — the tri-state ContextVar design, all four gateway entry points covered, and comprehensive E2E isolation tests. Thank you! The original PR branch was stale against current main (conflicts in gateway/run.py, acp_adapter/server.py, and test files), so the diff was applied onto a fresh worktree at upstream/main and conflicts resolved preserving current main's changes plus your intended fix. |
What does this PR do?
This PR fixes a cron-session isolation bug where
HERMES_CRON_SESSIONwas stored in process-global environment state and could leak into later non-cron turns handled by the same Python process.In practice, this could cause normal gateway/API/ACP/TUI
execute_codeapprovals to be misclassified as cron context and incorrectly routed throughapprovals.cron_mode.This patch fixes the problem at the root-cause level by moving cron-session state into scoped per-run context instead of relying on
os.environ["HERMES_CRON_SESSION"].It also adds regression coverage proving that:
approvals.cron_modeexecute_codecan still be blocked whenapprovals.cron_mode: denyexecute_codeapproval flow is not polluted by the earlier cron runRelated Issue
Fixes #37968
Related:
Type of Change
Changes Made
cron/scheduler.pywith scoped session context viaset_session_vars(..., cron_session="1")HERMES_CRON_SESSIONContextVar support ingateway/session_context.pytools/approval.pyto resolve cron approval context from session-scoped context first, with env fallback for compatibilitygateway/run.pygateway/platforms/api_server.pyacp_adapter/server.pytui_gateway/server.pytests/tools/test_execute_code_approval_cluster.pytests/tools/test_cron_approval_mode.pytests/cron/test_scheduler_cron_session_isolation.pyHow to Test
approvals.cron_mode: deny, then triggering a normal gatewayexecute_coderequest in the same process.Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/AScreenshots / Logs
Scheduler isolation regression:
Combined targeted suite: