Skip to content

fix(agent-runner,api): keep U+0000 out of conversation event payloads - #836

Merged
pikann merged 1 commit into
Paca-AI:masterfrom
kayahk:fix/event-payload-nul
Nov 30, 2026
Merged

pikann merged 1 commit into
Paca-AI:masterfrom
kayahk:fix/event-payload-nul

Conversation

@kayahk

@kayahk kayahk commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

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:

ERROR: unsupported Unicode escape sequence (SQLSTATE 22P05)

agent_conversation_events.payload is jsonb, 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 in event_index — while the realtime copy still went out over Pub/Sub. services/api's event insert has the same exposure.

Fix

Replace JSON \u0000 escapes with U+FFFD before the payload reaches the jsonb column, in both writers:

  • services/agent-runner — ConversationRepository.InsertEvent
  • services/api — conversationEventToRecord (used by CreateAgentConversationEvent)

An escaped backslash (\u0000 as 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 TestJSONBSafeEventPayload in both services: plain payload untouched, single/multiple NUL escapes replaced, escaped backslash preserved, \u0001 preserved, 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.

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.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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-runner event inserts — InsertEvent pipes the payload through jsonbSafeEventPayload before the $6::jsonb cast.
  • jsonbSafeEventPayload (both services) — replaces \u0000 escapes with \uFFFD, leaves escaped-backslash text and \u0001-style control escapes untouched, and short-circuits when 0000 does not appear.
  • api event inserts — conversationEventToRecord sanitizes the marshaled payload for CreateConversationEvent.
  • 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?

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pikann pikann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you for the contribution! 🚀

@pikann
pikann merged commit 3998cea into Paca-AI:master Nov 30, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants