"""Choosing the cheapest worker that is capable enough for a kind and difficulty, from the best evidence about each worker: judged record (exact, a harder difficulty, all kinds), then benchmark standing against other workers' standing only, then nothing known (difficulty 1 only). Requirement ids: R-7.2-1, R-7.2-2, R-7.2-3, R-7.3-3. Property tests are seeded and scale with SISI_TEST_ITERS.""" import json, os, random, unittest from support import * from sisi import routing KIND = "research" def cand(name, cost=None, standing=None, plan="per-use", allowance=None, maker=None, usable=True): w = "t/%s" % name return {"worker": w, "tool": "t", "model": name, "maker": maker or name.split("-")[0], "plan": plan, "cost": cost, "allowance_pct_left": allowance, "usable": usable, "reason": None, "standing": standing, "standing_source": "fixture benchmark", "standing_date": "2000-01-01", "aliases": [w, name]} _n = [0] def attempts(worker, kind, difficulty, good, bad, kind_name="attempt"): rows = [] for i in range(good + bad): _n[0] += 1 rows.append({"kind": kind_name, "id": "x%d" % _n[0], "type": kind, "difficulty": difficulty, "worker": worker, "outcome": "accepted" if i < good else "rejected", "check": "tests"}) return rows def pick(cands, records, difficulty, kind=KIND, **kw): return routing.choose(cands, records, {}, kind, difficulty, now=1000.0, **kw) def chosen(cands, records, difficulty, **kw): r = pick(cands, records, difficulty, **kw) return r["choice"]["worker"] if r["choice"] else None class SufficientCapabilityTests(unittest.TestCase): def fleet(self): # the shape that went wrong: the cheapest worker has the weakest standing return [cand("weak-1", cost=0.2, standing=40.0), cand("mid-1", cost=4.0, standing=64.0), cand("top-1", cost=8.0, standing=66.0)] def test_a_hard_task_goes_to_a_worker_near_the_top_not_the_cheapest(self): """R-7.2-2: difficulty 3 needs at least 85 percent of the best standing; the cheapest of those wins.""" self.assertEqual(chosen(self.fleet(), [], 3), "t/mid-1") self.assertEqual(chosen(self.fleet(), [], 1), "t/weak-1") # no floor at difficulty 1 self.assertEqual(chosen(self.fleet(), [], 2), "t/weak-1") # 40 is 60.6 percent of 66, above the 60 percent floor def test_the_floor_is_configurable(self): got = chosen(self.fleet(), [], 2, floors={"2": 80}) self.assertEqual(got, "t/mid-1") self.assertEqual(chosen(self.fleet(), [], 3, floors={3: 100}), "t/top-1") def test_a_flat_plan_with_low_standing_does_not_take_a_hard_task(self): cands = [cand("flat-1", standing=30.0, plan="flat", allowance=80), cand("pay-1", cost=5.0, standing=64.0)] self.assertEqual(chosen(cands, [], 3), "t/pay-1") self.assertEqual(chosen(cands, [], 2), "t/pay-1") # 30 is under 60 percent of 64 self.assertEqual(chosen(cands, [], 1), "t/flat-1") # flat first where nothing is needed def test_a_judged_record_proves_capability_whatever_the_standing(self): """The record outranks the benchmark: 9 of 10 at this kind and difficulty beats a low standing.""" cands = self.fleet() recs = attempts("t/weak-1", KIND, 3, 9, 1) r = pick(cands, recs, 3) self.assertEqual(r["choice"]["worker"], "t/weak-1") self.assertEqual(r["assessments"]["t/weak-1"]["tier"], "a") def test_a_harder_difficulty_of_the_same_kind_proves_an_easier_one(self): cands = [cand("a-1", cost=1.0), cand("b-1", cost=2.0)] recs = attempts("t/b-1", KIND, 3, 10, 0) r = pick(cands, recs, 2) self.assertEqual(r["choice"]["worker"], "t/b-1") self.assertEqual(r["assessments"]["t/b-1"]["tier"], "b-higher") self.assertFalse(r["assessments"]["t/a-1"]["capable"]) def test_an_easier_difficulty_is_weak_evidence_used_only_as_a_last_resort(self): cands = [cand("a-1", cost=1.0), cand("b-1", cost=2.0)] recs = attempts("t/b-1", KIND, 1, 10, 0) r = pick(cands, recs, 3) self.assertEqual(r["choice"]["worker"], "t/b-1") self.assertEqual(r["basis"], "weak") # with a capable worker on hand, the weak evidence does not count cands2 = cands + [cand("c-1", cost=9.0, standing=70.0)] r2 = pick(cands2, recs, 3) self.assertEqual(r2["choice"]["worker"], "t/c-1") self.assertEqual(r2["basis"], "capable") def test_the_same_difficulty_across_all_kinds_counts_when_the_kind_has_no_record(self): cands = [cand("a-1", cost=1.0), cand("b-1", cost=2.0)] recs = attempts("t/b-1", "other-kind", 2, 6, 0) + attempts("t/b-1", "third-kind", 2, 5, 1) r = pick(cands, recs, 2) self.assertEqual(r["choice"]["worker"], "t/b-1") self.assertEqual(r["assessments"]["t/b-1"]["tier"], "b-all") def test_unknown_workers_are_an_exploration_at_difficulty_one_only(self): cands = [cand("a-1", cost=1.0), cand("b-1", cost=2.0, standing=50.0)] r1 = pick(cands, [], 1) self.assertEqual(r1["choice"]["worker"], "t/a-1") self.assertEqual(r1["assessments"]["t/a-1"]["tier"], "unknown") r2 = pick(cands, [], 2) self.assertEqual(r2["choice"]["worker"], "t/b-1") self.assertFalse(r2["assessments"]["t/a-1"]["capable"]) def test_all_workers_unknown_still_gives_an_answer_and_says_nothing_is_proven(self): cands = [cand("a-1", cost=1.0), cand("b-1", cost=2.0)] for d in (2, 3): r = pick(cands, [], d) self.assertEqual(r["choice"]["worker"], "t/a-1") self.assertEqual(r["basis"], "nothing") def test_no_capable_worker_falls_back_to_the_strongest_known(self): """When the best-standing worker is itself out (a poor record), nobody clears the floor: the strongest known usable worker is taken and the result says that nothing is proven.""" cands = [cand("great-1", cost=1.0, standing=99.0), cand("ok-1", cost=3.0, standing=70.0), cand("low-1", cost=0.5, standing=50.0)] recs = attempts("t/great-1", KIND, 3, 3, 7) r = pick(cands, recs, 3) self.assertEqual(r["choice"]["worker"], "t/ok-1") self.assertEqual(r["basis"], "strongest") def test_a_great_benchmark_never_rescues_a_thick_bad_record(self): cands = [cand("great-1", cost=1.0, standing=99.0), cand("ok-1", cost=30.0, standing=70.0)] recs = attempts("t/great-1", KIND, 3, 3, 7) self.assertEqual(chosen(cands, recs, 3), "t/ok-1") # a bad record at difficulty 3 says nothing about difficulty 1: there the cheaper great-1 is fine self.assertEqual(chosen(cands, recs, 1), "t/great-1") def test_a_bad_record_at_an_easier_difficulty_also_blocks_the_benchmark(self): cands = [cand("great-1", cost=1.0, standing=99.0), cand("ok-1", cost=30.0, standing=98.0)] recs = attempts("t/great-1", KIND, 2, 2, 8) self.assertEqual(chosen(cands, recs, 3), "t/ok-1") self.assertEqual(chosen(cands, recs, 2), "t/ok-1") def test_a_thin_record_of_repeated_rejections_cancels_the_prior(self): cands = [cand("cheap-1", cost=0.2, standing=40.0), cand("mid-1", cost=4.0, standing=64.0)] recs = attempts("t/cheap-1", KIND, 2, 0, 2) r = pick(cands, recs, 2) self.assertEqual(r["choice"]["worker"], "t/mid-1") self.assertTrue(r["assessments"]["t/cheap-1"]["contradicted"]) def test_every_usable_worker_poor_still_names_the_best_rate(self): cands = [cand("a-1", cost=1.0), cand("b-1", cost=2.0)] recs = attempts("t/a-1", KIND, 2, 1, 9) + attempts("t/b-1", KIND, 2, 4, 6) r = pick(cands, recs, 2) self.assertEqual(r["choice"]["worker"], "t/b-1") self.assertEqual(r["basis"], "all-bad") def test_equal_evidence_and_price_is_stable_and_explained(self): cands = [cand("b-1", cost=2.0, standing=60.0), cand("a-1", cost=2.0, standing=60.0)] for _ in range(3): r = pick(list(reversed(cands)), [], 3) self.assertEqual(r["choice"]["worker"], "t/a-1") text = " ".join(reason for _w, reason in r["passed_over"]) self.assertIn("equal on price and load", text) def test_a_single_usable_worker_is_recommended_whatever_it_knows(self): for d in (1, 2, 3): self.assertEqual(chosen([cand("only-1")], [], d), "t/only-1") self.assertEqual(chosen([cand("only-1", standing=5.0)], [], d), "t/only-1") self.assertEqual(pick([cand("only-1")], [], 3)["basis"], "nothing") def test_the_existing_constraints_still_hold(self): cands = [cand("a-1", cost=1.0, standing=60.0), cand("b-1", cost=2.0, standing=60.0), cand("c-contributor-1", cost=0.1, standing=60.0), cand("d-1", cost=0.5, standing=60.0, usable=False)] r = pick(cands, [], 2, review_of="t/a-1") self.assertEqual(r["choice"]["worker"], "t/b-1") # another maker reviews reasons = dict(r["passed_over"]) self.assertIn("trains on prompts", reasons["t/c-contributor-1"]) self.assertIn("t/d-1", reasons) avoided = pick(cands, [], 2, avoid=[{"pattern": "a-1", "why": "mine"}]) self.assertEqual(avoided["choice"]["worker"], "t/b-1") preferred = pick(cands, [], 2, prefer=[{"pattern": "b-1"}]) self.assertEqual(preferred["choice"]["worker"], "t/b-1") down = routing.choose(cands, [], {"t": {"retry_at": 5000.0, "reason": "limit"}}, KIND, 2, now=1000.0) self.assertIsNone(down["choice"]) def test_the_two_scales_are_never_compared(self): """R-7.2-2: V (standing 60, the best) is cheaper than W (standing 50, but 10 of 10 accepted here). At difficulty 3 both are capable (V on 60 against its own best, W on its record) and the cheapest, V, wins. A comparison of W's accepted rate (100) with a standing would make 100 the 'best' and V (60) incapable.""" cands = [cand("v-1", cost=1.0, standing=60.0), cand("w-1", cost=5.0, standing=50.0)] recs = attempts("t/w-1", KIND, 3, 10, 0) r = pick(cands, recs, 3) self.assertEqual(r["choice"]["worker"], "t/v-1") self.assertEqual(r["best_standing"], 60.0) def test_a_standing_scaled_by_any_factor_changes_nothing(self): rng = random.Random(7) for _ in range(60 * ITERS): cands, recs = gen_world(rng) for d in (1, 2, 3): base = pick(cands, recs, d) for k in (0.01, 0.37, 3.0, 40.0): scaled = [dict(c, standing=(c["standing"] * k if c["standing"] is not None else None)) for c in cands] got = pick(scaled, recs, d) self.assertEqual((got["choice"] or {}).get("worker"), (base["choice"] or {}).get("worker"), (d, k)) self.assertEqual(got["basis"], base["basis"]) def gen_world(rng, kinds=("research", "edit")): """Generated workers and a judged ledger: prices, plans, standings (or none), cells of every size.""" cands, recs = [], [] for i in range(rng.randint(1, 6)): flat = rng.random() < 0.2 c = cand("w%d-%d" % (i, rng.randint(0, 9)), cost=None if flat else rng.choice([None, 0.2, 1.0, 4.0, 8.0, 30.0]), standing=rng.choice([None, None, rng.uniform(10, 100)]), plan="flat" if flat else rng.choice(["per-use", None]), allowance=rng.choice([None, 40, 90]) if flat else None) cands.append(c) for kind in kinds: for d in (1, 2, 3): n = rng.choice([0, 0, 2, 3, 8, 8, 12, 30]) good = rng.randint(0, n) recs += attempts(c["worker"], kind, d, good, n - good) rng.shuffle(recs) return cands, recs class CapabilityPropertyTests(unittest.TestCase): def test_raising_the_difficulty_never_gives_weaker_evidence(self): """R-7.2-2, R-7.2-3: for the same data, a worker capable at difficulty d+1 is capable at d, its evidence at d is at least as strong, and the worker chosen for a harder task on evidence is capable on the easier one.""" rng = random.Random(20261123) for _ in range(300 * ITERS): cands, recs = gen_world(rng) runs = {d: pick(cands, recs, d) for d in (1, 2, 3)} for d in (1, 2): lo, hi = runs[d]["assessments"], runs[d + 1]["assessments"] for w in lo: self.assertLessEqual(hi[w]["capable"], lo[w]["capable"], (d, w, lo[w], hi[w])) self.assertGreaterEqual(lo[w]["level"], hi[w]["level"], (d, w, lo[w], hi[w])) got = runs[d + 1]["choice"] if got is not None and runs[d + 1]["basis"] == "capable": self.assertTrue(lo[got["worker"]]["capable"], (d, got["worker"])) def test_a_capable_worker_that_is_as_good_and_cheaper_is_never_passed_over(self): """R-7.2-2: the choice is the cheapest of the capable workers (a prefer pattern aside, none here).""" rng = random.Random(4242) for _ in range(300 * ITERS): cands, recs = gen_world(rng) for d in (1, 2, 3): r = pick(cands, recs, d) if r["choice"] is None or r["basis"] != "capable": continue x = r["choice"] price = lambda c: (routing._price_tier(c), c["cost"] if c.get("cost") is not None else float("inf")) for c in cands: a = r["assessments"].get(c["worker"]) if a and a["capable"] and c["worker"] != x["worker"]: self.assertGreaterEqual(price(c), price(x), (d, c["worker"], x["worker"])) def test_a_thick_bad_record_is_never_chosen_over_a_usable_worker_without_one(self): """R-7.2-3, R-7.3-3.""" rng = random.Random(99) seen = 0 for _ in range(400 * ITERS): cands, recs = gen_world(rng) for d in (1, 2, 3): r = pick(cands, recs, d) if r["choice"] is None: continue cell = routing._cell(r["stats"], r["choice"]["worker"], KIND, d) if cell["n"] >= routing.MIN_ATTEMPTS and cell["good"] / cell["n"] < routing.MIN_ACCEPTED_RATE: seen += 1 self.assertTrue(all(a["bad"] for a in r["assessments"].values()), (d, r["choice"]["worker"])) self.assertGreater(seen, 0, "the generator never produced the case it is meant to test") def test_the_choice_does_not_depend_on_candidate_order(self): rng = random.Random(5) for _ in range(120 * ITERS): cands, recs = gen_world(rng) d = rng.choice([1, 2, 3]) a = chosen(cands, recs, d) b = chosen(list(reversed(cands)), recs, d) self.assertEqual(a, b) def explain(cands, records, difficulty, **kw): r = pick(cands, records, difficulty, **kw) return r, routing.explain_choice(r, KIND, difficulty) def line_with(lines, start): got = [l for l in lines if l.startswith(start)] return got class ExplainTests(unittest.TestCase): """R-7.2-1, R-7.2-2: --explain says which evidence decided, why cheaper workers were not capable enough, and when the choice is an exploration or a fallback; one line each, 20 lines at most.""" def test_the_evidence_for_each_tier_is_one_line_with_its_numbers(self): cases = [] cands = [cand("w-1", cost=1.0)] _r, lines = explain(cands, attempts("t/w-1", KIND, 3, 9, 1), 3) cases.append((lines, ["evidence (a)", "9 of 10 accepted", "difficulty 3", "90%"])) _r, lines = explain(cands, attempts("t/w-1", KIND, 3, 10, 0), 2) cases.append((lines, ["evidence (b)", "harder difficulty", "10 of 10", "difficulty 3", "proves difficulty 2"])) _r, lines = explain(cands, attempts("t/w-1", "other", 2, 8, 2), 2) cases.append((lines, ["evidence (b)", "all kinds", "8 of 10", "difficulty 2"])) _r, lines = explain([cand("w-1", cost=1.0, standing=64.0), cand("w-2", cost=5.0, standing=66.0)], [], 3) cases.append((lines, ["evidence (c)", "benchmark standing", "64", "fixture benchmark", "2000-01-01", "85%", "66"])) _r, lines = explain(cands, [], 1) cases.append((lines, ["evidence (d)", "nothing is known", "exploration"])) _r, lines = explain(cands, attempts("t/w-1", KIND, 1, 10, 0), 3) cases.append((lines, ["evidence (b, weak)", "10 of 10", "easier difficulty 1"])) for lines, words in cases: ev = line_with(lines, "evidence") self.assertEqual(len(ev), 1, lines) for w in words: self.assertIn(w, ev[0], lines) def test_standing_and_accepted_rate_are_named_apart(self): _r, a = explain([cand("w-1", cost=1.0)], attempts("t/w-1", KIND, 3, 9, 1), 3) _r, c = explain([cand("w-1", cost=1.0, standing=64.0)], [], 3) self.assertNotIn("standing", line_with(a, "evidence")[0]) self.assertNotIn("accepted", line_with(c, "evidence")[0]) def test_cheaper_workers_that_were_not_capable_are_one_grouped_line_with_a_count(self): cands = [cand("c%d-1" % i, cost=0.1 + i / 100, standing=10.0 + i) for i in range(5)] cands += [cand("good-1", cost=4.0, standing=64.0), cand("best-1", cost=8.0, standing=66.0)] r, lines = explain(cands, [], 3) self.assertEqual(r["choice"]["worker"], "t/good-1") grp = [l for l in lines if "not capable enough" in l] self.assertEqual(len(grp), 1, lines) self.assertIn("passed over 5 worker(s)", grp[0]) self.assertIn("cheaper than the choice", grp[0]) self.assertIn("56.1", grp[0]) # the standing needed: 85 percent of 66 = 56.1 self.assertIn("and 2 more", grp[0]) # three example names at most self.assertLessEqual(len(lines) + routing.EXPLAIN_TAIL_LINES, routing.MAX_EXPLAIN_LINES) def test_a_fallback_says_plainly_that_nothing_is_proven(self): cands = [cand("a-1", cost=1.0, standing=99.0), cand("b-1", cost=3.0, standing=70.0)] r, lines = explain(cands, attempts("t/a-1", KIND, 3, 2, 8), 3) self.assertEqual(r["basis"], "strongest") head = lines[0] self.assertIn("none is proven capable", head) self.assertIn("strongest known", head) self.assertEqual(len(line_with(lines, "evidence")), 1) _r, cold = explain([cand("a-1"), cand("b-1")], [], 3) self.assertIn("none is proven capable", cold[0]) self.assertIn("nothing is recorded", cold[0] + cold[1]) def test_an_exploration_is_called_one(self): cands = [cand("a-1", cost=5.0, standing=90.0), cand("b-1", cost=5.0, standing=10.0)] recs = attempts("t/a-1", "read", 1, 9, 0) r = routing.choose(cands, recs, {}, "read", 1, now=1000.0) self.assertEqual(r["choice"]["worker"], "t/b-1") lines = routing.explain_choice(r, "read", 1) self.assertTrue(line_with(lines, "this is an exploration pick"), lines) def test_the_explanation_stays_within_twenty_lines_with_many_workers(self): rng = random.Random(3) for _ in range(80 * ITERS): cands = [cand("w%d-%d" % (i, rng.randint(0, 5)), cost=rng.choice([None, 0.1, 2.0]), standing=rng.choice([None, rng.uniform(5, 100)])) for i in range(rng.randint(1, 60))] cands = list({c["worker"]: c for c in cands}.values()) d = rng.choice([1, 2, 3]) _r, lines = explain(cands, [], d) self.assertLessEqual(len(lines) + routing.EXPLAIN_TAIL_LINES, routing.MAX_EXPLAIN_LINES, lines) class ExplainCliTests(SisiCase): def seed_world(self): os.makedirs(self.data, exist_ok=True) rows = [] for w, d, good, bad in (("wk-mid", 2, 9, 1), ("wk-weak", 2, 0, 2)): for i in range(good + bad): rows.append({"kind": "attempt", "id": "%s-%d" % (w, i), "ts": 1760000000 + i, "project": "p", "type": "research", "difficulty": d, "worker": w, "outcome": "accepted" if i < good else "rejected", "check": "tests", "minutes": 1}) open(os.path.join(self.data, "delegations.jsonl"), "w").write("".join(json.dumps(r) + "\n" for r in rows)) json.dump({"models": {"wk-weak": {"standing": 40, "source": "fixture board", "date": "2000-01-01"}, "wk-mid": {"standing": 64, "source": "fixture board", "date": "2000-01-01"}, "wk-top": {"standing": 66, "source": "fixture board", "date": "2000-01-01"}}}, open(os.path.join(self.data, "model-strength.json"), "w")) json.dump({"models": {"wk-weak": {"cost_per_million_tokens": 0.2, "source": "fixture", "date": "2000-01-01"}, "wk-mid": {"cost_per_million_tokens": 4, "source": "fixture", "date": "2000-01-01"}, "wk-top": {"cost_per_million_tokens": 8, "source": "fixture", "date": "2000-01-01"}}}, open(os.path.join(self.data, "worker-prices.json"), "w")) def test_a_hard_task_is_not_given_to_the_cheapest_weak_worker(self): """The reported fault: with prices and standings recorded, a hard research task went to the cheapest, weakest worker. Now it goes to the cheapest worker near the top, and `--explain` says why.""" self.seed_world() env = dict(PATH="") plain = self.sisi("route", "--kind", "research", "--difficulty", "3", env=env) self.assertEqual(plain.returncode, 0, plain.stderr) self.assertEqual(plain.stdout.splitlines()[0], "wk-mid") self.assertLessEqual(len(plain.stdout.splitlines()), 6) self.assertNotIn("evidence", plain.stdout) ex = self.sisi("route", "--kind", "research", "--difficulty", "3", "--explain", env=env) lines = ex.stdout.splitlines() self.assertLessEqual(len(lines), 20, ex.stdout) self.assertEqual(len([l for l in lines if l.startswith("evidence")]), 1, ex.stdout) self.assertIn("evidence (c)", ex.stdout) self.assertTrue([l for l in lines if l.startswith("passed over 1 worker(s) (wk-weak") and "not capable enough" in l], ex.stdout) easy = self.sisi("route", "--kind", "research", "--difficulty", "1", env=env) self.assertEqual(easy.stdout.splitlines()[0], "wk-weak") if __name__ == "__main__": unittest.main()