Skip to content

[Bug]: Plain-envelope hook output fails with UnicodeEncodeError on non-UTF-8 stdout #5627

Backlog

Description

@markuswondrak

Bug Description

With the plain context envelope, the event dispatcher writes handler output to stdout as raw text (sys.stdout.write(output) in _emit_event_stdout and in the generated dispatcher's _emit, src/specify_cli/events/__init__.py). Python encodes that text with the stdout encoding. When the agent pipes hook stdout and the host encoding is not UTF-8 (for example Windows with cp1252), any character the codec cannot represent raises UnicodeEncodeError. The hook then fails and the agent receives no context.

The JSON envelopes (hookSpecificOutput, additionalContext, additional_context, hook_specific_output) are not affected, because json.dumps escapes non-ASCII characters.

This affects every handler that uses the plain envelope on main (f2010547), for example Claude Code and Codex CLI. #5616 reuses the same emit path for workflow additional_context (review comment: #5616 (comment)).

Steps to Reproduce

Run on main at f2010547, Linux. A non-UTF-8 pipe is simulated with PYTHONIOENCODING=cp1252.

  1. specify init . --integration claude --script sh --ignore-agent-tools

  2. Add a local extension with specify extension add --dev <path>. It declares events: {session_start: {command: speckit.evt.hello}}, and the command's scripts.sh prints Kontext: Größe α.

  3. Run the generated hook command from .claude/settings.json:

    echo '{}' | PYTHONIOENCODING=cp1252 python3 "$PWD/.specify/events.py" speckit.evt.hello session_start 60
  4. Output: Event command speckit.evt.hello error: 'charmap' codec can't encode character '\u03b1' in position 15: character maps to <undefined>, exit 2. The inline dispatcher path (no importable specify_cli) fails the same way.

With PYTHONIOENCODING=utf-8, the same command prints Kontext: Größe α and exits 0.

Expected Behavior

Plain-envelope handler output reaches the agent intact, independent of the host's stdout encoding, both through specify_cli and through the inline dispatcher path.

The fix has to account for how each agent decodes hook stdout. Writing UTF-8 bytes (for example through sys.stdout.buffer) avoids the exception, but an agent that decodes the pipe with the host code page would show garbled text. The agent-specific decoding should be checked before choosing the fix.

Actual Behavior

UnicodeEncodeError, the hook fails, and the context is dropped.

Specify CLI Version

1.4.6.dev0 (main at f2010547)

AI Agent

All integrations that use the plain envelope (for example Claude Code, Codex CLI, opencode).

Operating System

Windows (reasoned from code and simulated with PYTHONIOENCODING=cp1252 on Linux; not run on Windows).

Python Version

3.14.4 (reproduction host)

Error Logs

Event command speckit.evt.hello error: 'charmap' codec can't encode character '\u03b1' in position 15: character maps to <undefined>

Additional Context

Found during review of #5616 (part of #5533). Deferred there because it is pre-existing on main. The context-delivery integrations in that stack are not affected: Copilot (#5621) and agy (#5617) use JSON envelopes, and pi (#5623) starts the dispatcher with PYTHONIOENCODING=utf-8.

AI Disclosure

Filed on behalf of @markuswondrak by the pi coding agent (provider github-copilot, model claude-opus-5.5, reasoning effort xhigh, interactive and human-supervised). The agent reproduced the failure on main on Linux and wrote this issue at @markuswondrak's request. It was not run on Windows.

Activity

  1. added
    triage-must-have
    on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage-must-have

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions