Skip to content

fix: centralize ANSI codes and disable colors in non-TTY output#1399

Merged
Hweinstock merged 1 commit into
aws:mainfrom
Hweinstock:fix/centralize-ansi
May 27, 2026
Merged

fix: centralize ANSI codes and disable colors in non-TTY output#1399
Hweinstock merged 1 commit into
aws:mainfrom
Hweinstock:fix/centralize-ansi

Conversation

@Hweinstock
Copy link
Copy Markdown
Contributor

@Hweinstock Hweinstock commented May 27, 2026

Description

Inline ANSI escape codes were scattered across the codebase and always emitted regardless of whether output was going to a terminal or a file. This caused garbled text when CLI output was redirected (e.g. agentcore import --source agent.yaml 2> log.txt).

This PR:

  1. Centralizes all ANSI color codes into src/cli/constants.ts behind a shared ANSI object
  2. Adds TTY detection and NO_COLOR support — colors auto-disable when output is piped or redirected
  3. Removes the duplicate ANSI definition from src/cli/commands/import/constants.ts
  4. Updates all call sites to import from the centralized constant

Terminal control sequences (alt screen, show cursor, clear line) remain unconditional since they are only used in TUI contexts that require a TTY.

Related Issue

Closes # #1391

Documentation PR

N/A

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Other (please describe):

Testing

How have you tested the change?

  • I ran npm run test:unit and npm run test:integ
  • I ran npm run typecheck
  • I ran npm run lint
  • If I modified src/assets/, I ran npm run test:update-snapshots and committed the updated snapshots

Manual verification:

# Built and installed the bundled tarball
npm run bundle && npm install -g ./aws-agentcore-0.15.0-preview-*.tgz

# Confirmed no escape codes in redirected output
agentcore import --source /tmp/nonexistent.yaml 2> /tmp/err.txt
cat -v /tmp/err.txt
# Output: [error] Import failed: Source file not found: /tmp/nonexistent.yaml

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the
terms of your choice.

@github-actions github-actions Bot added the size/m PR size: M label May 27, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@github-actions github-actions Bot added the agentcore-harness-reviewing AgentCore Harness review in progress label May 27, 2026
@agentcore-devx-automation
Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Package Tarball

aws-agentcore-0.15.0.tgz

How to install

gh release download pr-1399-tarball --repo aws/agentcore-cli --pattern "*.tgz" --dir /tmp/pr-tarball
npm install -g /tmp/pr-tarball/aws-agentcore-0.15.0.tgz

@Hweinstock Hweinstock changed the title Fix ANSI escape codes leaking into redirected output fix: centralize ANSI codes and disable colors in non-TTY output May 27, 2026
Copy link
Copy Markdown

@agentcore-cli-automation agentcore-cli-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for centralizing ANSI handling and adding NO_COLOR support. I have one blocking concern about the TTY detection that I think defeats the very bug this PR aims to fix.

See inline comments. Happy to approve once the per-stream gating is addressed (or after we discuss).

Comment thread src/cli/constants.ts Outdated
*
* If NO_COLOR is set, no colors. Otherwise, enable if we have a TTY.
*/
const isTTY = process.stdout.isTTY || process.stderr.isTTY;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OR-based TTY check defeats the redirect case this PR is trying to fix.

Consider the canonical scenario from the PR description:

agentcore import --source agent.yaml 2> log.txt

Here process.stdout.isTTY === true (terminal) and process.stderr.isTTY === undefined (file). The check evaluates to true || undefined === true, so useColor = true. All the new color writes that go to stderr (console.error in import/command.ts, printTelemetryNotice and printUpdateNotification writing to process.stderr) will still emit ANSI codes into log.txt. The exact case the PR description shows working should still be garbled.

A few options to fix:

  1. Per-stream gating — expose two color sets (or a helper) keyed off the destination stream. e.g.

    const stdoutColor = !process.env.NO_COLOR && !!process.stdout.isTTY;
    const stderrColor = !process.env.NO_COLOR && !!process.stderr.isTTY;
    export const ANSI = { stdout: makeCodes(stdoutColor), stderr: makeCodes(stderrColor), control: { ... } };

    Call sites that write to stderr (console.error, process.stderr.write) use ANSI.stderr.red, etc.

  2. AND instead of ORprocess.stdout.isTTY && process.stderr.isTTY. Pessimistic but matches the spirit of the PR (any redirection disables colors everywhere). Simpler than option 1.

  3. Adopt an existing library — e.g. the chalk / supports-color ecosystem already does this correctly per stream. May be overkill if you want to keep dependencies minimal.

Could you also add a test (or update the manual repro) that specifically exercises 2> with a TTY stdout? That would catch this regression.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed and verified e2e by using the and approach.

Comment thread src/cli/constants.ts Outdated
@github-actions github-actions Bot removed the agentcore-harness-reviewing AgentCore Harness review in progress label May 27, 2026
@Hweinstock Hweinstock force-pushed the fix/centralize-ansi branch from c3df7df to f1fa129 Compare May 27, 2026 20:52
@github-actions github-actions Bot added size/m PR size: M and removed size/m PR size: M labels May 27, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@agentcore-devx-automation
Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@Hweinstock Hweinstock force-pushed the fix/centralize-ansi branch from f1fa129 to b9bfa19 Compare May 27, 2026 21:25
@github-actions github-actions Bot added size/m PR size: M and removed size/m PR size: M labels May 27, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@agentcore-devx-automation
Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@Hweinstock Hweinstock force-pushed the fix/centralize-ansi branch from b9bfa19 to 2e47cae Compare May 27, 2026 21:29
@github-actions github-actions Bot added size/m PR size: M and removed size/m PR size: M labels May 27, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@agentcore-devx-automation
Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@agentcore-devx-automation
Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@Hweinstock Hweinstock force-pushed the fix/centralize-ansi branch from 9b0342c to c18ec1d Compare May 27, 2026 21:47
@github-actions github-actions Bot added size/m PR size: M and removed size/m PR size: M labels May 27, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@agentcore-devx-automation
Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@Hweinstock Hweinstock force-pushed the fix/centralize-ansi branch from c18ec1d to 8ce22d2 Compare May 27, 2026 21:49
@github-actions github-actions Bot added size/m PR size: M and removed size/m PR size: M labels May 27, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@agentcore-devx-automation
Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@Hweinstock Hweinstock force-pushed the fix/centralize-ansi branch from 8ce22d2 to 412626d Compare May 27, 2026 21:53
@github-actions github-actions Bot added size/m PR size: M and removed size/m PR size: M labels May 27, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@agentcore-devx-automation
Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label May 27, 2026
@Hweinstock Hweinstock marked this pull request as ready for review May 27, 2026 22:08
@Hweinstock Hweinstock requested a review from a team May 27, 2026 22:08
@Hweinstock Hweinstock merged commit f32987d into aws:main May 27, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/m PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants