Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/specify_cli/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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) -------------------------
Expand Down
39 changes: 39 additions & 0 deletions tests/specify_cli/events/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading