codex icon indicating copy to clipboard operation
codex copied to clipboard

Windows CLI: command runner ignores session cwd and executes in a different repository

Open thomassge opened this issue 2 months ago • 5 comments

Summary

On Windows, Codex CLI starts in the correct directory and reports the correct session working directory, but shell/tool commands execute from a different Git repository.

This is dangerous because the UI/session context appears correct while commands such as git status, tests, file operations, etc. can run against the wrong checkout.

Environment

  • Codex CLI: 0.153.2
  • OS: Windows 11 x64
  • Shell: PowerShell 7
  • Installation: npm (@openai/codex)
  • Git: 2.53.0.windows.1
  • Background app server: not running (codex doctor --json reports ephemeral / not running)

All paths below are anonymized.

Reproduction

Assume two local checkouts:

C:\Users\<user>\dev\repo
C:\Users\<user>\workspaces\repo-copy

From PowerShell:

cd C:\Users\<user>\workspaces\repo-copy
git status --short --branch
codex

Before starting Codex, PowerShell is correctly in repo-copy.

Codex UI also shows repo-copy as the active directory.

Inside Codex:

/pwd

reports:

C:\Users\<user>\workspaces\repo-copy

But asking Codex to run:

Run Get-Location and git status --short --branch. Do not modify anything.

produces:

Get-Location:
C:\Users\<user>\dev\repo

git status:
## main...origin/main

So the session/TUI cwd and the command runner cwd disagree.

Important: reproduces outside Git worktrees

The issue was first observed in a linked Git worktree, but it also reproduces in a separate normal clone.

Therefore this does not appear to be specific to Git linked worktrees.

Diagnostics

A sanitized codex doctor --json confirms that Codex initialization sees the correct checkout before the turn starts:

config.load.cwd:
C:\Users\<user>\workspaces\repo-copy

git.environment.repo root:
C:\Users\<user>\workspaces\repo-copy

git.environment.git branch:
<expected branch>

app_server.status:
background server is not running

The wrong cwd only becomes visible when Codex executes shell/tool commands.

Variants tested

The same wrong command-runner cwd occurs with:

  • normal codex
  • codex --sandbox danger-full-access
  • codex --yolo
  • codex -c 'model_reasoning_effort="medium"'
  • codex -c 'windows.sandbox="unelevated"'

Changing sandbox mode does not fix the problem.

Expected behavior

If Codex is launched from:

C:\Users\<user>\workspaces\repo-copy

and /pwd reports that directory, every shell/tool command should inherit or explicitly use that same cwd.

If the command runner cannot honor the session cwd, it should fail closed rather than silently execute in another repository.

Actual behavior

The UI and /pwd report the requested checkout, while the command runner executes in another existing repository.

This can cause commands to read, test, stage, commit, or modify the wrong repository/branch.

Related

#42001 reports a similar symptom in Codex Desktop where an explicit project cwd is ignored and commands run from a stale directory. This report is for Codex CLI 0.153.2 on Windows and reproduces without the Desktop app running.

thomassge avatar Sep 04 '26 16:09 thomassge

Potential duplicates detected. Please review them and close your issue if it is a duplicate.

  • #42001

Powered by Codex Action

github-actions[bot] avatar Sep 04 '26 16:09 github-actions[bot]

I found a useful source-level discriminator in the exact rust-v0.153.2 release (657a993cbee87acf52d14b758ce49dbd46d1b8eb). There are two cwd values on the CLI path:

  • /pwd is handled locally by the TUI and prints self.config.cwd.
  • exec_command does not use that value by default; it resolves the selected TurnEnvironment and takes turn_environment.cwd() (TurnEnvironment.selection.cwd). That value is then copied into ExecCommandRequest and carried onward to the exec-server/process launch.

So /pwd reporting repo-copy while Get-Location runs in repo can happen if those two cwd states have diverged; /pwd by itself does not show the cwd the tool handler is actually sending. Fresh threads without an explicit environment selection are initialized from config.cwd, so if this is a fresh CLI thread the interesting question is where an explicit/updated selected environment could have become stale afterward.

A sharp next diagnostic would be to log, immediately inside the exec_command handler and before any sandbox transform, both self/config (or legacy/session) cwd and turn_environment.selection.cwd. If they already differ, that exonerates the Windows sandbox/runner and moves the bug to environment selection/update/inheritance. If they are equal there, the next useful log point is the exec-server SpawnRequest.cwd / runner effective cwd.

