diff --git a/portable/patches/apply_all.sh b/portable/patches/apply_all.sh index 2ac67814..7dc5eba6 100755 --- a/portable/patches/apply_all.sh +++ b/portable/patches/apply_all.sh @@ -89,9 +89,61 @@ PATCHES=( "$ROOT/0019-attestations-close-file-handles.patch" ) +# Content-level already-applied test for a unified diff against the current +# tree: for every target file, all '+' lines must be present and all '-' lines +# (that are not re-added) must be absent. Whitespace-stripped line matching; +# a patch that deletes or creates files never qualifies. +semantic_already_applied() { + python3 - "$1" <<'PY' +import sys +from pathlib import Path + +patch = Path(sys.argv[1]).read_text(encoding="utf-8", errors="replace").splitlines() +files = {} +target = None +for line in patch: + if line.startswith("+++ "): + name = line[4:].split("\t")[0].strip() + if name == "/dev/null": + sys.exit(1) + target = name[2:] if name.startswith("b/") else name + files.setdefault(target, ([], [])) + elif line.startswith("--- "): + if line[4:].split("\t")[0].strip() == "/dev/null": + sys.exit(1) + elif target is not None and line.startswith("+") and not line.startswith("+++"): + files[target][0].append(line[1:].strip()) + elif target is not None and line.startswith("-") and not line.startswith("---"): + files[target][1].append(line[1:].strip()) + +if not files: + sys.exit(1) +for name, (added, removed) in files.items(): + path = Path(name) + if not path.is_file(): + sys.exit(1) + tree = {ln.strip() for ln in path.read_text(encoding="utf-8", errors="replace").splitlines()} + added_set = {ln for ln in added if ln} + removed_only = {ln for ln in removed if ln} - added_set + if not added_set and not removed_only: + sys.exit(1) + if not added_set <= tree: + sys.exit(1) + if removed_only & tree: + sys.exit(1) +sys.exit(0) +PY +} + # Batch 231: idempotent apply — if a patch is already on the tree (Path C landed # @ 93a4ecd / PR #64), skip it via reverse --check. Fail only when neither # forward nor reverse applies (real conflict / tip drift). +# Outcome counters (summary line lets callers decide whether the whole stack is +# already on the tree — the tip-drift tolerance signal — without parsing echoes). +N_FORWARD=0 +N_ALREADY=0 +N_SKIPPED=0 + apply_one() { local p="$1" local check_only="${2:-0}" @@ -101,6 +153,7 @@ apply_one() { bn="$(basename "$p")" if [[ ( "$bn" == 0018-* || "$bn" == 0019-* ) && ! -d attestations ]]; then echo "skip (attestations/ absent): ${bn}" + N_SKIPPED=$((N_SKIPPED + 1)) return 0 fi # Batch 378 tip-sync @ebedb78: 0018/0019 already on tip but context drifted @@ -108,21 +161,17 @@ apply_one() { # Treat semantic already-applied as success so keep-prior refresh can proceed. if [[ "$bn" == 0018-* ]] && grep -q '"attestations"' engine/bridge/work_order.py 2>/dev/null; then echo "already-applied (semantic): ${bn}" + N_ALREADY=$((N_ALREADY + 1)) return 0 fi if [[ "$bn" == 0019-* ]] && grep -q 'with open(candidate' tools/attestations_check.py 2>/dev/null; then echo "already-applied (semantic): ${bn}" + N_ALREADY=$((N_ALREADY + 1)) return 0 fi - # Tip @e7652a1 (2026-09-27): 0017 close-handles already on tip but the third - # hunk's context drifted (survey → full_survey). Reverse --check fails while - # the semantic content is present: no bare INDEX_REL open().read() remains. - if [[ "$bn" == 0017-* && -f tests/test_pinned_sources.py ]] \ - && grep -q 'with open(os.path.join(ROOT, INDEX_REL)' tests/test_pinned_sources.py \ - && ! grep -q 'open(os.path.join(ROOT, INDEX_REL), encoding="utf-8").read()' tests/test_pinned_sources.py; then - echo "already-applied (semantic): ${bn}" - return 0 - fi + # 0017 (tip @e7652a1: third-hunk context drifted survey → full_survey while the + # close-handles change itself is on tip) is covered by the generic + # semantic_already_applied() fallback below — no per-patch grep needed. if git apply --check "$p" >/dev/null 2>&1; then if [[ "$check_only" -eq 0 ]]; then git apply "$p" @@ -133,10 +182,22 @@ apply_one() { git apply "$p" echo "check-ok: $(basename "$p")" fi + N_FORWARD=$((N_FORWARD + 1)) return 0 fi if git apply --reverse --check "$p" >/dev/null 2>&1; then echo "already-applied: $(basename "$p")" + N_ALREADY=$((N_ALREADY + 1)) + return 0 + fi + # Batch 810 tip-sync @e7652a1: landed patches whose surrounding context later + # drifted (e.g. 0017 after main #140 grew tests/test_pinned_sources.py) fail + # both forward and reverse --check although every hunk is on the tree. Generic + # semantic check: every added line present and every removed line absent in + # each target file ⇒ already-applied. Anything else still fails closed. + if semantic_already_applied "$p"; then + echo "already-applied (semantic): ${bn}" + N_ALREADY=$((N_ALREADY + 1)) return 0 fi echo "error: patch does not apply (forward or reverse): $(basename "$p")" >&2 @@ -152,6 +213,18 @@ apply_series() { done } +# Machine-readable stack summary. already_on_tip=1 iff no patch needed a forward +# apply (every patch already-applied or intentionally skipped) — this is the +# signal tip-drift gates use to tolerate BASE_TIP lagging a moved hardening tip +# once Path C has landed. Any forward apply or failure keeps it 0. +print_summary() { + local on_tip=0 + if [[ "$N_FORWARD" -eq 0 && "$N_ALREADY" -gt 0 ]]; then + on_tip=1 + fi + echo "apply_all: summary forward=${N_FORWARD} already_applied=${N_ALREADY} skipped=${N_SKIPPED} already_on_tip=${on_tip} head=$(git rev-parse --short HEAD 2>/dev/null || echo unknown)" +} + if [[ "$CHECK_ONLY" -eq 1 ]]; then # Disposable worktree so sequential deps validate without dirtying the caller's tree. WT="$(mktemp -d "${TMPDIR:-/tmp}/trial-apply-all-check.XXXXXX")" @@ -160,17 +233,22 @@ if [[ "$CHECK_ONLY" -eq 1 ]]; then } trap cleanup EXIT git worktree add --detach "$WT" HEAD >/dev/null - ( - cd "$WT" - apply_series 1 - ) + # pushd (not a subshell) so the outcome counters survive for print_summary. + pushd "$WT" >/dev/null + if ! apply_series 1; then + popd >/dev/null + exit 1 + fi + popd >/dev/null trap - EXIT cleanup + print_summary echo "Check OK (no changes applied; already-applied patches skipped)." exit 0 fi apply_series 0 +print_summary echo "Applied (or already on tip). Recommended verification:" echo " python3 tools/math_status_check.py" echo " python3 -m pytest -q tests/test_carriers.py tests/test_math_status.py tests/test_inventable_jetmod_probes.py tests/test_gaussian_moments.py tests/test_inventable_jetmod_instrumentation_status.py tests/test_claims.py tests/test_recovery.py" diff --git a/scripts/tip_drift_class.py b/scripts/tip_drift_class.py index bd6568d4..56d65a34 100755 --- a/scripts/tip_drift_class.py +++ b/scripts/tip_drift_class.py @@ -195,8 +195,15 @@ def path_c_landed(verify_file: Path = VERIFY_FILE) -> bool: return False +def strict_tip() -> bool: + """PATH_C_STRICT_TIP=1 restores the pre-b3c6 exact-equality gate (operator escape hatch).""" + return os.environ.get("PATH_C_STRICT_TIP") == "1" + + def acceptable(cls: str, landed: bool) -> bool: - """MATCH always; DESCENDANT only when Path C is landed on tip.""" + """MATCH always; DESCENDANT only when Path C is landed on tip (and not strict).""" + if strict_tip(): + return cls == "MATCH" return cls == "MATCH" or (cls == "DESCENDANT" and landed) diff --git a/tests/test_apply_all_summary.py b/tests/test_apply_all_summary.py new file mode 100644 index 00000000..eaa62928 --- /dev/null +++ b/tests/test_apply_all_summary.py @@ -0,0 +1,165 @@ +"""apply_all.sh: generic semantic already-applied check + machine-readable summary. + +A landed portable patch whose surrounding context later drifted on the +hardening tip (0017 after main #140: ``survey -> full_survey``) fails both +forward and reverse ``git apply --check`` although every hunk is on the tree. +``semantic_already_applied()`` recognises that case generically, and +``apply_all: summary ... already_on_tip=0|1`` tells callers whether the whole +stack is on the tree without parsing echoes. + +Repository-intent only. Not evidence about any claim; scientific effect NONE. +""" + +from __future__ import annotations + +import os +import re +import subprocess +import sys +import tempfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +APPLY_ALL = ROOT / "portable" / "patches" / "apply_all.sh" + + +def _extract_semantic_check() -> str: + """Pull the python body of semantic_already_applied() out of the bash file.""" + text = APPLY_ALL.read_text(encoding="utf-8") + m = re.search(r"semantic_already_applied\(\) \{\n python3 - \"\$1\" <<'PY'\n(.*?)\nPY\n\}", text, re.S) + assert m, "semantic_already_applied() heredoc not found" + return m.group(1) + + +def _semantic(patch_text: str, tree: dict[str, str]) -> int: + """Run the extracted check against a temp tree; return its exit code.""" + body = _extract_semantic_check() + with tempfile.TemporaryDirectory(prefix="apply-all-sem-") as td: + root = Path(td) + for rel, content in tree.items(): + p = root / rel + p.parent.mkdir(parents=True, exist_ok=True) + p.write_text(content, encoding="utf-8") + patch = root / "x.patch" + patch.write_text(patch_text, encoding="utf-8") + proc = subprocess.run( + [sys.executable, "-", str(patch)], + input=body, + capture_output=True, + text=True, + cwd=str(root), + check=False, + ) + return proc.returncode + + +PATCH = """\ +diff --git a/pkg/mod.py b/pkg/mod.py +--- a/pkg/mod.py ++++ b/pkg/mod.py +@@ -1,4 +1,5 @@ + import os +-data = open(path).read() ++with open(path, encoding="utf-8") as fh: ++ data = fh.read() + print(data) +""" + + +def test_script_has_summary_and_generic_check() -> None: + text = APPLY_ALL.read_text(encoding="utf-8") + assert "semantic_already_applied()" in text + assert 'semantic_already_applied "$p"' in text + assert "apply_all: summary forward=" in text + assert "already_on_tip=" in text + # Counters must survive the --check worktree (pushd, not a subshell). + assert 'pushd "$WT"' in text + # No per-patch grep for 0017 any more; the generic fallback covers it. + assert '"$bn" == 0017-*' not in text + assert subprocess.run(["bash", "-n", str(APPLY_ALL)], check=False).returncode == 0 + + +def test_semantic_check_accepts_applied_with_drifted_context() -> None: + tree = { + "pkg/mod.py": ( + "import os\n" + "# context drifted after landing\n" + "with open(path, encoding=\"utf-8\") as fh:\n" + " data = fh.read()\n" + "print(data) # also drifted\n" + ) + } + assert _semantic(PATCH, tree) == 0 + + +def test_semantic_check_rejects_unapplied_tree() -> None: + tree = {"pkg/mod.py": "import os\ndata = open(path).read()\nprint(data)\n"} + assert _semantic(PATCH, tree) == 1 + + +def test_semantic_check_rejects_partial_apply() -> None: + # '+' lines present but the removed line is still there → not applied. + tree = { + "pkg/mod.py": ( + "import os\n" + "data = open(path).read()\n" + "with open(path, encoding=\"utf-8\") as fh:\n" + " data = fh.read()\n" + "print(data)\n" + ) + } + assert _semantic(PATCH, tree) == 1 + + +def test_semantic_check_rejects_missing_target_and_create_delete() -> None: + assert _semantic(PATCH, {}) == 1 + create = PATCH.replace("--- a/pkg/mod.py", "--- /dev/null") + assert _semantic(create, {"pkg/mod.py": "x\n"}) == 1 + delete = PATCH.replace("+++ b/pkg/mod.py", "+++ /dev/null") + assert _semantic(delete, {"pkg/mod.py": "x\n"}) == 1 + + +def test_check_refuses_non_hardening_tree_before_summary() -> None: + with tempfile.TemporaryDirectory(prefix="apply-all-shape-") as td: + subprocess.run(["git", "init", "-q", td], check=True) + (Path(td) / "README.md").write_text("not hardening\n", encoding="utf-8") + subprocess.run(["git", "-C", td, "add", "."], check=True) + subprocess.run( + ["git", "-C", td, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "x"], + check=True, + ) + proc = subprocess.run( + ["bash", str(APPLY_ALL), "--check"], cwd=td, capture_output=True, text=True, check=False + ) + assert proc.returncode == 2 + assert "apply_all: summary" not in proc.stdout + + +def test_live_tip_summary_is_consistent() -> None: + """Live: on the current hardening tip the summary must be self-consistent + (already_on_tip=1 iff forward=0 and already_applied>0). Skipped on transport.""" + env = dict(os.environ) + env["GIT_TERMINAL_PROMPT"] = "0" + with tempfile.TemporaryDirectory(prefix="apply-all-live-") as td: + clone = subprocess.run( + [ + "git", "clone", "-q", "--depth", "1", "--branch", + "chatgpt/drive-github-hardening-20260919", + "https://github.com/d6g8k5htny-coder/main.git", td, + ], + capture_output=True, text=True, env=env, check=False, timeout=300, + ) + if clone.returncode != 0: + return + proc = subprocess.run( + ["bash", str(APPLY_ALL), "--check"], cwd=td, capture_output=True, text=True, check=False, timeout=300 + ) + assert proc.returncode == 0, proc.stdout + proc.stderr + m = re.search( + r"^apply_all: summary forward=(\d+) already_applied=(\d+) skipped=(\d+) already_on_tip=([01])", + proc.stdout, re.M, + ) + assert m, proc.stdout + forward, already, _skipped, on_tip = (int(m.group(1)), int(m.group(2)), int(m.group(3)), m.group(4)) + assert (on_tip == "1") == (forward == 0 and already > 0) + assert "Check OK" in proc.stdout diff --git a/tests/test_tip_drift_class.py b/tests/test_tip_drift_class.py index ec3839df..d3840fe7 100644 --- a/tests/test_tip_drift_class.py +++ b/tests/test_tip_drift_class.py @@ -78,6 +78,17 @@ def test_acceptable_policy(): assert tdc.acceptable(cls, landed=False) is False +def test_strict_tip_env_restores_exact_equality(monkeypatch): + """PATH_C_STRICT_TIP=1: only MATCH is acceptable, even when Path C is landed.""" + monkeypatch.setenv("PATH_C_STRICT_TIP", "1") + assert tdc.strict_tip() is True + assert tdc.acceptable("MATCH", landed=True) is True + assert tdc.acceptable("DESCENDANT", landed=True) is False + monkeypatch.delenv("PATH_C_STRICT_TIP") + assert tdc.strict_tip() is False + assert tdc.acceptable("DESCENDANT", landed=True) is True + + def test_bad_env_token_falls_back_to_anonymous(monkeypatch): """A 401 on the authenticated compare call must not yield UNKNOWN.""" import urllib.error @@ -274,9 +285,14 @@ def test_gates_carry_descendant_ok_and_keep_hard_fail(): def test_apply_all_0017_semantic_guard_present(): + """0017's drifted context is handled by the generic semantic already-applied + fallback (every '+' line present, every removed line absent) rather than a + per-patch grep; the fallback runs after forward and reverse --check fail.""" text = (ROOT / "portable" / "patches" / "apply_all.sh").read_text(encoding="utf-8") - assert '"$bn" == 0017-*' in text + assert "semantic_already_applied()" in text + assert 'semantic_already_applied "$p"' in text assert "already-applied (semantic)" in text + assert "0017" in text def test_guard_prefers_checkout_head_over_wrong_tip_sha(tmp_path):