| 1 | package metered |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "fmt" |
| 7 | "maps" |
| 8 | "math/rand/v2" |
| 9 | "slices" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/runner/provider/opencode" |
| 14 | "amadan.net/oficina/equipo/internal/runner/provider/pi" |
| 15 | "amadan.net/oficina/equipo/internal/wire" |
| 16 | ) |
| 17 | |
| 18 | // §16.2: every priced model has a tier and every tiered model a price, |
| 19 | // in the current versions, provider by provider; each tier is one of the |
| 20 | // three; each family is the one the harness's own rule gives (Review's |
| 21 | // label is judged on the harness's rule, the choice on the table's). |
| 22 | func TestTierTableMatchesPrices(t *testing.T) { |
| 23 | tiers, ok := TierTables[CurrentTiers] |
| 24 | if !ok || tiers.Version != CurrentTiers || tiers.Date != "2026-11-12" { |
| 25 | t.Fatalf("current tier table %q: %+v", CurrentTiers, tiers.Version) |
| 26 | } |
| 27 | for provider, version := range Current { |
| 28 | prices := Tables[version].Models |
| 29 | tiered := tiers.Models[provider] |
| 30 | if p, q := slices.Sorted(maps.Keys(prices)), slices.Sorted(maps.Keys(tiered)); !slices.Equal(p, q) { |
| 31 | t.Fatalf("%s: priced %v, tiered %v", provider, p, q) |
| 32 | } |
| 33 | for model, mt := range tiered { |
| 34 | if TierRank(mt.Tier) == 0 { |
| 35 | t.Fatalf("%s %s: tier %q", provider, model, mt.Tier) |
| 36 | } |
| 37 | family := pi.Family(model) |
| 38 | if wire.APIKeyProviders[provider].Harness == wire.HarnessOpenCode { |
| 39 | family = opencode.Family(model) |
| 40 | } |
| 41 | if mt.Family != family { |
| 42 | t.Fatalf("%s %s: family %q, the harness says %q", provider, model, mt.Family, family) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | // §16.2's ten TensorX models, as listed. |
| 47 | want := map[string]string{"z-ai/glm-5.3": TierStrong, "moonshotai/kimi-k3.1-pro": TierStrong, "moonshotai/kimi-k3": TierStrong, "deepseek/deepseek-v4-pro-0813": TierStrong, "z-ai/glm-5.2": TierStandard, "qwen/qwen3.8-27b": TierStandard, "deepseek/deepseek-v4.1-flash": TierStandard, "z-ai/glm-5.3-flash": TierCheap, "deepseek/deepseek-v4-flash-0731": TierCheap, "minimax/minimax-m3.1-flash": TierCheap} |
| 48 | for model, tier := range want { |
| 49 | if got, ok := TierOf(wire.ProviderTensorX, model); !ok || got.Tier != tier { |
| 50 | t.Fatalf("%s: %+v, want %s", model, got, tier) |
| 51 | } |
| 52 | } |
| 53 | // Not priced, or priced only under another provider: not eligible. |
| 54 | if _, _, ok := Eligible(wire.ProviderTensorX, Current[wire.ProviderTensorX], "vendor/unpriced"); ok { |
| 55 | t.Fatal("an unpriced model eligible") |
| 56 | } |
| 57 | if _, _, ok := Eligible(wire.ProviderOpenCodeGo, Current[wire.ProviderTensorX], "z-ai/glm-5.3"); ok { |
| 58 | t.Fatal("a model eligible under another provider's prices") |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // A model tiered under an older tier table only stops being eligible, |
| 63 | // even while its connection's pinned price table still prices it. |
| 64 | func TestTierVersionsRetireModels(t *testing.T) { |
| 65 | old := CurrentTiers |
| 66 | t.Cleanup(func() { CurrentTiers = old; delete(TierTables, "tiers-test") }) |
| 67 | next := map[string]ModelTier{} |
| 68 | maps.Copy(next, TierTables[old].Models[wire.ProviderTensorX]) |
| 69 | delete(next, "z-ai/glm-5.3") |
| 70 | TierTables["tiers-test"] = TierTable{Version: "tiers-test", Date: "2026-12-01", Models: map[string]map[string]ModelTier{wire.ProviderTensorX: next}} |
| 71 | CurrentTiers = "tiers-test" |
| 72 | if _, _, ok := Eligible(wire.ProviderTensorX, Current[wire.ProviderTensorX], "z-ai/glm-5.3"); ok { |
| 73 | t.Fatal("a model with no tier in the current table eligible") |
| 74 | } |
| 75 | if _, _, ok := Eligible(wire.ProviderTensorX, Current[wire.ProviderTensorX], "z-ai/glm-5.2"); !ok { |
| 76 | t.Fatal("a tiered model not eligible") |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // The cheapest model: the lower output price, then the lower input |
| 81 | // price, then the id bytewise (§16.1's proof, §16.3's tie rule). |
| 82 | func TestCheapestTieRule(t *testing.T) { |
| 83 | v := Current[wire.ProviderTensorX] |
| 84 | if got := Cheapest(wire.ProviderTensorX, v, []string{"z-ai/glm-5.3", "deepseek/deepseek-v4-flash-0731", "z-ai/glm-5.3-flash"}); got != "deepseek/deepseek-v4-flash-0731" { |
| 85 | t.Fatalf("cheapest %s", got) |
| 86 | } |
| 87 | // kimi-k3 and kimi-k3.1-pro have the same prices: the id decides. |
| 88 | if got := Cheapest(wire.ProviderTensorX, v, []string{"moonshotai/kimi-k3.1-pro", "moonshotai/kimi-k3"}); got != "moonshotai/kimi-k3" { |
| 89 | t.Fatalf("cheapest of equals %s", got) |
| 90 | } |
| 91 | // glm-5.2 and glm-5.3 share the output price; the input decides. |
| 92 | if got := Cheapest(wire.ProviderTensorX, v, []string{"z-ai/glm-5.3", "z-ai/glm-5.2"}); got != "z-ai/glm-5.2" { |
| 93 | t.Fatalf("cheapest by input %s", got) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // oldDB is a thin.db from before S5b: no models_revision, no |
| 98 | // metered_models. |
| 99 | func oldDB(t *testing.T) *sql.DB { |
| 100 | t.Helper() |
| 101 | db, err := sql.Open("sqlite", "file:"+t.TempDir()+"/thin.db") |
| 102 | if err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | db.SetMaxOpenConns(1) |
| 106 | t.Cleanup(func() { db.Close() }) |
| 107 | if _, err := db.Exec(`CREATE TABLE metered_connections (local_id TEXT PRIMARY KEY, connection TEXT NOT NULL DEFAULT '', retiring INTEGER NOT NULL DEFAULT 0, person TEXT NOT NULL, provider TEXT NOT NULL, harness TEXT NOT NULL, upstream TEXT NOT NULL, billing TEXT NOT NULL, model TEXT NOT NULL DEFAULT '', home TEXT NOT NULL, currency TEXT NOT NULL, price_version TEXT NOT NULL, budget_day INTEGER NOT NULL, budget_month INTEGER NOT NULL, budget_revision INTEGER NOT NULL DEFAULT 0, spend_unknown INTEGER NOT NULL DEFAULT 0, pause_revision INTEGER NOT NULL DEFAULT 0, validated INTEGER NOT NULL DEFAULT 0, refused INTEGER NOT NULL DEFAULT 0, created_at TEXT NOT NULL)`); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | v := Current[wire.ProviderTensorX] |
| 111 | for _, row := range []struct { |
| 112 | local, model string |
| 113 | validated, refused int |
| 114 | }{{"proven", "z-ai/glm-5.3", 1, 0}, {"unproven", "z-ai/glm-5.2", 0, 0}, {"refused", "z-ai/glm-5.3", 1, 1}, {"none", "", 0, 0}} { |
| 115 | if _, err := db.Exec("INSERT INTO metered_connections(local_id, connection, person, provider, harness, upstream, billing, model, home, currency, price_version, budget_day, budget_month, validated, refused, created_at) VALUES(?,?,?,?,?,?,?,?,?,?,?,1,2,?,?,?)", row.local, "cn_"+row.local, "p", wire.ProviderTensorX, wire.HarnessPi, "https://api.tensorx.ai", wire.BillingMetered, row.model, "/h", "USD", v, row.validated, row.refused, "2026-11-01T00:00:00Z"); err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | } |
| 119 | return db |
| 120 | } |
| 121 | |
| 122 | // §16.5's migration: a connection with a model keeps that one model, |
| 123 | // healthy only where it was proven, with no time invented; refused stays |
| 124 | // refused; one with none gets every model that can be allowed, |
| 125 | // unprobed. Each at revision 1; running it again changes nothing. |
| 126 | func TestModelsMigration(t *testing.T) { |
| 127 | db := oldDB(t) |
| 128 | ctx := context.Background() |
| 129 | for range 2 { |
| 130 | if err := Migrate(ctx, db); err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | } |
| 134 | want := map[string][]Model{ |
| 135 | "proven": {{ID: "z-ai/glm-5.3", State: wire.ModelHealthy}}, |
| 136 | "unproven": {{ID: "z-ai/glm-5.2", State: wire.ModelUnprobed}}, |
| 137 | "refused": {{ID: "z-ai/glm-5.3", State: wire.ModelHealthy}}, |
| 138 | } |
| 139 | for local, models := range want { |
| 140 | got, err := Models(ctx, db, local) |
| 141 | if err != nil || !slices.Equal(got, models) { |
| 142 | t.Fatalf("%s: %+v %v", local, got, err) |
| 143 | } |
| 144 | c, _, _ := Get(ctx, db, local) |
| 145 | if c.ModelsRevision != 1 { |
| 146 | t.Fatalf("%s: revision %d", local, c.ModelsRevision) |
| 147 | } |
| 148 | if local == "refused" && !c.Refused { |
| 149 | t.Fatal("a refused connection made usable by the migration") |
| 150 | } |
| 151 | } |
| 152 | got, err := Models(ctx, db, "none") |
| 153 | if err != nil || len(got) != len(Tables[Current[wire.ProviderTensorX]].Models) { |
| 154 | t.Fatalf("none: %+v %v", got, err) |
| 155 | } |
| 156 | for _, m := range got { |
| 157 | if m.State != wire.ModelUnprobed || !m.FailedAt.IsZero() { |
| 158 | t.Fatalf("none: %+v", m) |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // §16.1: the set applies only above the revision held, only for the |
| 164 | // connection the frame names, only with models that can be allowed and |
| 165 | // at least one; kept models keep their state, new ones start unprobed, a |
| 166 | // removed one goes; the connection's one model is the cheapest allowed. |
| 167 | func TestSetModelsRevisionAndBinding(t *testing.T) { |
| 168 | r := newRig(t, 1_000_000, 10_000_000) |
| 169 | ctx := context.Background() |
| 170 | set := func(conn string, allowed []string, rev int64) (bool, error) { |
| 171 | var ok bool |
| 172 | err := r.m.tx(ctx, func(tx *sql.Tx) error { |
| 173 | var err error |
| 174 | ok, err = SetModels(ctx, tx, tx1, conn, allowed, rev) |
| 175 | return err |
| 176 | }) |
| 177 | return ok, err |
| 178 | } |
| 179 | unchanged := func(what string) { |
| 180 | t.Helper() |
| 181 | got, _ := Models(ctx, r.db, tx1) |
| 182 | c, _, _ := Get(ctx, r.db, tx1) |
| 183 | if len(got) != 1 || got[0].ID != "z-ai/glm-5.3" || c.ModelsRevision != 1 { |
| 184 | t.Fatalf("%s changed the set: %+v at %d", what, got, c.ModelsRevision) |
| 185 | } |
| 186 | } |
| 187 | if ok, err := set("cn_1", []string{"z-ai/glm-5.3", "moonshotai/kimi-k3"}, 1); ok || err != nil { |
| 188 | t.Fatalf("revision 1 again applied: %v %v", ok, err) |
| 189 | } |
| 190 | unchanged("revision 1 again") |
| 191 | if ok, err := set("cn_other", []string{"z-ai/glm-5.3", "moonshotai/kimi-k3"}, 2); ok || err != nil { |
| 192 | t.Fatalf("another connection's set applied: %v %v", ok, err) |
| 193 | } |
| 194 | unchanged("another connection's set") |
| 195 | if ok, err := set("cn_1", []string{"z-ai/glm-5.3", "vendor/unpriced"}, 2); ok || err == nil { |
| 196 | t.Fatalf("an unpriced model allowed: %v %v", ok, err) |
| 197 | } |
| 198 | if ok, err := set("cn_1", []string{}, 2); ok || err == nil { |
| 199 | t.Fatalf("an empty set applied: %v %v", ok, err) |
| 200 | } |
| 201 | // Security round #3: a connection from before S5b has no list of the |
| 202 | // key's models yet: its set can shrink or stay, never grow. |
| 203 | if ok, err := set("cn_1", []string{"z-ai/glm-5.3", "moonshotai/kimi-k3"}, 2); ok || err == nil { |
| 204 | t.Fatalf("a model added with no list of the key's: %v %v", ok, err) |
| 205 | } |
| 206 | unchanged("an addition with no list") |
| 207 | // With the key's list: only models it lists, priced and tiered. |
| 208 | if err := r.m.tx(ctx, func(tx *sql.Tx) error { |
| 209 | return SetListed(ctx, tx, tx1, []string{"z-ai/glm-5.3", "moonshotai/kimi-k3", "z-ai/glm-5.3-flash", "vendor/unpriced"}) |
| 210 | }); err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | if ok, err := set("cn_1", []string{"z-ai/glm-5.3", "z-ai/glm-5.2"}, 2); ok || err == nil { |
| 214 | t.Fatalf("a priced, tiered model the key does not list allowed: %v %v", ok, err) |
| 215 | } |
| 216 | unchanged("a model the key does not list") |
| 217 | if ok, err := set("cn_1", []string{"z-ai/glm-5.3", "moonshotai/kimi-k3", "z-ai/glm-5.3-flash"}, 2); !ok || err != nil { |
| 218 | t.Fatalf("revision 2: %v %v", ok, err) |
| 219 | } |
| 220 | got, _ := Models(ctx, r.db, tx1) |
| 221 | want := []Model{{ID: "moonshotai/kimi-k3", State: wire.ModelUnprobed}, {ID: "z-ai/glm-5.3", State: wire.ModelHealthy}, {ID: "z-ai/glm-5.3-flash", State: wire.ModelUnprobed}} |
| 222 | if !slices.Equal(got, want) { |
| 223 | t.Fatalf("set %+v", got) |
| 224 | } |
| 225 | c, _, _ := Get(ctx, r.db, tx1) |
| 226 | if c.ModelsRevision != 2 || c.Model != "z-ai/glm-5.3-flash" { |
| 227 | t.Fatalf("revision %d, model %s", c.ModelsRevision, c.Model) |
| 228 | } |
| 229 | if ok, _ := set("cn_1", []string{"z-ai/glm-5.3-flash"}, 3); !ok { |
| 230 | t.Fatal("revision 3") |
| 231 | } |
| 232 | if got, _ := Models(ctx, r.db, tx1); len(got) != 1 || got[0].ID != "z-ai/glm-5.3-flash" { |
| 233 | t.Fatalf("removed models stayed: %+v", got) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // §16.6 (a) and (g) at the guard: a step is admitted only on an allowed, |
| 238 | // eligible, healthy model; a probe on an allowed, eligible one; a |
| 239 | // question on an allowed one that is not unhealthy. A removed model is |
| 240 | // never admitted again. Nothing names no model but a probe. |
| 241 | func TestGuardAdmitsOnlyAllowedModels(t *testing.T) { |
| 242 | r := newRig(t, 1_000_000, 10_000_000) |
| 243 | ctx := context.Background() |
| 244 | if err := r.m.tx(ctx, func(tx *sql.Tx) error { |
| 245 | if err := SetListed(ctx, tx, tx1, []string{"z-ai/glm-5.3", "moonshotai/kimi-k3", "z-ai/glm-5.2", "z-ai/glm-5.3-flash"}); err != nil { |
| 246 | return err |
| 247 | } |
| 248 | _, err := SetModels(ctx, tx, tx1, "cn_1", []string{"z-ai/glm-5.3", "moonshotai/kimi-k3", "z-ai/glm-5.2"}, 2) |
| 249 | if err != nil { |
| 250 | return err |
| 251 | } |
| 252 | return MarkModel(ctx, tx, tx1, "z-ai/glm-5.2", wire.ModelUnhealthy, wire.ModelNotFound, r.clock) |
| 253 | }); err != nil { |
| 254 | t.Fatal(err) |
| 255 | } |
| 256 | for _, c := range []struct { |
| 257 | h Holder |
| 258 | admit bool |
| 259 | }{ |
| 260 | {Holder{Step: "a", Attempt: 1, Role: RoleEngineering, Model: "z-ai/glm-5.3"}, true}, |
| 261 | {Holder{Step: "b", Attempt: 1, Role: RoleEngineering, Model: "moonshotai/kimi-k3"}, false}, |
| 262 | {Holder{Step: "c", Attempt: 1, Role: RoleProbe, Model: "moonshotai/kimi-k3"}, true}, |
| 263 | {Holder{Step: "d", Attempt: 1, Role: RoleReview, Model: "z-ai/glm-5.2"}, false}, |
| 264 | {Holder{Step: "e", Attempt: 1, Role: RoleAsk, Model: "z-ai/glm-5.2"}, false}, |
| 265 | {Holder{Step: "f", Attempt: 1, Role: RoleAsk, Model: "z-ai/glm-5.3"}, true}, |
| 266 | {Holder{Step: "g", Attempt: 1, Role: RoleReview, Model: "z-ai/glm-5.3-flash"}, false}, |
| 267 | {Holder{Step: "h", Attempt: 1, Role: RoleProbe, Model: "vendor/unpriced"}, false}, |
| 268 | {Holder{Step: "i", Attempt: 1, Role: RoleEngineering}, false}, |
| 269 | {Holder{Step: "j", Attempt: 1, Role: RoleAsk}, false}, |
| 270 | } { |
| 271 | ran := false |
| 272 | a, _, err := r.m.Run(ctx, tx1, c.h, func(_ context.Context, l Launch) Result { |
| 273 | ran = true |
| 274 | return Result{Ending: Ending{Proven: true, NotStarted: true}} |
| 275 | }, nil) |
| 276 | if err != nil || a.Admitted != c.admit || ran != c.admit || !c.admit && a.Hold != HoldModel { |
| 277 | t.Fatalf("%+v: admitted %v (%+v) ran %v %v", c.h, a.Admitted, a, ran, err) |
| 278 | } |
| 279 | } |
| 280 | // Removed: never admitted again. |
| 281 | if err := r.m.tx(ctx, func(tx *sql.Tx) error { |
| 282 | _, err := SetModels(ctx, tx, tx1, "cn_1", []string{"moonshotai/kimi-k3"}, 3) |
| 283 | return err |
| 284 | }); err != nil { |
| 285 | t.Fatal(err) |
| 286 | } |
| 287 | for _, role := range []string{RoleEngineering, RoleAsk, RoleProbe} { |
| 288 | if a, _, _ := r.m.Run(ctx, tx1, Holder{Step: "k-" + role, Attempt: 1, Role: role, Model: "z-ai/glm-5.3"}, harness(), nil); a.Admitted { |
| 289 | t.Fatalf("%s admitted on a removed model", role) |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // §16.5: a usage row naming another model than the one chosen is marked |
| 295 | // model_mismatch, charged at the reported model's price (spend unknown |
| 296 | // when it has none), and makes the chosen model unhealthy until Check |
| 297 | // again. Each row carries the reported model's family and tier and the |
| 298 | // tier version. |
| 299 | func TestChargeModelMismatch(t *testing.T) { |
| 300 | r := newRig(t, 1_000_000, 10_000_000) |
| 301 | ctx := context.Background() |
| 302 | h := step("s1", RoleEngineering) |
| 303 | var priced []wire.Usage |
| 304 | if _, _, err := r.m.Run(ctx, tx1, h, harness(usage("z-ai/glm-5.3-flash", 1, 1_000_000, 0)), func(_ *sql.Tx, rows []wire.Usage) error { priced = rows; return nil }); err != nil { |
| 305 | t.Fatal(err) |
| 306 | } |
| 307 | if len(priced) != 1 || !priced[0].ModelMismatch || priced[0].CostMicros == nil || *priced[0].CostMicros != 200_000 || priced[0].Family != "glm" || priced[0].Tier != TierCheap || priced[0].TierVersion != CurrentTiers { |
| 308 | t.Fatalf("priced %+v", priced) |
| 309 | } |
| 310 | ms, _ := Models(ctx, r.db, tx1) |
| 311 | if len(ms) != 1 || ms[0].State != wire.ModelUnhealthy || ms[0].Reason != wire.ModelMismatch || !ms[0].FailedAt.Equal(r.clock) { |
| 312 | t.Fatalf("chosen model %+v", ms) |
| 313 | } |
| 314 | // The same model reported: no mismatch (proven healthy again first). |
| 315 | if err := r.m.tx(ctx, func(tx *sql.Tx) error { |
| 316 | return MarkModel(ctx, tx, tx1, "z-ai/glm-5.3", wire.ModelHealthy, "", r.clock) |
| 317 | }); err != nil { |
| 318 | t.Fatal(err) |
| 319 | } |
| 320 | if _, _, err := r.m.Run(ctx, tx1, step("s2", RoleEngineering), harness(usage("z-ai/glm-5.3", 1, 10, 10)), func(_ *sql.Tx, rows []wire.Usage) error { priced = rows; return nil }); err != nil { |
| 321 | t.Fatal(err) |
| 322 | } |
| 323 | if priced[0].ModelMismatch || priced[0].Tier != TierStrong { |
| 324 | t.Fatalf("same model %+v", priced[0]) |
| 325 | } |
| 326 | // An unpriced model reported: the spend is unknown. |
| 327 | if _, _, err := r.m.Run(ctx, tx1, step("s3", RoleEngineering), harness(usage("vendor/other", 1, 10, 10)), func(_ *sql.Tx, rows []wire.Usage) error { priced = rows; return nil }); err != nil { |
| 328 | t.Fatal(err) |
| 329 | } |
| 330 | if !priced[0].ModelMismatch || priced[0].CostMicros != nil || !r.spend().Unknown { |
| 331 | t.Fatalf("unpriced mismatch %+v, spend %+v", priced[0], r.spend()) |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // An unhealthy model may be retried on demand a day after it failed. |
| 336 | func TestModelRetryable(t *testing.T) { |
| 337 | at := time.Date(2026, 11, 12, 10, 0, 0, 0, time.UTC) |
| 338 | m := Model{ID: "m", State: wire.ModelUnhealthy, FailedAt: at} |
| 339 | if m.Retryable(at.Add(RetryAfter-time.Second)) || !m.Retryable(at.Add(RetryAfter)) { |
| 340 | t.Fatal("retry boundary") |
| 341 | } |
| 342 | // Security round #6: a model_mismatch stays until Check again (§16.5), |
| 343 | // however long ago. |
| 344 | if (Model{ID: "m", State: wire.ModelUnhealthy, Reason: wire.ModelMismatch, FailedAt: at}).Retryable(at.Add(30 * 24 * time.Hour)) { |
| 345 | t.Fatal("a mismatch retried by time") |
| 346 | } |
| 347 | if (Model{State: wire.ModelUnhealthy}).Retryable(at) || (Model{State: wire.ModelHealthy, FailedAt: at}).Retryable(at.Add(48*time.Hour)) { |
| 348 | t.Fatal("retryable without a failure time, or while healthy") |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // §16.6 (a), (d) and (g) as properties of the guard, over random sets, |
| 353 | // states and attempts: a step is admitted only on an allowed, eligible, |
| 354 | // healthy model (so never on one whose first-use probe has not settled |
| 355 | // healthy); a question only on an allowed, priced model that is not |
| 356 | // unhealthy; a probe only on an allowed, eligible model; nothing naming |
| 357 | // no model but a probe. |
| 358 | func TestGuardModelInvariants(t *testing.T) { |
| 359 | r := newRig(t, 1_000_000_000, 10_000_000_000) |
| 360 | ctx := context.Background() |
| 361 | rnd := rand.New(rand.NewPCG(5, 8)) |
| 362 | models := slices.Sorted(maps.Keys(Tables[Current[wire.ProviderTensorX]].Models)) |
| 363 | candidates := append(slices.Clone(models), "vendor/unpriced", "") |
| 364 | states := []string{wire.ModelUnprobed, wire.ModelHealthy, wire.ModelUnhealthy} |
| 365 | admitted := 0 |
| 366 | for n := range 600 { |
| 367 | var allowed []string |
| 368 | for _, m := range models { |
| 369 | if rnd.IntN(3) == 0 { |
| 370 | allowed = append(allowed, m) |
| 371 | } |
| 372 | } |
| 373 | if len(allowed) == 0 { |
| 374 | allowed = []string{models[rnd.IntN(len(models))]} |
| 375 | } |
| 376 | state := map[string]string{} |
| 377 | if err := r.m.tx(ctx, func(tx *sql.Tx) error { |
| 378 | if err := InitModels(ctx, tx, tx1, allowed, int64(n+2)); err != nil { |
| 379 | return err |
| 380 | } |
| 381 | for _, m := range allowed { |
| 382 | state[m] = states[rnd.IntN(3)] |
| 383 | if err := MarkModel(ctx, tx, tx1, m, state[m], "x", r.clock); err != nil { |
| 384 | return err |
| 385 | } |
| 386 | } |
| 387 | return nil |
| 388 | }); err != nil { |
| 389 | t.Fatal(err) |
| 390 | } |
| 391 | role := []string{RoleEngineering, RoleReview, RoleAsk, RoleProbe}[rnd.IntN(4)] |
| 392 | model := candidates[rnd.IntN(len(candidates))] |
| 393 | h := Holder{Step: fmt.Sprintf("s%d", n), Attempt: 1, Role: role, Model: model} |
| 394 | a, _, err := r.m.Run(ctx, tx1, h, func(context.Context, Launch) Result { return Result{Ending: Ending{Proven: true, NotStarted: true}} }, nil) |
| 395 | if err != nil { |
| 396 | t.Fatal(err) |
| 397 | } |
| 398 | in := slices.Contains(allowed, model) |
| 399 | _, _, eligible := Eligible(wire.ProviderTensorX, Current[wire.ProviderTensorX], model) |
| 400 | var want bool |
| 401 | switch { |
| 402 | case model == "": |
| 403 | want = role == RoleProbe |
| 404 | case role == RoleProbe: |
| 405 | want = in && eligible |
| 406 | case role == RoleAsk: |
| 407 | want = in && eligible && state[model] != wire.ModelUnhealthy |
| 408 | default: |
| 409 | want = in && eligible && state[model] == wire.ModelHealthy |
| 410 | } |
| 411 | if a.Admitted != want { |
| 412 | t.Fatalf("#%d: %s on %q (allowed %v, state %q): admitted %v", n, role, model, in, state[model], a.Admitted) |
| 413 | } |
| 414 | if a.Admitted { |
| 415 | admitted++ |
| 416 | } |
| 417 | } |
| 418 | if admitted < 60 { |
| 419 | t.Fatalf("the generator rarely admits: %d", admitted) |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // A key's list is bounded for its card and hello: the models that can |
| 424 | // be allowed first. |
| 425 | func TestCapChoices(t *testing.T) { |
| 426 | var cs []wire.ModelChoice |
| 427 | for i := range wire.MaxModels + 50 { |
| 428 | cs = append(cs, wire.ModelChoice{ID: fmt.Sprintf("m%03d", i), Eligible: i >= wire.MaxModels}) |
| 429 | } |
| 430 | out := CapChoices(cs) |
| 431 | if len(out) != wire.MaxModels || !out[0].Eligible || out[0].ID != fmt.Sprintf("m%03d", wire.MaxModels) || !out[49].Eligible || out[50].Eligible { |
| 432 | t.Fatalf("capped %d, first %+v", len(out), out[0]) |
| 433 | } |
| 434 | if got := CapChoices(cs[:3]); len(got) != 3 { |
| 435 | t.Fatal("a short list cut") |
| 436 | } |
| 437 | } |
| 438 | |