From 52d343f6ae8f841aead12bffe2655c81511efc3e Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 23 Nov 2026 21:10:36 -0700 Subject: [PATCH] Time each delegation from its pick; the wheel favours faster workers Co-Authored-By: Claude Sonnet 5.5 --- README.md | 4 +-- sisi/models.py | 64 +++++++++++++++++++++++++++++++++++++--------- tests/test_core.py | 64 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 117 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index e612e2b..56f8fd2 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ organizes the rules. **Delegation.** The lead agent is told to delegate everything, and is stopped when it does the work itself. It routes by a models table: which workers you have, what each is good at, what it costs. A research worker rebuilds that table when it gets old, and every judged delegation feeds the next research. The lead does not -choose the worker: `sisi pick CLASS` spins a wheel over the workers of that class (weighted by table order), +choose the worker: `sisi pick CLASS` spins a wheel over the workers of that class (weighted by table order and by speed), and a delegation to a worker the wheel did not give is refused. One pick, one delegation. **Done.** Nothing starts before `sisi done` has recorded the goal, the checks that decide success and what the @@ -73,7 +73,7 @@ its limits are. | `sisi learn [--project] "…"` | The person's own words become a rule at once | | `sisi rules` / `sisi rules organize [--record FILE]` / `sisi rules revert` | See the learned rules; have a worker organize them; undo the last change | | `sisi models` / `sisi models research [--record FILE]` | The workers table; have a worker rebuild it | -| `sisi judge WORKER good\|bad "…"` | How a delegation went | +| `sisi judge WORKER good\|bad "…"` | How a delegation went; the minutes are measured from the pick's delegation (or `--minutes N`) and make the wheel favour faster workers | | `sisi direct "…"` | Allow more direct tool calls this turn, with the reason | ## Where things live diff --git a/sisi/models.py b/sisi/models.py index 180ab1a..5b5e45d 100644 --- a/sisi/models.py +++ b/sisi/models.py @@ -118,6 +118,7 @@ def claim(s, text): for name in s.get("picked") or []: if any(re.search(r"(? 0: + out.setdefault(short(o["worker"]), []).append(o["minutes"]) + except (ValueError, KeyError, TypeError): + pass + return {w: v[-10:] for w, v in out.items()} + + +def choose(text, cls, skip=(), review=None, rand=None, today=None, speed=None): + """One worker of this class, by a wheel weighted by table order (the first of n has weight n, the last 1) + times its speed: the fastest candidate's median minutes over its own, at least 0.25 (1 with no timed jobs).""" table_rows, today = rows(text), today or time.strftime("%Y-%m-%d") bad = next((maker(r) for r in table_rows if short(r[0]) == short(review)), None) if review else None left = [r for r in table_rows if cls in r[2].lower() and not out(r, skip, bad, today)] @@ -140,8 +160,11 @@ def choose(text, cls, skip=(), review=None, rand=None, today=None): if rand is None: import random rand = random.random - x = rand() * len(left) * (len(left) + 1) / 2 - for weight, r in zip(range(len(left), 0, -1), left): + med = {short(r[0]): median(speed[short(r[0])]) for r in left if (speed or {}).get(short(r[0]))} + fast = min(med.values(), default=1) + weights = [w * max(0.25, fast / med[short(r[0])]) if short(r[0]) in med else w for w, r in zip(range(len(left), 0, -1), left)] + x = rand() * sum(weights) + for weight, r in zip(weights, left): x -= weight if x < 0: return r @@ -167,7 +190,8 @@ def pick(args, cwd=None, rand=None): if review is not None and not any(short(r[0]) == short(review) for r in rows(text)): print("sisi pick: %r is not a worker in the models table; --review-of names the worker whose work is reviewed." % review) return 1 - r = choose(text, cls, skip, review, rand) + speed = speeds() + r = choose(text, cls, skip, review, rand, speed=speed) if not r: bad = next((maker(x) for x in rows(text) if short(x[0]) == short(review)), None) print("sisi pick: every worker of class %s is out or excluded: %s." % (cls, "; ".join( @@ -182,6 +206,8 @@ def pick(args, cwd=None, rand=None): if store.config()["shell_workers"] or not command(r): print("how to call it: " + r[1]) print("good at, bad at: %s\nnotes: %s" % (r[4], r[5])) + t = speed.get(short(r[0])) + print("speed: median %g min over %d timed jobs" % (median(t), len(t)) if t else "speed: no timed jobs yet") return 0 @@ -190,7 +216,8 @@ def outcomes(n): for line in store.read(store.data("outcomes.jsonl")).splitlines()[-n:]: try: o = json.loads(line) - out.append("%s %s %s %s" % (o["ts"][:10], o["worker"], o["verdict"], o["why"])) + out.append("%s %s %s %s%s" % (o["ts"][:10], o["worker"], o["verdict"], + "(%d min) " % o["minutes"] if type(o.get("minutes")) is int else "", o["why"])) except (ValueError, KeyError): pass return out @@ -221,14 +248,27 @@ def research_record(answer_file): def judge(args, cwd=None): - """`sisi judge WORKER good|bad ""`: how a delegation went.""" - if len(args) != 3 or args[1] not in ("good", "bad") or len(args[2].split()) < 3: - print('usage: sisi judge WORKER good|bad ""') + """`sisi judge WORKER good|bad "" [--minutes N]`: how a delegation went, and how long it took (from the + pick's start in this session unless --minutes says).""" + minutes = None + if "--minutes" in args: + i = args.index("--minutes") + v, args = (args[i + 1:i + 2] or [""])[0], args[:i] + args[i + 2:] + minutes = int(v) if re.fullmatch(r"[1-9]\d*", v) else 0 + if len(args) != 3 or args[1] not in ("good", "bad") or len(args[2].split()) < 3 or minutes == 0: + print('usage: sisi judge WORKER good|bad "" [--minutes N]') return 1 + sid = store.current_session(cwd) + if sid: + with store.session(sid) as s: + starts = s.get("starts", {}).get(short(args[0])) + if starts: + began = starts.pop(0) + minutes = minutes or max(1, int((time.time() - began) / 60)) log = store.data("outcomes.jsonl") - store.append(log, json.dumps({"ts": time.strftime("%Y-%m-%dT%H:%M:%S"), "worker": args[0], "verdict": args[1], - "why": " ".join(args[2].split()), - "project": os.path.basename(store.project_root(cwd))}, sort_keys=True)) + store.append(log, json.dumps(dict({"ts": time.strftime("%Y-%m-%dT%H:%M:%S"), "worker": args[0], "verdict": args[1], + "why": " ".join(args[2].split()), "project": os.path.basename(store.project_root(cwd))}, + **{"minutes": minutes} if minutes else {}), sort_keys=True)) store.commit("delegation judged", log) print("recorded.") return 0 diff --git a/tests/test_core.py b/tests/test_core.py index 807b6da..292c8f4 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -345,6 +345,68 @@ class Core(unittest.TestCase): self.assertEqual(self.sisi("pick", "easy")[0], 1) self.assertEqual(self.sisi("pick")[0], 1) + def outcomes(self): + return [json.loads(l) for l in self.read(self.data, "outcomes.jsonl").splitlines()] + + def test_judge_times_a_delegation_from_its_pick(self): + self.table(WORKERS) + self.hook("tick") + self.hook("prompt", prompt="go") + self.done() + delegate = lambda: (self.sisi("pick", "hard", sid="s1")[0], self.tool("Agent", tool_input={"model": "sonnet"})[0]) + judge = lambda *a: self.sisi("judge", "sonnet", "good", "did the job in one pass", *a, sid="s1") + self.assertEqual(delegate(), (0, 0)) + starts = self.state()["starts"]["sonnet"] + self.assertEqual(len(starts), 1) + with open(os.path.join(self.tmp, "state", "core-sessions", "s1.json"), "r+") as f: # the clock: it began 25 minutes ago + st = json.load(f) + st["starts"]["sonnet"] = [time.time() - 25 * 60 - 5] + f.seek(0), f.truncate(), json.dump(st, f) + self.assertEqual(judge()[0], 0) + self.assertEqual(self.outcomes()[-1]["minutes"], 25) + self.assertEqual(self.state()["starts"]["sonnet"], []) + self.assertEqual(judge()[0], 0) # no start left: no minutes + self.assertNotIn("minutes", self.outcomes()[-1]) + self.assertEqual(delegate(), (0, 0)) + for bad in ("abc", "0", "-3", "1.5", ""): + self.assertEqual(judge("--minutes", bad)[0], 1, bad) + self.assertEqual(judge("--minutes")[0], 1) + self.assertEqual(len(self.outcomes()), 2) # refused: nothing written + self.assertEqual(judge("--minutes", "7")[0], 0) + self.assertEqual(self.outcomes()[-1]["minutes"], 7) + self.assertEqual(self.state()["starts"]["sonnet"], []) + self.assertEqual(delegate(), (0, 0)) # a quick one is at least 1 minute + self.assertEqual(judge()[0], 0) + self.assertEqual(self.outcomes()[-1]["minutes"], 1) + + def test_the_wheel_favours_the_faster_worker(self): + sys.path.insert(0, REPO) + from sisi import models + table = HEADER + "| slow | `slow \"\"` | ordinary | flat | x | |\n| fast | `fast \"\"` | ordinary | flat | x | |\n" + name = lambda r: r[0] + spins = lambda speed: [name(models.choose(table, "ordinary", rand=lambda: i / 1000, speed=speed)) for i in range(1000)] + self.assertEqual(spins(None).count("fast"), 333) # table order alone: weights 2 and 1 + self.assertEqual(spins({"slow": [60] * 3, "fast": [10, 10]}).count("fast"), 666) # slow is floored at 0.25: 0.5 against 1 + self.assertEqual(spins({"slow": [60, 61, 62], "fast": [10]}).count("fast"), 666) + self.assertEqual(spins({"fast": [10]}).count("fast"), 333) # slow has no timed jobs: factor 1, table order + self.assertEqual(spins({"slow": [20], "fast": [10]}).count("fast"), 500) # factor 0.5: weights 1 and 1 + self.assertEqual(name(models.choose(table, "ordinary", rand=lambda: 0.33, speed={"slow": [60], "fast": [10]})), "slow") + self.assertEqual(name(models.choose(table, "ordinary", rand=lambda: 0.34, speed={"slow": [60], "fast": [10]})), "fast") + + def test_pick_and_models_show_the_minutes(self): + self.table(WORKERS) + self.hook("tick") + self.assertIn("speed: no timed jobs yet", self.sisi("pick", "hard", sid="s1")[1]) + rows = [{"ts": "2026-11-20T10:00:00", "worker": "sonnet", "verdict": "good", "why": "an old untimed record", "project": "p"}] + rows += [{"ts": "2026-11-2%dT10:00:00" % i, "worker": "sonnet", "verdict": "good", "why": "timed job number %d" % i, + "project": "p", "minutes": m} for i, m in enumerate((10, 30, 20, 50), 1)] + self.write(os.path.join(self.data, "outcomes.jsonl"), "".join(json.dumps(r) + "\n" for r in rows)) + self.assertIn("speed: median 25 min over 4 timed jobs", self.sisi("pick", "hard", sid="s1")[1]) + out = self.sisi("models")[1] + self.assertIn("sonnet good (30 min) timed job number 2", out) + self.assertIn("sonnet good an old untimed record", out) + self.assertIn("(30 min)", self.sisi("models", "research")[1]) + def test_a_delegation_needs_a_pick_and_one_pick_buys_one_delegation(self): self.table(WORKERS) self.hook("tick") @@ -947,7 +1009,7 @@ class Core(unittest.TestCase): 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.assertLess(code, 1430, "sisi is two loops. If it needs more than this, find the simpler shape.") + self.assertLess(code, 1465, "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()), 120) self.assertLess(len(rulebook), 8000)