Something went wrong. Try again.
A Kubernetes operator that runs Claude Code sessions on long-horizon work
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242package session_test
import ( "testing" "testing/synctest" "time"
batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1"
"tangled.org/guid.foo/simmer/api/v1alpha1" "tangled.org/guid.foo/simmer/brake" "tangled.org/guid.foo/simmer/cluster" "tangled.org/guid.foo/simmer/dataplane" "tangled.org/guid.foo/simmer/inject" "tangled.org/guid.foo/simmer/internal/memstore" "tangled.org/guid.foo/simmer/internal/world" "tangled.org/guid.foo/simmer/session")
// fixture is a cluster with the Ready Mandate ops in telemetry, and// the Session ops-a of its Duty triage. The Duty itself is stored only// by the tests that need its settings; without it, ops-a runs with the// defaults of a Session whose Duty is gone.type fixture struct { w *world.World store *memstore.Store r *session.Reconciler ref dataplane.SessionRef}
func readyMandate() *v1alpha1.Mandate { m := world.Mandate("telemetry", "ops", "upkeep") m.Status.Conditions = []v1alpha1.Condition{{Type: v1alpha1.ConditionReady, Status: v1alpha1.ConditionTrue, Reason: v1alpha1.ReasonReady}} return m}
// start builds the fixture. The Session holds keys, and events wait in// its inbox.func start(t *testing.T, keys []string, events int) *fixture { f := &fixture{w: world.Start(t), store: memstore.New(), ref: dataplane.SessionRef{Namespace: "telemetry", Name: "ops-a"}} f.r = &session.Reconciler{Cluster: f.w.Cluster, Store: f.store, Settings: inject.Settings{OperatorImage: "simmer:1", SessionImage: "claude:1"}} f.w.Put(cluster.Mandates, readyMandate()) s := world.Session("telemetry", "ops-a", "ops", "triage") s.Status.Keys = keys f.w.Put(cluster.Sessions, s) for i := range events { _ = f.store.Deliver(f.ref, dataplane.Event{ID: string(rune('a' + i)), Signal: "alerts"}) } time.Sleep(time.Second) synctest.Wait() return f}
var room = brake.Verdicts{ Allowances: map[string]brake.Verdict{"upkeep": {Reason: v1alpha1.ReasonRoom}}, AccountOf: map[string]string{"upkeep": "personal"},}
var holding = brake.Verdicts{ Allowances: map[string]brake.Verdict{"upkeep": {Holding: true, Reason: v1alpha1.ReasonOverBudget, Message: "over the budget"}}, AccountOf: map[string]string{"upkeep": "personal"},}
// pass runs one pass over the Sessions.func (f *fixture) pass(t *testing.T, verdicts brake.Verdicts, stopped bool) session.Result { t.Helper() sessions, _ := f.w.Cluster.Sessions() jobs, _ := f.w.Cluster.Jobs() mandates, _ := f.w.Cluster.Mandates() duties, _ := f.w.Cluster.Duties() signals, _ := f.w.Cluster.Signals() result, err := f.r.Reconcile(session.Inputs{Sessions: sessions, Jobs: jobs, Mandates: mandates, Duties: duties, Signals: signals, Verdicts: verdicts, Stopped: stopped}, time.Now()) if err != nil { t.Fatal(err) } synctest.Wait() return result}
// try runs one pass over the Sessions, and answers its error.func (f *fixture) try(verdicts brake.Verdicts, stopped bool) error { sessions, _ := f.w.Cluster.Sessions() jobs, _ := f.w.Cluster.Jobs() mandates, _ := f.w.Cluster.Mandates() duties, _ := f.w.Cluster.Duties() signals, _ := f.w.Cluster.Signals() _, err := f.r.Reconcile(session.Inputs{Sessions: sessions, Jobs: jobs, Mandates: mandates, Duties: duties, Signals: signals, Verdicts: verdicts, Stopped: stopped}, time.Now()) synctest.Wait() return err}
func (f *fixture) session() v1alpha1.Session { s, _ := world.Get[v1alpha1.Session](f.w, cluster.Sessions, "telemetry", "ops-a") return s}
// end makes the Job of a turn end, the way the Job controller does.func (f *fixture) end(t *testing.T, name string, succeeded bool) { t.Helper() job, ok := world.Get[cluster.Job](f.w, cluster.Jobs, "telemetry", name) if !ok { t.Fatalf("the Job %s does not exist", name) } kind := batchv1.JobFailed if succeeded { kind = batchv1.JobComplete } job.Status.Conditions = []batchv1.JobCondition{{Type: kind, Status: corev1.ConditionTrue}} f.w.Put(cluster.Jobs, &job) synctest.Wait()}
func TestASessionWithEventsTakesATurn(t *testing.T) { synctest.Test(t, func(t *testing.T) { f := start(t, []string{"alert/Low/assistant"}, 1)
f.pass(t, room, false)
s := f.session() _, created := world.Get[cluster.Job](f.w, cluster.Jobs, "telemetry", "ops-a-1") if !created || s.Status.Phase != v1alpha1.PhaseRunning || s.Status.WaitingOn != v1alpha1.WaitingOnJob || s.Status.TurnCount != 1 { t.Errorf("Job created = %v, status = %+v; want the Job ops-a-1 and Running on job", created, s.Status) } })}
func TestATurnsEndIsRecorded(t *testing.T) { cases := []struct { name string keys []string succeeded bool outcome v1alpha1.Outcome phase v1alpha1.Phase waiting v1alpha1.WaitingOn pending int }{ {"a success with keys waits for events", []string{"k"}, true, v1alpha1.OutcomeSucceeded, v1alpha1.PhaseWaiting, v1alpha1.WaitingOnEvent, 0}, {"a success with no keys finishes", nil, true, v1alpha1.OutcomeSucceeded, v1alpha1.PhaseFinished, v1alpha1.WaitingOnNothing, 0}, {"a failure keeps its events and backs off", nil, false, v1alpha1.OutcomeFailed, v1alpha1.PhaseWaiting, v1alpha1.WaitingOnBackoff, 1}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { synctest.Test(t, func(t *testing.T) { f := start(t, c.keys, 1) f.pass(t, room, false) _, _ = f.store.Take(f.ref, 1) f.end(t, "ops-a-1", c.succeeded)
f.pass(t, room, false)
s := f.session() pending, _ := f.store.Pending(f.ref) if s.Status.Turns[0].Outcome != c.outcome || s.Status.Phase != c.phase || s.Status.WaitingOn != c.waiting || pending != c.pending { t.Errorf("status = %+v with %d pending, want %s, %s on %s, %d pending", s.Status, pending, c.outcome, c.phase, c.waiting, c.pending) } }) }) }}
func TestASessionWaitsOnTheBrakeOrAStop(t *testing.T) { cases := []struct { name string verdicts brake.Verdicts stopped bool waiting v1alpha1.WaitingOn reason string account bool }{ {"its Allowance holds", holding, false, v1alpha1.WaitingOnBrake, v1alpha1.ReasonHolding, true}, {"a Stop exists", room, true, v1alpha1.WaitingOnStop, v1alpha1.ReasonStopped, false}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { synctest.Test(t, func(t *testing.T) { f := start(t, nil, 1)
result := f.pass(t, c.verdicts, c.stopped)
s := f.session() jobs := world.List[cluster.Job](f.w, cluster.Jobs) ready := s.Status.Conditions[0] if len(jobs) != 0 || s.Status.Phase != v1alpha1.PhasePending || s.Status.WaitingOn != c.waiting || ready.Reason != c.reason || result.Waiting["personal"] != c.account { t.Errorf("%d Jobs, status = %+v, waiting accounts %v; want none, Pending on %s with %s", len(jobs), s.Status, result.Waiting, c.waiting, c.reason) } }) }) }}
func TestAStopDeletesTheRunningJobAndInterruptsTheTurn(t *testing.T) { synctest.Test(t, func(t *testing.T) { f := start(t, nil, 1) f.pass(t, room, false) _, _ = f.store.Take(f.ref, 1)
f.pass(t, room, true)
s := f.session() _, exists := world.Get[cluster.Job](f.w, cluster.Jobs, "telemetry", "ops-a-1") if exists || s.Status.Turns[0].Outcome != v1alpha1.OutcomeInterrupted || s.Status.WaitingOn != v1alpha1.WaitingOnStop { t.Errorf("the Job exists = %v, status = %+v; want it gone, the turn Interrupted, and waiting on stop", exists, s.Status) } })}
func TestAFinishedSessionIsDeletedAfterItsTimeToLive(t *testing.T) { synctest.Test(t, func(t *testing.T) { f := start(t, nil, 0) finishing := f.pass(t, room, false) finished := f.session().Status.FinishedAt
early := f.pass(t, room, false) time.Sleep(24 * time.Hour) f.pass(t, room, false)
_, exists := world.Get[v1alpha1.Session](f.w, cluster.Sessions, "telemetry", "ops-a") if finished == nil || !finishing.Wake.Equal(finished.Add(24*time.Hour)) || !early.Wake.Equal(finishing.Wake) || exists { t.Errorf("finished at %v, woke for %v then %v, exists after 24h = %v; want a wake at its end of life from the pass that finished it, then gone", finished, finishing.Wake, early.Wake, exists) } })}
func TestAFinishedSessionWithNoTimeToLiveSetIsKeptForTheDefault(t *testing.T) { synctest.Test(t, func(t *testing.T) { f := start(t, nil, 0) m := readyMandate() m.Spec.Sessions.TTLAfterFinished = "" f.w.Put(cluster.Mandates, m) synctest.Wait()
finishing := f.pass(t, room, false)
finished := f.session().Status.FinishedAt if finished == nil || !finishing.Wake.Equal(finished.Add(24*time.Hour)) { t.Errorf("finished at %v, woke for %v; want a wake 24h after it finished", finished, finishing.Wake) } })}