| 1 | """devloop verdicts replayed on REAL identical-binary rows (QA P1-1, P1-2, detection power).
|
| 2 |
|
| 3 | run_lane is replaced by a writer that serves slimmed real rows captured by
|
| 4 | independent QA (tests/fixtures/devloop_real_rows), optionally transformed to
|
| 5 | inject a known effect, so the whole verdict path (screen, confirmation,
|
| 6 | guardrail, fail-closed) runs exactly as in production.
|
| 7 | """
|
| 8 | from __future__ import annotations
|
| 9 |
|
| 10 | import copy
|
| 11 | import gzip
|
| 12 | import importlib.util
|
| 13 | import json
|
| 14 | import sys
|
| 15 | import tempfile
|
| 16 | import unittest
|
| 17 | from pathlib import Path
|
| 18 | from unittest import mock
|
| 19 |
|
| 20 | ROOT = Path(__file__).resolve().parents[1]
|
| 21 | FIXTURES = ROOT / "tests" / "fixtures" / "devloop_real_rows"
|
| 22 | sys.path.insert(0, str(ROOT / "scripts"))
|
| 23 |
|
| 24 |
|
| 25 | def load_devloop():
|
| 26 | spec = importlib.util.spec_from_file_location("devloop_real_rows", ROOT / "scripts" / "devloop.py")
|
| 27 | module = importlib.util.module_from_spec(spec)
|
| 28 | spec.loader.exec_module(module)
|
| 29 | return module
|
| 30 |
|
| 31 |
|
| 32 | def rows(name: str) -> list[dict]:
|
| 33 | with gzip.open(FIXTURES / f"{name}.jsonl.gz", "rt") as handle:
|
| 34 | return [json.loads(line) for line in handle if line.strip()]
|
| 35 |
|
| 36 |
|
| 37 | def pooled(profile: str) -> list[dict]:
|
| 38 | """Two independent real A/A runs pooled as one 10-run confirmation sample."""
|
| 39 | first = rows(f"aa-{profile}")
|
| 40 | second = copy.deepcopy(rows(f"aa2-{profile}"))
|
| 41 | for row in second:
|
| 42 | row["run"] = row["run"] + 100
|
| 43 | return first + second
|
| 44 |
|
| 45 |
|
| 46 | def shift(data: list[dict], pct: float, cell: tuple[str, str] | None = None, ms: float = 0.0) -> list[dict]:
|
| 47 | data = copy.deepcopy(data)
|
| 48 | for row in data:
|
| 49 | if row.get("subject") != "oak_local" or not isinstance(row.get("elapsed_ms"), (int, float)):
|
| 50 | continue
|
| 51 | if cell and (row["scenario"], row["operation"]) != cell:
|
| 52 | continue
|
| 53 | row["elapsed_ms"] = row["elapsed_ms"] * (1 + pct / 100.0) + ms
|
| 54 | return data
|
| 55 |
|
| 56 |
|
| 57 | class DevloopRealRowsTest(unittest.TestCase):
|
| 58 | def run_devloop(self, *extra, stage1=None, confirm=None, runs_filter=None):
|
| 59 | devloop = load_devloop()
|
| 60 | stage1 = stage1 or {}
|
| 61 | confirm = confirm or {}
|
| 62 | calls = []
|
| 63 |
|
| 64 | def fake_lane(command, label):
|
| 65 | calls.append(label)
|
| 66 | results = Path(command[command.index("--results") + 1])
|
| 67 | profile = command[command.index("--profile") + 1]
|
| 68 | subjects = command[command.index("--subjects") + 1].split(",")
|
| 69 | data = (confirm if "--scenarios" in command else stage1)[profile]
|
| 70 | if "--scenarios" in command:
|
| 71 | wanted = set(command[command.index("--scenarios") + 1].split(","))
|
| 72 | data = [row for row in data if row["scenario"] in wanted]
|
| 73 | data = [row for row in data if row["subject"] in subjects]
|
| 74 | if runs_filter is not None:
|
| 75 | data = [row for row in data if row["run"] in runs_filter]
|
| 76 | results.mkdir(parents=True, exist_ok=True)
|
| 77 | (results / "latest.jsonl").write_text("".join(json.dumps(row) + "\n" for row in data))
|
| 78 | return 1.0
|
| 79 |
|
| 80 | with tempfile.TemporaryDirectory() as tmp:
|
| 81 | argv = ["devloop.py", "--skip-build", "--oak-baseline-bin", sys.executable, "--oak-local-bin", sys.executable,
|
| 82 | "--lanes", "core", "--core-profiles", "micro,smoke", "--results", tmp, *extra]
|
| 83 | with mock.patch.object(sys, "argv", argv), mock.patch.object(devloop, "run_lane", side_effect=fake_lane), \
|
| 84 | mock.patch("builtins.print"):
|
| 85 | code = devloop.main()
|
| 86 | out = next(Path(tmp).iterdir())
|
| 87 | verdict = (out / "verdict.md").read_text()
|
| 88 | cells = json.loads((out / "cells.json").read_text())
|
| 89 | return code, verdict, cells, calls
|
| 90 |
|
| 91 | def oak_cells(self, cells):
|
| 92 | return {(c["lane"], c["scenario"], c["operation"]): c for c in cells if c["comparison"] == "oak_local_vs_oak_installed"}
|
| 93 |
|
| 94 | def test_identical_binary_guardrail_crossing_is_not_a_new_breach(self):
|
| 95 | # aa2 is the reviewer's run that the pre-fix devloop called REGRESSED
|
| 96 | # ("new" few_large_binaries/branch.create Git breach, identical binaries).
|
| 97 | stage1 = {"micro": rows("aa2-micro"), "smoke": rows("aa2-smoke")}
|
| 98 | confirm = {"micro": pooled("micro"), "smoke": pooled("smoke")}
|
| 99 | code, verdict, cells, _ = self.run_devloop(stage1=stage1, confirm=confirm)
|
| 100 | self.assertEqual(code, 0, verdict)
|
| 101 | self.assertIn("**PASS**", verdict)
|
| 102 | self.assertIn("## New Git-guardrail breaches introduced by this changeset (0)", verdict)
|
| 103 | oak = self.oak_cells(cells)
|
| 104 | self.assertFalse([k for k, c in oak.items() if c["verdict"] in ("regression", "improvement")])
|
| 105 | for cell in oak.values():
|
| 106 | if cell["verdict"] == "no_change":
|
| 107 | self.assertIsNotNone(cell["mde_pct"], cell) # never a bare no_change
|
| 108 | self.assertIn("no change detected >=", verdict)
|
| 109 |
|
| 110 | def test_identical_binary_without_confirmation_never_claims(self):
|
| 111 | stage1 = {"micro": rows("aa-micro"), "smoke": rows("aa-smoke")}
|
| 112 | code, verdict, cells, calls = self.run_devloop("--no-confirm", stage1=stage1)
|
| 113 | # Without confirmation, n=5 screening candidates fail CLOSED (UNMEASURED),
|
| 114 | # never as a claim: zero Oak-vs-Oak regressions or improvements.
|
| 115 | self.assertIn(code, (0, 2), verdict)
|
| 116 | self.assertNotIn("**REGRESSED**", verdict)
|
| 117 | self.assertFalse([k for k, c in self.oak_cells(cells).items() if c["verdict"] in ("regression", "improvement")])
|
| 118 | self.assertFalse([label for label in calls if label.startswith("confirmation")])
|
| 119 |
|
| 120 | def test_missing_aa_control_fails_closed(self):
|
| 121 | # --skip-noise-floor with a +10 ms slowdown: every cell lacks a floor.
|
| 122 | stage1 = {"micro": shift(rows("aa-micro"), 0, ms=10), "smoke": shift(rows("aa-smoke"), 0, ms=10)}
|
| 123 | code, verdict, _, _ = self.run_devloop("--skip-noise-floor", stage1=stage1)
|
| 124 | self.assertEqual(code, 2, verdict)
|
| 125 | self.assertIn("**UNMEASURED**", verdict)
|
| 126 |
|
| 127 | def test_too_few_runs_fail_closed(self):
|
| 128 | stage1 = {"micro": shift(rows("aa-micro"), 0, ms=10), "smoke": shift(rows("aa-smoke"), 0, ms=10)}
|
| 129 | code, verdict, _, _ = self.run_devloop("--runs", "2", "--no-confirm", stage1=stage1, runs_filter={0, 1})
|
| 130 | self.assertEqual(code, 2, verdict)
|
| 131 | self.assertIn("**UNMEASURED**", verdict)
|
| 132 |
|
| 133 | def test_single_cell_twenty_percent_on_ten_ms_op_is_confirmed(self):
|
| 134 | # tiny_text/branch.create is a >= 10 ms Oak op (about 13-16 ms here).
|
| 135 | cell = ("tiny_text", "branch.create")
|
| 136 | stage1 = {"micro": shift(rows("aa2-micro"), 20, cell), "smoke": rows("aa2-smoke")}
|
| 137 | confirm = {"micro": shift(pooled("micro"), 20, cell), "smoke": pooled("smoke")}
|
| 138 | baseline = [r["elapsed_ms"] for r in stage1["micro"] if r["subject"] == "oak_installed" and (r["scenario"], r["operation"]) == cell]
|
| 139 | self.assertGreaterEqual(sorted(baseline)[len(baseline) // 2], 10.0)
|
| 140 | code, verdict, cells, calls = self.run_devloop(stage1=stage1, confirm=confirm)
|
| 141 | self.assertEqual(code, 1, verdict)
|
| 142 | oak = self.oak_cells(cells)
|
| 143 | target = oak[("core:micro", *cell)]
|
| 144 | self.assertEqual(target["verdict"], "regression", target)
|
| 145 | self.assertEqual(target["stage"], "confirmation")
|
| 146 | self.assertTrue([label for label in calls if label.startswith("confirmation (core:micro")])
|
| 147 | others = [k for k, c in oak.items() if c["verdict"] in ("regression", "improvement") and k[1:] != cell]
|
| 148 | self.assertEqual(others, [])
|
| 149 |
|
| 150 |
|
| 151 | if __name__ == "__main__":
|
| 152 | unittest.main()
|