protocol

package
v0.1.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: AGPL-3.0 Imports: 1 Imported by: 0 modules, 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 protocol defines the Menagerie relay protocol message types.

protocol-v1.3

This is the Go port of protocol/types.ts, which is the canonical definition. Keep the two in sync — if they disagree, types.ts wins.

Transport: WebSocket, JSON text frames. One connection per browser<->relay pair; sessions multiplex via session_id. v1.2 adds structured sessions ("acp"): ACP payloads ride nested inside Menagerie frames as RawMessage and are never interpreted here (see docs/acp-protocol.md).

Index

Constants

View Source
const (
	TypeHello        = "hello"
	TypeRegister     = "register"
	TypeRegistered   = "registered"
	TypeSpawn        = "spawn"
	TypeSpawned      = "spawned"
	TypeOutput       = "output"
	TypeInput        = "input"
	TypeSignal       = "signal"
	TypeEvent        = "event"
	TypeResume       = "resume"
	TypeResumeFailed = "resume_failed"
	TypeError        = "error"

	// protocol 1.1: live re-attach after a client reconnect
	TypeSessions = "sessions"
	TypeAttach   = "attach"
	// TypeSeen: a client reporting that a human actually looked at this session.
	// The ONLY thing that demotes `done` to `idle` — reading a session over the
	// protocol must not (spec §8.1.1 E5), or a supervisor polling its workers
	// would silently clear the human's attention badge.
	TypeSeen = "seen"
	// TypeWait / TypeWaited: park until a session reaches one of the named
	// lifecycle states (spec §8.1). Server-owned and event-driven — the relay
	// resolves on a transition it already tracks, so no client polls.
	// TypeReportStatus: an agent declaring its own lifecycle state (spec §8.3).
	// Authoritative — from the first report the relay stops second-guessing that
	// session with output heuristics (E9: one status authority, never two).
	TypeReportStatus = "report_status"
	TypeWait         = "wait"
	TypeWaited       = "waited"
	TypeAttached     = "attached"

	// protocol 1.2: structured sessions (transport "acp")
	TypeSessionUpdate      = "session_update"
	TypePermissionRequest  = "permission_request"
	TypePermissionResponse = "permission_response"
	TypePrompt             = "prompt"
)

Message type discriminators.

View Source
const (
	TransportPTY = "pty"
	TransportACP = "acp"
)

Transports.

View Source
const (
	OutcomeApprove       = "approve"
	OutcomeReject        = "reject"
	OutcomeApproveAlways = "approve_always" // session-scoped only; never persisted
)

Permission outcomes for permission_response.

View Source
const (
	ErrAuthFailed   = "auth_failed"
	ErrUnknownAgent = "unknown_agent"
	ErrSpawnFailed  = "spawn_failed"
	// ErrResumeUnsupported: a spawn asked to reopen an agent's own past
	// conversation, but this agent has no recorded resume invocation. Refused
	// rather than downgraded to a fresh session, so the caller learns the
	// conversation is not coming back instead of silently losing it.
	ErrResumeUnsupported = "resume_unsupported"
	// ErrBadWait: a wait named no states, or named one outside the lifecycle
	// vocabulary. Refused rather than silently narrowed — a wait that can never
	// resolve is indistinguishable from a hang.
	ErrBadWait = "bad_wait"
	// ErrBadStatus: a self-report named a state outside the lifecycle vocabulary,
	// or one a session may not declare about itself.
	ErrBadStatus = "bad_status"
	// ErrSessionBlocked: the session is waiting on a human decision, so a prompt
	// was refused and NOTHING was sent (spec §8.2.1 E6). An approval dialog reads
	// the next input as its answer, so a prompt here would approve or reject a
	// tool call its sender never saw. Inspect the pending request and answer it
	// deliberately instead.
	ErrSessionBlocked = "session_blocked"
	// ErrSessionTaken tells a session's previous subscriber that another client
	// attached: its token is void and live frames now go to the newcomer.
	ErrSessionTaken = "session_taken"
	ErrInvalidToken = "invalid_token"
)

