Skip to content

apply_patch: pure-addition chunk ignores its @@ context and silently inserts at end of file, reporting Success #30946

Description

@evanklem

What version of Codex CLI is running?

codex-cli 0.142.5

What subscription do you have?

Pro

Which model were you using?

N/A. Model independent.

What platform is your computer?

Linux 7.0.9-arch2-1 x86_64 unknown

What terminal emulator and version are you using (if applicable)?

Polypore 0.1.0 (AppImage), TERM=xterm-256color, no multiplexer.

Codex doctor report

{
  "schemaVersion": 1,
  "generatedAt": "1783019646s since unix epoch",
  "overallStatus": "fail",
  "codexVersion": "0.142.5",
  "checks": {
    "app_server.status": {
      "id": "app_server.status",
      "category": "app-server",
      "status": "ok",
      "summary": "background server is not running",
      "details": {
        "control socket": "/home/klemlitos/.codex/app-server-control/app-server-control.sock",
        "daemon state dir": "/home/klemlitos/.codex/app-server-daemon",
        "mode": "ephemeral",
        "pid file": "/home/klemlitos/.codex/app-server-daemon/app-server.pid (missing)",
        "settings": "/home/klemlitos/.codex/app-server-daemon/settings.json (missing)",
        "status": "not running",
        "update-loop pid file": "/home/klemlitos/.codex/app-server-daemon/app-server-updater.pid (missing)"
      },
      "remediation": null,
      "durationMs": 0
    },
    "auth.credentials": {
      "id": "auth.credentials",
      "category": "auth",
      "status": "ok",
      "summary": "auth is configured",
      "details": {
        "auth file": "/home/klemlitos/.codex/auth.json",
        "auth storage mode": "File",
        "stored API key": "false",
        "stored ChatGPT tokens": "true",
        "stored agent identity": "false",
        "stored auth mode": "chatgpt"
      },
      "remediation": null,
      "durationMs": 0
    },
    "config.load": {
      "id": "config.load",
      "category": "config",
      "status": "ok",
      "summary": "config loaded",
      "details": {
        "CODEX_HOME": "/home/klemlitos/.codex",
        "config.toml": "/home/klemlitos/.codex/config.toml",
        "config.toml parse": "ok",
        "cwd": "/home/klemlitos/Dev/opensourcecontribution/oscontribution",
        "enabled feature flags": "shell_tool, unified_exec, shell_snapshot, terminal_resize_reflow, sqlite, hooks, enable_request_compression, multi_agent, apps, tool_search_always_defer_mcp_tools, tool_suggest, plugins, in_app_browser, browser_use, browser_use_full_cdp_access, browser_use_external, computer_use, plugin_sharing, image_generation, resize_all_images, skill_mcp_dependency_install, mentions_v2, steer, guardian_approval, goals, collaboration_modes, tool_call_mcp_elicitation, personality, fast_mode, tui_app_server, auto_compaction, remote_compaction_v2, workspace_dependencies",
        "feature flag overrides": "none",
        "feature flags enabled": "33",
        "log dir": "/home/klemlitos/.codex/log",
        "mcp servers": "0",
        "model": "gpt-5.5",
        "model provider": "openai",
        "sqlite home": "/home/klemlitos/.codex"
      },
      "remediation": null,
      "durationMs": 0
    },
    "git.environment": {
      "id": "git.environment",
      "category": "git",
      "status": "ok",
      "summary": "git version 2.54.0",
      "details": {
        "PATH git #1": "/usr/bin/git",
        "PATH git entries": "1",
        "git build options": "git version 2.54.0; cpu: x86_64; built from commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0; sizeof-long: 8; sizeof-size_t: 8; shell-path: /bin/sh; rust: enabled; gettext: enabled; libcurl: 8.19.0; OpenSSL: OpenSSL 3.6.2 7 Apr 2026; zlib-ng: 2.3.3; SHA-1: SHA1_DC; SHA-256: SHA256_BLK; default-ref-format: files; default-hash: sha1",
        "git exec path": "/usr/lib/git-core",
        "git version": "git version 2.54.0",
        "repo detected": "false",
        "selected git": "/usr/bin/git"
      },
      "remediation": null,
      "durationMs": 45
    },
    "installation": {
      "id": "installation",
      "category": "install",
      "status": "fail",
      "summary": "npm install -g @openai/codex would update a different install",
      "details": {
        "PATH codex #1": "/home/klemlitos/.local/bin/codex",
        "PATH codex #2": "/home/klemlitos/.local/bin/codex",
        "PATH codex #3": "/home/klemlitos/.local/bin/codex",
        "PATH codex entries": "3",
        "current executable": "/home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin/codex",
        "install context": "npm (package /home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl, bin /home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin, resources /home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/codex-resources, path /home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/codex-path)",
        "managed by bun": "false",
        "managed by npm": "true",
        "managed package root": "/home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex",
        "npm package root": "/home/klemlitos/.npm-global/lib/node_modules/@openai/codex",
        "running package root": "/home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex"
      },
      "remediation": "Fix PATH or npm prefix so the running package root (/home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex) matches the npm global package root (/home/klemlitos/.npm-global/lib/node_modules/@openai/codex).",
      "durationMs": 61
    },
    "mcp.config": {
      "id": "mcp.config",
      "category": "mcp",
      "status": "ok",
      "summary": "no MCP servers configured",
      "details": {},
      "remediation": null,
      "durationMs": 0
    },
    "network.env": {
      "id": "network.env",
      "category": "network",
      "status": "ok",
      "summary": "network-related environment looks readable",
      "details": {
        "SSL_CERT_FILE": "readable file /etc/ssl/certs/ca-certificates.crt",
        "proxy env vars": "none"
      },
      "remediation": null,
      "durationMs": 0
    },
    "network.provider_reachability": {
      "id": "network.provider_reachability",
      "category": "reachability",
      "status": "ok",
      "summary": "active provider endpoints are reachable over HTTP",
      "details": {
        "ChatGPT base URL": "https://chatgpt.com/backend-api/ reachable (HTTP 404)",
        "reachability mode": "ChatGPT auth"
      },
      "remediation": null,
      "durationMs": 151
    },
    "network.websocket_reachability": {
      "id": "network.websocket_reachability",
      "category": "websocket",
      "status": "ok",
      "summary": "Responses WebSocket handshake succeeded",
      "details": {
        "DNS": "2 IPv4, 2 IPv6, first IPv6",
        "auth mode": "chatgpt",
        "connect timeout": "15000 ms",
        "endpoint": "wss://chatgpt.com/backend-api/<redacted>",
        "handshake result": "HTTP 101 Switching Protocols",
        "model provider": "openai",
        "models etag present": "true",
        "provider name": "OpenAI",
        "proxy env vars": "none",
        "reasoning header": "false",
        "server model present": "false",
        "supports websockets": "true",
        "wire API": "responses"
      },
      "remediation": null,
      "durationMs": 794
    },
    "runtime.provenance": {
      "id": "runtime.provenance",
      "category": "runtime",
      "status": "ok",
      "summary": "running npm on linux-x86_64",
      "details": {
        "commit": "unknown",
        "current executable": "/home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin/codex",
        "install method": "npm (package /home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl, bin /home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin, resources /home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/codex-resources, path /home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/codex-path)",
        "platform": "linux-x86_64",
        "version": "0.142.5"
      },
      "remediation": null,
      "durationMs": 0
    },
    "runtime.search": {
      "id": "runtime.search",
      "category": "search",
      "status": "ok",
      "summary": "search is OK (bundled)",
      "details": {
        "search command": "/home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/codex-path/rg",
        "search command readiness": "file exists",
        "search provider": "bundled"
      },
      "remediation": null,
      "durationMs": 0
    },
    "sandbox.helpers": {
      "id": "sandbox.helpers",
      "category": "sandbox",
      "status": "ok",
      "summary": "sandbox configuration is readable",
      "details": {
        "approval policy": "OnRequest",
        "codex-linux-sandbox helper": "/home/klemlitos/.codex/tmp/arg0/codex-arg0Quyqni/codex-linux-sandbox",
        "execve wrapper helper": "/home/klemlitos/.codex/tmp/arg0/codex-arg0Quyqni/codex-execve-wrapper",
        "filesystem sandbox": "restricted",
        "network sandbox": "restricted"
      },
      "remediation": null,
      "durationMs": 0
    },
    "state.paths": {
      "id": "state.paths",
      "category": "state",
      "status": "ok",
      "summary": "state paths and databases are inspectable",
      "details": {
        "CODEX_HOME": "/home/klemlitos/.codex (dir)",
        "active rollout files": "162 files, 800612511 total bytes, 4942052 average bytes",
        "archived rollout files": "0 files, 0 total bytes, 0 average bytes",
        "goals DB": "/home/klemlitos/.codex/goals_1.sqlite (file)",
        "goals DB integrity": "ok",
        "log DB": "/home/klemlitos/.codex/logs_2.sqlite (file)",
        "log DB integrity": "ok",
        "log dir": "/home/klemlitos/.codex/log (dir)",
        "memories DB": "/home/klemlitos/.codex/memories_1.sqlite (file)",
        "memories DB integrity": "ok",
        "sqlite home": "/home/klemlitos/.codex (dir)",
        "state DB": "/home/klemlitos/.codex/state_5.sqlite (file)",
        "state DB integrity": "ok"
      },
      "remediation": null,
      "durationMs": 402
    },
    "state.rollout_db_parity": {
      "id": "state.rollout_db_parity",
      "category": "threads",
      "status": "ok",
      "summary": "rollout files and state DB thread inventory agree",
      "details": {
        "default model provider": "openai",
        "rollout DB active files": "162",
        "rollout DB active rows": "162",
        "rollout DB archive mismatches": "0",
        "rollout DB archived files": "0",
        "rollout DB archived rows": "0",
        "rollout DB duplicate DB paths": "0",
        "rollout DB duplicate rollout thread ids": "0",
        "rollout DB malformed file names": "0",
        "rollout DB missing active rows": "0",
        "rollout DB missing archived rows": "0",
        "rollout DB model providers": "openai=162",
        "rollout DB rows": "162",
        "rollout DB scan cap reached": "false",
        "rollout DB scan errors": "0",
        "rollout DB sources": "cli=154, exec=4, subagent:thread_spawn=4",
        "rollout DB stale rows": "0"
      },
      "remediation": null,
      "durationMs": 4749
    },
    "system.environment": {
      "id": "system.environment",
      "category": "system",
      "status": "ok",
      "summary": "OS language en-US",
      "details": {
        "EDITOR": "set",
        "LANG": "en_US.UTF-8",
        "VISUAL": "not set",
        "os": "Arch Linux Rolling Release [64-bit]",
        "os language": "en-US",
        "os type": "Arch Linux",
        "os version": "Rolling Release"
      },
      "remediation": null,
      "durationMs": 8
    },
    "terminal.env": {
      "id": "terminal.env",
      "category": "terminal",
      "status": "ok",
      "summary": "terminal metadata was detected",
      "details": {
        "COLORTERM": "truecolor",
        "DISPLAY": "present",
        "TERM": "xterm-256color",
        "WAYLAND_DISPLAY": "present",
        "color output": "disabled (stdout is not a terminal)",
        "effective locale": "en_US.UTF-8",
        "stderr is terminal": "false",
        "stdin is terminal": "false",
        "stdout is terminal": "false",
        "terminal": "unknown",
        "terminal size": "80x24"
      },
      "remediation": null,
      "durationMs": 3
    },
    "terminal.title": {
      "id": "terminal.title",
      "category": "title",
      "status": "ok",
      "summary": "terminal title default",
      "details": {
        "terminal title activity": "true",
        "terminal title items": "activity, project-name",
        "terminal title project source": "cwd",
        "terminal title project value": "oscontribution",
        "terminal title source": "default"
      },
      "remediation": null,
      "durationMs": 0
    },
    "updates.status": {
      "id": "updates.status",
      "category": "updates",
      "status": "fail",
      "summary": "update would target a different npm install",
      "details": {
        "cached latest version": "0.142.5",
        "check for update on startup": "true",
        "last checked at": "2026-07-02T18:46:31.255194535Z",
        "latest version": "0.142.5",
        "latest version status": "current version is not older",
        "npm package root": "/home/klemlitos/.npm-global/lib/node_modules/@openai/codex",
        "running package root": "/home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex",
        "update action": "npm install -g @openai/codex",
        "version cache": "/home/klemlitos/.codex/version.json"
      },
      "remediation": "Fix PATH or npm prefix so the running package root (/home/klemlitos/.npm/_npx/c8ab89660c602c20/node_modules/@openai/codex) matches the npm global package root (/home/klemlitos/.npm-global/lib/node_modules/@openai/codex).",
      "durationMs": 344
    }
  }
}

