Repository navigation
Description
Summary
On Windows, when Codex is used with the “auto review / approve for me” permission mode, Codex enters the Windows sandbox path. In this mode, apply_patch fails consistently.
The issue appears to be in the generated apply_patch.bat wrapper, not in the patch content, target directory, file encoding, or the underlying codex.exe --codex-run-as-apply-patch implementation.
Two separate problems were observed:
- The generated
apply_patch.batmay point to aWindowsAppspackagedcodex.exe, which fails withAccess is denied. - Even when the wrapper is manually changed to point to a working local
codex.exe,.bat/.cmdforwarding via%*corrupts multiline patch arguments, causing patch parsing errors.
A PowerShell .ps1 wrapper preserves the multiline patch argument correctly and works both from the shell and from a real codex exec session when placed earlier in PATH.
Environment
-
OS: Windows
-
Codex CLI version:
codex-cli 0.143.0 -
Permission mode: “approve for me” / auto review
-
Windows sandbox config:
[windows].sandbox = "elevated" -
Main Codex entry from PowerShell:
C:\Users\bart\AppData\Roaming\npm\codex.ps1
-
Also present:
C:\Program Files\WindowsApps\OpenAI.Codex_26.623.19656.0_x64__2p2nqsd0c76g0\app\resources\codex.exeC:\Users\bart\AppData\Local\OpenAI\Codex\bin\<hash>\codex.exe
Reproduction
Create a clean repro workspace:
mkdir C:\codex-sandbox-probe
cd C:\codex-sandbox-probe
git initRun Codex in Windows sandbox / auto review mode and ask it to create a file using apply_patch.
Observed failure:
Access is denied.
The generated wrapper was found at:
C:\Users\bart\.codex\tmp\arg0\codex-arg0zmS40N\apply_patch.bat
Its content was effectively:
"C:\Program Files\WindowsApps\OpenAI.Codex_26.623.19656.0_x64__2p2nqsd0c76g0\app\resources\codex.exe" --codex-run-as-apply-patch %*Directly invoking that WindowsApps codex.exe also failed with Access is denied.
Additional Findings
A local Codex binary exists and is executable:
C:\Users\bart\AppData\Local\OpenAI\Codex\bin\ea1c60319a1dcb19\codex.exe
Direct call succeeds:
& "C:\Users\bart\AppData\Local\OpenAI\Codex\bin\ea1c60319a1dcb19\codex.exe" --codex-run-as-apply-patch $patchThis successfully created files at multiple directory depths:
local-codex-probe.txt
dir1/probe-1.txt
dir1/probe-2.txt
src/main/java/Probe.java
However, using a .bat or .cmd wrapper around the same local codex.exe fails:
@echo off
"C:\Users\bart\AppData\Local\OpenAI\Codex\bin\ea1c60319a1dcb19\codex.exe" --codex-run-as-apply-patch %*Failure:
Invalid patch: The last line of the patch must be '*** End Patch'
This suggests that .bat / .cmd argument forwarding via %* corrupts multiline patch arguments.
stdin was also tested:
$patch | & "...\codex.exe" --codex-run-as-apply-patchIt failed with:
Error: --codex-run-as-apply-patch requires a UTF-8 PATCH argument.
So stdin does not appear to be supported for this helper mode.
Working Workaround
A PowerShell wrapper works:
# C:\Users\bart\.codex\bin\apply_patch.ps1
$CodexExe = "C:\Users\bart\AppData\Local\OpenAI\Codex\bin\ea1c60319a1dcb19\codex.exe"
if (-not (Test-Path $CodexExe)) {
Write-Error "codex.exe not found: $CodexExe"
exit 1
}
if ($args.Count -lt 1) {
Write-Error "missing patch argument"
exit 1
}
& $CodexExe --codex-run-as-apply-patch $args[0]
exit $LASTEXITCODEThen prepend the wrapper directory to the current PowerShell session PATH before launching Codex:
$env:PATH = "C:\Users\bart\.codex\bin;$env:PATH"
codexThis makes Get-Command apply_patch resolve to:
C:\Users\bart\.codex\bin\apply_patch.ps1
This workaround was also validated in a real Codex non-interactive session:
codex exec -C C:\codex-sandbox-probe "Use apply_patch to add codex-agent-path-probe2.txt with exactly: hello from codex exec path wrapper. Then stop."Result:
codex-agent-path-probe2.txtwas created successfully.- File content matched the expected text.
- Codex output showed
apply patchcompleted.
Expected Behavior
Codex-generated apply_patch wrapper on Windows should:
- Select an executable path that is actually callable from the sandboxed context.
- Preserve multiline patch arguments exactly.
- Avoid using
.bat/.cmd %*if that corrupts multiline patch content. - Prefer a robust mechanism such as a PowerShell wrapper, a native shim executable, or another argument-safe transport.
Actual Behavior
The generated wrapper may:
- Point to a
WindowsAppscodex.exethat fails withAccess is denied. - Use
.bat %*, which corrupts multiline patch arguments even if the targetcodex.exeis otherwise valid. - Cause
apply_patchto fail under Windows sandbox / auto-review mode.
Impact
This makes normal Codex editing unreliable under the safer Windows sandbox permission mode. The only built-in workaround appears to be using full access, which is too risky for routine development.
Using PowerShell or Python to edit files manually is not a good replacement because it can introduce encoding issues in existing projects.
Suggested Fix
Consider changing the Windows apply_patch wrapper generation strategy:
- Do not generate
.bat/.cmdwrappers for multiline patch forwarding. - Avoid resolving to
WindowsAppspackaged executables when they cannot be executed from the sandbox context. - Prefer a PowerShell wrapper, native shim executable, or argument-file based approach that preserves multiline UTF-8 patch content.
- Optionally allow
--codex-run-as-apply-patchto read patch content from stdin or a temp file to avoid command-line argument corruption.
Activity
aiYb04 commentedon Sep 2, 2026
Still reproducible on September 2, 2026 with:
I observed a regression immediately after the Codex App updated from 26.825 to 26.831: an existing task that previously used the internal Code Mode
tools.apply_patchpath and produced nativeFileChangeevents switched to directexec_commandcalls. New tasks also invoke the generated%USERPROFILE%\.codex\tmp\arg0\...\apply_patch.batwrapper.The same two failures described in this issue occur:
Invoking the local
codex.exe --codex-run-as-apply-patchdirectly through PowerShellProcessStartInfo.ArgumentListsucceeds with the same patch. This appears to be both an unresolved Windows wrapper bug and a new desktop routing/exposure regression: the 26.831 app update causes ordinary file edits to hit the broken wrapper much more frequently, leading simple edits to stall and retry.Session/task IDs and local absolute paths are intentionally omitted.
djs4000 commentedon Sep 18, 2026
I can reproduce this issue on Windows with Codex CLI
0.155.0-alpha.9. I did some additional testing to isolate where the failure occurs.Environment
26100.94575.1.26100.94440.155.0-alpha.9The generated
apply_patch.batcontains:So in this case the wrapper is using the cached executable under
AppData\Local, not aWindowsAppsexecutable.Result:
--codex-run-as-apply-patchdirectly worksI created a multiline patch and passed it directly to
codex.exe:Result:
So the underlying patch implementation appears to work correctly, including receiving the multi-line patch as a single argument.
.batfailsI then located a currently generated Codex
apply_patch.batand invoked it with essentially the same patch:Result:
No file was created:
Result:
A/B result
The same multiline patch therefore behaves differently depending only on whether the generated batch wrapper is involved:
This seems to isolate the problem to argument forwarding through the generated Windows
.batwrapper, rather than:codex.exebinary;--codex-run-as-apply-patchimplementation; orIn particular,
%*in the generated.batappears to be altering the multiline argument before it reachescodex.exe.Effect during normal Codex operation
This has been going on for long time. I've ignored it thus far because the agent could successfully run normal PowerShell/file inspection commands, but attempts to use the patch helper failed before any target file was modified.
The direct test above suggests that falling back to direct file writes should not actually be necessary: the patch engine itself works; it is the Windows wrapper used to reach it that fails.
A PowerShell-based wrapper, direct invocation of the executable, stdin-based patch transport, or another mechanism that avoids forwarding a multiline patch through batch
%*would seem likely to avoid this failure.