Documentation
¶
Overview ¶
Package wire is the bounded, strict decoder for bytes AgentKit did not produce.
Every other decoder in this module reads data we or the model authored — a tool's arguments, a catalog file — and can afford to be lenient. This one reads what a peer sent: an MCP server we spawned, or a gateway in front of a provider. The difference is not tone. A lenient decoder on that surface turns a malformed message into an allocation the peer chose, a duplicate key into a value the peer chose, and a deep nest into a stack the peer chose.
WHY encoding/json/v2, NOT v1. encoding/json enforces none of these bounds, silently accepts duplicate keys, and matches field names case-insensitively. jsontext rejects duplicate names itself and exposes a token stream, so the bounds are a loop over it, each checked before the token that would exceed it is read.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrRejected = errors.New("wire: message rejected")
ErrRejected matches every rejection this package produces, so a transport can poison itself on the first rejection without enumerating rules.
Functions ¶
func Guard ¶
Guard validates a message against the bounds WITHOUT building a tree.
It exists because the bounds and the strict binding have different scopes. Every untrusted surface needs the bounds — a provider response included — but a provider response must stay tolerant of fields a vendor added last week, so it is guarded and then decoded with the ordinary lenient path. One extra linear scan is the whole cost, and it buys duplicate-key rejection on a surface where last-wins would let a compromised gateway choose which of two stop_reasons AgentKit sees.
Types ¶
type Error ¶
Error is a rejection. It names the rule and the path, because "invalid JSON" from a peer is not something an operator can act on.
type FrameReader ¶
type FrameReader struct {
// contains filtered or unexported fields
}
FrameReader reads one untrusted message at a time, bounded before it allocates.
It is POISONED by its first malformed message: once a frame is rejected, every later call returns the same error and no attempt is made to resynchronize. Resynchronizing a framed stream after a parse error means guessing where the next frame starts, using framing that has already proved untrustworthy — and a peer that can desynchronize the framing can then choose what AgentKit reads as a message boundary.
func NewContentLength ¶
func NewContentLength(r io.Reader, l Limits) *FrameReader
NewContentLength reads LSP-style `Content-Length: N` framing.
func NewNDJSON ¶
func NewNDJSON(r io.Reader, l Limits) *FrameReader
NewNDJSON reads newline-delimited JSON: MCP stdio's framing and Ollama's native streaming shape.
func (*FrameReader) Next ¶
func (f *FrameReader) Next() ([]byte, error)
Next returns the next frame's bytes, or io.EOF at a clean end of stream.
func (*FrameReader) Poisoned ¶
func (f *FrameReader) Poisoned() bool
Poisoned reports whether a malformed frame has torn the reader down.
type Limits ¶
type Limits struct {
// MaxMessageBytes rejects the whole message. Default 16 MiB.
MaxMessageBytes int64
// MaxContainerLen bounds array elements and object members. Default 1e6.
MaxContainerLen int
// MaxDepth bounds nesting. Default 64.
MaxDepth int
// MaxNodes bounds the TOTAL number of values in one message, across every
// container. Default 2e6.
//
// The other three bounds do not imply it. A decoded Value is 96 bytes
// and its shortest encoding is two, so a 16 MiB message of `[[],[],…]`
// that respects MaxContainerLen and MaxDepth still materializes as a
// tree sixty-four times its own size. This is the bound on that
// amplification: 2e6 nodes is under 200 MiB of tree at the worst case,
// and a legitimate message that needs more is not one this protocol has.
MaxNodes int
}
Limits are the decoder's bounds. The zero value means Defaults.
func (Limits) WithDefaults ¶
WithDefaults fills the zero fields. It is exported because a caller that bounds a read BEFORE handing bytes to Parse — an SSE event, an HTTP body — needs the same number Parse will use, and re-deriving it is how two bounds drift apart.
type Rule ¶
type Rule string
Rule names which contract a rejection violated. It is a closed set so a caller can branch on it without matching on message text.
type Value ¶
type Value struct {
Kind Kind
Bool bool
Number json.Number
String string
Array []Value
// Object holds members. Duplicate keys are REJECTED at decode, so a map
// is safe here: there is never a second value to lose.
Object map[string]Value
// Keys is insertion order, for deterministic error messages and for a
// caller that needs to know what the peer actually sent.
Keys []string
}
Value is one decoded JSON value.
Numbers are held as json.Number — the VERBATIM literal — so 1, 1.0 and 1e3 stay distinguishable. Cross-language number semantics need that distinction, and so does diffing a request body without laundering it through a float64.
func (Value) Any ¶
Any materializes the tree as ordinary Go values: map[string]any, []any, json.Number, string, bool, nil.
Numbers come back as json.Number, never float64. A tool argument of 9007199254740993 that round-trips through a float64 comes back as 9007199254740992, and nothing downstream can tell.