The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): decode UTF-8 child output explicitly on the serve/agent path (#83851)
brooklyn!0a0e6334c65d
diff --git a/hermes_cli/web_routers/files.py b/hermes_cli/web_routers/files.pyindex c2657aa70a5..54591d684a7 100644--- a/hermes_cli/web_routers/files.py+++ b/hermes_cli/web_routers/files.py@@ -217,7 +217,10 @@ def _fs_default_cwd() -> str:def _fs_git_branch(cwd: str) -> str:try:- run_kwargs: Dict[str, Any] = {"capture_output": True, "text": True, "timeout": 2, "check": False}+ # git emits UTF-8 (branch names, localized "not a git repository" stderr); the locale codec+ # (cp936 on zh-CN Windows) raised inside communicate()'s reader threads on every poll (#83851).+ run_kwargs: Dict[str, Any] = {"capture_output": True, "text": True, "encoding": "utf-8",+ "errors": "replace", "timeout": 2, "check": False}if sys.platform == "win32":run_kwargs["creationflags"] = windows_hide_flags()result = subprocess.run(["git", "-C", cwd, "branch", "--show-current"], **run_kwargs)diff --git a/tests/hermes_cli/test_web_server_files.py b/tests/hermes_cli/test_web_server_files.pyindex 344b4579f0c..fd9ed4d078c 100644--- a/tests/hermes_cli/test_web_server_files.py+++ b/tests/hermes_cli/test_web_server_files.py@@ -419,3 +419,23 @@ def test_credential_dir_trees_blocked_on_subdir_descent(forced_files_client):assert [e["name"] for e in mcp_listing.json()["entries"]] == []+++def test_git_branch_decodes_utf8_under_a_gbk_default_codec(tmp_path, monkeypatch):+ """#83851: the Desktop polls ``/api/fs/default-cwd``; on zh-CN Windows the serve process's default+ subprocess codec is cp936, and git's UTF-8 output (branch names, localized stderr) raised+ UnicodeDecodeError in communicate()'s reader threads on every poll. The branch must round-trip."""+ import shutil+ import subprocess++ git = shutil.which("git")+ if git is None:+ pytest.skip("git not installed")+ branch = "功能/✅-修复" # UTF-8 bytes that are illegal multibyte sequences in GBK+ subprocess.run([git, "init", "-q", str(tmp_path)], check=True)+ subprocess.run([git, "-C", str(tmp_path), "symbolic-ref", "HEAD", f"refs/heads/{branch}"], check=True)+ # subprocess resolves an unspecified text-mode codec through _text_encoding() → locale.getencoding()+ # (cp936 on zh-CN Windows); patch that seam since run_tests.sh's PYTHONUTF8=1 short-circuits locale.+ monkeypatch.setattr(subprocess, "_text_encoding", lambda: "gbk")++ assert _rt_files._fs_git_branch(str(tmp_path)) == branchdiff --git a/tests/tools/test_bot_mode_dm.py b/tests/tools/test_bot_mode_dm.pyindex 2632cede2f1..7a73230f128 100644--- a/tests/tools/test_bot_mode_dm.py+++ b/tests/tools/test_bot_mode_dm.py@@ -1117,3 +1117,18 @@ def test_local_turn_survives_undecodable_transport_output(tmp_path, capsys):assert bot_mode_dm._run_local_turn(argv, str(dm_file)) == 0assert "reply" in capsys.readouterr().out+++def test_local_turn_relays_utf8_reply_under_a_gbk_default_codec(tmp_path, monkeypatch, capsys):+ """#83851: the transport is a Hermes CLI child, which always writes UTF-8 stdio. Decoding it with+ the host's default codec (cp936 on zh-CN Windows) crashed or garbled the reply; it must round-trip."""+ dm_file = tmp_path / "dm.txt"+ dm_file.write_text("hello", encoding="utf-8")+ reply = "✅ 已完成…"+ argv = [sys.executable, "-c", f"import sys; sys.stdout.buffer.write({reply.encode('utf-8')!r})"]+ # subprocess resolves an unspecified text-mode codec through _text_encoding() → locale.getencoding();+ # patch that seam since run_tests.sh's PYTHONUTF8=1 short-circuits the locale lookup.+ monkeypatch.setattr(subprocess, "_text_encoding", lambda: "gbk")++ assert bot_mode_dm._run_local_turn(argv, str(dm_file)) == 0+ assert reply in capsys.readouterr().outdiff --git a/tools/bot_mode_dm.py b/tools/bot_mode_dm.pyindex 090061d3ba4..52a31cfc8b7 100644--- a/tools/bot_mode_dm.py+++ b/tools/bot_mode_dm.py@@ -416,7 +416,7 @@ def _run_local_turn(argv: list[str], dm_file: str, *, env: Optional[dict[str, stdef _turn(turn_env=env):return subprocess.run([*argv, "--query-file", dm_file], check=False, stdin=subprocess.DEVNULL,- capture_output=True, text=True, errors="replace", env=turn_env)+ capture_output=True, text=True, encoding="utf-8", errors="replace", env=turn_env)proc = _turn()if proc.returncode != 0:diff --git a/tools/browser_lightpanda.py b/tools/browser_lightpanda.pyindex 8fe03531e7f..79036fc9c25 100644--- a/tools/browser_lightpanda.py+++ b/tools/browser_lightpanda.py@@ -117,7 +117,7 @@ def _binary_supports_http_cache(binary: str) -> bool:try:proc = subprocess.run([binary, "help"],- capture_output=True, text=True, timeout=3.0,+ capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=3.0,stdin=subprocess.DEVNULL,)return _HTTP_CACHE_FLAG in ((proc.stdout or "") + (proc.stderr or ""))diff --git a/tools/browser_tool_install.py b/tools/browser_tool_install.pyindex 30172bf2a70..1f52b436198 100644--- a/tools/browser_tool_install.py+++ b/tools/browser_tool_install.py@@ -179,7 +179,8 @@ def warm_agent_browser_npx_cache(timeout: float = 60.0) -> bool:return Falseenv = _bt._build_browser_env()env["PATH"] = _merge_browser_path(env.get("PATH", ""))- popen_kwargs: dict = {"stdout": subprocess.PIPE, "stderr": subprocess.PIPE, "text": True, "env": env}+ popen_kwargs: dict = {"stdout": subprocess.PIPE, "stderr": subprocess.PIPE, "text": True,+ "encoding": "utf-8", "errors": "replace", "env": env}if os.name == "posix":popen_kwargs.update(creationflags=windows_hide_flags(), start_new_session=True)else:diff --git a/tools/computer_use/cua_backend_daemon.py b/tools/computer_use/cua_backend_daemon.pyindex 8c90e9e27be..9622bb66ff5 100644--- a/tools/computer_use/cua_backend_daemon.py+++ b/tools/computer_use/cua_backend_daemon.py@@ -167,7 +167,8 @@ class _EmbeddedCuaDaemon:env = self._sanitized_env()command = _embedded_daemon_spawn_command(self._command, self._serve_args(), platform=sys.platform)self._process = subprocess.Popen(command, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,- stderr=subprocess.PIPE, text=True, env=env)+ stderr=subprocess.PIPE, text=True, encoding="utf-8", errors="replace",+ env=env)self._owns_runtime = Truethreading.Thread(target=self._drain_stderr, args=(self._process,), name="hermes-cua-daemon-stderr", daemon=True).start()deadline = time.monotonic() + self._START_TIMEOUT_SECONDSdiff --git a/tools/self_repo_guard.py b/tools/self_repo_guard.pyindex 0b03fe80f24..f0fb5aa1691 100644--- a/tools/self_repo_guard.py+++ b/tools/self_repo_guard.py@@ -446,7 +446,7 @@ def _read_git_alias(executable: str, target: Path, alias: str) -> str | None:with contextlib.suppress(OSError, subprocess.SubprocessError):result = subprocess.run([executable, "-C", str(target), "config", "--get", f"alias.{alias}"],- capture_output=True, text=True, timeout=1, check=False)+ capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=1, check=False)return (result.stdout.strip() or None) if result.returncode == 0 else Nonereturn None