#!/usr/bin/env python3 """C1 positive control for the anchor probe (pre-registered in prereg-anchor.md). Local, no server. Two cases, both written as plain `diff -u` output with no `index` line, with the base deleted from disk before the probe runs: CASE U (unique context) — every hunk's pre-image slice occurs exactly once. The probe must reconstruct the post-version byte-for-byte. CASE A (repetitive context) — a hunk whose pre-image slice occurs twice (overlapping). The probe must REFUSE the row, not mis-apply it. This case is the control for the non-overlapping `str.count()` bug found on 2026-09-23: `"t\n"*3` inside `"t\n"*4` reports count 1 to `str.count` but has two occurrences, and a first-match replacement then writes the insertion at the wrong offset. PASS = CASE U reconstructs exactly AND CASE A is refused (and CASE A, if the buggy path were used, would have produced a wrong file — checked explicitly). """ import os, sys, tempfile, subprocess, hashlib, json HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) from anchor_probe import parse_patch, locate, occ_offsets from identify_base import apply_hunks OUT = {} def make_case(A, B, name): d = tempfile.mkdtemp(prefix="c1-anchor-") pa, pb = os.path.join(d, "a.txt"), os.path.join(d, "b.txt") open(pa, "w").write(A) open(pb, "w").write(B) p = subprocess.run(["diff", "-u", "a.txt", "b.txt"], cwd=d, capture_output=True, text=True) os.remove(pa) # the base is gone from disk, exactly the served situation secs = parse_patch(p.stdout) rebuilt, notes = apply_hunks(A, secs[0]["hunks"]) naive = None try: # the buggy path, for the record: str.count + first-match replace cur = A for hk in secs[0]["hunks"]: if cur.count(hk["pre_slice"]) != 1: cur = None break cur = cur.replace(hk["pre_slice"], hk["post_slice"], 1) naive = cur except Exception as e: naive = f"error {e}" rec = {"patch": p.stdout, "patch_sha256": hashlib.sha256(p.stdout.encode()).hexdigest(), "has_index_line": ("index " in p.stdout or "diff --git" in p.stdout), "n_hunks": sum(len(s["hunks"]) for s in secs), "hunk_occ": [{"header": h["header"], "occ_in_base": len(occ_offsets(A, h["pre_slice"]))} for s in secs for h in s["hunks"]], "rebuilt_equals_post": rebuilt == B, "rebuilt_is_none": rebuilt is None, "naive_rebuilt_equals_post": (naive == B), "naive_note": (naive if isinstance(naive, str) and len(naive) < 30 else "produced text"), "base_deleted_from_disk": not os.path.exists(pa), "post_sha256": hashlib.sha256(B.encode()).hexdigest()} OUT[name] = rec return rec # CASE U: distinct context lines, unique anchors A_U = "\n".join(f"alpha line {i}" for i in range(8)) + "\nBASE-ONLY-MARKER\n" + \ "\n".join(f"common line {i}" for i in range(12)) + "\ntail-U\n" B_U = A_U.replace("BASE-ONLY-MARKER", "POST-ONLY-MARKER") + "appended-B\n" make_case(A_U, B_U, "case_unique") # CASE A: repetitive context lines, one hunk with two occurrences of its pre-image slice A_A = "head\n" + ("tail-A\n" * 4) B_A = "head\n" + ("tail-A\n" * 4) + ("appended-B\n" * 3) make_case(A_A, B_A, "case_ambiguous") print(json.dumps(OUT, indent=1)[:2600]) u, a = OUT["case_unique"], OUT["case_ambiguous"] ok = (u["rebuilt_equals_post"] and u["hunk_occ"] and all(h["occ_in_base"] == 1 for h in u["hunk_occ"]) and a["rebuilt_is_none"] and a["naive_rebuilt_equals_post"] is False) print("\nC1 PASS" if ok else "\nC1 FAIL") print("unique case: rebuilt==post:", u["rebuilt_equals_post"]) print("ambiguous case: refused:", a["rebuilt_is_none"], "| buggy path would have produced post:", a["naive_rebuilt_equals_post"]) json.dump(OUT, open(os.path.join(HERE, "control_c1.json"), "w"), indent=1)