The agent that turns websites into APIs!
fix(cli): preserve UTF-8 output from non-Python clients
kalil03214c78e3189b75
diff --git a/src/reverse_api/cli.py b/src/reverse_api/cli.pyindex 801c028..6969b58 100644--- a/src/reverse_api/cli.py+++ b/src/reverse_api/cli.py@@ -3007,6 +3007,7 @@ def _run_script_machine_payload(if script.suffix != ".py":import shutil+ from .runtime_commands import decode_process_outputfrom .utils import build_script_commandstry:@@ -3039,7 +3040,7 @@ def _run_script_machine_payload(result = Nonefor cmd in steps:emit_event("process_started", run_id=run_id, script_path=str(script))- result = subprocess.run(cmd, cwd=str(script.parent), capture_output=True, text=True, errors="replace")+ result = subprocess.run(cmd, cwd=str(script.parent), capture_output=True)if result.returncode != 0:breakassert result is not None, "build_script_commands returned no execution steps"@@ -3049,8 +3050,8 @@ def _run_script_machine_payload(script_path=str(script),script_args=script_args,returncode=result.returncode,- stdout=result.stdout or "",- stderr=result.stderr or "",+ stdout=decode_process_output(result.stdout),+ stderr=decode_process_output(result.stderr),scripts=scripts,)diff --git a/src/reverse_api/runtime_commands.py b/src/reverse_api/runtime_commands.pyindex 3cd659e..a5f7bb7 100644--- a/src/reverse_api/runtime_commands.py+++ b/src/reverse_api/runtime_commands.py@@ -2,12 +2,25 @@from __future__ import annotations+import localeimport reimport shutilimport sysfrom pathlib import Path+def decode_process_output(output: bytes | str | None) -> str:+ """Preserve UTF-8 tool output, with a fallback for legacy native tools."""+ if output is None:+ return ""+ if isinstance(output, bytes):+ try:+ output = output.decode("utf-8")+ except UnicodeDecodeError:+ output = output.decode(locale.getencoding(), errors="replace")+ return output.replace("\r\n", "\n").replace("\r", "\n")++def resolve_windows_command(argv: list[str]) -> list[str]:"""Prefer native npm/npx entrypoints, and reject unsafe batch fallbacks.diff --git a/tests/test_runtime_commands.py b/tests/test_runtime_commands.pyindex 1811f40..67c9d34 100644--- a/tests/test_runtime_commands.py+++ b/tests/test_runtime_commands.py@@ -8,7 +8,17 @@ from unittest.mock import patchimport pytest-from reverse_api.runtime_commands import resolve_windows_command+from reverse_api.runtime_commands import decode_process_output, resolve_windows_command+++def test_utf8_output_preferred_over_legacy_locale(monkeypatch):+ monkeypatch.setattr("locale.getencoding", lambda: "gbk")+ assert decode_process_output("中文 — 🐍\r\n".encode()) == "中文 — 🐍\n"+++def test_native_output_falls_back_to_legacy_locale(monkeypatch):+ monkeypatch.setattr("locale.getencoding", lambda: "cp1252")+ assert decode_process_output("café\r\n".encode("cp1252")) == "café\n"@pytest.mark.parametrize("argument", ["a&b", "a|b", "%PATH%", "!PATH!", "(a)", "a^b", 'a"b', "a\nb", "a>b", "a<b"])diff --git a/tests/test_windows_compatibility.py b/tests/test_windows_compatibility.pyindex 7c2c5ea..8074af2 100644--- a/tests/test_windows_compatibility.py+++ b/tests/test_windows_compatibility.py@@ -139,7 +139,8 @@ def test_node_diagnostic_probe_decodes_utf8_under_gbk(monkeypatch):assert "中文 — 🐍" in error-def test_run_json_real_python_unicode_output(tmp_path, monkeypatch):[email protected]("language", ["python", "javascript"])+def test_run_json_real_unicode_output(tmp_path, monkeypatch, language):"""Exercise actual child pipes under a legacy locale, including a spaced path."""import venv@@ -150,9 +151,13 @@ def test_run_json_real_python_unicode_output(tmp_path, monkeypatch):output_dir = tmp_path / "User Name 中文"scripts = output_dir / "scripts" / "unicode-run"scripts.mkdir(parents=True)- script = scripts / "api_client.py"- script.write_text("import sys\nprint('中文 — café 🐍')\nprint('erreur — 中文', file=sys.stderr)\n", encoding="utf-8")- venv.EnvBuilder(with_pip=False).create(output_dir / ".venv")+ if language == "python":+ script = scripts / "api_client.py"+ script.write_text("import sys\nprint('中文 — café 🐍')\nprint('erreur — 中文', file=sys.stderr)\n", encoding="utf-8")+ venv.EnvBuilder(with_pip=False).create(output_dir / ".venv")+ else:+ script = scripts / "api_client.js"+ script.write_text("console.log('中文 — café 🐍'); console.error('erreur — 中文');", encoding="utf-8")config = ConfigManager(tmp_path / "config.json")config.set("output_dir", str(output_dir))session = SessionManager(tmp_path / "history.json")