router

package
v0.177.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 11 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 router is muster's client for the permission router it was extracted from — the ONE place every outbound call to that service is spelled.

🔴 WHY A PACKAGE AND NOT A FEW http.Get CALLS AT THE SITES THAT NEED THEM. Before the extraction each of these was an in-process function call on a store this binary owned. After it they are network calls to a service with its own auth tier, its own failure modes and its own wire spellings, and the thing that goes wrong is not any single call — it is that the fifth one invents a second way to pass the credential, or forgets the actor header, or degrades a transport failure to a zero value. Consolidating them means the credential, the actor assertion, the timeout and the "a failed read is an ERROR, not a false" rule are decided once. The extraction plan's own §2.2 records the opposite pattern — one predicate open-coded at N sites — as the thing it keeps finding wrong at N-1 of them.

🔴 EVERY METHOD HERE IS A SEAM THE CARVE CREATED, AND EACH ONE REPLACES AN IN-PROCESS CALL THAT STILL EXISTS UPSTREAM. That is the test for whether something belongs in this file: if muster could answer it from its own database, it does not go here.

⚠ TWO OF THESE CALL ROUTES muster's CREDENTIAL CANNOT CURRENTLY REACH, AND THAT IS RECORDED RATHER THAN PAPERED OVER. See Notify and Directories; both carry the tier they need, the tier the route has today, and the closing condition. They are built because the consumer exists and the shape is settled; they will 401 until the upstream route is re-tiered.

Index

Constants

View Source
const (
	// GateDecided means the card was answered — by a human, or by the router's
	// auto-approval for a type it lets that decide. Response/Comment are populated.
	GateDecided = "decided"
	// GatePending means the card is still on the operator's screen.
	GatePending = "pending"
	// GateGone means no decision will ever arrive — evicted, swept, deleted, or
	// an id this door does not speak for. A caller must stop polling on it.
	GateGone = "gone"
)

Gate states, as the router spells them on the wire.

View Source
const DefaultActorHeader = "X-Router-Actor"

DefaultActorHeader is the header this client asserts its caller's name in, unless Config.ActorHeader names another.

🔴 THE NAME IS THE ROUTER'S CHOICE, NOT THIS CLIENT'S, WHICH IS WHY IT IS CONFIGURABLE. A client does not get to choose the server's protocol: the receiving door reads one exact header name, validates the value as an actor label and refuses the request when it is absent. A router that reads a different name than the one sent sees no actor at all and refuses every call, so a deployment whose router spells it differently must set Config.ActorHeader (MUSTER_ROUTER_ACTOR_HEADER) to that router's spelling.

⚠ EACH DEFAULT IS SPELLED EXACTLY ONCE, HERE. A second literal is how the two drift.

View Source
const DefaultTimeout = 15 * time.Second

DefaultTimeout bounds a single call. It is NOT the checkpoint wait — that is a poll loop in the caller, and each poll is one request bounded by this.

View Source
const DefaultTokenHeader = "X-Router-Token"

DefaultTokenHeader is the alternative to `Authorization: Bearer <token>` the receiving doors accept, unless Config.TokenHeader names another. Both are sent; the server compares whichever it reads first, constant-time.

Variables

View Source
var ErrNotConfigured = errors.New("router: no permission router is configured")

ErrNotConfigured is returned by every method on a nil or unconfigured Client.

🔴 IT IS AN ERROR, NOT A ZERO VALUE, AND THAT IS THE WHOLE POINT OF THE TYPE. The failure this repository keeps finding is a read that could not happen being reported as a read that found nothing: "no transcript recorded" over a live transcript, an empty directory picker that queried successfully and returned nothing, a checkpoint nobody could file reading as consent. Every consumer of this package must be able to tell "the router said no" from "there was no router to ask", so the unconfigured case is loud at the type level and the caller decides what it means.

View Source
var ErrUnauthorized = errors.New("router: the permission router refused this credential")

ErrUnauthorized is returned when the router refuses this client's credential (401/403). It is distinguished from a generic failure because two of the routes this client calls are on tiers muster's service credential does not currently reach, and a caller debugging that deserves to see which.

Functions

This section is empty.

Types

type Client

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

Client calls the permission router.

The zero value is NOT usable; use New. A nil *Client is explicitly valid and every method returns ErrNotConfigured on it, so a deployment without a router does not need nil checks at each of the call sites.

func New

func New(cfg Config) *Client

New builds a Client, or returns nil when the configuration is incomplete.

🔴 IT RETURNS nil RATHER THAN A HALF-BUILT CLIENT, AND THE CALLER MUST NOT "FIX" THAT WITH A NON-nil EMPTY ONE. A Client with no token produces a 401 on every call, which is indistinguishable at the call site from a router that is down — so the deployment that simply has no router configured would look like a broken one, forever, in the logs. nil says "not configured" once, at boot, and ErrNotConfigured says it at every call.

func (*Client) ClearGate

func (c *Client) ClearGate(ctx context.Context, id string) error

ClearGate removes a pending card this client minted. Best-effort: a decision already clears it, so this covers the timeout and abort paths.

