fix(cron): scope cron job workdir to its own session instead of process-global cwd - #70989
Merged
Conversation
#69396) The cron scheduler was mutating process-global state in two places: 1. no_agent path called os.chdir() which changed the global process cwd, leaking into concurrent gateway sessions. 2. The agent path set os.environ['TERMINAL_CWD'] which any gateway session could read during context-file discovery via resolve_context_cwd/build_context_files_prompt. Fix: - no_agent path: pass workdir as subprocess cwd parameter to _run_job_script() instead of os.chdir(). The Python process cwd is never mutated. - Agent path: in addition to the lock-serialized TERMINAL_CWD, also set the per-context _SESSION_CWD ContextVar from agent.runtime_cwd. This ContextVar is scoped to the current thread/context and NEVER leaks into other sessions. resolve_context_cwd() checks _SESSION_CWD first, so the cron's own context file discovery uses the correct workdir, while gateway sessions (which have no override) fall through to their own TERMINAL_CWD.
Eliminates the separate import/set/clear dance for _SESSION_CWD by passing cwd= directly to set_session_vars(), which already handles the ContextVar set internally and clears it via clear_session_vars(). Also includes the exception message in the no_agent error path. Follow-up to salvaged PR #70548 (#69396).
Follow-up to salvaged PR #70548 — _run_job_script now accepts workdir= kwarg, test mocks need to accept it too.
Contributor
૮ >ﻌ< ა ci reviewran on 2799c75 all good! |
This was referenced Jul 24, 2026
Closed
Closed
Contributor
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
A cron job's workdir no longer leaks into gateway sessions created during the run — workdir is applied via the existing per-session cwd machinery instead of process-global os.chdir.
Changes
Validation
Branch diff byte-identical to #70915 which had full green CI; targeted cron suites green.
Supersedes #70548 (CONFLICTING, bundled an already-merged commit).
Fixes #69396
Infographic