Skip to content

fix(cron): scope cron job workdir to its own session instead of process-global cwd - #70989

Merged
teknium1 merged 4 commits into
mainfrom
fix/cron-workdir-scope
Jul 24, 2026
Merged

fix(cron): scope cron job workdir to its own session instead of process-global cwd#70989
teknium1 merged 4 commits into
mainfrom
fix/cron-workdir-scope

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

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

cron-workdir-scope

Hermes Agent and others added 4 commits July 24, 2026 13:21
#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.
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/cron Cron scheduler and job management labels Jul 24, 2026
@github-actions

github-actions Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

૮ >ﻌ< ა ci review

ran on 2799c75

all good!

@webtecnica

Copy link
Copy Markdown
Contributor

Closing — absorbed by @teknium1 in #70989 (merged 4582376c). Salvage of your #70548 via @kshitijk4poor's #70915. The cron workdir scoping fix was cherry-picked with your authorship preserved. Supersedes #70548 (conflicting, bundled already-merged commit).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cron Cron scheduler and job management P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: cron job workdir is applied as global process cwd and leaks into gateway sessions created during the run

4 participants