Documentation
¶
Overview ¶
Package chiefusage is muster's record of what the owner's CHIEF costs and what it is spared (migration 0107; docs/usage.md):
- one row per chief turn (chief_turn_usage): what started it (Trigger), what it did (Outcome, from the tools it called) and the tokens its runtime reported for it — input, output, cache read, cache creation;
- one row per inbound plugin message (plugin_message_handling): who handled it — one of muster's own deterministic paths, an approved routing rule, or the chief.
The usage page (internal/api usage.go) reads both through Report.
🔴 THE TOKENS ARE THE RUNTIME'S OWN NUMBERS, NEVER AN ESTIMATE. Every chief runtime answers muster's agent wire (internal/agents responses.go), whose response.completed carries the turn's `usage`: ccd sums the Claude Code transcript's per-message usage for the turn (cmd/ccd transcript.go) — the same numbers whether the CLI reaches Anthropic directly or OpenRouter through muster's LLM proxy — and a gateway runtime sends the Responses API's own. A turn whose runtime sent none is recorded with usage_known false and zero tokens, and the page counts it apart, never as free.
🔴 OWNED AND UNDER ROW-LEVEL SECURITY: ctx acts as the owner whose chief it is, and every read is that owner's alone.
Index ¶
Constants ¶
const ( TriggerPlugin = "plugin" // an inbound plugin message delivered to the chief TriggerTaskEvent = "task_event" // a task muster handed the chief (a plugin's task notice) TriggerOwnerChat = "owner_chat" // a line the owner typed in the chief's chat TriggerView = "view" // a line the owner typed on a session's view TriggerAnswer = "answer" // the owner's answer to the chief's question, delivered TriggerHandBack = "hand_back" // a route handed back to the chief // TriggerSessionQuestion: one of the owner's mirrored sessions asked the // chief a question (chief_ask), delivered as a turn. TriggerSessionQuestion = "session_question" // TriggerMachine: the machine message door (POST /api/agents/{name}/messages). TriggerMachine = "machine" // TriggerKickoff: the chief's first turn, handed over by the kickoff deliverer. TriggerKickoff = "kickoff" TriggerOther = "other" // anything else (a nudge, a relay, a door this package does not know) )
Trigger kinds: what started a chief turn (chief_turn_usage.trigger).
const ( OutcomeRouted = "routed" OutcomeSettled = "settled" OutcomeAsk = "ask" OutcomeTask = "task" OutcomeRuleProposed = "rule_proposed" OutcomeOther = "other" OutcomeFailed = "failed" )
Outcomes: what a chief turn did (chief_turn_usage.outcome).
const ( HandlerFleetReport = "fleet_report" HandlerThread = "thread" HandlerClosedPR = "closed_pr" HandlerPRRoute = "pr_route" HandlerMailResume = "mail_resume" HandlerTaskEventMention = "task_event_mention" // The task triage (internal/api task_triage.go): an assignment muster // placed itself — forwarded to the agent on the task, routed to the one // session on it, or put on the owner's board. HandlerTaskForward = "task_forward" HandlerTaskRoute = "task_route" HandlerTaskBoard = "task_board" // HandlerTaskClaimed: the triage left an assignment of a CLAIMED task // alone (notes.Claims): no card, no turn. HandlerTaskClaimed = "task_claimed" HandlerRule = "rule" HandlerChief = "chief" )
Handlers: who handled an inbound plugin message (plugin_message_handling.handler).
const RuleProposeTool = "routing_rule_propose"
RuleProposeTool is the chief's tool that proposes a routing rule (internal/api routing_rules.go): named here because the outcome reads it.
const TopSendersShown = 10
TopSendersShown bounds Report.TopSenders.
Variables ¶
var MusterHandlers = []string{HandlerFleetReport, HandlerThread, HandlerClosedPR, HandlerPRRoute, HandlerMailResume, HandlerTaskEventMention, HandlerTaskForward, HandlerTaskRoute, HandlerTaskBoard, HandlerTaskClaimed}
MusterHandlers are the handlers that are muster's own deterministic paths (neither a rule nor the chief).
Functions ¶
Types ¶
type Day ¶
type Day struct {
Day time.Time
Turns, UnknownTurns int64
Tokens Tokens
Messages, ByRule, ByMuster, ByChief int64
}
Day is one UTC day's chief turns and messages.
type Handling ¶
type Handling struct {
MessageID int64
Plugin, Sender string
Handler string
RuleID int64
FallbackRuleID int64
}
Handling is who handled one inbound plugin message. RuleID is set exactly for HandlerRule; FallbackRuleID for HandlerChief when a rule matched and its session could not take the message.
type PGStore ¶
type PGStore struct {
// contains filtered or unexported fields
}
PGStore is Store on chief_turn_usage and plugin_message_handling (migration 0107), under row-level security, reading routing_rules for the rules' impact. Numbers, tags and ids only: no column holds a prompt, a reply or a message body.
func (*PGStore) RecordHandling ¶
RecordHandling implements Store. One row per message: a later handler replaces an earlier one (a mail resume that fell back to the chief is the chief's), keeping the first fallback rule a chief hand-off names.
func (*PGStore) RecordTurn ¶
RecordTurn implements Store.
type Report ¶
type Report struct {
Since time.Time
// Days is one entry per UTC day from Since's day to today, oldest first,
// days with nothing included (zeros).
Days []Day
Totals Totals
// Sources is the messages by plugin, most first.
Sources []Source
// BySource is the chief's tokens by what started its turns (the trigger,
// and the plugin for a plugin message or task event), costliest first.
BySource []TokenRow
// TopSenders is the costliest plugin senders (at most TopSendersShown).
TopSenders []TokenRow
// Rules is every rule the owner approved, with its impact.
Rules []RuleImpact
}
Report is the usage page's numbers over [Since, now).
type RuleImpact ¶
type RuleImpact struct {
RuleID int64
Plugin, Sender string
State string
Hits int64 // in the window
HitsAll int64 // since approval
BeforeTurns int64
AvgBefore, Avoided int64
}
RuleImpact is one approved rule's hits and the tokens it is estimated to have saved: Avoided = AvgBefore × Hits, where AvgBefore is the average total tokens of the chief's plugin-message turns for the rule's plugin (and its sender, when it matches on one) that ran BEFORE the rule was approved, over the BeforeTurns such turns whose usage is known. With none, AvgBefore and Avoided are 0 and the page says there is nothing to estimate from.
type Store ¶
type Store interface {
RecordTurn(ctx context.Context, t Turn) error
RecordHandling(ctx context.Context, h Handling) error
Report(ctx context.Context, since time.Time) (Report, error)
}
Store records turns and handlings and reads them back as a Report.
type TokenRow ¶
TokenRow is a group of chief turns and their tokens: by trigger and plugin (BySource) or by plugin and sender (TopSenders).
type Tokens ¶
type Tokens struct {
Input, Output, CacheRead, CacheCreation int64
}
Tokens is a turn's token counts, as its runtime reported them.
type Totals ¶
type Totals struct {
Turns, UnknownTurns int64
Tokens Tokens
Messages, ByRule, ByMuster, ByChief, Fallbacks int64
}
Totals is the window's sums.
func (Totals) WithoutChiefPercent ¶
WithoutChiefPercent is the share of the window's messages handled without a chief turn (by a rule or muster's own paths), in whole percent; 0 with none.
type Trigger ¶
type Trigger struct {
Kind string
Plugin string
Sender string
MessageID int64
// AskID is the attention ask whose answer a TriggerAnswer turn delivers
// (0 for every other kind): read by the chief's approval card, not recorded.
AskID int64
}
Trigger is what started a chief turn: its kind and, for a plugin message or a task event, the plugin, the sender and the message.
func TriggerFrom ¶
TriggerFrom is ctx's trigger, TriggerOther when no door set one.