From ce6a5b89b9fc0a7fab818a2d2947e0e4783286e0 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 23 Nov 2026 16:04:18 -0700 Subject: [PATCH] Item 1: a model id read into maker, line, class and version (tier words are data); unknown when it cannot be read with confidence. done / next: facts (item 2) Co-Authored-By: Claude Sonnet 5.5 --- canon/model-words.json | 5 + sisi/modelid.py | 205 +++++++++++++++++++++++++++++++++++++++++ tests/test_modelid.py | 119 ++++++++++++++++++++++++ 3 files changed, 329 insertions(+) create mode 100644 canon/model-words.json create mode 100644 sisi/modelid.py create mode 100644 tests/test_modelid.py diff --git a/canon/model-words.json b/canon/model-words.json new file mode 100644 index 0000000..07fb29f --- /dev/null +++ b/canon/model-words.json @@ -0,0 +1,5 @@ +{ + "about": "Words that appear in model ids. They are data, not code: edit this file when a maker invents a new tier word. A tier word names the class of a model inside its line (it comes before or after the version number; no tier word means the line's standard model). An ignored word is neither the line nor the class (a stage, a price label, a style). A word in neither list that follows the version is a word this file does not know, so the class of that model is reported as unknown until the lead agent records it with `sisi workers fact WORKER --class NAME --source S`.", + "tiers": ["air", "base", "code", "coder", "fast", "flash", "full", "highspeed", "large", "lite", "max", "medium", "micro", "mini", "nano", "plus", "pro", "small", "tiny", "turbo", "ultra"], + "ignore": ["beta", "chat", "contributor", "default", "exp", "experimental", "free", "instruct", "latest", "new", "preview", "rc", "stable", "thinking"] +} diff --git a/sisi/modelid.py b/sisi/modelid.py new file mode 100644 index 0000000..a2ad2cf --- /dev/null +++ b/sisi/modelid.py @@ -0,0 +1,205 @@ +"""Reading a model id: its maker, line, class and version. Pure: no file access, no clock. + +A first guess from the name alone, and the recorded facts that override it. The words that mark +a class (tier words) or mean nothing (ignored words) are data passed in, loaded from +canon/model-words.json by the caller. + +What is read from an id (the part after the provider, case aside): + version the numbers in it, as a tuple compared numerically (5.10 is newer than 5.9); a date in it + (20261101 or 2026-11-01) is kept apart and compared as a date. A letter in front of a number + (v4.1, k2.7) is a marker and is dropped. + line the name words before the version that identify the model line (the maker's own name leading + them is dropped: "abc-flash-5.4" and "abc-5.4-flash" both have no extra line name). + class the tier words, before or after the version, or "standard" when there is none. The class + label for ordering and display adds the line when there is one: "wonder", "wonder-full". + maker a recorded maker fact, else the first name word. +An id with no version, or with a word after the version that the data does not know, cannot be read +with confidence: its class is "unknown" and it takes part in no comparison of versions. + +A recorded fact (class, family, released, maker) always beats the reading. A bare host alias with no +version in it (the host resolves it to its current model of that class) counts as the newest version +of its class, once a class fact says which class that is. +""" +import re + +STANDARD = "standard" +UNKNOWN = "unknown" +_DATE = re.compile(r"(? 1 or not names): + names.append(prefix) + number(num) + elif _SUFFIXED.fullmatch(t): + num, suffix = _SUFFIXED.fullmatch(t).groups() + number(num) + if suffix in ignored: + continue + word(suffix) + elif _WORD.fullmatch(t): + word(t) + else: + unknown.append(t) + if started: + closed = True + first = names[0] if names else "" + mk = (str(maker).lower() if maker else first) + line = list(names) + if line and line[0] == mk: + line = line[1:] + cls = "-".join(tiers) if tiers else STANDARD + version = tuple(nums) + date = max(dates) if dates else None + confident = bool(names) and bool(version or date) and not unknown + return {"maker": mk, "line": "-".join(line), "family": "-".join(names), "tiers": tuple(tiers), + "class": cls if confident else UNKNOWN, "label": _label("-".join(line), cls) if confident else UNKNOWN, + "version": version or None, "date": date, "confident": confident, "unknown_tokens": unknown} + + +def _label(line, cls): + """The class as the maker's classes are listed: the line name and the tier, or "standard".""" + if cls in (None, "", STANDARD): + return line or STANDARD + return "%s-%s" % (line, cls) if line else cls + + +def _normal_class(line, cls): + """A recorded class, lower case, with the line name taken off when the lead agent wrote the whole label.""" + c = str(cls or "").strip().lower() + if c in (line, "") and line: + return STANDARD + if line and c.startswith(line + "-"): + return c[len(line) + 1:] + return c or STANDARD + + +def resolve(worker, words, fact=None, host_native=False, maker_hint=None): + """The model's identity: parse the id, then let a recorded fact override it. Returns + {key, maker, line, class, label, version, date, released, alias, source} + source is "fact" (recorded), "guess" (read from the id, not yet confirmed) or "unknown" + (no key: the model takes part in no comparison of versions).""" + fact = fact if isinstance(fact, dict) else {} + p = parse(worker, words, maker=fact.get("maker") or maker_hint) + released = str(fact["released"]) if fact.get("released") else None + maker = p["maker"] + line = str(fact["family"]).strip().lower() if fact.get("family") else p["line"] + if fact.get("family") and line == maker: + line = "" + out = {"key": None, "maker": maker, "line": line, "class": UNKNOWN, "label": UNKNOWN, "version": p["version"], + "date": p["date"], "released": released, "alias": False, "source": UNKNOWN} + if fact.get("class"): + cls = _normal_class(line, fact["class"]) + out.update(source="fact") + elif p["confident"]: + cls = p["class"] + out.update(source="guess") + else: + return out + out.update({"class": cls, "label": _label(line, cls), "key": (maker, line, cls)}) + if host_native and not (p["version"] or p["date"]) and out["source"] == "fact": + out["alias"] = True + return out + + +def compare(a, b): + """1 when a is the more recently released model, -1 when b is, 0 when equal or unknowable. A recorded + release date on both decides; else the version numbers on both; else the dates in the ids on both. A bare + alias is the newest of its class.""" + if a.get("alias") or b.get("alias"): + return (1 if a.get("alias") else 0) - (1 if b.get("alias") else 0) + ra, rb = a.get("released"), b.get("released") + if ra and rb and ra != rb: + return 1 if ra > rb else -1 + va, vb = a.get("version"), b.get("version") + if va and vb: + n = max(len(va), len(vb)) + pa, pb = tuple(va) + (0,) * (n - len(va)), tuple(vb) + (0,) * (n - len(vb)) + if pa != pb: + return 1 if pa > pb else -1 + da, db = a.get("date"), b.get("date") + if da and db and da != db: + return 1 if da > db else -1 + return 0 + if not va and not vb: + da, db = a.get("date"), b.get("date") + if da and db and da != db: + return 1 if da > db else -1 + return 0 + + +def recency_key(l): + """A tuple that sorts the more recently released first, for tie-breaks: recorded release date, then version + inside one line (the line first, so versions of different lines are never compared), then the id's date.""" + if not l: + return (0, 0, "", (), 0) + rel = l.get("released") or "" + days = int(rel[0:4]) * 372 + int(rel[5:7]) * 31 + int(rel[8:10]) if len(rel) >= 10 and rel[:4].isdigit() else 0 + ver = tuple(-x for x in (tuple(l.get("version") or ()) + (0,) * 6)[:6]) + d = l.get("date") or "" + dd = int(d[0:4]) * 372 + int(d[5:7]) * 31 + int(d[8:10]) if len(d) >= 10 else 0 + return (0 if l.get("alias") else 1, -days, "|".join(l.get("key") or ()), ver, -dd) diff --git a/tests/test_modelid.py b/tests/test_modelid.py new file mode 100644 index 0000000..d619166 --- /dev/null +++ b/tests/test_modelid.py @@ -0,0 +1,119 @@ +"""Reading a model id: maker, line, class, version, and which of two is the more recently released. +Fixtures are invented ids shaped like real ones. Requirement ids: R-7.2-1, R-7.2-2, R-7.2-3.""" +import json, os, random, unittest +from support import * +from sisi import modelid + +WORDS = json.load(open(os.path.join(REPO, "canon", "model-words.json"))) + + +def p(worker, **kw): + return modelid.parse(worker, WORDS, **kw) + + +def r(worker, **kw): + return modelid.resolve(worker, WORDS, **kw) + + +class ParseTests(unittest.TestCase): + def test_version_line_and_class_from_the_name(self): + """R-7.2-1: the class is read from the id; no tier word means the line's standard model.""" + a = p("prov/abc-5.3") + self.assertEqual((a["maker"], a["class"], a["version"], a["confident"]), ("abc", "standard", (5, 3), True)) + self.assertEqual(p("prov/abc-5.3-flash")["class"], "flash") + + def test_a_tier_word_before_or_after_the_version_parses_the_same(self): + after, before = p("prov/abc-5.4-flash"), p("prov/abc-flash-5.4") + for k in ("maker", "line", "class", "label", "version", "confident"): + self.assertEqual(after[k], before[k], k) + + def test_versions_compare_numerically(self): + """5.10 is newer than 5.9, not older (the string order would say so).""" + self.assertGreater(modelid.compare(r("prov/abc-5.10"), r("prov/abc-5.9")), 0) + self.assertLess(modelid.compare(r("prov/abc-5.9"), r("prov/abc-5.10")), 0) + self.assertEqual(modelid.compare(r("prov/abc-5"), r("prov/abc-5.0")), 0) + + def test_a_dated_suffix_compares_as_a_date_and_does_not_disturb_the_version(self): + dated = p("prov/abc-5-4-20261101") + self.assertEqual((dated["version"], dated["date"], dated["class"]), ((5, 4), "2026-11-01", "standard")) + self.assertEqual(p("prov/abc-5.4-2026-11-01")["date"], "2026-11-01") + self.assertGreater(modelid.compare(r("prov/abc-5-4-20261101"), r("prov/abc-5.3")), 0) + self.assertEqual(modelid.compare(r("prov/abc-5-4-20261101"), r("prov/abc-5.4")), 0) + self.assertGreater(modelid.compare(r("prov/abc-5.4-20261101"), r("prov/abc-5.4-20260901")), 0) + self.assertGreater(modelid.compare(r("prov/abc-20261101"), r("prov/abc-20250301")), 0) + self.assertEqual(modelid.compare(r("prov/abc-20261101"), r("prov/abc-5.3")), 0) # not comparable + self.assertIsNone(p("prov/abc-5.4-20261345")["date"]) # not a date + + def test_two_tier_words_make_one_class_and_letter_prefixed_versions_read(self): + self.assertEqual(p("prov/abc-flash-lite-5.4")["class"], "flash-lite") + self.assertEqual(p("prov/xyz-v4.1-pro")["version"], (4, 1)) + self.assertEqual(p("prov/xyz-k2.7-code")["class"], "code") + self.assertEqual(p("prov/xyz3.8-flash")["version"], (3, 8)) + + def test_ignored_words_are_neither_line_nor_class(self): + self.assertEqual(p("prov/abc-5.4-preview-free")["class"], "standard") + self.assertEqual(p("prov/abc-5.4-flash-latest")["class"], "flash") + + def test_a_named_line_is_the_class_label(self): + """Item 7: for a maker whose lines are the classes the line name is the class label.""" + a, b = p("prov/abc-wonder-1.0"), p("prov/abc-wonder-full-1.0") + self.assertEqual((a["line"], a["label"]), ("wonder", "wonder")) + self.assertEqual((b["line"], b["class"], b["label"]), ("wonder", "full", "wonder-full")) + self.assertEqual(p("prov/abc-ember-1.0")["label"], "ember") + + def test_what_cannot_be_read_with_confidence_says_unknown(self): + """R-7.2-3: no version, or a word after the version that the data does not know: class unknown.""" + for ident in ("prov/xyz-mini", "prov/abc", "prov/abc-6.2-sol", "prov/abc-3-5-sonnet", "prov/5.4"): + got = p(ident) + self.assertFalse(got["confident"], ident) + self.assertEqual(got["class"], "unknown", ident) + self.assertIsNone(r(ident)["key"], ident) + self.assertEqual(r(ident)["source"], "unknown", ident) + + def test_tier_words_are_data_not_code(self): + """Adding a tier word to the data changes the reading; the code names none.""" + self.assertFalse(p("prov/abc-5.4-sol")["confident"]) + got = modelid.parse("prov/abc-5.4-sol", {"tiers": ["sol"], "ignore": []}) + self.assertEqual((got["class"], got["confident"]), ("sol", True)) + text = open(os.path.join(REPO, "sisi", "modelid.py")).read() + for word in WORDS["tiers"] + WORDS["ignore"]: + self.assertNotRegex(text.replace("standard", ""), r'"%s"' % word) + + def test_a_recorded_fact_beats_the_reading(self): + got = r("prov/abc-6.2-sol", fact={"class": "sol", "maker": "abc"}) + self.assertEqual((got["source"], got["class"], got["key"]), ("fact", "sol", ("abc", "", "sol"))) + flipped = r("prov/abc-5.3-flash", fact={"class": "standard"}) + self.assertEqual(flipped["class"], "standard") + self.assertEqual(r("prov/abc-5.4", fact={"family": "wonder", "class": "full"})["label"], "wonder-full") + self.assertEqual(r("prov/abc-5.4", fact={"family": "wonder", "class": "wonder-full"})["label"], "wonder-full") + self.assertEqual(r("prov/abc-5.4")["source"], "guess") + + def test_a_recorded_release_date_beats_version_numbers(self): + old_but_later = r("prov/abc-5.3", fact={"released": "2026-09-01"}) + new_but_earlier = r("prov/abc-5.4", fact={"released": "2026-03-01"}) + self.assertGreater(modelid.compare(old_but_later, new_but_earlier), 0) + self.assertGreater(modelid.compare(r("prov/abc-5.4"), r("prov/abc-5.3")), 0) + + def test_a_bare_host_alias_is_the_newest_of_its_class_once_the_class_is_recorded(self): + alias = r("abc", host_native=True, fact={"class": "standard", "maker": "abc"}) + self.assertTrue(alias["alias"]) + self.assertGreater(modelid.compare(alias, r("prov/abc-9.9")), 0) + self.assertEqual(alias["key"], r("prov/abc-5.3")["key"]) + unrecorded = r("abc", host_native=True) + self.assertFalse(unrecorded["alias"]) + self.assertIsNone(unrecorded["key"]) + + def test_the_parser_never_raises_and_compare_is_antisymmetric(self): + rng = random.Random(7) + pool = ["abc", "xyz", "5", "4", "10", "3.2", "flash", "mini", "pro", "preview", "20261101", "v2", "k3", "x", "", "-", "70b", "sol", "4o", "2026", "11", "01"] + for _ in range(500 * ITERS): + ident = "prov/" + "-".join(rng.choice(pool) for _ in range(rng.randint(0, 6))) + a, b = r(ident), r("prov/" + "-".join(rng.choice(pool) for _ in range(rng.randint(0, 6)))) + self.assertEqual(modelid.compare(a, b), -modelid.compare(b, a), ident) + if a["source"] == "unknown": + self.assertIsNone(a["key"]) + modelid.recency_key(a) + + +if __name__ == "__main__": + unittest.main()