fix(cli): escape control characters at the CLI's stdout and stderr writes

Merged
TH
8d ago
+1748-334

Summary

Text the CLI prints can name values that a device or a remote daemon chose. Every CLI write now escapes C0 (except newline and tab), DEL and C1 as \u00NN. That form is also a JSON escape, so any JSON the CLI prints, with or without --json, still parses back to the exact values.

  • Design: values stay raw (data, --json, files, JUnit) until a display line is built.
    • The cli zone writes through src/cli/process-exit.ts, and lower zones apply escapeTerminalText from kernel/redaction themselves.
    • Only intended color and cursor codes are written raw: colored diffs, proxy startup, the doctor summary, and the replay reporter's own codes.
  • Paths covered:
    • every formatter, warning line and error, including details;
    • the replay test reporter (escaped values, checked counters, single-row live lines, command progress on its own row, written only by the built-in reporter);
    • upload progress through the CLI's progress sink;
    • the proxy takeover notice through a structured onTakeover callback;
    • --debug diagnostics;
    • agent-cdp child output (a verbatim opt-in can follow if someone needs it);
    • streamed doctor lines, via an additive optional marker on CommandProgressEvent with a ledger ack; the CLI colors the marker after escaping.
  • Warning lines: every warning folds to one Warning: line. Formatters never print response warnings: the dispatcher prints each one exactly once, on stderr for a parseable-output command such as record. Effects you can see:
    • record warnings go to stderr, not into the stdout path.
    • snapshot warnings come after the tree as Warning: lines, not as plain lines above it.
    • open warnings come after the --foreground snapshot tree, not right after Opened:. The nested snapshot warnings print once, after the tree and before the open warnings.
  • Docs: the command reference documents the \u00NN form, the layout rule, and the MCP and AI SDK text.

Split out of #4744, which stays a remote-only change. 63 files, +1553/โˆ’294 (production about +440/โˆ’240 across 37 files).

Validation

At b2b83cb50c:

  • End-to-end tests cover each surface: open, snapshot, snapshot diff, screenshot, close, get attrs, --json, errors, the replay reporter (TTY live row, malformed counters, JUnit exactness, trace lookup), upload progress, the takeover notice, doctor, debug, record, replay export, agent-cdp and --debug diagnostics.
  • Mutations: each fix has a test that fails when it is reverted.
  • Suites: src, command-output, kernel, remote, daemon-client, daemon sessions, host-kit and wire-compat (19,154 tests) and the eager-closure check pass.
  • Gates: typecheck, lint, check:layering, fallow audit --base origin/main and check:production-exports are clean.

CI validates the head.

๐Ÿค– Generated with Claude Code


Autofix with [code]smith
Open in [code]smith ยท Need help on this PR? Tag @codesmith-bot with what you need.