Repository navigation
fix(cli): no ANSI colour off-TTY or with NO_COLOR; agent output warnings to stderr - #2623
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Testers of v1.0.0-rc.3 reported:
ailang verify contract.ail | head(any piped/captured run) printed raw escapes like[2m208.583333ms[0m; thestore:header ofailang messages listdid the same.ailang inspect --format=agentshowed "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 orNO_COLORis set. Four paths skipped it and wrote escapes no matter what:dim()incmd/ailang/main.go, a hand-written"\033[2m"+s+"\033[0m"used at 88 call sites: verify timings, the messagesstore:header, coordinator/chains listings, docs.dim()ininternal/runner/debug_types.go.ailang test: the reporter coloured unless--no-colorwas passed.ailang eval-compare: calledFormatComparison(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 mismatchedAILANG_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
internal/display/color.gomakes the single decision: colour only when the writer is a terminal,NO_COLORis empty or unset (no-color.org), andTERMis notdumb. Itsinitsetscolor.NoColorfrom that decision, so every fatih SprintFunc in cmd/ailang, runner and repl follows it. It exportsColorEnabled(),ColorEnabledFor(w)andDim(s).cmd/ailang/main.go:dimnow callsdisplay.Dim.internal/runner: removed the duplicatedim;colors.gousesdim = display.Dim.cmd/ailang/commands_language.go:ailang testcolours only when--no-coloris absent anddisplay.ColorEnabledFor(os.Stdout)is true (newtestColor).cmd/ailang/eval_tools.go: eval-compare passesdisplay.ColorEnabledFor(os.Stdout).internal/config/compiler.go: registeredNO_COLORandTERMwith typed getters, because forbidigo routes all env reads through config.docs/docs/reference/env-vars.mdregenerated.internal/observatory/migrate_v8.go: two trace_summaries back-fill warnings moved from stdout to stderr, where they could otherwise land inside a--jsonlisting.Deliberately left alone:
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 fatihFaint.WARNING: No interfaceline: debug-only, behind TraceDefaulting.pkg publishand 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.goTestInspectAgent_StdoutHasNoWarningOrEscape: builds the binary and points it at a stdlib copy whose VERSION is mismatched. It asserts that stdout from--format=agentand--jsonhas no warning and no escape, and, as a control, that stderr does carry the warning.ailang verifypiped gives 0 escapes; under a pty (script) colour appears; under a pty withNO_COLOR=1it gives 0 escapes.messages listpiped gives 0 escapes.Mutation results
Dimto raw escapes:TestDim_FollowsTheSwitchandTestProcessDecision_PipedStdoutFAIL.TestColorPolicyandTestNoColorEnvFAIL.os.Stdout:TestInspectAgent_StdoutHasNoWarningOrEscapeFAILs 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-boundariesall pass. No flags or help changed, socheck-cli-docswas not needed.🤖 Generated with Claude Code
Fixes #2581