From 64a45cfccdad6f48b7e5501f7307e2f366bc1fdb Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 23 Nov 2026 16:24:12 -0700 Subject: [PATCH] Item 4: a new version starts from its predecessor's judged record (walks back one version at a time, never a bad record, never over its own thick record, routable through its predecessor, named in the explanation). done / next: recency tie-break and rule text (item 5) Co-Authored-By: Claude Sonnet 5.5 --- sisi/routing.py | 75 +++++++++++++++++- sisi/workers.py | 3 + tests/test_model_inheritance.py | 130 ++++++++++++++++++++++++++++++++ tests/test_worker_models.py | 18 +++++ 4 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 tests/test_model_inheritance.py diff --git a/sisi/routing.py b/sisi/routing.py index f28e2bc..db96633 100644 --- a/sisi/routing.py +++ b/sisi/routing.py @@ -183,6 +183,60 @@ def candidate_stats(candidates, records): return stats +def _versions_newest_first(models): + """[[workers of one version], ...] from the newest version to the oldest (one model through several + providers is one version).""" + out = [] + for d in sorted(models, key=lambda d: (modelid.recency_key(_lineage(d)), d["worker"])): + if out and modelid.compare(_lineage(out[-1][0]), _lineage(d)) == 0: + out[-1].append(d) + else: + out.append([d]) + return out + + +def inherit_stats(candidates, stats, opts=None): + """Judged cells as evidence sees them: a model with no thick record of its own for a kind and difficulty + starts from the thick record of the closest older version of the same maker and class (walking back one + version at a time, the first thick record decides). Only a record that proves the work is inherited: a + predecessor's bad record starts the newer model from its benchmark or from nothing, and a thin record of the + model's own that already repeats rejections is not papered over. The model's own thick record always stands + alone. An inherited cell carries `from`, the worker whose record it is.""" + eff = {w: {"uses": st["uses"], "by_cell": dict(st["by_cell"])} for w, st in stats.items()} + groups = {} + for c in candidates: + l = _comparable(c, opts) + if l: + groups.setdefault(tuple(l["key"]), []).append(c) + for group in groups.values(): + for c in group: + older = [d for d in group if modelid.compare(_lineage(c), _lineage(d)) > 0] + if not older: + continue + levels = _versions_newest_first(older) + own = stats[c["worker"]]["by_cell"] + for ck in sorted({k for d in older for k in stats[d["worker"]]["by_cell"]}, key=str): + mine = own.get(ck, _NO_CELL) + if _thick(mine) or _contradicts(mine): + continue + for level in levels: + cells = [(d["worker"], stats[d["worker"]]["by_cell"].get(ck)) for d in level] + cells = [(w, x) for w, x in cells if x] + merged = {"n": sum(x["n"] for _w, x in cells), "good": sum(x["good"] for _w, x in cells)} + if _thick(merged): + if _proven(merged): + eff[c["worker"]]["by_cell"][ck] = dict(merged, **{"from": max(cells, key=lambda t: (t[1]["n"], t[0]))[0]}) + break + return eff + + +def has_predecessor(c, among, opts=None): + """Whether some worker in `among` is an older version of the same maker and class as `c`.""" + l = _comparable(c, opts) + return bool(l) and any(_comparable(d, opts) and tuple(_lineage(d)["key"]) == tuple(l["key"]) + and modelid.compare(l, _lineage(d)) > 0 for d in among) + + def _price_tier(c): if c.get("plan") == "flat": return 0 @@ -234,6 +288,8 @@ def _by_difficulty(stats, worker, kind): p = pooled.setdefault(d, {"n": 0, "good": 0}) p["n"] += cell["n"] p["good"] += cell["good"] + if cell.get("from"): + p["from"] = cell["from"] return own, pooled @@ -279,7 +335,8 @@ def assess(stats, worker, kind, difficulty, standing=None, best_standing=None, f proof = ("b-all", wide[0], pooled[wide[0]]) if proof is not None: tier, at, cell = proof - out.update(tier=tier, capable=True, level=3, at=at, n=cell["n"], good=cell["good"], measure=_rate(cell)) + out.update(tier=tier, capable=True, level=3, at=at, n=cell["n"], good=cell["good"], measure=_rate(cell), + inherited_from=cell.get("from")) return out if standing is not None: pct = _floor_pct(floors, d) @@ -304,6 +361,8 @@ def strength(a): if a["bad"]: return (-1, 0, 0.0) if a["tier"] in _TIER_RANK: + if a.get("inherited_from"): + return (3, 1, a["measure"]) # a predecessor's record: above a benchmark, below the model's own return (4, -_TIER_RANK[a["tier"]], a["measure"]) if a["standing"] is not None and not a["contradicted"]: return (3, 0, float(a["standing"])) @@ -502,7 +561,7 @@ def choose(candidates, records, availability, kind, difficulty, review_of=None, now = float(now or 0) eligible, passed, stats, author_maker, superseded = _prepare(candidates, records, availability, now, exclude, avoid, review_of, opts) - assessments, best = _assess_all(eligible, stats, kind, difficulty, floors) + assessments, best = _assess_all(eligible, inherit_stats(candidates, stats, opts), kind, difficulty, floors) key, rate, load_ratio = _make_key(stats, kind, difficulty, prefer) pool, basis = _select(eligible, assessments, difficulty) if eligible else ([], "none") @@ -634,7 +693,8 @@ def retry_choice(candidates, records, availability, kind, difficulty, rejected, now = float(now or 0) eligible, passed, stats, author_maker, _superseded = _prepare(candidates, records, availability, now, exclude, avoid, review_of, opts) - assessments, best = _assess_all(eligible, stats, kind, difficulty, floors) + eff = inherit_stats(candidates, stats, opts) + assessments, best = _assess_all(eligible, eff, kind, difficulty, floors) key, _rate, _load = _make_key(stats, kind, difficulty, prefer) out = {"choice": None, "step": None, "passed_over": passed, "stats": stats, "author_maker": author_maker, "assessments": assessments, "best_standing": best, "kind": kind, "difficulty": difficulty, @@ -650,7 +710,7 @@ def retry_choice(candidates, records, availability, kind, difficulty, rejected, return out gone = next((c for c in candidates if c["worker"] == rejected), None) 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))) + strength(assess(eff, 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: @@ -685,6 +745,13 @@ def evidence_line(a, kind, difficulty, choice, basis): if a["bad"]: return ("evidence: none favourable; every usable worker has a poor record, so this one has the best " "accepted rate (%d of %d for %s)" % (a["good"], a["n"], kind)) + line = _evidence_line(a, kind, difficulty, choice, basis) + if a.get("inherited_from"): + line += " (the record of its predecessor %s; it has none of its own yet)" % a["inherited_from"] + return line + + +def _evidence_line(a, kind, difficulty, choice, basis): tier = a["tier"] if tier == "a": return ("evidence (a), judged record: %d of %d accepted for %s at difficulty %d (%s, bar %s)" diff --git a/sisi/workers.py b/sisi/workers.py index f4b24c6..bb5b146 100644 --- a/sisi/workers.py +++ b/sisi/workers.py @@ -935,6 +935,9 @@ def routable_candidates(candidates, records): keep.append(c) if not keep: return list(candidates), True, 0 + # a new version of a model that has a record, price or standing starts from its predecessor's record + opts = route_opts() + keep = keep + [c for c in candidates if c not in keep and routing.has_predecessor(c, keep, opts)] return keep, False, len(candidates) - len(keep) diff --git a/tests/test_model_inheritance.py b/tests/test_model_inheritance.py new file mode 100644 index 0000000..5f63f7b --- /dev/null +++ b/tests/test_model_inheritance.py @@ -0,0 +1,130 @@ +"""A new version of a model starts from its predecessor's judged record: inherited evidence ranks below the model's +own record and above a benchmark, counts as judged proof, stops once the model has a thick record of its own, and a +predecessor's bad record does not block it. Invented ids. Requirement ids: R-7.2-1, R-7.2-3.""" +import random, unittest +from support import * +from sisi import routing +from test_model_supersession import cand, attempts, pick, NOW + + +def assessed(cands, records, worker, difficulty=2, kind="impl", **kw): + r = routing.choose(cands, list(records), {}, kind, difficulty, now=NOW, **kw) + return r["assessments"].get(worker), r + + +class InheritanceTests(unittest.TestCase): + def test_a_new_version_inherits_the_record_of_its_predecessor_and_says_so(self): + old, new = cand("prov/abc-5.3", cost=1.0), cand("prov/abc-5.4", cost=2.0) + records = attempts("prov/abc-5.3", 12, 11, difficulty=2) + a, r = assessed([old, new], records, "prov/abc-5.4") + self.assertEqual((a["tier"], a["capable"], a["inherited_from"]), ("a", True, "prov/abc-5.3")) + self.assertEqual(r["choice"]["worker"], "prov/abc-5.4") + line = routing.evidence_line(a, "impl", 2, r["choice"], r["basis"]) + self.assertIn("the record of its predecessor prov/abc-5.3", line) + self.assertIn("11 of 12", line) + + def test_it_walks_back_one_predecessor_at_a_time(self): + cands = [cand("prov/abc-5.2", cost=1.0), cand("prov/abc-5.3", cost=1.0), cand("prov/abc-5.4", cost=1.0)] + records = attempts("prov/abc-5.2", 12, 12, difficulty=2) + attempts("prov/abc-5.3", 3, 3, difficulty=2) + a, _ = assessed(cands, records, "prov/abc-5.4") + self.assertEqual(a["inherited_from"], "prov/abc-5.2") # 5.3 has only a thin record + records += attempts("prov/abc-5.3", 9, 9, difficulty=2, start=50) + a, _ = assessed(cands, records, "prov/abc-5.4") + self.assertEqual(a["inherited_from"], "prov/abc-5.3") # now the closest thick one + + def test_a_predecessors_bad_record_does_not_block_the_newer_model(self): + cands = [cand("prov/abc-5.3", cost=1.0), cand("prov/abc-5.4", cost=1.0, standing=70.0)] + records = attempts("prov/abc-5.3", 12, 2, difficulty=2) + a, _ = assessed(cands, records, "prov/abc-5.4") + self.assertFalse(a["bad"]) + self.assertEqual(a["tier"], "standing") # it starts from its benchmark, not from the old verdict + bare = assessed([cands[0], cand("prov/abc-5.4", cost=1.0)], records, "prov/abc-5.4")[0] + self.assertFalse(bare["bad"]) + self.assertFalse(bare["capable"]) + + def test_the_models_own_thick_record_replaces_the_inherited_one(self): + cands = [cand("prov/abc-5.3", cost=1.0), cand("prov/abc-5.4", cost=1.0)] + records = attempts("prov/abc-5.3", 12, 12, difficulty=2) + attempts("prov/abc-5.4", 9, 1, difficulty=2) + a, _ = assessed(cands, records, "prov/abc-5.4") + self.assertTrue(a["bad"]) + self.assertIsNone(a.get("inherited_from")) + records = attempts("prov/abc-5.3", 12, 2, difficulty=2) + attempts("prov/abc-5.4", 9, 9, difficulty=2) + a, _ = assessed(cands, records, "prov/abc-5.4") + self.assertEqual((a["tier"], a.get("inherited_from")), ("a", None)) + + def test_a_thin_record_that_already_contradicts_is_not_papered_over(self): + cands = [cand("prov/abc-5.3", cost=1.0), cand("prov/abc-5.4", cost=1.0)] + records = attempts("prov/abc-5.3", 12, 12, difficulty=2) + attempts("prov/abc-5.4", 3, 0, difficulty=2) + a, _ = assessed(cands, records, "prov/abc-5.4") + self.assertIsNone(a.get("inherited_from")) + self.assertFalse(a["capable"]) + + def test_nothing_is_inherited_across_classes_or_makers(self): + flash, new = cand("prov/abc-5.3-flash", cost=1.0), cand("prov/abc-5.4", cost=1.0) + records = attempts("prov/abc-5.3-flash", 12, 12, difficulty=2) + self.assertFalse(assessed([flash, new], records, "prov/abc-5.4")[0]["capable"]) + other = cand("prov/xyz-5.4", cost=1.0, maker="xyz") + self.assertFalse(assessed([cand("prov/abc-5.3"), other], attempts("prov/abc-5.3", 12, 12, difficulty=2), "prov/xyz-5.4")[0]["capable"]) + + def test_nothing_is_inherited_by_an_unreadable_class(self): + cands = [cand("prov/abc-6.2-sol"), cand("prov/abc-6.3-sol")] + records = attempts("prov/abc-6.2-sol", 12, 12, difficulty=2) + self.assertFalse(assessed(cands, records, "prov/abc-6.3-sol")[0]["capable"]) + + def test_inherited_evidence_counts_as_a_judged_record_before_a_benchmark(self): + """Difficulty 2 and 3: proof comes before benchmark, and an inherited record is proof.""" + new = cand("prov/abc-5.4", cost=5.0) + rival = cand("prov/xyz-2.0", cost=0.1, maker="xyz", standing=90.0) + records = attempts("prov/abc-5.3", 12, 12, difficulty=3) + who, r = pick([cand("prov/abc-5.3", cost=5.0), new, rival], records, 3) + self.assertEqual(who, "prov/abc-5.4") + self.assertTrue(r["judged_first"]) + + def test_inherited_ranks_below_the_models_own_record_in_strength(self): + own = routing.assess({"w": {"uses": 0, "by_cell": {("impl", 2): {"n": 10, "good": 10}}}}, "w", "impl", 2) + inherited = routing.assess({"w": {"uses": 0, "by_cell": {("impl", 2): {"n": 10, "good": 10, "from": "old"}}}}, "w", "impl", 2) + bench = routing.assess({"w": {"uses": 0, "by_cell": {}}}, "w", "impl", 2, standing=80.0, best_standing=80.0) + self.assertGreater(routing.strength(own), routing.strength(inherited)) + self.assertGreater(routing.strength(inherited), routing.strength(bench)) + self.assertEqual(inherited["inherited_from"], "old") + + def test_a_model_with_no_data_of_its_own_is_routable_through_its_predecessor(self): + from sisi import workers + old, new, lone = cand("prov/abc-5.3"), cand("prov/abc-5.4"), cand("prov/xyz-9.9", maker="xyz") + for c in (old, new, lone): + c["price_source"] = None + old["price_source"] = "fixture" + keep, cold, excluded = workers.routable_candidates([old, new, lone], []) + self.assertEqual(sorted(c["worker"] for c in keep), ["prov/abc-5.3", "prov/abc-5.4"]) + self.assertEqual(excluded, 1) + + def test_property_inherited_evidence_never_outranks_a_thick_record_of_the_models_own(self): + rng = random.Random(41) + seen = 0 + for _ in range(300 * ITERS): + versions = rng.sample(["5.1", "5.2", "5.3", "5.4", "5.10"], rng.randint(2, 4)) + cands = [cand("prov/abc-%s" % v, cost=rng.choice([None, 1.0, 2.0])) for v in versions] + records = [] + for c in cands: + for d in (1, 2, 3): + if rng.random() < 0.6: + n = rng.choice([2, 8, 12]) + records += attempts(c["worker"], n, rng.randint(0, n), difficulty=d, start=d * 100) + stats = routing.candidate_stats(cands, records) + eff = routing.inherit_stats(cands, stats) + for c in cands: + for ck, cell in stats[c["worker"]]["by_cell"].items(): + if cell["n"] >= routing.MIN_ATTEMPTS: + self.assertEqual(eff[c["worker"]]["by_cell"][ck], cell) + seen += 1 + for ck, cell in eff[c["worker"]]["by_cell"].items(): + if cell.get("from"): + own = stats[c["worker"]]["by_cell"].get(ck, {"n": 0}) + self.assertLess(own["n"], routing.MIN_ATTEMPTS) + self.assertGreaterEqual(cell["n"], routing.MIN_ATTEMPTS) + self.assertGreaterEqual(cell["good"] / cell["n"], routing.MIN_ACCEPTED_RATE) # a bad record is never inherited + self.assertGreater(seen, 100) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_worker_models.py b/tests/test_worker_models.py index 24f393c..fa8652c 100644 --- a/tests/test_worker_models.py +++ b/tests/test_worker_models.py @@ -143,5 +143,23 @@ class RouteSupersessionTests(ModelCase): self.assertNotIn("cannot be read", self.route(1).stdout) +class RouteInheritanceTests(ModelCase): + def test_a_new_release_with_no_data_of_its_own_gets_ordinary_work_at_once(self): + """R-7.2-1: discovered, unpriced, unjudged, yet routable and capable through its predecessor's record.""" + self.seed([("muse/abc-5.3", 12, 12)], difficulty=2) + r = self.route(2, "--explain") + self.assertEqual(r.stdout.splitlines()[0], "muse/abc-5.4", r.stdout) + self.assertIn("the record of its predecessor muse/abc-5.3", r.stdout) + self.assertLessEqual(len(r.stdout.splitlines()), 20, r.stdout) + plain = self.route(2).stdout.splitlines() + self.assertLessEqual(len(plain), 6, plain) + + def test_once_it_has_a_thick_record_of_its_own_the_inherited_one_stops_applying(self): + self.seed([("muse/abc-5.3", 12, 12), ("muse/abc-5.4", 9, 9)], difficulty=2) + r = self.route(2, "--explain") + self.assertEqual(r.stdout.splitlines()[0], "muse/abc-5.4", r.stdout) + self.assertNotIn("predecessor", r.stdout) + + if __name__ == "__main__": unittest.main()