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 ¶
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 (*Bus) Publish ¶
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
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 ¶
Subscribers returns the current subscriber count.
type Frame ¶
type Frame struct {
Type FrameType `json:"type"`
Seq uint64 `json:"seq,omitempty"`
Data any `json:"data"`
// LocalOnly frames go to this machine's own /v1/live clients and to no
// paired device: what another computer said (ADR-067, Phase 2) must not
// reach a phone paired with this one, or access would be transitive.
LocalOnly bool `json:"-"`
}
Frame is what goes over /v1/live. Seq is set by Publish: one more than the frame before it.
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" // FrameContinued says a session was continued on another provider or // sign-in (api.ContinueOnResult): by a button, or by Caprock itself on // a usage limit (auto true) — the app's toast, with Undo. FrameContinued FrameType = "continued" // FrameJarvis is Jarvis's news (03-contracts.md § Jarvis): {op: // "handled", item} when Caprock did something on its own (ADR-096); // {op: "thread", message}, a thread message new or settled (a pending // reply comes again with the same id), and {op: "brain", brain} when the // brain's state changed (ADR-097). FrameJarvis FrameType = "jarvis" // 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" // FrameMachines says something changed on one of the computers this // machine reaches (ADR-067, Phase 2): {id, why} — a prompt began or // ended there, a notification arrived, or its channel came up or went // down — so a screen listing their sessions reads GET // /v1/machines/sessions again. Local only. FrameMachines FrameType = "machines" )
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.