Error codes. The set is open-ended (clients must tolerate unknown codes); these are the v1.0 named catalog from protocol.md §7.

View Source
const (
	SignalKill      = "kill"
	SignalInterrupt = "interrupt"
	SignalResize    = "resize"
)

Signal kinds.

View Source
const (
	EventRunning      = "running"
	EventUnknown      = "unknown"
	EventExited       = "exited"
	EventIdle         = "idle"
	EventNeedsInput   = "needs_input"
	EventChildSpawned = "child_spawned"
	EventRateLimited  = "rate_limited" // protocol 1.1: generic provider rate-limit signal
	EventStalled      = "stalled"      // recent output keeps repeating — likely stuck in a loop
	// EventDone: the agent finished and no one has looked yet. A `seen` frame
	// from a client that actually showed it to a human demotes it to idle.
	// Reading a session over the protocol must never demote it (spec §8.1.1 E5).
	EventDone = "done"
)

Session events.

View Source
const (
	StatusRunning     = "running"
	StatusIdle        = "idle"
	StatusDone        = "done"
	StatusNeedsInput  = "needs_input"
	StatusStalled     = "stalled"
	StatusRateLimited = "rate_limited"
	StatusExited      = "exited"
	// StatusUnknown: an agent is present but its state cannot be classified.
	// Never a claim of completion, and it never satisfies a wait unless the
	// caller named it (spec §8.1.1 E3).
	StatusUnknown = "unknown"
)

Session lifecycle statuses. The relay tracks these server-side so `wait` has something to resolve against; they are the same vocabulary the events carry.

View Source
const Version = "1.3"

Version is the protocol version this relay speaks.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attach

type Attach struct {
	Type      string `json:"type"`
	SessionID string `json:"session_id"`
}

Attach (browser -> relay) re-attaches a registered client to an existing session.

type Attached

type Attached struct {
	Type         string `json:"type"`
	SessionID    string `json:"session_id"`
	SessionToken string `json:"session_token"`
	Agent        string `json:"agent"`
	StartedAt    string `json:"started_at"`
	PID          int    `json:"pid"`
}

Attached (relay -> browser) confirms re-attach and issues a fresh session token. The relay then replays the session's buffered output and resumes live streaming.

type Envelope

type Envelope struct {
	Type      string `json:"type"`
	SessionID string `json:"session_id,omitempty"`
}

Envelope peeks at a message's discriminator before full decoding.

type Error

type Error struct {
	Type      string `json:"type"`
	SessionID string `json:"session_id,omitempty"`
	Code      string `json:"code"`
	Message   string `json:"message"`
	WaitID    string `json:"wait_id,omitempty"`
	TurnID    uint64 `json:"turn_id,omitempty"`
	RPCCode   *int   `json:"rpc_code,omitempty"`
}

func NewError

func NewError(sessionID, code, message string) Error

NewError builds an Error message.

type Event

type Event struct {
	Type           string `json:"type"`
	SessionID      string `json:"session_id"`
	Event          string `json:"event"`
	ExitCode       *int   `json:"exit_code,omitempty"`
	ChildSessionID string `json:"child_session_id,omitempty"`
	At             string `json:"at"`
}

type Hello

type Hello struct {
	Type            string              `json:"type"`
	ProtocolVersion string              `json:"protocol_version"`
	RelayVersion    string              `json:"relay_version"`
	RelayName       string              `json:"relay_name"`
	HostOS          string              `json:"host_os"`
	HostArch        string              `json:"host_arch"`
	Agents          []string            `json:"agents"`
	Transports      []string            `json:"transports"`
	HostsChildren   bool                `json:"hosts_children"`
	AgentTransports map[string][]string `json:"agent_transports,omitempty"` // protocol 1.2
	// ResumeAgents lists the agent ids this relay can restart into one of their
	// own past conversations (spawn.resume_agent_session). Absent/empty ⇒ none.
	ResumeAgents []string `json:"resume_agents,omitempty"`
}

