codex icon indicating copy to clipboard operation
codex copied to clipboard

codex exec --output-last-message leaves an empty file behind when the run fails (0.168.0)

Open christianburrell opened this issue 2 weeks ago • 3 comments

What version of Codex CLI is running?

codex-cli 0.168.0 (standalone package, x86_64-unknown-linux-musl). 0.163.0 behaves as before. main is unchanged here: codex-rs/exec/src/lib.rs still binds the file before the agent starts, and codex-rs/exec-server/src/no_follow/mod.rs is identical to the 0.168.0 tag.

What subscription do you have?

ChatGPT. The repro runs logged out, so no plan is involved.

Which model were you using?

None: the repro fails at the first request, before a model runs.

What platform is your computer?

Linux 7.0.0-42-generic x86_64 unknown

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

None: non-interactive codex exec started from a script.

Codex doctor report

not included: the repro uses an empty CODEX_HOME and no login, so no local state is involved

What issue are you seeing?

Since #58974 (first released in 0.168.0), codex exec -o FILE opens FILE before the agent starts. When the run then ends without a final message (here a 401; in practice a usage limit or a network failure), FILE stays behind as a 0-byte file. Before #58974 (0.167.x and earlier; tested here on 0.163.0), FILE appeared only when the last message was written, so "FILE exists" meant "the agent answered".

Scripts that rely on that now misread failed runs. Ours review a bundle once: they refuse to start when the output file already exists, and after a usage-limit failure they retry on another account. On 0.168.0 the failed first attempt leaves an empty file, so the retry is refused.

What steps can reproduce the bug?

Save as probe.sh and run bash probe.sh /path/to/old/codex /path/to/new/codex (no login, no API key; each run fails with 401 because no credentials are sent):

#!/usr/bin/env bash
# Probe (openai/codex): does `codex exec --output-last-message FILE` leave FILE behind when the run fails before the
# agent answers? No login, no API key, no model run: with an empty CODEX_HOME the first request is refused with
# 401 (no credentials are sent), so every run fails the same way. Prints one row per binary.
# Usage: probe_codex_olm_failed_run.sh /path/to/codex [/path/to/another/codex ...]
set -u
for bin in "$@"; do
  w=$(mktemp -d "${TMPDIR:-/tmp}/olm-probe.XXXXXX")
  mkdir -p "$w/home" "$w/work"
  echo "say hi" > "$w/prompt.txt"
  ver=$(env -i HOME="$w" PATH=/usr/bin:/bin CODEX_HOME="$w/home" "$bin" --version 2>/dev/null | tail -1)
  env -i HOME="$w" PATH=/usr/bin:/bin CODEX_HOME="$w/home" \
    timeout 180 "$bin" exec --skip-git-repo-check -s read-only -C "$w/work" --color never \
    -o "$w/work/last.md" -- - < "$w/prompt.txt" > "$w/run.log" 2>&1
  rc=$?
  if [ -e "$w/work/last.md" ]; then st="exists, $(stat -c %s "$w/work/last.md") bytes"; else st="absent"; fi
  echo "$ver | exit $rc | -o file after the failed run: $st | last log line: $(grep -v '^\s*$' "$w/run.log" | tail -1 | cut -c1-120)"
  rm -rf "$w"
done

Output on the machine above:

codex-cli 0.163.0 | exit 1 | -o file after the failed run: absent | last log line: ERROR: unexpected status 401 Unauthorized: Missing bearer or basic authentication in header, url: https://api.openai.com
codex-cli 0.168.0 | exit 1 | -o file after the failed run: exists, 0 bytes | last log line: ERROR: unexpected status 401 Unauthorized: Missing bearer or basic authentication in header, url: https://api.openai.com

What is the expected behavior?

A run that ends without a last message leaves FILE as it was before the run: absent if it didn't exist (as before 0.168.0), untouched if it did. One way that keeps #58974's protection: at exit, remove the file if this run created it and never wrote it, after the same identity check the final write does. If the empty file is intended, the --output-last-message help could say that an empty file means the run produced no answer.

Additional information

  • Where: codex-rs/exec/src/lib.rs ("Bind before starting the agent. Sandbox activity cannot redirect the final write.") calls codex_exec_server::BoundOutput::open(path), which creates the file (codex-rs/exec-server/src/no_follow/mod.rs). Nothing removes it on the failure path.
  • Binding the output early is a good fix. The only new behavior is the file left behind.
  • Our workaround: treat a 0-byte output file as "no answer" and delete it before a retry.

christianburrell avatar Nov 13 '26 06:11 christianburrell

I'm interested in working on this — please assign it to me.

akhil9tiet avatar Nov 13 '26 07:11 akhil9tiet

Root cause (verified against main): since #58974 (0.168.0), codex exec -o FILE binds the output file before the agent starts. In codex-rs/exec/src/lib.rs (~line 2127, comment: "Bind before starting the agent. Sandbox activity cannot redirect the final write.") codex_exec_server::BoundOutput::open(path) runs before the agent starts, and in codex-rs/exec-server/src/no_follow/mod.rs BoundOutput::open calls imp::open_output(&path), which creates the file. BoundOutput::write() — which truncates and writes — only runs when a final message arrives (the existing test regular_output_is_truncated_only_when_written confirms open itself does not truncate). So a run that fails before any final message (401, usage limit, network failure) leaves the 0-byte file behind, breaking the pre-0.168 behavior where "FILE exists" meant "the agent answered", which the reporter's probe confirms (0.163.0: absent; 0.168.0: exists, 0 bytes).

Proposed fix: track whether BoundOutput::write() was ever called, and on failure paths (or at end of run when no final message was produced) remove the bound path if nothing was written. This preserves the early-bind security property — the fd/path binding still happens up front, so sandboxed tools still cannot redirect the final write; only the empty artifact is cleaned up. Deferring the open until the first final message would reopen the redirect risk the bind was added to close, so cleanup-on-failure is the safer shape. A regression test mirroring the reporter's probe (fail fast with no final message → file absent) plus a unit test in output_tests for the never-written case would cover it.

akhil9tiet avatar Nov 13 '26 07:11 akhil9tiet

I independently reproduced this on Linux with official Codex CLI 0.167.1 and 0.168.0. Each run used a fresh CODEX_HOME and a loopback Responses endpoint returning a synthetic 401, inside a network-disabled container. Both CLIs exited 1. With an initially absent -o path, 0.167.1 left it absent; 0.168.0 left a zero-byte file. Both preserved a pre-existing 38-byte sentinel.

I also tested a local build from a pinned Codex main commit with a Unix candidate that binds the no-follow parent for an absent output and creates the leaf exclusively when writing the final message. Eight focused output tests and five synthetic CLI cases passed, including failed human/JSONL output and successful final messages. This is Linux-only evidence, not a cross-platform fix. A verify-then-unlink cleanup has a path-replacement window; the delayed-create approach avoids deleting a competing leaf. Concurrent parent renames and Windows/macOS behavior still need review.

Would maintainers want the failed-run -o contract to preserve an initially absent path across platforms while retaining early binding for existing destinations?

TheOutlandsDev avatar Nov 13 '26 12:11 TheOutlandsDev