serve

package
v0.1.15 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package serve runs the ctm web UI HTTP daemon (`ctm serve`).

v0.1 scope: auth-gated REST + SSE endpoints on loopback:37778, backed by an in-memory hub, a read-through sessions projection over ~/.config/ctm/sessions.json, and per-workdir git checkpoint / revert handlers.

Index

Constants

View Source
const (
	// DefaultPort is the loopback port ctm serve binds by default.
	DefaultPort = 37778

	// ServeVersionHeader is set on /healthz responses so a second
	// process can identify a live sibling daemon portably (no /proc).
	ServeVersionHeader = "X-Ctm-Serve"
)

Variables

View Source
var ErrAlreadyRunning = errors.New("ctm serve already running on this port")

ErrAlreadyRunning is returned by New when another `ctm serve` already owns the port (detected via the X-Ctm-Serve header on /healthz). Callers should treat it as silent success.

Functions

This section is empty.

Types

type Options

type Options struct {
	Port    int
	Version string

	// Token, if non-empty, is pre-seeded into the in-memory session store
	// at startup so tests can authenticate without going through signup/
	// login. Production code leaves this empty.
	Token string

	// SessionsPath overrides the path serve watches for the sessions
	// projection. Empty means config.SessionsPath(). Primarily a test
	// seam.
	SessionsPath string

	// TmuxConfPath overrides the tmux client's conf path. Empty means
	// config.TmuxConfPath(). Primarily a test seam.
	TmuxConfPath string

	// LogDir overrides the directory the JSONL tailer manager watches.
	// Empty means filepath.Join(config.Dir(), "logs"). Test seam.
	LogDir string

	// StatuslineDumpDir overrides the directory the quota ingester
	// watches for `cmd statusline` per-session JSON dumps. Empty
	// means /tmp/ctm-statusline (per design spec §4 default).
	StatuslineDumpDir string

	// WebhookURL enables the webhook dispatcher. Empty → disabled.
	// HasWebhook in /api/bootstrap is derived from this.
	WebhookURL string

	// WebhookAuth, if non-empty, is sent verbatim in the Authorization
	// header on each POST (e.g. "Bearer abc123").
	WebhookAuth string

	// AttentionThresholds overrides the built-in defaults for the
	// attention engine's seven triggers. A zero-valued Thresholds falls
	// back to attention.Defaults().
	AttentionThresholds attention.Thresholds

	// Config is the already-loaded user config, threaded through so
	// the /api/doctor handler can report on required_env /
	// required_in_path without re-reading from disk. Zero value is
	// safe — the doctor runner treats unset fields as "not
	// configured" rather than failing.
	Config config.Config
}

Options configures a Server.

type Server

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

Server is the ctm serve HTTP daemon.

func New

func New(opts Options) (*Server, error)

New binds the listener, loads the bearer token, constructs the hub and sessions projection, and wires routes. See DefaultPort, single- instance guard semantics in the package doc.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the bound listener address.

func (*Server) Hub

func (s *Server) Hub() *events.Hub

Hub returns the server's event hub. Exposed for tests and, in later steps, for ingest layers that publish events from the same process.

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

Run blocks until ctx is cancelled, then gracefully shuts down. Returns nil on clean shutdown; propagates non-ErrServerClosed errors.

func (*Server) Shutdown

func (s *Server) Shutdown(reason string)

Shutdown cancels the daemon's root context so Run(ctx) returns and callers that want to respawn (e.g. config save → restart) can do so via proc.EnsureServeRunning. Safe to call more than once; no-op if Run hasn't started or has already returned.

Directories

Path Synopsis
Package api implements the JSON HTTP handlers mounted under /api by internal/serve.Server.
Package api implements the JSON HTTP handlers mounted under /api by internal/serve.Server.
Package attention implements the v0.1 attention engine: the seven locked triggers (A–G) from docs/superpowers/specs/2026-04-20-ctm-serve- ui-v0.1-design.md §4 "Attention engine".
Package attention implements the v0.1 attention engine: the seven locked triggers (A–G) from docs/superpowers/specs/2026-04-20-ctm-serve- ui-v0.1-design.md §4 "Attention engine".
Package auth owns the ctm serve password hashing, user credentials file, and in-memory session store (V27 single-user auth).
Package auth owns the ctm serve password hashing, user credentials file, and in-memory session store (V27 single-user auth).
Package events implements the in-memory pub/sub hub and SSE handler that fan tool-call, quota, and lifecycle events out to the UI.
Package events implements the in-memory pub/sub hub and SSE handler that fan tool-call, quota, and lifecycle events out to the UI.
Package git provides git-backed primitives for ctm serve: listing the YOLO checkpoint commits in a session's workdir and reverting to one of them.
Package git provides git-backed primitives for ctm serve: listing the YOLO checkpoint commits in a session's workdir and reverting to one of them.
Package ingest builds the in-memory projections that the serve API reads from.
Package ingest builds the in-memory projections that the serve API reads from.
Package proc provides the CLI-side glue between session-creating commands (`ctm attach`, `ctm new`, `ctm yolo`, etc.) and the local `ctm serve` daemon: a fire-and-forget spawner that ensures serve is up, and a tiny HTTP client that POSTs lifecycle events to its /api/hooks/:event endpoint.
Package proc provides the CLI-side glue between session-creating commands (`ctm attach`, `ctm new`, `ctm yolo`, etc.) and the local `ctm serve` daemon: a fire-and-forget spawner that ensures serve is up, and a tiny HTTP client that POSTs lifecycle events to its /api/hooks/:event endpoint.
Package store persists per-session token/cost history so the dashboard can render a cumulative-cost chart (V13) that survives daemon restarts.
Package store persists per-session token/cost history so the dashboard can render a cumulative-cost chart (V13) that survives daemon restarts.
Package webhook dispatches attention_raised events to a user-configured HTTP endpoint.
Package webhook dispatches attention_raised events to a user-configured HTTP endpoint.

Jump to

Keyboard shortcuts

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