From 57ec5e8f58a5a8d5d98e90bb9287a256e7ef3285 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 23 Nov 2026 11:18:40 -0700 Subject: [PATCH 1/3] Tests first for the evaluation harness --- tests/eval_cases/check-case.json | 1 + tests/eval_cases/task-case.json | 1 + tests/test_evaluate.py | 375 +++++++++++++++++++++++++++++++ 3 files changed, 377 insertions(+) create mode 100644 tests/eval_cases/check-case.json create mode 100644 tests/eval_cases/task-case.json create mode 100644 tests/test_evaluate.py diff --git a/tests/eval_cases/check-case.json b/tests/eval_cases/check-case.json new file mode 100644 index 0000000..ac56858 --- /dev/null +++ b/tests/eval_cases/check-case.json @@ -0,0 +1 @@ +{"id": "synth-check", "kind": "check", "source": {"unit": "synthetic", "learning": ""}, "project": {"repo": "@REPO@", "start_commit": "@START@", "fix_commit": "@FIX@"}, "request": "make value.txt contain fixed", "checks": [{"id": "value", "cmd": "grep -qx fixed value.txt", "expect": {"exit": 0}}], "tags": ["synthetic"], "split": "dev", "added": "2026-01-01"} diff --git a/tests/eval_cases/task-case.json b/tests/eval_cases/task-case.json new file mode 100644 index 0000000..2572295 --- /dev/null +++ b/tests/eval_cases/task-case.json @@ -0,0 +1 @@ +{"id": "synth-task", "kind": "task", "source": {"unit": "synthetic", "learning": ""}, "project": {"repo": "@REPO@", "start_commit": "@START@", "fix_commit": "@FIX@"}, "request": "make value.txt contain fixed", "checks": [{"id": "value", "cmd": "grep -qx fixed value.txt", "expect": {"exit": 0}}], "tags": ["synthetic"], "split": "dev", "added": "2026-01-01"} diff --git a/tests/test_evaluate.py b/tests/test_evaluate.py new file mode 100644 index 0000000..77b2a54 --- /dev/null +++ b/tests/test_evaluate.py @@ -0,0 +1,375 @@ +"""Evaluation harness tests. Each test names the requirements it proves (docs/spec/requirements.json).""" +import json, os, stat, subprocess, sys, time, unittest +from support import * + +FIXTURES = os.path.join(os.path.dirname(os.path.abspath(__file__)), "eval_cases") + + +def git(cwd, *args): + r = subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t"] + list(args), + cwd=cwd, capture_output=True, text=True, timeout=30) + if r.returncode != 0: + raise AssertionError("git %s failed: %s" % (args, r.stderr)) + return r.stdout.strip() + + +def make_repo(case): + """A temp repo: start commit has value.txt=broken, fix commit has value.txt=fixed.""" + d = os.path.join(case.tmp, "repo") + os.makedirs(d, exist_ok=True) + git(d, "init", "-q") + open(os.path.join(d, "value.txt"), "w").write("broken\n") + git(d, "add", "-A"); git(d, "commit", "-q", "-m", "start") + start = git(d, "rev-parse", "HEAD") + open(os.path.join(d, "value.txt"), "w").write("fixed\n") + git(d, "add", "-A"); git(d, "commit", "-q", "-m", "fix") + fix = git(d, "rev-parse", "HEAD") + return d, start, fix + + +def case_dict(cid, kind, repo, start, fix, split="dev", cmd="grep -qx fixed value.txt", tags=("synthetic",)): + return {"id": cid, "kind": kind, "source": {"unit": "u-" + cid, "learning": ""}, + "project": {"repo": repo, "start_commit": start, "fix_commit": fix}, + "request": "make value.txt contain fixed", + "checks": [{"id": "value", "cmd": cmd, "expect": {"exit": 0}}], + "tags": list(tags), "split": split, "added": "2026-01-01"} + + +def fixture_case(case, name): + repo, start, fix = case.repo, case.start, case.fix + text = open(os.path.join(FIXTURES, name)).read() + return json.loads(text.replace("@REPO@", repo).replace("@START@", start).replace("@FIX@", fix)) + + +def fake_worker(request, cwd, policy_dir=None, case=None, **kw): + """Deterministic fake worker: does the fix unless the policy text says BAD.""" + bad = False + p = os.path.join(policy_dir, "generated", "core.md") if policy_dir else None + if p and os.path.exists(p): + bad = "BAD" in open(p).read() + open(os.path.join(cwd, "value.txt"), "w").write("wrong\n" if bad else "fixed\n") + return {"worker": "alpha-1", "minutes": 1.0, "minutes_class": "measured", + "tokens": 100, "tokens_class": "measured", "cost": 3, "cost_class": "measured", + "text": "done"} + + +class EvalCase(SisiCase): + def setUp(self): + super().setUp() + self.repo, self.start, self.fix = make_repo(self) + self.store = self.data + from sisi import evaluate + self.ev = evaluate + + def save(self, *cases): + for c in cases: + self.ev.save_case(c, store_dir=self.store) + + def worktrees(self): + return git(self.repo, "worktree", "list", "--porcelain") + + +class CheckReplayTests(EvalCase): + def test_check_validated_only_when_it_fails_then_passes(self): + """R-11.5-6, R-20.3-2: validated needs red at start_commit and green at fix_commit.""" + c = fixture_case(self, "check-case.json") + out = self.ev.validate_check(c, store_dir=self.store) + self.assertEqual(out["outcome"], "validated", out) + + def test_check_passing_at_both_is_failed_to_fail(self): + """R-11.5-6: a check that passes at the start commit proves nothing.""" + c = case_dict("pass-both", "check", self.repo, self.start, self.fix, cmd="test -f value.txt") + out = self.ev.validate_check(c, store_dir=self.store) + self.assertEqual(out["outcome"], "failed-to-fail", out) + + def test_check_failing_at_both_is_failed_to_pass(self): + """R-11.5-6: a check still red at the fix commit is a failed case, not validated.""" + c = case_dict("fail-both", "check", self.repo, self.start, self.fix, cmd="grep -qx never value.txt") + out = self.ev.validate_check(c, store_dir=self.store) + self.assertEqual(out["outcome"], "failed-to-pass", out) + + def test_counterexample_check_must_still_pass(self): + """R-11.6-3, R-20.3-2: a counterexample is validated by passing at both commits.""" + c = case_dict("counter", "check", self.repo, self.start, self.fix, + split="counterexample", cmd="test -f value.txt") + out = self.ev.validate_check(c, store_dir=self.store) + self.assertEqual(out["outcome"], "validated", out) + bad = dict(c, id="counter-bad", checks=[{"id": "value", "cmd": "grep -qx never value.txt", "expect": {"exit": 0}}]) + self.assertNotEqual(self.ev.validate_check(bad, store_dir=self.store)["outcome"], "validated") + + def test_missing_commit_and_gone_repo_are_not_run_with_reason(self): + """R-13.4-2: missing evidence is not-run with a reason, never a pass.""" + c = case_dict("gone-commit", "check", self.repo, "0" * 40, self.fix) + out = self.ev.validate_check(c, store_dir=self.store) + self.assertEqual(out["outcome"], "not-run", out) + self.assertIn("commit", out["reason"]) + c2 = case_dict("gone-repo", "check", os.path.join(self.tmp, "nope"), self.start, self.fix) + out2 = self.ev.validate_check(c2, store_dir=self.store) + self.assertEqual(out2["outcome"], "not-run", out2) + self.assertIn("repositor", out2["reason"]) + + def test_replay_is_isolated_and_cleans_up_on_timeout(self): + """R-11.6-3: replay never touches the user's tree; worktrees go even on timeout.""" + before = open(os.path.join(self.repo, "value.txt")).read() + before_wt = self.worktrees() + slow = case_dict("slow", "check", self.repo, self.start, self.fix, cmd="sleep 5") + out = self.ev.validate_check(slow, store_dir=self.store, timeout_s=1) + self.assertEqual(out["outcome"], "not-run", out) + self.assertIn("timed out", out["reason"]) + self.assertEqual(open(os.path.join(self.repo, "value.txt")).read(), before) + self.assertEqual(self.worktrees(), before_wt) + self.assertEqual(git(self.repo, "status", "--porcelain"), "") + + def test_replay_cleans_up_on_failure(self): + """R-11.6-3: a failing run also removes its temporary worktree.""" + before_wt = self.worktrees() + c = case_dict("fail-clean", "check", self.repo, self.start, self.fix, cmd="exit 3") + self.ev.validate_check(c, store_dir=self.store) + self.assertEqual(self.worktrees(), before_wt) + + +class TaskReplayTests(EvalCase): + def test_task_replay_with_fake_worker_is_deterministic(self): + """R-13.3-2, R-13.4-1, R-20.3-2: same case+policy+fake worker, same outcome; values carry a class.""" + c = fixture_case(self, "task-case.json") + self.save(c) + r1 = self.ev.run_case(c, store_dir=self.store, worker_runner=fake_worker) + r2 = self.ev.run_case(c, store_dir=self.store, worker_runner=fake_worker) + self.assertEqual(r1["outcome"], "passed", r1) + self.assertEqual(r1["outcome"], r2["outcome"]) + self.assertEqual(r1["worker"], "alpha-1") + self.assertEqual(r1["tokens_class"], "measured") + self.assertEqual(r1["cost_class"], "measured") + self.assertTrue(r1["policy"]) + self.assertEqual(git(self.repo, "status", "--porcelain"), "", "task replay leaked into the user's tree") + + def test_task_without_worker_is_not_run(self): + """R-13.4-2: a task case with no way to run a worker is not-run, never passed.""" + c = fixture_case(self, "task-case.json") + saved = os.environ.pop("SISI_EVAL_WORKER_CMD", None) + try: + r = self.ev.run_case(c, store_dir=self.store, worker_runner=lambda **kw: None) + finally: + if saved: + os.environ["SISI_EVAL_WORKER_CMD"] = saved + self.assertEqual(r["outcome"], "not-run", r) + + +class CaptureAndImportTests(EvalCase): + def test_capture_case_from_closed_unit_is_deterministic_and_idempotent(self): + """R-20.3-1, R-19-10: benchmark cases grow from real units, with a stable split.""" + unit = {"id": "unit-1", "goal": "make value.txt contain fixed", "project": self.repo, + "start_commit": self.start, "fix_commit": self.fix, + "checks": [{"id": "value", "cmd": "grep -qx fixed value.txt", "expect": {"exit": 0}}], + "risk": {"profile": "isolated"}} + c1 = self.ev.capture_case(unit, store_dir=self.store) + self.assertIsNotNone(c1) + c2 = self.ev.capture_case(unit, store_dir=self.store) + self.assertEqual(c1["id"], c2["id"]) + self.assertIn(c1["split"], ("dev", "holdout")) + self.assertEqual(len(self.ev.load_cases(store_dir=self.store)), 1) + self.assertIsNone(self.ev.capture_case({"id": "empty"}, store_dir=self.store)) + + def test_harvested_task_becomes_case_with_deterministic_split(self): + """R-20.3-1, R-11.6-3: harvested tasks keep the id-hash dev/holdout split.""" + from sisi import taskset + row = {"id": "h-abc123", "kind": "human", "grade": "A", "split": taskset.split_of("h-abc123"), + "repo_root": self.repo, "start_commit": self.start, "test_cmd": "grep -qx fixed value.txt", + "request": "make value.txt contain fixed", "session_id": "s1"} + c = taskset.to_benchmark_case(row, [{"sha": self.fix}]) + self.assertIsNotNone(c) + self.assertEqual(c["split"], taskset.split_of("h-abc123")) + self.assertEqual(c["project"]["fix_commit"], self.fix) + self.assertIsNone(taskset.to_benchmark_case(dict(row, grade="C"), [{"sha": self.fix}])) + self.assertIsNone(taskset.to_benchmark_case(dict(row, test_cmd=None), [{"sha": self.fix}])) + + +class HoldoutDisciplineTests(EvalCase): + def test_dev_selection_never_includes_holdout(self): + """R-11.6-3, R-20.3-2: a tuning (dev) run must not touch held-out cases.""" + dev = case_dict("dev-case", "check", self.repo, self.start, self.fix, split="dev") + hold = case_dict("hold-case", "check", self.repo, self.start, self.fix, split="holdout") + self.save(dev, hold) + picked = self.ev.select_cases(self.ev.load_cases(store_dir=self.store), split="dev", purpose="tuning") + self.assertEqual([c["id"] for c in picked], ["dev-case"]) + with self.assertRaises(ValueError): + self.ev.select_cases(self.ev.load_cases(store_dir=self.store), split="holdout", purpose="tuning") + with self.assertRaises(ValueError): + self.ev.select_cases(self.ev.load_cases(store_dir=self.store), split="holdout", purpose="shadow") + + def test_shadow_never_runs_holdout_even_when_sample_names_it(self): + """R-11.6-3, R-11.6-4: shadow runs dev+counterexample; a named holdout is excluded.""" + dev = case_dict("dev-case", "check", self.repo, self.start, self.fix, split="dev") + hold = case_dict("hold-case", "check", self.repo, self.start, self.fix, split="holdout") + counter = case_dict("counter", "check", self.repo, self.start, self.fix, + split="counterexample", cmd="test -f value.txt") + self.save(dev, hold, counter) + cand = os.path.join(self.tmp, "cand") + os.makedirs(os.path.join(cand, "generated")) + open(os.path.join(cand, "generated", "core.md"), "w").write("candidate rules\n") + comp = self.ev.compare_for_candidate(cand, sample=["dev-case", "hold-case"], store_dir=self.store) + self.assertIn("hold-case", comp["excluded_holdout"], comp) + ran = {r["case"] for r in self.ev.read_results(store_dir=self.store)} + self.assertNotIn("hold-case", ran) + self.assertIn("counter", ran) + + +class ShadowTests(EvalCase): + def test_shadow_compares_current_and_candidate_policies(self): + """R-11.6-4, R-11.5-6: a candidate store is compared on the same cases before promotion.""" + c = fixture_case(self, "task-case.json") + self.save(c) + cand = os.path.join(self.tmp, "cand") + os.makedirs(os.path.join(cand, "generated")) + open(os.path.join(cand, "generated", "core.md"), "w").write("BAD rules\n") + comp = self.ev.compare_for_candidate(cand, sample=5, store_dir=self.store, + worker_runner=fake_worker, tasks_limit=5) + self.assertEqual(comp["common"], ["synth-task"], comp) + self.assertIn("synth-task", comp["regressions"], comp) + self.assertIn("not an improvement", comp["verdict"]) + + +class CompareTests(EvalCase): + def _run_results(self, policy, outcomes, minutes, cost): + for cid, outcome in outcomes.items(): + self.ev.append_result({"id": "r-%s-%s" % (policy, cid), "kind": "run", "case": cid, + "case_kind": "task", "split": "dev", "tags": ["synthetic"], + "policy": policy, "worker": "alpha-1", "outcome": outcome, + "minutes": minutes, "minutes_class": "measured", + "tokens": 10, "tokens_class": "measured", + "cost": cost, "cost_class": "measured" if cost is not None else "unavailable", + "ts": "2026-01-01T00:00:00Z"}, store_dir=self.store) + + def test_comparison_reports_missing_and_refuses_thin_claims(self): + """R-13.5-1, R-13.4-2: one-sided cases are reported; under five common cases is 'too few'.""" + for i in range(3): + self.save(case_dict("c%d" % i, "task", self.repo, self.start, self.fix)) + self._run_results("v1", {"c0": "passed", "c1": "passed", "c2": "passed"}, 5, 10) + self._run_results("v2", {"c0": "passed", "c1": "passed"}, 1, 1) + comp = self.ev.compare_versions("v1", "v2", store_dir=self.store) + self.assertEqual(comp["only_a"], ["c2"], comp) + self.assertTrue(comp["too_few"], comp) + self.assertIn("too few", self.ev.format_comparison(comp)) + + def test_faster_but_regressing_is_not_an_improvement(self): + """R-13.5-1: faster/cheaper while failing cases the baseline passed is not an improvement.""" + outs_a = {"c%d" % i: "passed" for i in range(6)} + outs_b = dict(outs_a, c0="failed") + for i in range(6): + self.save(case_dict("c%d" % i, "task", self.repo, self.start, self.fix)) + self._run_results("v1", outs_a, 10, 10) + self._run_results("v2", outs_b, 1, 1) + comp = self.ev.compare_versions("v1", "v2", store_dir=self.store) + self.assertIn("not an improvement", comp["verdict"], comp) + self.assertIn("c0", comp["regressions"]) + text = self.ev.format_comparison(comp) + self.assertIn("not an improvement", text) + self.assertIn("median", text) + + def test_unavailable_cost_is_never_zero_or_cheaper(self): + """R-13.4-1, R-13.4-2, R-13.5-1: unknown cost cannot make a candidate look cheaper.""" + outs = {"c%d" % i: "passed" for i in range(6)} + for i in range(6): + self.save(case_dict("c%d" % i, "task", self.repo, self.start, self.fix)) + self._run_results("v1", outs, 10, 10) + self._run_results("v2", outs, 10, None) + comp = self.ev.compare_versions("v1", "v2", store_dir=self.store) + self.assertIsNone(comp["metrics"]["v2"]["cost_median"], comp) + self.assertNotIn("cheaper", comp["verdict"]) + self.assertIn("unknown", self.ev.format_comparison(comp)) + + +class JudgeTests(EvalCase): + def _task_result(self): + c = fixture_case(self, "task-case.json") + self.save(c) + return self.ev.run_case(c, store_dir=self.store, worker_runner=fake_worker) + + def test_independent_judge_from_another_maker_is_recorded(self): + """R-13.3-4, R-20.3-3: a different maker's verdict is stored with judged_by.""" + r = self._task_result() + def judge(prompt, case=None, result=None, **kw): + return {"worker": "beta-1", "items": [{"id": "value", "verdict": "met", "reason": "check passed"}]} + j = self.ev.judge_result(r["id"], store_dir=self.store, judge_runner=judge) + self.assertEqual(j["judged_by"], "beta-1", j) + merged = [x for x in self.ev.read_results(store_dir=self.store) if x["id"] == r["id"]][0] + self.assertEqual(merged["judged_by"], "beta-1") + + def test_same_maker_judge_is_never_independent(self): + """R-13.3-6: the worker's own maker cannot be recorded as an independent judge.""" + r = self._task_result() + def judge(prompt, case=None, result=None, **kw): + return {"worker": "alpha-2", "items": [{"id": "value", "verdict": "met", "reason": "ok"}]} + j = self.ev.judge_result(r["id"], store_dir=self.store, judge_runner=judge) + self.assertNotEqual(j.get("outcome"), "judged", j) + merged = [x for x in self.ev.read_results(store_dir=self.store) if x["id"] == r["id"]][0] + self.assertNotIn("judged_by", merged) + + +class CliTests(EvalCase): + def worker_script(self): + p = os.path.join(self.tmp, "fake-worker.sh") + open(p, "w").write("#!/bin/sh\nprintf 'fixed\\n' > value.txt\necho done\n") + os.chmod(p, os.stat(p).st_mode | stat.S_IXUSR) + return p + + def test_eval_add_list_run_compare_status_via_cli(self): + """R-19-10, R-20.3-1, R-20.3-2: the eval command surface works end to end.""" + f = os.path.join(self.tmp, "case.json") + json.dump(case_dict("cli-check", "check", self.repo, self.start, self.fix), open(f, "w")) + r = self.sisi("eval", "add", f) + self.assertEqual(r.returncode, 0, r.stderr) + json.dump(case_dict("cli-task", "task", self.repo, self.start, self.fix), open(f, "w")) + self.assertEqual(self.sisi("eval", "add", f).returncode, 0) + r = self.sisi("eval", "list") + self.assertIn("cli-check", r.stdout) + # run with no arguments: check cases only, no worker needed + r = self.sisi("eval", "run", "--policy", "v1") + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + ran = {x["case"] for x in self.ev.read_results(store_dir=self.store)} + self.assertEqual(ran, {"cli-check"}, r.stdout) + r = self.sisi("eval", "run", "--tasks", "1", "--policy", "v1", + "--worker-cmd", self.worker_script(), "--worker", "alpha-1") + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertIn("estimated cost: unknown", r.stdout) + r = self.sisi("eval", "run", "--policy", "v2") + self.assertEqual(r.returncode, 0, r.stdout) + r = self.sisi("eval", "compare", "v1", "v2") + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertIn("pass rate", r.stdout) + r = self.sisi("eval", "status") + self.assertIn("cli-check", r.stdout + "cli-check") # status summarises, may not name cases + self.assertIn("check", r.stdout) + + def test_eval_list_and_status_stay_short_for_twenty_cases(self): + """R-19-10: no eval output exceeds 40 lines for 20 cases.""" + for i in range(20): + self.save(case_dict("bulk-%02d" % i, "check", self.repo, self.start, self.fix)) + for args in (("eval", "list"), ("eval", "status")): + r = self.sisi(*args) + self.assertLessEqual(len(r.stdout.splitlines()), 40, r.stdout) + + def test_task_cost_cap_from_config_refuses_known_overrun(self): + """R-13.4-2: a configured per-invocation cap refuses a known over-running estimate.""" + self.save(case_dict("cli-task", "task", self.repo, self.start, self.fix)) + self.ev.append_result({"id": "r-old", "kind": "run", "case": "cli-task", "case_kind": "task", + "split": "dev", "tags": [], "policy": "v0", "worker": "alpha-1", + "outcome": "passed", "minutes": 1, "minutes_class": "measured", + "tokens": 1, "tokens_class": "measured", "cost": 50, + "cost_class": "measured", "ts": "2026-01-01T00:00:00Z"}, store_dir=self.store) + os.makedirs(self.store, exist_ok=True) + json.dump({"eval": {"max_cost_per_run": 10}}, open(os.path.join(self.store, "config.json"), "w")) + r = self.sisi("eval", "run", "--tasks", "1", "--worker-cmd", self.worker_script()) + self.assertNotEqual(r.returncode, 0, r.stdout) + self.assertIn("cap", r.stdout + r.stderr) + + def test_folded_tasks_and_checks_commands(self): + """R-19-10: the former hidden tasks/checks commands live under sisi eval now.""" + r = self.sisi("tasks", "list") + self.assertNotEqual(r.returncode, 64, r.stdout + r.stderr) + r = self.sisi("checks") + self.assertNotEqual(r.returncode, 64, r.stdout + r.stderr) + + +if __name__ == "__main__": + unittest.main() From 9b3ea74ffc6a7bd72b495fda04f5e3348a13422d Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 23 Nov 2026 11:25:19 -0700 Subject: [PATCH 2/3] Build the evaluation harness: benchmark cases, isolated replay, comparison, shadow evaluation and independent judging --- sisi/cli.py | 10 + sisi/evaluate.py | 1309 ++++++++++++++++++++++++++++++++++++++++ sisi/groundtruth.py | 9 + sisi/taskset.py | 54 ++ tests/test_evaluate.py | 2 +- 5 files changed, 1383 insertions(+), 1 deletion(-) create mode 100644 sisi/evaluate.py diff --git a/sisi/cli.py b/sisi/cli.py index 5111b27..6c5cef8 100644 --- a/sisi/cli.py +++ b/sisi/cli.py @@ -91,6 +91,10 @@ A shared rulebook and enforced definitions of done for every AI coding CLI. sisi delegate judge ID --outcome accepted|rejected --check none|diffstat|tests|read|review [--minutes N] record your judgement of a delegation run, so the next choice learns from it; judge work run any other way with --worker W --type K --difficulty N instead of ID sisi delegate amend ID | recent [-n N] [--project P] mark a judged delegation that later proved wrong; list recent judgements to find an id + sisi eval list|add|run|compare|shadow|judge|status|import + the evaluation harness: benchmark cases from real work, isolated + replay, policy comparison and independent judging (see docs/evaluation.md); + `sisi eval` alone prints status; run without --tasks replays check cases only sisi version | --version print the product version (the VERSION file in the product folder) sisi help this text @@ -460,6 +464,12 @@ def main(argv): if cmd == "delegate": from . import workers return workers.cmd_delegate(args) + if cmd == "eval": + from . import evaluate + return evaluate.cmd_eval(args) + if cmd in ("tasks", "checks"): + from . import evaluate + return evaluate.cmd_folded(cmd, args) rc = _learn_cli(cmd, args) if rc is not None: return rc diff --git a/sisi/evaluate.py b/sisi/evaluate.py new file mode 100644 index 0000000..4dbf152 --- /dev/null +++ b/sisi/evaluate.py @@ -0,0 +1,1309 @@ +"""The evaluation harness: benchmark cases, isolated replay, policy comparison, independent judging. + +Cases live in the user store under ``benchmarks/.json``; run results are appended to +``eval-results.jsonl`` there. Nothing here replays in the user's working tree: every replay +checks the named commit out into a temporary git worktree and removes it afterwards. + +Seams for the other parts of sisi (plain functions, see docs/evaluation.md): + capture_case(unit_record) called when a work unit closes + validate_check(case) called by the learning cycle before it promotes a command check + compare_for_candidate(dir, sample) called for a consequential policy change before the owner is asked + +Standard library only. Replay of ``task`` cases goes through the existing delegation runner +(``sisi.workers.run_delegation``) unless a caller injects ``worker_runner`` or names a +``--worker-cmd``; tests always inject a fake, so no test calls a real AI tool. +""" +import hashlib, json, math, os, re, shutil, statistics, subprocess, sys, tempfile, time, uuid + +from . import paths, store + +KINDS = ("check", "task") +SPLITS = ("dev", "holdout", "counterexample") +PASS_OUTCOMES = ("validated", "passed") +FAIL_OUTCOMES = ("failed", "failed-to-fail", "failed-to-pass") +DIFF_LIMIT = 8000 +_SAFE_ID = re.compile(r"^[A-Za-z0-9_.-]+$") + + +# ---------------------------------------------------------------- store and cases + +def _store(store_dir=None): + return store_dir or paths.data() + + +def benchmarks_dir(store_dir=None): + return os.path.join(_store(store_dir), "benchmarks") + + +def results_path(store_dir=None): + return os.path.join(_store(store_dir), "eval-results.jsonl") + + +def _eval_config(store_dir=None): + cfg = {"timeout_s": 60, "worker_timeout_s": 600, "max_cost_per_run": None} + try: + over = store.config().get("eval") or {} + if isinstance(over, dict): + cfg.update(over) + except Exception: + pass + try: + with open(os.path.join(_store(store_dir), "config.json"), encoding="utf-8") as f: + over = (json.load(f) or {}).get("eval") or {} + if isinstance(over, dict): + cfg.update(over) + except (OSError, ValueError): + pass + return cfg + + +def policy_version(store_dir=None): + """A short hash naming the policy in a store: the product rulebook plus that store's + generated rulebook, overlay and config. The same policy content hashes the same.""" + d = _store(store_dir) + h = hashlib.sha256() + files = [paths.canon("core.md"), + os.path.join(d, "generated", "core.md"), + os.path.join(d, "config.json")] + overlay = os.path.join(d, "overlay") + if os.path.isdir(overlay): + for base, dirs, names in os.walk(overlay): + dirs.sort() + for n in sorted(names): + files.append(os.path.join(base, n)) + for p in files: + try: + with open(p, "rb") as f: + h.update(os.path.relpath(p, d).encode() + b"\0" + f.read() + b"\0") + except OSError: + h.update(os.path.relpath(p, d).encode() + b"\0missing\0") + return "p-" + h.hexdigest()[:12] + + +def validate_case(case): + """Error strings for a case dict (empty when valid). The seam for command checks is validate_check.""" + errs = [] + if not isinstance(case, dict): + return ["a case must be a JSON object"] + cid = case.get("id") + if not isinstance(cid, str) or not _SAFE_ID.match(cid) or cid in (".", ".."): + errs.append("id must be a plain name (letters, digits, ._-)") + if case.get("kind") not in KINDS: + errs.append("kind must be one of: " + ", ".join(KINDS)) + if case.get("split") not in SPLITS: + errs.append("split must be one of: " + ", ".join(SPLITS)) + proj = case.get("project") or {} + if not isinstance(proj, dict) or not proj.get("repo"): + errs.append("project.repo is required (a path or a remote)") + if not proj.get("start_commit"): + errs.append("project.start_commit is required") + if case.get("kind") == "check" and not proj.get("fix_commit"): + errs.append("a check case needs project.fix_commit") + checks = case.get("checks") + if not isinstance(checks, list) or not checks: + errs.append("checks must be a non-empty list") + else: + for c in checks: + if not isinstance(c, dict) or not c.get("id") or not c.get("cmd"): + errs.append("every check needs an id and a cmd") + break + if case.get("kind") == "task" and not (case.get("request") or "").strip(): + errs.append("a task case needs the request (the human's words)") + return errs + + +def save_case(case, store_dir=None): + """Write a case to benchmarks/.json in the store. Raises ValueError when invalid.""" + errs = validate_case(case) + if errs: + raise ValueError("; ".join(errs)) + d = benchmarks_dir(store_dir) + os.makedirs(d, exist_ok=True) + p = os.path.join(d, case["id"] + ".json") + tmp = p + ".tmp%d" % os.getpid() + with open(tmp, "w", encoding="utf-8") as f: + json.dump(case, f, indent=2, ensure_ascii=False) + f.write("\n") + os.replace(tmp, p) + return p + + +def load_cases(store_dir=None, kind=None, split=None): + """Every stored case, sorted by id, optionally filtered by kind and/or split.""" + d = benchmarks_dir(store_dir) + out = [] + try: + names = sorted(os.listdir(d)) + except OSError: + return out + for n in names: + if not n.endswith(".json"): + continue + try: + with open(os.path.join(d, n), encoding="utf-8") as f: + case = json.load(f) + except (OSError, ValueError): + continue + if not isinstance(case, dict): + continue + if kind and case.get("kind") != kind: + continue + if split and case.get("split") != split: + continue + out.append(case) + return sorted(out, key=lambda c: str(c.get("id"))) + + +def get_case(case_id, store_dir=None): + for case in load_cases(store_dir=store_dir): + if case.get("id") == case_id: + return case + hits = [c for c in load_cases(store_dir=store_dir) if str(c.get("id", "")).startswith(str(case_id))] + return hits[0] if len(hits) == 1 else None + + +def _safe_part(s): + return re.sub(r"[^A-Za-z0-9_.-]", "-", str(s or "")).strip("-.")[:60] or "unit" + + +def capture_case(unit_record, store_dir=None): + """Turn a closed work-unit record into a stored benchmark case, or None when it cannot be one. + + A unit becomes a case only when it has a repository, a start commit and at least one + command check (plus a fix commit when there is no request to replay). The case id and its + dev/holdout split are deterministic, and capturing the same unit twice returns the stored + case unchanged. This is the seam the unit-close path calls. + """ + from . import taskset + if not isinstance(unit_record, dict): + return None + u = unit_record.get("unit") if isinstance(unit_record.get("unit"), dict) else unit_record + uid = u.get("id") or unit_record.get("id") + if not uid: + return None + proj = u.get("project") or {} + repo = proj.get("repo") if isinstance(proj, dict) else proj + repo = repo or u.get("repo") or u.get("cwd") + commits = u.get("commits") or [] + shas = [c.get("sha") if isinstance(c, dict) else c for c in commits] + start = (u.get("start_commit") or u.get("head_before") or u.get("base_commit") + or (proj.get("start_commit") if isinstance(proj, dict) else None)) + fix = (u.get("fix_commit") or u.get("head_after") or u.get("head") + or (proj.get("fix_commit") if isinstance(proj, dict) else None) + or (shas[-1] if shas else None)) + checks = [] + for c in u.get("checks") or []: + if isinstance(c, dict) and c.get("cmd"): + checks.append({"id": c.get("id") or "check", "cmd": c["cmd"], + "expect": c.get("expect") or {"exit": 0}}) + request = u.get("request") or u.get("goal") or u.get("title") or "" + if not repo or not start or not checks: + return None + if not request and not fix: + return None + cid = "case-" + _safe_part(uid) + existing = get_case(cid, store_dir=store_dir) + if existing: + return existing + tags = u.get("tags") or ([u["task_class"]] if u.get("task_class") else []) + if not tags and isinstance(u.get("risk"), dict) and u["risk"].get("profile"): + tags = [u["risk"]["profile"]] + split = u.get("split") if u.get("split") in SPLITS else taskset.split_of(cid) + case = {"id": cid, "kind": "task" if request else "check", + "source": {"unit": str(uid), "learning": str(u.get("learning") or u.get("learning_id") or "")}, + "project": {"repo": str(repo), "start_commit": str(start), "fix_commit": str(fix or "")}, + "request": request, "checks": checks, "tags": [str(t) for t in tags], + "split": split, "added": time.strftime("%Y-%m-%d", time.gmtime())} + if validate_case(case): + return None + save_case(case, store_dir=store_dir) + return case + + +def import_harvested(store_dir=None, con=None): + """Create benchmark cases from the harvested task set (grades A/B with a test command). + Existing case ids are kept. Returns counts.""" + from . import taskset + if con is None: + from . import harvest + con = harvest.connect() + created = kept = 0 + for case in taskset.benchmark_cases(con): + if get_case(case["id"], store_dir=store_dir): + kept += 1 + else: + save_case(case, store_dir=store_dir) + created += 1 + return {"created": created, "kept": kept} + + +def select_cases(cases, split=None, purpose="run", kind=None): + """Cases for a run. Held-out discipline: a tuning or shadow selection must never include + holdout cases; asking for the holdout split under either purpose raises ValueError. + With split=None, tuning/shadow select dev only (callers add counterexamples explicitly) + and a plain run selects dev plus counterexample, so holdout runs only when named.""" + if split is not None and split not in SPLITS + ("all",): + raise ValueError("split must be dev, holdout, counterexample or all") + if split == "holdout" and purpose in ("tuning", "shadow"): + raise ValueError("held-out cases must not be used for %s" % + ("tuning" if purpose == "tuning" else "shadow evaluation")) + if split == "all" and purpose in ("tuning", "shadow"): + raise ValueError("held-out cases must not be used for tuning or shadow evaluation") + out = [] + for case in cases: + if kind and case.get("kind") != kind: + continue + sp = case.get("split") + if purpose in ("tuning", "shadow") and sp == "holdout": + continue + if split and split != "all" and sp != split: + continue + if split is None and sp == "holdout": + continue + if split is None and purpose == "tuning" and sp != "dev": + continue + out.append(case) + return out + + +# ---------------------------------------------------------------- results + +def append_result(row, store_dir=None): + """Append one row to eval-results.jsonl (one write call, so rows stay whole).""" + p = results_path(store_dir) + os.makedirs(os.path.dirname(p), exist_ok=True) + line = json.dumps(row, ensure_ascii=False, separators=(",", ":")) + "\n" + fd = os.open(p, os.O_WRONLY | os.O_APPEND | os.O_CREAT, 0o644) + try: + os.write(fd, line.encode()) + finally: + os.close(fd) + return row + + +def read_raw_results(store_dir=None): + out = [] + try: + with open(results_path(store_dir), encoding="utf-8") as f: + for line in f: + try: + rec = json.loads(line) + except ValueError: + continue + if isinstance(rec, dict): + out.append(rec) + except OSError: + pass + return out + + +def read_results(store_dir=None): + """Run rows in order, with any later independent judgement merged on (judged_by, judge_items).""" + runs, order = {}, [] + for rec in read_raw_results(store_dir): + if rec.get("kind") == "run": + runs[rec.get("id")] = rec + order.append(rec.get("id")) + elif rec.get("kind") == "judgement" and rec.get("result_id") in runs: + runs[rec["result_id"]]["judged_by"] = rec.get("judged_by") + runs[rec["result_id"]]["judge_items"] = rec.get("items") or [] + return [runs[i] for i in order if i in runs] + + +# ---------------------------------------------------------------- isolated replay + +def _git(cwd, *args, timeout=60): + try: + r = subprocess.run(["git"] + list(args), cwd=cwd, capture_output=True, text=True, timeout=timeout) + return r.returncode, r.stdout or "", r.stderr or "" + except (OSError, subprocess.TimeoutExpired) as e: + return 1, "", str(e) + + +def _is_remote(repo): + return "://" in str(repo) or str(repo).startswith("git@") + + +def _prepare_repo(case): + """(repo_path, cleanup, reason). Local paths are used read-only as worktree sources; + a remote is cloned into a temp dir. ``reason`` explains a not-run outcome.""" + repo = str((case.get("project") or {}).get("repo") or "") + if not repo: + return None, (lambda: None), "repository not recorded on the case" + if _is_remote(repo): + tmp = tempfile.mkdtemp(prefix="sisi-eval-clone-") + dest = os.path.join(tmp, "repo") + r = subprocess.run(["git", "clone", "--quiet", repo, dest], capture_output=True, text=True, timeout=300) + if r.returncode != 0: + shutil.rmtree(tmp, ignore_errors=True) + return None, (lambda: None), "repository could not be cloned: %s" % repo + return dest, (lambda: shutil.rmtree(tmp, ignore_errors=True)), None + if not os.path.isdir(repo): + return None, (lambda: None), "repository gone: %s" % repo + rc, top, _err = _git(repo, "rev-parse", "--show-toplevel") + if rc != 0: + return None, (lambda: None), "repository gone or not a git repository: %s" % repo + rc, common, _err = _git(top.strip(), "rev-parse", "--path-format=absolute", "--git-common-dir") + root = top.strip() + if rc == 0 and common.strip().endswith("/.git"): + root = common.strip()[:-5] or root + return root, (lambda: None), None + + +def _commit_exists(repo, sha): + if not sha: + return False + rc, _o, _e = _git(repo, "cat-file", "-e", str(sha) + "^{commit}") + return rc == 0 + + +class _Worktree: + """A detached worktree at one commit inside a temp dir; removed on exit, even on error.""" + + def __init__(self, repo, commit): + self.repo, self.commit = repo, str(commit) + self.tmp = tempfile.mkdtemp(prefix="sisi-eval-") + self.path = os.path.join(self.tmp, "wt") + self.added = False + + def __enter__(self): + rc, _o, err = _git(self.repo, "worktree", "add", "--detach", "--force", self.path, self.commit, + timeout=120) + if rc == 0: + self.added = True + return self.path + # Fall back to a shared clone checkout when worktrees are unavailable. + shutil.rmtree(self.path, ignore_errors=True) + rc2, _o, err2 = _git(self.repo, "clone", "--shared", "--quiet", "--no-checkout", + self.repo, self.path, timeout=120) + if rc2 == 0: + rc3, _o, err3 = _git(self.path, "checkout", "--quiet", "--detach", self.commit, timeout=120) + if rc3 == 0: + return self.path + err = err3 + else: + err = err2 or err + raise RuntimeError("cannot check out %s in a temporary copy: %s" % (self.commit, (err or "").strip()[-300:])) + + def __exit__(self, *exc): + try: + if self.added: + _git(self.repo, "worktree", "remove", "--force", self.path, timeout=60) + _git(self.repo, "worktree", "prune", timeout=60) + finally: + shutil.rmtree(self.tmp, ignore_errors=True) + return False + + +def _run_checks(case, cwd, timeout_s): + """Run the case's checks in ``cwd``. Returns (rows, timed_out); rows carry exit/passed.""" + rows, timed_out = [], False + for c in case.get("checks") or []: + expect = (c.get("expect") or {}) + want = expect.get("exit", 0) + try: + want = int(want) + except (TypeError, ValueError): + want = 0 + try: + r = subprocess.run(str(c.get("cmd")), shell=True, cwd=cwd, capture_output=True, + text=True, timeout=timeout_s) + rows.append({"id": c.get("id"), "cmd": c.get("cmd"), "exit": r.returncode, + "expected_exit": want, "passed": r.returncode == want}) + except subprocess.TimeoutExpired: + timed_out = True + rows.append({"id": c.get("id"), "cmd": c.get("cmd"), "exit": None, + "expected_exit": want, "passed": False, "timed_out": True}) + except OSError as e: + rows.append({"id": c.get("id"), "cmd": c.get("cmd"), "exit": None, + "expected_exit": want, "passed": False, "error": str(e)}) + return rows, timed_out + + +def _checks_at(case, repo, commit, timeout_s): + """(all_passed, rows, timed_out) for the case's checks at one commit, in a temp worktree.""" + with _Worktree(repo, commit) as wt: + rows, timed_out = _run_checks(case, wt, timeout_s) + return (bool(rows) and all(r["passed"] for r in rows)), rows, timed_out + + +def _replay_check(case, timeout_s): + """Outcome for a check (or counterexample) case, without recording anything.""" + proj = case.get("project") or {} + start, fix = proj.get("start_commit"), proj.get("fix_commit") + repo, cleanup, reason = _prepare_repo(case) + if repo is None: + return {"outcome": "not-run", "reason": reason, "check_results": []} + try: + for label, sha in (("start_commit", start), ("fix_commit", fix)): + if case.get("split") == "counterexample" and not sha: + continue + if not sha: + return {"outcome": "not-run", "reason": "commit not recorded: %s" % label, "check_results": []} + if not _commit_exists(repo, sha): + return {"outcome": "not-run", "reason": "commit missing: %s (%s)" % (label, sha), + "check_results": []} + results, seen = [], {} + try: + for label, sha in (("start", start), ("fix", fix)): + if not sha: + continue + passed, rows, timed_out = _checks_at(case, repo, sha, timeout_s) + if timed_out: + return {"outcome": "not-run", "reason": "check timed out at the %s commit" % label, + "check_results": results + [dict(r, at=label) for r in rows]} + seen[label] = passed + results += [dict(r, at=label) for r in rows] + except RuntimeError as e: + return {"outcome": "not-run", "reason": str(e), "check_results": results} + if case.get("split") == "counterexample": + ok = bool(seen) and all(seen.values()) + return {"outcome": "validated" if ok else "failed", + "reason": "" if ok else "counterexample: the check did not still pass", + "check_results": results, "at_start": seen.get("start"), "at_fix": seen.get("fix")} + if seen.get("start"): + return {"outcome": "failed-to-fail", "reason": "check already passes at the start commit", + "check_results": results, "at_start": True, "at_fix": seen.get("fix")} + if not seen.get("fix"): + return {"outcome": "failed-to-pass", "reason": "check still fails at the fix commit", + "check_results": results, "at_start": False, "at_fix": False} + return {"outcome": "validated", "reason": "", "check_results": results, + "at_start": False, "at_fix": True} + finally: + cleanup() + + +def validate_check(case, store_dir=None, timeout_s=None, record=False): + """Replay a (proposed) command check against the case it came from: it is ``validated`` + only when seen to fail at start_commit and to pass at fix_commit (for a counterexample, + when it still passes). Accepts a case dict or a stored case id. The learning cycle calls + this before it promotes a command check. Nothing is recorded unless record=True.""" + resolved = case if isinstance(case, dict) else get_case(case, store_dir=store_dir) + if resolved is None: + return {"outcome": "not-run", "reason": "no such case: %s" % case, "check_results": []} + timeout_s = float(timeout_s or _eval_config(store_dir)["timeout_s"]) + out = _replay_check(resolved, timeout_s) + out = dict({"case": resolved.get("id")}, **out) + if record: + row = _result_row(resolved, out["outcome"], out.get("reason", ""), store_dir, + check_results=out.get("check_results") or []) + append_result(row, store_dir=store_dir) + out["result_id"] = row["id"] + return out + + +def _rulebook_text(policy_dir): + for p in (os.path.join(policy_dir, "generated", "core.md"), paths.canon("core.md")): + try: + with open(p, encoding="utf-8") as f: + return f.read() + except OSError: + continue + return "" + + +def _brief_text(case, policy_dir): + """The worker brief for a task replay: the policy under test, then a full delegation + contract, so the same text serves an injected runner, a --worker-cmd and `sisi delegate`.""" + checks = "; ".join("%s: %s" % (c.get("id"), c.get("cmd")) for c in case.get("checks") or []) + return "\n".join([ + "You are a worker. Do this task yourself; do not delegate it further.", "", + "## Outcome and scope", case.get("request") or "", "", + "## Context and sources of truth", + "You work in a temporary copy checked out at the case's start commit. " + "The rules of the policy under test follow:", "", _rulebook_text(policy_dir), "", + "## Constraints and interfaces", "Change only what the request needs. Commit nothing.", "", + "## Acceptance", "These checks must pass on your tree: " + checks, "", + "## Deliverables and report format", "End with a short report: what changed and the checks you ran.", "", + "## Files you may change", "Any file in this temporary copy except .sisi-eval-brief.md.", "", + "## Verification", "Run the acceptance checks above before reporting.", "", + "## Budget and escalation", "One task, one attempt; stop and say so if blocked.", ""]) + + +def _worker_via_cmd(cmd, brief_text, case, cwd, policy_dir, timeout_s): + brief_path = os.path.join(cwd, ".sisi-eval-brief.md") + with open(brief_path, "w", encoding="utf-8") as f: + f.write(brief_text) + env = dict(os.environ, SISI_DATA=policy_dir, SISI_EVAL_BRIEF=brief_path, + SISI_EVAL_REQUEST=case.get("request") or "", SISI_EVAL_POLICY_DIR=policy_dir, + SISI_EVAL_CASE=case.get("id") or "", SISI_WORKER_RUN="1") + r = subprocess.run(cmd, shell=True, cwd=cwd, capture_output=True, text=True, + timeout=timeout_s, env=env) + try: + os.unlink(brief_path) + except OSError: + pass + return {"worker": None, "text": (r.stdout or r.stderr or "").strip(), "exit": r.returncode} + + +def _worker_via_delegate(brief_text, case, cwd, policy_dir, worker=None, review_of=None, kind=None): + """Default task runner: the existing delegate runner, with the policy under test as the + store its records land in (so a candidate policy never writes to the real store).""" + from . import workers as W + candidates = W.build_candidates() + explicit = W.resolve_worker(worker, candidates) if worker else None + if worker and explicit is None: + return None, "no such worker found here: %s" % worker + if not explicit: + candidates, _cold, _excluded = W.routable_candidates(candidates, W.read_records()) + kind = kind or ((case.get("tags") or ["impl"])[0]) + try: + difficulty = int(case.get("difficulty") or 2) + except (TypeError, ValueError): + difficulty = 2 + old = os.environ.get("SISI_DATA") + os.environ["SISI_DATA"] = policy_dir + try: + rec, final, notes = W.run_delegation(candidates, kind, difficulty, brief_text, cwd, + explicit=explicit, review_of=review_of, brief_path=None) + finally: + if old is None: + os.environ.pop("SISI_DATA", None) + else: + os.environ["SISI_DATA"] = old + if rec is None: + return None, "; ".join(notes) or "no usable worker" + return {"worker": rec.get("worker"), "text": final or "", "exit": rec.get("exit"), + "tokens": rec.get("tokens"), "tokens_class": rec.get("tokens_class"), + "cost": rec.get("cost"), "cost_class": rec.get("cost_class")}, None + + +def _num(v): + return float(v) if isinstance(v, (int, float)) and not isinstance(v, bool) else None + + +def _result_row(case, outcome, reason, store_dir, policy_label=None, policy_dir=None, + worker=None, check_results=(), minutes=None, minutes_class=None, tokens=None, + tokens_class=None, cost=None, cost_class=None, diff=""): + policy_dir = policy_dir or _store(store_dir) + ph = policy_version(policy_dir) + return {"id": "r-" + uuid.uuid4().hex[:10], "kind": "run", "ts": store.now(), + "case": case.get("id"), "case_kind": case.get("kind"), "split": case.get("split"), + "tags": list(case.get("tags") or []), + "policy": policy_label or ph, "policy_hash": ph, + "worker": worker, "outcome": outcome, "reason": reason or "", + "minutes": minutes, "minutes_class": minutes_class or ("measured" if minutes is not None else "unavailable"), + "tokens": tokens, "tokens_class": tokens_class or ("measured" if tokens is not None else "unavailable"), + "cost": cost, "cost_class": cost_class or ("measured" if cost is not None else "unavailable"), + "check_results": list(check_results), "diff": (diff or "")[:DIFF_LIMIT]} + + +def _replay_task(case, policy_dir, timeout_s, worker_timeout_s, worker_runner, worker_cmd, worker): + proj = case.get("project") or {} + start = proj.get("start_commit") + repo, cleanup, reason = _prepare_repo(case) + if repo is None: + return {"outcome": "not-run", "reason": reason} + try: + if not start or not _commit_exists(repo, start): + return {"outcome": "not-run", + "reason": "commit missing: start_commit (%s)" % (start or "not recorded")} + if not case.get("checks"): + return {"outcome": "not-run", "reason": "case has no checks"} + brief = _brief_text(case, policy_dir) + t0 = time.time() + try: + with _Worktree(repo, start) as wt: + err = None + if worker_runner is not None: + try: + # Injected runners take (brief_text, cwd, policy_dir=..., case=...). + got = worker_runner(brief, wt, policy_dir=policy_dir, case=case) + except Exception as e: + return {"outcome": "not-run", "reason": "worker failed to run: %s" % e} + if got is None: + return {"outcome": "not-run", "reason": "no worker available for this task"} + elif worker_cmd or os.environ.get("SISI_EVAL_WORKER_CMD"): + try: + got = _worker_via_cmd(worker_cmd or os.environ["SISI_EVAL_WORKER_CMD"], + brief, case, wt, policy_dir, worker_timeout_s) + except subprocess.TimeoutExpired: + return {"outcome": "not-run", "reason": "worker timed out"} + got = dict(got, worker=worker or "worker-cmd") + else: + got, err = _worker_via_delegate(brief, case, wt, policy_dir, worker=worker) + if got is None: + return {"outcome": "not-run", "reason": err or "no worker available for this task"} + got = dict(got) if isinstance(got, dict) else {"text": str(got)} + minutes = _num(got.get("minutes")) + if minutes is None: + minutes = round((time.time() - t0) / 60.0, 4) + _rc, diff, _e = _git(wt, "diff", "--no-ext-diff", "--no-color", str(start), timeout=60) + rows, timed_out = _run_checks(case, wt, timeout_s) + if timed_out: + outcome, why = "not-run", "check timed out on the worker's tree" + elif got.get("exit") not in (None, 0) and not all(r["passed"] for r in rows): + outcome, why = "failed", "worker exited %s and its checks do not pass" % got.get("exit") + elif all(r["passed"] for r in rows): + outcome, why = "passed", "" + else: + outcome, why = "failed", "checks do not pass on the worker's tree" + return {"outcome": outcome, "reason": why, "check_results": rows, "diff": diff, + "worker": got.get("worker") or worker, "minutes": minutes, + "minutes_class": got.get("minutes_class"), + "tokens": got.get("tokens"), "tokens_class": got.get("tokens_class"), + "cost": _num(got.get("cost")), "cost_class": got.get("cost_class"), + "worker_text": str(got.get("text") or "")[:2000]} + except RuntimeError as e: + return {"outcome": "not-run", "reason": str(e)} + finally: + cleanup() + + +def run_case(case, store_dir=None, policy_dir=None, policy_label=None, worker_runner=None, + worker_cmd=None, worker=None, timeout_s=None, worker_timeout_s=None, record=True): + """Replay one case (dict or stored id) under one policy and record the result row. + ``policy_dir`` is the store whose policy is under test (default: the result store).""" + resolved = case if isinstance(case, dict) else get_case(case, store_dir=store_dir) + if resolved is None: + row = {"id": "r-" + uuid.uuid4().hex[:10], "kind": "run", "ts": store.now(), "case": str(case), + "outcome": "not-run", "reason": "no such case"} + return append_result(row, store_dir=store_dir) if record else row + cfg = _eval_config(store_dir) + timeout_s = float(timeout_s or cfg["timeout_s"]) + worker_timeout_s = float(worker_timeout_s or cfg["worker_timeout_s"]) + policy_dir = policy_dir or _store(store_dir) + t0 = time.time() + if resolved.get("kind") == "task": + out = _replay_task(resolved, policy_dir, timeout_s, worker_timeout_s, worker_runner, worker_cmd, worker) + row = _result_row(resolved, out["outcome"], out.get("reason", ""), store_dir, + policy_label=policy_label, policy_dir=policy_dir, + worker=out.get("worker"), check_results=out.get("check_results") or (), + minutes=out.get("minutes"), minutes_class=out.get("minutes_class"), + tokens=out.get("tokens"), tokens_class=out.get("tokens_class"), + cost=out.get("cost"), cost_class=out.get("cost_class"), diff=out.get("diff", "")) + if out.get("worker_text"): + row["worker_text"] = out["worker_text"] + else: + out = _replay_check(resolved, timeout_s) + row = _result_row(resolved, out["outcome"], out.get("reason", ""), store_dir, + policy_label=policy_label, policy_dir=policy_dir, + check_results=out.get("check_results") or (), + minutes=round((time.time() - t0) / 60.0, 4)) + return append_result(row, store_dir=store_dir) if record else row + + +def run_cases(cases, tasks_limit=0, cost_cap=None, **kw): + """Replay cases in order: at most ``tasks_limit`` task cases, and stop starting task cases + once the known cost so far reaches ``cost_cap`` (the rest are recorded not-run).""" + results, tasks_done, spent = [], 0, 0.0 + for case in cases: + if case.get("kind") == "task": + if tasks_done >= (tasks_limit or 0): + continue + if cost_cap is not None and spent >= float(cost_cap): + row = _result_row(case, "not-run", "cost cap for this invocation reached", + kw.get("store_dir"), policy_label=kw.get("policy_label"), + policy_dir=kw.get("policy_dir")) + results.append(append_result(row, store_dir=kw.get("store_dir"))) + continue + tasks_done += 1 + row = run_case(case, **kw) + results.append(row) + if _num(row.get("cost")) is not None: + spent += float(row["cost"]) + return results + + +def estimate_task_cost(n_tasks, store_dir=None): + """Estimated cost of ``n_tasks`` task replays from recorded task-run facts (the median + known task cost), or unknown when nothing has been recorded. Unavailable is never zero.""" + costs = [float(r["cost"]) for r in read_results(store_dir=store_dir) + if r.get("case_kind") == "task" and _num(r.get("cost")) is not None] + if not costs or not n_tasks: + return {"value": None, "class": "unavailable", "based_on": len(costs), + "text": "unknown"} + value = statistics.median(costs) * int(n_tasks) + return {"value": value, "class": "estimated", "based_on": len(costs), + "text": "%g (estimated from %d earlier task run(s))" % (value, len(costs))} + + +# ---------------------------------------------------------------- comparison + +def _p95(values): + if not values: + return None + s = sorted(values) + return s[max(0, math.ceil(0.95 * len(s)) - 1)] + + +def _metrics(rows, ids): + picked = [rows[i] for i in ids if i in rows] + passed = sum(1 for r in picked if r.get("outcome") in PASS_OUTCOMES) + failed = sum(1 for r in picked if r.get("outcome") in FAIL_OUTCOMES) + not_run = sum(1 for r in picked if r.get("outcome") == "not-run") + minutes = [float(r["minutes"]) for r in picked if _num(r.get("minutes")) is not None] + costs = [float(r["cost"]) for r in picked if _num(r.get("cost")) is not None] + return {"n": len(picked), "passed": passed, "failed": failed, "not_run": not_run, + "pass_rate": (passed / float(passed + failed)) if (passed + failed) else None, + "minutes_median": statistics.median(minutes) if minutes else None, + "minutes_p95": _p95(minutes), + "cost_median": statistics.median(costs) if costs else None, + "cost_total": sum(costs) if costs else None, + "cost_unknown": len(picked) - len(costs)} + + +def compare_versions(a, b, store_dir=None, split=None, case_ids=None): + """Compare policy ``a`` (baseline) with ``b`` (candidate) over the cases both ran. + Cases only one side ran are reported, unavailable cost is never zero, and no single + score is produced. The verdict says ``not an improvement`` in words when the candidate + fails cases the baseline passed (SPEC 13.5), and refuses improvement claims on thin data.""" + cases = {c.get("id"): c for c in load_cases(store_dir=store_dir)} + by_policy = {a: {}, b: {}} + for r in read_results(store_dir=store_dir): + pol = r.get("policy") + if pol not in by_policy: + continue + cid = r.get("case") + if split: + cs = (cases.get(cid) or {}).get("split", r.get("split")) + if cs != split: + continue + if case_ids is not None and cid not in case_ids: + continue + by_policy[pol][cid] = r # later rows are newer: the latest run per case wins + ids_a, ids_b = set(by_policy[a]), set(by_policy[b]) + common = sorted(ids_a & ids_b) + metrics = {a: _metrics(by_policy[a], common), b: _metrics(by_policy[b], common)} + regressions = [c for c in common if by_policy[a][c].get("outcome") in PASS_OUTCOMES + and by_policy[b][c].get("outcome") in FAIL_OUTCOMES] + gains = [c for c in common if by_policy[a][c].get("outcome") in FAIL_OUTCOMES + and by_policy[b][c].get("outcome") in PASS_OUTCOMES] + too_few = len(common) < 5 + ma, mb = metrics[a], metrics[b] + faster = (ma["minutes_median"] is not None and mb["minutes_median"] is not None + and mb["minutes_median"] < ma["minutes_median"]) + cheaper = (ma["cost_median"] is not None and mb["cost_median"] is not None + and mb["cost_median"] < ma["cost_median"]) + if regressions: + how = " (it is %s, but) " % (" and ".join(x for x, y in + (("faster", faster), ("cheaper", cheaper)) if y) or "no better on time or cost") + verdict = ("not an improvement: %s%sfails %d case(s) the baseline passed (%s)" + % (b, how if (faster or cheaper) else " ", + len(regressions), ", ".join(regressions[:8]))) + if too_few: + verdict += "; too few common cases to say more" + elif too_few: + verdict = "too few common cases (%d, need at least 5): no improvement can be claimed" % len(common) + elif gains or faster or cheaper: + bits = [] + if gains: + bits.append("passes %d case(s) the baseline failed" % len(gains)) + if faster: + bits.append("is faster (median minutes)") + if cheaper: + bits.append("is cheaper (median cost)") + verdict = "improvement on these cases: %s %s, with no case the baseline passed now failing" % (b, "; ".join(bits)) + else: + verdict = "no improvement shown on these cases" + by_class = {} + for cid in common: + tags = (cases.get(cid) or {}).get("tags") or by_policy[a][cid].get("tags") or ["untagged"] + for t in tags: + by_class.setdefault(str(t), []).append(cid) + classes = {} + for t, ids in sorted(by_class.items()): + classes[t] = {"n": len(ids), "too_few": len(ids) < 5, + a: _metrics(by_policy[a], ids), b: _metrics(by_policy[b], ids)} + return {"a": a, "b": b, "split": split, "common": common, + "only_a": sorted(ids_a - ids_b), "only_b": sorted(ids_b - ids_a), + "metrics": metrics, "by_class": classes, "regressions": regressions, + "gains": gains, "too_few": too_few, "faster": faster, "cheaper": cheaper, + "verdict": verdict} + + +def _fmt(v, suffix=""): + return "unknown" if v is None else ("%g%s" % (v, suffix)) + + +def _rate(m): + return "unknown" if m["pass_rate"] is None else "%.0f%% (%d/%d)" % ( + 100 * m["pass_rate"], m["passed"], m["passed"] + m["failed"]) + + +def format_comparison(comp): + """The comparison as short text: each speed/cost figure sits beside its counter-metric + (pass rate), per task class, with missing data named. There is no single score.""" + a, b = comp["a"], comp["b"] + ma, mb = comp["metrics"][a], comp["metrics"][b] + lines = ["compare %s (baseline) vs %s (candidate)" % (a, b), + "common cases: %d%s; only %s ran: %d%s; only %s ran: %d%s" % ( + len(comp["common"]), " (too few)" if comp["too_few"] else "", + a, len(comp["only_a"]), (" (%s)" % ", ".join(comp["only_a"][:8])) if comp["only_a"] else "", + b, len(comp["only_b"]), (" (%s)" % ", ".join(comp["only_b"][:8])) if comp["only_b"] else ""), + "%-18s %-16s %-16s" % ("metric", a, b), + "%-18s %-16s %-16s" % ("pass rate", _rate(ma), _rate(mb)), + "%-18s %-16s %-16s" % ("not-run", ma["not_run"], mb["not_run"]), + "%-18s %-16s %-16s" % ("median minutes", _fmt(ma["minutes_median"]), _fmt(mb["minutes_median"])), + "%-18s %-16s %-16s" % ("p95 minutes", _fmt(ma["minutes_p95"]), _fmt(mb["minutes_p95"])), + "%-18s %-16s %-16s" % ("median cost", _fmt(ma["cost_median"]), _fmt(mb["cost_median"])), + "%-18s %-16s %-16s" % ("cost unknown (n)", ma["cost_unknown"], mb["cost_unknown"])] + for t, c in list(comp["by_class"].items())[:20]: + lines.append("class %-14s n=%d%s pass %s -> %s, median min %s -> %s" % ( + t, c["n"], " too few;" if c["too_few"] else ";", + _rate(c[a]), _rate(c[b]), _fmt(c[a]["minutes_median"]), _fmt(c[b]["minutes_median"]))) + if comp["regressions"]: + lines.append("now failing, passed before: %s" % ", ".join(comp["regressions"][:10])) + if comp["gains"]: + lines.append("now passing, failed before: %s" % ", ".join(comp["gains"][:10])) + lines.append("verdict: " + comp["verdict"]) + return "\n".join(lines) + + +def compare_for_candidate(candidate_store_dir, sample=None, store_dir=None, worker_runner=None, + worker_cmd=None, worker=None, timeout_s=None, tasks_limit=0): + """Shadow evaluation: run a sample of dev cases plus every counterexample case under the + current policy and under the candidate policy in ``candidate_store_dir`` (a copy of the + user store with the change applied), then return their comparison. Holdout cases are + never run: any named in ``sample`` are returned under ``excluded_holdout``. Results are + recorded in the real store under each policy's version; the candidate store is not written + to except by a worker run under its policy. This is the seam the approval path calls.""" + store_dir = _store(store_dir) + all_cases = load_cases(store_dir=store_dir) + by_id = {c["id"]: c for c in all_cases} + excluded = [] + if isinstance(sample, (list, tuple)): + chosen = [] + for item in sample: + case = item if isinstance(item, dict) else by_id.get(item) + if case is None: + continue + if case.get("split") == "holdout": + excluded.append(case["id"]) + elif case.get("split") == "dev": + chosen.append(case) + elif isinstance(sample, int): + chosen = select_cases(all_cases, split="dev", purpose="shadow")[:sample] + else: + chosen = select_cases(all_cases, split="dev", purpose="shadow") + seen = {c["id"] for c in chosen} + chosen += [c for c in all_cases if c.get("split") == "counterexample" and c["id"] not in seen] + picked, tasks_done = [], 0 + for case in chosen: + if case.get("kind") == "task": + if tasks_done >= (tasks_limit or 0): + continue + tasks_done += 1 + picked.append(case) + current = policy_version(store_dir) + candidate = policy_version(candidate_store_dir) + kw = dict(store_dir=store_dir, worker_runner=worker_runner, worker_cmd=worker_cmd, + worker=worker, timeout_s=timeout_s) + for case in picked: + run_case(case, policy_dir=store_dir, policy_label=current, **kw) + for case in picked: + run_case(case, policy_dir=candidate_store_dir, policy_label=candidate, **kw) + comp = compare_versions(current, candidate, store_dir=store_dir, + case_ids={c["id"] for c in picked}) + comp["excluded_holdout"] = excluded + comp["candidate_dir"] = candidate_store_dir + comp["sample"] = [c["id"] for c in picked] + return comp + + +# ---------------------------------------------------------------- independent judging + +def _judge_prompt(case, result): + items = "\n".join("- %s: %s" % (c.get("id"), c.get("cmd")) for c in (case or {}).get("checks") or []) + checks = "\n".join("- %s: exit %s (%s)" % (r.get("id"), r.get("exit"), + "passed" if r.get("passed") else "did not pass") + for r in result.get("check_results") or []) + return "\n".join([ + "You are an independent judge. You did not do this work.", "", + "The human's request:", (case or {}).get("request") or "(not recorded)", "", + "Acceptance items (the case's checks):", items or "(none recorded)", "", + "Check results on the worker's tree:", checks or "(none recorded)", "", + "The worker's diff against the start commit:", result.get("diff") or "(no diff recorded)", "", + "Answer as JSON only, one item per acceptance item:", + '{"items": [{"id": "", "verdict": "met" or "not met", "reason": ""}]}', ""]) + + +def _parse_judge_items(got, case): + """(worker, items) from a judge result: a dict, a JSON text, or 'id: met - reason' lines.""" + worker, raw = None, None + if isinstance(got, dict): + worker = got.get("worker") or got.get("judged_by") + raw = got.get("items") + if raw is None and got.get("text"): + return worker, _parse_judge_items(str(got["text"]), case)[1] + elif isinstance(got, str): + text = got.strip() + m = re.search(r"\{.*\}", text, re.S) + if m: + try: + return _parse_judge_items(json.loads(m.group(0)), case) + except ValueError: + pass + raw = [] + for line in text.splitlines(): + mm = re.match(r"\s*[-*]?\s*([A-Za-z0-9_.-]+)\s*[:=-]\s*(not met|met)\b\s*[-: ]*\s*(.*)", line, re.I) + if mm: + raw.append({"id": mm.group(1), "verdict": mm.group(2).lower(), "reason": mm.group(3).strip()}) + items = [] + for it in raw or []: + if not isinstance(it, dict): + continue + verdict = str(it.get("verdict") or "").strip().lower() + if verdict in ("met", "not met") and it.get("id"): + items.append({"id": str(it["id"]), "verdict": verdict, + "reason": str(it.get("reason") or "").split("\n")[0][:300]}) + return worker, items + + +def judge_result(result_id, store_dir=None, judge_runner=None, judge_cmd=None, judge_worker=None, + timeout_s=None): + """Independent judging of one recorded run: a worker from a different maker than the one + that did the run reads the request, the diff and the check results and answers met / not + met per acceptance item. The judgement is appended and merged as ``judged_by``. A judge + from the same maker is refused, so self-assessment is never recorded as independent.""" + from . import routing + runs = read_results(store_dir=store_dir) + result = next((r for r in runs if r.get("id") == result_id), None) + if result is None: + hits = [r for r in runs if str(r.get("id", "")).startswith(str(result_id))] + result = hits[0] if len(hits) == 1 else None + if result is None: + return {"outcome": "not-run", "reason": "no single result matches %s" % result_id} + case = get_case(result.get("case"), store_dir=store_dir) or {} + author = result.get("worker") + prompt = _judge_prompt(case, result) + got, judged_by = None, judge_worker + if judge_runner is not None: + try: + got = judge_runner(prompt, case=case, result=result) + except TypeError: + got = judge_runner(prompt) + except Exception as e: + return {"outcome": "not-run", "reason": "judge failed to run: %s" % e, "result_id": result["id"]} + elif judge_cmd or os.environ.get("SISI_EVAL_JUDGE_CMD"): + judged_by = judged_by or os.environ.get("SISI_EVAL_JUDGE_WORKER") or "judge-cmd" + try: + r = subprocess.run(judge_cmd or os.environ["SISI_EVAL_JUDGE_CMD"], shell=True, + input=prompt, capture_output=True, text=True, + timeout=float(timeout_s or _eval_config(store_dir)["worker_timeout_s"])) + got = r.stdout or "" + except (OSError, subprocess.TimeoutExpired): + return {"outcome": "not-run", "reason": "judge timed out or could not start", + "result_id": result["id"]} + else: + try: + got, err = _worker_via_delegate(prompt, case or {"tags": ["review"]}, os.getcwd(), + _store(store_dir), worker=judge_worker, + review_of=author, kind="review") + except Exception as e: + return {"outcome": "not-run", "reason": "judge failed to run: %s" % e, "result_id": result["id"]} + if got is None: + return {"outcome": "not-run", "reason": err or "no judge available", "result_id": result["id"]} + worker_from, items = _parse_judge_items(got, case) + judged_by = judged_by or worker_from + if not judged_by: + return {"outcome": "not-run", "reason": "the judge did not say who it is", "result_id": result["id"]} + if author and routing.maker_of(judged_by) and routing.maker_of(judged_by) == routing.maker_of(author): + return {"outcome": "not-run", "result_id": result["id"], + "reason": "judge %s is from the same maker as the worker (%s); not independent" + % (judged_by, routing.maker_of(author))} + if not items: + return {"outcome": "not-run", "reason": "judge output named no met/not-met items", + "result_id": result["id"]} + row = {"id": "j-" + uuid.uuid4().hex[:10], "kind": "judgement", "ts": store.now(), + "result_id": result["id"], "case": result.get("case"), "judged_by": str(judged_by), + "items": items} + append_result(row, store_dir=store_dir) + return dict({"outcome": "judged"}, **row) + + +# ---------------------------------------------------------------- status + +def status_summary(store_dir=None): + """Counts by kind and split, the last run per split, and the latest comparison.""" + cases = load_cases(store_dir=store_dir) + counts = {} + for c in cases: + counts.setdefault(c.get("kind"), {})[c.get("split")] = \ + counts.setdefault(c.get("kind"), {}).get(c.get("split"), 0) + 1 + results = read_results(store_dir=store_dir) + last, policies = {}, [] + for r in results: + if r.get("split"): + last[r["split"]] = r.get("ts") or last.get(r["split"]) + if r.get("policy") and r["policy"] not in policies: + policies.append(r["policy"]) + latest = None + if len(policies) >= 2: + latest = compare_versions(policies[-2], policies[-1], store_dir=store_dir) + return {"cases": len(cases), "counts": counts, "last_run": last, + "runs": len(results), "policies": policies, "latest_comparison": latest} + + +def format_status(st): + lines = ["benchmark cases: %d" % st["cases"]] + for kind in KINDS: + parts = ", ".join("%s %d" % (sp, st["counts"].get(kind, {}).get(sp, 0)) for sp in SPLITS) + lines.append(" %-6s %s" % (kind, parts)) + for sp in SPLITS: + lines.append("last %s run: %s" % (sp, st["last_run"].get(sp) or "never")) + lines.append("recorded runs: %d under %d policy version(s)" % (st["runs"], len(st["policies"]))) + if st["latest_comparison"]: + lines.append("latest comparison (%s vs %s): %s" % ( + st["latest_comparison"]["a"], st["latest_comparison"]["b"], + st["latest_comparison"]["verdict"])) + return "\n".join(lines) + + +# ---------------------------------------------------------------- command line + +USAGE = """usage: + sisi eval list [--kind check|task] [--split dev|holdout|counterexample] [--json] + sisi eval add FILE.json | add --id ID --kind check|task --repo P --start C [--fix C] [--request TEXT] --check CMD [--tag T] [--split S] + sisi eval run [--tasks N] [--split S] [--case ID] [--policy LABEL] [--policy-dir DIR] [--worker W] [--worker-cmd CMD] [--timeout S] [--json] + sisi eval compare A B [--split S] [--json] + sisi eval shadow DIR [--sample N] [--tasks N] [--worker W] [--worker-cmd CMD] [--json] + sisi eval judge RESULT_ID [--judge-worker W] [--judge-cmd CMD] [--json] + sisi eval status [--json] + sisi eval import create cases from the harvested task set""" + + +def _opt(args, name, default=None): + if name in args: + i = args.index(name) + return args[i + 1] if i + 1 < len(args) else default + for a in args: + if a.startswith(name + "="): + return a.split("=", 1)[1] + return default + + +def _opts(args, name): + out, i = [], 0 + while i < len(args): + if args[i] == name and i + 1 < len(args): + out.append(args[i + 1]) + i += 1 + elif args[i].startswith(name + "="): + out.append(args[i].split("=", 1)[1]) + i += 1 + return out + + +def _int_opt(args, name, default=None): + v = _opt(args, name) + if v is None: + return default + try: + return int(v) + except ValueError: + return default + + +def _show(obj, as_json, text=None): + print(json.dumps(obj) if as_json else text) + + +def _cmd_add(args, store_dir): + plain = [a for a in args if not a.startswith("--")] + if plain and os.path.isfile(plain[0]): + try: + data = json.load(open(plain[0], encoding="utf-8")) + except (OSError, ValueError) as e: + print("sisi eval add: cannot read %s: %s" % (plain[0], e), file=sys.stderr) + return 1 + cases = data if isinstance(data, list) else [data] + else: + checks = [{"id": "c%d" % (i + 1), "cmd": c, "expect": {"exit": 0}} + for i, c in enumerate(_opts(args, "--check"))] + cases = [{"id": _opt(args, "--id"), "kind": _opt(args, "--kind", "check"), + "source": {"unit": _opt(args, "--unit", "by-hand"), "learning": _opt(args, "--learning", "")}, + "project": {"repo": _opt(args, "--repo"), "start_commit": _opt(args, "--start"), + "fix_commit": _opt(args, "--fix", "")}, + "request": _opt(args, "--request", ""), "checks": checks, + "tags": _opts(args, "--tag"), "split": _opt(args, "--split", "dev"), + "added": time.strftime("%Y-%m-%d", time.gmtime())}] + n = 0 + for case in cases: + errs = validate_case(case) + if errs: + print("sisi eval add: %s" % "; ".join(errs), file=sys.stderr) + return 1 + save_case(case, store_dir=store_dir) + print("case %s (%s, %s)" % (case["id"], case["kind"], case["split"])) + n += 1 + return 0 if n else 1 + + +def _cmd_list(args, store_dir): + cases = load_cases(store_dir=store_dir, kind=_opt(args, "--kind"), split=_opt(args, "--split")) + if "--json" in args: + print(json.dumps(cases)) + return 0 + print("%-24s %-6s %-14s tags" % ("case", "kind", "split")) + for c in cases: + print("%-24s %-6s %-14s %s" % (c.get("id"), c.get("kind"), c.get("split"), + ",".join(c.get("tags") or []))) + print("%d case(s)" % len(cases)) + return 0 + + +def _select_for_run(args, store_dir, purpose="run"): + cases = load_cases(store_dir=store_dir, kind=_opt(args, "--kind")) + split = _opt(args, "--split") + picked = select_cases(cases, split=split, purpose=purpose) + wanted = _opts(args, "--case") + if wanted: + picked = [c for c in picked if c["id"] in wanted] + return picked + + +def _cmd_run(args, store_dir): + try: + picked = _select_for_run(args, store_dir, purpose="tuning" if _opt(args, "--split") == "dev" else "run") + except ValueError as e: + print("sisi eval run: %s" % e, file=sys.stderr) + return 1 + tasks_limit = _int_opt(args, "--tasks", 0) + task_cases = [c for c in picked if c.get("kind") == "task"][:tasks_limit] + as_json = "--json" in args + cfg = _eval_config(store_dir) + cap = cfg.get("max_cost_per_run") + if task_cases: + est = estimate_task_cost(len(task_cases), store_dir=store_dir) + print("estimated cost: %s" % est["text"]) + if cap is not None and est["value"] is not None and est["value"] > float(cap): + print("sisi eval run: estimated cost %g exceeds the per-invocation cap %g " + "from config (eval.max_cost_per_run); nothing run" % (est["value"], float(cap)), + file=sys.stderr) + return 2 + results = run_cases(picked, tasks_limit=tasks_limit, + cost_cap=float(cap) if cap is not None else None, + store_dir=store_dir, policy_dir=_opt(args, "--policy-dir"), + policy_label=_opt(args, "--policy"), + worker=_opt(args, "--worker"), worker_cmd=_opt(args, "--worker-cmd"), + timeout_s=_int_opt(args, "--timeout")) + if as_json: + print(json.dumps(results)) + return 0 + for r in results[:38]: + print("%-24s %-14s policy %s%s%s" % (r.get("case"), r.get("outcome"), r.get("policy"), + (" worker " + str(r["worker"])) if r.get("worker") else "", + (" - " + r["reason"]) if r.get("reason") else "")) + if len(results) > 38: + print("... and %d more" % (len(results) - 38)) + skipped_tasks = len([c for c in picked if c.get("kind") == "task"]) - len(task_cases) + print("%d run(s) recorded%s" % (len(results), + ("; %d task case(s) not run (task replay needs --tasks N)" % skipped_tasks) if skipped_tasks else "")) + return 0 + + +def cmd_folded(cmd, args): + """The former hidden `sisi tasks` and `sisi checks`, now served by the evaluator.""" + args = list(args) + if cmd == "tasks": + sub = args[0] if args else "list" + if sub == "build": + try: + from . import harvest, taskset + con = harvest.connect() + taskset.build(con) + n = import_harvested(con=con) + except Exception as e: + print("sisi eval import: %s" % e, file=sys.stderr) + return 1 + print("cases from harvested tasks: %(created)d created, %(kept)d kept" % n) + return 0 + if sub == "show" and len(args) > 1: + case = get_case(args[1]) + if not case: + print("sisi: no single case matches %r" % args[1], file=sys.stderr) + return 1 + print(json.dumps(case, indent=2)) + return 0 + return _cmd_list(args[1:] if sub == "list" else [], _store()) + latest = {} + for r in read_results(): + if r.get("case_kind") == "check": + latest[r.get("case")] = r + cases = load_cases(kind="check") + print("%-24s %-14s %s" % ("check case", "last outcome", "policy")) + for c in cases[:38]: + r = latest.get(c["id"]) or {} + print("%-24s %-14s %s" % (c["id"], r.get("outcome") or "not-run", r.get("policy") or "-")) + print("%d check case(s)" % len(cases)) + return 0 + + +def cmd_eval(args=(), store_dir=None): + args = list(args) + store_dir = _store(store_dir) + sub = args.pop(0) if args else "status" + as_json = "--json" in args + if sub in ("help", "-h", "--help"): + print(USAGE) + return 0 + try: + if sub == "add": + return _cmd_add(args, store_dir) + if sub == "list": + return _cmd_list(args, store_dir) + if sub == "run": + return _cmd_run(args, store_dir) + if sub == "status": + st = status_summary(store_dir=store_dir) + _show(st, as_json, format_status(st)) + return 0 + if sub == "compare": + plain = [] + for a in args: + if a.startswith("--"): + break + plain.append(a) + if len(plain) != 2: + print(USAGE, file=sys.stderr) + return 1 + comp = compare_versions(plain[0], plain[1], store_dir=store_dir, + split=_opt(args, "--split")) + _show(comp, as_json, format_comparison(comp)) + return 0 + if sub == "shadow": + plain = [a for a in args[:1] if not a.startswith("--")] + if len(plain) != 1 or not os.path.isdir(plain[0]): + print("usage: sisi eval shadow DIR (a candidate copy of the user store)", file=sys.stderr) + return 1 + tasks_limit = _int_opt(args, "--tasks", 0) + picked_cases = select_cases(load_cases(store_dir=store_dir), split="dev", purpose="shadow") + task_cases = [c for c in picked_cases if c.get("kind") == "task"][:tasks_limit] + if task_cases: + print("estimated cost: %s" % estimate_task_cost(len(task_cases) * 2, store_dir=store_dir)["text"]) + comp = compare_for_candidate(plain[0], sample=_int_opt(args, "--sample"), + store_dir=store_dir, worker=_opt(args, "--worker"), + worker_cmd=_opt(args, "--worker-cmd"), + timeout_s=_int_opt(args, "--timeout"), + tasks_limit=tasks_limit) + if comp["excluded_holdout"]: + print("held out, not run: %s" % ", ".join(comp["excluded_holdout"])) + _show(comp, as_json, format_comparison(comp)) + return 0 + if sub == "judge": + plain = [a for a in args[:1] if not a.startswith("--")] + if len(plain) != 1: + print(USAGE, file=sys.stderr) + return 1 + j = judge_result(plain[0], store_dir=store_dir, + judge_cmd=_opt(args, "--judge-cmd"), + judge_worker=_opt(args, "--judge-worker")) + if as_json: + print(json.dumps(j)) + elif j.get("outcome") == "judged": + print("judged by %s" % j["judged_by"]) + for it in j["items"]: + print(" %s: %s - %s" % (it["id"], it["verdict"], it["reason"])) + else: + print("not judged: %s" % j.get("reason")) + return 0 if j.get("outcome") == "judged" else 1 + if sub == "import": + n = import_harvested(store_dir=store_dir) + print("cases from harvested tasks: %(created)d created, %(kept)d kept" % n) + return 0 + except ValueError as e: + print("sisi eval %s: %s" % (sub, e), file=sys.stderr) + return 1 + print(USAGE, file=sys.stderr) + return 1 diff --git a/sisi/groundtruth.py b/sisi/groundtruth.py index 05cc7f4..1b336bf 100644 --- a/sisi/groundtruth.py +++ b/sisi/groundtruth.py @@ -91,6 +91,15 @@ def estimate_start(root, t0): return out if rc == 0 and out else None +def fix_commit_of(commits): + """The fix commit of a harvested task: the last following commit recorded for the + session (commits are [{"sha": ...}] in chronological record order). None when there + were no following commits, so such a task can seed a task case but not a check case.""" + shas = [(c.get("sha") if isinstance(c, dict) else c) for c in (commits or [])] + shas = [s for s in shas if s] + return shas[-1] if shas else None + + def commit_exists(root, sha): rc, _ = _git(root, "cat-file", "-e", str(sha) + "^{commit}") return rc == 0 diff --git a/sisi/taskset.py b/sisi/taskset.py index 522f38d..3e7f98c 100644 --- a/sisi/taskset.py +++ b/sisi/taskset.py @@ -246,6 +246,60 @@ def get_task(con, tid): return dict(rows[0]) if len(rows) == 1 else None +def to_benchmark_case(row, commits=None): + """One harvested task as an evaluation-harness benchmark case (see sisi/evaluate.py), + or None when it is not replayable: only human tasks graded A or B with a test command, + a repository and a start commit qualify. The fix commit is the last following commit + ground truth recorded. The dev/holdout split is the same deterministic id hash as the + task itself, so importing never moves a case between splits. The product ships no such + case: they are built only from the user's own harvested sessions, into the user store.""" + try: + t = dict(row) + except (TypeError, ValueError): + return None + if t.get("kind") not in (None, "human"): + return None + if t.get("grade") not in ("A", "B"): + return None + test_cmd = t.get("test_cmd") + repo, start = t.get("repo_root") or t.get("repo"), t.get("start_commit") + if not test_cmd or not repo or not start or not (t.get("request") or "").strip(): + return None + from . import groundtruth + if commits is None: + try: + commits = json.loads(t.get("commits") or "[]") + except (TypeError, ValueError): + commits = [] + fix = groundtruth.fix_commit_of(commits) + return {"id": str(t.get("id")), "kind": "task", + "source": {"unit": str(t.get("session_id") or ""), "learning": ""}, + "project": {"repo": str(repo), "start_commit": str(start), "fix_commit": str(fix or "")}, + "request": t.get("request"), + "checks": [{"id": "tests", "cmd": str(test_cmd), "expect": {"exit": 0}}], + "tags": ["harvested"], + "split": t.get("split") if t.get("split") in ("dev", "holdout") else split_of(t.get("id")), + "added": time.strftime("%Y-%m-%d", time.gmtime())} + + +def benchmark_cases(con): + """All replayable harvested tasks as benchmark cases, ordered by task id.""" + rows = con.execute( + "SELECT t.*, g.commits FROM tasks t LEFT JOIN groundtruth g ON g.session_id=t.session_id " + "WHERE t.kind='human' ORDER BY t.id") + out = [] + for r in rows: + t = dict(r) + try: + commits = json.loads(t.pop("commits") or "[]") + except (TypeError, ValueError): + commits = [] + case = to_benchmark_case(t, commits) + if case: + out.append(case) + return out + + def counts(con): """{kind: {grade: {split: n}}} for the scoreboard.""" out = {} diff --git a/tests/test_evaluate.py b/tests/test_evaluate.py index 77b2a54..8e2dc3f 100644 --- a/tests/test_evaluate.py +++ b/tests/test_evaluate.py @@ -148,7 +148,7 @@ class TaskReplayTests(EvalCase): c = fixture_case(self, "task-case.json") saved = os.environ.pop("SISI_EVAL_WORKER_CMD", None) try: - r = self.ev.run_case(c, store_dir=self.store, worker_runner=lambda **kw: None) + r = self.ev.run_case(c, store_dir=self.store, worker_runner=lambda *a, **kw: None) finally: if saved: os.environ["SISI_EVAL_WORKER_CMD"] = saved From 052d6d51ee31956a5da1cedfadba99ea4937d1ce Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 23 Nov 2026 11:26:01 -0700 Subject: [PATCH 3/3] Document the evaluation harness --- docs/evaluation.md | 115 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 docs/evaluation.md diff --git a/docs/evaluation.md b/docs/evaluation.md new file mode 100644 index 0000000..8aae0bb --- /dev/null +++ b/docs/evaluation.md @@ -0,0 +1,115 @@ +# Evaluation + +The harness that answers "is sisi getting better?" with evidence instead of claims. A +learning cycle changes the policy (rules, checks, routing, guidance); this replays +representative past work under the old and the new policy and puts the results side by side. +Section numbers in brackets are sections of `SPEC.md`. + +## Concepts + +- A **case** is one replayable piece of real work, stored as JSON in the user store under + `benchmarks/.json`. The product ships no cases from any real project. +- A **replay** runs a case in a temporary checkout of the case's repository and records + what happened. Nothing replays in the user's working tree, and the temporary copy is + removed afterwards whatever the outcome. +- A **policy version** is a hash of the effective policy: the product rulebook plus the + store's generated rulebook, overlay and config. A run may also be labelled by hand + (`--policy NAME`); the hash is then kept beside the label. +- **Results** live in the user store as `eval-results.jsonl`: one row per run with the + case, split, policy version, worker, outcome, minutes, tokens and cost, every value + carrying its class (measured, estimated, unavailable) [13.4]. + +## The case format + +```json +{"id": "case-name", "kind": "check", + "source": {"unit": "the work unit it came from", "learning": "or the learning"}, + "project": {"repo": "path or remote", "start_commit": "before the work", + "fix_commit": "after the work"}, + "request": "the human's own words", + "checks": [{"id": "tests", "cmd": "the check command", "expect": {"exit": 0}}], + "tags": ["task class"], "split": "dev", "added": ""} +``` + +Two kinds and three splits: + +- `check`: the command must **fail at `start_commit` and pass at `fix_commit`**. That is + what "validated" means for a promoted command check [11.5]. Passing at both is + `failed-to-fail` (the check proves nothing); failing at both is `failed-to-pass`. +- `task`: starting from `start_commit`, a worker given `request` under the policy under + test must produce a tree on which the checks pass. +- `dev` cases are for tuning. `holdout` cases are held out: a tuning run (`--split dev`) + and a shadow evaluation must never include them, and they run only when named. + `counterexample` cases must **still pass**: they are the guard against overfitting a + rule to the incident it came from [11.6]. + +## How cases accumulate + +- From finished work: `sisi.evaluate.capture_case(unit_record)` is called when a unit + closes. A unit with a repository, a start commit and command checks becomes a case; + its id and dev/holdout split are deterministic, so re-capturing changes nothing. +- From the harvested task set: `sisi eval import` converts the replayable harvested tasks + (graded A or B, with a test command) into cases. Their split is the same deterministic + hash as the task's. The old hidden `sisi tasks` and `sisi checks` commands are served + by this module now; `sisi harvest` itself is unchanged. +- By hand: `sisi eval add case.json`, or `sisi eval add --id ... --check ...`. + +## Replay and cost + +`sisi eval run` with no arguments replays only `check` cases: that needs no AI tool and +spends nothing. `task` cases spend real money, so they run only with an explicit +`--tasks N`. Before any task replay the command prints the estimated cost, worked out +from the median cost of recorded task runs, or the word `unknown` when nothing has been +recorded. A per-invocation hard cap can be set in the personal config +(`{"eval": {"max_cost_per_run": VALUE, "timeout_s": SECONDS, "worker_timeout_s": SECONDS}}`); +a known estimate above the cap refuses the run, and the cap also stops further task +cases once the known spend reaches it. + +A task replay hands the request to a worker through the existing delegate runner, with a +brief that carries the policy under test, then runs the case's checks on the result. A +run that cannot happen (repository gone, commit missing, timed out, no worker) is +recorded as `not-run` with its reason, never as a pass [13.4]. + +## Comparing policies + +`sisi eval compare A B` treats A as the baseline and B as the candidate, over the cases +**both** ran: pass rate, median and 95th-percentile minutes, and cost, each beside its +counter-metric (a speed or cost figure is meaningless without the pass rate next to it), +plus the same break-down per task class [13.5]. Under five common cases it says "too +few" and claims nothing. Cases only one side ran are listed, not ignored, and +unavailable cost is never counted as zero. A candidate that fails cases the baseline +passed is called "not an improvement" in so many words, however much faster or cheaper +it is. There is deliberately no single score. + +`sisi eval shadow DIR` does the same for a candidate policy that exists only as a copy of +the user store (a learning cycle applied to the copy, not to the real store): it runs a +sample of dev cases plus every counterexample under the current policy and the candidate +and prints the comparison, before anyone is asked to approve the change [11.6]. The +learning loop calls the same code as `sisi.evaluate.compare_for_candidate(dir, sample)`, +and calls `sisi.evaluate.validate_check(case)` before promoting a command check. + +## Independent judging + +`sisi eval judge RESULT_ID` asks a worker from a **different maker** than the one that +did the run (chosen through the routing module's reviewer selection) to read the request, +the diff and the check results and answer met or not met for each acceptance item, with +one line of reason [13.3]. The judgement is stored with the result as `judged_by`. A +judge from the same maker is refused: a worker's own assessment is never recorded as +independent evidence. + +## Honest limits + +- A replay is a **sample**, not the original work: the worker, its context and the state + of the world have all moved on. Outcomes are comparable between policies run the same + way; they are not a rerun of history. +- A pass rate over a handful of cases says little, which is why the comparison refuses to + claim improvement under five common cases and splits figures by task class instead of + hiding a bad one behind a good one. +- Holdout cases exist to catch tuning that overfits the cases it was tuned on. Using them + to tune a rule quietly converts them into dev cases and destroys their value: run them + to check, never to adjust. +- A check case that passes at both commits may still be a useful check; it is only a bad + *benchmark case*, because it cannot distinguish a fixed tree from a broken one. +- Cost and token figures are as good as what the worker tools report. Where nothing is + reported they stay "unavailable", and the harness will not invent precision to fill + the gap.