Skip to content

Normalize invalid UTF-8 tool output before protobuf streaming #402

Closed

Description

@jhrozek

Summary

Arbitrary command/file output can enter a session.ToolResult as an invalid UTF-8 Go string. The gRPC event mapper copies that string into a protobuf string field, causing the live session stream to terminate:

rpc error: code = Internal desc = grpc: error while marshaling: string field contains invalid UTF-8

The observed run was cancelled immediately after the failed stream.Send.

Reproduction

This was reproduced on macOS, where BSD cat -t can turn a valid UTF-8 em dash into invalid output:

printf '\342\200\224\n' | cat -t | xxd -p

Observed bytes:

e2 4d 2d 5e 40 4d 2d 5e 54 0a
   M  -  ^  @  M  -  ^  T

The E2 lead byte is retained while the continuation bytes are rendered as M-^@M-^T, leaving an orphaned lead byte.

The real failing Bash call was:

sed -n 386,394p docs/architecture/providers.md | cat -t

Its output contained the affected em dash. The corresponding successful tool.result then killed the gRPC stream during protobuf marshaling.

Root cause

The invalid bytes travel unchanged through the following path:

  1. internal/adapter/osfs.CommandRunner.Run captures stdout/stderr into bytes.Buffer and converts them directly with String().
  2. engine/adapter/fstools.BashTool copies the output into session.ToolResult.Content.
  3. The truncation helper only avoids splitting an assumed-valid rune; it does not repair pre-existing invalid UTF-8, and strings below the cap pass through unchanged.
  4. engine/session.NewToolResult accepts arbitrary strings.
  5. engine/agent.Engine.execute records and emits the result unchanged.
  6. internal/adapter/server.toProtoToolResult assigns it directly to protobuf ToolResult.content.
  7. HarnessServer.Converse calls stream.Send; protobuf rejects the invalid string and the relay cancels the run.

The durable JSON event was written before the gRPC send. Go's JSON encoder replaced the invalid byte with U+FFFD, so the persisted record was valid and showed:

�M-^@M-^T

This explains why replay/persistence can appear healthy while the original live gRPC event fails.

Broader exposure

Bash is the confirmed producer, but it is not the only possible one:

  • Read converts arbitrary file bytes directly to string.
  • The command capture cap can split a valid multibyte rune at the byte limit.
  • Custom tools and MCP typed result text/metadata can return invalid strings.
  • The protobuf mapper copies many domain strings verbatim, including event text, tool results, structured content, block metadata, team/subagent/parallel detail, and compaction archive fields.

Inbound gRPC and normal JSON provider text are less likely because those decoders generally establish valid UTF-8.

Proposed repair

Use defense in depth:

  1. Normalize every textual field of the effective ToolResult after post-hooks and before the recorder, event emission, conversation recording, or provider request. This keeps the model, audit log, durable event log, and client stream consistent.
  2. Add a protobuf projection backstop so an invalid string from any future/custom producer cannot terminate a gRPC stream.
  3. Preserve binary []byte content blocks unchanged; normalize only fields represented as text/protobuf strings.

A replacement such as strings.ToValidUTF8(s, "\uFFFD") is consistent with the current JSON persistence behavior.

Acceptance criteria

  • A Bash result containing \xe2M-^@M-^T does not terminate Converse.
  • The emitted, persisted, model-facing, and recorded tool result all contain the same repaired UTF-8 text.
  • Read of invalid byte content cannot produce an invalid protobuf string.
  • Output truncated at a multibyte boundary remains valid UTF-8.
  • Textual fields in typed tool-result blocks are normalized; binary data fields remain byte-identical.
  • A gRPC end-to-end regression asserts the stream reaches its terminal result instead of returning codes.Internal.
  • A mapper-level backstop test proves protobuf marshaling cannot fail for malformed domain strings.

Activity

  1. added a commit that references this issue on Aug 9, 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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions