#!/usr/bin/env python """Determinism verdict from resample.sh output. For every id in manifest_resample.json, gathers the response sha256 across all cold-cache samples of each build: AFTER : capture_resAFTER_r*.json + capture_after_extract + capture_after_replay BEFORE : capture_resBEFORE_r*.json + capture_before and classifies the id: STABLE-IDENTICAL each build emits exactly one sha and they are equal DETERMINISTIC-DIFF each build emits exactly one sha and they differ -> a genuine, reproducible commit-caused difference NONDETERMINISTIC at least one build emits >1 distinct sha across its samples -> a before/after difference on this request cannot be attributed to the commit range If the two builds' sha sets overlap at all, the builds can produce the same response, which independently rules out a deterministic commit change. Writes report_resample.md and report_resample.json. """ import argparse import glob import json import os from collections import Counter HERE = os.path.dirname(os.path.abspath(__file__)) def load(path): with open(path) as fh: return json.load(fh) def data_identical(sorted_sha_a, sorted_sha_b): """True if two responses carry the same data once every list is order- normalised -- i.e. their deep-sorted canonical hashes (sha256_sorted) match. """ return ( sorted_sha_a is not None and sorted_sha_b is not None and sorted_sha_a == sorted_sha_b ) def main(): ap = argparse.ArgumentParser() ap.add_argument("--out", default=os.path.join(HERE, "_out")) args = ap.parse_args() out = args.out sub = load(os.path.join(out, "manifest_resample.json")) ids = [r["id"] for r in sub["requests"]] req_by_id = {r["id"]: r for r in sub["requests"]} orig = {r["id"]: r for r in load(os.path.join(out, "report.json"))["rows"]} after_paths = sorted(glob.glob(os.path.join(out, "capture_resAFTER_r*.json"))) + [ os.path.join(out, "capture_after_extract.json"), os.path.join(out, "capture_after_replay.json"), ] before_paths = sorted(glob.glob(os.path.join(out, "capture_resBEFORE_r*.json"))) + [ os.path.join(out, "capture_before.json") ] def gather(paths): samples = {i: [] for i in ids} # id -> list of (sha256, sha256_sorted) used = [] for path in paths: if not os.path.exists(path): continue cap = load(path) used.append(os.path.basename(path)) for i in ids: r = cap["responses"].get(i) if r: samples[i].append((r.get("sha256"), r.get("sha256_sorted"))) return samples, used after, after_files = gather(after_paths) before, before_files = gather(before_paths) rows = [] for i in ids: a, b = after[i], before[i] after_shas = {s for s, _ in a} before_shas = {s for s, _ in b} overlap = bool(after_shas & before_shas) after_ndet = len(after_shas) > 1 before_ndet = len(before_shas) > 1 if after_ndet or before_ndet: cls = "NONDETERMINISTIC" elif after_shas == before_shas: cls = "STABLE-IDENTICAL" else: cls = "DETERMINISTIC-DIFF" data_same = None if cls != "STABLE-IDENTICAL" and a and b: data_same = data_identical(a[0][1], b[0][1]) rows.append( dict( id=i, cls=cls, after_samples=len(a), before_samples=len(b), after_distinct=len(after_shas), before_distinct=len(before_shas), overlap=overlap, data_same=data_same, orig_cat=orig.get(i, {}).get("category"), orig_attr=orig.get(i, {}).get("attribution"), ) ) by_cls = Counter(r["cls"] for r in rows) commit_rows = [r for r in rows if r["orig_attr"] == "COMMIT"] commit_overturned = [r for r in commit_rows if r["cls"] == "NONDETERMINISTIC"] commit_stable = [r for r in commit_rows if r["cls"] == "STABLE-IDENTICAL"] commit_upheld = [r for r in commit_rows if r["cls"] == "DETERMINISTIC-DIFF"] controls = [r for r in rows if r["orig_cat"] == "IDENTICAL"] controls_bad = [r for r in controls if r["cls"] != "STABLE-IDENTICAL"] det_diffs = [r for r in rows if r["cls"] == "DETERMINISTIC-DIFF"] det_content = [r for r in det_diffs if r["data_same"] is False] ndet = [r for r in rows if r["cls"] == "NONDETERMINISTIC"] ndet_overlap = [r for r in ndet if r["overlap"]] n_after, n_before = len(after_files), len(before_files) if det_content: verdict = ( f"DIFFERENCES CONFIRMED -- {len(det_content)} request(s) are a " f"deterministic content change attributable to the commit range." ) emoji = "FAIL" elif det_diffs: verdict = ( f"NO CONTENT CHANGE -- {len(det_diffs)} request(s) differ " f"deterministically between the builds, but every one carries " f"identical data (array element-order only). No deterministic " f"content change is attributable to the commit range." ) emoji = "PASS (order-only)" else: verdict = ( "NO COMMIT-ATTRIBUTABLE DIFFERENCE -- once resampled, every request " "is either stable-identical across both builds or nondeterministic " "on at least one build. Nothing differs deterministically between " "the two builds." ) emoji = "PASS" out_lines = [] add = out_lines.append add("# endpoint_diff -- resample determinism report") add("") add( f"Each of the {len(ids)} resampled requests was fired against " f"**{n_after} cold-cache AFTER samples** and **{n_before} cold-cache " f"BEFORE samples** -- the server is restarted before every sample, so " f"fakeredis is empty and no response is served from cache." ) add("") add(f"**Verdict ({emoji}):** {verdict}") add("") add("## Classification") add("") add("| class | count | meaning |") add("|---|---|---|") meaning = { "STABLE-IDENTICAL": "one sha per build, identical -- request unaffected", "DETERMINISTIC-DIFF": "one sha per build, differ -- reproducible build difference", "NONDETERMINISTIC": ">1 sha on some build -- not attributable to the commit range", } for c in ("STABLE-IDENTICAL", "DETERMINISTIC-DIFF", "NONDETERMINISTIC"): add(f"| {c} | {by_cls.get(c, 0)} | {meaning[c]} |") add("") add( f"Of the {len(ndet)} NONDETERMINISTIC ids, **{len(ndet_overlap)}** have " f"overlapping AFTER/BEFORE sha sets -- the two builds literally produced " f"the same response in at least one sample, which on its own rules out a " f"commit-caused difference." ) add("") add("## Controls") add("") add( f"{len(controls)} requests that the 3-capture compare found " f"byte-identical were resampled as a control. " + ( f"All {len(controls)} stayed STABLE-IDENTICAL across all " f"{n_after}+{n_before} samples -- the method produces no false diffs." if not controls_bad else f"**WARNING:** {len(controls_bad)} control(s) were not stable: " + ", ".join(r["id"] for r in controls_bad) ) ) add("") add( f"## The {len(commit_rows)} requests the 3-capture compare attributed " f"to the commit range" ) add("") add( f"- overturned -- now NONDETERMINISTIC (the after-ref itself varies once " f"resampled): **{len(commit_overturned)}**" ) add( f"- still STABLE-IDENTICAL once resampled (the 2-sample diff did not " f"reproduce at all): **{len(commit_stable)}**" ) add(f"- upheld as a DETERMINISTIC-DIFF: **{len(commit_upheld)}**") add("") if commit_upheld: add("### Upheld deterministic differences") add("") add("| id | data identical? | orig category | request |") add("|---|---|---|---|") for r in sorted(commit_upheld, key=lambda r: r["id"]): rq = req_by_id[r["id"]] add( f"| {r['id']} | {'yes' if r['data_same'] else '**NO**'} " f"| {r['orig_cat']} | `{rq['method']} {rq['url'][:88]}` |" ) add("") add("## Per-id detail") add("") add( "| id | class | AFTER distinct/n | BEFORE distinct/n | overlap | data same | orig |" ) add("|---|---|---|---|---|---|---|") for r in sorted(rows, key=lambda r: (r["cls"], r["id"])): ds = "" if r["data_same"] is None else ("yes" if r["data_same"] else "NO") add( f"| {r['id']} | {r['cls']} | {r['after_distinct']}/{r['after_samples']} " f"| {r['before_distinct']}/{r['before_samples']} " f"| {'yes' if r['overlap'] else 'no'} | {ds} " f"| {r['orig_attr']}/{r['orig_cat']} |" ) add("") add(f"AFTER sample files ({n_after}): {', '.join(after_files)}") add(f"BEFORE sample files ({n_before}): {', '.join(before_files)}") add("") report = "\n".join(out_lines) + "\n" with open(os.path.join(out, "report_resample.md"), "w") as fh: fh.write(report) with open(os.path.join(out, "report_resample.json"), "w") as fh: json.dump( { "verdict": verdict, "emoji": emoji, "by_class": dict(by_cls), "commit_overturned": [r["id"] for r in commit_overturned], "commit_upheld": [r["id"] for r in commit_upheld], "rows": rows, }, fh, indent=2, sort_keys=True, ) print(report) print(f"wrote {out}/report_resample.md and report_resample.json") if __name__ == "__main__": main()