diff --git a/canon/orchestration.md b/canon/orchestration.md index bef7dac..e5e5d75 100644 --- a/canon/orchestration.md +++ b/canon/orchestration.md @@ -29,8 +29,10 @@ present; nothing here names one. Read on demand: `sisi delegate guide`. counting rework as cost: cheap work that must be redone is expensive. - Evidence decides which workers are capable enough, in this order: the judged record for that kind and difficulty, the nearest judged record, then benchmark - standing against other workers' standing (never against an accepted rate); the - cheapest capable worker wins. A thin record is a reason for care, not a verdict. + standing against other workers' standing (never against an accepted rate). Above + routine work measured proof outranks a benchmark: the cheapest worker with a judged + record wins, and standing decides only when nobody has one. A thin record is a + reason for care, not a verdict. - Ask `sisi route --kind K --difficulty N` before delegating; `--explain` says why. Difficulty is 1 routine, 2 ordinary, 3 hard. Say the guess out loud; the record shows how good the guesses were. diff --git a/docs/workers.md b/docs/workers.md index 37794d6..2927c93 100644 --- a/docs/workers.md +++ b/docs/workers.md @@ -67,10 +67,14 @@ kinds at that difficulty or a harder one (the same kind at an easier difficulty used only when no usable worker is capable otherwise); (c) benchmark standing as a prior, compared only with other workers' standing: at least 85 percent of the best usable standing for difficulty 3, 60 percent for difficulty 2, no floor for difficulty 1 (`route.standing_floor` in the config); -(d) nothing known: difficulty 1 only, as an exploration. A thick bad record at this or an easier +(d) nothing known: difficulty 1 only, as an exploration. Above difficulty 1 a benchmark is a reason +to try a worker, not to prefer it to proof: when any usable worker is capable by a judged record for +the kind (a, or b from a harder difficulty of the kind), only those compete; the record across all +kinds counts as proof only when nobody has evidence from the kind; standing decides only when nobody +has a judged record for the request. A thick bad record at this or an easier difficulty, or a thin one with repeated rejections, keeps a worker off its benchmark. The two scales never meet: a standing is never compared with an accepted rate. Cheapest first among the -capable: flat-rate allowance before per-use prices (blended input and output), unknown prices +competing workers: flat-rate allowance before per-use prices (blended input and output), unknown prices last, then standing, `prefer` patterns first, then load balancing across allowances. When no worker is capable by the evidence, the strongest known usable worker is recommended and `--explain` says that none is proven. About one difficulty-1 task in ten goes to a cheaper thin-record worker diff --git a/sisi/routing.py b/sisi/routing.py index 2d9242d..354dc6d 100644 --- a/sisi/routing.py +++ b/sisi/routing.py @@ -31,7 +31,12 @@ The rule, in order: A worker with a thick bad record (MIN_ATTEMPTS attempts, rate under the bar) for the kind at this or an easier difficulty is never capable, so never rides on a benchmark. A thin record that already shows repeated rejections also cancels the benchmark prior. - 5. Among the capable workers the cheapest wins: a flat-rate plan with allowance left first + 5. Above difficulty 1 a benchmark is a reason to try a worker, not to prefer it to proof: when any + usable worker is capable by a judged record for the kind (evidence a, or b from a harder + difficulty of the kind), only those compete; the record across all kinds counts as proof only + when no usable worker has evidence from the kind; benchmark standing decides only when nobody + has a judged record for the request. At difficulty 1 every capable worker competes. + Among those the cheapest wins: a flat-rate plan with allowance left first (it costs nothing more per task), then the blended price (an unknown price counts as most expensive), then the benchmark standing, then load. A user's `prefer` patterns come first. If no worker is capable, the weak evidence decides; failing that, the strongest known usable @@ -401,9 +406,23 @@ def _make_key(stats, kind, difficulty, prefer): return key, rate, load_ratio -def _select(eligible, assessments): +def _prefer_judged(capable, assessments, difficulty): + """(workers, judged): above difficulty 1 a benchmark standing is a reason to try a worker on low-stakes + work, not to prefer it to measured proof. When any capable worker is capable by a judged record for + this kind (evidence a, or b from a harder difficulty of the kind), only those compete; failing that, + those capable by the judged record across all kinds; failing that, everyone capable.""" + if difficulty is None or difficulty < 2: + return list(capable), False + for tiers in (("a", "b-higher"), ("b-all",)): + own = [c for c in capable if assessments[c["worker"]]["tier"] in tiers] + if own: + return own, True + return list(capable), False + + +def _select(eligible, assessments, difficulty): """(pool, basis): the workers to rank by price, and what put them there. - basis is "capable", "weak" (nobody capable; judged evidence at an easier difficulty), + basis is "capable" (see _prefer_judged), "weak" (nobody capable; judged evidence at an easier difficulty), "strongest" (nobody capable; the strongest known by evidence), "nothing" (nobody has any evidence) or "all-bad" (every usable worker has a thick bad record).""" live = [c for c in eligible if not assessments[c["worker"]]["bad"]] @@ -411,7 +430,7 @@ def _select(eligible, assessments): return list(eligible), "all-bad" capable = [c for c in live if assessments[c["worker"]]["capable"]] if capable: - return capable, "capable" + return _prefer_judged(capable, assessments, difficulty)[0], "capable" weak = [c for c in live if assessments[c["worker"]]["weak"]] if weak: return weak, "weak" @@ -430,7 +449,7 @@ def choose(candidates, records, availability, kind, difficulty, review_of=None, assessments, best = _assess_all(eligible, stats, kind, difficulty, floors) key, rate, load_ratio = _make_key(stats, kind, difficulty, prefer) - pool, basis = _select(eligible, assessments) if eligible else ([], "none") + pool, basis = _select(eligible, assessments, difficulty) if eligible else ([], "none") detail = {} ranked = sorted(pool, key=key) @@ -485,11 +504,12 @@ def choose(candidates, records, availability, kind, difficulty, review_of=None, if r is not None and cr is not None and r < cr else "equal on price and load; the choice came first in worker-id order") passed.append((w, reason)) - capable_n = sum(1 for c in eligible if assessments[c["worker"]]["capable"]) + judged_first = basis == "capable" and _prefer_judged(pool, assessments, difficulty)[1] + capable_n = len(pool) if basis == "capable" else 0 return {"choice": choice, "passed_over": passed, "stats": stats, "author_maker": author_maker, "explored": explored["worker"] if explored else None, "load_ratios": {c["worker"]: load_ratio(c) for c in candidates}, - "assessments": assessments, "basis": basis, "capable_count": capable_n, + "assessments": assessments, "basis": basis, "capable_count": capable_n, "judged_first": judged_first, "best_standing": best, "passed_detail": detail, "floors": floors, "kind": kind, "difficulty": difficulty} @@ -503,6 +523,13 @@ def _why_not(a, difficulty, basis, cheaper): return ("%saccepted rate below the bar over %d attempts%s" % ("cheaper but " if cheaper else "", a["n"], where), "%d of %d accepted" % (a["good"], a["n"])) if basis == "capable": + if a["capable"]: + if a["tier"] == "b-all": + return (lead + "its only judged proof is across other kinds, and a worker with a judged record " + "for this kind is usable", None) + return (lead + "capable only by benchmark standing or as an exploration; above difficulty 1 a worker " + "with a judged record is preferred to a benchmark", + ("standing %g" % a["standing"]) if a["standing"] is not None else None) if a.get("below"): return (lead + "not capable enough for difficulty %d: benchmark standing under the %g needed " "(%g%% of the best usable standing, %g)" % (difficulty, a["needed"], a["floor"], a["best"]), @@ -553,6 +580,7 @@ def retry_choice(candidates, records, availability, kind, difficulty, rejected, floor = (strength(assessments[rejected]) if mine is not None else strength(assess(stats, rejected, kind, difficulty, _standing_of(gone) if gone else None, best, floors))) better = [c for c in others if assessments[c["worker"]]["capable"] and strength(assessments[c["worker"]]) > floor] + better = _prefer_judged(better, assessments, difficulty)[0] if better: out.update(choice=sorted(better, key=key)[0], step="better") return out @@ -636,7 +664,8 @@ def explain_choice(result, kind, difficulty): if basis in (None, "capable"): head = "chose %s: %s" % (choice["worker"], price) if result.get("capable_count"): - head += "; %d usable worker(s) capable enough" % result["capable_count"] + head += "; %d usable worker(s) capable %s" % ( + result["capable_count"], "by a judged record" if result.get("judged_first") else "enough") else: head = "chose %s: none is proven capable for %s at difficulty %s" % (choice["worker"], kind, difficulty) head += ("; taken for the strongest known evidence (%s)" % price if basis == "strongest" else diff --git a/tests/test_capability.py b/tests/test_capability.py index 0be86ee..ef3db9f 100644 --- a/tests/test_capability.py +++ b/tests/test_capability.py @@ -179,11 +179,11 @@ class SufficientCapabilityTests(unittest.TestCase): 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-7.2-2: V (standing 60, the best) is cheaper than W (standing 55, and 5 of 5 accepted: a thin record that + proves nothing). At difficulty 3 both clear the floor on standing and the cheaper, 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=55.0)] + recs = attempts("t/w-1", KIND, 3, 5, 0) r = pick(cands, recs, 3) self.assertEqual(r["choice"]["worker"], "t/v-1") self.assertEqual(r["best_standing"], 60.0) @@ -201,6 +201,90 @@ class SufficientCapabilityTests(unittest.TestCase): self.assertEqual(got["basis"], base["basis"]) +class MeasuredProofBeatsABenchmarkTests(unittest.TestCase): + """Above difficulty 1 a benchmark standing is a reason to try a worker, not to prefer it to a judged record. + The grid's case: a cheap worker whose standing is just over the floor, no record; a dearer one with a thick, + good record for exactly this kind and difficulty. R-7.2-2, R-7.2-3, R-7.3-3.""" + + def pair(self): + # 40 is 60.8 percent of the best usable standing, 65.8: over the difficulty-2 floor of 60 + return [cand("cheap-1", cost=0.2, standing=40.0), cand("proven-1", cost=4.0, standing=65.8)] + + def test_the_proven_worker_takes_ordinary_work_the_cheap_one_takes_routine_work(self): + recs = attempts("t/proven-1", KIND, 2, 21, 0) + r2 = pick(self.pair(), recs, 2) + self.assertEqual(r2["choice"]["worker"], "t/proven-1") + self.assertEqual(r2["assessments"]["t/proven-1"]["tier"], "a") + self.assertTrue(r2["assessments"]["t/cheap-1"]["capable"]) # capable by standing, still not preferred + self.assertEqual(chosen(self.pair(), recs, 1), "t/cheap-1") + self.assertEqual(chosen(self.pair(), attempts("t/proven-1", KIND, 3, 12, 0), 3), "t/proven-1") + + def test_with_the_proven_worker_unavailable_the_cheap_one_is_taken_on_its_standing(self): + recs = attempts("t/proven-1", KIND, 2, 21, 0) + down = {"t/proven-1": {"retry_at": 5000.0, "reason": "limit"}} + r = routing.choose(self.pair(), recs, down, KIND, 2, now=1000.0) + self.assertEqual(r["choice"]["worker"], "t/cheap-1") + self.assertIn("evidence (c)", routing.explain_choice(r, KIND, 2)[1]) + + def test_the_explanation_says_why_the_cheaper_benchmark_worker_was_passed_over(self): + recs = attempts("t/proven-1", KIND, 2, 21, 0) + r = pick(self.pair(), recs, 2) + lines = routing.explain_choice(r, KIND, 2) + self.assertIn("evidence (a)", lines[1]) + self.assertIn("by a judged record", lines[0]) + self.assertTrue([l for l in lines if "cheap-1" in l and "judged record is preferred to a benchmark" in l], lines) + + def test_the_judged_record_across_all_kinds_counts_only_when_nobody_has_evidence_from_the_kind(self): + cands = [cand("pooled-1", cost=1.0), cand("kind-1", cost=5.0)] + pooled = attempts("t/pooled-1", "other", 2, 10, 0) + self.assertEqual(chosen(cands, pooled, 2), "t/pooled-1") # nobody has the kind: the pooled record counts + both = pooled + attempts("t/kind-1", KIND, 2, 9, 1) + r = pick(cands, both, 2) + self.assertEqual(r["choice"]["worker"], "t/kind-1") # the kind's own record outranks it + self.assertEqual(r["assessments"]["t/pooled-1"]["tier"], "b-all") + + def test_a_record_from_a_harder_difficulty_of_the_kind_is_the_kinds_own_evidence(self): + cands = [cand("cheap-1", cost=0.2, standing=40.0), cand("hard-1", cost=4.0, standing=65.8)] + recs = attempts("t/hard-1", KIND, 3, 10, 0) + self.assertEqual(chosen(cands, recs, 2), "t/hard-1") + + def test_nobody_with_a_record_means_the_benchmark_decides_as_before(self): + self.assertEqual(chosen(self.pair(), [], 2), "t/cheap-1") + self.assertEqual(chosen(self.pair(), [], 3), "t/proven-1") # 40 is under 85 percent of 65.8 + + def test_a_worker_with_a_judged_record_is_always_chosen_when_one_is_capable(self): + """With at least one usable worker capable by a judged record at difficulty 2 or 3, the choice has a judged + record: evidence a or b, from the kind when any usable worker has it.""" + rng = random.Random(31337) + covered = 0 + for _ in range(400 * ITERS): + cands, recs = gen_world(rng) + for d in (2, 3): + r = pick(cands, recs, d) + a = r["assessments"] + same_kind = [w for w in a if a[w]["tier"] in ("a", "b-higher")] + pooled = [w for w in a if a[w]["tier"] == "b-all"] + if not (same_kind or pooled): + continue + covered += 1 + self.assertEqual(r["basis"], "capable") + self.assertIn(a[r["choice"]["worker"]]["tier"], ("a", "b-higher") if same_kind else ("b-all",), + (d, a[r["choice"]["worker"]]["tier"])) + self.assertGreater(covered, 100) + + def test_difficulty_one_is_unchanged_and_the_cheapest_capable_wins_whatever_the_evidence(self): + rng = random.Random(77) + for _ in range(200 * ITERS): + cands, recs = gen_world(rng) + r = pick(cands, recs, 1) + if r["basis"] != "capable" or r["explored"]: + continue + price = lambda c: (routing._price_tier(c), c["cost"] if c.get("cost") is not None else float("inf")) + for c in cands: + if r["assessments"][c["worker"]]["capable"]: + self.assertGreaterEqual(price(c), price(r["choice"])) + + def gen_world(rng, kinds=("research", "edit")): """Generated workers and a judged ledger: prices, plans, standings (or none), cells of every size.""" cands, recs = [], [] @@ -237,7 +321,8 @@ class CapabilityPropertyTests(unittest.TestCase): 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).""" + """R-7.2-2: the choice is the cheapest of the capable workers that compete (above difficulty 1 those capable + by a judged record when there are any); a prefer pattern aside, none here.""" rng = random.Random(4242) for _ in range(300 * ITERS): cands, recs = gen_world(rng) @@ -247,9 +332,11 @@ class CapabilityPropertyTests(unittest.TestCase): 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"]: + capable = [c for c in cands if r["assessments"][c["worker"]]["capable"]] + competing, _judged = routing._prefer_judged(capable, r["assessments"], d) + self.assertIn(x["worker"], [c["worker"] for c in competing]) + for c in competing: + if 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): @@ -500,6 +587,7 @@ class RetryLadderTests(unittest.TestCase): rejected_strength = routing.strength(a[rejected]) stronger = [c for c in cands if a[c["worker"]]["capable"] and c["worker"] != rejected and routing.strength(a[c["worker"]]) > rejected_strength] + stronger = routing._prefer_judged(stronger, a, d)[0] if r["step"] == "better": self.assertTrue(a[w]["capable"] and routing.strength(a[w]) > rejected_strength) price = lambda c: (routing._price_tier(c), c["cost"] if c.get("cost") is not None else float("inf"))