Repository navigation
Description
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 -pObserved 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 -tIts 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:
internal/adapter/osfs.CommandRunner.Runcaptures stdout/stderr intobytes.Bufferand converts them directly withString().engine/adapter/fstools.BashToolcopies the output intosession.ToolResult.Content.- 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.
engine/session.NewToolResultaccepts arbitrary strings.engine/agent.Engine.executerecords and emits the result unchanged.internal/adapter/server.toProtoToolResultassigns it directly to protobufToolResult.content.HarnessServer.Conversecallsstream.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:
Readconverts arbitrary file bytes directly tostring.- 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:
- Normalize every textual field of the effective
ToolResultafter 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. - Add a protobuf projection backstop so an invalid string from any future/custom producer cannot terminate a gRPC stream.
- Preserve binary
[]bytecontent 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-^Tdoes not terminateConverse. - The emitted, persisted, model-facing, and recorded tool result all contain the same repaired UTF-8 text.
-
Readof 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
fix(session): repair invalid UTF-8 in tool results before replay (#402)
fix(utf8): close the #402 crash class on its sibling surfaces (panel …
fix(osfs): cut the command-output cap on a rune boundary (#402 AC 4)
fix(server): repair TeamPayload.Cause on the wire (#402)
Merge pull request #418 from stacklok/invalid-utf8
fix(steer): round-2 rework — mutex inbox (drop supersede) + clean-exi…
fix(steer): round-2 rework — mutex inbox (drop supersede) + clean-exi…
feat(engine,server): durable run identity — a host-minted run_id
feat(engine,server): durable run identity — a host-minted run_id
feat(engine,server): durable run identity — a host-minted run_id
feat(engine,server): durable run identity — a host-minted run_id
feat(engine,server): durable run identity — a host-minted run_id
feat(engine,server): durable run identity — a host-minted run_id
feat(engine,server): durable run identity — a host-minted run_id (#827)
feat(server): WatchSessionEvents — durable replay-then-follow over th…
feat(server): WatchSessionEvents — durable replay-then-follow over th…
feat(server): WatchSessionEvents — durable replay-then-follow over th…
feat(server): WatchSessionEvents — durable replay-then-follow over th…