Skip to content

fix(cli): no ANSI colour off-TTY or with NO_COLOR; agent output warnings to stderr - #2623

Merged
sunholo-voight-kampff merged 1 commit into
devfrom
fix/cli-color-tty-agent-stdout
Dec 2, 2026
Merged

sunholo-voight-kampff merged 1 commit into
devfrom
fix/cli-color-tty-agent-stdout

Conversation

@sunholo-voight-kampff

@sunholo-voight-kampff sunholo-voight-kampff commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Testers of v1.0.0-rc.3 reported:

  1. ailang verify contract.ail | head (any piped/captured run) printed raw escapes like [2m208.583333ms[0m; the store: header of ailang messages list did the same.
  2. ailang inspect --format=agent showed "Warning: stdlib version mismatch..." next to its compact summary.

Root cause of (1): fatih/color (used for green/red/bold/...) already turns colour off when stdout is not a TTY or NO_COLOR is set. Four paths skipped it and wrote escapes no matter what:

  • dim() in cmd/ailang/main.go, a hand-written "\033[2m"+s+"\033[0m" used at 88 call sites: verify timings, the messages store: header, coordinator/chains listings, docs.
  • a copy of the same dim() in internal/runner/debug_types.go.
  • ailang test: the reporter coloured unless --no-color was passed.
  • ailang eval-compare: called FormatComparison(report, true), so colour was always on.

Finding on (2): the warning already goes to stderr (internal/loader/stdlib_resolver.go), and that line has not changed since rc.3. I checked this against HEAD with a mismatched AILANG_STDLIB_PATH: stdout is clean. The tester most likely merged the two streams (2>&1). This PR adds a regression test rather than changing that code.

Changes: one decision point

  • New internal/display/color.go makes the single decision: colour only when the writer is a terminal, NO_COLOR is empty or unset (no-color.org), and TERM is not dumb. Its init sets color.NoColor from that decision, so every fatih SprintFunc in cmd/ailang, runner and repl follows it. It exports ColorEnabled(), ColorEnabledFor(w) and Dim(s).
  • cmd/ailang/main.go: dim now calls display.Dim.
  • internal/runner: removed the duplicate dim; colors.go uses dim = display.Dim.
  • cmd/ailang/commands_language.go: ailang test colours only when --no-color is absent and display.ColorEnabledFor(os.Stdout) is true (new testColor).
  • cmd/ailang/eval_tools.go: eval-compare passes display.ColorEnabledFor(os.Stdout).
  • internal/config/compiler.go: registered NO_COLOR and TERM with typed getters, because forbidigo routes all env reads through config. docs/docs/reference/env-vars.md regenerated.
  • internal/observatory/migrate_v8.go: two trace_summaries back-fill warnings moved from stdout to stderr, where they could otherwise land inside a --json listing.

Deliberately left alone:

  • Screen-clear sequences in interactive watch/live modes: messages_util.go, chains_util.go, chains_live.go, coordinator_lifecycle.go, repl_commands.go.
  • internal/effects/terminal.go: the AILANG program's own Terminal effect.
  • internal/repl: already uses fatih Faint.
  • The pipeline's WARNING: No interface line: debug-only, behind TraceDefaulting.
  • Observatory OTLP receiver stdout logs: server logs.
  • pkg publish and coordinator start warnings: human-only output.

Tests

  • internal/display/color_test.go:
    • TestColorPolicy: off when not a terminal, off with NO_COLOR, off with TERM=dumb, on for a terminal with no opt-out.
    • TestColorEnabledFor_NonTerminalWriters: buffer, pipe and nil file.
    • TestNoColorEnv: any non-empty value counts.
    • TestDim_FollowsTheSwitch.
    • TestProcessDecision_PipedStdout: re-runs the test binary with stdout piped, with and without NO_COLOR.
  • cmd/ailang/inspect_stdout_hygiene_test.go TestInspectAgent_StdoutHasNoWarningOrEscape: builds the binary and points it at a stdlib copy whose VERSION is mismatched. It asserts that stdout from --format=agent and --json has no warning and no escape, and, as a control, that stderr does carry the warning.
  • Manual: ailang verify piped gives 0 escapes; under a pty (script) colour appears; under a pty with NO_COLOR=1 it gives 0 escapes. messages list piped gives 0 escapes.

Mutation results

  • Reverting Dim to raw escapes: TestDim_FollowsTheSwitch and TestProcessDecision_PipedStdout FAIL.
  • Dropping NO_COLOR from the policy: TestColorPolicy and TestNoColorEnv FAIL.
  • Sending the loader warning to os.Stdout: TestInspectAgent_StdoutHasNoWarningOrEscape FAILs on both formats.

Each mutation was made in place and restored from a scratchpad copy.

Gates: make test-changed, make lint (0 issues), make check-file-sizes, make check-changelog, make check-boundaries all pass. No flags or help changed, so check-cli-docs was not needed.

🤖 Generated with Claude Code

Fixes #2581

…ngs to stderr

Colour now has one decision point, internal/display: on only when the
writer is a terminal, NO_COLOR is unset/empty and TERM is not dumb. It
sets fatih/color's process-wide switch, and the sites that composed raw
escapes (the two dim() helpers, the `ailang test` reporter, eval-compare)
ask it too. A piped `ailang verify x.ail | head` or `messages list` no
longer prints [2m...[0m.

NO_COLOR and TERM are registered in internal/config (env-vars.md
regenerated). Two observatory migration warnings moved from stdout to
stderr. A test pins inspect --format=agent / --json stdout free of the
stdlib version-mismatch warning (it was already on stderr).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sunholo-voight-kampff
sunholo-voight-kampff merged commit 6c7b565 into dev Dec 2, 2026
21 of 23 checks passed
@sunholo-voight-kampff
sunholo-voight-kampff deleted the fix/cli-color-tty-agent-stdout branch December 2, 2026 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ailang verify / ailang test print ANSI colour codes when stdout is not a TTY, and ignore NO_COLOR

1 participant