From 64bd7234b349949a27cdab08fd991144cb0519ed Mon Sep 17 00:00:00 2001 From: bahtya Date: Tue, 7 Apr 2026 11:26:18 +0800 Subject: [PATCH] fix(cli): sanitize surrogate characters in prompt history to prevent UnicodeEncodeError On Windows, certain Unicode input (emoji, mixed-script text, surrogate pairs) causes prompt_toolkit's FileHistory to crash with UnicodeEncodeError when writing the history file. Fix: wrap FileHistory with a _SafeFileHistory subclass that sanitizes surrogate characters before writing, replacing invalid sequences instead of crashing. Fixes #2846 --- nanobot/cli/commands.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index 29eb19c31..2e18045f4 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -118,8 +118,17 @@ def _init_prompt_session() -> None: history_file = get_cli_history_path() history_file.parent.mkdir(parents=True, exist_ok=True) + # Wrap FileHistory to sanitize surrogate characters on write. + # Without this, special Unicode input (emoji, mixed-script) crashes + # prompt_toolkit's history file write on Windows with UnicodeEncodeError. + # See issue #2846. + class _SafeFileHistory(FileHistory): + def store_string(self, string: str) -> None: + safe = string.encode("utf-8", errors="surrogateescape").decode("utf-8", errors="replace") + super().store_string(safe) + _PROMPT_SESSION = PromptSession( - history=FileHistory(str(history_file)), + history=_SafeFileHistory(str(history_file)), enable_open_in_editor=False, multiline=False, # Enter submits (single line mode) )