From 7e083a0859884fc0ed050550e94833f890f53486 Mon Sep 17 00:00:00 2001 From: chelsealong Date: Sun, 29 Nov 2026 19:54:24 +0000 Subject: [PATCH] fix: emit plain hook context as UTF-8 (#5627) --- src/specify_cli/events/__init__.py | 13 +++++++-- tests/specify_cli/events/test_events.py | 39 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/specify_cli/events/__init__.py b/src/specify_cli/events/__init__.py index 47f86a5cb7..66bef8154c 100644 --- a/src/specify_cli/events/__init__.py +++ b/src/specify_cli/events/__init__.py @@ -372,7 +372,12 @@ def _emit(output, envelope, native_event=""): if envelope == "hook_specific_output": sys.stdout.write(json.dumps({"decision": "allow", "hook_specific_output": {"additional_context": output}}) + "\\n") return - sys.stdout.write(output) + # Hook hosts decode stdout as UTF-8; Python's text stream may use the + # Windows code page instead and reject characters outside it. + if hasattr(sys.stdout, "buffer"): + sys.stdout.buffer.write(output.encode("utf-8")) + else: + sys.stdout.write(output) def main(): @@ -862,7 +867,11 @@ def _emit_event_stdout(output: str, envelope: str, native_event: str = "") -> No # harmlessly ignored on pre_tool/post_agent). sys.stdout.write(json.dumps({"decision": "allow", "hook_specific_output": {"additional_context": output}}) + "\n") return - sys.stdout.write(output) + # Match the generated dispatcher's plain-output path above. + if hasattr(sys.stdout, "buffer"): + sys.stdout.buffer.write(output.encode("utf-8")) + else: + sys.stdout.write(output) # -- Sourcing events map (CLI/Orchestration domain) ------------------------- diff --git a/tests/specify_cli/events/test_events.py b/tests/specify_cli/events/test_events.py index c6373bdf35..f5f7183702 100644 --- a/tests/specify_cli/events/test_events.py +++ b/tests/specify_cli/events/test_events.py @@ -862,6 +862,45 @@ def test_copilot_stop_mapping(self): class TestContextInjectionEnvelopes: """C13: context-injection envelope resolution and emission.""" + @pytest.mark.parametrize("inline", [False, True], ids=["delegated", "inline"]) + def test_plain_output_is_utf8_with_non_utf8_stdout(self, tmp_path, inline): + """The agent receives UTF-8 context even when Python defaults to cp1252.""" + import subprocess + import sys + + manifest = MagicMock(spec=IntegrationManifest) + manifest.files = {} + install_integration_events( + ClaudeIntegration(), tmp_path, manifest, + {"session_start": [{"command": "speckit.boot"}]}, + ) + dispatcher = tmp_path / EVENTS_DISPATCHER_REL + cmd_dir = tmp_path / ".specify" / "templates" / "commands" + cmd_dir.mkdir(parents=True) + (cmd_dir / "boot.md").write_text( + "---\ndescription: Boot\nscripts:\n py: scripts/boot.py\n---\nBody\n", + encoding="utf-8", + ) + script_dir = tmp_path / ".specify" / "scripts" + script_dir.mkdir(parents=True) + (script_dir / "boot.py").write_text( + "import sys\nsys.stdout.buffer.write('Größe α'.encode('utf-8'))\n", + encoding="utf-8", + ) + env = dict(os.environ) + env["PYTHONIOENCODING"] = "cp1252" + if inline: + fake_dir = tmp_path / "_fake" + (fake_dir / "specify_cli").mkdir(parents=True) + (fake_dir / "specify_cli" / "__init__.py").write_text("", encoding="utf-8") + env["PYTHONPATH"] = str(fake_dir) + result = subprocess.run( + [sys.executable, str(dispatcher), "speckit.boot", "session_start", "60"], + input=b"{}", capture_output=True, cwd=tmp_path, env=env, + ) + assert result.returncode == 0, result.stderr.decode("utf-8", errors="replace") + assert result.stdout == "Größe α".encode("utf-8") + def test_emit_event_stdout_wrapping(self, capsys): from specify_cli.events import _emit_event_stdout