Something went wrong. Try again.
A Kubernetes operator that runs Claude Code sessions on long-horizon work
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192package session
import ( "cmp" "slices"
"tangled.org/guid.foo/simmer/api/v1alpha1" "tangled.org/guid.foo/simmer/cluster")
// A Mandate's maxActive bounds its turns that run at once, not its// Sessions: a Session that waits on an event between turns holds keys// and costs nothing. An event never waits for room to start a Session;// the Session it starts waits on capacity for its first turn instead.
// slots counts each Mandate's turns that run, by namespace/name.type slots map[string]int
// countSlots counts the turns whose Job runs. A Job that ended this// pass frees its slot for a Session later in the same pass, whatever// the order of the two.func countSlots(sessions []v1alpha1.Session, jobs map[string]*cluster.Job) slots { out := slots{} for i := range sessions { s := &sessions[i] turns := s.Status.Turns if s.Status.Phase.Ended() || len(turns) == 0 || turns[len(turns)-1].Outcome != "" { continue } // A turn whose before hook runs holds its slot already. if last := turns[len(turns)-1]; last.StartedAt == nil && last.Before != nil { out[s.Metadata.Namespace+"/"+s.Spec.Mandate]++ continue } job := jobs[s.Metadata.Namespace+"/"+turns[len(turns)-1].Job] if done, _ := jobOutcome(job); job != nil && !done { out[s.Metadata.Namespace+"/"+s.Spec.Mandate]++ } } return out}
// full reports whether a Mandate runs its most turns.func (s slots) full(mandate *v1alpha1.Mandate) bool { limit := int(mandate.Spec.Sessions.MaxActive) return limit > 0 && s[mandate.Metadata.Namespace+"/"+mandate.Metadata.Name] >= limit}
func (s slots) take(mandate *v1alpha1.Mandate) { s[mandate.Metadata.Namespace+"/"+mandate.Metadata.Name]++}
// Running counts a Mandate's Sessions whose turn runs.func Running(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 == v1alpha1.PhaseRunning { count++ } } return count}
// byPriority answers the order in which the pass steps the Sessions:// by their Duty's priority, highest first, then oldest first. A turn// slot that frees while a Mandate runs its most turns goes to the// first Session in this order that needs one, before hook or turn. A// Session whose Duty is gone has priority 0.func byPriority(sessions []v1alpha1.Session, duties []v1alpha1.Duty) []int { priority := make([]int32, len(sessions)) order := make([]int, len(sessions)) for i := range sessions { order[i] = i if d := DutyOf(duties, &sessions[i]); d != nil { priority[i] = d.Spec.Priority } } slices.SortStableFunc(order, func(a, b int) int { return cmp.Or(cmp.Compare(priority[b], priority[a]), compareCreated(&sessions[a], &sessions[b])) }) return order}
func compareCreated(a, b *v1alpha1.Session) int { switch at, bt := a.Metadata.CreationTimestamp, b.Metadata.CreationTimestamp; { case at == nil || bt == nil: return 0 default: return at.Compare(*bt) }}