func (*Client) Configured

func (c *Client) Configured() bool

Configured reports whether c can make calls. It is nil-safe.

func (*Client) Directories

func (c *Client) Directories(ctx context.Context, query string, limit int) ([]string, error)

Directories returns distinct working directories the router has observed, optionally filtered by a substring query, capped at limit.

🔴 SEAM — THE UPSTREAM ROUTE IS SESSION-GATED, SO THIS CALL WILL 401 UNTIL IT IS RE-TIERED. This is recorded in full in the upstream seam file too; the short version:

WHAT: GET /api/directories exists on the router, behind a signed operator
  session cookie, because a browser is its only caller there. A service
  credential is not a session, so muster cannot reach it.
WHY THE CALL IS BUILT: the picker is muster's feature — the task-create
  modal is on muster's side of the route partition — and the data it wants
  is on the router's side. That is precisely the shape a seam exists for,
  and the operator decided to REBUILD the picker here rather than drop it.
WHAT HAPPENS MEANWHILE: the handler degrades to a seeded-only picker and
  SAYS SO on screen. It does not render an empty dropdown as if the archive
  were empty — that exact conflation is why the upstream picker shipped
  broken and nobody noticed for the whole of its life.
CLOSING CONDITION: the router registers a service-tier read of the same
  data, and this method's path is repointed at it.
WHO CHECKS IT: the reviewer of that pull request, against the router's
  regenerated route golden.

func (*Client) MintGate

func (c *Client) MintGate(ctx context.Context, spec GateSpec) (string, error)

MintGate files an approval request and returns its id. Whether the router's auto-approval may decide it depends on spec.Type: the router's, not this client's, rule.

🔴 AN ERROR HERE IS A REFUSAL, NEVER AN APPROVAL. Every caller must treat a checkpoint that could not be FILED as not approved: nobody was asked, so nothing was consented to. The upstream chokepoint this replaces carries the same rule in the same words, and it is the one property of this whole seam that a wrong default silently inverts.

func (*Client) Notify

func (c *Client) Notify(ctx context.Context, n Notification) error

Notify asks the router to fan a notification out to its push subscriptions.

🔴 SEAM — THIS ROUTE IS ON THE HOOK TIER UPSTREAM AND muster MUST NOT HOLD THAT CREDENTIAL, SO THIS CALL WILL 401 UNTIL THE ROUTE IS RE-TIERED.

WHAT: POST /api/notify is the generic push fan-out. It is gated by the
  router's shared hook token — one value handed to every hook script on
  every host, travelling in cleartext to a LAN port, naming no caller.
WHY muster CANNOT SIMPLY HOLD IT: the extraction plan concludes in two
  separate sections that this service must not be given that tier. The
  token also opens task delete, task status and the comment retraction
  routes; handing it over to reach a notification would grant all of them.
WHY THE CALL IS BUILT ANYWAY: the consumer is real and already carved (the
  agent-ready push in internal/api), the body shape is settled, and the
  alternative — dropping the notification at the carve — removes a feature
  silently rather than visibly. It fails LOUD (ErrUnauthorized, logged by
  the caller) instead of quietly doing nothing.
CLOSING CONDITION: the router registers POST /api/notify on its service
  tier (the same door /api/gate and /api/sessions/{id} already use), OR
  this method and its one caller are deleted together and the agent-ready
  push is recorded as dropped.
WHO CHECKS IT: the reviewer of the pull request that re-tiers it, against
  the router's own regenerated route golden.

func (*Client) PublishEvent

func (c *Client) PublishEvent(ctx context.Context, name, data string) error

PublishEvent puts one event onto the router's SSE bus, so a browser holding a single EventSource against the router also sees muster's changes.

⚠ BEST-EFFORT BY CONTRACT AT THE CALL SITE, NOT HERE. This method reports its failure; whether a failed broadcast should fail the request that caused it is the caller's decision, and for every current caller the answer is no — a task was still created, and refusing the write because a notification did not land would be worse than a stale panel the next poll fixes.

func (*Client) ReadGate

func (c *Client) ReadGate(ctx context.Context, id string) (GateDecision, error)

ReadGate reads the three-state decision for a request this client minted.

func (*Client) SessionMeta

func (c *Client) SessionMeta(ctx context.Context, sessionID string) (SessionMeta, bool, error)

SessionMeta reads one session record, reporting whether the router holds one.

🔴 A 404 IS `false, nil` AND EVERYTHING ELSE IS AN ERROR. "The router has no record of this session" and "the router could not be asked" are different answers and the caller treats them differently: the first denormalises nothing onto the link row and moves on, the second is logged. Collapsing them to `false, nil` would make an outage look like a fleet of sessions that never existed.

func (*Client) SessionsExisting

func (c *Client) SessionsExisting(ctx context.Context, sessionIDs []string) (map[string]bool, error)

SessionsExisting reports which of sessionIDs the router still holds a transcript record for. Ids absent from the returned map have none.

