"""The models table: which workers this person can delegate to, what each is good at and what it costs. A research worker writes it, the lead reads it to route, and `sisi judge` adds how each delegation went so the next research knows. The table is plain text a person can read and correct.""" import json, math, os, re, time from . import rules, store ASK_FRESH = ("which providers and models from the shipped baseline table below are usable on this machine, how to call each, and what each costs here; map them to whatever providers this user has", "Reuse the shipped baseline table below for class (`use for`), baseline score, benchmarks, list price, and public good-at/bad-at — do not re-research them online. Combine them with our own judged record below, which outranks a benchmark.", "Use the baseline score to decide which models truly perform at a class; then choose and order by efficiency (step 3).") ASK_STALE = ("which AI models this machine can delegate work to, what each is good at and what it costs", "What each is good at. Use current public software-engineering benchmarks and the maker's own description (search the web), then our own record below, which outranks a benchmark.", "Only the newest version of each model belongs in the table. A smaller or faster variant is a different model. Then choose and order by efficiency (step 3).") EXTRA_SHARED = ("\nThen a second, separately marked section starting on its own line with `=== Shared models ===`, followed by `researched YYYY-MM-DD` on its first line and a baseline ranking table " "(`| model | maker | class | score | released | list price | benchmarks | good at, bad at |`) with one row per model (keyed by model id, never provider route), sorted by class then score descending, " "holding only shared public facts (no plans, allowances, availability, judged records, or personal notes).\n") RESEARCH = """sisi chore: models research You are a worker. Do this task yourself; do not delegate it further. Find out %(intro)s, and write the personal table an engineering lead will route by. 1. What is there: every provider (the account or plan a call draws its allowance or bill from) the host's delegation tool can start, and each one every AI coding tool on PATH routes to (run its auth/provider list and model list; a second plan behind one tool is a separate provider). Do not log in, change a setting or run a paid task. 2. %(step2)s 3. Efficiency: how much work at its class one unit of this person's allowance or money buys. The `efficiency` cell quotes the provider's own published allowance or price for that model with the plan/pricing page URL (or "measured: ..." from our record), and its quota windows exactly as `5h: N req; week: N req; month: N req` (`req` only when N counts requests; else name the unit). sisi spreads delegations by these so no plan runs dry early. Among models that truly perform at a class, keep the one whose allowance goes furthest (flat plan: most requests per window; per use: lowest cost per benchmark task): at most one row per provider per class; drop the rest. Notes hold `usage: ` where one exists. 4. %(step4)s 5. What the person has said, below, stands exactly and outranks all of this: it is not yours to change. List only workers the host's delegation tool can actually start: check the host's own list of enabled providers. Answer with exactly this and nothing else. First a markdown table of at most 40 rows, at most one row per provider per class, ordered within each class most efficient first (the wheel weights by row order), with these columns: | worker | provider | maker | how to call it | use for | cost | efficiency | good at, bad at | notes | "provider" names the account or plan as the person knows it. "maker" is the company or lab that trained the model, lower case, one word. "use for" is exactly one of: routine, ordinary, hard. It is the model's capability class, not a list of tasks it could do. Routine is the small, fast, cheap tier (the level of a maker's smallest current model); ordinary is the mid tier (a maker's main everyday model); hard is the top tier (a maker's strongest model). Place each model by comparing it with its peers from other makers (benchmarks and the maker's own positioning), so that every class holds models of like strength from several makers. A model that seems to fit two classes goes in the lower one only if it truly performs at that level, never in both. "how to call it" is exact: the host delegation tool's provider and model id (T3 Code's delegate_task); the host's subagent model name where it has subagents; the full command line with where the brief goes only for a worker the host's tool cannot reach. Do not say in the notes whether a worker is available: `sisi judge WORKER out ""` takes a worker that hit a limit off the wheel for a while, and sisi tries it again by itself. Then one line starting "Fallback order:" that says who takes over when a worker is out, level by level. %(extra)s === Shipped baseline table === %(shared)s === The table now === %(table)s === What the person has said === %(owner)s === How delegations went here, newest last === %(outcomes)s """ ASK = ("In the background, delegate just the first line of the output of `sisi models research` plus \"Run `sisi chore show ` and do what it says\" " "to a hard-class worker that can search the web (`sisi pick hard`: choosing models well pays for itself) and record its answer with `sisi models research --record FILE`.") def table(): return store.data("models.md") def shared_canon(): return os.path.join(store.ROOT, "canon", "models.md") def shared_store(): return store.data("models-shared.md") def _shared_date(text): m = re.search(r"\b(\d{4}-\d{2}-\d{2})\b", (text.splitlines() or [""])[0]) try: return (m.group(1), time.mktime(time.strptime(m.group(1), "%Y-%m-%d"))) if m else ("", None) except ValueError: return "", None def sync_shared(cwd=None): """In the sisi repo itself, copy the store's models-shared.md into canon/models.md if it is newer.""" top = store.git(["rev-parse", "--path-format=absolute", "--show-toplevel"], cwd or os.getcwd())[1].rstrip("\r\n") canon, src = shared_canon(), shared_store() if (not top or os.path.realpath(top) != os.path.realpath(store.ROOT) or not os.path.isfile(src) or store.ROOT.startswith(os.path.realpath(os.path.join(store.LIB, "releases")) + os.sep)): return False s_text, c_text = store.read(src), store.read(canon) s_date, c_date = _shared_date(s_text)[0], _shared_date(c_text)[0] if not s_date or (c_date and (s_date < c_date or (s_date == c_date and os.path.getmtime(src) <= os.path.getmtime(canon)))): return False s_text != c_text and store.write(canon, s_text) return True def age_days(): try: return (time.time() - os.path.getmtime(table())) / 86400.0 except OSError: return None def stale(mark=None): """One line when the table is missing, old, or missed a worker, else "".""" who = re.sub(r"[^\w./:+-]", "", str(store.read(store.data("models.stale")) if mark is None else mark), flags=re.A)[:80] if mark is not None and who: try: store.write(store.data("models.stale"), who + "\n") except Exception: pass # best effort: a refusal never depends on it age = age_days() if age is None or age > store.config()["research_days"]: return "sisi: " + ("there is no models table yet. " if age is None else "the models table is %d days old. " % age) + ASK return ("sisi: a delegation named `%s`, which the models table does not list. " % who + ASK) if who and not known(store.read(table()), who) else "" KEYS = ("worker", "how", "use", "cost", "good", "notes", "maker", "provider", "efficiency") # a column is known by its header's first word LAUNCHERS = {"python", "python3", "bash", "sh", "zsh", "cd", "env", "npx", "npm", "node", "uv", "uvx", "make", "ssh", "git", "docker", "sudo"} def rows(text): """The table's worker rows, each a list of cells in the order of KEYS, found by header name.""" out, cols = [], KEYS[:6] for line in text.splitlines(): c = [x.strip() for x in line.strip().strip("|").split("|")] if line.startswith("|") else [] if c and c[0].lower() == "worker": cols = [(x.split() or [""])[0].lower() for x in c] elif len(c) >= 5 and not set(c[0]) <= set("-: "): out.append([dict(zip(cols, c)).get(k, "") for k in KEYS]) return out def usable(text): return any(re.search(r"routine|ordinary|hard", r[2].lower()) for r in rows(text)) def short(cell): return (str(cell).split() or [""])[0].lower() def maker(r): """Who made a worker: its maker cell; else the leading letters of the last path segment of its name; else its call text without its own name (the same way of calling, the same maker).""" name, m = short(r[0]), re.match(r"[a-z]+", short(r[0]).rsplit("/", 1)[-1]) return r[6].lower() or (m.group() if "/" in name and m else r[1].lower().replace(name, "") or name) def command(row): return "" in row[1] and "host subagent" not in row[1].lower() def is_call(command_line): """Does any segment of this shell command start a worker in the table (after nohup, exec, time, env, VAR=)?""" starts = {(r[1].replace("`", "").split() or [""])[0] for r in rows(store.read(table())) if command(r)} - LAUNCHERS for seg in re.split(r"&&|\|\||[;|&\n]", command_line): words = [w for w in seg.split()] while words and (words[0] in ("nohup", "exec", "time", "env") or re.match(r"[A-Za-z_]\w*=", words[0])): words.pop(0) if words and os.path.basename(words[0]) in starts: return True return False def known(table_text, text): return claim({"picked": [short(r[0]) for r in rows(table_text)]}, text, True) def claim(s, text, segment=False, node=None): """Spend the pick that this delegation's model text names (the one made for `node` first); False when none fits. With `segment` (the host names its models shortly: `fast` for `maker-fast-2`) one whole `-` segment of a picked id's last `/` part that has a letter fits.""" for name in s.get("picked") or []: if any(re.search(r"(?= factor * threshold: [(worker, start, limit, branch, extended)].""" now, default_m, starts, meta, out = time.time(), store.config()["worker_minutes"], s.get("starts"), s.get("meta"), [] num = lambda x: type(x) in (int, float) and math.isfinite(x) for w, v in (starts.items() if isinstance(starts, dict) else ()): for t in (v if isinstance(v, list) else ()): m = meta.get("%s %r" % (w, t)) if num(t) and t <= now and isinstance(meta, dict) else {} m = m if isinstance(m, dict) else {} lim = m.get("limit") if num(m.get("limit")) and m["limit"] > 0 else None th = lim if lim is not None else default_m if num(t) and t <= now and num(th) and now - t >= 60 * factor * th: out.append((w, t, lim, m.get("branch") if isinstance(m.get("branch"), str) and m["branch"] else None, m.get("extended") if num(m.get("extended")) else None)) return out def overdue(s): """Open delegations older than their threshold: [(worker, its oldest such start)].""" by_w, l = {}, late(s, 1) [by_w.setdefault(w, min(t for w2, t, *_ in l if w2 == w)) for w, *_ in l] return list(by_w.items()) def waiting(s): """A delegation of this piece of work is open: started and not yet judged.""" return isinstance(s.get("starts"), dict) and any(isinstance(v, list) and v for v in s["starts"].values()) OUT = 6 * 3600 # a worker judged `out` sits out this long, then the wheel tries it again: 24 h / 6 h = at most 4 tries a day 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, 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. 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: 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 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, now=time.time()) def out(r, skip, bad, gone, now): """Why this worker is off the wheel, "" when it is on: excluded, the maker of the work under review, or out.""" since = gone.get(short(r[0])) return ("excluded" if short(r[0]) in skip else "same maker as the work under review" if maker(r) == bad else SINCE % (clock(since[0]), clock(since[0] + OUT)) if since and now - since[0] < OUT else "") def median(v): v = sorted(v) return (v[len(v) // 2] + v[(len(v) - 1) // 2]) / 2 def track(): """One pass over the log: (each worker's last 10 timed minutes from its good outcomes, oldest first; its quality factor over its last 10 good/bad outcomes: ((good+1)/(n+2))/((n+1)/(n+2)) = (good+1)/(n+1)).""" sp, ql = {}, {} for o in store.records("outcomes.jsonl"): w = short(o["model"] if isinstance(o.get("model"), str) and o["model"].strip() else o["worker"] if isinstance(o.get("worker"), str) else "") if w and o.get("verdict") == "good" and type(o.get("minutes")) is int and 0 < o["minutes"] <= 10000: sp.setdefault(w, []).append(o["minutes"]) if w and o.get("verdict") in ("good", "bad"): ql.setdefault(w, []).append(o["verdict"] == "good") return {w: v[-10:] for w, v in sp.items()}, {w: (sum(v[-10:]) + 1) / (len(v[-10:]) + 1) for w, v in ql.items()} def speeds(): return track()[0] def qualities(): return track()[1] WINDOWS, FLOOR = {"5h": 5 * 3600, "week": 7 * 86400, "month": 30 * 86400}, 0.05 pkey = lambda provider: " ".join(str(provider).lower().split()) def budgets(cell): """{window: N} from `5h: N req; week: N req; month: N req` in a cell; only counts of requests, which a delegation is counted as.""" return {w: int(n.replace(",", "")) for w, n in re.findall( r"\b(5h|week|month)\s*:\s*(\d[\d,]*)\s*(?:req|requests|prompts|messages|calls|delegations)\b", cell.lower()) if int(n.replace(",", ""))} def plan_use(text, cwd=None, now=None): """{provider: {window: share used}} by sisi's own delegations: each started in the window spends 1/N of its plan's window, N the allowance its row gives (outcomes.jsonl, plus the current session's open jobs).""" now, by, times, sid = now or time.time(), {short(r[0]): r for r in rows(text) if r[7]}, [], store.current_session(cwd) try: starts = json.loads(store.read(store._session_path(sid)) or "{}").get("starts") if sid else None except (ValueError, AttributeError): starts = None for o in store.records("outcomes.jsonl"): try: w = o["model"] if isinstance(o.get("model"), str) and o["model"] else o["worker"] times.append((short(w), time.mktime(time.strptime(o.get("started") or o["ts"], "%Y-%m-%dT%H:%M:%S")))) except (KeyError, TypeError, ValueError, AttributeError): pass # a damaged line spends nothing for w, v in (starts.items() if isinstance(starts, dict) else ()): times += [(short(w), t) for t in (v if isinstance(v, list) else ()) if type(t) in (int, float)] out, bud = {}, {w: budgets(r[3] + " " + r[8]) for w, r in by.items()} [out.setdefault(pkey(r[7]), {}).update(dict.fromkeys(bud[w], 0.0)) for w, r in by.items()] for w, t in times: if (r := by.get(w)) and t <= now: for win, n in bud[w].items(): if now - t < WINDOWS[win]: out[pkey(r[7])][win] += 1.0 / n return {p: u for p, u in out.items() if u} def shares(use): return {p: max(FLOOR, min(1 - x for x in u.values())) for p, u in use.items()} def choose(text, cls, skip=(), review=None, rand=None, speed=None, gone=None, now=None, quality=None, share=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, cubed, at least 0.02 (1 with no timed jobs), times its quality factor over its last 10 good/bad outcomes ((good+1)/(n+1), so no bad runs keeps weight 1), times its provider's share of plan left (`share`, from `shares`: at least FLOOR), so no plan runs dry early. A worker last said out less than OUT seconds ago (`gone`, from `outs`) is not on the wheel.""" table_rows, gone, now = rows(text), gone or {}, now or time.time() 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 r[2].strip().lower() == cls and not out(r, skip, bad, gone, now)] if not left: return None if rand is None: import random rand = random.random med = {short(r[0]): median(speed[short(r[0])]) for r in left if (speed or {}).get(short(r[0]))} fast, qual = min(med.values(), default=1), quality or {} weights = [w * (max(0.02, (fast / med[short(r[0])]) ** 3) if short(r[0]) in med else 1) * max(1 / 11, qual.get(short(r[0]), 1)) * (share or {}).get(pkey(r[7]), 1) for w, r in zip(range(len(left), 0, -1), left)] x = rand() * sum(weights) for weight, r in zip(weights, left): if (x := x - weight) < 0: return r return left[-1] def pick(args, cwd=None, rand=None): """`sisi pick routine|ordinary|hard [--review-of WORKER] [--not WORKER ...] [--limit MINUTES] [--branch NAME]`: the wheel gives one worker.""" node = None if args[:1] == ["--node"]: # the class and branch come from the plan node from . import plan args, node = plan.for_pick(args, cwd) if node is None: return 1 cls, skip, review, limit, branch, i = (args[0] if args and args[0] in ("routine", "ordinary", "hard") else None), set(), None, None, None, 1 while cls and i < len(args): if args[i] == "--not" and i + 1 < len(args): skip.add(short(args[i + 1])) elif args[i] == "--review-of" and i + 1 < len(args): review = args[i + 1] elif args[i] == "--limit" and i + 1 < len(args) and re.fullmatch(r"[1-9]\d{0,3}|10000", args[i + 1]): limit = int(args[i + 1]) elif args[i] == "--branch" and i + 1 < len(args) and re.fullmatch(r"[A-Za-z0-9._/-]{1,100}", args[i + 1]) and not args[i + 1].startswith("-"): branch = args[i + 1] else: cls = None i += 2 if not cls: print("usage: sisi pick routine|ordinary|hard [--review-of WORKER] [--not WORKER ...] [--limit MINUTES] [--branch NAME]") return 1 text = store.read(table()) mine = [r for r in rows(text) if r[2].strip().lower() == cls] for r in rows(text): if len(re.findall(r"routine|ordinary|hard", r[2].lower())) > 1: print("%s is in more than one class and is on no wheel: rebuild the table" % short(r[0])) 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 (speed, qual), gone, now = track(), away(), time.time() r = choose(text, cls, skip, review, rand, speed=speed, gone=gone, now=now, quality=qual, share=shares(plan_use(text, cwd, now))) 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((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) + (" The first is back on the wheel at %s." % clock(first) if first is not None else "") + again) return 1 sid = store.current_session(cwd) if sid: with store.session(sid) as s: s.setdefault("picked", []).append(w := short(r[0])) q = s["pending"] if isinstance(s.get("pending"), dict) else {} s["pending"] = {**q, w: (q[w] if isinstance(q.get(w), list) else []) + [{"limit": limit, "branch": branch, "node": node}]} # one entry per pick, claimed in order print("worker: %s (model id: %s)\nstart it with the host's delegation tool; one pick buys one delegation" % (r[0], short(r[0]))) 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") if g := gone.get(short(r[0])): # its last verdict was `out`: a retry, so a quick failure is expected and cheap print("retry: this worker hit a limit at %s: %s; if it fails for the limit again, judge it out again and pick another" % ( clock(g[0]), g[1][:160] + ("…" if len(g[1]) > 160 else ""))) from . import plan print(plan.record_line()) if node and sid: with store.session(sid) as s: print(plan.header(s["plan"], node)) return 0 def outcomes(n): out = [] for o in store.records("outcomes.jsonl")[-n:]: if all(type(o.get(k)) is str for k in ("ts", "worker", "verdict", "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"])) return out def research_brief(): owner = [l for l in rules.lines(rules.path(rules.USER)) if "[owner" in l] shipped = store.read(shared_canon()).strip() _, ts = _shared_date(shipped) fresh = ts is not None and (time.time() - ts) / 86400.0 <= store.config()["research_days"] intro, step2, step4 = ASK_FRESH if fresh else ASK_STALE return RESEARCH % {"intro": intro, "step2": step2, "step4": step4, "extra": "" if fresh else EXTRA_SHARED, "shared": shipped or "(none yet)", "table": store.read(table()).rstrip() or "(none yet)", "owner": "\n".join(owner) or "(nothing)", "outcomes": "\n".join(outcomes(60)) or "(none yet)"} def research_record(answer_file): parts = re.split(r"(?im)^===\s*shared\b.*===\s*$", store.read(answer_file).strip(), maxsplit=1) text, shared = parts[0].strip(), (parts[1].strip() if len(parts) > 1 else "") lines = [l for l in text.splitlines() if l.startswith("|")] if "| worker" not in text.lower() or "fallback order:" not in text.lower() or len(lines) < 3: return "the answer needs the table (a `| worker |` header and at least one row) and a `Fallback order:` line" seen = {} for r in rows(text): if not r[7] or not r[8]: return "each row needs its %s: | %s |" % ("provider (a `provider` column: the account or plan the call draws on)" if not r[7] else "efficiency (the published allowance or price, with its URL, or \"measured: ...\")", " | ".join(r[:6])) key = (pkey(r[7]), r[2].strip().lower()) if key in seen: return "keep at most one row per provider per class, the most efficient: %s and %s are both %s at %s" % (seen[key], r[0], r[2].strip(), r[7]) seen[key] = r[0] 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 "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]): return "the shared section needs a `researched YYYY-MM-DD` line and a table" paths = [table()] + ([shared_store()] if shared else []) with store.lock("models"): store.write(table(), text + "\n") if shared: store.write(shared_store(), shared + "\n") os.path.isfile(store.data("models.stale")) and os.remove(store.data("models.stale")) ok = store.commit("models table researched", *paths) print("models table recorded: %d workers.%s" % (len(lines) - 2, "" if ok else " (store commit failed)")) return None def sync_late(s, cwd, sid, add=True): if not (late2 := late(s, 2)) and not os.path.lexists(os.path.join(store.STATE, "core-faults", store.project_key(cwd) + ".json")): return from . import faults prefix, now, cfg_m = "late:%s:" % sid[:8], time.time(), store.config()["worker_minutes"] want = {"%s%s@%d" % (prefix, w, int(t)): 'worker %s has run %dm (past 2x %gm limit). Judge it (`sisi judge %s good|bad|out ""`) or extend (`sisi judge %s extend MINUTES ""`).' % ( w, int((now - t) / 60), lim if lim is not None else cfg_m, w, w) for w, t, lim, *_ in late2} try: faults.clear(cwd, prefix, set(want)); add and [faults.add(cwd, text, kind=k) for k, text in want.items()] except OSError: pass def judge(args, cwd=None): """`sisi judge WORKER good|bad|out "" [--minutes N] [--newest] | extend MINUTES "" [--newest]`.""" from . import retro # not at the top: hooks import this module and must stay quick args, newest, minutes, note = [a for a in args if a != "--newest"], "--newest" in args, 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{0,3}|10000", v) else 0 ext = len(args) == 4 and args[1] == "extend" and minutes is None and re.fullmatch(r"[1-9]\d{0,3}|10000", args[2]) if not args or not args[0].strip() or not (ext or (len(args) == 3 and args[1] in ("good", "bad", "out") and minutes != 0)) or len(args[-1].split()) < 3: print('usage: sisi judge WORKER good|bad|out "" [--minutes N] [--newest] | extend MINUTES "" [--newest]') 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: with store.session(sid) as s: starts = s["starts"].get(wkey) if isinstance(s.get("starts"), dict) else None cand = (starts.pop(-1 if newest else 0) if not ext else starts[-1 if newest else 0]) if isinstance(starts, list) and starts else None if ext and not newest: # the job that is late, not just the oldest open one cand = next((min(t for w, t, *_ in l if w == wkey) for l in (late(s, 2), late(s, 1)) if any(w == wkey for w, *_ in l)), cand) ok_t = type(cand) in (int, float) and math.isfinite(cand) and cand <= now if ext: if not ok_t: return print("sisi judge: no open job for %s." % args[0]) or 1 lim, m, k = max(0, int((now - cand) / 60)) + int(args[2]), (s["meta"] if isinstance(s.get("meta"), dict) else {}), "%s %r" % (wkey, cand) m[k], s["meta"] = {**(m[k] if isinstance(m.get(k), dict) else {}), "limit": lim, "extended": now, "reason": retro.clean(args[3])}, m isinstance(s.get("reminded"), list) and s.__setitem__("reminded", [x for x in s["reminded"] if x != [wkey, cand]]) return sync_late(s, cwd, sid, False) or print("extended %s limit to %d min." % (wkey, lim)) or 0 isinstance(s.get("meta"), dict) and s["meta"].pop("%s %r" % (wkey, cand), None) if s.get("plan"): from . import plan; plan.judged(s, wkey, cand, args[1]) if ok_t: began = cand minutes = minutes or max(1, int((now - began) / 60)) note = " (job started %s, %d more open for this worker)" % (time.strftime("%H:%M", time.localtime(began)), len(starts)) sync_late(s, cwd, sid, False) elif ext: return print("sisi judge: no open job for %s." % args[0]) or 1 iso = lambda t: time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(t)) rec = {"ts": iso(now), "ended": iso(now), "worker": args[0], "model": model, "verdict": args[1], "why": retro.clean(args[2]), "project": os.path.basename(store.project_root(cwd)), "project_key": store.project_key(cwd)} if sid: rec["session_id"] = sid if began is not None: rec["started"] = iso(began) if minutes: rec["minutes"] = minutes log = store.data("outcomes.jsonl") store.append(log, json.dumps(rec, sort_keys=True)) ok = store.commit("delegation judged", log) print("%s%s" % ("recorded: %d min%s." % (minutes, note) if minutes else "recorded, not timed.", "" if ok else " (store commit failed)")) return 0 def run(args, cwd=None): if not args: text = store.read(table()).rstrip() print(text or stale()) if text: print("\n(researched %d days ago)" % age_days()) gone, now, listed = away(), time.time(), {short(r[0]) for r in rows(text)} if sitting := sorted((t, w) for w, (t, _) in gone.items() if w in listed and now - t < OUT): print("\nOff the wheel now:\n" + "\n".join("%s: %s" % (w, SINCE % (clock(t), clock(t + OUT))) for t, w in sitting)) if use := plan_use(text, cwd, now): print("\nPlan use by sisi's own delegations (an estimate; the plan's own usage page is exact):\n" + "\n".join( "%s: %s" % (p, ", ".join("%s %d%%" % (w, round(100 * x)) for w, x in u.items())) for p, u in sorted(use.items()))) if recent := outcomes(15): print("\nHow recent delegations went:\n" + "\n".join(recent)) if stale(): print("\n" + stale()) return 0 if args == ["research"]: from . import plan return plan.issue("models research", research_brief(), cwd) or 0 if len(args) == 3 and args[:2] == ["research", "--record"]: why = research_record(args[2]) return (print("sisi models: %s. Nothing was changed." % why) or 1) if why else 0 print("usage: sisi models | sisi models research [--record FILE]") return 1