Something went wrong. Try again.
A Kubernetes operator that runs Claude Code sessions on long-horizon work
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160package operator_test
import ( "fmt" "testing" "testing/synctest" "time"
"tangled.org/guid.foo/simmer/api/v1alpha1" "tangled.org/guid.foo/simmer/cluster" "tangled.org/guid.foo/simmer/dataplane" "tangled.org/guid.foo/simmer/internal/world")
func sevenDay(utilization float64) dataplane.RateLimitInfo { var info dataplane.RateLimitInfo info.UnifiedWindows.FiveHour = dataplane.RateLimitWindow{Utilization: 0.1, ResetsAt: time.Now().Add(time.Hour).Unix()} info.UnifiedWindows.SevenDay = dataplane.RateLimitWindow{Utilization: utilization, ResetsAt: time.Now().Add(84 * time.Hour).Unix()} return info}
// A turn's tokens and cost land on its Session, and once the turn// ends, its cost is taken from its Allowance's balance.func TestATurnIsChargedWithItsCost(t *testing.T) { synctest.Test(t, func(t *testing.T) { r := start(t) r.pass(t) r.pass(t) s := r.only(t) ref := dataplane.SessionRef{Namespace: "telemetry", Name: s.Metadata.Name} _, _ = r.store.Take(ref, 1) _ = r.o.Release(t.Context(), ref, "alert/DiskFull/assistant")
errs := []error{ r.o.SessionUsage(t.Context(), ref, sevenDay(0.05)), r.o.SessionTokens(t.Context(), ref, dataplane.TokensRequest{Tokens: 1200, CostUSD: 2.5}), } synctest.Wait() r.succeed(t, s.Metadata.Name+"-1") r.pass(t) r.pass(t)
for _, err := range errs { if err != nil { t.Fatal(err) } } charged := r.only(t) usage := charged.Status.Turns[0].Usage if usage == nil || usage.Tokens != 1200 || usage.CostUSD != 2.5 || usage.Allowance != "upkeep" || usage.Account != "personal" || charged.Status.Cost != "$2.50" { t.Fatalf("the turn's usage is %+v and the Session's cost is %q, want 1200 tokens at 2.5 dollars, paid by upkeep on personal, and $2.50", usage, charged.Status.Cost) } allowance, _ := world.Get[v1alpha1.Allowance](r.w, cluster.Allowances, "", "upkeep") if allowance.Status.Ledger.Charged != 2.5 || allowance.Status.Balance != "$93" || allowance.Status.Spent != "$2" { t.Errorf("upkeep charged %v with a balance of %q having spent %q, want 2.5 with $93 having spent $2", allowance.Status.Ledger.Charged, allowance.Status.Balance, allowance.Status.Spent) } })}
// upkeep gains 4 dollars an hour and holds 96, so a turn of 100 dollars// leaves it 4 in debt, which an hour pays back.func TestASpentBudgetHoldsTheNextTurnUntilItIsPaidBack(t *testing.T) { synctest.Test(t, func(t *testing.T) { r := start(t) r.pass(t) r.pass(t) s := r.only(t) ref := dataplane.SessionRef{Namespace: "telemetry", Name: s.Metadata.Name} _, _ = r.store.Take(ref, 1) if err := r.o.SessionTokens(t.Context(), ref, dataplane.TokensRequest{Tokens: 1200, CostUSD: 100}); err != nil { t.Fatal(err) } synctest.Wait() r.succeed(t, s.Metadata.Name+"-1") r.pass(t) _ = r.store.Deliver(ref, dataplane.Event{ID: "again"}) r.pass(t) r.pass(t) held := r.only(t)
time.Sleep(2 * time.Hour) r.pass(t) r.pass(t)
paid := r.only(t) if held.Status.WaitingOn != v1alpha1.WaitingOnBrake || len(held.Status.Turns) != 1 || len(paid.Status.Turns) != 2 { t.Errorf("the Session waited on %s with %d turns, and has %d turns two hours later; want the brake with 1, and then 2", held.Status.WaitingOn, len(held.Status.Turns), len(paid.Status.Turns)) } })}
// Claude Code's result counts every turn of a resumed conversation,// so a resumed turn's tokens and cost are its totals less the totals of// the turn before it. Each turn here costs a dollar for each thousand// tokens. A turn that started the conversation, or whose total// fell, takes all of its total. A turn// recorded before sessionTokens holds the total in its tokens, and a// turn that posted no total is passed over.func TestATurnsTokensAndCostAreItsShareOfTheSessionsTotal(t *testing.T) { synctest.Test(t, func(t *testing.T) { r := start(t) r.pass(t) r.pass(t) s := r.only(t) ref := dataplane.SessionRef{Namespace: "telemetry", Name: s.Metadata.Name} turn := func(number int, total int64, resumed bool) *v1alpha1.TurnUsage { t.Helper() _, _ = r.store.Take(ref, number) post := r.o.SessionUsage(t.Context(), ref, sevenDay(0.02)) if total > 0 { post = r.o.SessionTokens(t.Context(), ref, dataplane.TokensRequest{Tokens: total, CostUSD: float64(total) / 1000, Resumed: resumed}) } if post != nil { t.Fatal(post) } synctest.Wait() r.succeed(t, fmt.Sprintf("%s-%d", s.Metadata.Name, number)) r.pass(t) _ = r.store.Deliver(ref, dataplane.Event{ID: fmt.Sprintf("after-%d", number)}) r.pass(t) r.pass(t) return r.only(t).Status.Turns[number-1].Usage }
first := turn(1, 1200, false) second := turn(2, 1500, true) fell := turn(3, 400, true) old := r.only(t) old.Status.Turns[2].Usage.SessionTokens = 0 r.w.Put(cluster.Sessions, &old) synctest.Wait() after := turn(4, 700, true) turn(5, 0, true) lost := turn(6, 1000, true) fresh := turn(7, 1200, false)
for _, c := range []struct { name string got *v1alpha1.TurnUsage tokens, total int64 }{ {"the first turn", first, 1200, 1200}, {"a resumed turn", second, 300, 1500}, {"a resumed turn whose total fell", fell, 400, 400}, {"the turn after an older record", after, 300, 700}, {"the turn after one that posted no total", lost, 300, 1000}, {"a turn that started the conversation again", fresh, 1200, 1200}, } { if c.got.Tokens != c.tokens || c.got.SessionTokens != c.total { t.Errorf("%s has %d tokens of %d, want %d of %d", c.name, c.got.Tokens, c.got.SessionTokens, c.tokens, c.total) } if cost, total := float64(c.tokens)/1000, float64(c.total)/1000; c.got.CostUSD != cost || c.got.SessionCostUSD != total { t.Errorf("%s cost %v of %v, want %v of %v", c.name, c.got.CostUSD, c.got.SessionCostUSD, cost, total) } } })}