From f510cc08de9f030f70d005f9c2db4588ea6aed51 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 23 Nov 2026 13:06:27 -0700 Subject: [PATCH] Open units whose contract predates the unit record get an age from the contract file's mtime, labelled inferred in status and record. done / next: repeat runs and report --- sisi/scorecard.py | 12 +++++++++--- sisi/unit.py | 31 ++++++++++++++++++++++++------- tests/test_unit.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 10 deletions(-) diff --git a/sisi/scorecard.py b/sisi/scorecard.py index fc59b13..82a4bee 100644 --- a/sisi/scorecard.py +++ b/sisi/scorecard.py @@ -369,7 +369,9 @@ def open_units(cwd=None): if not isinstance(c, dict) or not c.get("id") or c.get("status") == "closed" or C.is_unedited_skeleton(c): return out project = C.project_dir(p) - row = {"id": c["id"], "status": c.get("status", "open"), "age": _age(c.get("started")), "goal": c.get("goal") or ""} + started, basis = U.start_of(c, p) + row = {"id": c["id"], "status": c.get("status", "open"), "age": _age(started), "goal": c.get("goal") or "", + "age_basis": basis} if row["status"] == "paused": pz = next((x for x in reversed(c.get("pauses") or []) if isinstance(x, dict) and not x.get("until")), {}) row["waiting_for"], row["paused_for"] = pz.get("what"), _age(pz.get("since")) @@ -394,13 +396,17 @@ def _lowered_note(u): (": " + str(rk["why"])) if rk.get("why") else "") +def _basis_note(r): + return " (inferred from the contract file)" if r.get("age_basis") == "inferred" else "" + + def format_open(rows): lines = [] for r in rows: if r["status"] == "paused": - lines.append("paused %-24s age %s waiting for: %s (for %s)" % (r["id"], r["age"], r.get("waiting_for") or "?", r.get("paused_for"))) + lines.append("paused %-24s age %s%s waiting for: %s (for %s)" % (r["id"], r["age"], _basis_note(r), r.get("waiting_for") or "?", r.get("paused_for"))) else: - lines.append("open %-24s age %s %s" % (r["id"], r["age"], r["goal"][:80])) + lines.append("open %-24s age %s%s %s" % (r["id"], r["age"], _basis_note(r), r["goal"][:80])) if r.get("lowered"): lines.append(" lowered: " + r["lowered"]) if r.get("unverified"): diff --git a/sisi/unit.py b/sisi/unit.py index a90765d..affbe1e 100644 --- a/sisi/unit.py +++ b/sisi/unit.py @@ -219,11 +219,26 @@ def _transcript_stats(project, t0, t1): return agg -def build_telemetry(c, project, results, t0, t1): +def start_of(c, contract_path=None): + """(ISO UTC start, basis). A unit records `started` itself ("measured"). A contract written before the + unit record existed has none: the contract file's modification time stands in for it, and the basis + says "inferred" so nothing downstream reads it as measured. (None, None) when neither is known.""" + if isinstance(c.get("started"), str) and c["started"]: + return c["started"], "measured" + try: + mt = os.stat(contract_path).st_mtime if contract_path else None + except OSError: + mt = None + if mt is None: + return None, None + return time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(mt)) + "Z", "inferred" + + +def build_telemetry(c, project, results, t0, t1, t0_basis="measured"): """Every value classed. Sources: deliveries, results, pauses on the contract, the host transcript where a session record names one, git, and the delegation ledger.""" tel = {} - tel["time_to_validated_completion_s"] = val(round(t1 - t0, 1), "measured") if t0 is not None else unavailable() + tel["time_to_validated_completion_s"] = val(round(t1 - t0, 1), t0_basis) if t0 is not None else unavailable() if results: tel["time_in_checks_s"] = val(round(sum(float(r["duration_s"]) for r in results if isinstance(r.get("duration_s"), (int, float))), 3), "measured") @@ -456,10 +471,11 @@ def decide_status(c, rows, abandon=None): return "delivered", block, unrun -def build_record(c, project, delivery_status, ended_epoch, abandon_reason=None, rows=None): - t0 = harvest.epoch(c["started"]) if isinstance(c.get("started"), str) else None +def build_record(c, project, delivery_status, ended_epoch, abandon_reason=None, rows=None, contract_path=None): + started, basis = start_of(c, contract_path) + t0 = harvest.epoch(started) if isinstance(started, str) else None rows = _unit_results(c, project, c.get("started")) if rows is None else rows - tel = build_telemetry(c, project, rows, t0, ended_epoch) + tel = build_telemetry(c, project, rows, t0, ended_epoch, basis or "measured") models = tel.pop("_models") delegation_rows = tel.pop("_delegation_rows") changed = tel.pop("_changed") @@ -488,7 +504,7 @@ def build_record(c, project, delivery_status, ended_epoch, abandon_reason=None, "plan": c.get("plan") or [], "workers": delegation_rows, "models": models, "budgets": c.get("budgets"), - "started": c.get("started"), "ended": ended, "closed_at": ended, "telemetry": tel, + "started": started, "started_basis": basis, "ended": ended, "closed_at": ended, "telemetry": tel, "start_commit": c.get("base_head"), "closing_commit": gitutil.head(project) if gitutil.root(project) else None, "summary": c.get("summary"), @@ -603,7 +619,8 @@ def close_unit(c, contract_path, project, abandon=None, now=None): return None, ("sisi: unit %s cannot close as delivered: %s. Fix and re-run `sisi verify`, " "record a deviation with `sisi contract waive \"\"`, or close it with " "`sisi unit close --abandon \"\"`." % (c["id"], bad)), False - rec = build_record(c, project, status, ended_epoch, abandon_reason=abandon, rows=rows) + rec = build_record(c, project, status, ended_epoch, abandon_reason=abandon, rows=rows, + contract_path=contract_path) _, created = archive_record(rec, project) if created: _capture_case(rec) diff --git a/tests/test_unit.py b/tests/test_unit.py index e9a0c2b..22ae176 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -41,6 +41,40 @@ class UnitCase(SisiCase): subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", msg], cwd=self.proj, check=True) +class OldContractAgeTests(UnitCase): + def old_contract(self, hours=3): + c = self.start() + c.pop("started", None) + C.save(self.contract_path, c) + t = time.time() - hours * 3600 + os.utime(self.contract_path, (t, t)) + + def test_status_gives_an_age_to_a_contract_written_before_the_unit_record_existed(self): + """The contract file's modification time stands in for the start, and says it is inferred.""" + self.old_contract(3) + r = self.sisi("status") + line = next(l for l in r.stdout.splitlines() if l.strip().startswith("open")) + self.assertNotIn("age ?", line) + self.assertIn("age 3h", line) + self.assertIn("inferred", line) + + def test_a_measured_start_is_not_labelled_inferred(self): + self.start() + line = next(l for l in self.sisi("status").stdout.splitlines() if l.strip().startswith("open")) + self.assertNotIn("inferred", line) + self.assertNotIn("age ?", line) + + def test_the_record_of_such_a_unit_says_inferred_never_measured(self): + self.old_contract(2) + self.finish_green() + self.assertEqual(self.sisi("unit", "close").returncode, 0) + rec = self.archive("proj") + self.assertEqual(rec["started_basis"], "inferred") + t = rec["telemetry"]["time_to_validated_completion_s"] + self.assertEqual(t.get("class") or t.get("basis") or t.get("kind"), "inferred", t) + self.assertGreaterEqual(t["value"], 2 * 3600 - 120) + + 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,