hostserve

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 27 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 hostserve is the headless half of Sidecar's remote-host support: the loop behind `sidecar host serve --stdio`.

It is orchestration, not new behaviour. The awareness stack — workspaceinventory, agentactivity, agentstatus, shellliveness, tmuxserver — has no Bubble Tea dependency and takes injectable runners, captures, and clocks, so the whole of it runs headlessly with no reimplementation. What this package adds is the cycle that the Overview model contributes locally: the identity/inventory/status phase order, the shell-claim pass between them, the adaptive poll cadence, and tracker commit at the end of a completed generation.

What serve is allowed to touch

Serve does not take a geometry lease, does not resize a pane, and issues no mutating tmux command at all. It has two writes besides observation: the shell reap, which tombstones a manifest record whose tmux session is confirmed gone, and — when SIDECAR_VIEWER_INSTANCE is set — an ephemeral presence file under stateDir/viewers/. The reap goes through workspaceops/shellstate — the same flocked, conditional, tombstoning writer the Sessions browser calls — and its whole decision is shellliveness.PlanReap/ConfirmReap/ReapShell, shared with the browser rather than restated here. See reap.go. Presence files are isolation-gated and are not shells.json. Serve does not write ack files and does not apply a UI request.

Be precise about what kind of guarantee the rest is, because overstating it is how it quietly stops being true. It is NOT "the package links in no writer": hostserve depends on internal/tty, which contains resize-window, send-keys, kill-session and the geometry lease. The guarantee is call-graph discipline — the only tty function reached from here is CapturePaneWithState, and the only subprocesses this package can cause are `tmux list-panes`, `tmux capture-pane`, `tmux list-sessions`, `tmux display-message`, git, and ps.

TestServeIsReadOnly enforces that by asserting on the commands actually issued, which is the level the guarantee actually holds at. Anyone adding a call into tty from this package is responsible for keeping it true; the shells-wipe incident (td-8d18de) is what the care is for, and it is why the reap write serve does have carries none of its own logic.

The one place that discipline needs care is the capture path, because a capture is an observation that can have a side effect: the Overview's semantic preview resizes a pane under a geometry lease before capturing it. Serve never calls that path. It captures at whatever size the pane already is, exactly as the ordinary status pass does.

Index

Constants

View Source
const (
	DefaultLivePoll  = 5 * time.Second
	DefaultReadyPoll = 10 * time.Second
	DefaultIdlePoll  = 30 * time.Second

	// DefaultInventoryEvery is how often the expensive phase — git worktree
	// listing and state-tree reads — runs. In between, only tmux evidence is
	// refreshed, which is the same split RefreshProjectStatus exists to make.
	DefaultInventoryEvery = 60 * time.Second

	// DefaultPreviewLines matches the status pass's own capture depth, so
	// shipping a preview costs no extra capture at all: serve reuses the text
	// the status pass already took.
	DefaultPreviewLines = 80

	// DefaultPreviewBytes bounds one preview on the wire. The local capture
	// discipline is a byte cap (plugins.workspace.tmuxCaptureMaxBytes); this
	// is the same idea applied per row before transmission.
	DefaultPreviewBytes = 16 << 10

	// DefaultSnapshotPreviewBytes bounds the previews in ONE snapshot, which
	// the per-row cap does not: a host with enough agent panes would otherwise
	// build a message past hostproto.MaxLineBytes, and a viewer whose scanner
	// hits that limit is dead for the rest of the connection with no resync.
	// Rows past the budget ship without a preview rather than not at all.
	DefaultSnapshotPreviewBytes = 1 << 20

	// DefaultMaxCaptures bounds concurrent capture-pane subprocesses, the same
	// bound the Overview applies for the same reason: an unbounded fan-out
	// over a machine's panes is felt by the human sitting at that machine.
	DefaultMaxCaptures = 4
)

Poll cadences. These are the Overview's numbers and the comment explaining them is the Overview's reasoning: an idle agent is not inert, it is an agent at a prompt that can start a turn at any moment, so only a host with nothing live at all earns the quiet cadence.

View Source
const ViewerCapabilityUIRequestRelayV1 = viewerCapabilityUIRequestRelayV1

ViewerCapabilityUIRequestRelayV1 is the presence capability the host CLI looks for before waiting for a relayed ack.

