From 89cf65ab63988656124770e43cf8defd1ec8799b Mon Sep 17 00:00:00 2001 From: Brooklyn Nicholson Date: Wed, 1 Jul 2026 16:25:48 -0500 Subject: [PATCH] fix(tui_gateway): strip ANSI from slash-worker output for desktop chat Desktop chat bubbles render plain text, but a worker-routed command that builds its own Rich Console (e.g. /journey) picks up truecolor from the gateway's inherited COLORTERM and leaks raw escapes into the bubble. Strip ANSI at the single worker-return choke point so every command renders cleanly. The TUI opens /journey as an overlay, so it never travels this path. --- tests/tui_gateway/test_slash_worker_ansi.py | 23 +++++++++++++++++++++ tui_gateway/slash_worker.py | 9 +++++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/tui_gateway/test_slash_worker_ansi.py diff --git a/tests/tui_gateway/test_slash_worker_ansi.py b/tests/tui_gateway/test_slash_worker_ansi.py new file mode 100644 index 000000000..3c061c4d7 --- /dev/null +++ b/tests/tui_gateway/test_slash_worker_ansi.py @@ -0,0 +1,23 @@ +"""The slash worker feeds desktop chat bubbles, which render plain text — so +any ANSI a worker-routed command emits (e.g. /journey's own Rich Console) must +be stripped from the worker's return value.""" + +from __future__ import annotations + + +class _FakeCLI: + console = None + + def process_command(self, cmd: str) -> None: + import sys + + sys.stdout.write("\x1b[38;2;1;2;3mcolored\x1b[0m plain") + + +def test_run_strips_ansi_from_output(): + from tui_gateway import slash_worker + + out = slash_worker._run(_FakeCLI(), "/anything") + + assert "\x1b[" not in out + assert out == "colored plain" diff --git a/tui_gateway/slash_worker.py b/tui_gateway/slash_worker.py index d8a6ba047..00e83bedf 100644 --- a/tui_gateway/slash_worker.py +++ b/tui_gateway/slash_worker.py @@ -102,7 +102,14 @@ def _run(cli: HermesCLI, command: str) -> str: if old is not None: cli_mod._cprint = old - return buf.getvalue().rstrip() + # Desktop chat bubbles render plain text, not ANSI. A worker-routed command + # that emits Rich color (e.g. /journey building its own Console, which picks + # up truecolor from the gateway's inherited COLORTERM) would otherwise leak + # raw escapes; strip them at the single choke point. (The TUI opens /journey + # as an overlay, so it never travels this path.) + from tools.ansi_strip import strip_ansi + + return strip_ansi(buf.getvalue().rstrip()) def main():