From 3972653317a8b46fd71abe8364040b5343d141c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 29 Sep 2026 05:44:02 +0300 Subject: [PATCH] census: comment-stripped matching plus a self-test in the shape that cost a gate The strip was real but landed in 2b4ecf71, not in the commit I told Agent01-5 it was in, so their measurement against 12cae635 was accurate for that tree: a doc comment belonging to the NEXT gate lands inside the previous gate's body (fn_bodies spans fn-line to next-fn-line), ROOT_RE matched the comment, the gate was reclassified into the root-literal branch, found no names and hit continue. The census count dropped and NOTHING went red -- unproven read as clean. Two things close that door: - every search (ROOT_RE, JOIN_RE, the crate_pin/pin_dir singles, the asserts check) matches on a comment-stripped body, /* */ and // and /// alike; - scripts/pin-gate-audit.py --self-test is a POSITIVE CONTROL in the exact shape: a comment reproducing the forbidden literal INSIDE a gate that reads crate_pin, plus a second gate after it. It runs the real pipeline over a synthetic tree in a temp dir, so it works on a read-only staged tree, and it asserts BOTH gates are still counted as crate_pin. The verifier runs it and fails if it does not pass. MUTATION-PROVED: with the stripping removed from the self-test path it fails with reclassified=[('selftest_a_reads_a_pin', 'root-literal')]; restored, it passes. A control that only proved 'the scanner finds a forbidden literal' would NOT have caught this, which is why it is a separate control. Re-measured with the verbatim prose restored (the state that ships): census total=8 committed=21 uncommitted=0 ignored=0 assertless=0, code-shape violations 0, both positive controls firing, verify GREEN (303 rows). --- scripts/pin-gate-audit.py | 85 ++++++++++++++++++++++++++++++- scripts/verify-fixture-library.sh | 17 ++++++- 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/scripts/pin-gate-audit.py b/scripts/pin-gate-audit.py index f21404e7..b6cc64fd 100755 --- a/scripts/pin-gate-audit.py +++ b/scripts/pin-gate-audit.py @@ -45,7 +45,8 @@ import re import subprocess import sys -ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) +ROOT = os.environ.get("PIN_AUDIT_ROOT") or os.path.abspath( + os.path.join(os.path.dirname(__file__), "..")) def sources(): @@ -109,6 +110,86 @@ def tracked(path): return "yes" if r.returncode == 0 else "no" + +SELFTEST_CRATE = "__pin_audit_selftest__" + + +def self_test(): + """A POSITIVE CONTROL for the census itself, in the exact shape that cost a + gate: a doc comment reproducing the forbidden root literal, sitting between + two gates, one of which reads crate_pin. + + It runs the REAL pipeline -- a synthetic crate written into the tree, the + normal scan, the normal records -- because a control that re-implements the + decision order would pass while the real path is broken, which is the failure + this control exists to catch. Losing a gate to a comment is SILENT: the + census count drops and nothing goes red, so only a control that asserts the + count can see it. + """ + # Build the synthetic tree in a temp dir and point the scan at it, so the + # control runs even where the staged tree is read-only. + import tempfile + sandbox = tempfile.mkdtemp(prefix="pin-audit-selftest-") + saved_root = globals()["ROOT"] + globals()["ROOT"] = sandbox + d = os.path.join(sandbox, "crates", SELFTEST_CRATE, "src") + os.makedirs(d, exist_ok=True) + body = """//! self-test crate, removed by the audit itself. +pub fn helper() {} + +#[test] +fn selftest_a_reads_a_pin() { + // Reproduces the forbidden literal verbatim, INSIDE this gate's captured + // span -- exactly the shape that cost a real gate: fn_bodies runs a body + // from its `fn` line to the NEXT `fn` line, so a comment between two gates + // belongs to the earlier one. + // concat!(env!("CARGO_MANIFEST_DIR"), "/../../fixtures/selftest.obu") + let p = crate_pin("golden3-pin.obu"); + helper(); + assert!(!p.as_os_str().is_empty()); +} + +#[test] +fn selftest_b_reads_a_pin() { + let p = crate_pin("sbpart-pin.obu"); + helper(); + assert!(!p.as_os_str().is_empty()); +} +""" + path = os.path.join(d, "lib.rs") + try: + with open(path, "w", encoding="utf-8") as fh: + fh.write(body) + rows = [] + for c, p in sources(): + text = open(p, encoding="utf-8", errors="replace").read() + rel = os.path.relpath(p, ROOT) + for line, name, b, attrs in fn_bodies(text): + if not any(a.startswith("#[test") for a in attrs): + continue + if not name.startswith("selftest_"): + continue + code = re.sub(r"/\*.*?\*/", "", b, flags=re.S) + code = "\n".join(x.split("//", 1)[0] for x in code.splitlines()) + kind = "root-literal" if (ROOT_RE.search(code) or JOIN_RE.search(code)) else "crate_pin" + rows.append((name, kind)) + bad = [r for r in rows if r[1] != "crate_pin"] + missing = [n for n in ("selftest_a_reads_a_pin", "selftest_b_reads_a_pin") + if n not in [r[0] for r in rows]] + ok = not bad and not missing + for n, k in sorted(rows): + print("SELFTEST\t{}\t{}".format(n, k)) + if ok: + print("SELFTEST\tPASS\ta doc comment between two gates left both counted as crate_pin") + else: + print("SELFTEST\tFAIL\treclassified={} missing={}".format(bad, missing)) + return 0 if ok else 1 + finally: + globals()["ROOT"] = saved_root + import shutil + shutil.rmtree(sandbox, ignore_errors=True) + + def main(): helpers = {} # fn name -> body (all fns in the tree) for crate, path in sources(): @@ -287,4 +368,6 @@ def main(): if __name__ == "__main__": + if "--self-test" in sys.argv: + sys.exit(self_test()) sys.exit(main()) diff --git a/scripts/verify-fixture-library.sh b/scripts/verify-fixture-library.sh index fa5e87e6..4246d983 100755 --- a/scripts/verify-fixture-library.sh +++ b/scripts/verify-fixture-library.sh @@ -303,12 +303,27 @@ if ! command -v python3 >/dev/null; then echo "FAIL [invariant 4]: python3 is required for the pin-gate audit and is absent" >&2 fail=1 else + selftest_out=$(mktemp) audit=$("$ROOT/scripts/pin-gate-audit.py" 2>&1) if [ $? -ne 0 ] && [ -z "$audit" ]; then echo "FAIL [invariant 4]: scripts/pin-gate-audit.py did not run" >&2 fail=1 fi echo "$audit" | awk -F'\t' '/^COUNT/{print " pin gates: "$2" "$3" "$4" "$5" "$6}' >&2 + # The census's own POSITIVE CONTROL. Losing a gate to a doc comment is + # SILENT -- the count drops and nothing goes red -- so a control that only + # proved "the scanner finds a bad literal" would not catch it. This one + # plants the exact shape: a comment reproducing the forbidden literal + # inside a gate that reads crate_pin. + if ! "$ROOT/scripts/pin-gate-audit.py" --self-test >"$selftest_out" 2>&1; then + echo "FAIL [invariant 4]: the census self-test did not pass -- a doc comment" >&2 + echo " can reclassify a pin-reading gate into a branch that counts" >&2 + echo " nothing, which loses the gate SILENTLY. Fix the scanner first." >&2 + sed 's/^/ /' "$selftest_out" >&2 + fail=1 + else + note " invariant 4: census self-test passed (a comment cannot steal a gate)" + fi badrows=$(echo "$audit" | awk -F'\t' '/^BADROW/{print " "$2":"$3" "$4" "$5}') bardir=$(echo "$audit" | awk -F'\t' '/^BARDIR/{print " "$2":"$3" "$4" "$5}') absent=$(echo "$audit" | awk -F'\t' '/^NAME/ && $4=="absent"{print " "$2":"$3" "$4}') @@ -376,7 +391,7 @@ echo " EXTRACTED sets, so the blobs are not required at test time" >&2 # --- DRIFT ---------------------------------------------------------------- regen=$(mktemp) patch=$(mktemp) -trap 'rm -f "$regen" "$patch"' EXIT +trap 'rm -f "$regen" "$patch" ${selftest_out:-} ' EXIT if ! "$ROOT/scripts/gen-fixture-library.sh" "$regen" >/dev/null 2>&1; then echo "FAIL: scripts/gen-fixture-library.sh did not run" >&2 exit 1