Documentation
¶
Index ¶
- type Account
- type AccountConfig
- type AccountStat
- type Candidate
- type CommandCodePolicy
- func (p *CommandCodePolicy) ClassifyRefusal(status int, body []byte, snapshot agentquota.Snapshot) Refusal
- func (p *CommandCodePolicy) Compare(a, b Candidate) int
- func (p *CommandCodePolicy) Exhausted(snapshot agentquota.Snapshot) (bool, string)
- func (p *CommandCodePolicy) ShouldUnpark(parked, fresh agentquota.Snapshot) bool
- type OpenCodeGoPolicy
- func (p *OpenCodeGoPolicy) ClassifyRefusal(status int, body []byte, _ agentquota.Snapshot) Refusal
- func (p *OpenCodeGoPolicy) Compare(a, b Candidate) int
- func (p *OpenCodeGoPolicy) Exhausted(snapshot agentquota.Snapshot) (bool, string)
- func (p *OpenCodeGoPolicy) ShouldUnpark(parked, fresh agentquota.Snapshot) bool
- type Options
- type Policy
- type Pool
- func (p *Pool) AccountParked(name string) bool
- func (p *Pool) AccountSnapshot(name string) (agentquota.Snapshot, bool)
- func (p *Pool) Acquire(name string) bool
- func (p *Pool) AcquireSlot(ctx context.Context, preferred string, exclude map[string]bool) *Account
- func (p *Pool) Bind(sessionID, accountName string)
- func (p *Pool) BlockRefusal(name string, refusal Refusal) bool
- func (p *Pool) ClassifyRefusal(name string, status int, body []byte) Refusal
- func (p *Pool) Close()
- func (p *Pool) Configure(configs []AccountConfig, settings Settings)
- func (p *Pool) Cooldown(name, reason string, d time.Duration)
- func (p *Pool) HasCandidate(preferred string, exclude map[string]bool) bool
- func (p *Pool) Len() int
- func (p *Pool) LiveWindow() time.Duration
- func (p *Pool) LoadBalance() bool
- func (p *Pool) Names() []string
- func (p *Pool) OutOfQuota() bool
- func (p *Pool) Preferred(sessionID string) string
- func (p *Pool) Refresh(ctx context.Context)
- func (p *Pool) RefreshAccount(ctx context.Context, name string)
- func (p *Pool) Release(name string)
- func (p *Pool) RouteName(preferred string, exclude map[string]bool) (string, bool)
- func (p *Pool) Select(preferred string, exclude map[string]bool) *Account
- func (p *Pool) SessionTTL() time.Duration
- func (p *Pool) Snapshot() []AccountStat
- type Refusal
- type RefusalKind
- type Settings
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AccountConfig ¶
type AccountConfig struct {
Name string
Key string
Disabled bool
MaxInFlight int
Selected bool
Value any
}
AccountConfig is one credential a pool may route to. Value is opaque account data owned by the provider adapter (a client, a token file descriptor, ...).
type AccountStat ¶
type AccountStat struct {
Name string
Key string
Available bool
Reason string
Enabled bool
MaxInFlight int
Selected bool
InFlight int
Sessions int
Windows []agentquota.Window
Balance *agentquota.Balance
RefreshedAt time.Time
LastError string
}
AccountStat is one row of the pool's view.
type Candidate ¶
type Candidate struct {
Name string
Snapshot agentquota.Snapshot
InFlight int
Order int
}
Candidate is the state a policy compares. InFlight and Order are supplied by the pool; the policy never reads account storage.
type CommandCodePolicy ¶
type CommandCodePolicy struct {
// LowBalance is the balance at or below which an "insufficient credits"
// refusal parks the account. A healthy account refused for one oversized
// request keeps serving smaller ones.
LowBalance float64
}
CommandCodePolicy is the routing strategy for Command Code accounts: weekly is the balance that decides long-term spread, 5h breaks ties, and a refusal only parks the account when its credit balance is low (or unknown).
func NewCommandCodePolicy ¶
func NewCommandCodePolicy() *CommandCodePolicy
NewCommandCodePolicy returns the Command Code strategy.
func (*CommandCodePolicy) ClassifyRefusal ¶
func (p *CommandCodePolicy) ClassifyRefusal(status int, body []byte, snapshot agentquota.Snapshot) Refusal
ClassifyRefusal implements Policy.
func (*CommandCodePolicy) Compare ¶
func (p *CommandCodePolicy) Compare(a, b Candidate) int
Compare implements Policy.
func (*CommandCodePolicy) Exhausted ¶
func (p *CommandCodePolicy) Exhausted(snapshot agentquota.Snapshot) (bool, string)
Exhausted implements Policy.
func (*CommandCodePolicy) ShouldUnpark ¶
func (p *CommandCodePolicy) ShouldUnpark(parked, fresh agentquota.Snapshot) bool
ShouldUnpark implements Policy: only a balance that grew is evidence of a refill. A window moving is not enough for a zero-credit account.
type OpenCodeGoPolicy ¶
type OpenCodeGoPolicy struct{}
OpenCodeGoPolicy is the routing strategy for OpenCode Go accounts: monthly is the long-term allowance that decides spread, weekly and 5h break ties, and an allowance refusal always parks the account.
func NewOpenCodeGoPolicy ¶
func NewOpenCodeGoPolicy() *OpenCodeGoPolicy
NewOpenCodeGoPolicy returns the OpenCode Go strategy.
func (*OpenCodeGoPolicy) ClassifyRefusal ¶
func (p *OpenCodeGoPolicy) ClassifyRefusal(status int, body []byte, _ agentquota.Snapshot) Refusal
ClassifyRefusal implements Policy.
func (*OpenCodeGoPolicy) Compare ¶
func (p *OpenCodeGoPolicy) Compare(a, b Candidate) int
Compare implements Policy.
func (*OpenCodeGoPolicy) Exhausted ¶
func (p *OpenCodeGoPolicy) Exhausted(snapshot agentquota.Snapshot) (bool, string)
Exhausted implements Policy.
func (*OpenCodeGoPolicy) ShouldUnpark ¶
func (p *OpenCodeGoPolicy) ShouldUnpark(parked, fresh agentquota.Snapshot) bool
ShouldUnpark implements Policy: any window that was at its cap must have fallen below it.
type Options ¶
type Options struct {
Source agentquota.Source
Policy Policy
Settings Settings
Now func() time.Time
DisableRefresh bool
WaitForSlot time.Duration
}
Options configures a pool. The source only reads; the policy only decides.
type Policy ¶
type Policy interface {
// Compare orders two candidates. Negative means a is preferred.
Compare(a, b Candidate) int
// Exhausted reports whether the snapshot itself means the account cannot
// serve, with the reason to show.
Exhausted(snapshot agentquota.Snapshot) (bool, string)
// ShouldUnpark reports whether a parked account may serve again.
ShouldUnpark(parked, fresh agentquota.Snapshot) bool
// ClassifyRefusal maps an upstream answer to a routing decision. It sees the
// account's current snapshot so a low-credit rule stays in the policy.
ClassifyRefusal(status int, body []byte, snapshot agentquota.Snapshot) Refusal
}
Policy is the provider-specific routing strategy. A pool is constructed with exactly one Policy; the pool never branches on the source id.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool owns one provider's accounts, quotas, in-flight counts and session bindings. It never sees another provider's pool.
func NewPool ¶
func NewPool(configs []AccountConfig, opts Options) (*Pool, error)
NewPool builds one provider's pool and starts its refresher unless disabled.
func (*Pool) AccountParked ¶
AccountParked reports whether one account is parked for quota right now.
func (*Pool) AccountSnapshot ¶
func (p *Pool) AccountSnapshot(name string) (agentquota.Snapshot, bool)
AccountSnapshot returns one account's current quota.
func (*Pool) AcquireSlot ¶
AcquireSlot picks an account and reserves a slot, waiting up to the pool's WaitForSlot for a busy pool to drain.
func (*Pool) BlockRefusal ¶
BlockRefusal reacts to one classified refusal. Park parks the account; otherwise the account only cools down. This is the whole rule: no balance shape is read here.
func (*Pool) ClassifyRefusal ¶
ClassifyRefusal lets the pool's policy classify one upstream answer against the account's current snapshot.
func (*Pool) Configure ¶
func (p *Pool) Configure(configs []AccountConfig, settings Settings)
Configure reconciles the pool with its provider's current credentials.
func (*Pool) HasCandidate ¶
HasCandidate reports whether any account could take a request.
func (*Pool) LiveWindow ¶
LiveWindow returns the pool's live-count window.
func (*Pool) LoadBalance ¶
LoadBalance reports whether the pool routes across accounts.
func (*Pool) OutOfQuota ¶
OutOfQuota reports whether every routable account is parked.
func (*Pool) Preferred ¶
Preferred returns the account a conversation is bound to, while the binding is still within SessionTTL.
func (*Pool) Refresh ¶
Refresh polls every account once. A failure keeps the previous availability; an auth failure marks the account unavailable until a later success.
func (*Pool) RefreshAccount ¶
RefreshAccount polls one account. Concurrent refusals for the same account share one fetch.
func (*Pool) SessionTTL ¶
SessionTTL returns the pool's stickiness window.
func (*Pool) Snapshot ¶
func (p *Pool) Snapshot() []AccountStat
Snapshot copies the current state for the stats endpoints.
type Refusal ¶
type Refusal struct {
Kind RefusalKind
Message string
Detail string
Park bool
}
Refusal is a classified upstream answer. Park is the policy's decision that the account itself, not this request, is the problem. The pool only reads Park: true parks, false cools down.
type RefusalKind ¶
type RefusalKind string
RefusalKind is the routing meaning of one upstream answer.
const ( // RefusalRequest is a request-level refusal: another account would answer // the same, so the request is passed through. RefusalRequest RefusalKind = "request" // RefusalAllowance means the account has no allowance left. RefusalAllowance RefusalKind = "allowance" // RefusalAuth means the credential itself was rejected. RefusalAuth RefusalKind = "auth" // RefusalRateLimit is a short-window rate limit. RefusalRateLimit RefusalKind = "rate_limit" // RefusalTransient is a 5xx or another retryable upstream failure. RefusalTransient RefusalKind = "transient" )
type Settings ¶
type Settings struct {
LoadBalance bool
Selected string
RefreshEvery time.Duration
SessionTTL time.Duration
LiveWindow time.Duration
}
Settings are the per-pool knobs. SessionTTL is how long a conversation stays bound to one account; LiveWindow is how recently a conversation must have been seen to count as live. They are deliberately separate.