diff --git a/.github/workflows/data-integrity.yml b/.github/workflows/data-integrity.yml index 362f49a..872f57c 100644 --- a/.github/workflows/data-integrity.yml +++ b/.github/workflows/data-integrity.yml @@ -145,6 +145,12 @@ jobs: - name: A wrong decode still fails at the boundary run: python tests/test_mojibake_boundary.py + - name: Text I/O names its codec and newline + run: python scripts/validation/check_source_portability.py + + - name: The source portability gate can still fail + run: python tests/test_source_portability.py + - name: Serveable zone matches a fresh build (candidates) run: python scripts/build/project_candidates.py --check diff --git a/scripts/validation/check_all.py b/scripts/validation/check_all.py index 38c7479..aa88b73 100644 --- a/scripts/validation/check_all.py +++ b/scripts/validation/check_all.py @@ -115,6 +115,16 @@ # approved rather than re-deriving it. ("descriptions serve only what a human accepted", ["tests/test_description_overlay.py"], True), + # Added 2026-08-26 and wired into nothing, so when it went red on a false + # positive three days later nobody saw it for ten weeks. CI is + # ubuntu-latest, which cannot reproduce the cp1252/CRLF writes this exists + # to prevent, so this gate is the only thing standing between a Windows + # seat and a silently re-encoded record. Found by the 2026-11-05 estate + # pass, card PDOOM-DATA-1. + ("text I/O names its codec and newline", + ["scripts/validation/check_source_portability.py"], True), + ("source portability gate can still fail", + ["tests/test_source_portability.py"], True), ] REBUILD = [ diff --git a/scripts/validation/check_source_portability.py b/scripts/validation/check_source_portability.py index 7a60e31..d8fb23b 100644 --- a/scripts/validation/check_source_portability.py +++ b/scripts/validation/check_source_portability.py @@ -84,6 +84,17 @@ # this entry is now stale cover for whatever lands next. EXEMPT = {os.path.join("scripts", "transformation", "clean_events.py"): 6} +# Modules whose `.open` is not a file opener. The attribute arm below has to +# catch `Path(...).open()` and `io.open()`, so it cannot simply ignore every +# attribute call named `open` -- but it flagged `webbrowser.open(url)` in +# serve_corpus_review.py on 2026-08-29 and the check sat red, unnoticed, for +# ten weeks because nothing ran it. Each entry names why its `open` takes no +# codec. Matched on a bare module name only, so `self.os.open` still counts. +NOT_FILE_OPENERS = { + "webbrowser": "opens a URL in a browser; there is no file and no codec", + "os": "os.open returns a raw descriptor and takes no encoding argument", +} + def source_files(): """Every .py in the repository, repo-relative, in a stable order.""" @@ -120,6 +131,10 @@ def offenders_in(tree): name = "open" elif isinstance(node.func, ast.Attribute) and node.func.attr in ( "open", "read_text", "write_text"): + receiver = node.func.value + if (node.func.attr == "open" and isinstance(receiver, ast.Name) + and receiver.id in NOT_FILE_OPENERS): + continue # Attribute form only. `check_references.read_text` and # `redact_emails.write_text` are module-level helpers of the same # names that already do this correctly, and flagging their call diff --git a/tests/test_source_portability.py b/tests/test_source_portability.py new file mode 100644 index 0000000..5d2aa0e --- /dev/null +++ b/tests/test_source_portability.py @@ -0,0 +1,87 @@ +"""Prove the source-portability gate fires, and fires only where it should. + + python tests/test_source_portability.py + +Why this exists +--------------- +`check_source_portability.py` was added on 2026-08-26 and wired into nothing: +not `check_all.py`, not `data-integrity.yml`. On 2026-08-29 it went red on a +false positive -- `webbrowser.open(url)` read as a file open -- and stayed red +for ten weeks with nobody seeing it, because a check nothing runs cannot fail +anyone. Found by the 2026-11-05 estate protocol pass (coordination +`reports/PASS_2026-11-05_pdoom-data.md`, card PDOOM-DATA-1). + +So the gate's matcher is fed known-bad and known-good calls directly. The +must-not-fire half pins the false positive that hid it; the must-fire half +proves the narrowing did not blind the attribute arm, which still has to catch +`Path(...).open()` and `io.open()`. +""" +import ast +import os +import sys + +REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.join(REPO_ROOT, "scripts", "validation")) + +import check_source_portability as csp + +FAILURES = [] + + +def offenders(source): + return csp.offenders_in(ast.parse(source)) + + +def check(label, condition, detail=""): + if condition: + print(" ok " + label) + else: + print(" FAIL " + label + ((" -- " + detail) if detail else "")) + FAILURES.append(label) + + +MUST_FIRE = [ + ("builtin read without encoding", "open(p)"), + ("builtin write without encoding or newline", "open(p, 'w')"), + ("builtin write with encoding but no newline", "open(p, 'w', encoding='utf-8')"), + ("append mode by keyword, no newline", "open(p, mode='a', encoding='utf-8')"), + ("Path.open without encoding", "Path(p).open()"), + ("io.open without encoding", "io.open(p)"), + ("write_text without newline", "Path(p).write_text(s, encoding='utf-8')"), + ("read_text without encoding", "Path(p).read_text()"), + # The exclusion is by bare module name. An attribute chain that merely + # ends in .os is not the os module and must still be checked. + ("self.os.open is not os.open", "self.os.open(p)"), +] + +MUST_NOT_FIRE = [ + ("webbrowser.open -- the 2026-08-29 false positive", "webbrowser.open(url)"), + ("os.open takes no codec", "os.open(p, os.O_RDONLY)"), + ("binary read", "open(p, 'rb')"), + ("binary write", "open(p, 'wb')"), + ("read naming its encoding", "open(p, encoding='utf-8')"), + ("write naming encoding and newline", "open(p, 'w', encoding='utf-8', newline='\\n')"), + ("io.open done right", "io.open(p, 'w', encoding='utf-8', newline='\\n')"), +] + + +def main(): + print("must fire") + for label, src in MUST_FIRE: + found = offenders(src) + check(label, len(found) == 1, "found %r in %r" % (found, src)) + print("must not fire") + for label, src in MUST_NOT_FIRE: + found = offenders(src) + check(label, found == [], "found %r in %r" % (found, src)) + + if FAILURES: + print("\n%d case(s) failed." % len(FAILURES)) + return 1 + print("\nsource portability: %d must-fire and %d must-not-fire cases pass." + % (len(MUST_FIRE), len(MUST_NOT_FIRE))) + return 0 + + +if __name__ == "__main__": + sys.exit(main())