control

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 16 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 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

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

View Source
const EnvSocket = "CODEX_ROUTER_CONTROL"

EnvSocket names the environment variable the launcher sets for the hook and the TUI.

View Source
const Protocol = 2

Protocol is the control protocol version a client names in its hello.

View Source
const PublishInterval = 50 * time.Millisecond

PublishInterval is the minimum gap between pushes per client (20 per second).

View Source
const SocketMode os.FileMode = 0o600

SocketMode is the permission the socket is created with.

View Source
const SocketName = "control.sock"

SocketName is the control socket file inside the runtime dir.

Variables

View Source
var ErrDisconnected = errors.New("control socket disconnected")

ErrDisconnected is returned by Send and Subscribe while the socket is down.

Functions

func SessionID added in v0.3.0

func SessionID(homeID, threadID string) string

SessionID is the wire id of a session: "<home id>/<thread id>", the same as route.SessionInfo.ID.

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

func Dial(path string) (*Client, error)

Dial connects to the socket at path and does the hello handshake. The first connect must succeed.

func (*Client) Close

func (c *Client) Close() error

Close stops the client and closes Sessions and Snapshots.

func (*Client) Hello added in v0.3.0

func (c *Client) Hello() Hello

Hello is the endpoint's identity from the handshake.

func (*Client) Send

func (c *Client) Send(cmd route.Command) error

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

func (c *Client) Snapshots() <-chan route.Snapshot

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.

func (*Client) Subscribe added in v0.3.0

func (c *Client) Subscribe(session string) error

Subscribe switches the connection to session's snapshots ("" for none) and waits for the ack. Snapshots queued for the old session are dropped; the new session's current snapshot is queued before Subscribe returns.

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

type Request struct {
	route.Command
	Protocol int `json:"protocol,omitempty"`
}

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

func Listen(path string, h Handler) (*Server, error)

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.

func (*Server) Close

func (s *Server) Close() error

Close stops accepting, drops all clients and removes the socket file.

func (*Server) Publish

func (s *Server) Publish(snap route.Snapshot)

Publish records a changed thread's snapshot for its subscribers and schedules a sessions list check. Bursts coalesce: a client gets the newest state at most once per PublishInterval. A snapshot older than the one held lost a publish race and is dropped.

Jump to

Keyboard shortcuts

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