bus

package
v0.151.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 4 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 bus is the in-process fan-out for live frames: every stored event, session change, or alert is published once and delivered to every subscriber (WebSocket clients, the loop detector, tests). Slow subscribers are dropped rather than allowed to stall the writer — the UI can always catch up via REST.

Every published frame is numbered (Seq) and kept in a replay ring, so a /v1/live client that reconnects gets exactly what it missed (.ai/21-app.md § Live replay).

Index

Constants

View Source
const (
	// ReplayFrames is the most frames the replay ring holds.
	ReplayFrames = 2000
	// ReplayAge is how long a frame stays in the replay ring.
	ReplayAge = 10 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus struct {
	// contains filtered or unexported fields
}

Bus fans frames out to subscribers.

func New

func New() *Bus

New creates a bus.

func (*Bus) Publish

func (b *Bus) Publish(f Frame)

Publish numbers a frame, keeps it for replay, and delivers it to every subscriber without blocking. Numbering and delivery share one lock, so every subscriber receives frames in seq order.

func (*Bus) Resume added in v0.76.0

func (b *Bus) Resume(buffer int, since *uint64) (s *Subscriber, missed []Frame, seq uint64, ok bool)

Resume subscribes and, in the same step, reports where the subscriber starts. With since nil it starts at the newest frame (seq). With since set, missed is every frame after since, in order, and ok is false when the ring no longer holds them (or never did) — the client must refetch. Frames on the subscriber's channel always come after seq.

func (*Bus) Since added in v0.76.0

func (b *Bus) Since(since uint64) (frames []Frame, seq uint64, ok bool)

Since returns the frames after since, in order, and the newest seq; ok is false when the ring does not hold all of them.

func (*Bus) Subscribe

func (b *Bus) Subscribe(buffer int) *Subscriber

Subscribe registers a subscriber with the given buffer size.

func (*Bus) Subscribers

func (b *Bus) Subscribers() int

Subscribers returns the current subscriber count.

type Frame

type Frame struct {
	Type FrameType `json:"type"`
	Seq  uint64    `json:"seq,omitempty"`
	Data any       `json:"data"`
}

Frame is what goes over /v1/live. Seq is set by Publish: one more than the frame before it.

func (Frame) Marshal

func (f Frame) Marshal() ([]byte, error)

Marshal encodes the frame once for all subscribers.

type FrameType

type FrameType string

FrameType matches the WS contract: event | session | alert.

const (
	FrameEvent FrameType = "event"
	// FrameEventUpdate carries an event already sent as "event", rewritten in
	// place: the same id, a new payload. Today only a reply's prose, which
	// Claude Code writes as transcript lines after the first line of the same
	// response (store.MergeAssistantText). A screen holding that id replaces
	// it; anything counting events ignores this frame.
	FrameEventUpdate FrameType = "event_update"
	FrameSession     FrameType = "session"
	FrameAlert       FrameType = "alert"
	FrameStats       FrameType = "stats" // per-session stats snapshot; UI convenience
	// FramePermission says an owned session started or stopped waiting on a
	// permission prompt: {session_id, permission}, permission null when it
	// stopped.
	FramePermission FrameType = "permission"
	// FrameNotify is a notification for the app (alerts.Notification): what
	// Telegram would say, as plain text, with the prompt's answers when there
	// is one Caprock can give.
	FrameNotify FrameType = "notify"
	// FrameResources is the resource meter's sample (daemon.ResourceReport),
	// every few seconds while a dashboard is connected.
	FrameResources FrameType = "resources"
	// FrameQueue is an owned session's message queue after any change
	// (ADR-053): {session_id, messages, waiting}.
	FrameQueue FrameType = "queue"
	// FrameInbox says the Inbox changed (03-contracts.md § Inbox): {id,
	// deleted}, so a screen showing it reads it again.
	FrameInbox FrameType = "inbox"
	// FrameBrain says Second Brain changed (03-contracts.md § Second Brain):
	// {id, op: "added"|"changed"|"deleted", kind?, title?, auto?}, so a
	// screen can drop a new memory in as it arrives; or {op: "progress"}
	// when an automatic read begins or ends, so the build's status is read
	// again.
	FrameBrain FrameType = "brain"
	// FrameStatus says an owned session's status moved without an event —
	// input typed into it that it has not answered yet (narrate.Nudge):
	// {session_id, kind, at}. A screen asks for the session again at once.
	FrameStatus FrameType = "status"
	// FrameDevices says the paired devices changed — one paired, removed,
	// given a role, or a computer waiting to be allowed (ADR-067). It carries
	// nothing; the machine's own dashboard re-reads GET /v1/pair/state.
	FrameDevices FrameType = "devices"
	// FrameFootprint is GET /v1/footprint's answer without the orphan list
	// (daemon.FootprintReport), sent when it changed, at most every 10 s,
	// while a /v1/live client is connected (03-contracts.md § Footprint).
	FrameFootprint FrameType = "footprint"
	// FrameShells is every running shell, {shells: [Shell]} as GET
	// /v1/shells answers it, sent when the list changed (03-contracts.md
	// § Projects and shells).
	FrameShells FrameType = "shells"
	// FrameAdmin says the admin state changed without the key changing —
	// today a private module's attention set, cleared or replaced
	// (03-contracts.md § Private modules). It carries nothing, so a viewer
	// learns nothing of a module; a dashboard that shows modules re-reads
	// GET /v1/admin.
	FrameAdmin FrameType = "admin"
)

type Subscriber

type Subscriber struct {
	C chan Frame
	// contains filtered or unexported fields
}

Subscriber receives frames on C. Close it with Unsubscribe.

func (*Subscriber) Lagged added in v0.76.0

func (s *Subscriber) Lagged() <-chan struct{}

Lagged is signalled when a frame was dropped because C was full, so a subscriber that can replay (Since) catches up even when no later frame arrives to show the gap.

func (*Subscriber) Unsubscribe

func (s *Subscriber) Unsubscribe()

Unsubscribe removes the subscriber and closes its channel.

Jump to

Keyboard shortcuts

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