DEV Community

codex exec Writes Progress to stderr. Keep It Out of Your Patch Artifact.

Conclusion first. In its default output mode, codex exec writes progress to stderr and the final agent message to stdout (docs). With --json, stdout becomes a JSON Lines event stream: one JSON object per line, so stdout is not a patch artifact.

A refactor gate must preserve the split and use a mode-specific validation path. Merge the streams and the artifact can contain narration or event lines rather than a clean proposal.

This post covers one narrow problem: how to capture an agent's proposed change as a clean artifact, then let pinned characterization tests decide. It assumes you already pin behavior before touching code. The checks below are proposed observations; no command was executed. Verify output behavior, exit semantics, and event schema against the official docs and your installed version.

The edge case: one command, two kinds of text

Most shell habits push both streams into one destination. Tooling built on that habit sees a mixture of commentary and code. The failure is quiet. A diff tool may still find header lines inside the noise. A JSON Lines parser may stop or fail at the first non-JSON line. Either way you gate on a fragment.

With --json, stdout is the event stream. The supplied documentation establishes only that each stdout line is a JSON object; it does not define event types or a patch field. Do not assume that raw stdout is a diff. Default-mode final message and JSONL event stream are different contracts. Treat this as a design constraint, not a parsing inconvenience.

Prerequisites

  • A way to invoke codex exec that you have already checked by hand, at least once.
  • Characterization tests that pass on the current commit, before any AI turn.
  • A temp directory for run artifacts, excluded from version control.
  • A shell with separate stdout and stderr redirection.

The procedures below are observations, not tested command lines. Verify the exit-code and streaming semantics against the current docs for your version. Do not infer them from this article.

Step 1 - Pin behavior before the first prompt

Run the baseline first, by hand. No model, no patch. Record the baseline commit and the baseline test result.

Observe two things: the tests pass, and the commit identifier is stored with the test log. If the tests already fail, stop here. An agent patch against a red baseline proves nothing. A pinned test with no commit is not reproducible.

Step 2 - Capture the streams separately, then choose one mode

Use two destinations: one for stdout and one for stderr. Do not merge them. In default mode, the stdout destination is the candidate final message. The stderr destination is progress and diagnostics. In --json mode, the stdout destination is a JSON Lines event stream. The supplied note says --json changes stdout; it does not separately restate stderr behavior for that mode, so verify stderr on your version.

Observation-based check after one run:

  • Compare the stdout and stderr file sizes. An empty stdout with a large stderr is a capture problem, not a patch result.
  • In default mode, inspect the first non-empty stdout line. It should read like a final message, not progress. If it reads like progress, the streams were merged or the wrong destination was used.
  • In --json mode, try parsing each non-empty stdout line as JSON. If one line fails, suspect merged stderr, a truncated write, or a different output mode. If every line parses but you feed the file to a diff parser, you have mode confusion: JSON Lines is not a patch.
  • A successful JSON parse proves only that the line is a JSON object. It does not prove a patch exists or that the patch is valid.

Choose one mode per gate. Do not write default and JSON runs into the same artifact. If you use --json, keep the raw event stream immutable and extract the candidate final message or patch according to the documented event schema for your version. If the schema is not documented, use default mode or stop; do not guess a field name.

Step 3 - Treat the candidate as a contract, not as a patch

In default mode, stdout is the final message. It is prose. It may or may not be a diff. In --json mode, the candidate is whatever you extract from the event stream, not the raw stream.

State a format contract in the prompt, for example: 'Return only a unified diff touching at most one file.' Then validate the candidate before any test runs.

Proposed observations:

  • The candidate artifact is non-empty.
  • It has recognizable unified-diff file headers and hunk markers. The exact marker set depends on the diff format.
  • It does not begin with progress narration.
  • A dry-run apply check from your version-control tool reports no patch-format or context errors.

For --json, validate the raw stream first: each non-empty stdout line should parse as JSON. Then validate the extracted candidate with the same diff checks. If the raw stream is contaminated, reject the run before extraction. If the extracted candidate is prose or empty, reject it before tests.

Rejection is the cheap outcome. A rejected artifact costs one run. A silently truncated artifact costs a false green.

Step 4 - Apply the smallest safe change

Before applying, inspect the scope with a dry-run apply check and a diffstat from your version-control tool. Observe: one file, a small hunk count, no dependency files, no unrelated paths. If the proposal exceeds your budget, reject it instead of trimming it by hand.

Then apply the candidate in the working tree, run the pinned tests, and keep the artifact until review closes. Observe the baseline result and the after-apply result. The tests decide. Not the model's summary. Not output equality with a previous run. If the tests fail, reject or revise the proposal under normal review. Do not treat the model's claim of success as evidence.

Step 5 - Check the gate itself

There are two separate checks. Do not collapse them.

Empty-change guard. After applying, observe whether the working tree has a non-empty change set relative to the baseline. If the change set is empty, reject it. Characterization tests pass happily when nothing changed, so a green run on no change proves nothing.

Test sensitivity. On a throwaway copy of the baseline, make a deliberate behavior-changing edit that your pinned tests should catch. Run the tests and observe failure. If they pass, the tests do not constrain that behavior; fix the tests or narrow the claim.

Do not revert a correct behavior-preserving refactor and expect the characterization tests to fail. A correct refactor reverts to the same passing baseline. After reverting a correct refactor, the tests should return to passing. If they do not, suspect the environment, the patch application, or the baseline record - not a successful refactor.

Failure modes

Symptom Likely cause Patch file starts with commentary Streams merged into one destination JSONL parser dies on line 1 stderr text folded into stdout, or raw JSONL treated as a patch Empty artifact, exit looked fine Final message captured elsewhere; check stdout and stderr sizes Tests pass with no diff applied Missing non-empty change precondition Diff applies but touches five files No scope budget before apply Correct refactor fails after revert Adversarial check aimed at the wrong target; expect a passing baseline after revert and test sensitivity separately

Record both stdout and stderr sizes after every run. That single habit catches most of the list.

When this does not apply

This workflow fits small, testable refactors with a stable test command. It does not fit migrations where the baseline is already unstable. It also adds little when a human applies the change directly in an editor. There the artifact is the working tree, not stdout.

If your harness needs streaming events, parse the documented event stream. Do not reconstruct it from a merged log.

Closing check before you publish or merge

Confirm these gates: streams stayed separate; the mode-specific artifact was validated; the change budget held; an empty change set was rejected; test sensitivity was observed on an intentional break; and a reverted correct change returned to a passing baseline. Anything unverified stays unverified.

AI disclosure: this article was produced with AI assistance. No command or test above was executed by the author. A human must verify the current codex exec output behavior, --json event schema, exit semantics, and the destination platform's policy before publishing or relying on this workflow.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.