Jump to content

The Principia Machina/TPC-PY/gen hunk corpus.py

From The Invariance Itself
Revision as of 15:43, 27 September 2026 by BFP Rauch (talk | contribs) (TPC-PY source gen_hunk_corpus.py (0.1.0), SHA-256 4eef7f3bba8ff6a9; conformed TPS 0.3.0; PROPOSED, not declared (CO-8))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
"""gen_hunk_corpus 0.1.0: builds hunk_corpus.json for tpc_hunk 0.1.0.
Expectations are computed by tpc_hunk.py and pinned; hand_checked cases
were worked by hand. Case H30 is landing round 7's request hunk set
(SRC/hunks_request.json, 15 hunks): the baseline (request 0.1.8) is
rebuilt from the delivered 0.1.9 by reverse application and checked
against the recorded baseline SHA-256, then the forward application must
give 0.1.9 byte for byte; the case carries the rebuilt baseline as the
base and 0.1.9 as the expected text (MEASURED). The record and README sets
are replayed by verify_lr7_hunks.py, too large for the corpus.
SRC is hunk_inputs/lr7_request beside this file (GEN-PATHS, 2026-09-27):
hunks_request.json and tpm_tps_thread_request_0_1_9.txt, rebuilt from this
corpus's own H30 and checked equal to the reverse application of landing
round 8's request hunks to request 0.1.10. The environment variable
TPM_SRC, when set, names another folder holding the same two files. The
corpus is refused, not written short, when they are missing."""
import hashlib
import json
import os
import sys

here = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, here)
from tpc_hunk import hunk_apply, run_case  # noqa: E402

SRC = os.environ.get("TPM_SRC") or os.path.join(here, "hunk_inputs", "lr7_request")


def H(old, new, **extra):
    d = {"old": old, "new": new}
    d.update(extra)
    return d


base1 = "The quick brown fox\njumps over the lazy dog.\nEND\n"
cases = [
 ("H01 one hunk", base1, [H("brown", "red")], True),
 ("H02 three hunks in position order", base1, [H("quick", "slow"), H("lazy", "sleeping"), H("END", "FIN")], True),
 ("H03 three hunks listed out of position order: applied by position, the same text", base1, [H("END", "FIN"), H("quick", "slow"), H("lazy", "sleeping")], True),
 ("H04 an old string absent from the base: ABSENT at hunk 2", base1, [H("quick", "slow"), H("cat", "dog")], True),
 ("H05 an old string occurring more than once (\"o\" four times): MULTIPLE", base1, [H("o", "0")], True),
 ("H06 overlapping hunks: OVERLAP at hunk 2", base1, [H("brown fox", "red fox"), H("fox\njumps", "fox leaps")], True),
 ("H07 adjacent hunks (one ends where the next begins) do not overlap", base1, [H("quick ", "slow "), H("brown", "red")], True),
 ("H08 an empty old string: EMPTY", base1, [H("", "x")], True),
 ("H09 an empty hunk list: OK, the base unchanged", base1, [], True),
 ("H10 old equal to new: a no-op, applied", base1, [H("fox", "fox")], True),
 ("H11 overlapping occurrences count: \"aa\" in \"aaa\" occurs twice, MULTIPLE", "xaaay", [H("aa", "b")], True),
 ("H12 a later hunk's old string found only inside an earlier hunk's new text: ABSENT (located in the base)", base1, [H("brown", "bright green"), H("green", "blue")], True),
 ("H13 a deletion hunk (new empty)", base1, [H("quick ", "")], True),
 ("H14 an insertion by anchor", base1, [H("END\n", "END\nAppendix\n")], True),
 ("H15 the old string is the whole base", base1, [H(base1, "replaced\n")], True),
 ("H16 NFC and NFD spellings are not equal: ABSENT (no normalization)", "café au lait", [H("café", "thé")], True),
 ("H17 multi-byte text: CJK, an emoji, combining marks", "模型 \U0001F600 é αβγ", [H("\U0001F600", "\U0001F601\U0001F602"), H("β", "ββ")], True),
 ("H18 CRLF against LF: ABSENT", "a\r\nb\r\n", [H("a\nb", "c")], True),
 ("H19 the render escapes | and backslash; a tab is \\x09", "x|y\\z\tw", [H("|", "||")], True),
 ("H20 two hunks with one old string: the second overlaps the first", base1, [H("fox", "cat"), H("fox", "dog")], True),
 ("H21 optional keys n, anchor and count are accepted", base1, [H("fox", "cat", n=1, anchor="animal", count=1)], True),
 ("H22 an unknown key is refused", base1, [H("fox", "cat", extra=1)], True),
 ("H23 a hunk without new is refused", base1, [{"old": "fox"}], True),
 ("H24 hunks not a list is refused", base1, {"old": "fox", "new": "cat"}, True),
 ("H25 a base that is not a string is refused", 12, [H("1", "2")], True),
 ("H26 an old string that is not a string is refused", base1, [H(5, "x")], True),
 ("H27 a needle spanning a line end", base1, [H("fox\njumps", "fox leaps")], True),
 ("H28 three hunks, the third out of order and overlapping the first: OVERLAP", base1, [H("quick brown", "slow red"), H("lazy", "idle"), H("brown fox", "fox")], True),
 ("H29 an empty base with an empty hunk list", "", [], True),
]
out = []
for name, base, hunks, hand in cases:
    args = {"base": base, "hunks": hunks}
    out.append({"name": name, "fn": "hunk", "args": args, "hand_checked": hand,
                "expected": run_case("hunk", args)})

# ---- H30: landing round 7's request hunk set, MEASURED ---------------------
req = os.path.join(SRC, "hunks_request.json")
if os.path.exists(req):
    d = json.load(open(req, encoding="utf-8"))
    target = open(os.path.join(SRC, d["target"]), encoding="utf-8",
                  newline="").read()
    rev = hunk_apply(target, [{"old": h["new"], "new": h["old"]} for h in d["hunks"]])
    assert rev["status"] == "OK", rev
    base = rev["text"]
    assert hashlib.sha256(base.encode("utf-8")).hexdigest() == d["baseline_sha256"]
    fwd = hunk_apply(base, [{"old": h["old"], "new": h["new"]} for h in d["hunks"]])
    assert fwd["status"] == "OK" and fwd["text"] == target
    args = {"base": base, "hunks": [{"old": h["old"], "new": h["new"], "n": h["n"], "anchor": h["anchor"], "count": h["count"]} for h in d["hunks"]]}
    out.append({"name": "H30 landing round 7's request hunk set (SRC/hunks_request.json, 15 hunks): baseline 0.1.8 rebuilt by reverse application, SHA-256 %s; forward gives request 0.1.9, SHA-256 %s" % (d["baseline_sha256"][:16], fwd["sha256"][:16]),
                "fn": "hunk", "args": args, "hand_checked": False, "expected": run_case("hunk", args)})
    print("H30 request set: baseline sha256 match, forward byte-identical")
else:
    sys.exit("refusing to write: H30's inputs not found in %s" % SRC)

with open(os.path.join(here, "hunk_corpus.json"), "w", encoding="utf-8",
          newline="\n") as f:
    json.dump({"corpus": "hunk", "version": "0.1.0", "cases": out}, f,
              indent=1, sort_keys=True, ensure_ascii=False)
print("cases", len(out))