Something went wrong. Try again.
A Kubernetes operator that runs Claude Code sessions on long-horizon work
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242package operator
import ( "context" "fmt" "time"
"tangled.org/guid.foo/simmer/api/v1alpha1" "tangled.org/guid.foo/simmer/brake" "tangled.org/guid.foo/simmer/dataplane" "tangled.org/guid.foo/simmer/session")
// The operator is the data plane's Control: each request from a turn// becomes a write to a Session's or an Account's status, under the same// lock as the pass, and then wakes the pass to act on it.var _ dataplane.Control = (*Operator)(nil)
// live answers a Session that exists and has not ended, and its// Mandate, which may be nil.func (o *Operator) live(ref dataplane.SessionRef) (*v1alpha1.Session, *v1alpha1.Mandate, error) { sessions, err := o.cluster.Sessions() if err != nil { return nil, nil, err } s := session.Find(sessions, ref) if s == nil || s.Status.Phase.Ended() { return nil, nil, dataplane.ErrUnknownSession } mandates, err := o.cluster.Mandates() if err != nil { return nil, nil, err } for i := range mandates { if mandates[i].Metadata.Namespace == ref.Namespace && mandates[i].Metadata.Name == s.Spec.Mandate { return s, &mandates[i], nil } } return s, nil, nil}
func (o *Operator) ServiceAccount(_ context.Context, ref dataplane.SessionRef) (string, error) { return o.serviceAccount(ref, session.ServiceAccountOf)}
func (o *Operator) HookServiceAccount(_ context.Context, ref dataplane.SessionRef) (string, error) { return o.serviceAccount(ref, session.HookServiceAccountOf)}
// serviceAccount names the ServiceAccount that of answers for a// Session's Mandate and Duty, or the Mandate's own when the Mandate is// gone.func (o *Operator) serviceAccount(ref dataplane.SessionRef, of func(*v1alpha1.Mandate, *v1alpha1.Duty) string) (string, error) { o.mu.Lock() defer o.mu.Unlock() s, mandate, err := o.live(ref) if err != nil { return "", err } if mandate == nil { return session.ServiceAccount(s.Spec.Mandate), nil } duty, err := o.dutyOf(s) if err != nil { return "", err } return of(mandate, duty), nil}
// dutyOf answers the Duty that a Session runs, or nil when it is gone.func (o *Operator) dutyOf(s *v1alpha1.Session) (*v1alpha1.Duty, error) { duties, err := o.cluster.Duties() if err != nil { return nil, err } return session.DutyOf(duties, s), nil}
func (o *Operator) Turn(_ context.Context, ref dataplane.SessionRef) (dataplane.Turn, error) { o.mu.Lock() defer o.mu.Unlock() s, mandate, err := o.live(ref) if err != nil { return dataplane.Turn{}, err } servers, err := o.cluster.McpServers() if err != nil { return dataplane.Turn{}, err } jobs, err := o.cluster.Jobs() if err != nil { return dataplane.Turn{}, err } duty, err := o.dutyOf(s) if err != nil { return dataplane.Turn{}, err } turn, err := session.Describe(o.store, s, mandate, duty, servers, jobs) if err == nil { o.sessions.Metrics.TurnAsked(s, time.Now()) } return turn, err}
func (o *Operator) Follow(_ context.Context, ref dataplane.SessionRef, key string) error { return o.keys(ref, func(s *v1alpha1.Session) error { return session.Follow(o.cluster, s, key) })}
func (o *Operator) Release(_ context.Context, ref dataplane.SessionRef, key string) error { return o.keys(ref, func(s *v1alpha1.Session) error { return session.Release(o.cluster, s, key) })}
// Holders answers the Sessions other than the caller that hold a key,// in any Mandate.func (o *Operator) Holders(_ context.Context, ref dataplane.SessionRef, key string) ([]dataplane.Holder, error) { o.mu.Lock() defer o.mu.Unlock() if _, _, err := o.live(ref); err != nil { return nil, err } sessions, err := o.cluster.Sessions() if err != nil { return nil, err } return session.HoldersOfAny(sessions, []string{key}, ref), nil}
// SetFollowUp checks the follow-up against the Session's Mandate and// writes it. It wakes the pass, which shows the Session waiting on it// once the turn ends.func (o *Operator) SetFollowUp(_ context.Context, ref dataplane.SessionRef, after time.Duration, reason string) (time.Time, error) { o.mu.Lock() defer o.mu.Unlock() s, mandate, err := o.live(ref) if err != nil { return time.Time{}, err } at, err := session.SetFollowUp(o.cluster, s, mandate, after, reason, time.Now()) if err != nil { return time.Time{}, err } o.bell.Ring(causeDataPlane) return at, nil}
// CancelFollowUp removes the Session's follow-up. A Session with no// keys may then finish.func (o *Operator) CancelFollowUp(_ context.Context, ref dataplane.SessionRef) error { return o.keys(ref, func(s *v1alpha1.Session) error { return session.CancelFollowUp(o.cluster, s) })}
// Hook answers the context of the Session's hook that runs in the Job.// It reads the Store, and takes nothing from it.func (o *Operator) Hook(_ context.Context, ref dataplane.SessionRef, job string) (dataplane.HookContext, error) { o.mu.Lock() defer o.mu.Unlock() s, _, err := o.live(ref) if err != nil { return dataplane.HookContext{}, err } return session.HookContext(o.store, s, job)}
func (o *Operator) keys(ref dataplane.SessionRef, write func(*v1alpha1.Session) error) error { o.mu.Lock() defer o.mu.Unlock() s, _, err := o.live(ref) if err != nil { return err } if err := write(s); err != nil { return err } // A Session that releases its last key may finish. o.bell.Ring(causeDataPlane) return nil}
// SessionUsage records a turn's reading on its Account.func (o *Operator) SessionUsage(_ context.Context, ref dataplane.SessionRef, info dataplane.RateLimitInfo) error { o.mu.Lock() defer o.mu.Unlock() _, payer, err := o.payer(ref) if err != nil { return err } _, err = o.record(payer.Account, info, v1alpha1.UsageSourceTurn) return err}
// SessionTokens records a turn's tokens and cost on its Session, which// the brake charges to the turn's Allowance when the turn ends.func (o *Operator) SessionTokens(_ context.Context, ref dataplane.SessionRef, tokens dataplane.TokensRequest) error { o.mu.Lock() defer o.mu.Unlock() s, payer, err := o.payer(ref) if err != nil { return err } return session.NoteResult(o.cluster, s, payer, tokens.Tokens, tokens.CostUSD, tokens.Resumed)}
// payer answers a live Session and the Allowance and Account that pay// for its turns.func (o *Operator) payer(ref dataplane.SessionRef) (*v1alpha1.Session, session.Payer, error) { s, mandate, err := o.live(ref) if err != nil { return nil, session.Payer{}, err } if mandate == nil { return nil, session.Payer{}, fmt.Errorf("%w: its Mandate is gone", dataplane.ErrUnknownSession) } allowances, err := o.cluster.Allowances() if err != nil { return nil, session.Payer{}, err } for _, a := range allowances { if a.Metadata.Name == mandate.Spec.Allowance { return s, session.Payer{Allowance: a.Metadata.Name, Account: a.Spec.Account}, nil } } return nil, session.Payer{}, fmt.Errorf("the Allowance %s does not exist", mandate.Spec.Allowance)}
func (o *Operator) AccountUsage(_ context.Context, account string, info dataplane.RateLimitInfo) error { o.mu.Lock() defer o.mu.Unlock() _, err := o.record(account, info, v1alpha1.UsageSourceProbe) return err}
// record writes a reading, and wakes the pass, because the reading can// lift a hold.func (o *Operator) record(account string, info dataplane.RateLimitInfo, source v1alpha1.UsageSource) (v1alpha1.Usage, error) { usage, err := brake.Record(o.cluster, account, brake.Reading(info, source, time.Now())) if err != nil { return usage, err } o.bell.Ring(causeDataPlane) return usage, nil}