type Input

type Input struct {
	Type         string `json:"type"`
	SessionID    string `json:"session_id"`
	SessionToken string `json:"session_token"`
	Data         string `json:"data"`
}

type Output

type Output struct {
	Type      string `json:"type"`
	SessionID string `json:"session_id"`
	Data      string `json:"data"` // base64-encoded raw PTY bytes
	Seq       int    `json:"seq"`
}

type PermissionRequest

type PermissionRequest struct {
	Type      string          `json:"type"`
	SessionID string          `json:"session_id"`
	RequestID string          `json:"request_id"`
	Seq       int             `json:"seq"`
	Acp       json.RawMessage `json:"acp"`
}

PermissionRequest (relay -> browser) surfaces an ACP agent asking to proceed. RequestID is relay-correlated; echo it in PermissionResponse.

type PermissionResponse

type PermissionResponse struct {
	Type         string `json:"type"`
	SessionID    string `json:"session_id"`
	SessionToken string `json:"session_token"`
	RequestID    string `json:"request_id"`
	Outcome      string `json:"outcome"`
	OptionID     string `json:"option_id,omitempty"` // explicit ACP option id when known
}

PermissionResponse (browser -> relay) answers a PermissionRequest. ApproveAlways is session-scoped only — never persisted across sessions.

type Prompt

type Prompt struct {
	Type         string `json:"type"`
	SessionID    string `json:"session_id"`
	SessionToken string `json:"session_token"`
	Text         string `json:"text"`
	// Wait arms a wait in the SAME frame as the prompt (spec §8.2), so a
	// supervisor cannot miss a transition that happens between a separate prompt
	// and wait. The relay arms it before dispatching the prompt.
	Wait *WaitSpec `json:"wait,omitempty"`
}

Prompt (browser -> relay) prompts a structured session; the structured analogue of Input. Input stays PTY-only.

type Register

type Register struct {
	Type              string `json:"type"`
	RegistrationToken string `json:"registration_token"`
}

type Registered

type Registered struct {
	Type string `json:"type"`
}

type ReportStatus

type ReportStatus struct {
	Type         string `json:"type"`
	SessionID    string `json:"session_id"`
	SessionToken string `json:"session_token"`
	State        string `json:"state"`
	Message      string `json:"message,omitempty"`
}

ReportStatus (agent -> relay) declares the session's own lifecycle state. More reliable than the relay's LooksLike* heuristics, which is the point: an agent knows whether it is working, and guessing from output has false-positives. Message is display-only and never drives a wait.

type Resume

type Resume struct {
	Type         string `json:"type"`
	SessionID    string `json:"session_id"`
	SessionToken string `json:"session_token"`
	LastSeq      int    `json:"last_seq"`
}

type ResumeFailed

type ResumeFailed struct {
	Type      string `json:"type"`
	SessionID string `json:"session_id"`
}

type Seen

type Seen struct {
	Type         string `json:"type"`
	SessionID    string `json:"session_id"`
	SessionToken string `json:"session_token"`
}

Seen (client -> relay) marks a session as looked-at by a human.

type SessionInfo

type SessionInfo struct {
	SessionID       string `json:"session_id"`
	Agent           string `json:"agent"`
	StartedAt       string `json:"started_at"`
	PID             int    `json:"pid"`
	Transport       string `json:"transport,omitempty"`         // protocol 1.2; absent ⇒ pty (so re-attach doesn't guess)
	ParentSessionID string `json:"parent_session_id,omitempty"` // protocol 1.3; present for a child (re-attach rebuilds the tree)
	AgentSessionID  string `json:"agent_session_id,omitempty"`  // the agent's OWN session reference; feed back as spawn.resume_agent_session
	Status          string `json:"status,omitempty"`            // the relay's tracked lifecycle status, so a re-attach restores done/idle instead of assuming running
}

SessionInfo describes one live session in a Sessions list.

type SessionUpdate

type SessionUpdate struct {
	Type      string          `json:"type"`
	SessionID string          `json:"session_id"`
	Seq       int             `json:"seq"`
	Acp       json.RawMessage `json:"acp"`
}

