ws

package
v0.0.0-...-628f028 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MPL-2.0 Imports: 8 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServeWS

func ServeWS(hub *Hub, w http.ResponseWriter, r *http.Request)

ServeWS upgrades an HTTP connection to WebSocket, creates a Client, starts its read/write pumps, and registers it with the hub.

Types

type Client

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

Client represents a single WebSocket connection managed by the Hub.

type ClientFrame

type ClientFrame struct {
	Type string `json:"type"` // only "ping" handled in M0
}

ClientFrame is the only accepted client-to-server frame shape.

type EventType

type EventType string
const (
	EventHello        EventType = "hello"
	EventBeadCreated  EventType = "bead.created"
	EventBeadUpdated  EventType = "bead.updated"
	EventBeadMoved    EventType = "bead.moved"
	EventBeadDeleted  EventType = "bead.deleted"
	EventCommentAdded EventType = "comment.added"
	EventPong         EventType = "pong"

	// M2 additions (additive; M1 events unchanged — FR-019).
	EventRunlogLine EventType = "runlog.line"         // agent pane output
	EventTmuxOpened EventType = "tmux.session.opened" // session spawned
	EventTmuxClosed EventType = "tmux.session.closed" // session ended

	// M4 additions (additive; M0–M3 events unchanged).
	EventDispatchQueued EventType = "dispatch.queued" // bead enqueued waiting for scheduler capacity

	// EventDispatchAdmitted signals that the bead's queued run has been ADMITTED
	// from the scheduler queue and its async launch is STARTING. It does NOT
	// guarantee the launch succeeded — a subsequent run.failed event reports any
	// async launch failure.
	EventDispatchAdmitted EventType = "dispatch.admitted"

	// EventStepAdvanced signals that the operator's Advance request was ACCEPTED
	// and the next step's async launch is STARTING. It does NOT guarantee the
	// relaunch succeeded — a subsequent run.failed event reports any failure.
	EventStepAdvanced EventType = "step.advanced"

	// EventStepLoopedBack signals that the operator's LoopBack request was ACCEPTED
	// and the targeted step's async relaunch is STARTING. It does NOT guarantee
	// the relaunch succeeded — a subsequent run.failed event reports any failure.
	EventStepLoopedBack EventType = "step.loopedback"

	EventWorktreeFinalized EventType = "worktree.finalized" // agent worktree committed (finalize complete)
	EventWorktreePushed    EventType = "worktree.pushed"    // agent worktree branch pushed to remote
	EventWorktreeRemoved   EventType = "worktree.removed"   // agent worktree directory removed
	EventRunQuota          EventType = "run.quota"          // token/cost quota captured at run end

	// EventRunFailed is emitted when an accepted run fails to LAUNCH asynchronously
	// (queued-run admission launch failure, or step-transition relaunch failure).
	// It is distinct from tmux.session.closed, which reports a session that
	// launched successfully and then exited. Clients that observed the preceding
	// dispatch.admitted / step.advanced / step.loopedback event (signalling the
	// transition/admission was ACCEPTED and launch was starting) should use this
	// event to learn that the launch ultimately failed.
	EventRunFailed EventType = "run.failed"

	// M6 additions (additive; M0–M5 events unchanged — Principle V).
	// EventConstitutionChanged is emitted after a successful PUT /api/v1/constitution;
	// it carries the new monotonic Version. The next dispatch (not any running step)
	// picks up the new constitution.
	EventConstitutionChanged EventType = "constitution.changed"

	// EventRunlogWarning is a non-blocking warning tied to a bead/step — e.g. an
	// unresolvable skill id or an MCP server named by a skill that is absent from
	// the agent's own config. Unlike runlog.line, which is deliberately dropped at
	// INGRESS under backpressure, a warning is not dropped at ingress. (The WS hub
	// may still drop ANY frame to an individual slow client during fan-out — see
	// internal/ws/hub.go; this event only avoids the runlog.line ingress drop.)
	EventRunlogWarning EventType = "runlog.warning"
)

type Frame