What issue are you seeing?

When an *** Update File hunk contains a chunk that consists only of + (added) lines anchored by a @@ context marker, a pure insertion, with no surrounding context lines and no - lines apply_patch:

  1. successfully locates the @@ context line in the file,
  2. then discards that position and appends the new lines at the end of the file instead,
  3. and reports Success. Updated the following files: M <file> with exit code 0.

The inserted code lands in a different scope than the patch requested (e.g. inside the last class in the file instead of the class named in the @@ line). Since the file still parses, the misplaced method can go unnoticed.

If the @@ line doesn't exist in the file, apply_patch correctly fails with Failed to find context ... so the anchor is validated, just not used.

What steps can reproduce the bug?

In a normal session, this triggers whenever the model emits a pure-addition chunk (an @@ anchor followed by only + lines) e.g. when asked to add a method to a named class. Since whether the model repeats context lines is nondeterministic per turn, the steps below invoke the apply_patch applet directly via --codex-run-as-apply-patch, which is the same entry point codex core uses when the model calls apply_patch (arg0/src/lib.rs, CODEX_CORE_APPLY_PATCH_ARG1), with a fixed payload:

cd "$(mktemp -d)"

cat > sample.py <<'PYEOF'
class Alpha:
    def method_a(self):
        return 1

class Beta:
    def method_b(self):
        return 2

