Documentation
¶
Overview ¶
Package limits computes the state of the limit rules of an organization and the effective limit of each person (docs/hub/limits.md): every rule that applies to a call counts it, the tightest one wins. The hub shows the result and, unless HUB_LIMITS_ENFORCE=off, sends it to the machines (push.go).
Index ¶
- Constants
- func Length(period string) time.Duration
- func Location(ctx context.Context, st store.Store, orgKey string) (*time.Location, error)
- func ModelMatches(pattern, model string) bool
- func Worse(a, b string) string
- type Book
- func (b *Book) AccountLabel(id string) string
- func (b *Book) Applies(p Person, r store.LimitRule) bool
- func (b *Book) Effective(p Person) []Effective
- func (b *Book) MachineEntries(ctx context.Context, st store.Store, spaceID string) ([]hubproto.BudgetEntry, error)
- func (b *Book) PersonOf(ctx context.Context, st store.Store, userID string) (Person, error)
- func (b *Book) Rules(p Person) []State
- type Caller
- type Effective
- type Person
- type PushDeps
- type Pusher
- type State
Constants ¶
const ( OK = hubproto.BudgetOK Warn = hubproto.BudgetWarn Stop = hubproto.BudgetStop // DefaultWarnPct is the share at which a rule warns unless set. DefaultWarnPct = 0.8 )
const DefaultTZ = "Europe/Moscow"
DefaultTZ is the time zone of an organization that has none (an implicit one).
Variables ¶
This section is empty.
Functions ¶
func ModelMatches ¶
ModelMatches says whether a rule's model pattern covers a call's model: the same name, a family key of the price list, or a part of the name («opus-4-1» covers claude-opus-4-1-20250805).
Types ¶
type Book ¶
type Book struct {
OrgKey string
Loc *time.Location
Now time.Time
States []State
Accounts map[string]store.Account
// contains filtered or unexported fields
}
Book is the rules of one organization evaluated at one instant.
func Evaluate ¶
func Evaluate(ctx context.Context, st store.Store, orgKey string, loc *time.Location, now time.Time) (*Book, error)
Evaluate computes every rule of the organization (orgKey: store.OrgKey) at now, months cut in loc.
func (*Book) AccountLabel ¶
AccountLabel is the name of an account of the book.
func (*Book) Effective ¶
Effective is the person's effective limits. One entry stands for every account and model (when any rule binds the person), and one more for each account or model whose own rule is the binding one there: what the machine needs to tell a stop through «Claude Max №2» from a warn anywhere else.
func (*Book) MachineEntries ¶
func (b *Book) MachineEntries(ctx context.Context, st store.Store, spaceID string) ([]hubproto.BudgetEntry, error)
MachineEntries is the list a machine of the space holds: every limit of its people that is not ok.
type Caller ¶
type Caller interface {
DoInto(ctx context.Context, machineID, op string, params, out any, timeout time.Duration) error
Online() []string
}
Caller sends an operation to a machine over its link (link.Registry).
type Effective ¶
type Effective struct {
AccountID string
ModelPattern string
Status string
Pct float64
Binding string
RuleIDs []string // the binding one first
Reason string
ResetsAt *time.Time
}
Effective is the limit of a person for one account ("" any) and one model ("" any): the worst rule that applies.
type Person ¶
Person is where a person is in the organization: the tenants and the teams whose rules bind them.
type PushDeps ¶
type PushDeps struct {
DB *store.DB
Reg Caller
Mode string // hubproto.EnforceWarn or EnforceStop
Log *slog.Logger
Now func() time.Time
// Every is how often every machine's list is checked; 0 means 60 s (the contract's bound).
Every time.Duration
}
PushDeps are what the pusher is built from.
type Pusher ¶
type Pusher struct {
// contains filtered or unexported fields
}
Pusher keeps the budget lists of the machines current: it computes each machine's list, and sends budget.set when the list changed or the machine holds an older version than the hub sent.
func (*Pusher) Close ¶
func (p *Pusher) Close()
Close stops the pusher and waits for the pass running.
func (*Pusher) Connected ¶
Connected is called after a machine's hello: an agent that applies budgets gets its list now if it is behind.