Dev.to AI 🤖 Ai 👁 2 📖 6 min read

stdout Is the Draft, stderr Is the Trace: A `codex exec` Capture Contract

A documentation run fails quietly when the capture step is wrong: the draft reads plausibly, the trace is gone, and nobody can say which sentences a human ever approved. The fix is a write boundary plus a stream contract

A documentation run fails quietly when the capture step is wrong: the draft reads plausibly, the trace is gone, and nobody can say which sentences a human ever approved. The fix is a write boundary plus a stream contract. The model returns text; the operator captures stdout; progress goes to stderr; human-owned files are never written by the run. The non-interactive mode documentation states that codex exec writes progress to stderr and the final agent message to stdout by default, and that --json changes stdout to a JSON Lines event stream. Those are the only stream facts this article relies on.

What the model may draft, and what a human must own

The boundary is about consequence, not difficulty. A sentence that describes documented behavior can be checked against a primary source, while a sentence that promises retention, support or a date cannot be checked by reading code. Write the split down before the first run, because otherwise the model infers it from whatever context you paste in. The model does not need write access to the repository. The operator redirects stdout to a fragment path; the prompt tells the model to return only the requested Markdown and not to write files. That keeps the fragment path an output destination, not a model-editable file.

Content Writer Reason
Description of a documented flag or behavior Model may draft Verifiable against the primary source
Command names, defaults, version numbers Model may draft, human verifies Drift is silent and easy to miss
Retention, SLA, deprecation, pricing Human only Commitments with external consequences
Security and privacy statements Human only Legal weight the author carries
Migration and rollback steps Model may draft, human executes Untested steps are a hazard, not a draft

Prerequisites and constraints

You need a repository where documentation lives beside the code it describes, and a shell that can redirect the two output streams independently. This article uses codex exec; per the linked documentation, default progress goes to stderr and the final message goes to stdout, while --json makes stdout a JSONL event stream. You also need one reviewer who reads the fragment before it lands.

Flag and argument shapes change between releases, so confirm the invocation against the documentation for your installed version before wiring it into CI. Nothing here assumes deterministic output, and a clean exit is not evidence that the prose is correct.

Step 1 — Split the page into three files

Create one human-owned page, one brief the model may read, and one fragment path the operator may capture. The brief describes the section; the fragment is the only output destination. Keep an explicit allowlist of headings the fragment may contain. A model that invents ### Notes can break anchor links and review diffs; an explicit list turns that into a checkable failure instead of a review comment. The model is instructed to return only the fragment text and not to write files.

Step 2 — Capture the two streams separately

Redirect stdout to the fragment and stderr to a trace file, and never merge them. Merging with the usual shell redirection can put progress lines inside a Markdown table. The trace file is not decoration; it is the record of what the run did when the fragment looks wrong. Keep it next to the fragment and read it before re-prompting, so you can distinguish a prompt defect from a capture defect. The redirection is an operator action; the model is not asked to write the fragment.

Step 3 — If you use --json, parse instead of redirecting

With --json, the documentation says stdout is a JSON Lines event stream, with each line a JSON object. That stream is not a Markdown fragment. Do not save the raw stream to a .md path and call it a draft. Inspect the event schema for the installed version, identify the event that carries the final assistant message only if the documentation names it, and extract that text. If the schema does not document a field for the final message, do not guess; use the default output mode or inspect a short, non-sensitive run before promoting an extraction step. A useful observation test is to use a brief prompt, confirm every stdout line parses as JSON, and confirm the extracted text is the only thing treated as Markdown. A mismatch means the parser or the mode is wrong, not that the draft is ready.

Step 4 — Check the fragment before a human reads it

Run the cheap checks first so the reviewer spends attention on claims rather than formatting noise. These are proposed checks, not a tested script. First, confirm the process exit status is zero. Second, confirm the fragment is non-empty. Third, inspect the raw fragment for ANSI escape sequences; any escape is a capture defect. Fourth, compare the human-owned page against the intended base in version control, checking both staged and unstaged changes. Fifth, extract headings from the fragment and compare them with the allowlist; any heading not in the list is a prompt defect. If --json was used, also confirm the raw JSONL was not treated as Markdown and that the extracted final message is the text under review. A mismatch in any check means discard the fragment rather than hand-edit it, because hand-fixing output hides the prompt problem that produced it.

How to verify the outcome

A run is acceptable when the conditions hold together: the process exited zero, the fragment is non-empty, the heading check found no extra headings, the human-owned page is unchanged, and any JSONL was parsed through a documented field before review. A zero exit only means the process reported success; it does not prove the prose is correct. The merge commit into the human-owned page is the approval record, and the reviewer who merges it owns those sentences.

Failure modes worth designing for

  1. Merged streams. Progress text enters the artifact and can corrupt the first table it touches.
  2. JSONL treated as Markdown. A transport stream is not prose; extract the final message through a documented field or do not use --json for drafting.
  3. Context bleed. Pasting the whole page as context invites the model to rewrite human-owned prose, so pass the brief and allowed headings instead.
  4. Partial output on failure. A non-zero exit can still leave a plausible fragment, so check the exit code before reading the file.
  5. Untrusted repository content. A non-interactive run reads whatever paths you point it at, and fetched text should be treated as data rather than instructions.
  6. Heading drift. Invented headings break anchors, and the allowlist check is the only automated guard against it.

When this contract costs more than it saves

For a single-paragraph edit, the three-file ceremony costs more than it returns, and a plain editor is the better tool. For fully generated reference pages, the human owns the generator and its inputs rather than the sentences, so the fragment boundary adds a merge step without reducing review load. Regulated text where every sentence needs a named approver falls into the same category.

One practice to skip: diffing fragments across runs as a quality signal. Output is non-deterministic, a large diff does not mean a worse draft, and treating it as a metric trains reviewers to reward stability over accuracy.

AI assistance disclosure

This draft was produced with AI assistance. No executable example in this revision was run; the checks above are proposed verification criteria. The CLI behavior described is limited to the linked documentation and must be confirmed by a human against the installed version before publication.

This article was created with AI assistance.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.