A regression for this failure family (#42001 looks compatible) should switch/reopen from repo A to repo B and assert all three agree: UI /pwd, selected environment cwd, and the spawned shell's Get-Location. This seems more discriminating than another sandbox-mode A/B because the reporter has already reproduced the bug with danger-full-access, --yolo, and windows.sandbox="unelevated".

84dnnvbdvp-debug avatar Sep 04 '26 17:09 84dnnvbdvp-debug

I traced the current main execution path to narrow where this cwd split can occur. This is source-level analysis, not a local reproduction of 0.153.2 (my Windows host currently has 0.149.1), so I don't want to overstate the exact trigger.

The useful distinction is that the TUI and the tool runner do not read the same cwd source:

  • /pwd in codex-rs/tui/src/chatwidget/slash_dispatch.rs displays self.config.cwd.
  • exec_command in codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs instead takes its base cwd from turn_environment.cwd(), then stores that in the ExecCommandRequest.
  • codex-rs/core/src/unified_exec/process_manager.rs forwards request.cwd into exec-server unchanged.
  • the normal core spawn calls cmd.current_dir(cwd), and the elevated Windows command runner likewise derives effective_cwd(&req.cwd, ...) before spawning either ConPTY or pipes.

So the source currently suggests the divergence is above the final process-spawn layer: config.cwd can look correct to /pwd while the selected TurnEnvironment carries a different cwd into execution. There is also a resume/inheritance path in thread_manager.rs that can prefer explicit/inherited environment selections before falling back to defaults built from config.cwd; I have not proven that is the trigger for this report, especially because the report also reproduces from a normal codex launch.

A focused diagnostic/regression that may make this deterministic would record, for the same tool call, (config.cwd, primary TurnEnvironment.selection.cwd, ExecCommandRequest.cwd) and assert they cannot silently diverge for the local default environment. A useful regression shape is a thread/session whose visible fallback cwd is repo B while a retained local environment still points at repo A: /pwd and Get-Location should either agree on B or execution should fail closed before running the command.

This also explains why changing sandbox mode would not necessarily affect the symptom: both the ordinary spawn path and the elevated Windows runner honor the cwd they receive.

fscfede-beep avatar Sep 04 '26 19:09 fscfede-beep

Update (2026-10-13): still reproducible on Codex CLI v0.162.1 — cwd mismatch and command-runner setup errors

The original Windows command-runner working-directory problem remains reproducible on Codex CLI v0.162.1 (original report was v0.153.2). This update contains anonymized paths only.

Environment / reproduction

  • Windows 11, PowerShell 7, Codex CLI 0.162.1.
  • Primary checkout: C:\Users\<user>\dev\repo (main).
  • Expected task worktree: C:\Users\<user>\workspaces\issue-170\repo (dedicated task branch).
  • From an ordinary PowerShell session in the task worktree, git status --short --branch, git fetch origin, and git log --oneline HEAD..origin/<task-branch> all execute correctly and resolve the expected task branch.
  • During Codex session resume, the UI offered a choice between a previous session directory (another issue/worktree) and the current working directory (issue-170 worktree). Even when choosing the current directory, the command runner could execute in the primary checkout instead.

Observations

  1. A read-only Codex command Get-Location; git branch --show-current; git status --short --branch produced the primary checkout and main instead of the configured worktree.
  2. In a follow-up read-only diagnostic session, an invocation with explicit issue-worktree workdir failed before PowerShell launched:
    Failed to create unified exec process:
    helper_unknown_error: setup refresh had errors
    
  3. A minimal Get-Location call without an explicit workdir succeeded, but returned the primary checkout. Further probes (including simple location/profile diagnostics) intermittently failed at the same process-setup stage.
  4. The diagnostic agent could not inspect PowerShell profiles, child process arguments, or -NoProfile behavior because those process launches were blocked. Thus a PowerShell-profile cause is not conclusively excluded, although failures occurring before shell launch implicate command-runner setup.
  5. No files or Git configuration were modified by the diagnostic attempt.

Impact

Task isolation is unsafe: the session/worktree shown in the UI may not match the repository receiving tool commands. The new command-runner setup errors additionally block ordinary read-only diagnostics and task completion. Independent PowerShell works correctly.

Request

Please investigate whether Codex CLI's Windows unified-exec/session resume/workdir setup can inherit the wrong checkout, and whether the setup refresh had errors failure shares an underlying cause. The exact root cause is not confirmed. A guard or explicit error when execution cwd differs from configured cwd would also reduce risk of unintended writes to main.

thomassge avatar Oct 13 '26 17:10 thomassge

I made a small read-only tool for checking this on Windows. If you’re still seeing it, could you run python path/to/preflight.py --expected "C:/path/to/the/intended/checkout" once in PowerShell and once from a shell command inside Codex? It shows whether Git is in the expected checkout and whether giving Git the path directly helps. The report leaves out local paths and filenames.

I tried it with two local Windows checkouts. I’d be curious what it reports when the Codex issue actually happens.

https://github.com/viniciusaridio-del/worktree-preflight

Ristrct avatar Nov 17 '26 03:11 Ristrct