🔴 THIS IS THE REPLACEMENT FOR A SQL JOIN, AND THE THREE-STATE RENDER IT FEEDS IS WHY IT MUST NOT DEGRADE TO false. The bit it produces drives SessionLink.DetailAvailable, which distinguishes "a live transcript" from "reaped" from "never recorded". A transport failure turned into false renders the permanent-looking sentence "no transcript recorded" over a session whose transcript is perfectly alive — silently, with nothing logged, on every row of the page. Upstream that exact conflation disguised a months-long outage as ordinary retention. So: an error propagates, and the read fails, exactly as the JOIN failed before it.

🔴 N REQUESTS FOR N IDS, AND THAT IS A MEASURED-COST DECISION RATHER THAN AN OVERSIGHT. The route is per-id; no batch form exists. See the package's BatchFormOwed note in doc_seams.go for what would close it.

type Config

type Config struct {
	// BaseURL is the router's origin, e.g. "https://router.example". No trailing
	// slash is required; one is tolerated.
	BaseURL string
	// Token is the SERVICE-tier credential. The router's service door is
	// fail-closed: it answers 503 while its own copy is unset, so an empty token
	// here is not a quiet degradation, it is a client that cannot be built.
	Token string
	// Actor is the name this service asserts on every request. The router
	// validates it as an actor label and refuses an unattributable request.
	Actor string
	// ActorHeader and TokenHeader are the header names the router reads the
	// actor and the token from. Empty means DefaultActorHeader and
	// DefaultTokenHeader.
	ActorHeader string
	TokenHeader string
	// HTTP is optional; a bounded default is used when nil.
	HTTP *http.Client
}

Config is what a Client needs to exist.

type GateDecision

type GateDecision struct {
	State    string `json:"state"`
	Response string `json:"response,omitempty"`
	Comment  string `json:"comment,omitempty"`
}

GateDecision is the GET /api/gate/{id} response.

type GateSpec

type GateSpec struct {
	Type      string   `json:"type"`
	Tool      string   `json:"tool"`
	Command   string   `json:"command"`
	Host      string   `json:"host"`
	Project   string   `json:"project"`
	Cwd       string   `json:"cwd,omitempty"`
	Context   []string `json:"context,omitempty"`
	SessionID string   `json:"sessionId,omitempty"`
}

GateSpec is the POST /api/gate body. Field names and spellings are the router's, not muster's.

⚠ `sessionId`, NOT `session`. The receiving service has exactly one meaning for the bare word and refuses a second one on a new producer struct.

type Notification

type Notification struct {
	Type  string            `json:"type"`
	ID    string            `json:"id,omitempty"`
	Title string            `json:"title,omitempty"`
	Body  string            `json:"body,omitempty"`
	Tag   string            `json:"tag,omitempty"`
	Data  map[string]string `json:"data,omitempty"`
}

Notification is the POST /api/notify body: a fire-and-forget Web Push with a title and body. It creates no request, no card and nothing to poll.

type Port

type Port struct{ C *Client }

Port adapts Client to the interfaces internal/api declares.

🔴 THE INTERFACES LIVE CONSUMER-SIDE AND THE ADAPTER LIVES HERE, WHICH IS THE ONLY ARRANGEMENT THAT KEEPS THE DEPENDENCY POINTING ONE WAY. internal/api declares what it needs (api.RouterPort, api.GatePort) and imports nothing to say so; this package knows about api and converts. Inverting that — api importing router for a concrete type — would make the domain layer depend on a transport, and would make api untestable without an HTTP server.

⚠ THE TYPE CONVERSIONS BELOW LOOK LIKE BOILERPLATE AND ARE NOT. Each one is the place a wire spelling meets a domain spelling, and each is the place a rename on either side becomes a compile error instead of a field that silently marshals to null. GateSpec is the worked example: the wire wants `sessionId` and the domain calls it Session, and exactly one line knows that.

func NewPort

func NewPort(cfg Config) Port

New builds a Port, or returns a zero Port whose C is nil when the configuration is incomplete. Callers should check Configured before wiring.

func (Port) ClearGate

func (p Port) ClearGate(ctx context.Context, id string) error

func (Port) Configured

func (p Port) Configured() bool

Configured reports whether this port can reach a router.

func (Port) Directories

func (p Port) Directories(ctx context.Context, query string, limit int) ([]string, error)

func (Port) MintGate

func (p Port) MintGate(ctx context.Context, spec api.GateSpec) (string, error)

func (Port) Notify

func (p Port) Notify(ctx context.Context, n api.RouterNotification) error

func (Port) PublishEvent

func (p Port) PublishEvent(ctx context.Context, name, data string) error

func (Port) ReadGate

func (p Port) ReadGate(ctx context.Context, id string) (api.GateDecision, error)

func (Port) SessionMeta

func (p Port) SessionMeta(ctx context.Context, sessionID string) (api.SessionMeta, bool, error)

func (Port) SessionsExisting

func (p Port) SessionsExisting(ctx context.Context, ids []string) (map[string]bool, error)

type SessionMeta

type SessionMeta struct {
	SessionID string
	Project   string
	Cwd       string
	Host      string
}

SessionMeta is what the router knows about one session.

Jump to

Keyboard shortcuts

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