#!/usr/bin/env python3 """census-selftest.py -- prove the census DIFF, the WATCH, and the ESCALATION fire, in situ. A census that reports `appeared: 0, vanished: 0, changed: 0` proves nothing unless its diff can report a non-zero. Unit fixtures are not enough here: the claim being defended is about the corpus, so the detector is exercised on the corpus, with a baseline deliberately falsified in every way the diff and the watch are supposed to notice: * APPEARED -- drop the smallest patch-bearing id from the baseline's `existing_ids`: the return still exists, so the pass must report it as appeared (and must NOT alert on it). * VANISHED -- add an id that never existed, inside the range the pass will query: the pass must report it as vanished AND raise a VANISHED alert. * CHANGED -- corrupt one patch-bearing return's fingerprint: the pass must name that return. * STATUS -- falsify another return's PROJECTION `status`: a STATUS MOVED alert. A hash could not tell this from a text movement, so a baseline that moves ONLY `status` is what proves the typing is real. * TEXT -- falsify a third return's projection `patch_sha`: an ACCEPTED TEXT MOVED alert. * SAMPLE-BLIND -- falsify a return that an incremental pass will NOT query (not patch-bearing, not in the hot zone, not on the sample step). THE PASS MUST MISS IT. This is the control for the whole point of the escalation: without it, "the sample might hide something" is an argument, not a measurement. * ESCALATION -- the same falsified baseline is then run a SECOND time with escalation ON: the alert the sample missed MUST appear, the scope must read as COMPLETE, and the alert sets must be a superset of the sampled run's. * EXIT -- alerts must exit 5, which is the only thing a scheduler can see. * HISTORY -- each pass appends exactly ONE dated line, and the line must carry the same alert sets and the same scope as its stdout, or the series and the printout can disagree. THE DRIFT CONTROL. The baseline is a DATED snapshot, and the corpus moves underneath it: between the snapshot and this run a sibling thread files returns, and the pass then reports them -- correctly -- inside the very sets this test inspects. The first version asserted those sets as SINGLETONS and so failed against real drift (`appeared` held six ids instead of one), which is a report about the corpus and not about the detector. A test cannot assert the world holds still, and it must not absorb the movement either, so the drift is MEASURED: one pre-pass against the UNMUTATED baseline, complete over the alert-capable population, gives the drift, and the five victims are chosen OUTSIDE it, so "the injection caused this" is a claim with no confounder. A measured drift is still a DATED measurement, and the corpus here moves fast enough that a sibling moved a return's status 20 s after the control (id 929). Rather than tolerate that or fail on it, every id the control could not have seen is CHECKED against the baseline FILE: the verdict the pass printed carries the OLD and NEW value of each field that moved, so the reported `old` side must equal what the baseline recorded, and the `new` side must differ. A movement is therefore believed only when a second artefact corroborates it, and an id with no corroboration fails the check. * every member of every inspected set is: INJECTED | measured drift | baseline-corroborated * COMPLETE passes additionally assert the set is the exact equality, unless the difference is entirely ids the corpus moved after the control -- in which case they are printed, not asserted away * SAMPLED passes assert the injected ids are present and no unexplained alert was raised; the sampled pass may legitimately miss drift, which is the blind spot the escalation exists to close. All mutations go to COPIES of the state (the real baseline is untouched) and the passes append to their OWN history file, so a falsified or drift measurement never enters the real series. python census-selftest.py """ import json import os import subprocess import sys HERE = os.path.dirname(os.path.abspath(__file__)) CENSUS = os.path.join(HERE, "census.py") STATE = os.path.join(HERE, "census-state.json") MUT = os.path.join(HERE, "census-state-selftest.json") MUT_B = os.path.join(HERE, "census-state-selftest-b.json") MUT_C = os.path.join(HERE, "census-state-selftest-c.json") MUT_D = os.path.join(HERE, "census-state-selftest-d.json") CTRL = os.path.join(HERE, "census-state-selftest-control.json") OUT = os.path.join(HERE, "census-selftest.out") HIST = os.path.join(HERE, "census-selftest-history.jsonl") HIST_CTRL = os.path.join(HERE, "census-selftest-drift.jsonl") STEP = 20 # must match --sample-rate 0.05 below HOT = 50 def parse(stdout): """The census JSON body. It starts at the first line that is a bare `{`: the ALERTS block above it also carries braces (the field pairs it names), so the first `{` in stdout is not the dump.""" start = stdout.rfind("\n{\n") if start < 0: raise ValueError("no census JSON in stdout") return json.loads(stdout[start + 1:stdout.rfind("state ->")]) def run(state_path, extra=(), history=HIST): r = subprocess.run([sys.executable, CENSUS, "--state", state_path, "--history", history, "--sample-rate", "0.05", "--hot", str(HOT), "--grow", "0"] + list(extra), capture_output=True, text=True) open(OUT, "w", encoding="utf-8").write(r.stdout + r.stderr) if r.returncode not in (0, 5): raise RuntimeError("census refused or failed, exit %d -- see %s\n%s" % (r.returncode, OUT, (r.stdout + r.stderr)[:1200])) return r, parse(r.stdout) def main(): if not os.path.exists(STATE): print("no baseline: run `python census.py --full` first") return 2 st = json.load(open(STATE, encoding="utf-8")) ids = st["existing_ids"] patchy = st.get("patch_bearing_ids") or [] projs = st.get("projections") or {} if not ids or len(patchy) < 5 or not projs: print("baseline needs >= 5 patch-bearing returns and stored projections: run a pass (or " "`--full`) first, so the typed half of the watch can be exercised") return 2 # ---- THE DRIFT CONTROL: what the corpus does to a pass that mutates NOTHING -------------------- # Measured BEFORE any victim is chosen, because the victims must lie outside it. A victim that is # ALSO drifting would make "the injection caused this" unfalsifiable: the same observed id would be # explained twice and neither explanation would be tested. json.dump(st, open(CTRL, "w", encoding="utf-8"), indent=1, sort_keys=True) rc0, d0 = run(CTRL, ["--complete"], history=HIST_CTRL) assert d0["alert_scope"]["complete"] is True, \ "the drift control did not read a complete scope: %r" % (d0["alert_scope"],) drift = {k: set(d0["alerts"][k]) for k in ("vanished", "status_moved", "accepted_text_moved")} drift_diff = {k: set(d0["diff"][k]) for k in ("appeared", "vanished", "changed")} drift_ids = set().union(*drift.values()) | set().union(*drift_diff.values()) print("DRIFT CONTROL (unmutated baseline, complete pass, exit %d): the corpus moved under the " "baseline at %d id(s) -- %s" % (rc0.returncode, len(drift_ids), json.dumps({k: sorted(v) for k, v in drift.items()} | {("diff:" + k): sorted(v) for k, v in drift_diff.items()}, sort_keys=True))) if drift_ids: print(" ...the five injected victims are chosen OUTSIDE this set, so each injected movement " "has exactly one cause and the equality checks below are not cofounded") order = [i for i in sorted(patchy) if i not in drift_ids] if len(order) < 5: print("only %d patch-bearing ids are free of live drift; re-run (the corpus moved under the " "baseline)" % len(order)) return 2 dropped, status_victim, text_victim, victim, first_victim = order[:5] # The GHOST -- an id that never existed -- must be ABOVE every id the live corpus holds (the drift # control measured the last one) and it needs MARGIN, because returns are filed in sequence: an id # one past the last hit is the id the very next filing will occupy. The ghost is QUERIED through # the hot zone (it is the largest id the falsified baseline now holds) and the frontier stays open # above it, which is where a filing during this selftest lands. ghost = max(int(d0["last_hit"]), max(ids)) + 10 if ghost > int(st["ceiling"]): print("the live corpus has reached the probe ceiling %d: no id above it can be shown absent " "inside the range a pass queries -- grow the ceiling first" % st["ceiling"]) return 2 # The SAMPLE-BLIND victim: an existing return an incremental pass will not query. It must be # none of patch-bearing (always queried), the top HOT ids, a multiple of STEP (the sample), or # above max(known) (the frontier). patchy_set, hot_zone = set(patchy), set(sorted(ids)[-HOT:]) blind = [i for i in sorted(ids) if i not in patchy_set and i not in hot_zone and i not in drift_ids and i % STEP and i < max(ids)] if len(blind) < 2: print("cannot find a sample-blind id to falsify: the baseline's selection rule leaves none") return 2 blind_victim = blind[len(blind) // 2] def falsify(): m = dict(st) m["existing_ids"] = [i for i in ids if i != dropped] + [ghost] fps = dict(m.get("fingerprints") or {}) assert fps.get(str(victim)), "no fingerprint recorded for %d" % victim fps[str(victim)] = "0" * 64 m["fingerprints"] = fps pr = json.loads(json.dumps(projs)) for rid, field, bad in ((status_victim, "status", "SELFTEST-FALSIFIED-STATUS"), (text_victim, "patch_sha", "f" * 64), (blind_victim, "patch_status", "SELFTEST-FALSIFIED-PATCH-STATUS")): assert pr.get(str(rid)), "no projection recorded for %d" % rid pr[str(rid)][field] = bad m["projections"] = pr return m def falsify_stripped(): """A FIRST ASCENT: an id whose baseline projection carries NO patch/status field at all, so a scope narrowed to `ids already carrying a patch` would stop looking at it -- while the corpus, where the return DOES have a patch, raises exactly a text move under it. This is the pre-registered falsifier for the tempting optimisation. A confirmation that only re-measures ids which already show the field would report a smaller bill and look identical in every other respect, and it would miss the one event this watch exists to see under a fixed id. """ m = falsify() pr = json.loads(json.dumps(m["projections"])) assert pr.get(str(first_victim)), "no projection for %d" % first_victim pr[str(first_victim)] = {k: v for k, v in pr[str(first_victim)].items() if k not in ("patch_sha", "revision_sha", "declared", "status", "patch_status")} assert not any(pr[str(first_victim)].get(f) for f in ("patch_sha", "revision_sha", "declared")), "strip failed" m["projections"] = pr return m def falsify_min(): """ONLY the sample-blind mutation: a baseline that raises NO alert on a sampled pass, so the forced-complete mode is the only thing that can see it -- the control for `--complete`.""" m = dict(st) pr = json.loads(json.dumps(projs)) pr[str(blind_victim)]["patch_status"] = "SELFTEST-FALSIFIED-BLIND" m["projections"] = pr return m json.dump(falsify(), open(MUT, "w", encoding="utf-8"), indent=1, sort_keys=True) json.dump(falsify(), open(MUT_B, "w", encoding="utf-8"), indent=1, sort_keys=True) json.dump(falsify_stripped(), open(MUT_C, "w", encoding="utf-8"), indent=1, sort_keys=True) json.dump(falsify_min(), open(MUT_D, "w", encoding="utf-8"), indent=1, sort_keys=True) # the four baselines as OBJECTS, so every unexplained id can be checked against the file the pass # actually diffed against rather than against the clock bases = {p: json.load(open(p, encoding="utf-8")) for p in (MUT, MUT_B, MUT_C, MUT_D)} # The NAIVE scope has to be read from the FALSIFIED baseline, not from the real state: computed from # the real one it holds 1140 ids and contains the victim, because the victim's REAL projection does # carry a patch -- which is the whole point. (First version got this wrong and the control failed # exactly as it should have: the falsifier has to describe the world the confirmation sees.) stripped_projs = json.load(open(MUT_C, encoding="utf-8"))["projections"] naive_text_scope = {int(i) for i, p in stripped_projs.items() if any((p or {}).get(f) for f in ("patch_sha", "revision_sha", "declared"))} print("the NAIVE text scope -- ids whose BASELINE already carries a text field -- holds %d of %d " "projected ids; it excludes the first-ascent victim %d, which is the falsifier below" % (len(naive_text_scope), len(stripped_projs), first_victim)) print("falsified baseline: dropped %d (must APPEAR), added %d (must VANISH+ALERT), corrupted the " "fingerprint of %d (must CHANGE), the status of %d, the patch_sha of %d, and -- SAMPLE-BLIND " "-- the patch_status of %d" % (dropped, ghost, victim, status_victim, text_victim, blind_victim)) before = sum(1 for _ in open(HIST, encoding="utf-8")) if os.path.exists(HIST) else 0 ok = True def chk(label, cond, detail=None): nonlocal ok if not cond: ok = False print("%-6s %s%s" % ("ok" if cond else "FAIL", label, "" if detail is None else " " + str(detail)[:170])) def drift_of(kind): """The measured corpus movement for one set -- the two dicts are kept apart because the alert kinds and the diff kinds are not the same partition, but a lookup must not care.""" return drift.get(kind) or drift_diff.get(kind) or set() late = {} # ids seen to move AFTER the drift control, and how they were verified def corroborated(verdict, base, rid): """Is a movement the drift control did NOT see independently corroborated by the baseline? The drift control is a DATED measurement, so a corpus that keeps moving during the selftest produces movements it cannot have seen -- and those are precisely the ids a weaker test says `drift | injected`, and a dishonest one silently absorbs. Neither is acceptable, and neither is needed: the verdict the pass printed carries, for every id it names, the OLD and NEW value of every field that moved, and the baseline is a FILE. So the movement is not believed and not dismissed -- it is CHECKED: the reported `old` side must equal what the baseline recorded, and the `new` side must differ. That is a positive control per id, it costs no request, and it ties the claim to a second artefact instead of to the clock. """ ids_b = set(base["existing_ids"]) mv = (verdict.get("moved_values") or {}).get(str(rid)) if not mv: return rid not in ids_b # a reported ARRIVAL: the baseline does not hold the id proj = (base.get("projections") or {}).get(str(rid)) if not proj: return False # it was never projected, so nothing can have moved return all(pair and len(pair) == 2 and pair[0] == proj.get(f) and pair[1] != pair[0] for f, pair in mv.items()) def explain(verdict, base, kind, observed, injected): """The ids of an observed set that are NOT accounted for, as a LIST OF REASONS per member. Every member must be one of: an INJECTED id, an id the drift control measured, or an id whose movement the baseline file itself corroborates. The third branch is what lets the claims below stay exact on a corpus that moves while the selftest runs, without letting anything through: an id with no corroboration is returned as unexplained and the check FAILS. """ out, seen = [], [] for i in sorted(set(observed) - drift_of(kind) - set(injected)): if corroborated(verdict, base, i): seen.append(i) else: out.append(i) late[kind] = seen return out # ---- PASS A: the fast path, escalation OFF. Proves the sample's blind spot is REAL. --------- ra, da = run(MUT, ["--no-escalate"]) al_a, diff_a = da["alerts"], da["diff"] chk("A: the dropped id comes back as APPEARED", dropped in diff_a["appeared"], diff_a["appeared"][:6]) chk("A: an APPEARED return raises NO alert (a new return is not a changed one)", dropped not in al_a["vanished"] and dropped not in al_a["status_moved"] and dropped not in al_a["accepted_text_moved"]) chk("A: the ghost id comes back as VANISHED and alerts", ghost in diff_a["vanished"] and ghost in al_a["vanished"], diff_a["vanished"][:6]) chk("A: the corrupted fingerprint comes back as CHANGED", victim in diff_a["changed"], diff_a["changed"][:6]) chk("A: the falsified STATUS alerts as a status move", status_victim in al_a["status_moved"], al_a["status_moved"]) chk("A: the falsified accepted TEXT alerts as a text move", text_victim in al_a["accepted_text_moved"], al_a["accepted_text_moved"]) chk("A: the SAMPLE-BLIND return is NOT seen -- the sample's blind spot, demonstrated", blind_victim not in al_a["status_moved"] and blind_victim not in diff_a["changed"], "blind id %d" % blind_victim) a_bad = {k: explain(da, bases[MUT], k, obs, inj) for k, obs, inj in ( ("appeared", diff_a["appeared"], [dropped]), ("vanished", al_a["vanished"], [ghost]), ("status_moved", al_a["status_moved"], [status_victim]), ("accepted_text_moved", al_a["accepted_text_moved"], [text_victim]), ("changed", diff_a["changed"], [victim, status_victim, text_victim]))} chk("A: every member of every set is an INJECTED id, measured drift, or a movement the baseline " "corroborates -- no alert is raised that no artefact explains", not any(a_bad.values()), {"unexplained": {k: v for k, v in a_bad.items() if v}, "late-but-corroborated": late}) chk("A: the scope says SAMPLED, not complete", da["alert_scope"]["complete"] is False and da["escalated"] is None, da["alert_scope"]) chk("A: the pass exits 5, so a scheduler can see the alarm", ra.returncode == 5, ra.returncode) # ---- PASS B: the same baseline, escalation ON. Proves the escalation closes the blind spot. -- rb, db = run(MUT_B) al_b, diff_b = db["alerts"], db["diff"] chk("B: the SAMPLE-BLIND alert is FOUND once the whole range is re-measured", blind_victim in al_b["status_moved"], al_b["status_moved"]) chk("B: the complete pass is a SUPERSET of the sampled one", set(al_a["vanished"]) <= set(al_b["vanished"]) and set(al_a["status_moved"]) <= set(al_b["status_moved"]) and set(al_a["accepted_text_moved"]) <= set(al_b["accepted_text_moved"]), {"sampled": al_a, "complete": al_b}) chk("B: the scope says COMPLETE OVER THE ALERT-CAPABLE POPULATION and names what it escalated from", db["alert_scope"]["complete"] is True and db["escalated"] and db["escalated"]["sampled_alerts"] == da["alert_count"], db["escalated"]) esc = db.get("escalated") or {} chk("B: the confirmation REUSED this pass's measurements instead of paying for them twice", 0 < esc.get("ids_reused", 0) <= esc.get("sampled_ids_queried", 0) and esc.get("ids_added", 0) == db["ids_queried"] - esc["sampled_ids_queried"], "%d alert-capable ids reused of %d measured, %d added, total %d" % (esc.get("ids_reused", 0), esc.get("sampled_ids_queried", 0), esc.get("ids_added", 0), db["ids_queried"])) chk("B: the measured ids that can carry NO alert are stated, not hidden (the arithmetic adds up)", esc.get("sampled_ids_without_alert_role", 0) == esc["sampled_ids_queried"] - esc["ids_reused"], esc.get("sampled_ids_without_alert_role")) chk("B: it cost LESS than the id space it could have walked (the pre-scoping is the saving)", db["ids_queried"] < db["ceiling"] and db["ids_queried"] > da["ids_queried"], "%d measured vs %d ids in 1..%d (the sampled pass was %d)" % (db["ids_queried"], db["ceiling"], db["ceiling"], da["ids_queried"])) # ---- the EQUALITY: a complete pass must account for EVERY member of every set ------------------ # This is the assertion the drift control exists to license. `drift | injected == observed`, with # nothing left over, on all five sets at once -- so a detector that invented a movement, or dropped # one, or mis-typed one, is caught; and a failure can no longer be blamed on the corpus, because # the corpus's own contribution was measured first. b_bad = {k: explain(db, bases[MUT_B], k, obs, inj) for k, obs, inj in ( ("changed", diff_b["changed"], [victim, status_victim, text_victim, blind_victim]), ("status_moved", al_b["status_moved"], [status_victim, blind_victim]), ("accepted_text_moved", al_b["accepted_text_moved"], [text_victim]), ("vanished", al_b["vanished"], [ghost]))} chk("B: on a COMPLETE pass all four typed sets are exactly the injected ids plus what the corpus " "did -- the drift control measured, and anything later than it verified against the baseline " "file -- with nothing left over", not any(b_bad.values()), {"unexplained": {k: v for k, v in b_bad.items() if v}, "sets": {k: sorted(v) for k, v in (("changed", set(diff_b["changed"])), ("status_moved", set(al_b["status_moved"])), ("accepted_text_moved", set(al_b["accepted_text_moved"])), ("vanished", set(al_b["vanished"])))}}) eq_bad = {} for kind, observed, injected, dr in ( ("changed", diff_b["changed"], [victim, status_victim, text_victim, blind_victim], drift_diff["changed"]), ("status_moved", al_b["status_moved"], [status_victim, blind_victim], drift["status_moved"]), ("accepted_text_moved", al_b["accepted_text_moved"], [text_victim], drift["accepted_text_moved"]), ("vanished", al_b["vanished"], [ghost], drift["vanished"])): rest = sorted(set(observed) - dr - set(injected) - set(late[kind])) if rest: eq_bad[kind] = rest chk("B: the four typed sets are exact -- drift | injected -- or differ from exact ONLY by ids the " "corpus moved after the drift control and whose movement the baseline file corroborates", not eq_bad, {"beyond-drift": eq_bad, "late-but-corroborated": {k: v for k, v in late.items() if v}}) # `appeared` is the ONE set a complete pass does not make exact, and the reason is the census's own # disclosed selection rule, not the detector: an ARRIVAL is by construction absent from the baseline, # so whether a pass sees it depends on the frontier, the hot zone and the absent-id sample -- the # coverage `not_queried_existing` reports. So the claim here is the detector's: the dropped id IS # reported, and no return is reported that the corpus did not actually gain. a_b = explain(db, bases[MUT_B], "appeared", diff_b["appeared"], [dropped]) chk("B: the dropped id is reported as APPEARED, and no arrival is INVENTED beyond the measured " "drift (the arrival set is the one a complete pass cannot make exact -- whether an ARRIVAL is " "seen at all is the selection rule's coverage, disclosed as `not_queried_existing`)", dropped in diff_b["appeared"] and not a_b and ghost not in diff_b["appeared"], {"invented": a_b, "late-but-corroborated": late["appeared"], "arrivals the control held": len(drift_diff["appeared"])}) chk("B: the pass still exits 5", rb.returncode == 5, rb.returncode) chk("B: the gate admitted the claims", "controls" not in str(db.get("claim_refused", "")), db.get("claim_refused", "")[:80]) chk("B: the population moves by the ghost's removal (and any return filed since the baseline)", len(ids) - 1 <= db["existing_now"] <= len(ids) + len(drift_diff["appeared"]) + 2, "%d now vs %d in the falsified baseline (+%d arrivals measured by the drift control)" % (db["existing_now"], len(ids), len(drift_diff["appeared"]))) # ---- PASS C: THE FIRST ASCENT, and the pre-registered falsifier for the tempting narrowing ---- # A return that carries a patch in the corpus but NOT in the baseline is the shape of the first # ascent under a fixed id. A confirmation scoped to "ids that already carry a patch" would stop # looking at it and report a smaller bill that looks identical in every other respect. rc, dc = run(MUT_C, ["--complete"]) al_c = dc["alerts"] chk("C: a return whose BASELINE projection carried no text field still raises an ACCEPTED TEXT " "move (the first ascent)", first_victim in al_c["accepted_text_moved"], al_c["accepted_text_moved"][:8]) chk("C: ...and the NAIVE scope -- ids already carrying a text field -- excludes exactly that id, " "so the narrowing is falsified by a real alert", first_victim not in naive_text_scope, "naive scope holds %d ids, victim %d" % (len(naive_text_scope), first_victim)) chk("C: the scope that DID find it is the projected population, not the text-bearing subset", dc["alert_scope"]["complete"] is True and dc["ids_queried"] > len(naive_text_scope), dc["alert_scope"]) chk("C: the per-kind report says how much the narrowing would have saved, and that it is 0 once " "every kind has rung", (dc.get("escalated") or {}).get("kind_scope", {}).get("narrowed_by_kind") == 0, (dc.get("escalated") or {}).get("kind_scope")) c_bad = explain(dc, bases[MUT_C], "accepted_text_moved", al_c["accepted_text_moved"], [text_victim, first_victim]) chk("C: the text set is accounted for by both injected text movements (the strip and the " "patch_sha), the measured drift, and nothing else", not c_bad, {"unexplained": c_bad, "late-but-corroborated": late["accepted_text_moved"]}) # ---- PASS D: `--complete` with NO alert on the sampled pass ----------------------------------- # The corpus-wide reading the watch is asked for ("did any accepted text move anywhere") cannot be # stated by a pass that decides its own completeness from whether its own sample happened to ring. rd1, dd1 = run(MUT_D, ["--no-escalate"]) chk("D1: the sample-blind status move raises NO alert on the sampled pass -- a zero at the " "sample's probability, not a finding", blind_victim not in dd1["alerts"]["status_moved"] and blind_victim not in dd1["diff"]["changed"], dd1["alerts"]) d1_bad = explain(dd1, bases[MUT_D], "status_moved", dd1["alerts"]["status_moved"], []) chk("D1: every alert the sampled pass DOES raise is explained by an artefact (measured drift or a " "baseline-corroborated movement), so the injected miss is the only unexplained absence", not d1_bad, {"unexplained": d1_bad, "raised": dd1["alerts"]["status_moved"], "drift": sorted(drift["status_moved"])}) chk("D1: the exit code AGREES with the pass's own alert count (5 iff >0), so the alarm cannot " "contradict the verdict it reports", rd1.returncode == (5 if dd1["alert_count"] else 0), "%d alerts, exit %d" % (dd1["alert_count"], rd1.returncode)) rd2, dd2 = run(MUT_D, ["--complete"]) chk("D2: --complete finds it with NO alert having rung first", blind_victim in dd2["alerts"]["status_moved"], dd2["alerts"]) chk("D2: the reason is NAMED (a corpus-wide reading was claimed), not left implicit", "--complete" in ((dd2.get("escalated") or {}).get("forced_by") or ""), (dd2.get("escalated") or {}).get("forced_by")) chk("D2: the scope reads COMPLETE and the pass exits 5", dd2["alert_scope"]["complete"] is True and rd2.returncode == 5, rd2.returncode) d2_bad = explain(dd2, bases[MUT_D], "status_moved", dd2["alerts"]["status_moved"], [blind_victim]) chk("D2: the status set is the blind mutation, plus what the corpus did and the baseline " "corroborates, and nothing else", not d2_bad, {"unexplained": d2_bad, "late-but-corroborated": late["status_moved"]}) # ---- the series: ONE line per pass, each agreeing with its own stdout -------------------------- lines = [json.loads(l) for l in open(HIST, encoding="utf-8") if l.strip()] \ if os.path.exists(HIST) else [] chk("each of the five passes appended exactly one dated line", len(lines) == before + 5, "%d -> %d" % (before, len(lines))) la, lb = (lines[-5], lines[-4]) if len(lines) >= 5 else ({}, {}) chk("the sampled line is DATED, names its basis and its scope", bool(la.get("at")) and bool(la.get("baseline_at")) and la.get("scope", {}).get("complete") is False, (la.get("at"), la.get("basis"))) chk("the escalated line records the COMPLETE scope", lb.get("scope", {}).get("complete") is True and lb.get("escalated"), lb.get("scope")) chk("the escalated line carries the same alert sets as its run", lb.get("alerts") == al_b and lb.get("alert_count") == db["alert_count"], "series and stdout agree" if lb.get("alerts") == al_b else lb.get("alerts")) chk("the FORCED-COMPLETE line records its reason and the same alerts as its run", (lines[-1].get("escalated") or {}).get("forced_by", "").find("--complete") >= 0 and lines[-1].get("alerts") == dd2["alerts"], lines[-1].get("escalated", {}).get("forced_by")) # ...and the DRIFT measurement is itself on the record, dated, in its own series: a later reader # can see what the corpus did during this selftest instead of taking the equality claims on faith. dlines = [json.loads(l) for l in open(HIST_CTRL, encoding="utf-8") if l.strip()] \ if os.path.exists(HIST_CTRL) else [] chk("the drift control's own measurement is preserved as a DATED series line", bool(dlines) and dlines[-1].get("at") == d0["at"] and dlines[-1].get("alerts") == d0["alerts"], dlines[-1].get("at") if dlines else None) print("\ncensus diff + watch + escalation selftest: %s" % ("PASS" if ok else "FAIL")) return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())