SessionUpdate (relay -> browser) wraps ONE ACP agent->client message, verbatim, in Acp. The relay never interprets the payload.

type Sessions

type Sessions struct {
	Type     string        `json:"type"`
	Sessions []SessionInfo `json:"sessions"`
}

Sessions (relay -> browser) lists live sessions, sent right after `registered`.

type Signal

type Signal struct {
	Type         string `json:"type"`
	SessionID    string `json:"session_id"`
	SessionToken string `json:"session_token"`
	Signal       string `json:"signal"`
	Cols         int    `json:"cols,omitempty"`
	Rows         int    `json:"rows,omitempty"`
	Subtree      bool   `json:"subtree,omitempty"` // protocol 1.3; kill only — also kill descendants (leaf-first)
}

type Spawn

type Spawn struct {
	Type            string            `json:"type"`
	Agent           string            `json:"agent"`
	Cwd             string            `json:"cwd"`
	Args            []string          `json:"args"`
	Env             map[string]string `json:"env"`
	ClientID        string            `json:"client_id"`
	Transport       string            `json:"transport,omitempty"`         // protocol 1.2; absent ⇒ pty
	ParentSessionID string            `json:"parent_session_id,omitempty"` // protocol 1.3; spawn as a child (supervisor tree)
	// ResumeAgentSession asks the agent to reopen one of ITS OWN past
	// conversations, identified by a reference the agent itself issued. Distinct
	// from the `resume` frame, which re-attaches this browser to a live relay
	// session. The relay appends the agent's recorded resume argv; it never
	// guesses a flag, and refuses when the agent has none.
	ResumeAgentSession string `json:"resume_agent_session,omitempty"`
}

type Spawned

type Spawned struct {
	Type            string `json:"type"`
	SessionID       string `json:"session_id"`
	ClientID        string `json:"client_id"`
	SessionToken    string `json:"session_token"`
	Agent           string `json:"agent"`
	PID             int    `json:"pid"`
	StartedAt       string `json:"started_at"`
	ParentSessionID string `json:"parent_session_id,omitempty"` // protocol 1.3; set for a child session
	// AgentSessionID is the agent's OWN session reference, when it issued one
	// (structured sessions do at session/new). Persist it: handing it back as
	// spawn.resume_agent_session is what reopens this conversation later.
	AgentSessionID string `json:"agent_session_id,omitempty"`
}

type Wait

type Wait struct {
	Type         string   `json:"type"`
	SessionID    string   `json:"session_id"`
	SessionToken string   `json:"session_token"`
	Until        []string `json:"until"`
	TimeoutMS    int      `json:"timeout_ms,omitempty"`
	// WaitID is echoed back, so one client can hold several waits on one session.
	WaitID string `json:"wait_id,omitempty"`
}

Wait (client -> relay) parks until the named session reaches one of `Until`. A wait whose condition is ALREADY true resolves immediately (§8.1.1 E1) — otherwise a supervisor that misses a transition by a millisecond parks until timeout on something that already happened.

type WaitSpec

type WaitSpec struct {
	Until     []string `json:"until"`
	TimeoutMS int      `json:"timeout_ms,omitempty"`
	WaitID    string   `json:"wait_id,omitempty"`
}

WaitSpec is the wait half of an atomic prompt+wait.

type Waited

type Waited struct {
	Type      string `json:"type"`
	SessionID string `json:"session_id"`
	WaitID    string `json:"wait_id,omitempty"`
	State     string `json:"state"`
	TimedOut  bool   `json:"timed_out"`
	// Brokered marks a wait that some client composed on the relay's behalf
	// rather than one this relay owns. Always false from a relay; a broker sets
	// it so a supervisor knows the wait dies with the broker instead of assuming
	// the durability a relay-owned wait has.
	Brokered bool `json:"brokered,omitempty"`
}

Waited (relay -> client) resolves a Wait.

Jump to

Keyboard shortcuts

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