Documentation
¶
Overview ¶
Package control is the router's local control channel, protocol v2: JSON lines over a 0600 unix socket. A client opens with a hello naming the protocol version and gets the endpoint's identity back. The server then pushes the sessions list on change and the snapshots of the one session the client subscribed to; commands name a session and an id, and the ack or error echoes the id. The TUI and the hook are clients. Nothing from Codex requests (headers, text) is on it. The names in a payload are the protocol, so every wire type spells its JSON names out and the fixtures in testdata/protocol-v2 pin them byte for byte, in both directions.
Index ¶
Constants ¶
const ( OpHello route.Op = "hello" OpSubscribe route.Op = "subscribe" )
Ops a client sends besides route.Command ops. Hello must be the first line; subscribe picks the one session whose snapshots the connection gets.
const EnvSocket = "CODEX_ROUTER_CONTROL"
EnvSocket names the environment variable the launcher sets for the hook and the TUI.
const Protocol = 2
Protocol is the control protocol version a client names in its hello.
const PublishInterval = 50 * time.Millisecond
PublishInterval is the minimum gap between pushes per client (20 per second).
const SocketMode os.FileMode = 0o600
SocketMode is the permission the socket is created with.
const SocketName = "control.sock"
SocketName is the control socket file inside the runtime dir.
Variables ¶
var ErrDisconnected = errors.New("control socket disconnected")
ErrDisconnected is returned by Send and Subscribe while the socket is down.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client talks to a Server. It never redials: when the socket is lost, Sessions and Snapshots close and Send returns ErrDisconnected, so the owner redials.
func Dial ¶
Dial connects to the socket at path and does the hello handshake. The first connect must succeed.
func (*Client) Send ¶
Send issues a command and waits for the reply with its id; Send sets cmd.ID. Session is a session id (SessionID). A server-side rejection comes back as an error.
func (*Client) Sessions ¶ added in v0.3.0
func (c *Client) Sessions() <-chan []route.SessionInfo
Sessions yields each new sessions list. When the consumer lags, it gets the newest list only. The channel closes on Close or when the connection is lost.
func (*Client) Snapshots ¶ added in v0.3.0
Snapshots yields the subscribed session's snapshots, never another session's and never one older than the last delivered. When the consumer lags, the oldest queued snapshot is dropped. The channel closes on Close or when the connection is lost.
type Handler ¶
type Handler interface {
Apply(c route.Command) error
Sessions() []route.SessionInfo
SnapshotFor(threadID string) route.Snapshot
Hello() Hello
}
Handler is what the server drives: the route engine plus the endpoint's identity. SnapshotFor takes a bare thread id.
type Hello ¶ added in v0.3.0
type Hello struct {
EndpointID string `json:"endpoint_id"`
Kind string `json:"kind"`
Home home.Home `json:"home"`
Label string `json:"label,omitempty"`
Version string `json:"version,omitempty"`
}
Hello identifies the endpoint behind a socket. Label is the launcher's working directory as "home · repo · branch", so a panel can name a router whose Codex has no session yet; a brain serves many directories and leaves it empty. Protocol 2 clients ignore it.
type Message ¶
type Message struct {
Type MessageType `json:"type"`
ID string `json:"id,omitempty"`
Hello *Hello `json:"hello,omitempty"`
Seq uint64 `json:"seq,omitempty"`
Items []route.SessionInfo `json:"items,omitempty"`
Session string `json:"session,omitempty"`
Snapshot *route.Snapshot `json:"snapshot,omitempty"`
Error string `json:"error,omitempty"`
}
Message is one server-to-client line. ID echoes the request's id on ack and error. Seq orders sessions lists; a client drops one not newer than the last it got.
type MessageType ¶
type MessageType string
MessageType tags a server-to-client line.
const ( MessageHello MessageType = "hello" MessageSessions MessageType = "sessions" MessageSnapshot MessageType = "snapshot" MessageAck MessageType = "ack" MessageError MessageType = "error" )
type Request ¶ added in v0.3.0
Request is one client-to-server line: a route.Command, a subscribe, or the hello. Session is a session id (SessionID), not a bare thread id.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the control socket. After a client's hello it pushes the sessions list on change and the snapshots of the one session the client subscribed to, and applies commands through the Handler. One publisher goroutine computes pushes; each client has its own writer, so a slow client delays only itself.
func Listen ¶
Listen binds the socket at path with mode 0600, removing a stale socket file and refusing a live one or a non-socket file. The parent dir must not be accessible to group or others: the mode is set right after bind, not at bind, so the dir closes that window.