"""Make writing to the console incapable of failing. The ported agent and service code prints progress with emoji - fifty-odd such lines across fourteen modules. On Windows the console defaults to a legacy code page, so ``print("🧠 …")`` raises ``UnicodeEncodeError`` and, because those prints sit inside pipeline steps, a diagnostic message kills a run that has already been paid for. A search step died on its own progress line. Rewriting fifty prints would fix today's fifty. Making the stream lenient fixes the class: after this, no message can raise no matter what it contains or where it is written from. Imported for its side effect, before anything else prints. """ from __future__ import annotations import sys def enable_utf8_output() -> None: """UTF-8 out, and never raise on a character the terminal cannot draw. ``errors="replace"`` is the important half. UTF-8 alone still fails when the terminal genuinely cannot encode something; replacement means the worst case is a question mark in a log line rather than a lost run. """ for stream in (sys.stdout, sys.stderr): reconfigure = getattr(stream, "reconfigure", None) if reconfigure is None: # Already wrapped by something else - a captured stream under # pytest, for instance. Leave it alone. continue try: reconfigure(encoding="utf-8", errors="replace") except (ValueError, OSError): # A detached or closed stream. Nothing to do, and nothing lost. pass