diff --git a/.github/ci/delivery-cutover.md b/.github/ci/delivery-cutover.md index 2520f7ff..546a1a3b 100644 --- a/.github/ci/delivery-cutover.md +++ b/.github/ci/delivery-cutover.md @@ -176,11 +176,20 @@ refusals from incomplete fast evidence and reread races before a fresh attempt. Unrelated description prose may differ between the triggering source event and the live PR, for example when a summary is appended before CI starts. Both descriptions must independently contain the unique supported shadow/delivery -opt-ins and neither may declare a dependency. Every other snapshot field and -all Git/fast provenance checks remain exact. Later live rereads still compare +opt-ins and neither may declare a dependency. Every other snapshot field except +the bounded generated hint below, and all Git/fast provenance checks, remain exact. Later live rereads still compare the complete body strictly; this does not ignore changes during verification, trust an editing actor, grant queue authority or widen eligible files/actions. +At the initial source or genuine candidate PR comparison only, generated +`merge_commit_sha` hints may be missing/null or equal the trusted workflow +`GITHUB_SHA`. Both hints must qualify; unknown or malformed non-null hints retain +full even when both snapshots agree. These hints never identify the tested +commit: the actual commit and ordered current-main/head parents are still +independently verified. Candidate bodies remain exact, and genuine admission +and source proofs remain mandatory. Subsequent live rereads compare the original +PR strictly, including the full body and generated hint; no race is normalized. + ## Exact-head local review receipts This is an **alternative review contract**, not activation or merge diff --git a/.github/ci/delivery_route.py b/.github/ci/delivery_route.py index fdedb8b7..845f83d6 100644 --- a/.github/ci/delivery_route.py +++ b/.github/ci/delivery_route.py @@ -33,6 +33,7 @@ "Source event ref differs": "source-event-ref", "Source test-merge identity differs": "source-test-merge", "Source moved during verification": "source-reread", + "Event merge hint is neither absent nor current": "event-merge-hint", "Missing or ambiguous fast check": "fast-check-count", "Fast check is not successful exact-head Actions evidence": "fast-check-identity", "Fast evidence is not successful or authenticated pending work": "fast-state", @@ -115,20 +116,33 @@ def current_run(env, request): return run +def current_merge_hints(env, pr, expected): + tested = policy.sha(env["GITHUB_SHA"]) + return all(hint is None or isinstance(hint, str) and hint == tested + for hint in (pr.get("merge_commit_sha"), expected.get("merge_commit_sha"))) + + def source(env, event, root, main, request, base_changes): require(event["action"] in {"opened", "synchronize", "reopened"}, "Unsupported PR event") number = evidence.positive(event["number"]) pr = request(REPOSITORY, f"pulls/{number}") expected = event["pull_request"] - live_snapshot = policy.snapshot(dict(pr, body=expected.get("body"))) + hints_current = current_merge_hints(env, pr, expected) + comparison = dict(pr, body=expected.get("body")) + if hints_current: + comparison["merge_commit_sha"] = expected.get("merge_commit_sha") + live_snapshot = policy.snapshot(comparison) event_snapshot = policy.snapshot(expected) if live_snapshot != event_snapshot: names = ("number", "state", "draft", "merged", "body", "merge_commit_sha", "head", "base", "author") diagnostic("source", "snapshot-mismatch", fields=[name for name, live, old in zip(names, live_snapshot, event_snapshot) if live != old]) + # Generated hints are not tested-commit authority. Keep ordered Git-parent + # verification below and strict subsequent rereads of the original live PR. + require(hints_current, "Event merge hint is neither absent nor current") # Prose may change before routing. Validate both decisions independently; - # retain every other snapshot field and the strict subsequent live rereads. + # retain all other snapshot fields and strict subsequent live rereads. require(verifier.delivery_intended(expected.get("body")) and not re.search(r"(?m)^Depends-On:", expected["body"]) and live_snapshot == event_snapshot @@ -156,7 +170,10 @@ def duplicate(env, event, root, main, request, fetch, threads, base_changes): number = evidence.positive(event["number"]) pr = request(REPOSITORY, f"pulls/{number}") head, branch = policy.side(pr, "head") - require(policy.snapshot(pr) == policy.snapshot(event["pull_request"]) + expected = event["pull_request"] + require(current_merge_hints(env, pr, expected), "Event merge hint is neither absent nor current") + comparison = dict(pr, merge_commit_sha=expected.get("merge_commit_sha")) + require(policy.snapshot(comparison) == policy.snapshot(expected) and pr["number"] == number and pr["state"] == "open" and pr["draft"] is True and pr["user"]["login"] == "mergify[bot]" and policy.side(pr, "base") == (main, "main") diff --git a/tests/test_ci_cutover.py b/tests/test_ci_cutover.py index 66a9ad34..f8bc4d16 100644 --- a/tests/test_ci_cutover.py +++ b/tests/test_ci_cutover.py @@ -134,13 +134,14 @@ def test_ci_cutover_source_snapshot_diagnostic_has_names_not_values(cutover, bat env, event = source_fixture(cutover, batch) if field == "merge_commit_sha": batch["prs"][7][field] = env["GITHUB_SHA"] - event["pull_request"][field] = None + event["pull_request"][field] = "f" * 40 if field == "merge_commit_sha" else None batch["prs"][7]["body"] += "\nPrivate prose must never be logged." assert route_fixture(cutover, batch, env, event) == cutover.FULL rows = [json.loads(line) for line in capsys.readouterr().err.splitlines()] assert rows == [ {"delivery_diagnostic": "snapshot-mismatch", "stage": "source", "fields": [field]}, - {"delivery_diagnostic": "source-metadata", "stage": "source"}, + {"delivery_diagnostic": "event-merge-hint" if field == "merge_commit_sha" else "source-metadata", + "stage": "source"}, ] @@ -487,6 +488,111 @@ def duplicate_fixture(cutover, batch, artifact): return env, event, data +@pytest.fixture(params=["source", "candidate"]) +def merge_hint_case(request, cutover, batch): + if request.param == "source": + env, event = source_fixture(cutover, batch) + data = None + else: + env, event, data = duplicate_fixture(cutover, batch, request.getfixturevalue("artifact")) + return env, event, data, batch["data"][f"pulls/{event['number']}"] + + +def hint_route(cutover, batch, case, request=None): + env, event, data, _live = case + return cutover.route(env, event, batch["root"], request or request_for(batch), + lambda _id: data, lambda number: threads(number, batch), base_changes) + + +@pytest.mark.parametrize("action", ["opened", "synchronize", "reopened"]) +@pytest.mark.parametrize("event_hint,live_hint", [ + ("missing", "current"), ("null", "current"), ("current", "missing"), + ("current", "null"), ("current", "current"), ("null", "null"), +]) +def test_ci_cutover_generated_merge_hint_transition_preserves_proof(cutover, batch, merge_hint_case, + action, event_hint, live_hint): + env, event, _data, live = merge_hint_case + expected = hint_route(cutover, batch, merge_hint_case) + assert expected["omit_full"] is True + event["action"] = action + for pr, hint in ((event["pull_request"], event_hint), (live, live_hint)): + if hint == "missing": + pr.pop("merge_commit_sha", None) + else: + pr["merge_commit_sha"] = None if hint == "null" else env["GITHUB_SHA"] + if expected["mode"] == "source": + live["body"] += "\nUnrelated summary." + assert hint_route(cutover, batch, merge_hint_case) == expected + + +@pytest.mark.parametrize("side", ["event", "live", "both"]) +@pytest.mark.parametrize("hint", ["f" * 40, "malformed", True, 1, {}, []], + ids=["foreign-sha", "malformed", "boolean", "number", "object", "list"]) +def test_ci_cutover_generated_merge_hint_unknown_retains_full(cutover, batch, merge_hint_case, + side, hint): + env, event, _data, live = merge_hint_case + assert hint_route(cutover, batch, merge_hint_case)["omit_full"] is True + event["pull_request"]["merge_commit_sha"] = live["merge_commit_sha"] = env["GITHUB_SHA"] + if side in {"event", "both"}: + event["pull_request"]["merge_commit_sha"] = hint + if side in {"live", "both"}: + live["merge_commit_sha"] = hint + assert hint_route(cutover, batch, merge_hint_case) == cutover.FULL + + +@pytest.mark.parametrize("commit", ["base", "source-head"]) +def test_ci_cutover_generated_merge_hint_cannot_replace_tested_parents(cutover, batch, + merge_hint_case, commit, capsys): + env, event, _data, live = merge_hint_case + env["GITHUB_SHA"] = batch["base"] if commit == "base" else batch["heads"][0] + event["pull_request"]["merge_commit_sha"] = None + live["merge_commit_sha"] = env["GITHUB_SHA"] + assert hint_route(cutover, batch, merge_hint_case) == cutover.FULL + rows = [json.loads(line) for line in capsys.readouterr().err.splitlines()] + assert rows == [{"delivery_diagnostic": "source-test-merge" if event["number"] == 7 + else "incomplete-evidence", "stage": "source" if event["number"] == 7 else "candidate"}] + + +def test_ci_cutover_generated_merge_hint_live_reread_stays_strict(cutover, batch, merge_hint_case): + env, event, _data, live = merge_hint_case + event["pull_request"]["merge_commit_sha"] = None + live["merge_commit_sha"] = env["GITHUB_SHA"] + reader, reads = request_for(batch), 0 + def request(repo, endpoint): + nonlocal reads + if endpoint == f"pulls/{event['number']}": + reads += 1 + if reads == 2: + live["merge_commit_sha"] = None + return reader(repo, endpoint) + assert hint_route(cutover, batch, merge_hint_case, request) == cutover.FULL + assert reads == 2 + + +def test_ci_cutover_generated_merge_hint_keeps_candidate_body_exact(cutover, batch, artifact): + env, event, data = duplicate_fixture(cutover, batch, artifact) + live = batch["data"]["pulls/9"] + event["pull_request"]["merge_commit_sha"] = None + live["merge_commit_sha"] = env["GITHUB_SHA"] + live["body"] += "\nChanged candidate body." + assert hint_route(cutover, batch, (env, event, data, live)) == cutover.FULL + + +@pytest.mark.parametrize("failure", ["missing", "corrupt"]) +def test_ci_cutover_generated_merge_hint_still_requires_admission(cutover, batch, artifact, failure): + env, event, data = duplicate_fixture(cutover, batch, artifact) + live = batch["data"]["pulls/9"] + event["pull_request"]["merge_commit_sha"] = None + live["merge_commit_sha"] = env["GITHUB_SHA"] + if failure == "missing": + batch["data"]["actions/runs/601/artifacts?per_page=100&page=1"] = {"total_count": 0, "artifacts": []} + else: + artifact["record"]["proof"]["members"][0]["tree"] = "0" * 40 + data, _ = install_artifact(cutover, batch, artifact) + expected = {**cutover.FULL, "retryable": True} if failure == "missing" else cutover.FULL + assert hint_route(cutover, batch, (env, event, data, live)) == expected + + def test_ci_cutover_candidate_duplicate_requires_real_admission(cutover, batch, artifact): env, event, data = duplicate_fixture(cutover, batch, artifact) result = cutover.route(env, event, batch["root"], request_for(batch), lambda _id: data,