diff --git a/sisi/cli.py b/sisi/cli.py index 2ccccb0..706f548 100644 --- a/sisi/cli.py +++ b/sisi/cli.py @@ -41,7 +41,7 @@ Hooks (the agent tool calls these): sisi tick | prompt | watch | gate Your rules, models table and retro log live in ~/.local/share/sisi ($SISI_DATA, else $XDG_DATA_HOME/sisi), a git repository. Settings: "core" in its config.json (gate, meter, direct_calls, direct_extension, rules_lines, rules_chars, organize_after, -research_days, shell_workers).""" +research_days, shell_workers, worker_minutes).""" def main(argv): @@ -66,11 +66,8 @@ def main(argv): if cmd in ("install", "uninstall", "release", "update", "rollback", "doctor"): from . import install return getattr(install, cmd)() - if cmd == "rulebook": - print(rules.build_rulebook()) - return 0 - if cmd == "version": - print(store.read(os.path.join(store.ROOT, "VERSION")).strip()) + if cmd in ("rulebook", "version"): + print(rules.build_rulebook() if cmd == "rulebook" else store.read(os.path.join(store.ROOT, "VERSION")).strip()) return 0 print(HELP) return 0 if cmd in ("help", "--help", "-h") else 1 diff --git a/sisi/hooks.py b/sisi/hooks.py index 856a81f..feb8f0d 100644 --- a/sisi/hooks.py +++ b/sisi/hooks.py @@ -27,7 +27,8 @@ FAILED = ("sisi: check %s failed. A failed check is a gap: record it with `sisi "short>\" --cause \"\" --improve \"\" --term `.") OPEN = ("sisi: these checks have no verdict yet: %s. Give each one: sisi done --pass N \"\" or --fail N " "\"\". If the work is only pausing: sisi retro --later \"\".") - +LATE = ("%s was started %s and has not reported. Check it now: if it is dead or no longer wanted, cancel it with the " + "host's tool and record `sisi judge WORKER bad \"\"`; if it reported, judge it.") # The names each host gives a hook's fields; the first one present (and of the right type) wins. FIELDS = (("session_id", str, ("session_id", "sessionId", "conversation_id", "conversationId")), @@ -196,17 +197,19 @@ def gate(p, for_subagent=False): return 0 with store.session(sid) as s: _begin(s, cwd) - if s.get("worker") or s.get("paused"): + if s.get("worker"): return 0 - checks = (s.get("done") or {}).get("checks") or [] - unsaid = ["%d. %s" % (i, c["text"]) for i, c in enumerate(checks, 1) if not c["verdict"]] - failed = [str(i) for i, c in enumerate(checks, 1) if c["verdict"] == "fail"] - worked = s.get("changed") or store.snapshot(cwd) != s.get("snapshot") - if checks and not unsaid and not failed and not worked: - from . import retro - retro.record(s, cwd, {}) # every check passed and nothing changed: the work closes itself - return 0 - reason = OPEN % "; ".join(unsaid) if unsaid else GATE if worked else FAILED % ", ".join(failed) if failed else "" + reason = " ".join("sisi: " + LATE % (w, time.strftime("%H:%M", time.localtime(t))) for w, t in models.overdue(s)) + if not reason and not s.get("paused"): + checks = (s.get("done") or {}).get("checks") or [] + unsaid = ["%d. %s" % (i, c["text"]) for i, c in enumerate(checks, 1) if not c["verdict"]] + failed = [str(i) for i, c in enumerate(checks, 1) if c["verdict"] == "fail"] + worked = s.get("changed") or store.snapshot(cwd) != s.get("snapshot") + if checks and not unsaid and not failed and not worked: + from . import retro + retro.record(s, cwd, {}) # every check passed and nothing changed: the work closes itself + return 0 + reason = OPEN % "; ".join(unsaid) if unsaid else GATE if worked else FAILED % ", ".join(failed) if failed else "" if not reason or s.get("blocks", 0) >= 2: return 0 s["blocks"] = s.get("blocks", 0) + 1 diff --git a/sisi/models.py b/sisi/models.py index 0f84c13..78cc4e0 100644 --- a/sisi/models.py +++ b/sisi/models.py @@ -61,10 +61,8 @@ def age_days(): def stale(): """One line when the table is missing or old, else "".""" age = age_days() - if age is None: - return "sisi: there is no models table yet. " + ASK - if age > store.config()["research_days"]: - return "sisi: the models table is %d days old. %s" % (age, ASK) + 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 "" @@ -133,6 +131,13 @@ def claim(s, text, segment=False): return False +def overdue(s): + """Open delegations older than `worker_minutes`: [(worker, its oldest such start)]. A bad or future start is none.""" + mins, starts = store.config()["worker_minutes"], s.get("starts") + old = lambda v: [t for t in v if type(t) in (int, float) and math.isfinite(t) and t <= time.time() - 60 * mins] if isinstance(v, list) else [] + return [(w, min(old(v))) for w, v in (starts.items() if isinstance(starts, dict) and type(mins) in (int, float) else []) if old(v)] + + def out(r, skip, bad, today): """Why this worker is off the wheel, "" when it is on.""" until = [d for d in re.findall(r"out until (\d{4}-\d{2}-\d{2})", " ".join(r).lower()) if d > today] diff --git a/sisi/retro.py b/sisi/retro.py index 5d1c673..8cbf3e3 100644 --- a/sisi/retro.py +++ b/sisi/retro.py @@ -163,8 +163,7 @@ def run(args, cwd=None): cwd = cwd or os.getcwd() sid = store.current_session(cwd) if not args: - report(sid, cwd) - return 0 + return report(sid, cwd) or 0 opt = _options(args) scope, term, better = [(opt or {}).pop(k, None) for k in ("scope", "term", "better")] scope = scope or rules.PROJECT diff --git a/sisi/store.py b/sisi/store.py index 21f671b..ef806be 100644 --- a/sisi/store.py +++ b/sisi/store.py @@ -18,7 +18,8 @@ LIB = os.environ.get("SISI_LIB") or os.path.join(HOME, ".local", "lib", "sisi") # Every setting there is. The person overrides one under "core" in config.json in their store. DEFAULTS = {"gate": True, "meter": True, "direct_calls": 8, "direct_extension": 8, - "rules_lines": 30, "rules_chars": 4000, "organize_after": 8, "research_days": 7, "shell_workers": False} + "rules_lines": 30, "rules_chars": 4000, "organize_after": 8, "research_days": 7, "shell_workers": False, + "worker_minutes": 15} def config(): @@ -97,9 +98,7 @@ def project_root(cwd=None): """The project a folder belongs to. Every checkout of one repository is the same project.""" cwd = cwd or os.getcwd() rc, out = git(["rev-parse", "--path-format=absolute", "--git-common-dir"], cwd) - if rc == 0 and os.path.basename(out.strip()) == ".git": - return os.path.dirname(out.strip()) - return os.path.abspath(cwd) + return os.path.dirname(out.strip()) if rc == 0 and os.path.basename(out.strip()) == ".git" else os.path.abspath(cwd) def project_key(cwd=None): diff --git a/tests/test_core.py b/tests/test_core.py index 598b153..e922f02 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1237,6 +1237,38 @@ class Core(unittest.TestCase): # the standing constraints + def test_the_gate_blocks_on_a_worker_that_was_started_and_never_reported(self): + self.table(WORKERS) + self.hook("tick") + self.hook("prompt", prompt="go") + self.done() + self.assertEqual((self.sisi("pick", "hard", sid="s1")[0], self.tool("Agent", tool_input={"model": "sonnet"})[0]), (0, 0)) + + def began(minutes): # the clock: sonnet's open job began this long ago + with open(os.path.join(self.tmp, "state", "core-sessions", "s1.json"), "r+") as f: + st = json.load(f) + st["starts"]["sonnet"] = [time.time() - minutes * 60] if type(minutes) is int else minutes + f.seek(0), f.truncate(), json.dump(st, f) + late = lambda: "has not reported" in self.hook("gate")[1] + began(5) + self.assertFalse(late()) # younger than the limit + for bad in (["x"], [time.time() + 3600], [float("nan")], "x", [None]): # malformed or future: never a block, never a crash + began(bad) + self.assertFalse(late(), bad) + began(16) + self.hook("prompt", prompt="go on") + rc, out, _ = self.hook("gate") + self.assertEqual((rc, '"block"' in out, "sonnet was started" in out, "judge WORKER bad" in out), (0, True, True, True), out) + self.sisi("retro", "--later", "waiting for sonnet", sid="s1") + began(16) # a retro ends the stored starts; this one is still open + self.hook("prompt", prompt="and again") + self.assertTrue(late()) # a pause does not silence it + self.assertTrue(late()) + self.assertFalse(late()) # at most two blocks in a turn + self.hook("prompt", prompt="again") + self.assertEqual(self.sisi("judge", "sonnet", "bad", "cancelled, never reported", sid="s1")[0], 0) + self.assertFalse(late()) + def test_the_spec_is_unchanged(self): for line in self.read(REPO, "docs", "spec", "SHA256SUMS").splitlines(): digest, rel = line.split()