diff --git a/scripts/change_ledger.py b/scripts/change_ledger.py new file mode 100644 index 0000000000..cef81cf99a --- /dev/null +++ b/scripts/change_ledger.py @@ -0,0 +1,230 @@ +#!/usr/bin/env python3 +"""Check the pool change-event ledger against config changes (#1334 / #1339). + +The steward spec (#1339, child of epic #1334) says the change-event +ledger is append-only and every decision is an event — timestamp, pool, +the change (PR link or proposal), rationale, expected effect, the +pool's window, and a quota snapshot before — and that "a config change +with no ledger event ... is a defect". This script is the check the +meta-reviewer (#1340) runs: given a ledger file and a list of config +changes in a window, it reports which changes have no matching event +and which events are malformed or incomplete. + +Ledger format: JSONL, one event per line, fields per #1339: + timestamp (ISO-8601), pool, change (PR link, "PR #N", or "commit "), + rationale, expected_effect, window, before_snapshot (non-empty object). + outcome (measured follow-up) is optional and appended later. + +Changes file: JSON produced from the canonical config's history: + {"config": ".opencode/opencode.jsonc", + "window": {"since": "2026-10-27", "until": "2026-11-23"}, + "changes": [{"pr": 13838, "sha": "...", "date": "...", "title": "..."}]} + One entry per merged PR touching the config in the window (commits + grouped by PR via GET /repos/{owner}/{repo}/commits/{sha}/pulls); + a direct-to-main commit is its own entry with "pr": null and its sha. + Generate the commit list with: + gh api "repos/developing-today/code/commits?path=.opencode/opencode.jsonc\ + &since=2026-10-27T00:00:00Z&per_page=100" --paginate \ + --jq '.[] | [.sha, .commit.author.date, (.commit.message | split("\\n")[0])] | @tsv' + +Argument contract (#3352): the whole command line is declared here +(one subcommand, two required options); anything unaccounted for is a +usage error naming the offending argument, exit 2. Exit codes: 0 every +change matched and no defective event; 1 unmatched change or defective +event; 2 usage error or unreadable changes file. + +Usage: + python3 -I scripts/change_ledger.py check \ + --ledger ~/workspace/goals/keep-the-four-repos-healthy/hidden_files/change-event-ledger.jsonl \ + --changes /tmp/config-changes.json +""" + +import argparse +import json +import re +import sys +from datetime import datetime + +REQUIRED_TEXT_FIELDS = ("timestamp", "pool", "change", "rationale", + "expected_effect", "window") +PR_RE = re.compile(r"(?:pull/|#)(\d+)") +SHA_RE = re.compile(r"\b[0-9a-f]{40}\b") + + +class Parser(argparse.ArgumentParser): + """argparse that names unaccounted-for arguments per #3352.""" + + def error(self, message): + message = message.replace("unrecognized arguments", + "unexpected argument") + super().error(message) + + +def build_parser(): + parser = Parser(prog="change_ledger.py", + description="Check the pool change-event ledger " + "against config changes (#1339).") + sub = parser.add_subparsers(dest="command", required=True) + check = sub.add_parser("check", help="check a ledger against a " + "changes file") + check.add_argument("--ledger", required=True, + help="path to the ledger JSONL file") + check.add_argument("--changes", required=True, + help="path to the changes JSON file") + return parser + + +def _valid_timestamp(value): + if not isinstance(value, str) or not value: + return False + try: + datetime.fromisoformat(value.replace("Z", "+00:00")) + except ValueError: + return False + return True + + +def _event_defects(line_no, event): + """Defect dicts for one parsed event; [] when the event is complete.""" + defects = [] + for field in REQUIRED_TEXT_FIELDS: + value = event.get(field) + if field == "timestamp": + if not _valid_timestamp(value): + kind = ("missing_timestamp" if not value + else "bad_timestamp") + defects.append({"line": line_no, "kind": kind, + "detail": "timestamp must be ISO-8601"}) + elif not isinstance(value, str) or not value.strip(): + defects.append({"line": line_no, "kind": "missing_" + field, + "detail": "event needs a non-empty " + field}) + snapshot = event.get("before_snapshot") + if not isinstance(snapshot, dict) or not snapshot: + defects.append({"line": line_no, + "kind": "missing_before_snapshot", + "detail": "event needs a non-empty before_snapshot " + "object (quota snapshot before the change)"}) + refs = extract_refs(event.get("change")) + if not refs: + defects.append({"line": line_no, "kind": "unmatchable_change", + "detail": "change must reference a PR number " + "(#N or pull/N) or a full commit SHA"}) + return defects + + +def extract_refs(change): + """(pr_numbers, shas) referenced by a ledger event's change field.""" + if not isinstance(change, str): + return set(), set() + return set(int(n) for n in PR_RE.findall(change)), set(SHA_RE.findall(change)) + + +def load_changes(path): + with open(path, encoding="utf-8") as fh: + doc = json.load(fh) + if not isinstance(doc, dict) or not isinstance(doc.get("changes"), list): + raise ValueError("changes file must be an object with a " + "'changes' list") + return doc + + +def check(ledger_path, changes_path): + """Check the ledger; return the report dict (includes exit_code).""" + doc = load_changes(changes_path) + changes = doc["changes"] + window = doc.get("window", {}) + config = doc.get("config", "") + + by_pr = {} + by_sha = {} + for idx, entry in enumerate(changes): + if entry.get("pr") is not None: + by_pr.setdefault(entry["pr"], idx) + if entry.get("sha"): + by_sha.setdefault(entry["sha"], idx) + + report = { + "config": config, + "window": window, + "ledger": {"path": ledger_path, "missing": False, "events": 0}, + "total_changes": len(changes), + "matched": 0, + "unmatched": [], + "defects": [], + "outside_window_events": [], + } + + try: + with open(ledger_path, encoding="utf-8") as fh: + lines = fh.readlines() + except FileNotFoundError: + report["ledger"]["missing"] = True + report["unmatched"] = list(changes) + report["exit_code"] = 1 if changes else 0 + return report + + matched_keys = set() + for line_no, raw in enumerate(lines, start=1): + if not raw.strip(): + continue + try: + event = json.loads(raw) + except json.JSONDecodeError as exc: + report["defects"].append( + {"line": line_no, "kind": "malformed_event", + "detail": "not valid JSON: %s" % exc}) + continue + if not isinstance(event, dict): + report["defects"].append( + {"line": line_no, "kind": "malformed_event", + "detail": "event must be a JSON object"}) + continue + report["ledger"]["events"] += 1 + defects = _event_defects(line_no, event) + report["defects"].extend(defects) + if defects: + # An incomplete event is not a ledger event for its change. + continue + prs, shas = extract_refs(event["change"]) + hit = False + for pr in prs: + if pr in by_pr: + matched_keys.add(by_pr[pr]) + hit = True + for sha in shas: + if sha in by_sha: + matched_keys.add(by_sha[sha]) + hit = True + if not hit: + report["outside_window_events"].append( + {"line": line_no, "change": event["change"]}) + + report["matched"] = len(matched_keys) + report["unmatched"] = [c for i, c in enumerate(changes) + if i not in matched_keys] + report["exit_code"] = 0 if (not report["unmatched"] + and not report["defects"]) else 1 + return report + + +def main(argv=None): + args = build_parser().parse_args(argv) + if args.command == "check": + try: + report = check(args.ledger, args.changes) + except (OSError, ValueError, json.JSONDecodeError) as exc: + print("change_ledger.py: error: cannot read changes file " + "%s: %s" % (args.changes, exc), file=sys.stderr) + return 2 + print(json.dumps(report, indent=2)) + print("change-event ledger: %d/%d config changes matched, " + "%d defective event(s), %d event(s) outside the window" + % (report["matched"], report["total_changes"], + len(report["defects"]), + len(report["outside_window_events"])), file=sys.stderr) + return report["exit_code"] + return 2 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/test_change_ledger.py b/scripts/test_change_ledger.py new file mode 100644 index 0000000000..8f8edea2bc --- /dev/null +++ b/scripts/test_change_ledger.py @@ -0,0 +1,204 @@ +"""Tests for scripts/change_ledger.py. + +Oracle: issue #1339's change-event ledger spec (epic #1334, criterion 3) +— "a config change with no ledger event is a defect" — plus the +operator-script argument contract (#3352): the whole command line is +declared, anything unaccounted for is a usage error (exit 2). + +Run: python3 scripts/test_change_ledger.py +""" + +import json +import os +import subprocess +import sys +import tempfile +import unittest + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + +import change_ledger as cl + + +def write(path, text): + with open(path, "w", encoding="utf-8") as fh: + fh.write(text) + + +def changes_doc(changes, since="2026-10-27", until="2026-11-23"): + return { + "config": ".opencode/opencode.jsonc", + "window": {"since": since, "until": until}, + "changes": changes, + } + + +def change(pr=None, sha=None, date="2026-11-20T00:00:00Z", title="config change"): + entry = {"date": date, "title": title} + if pr is not None: + entry["pr"] = pr + if sha is not None: + entry["sha"] = sha + return entry + + +def event(change, **overrides): + base = { + "timestamp": "2026-11-20T01:00:00Z", + "pool": "workhorse", + "change": change, + "rationale": "burn down the refilled window before reset", + "expected_effect": "workhorse absorbs the next hours of work", + "window": "7d, reset 2026-11-25T13:00-06:00", + "before_snapshot": {"workhorse": {"remaining_pct": 62}}, + } + base.update(overrides) + return base + + +class CheckTests(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.ledger = os.path.join(self.tmp.name, "ledger.jsonl") + self.changes = os.path.join(self.tmp.name, "changes.json") + + def tearDown(self): + self.tmp.cleanup() + + def run_check(self): + return cl.check(self.ledger, self.changes) + + def test_pr_link_match_passes(self): + write(self.changes, json.dumps(changes_doc([change(pr=13838)]))) + write(self.ledger, json.dumps(event( + "https://github.com/developing-today/code/pull/13838")) + "\n") + report = self.run_check() + self.assertEqual(report["matched"], 1) + self.assertEqual(report["unmatched"], []) + self.assertEqual(report["defects"], []) + self.assertEqual(report["exit_code"], 0) + + def test_hash_ref_and_pull_path_match(self): + write(self.changes, json.dumps(changes_doc( + [change(pr=13838), change(pr=13784)]))) + write(self.ledger, "\n".join([ + json.dumps(event("PR #13838")), + json.dumps(event("https://github.com/developing-today/code/pull/13784")), + ]) + "\n") + report = self.run_check() + self.assertEqual(report["matched"], 2) + self.assertEqual(report["exit_code"], 0) + + def test_unmatched_change_is_a_defect(self): + write(self.changes, json.dumps(changes_doc( + [change(pr=13838), change(pr=13784)]))) + write(self.ledger, json.dumps(event("PR #13838")) + "\n") + report = self.run_check() + self.assertEqual(report["matched"], 1) + self.assertEqual(len(report["unmatched"]), 1) + self.assertEqual(report["unmatched"][0]["pr"], 13784) + self.assertEqual(report["exit_code"], 1) + + def test_duplicate_events_match_once(self): + write(self.changes, json.dumps(changes_doc([change(pr=13838)]))) + write(self.ledger, "\n".join([ + json.dumps(event("PR #13838")), + json.dumps(event("PR #13838", timestamp="2026-11-21T01:00:00Z", + outcome="burn shifted as expected")), + ]) + "\n") + report = self.run_check() + self.assertEqual(report["matched"], 1) + self.assertEqual(report["exit_code"], 0) + + def test_event_missing_before_snapshot_is_a_defect(self): + bad = event("PR #13838") + del bad["before_snapshot"] + write(self.changes, json.dumps(changes_doc([change(pr=13838)]))) + write(self.ledger, json.dumps(bad) + "\n") + report = self.run_check() + self.assertEqual(report["exit_code"], 1) + self.assertTrue(any( + d["kind"] == "missing_before_snapshot" for d in report["defects"])) + + def test_event_with_empty_before_snapshot_is_a_defect(self): + write(self.changes, json.dumps(changes_doc([change(pr=13838)]))) + write(self.ledger, json.dumps(event( + "PR #13838", before_snapshot={})) + "\n") + report = self.run_check() + self.assertEqual(report["exit_code"], 1) + self.assertTrue(any( + d["kind"] == "missing_before_snapshot" for d in report["defects"])) + + def test_event_missing_expected_effect_is_a_defect(self): + bad = event("PR #13838") + del bad["expected_effect"] + write(self.changes, json.dumps(changes_doc([change(pr=13838)]))) + write(self.ledger, json.dumps(bad) + "\n") + report = self.run_check() + self.assertEqual(report["exit_code"], 1) + self.assertTrue(any( + d["kind"] == "missing_expected_effect" for d in report["defects"])) + + def test_malformed_ledger_line_is_a_defect(self): + write(self.changes, json.dumps(changes_doc([change(pr=13838)]))) + write(self.ledger, json.dumps(event("PR #13838")) + "\n{not json\n") + report = self.run_check() + self.assertEqual(report["exit_code"], 1) + self.assertTrue(any( + d["kind"] == "malformed_event" for d in report["defects"])) + + def test_missing_ledger_file_means_zero_matched(self): + write(self.changes, json.dumps(changes_doc( + [change(pr=13838), change(pr=13784)]))) + # No ledger file written at all: the ledger does not exist yet. + report = self.run_check() + self.assertTrue(report["ledger"]["missing"]) + self.assertEqual(report["matched"], 0) + self.assertEqual(len(report["unmatched"]), 2) + self.assertEqual(report["exit_code"], 1) + + def test_event_for_pr_outside_window_is_noted_not_a_defect(self): + write(self.changes, json.dumps(changes_doc([change(pr=13838)]))) + write(self.ledger, "\n".join([ + json.dumps(event("PR #13838")), + json.dumps(event("PR #13700")), + ]) + "\n") + report = self.run_check() + self.assertEqual(report["exit_code"], 0) + self.assertEqual(len(report["outside_window_events"]), 1) + + def test_sha_change_matches_full_sha_reference(self): + sha = "cc2708031280a69437749b640f36c8bf14383c16" + write(self.changes, json.dumps(changes_doc([change(sha=sha)]))) + write(self.ledger, json.dumps(event("commit " + sha)) + "\n") + report = self.run_check() + self.assertEqual(report["matched"], 1) + self.assertEqual(report["exit_code"], 0) + + def test_empty_window_passes_vacuously(self): + write(self.changes, json.dumps(changes_doc([]))) + write(self.ledger, "") + report = self.run_check() + self.assertEqual(report["exit_code"], 0) + + +class CliTests(unittest.TestCase): + def test_unknown_flag_is_usage_error(self): + proc = subprocess.run( + [sys.executable, "-I", "scripts/change_ledger.py", + "check", "--ledger", "x", "--changes", "y", "--nope"], + cwd=os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."), + capture_output=True, text=True) + self.assertEqual(proc.returncode, 2) + self.assertIn("unexpected argument", proc.stderr) + + def test_missing_required_arg_is_usage_error(self): + proc = subprocess.run( + [sys.executable, "-I", "scripts/change_ledger.py", "check"], + cwd=os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."), + capture_output=True, text=True) + self.assertEqual(proc.returncode, 2) + + +if __name__ == "__main__": + unittest.main(verbosity=2)