usage

package
v0.232.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 10 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 usage pushes each officer instance's Claude usage — API calls, tokens and turns per session and UTC hour — to the officer's HQ (docs/officers.md, "Claude usage to HQ").

THE SOURCE is the instance's own Claude Code transcripts, counted by the instance pod's ccd on GET /v1/usage (cmd/ccd/usage_route.go) and read through the agent gateway with the instance's turn bearer. muster-server counts nothing itself: ccd owns the counting rule, next to the files.

THE SINK is HQ's PUT /officers/{id}/usage (officers.Client.PutUsage), sent with the officer's own token. HQ upserts each bucket and REPLACES its counts, so every push is the whole of the window (Window) and a push is idempotent: a later one only ever corrects an earlier one.

WHEN: every Interval, for every declared officer, for each instance the wake loop's own view (officers.Fleet.Instances) says exists and is up — and once more just before the wake loop scales an instance to zero (Flush), so the hour it stopped in is not left partial at HQ until it next runs. Its pod volume is kept on scale-to-zero, so the transcripts survive; but hours older than Window when the instance comes back are never pushed.

🔴 IT NEVER RETRIES WITHIN A TICK AND NEVER BLOCKS THE OFFICER: a failed read or push is counted (muster_officer_usage_push_total{result}) and logged once per change of state, and the next tick's push covers it. Flush is bounded by FlushTimeout and its failure never fails the stop. No transcript text passes through here, and an HQ refusal is logged by its status, never its body.

Index

Constants

View Source
const (
	// DefaultInterval is how often every up instance's usage is pushed.
	DefaultInterval = 10 * time.Minute
	// Window is how far back each push reaches. ccd truncates its start down
	// to the hour, so the oldest hour pushed is whole. Longer than a day, so
	// a muster restart or an HQ outage of up to a day loses nothing.
	Window = 25 * time.Hour
	// DefaultRequestTimeout bounds one read or one push.
	DefaultRequestTimeout = 30 * time.Second
	// FlushTimeout bounds the push before a scale-to-zero, read and push
	// together.
	FlushTimeout = 10 * time.Second
)

Variables

View Source
var ErrUnsupported = errors.New("usage: the instance's ccd does not serve GET /v1/usage (its image predates it)")

ErrUnsupported is an instance whose ccd does not serve GET /v1/usage (an image that predates it). The instance is skipped quietly.

Functions

This section is empty.

Types

type Collector

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

Collector is the usage collector.

func New

func New(cfg Config) (*Collector, error)

New builds a collector.

func (*Collector) Flush

func (c *Collector) Flush(ctx context.Context, o officers.Officer, instance string)

Flush pushes instance's usage once, now: the wake loop is about to scale it to zero (officers.Fleet.Stop). Bounded by FlushTimeout, read and push together; best-effort — whatever happens is counted and logged, and nothing is returned for the stop to fail on.

func (*Collector) Run

func (c *Collector) Run(ctx context.Context)

Run ticks every Interval until ctx ends.

func (*Collector) Tick

func (c *Collector) Tick(ctx context.Context)

Tick pushes the usage of every up instance of every declared officer's pools, one at a time. A pool whose instances cannot be seen is skipped this tick.

type Config

type Config struct {
	// Officers are the declared officers; each one's pools are walked.
	Officers  []officers.Officer
	Instances Instances
	Source    Source
	Poster    Poster
	Logger    *log.Logger
	Now       func() time.Time
	// Interval is the cadence (DefaultInterval).
	Interval time.Duration
	// RequestTimeout bounds one read or push (DefaultRequestTimeout).
	RequestTimeout time.Duration
}

Config is New's input. Zero values take the defaults.

type Instances

type Instances func(ctx context.Context, o officers.Officer) ([]officers.InstanceState, error)

Instances is the wake loop's view of pool o's instances (officers.Fleet.Instances): THE rule for which instance exists and is up.

type Poster

type Poster interface {
	PutUsage(ctx context.Context, o officers.Officer, r officers.UsageReport) error
}

Poster is the HQ side.

func NewBackendPoster added in v0.209.0

func NewBackendPoster(b *officers.Backends) Poster

NewBackendPoster is a Poster over each officer's own backend. A backend that does not serve usage answers officers.ErrNotServed.

type Reading

type Reading struct {
	// Account is the pooled Claude account the instance is on NOW, "" for a
	// pool on the LLM proxy (officers.UsageReport.Account).
	Account string
	Buckets []cctranscript.UsageBucket
}

Reading is one instance's usage as its Source read it.

type Source

type Source interface {
	Read(ctx context.Context, o officers.Officer, instance string, since time.Time) (Reading, error)
}

Source reads an officer instance's usage since since (production: its ccd's GET /v1/usage, through the agent gateway). An image without the route is ErrUnsupported.

Jump to

Keyboard shortcuts

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