wire

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2026 License: Apache-2.0 Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func Guard(data []byte, l Limits) error

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

type Error struct {
	Rule Rule
	Path string
	Msg  string
	Err  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.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

func (*Error) Unwrap

func (e *Error) Unwrap() error

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 Kind

type Kind uint8

Kind is a JSON value's type.

const (
	KindNull Kind = iota
	KindBool
	KindNumber
	KindString
	KindArray
	KindObject
)

func (Kind) String

func (k Kind) String() string

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 Defaults

func Defaults() Limits

Defaults returns the default bounds.

func (Limits) WithDefaults

func (l Limits) WithDefaults() Limits

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.

const (
	RuleMessageBytes Rule = "message_bytes"
	RuleContainerLen Rule = "container_len"
	RuleDepth        Rule = "depth"
	RuleNodes        Rule = "nodes"
	RuleDuplicateKey Rule = "duplicate_key"
	RuleSyntax       Rule = "syntax"
	RuleType         Rule = "type"
)

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 Parse

func Parse(data []byte, l Limits) (Value, error)

Parse decodes a bounded tree. It is the strict decoder's entry point.

func (Value) Any

func (v Value) Any() (any, error)

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.

func (Value) Get

func (v Value) Get(key string) (Value, bool)

Get returns a member.

func (Value) JSON

func (v Value) JSON() ([]byte, error)

JSON re-encodes the tree, preserving member order and number literals.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL