DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(cli): keep launcher logs alive on legacy Windows code pages (#702)
Bingxi Zhao (Frank)a1fcbfd38a74
diff --git a/deeptutor/runtime/launcher.py b/deeptutor/runtime/launcher.pyindex 841f89f36..f678d691b 100644--- a/deeptutor/runtime/launcher.py+++ b/deeptutor/runtime/launcher.py@@ -144,6 +144,28 @@ def _terminate(proc: ManagedProcess | None) -> None:pass+def _relax_console_encoding(streams: tuple[object, ...] | None = None) -> None:+ """Make the launcher's own console output lossy instead of fatal.++ Child pipes are already decoded with ``errors="replace"`` (see ``_spawn``)+ and the children themselves get ``PYTHONIOENCODING=utf-8:replace``, but the+ parent process re-encodes every relayed line with the console's own codec.+ On a legacy Windows code page (cp950/cp936/cp932) an ordinary Next.js+ banner character like ``✓`` then raises ``UnicodeEncodeError`` inside+ ``_stream_output``, killing the relay thread — the app keeps running but+ goes silent for the rest of the session (issue #702).+ """+ for stream in streams if streams is not None else (sys.stdout, sys.stderr):+ reconfigure = getattr(stream, "reconfigure", None)+ if reconfigure is None:+ continue+ try:+ reconfigure(errors="replace")+ except (ValueError, OSError):+ # Already-detached or non-reconfigurable stream: nothing to relax.+ continue++def _stream_output(prefix: str, process: subprocess.Popen[str]) -> None:assert process.stdout is not Nonefor line in process.stdout:@@ -720,6 +742,7 @@ def _install_signal_handlers(request_shutdown: Callable[[str | None], None]) ->def start(home: str | Path | None = None) -> None:+ _relax_console_encoding()runtime_home = get_runtime_home(home)runtime_home.mkdir(parents=True, exist_ok=True)os.environ[DEEPTUTOR_HOME_ENV] = str(runtime_home)diff --git a/tests/runtime/test_launcher.py b/tests/runtime/test_launcher.pyindex 0d9a88a82..1d5a1161d 100644--- a/tests/runtime/test_launcher.py+++ b/tests/runtime/test_launcher.py@@ -185,3 +185,33 @@ def test_resolve_port_conflicts_change_option_prompts_and_persists(assert result == (8002, 3785)assert saved == {"backend": 8002, "frontend": 3785}+++class _RecordingStream:+ """Stand-in for a console stream that records ``reconfigure`` calls."""++ def __init__(self, *, raises: Exception | None = None) -> None:+ self.calls: list[dict[str, object]] = []+ self._raises = raises++ def reconfigure(self, **kwargs: object) -> None:+ self.calls.append(kwargs)+ if self._raises is not None:+ raise self._raises+++def test_relax_console_encoding_replaces_unencodable_output() -> None:+ """A legacy Windows code page can't encode the frontend's ``✓`` banner; the+ relay thread used to die on it and the session went silent (issue #702)."""+ stdout = _RecordingStream()+ stderr = _RecordingStream()++ launcher._relax_console_encoding((stdout, stderr))++ assert stdout.calls == [{"errors": "replace"}]+ assert stderr.calls == [{"errors": "replace"}]+++def test_relax_console_encoding_tolerates_odd_streams() -> None:+ """Redirected / already-detached streams must not break startup."""+ launcher._relax_console_encoding((object(), _RecordingStream(raises=ValueError("detached"))))