fix(output): force UTF-8 for machine output on non-UTF-8 consoles (#546) - #547
Draft
padak wants to merge 1 commit into
Draft
fix(output): force UTF-8 for machine output on non-UTF-8 consoles (#546)#547padak wants to merge 1 commit into
padak wants to merge 1 commit into
Conversation
`kbagent --json <anything>` crashed on Windows with UnicodeEncodeError as soon as the payload carried a non-ASCII character -- an arrow in a flow name, an accented config name, an emoji. sys.stdout inherits the console codepage (cp1250 on Czech/Polish/Hungarian Windows 11), and pydantic's model_dump_json() emits raw UTF-8 rather than \uXXXX escapes, so the write hit the cp1250 codec and aborted the command instead of printing JSON. --json exists for machine consumption -- piping to a file or another program -- so it must not depend on the attached terminal. Add write_machine_output() in output.py and route every machine-output writer through it: OutputFormatter.output/error/success, `kbagent http`, and the agent --stream NDJSON events (the latter also serialized with ensure_ascii=False, so it shared the crash). Two layers: force_utf8_stdout() reconfigures the stream to UTF-8, which covers every real TextIOWrapper; a UnicodeEncodeError fallback then writes UTF-8 bytes to sys.stdout.buffer for streams that cannot be reconfigured. A stream with neither surfaces the original error -- for a machine consumer, a mangled payload would be worse than a crash. Unlike the serve startup banner (#522), transliterating to ASCII is not an option here: the banner is decoration, this is data. Verified end to end under PYTHONIOENCODING=cp1250: the raw write raises, the formatter now emits valid UTF-8 JSON that round-trips through json.load. Modern UTF-8 terminals are byte-identical to before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
kbagent --json <anything>crashed withUnicodeEncodeErroron Windows whenever the payload contained a non-ASCII character (the reporter hit an arrow inside a flow name viakbagent --json flow list).Machine output is now written as UTF-8 regardless of the console codepage:
write_machine_output()inoutput.py, used byOutputFormatter.output/error/success,kbagent http, and theagent --streamNDJSON events.force_utf8_stdout()reconfiguressys.stdoutto UTF-8 (covers every realTextIOWrapper).UnicodeEncodeErrorthe payload is written as UTF-8 bytes tosys.stdout.buffer, which bypasses the text layer's codec. A stream with neitherreconfigurenorbufferre-raises — for a machine consumer, a silently mangled payload is worse than a crash.Why
sys.stdoutinherits the console codepage — cp1250 on Czech/Polish/Hungarian Windows 11. pydantic'smodel_dump_json()emits raw UTF-8 rather than\uXXXXescapes, so the write hits the cp1250 codec and aborts the command.--jsonexists specifically for machine consumption (piping to a file or another program), so it must not depend on which terminal happens to be attached.agent.py's stream events serialize withensure_ascii=Falseand shared the same exposure;kbagent httpused thejson.dumpsdefault (ensure_ascii=True) so it never crashed, but it is routed through the same helper for consistency.Unlike the
kbagent servestartup banner (#522, fixed in #526), transliterating to ASCII is not an option here: the banner is decoration, this is data.How it was tested
tests/test_json_output_encoding.py(24 tests): a cp1250 stdout double that raises exactly like a real Windows console, plus a fidelity guard proving the double reproduces the crash (so the passing tests are not vacuous). Covers the reconfigure path, the byte-level fallback, the no-fallback re-raise, andOutputFormatter.output/error/successround-tripping a→throughjson.loads.PYTHONIOENCODING=cp1250: a rawsys.stdout.write("extract → load")raisesUnicodeEncodeError, whileOutputFormatter(json_mode=True)now emits valid UTF-8 JSON that parses back withjson.load, both to a terminal and through a pipe.make check: lint + format + changelog-check + full suite (4707 passed, 8 skipped).Modern UTF-8 terminals are byte-identical to before — no
\uXXXXescaping is introduced.Fixes #546