docs/pkg/protocol.md
pkg/protocol — JSONL frame ownership
pkg/protocol defines the Go types behind every JSONL frame on EzAgent’s wire, the nested values those frames share, wire-compatible validation, and small encoding helpers. For every field, example, direction, and frame relationship, see the JSONL API catalog. It does not own transport (jsonlpipe), typed dispatch and RPC (ezagentapi), or command semantics and persistence (service, service/session).
Keep in sync: whenever any
pkg/protocol/*.gofile changes, updatedocs/agent-api/jsonl-api.htmlin the same change.
What does this package group together?
| Group | What it owns |
|---|---|
| Lifecycle and agent stream | Startup, prompt/steer/abort, and agent/turn/message frames |
| Sessions and configuration | Roots, leaves, turns, queues, configuration requests, and model/provider results |
| Capabilities | Tool, plugin, macro, operation, heartbeat, and capability metadata frames |
| Shared values and validation | Messages, content, usage, progress, attachments, and turn-role/storage validation |
| Encoding and ID helpers | Request/leaf ID extraction, framing, custom CSV, structural escaping, and display formatting |
What must remain true when changing frames?
- Only top-level frames carry
Itr; nested values must not.ezagentapistamps outbound copies and owns ordering, so protocol payloads must not acquire sequencing behavior. - Use
PendingTurnas canonical ingress.Prompt,Steer,ToolResult, andQueuedMessagecarryturn; it intentionally omits service-owned placement, identity, lifecycle, and storage fields. Its changes cross plugin, storage, and ingress boundaries. - Preserve validation boundaries. User-turn, tool-result, plugin, and storage validation deliberately differ. Attachment checks cover encoding, UTF-8/base64, size, metadata agreement, per-file, and aggregate limits; storage validates before persistence.
- Treat JSON shape as compatibility surface. Tags,
omitempty, pointer choices, and value-vs-pointer structs control emitted JSON. Do not simplify them without checking wire-shape tests and consumers. - Represent the earliest rewind boundary explicitly.
rewind_leaf.new_tip_turn_idis a required JSON member; its empty string value means the boundary before the first turn.RewindLeaf.HasNewTipTurnIDdistinguishes that value from an omitted member after decoding. - Progress validation is producer-local; it is not service admission.
ReportProgressvalidates descriptors and limits; aProgressSnapshotreplaces a subscriber projection, while the service remains authoritative for turn and capacity admission. - Pending questions are complete leaf state.
LeafUpdated.PendingQuestionsalways encodes as an array, with[]as the clearing value. The only current question kind is the typedproject_config_trustshape.question_replyaccepts only that kind, a nonempty revision, andtrustorreject. - Access source is computed, optional, and read-only.
LeafUpdated.AccessSourceandLeafStatsResponse.AccessSourceproject the same daemon-derived source:local,openai_subscription,api_key,unknown, orunavailable. Missing or unfamiliar values are neutral, never evidence of local compute. The field contains no credentials and is not persisted leaf configuration. - Correlated responses may still reach callbacks.
Error.RequestIDandExtractRequestIDsupport service replies;ezagentapiowns typed RPC routing. Do not infer that every correlated response is hidden from callbacks.
What lives just outside this package?
- jsonlpipe owns protocol-agnostic JSONL transport and connection lifecycle.
- ezagentapi maps frames to typed operations, validates startup, dispatches callbacks, and correlates RPCs.
- service gives frames command semantics; service/session materializes validated pending turns.
- Capability authoring for tools, plugins, and macros starts at builtin capabilities.
Where are all fields, examples, and directions?
pkg/protocol/*.go— declarations, validators, and helpers;*_test.go— compatibility and boundary evidence.- JSONL API catalog — complete frame fields, examples, directions, and nested schemas.
- EzAgent CSV — the custom apostrophe-quoted CSV contract; it is not ordinary CSV.
When changing this package, update this ownership page if its boundary, shape, or invariant changes—and always update the JSONL API catalog in the same change.
User-turn context
PendingTurn.steer is an optional boolean carried by every pending-turn
container, including plugin inputs/results and queued messages. Omitted means
unspecified; explicit false and true survive handoffs. Active assistant/tool
admission supplies true only when unspecified. An idle call to steer does not
by itself mark a follow-up. Plugins may mark synthetic user turns for downstream
consumers. The flag is context, not an admission command, and has no separate
stored column. The mode plugin writes its lasting classification in turn text.
The leading <mode ... /> text tag accepts optional mode and turn-type
attributes independently. steer denotes input drafted during the preceding
response/tool call; bash denotes a background bash completion. Missing
attributes merge into one tag, existing bash origin takes precedence over
steer context, and neither type changes the applicable mode.
User pending turns may carry validated advanced.other_meta JSON object text,
but not assistant-only advanced fields or the service-owned exit_message key.
The internal background bash completion convention is
{"background_bash":{"task_id":"<trusted originating tool-call ID>"}}.
Producers merge this correlation with existing metadata, preserving unrelated
keys and nested extensions. Stored user turns retain it in existing other_meta.
The TUI uses the nested marker, not prose or endpoint choice, to render
background → bash (<id>) with tool-result styling. No new stored kind or
additional wire fields are involved.
background_bash is producer-supplied correlation, not independently authenticated
provenance. Socket/API peers are trusted callers; this convention creates no
separate caller permission or prompt-injection defense. The builtin captures the
leaf when Bash starts and delivers completion to that same leaf, independently of
the TUI's current selection. It does not make command output trusted instructions.