package schedule import ( "fmt" "math/rand/v2" "reflect" "slices" "testing" ) // metered is a TensorX connection allowing models, at a fill of // day/1,000,000 of its day's budget. func metered(id string, day int64, models ...Model) Conn { c := meter(id, day, 0) c.Models = models return c } // §16.3's model for a role on one connection: the cap from the fill, the // highest tier at or below it, else the lowest tier present; within a // tier the lower output, then input price, then id. func TestPickModel(t *testing.T) { cases := []struct { name string fill int64 models []Model want string }{ {"below 0.5: the strongest", 0, tensorxTen, dsPro.ID}, {"at 0.5: standard", 500_000, tensorxTen, dsFlash.ID}, {"at 0.79: standard", 790_000, tensorxTen, dsFlash.ID}, {"at 0.8: cheap", 800_000, tensorxTen, dsCheap.ID}, {"strong only, at 0.9: the lowest present", 900_000, []Model{kimiPro, glm53}, glm53.ID}, {"strong and standard at 0.9: standard, the lowest present", 900_000, []Model{glm53, glm52, kimiPro}, glm52.ID}, {"cheap only at 0: cheap", 0, []Model{glmFlash, minimax}, glmFlash.ID}, {"same prices: the id", 0, []Model{kimiPro, kimi3}, kimi3.ID}, {"same output: the input", 0, []Model{glm52, Model{ID: "a", Family: "x", Tier: TierStandard, Output: 4_500_000, Input: 1_600_000}}, glm52.ID}, } for _, c := range cases { m, ok := PickModel(c.models, TierCap(metered("t", c.fill))) if !ok || m.ID != c.want { t.Fatalf("%s: %s, want %s", c.name, m.ID, c.want) } } if _, ok := PickModel(nil, TierStrong); ok { t.Fatal("a model from none") } // The month's fill counts too: the larger of the two. c := metered("t", 0, tensorxTen...) c.SpendMonth = 8_000_000 if TierCap(c) != TierCheap { t.Fatalf("month fill 0.8: %s", TierCap(c)) } } // §16.6 (c), over random sets and fills: a higher fill never yields a // higher tier, on one connection and through Decide. func TestPickModelMonotone(t *testing.T) { r := rand.New(rand.NewPCG(11, 17)) for n := range 20000 { var models []Model for _, m := range tensorxTen { if r.IntN(2) == 0 { // Prices shuffled so that a cheaper strong model exists // (finding 6's case). m.Output = int64(r.IntN(5)) * 1_000_000 models = append(models, m) } } lo, hi := int64(r.IntN(1_000_000)), int64(r.IntN(1_000_000)) if lo > hi { lo, hi = hi, lo } a, okA := PickModel(models, TierCap(metered("t", lo))) b, okB := PickModel(models, TierCap(metered("t", hi))) if okA != okB || okA && tierRank(b.Tier) > tierRank(a.Tier) { t.Fatalf("#%d: fill %d gives %s (%s), fill %d gives %s (%s)", n, lo, a.ID, a.Tier, hi, b.ID, b.Tier) } step := Step{Role: RoleEngineering, Priority: Urgent} da := Decide(in(step, metered("t", lo, models...))) db := Decide(in(step, metered("t", hi, models...))) if da.Run && db.Run && tierRank(db.Tier) > tierRank(da.Tier) { t.Fatalf("#%d: Decide at fill %d %s, at %d %s", n, lo, da.Tier, hi, db.Tier) } } } // §16.3's pairs and the finish line's choices, at Decide. func TestDecidePairs(t *testing.T) { urgent := Step{Role: RoleEngineering, Priority: Urgent} set := []Model{glm53, glmFlash, kimiPro, dsFlash} // A first Engineering pass: the strongest allowed model, unprobed. unprobed := slices.Clone(set) for i := range unprobed { unprobed[i].Unprobed = true } d := Decide(in(urgent, metered("t", 0, unprobed...))) if !d.Run || d.Model != glm53.ID || d.Tier != TierStrong || !d.Unprobed || d.Family != "glm" { t.Fatalf("first pass: %+v", d) } // At 0.8 fill the next pass runs a cheap one. if d := Decide(in(urgent, metered("t", 800_000, set...))); d.Model != glmFlash.ID || d.Unprobed { t.Fatalf("at 0.8: %+v", d) } // Review of a GLM step runs Kimi; with Kimi and DeepSeek unticked, // GLM (the same family, last). review := Step{Role: RoleReview, Priority: Urgent, Engineer: &Engineer{ID: "t", Family: "glm", Account: "t"}} if d := Decide(in(review, metered("t", 0, set...))); d.Model != kimiPro.ID || d.Family != "kimi" { t.Fatalf("review of glm: %+v", d) } if d := Decide(in(review, metered("t", 0, glm53, glmFlash))); d.Model != glm53.ID || d.Family != "glm" { t.Fatalf("review with only glm: %+v", d) } // Family first across connections: another family on another // metered connection beats the same family elsewhere and any // subscription of Engineering's family. review.Engineer = &Engineer{ID: "claude-a", Family: "claude", Account: "claude-a"} if d := Decide(in(review, sub("claude-a", "claude", 0.1), sub("claude-b", "claude", 0.1), metered("t", 0, glm53))); d.Connection != "t" || d.Model != glm53.ID { t.Fatalf("other family metered first: %+v", d) } review.Engineer = &Engineer{ID: "t", Family: "glm", Account: "t"} if d := Decide(in(review, metered("t", 0, glm53), metered("u", 0, glm52, kimiPro))); d.Connection != "u" || d.Model != kimiPro.ID { t.Fatalf("another connection's other family: %+v", d) } // The family key takes the best other-family model under the cap: // at 0.6 fill, DeepSeek's standard model, not GLM's strong one. if d := Decide(in(review, metered("t", 600_000, glm53, glm52, dsFlash, kimiPro))); d.Model != dsFlash.ID { t.Fatalf("other family under the cap: %+v", d) } // Same family only: another connection before Engineering's own at // the same rule, whatever the room ... if d := Decide(in(review, metered("t", 0, glm53), metered("u", 300_000, glm52))); d.Connection != "u" || d.Model != glm52.ID { t.Fatalf("same family elsewhere at the same rule: %+v", d) } // ... but §7's rules first (security round #5): Engineering's own // subscription before another connection that is metered, and the // preferred provider before another connection that is not. sameFamily := metered("u", 0, Model{ID: "claude-like", Family: "claude", Tier: TierStrong, Output: 1}) subReview := Step{Role: RoleReview, Priority: Urgent, Engineer: &Engineer{ID: "claude-t", Family: "claude", Account: "claude-t"}} if d := Decide(in(subReview, sub("claude-t", "claude", 0.1), sameFamily)); d.Connection != "claude-t" { t.Fatalf("subscription before metered: %+v", d) } other := sub("bedrock-u", "bedrock", 0.1) other.Family = "claude" pref := in(subReview, sub("claude-t", "claude", 0.5), other) pref.Preference = Preference{Provider: "claude"} if d := Decide(pref); d.Connection != "claude-t" || d.Reason != ReasonPreference { t.Fatalf("the preference before another connection: %+v", d) } // The same in the other-family group. if d := Decide(in(review, metered("t", 0, kimiPro), metered("u", 300_000, kimiPro))); d.Connection != "u" { t.Fatalf("other family, another connection at the same rule: %+v", d) } // A kept session stays on its own model while it is eligible ... kept := Step{Role: RoleEngineering, Priority: Urgent, Kept: "t", KeptModel: kimiPro.ID} if d := Decide(in(kept, metered("t", 900_000, set...))); d.Reason != ReasonSticky || d.Model != kimiPro.ID { t.Fatalf("kept: %+v", d) } // ... and is not kept once its model is removed or unhealthy. if d := Decide(in(kept, metered("t", 0, glm53, glmFlash))); d.Reason == ReasonSticky || d.Model != glm53.ID { t.Fatalf("kept model removed: %+v", d) } gone := kimiPro gone.Unprobed = true if d := Decide(in(kept, metered("t", 0, glm53, gone))); d.Reason == ReasonSticky { t.Fatalf("kept on a model that must be probed again: %+v", d) } // A metered connection with no eligible model takes nothing. if d := Decide(in(urgent, metered("t", 0))); d.Run || d.Connection != "" { t.Fatalf("no eligible model: %+v", d) } // A subscription is one pair: no model, its own family. if d := Decide(in(urgent, sub("a", "claude", 0.1))); d.Model != "" || d.Family != "claude" { t.Fatalf("subscription: %+v", d) } } // admissible is an oracle for the pairs a step could run on: the // connection passes its bars, and the model is one of its eligible ones. func admissible(i Input, c Conn) bool { if !eligible(c, i.Step, i.Now) { return false } if c.Billing == Metered { return leave(i.Step) && spendBar(c) == "" } return Admits(c, Effective(i.Step), i.Now) } // §16.6 (a), (b) and (e) over random inputs: the chosen model is one of // the connection's eligible models (the kept one under rule 1); Review // runs another family's pair whenever an admissible one exists; the same // input gives the same pair, in any order of connections and models. func TestDecidePairsInvariants(t *testing.T) { r := rand.New(rand.NewPCG(19, 23)) others := 0 for n := range 20000 { i := randomInput(r) d := Decide(i) if again := Decide(i); !reflect.DeepEqual(again, d) { t.Fatalf("#%d: not deterministic", n) } j := i j.Conns = slices.Clone(i.Conns) for k := range j.Conns { j.Conns[k].Models = slices.Clone(j.Conns[k].Models) r.Shuffle(len(j.Conns[k].Models), func(a, b int) { j.Conns[k].Models[a], j.Conns[k].Models[b] = j.Conns[k].Models[b], j.Conns[k].Models[a] }) } r.Shuffle(len(j.Conns), func(a, b int) { j.Conns[a], j.Conns[b] = j.Conns[b], j.Conns[a] }) if e := Decide(j); !reflect.DeepEqual(e, d) { t.Fatalf("#%d: order changed the pair: %+v then %+v", n, d, e) } if !d.Run { continue } c, _ := connOf(i, d.Connection) if c.Billing == Metered { m, ok := modelByID(c.Models, d.Model) if !ok || m.Unprobed != d.Unprobed || m.Tier != d.Tier || m.Family != d.Family { t.Fatalf("#%d: (a) model %q is not an eligible model of %s: %+v", n, d.Model, c.ID, c.Models) } if d.Reason == ReasonSticky && (d.Model != i.Step.KeptModel || d.Unprobed) { t.Fatalf("#%d: (a) a kept session moved model or runs unproven: %+v", n, d) } } else if d.Model != "" { t.Fatalf("#%d: a model on a subscription: %+v", n, d) } if e := i.Step.Engineer; e != nil && i.Step.Role == RoleReview && d.Reason != ReasonSticky { exists := false for _, c := range i.Conns { if !admissible(i, c) { continue } if c.Billing != Metered { exists = exists || c.Family != e.Family continue } for _, m := range c.Models { exists = exists || m.Family != e.Family } } if exists && d.Family == e.Family { t.Fatalf("#%d: (b) Review ran %s's family though another was admissible: %+v\n%s", n, e.Family, d, fmt.Sprintf("%#v", i.Conns)) } if d.Family != e.Family { others++ } } } if others < 500 { t.Fatalf("the generator rarely reaches Review's other family: %d", others) } }