| 1 | package schedule |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "math/rand/v2" |
| 6 | "reflect" |
| 7 | "slices" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // metered is a TensorX connection allowing models, at a fill of |
| 12 | // day/1,000,000 of its day's budget. |
| 13 | func metered(id string, day int64, models ...Model) Conn { |
| 14 | c := meter(id, day, 0) |
| 15 | c.Models = models |
| 16 | return c |
| 17 | } |
| 18 | |
| 19 | // §16.3's model for a role on one connection: the cap from the fill, the |
| 20 | // highest tier at or below it, else the lowest tier present; within a |
| 21 | // tier the lower output, then input price, then id. |
| 22 | func TestPickModel(t *testing.T) { |
| 23 | cases := []struct { |
| 24 | name string |
| 25 | fill int64 |
| 26 | models []Model |
| 27 | want string |
| 28 | }{ |
| 29 | {"below 0.5: the strongest", 0, tensorxTen, dsPro.ID}, |
| 30 | {"at 0.5: standard", 500_000, tensorxTen, dsFlash.ID}, |
| 31 | {"at 0.79: standard", 790_000, tensorxTen, dsFlash.ID}, |
| 32 | {"at 0.8: cheap", 800_000, tensorxTen, dsCheap.ID}, |
| 33 | {"strong only, at 0.9: the lowest present", 900_000, []Model{kimiPro, glm53}, glm53.ID}, |
| 34 | {"strong and standard at 0.9: standard, the lowest present", 900_000, []Model{glm53, glm52, kimiPro}, glm52.ID}, |
| 35 | {"cheap only at 0: cheap", 0, []Model{glmFlash, minimax}, glmFlash.ID}, |
| 36 | {"same prices: the id", 0, []Model{kimiPro, kimi3}, kimi3.ID}, |
| 37 | {"same output: the input", 0, []Model{glm52, Model{ID: "a", Family: "x", Tier: TierStandard, Output: 4_500_000, Input: 1_600_000}}, glm52.ID}, |
| 38 | } |
| 39 | for _, c := range cases { |
| 40 | m, ok := PickModel(c.models, TierCap(metered("t", c.fill))) |
| 41 | if !ok || m.ID != c.want { |
| 42 | t.Fatalf("%s: %s, want %s", c.name, m.ID, c.want) |
| 43 | } |
| 44 | } |
| 45 | if _, ok := PickModel(nil, TierStrong); ok { |
| 46 | t.Fatal("a model from none") |
| 47 | } |
| 48 | // The month's fill counts too: the larger of the two. |
| 49 | c := metered("t", 0, tensorxTen...) |
| 50 | c.SpendMonth = 8_000_000 |
| 51 | if TierCap(c) != TierCheap { |
| 52 | t.Fatalf("month fill 0.8: %s", TierCap(c)) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // §16.6 (c), over random sets and fills: a higher fill never yields a |
| 57 | // higher tier, on one connection and through Decide. |
| 58 | func TestPickModelMonotone(t *testing.T) { |
| 59 | r := rand.New(rand.NewPCG(11, 17)) |
| 60 | for n := range 20000 { |
| 61 | var models []Model |
| 62 | for _, m := range tensorxTen { |
| 63 | if r.IntN(2) == 0 { |
| 64 | // Prices shuffled so that a cheaper strong model exists |
| 65 | // (finding 6's case). |
| 66 | m.Output = int64(r.IntN(5)) * 1_000_000 |
| 67 | models = append(models, m) |
| 68 | } |
| 69 | } |
| 70 | lo, hi := int64(r.IntN(1_000_000)), int64(r.IntN(1_000_000)) |
| 71 | if lo > hi { |
| 72 | lo, hi = hi, lo |
| 73 | } |
| 74 | a, okA := PickModel(models, TierCap(metered("t", lo))) |
| 75 | b, okB := PickModel(models, TierCap(metered("t", hi))) |
| 76 | if okA != okB || okA && tierRank(b.Tier) > tierRank(a.Tier) { |
| 77 | t.Fatalf("#%d: fill %d gives %s (%s), fill %d gives %s (%s)", n, lo, a.ID, a.Tier, hi, b.ID, b.Tier) |
| 78 | } |
| 79 | step := Step{Role: RoleEngineering, Priority: Urgent} |
| 80 | da := Decide(in(step, metered("t", lo, models...))) |
| 81 | db := Decide(in(step, metered("t", hi, models...))) |
| 82 | if da.Run && db.Run && tierRank(db.Tier) > tierRank(da.Tier) { |
| 83 | t.Fatalf("#%d: Decide at fill %d %s, at %d %s", n, lo, da.Tier, hi, db.Tier) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // §16.3's pairs and the finish line's choices, at Decide. |
| 89 | func TestDecidePairs(t *testing.T) { |
| 90 | urgent := Step{Role: RoleEngineering, Priority: Urgent} |
| 91 | set := []Model{glm53, glmFlash, kimiPro, dsFlash} |
| 92 | // A first Engineering pass: the strongest allowed model, unprobed. |
| 93 | unprobed := slices.Clone(set) |
| 94 | for i := range unprobed { |
| 95 | unprobed[i].Unprobed = true |
| 96 | } |
| 97 | d := Decide(in(urgent, metered("t", 0, unprobed...))) |
| 98 | if !d.Run || d.Model != glm53.ID || d.Tier != TierStrong || !d.Unprobed || d.Family != "glm" { |
| 99 | t.Fatalf("first pass: %+v", d) |
| 100 | } |
| 101 | // At 0.8 fill the next pass runs a cheap one. |
| 102 | if d := Decide(in(urgent, metered("t", 800_000, set...))); d.Model != glmFlash.ID || d.Unprobed { |
| 103 | t.Fatalf("at 0.8: %+v", d) |
| 104 | } |
| 105 | // Review of a GLM step runs Kimi; with Kimi and DeepSeek unticked, |
| 106 | // GLM (the same family, last). |
| 107 | review := Step{Role: RoleReview, Priority: Urgent, Engineer: &Engineer{ID: "t", Family: "glm", Account: "t"}} |
| 108 | if d := Decide(in(review, metered("t", 0, set...))); d.Model != kimiPro.ID || d.Family != "kimi" { |
| 109 | t.Fatalf("review of glm: %+v", d) |
| 110 | } |
| 111 | if d := Decide(in(review, metered("t", 0, glm53, glmFlash))); d.Model != glm53.ID || d.Family != "glm" { |
| 112 | t.Fatalf("review with only glm: %+v", d) |
| 113 | } |
| 114 | // Family first across connections: another family on another |
| 115 | // metered connection beats the same family elsewhere and any |
| 116 | // subscription of Engineering's family. |
| 117 | review.Engineer = &Engineer{ID: "claude-a", Family: "claude", Account: "claude-a"} |
| 118 | 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 { |
| 119 | t.Fatalf("other family metered first: %+v", d) |
| 120 | } |
| 121 | review.Engineer = &Engineer{ID: "t", Family: "glm", Account: "t"} |
| 122 | if d := Decide(in(review, metered("t", 0, glm53), metered("u", 0, glm52, kimiPro))); d.Connection != "u" || d.Model != kimiPro.ID { |
| 123 | t.Fatalf("another connection's other family: %+v", d) |
| 124 | } |
| 125 | // The family key takes the best other-family model under the cap: |
| 126 | // at 0.6 fill, DeepSeek's standard model, not GLM's strong one. |
| 127 | if d := Decide(in(review, metered("t", 600_000, glm53, glm52, dsFlash, kimiPro))); d.Model != dsFlash.ID { |
| 128 | t.Fatalf("other family under the cap: %+v", d) |
| 129 | } |
| 130 | // Same family only: another connection before Engineering's own at |
| 131 | // the same rule, whatever the room ... |
| 132 | if d := Decide(in(review, metered("t", 0, glm53), metered("u", 300_000, glm52))); d.Connection != "u" || d.Model != glm52.ID { |
| 133 | t.Fatalf("same family elsewhere at the same rule: %+v", d) |
| 134 | } |
| 135 | // ... but §7's rules first (security round #5): Engineering's own |
| 136 | // subscription before another connection that is metered, and the |
| 137 | // preferred provider before another connection that is not. |
| 138 | sameFamily := metered("u", 0, Model{ID: "claude-like", Family: "claude", Tier: TierStrong, Output: 1}) |
| 139 | subReview := Step{Role: RoleReview, Priority: Urgent, Engineer: &Engineer{ID: "claude-t", Family: "claude", Account: "claude-t"}} |
| 140 | if d := Decide(in(subReview, sub("claude-t", "claude", 0.1), sameFamily)); d.Connection != "claude-t" { |
| 141 | t.Fatalf("subscription before metered: %+v", d) |
| 142 | } |
| 143 | other := sub("bedrock-u", "bedrock", 0.1) |
| 144 | other.Family = "claude" |
| 145 | pref := in(subReview, sub("claude-t", "claude", 0.5), other) |
| 146 | pref.Preference = Preference{Provider: "claude"} |
| 147 | if d := Decide(pref); d.Connection != "claude-t" || d.Reason != ReasonPreference { |
| 148 | t.Fatalf("the preference before another connection: %+v", d) |
| 149 | } |
| 150 | // The same in the other-family group. |
| 151 | if d := Decide(in(review, metered("t", 0, kimiPro), metered("u", 300_000, kimiPro))); d.Connection != "u" { |
| 152 | t.Fatalf("other family, another connection at the same rule: %+v", d) |
| 153 | } |
| 154 | // A kept session stays on its own model while it is eligible ... |
| 155 | kept := Step{Role: RoleEngineering, Priority: Urgent, Kept: "t", KeptModel: kimiPro.ID} |
| 156 | if d := Decide(in(kept, metered("t", 900_000, set...))); d.Reason != ReasonSticky || d.Model != kimiPro.ID { |
| 157 | t.Fatalf("kept: %+v", d) |
| 158 | } |
| 159 | // ... and is not kept once its model is removed or unhealthy. |
| 160 | if d := Decide(in(kept, metered("t", 0, glm53, glmFlash))); d.Reason == ReasonSticky || d.Model != glm53.ID { |
| 161 | t.Fatalf("kept model removed: %+v", d) |
| 162 | } |
| 163 | gone := kimiPro |
| 164 | gone.Unprobed = true |
| 165 | if d := Decide(in(kept, metered("t", 0, glm53, gone))); d.Reason == ReasonSticky { |
| 166 | t.Fatalf("kept on a model that must be probed again: %+v", d) |
| 167 | } |
| 168 | // A metered connection with no eligible model takes nothing. |
| 169 | if d := Decide(in(urgent, metered("t", 0))); d.Run || d.Connection != "" { |
| 170 | t.Fatalf("no eligible model: %+v", d) |
| 171 | } |
| 172 | // A subscription is one pair: no model, its own family. |
| 173 | if d := Decide(in(urgent, sub("a", "claude", 0.1))); d.Model != "" || d.Family != "claude" { |
| 174 | t.Fatalf("subscription: %+v", d) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // admissible is an oracle for the pairs a step could run on: the |
| 179 | // connection passes its bars, and the model is one of its eligible ones. |
| 180 | func admissible(i Input, c Conn) bool { |
| 181 | if !eligible(c, i.Step, i.Now) { |
| 182 | return false |
| 183 | } |
| 184 | if c.Billing == Metered { |
| 185 | return leave(i.Step) && spendBar(c) == "" |
| 186 | } |
| 187 | return Admits(c, Effective(i.Step), i.Now) |
| 188 | } |
| 189 | |
| 190 | // §16.6 (a), (b) and (e) over random inputs: the chosen model is one of |
| 191 | // the connection's eligible models (the kept one under rule 1); Review |
| 192 | // runs another family's pair whenever an admissible one exists; the same |
| 193 | // input gives the same pair, in any order of connections and models. |
| 194 | func TestDecidePairsInvariants(t *testing.T) { |
| 195 | r := rand.New(rand.NewPCG(19, 23)) |
| 196 | others := 0 |
| 197 | for n := range 20000 { |
| 198 | i := randomInput(r) |
| 199 | d := Decide(i) |
| 200 | if again := Decide(i); !reflect.DeepEqual(again, d) { |
| 201 | t.Fatalf("#%d: not deterministic", n) |
| 202 | } |
| 203 | j := i |
| 204 | j.Conns = slices.Clone(i.Conns) |
| 205 | for k := range j.Conns { |
| 206 | j.Conns[k].Models = slices.Clone(j.Conns[k].Models) |
| 207 | r.Shuffle(len(j.Conns[k].Models), func(a, b int) { |
| 208 | j.Conns[k].Models[a], j.Conns[k].Models[b] = j.Conns[k].Models[b], j.Conns[k].Models[a] |
| 209 | }) |
| 210 | } |
| 211 | r.Shuffle(len(j.Conns), func(a, b int) { j.Conns[a], j.Conns[b] = j.Conns[b], j.Conns[a] }) |
| 212 | if e := Decide(j); !reflect.DeepEqual(e, d) { |
| 213 | t.Fatalf("#%d: order changed the pair: %+v then %+v", n, d, e) |
| 214 | } |
| 215 | if !d.Run { |
| 216 | continue |
| 217 | } |
| 218 | c, _ := connOf(i, d.Connection) |
| 219 | if c.Billing == Metered { |
| 220 | m, ok := modelByID(c.Models, d.Model) |
| 221 | if !ok || m.Unprobed != d.Unprobed || m.Tier != d.Tier || m.Family != d.Family { |
| 222 | t.Fatalf("#%d: (a) model %q is not an eligible model of %s: %+v", n, d.Model, c.ID, c.Models) |
| 223 | } |
| 224 | if d.Reason == ReasonSticky && (d.Model != i.Step.KeptModel || d.Unprobed) { |
| 225 | t.Fatalf("#%d: (a) a kept session moved model or runs unproven: %+v", n, d) |
| 226 | } |
| 227 | } else if d.Model != "" { |
| 228 | t.Fatalf("#%d: a model on a subscription: %+v", n, d) |
| 229 | } |
| 230 | if e := i.Step.Engineer; e != nil && i.Step.Role == RoleReview && d.Reason != ReasonSticky { |
| 231 | exists := false |
| 232 | for _, c := range i.Conns { |
| 233 | if !admissible(i, c) { |
| 234 | continue |
| 235 | } |
| 236 | if c.Billing != Metered { |
| 237 | exists = exists || c.Family != e.Family |
| 238 | continue |
| 239 | } |
| 240 | for _, m := range c.Models { |
| 241 | exists = exists || m.Family != e.Family |
| 242 | } |
| 243 | } |
| 244 | if exists && d.Family == e.Family { |
| 245 | 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)) |
| 246 | } |
| 247 | if d.Family != e.Family { |
| 248 | others++ |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | if others < 500 { |
| 253 | t.Fatalf("the generator rarely reaches Review's other family: %d", others) |
| 254 | } |
| 255 | } |
| 256 | |