diff --git a/sisi/models.py b/sisi/models.py index 0415e4c..6f34df4 100644 --- a/sisi/models.py +++ b/sisi/models.py @@ -205,28 +205,28 @@ clock = lambda t: time.strftime("%m-%d %H:%M", time.localtime(t)) SINCE = "out since %s, back on the wheel at %s" -def outs(text, log, written, today=None): +def outs(text, log, written, today=None, now=None): """{worker: (since, why)}: each worker last said to be out, and when. An `out` verdict in the log says it, and any later verdict takes it back. A table from before `out` verdicts says it with a note, `out until DATE` (DATE today or - later), unless the log holds a verdict for that worker newer than the table (`written`); the note dates from the table.""" - last, today = {}, today or time.strftime("%Y-%m-%d") + later), unless the log holds a verdict for that worker newer than the table (`written`); the note dates from the table. A time OUT or more ahead of `now` is a wrong clock and says nothing.""" + last, today, now = {}, today or time.strftime("%Y-%m-%d"), now or time.time() for o in log: try: - last[short(o["model"] if isinstance(o.get("model"), str) and o["model"] else o["worker"])] = ( - time.mktime(time.strptime(o["ts"], "%Y-%m-%dT%H:%M:%S")), o.get("verdict"), str(o.get("why") or "")) + t = time.mktime(time.strptime(o["ts"], "%Y-%m-%dT%H:%M:%S")) + t - now < OUT and last.__setitem__(short(o["model"] if isinstance(o.get("model"), str) and o["model"] else o["worker"]), (t, o.get("verdict"), str(o.get("why") or ""))) except (KeyError, TypeError, ValueError): pass # a damaged line is no verdict gone = {w: (t, why) for w, (t, verdict, why) in last.items() if verdict == "out"} for r in rows(text): w, until = short(r[0]), [d for d in re.findall(r"out until (\d{4}-\d{2}-\d{2})", " ".join(r).lower()) if d >= today] - if until and written is not None and not (w in last and last[w][0] >= int(written)): # the log keeps whole seconds + if until and written is not None and written - now < OUT and not (w in last and last[w][0] >= int(written)): # the log keeps whole seconds gone[w] = (written, "out until " + until[0]) return gone def away(): """`outs` for the table and the log as they are now.""" - return outs(store.read(table()), store.records("outcomes.jsonl"), os.path.getmtime(table()) if os.path.exists(table()) else None) + return outs(store.read(table()), store.records("outcomes.jsonl"), os.path.getmtime(table()) if os.path.exists(table()) else None, now=time.time()) def out(r, skip, bad, gone, now): @@ -299,7 +299,7 @@ def pick(args, cwd=None, rand=None): if not r: bad = next((maker(x) for x in rows(text) if short(x[0]) == short(review)), None) again = {"routine": " Choose again from ordinary.", "ordinary": " Choose again from hard."}.get(cls, "") - first = min((t + OUT for x in mine if (t := gone.get(short(x[0]), (None,))[0]) is not None and now - t < OUT), default=None) + first = min((gone[short(x[0])][0] + OUT for x in mine if out(x, skip, bad, gone, now).startswith("out since")), default=None) print(("sisi pick: every worker of class %s is out or excluded: %s." % (cls, "; ".join( "%s (%s)" % (x[0], out(x, skip, bad, gone, now)) for x in mine)) if mine else "sisi pick: the models table has no worker of class %s." % cls) @@ -353,7 +353,7 @@ def research_record(answer_file): if len(re.findall(r"routine|ordinary|hard", r[2].lower())) != 1: return "each row's \"use for\" must name exactly one of routine, ordinary or hard: | %s |" % " | ".join(r[:6]) if re.search(r"\bout (?:until|till)\b", " ".join(r), re.I): # what the wheel would read as a legacy note - return "the table does not say who is out: judge the worker instead (`sisi judge WORKER out \"\"`) and take that out of the row: | %s |" % " | ".join(r[:6]) + return "a row may not say who is out: delete the `out until` note from it (the agent that delegates judges a worker that hit a limit: `sisi judge WORKER out \"\"`): | %s |" % " | ".join(r[:6]) if len(lines) > 42 or len(text) > 12000: return "the answer is too long (at most 40 rows and 12,000 characters)" if shared and ("|" not in shared or not _shared_date(shared)[0]): @@ -394,6 +394,8 @@ def judge(args, cwd=None): return 1 trows = rows(store.read(table())) hits = [r for r in trows if short(r[0]) == short(args[0])] or [r for r in trows if claim({"picked": [short(r[0])]}, args[0], True)] + if args[1] == "out" and len(hits) != 1: # a verdict that names no one worker would change nothing + return print("sisi judge: %r is not one worker of the models table; name it as the table does." % args[0]) or 1 model = (hits[0][0].split() or [""])[0] if len(hits) == 1 else args[0] wkey, sid, now, began = short(model), store.current_session(cwd), time.time(), None if sid: diff --git a/tests/test_core.py b/tests/test_core.py index 83626d0..53dd332 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1700,7 +1700,8 @@ cli.main(["tick"]) sys.path.insert(0, REPO) from sisi import models name = lambda r: r[0].split()[0].lower() - spin = lambda cls, **kw: models.choose(FIXTURE, cls, gone=models.outs(FIXTURE, [], time.time()), **kw) # the table as just written + written = time.time() + spin = lambda cls, **kw: models.choose(FIXTURE, cls, gone=models.outs(FIXTURE, [], written), **kw) # the table as just written for _ in range(300): self.assertNotIn("muse-wonder", name(spin("ordinary", rand=random.random))) # its `out until 2999` note is six hours old at most self.assertNotEqual(name(spin("ordinary", skip=["zai-coding-plan/glm-5.4"], rand=random.random)), "zai-coding-plan/glm-5.4") @@ -1712,7 +1713,7 @@ cli.main(["tick"]) ["zai-coding-plan/glm-5.4"] * 2 + ["zai-coding-plan/glm-5.3-flash"] * 2 + ["muse/muse-ember-1.0"] * 2 + ["haiku"] * 2) self.assertEqual(name(spin("ordinary", review="zai-coding-plan/glm-5.4", rand=lambda: 0.0)), "muse/muse-ember-1.0") self.assertIsNone(spin("routine", rand=lambda: 0.0)) - later = [name(spin("ordinary", rand=lambda: i / 1000, now=time.time() + 6 * 3600)) for i in range(1000)] + later = [name(spin("ordinary", rand=lambda: i / 1000, now=written + 6 * 3600)) for i in range(1000)] self.assertEqual({w.split("/")[-1] for w in later}, {"glm-5.4", "glm-5.3-flash", "muse-ember-1.0", "haiku", "muse-wonder-1.0"}) # six hours on, the note is spent row = lambda *c: list(c) + [""] * (7 - len(c)) self.assertEqual(models.maker(row("agy", "")), "agy") @@ -2705,6 +2706,32 @@ cli.main(["tick"]) first = time.mktime(time.strptime("2026-" + out.split("The first is back on the wheel at ")[1][:11], "%Y-%m-%d %H:%M")) self.assertLess(abs(first - (time.time() + 5 * 3600)), 120) # beta, out for an hour: in five more + def test_a_verdict_or_table_dated_far_ahead_is_a_wrong_clock_and_takes_nobody_out(self): + self.table(ONE) + self.judged("alpha", "out") + self.dated(-24 * 60) # a day ahead: not a verdict + self.assertEqual(self.picked("routine")[0], True) + self.table(ONE.replace("| |", "| out until 2999-01-01 |")) + os.remove(os.path.join(self.data, "outcomes.jsonl")) + ahead = time.time() + 10 * 86400 + os.utime(os.path.join(self.data, "models.md"), (ahead, ahead)) # a table written ten days ahead: its note is no note + self.assertEqual(self.picked("routine")[0], True) + + def test_a_worker_the_table_does_not_list_cannot_be_judged_out(self): + self.table(ONE) + rc, out, _ = self.sisi("judge", "nosuch", "out", LIMIT) + self.assertEqual((rc, "nosuch" in out, os.path.exists(os.path.join(self.data, "outcomes.jsonl"))), (1, True, False), out) # it would be recorded and change nothing + + def test_the_return_time_pick_names_is_one_of_a_worker_that_could_be_picked_then(self): + self.table(TWO) + self.judged("alpha", "out") + self.judged("beta", "out") + self.dated(4 * 60, 60) # alpha is back in two hours, beta in five + got, out = self.picked("routine", "--not", "alpha") # alpha comes back, but is excluded + self.assertEqual(got, False, out) + first = time.mktime(time.strptime("2026-" + out.split("The first is back on the wheel at ")[1][:11], "%Y-%m-%d %H:%M")) + self.assertLess(abs(first - (time.time() + 5 * 3600)), 120) + def test_pick_says_a_returning_worker_is_a_retry(self): self.table(ONE) self.judged("alpha", "out", LIMIT + ", please try later. " * 30) @@ -2743,7 +2770,7 @@ cli.main(["tick"]) for notes in ("out till 2999-01-01", "out until next week", "Out Until 2999-1-1", "out until 2999-01-01"): self.write(answer, HEADER + "| small | host subagent `small` | routine | flat | fast | %s |\nFallback order: none.\n" % notes) rc, out, _ = self.sisi("models", "research", "--record", answer) - self.assertEqual((rc, "| small |" in out, "sisi judge WORKER out" in out), (1, True, True), notes) # availability is judged, never written + self.assertEqual((rc, "| small |" in out, "delete the" in out, "judge the worker instead" in out), (1, True, True, False), notes) # a table writer is told to delete it, not to invent a verdict self.assertEqual(self.read(self.data, "models.md").strip(), clean.strip()) def test_a_failed_check_is_counted_even_when_it_is_fixed(self): @@ -3778,7 +3805,7 @@ cli.main(["tick"]) def test_the_product_stays_small(self): code = sum(len(self.read(p).splitlines()) for p in glob.glob(os.path.join(REPO, "sisi", "*.py"))) - self.assertLessEqual(code, 2265, "sisi is two loops. If it needs more than this, find the simpler shape.") + self.assertLessEqual(code, 2267, "sisi is two loops. If it needs more than this, find the simpler shape.") rulebook = self.read(REPO, "canon", "core.md") self.assertLess(len(rulebook.splitlines()), 137) self.assertLessEqual(len(rulebook), 10561)