Something went wrong. Try again.
A Kubernetes operator that runs Claude Code sessions on long-horizon work
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150// Package session runs the Sessions: one conversation under one// Mandate, from the event, cadence, or schedule that starts it, through its turns,// to its finish and its time to live. Each turn is a Job that runs the// injected simmer binary. Only the operator writes a Session: an agent// that follows or releases a key asks through the data plane.package session
import ( "fmt" "slices"
"tangled.org/guid.foo/simmer/api/v1alpha1" "tangled.org/guid.foo/simmer/cluster" "tangled.org/guid.foo/simmer/dataplane")
// Ref answers the data plane's name of a Session.func Ref(s *v1alpha1.Session) dataplane.SessionRef { return dataplane.SessionRef{Namespace: s.Metadata.Namespace, Name: s.Metadata.Name}}
// Active counts a Mandate's Sessions that have not ended.func Active(sessions []v1alpha1.Session, mandate *v1alpha1.Mandate) int { count := 0 for _, s := range sessions { if s.Metadata.Namespace == mandate.Metadata.Namespace && s.Spec.Mandate == mandate.Metadata.Name && !s.Status.Phase.Ended() { count++ } } return count}
// Start creates a Session of a Mandate for one of its Duties, holding// keys, with event in its inbox. A Mandate that runs its most turns// still gets the Session, which waits on capacity for its first turn,// so no event is lost to the limit.func Start(c *cluster.Cluster, store dataplane.Store, mandate *v1alpha1.Mandate, duty *v1alpha1.Duty, origin *v1alpha1.Origin, keys []string, event dataplane.Event) (*v1alpha1.Session, error) { created, err := Create(c, mandate, duty, origin) if err != nil { return nil, err } return created, Begin(c, store, created, keys, event)}
// Create creates a Session of a Mandate for one of its Duties. The// Session runs the Duty's skill. origin is nil for a Duty that runs// on its cadence or its schedule. The Session holds no key and has no// event until Begin, so the router can create every Session that one// event starts and then name them all to each other.func Create(c *cluster.Cluster, mandate *v1alpha1.Mandate, duty *v1alpha1.Duty, origin *v1alpha1.Origin) (*v1alpha1.Session, error) { controller := true created, err := cluster.Create(c, cluster.Sessions, mandate.Metadata.Namespace, &v1alpha1.Session{ APIVersion: v1alpha1.APIVersion, Kind: v1alpha1.SessionKind.Name, Metadata: v1alpha1.ObjectMeta{ GenerateName: mandate.Metadata.Name + "-", Namespace: mandate.Metadata.Namespace, Labels: map[string]string{v1alpha1.LabelMandate: mandate.Metadata.Name, v1alpha1.LabelDuty: duty.Metadata.Name}, // The Mandate owns its Sessions, so they go when it goes. OwnerReferences: []v1alpha1.OwnerReference{{ APIVersion: v1alpha1.APIVersion, Kind: v1alpha1.MandateKind.Name, Name: mandate.Metadata.Name, UID: mandate.Metadata.UID, Controller: &controller, }}, }, Spec: v1alpha1.SessionSpec{ Mandate: mandate.Metadata.Name, Duty: duty.Metadata.Name, Skill: duty.Spec.Skill, Origin: origin, }, }) if err != nil { return nil, err } why := "on its cadence or its schedule" if origin != nil { why = fmt.Sprintf("for %s on the Signal %s", origin.Key, origin.Signal) } c.Post(cluster.Ref(cluster.Sessions, created.Metadata.Namespace, created.Metadata.Name, created.Metadata.UID), false, "SessionStarted", fmt.Sprintf("The Duty %s of the Mandate %s started the Session, %s.", duty.Metadata.Name, mandate.Metadata.Name, why)) return created, nil}
// Begin puts a created Session's first event in its inbox, and writes// the keys it holds.func Begin(c *cluster.Cluster, store dataplane.Store, created *v1alpha1.Session, keys []string, event dataplane.Event) error { if err := store.Deliver(Ref(created), event); err != nil { return err } return cluster.WriteStatus(c, cluster.Sessions, created, func(o *v1alpha1.Session) { o.Status.Phase = v1alpha1.PhasePending o.Status.Keys = slices.Compact(slices.Sorted(slices.Values(keys))) o.Status.PendingEvents = 1 }, sessionConditions, isBadSession)}
// Resume puts an event in the inbox of a Session that already exists,// and adds the keys to the ones it holds. The router resumes a Session// that an earlier event released the keys from, in place of starting a// second one. It writes the keys first, so an event that the router// replays after a crash between the two finds the Session as a holder// and reaches it, and starts no second Session.func Resume(c *cluster.Cluster, store dataplane.Store, s *v1alpha1.Session, keys []string, event dataplane.Event) error { err := cluster.WriteStatus(c, cluster.Sessions, s, func(o *v1alpha1.Session) { o.Status.Keys = slices.Compact(slices.Sorted(slices.Values(append(slices.Clone(o.Status.Keys), keys...)))) }, sessionConditions, isBadSession) if err != nil { return err } return store.Deliver(Ref(s), event)}
func sessionConditions(s *v1alpha1.SessionStatus) []v1alpha1.Condition { return s.Conditions }
// isBadSession: a Session whose Mandate is gone, one whose// turns failed in a row, and one that needed a turn past its Mandate's// maxTurns need a person. A Session that waits on the brake, a Stop, a// backoff, or capacity waits as designed.func isBadSession(c v1alpha1.Condition) bool { switch c.Reason { case v1alpha1.ReasonMandateMissing, v1alpha1.ReasonTurnsFailed, v1alpha1.ReasonMaxTurns: return true } return false}
// Follow adds a key to a Session.func Follow(c *cluster.Cluster, s *v1alpha1.Session, key string) error { return cluster.WriteStatus(c, cluster.Sessions, s, func(o *v1alpha1.Session) { if !slices.Contains(o.Status.Keys, key) { o.Status.Keys = slices.Sorted(slices.Values(append(o.Status.Keys, key))) } }, sessionConditions, isBadSession)}
// Release removes a key from a Session.func Release(c *cluster.Cluster, s *v1alpha1.Session, key string) error { return cluster.WriteStatus(c, cluster.Sessions, s, func(o *v1alpha1.Session) { o.Status.Keys = slices.DeleteFunc(slices.Clone(o.Status.Keys), func(k string) bool { return k == key }) }, sessionConditions, isBadSession)}
// Find answers the Session a ref names, or nil.func Find(sessions []v1alpha1.Session, ref dataplane.SessionRef) *v1alpha1.Session { for i := range sessions { if Ref(&sessions[i]) == ref { return &sessions[i] } } return nil}