Variables

This section is empty.

Functions

func Serve

func Serve(ctx context.Context, opts Options) error

Serve runs the collection loop until ctx is cancelled, the configured cycle budget is exhausted, or the output pipe dies. A dead pipe is a normal ending: it means the viewer disconnected and this ephemeral process has no further reason to exist.

Types

type Options

type Options struct {
	// Out receives the JSONL stream. In production this is os.Stdout, which is
	// the ssh pipe.
	Out io.Writer

	// Projects is what to observe. Empty means the loop still runs and still
	// emits a hello and empty snapshots, which is the correct behaviour for a
	// host that has sidecar installed but no projects configured — a viewer
	// must be able to tell that apart from an unreachable host.
	Projects []Project

	// HostID scopes every emitted item ID. A viewer keying rows on the
	// collector's ID alone would collide two hosts that happen to agree.
	HostID string

	Now     func() time.Time
	Runner  workspaceinventory.Runner
	Capture workspaceinventory.CaptureFunc

	// ServerIncarnation reads the tmux server's identity. Injectable so a test
	// can drive a server death without killing one.
	ServerIncarnation func() tmuxserver.Incarnation

	// Namespace, ProbeShell and ForgetShell are the reap's three seams. They
	// are injectable for the same reason the rest of this struct is: a test
	// that had to spawn a real tmux to exercise a reap would be a test nobody
	// runs, and this is the one path in the package that writes.
	//
	// Namespace reports which tmux server this host's shell records belong to;
	// a record in another namespace is invisible to the pane listing and is
	// never judged. ProbeShell is the independent second opinion. ForgetShell
	// writes the tombstone — workspaceops.ReapManagedShellFunc in production,
	// which is the browser's writer, not a second one.
	Namespace   func() string
	ProbeShell  shellliveness.ProbeFunc
	ForgetShell shellliveness.ForgetFunc

	// LeaseOwner returns the geometry-lease instance ID for a tmux session,
	// or "". Tests inject it; production reads @sidecar-owner via tmux.
	LeaseOwner func(session string) string

	// Hostname, TmuxPath and TmuxVersion feed the hello. Injectable for the
	// same reason.
	Hostname    func() string
	TmuxPath    func() (string, bool)
	TmuxVersion func() string

	LivePoll       time.Duration
	ReadyPoll      time.Duration
	IdlePoll       time.Duration
	InventoryEvery time.Duration

	PreviewLines int
	PreviewBytes int
	// SnapshotPreviewBytes bounds the total preview payload in one snapshot.
	SnapshotPreviewBytes int
	MaxCaptures          int

	// NotifyDebounce is how long a lane must hold before it counts as a real
	// transition worth forwarding. Zero uses notify.DefaultLaneDebounce, which
	// is the same window the local surfaces settle on, so a remote agent and a
	// local one have to hold a state for equally long to be worth saying.
	NotifyDebounce time.Duration

	// Cycles bounds how many collection cycles run before Serve returns. Zero
	// means run until the context is cancelled. A measurement harness sets it
	// so a run terminates on its own.
	Cycles int

	// OnCycle is called after each completed cycle with its wall duration. It
	// exists for the Phase 0 measurement harness; production leaves it nil.
	OnCycle func(generation uint64, elapsed time.Duration, captures int64)
}

Options configures a serve loop. Every field has a working default, and the injectable ones exist so tests can drive the whole loop with no tmux, no filesystem, and a fake clock.

type Project

type Project struct {
	Name string
	Path string
}

Project names one project the host should observe. It mirrors the remote machine's own config `projects.list` entries; discovery is unchanged.

type ViewerPresence added in v1.12.0

type ViewerPresence = viewerPresence

ViewerPresence is the exported form of a live viewer presence record.

func LookupLiveViewer added in v1.12.0

func LookupLiveViewer(stateDir, instance string, now time.Time) (ViewerPresence, bool)

LookupLiveViewer reads stateDir/viewers/<instance>.json and reports whether it is still unexpired at now. Isolation-gated; a missing or expired file is not an error, just not live.

func (ViewerPresence) HasCapability added in v1.12.0

func (p ViewerPresence) HasCapability(name string) bool

Jump to

Keyboard shortcuts

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