type Frame struct {
	Type EventType `json:"type"`

	// hello fields
	Build         string `json:"build,omitempty"`
	SchemaVersion int    `json:"schemaVersion,omitempty"`
	ServerTime    string `json:"serverTime,omitempty"`
	BeadsVersion  string `json:"beadsVersion,omitempty"`

	// bead.created / bead.updated / bead.moved / comment.added
	Bead *core.Bead `json:"bead,omitempty"`

	// bead.moved / bead.deleted / comment.added
	ID string `json:"id,omitempty"`

	// bead.moved
	FromColumn core.Column `json:"fromColumn,omitempty"`
	ToColumn   core.Column `json:"toColumn,omitempty"`
	BeforeID   string      `json:"beforeID,omitempty"`

	// comment.added
	Event *core.HistoryEvent `json:"event,omitempty"`

	// pong
	At string `json:"at,omitempty"`

	// M2: runlog.line — agent pane output (raw bytes; ANSI preserved)
	BeadID  string `json:"beadID,omitempty"`
	StepIdx *int   `json:"stepIdx,omitempty"` // *int so the valid M2 value 0 isn't dropped by omitempty (M1 frames leave it nil)
	Seq     uint64 `json:"seq,omitempty"`     // set on every runlog.line frame, omitted elsewhere. seq is 1-based (runlogStreamer uses Add(1)), so it is never legitimately 0 — a plain value with omitempty drops it on non-runlog frames just like a pointer would, without the per-frame heap allocation a *uint64 forces on this hot path.
	Data    string `json:"data,omitempty"`    // base64-encoded raw pane bytes (terminal output is not guaranteed UTF-8)

	// M2: tmux.session.opened / tmux.session.closed
	Session  string `json:"session,omitempty"`
	ExitCode *int   `json:"exitCode,omitempty"` // on closed; nil on opened

	// M4: dispatch.queued / dispatch.admitted
	WaitingPos *int `json:"waitingPos,omitempty"` // 0-based FIFO position; dispatch.queued only

	// M4: step.advanced / step.loopedback
	ChainLen *int `json:"chainLen,omitempty"` // total number of steps in the chain

	// M4: worktree.finalized — whether a commit was actually created.
	// *bool (pointer) so false is not silently dropped by omitempty; nil on all
	// other frame types (same rationale as StepIdx/*int above).
	Committed *bool `json:"committed,omitempty"`

	// M4: worktree.pushed — the branch name and remote that were pushed to.
	Branch string `json:"branch,omitempty"`
	Remote string `json:"remote,omitempty"`

	// M4 US5: run.quota — best-effort token/cost record captured at run end.
	// Non-nil only on run.quota frames; nil on all other frame types.
	Quota *QuotaPayload `json:"quota,omitempty"`

	// run.failed — human-readable reason the launch failed (err.Error()).
	// runlog.warning also reuses Reason for the warning message.
	// Non-empty only on those frames; omitted on all other frame types.
	Reason string `json:"reason,omitempty"`

	// M6: constitution.changed — the new monotonic constitution version.
	// *int (pointer) so version 0 is not dropped by omitempty; nil on all other
	// frame types (same rationale as StepIdx/*int above).
	Version *int `json:"version,omitempty"`
}

Frame is the server-to-client event envelope. Each event type populates a subset of fields; all others are zero (omitted from JSON).

type Hub

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

Hub maintains the set of active clients and fans out broadcast frames.

func NewHub

func NewHub(beadsVersion string) *Hub

NewHub constructs a Hub. beadsVersion is injected from seed data and included in the hello frame sent to each connecting client.

func (*Hub) Broadcast

func (h *Hub) Broadcast(f Frame)

Broadcast enqueues f for fan-out to all connected clients.

Frame handling is deliberately split by type:

  • runlog.line is high-volume, best-effort pane output produced by the tmux transport-reader goroutine. A blocking send there could stall the reader and, via the tmux FIFO, apply backpressure to the agent itself. So these are enqueued non-blocking: if the ingress buffer is full the frame is dropped (and counted). Clients recover scrollback via capture-pane.
  • every other type is a low-volume lifecycle/state event (bead.updated, tmux.session.closed, …) where a dropped frame would leave clients stale. These use a blocking send so they are never dropped at the *ingress* buffer (unlike runlog). Because runlog frames never queue past the buffer, the buffer drains continuously and a lifecycle send almost always finds room immediately — and its producers are never the transport reader, so a brief block can't stall the agent.

This ingress guarantee is NOT end-to-end delivery: Run's per-client fan-out still drops to any individual client whose send buffer is full (select default), and unregisters a persistently-slow client. So a slow client can still miss lifecycle frames and must recover via re-fetch; the blocking send only removes the *shared* ingress buffer as a drop point for those frames.

func (*Hub) DroppedFrames

func (h *Hub) DroppedFrames() uint64

DroppedFrames returns the number of frames dropped by Broadcast because the ingress buffer was full. Exposed for observability (e.g. status/metrics).

func (*Hub) Run

func (h *Hub) Run()

Run is the hub's main event loop. It must be started in a goroutine.

type QuotaPayload

type QuotaPayload struct {
	Known        bool    `json:"known"`
	InputTokens  int64   `json:"inputTokens"`
	OutputTokens int64   `json:"outputTokens"`
	CostUSD      float64 `json:"costUSD"`
}

QuotaPayload is the run.quota event body. Known is false when no on-disk session record was found (best-effort advisory). CostUSD is 0 for interactive sessions (claude does not write costUSD to JSONL for non -p runs; see spike R8 in specs/005-m4-dispatcher/research.md).

Jump to

Keyboard shortcuts

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