codex icon indicating copy to clipboard operation
codex copied to clipboard

`--output-last-message` doesn't create parent dirs; write fails after the turn completes yet exits 0, silently losing the final message

Open BigCactusLabs opened this issue 4 months ago • 2 comments

What happened

codex exec --output-last-message <path> does not create the parent directory of <path>. If the directory doesn't exist, the failure surfaces only after the turn completes — the model does all its work, the JSONL stream ends with turn.completed, and then the final-message write fails with a line on stderr:

Failed to write last message file "/tmp/.../missing-dir/out.md": No such file or directory (os error 2)

Worse, the process still exits 0, so the failure is invisible to any script checking the exit code. The tokens are spent, the final message is lost, and nothing but a stderr line (easily swallowed in headless/background contexts) indicates anything went wrong.

Repro

codex exec -s read-only --skip-git-repo-check --json \
  --output-last-message /tmp/does-not-exist-$RANDOM/out.md \
  "Output exactly: REPRO" </dev/null
echo $?   # prints 0; no file was written

Observed on codex-cli 0.144.5, macOS 26 (Darwin 25.5.0, arm64).

Expected

Any of these would fix it (in descending order of preference):

  1. Create parent directories before writing, like most output-file flags.
  2. Validate the path at argument-parse time and fail fast — before the run spends tokens.
  3. At minimum, exit non-zero when the write fails so orchestration scripts can detect the loss.

Why it matters

--output-last-message is the natural primitive for headless orchestration (one output file per background worker). In that setting the flag's current behavior is the worst possible combination: the cost is incurred, the artifact is lost, and the exit code reports success. We now mkdir -p defensively before every dispatch, but the failure mode is subtle enough that it seems worth fixing at the source.

BigCactusLabs avatar Jul 17 '26 20:07 BigCactusLabs

I traced this to the final-output boundary in codex-rs/exec:

  • write_last_message_file writes directly to the requested path without creating its parent directory.
  • The helper logs std::fs::write failures but does not return them, so run_exec_session still resolves successfully.
  • Both human and JSONL output modes use this same path, so the fix can remain shared and mode-independent.

A focused fix would:

  1. create a non-empty parent path with create_dir_all before writing;
  2. return filesystem failures from the shared final-message helper and EventProcessor::print_final_output;
  3. propagate that result from run_exec_session, preserving a nonzero CLI exit for any remaining write failure.

I prepared and locally validated that approach with two regression tests: one confirms nested parents are created and the final message is written, and one uses a file as the parent path to confirm the write error is returned. Existing failed-turn behavior remains unchanged (an unsuccessful turn does not overwrite a previous output file).

Validation completed:

  • just test -p codex-exec — 130 passed, 0 failed
  • just fix -p codex-exec
  • just fmt

Per the external-contribution policy, I have not opened an unsolicited PR. I can submit the focused patch if a maintainer would like to invite it.

Disclosure: AI assistance was used for public-repository navigation, implementation, and test execution; I reviewed the resulting diff and validation output. The work was prepared solely from this public issue and the public repository and contains no private or proprietary code, data, or plans.

realmehmetali avatar Jul 18 '26 09:07 realmehmetali

(Response from codex)

I verified on 0.172.0 that codex exec -o missing/last.txt exits with code 1 before starting the turn if missing/ doesn't exist. The error includes the path Codex couldn't open.

You still need to create the parent directory first. Closing this since the CLI now reports the failure and returns a nonzero exit code.

etraut-openai avatar Nov 28 '26 23:11 etraut-openai