class Gamma:
    def method_c(self):
        return 3
PYEOF

codex --codex-run-as-apply-patch "$(cat <<'EOF'
*** Begin Patch
*** Update File: sample.py
@@ class Alpha:
+    def method_new(self):
+        return 99
*** End Patch
EOF
)"

cat sample.py

Observed exit code 0, and method_new lands inside Gamma, not Alpha:

Success. Updated the following files:
M sample.py
class Alpha:
    def method_a(self):
        return 1

class Beta:
    def method_b(self):
        return 2

class Gamma:
    def method_c(self):
        return 3
    def method_new(self):
        return 99

What is the expected behavior?

Either of:

  1. The added lines are inserted immediately after the located @@ class Alpha: context line, matching how every other chunk type respects the seek position after a context match, or
  2. If pure-addition chunks are intentionally unsupported, apply_patch rejects the chunk with an error instead of applying it at the wrong location.

Additional information

Root cause is in codex-rs/apply-patch/src/lib.rs, compute_replacements():

  • The @@ context is found via seek_sequence, and line_index is advanced to just past it (~lines 727–740).
  • The next branch handles empty old_lines by unconditionally appending at EOF, never reading line_index:
if chunk.old_lines.is_empty() {
    // Pure addition (no old lines). We'll add them at the end or just
    // before the final empty line if one exists.
    let insertion_idx = if original_lines.last().is_some_and(String::is_empty) {
        original_lines.len() - 1
    } else {
        original_lines.len()
    };
    replacements.push((insertion_idx, 0, chunk.new_lines.clone()));
    continue;
}

The EOF fallback is only correct for the anchorless pure-addition case (no @@ at all). When chunk.change_context is Some(..) and the seek succeeded, the insertion should use line_index:

let insertion_idx = if chunk.change_context.is_some() {
    line_index
} else if original_lines.last().is_some_and(String::is_empty) {
    original_lines.len() - 1
} else {
    original_lines.len()
};

The parser explicitly allows this chunk shape (UpdateFileChunk { change_context: Some(..), old_lines: vec![], .. }parser.rs doc comment: "old_lines must occur strictly after change_context"). This chunk shape is emitted whenever the model adds a method/function to a named scope without repeating context lines.

Distinct from #30505 (indentation corruption on fuzzy match).

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLIIssues related to the Codex CLIbugSomething isn't workingtool-callsIssues related to tool calling

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions