Skip to content

fix: resolve garbled ANSI escape codes in status printouts#2265

Closed
amethystani wants to merge 1 commit into
NousResearch:mainfrom
amethystani:fix/issue-2262-garbled-ansi-codes
Closed

fix: resolve garbled ANSI escape codes in status printouts#2265
amethystani wants to merge 1 commit into
NousResearch:mainfrom
amethystani:fix/issue-2262-garbled-ansi-codes

Conversation

@amethystani

Copy link
Copy Markdown
Contributor

Summary

Fixes #2262 — garbled terminal output like ?[33mTool progress: NEW?[0m on kitty, alacritty, ghostty and gnome-console.

Root Cause

Two separate callsites bypass _cprint(), the existing helper that routes ANSI-coloured text through prompt_toolkit's print_formatted_text(ANSI(...)) renderer. Inside the interactive chat loop, patch_stdout's StdoutProxy intercepts raw print() / Console.print() calls and mangles ANSI escape sequences, replacing the ESC byte with ?.

Callsite 1 — /verbose feedback labels (cli.py):

# Before — Rich markup written via plain Console() inside patch_stdout:
labels = {"new": "[yellow]Tool progress: NEW[/] — ..."}
self.console.print(labels.get(...))   # Console() → stdout → StdoutProxy mangling

Callsite 2 — context pressure status (run_agent.py):

@staticmethod
def _safe_print(*args, **kwargs):
    print(*args, **kwargs)   # always goes to stdout → StdoutProxy mangling

Fix

  • run_agent.py: Convert _safe_print from a @staticmethod to an instance method that delegates to self._print_fn (defaults to print, preserving all non-CLI behaviour).
  • cli.py: After creating the AIAgent instance, set self.agent._print_fn = _cprint so all agent status output flows through the prompt_toolkit renderer.
  • cli.py: Replace Rich markup in /verbose labels with hermes_cli.colors.Colors ANSI constants in f-strings, and emit via _cprint() directly.

Testing

hermes chat -q "Hello"
# Then type: /verbose
# Should show clean colour output, not ?[33mTool progress: NEW?[0m
# Context pressure bar ('approaching compaction') should also render correctly

…rch#2262)

Two related root causes for the '?[33mTool progress: NEW?[0m' garbling
reported on kitty, alacritty, ghostty and gnome-console:

1. /verbose label printing used self.console.print() with Rich markup
   ([yellow]...[/]).  self.console is a plain Rich Console() whose output
   goes directly to sys.stdout, which patch_stdout's StdoutProxy
   intercepts and mangles raw ANSI sequences.

2. Context pressure status lines (e.g. 'approaching compaction') from
   AIAgent._safe_print() had the same problem -- _safe_print() was a
   @staticmethod that always called builtin print(), bypassing the
   prompt_toolkit renderer entirely.

Fix:
- Convert AIAgent._safe_print() from @staticmethod to an instance method
  that delegates to self._print_fn (defaults to builtin print, preserving
  all non-CLI behaviour).
- After the CLI creates its AIAgent instance, wire self.agent._print_fn to
  the existing _cprint() helper which routes through
  prompt_toolkit.print_formatted_text(ANSI(text)).
- Rewrite the /verbose feedback labels to use hermes_cli.colors.Colors
  ANSI constants in f-strings and emit them via _cprint() directly,
  removing the Rich-markup-inside-patch_stdout anti-pattern.

Fixes NousResearch#2262
@teknium1

Copy link
Copy Markdown
Contributor

Closing for now — will revisit later.

@teknium1

Copy link
Copy Markdown
Contributor

Cherry-picked and merged via PR #2448 with your authorship preserved. Added a follow-up fix: changed _print_fn default from a captured print reference to late-bound None so test patching still works. Live tested interactively — all /verbose modes and tool calls render clean. Thanks for the thorough root cause analysis!

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.

[Bug]: Garbled ANSI codes from status printouts like \verbose and approaching compaction

2 participants