apply_patch sometimes invoked as shell command -> 'apply_patch: command not found'
@bolinfest @gpeal @pakrym-oai @dylan-hurd-oai
What version of Codex is running?
Built from source at commit cb78f23 (local copy).
Which model were you using?
Observed when assistant outputs resembling gpt-4.1 style tool calls; the bug is about how assistant-generated apply_patch invocations are parsed/handled by Codex regardless of model.
What platform is your computer?
Linux 6.14.3-x86_64-linode168 x86_64 unknown
Steps to reproduce
- Have the assistant produce a shell/tool call that embeds an
apply_patchinvocation in a shell script. Example variants that can trigger the issue:
{"command": ["apply_patch", "<<'EOF'\n*** Begin Patch\n*** Update File: README.md\n@@\n- old\n+ new\n*** End Patch\nEOF\n"]}{"command": ["bash","-lc","cd repo && apply_patch <<'EOF'\n*** Begin Patch\n*** Update File: README.md\n@@\n- old\n+ new\n*** End Patch\nEOF\n"]}
- Codex attempts to execute the tool call. If the invocation is not recognized by the
maybe_parse_apply_patchdetection logic, the runtime will spawn the requested shell command literally. - Since
apply_patchis a virtual tool implemented inside the Codex binary (invoked via--codex-run-as-apply-patch), there is noapply_patchbinary on PATH, so bash reports:
bash: line N: apply_patch: command not found
Expected behavior
- Codex should robustly detect
apply_patchinvocations (including heredoc forms and whenapply_patchis not at the start of the script) and either: (A) handle the patch programmatically, or (B) exec the Codex binary with--codex-run-as-apply-patch <patch>so the virtual tool runs — instead of running the shell which will try to find a realapply_patchbinary.
Actual behavior
- In some model-generated cases the detection logic misses the
apply_patchinvocation (examples: scripts that includecd && apply_patch <<'EOF', other shells likesh -lc, or different command-name spellings likeapply-patch). The fallback is to spawn the shell literally, which leads toapply_patch: command not found.
Relevant code pointers
- Detection/parsing:
codex-rs/apply-patch/src/lib.rs(maybe_parse_apply_patch,extract_heredoc_body_from_apply_patch_command) - Patch parsing:
codex-rs/apply-patch/src/parser.rs - Replacement exec:
codex-rs/core/src/codex.rs(creates exec with--codex-run-as-apply-patchwhen a patch is recognized) - Spawn/exec:
codex-rs/core/src/exec.rsandcodex-rs/core/src/spawn.rs
Suggested fixes / mitigation
- Relax the initial check in
extract_heredoc_body_from_apply_patch_command(it currently requires the script to start withapply_patch) and instead traverse the AST to find anyapply_patchinvocation and extract its heredoc body. - Accept more shell invocation forms (e.g.
sh -lc) and common command-name variants (e.g.apply-patch) or normalize names in detection. - Add a pre-spawn, permissive check: if any element of
params.commandcontainsapply_patch/applypatch/<<EOFetc., try to parse it as a patch before actually spawning. - Add unit tests that cover realistic failing variants (e.g.,
cd && apply_patch <<'EOF'andsh -lcforms).
Additional notes
This is a reliability/usability issue that causes noisy user-visible errors when the model's generated tool call is syntactically different from the narrow forms currently recognized. Improving detection should reduce these failures.
17 Comments
Note: This issue (including the analysis, examples, and suggested fixes) was generated automatically by Codex and has NOT been human-reviewed. It may contain inaccuracies, omissions, or incorrect assumptions.
Please treat the reproducer and remediation suggestions as preliminary: verify the reproduction steps and the diagnosis before acting.
If helpful, I can provide a minimal repro script, run logs, or submit a preliminary PR that implements a targeted mitigation (e.g. more permissive detection of
apply_patchheredocs). Please let me know which would be most useful.Note: This comment was generated automatically by Codex and has NOT been human-reviewed. Please verify the content before acting on it.
This snippet is intended to be added to the repository's
AGENTS.mdso that Codex will load it on startup; it contains a focusedapply_patchsection only. This snippet may help mitigate the issue described above, but please validate before relying on it.It seems to happen almost on every session for me. It will first try it wrongly, then it realizes it got it wrong and from then on it uses the tool correctly.
A bit of a waste of time, context and money.
@vitatea2023 @xdnlprs I believe https://github.com/openai/codex/pull/2646 should fix the issue, which was available starting in https://github.com/openai/codex/releases/tag/rust-v0.24.0.
Does this fix your issue?
I have this issue in 0.31.0 like that:
/c/scoop/apps/git-with-openssh/current/usr/bin/bash: line 1: applypatch: command not foundsame... here's a snippet:
same - this happens a lot:
the most fun part is sometimes it doesnt notice it didnt work, so it acts like it was completed...and then it's 2 or 3 more messages before it finally does the work.
Yeap... it does this a lot... some times it tries to run
applypatch. I was having problems with it on my system cause that was an actual program (from themakepatchpackage in ubuntu apt) and it would pass and codex would think he was successfully applying patches then to use an LSP server tool I make him use to check if he actually fixed the errors I asked him to fix, for him to find nothing changed. Then he'd go and try the same tool again...I've since uninstalled the package
makepatchthat addedapplypatchMy solution for most sessions is to tell it to use the filesystem mcp tool I set up in it's environment.
Just ran into another variation of this issue, where it tries to call
/usr/bin/apply_patch, and the path prefix means that the TUI hook doesn't catch it:I see the exact same error in my hobby project agent.
when using the
gpt-5-codexmodel, I have my own command tool schema for executing commands, but it does indeed believe that apply_patch is a native command.It seems to me, as if the
gpt-5-codexmodel has been trained to know about built in Codex agent commands, since it´s replicated completely outside of the codex agent itself, just using the model.I might be wrong, but it´s the reason I found this issue.
This issue still exists in the latest version 0.46.
apply_patchshould always be invoked as a shell command.Here is the logic where we modify the
PATHsuch that it should be available:https://github.com/openai/codex/blob/fc1723f13185a06e29ee263c05b6e0750024daaf/codex-rs/arg0/src/lib.rs#L82-L90
Are you using Codex in a bespoke way that would avoid that setup?
i also just ran into this. I am running the fish shell and launching codex via a custom command:
this happens to me recently more often (codex max) and fails very often due to incorrect syntax.
even the retries often fail after the first failure.
It is generating long agent loops for the most simple file edits.
vscode codex plugin version:
0.4.46<img width="559" height="481" alt="Image" src="https://github.com/user-attachments/assets/76984e90-aa09-4616-835f-9009dfba274c" />
Ran into this while using codex max. I stopped it, and told it to stop running apply_patch as a cli tool and stop using bash. I told it that it is running inside a codex cli environment. At least that stopped it for now.
Simply ridiculous behavior, I hope openAI fixes it.
Hi @bolinfest, I noticed this issue is still affecting users on recent versions despite the PATH fix in #2646.
I've opened a PR to address the remaining edge cases: #9498
The fix handles:
/usr/bin/apply_patch(absolute path prefix)./apply_patch(relative path prefix)apply-patch(hyphenated variant)For Linux environments, please consider using RAP:
https://github.com/francescobianco/rap
It provides a more reliable patch application workflow and helps avoid issues related to apply_patch behavior and sandbox/environment inconsistencies.