Repository navigation
fix(agent-runner,api): keep U+0000 out of conversation event payloads - #836
Conversation
An event whose payload carried a NUL byte (agent tool output such as a binary or minified file) failed its insert with SQLSTATE 22P05 — "unsupported Unicode escape sequence" — because jsonb cannot store U+0000. The runner logged it at WARN and moved on, so the event was silently lost from conversation history; the API's own event insert has the same exposure. Replace JSON \u0000 escapes with U+FFFD before the payload reaches the jsonb column in both writers. An escaped backslash (\u0000 as literal text) is left alone: only an odd backslash run starts an escape.
There was a problem hiding this comment.
✅ No new issues found.
Approach: sound — sanitizing the JSON text at the repository boundary in both writers means no caller can bypass it, and the backslash-run parity logic is correct for even/odd runs, escaped text, and control escapes other than NUL.
Reviewed changes
agent-runnerevent inserts —InsertEventpipes the payload throughjsonbSafeEventPayloadbefore the$6::jsonbcast.jsonbSafeEventPayload(both services) — replaces\u0000escapes with\uFFFD, leaves escaped-backslash text and\u0001-style control escapes untouched, and short-circuits when0000does not appear.apievent inserts —conversationEventToRecordsanitizes the marshaled payload forCreateConversationEvent.- Tests — table-driven cases cover plain payloads, single/multiple/trailing NUL escapes, an escaped backslash, and
\u0001.
ℹ️ The services/api insert has no production caller
The PR body's premise that the API shares the exposure does not hold today: CreateConversationEvent is referenced only by the pagination e2e seed and a mock, while every live write goes through agent-runner's InsertEvent. This is not a defect — keeping the mirrored helper is reasonable defense in depth — but the runtime fix comes entirely from the agent-runner change, and the API-side test guards a helper that no production path exercises.
Technical details
# api event-write reach
## Affected sites
- `services/api/internal/repository/postgres/agent_repository.go:1520` — `CreateConversationEvent`, the only `INSERT INTO agent_conversation_events` in the API.
- `services/api/test/e2e/conversation_pagination_test.go:957` — the only caller (test seed).
- `services/api/internal/service/agent/agent_service_test.go:475` — mock implementation.
- `services/agent-runner/internal/handler/handler.go:305,596,632` and `internal/acpbridge/server.go:327` — live write sites.
## Open questions for the human
- Is a production caller for the API path planned, or is the mirrored helper purely defensive parity?deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
pikann
left a comment
There was a problem hiding this comment.
LGTM! Thank you for the contribution! 🚀

Problem
An event whose payload carries a NUL byte — normal agent tool output, e.g. reading a binary or minified asset — fails its insert with:
agent_conversation_events.payloadisjsonb, and jsonb cannot represent U+0000. The runner logs the failure at WARN (failed to persist event) and continues, so the event is silently missing from conversation history — a visible gap inevent_index— while the realtime copy still went out over Pub/Sub.services/api's event insert has the same exposure.Fix
Replace JSON
\u0000escapes with U+FFFD before the payload reaches the jsonb column, in both writers:services/agent-runner—ConversationRepository.InsertEventservices/api—conversationEventToRecord(used byCreateAgentConversationEvent)An escaped backslash (
\u0000as literal text) is intentionally left untouched: only an odd backslash run starts an escape. Other control escapes (\u0001, …) are left as-is — jsonb accepts them.Tests
go test ./internal/repository/postgres/ -run TestJSONBSafeEventPayloadin both services: plain payload untouched, single/multiple NUL escapes replaced, escaped backslash preserved,\u0001preserved, trailing NUL escape.Note: the stored payload differs from the realtime copy for those events (U+FFFD vs the raw escape). That is the point — history is durable, the live frame is ephemeral.