Records: the durable work-unit record, five verification statuses with selection and reuse, telemetry with honest classes, the scorecard and sisi status, scratch pruning
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
commit
563cac426a
31 files changed
+3132
-783
No files matched your search
@@ -115,6 +115,8 @@ class SisiCase(unittest.TestCase):
|
||||
|
||||
|
||||
def check(id, cmd, rung=3, kind="general", exit=0, timeout_s=10, **expect):
|
||||
if cmd == "true": # a passing check that can fail (bare `true` is vacuous and refused)
|
||||
cmd = "test -f .sisi/contract.json"
|
||||
return {"id": id, "claim": "claim " + id, "kind": kind, "rung": rung, "cmd": cmd,
|
||||
"expect": dict({"exit": exit}, **expect), "timeout_s": timeout_s}
|
||||
|
||||
|
||||
@@ -222,96 +222,6 @@ class DeliveryRowTests(SisiCase):
|
||||
per = (time.perf_counter() - t) / 5
|
||||
self.assertLess(per, 0.25)
|
||||
|
||||
def ref_percentile(xs, p):
|
||||
s = sorted(xs)
|
||||
if len(s) == 1:
|
||||
return s[0]
|
||||
pos = (len(s) - 1) * p / 100.0
|
||||
lo, frac = int(pos // 1), pos - int(pos // 1)
|
||||
return s[lo] if frac == 0 else s[lo] * (1 - frac) + s[lo + 1] * frac
|
||||
|
||||
|
||||
class TimeCommandTests(SisiCase):
|
||||
def test_small_known_values(self):
|
||||
self.assertEqual(delivery.median([5, 1, 3]), 3)
|
||||
self.assertEqual(delivery.median([4, 1, 3, 2]), 2.5)
|
||||
self.assertEqual(delivery.percentile([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 90), 9.1)
|
||||
self.assertEqual(delivery.percentile([7], 90), 7)
|
||||
self.assertIsNone(delivery.median([]))
|
||||
self.assertIsNone(delivery.percentile([], 90))
|
||||
|
||||
def test_median_and_percentile_property(self):
|
||||
rnd = random.Random(7)
|
||||
for _ in range(300 * ITERS):
|
||||
xs = [rnd.choice([rnd.randint(0, 50), rnd.random() * 1000]) for _ in range(rnd.randint(1, 40))]
|
||||
half = sorted(xs)
|
||||
n = len(half)
|
||||
want_med = half[n // 2] if n % 2 else (half[n // 2 - 1] + half[n // 2]) / 2
|
||||
self.assertAlmostEqual(delivery.median(xs), want_med)
|
||||
for p in (0, 50, 90, 100, rnd.randint(0, 100)):
|
||||
self.assertAlmostEqual(delivery.percentile(xs, p), ref_percentile(xs, p), places=6)
|
||||
self.assertLessEqual(delivery.percentile(xs, 50), delivery.percentile(xs, 90) + 1e-9)
|
||||
|
||||
def add_rows(self, now, spec):
|
||||
"""spec: list of (days_ago, seconds, project, send_backs, check_s)."""
|
||||
for d, secs, proj, sb, cs in spec:
|
||||
end = now - d * 86400
|
||||
store.append("deliveries", {"ts": delivery._iso(end), "session": "s", "project": proj, "start": delivery._iso(end - secs),
|
||||
"end": delivery._iso(end), "seconds": secs, "send_backs": sb, "check_s": cs, "contract": True})
|
||||
|
||||
def test_report_windows_projects_shares_and_send_backs(self):
|
||||
now = time.time()
|
||||
self.add_rows(now, [(1, 100, "/a/alpha", 0, 10), (2, 300, "/a/alpha", 1, 90), (3, 200, "/b/beta", 0, 20),
|
||||
(9, 400, "/a/alpha", 0, 40), (10, 800, "/b/beta", 2, 80), (20, 5, "/a/alpha", 0, 0)])
|
||||
r = delivery.report(now)
|
||||
cur, prev = r["all"]["last7"], r["all"]["prev7"]
|
||||
self.assertEqual((cur["n"], cur["median_s"], prev["n"], prev["median_s"]), (3, 200, 2, 600))
|
||||
self.assertAlmostEqual(cur["check_share"], 120 / 600)
|
||||
self.assertEqual(cur["send_back_deliveries"], 1)
|
||||
self.assertEqual(r["projects"]["/a/alpha"]["last7"]["median_s"], 200)
|
||||
self.assertEqual(r["projects"]["/b/beta"]["prev7"]["n"], 1)
|
||||
out = []
|
||||
delivery.cmd_time([], out=out.append)
|
||||
text = "\n".join(out)
|
||||
self.assertIn("median 3m20s", text)
|
||||
self.assertIn("1 of 3", text)
|
||||
self.assertIn("alpha", text)
|
||||
js = []
|
||||
delivery.cmd_time(["--json"], out=js.append)
|
||||
self.assertEqual(json.loads(js[0])["all"]["last7"]["n"], 3)
|
||||
|
||||
def test_windows_agree_with_a_simple_reference(self):
|
||||
rnd = random.Random(11)
|
||||
now = time.time()
|
||||
for _ in range(30 * ITERS):
|
||||
rows = [{"end": delivery._iso(now - rnd.random() * 20 * 86400), "seconds": rnd.random() * 500} for _ in range(rnd.randint(0, 30))]
|
||||
cur, prev = delivery.windows(rows, now)
|
||||
ref_cur = [r for r in rows if now - 7 * 86400 <= harvest.epoch(r["end"]) <= now]
|
||||
ref_prev = [r for r in rows if now - 14 * 86400 <= harvest.epoch(r["end"]) < now - 7 * 86400]
|
||||
self.assertEqual((sorted(map(id, cur)), sorted(map(id, prev))), (sorted(map(id, ref_cur)), sorted(map(id, ref_prev))))
|
||||
|
||||
def test_no_data_is_said_plainly(self):
|
||||
r = self.sisi("time")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertIn("no deliveries recorded", r.stdout)
|
||||
self.assertIn("no deliveries recorded in the last 7 days", self.sisi("status").stdout)
|
||||
self.assertEqual(json.loads(self.sisi("time", "--json").stdout)["all"]["last7"]["n"], 0)
|
||||
|
||||
def test_status_shows_the_seven_day_median_and_slow_checks_get_advice(self):
|
||||
now = time.time()
|
||||
self.add_rows(now, [(1, 120, "/p", 0, 5), (2, 60, "/p", 0, 5), (3, 600, "/p", 0, 5)])
|
||||
self.assertIn("median 2m00s over the last 7 days (3 deliveries)", self.sisi("status").stdout)
|
||||
for dur in (50, 70):
|
||||
store.append("results", {"ts": store.now(), "check": "slow-one", "status": "passed", "duration_s": dur, "tier": "fast"})
|
||||
store.append("results", {"ts": store.now(), "check": "quick", "status": "passed", "duration_s": 0.5, "tier": "fast"})
|
||||
store.append("results", {"ts": store.now(), "check": "mutation", "status": "passed", "duration_s": 300, "tier": "deep"})
|
||||
slow = delivery.report(now)["slowest_checks"]
|
||||
self.assertEqual([x["id"] for x in slow], ["mutation", "slow-one", "quick"])
|
||||
self.assertAlmostEqual(slow[1]["mean_s"], 60)
|
||||
self.assertIn('"tier": "deep"', slow[1]["suggestion"])
|
||||
self.assertIn("split", slow[0]["suggestion"])
|
||||
|
||||
|
||||
class ParallelVerifyTests(SisiCase):
|
||||
def strip(self, results):
|
||||
return [{k: r[k] for k in ("item", "id", "status", "kind", "rung", "tier")} for r in results]
|
||||
|
||||
+3
-2
@@ -27,9 +27,10 @@ class EndToEnd(SisiCase):
|
||||
r = self.sisi("gate", stdin=stop)
|
||||
self.assertEqual((r.returncode, r.stdout), (0, ""))
|
||||
|
||||
self.assertEqual(self.sisi("unit", "close").returncode, 0)
|
||||
out = self.sisi("status").stdout
|
||||
self.assertIn("contracts verified : 1", out)
|
||||
self.assertIn("3: 100%", out)
|
||||
self.assertIn("[delivered]", out)
|
||||
self.assertIn("correctness and assurance", out)
|
||||
self.assertEqual(self.sisi("uninstall").returncode, 0)
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -57,7 +57,7 @@ class GateContractTests(GateBase):
|
||||
a, msg = self.call()
|
||||
self.assertEqual(a, "block")
|
||||
st = {r["check"]: r["status"] for r in self.results()}
|
||||
self.assertEqual(st, {"a": "timeout", "b": "timeout"}) # b never ran: counts as timeout
|
||||
self.assertEqual(st, {"a": "not-run", "b": "not-run"}) # a timed out, b never ran: neither passed
|
||||
|
||||
def test_three_strikes_then_unverified_release_then_reset(self):
|
||||
self.write_contract(contract([check("bad", "false")]))
|
||||
|
||||
@@ -247,7 +247,6 @@ class OverlayTests(PersonalCase):
|
||||
self.assertIn("rulebook %d lines" % lines, out)
|
||||
self.assertIn("%d characters" % len(self.generated()), out)
|
||||
self.assertNotIn("lessons block", out) # no lesson list is loaded at session start
|
||||
self.assertIn("rulebook %d lines" % lines, self.ok("status").stdout)
|
||||
|
||||
def test_core_md_rules_have_unique_ids_and_the_budget_holds(self):
|
||||
text = open(os.path.join(REPO, "canon", "core.md")).read()
|
||||
|
||||
@@ -106,18 +106,6 @@ class StoreTests(SisiCase):
|
||||
[t.start() for t in ts]; [t.join() for t in ts]
|
||||
self.assertEqual(len(self.read("learn.jsonl")), 300)
|
||||
|
||||
def test_status(self):
|
||||
self.write_contract(contract([check("a", "true", rung=2), check("b", "true", rung=3), human("h")]))
|
||||
self.sisi("verify")
|
||||
self.write_contract(contract([check("a", "false", rung=2)]))
|
||||
self.sisi("verify")
|
||||
self.sisi("contract", "waive", "tiny")
|
||||
out = self.sisi("status").stdout
|
||||
self.assertIn("contracts verified : 1", out)
|
||||
self.assertIn("pass rate 50%", out)
|
||||
self.assertIn("waivers : 1", out)
|
||||
self.assertIn("unverified passes : 0", out)
|
||||
|
||||
|
||||
class HelpTests(SisiCase):
|
||||
def test_help_and_unknown(self):
|
||||
|
||||
@@ -0,0 +1,593 @@
|
||||
"""The work-unit record (SPEC 10), telemetry and the scorecard (SPEC 6.1, 13), `sisi status`, and
|
||||
scratch pruning (docs/target.md sections 2, 4, 6)."""
|
||||
import json, os, subprocess, time, unittest
|
||||
from unittest import mock
|
||||
from support import *
|
||||
from sisi import contract as C, harvest, scorecard, store, unit as U, workers
|
||||
|
||||
SCORE_DIMENSIONS = ("intent_fulfilment", "correctness_and_assurance", "delivery_velocity", "resource_efficiency",
|
||||
"parallelism", "human_experience", "lifecycle_quality", "organizational_improvement")
|
||||
|
||||
|
||||
def real(id, cmd, **kw):
|
||||
return dict(check(id, cmd), **kw)
|
||||
|
||||
|
||||
class UnitCase(SisiCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.git_init()
|
||||
self.contract_path = os.path.join(self.proj, ".sisi", "contract.json")
|
||||
|
||||
def start(self, checks=None, **fields):
|
||||
r = self.sisi("contract", "init", "--goal", "make the thing")
|
||||
self.assertEqual(r.returncode, 0, r.stdout)
|
||||
c = C.load(self.contract_path)
|
||||
c["checks"] = checks or [real("works", "test -f done.txt")]
|
||||
c.update(fields)
|
||||
C.save(self.contract_path, c)
|
||||
return c
|
||||
|
||||
def finish_green(self):
|
||||
open(os.path.join(self.proj, "done.txt"), "w").write("x")
|
||||
r = self.sisi("verify")
|
||||
self.assertEqual(r.returncode, 0, r.stdout)
|
||||
|
||||
def archive(self, uid):
|
||||
return json.load(open(os.path.join(self.proj, ".sisi", "units", "%s.json" % uid)))
|
||||
|
||||
def commit_all(self, msg="c"):
|
||||
subprocess.run(["git", "add", "-A"], cwd=self.proj, check=True)
|
||||
subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", msg], cwd=self.proj, check=True)
|
||||
|
||||
|
||||
class RecordTests(UnitCase):
|
||||
def test_close_archives_all_fourteen_items(self):
|
||||
"""R-10.2-1, R-10.2-2, R-10.2-3, R-10.2-4, R-10.2-5, R-10.2-6, R-10.2-7, R-10.2-8, R-10.2-9,
|
||||
R-10.2-10, R-10.2-11, R-10.2-12, R-10.2-13, R-10.2-14, R-10.2-15: the archived record carries
|
||||
every item of SPEC 10.2, and a summary line goes to the user store."""
|
||||
self.start(parent="request-9", scope={"in": ["app"], "out": ["docs"]}, plan=["write it", "check it"],
|
||||
requirements=[{"id": "q1", "kind": "explicit", "text": "it works", "decision": "now", "check": "works"}],
|
||||
risk={"profile": "isolated"}, limitations=["only tested here"],
|
||||
deferred=[{"what": "perf run", "why": "slow", "safe_because": "no hot path"}])
|
||||
self.finish_green()
|
||||
r = self.sisi("unit", "close")
|
||||
self.assertEqual(r.returncode, 0, r.stdout)
|
||||
rec = self.archive("proj")
|
||||
for key in ("id", "parent", "goal", "scope", "requirements", "checks", "invariants", "risk", "plan",
|
||||
"workers", "models", "budgets", "started", "ended", "telemetry", "changed_artifacts",
|
||||
"verification", "deferred", "limitations", "delivery_status", "scorecard", "retro_findings",
|
||||
"learning_ids", "policy_version", "start_commit", "closing_commit"):
|
||||
self.assertIn(key, rec, key)
|
||||
self.assertEqual(rec["parent"], "request-9")
|
||||
self.assertEqual(rec["risk"]["declared"], "isolated")
|
||||
self.assertIn("floor", rec["risk"])
|
||||
self.assertTrue(rec["policy_version"] and rec["start_commit"])
|
||||
self.assertEqual(rec["closing_commit"], rec["start_commit"]) # nothing committed in this unit
|
||||
self.assertEqual(rec["delivery_status"], "delivered-with-deferred") # the contract defers something
|
||||
self.assertEqual(set(rec["scorecard"]), set(SCORE_DIMENSIONS))
|
||||
lines = store.read("units")
|
||||
self.assertEqual(len(lines), 1)
|
||||
for key in ("id", "project", "closed_at", "risk", "delivery_status", "scorecard"):
|
||||
self.assertIn(key, lines[0], key)
|
||||
|
||||
def test_policy_version_is_stable_and_follows_the_policies(self):
|
||||
"""R-10.2-6: the hash does not depend on the project's path and changes when its risk table does."""
|
||||
c = {"id": "x"}
|
||||
a = U.policy_version(self.proj, c)
|
||||
self.assertEqual(a, U.policy_version(self.proj, c))
|
||||
os.makedirs(os.path.join(self.proj, ".sisi"), exist_ok=True)
|
||||
with open(os.path.join(self.proj, ".sisi", "risk.json"), "w") as f:
|
||||
json.dump({"patterns": []}, f)
|
||||
self.assertNotEqual(a, U.policy_version(self.proj, c))
|
||||
other = os.path.join(self.tmp, "elsewhere")
|
||||
os.makedirs(other)
|
||||
self.assertEqual(U.policy_version(other, c), a)
|
||||
|
||||
def test_start_and_closing_commits_and_changed_artifacts(self):
|
||||
"""R-10.2-10: the start commit, the closing commit and the changed files come from git."""
|
||||
self.start()
|
||||
base = json.load(open(self.contract_path))["base_head"]
|
||||
open(os.path.join(self.proj, "done.txt"), "w").write("x\n")
|
||||
self.commit_all("work")
|
||||
self.assertEqual(self.sisi("verify").returncode, 0)
|
||||
self.sisi("unit", "close")
|
||||
rec = self.archive("proj")
|
||||
self.assertEqual(rec["start_commit"], base)
|
||||
self.assertNotEqual(rec["closing_commit"], base)
|
||||
self.assertIn("done.txt", rec["changed_artifacts"]["files"])
|
||||
self.assertNotIn(".sisi/units/proj.json", rec["changed_artifacts"]["files"])
|
||||
|
||||
def test_risk_floor_is_stored_beside_the_declared_profile(self):
|
||||
"""R-10.2-6: the floor comes from the changed paths; the declared profile is kept as declared."""
|
||||
self.start(risk={"profile": "trivial", "why": "docs"})
|
||||
os.makedirs(os.path.join(self.proj, "auth"))
|
||||
open(os.path.join(self.proj, "auth", "password_reset.py"), "w").write("secret = 1\n")
|
||||
open(os.path.join(self.proj, "done.txt"), "w").write("x")
|
||||
self.assertEqual(self.sisi("verify").returncode, 0)
|
||||
self.sisi("unit", "close")
|
||||
risk = self.archive("proj")["risk"]
|
||||
self.assertEqual(risk["declared"], "trivial")
|
||||
self.assertEqual(risk["floor"]["class"], "measured")
|
||||
self.assertNotEqual(risk["floor"]["value"], "trivial") # authorization paths raise the floor
|
||||
|
||||
def test_findings_and_delegations_come_from_their_stores(self):
|
||||
"""R-10.2-7, R-10.2-15: the record holds the retro findings recorded for the unit and the
|
||||
delegations the ledger has for the project in its window."""
|
||||
self.start()
|
||||
self.finish_green()
|
||||
r = self.sisi("retro", "--finding", "the check failed once because done.txt did not exist yet",
|
||||
"--cause", "the file was created after the first run, not before", "--impact", "time",
|
||||
"--disposition", "record", "--limits", "only for tasks that create files")
|
||||
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
|
||||
now = time.time()
|
||||
for i, (start, minutes) in enumerate(((now - 600, 8), (now - 500, 5))):
|
||||
workers.append_record({"kind": "attempt", "id": "d%d" % i, "ts": int(now), "project": "proj",
|
||||
"type": "impl", "difficulty": 1, "worker": "w", "outcome": "accepted",
|
||||
"check": "tests", "minutes": minutes, "started": start, "ended": start + minutes * 60})
|
||||
workers.append_record({"kind": "attempt", "id": "z", "ts": int(now), "project": "elsewhere", "worker": "w"})
|
||||
self.sisi("unit", "close")
|
||||
rec = self.archive("proj")
|
||||
self.assertEqual(len(rec["retro_findings"]), 1)
|
||||
self.assertEqual(rec["telemetry"]["retro_findings"], {"value": 1, "class": "measured"})
|
||||
self.assertEqual(len(rec["workers"]), 2)
|
||||
par = rec["scorecard"]["parallelism"]
|
||||
self.assertEqual(par["delegations"]["value"], 2)
|
||||
self.assertEqual(par["overlapped_delegations"]["value"], 1) # the two ran at the same time
|
||||
self.assertEqual(par["worker_minutes"]["class"], "estimated")
|
||||
|
||||
def test_the_closed_unit_is_offered_to_the_evaluation_harness(self):
|
||||
"""R-13.3-3: a close calls the harness's capture, and a failure there never stops the close."""
|
||||
self.start()
|
||||
self.finish_green()
|
||||
with mock.patch("sisi.evaluate.capture_case") as cap:
|
||||
self.assertEqual(self.sisi("unit", "close").returncode, 0)
|
||||
cap.assert_called_once()
|
||||
view = cap.call_args[0][0]
|
||||
self.assertEqual((view["id"], view["delivery_status"]), ("proj", "delivered"))
|
||||
self.assertTrue(view["start_commit"])
|
||||
self.sisi("contract", "init")
|
||||
self.finish_green_again()
|
||||
with mock.patch("sisi.evaluate.capture_case", side_effect=RuntimeError("harness down")):
|
||||
self.assertEqual(self.sisi("unit", "close").returncode, 0)
|
||||
self.assertEqual({r["id"] for r in store.read("units")}, {"proj", "proj-2"})
|
||||
|
||||
def finish_green_again(self):
|
||||
c = C.load(self.contract_path)
|
||||
c["checks"] = [real("again", "test -f done.txt")]
|
||||
C.save(self.contract_path, c)
|
||||
self.assertEqual(self.sisi("verify").returncode, 0)
|
||||
|
||||
def test_a_real_benchmark_case_is_stored_for_a_repository_unit(self):
|
||||
"""R-13.3-3: a unit with a start commit and a command check becomes a stored case."""
|
||||
from sisi import evaluate
|
||||
self.start()
|
||||
self.finish_green()
|
||||
self.sisi("unit", "close")
|
||||
self.assertIsNotNone(evaluate.get_case("case-proj"))
|
||||
|
||||
|
||||
class DeliveryStatusTests(UnitCase):
|
||||
def test_deferred_and_human_checks_close_as_delivered_with_deferred(self):
|
||||
"""R-10.3-4, R-10.3-5: a check that needs a person is not-run (needs human) and does not
|
||||
block; the unit says so in its delivery status and lists it."""
|
||||
self.start(checks=[real("works", "test -f done.txt"), human("eyes", 5)])
|
||||
self.finish_green()
|
||||
self.sisi("unit", "close")
|
||||
rec = self.archive("proj")
|
||||
self.assertEqual(rec["delivery_status"], "delivered-with-deferred")
|
||||
self.assertEqual(rec["deferred_checks"], ["eyes"])
|
||||
|
||||
def test_a_failing_check_refuses_a_close_as_delivered(self):
|
||||
"""R-10.3-4: required verification must have passed."""
|
||||
self.start()
|
||||
r = self.sisi("unit", "close")
|
||||
self.assertEqual(r.returncode, 1)
|
||||
self.assertIn("cannot close as delivered", r.stdout)
|
||||
self.assertFalse(os.path.exists(os.path.join(self.proj, ".sisi", "units")))
|
||||
self.assertEqual(store.read("units"), [])
|
||||
|
||||
def test_gate_release_after_send_backs_closes_as_unverified(self):
|
||||
"""R-10.3-5: a unit the gate released with failures standing is `unverified`."""
|
||||
self.start()
|
||||
self.sisi("verify")
|
||||
store.append("results", {"ts": store.now(), "contract": "proj", "project": self.proj, "check": "gate",
|
||||
"item": "gate", "status": "unverified", "duration_s": 0.0})
|
||||
self.assertEqual(self.sisi("unit", "close").returncode, 0)
|
||||
self.assertEqual(self.archive("proj")["delivery_status"], "unverified")
|
||||
|
||||
def test_abandon_records_the_reason_and_needs_one(self):
|
||||
"""R-10.3-8: a unit may be stopped; the disposition and reason are recorded."""
|
||||
self.start()
|
||||
self.assertEqual(self.sisi("unit", "close", "--abandon").returncode, 1)
|
||||
self.assertEqual(self.sisi("unit", "close", "--abandon", "premise disproven").returncode, 0)
|
||||
rec = self.archive("proj")
|
||||
self.assertEqual((rec["delivery_status"], rec["abandon_reason"]), ("abandoned", "premise disproven"))
|
||||
|
||||
def test_only_the_five_delivery_statuses_exist(self):
|
||||
"""R-10.2-13."""
|
||||
self.assertEqual(set(U.DELIVERY_STATUSES),
|
||||
{"delivered", "delivered-with-deferred", "deviation", "unverified", "abandoned"})
|
||||
|
||||
|
||||
class NeverLostTests(UnitCase):
|
||||
def test_contract_init_refuses_an_open_unit_and_replaces_a_closed_one(self):
|
||||
"""R-10.2-1, R-10.3-1: `sisi contract init` on an open unit refuses and names the abandon
|
||||
command; on a closed unit it starts a new one and the old record stays."""
|
||||
self.start()
|
||||
before = open(self.contract_path).read()
|
||||
r = self.sisi("contract", "init")
|
||||
self.assertEqual(r.returncode, 1)
|
||||
self.assertIn('sisi unit close --abandon "<reason>"', r.stdout)
|
||||
self.assertEqual(open(self.contract_path).read(), before)
|
||||
self.finish_green()
|
||||
self.sisi("unit", "close")
|
||||
old = open(os.path.join(self.proj, ".sisi", "units", "proj.json"), "rb").read()
|
||||
self.assertEqual(self.sisi("contract", "init").returncode, 0)
|
||||
c = C.load(self.contract_path)
|
||||
self.assertEqual((c["id"], c["status"]), ("proj-2", "open"))
|
||||
self.assertEqual(open(os.path.join(self.proj, ".sisi", "units", "proj.json"), "rb").read(), old)
|
||||
|
||||
def test_closing_twice_changes_nothing(self):
|
||||
"""R-10.2-1: a record is never overwritten or duplicated."""
|
||||
self.start()
|
||||
self.finish_green()
|
||||
self.sisi("unit", "close")
|
||||
p = os.path.join(self.proj, ".sisi", "units", "proj.json")
|
||||
old, lines = open(p, "rb").read(), open(os.path.join(self.data, "units.jsonl")).read()
|
||||
r = self.sisi("unit", "close")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertIn("already closed", r.stdout)
|
||||
self.assertEqual(open(p, "rb").read(), old)
|
||||
self.assertEqual(open(os.path.join(self.data, "units.jsonl")).read(), lines)
|
||||
|
||||
def test_abandoning_then_starting_again_keeps_every_record(self):
|
||||
"""R-10.2-1."""
|
||||
for n in range(3):
|
||||
self.start()
|
||||
self.assertEqual(self.sisi("unit", "close", "--abandon", "try %d" % n).returncode, 0)
|
||||
names = sorted(os.listdir(os.path.join(self.proj, ".sisi", "units")))
|
||||
self.assertEqual(names, ["proj-2.json", "proj-3.json", "proj.json"])
|
||||
self.assertEqual(len(store.read("units")), 3)
|
||||
|
||||
def test_a_crash_between_archive_and_reset_loses_nothing_and_converges(self):
|
||||
"""R-10.2-1: the archive written, the summary line and the contract reset not: closing again
|
||||
keeps the archive byte for byte, adds the missing summary once and marks the contract closed."""
|
||||
c = self.start()
|
||||
self.finish_green()
|
||||
c = C.load(self.contract_path)
|
||||
rec = U.build_record(c, self.proj, "delivered", time.time())
|
||||
path, created = U.archive_record(rec, self.proj)
|
||||
self.assertTrue(created)
|
||||
before = open(path, "rb").read()
|
||||
self.assertEqual(store.read("units"), [])
|
||||
self.assertEqual(self.sisi("contract", "init").returncode, 1) # still open: refused
|
||||
self.assertEqual(self.sisi("unit", "close").returncode, 0)
|
||||
self.assertEqual(open(path, "rb").read(), before)
|
||||
self.assertEqual(len(store.read("units")), 1)
|
||||
self.assertEqual(C.load(self.contract_path)["status"], "closed")
|
||||
self.sisi("unit", "close")
|
||||
self.assertEqual(len(store.read("units")), 1)
|
||||
|
||||
def test_an_archive_that_belongs_to_another_unit_is_never_overwritten(self):
|
||||
"""R-10.2-1: a hand-made id collision moves the new unit to a fresh id."""
|
||||
self.start()
|
||||
self.finish_green()
|
||||
os.makedirs(os.path.join(self.proj, ".sisi", "units"))
|
||||
p = os.path.join(self.proj, ".sisi", "units", "proj.json")
|
||||
with open(p, "w") as f:
|
||||
json.dump({"id": "proj", "started": "2001-01-01T00:00:00Z", "project": self.proj}, f)
|
||||
before = open(p, "rb").read()
|
||||
r = self.sisi("unit", "close")
|
||||
self.assertEqual(r.returncode, 0, r.stdout)
|
||||
self.assertEqual(open(p, "rb").read(), before)
|
||||
self.assertTrue(os.path.exists(os.path.join(self.proj, ".sisi", "units", "proj-2.json")))
|
||||
|
||||
def test_archive_record_never_replaces_an_existing_archive(self):
|
||||
"""R-10.2-1: two writers racing for one id: the first record stays, byte for byte."""
|
||||
path, created = U.archive_record({"id": "u", "v": 1}, self.proj)
|
||||
self.assertTrue(created)
|
||||
first = open(path, "rb").read()
|
||||
path2, created2 = U.archive_record({"id": "u", "v": 2}, self.proj)
|
||||
self.assertFalse(created2)
|
||||
self.assertEqual(open(path, "rb").read(), first)
|
||||
self.assertEqual([n for n in os.listdir(os.path.dirname(path)) if ".tmp" in n], [])
|
||||
|
||||
def test_an_interrupted_write_leaves_no_partial_archive(self):
|
||||
"""R-10.2-1: the archive appears whole or not at all."""
|
||||
self.start()
|
||||
self.finish_green()
|
||||
with mock.patch("os.fsync", side_effect=OSError("disk gone")):
|
||||
with self.assertRaises(OSError):
|
||||
U.archive_record({"id": "proj"}, self.proj)
|
||||
self.assertEqual([n for n in os.listdir(os.path.join(self.proj, ".sisi", "units")) if n == "proj.json"], [])
|
||||
|
||||
def test_show_and_list(self):
|
||||
"""R-10.2-1: `sisi unit show [ID]` and `sisi unit list`."""
|
||||
self.start()
|
||||
self.assertIn('"id": "proj"', self.sisi("unit", "show").stdout) # the open unit
|
||||
self.assertIn("open", self.sisi("unit", "list").stdout)
|
||||
self.finish_green()
|
||||
self.sisi("unit", "close")
|
||||
self.assertEqual(json.loads(self.sisi("unit", "show", "proj").stdout)["delivery_status"], "delivered")
|
||||
self.assertIn("delivered", self.sisi("unit", "list").stdout)
|
||||
self.assertEqual(self.sisi("unit", "show", "nope").returncode, 1)
|
||||
|
||||
|
||||
class PauseTests(UnitCase):
|
||||
def test_pause_lets_the_turn_end_and_a_change_ends_the_wait(self):
|
||||
"""R-6.1-5: a pause is recorded, the gate makes no demands while nothing changes, a new change
|
||||
ends the wait, and the wait is telemetry."""
|
||||
self.start()
|
||||
self.assertEqual(self.sisi("unit", "pause", "").returncode, 1)
|
||||
self.assertEqual(self.sisi("unit", "pause", "the owner's decision").returncode, 0)
|
||||
c = C.load(self.contract_path)
|
||||
self.assertEqual(c["status"], "paused")
|
||||
self.assertEqual(c["pauses"][0]["what"], "the owner's decision")
|
||||
stop = json.dumps({"session_id": "p1", "cwd": self.proj, "hook_event_name": "Stop", "stop_hook_active": False})
|
||||
self.sisi("tick", stdin=json.dumps({"session_id": "p1", "cwd": self.proj, "hook_event_name": "SessionStart"}))
|
||||
open(os.path.join(self.proj, "new.txt"), "w").write("x") # a change after the pause
|
||||
self.assertIn("block", self.sisi("gate", stdin=stop).stdout)
|
||||
self.assertEqual(C.load(self.contract_path)["status"], "open") # the wait ended
|
||||
self.assertTrue(C.load(self.contract_path)["pauses"][0]["until"])
|
||||
self.finish_green()
|
||||
self.sisi("unit", "close")
|
||||
wait = self.archive("proj")["telemetry"]["time_in_waits_s"]
|
||||
self.assertEqual(wait["class"], "measured")
|
||||
|
||||
def test_a_paused_unit_with_no_change_passes_silently(self):
|
||||
"""R-6.1-5."""
|
||||
self.start()
|
||||
self.sisi("unit", "pause", "waiting for a review")
|
||||
stop = json.dumps({"session_id": "p2", "cwd": self.proj, "hook_event_name": "Stop", "stop_hook_active": False})
|
||||
self.sisi("tick", stdin=json.dumps({"session_id": "p2", "cwd": self.proj, "hook_event_name": "SessionStart"}))
|
||||
r = self.sisi("gate", stdin=stop)
|
||||
self.assertEqual((r.returncode, r.stdout.strip()), (0, ""))
|
||||
|
||||
|
||||
class TelemetryHonestyTests(UnitCase):
|
||||
def transcript(self, with_usage, model="model-one"):
|
||||
now = store.now()
|
||||
assistant = {"model": model, "content": [{"type": "tool_use", "name": "AskUserQuestion", "input": {}}]}
|
||||
if with_usage:
|
||||
assistant["usage"] = {"input_tokens": 1000, "output_tokens": 500}
|
||||
lines = [{"type": "user", "timestamp": now, "message": {"role": "user", "content": "please build the thing"}},
|
||||
{"type": "assistant", "timestamp": now, "message": assistant},
|
||||
{"type": "user", "timestamp": now, "message": {"role": "user", "content": "no, that is wrong"}}]
|
||||
tp = os.path.join(self.tmp, "t.jsonl")
|
||||
with open(tp, "w") as f:
|
||||
f.write("\n".join(json.dumps(x) for x in lines) + "\n")
|
||||
store.save_session("s-tel", {"cwd": self.proj, "transcript_path": tp, "started": store.now()})
|
||||
return tp
|
||||
|
||||
def close_and_load(self):
|
||||
self.finish_green()
|
||||
self.assertEqual(self.sisi("unit", "close").returncode, 0)
|
||||
return self.archive("proj")["telemetry"]
|
||||
|
||||
def test_without_a_transcript_nothing_is_measured_and_nothing_is_zero(self):
|
||||
"""R-13.4-1, R-13.4-2: no transcript: tokens, cost, messages and dialogs are unavailable, with
|
||||
no value, never 0 and never measured."""
|
||||
self.start()
|
||||
tel = self.close_and_load()
|
||||
for k in ("tokens_by_category", "tokens_by_model", "cost", "human_messages", "question_dialogs",
|
||||
"human_corrections"):
|
||||
self.assertEqual(tel[k], {"value": None, "class": "unavailable"}, k)
|
||||
|
||||
def test_a_transcript_without_usage_gives_no_tokens_and_no_cost(self):
|
||||
"""R-13.4-2: the host wrote a transcript but no usage numbers: the human counts are measured,
|
||||
and no token or cost figure may be shown as measured."""
|
||||
self.start()
|
||||
self.transcript(with_usage=False)
|
||||
tel = self.close_and_load()
|
||||
self.assertEqual(tel["human_messages"], {"value": 2, "class": "measured"})
|
||||
self.assertEqual(tel["question_dialogs"], {"value": 1, "class": "measured"})
|
||||
self.assertEqual(tel["human_corrections"]["class"], "inferred")
|
||||
self.assertEqual(tel["human_corrections"]["value"], 1)
|
||||
for k in ("tokens_by_category", "tokens_by_model", "cost"):
|
||||
self.assertEqual(tel[k]["class"], "unavailable", k)
|
||||
self.assertIsNone(tel[k]["value"], k)
|
||||
|
||||
def test_with_usage_tokens_are_measured_by_category_and_model_and_cost_needs_a_price(self):
|
||||
"""R-13.3-2, R-13.4-1: usage in the transcript is measured by category and by model; a cost
|
||||
is only ever estimated, and only from a recorded price."""
|
||||
self.start()
|
||||
self.transcript(with_usage=True)
|
||||
tel = self.close_and_load()
|
||||
self.assertEqual(tel["tokens_by_category"]["class"], "measured")
|
||||
self.assertEqual(tel["tokens_by_category"]["value"]["input"], 1000)
|
||||
self.assertEqual(tel["tokens_by_model"]["value"]["model-one"]["output"], 500)
|
||||
self.assertEqual(tel["cost"], {"value": None, "class": "unavailable"}) # no price recorded
|
||||
self.sisi("contract", "init")
|
||||
self.finish_green_again()
|
||||
with open(os.path.join(self.data, "worker-prices.json"), "w") as f:
|
||||
json.dump({"models": {"model-one": {"cost_per_million_tokens": 2.0, "source": "test", "date": "2026-01-01"}}}, f)
|
||||
self.sisi("unit", "close")
|
||||
cost = self.archive("proj-2")["telemetry"]["cost"]
|
||||
self.assertEqual(cost["class"], "estimated")
|
||||
self.assertAlmostEqual(cost["value"], 0.003)
|
||||
|
||||
def finish_green_again(self):
|
||||
c = C.load(self.contract_path)
|
||||
c["checks"] = [real("again", "test -f done.txt")]
|
||||
C.save(self.contract_path, c)
|
||||
self.assertEqual(self.sisi("verify").returncode, 0)
|
||||
|
||||
def test_every_scorecard_value_carries_a_class_and_unavailable_has_no_value(self):
|
||||
"""R-13.1-1, R-13.4-1: the eight dimensions, each leaf classed; nothing missing reads as a number."""
|
||||
self.start()
|
||||
self.close_and_load()
|
||||
card = self.archive("proj")["scorecard"]
|
||||
self.assertEqual(list(card), list(SCORE_DIMENSIONS))
|
||||
for dim, vals in card.items():
|
||||
for k, v in vals.items():
|
||||
self.assertIn(v["class"], ("measured", "inferred", "estimated", "unavailable"), "%s.%s" % (dim, k))
|
||||
if v["class"] == "unavailable":
|
||||
self.assertIsNone(v["value"], "%s.%s" % (dim, k))
|
||||
|
||||
def test_first_useful_result_and_completion_time_are_recorded(self):
|
||||
"""R-6.1-2, R-6.1-3, R-6.1-4: time to first useful result (first green run) and to validated completion."""
|
||||
self.start()
|
||||
tel = self.close_and_load()
|
||||
self.assertEqual(tel["time_to_validated_completion_s"]["class"], "measured")
|
||||
self.assertEqual(tel["time_to_first_useful_result_s"]["class"], "inferred")
|
||||
self.assertEqual(tel["time_in_checks_s"]["class"], "measured")
|
||||
self.assertEqual(tel["files_changed"]["class"], "measured")
|
||||
|
||||
def test_a_window_start_does_not_turn_a_first_in_window_message_into_a_non_correction(self):
|
||||
"""R-6.1-2: only the transcript's own first message is exempt from correction counting."""
|
||||
tp = os.path.join(self.tmp, "w.jsonl")
|
||||
rows = [("2026-01-01T00:00:00Z", "start the work"), ("2026-01-01T01:00:00Z", "no, that is wrong")]
|
||||
with open(tp, "w") as f:
|
||||
for ts, text in rows:
|
||||
f.write(json.dumps({"type": "user", "timestamp": ts, "message": {"content": text}}) + "\n")
|
||||
st = harvest.transcript_stats(tp, harvest.epoch("2026-01-01T00:30:00Z"), harvest.epoch("2026-01-01T02:00:00Z"))
|
||||
self.assertEqual((st["human_messages"], st["corrections"]), (1, 1))
|
||||
self.assertIsNone(st["usage"])
|
||||
|
||||
|
||||
class StatusTests(UnitCase):
|
||||
def line(self, i, project="/w/alpha", risk="logic", model="m1", policy="p1", secs=10.0):
|
||||
return {"id": "u%d" % i, "project": project, "goal": "g%d" % i, "delivery_status": "delivered",
|
||||
"risk": {"declared": risk, "floor": risk}, "models": [model], "policy_version": policy,
|
||||
"started": "2026-01-01T00:00:00Z", "ended": "2026-01-0%dT00:00:00Z" % (1 + i % 8),
|
||||
"scorecard": {"delivery_velocity": {"time_to_validated_completion_s": {"value": secs, "class": "measured"}}}}
|
||||
|
||||
def test_trends_say_too_few_under_five_and_report_median_and_p95_from_five(self):
|
||||
"""R-13.1-1, R-13.5-1: trends by project, risk profile, model and policy version; no
|
||||
percentile from fewer than five units; no single score."""
|
||||
for i, s in enumerate((10, 20, 30, 40, 100)):
|
||||
store.append("units", self.line(i, secs=s))
|
||||
store.append("units", self.line(9, project="/w/beta", risk="data", model="m2", policy="p2"))
|
||||
out = self.sisi("status").stdout
|
||||
self.assertIn("by project", out)
|
||||
self.assertRegex(out, r"alpha\s+validated completion \(s\) 30/88 \(n=5\)")
|
||||
self.assertRegex(out, r"beta\s+too few")
|
||||
for kind in ("by risk profile", "by model", "by policy version"):
|
||||
self.assertIn(kind, out)
|
||||
self.assertRegex(out, r"data\s+too few")
|
||||
self.assertNotRegex(out.lower(), r"overall score|total score|score:")
|
||||
|
||||
def test_unavailable_is_not_counted_as_a_number_in_trends(self):
|
||||
"""R-13.4-2: a unit with no timing leaves the median to the units that have one."""
|
||||
rows = [self.line(i, secs=s) for i, s in enumerate((10, 20, 30, 40, 50))]
|
||||
rows[0]["scorecard"]["delivery_velocity"]["time_to_validated_completion_s"] = {"value": None, "class": "unavailable"}
|
||||
t = scorecard.trends(rows)["project"]["alpha"]["validated completion (s)"]
|
||||
self.assertEqual((t["n"], t["median"]), (4, 35.0))
|
||||
|
||||
def test_status_with_no_units_says_so(self):
|
||||
"""R-13.4-3."""
|
||||
out = self.sisi("status").stdout
|
||||
self.assertIn("no closed work units yet", out)
|
||||
|
||||
def test_status_json_and_learning_view(self):
|
||||
"""R-13.1-1: `--json` is machine-readable; `--learning` prints the learning report."""
|
||||
store.append("units", self.line(1))
|
||||
d = json.loads(self.sisi("status", "--json").stdout)
|
||||
self.assertEqual(d["units"][0]["id"], "u1")
|
||||
out = self.sisi("status", "--learning").stdout
|
||||
self.assertIn("Learning loop", out)
|
||||
|
||||
def test_retired_reporters_are_gone_and_the_new_commands_are_listed(self):
|
||||
"""R-13.1-1: `sisi time`, `sisi scoreboard` and `sisi improve` no longer exist."""
|
||||
helptext = self.sisi("help").stdout
|
||||
for gone in ("sisi time", "sisi scoreboard", "sisi improve", "sisi backlog"):
|
||||
self.assertNotIn(gone, helptext)
|
||||
for kept in ("sisi unit close", "sisi unit pause", "sisi unit show", "sisi status"):
|
||||
self.assertIn(kept, helptext)
|
||||
for cmd in ("time", "scoreboard"):
|
||||
self.assertNotEqual(self.sisi(cmd).returncode, 0, cmd)
|
||||
|
||||
def test_status_after_a_real_close_shows_the_unit_with_its_scorecard(self):
|
||||
"""R-13.1-1."""
|
||||
self.start()
|
||||
self.finish_green()
|
||||
self.sisi("unit", "close")
|
||||
out = self.sisi("status").stdout
|
||||
self.assertIn("[delivered]", out)
|
||||
for dim in SCORE_DIMENSIONS:
|
||||
self.assertIn(dim.replace("_", " "), out)
|
||||
|
||||
def test_retro_with_no_arguments_prints_the_units_scorecard_and_what_stands_out(self):
|
||||
"""R-10.3-7: `sisi retro` is grounded in the record."""
|
||||
self.start()
|
||||
self.sisi("verify") # fails: done.txt missing
|
||||
open(os.path.join(self.proj, "done.txt"), "w").write("x")
|
||||
self.sisi("verify")
|
||||
out = self.sisi("retro").stdout
|
||||
self.assertIn("what stands out", out)
|
||||
self.assertIn("failed and then passed", out)
|
||||
self.assertIn("correctness and assurance", out)
|
||||
|
||||
|
||||
class PruneTests(SisiCase):
|
||||
def make(self, name, age_days, transcript_age_days=None):
|
||||
p = os.path.join(self.state, "sessions", name + ".json")
|
||||
os.makedirs(os.path.dirname(p), exist_ok=True)
|
||||
rec = {"cwd": self.proj}
|
||||
if transcript_age_days is not None:
|
||||
tp = os.path.join(self.tmp, name + ".jsonl")
|
||||
open(tp, "w").write("{}\n")
|
||||
os.utime(tp, (time.time() - transcript_age_days * 86400,) * 2)
|
||||
rec["transcript_path"] = tp
|
||||
with open(p, "w") as f:
|
||||
json.dump(rec, f)
|
||||
os.utime(p, (time.time() - age_days * 86400,) * 2)
|
||||
return p
|
||||
|
||||
def test_old_sessions_go_on_the_first_start_and_every_nth_after(self):
|
||||
"""R-12.1-1: pruning runs from a counter at session start, with no scheduler."""
|
||||
old, new = self.make("old", 60), self.make("new", 1)
|
||||
self.assertEqual(store.prune_sessions(), 1) # the first start sweeps
|
||||
self.assertFalse(os.path.exists(old))
|
||||
self.assertTrue(os.path.exists(new))
|
||||
again = self.make("old2", 60)
|
||||
for _ in range(store.DEFAULTS["scratch"]["prune_every"] - 1):
|
||||
self.assertEqual(store.prune_sessions(), 0) # not their turn
|
||||
self.assertTrue(os.path.exists(again))
|
||||
self.assertEqual(store.prune_sessions(), 1) # the Nth start
|
||||
self.assertFalse(os.path.exists(again))
|
||||
|
||||
def test_the_age_is_configurable(self):
|
||||
"""R-12.1-1."""
|
||||
keep = self.make("a", 10)
|
||||
os.makedirs(os.path.join(self.home, ".config", "sisi"), exist_ok=True)
|
||||
with mock.patch.object(store, "config", return_value=dict(store.DEFAULTS, scratch=dict(store.DEFAULTS["scratch"], session_max_age_days=5))):
|
||||
self.assertEqual(store.prune_sessions(), 1)
|
||||
self.assertFalse(os.path.exists(keep))
|
||||
|
||||
def test_an_active_session_is_never_touched(self):
|
||||
"""R-12.1-1: the named current session, and one whose transcript the host is still writing."""
|
||||
cur, live, dead = self.make("cur", 90), self.make("live", 90, transcript_age_days=1), self.make("dead", 90, transcript_age_days=90)
|
||||
self.assertEqual(store.prune_sessions(active_ids=("cur",)), 1)
|
||||
self.assertTrue(os.path.exists(cur) and os.path.exists(live))
|
||||
self.assertFalse(os.path.exists(dead))
|
||||
|
||||
def test_a_run_is_bounded_and_the_next_start_continues(self):
|
||||
"""R-12.1-1: at most prune_max_delete files per run; the sweep resumes at the next start."""
|
||||
paths_ = [self.make("s%d" % i, 90) for i in range(7)]
|
||||
cfg = dict(store.DEFAULTS, scratch=dict(store.DEFAULTS["scratch"], prune_max_delete=3))
|
||||
with mock.patch.object(store, "config", return_value=cfg):
|
||||
self.assertEqual(store.prune_sessions(), 3)
|
||||
self.assertEqual(store.prune_sessions(), 3) # the very next start goes on
|
||||
self.assertEqual(store.prune_sessions(), 1)
|
||||
self.assertEqual([p for p in paths_ if os.path.exists(p)], [])
|
||||
|
||||
def test_the_time_budget_bounds_a_run(self):
|
||||
"""R-12.1-1."""
|
||||
for i in range(5):
|
||||
self.make("t%d" % i, 90)
|
||||
cfg = dict(store.DEFAULTS, scratch=dict(store.DEFAULTS["scratch"], prune_budget_s=0.0))
|
||||
with mock.patch.object(store, "config", return_value=cfg):
|
||||
self.assertEqual(store.prune_sessions(), 0)
|
||||
|
||||
def test_session_start_runs_the_prune(self):
|
||||
"""R-12.1-1: `sisi tick` prunes during normal use."""
|
||||
old = self.make("ancient", 90)
|
||||
self.sisi("tick", stdin=json.dumps({"session_id": "now1", "cwd": self.proj, "hook_event_name": "SessionStart"}))
|
||||
self.assertFalse(os.path.exists(old))
|
||||
self.assertTrue(os.path.exists(os.path.join(self.state, "sessions", "now1.json")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,338 @@
|
||||
"""The verification record (SPEC 5.4-5.6, 6.4): five statuses, refusal of checks that cannot fail,
|
||||
selection by change, reuse on identical inputs, and the rule that nothing that did not run is
|
||||
ever reported, stored or summarised as passed."""
|
||||
import json, os, random, subprocess, unittest
|
||||
from support import *
|
||||
from sisi import contract as C, unit as U, verify as V
|
||||
|
||||
|
||||
def real(id, cmd, **kw):
|
||||
return dict(check(id, cmd), **kw)
|
||||
|
||||
|
||||
class VacuousChecksTests(SisiCase):
|
||||
def refused(self, cmd, **extra):
|
||||
c = contract([dict({"id": "v", "claim": "c", "kind": "general", "rung": 3, "cmd": cmd,
|
||||
"expect": {"exit": 0}}, **extra)])
|
||||
return [e for e in C.validate(c) if "vacuous" in e]
|
||||
|
||||
def test_vacuous_variants_are_refused(self):
|
||||
"""R-5.6-3: a check that cannot fail shows nothing, so a contract holding one is invalid."""
|
||||
for cmd in ("true", ":", "exit 0", "echo ok", "test 1 = 1", "sh -c true", "bash -c 'exit 0'",
|
||||
"[ 1 = 1 ]", "/bin/true", "true && true", "pytest || true", "make test || :",
|
||||
"exit 0 # fine", "env true", "echo ok;", "sh -c \"sh -c ':'\"", "printf done"):
|
||||
self.assertTrue(self.refused(cmd), cmd)
|
||||
|
||||
def test_commands_that_can_fail_are_accepted(self):
|
||||
for cmd in ("false", "test -f x", "pytest", "grep -q x f", "echo ok > out.txt", "true && false",
|
||||
"sh -c 'test -f x'", "test 1 = 2", "python3 -c 'print(1)'", "cd src && make"):
|
||||
self.assertFalse(self.refused(cmd), cmd)
|
||||
self.assertFalse(self.refused("echo hi", expect={"exit": 0, "stdout_contains": "zzz"}))
|
||||
|
||||
def test_an_echo_that_already_prints_what_it_expects_is_vacuous(self):
|
||||
"""R-5.6-3: stdout_contains against a literal echo is a constant."""
|
||||
self.assertTrue(self.refused("echo hello", expect={"exit": 0, "stdout_contains": "hello"}))
|
||||
|
||||
def test_vacuous_check_blocks_contract_check_and_verify(self):
|
||||
"""R-5.6-3: `sisi contract check` and `sisi verify` both refuse, and nothing is recorded as passed."""
|
||||
self.write_contract(contract([dict(check("v", "x"), cmd="sh -c true")]))
|
||||
self.assertEqual(self.sisi("contract", "check").returncode, 1)
|
||||
r = self.sisi("verify")
|
||||
self.assertEqual(r.returncode, 1)
|
||||
self.assertIn("vacuous", r.stdout)
|
||||
self.assertEqual([x for x in self.results() if x["status"] == "passed"], [])
|
||||
|
||||
def test_invariants_are_held_to_the_same_rule(self):
|
||||
"""R-5.6-3."""
|
||||
c = contract([real("ok", "test -f .sisi/contract.json")], [real("inv", "exit 0")])
|
||||
self.assertTrue([e for e in C.validate(c) if "vacuous" in e])
|
||||
|
||||
|
||||
class NewFieldsTests(SisiCase):
|
||||
def test_new_optional_fields_validate(self):
|
||||
"""R-10.2-1: the unit's optional fields are validated by `sisi contract check`."""
|
||||
c = contract([real("a", "test -f x", covers="x missing", when_changed=["src/**"], mandatory=True,
|
||||
independent=False)])
|
||||
c.update(parent="req-1", scope={"in": ["a"], "out": ["b"]}, deferred=[{"what": "w", "why": "y", "safe_because": "s"}],
|
||||
limitations=["l"], plan=["step"], risk={"profile": "logic", "why": "money"},
|
||||
requirements=[{"id": "q1", "kind": "explicit", "text": "t", "decision": "now", "check": "a"}],
|
||||
approach={"chosen": "c", "why": "w", "alternatives": ["x"], "reuses": ["y"]})
|
||||
self.assertEqual(C.validate(c), [])
|
||||
bad = json.loads(json.dumps(c))
|
||||
bad["checks"][0]["when_changed"] = []
|
||||
bad["checks"][0]["mandatory"] = "yes"
|
||||
bad["risk"]["profile"] = "huge"
|
||||
bad["requirements"][0]["check"] = "nope"
|
||||
bad["approach"] = {"chosen": 3, "bogus": 1}
|
||||
bad["scope"] = {"in": "x"}
|
||||
errs = " | ".join(C.validate(bad))
|
||||
for part in ("when_changed", "mandatory", "risk.profile", "nope", "approach.chosen", "unknown key 'bogus'", "scope.in"):
|
||||
self.assertIn(part, errs)
|
||||
|
||||
|
||||
class SelectionAndReuseTests(SisiCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.git_init()
|
||||
|
||||
def run_verify(self, c):
|
||||
self.write_contract(c)
|
||||
return self.sisi("verify", "--json")
|
||||
|
||||
def rows(self, r):
|
||||
return {x["id"]: x for x in json.loads(r.stdout)["results"]}
|
||||
|
||||
def test_when_changed_selects_by_change(self):
|
||||
"""R-6.4-1, R-6.4-2, R-5.4-10: a check runs only when a changed path matches; otherwise it is
|
||||
skipped with the reason recorded, never reported as passed."""
|
||||
os.makedirs(os.path.join(self.proj, "src"))
|
||||
open(os.path.join(self.proj, "src", "a.py"), "w").write("x=1\n")
|
||||
subprocess.run(["git", "add", "-A"], cwd=self.proj, check=True)
|
||||
subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "src"], cwd=self.proj, check=True)
|
||||
c = contract([real("src", "test -f src/a.py", when_changed=["src/**"]), real("always", "test -f src/a.py")])
|
||||
rows = self.rows(self.run_verify(c))
|
||||
self.assertEqual(rows["src"]["status"], "skipped")
|
||||
self.assertIn("not affected", rows["src"]["reason"])
|
||||
self.assertEqual(rows["always"]["status"], "passed")
|
||||
open(os.path.join(self.proj, "src", "a.py"), "w").write("x=2\n")
|
||||
rows = self.rows(self.sisi("verify", "--json"))
|
||||
self.assertEqual(rows["src"]["status"], "passed")
|
||||
|
||||
def test_a_watch_that_matches_nothing_is_not_applicable_and_not_a_pass(self):
|
||||
"""R-5.6-2: not-applicable is its own status with a reason."""
|
||||
c = contract([real("w", "test -f nope", when_changed=["nothing/**"]), real("a", "test -f .sisi/contract.json")])
|
||||
rows = self.rows(self.run_verify(c))
|
||||
self.assertEqual(rows["w"]["status"], "not-applicable")
|
||||
self.assertIn("no file matches", rows["w"]["reason"])
|
||||
|
||||
def test_all_runs_everything(self):
|
||||
"""R-6.4-3: --all overrides selection."""
|
||||
os.makedirs(os.path.join(self.proj, "src"))
|
||||
open(os.path.join(self.proj, "src", "a.py"), "w").write("x=1\n")
|
||||
c = contract([real("src", "test -f src/a.py", when_changed=["src/**"])])
|
||||
self.git_commit_all()
|
||||
self.write_contract(c)
|
||||
self.assertEqual(self.rows(self.sisi("verify", "--json"))["src"]["status"], "skipped")
|
||||
self.assertEqual(self.rows(self.sisi("verify", "--all", "--json"))["src"]["status"], "passed")
|
||||
|
||||
def git_commit_all(self):
|
||||
subprocess.run(["git", "add", "-A"], cwd=self.proj, check=True)
|
||||
subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "x"], cwd=self.proj, check=True)
|
||||
|
||||
def test_reuse_on_identical_inputs_and_not_after_a_change(self):
|
||||
"""R-5.4-9: an identical tree reuses the last pass (skipped, never passed); any change to
|
||||
the tree, or to the check's command, runs it again."""
|
||||
open(os.path.join(self.proj, "f.txt"), "w").write("1")
|
||||
c = contract([real("a", "test -f f.txt")])
|
||||
first = self.rows(self.run_verify(c))["a"]
|
||||
self.assertEqual(first["status"], "passed")
|
||||
again = self.rows(self.sisi("verify", "--json"))["a"]
|
||||
self.assertEqual(again["status"], "skipped")
|
||||
self.assertTrue(again["reason"].startswith("reused: passed at"))
|
||||
open(os.path.join(self.proj, "f.txt"), "w").write("22")
|
||||
self.assertEqual(self.rows(self.sisi("verify", "--json"))["a"]["status"], "passed")
|
||||
c["checks"][0]["cmd"] = "test -f f.txt && test -s f.txt"
|
||||
self.assertEqual(self.rows(self.run_verify(c))["a"]["status"], "passed") # new command: not reused
|
||||
|
||||
def test_a_change_inside_when_changed_is_never_hidden_by_reuse(self):
|
||||
"""R-5.4-9: reuse keys on the contents of the watched files."""
|
||||
open(os.path.join(self.proj, "w.txt"), "w").write("good")
|
||||
c = contract([real("w", "grep -q good w.txt", when_changed=["w.txt"])])
|
||||
self.assertEqual(self.rows(self.run_verify(c))["w"]["status"], "passed")
|
||||
open(os.path.join(self.proj, "w.txt"), "w").write("bad")
|
||||
r = self.sisi("verify", "--json")
|
||||
self.assertEqual(self.rows(r)["w"]["status"], "failed")
|
||||
self.assertEqual(r.returncode, 1)
|
||||
|
||||
def test_a_failure_is_never_reused(self):
|
||||
"""R-5.6-3."""
|
||||
c = contract([real("f", "test -f missing.txt")])
|
||||
self.assertEqual(self.rows(self.run_verify(c))["f"]["status"], "failed")
|
||||
self.assertEqual(self.rows(self.sisi("verify", "--json"))["f"]["status"], "failed")
|
||||
|
||||
def test_seen_failing_duration_and_time_saved_are_recorded(self):
|
||||
"""R-5.4-7, R-5.6-1: every result stores its duration and whether the check was seen failing;
|
||||
`sisi verify` prints the slowest checks and the time saved."""
|
||||
c = contract([real("a", "test -f f.txt")])
|
||||
self.write_contract(c)
|
||||
self.sisi("verify") # fails: f.txt missing
|
||||
open(os.path.join(self.proj, "f.txt"), "w").write("1")
|
||||
r = self.sisi("verify", "--json")
|
||||
a = self.rows(r)["a"]
|
||||
self.assertTrue(a["seen_failing"])
|
||||
self.assertIn("duration_s", a)
|
||||
r = self.sisi("verify")
|
||||
self.assertIn("time saved by selection and reuse", r.stdout)
|
||||
self.assertIn("slowest checks", self.sisi("verify", "--all").stdout)
|
||||
self.assertTrue(all("duration_s" in x for x in self.results()))
|
||||
|
||||
def test_text_output_never_presents_a_skip_as_a_pass(self):
|
||||
"""R-5.6-3."""
|
||||
c = contract([real("w", "test -f nope", when_changed=["nothing/**"])])
|
||||
self.write_contract(c)
|
||||
out = self.sisi("verify").stdout
|
||||
self.assertIn("did not run (not a pass)", out)
|
||||
self.assertIn("no evidence yet", out)
|
||||
|
||||
|
||||
class WaiverTests(SisiCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.git_init()
|
||||
|
||||
def test_a_waiver_turns_nothing_green_and_the_unit_reads_deviation_everywhere(self):
|
||||
"""R-10.3-3, R-13.4-2: a waiver records a deviation; the failing check still fails, and the
|
||||
unit's status is `deviation` in the record, the summary line, the status report and the list."""
|
||||
self.write_contract(contract([real("f", "test -f missing.txt")]))
|
||||
self.assertEqual(self.sisi("verify").returncode, 1)
|
||||
self.assertEqual(self.sisi("contract", "waive", "--check", "f", "not worth it").returncode, 0)
|
||||
self.assertEqual(self.sisi("verify").returncode, 1) # still failing
|
||||
r = self.sisi("unit", "close")
|
||||
self.assertEqual(r.returncode, 0, r.stdout)
|
||||
self.assertIn("deviation", r.stdout)
|
||||
self.assertIn("must say so", r.stdout)
|
||||
rec = json.loads(self.sisi("unit", "show", "t").stdout)
|
||||
self.assertEqual(rec["delivery_status"], "deviation")
|
||||
self.assertEqual(rec["deviations"][0]["reason"], "not worth it")
|
||||
self.assertTrue(rec["scorecard"]["correctness_and_assurance"]["deviation"]["value"])
|
||||
self.assertEqual([r["delivery_status"] for r in store_units()], ["deviation"])
|
||||
self.assertIn("[deviation]", self.sisi("status").stdout)
|
||||
self.assertIn("deviation", self.sisi("unit", "list").stdout)
|
||||
self.assertNotIn("delivered", rec["delivery_status"])
|
||||
|
||||
|
||||
def store_units():
|
||||
from sisi import store
|
||||
return store.read("units")
|
||||
|
||||
|
||||
class NeverRunMandatoryTests(SisiCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.git_init()
|
||||
|
||||
def test_a_mandatory_check_that_never_ran_cannot_close_as_delivered(self):
|
||||
"""R-5.6-3, R-6.4-3: a `when_changed` glob that matches nothing used to skip a mandatory check
|
||||
for ever must not let the unit close as plainly delivered."""
|
||||
self.write_contract(contract([real("real", "test -f .sisi/contract.json"),
|
||||
real("ghost", "test -f nope", when_changed=["nothing/**"])]))
|
||||
r = self.sisi("verify")
|
||||
self.assertEqual(r.returncode, 0) # not blocking...
|
||||
self.assertIn("no evidence yet", r.stdout) # ...but said plainly
|
||||
self.sisi("unit", "close")
|
||||
rec = json.loads(self.sisi("unit", "show", "t").stdout)
|
||||
self.assertEqual(rec["delivery_status"], "delivered-with-deferred")
|
||||
self.assertEqual([x["id"] for x in rec["never_run_mandatory"]], ["ghost"])
|
||||
|
||||
def test_an_optional_check_may_be_kept_back(self):
|
||||
"""R-6.4-3: optional confidence-building checks are distinguished from mandatory ones."""
|
||||
self.write_contract(contract([real("real", "test -f .sisi/contract.json"),
|
||||
real("opt", "test -f nope", when_changed=["nothing/**"], mandatory=False)]))
|
||||
self.sisi("verify")
|
||||
self.sisi("unit", "close")
|
||||
self.assertEqual(json.loads(self.sisi("unit", "show", "t").stdout)["delivery_status"], "delivered")
|
||||
|
||||
def test_a_mandatory_check_not_affected_by_the_change_is_a_justified_skip(self):
|
||||
"""R-5.4-10: selection with the reason recorded is the documented justification."""
|
||||
os.makedirs(os.path.join(self.proj, "src"))
|
||||
open(os.path.join(self.proj, "src", "a"), "w").write("1")
|
||||
subprocess.run(["git", "add", "-A"], cwd=self.proj, check=True)
|
||||
subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "s"], cwd=self.proj, check=True)
|
||||
open(os.path.join(self.proj, "other.txt"), "w").write("1")
|
||||
self.write_contract(contract([real("real", "test -f other.txt"), real("src", "test -f src/a", when_changed=["src/**"])]))
|
||||
self.sisi("verify")
|
||||
self.sisi("unit", "close")
|
||||
self.assertEqual(json.loads(self.sisi("unit", "show", "t").stdout)["delivery_status"], "delivered")
|
||||
|
||||
|
||||
class NothingUnrunIsEverPassedTests(SisiCase):
|
||||
"""A seeded property test: random tree changes, verify runs and closes, against an oracle that
|
||||
knows, from an external log, which commands actually ran. Nothing that did not run to a pass on a
|
||||
tree where it holds may appear as passed in any result, in the stored rows or in the scorecard."""
|
||||
|
||||
FILES = ("f_a", "f_b", "f_b2", "f_c", "f_d", "g")
|
||||
|
||||
def build(self, n):
|
||||
d = os.path.join(self.tmp, "p%d" % n)
|
||||
os.makedirs(d)
|
||||
self.git_init(d)
|
||||
log = os.path.join(self.tmp, "log%d" % n)
|
||||
|
||||
def mk(id, f, wc, mandatory):
|
||||
x = {"id": id, "claim": "claim " + id, "kind": "general", "rung": 3, "timeout_s": 10,
|
||||
"cmd": "echo %s >> %s; test -f %s" % (id, log, f), "expect": {"exit": 0}, "mandatory": mandatory}
|
||||
if wc:
|
||||
x["when_changed"] = wc
|
||||
return x
|
||||
c = {"id": "t", "goal": "g", "assumptions": [], "questions": [], "invariants": [],
|
||||
"checks": [mk("a", "f_a", None, True), mk("b", "f_b", ["f_b*"], True),
|
||||
mk("c", "f_c", ["nothing/**"], True), mk("d", "f_d", None, False)]}
|
||||
self.write_contract(c, d)
|
||||
return d, log, {"a": "f_a", "b": "f_b", "c": "f_c", "d": "f_d"}
|
||||
|
||||
def logged(self, log):
|
||||
try:
|
||||
return open(log).read().split()
|
||||
except OSError:
|
||||
return []
|
||||
|
||||
def run_sequence(self, rng, n):
|
||||
d, log, file_of = self.build(n)
|
||||
steps = rng.choice([8, 12, 16])
|
||||
for _ in range(steps):
|
||||
op = rng.choice(["touch", "touch", "rm", "verify", "verify", "all", "close"])
|
||||
if op in ("touch", "rm"):
|
||||
p = os.path.join(d, rng.choice(self.FILES))
|
||||
if op == "touch":
|
||||
open(p, "w").write(str(rng.random()))
|
||||
elif os.path.exists(p):
|
||||
os.remove(p)
|
||||
continue
|
||||
if op in ("verify", "all"):
|
||||
before_rows, before_log = len(self.results()), len(self.logged(log))
|
||||
r = self.sisi("verify", *(["--all"] if op == "all" else []), "--json", cwd=d)
|
||||
ran = set(self.logged(log)[before_log:])
|
||||
new = [x for x in self.results()[before_rows:] if x.get("item") == "check"]
|
||||
self.assertEqual({x["check"] for x in new}, set(file_of))
|
||||
for x in new:
|
||||
exists = os.path.exists(os.path.join(d, file_of[x["check"]]))
|
||||
if x["status"] == "passed":
|
||||
self.assertIn(x["check"], ran, "passed without running")
|
||||
self.assertTrue(exists, "passed where the check cannot hold")
|
||||
if x["check"] in ran:
|
||||
self.assertEqual(x["status"], "passed" if exists else "failed")
|
||||
else:
|
||||
self.assertIn(x["status"], ("skipped", "not-applicable", "not-run"))
|
||||
if str(x.get("reason")).startswith("reused"):
|
||||
self.assertTrue(exists, "reused a pass on a tree where the check fails")
|
||||
self.assertTrue(x.get("reason"), "a status other than passed or failed carries a reason")
|
||||
self.assertEqual(r.returncode == 0, not any(x["status"] == "failed" for x in new))
|
||||
else:
|
||||
r = self.sisi("unit", "close", cwd=d)
|
||||
path = os.path.join(d, ".sisi", "units", "t.json")
|
||||
if r.returncode == 0:
|
||||
rec = json.load(open(path))
|
||||
latest = {x["id"]: x for x in rec["final_verification"]}
|
||||
passed_now = sorted(i for i, x in latest.items() if x["status"] == "passed")
|
||||
card = rec["scorecard"]["correctness_and_assurance"]["results_by_status"]["value"]
|
||||
self.assertEqual(card.get("passed", 0), len(passed_now))
|
||||
if rec["delivery_status"] == "delivered":
|
||||
for cid in ("a", "b", "c"): # mandatory checks
|
||||
ever = any(x["check"] == cid and x["status"] == "passed" for x in rec["verification"])
|
||||
just = latest[cid]["status"] == "skipped" and "not affected" in (latest[cid]["reason"] or "")
|
||||
self.assertTrue(ever or just, "delivered with %s never run to a pass" % cid)
|
||||
self.assertNotEqual(latest[cid]["status"], "failed")
|
||||
return
|
||||
else:
|
||||
self.assertFalse(os.path.exists(path))
|
||||
|
||||
def test_property_over_generated_sequences(self):
|
||||
"""R-5.6-2, R-5.6-3: nothing that did not run is ever reported, stored or summarised as passed."""
|
||||
seeds = [1, random.randrange(2 ** 32)] + [random.randrange(2 ** 32) for _ in range(ITERS)]
|
||||
for n, seed in enumerate(seeds):
|
||||
with self.subTest(seed=seed):
|
||||
self.run_sequence(random.Random(seed), n)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -311,15 +311,6 @@ class LintCrossCheckTests(LibCase):
|
||||
self.assertIn("short.md: 30" if False else "verified: memory", r.stdout)
|
||||
self.assertIn("warning(s)", r.stdout)
|
||||
|
||||
def test_status_shows_library_size(self):
|
||||
self.put("prove/a.md", vfile("a", extra="verified: ran\n"))
|
||||
self.put("prove/b.md", vfile("b", extra="verified: memory\n"))
|
||||
self.put("prove/c.md", vfile("c", extra="verified: docs\n"))
|
||||
self.put("prove/d.md", vfile("d", extra="verified: docs\n"))
|
||||
self.put("r.md", dfile("r"), self.ddir)
|
||||
out = self.sisi("status").stdout
|
||||
self.assertIn("4 verifiers (verified ran 1, docs 2, memory 1), 1 recipes", out)
|
||||
|
||||
|
||||
class CoreMdTests(SisiCase):
|
||||
def test_help_lists_new_commands(self):
|
||||
|
||||
+10
-5
@@ -11,16 +11,21 @@ class VerifyTests(SisiCase):
|
||||
return {r["check"]: r["status"] for r in self.results()}
|
||||
|
||||
def test_statuses(self):
|
||||
# R-5.6-2, R-5.6-3: five statuses only. A timeout is not-run (timed out) and a
|
||||
# human check is not-run (needs human): neither is ever reported as passed.
|
||||
c = contract([check("ok", "true"), check("bad", "exit 3"), check("slow", "sleep 5", timeout_s=0.3),
|
||||
check("out", "echo hello", stdout_contains="hello"), check("out2", "echo hi", stdout_contains="zzz"),
|
||||
check("out", "python3 -c \"print('hello')\"", stdout_contains="hello"), check("out2", "echo hi", stdout_contains="zzz"),
|
||||
check("code", "exit 3", exit=3), human("h5", 5), human("h6", 6)],
|
||||
[check("inv", "true")])
|
||||
r = self.run_verify(c)
|
||||
self.assertEqual(r.returncode, 1)
|
||||
self.assertEqual(self.statuses(), {"ok": "passed", "bad": "failed", "slow": "timeout", "out": "passed",
|
||||
"out2": "failed", "code": "passed", "h5": "awaiting-human",
|
||||
"h6": "awaiting-human", "inv": "passed"})
|
||||
self.assertIn("awaiting-human", r.stdout)
|
||||
self.assertEqual(self.statuses(), {"ok": "passed", "bad": "failed", "slow": "not-run", "out": "passed",
|
||||
"out2": "failed", "code": "passed", "h5": "not-run",
|
||||
"h6": "not-run", "inv": "passed"})
|
||||
self.assertIn("not-run (needs human)", r.stdout)
|
||||
self.assertIn("not-run (timed out)", r.stdout)
|
||||
reasons = {x["check"]: x.get("reason") for x in self.results()}
|
||||
self.assertEqual((reasons["slow"], reasons["h5"]), ("timed out", "needs human"))
|
||||
|
||||
def test_all_good_exit_zero_and_record_fields(self):
|
||||
r = self.run_verify(contract([check("ok", "true"), human("h")]))
|
||||
|
||||
Reference in new issue
Block a user