Skip to content

Commit 8fd1842

Browse files
committed
fix: escape Rich markup in error and user content to prevent crashes
When user input or error messages contain text resembling Rich markup tags (e.g., [/login]), rich.print() tries to interpret them as formatting tags, causing MarkupError crashes. Escape all dynamic content passed to rich.print() in print mode using rich.markup.escape() to prevent Rich from interpreting brackets as markup. Fixes #1291
1 parent 7ba9695 commit 8fd1842

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

src/kimi_cli/ui/print/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from kosong.chat_provider import ChatProviderError
1010
from kosong.message import Message
1111
from rich import print
12+
from rich.markup import escape
1213

1314
from kimi_cli.cli import InputFormat, OutputFormat
1415
from kimi_cli.soul import (
@@ -80,7 +81,7 @@ def _handler():
8081
if command:
8182
logger.info("Running agent with command: {command}", command=command)
8283
if self.output_format == "text" and not self.final_only:
83-
print(command)
84+
print(escape(command))
8485
await run_soul(
8586
self.soul,
8687
command,
@@ -94,22 +95,22 @@ def _handler():
9495
command = None
9596
except LLMNotSet as e:
9697
logger.exception("LLM not set:")
97-
print(str(e))
98+
print(escape(str(e)))
9899
except LLMNotSupported as e:
99100
logger.exception("LLM not supported:")
100-
print(str(e))
101+
print(escape(str(e)))
101102
except ChatProviderError as e:
102103
logger.exception("LLM provider error:")
103-
print(str(e))
104+
print(escape(str(e)))
104105
except MaxStepsReached as e:
105106
logger.warning("Max steps reached: {n_steps}", n_steps=e.n_steps)
106-
print(str(e))
107+
print(escape(str(e)))
107108
except RunCancelled:
108109
logger.error("Interrupted by user")
109110
print("Interrupted by user")
110111
except BaseException as e:
111112
logger.exception("Unknown error:")
112-
print(f"Unknown error: {e}")
113+
print(f"Unknown error: {escape(str(e))}")
113114
raise
114115
finally:
115116
remove_sigint()

0 commit comments

Comments
 (0)