diff --git a/.github/ci/delivery-cutover.md b/.github/ci/delivery-cutover.md index c769b19a..2520f7ff 100644 --- a/.github/ci/delivery-cutover.md +++ b/.github/ci/delivery-cutover.md @@ -166,6 +166,13 @@ retain their original full checks; this is not an arbitrary-lane fast path. ## Source-event description updates +Routing refusals emit fixed-vocabulary diagnostic codes to stderr, with the +verification stage. A source snapshot mismatch reports only mismatched field +names, never their values. Exception text, PR bodies and raw API payloads are +not logged. Diagnostics do not change the full-fallback result, retry policy, +proof schema, skip decision or merge authority. They distinguish early metadata +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 diff --git a/.github/ci/delivery_route.py b/.github/ci/delivery_route.py index aa4c3a9b..fdedb8b7 100644 --- a/.github/ci/delivery_route.py +++ b/.github/ci/delivery_route.py @@ -8,6 +8,7 @@ import os from pathlib import Path import re +import sys import time import zipfile @@ -25,6 +26,32 @@ ERRORS = policy.ERRORS + (zipfile.BadZipFile, RuntimeError) require = verifier.require +# Only fixed vocabulary reaches logs: never exception text or API/body values. +REFUSALS = { + "Source is not opted-in/current": "source-metadata", + "Source is outside fixed scope": "source-scope", + "Source event ref differs": "source-event-ref", + "Source test-merge identity differs": "source-test-merge", + "Source moved during verification": "source-reread", + "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", + "Unrecognized fast workflow identity": "fast-details-url", + "Fast job identity differs": "fast-job-id", + "Fast run belongs to another workflow/head or is incomplete": "fast-run-identity", + "Fast run has no unique source PR": "fast-source-count", + "Fast run source association differs": "fast-source-identity", + "Incomplete base-change history": "fast-base-history", + "Fast run predates a base change": "fast-retarget", + "Fast job does not match the current successful run": "fast-job-identity", + "Fast run/check changed during verification": "fast-reread", +} + + +def diagnostic(stage, reason, **details): + print(json.dumps({"delivery_diagnostic": reason, "stage": stage, **details}, + sort_keys=True), file=sys.stderr) + def enabled(root): try: @@ -93,11 +120,18 @@ def source(env, event, root, main, request, base_changes): 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"))) + 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]) # Prose may change before routing. Validate both decisions independently; # retain every other snapshot field and the strict subsequent live rereads. require(verifier.delivery_intended(expected.get("body")) and not re.search(r"(?m)^Depends-On:", expected["body"]) - and policy.snapshot(dict(pr, body=expected["body"])) == policy.snapshot(expected) + and live_snapshot == event_snapshot and pr["state"] == "open" and pr["draft"] is False and pr["number"] == number and verifier.delivery_intended(pr["body"]) and not re.search(r"(?m)^Depends-On:", pr["body"]), "Source is not opted-in/current") @@ -183,36 +217,45 @@ def route(env, event, root, request=policy.github, fetch=evidence.download, threads=verifier.read_threads, base_changes=verifier.read_base_changes): if not enabled(root): return dict(FULL) + stage = "effective-protection" try: require(effective_gate(request), "Effective protection is not migrated") + stage = "workflow-identity" run = current_run(env, request) + stage = "trusted-main" main = policy.sha(policy.git(root, "rev-parse", "HEAD").decode().strip()) require(request(REPOSITORY, "git/ref/heads/main")["object"]["sha"] == main, "Trusted main changed") kind = env["GITHUB_EVENT_NAME"] if kind == "pull_request": + stage = "event-identity" head, branch = policy.side(event["pull_request"], "head") require(run["head_sha"] == head and run["head_branch"] == branch, "Run source differs") if policy.QUEUE.fullmatch(branch): + stage = "candidate" proof = duplicate(env, event, root, main, request, fetch, threads, base_changes) mode = "candidate" else: + stage = "source" proof = source(env, event, root, main, request, base_changes) mode = "source" elif kind == "push" and env["GITHUB_REF"] == "refs/heads/main": + stage = "landing" require(run["head_sha"] == policy.sha(env["GITHUB_SHA"]) and run["head_branch"] == "main", "Run landing differs") proof = landed(env, event, root, request, fetch) mode = "landing" else: return dict(FULL) + stage = "final-reread" require(effective_gate(request) and request(REPOSITORY, "git/ref/heads/main")["object"]["sha"] == main and run == request(REPOSITORY, f"actions/runs/{run['id']}"), "Routing evidence changed") return {"mode": mode, "omit_full": True, "proof": evidence.digest(proof)} except verifier.PendingEvidence: return {**FULL, "retryable": True} - except ERRORS: + except ERRORS as exc: + diagnostic(stage, REFUSALS.get(str(exc), "incomplete-evidence")) return dict(FULL) diff --git a/tests/test_ci_cutover.py b/tests/test_ci_cutover.py index 4df2378e..66a9ad34 100644 --- a/tests/test_ci_cutover.py +++ b/tests/test_ci_cutover.py @@ -129,6 +129,57 @@ def test_ci_cutover_verified_source_selects_fast_but_not_merge_authority(cutover assert "queue_admission" not in result +@pytest.mark.parametrize("field", ["merged", "merge_commit_sha"]) +def test_ci_cutover_source_snapshot_diagnostic_has_names_not_values(cutover, batch, capsys, field): + env, event = source_fixture(cutover, batch) + if field == "merge_commit_sha": + batch["prs"][7][field] = env["GITHUB_SHA"] + event["pull_request"][field] = 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"}, + ] + + +@pytest.mark.parametrize("message,reason", [ + ("Reusable/foreign workflow must remain full", "incomplete-evidence"), + ("Private exception / token / body must never be logged.", "incomplete-evidence"), +]) +def test_ci_cutover_diagnostic_does_not_echo_exception_text(cutover, batch, monkeypatch, capsys, + message, reason): + env, event = source_fixture(cutover, batch) + def fail(*_args): + raise ValueError(message) + monkeypatch.setattr(cutover, "current_run", fail) + assert route_fixture(cutover, batch, env, event) == cutover.FULL + assert json.loads(capsys.readouterr().err) == { + "delivery_diagnostic": reason, "stage": "workflow-identity"} + + +@pytest.mark.parametrize("message,reason", [ + ("Missing or ambiguous fast check", "fast-check-count"), + ("Fast run/check changed during verification", "fast-reread"), +]) +def test_ci_cutover_fast_refusal_diagnostic_keeps_full(cutover, batch, monkeypatch, capsys, + message, reason): + env, event = source_fixture(cutover, batch) + def fail(*_args, **_kwargs): + raise cutover.verifier.Refused(message) + monkeypatch.setattr(cutover.verifier, "fast_receipt", fail) + assert route_fixture(cutover, batch, env, event) == cutover.FULL + assert json.loads(capsys.readouterr().err) == { + "delivery_diagnostic": reason, "stage": "source"} + + +def test_ci_cutover_success_has_no_refusal_diagnostic(cutover, batch, capsys): + env, event = source_fixture(cutover, batch) + assert route_fixture(cutover, batch, env, event)["mode"] == "source" + assert capsys.readouterr().err == "" + + @pytest.mark.parametrize("side", ["event", "live"]) @pytest.mark.parametrize("prose", ["prefix", "suffix", "replace"]) def test_ci_cutover_source_description_prose_preserves_decision(cutover, batch, side, prose):