Documentation
¶
Overview ¶
Package ccfailover is per-agent Claude ACCOUNT FAILOVER: when a turn on a claude-code agent fails `rate_limited` or `auth_failed`, the agent is REBOUND to another of its owner's Claude accounts that is usable, instead of staying on the exhausted one until its limit resets.
Why ¶
A claude-code agent runs on one account: its pod's CLAUDE_CODE_OAUTH_TOKEN. The account cooldown (internal/ccpool) only steers which worker a NEW pool job goes to, so a deployment with ONE worker (or one officer instance) on an exhausted account stalled even with a second account free.
What happens ¶
internal/agentgateway calls Service.AccountFailed after it marks the account (ccpool.Pool.MarkFailure), for every turn shape — a chat, a kickoff, an officer re-prompt, a job. AccountFailed:
- ignores every failure but rate_limited and auth_failed;
- does nothing when failover is off (MUSTER_AGENT_CC_FAILOVER=off), or the agent is PINNED (its account was named at creation — a dispatch pin, the chief's, an officer's `cc_account`) and has not opted in (cc_failover);
- picks the target with ccpool.ChooseFailover — another account of the owner's that is neither auth_failed for its current token nor cooling down; never rate-limited first, then rate-limited longest ago, then the name. None usable (every account cooling): nothing happens, as before;
- moves the agent (agentprovision.Adapter.Rebind): the row's cc_account changes (conditionally on the old one) and its spec is re-applied, so its pod restarts on the new token, keeping its volume, namespace and identity;
- records an event (cc_account_failovers) and logs one line — account NAMES, the reason and the reset time, never a token.
Idempotent on (agent, from account) ¶
Two turns on one agent failing at once produce ONE rebind: in this process a per-agent lock and a re-read of the row under it (an agent no longer on the failed account was already moved); across processes the row's conditional move (WHERE cc_account = from). A later caller for the same (agent, from) gets Rebound false and Moved true, with the move's details from a short memo — which is how the job runner, which also asks after the gateway already did, learns where its agent went and which pod it replaced.
No fail-back ¶
Once moved, an agent stays on its new account when the old one resets: a move back would restart its pod again for nothing. It moves again only when the new account in turn fails.
Why a pod restart, not a token re-read ¶
The token is an environment variable the CLI (and each job lane's CLI) is started with. A cheaper path — ccd re-reading a mounted Secret file and restarting its CLIs — would need new ccd code, a Secret volume the kubelet refreshes on its own schedule (up to a minute late, and never for a subPath mount), and would still restart every CLI the pod runs. The pod restart is the path muster already uses for a rotated token (Stop → Start), costs a cold start (the running CLI session restarts; the volume, transcripts and checkout are kept), and leaves exactly one place the token is written (agentspec, the per-agent Secret).
Index ¶
- type Agents
- type Config
- type Event
- type PGStore
- type Policy
- type Pool
- type Prepare
- type Rebinder
- type Result
- type Service
- func (s *Service) AccountFailed(ctx context.Context, agentID int64, account, failure, detail string) Result
- func (s *Service) Allowed(ag agents.Agent) (bool, string)
- func (s *Service) AwaitRebound(ctx context.Context, r Result) error
- func (s *Service) Enabled() bool
- func (s *Service) Events(ctx context.Context, agentID int64, limit int) ([]Event, error)
- func (s *Service) NoteAccountFailure(ctx context.Context, agentID int64, account, failure, detail string)
- func (s *Service) UseHooks(policy Policy, prepare Prepare)
- type Store
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Enabled is MUSTER_AGENT_CC_FAILOVER (default on). Off: nothing is moved.
Enabled bool
Pool Pool
Agents Agents
Rebinder Rebinder
// Store is optional: without one a rebind is logged but not recorded.
Store Store
// Instances lists the backend's instances, for AwaitRebound. Optional:
// without it AwaitRebound returns at once.
Instances func(ctx context.Context) ([]provision.Instance, error)
Logger *log.Logger
// Now is a test seam (time.Now when nil).
Now func() time.Time
// ReadyPoll and ReadyWait bound AwaitRebound (defaults 3s and 5m).
ReadyPoll, ReadyWait time.Duration
}
Config builds a Service.
type Event ¶
type Event struct {
ID int64 `json:"id"`
OwnerID int64 `json:"-"`
AgentID int64 `json:"agent_id"`
AgentName string `json:"agent"`
From string `json:"from"`
To string `json:"to"`
Reason string `json:"reason"`
ResetsAt time.Time `json:"resets_at,omitzero"`
At time.Time `json:"at"`
}
Event is one recorded rebind.
type PGStore ¶
type PGStore struct {
// contains filtered or unexported fields
}
PGStore is the Postgres Store (table cc_account_failovers, migration 0066).
func (*PGStore) Record ¶
Record inserts one rebind, owned by the agent's owner.
⚠ owner_id IS THE AGENT'S, READ IN THE INSERT, NOT THE CALLER'S: a failover runs under whichever principal the failing turn had (the kickoff deliverer's is the system's, which names no user), and the row belongs to the agent's owner whoever reported the failure.
type Policy ¶
Policy reports, for an agent, whether its account is pinned and whether it opted in to failover anyway, beyond what its row says (the officer runtime answers for an officer's configured `cc_account` / `cc_failover`).
type Pool ¶
type Pool interface {
FailoverTarget(ctx context.Context, owner int64, from string) (string, bool, error)
CoolingUntil(ctx context.Context, owner int64, account string) (time.Time, error)
}
Pool is the part of ccpool.Pool a failover reads.
type Rebinder ¶
type Rebinder interface {
Rebind(ctx context.Context, agentID int64, from, to string, prepare func(context.Context, agents.Agent) error) (agentprovision.RebindResult, error)
}
Rebinder is the lifecycle half (agentprovision.Adapter.Rebind).
type Result ¶
type Result struct {
// AgentName is the agent's name ("" when it could not be read).
AgentName string
// From is the account the failure was reported for.
From string
// To is the account the agent is on after the call, when it is not From.
To string
// Moved: the agent is on To, not From — by this call or an earlier one.
Moved bool
// Rebound: THIS call moved it.
Rebound bool
// PrevInstance is the instance that ran before the move ("" unknown).
PrevInstance string
// Event is the recorded rebind, when Moved by a call this service made.
Event Event
// Why is why the agent was not moved ("" when it was).
Why string
}
Result is what Service.AccountFailed found or did.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the failover.
func (*Service) AccountFailed ¶
func (s *Service) AccountFailed(ctx context.Context, agentID int64, account, failure, detail string) Result
AccountFailed is the failover's entry point: a turn on agentID, running on account, failed with failure (ccd's typed error) and detail (its message). It never returns an error: what it could not do is in Result.Why and the log.
func (*Service) AwaitRebound ¶
AwaitRebound waits until a moved agent's replacement instance is ready: an instance of r.AgentName that is Ready and is not r.PrevInstance. It returns nil at once when the service cannot list instances, and an error when ctx ends or ReadyWait passes first.