# ADR 0002: cdx exec captures codex output to a run log
Audience: AI coding agents first.

## Status
Accepted.

## Context
`cdx` is the account-routing wrapper for `codex`. For interactive subcommands it `os.execvpe`s into `codex`, inheriting stdio — correct for a TUI.

Non-interactive `cdx exec` calls (from the `ask-codex` skill and agents) also inherited stdio, so codex's full response streamed back to the caller as tool output. A long response is truncated by the calling harness. The caller then re-ran the same expensive codex call just to recapture the output — wasteful and non-deterministic (a second inference).

## Decision
- `cdx exec` (and only `exec`) captures codex's combined stdout+stderr to a **run log** and prints **only the run log path** on stdout.
- Run log location: `~/.systray-ai/logs/cdx/<UTC-ish-stamp>-<pid>.log`. NEVER `/tmp`.
- `exec` detection: first non-flag token in the forwarded argv is `exec` (`is_exec_invocation`).
- The captured child gets `stdin=/dev/null`; callers MUST NOT append `</dev/null` and MUST NOT pipe/tee/redirect the call themselves.
- `cdx` exits with codex's own return code.
- All other (interactive) subcommands keep the `os.execvpe` stdio-inheriting path unchanged.
- Generalization seam: `ToolAdapter.capture_log_path()` returns `None` by default (no capture); `CodexAdapter` overrides it. `cld`/claude is unaffected until an adapter opts in.

## Consequences
- One `cdx exec` call produces one run log; the caller `Read`s that file for the full response. No re-run to recapture, no truncation.
- Run logs accumulate under `~/.systray-ai/logs/cdx/`; no rotation yet (future ADR if it grows).
- Output contract changed: `exec` stdout is a path, not codex's response. `ask-codex` SKILL.md documents this.

## Anti-patterns
- Do not capture interactive subcommands — it would break the TUI.
- Do not write run logs to `/tmp` or any world-writable dir.
- Do not special-case `exec --help` out of capture — every `exec` invocation logs.

## Checks
- A `cdx exec` run prints exactly one line, and it is a path under `~/.systray-ai/logs/cdx/`.
- The exit code equals codex's exit code.
- Interactive `cdx <tui>` still attaches to the terminal.
