diff --git a/.claude/hooks/pretooluse-merge-consent.sh b/.claude/hooks/pretooluse-merge-consent.sh index f3744fb03..738227099 100755 --- a/.claude/hooks/pretooluse-merge-consent.sh +++ b/.claude/hooks/pretooluse-merge-consent.sh @@ -356,6 +356,25 @@ else # present -> proceed, ABSENT -> deny. Absence is not a degraded read; it is #622's hole reopened, # and the whole point of that issue is that the failure is silent from the merge caller's side. [ -n "$base_ref" ] || decide ask "H6/H10 merge gate: could not resolve PR #$pr's base branch, so the 'review-verdict/h10' required-check protection that makes a scheduled auto-merge safe (ersatztv#622) can't be confirmed. Verify branch protection on the base, or merge immediately instead of scheduling." + # RE-READ THE BASE FIRST — this guard had the very defect it exists to enforce (found in the fifth + # cold-review round). `$base_ref` comes from the PR snapshot taken at the top of this hook, and + # everything between then and here is round trips: the file enumeration alone can be forty pages. + # A PERSISTENT retarget in that gap needs no ABA and no force-push: the protection lookup would + # name the OLD base, confirm `review-verdict/h10` on a branch the PR no longer targets, and grant a + # scheduled merge onto one that may require nothing. Checking a stale identifier is not checking. + # So the identifier is refreshed and compared before it is used, which is this record's own + # read-compare-refuse rule applied to the read that implements it. + prjson_bp=$(gq "repos/$owner/$repo/pulls/$pr") + if [ -z "${prjson_bp//[[:space:]]/}" ] || ! printf '%s' "$prjson_bp" | jq -e 'type == "object"' >/dev/null 2>&1; then + decide ask "H6/H10 merge gate: could not re-read PR #$pr to confirm it still targets '$base_ref' before checking that branch's protection. Confirm the target branch and its 'review-verdict/h10' required check manually, or merge immediately instead of scheduling." + fi + base_now_bp=$(printf '%s' "$prjson_bp" | jq -r '.base.ref // ""' 2>/dev/null || true) + if [ -z "$base_now_bp" ]; then + decide ask "H6/H10 merge gate: re-read PR #$pr but it reported no base branch, so it is unproven that it still targets '$base_ref'. Confirm the target and its required checks before scheduling an auto-merge." + fi + if [ "$base_now_bp" != "$base_ref" ]; then + decide deny "H6/H10 merge gate: BLOCKED — PR #$pr was retargeted from '$base_ref' to '$base_now_bp' while this gate was evaluating. Every check above was formed against '$base_ref', including the review verdict, so none of them describe the merge that would now happen (ersatztv#632). Re-review against '$base_now_bp' and run: scripts/post-review-verdict.sh $pr MERGEABLE" + fi # 404 MUST be separated from every other failure, and `gq` cannot do it: it wraps `curl -sf`, # which turns any HTTP error into exit 22 and empty output, so "no branch protection exists at # all" is indistinguishable from "Gitea is down" through that helper. Those are opposite answers. diff --git a/scripts/tests/test_merge_consent_required_check.py b/scripts/tests/test_merge_consent_required_check.py index b48802e30..1b9444fc4 100644 --- a/scripts/tests/test_merge_consent_required_check.py +++ b/scripts/tests/test_merge_consent_required_check.py @@ -86,6 +86,9 @@ if "/branch_protections/" in url: # string is substring search, so the naive membership test answers "yes" here. respond('{"enable_status_check": true,' ' "status_check_contexts": "prefix-review-verdict/h10-suffix"}') + if mode == "NONSTRING-STATUS-ROW": + respond('{"enable_status_check": true,' + ' "status_check_contexts": ["review-verdict/h10"]}') if mode == "STRING-ENABLE": respond('{"enable_status_check": "true",' ' "status_check_contexts": ["review-verdict/h10"]}') @@ -127,9 +130,19 @@ if "/status" in url: # A 2xx body whose `.statuses` IS an array but whose members are scalars. `.statuses | type == # "array"` passes; indexing a number then makes jq exit 5 and, under `set -e`, kills the hook # with no JSON at all. - if (state / "bp").read_text().strip() == "SCALAR-STATUS-ROW": + _m = (state / "bp").read_text().strip() + if _m == "SCALAR-STATUS-ROW": print('{"state":"success","statuses":[1]}') sys.exit(0) + if _m == "NONSTRING-STATUS-ROW": + # Object shape, string context, valid description — passes the #632 block, which does NOT + # validate `.status` — but a NUMERIC status. This is the shape that actually reaches the + # scheduled branch's validator, i.e. the new clause's reachable contribution. + print(json.dumps({"state": "success", "statuses": [ + {"context": "review-verdict/h10", "status": 7, + "description": "Review-verdict: MERGEABLE @ %s (base: main)" + % os.environ["STUB_SHORT"]}]})) + sys.exit(0) print(json.dumps({"state": "success", "statuses": [ {"context": "review-verdict/h10", "status": "success", "description": "Review-verdict: MERGEABLE @ %s (base: main)" % os.environ["STUB_SHORT"]}]})) @@ -144,8 +157,17 @@ if "/issues/" in url: sys.exit(0) if "/pulls/" in url: + # The base is served per-GET so a PERSISTENT retarget mid-run can be modelled: the first read + # (top of the hook) sees `main`, a later one sees whatever `retarget` names. + counter = state / "pr_get_count" + n = int(counter.read_text()) if counter.exists() else 0 + counter.write_text(str(n + 1)) + base = "main" + rt = state / "retarget" + if rt.exists() and n >= 1: + base = rt.read_text().strip() print(json.dumps({"head": {"sha": os.environ["STUB_SHA"]}, - "base": {"ref": "main", "sha": "b" * 40}, + "base": {"ref": base, "sha": "b" * 40}, "body": "fixes #1"})) sys.exit(0) @@ -174,6 +196,10 @@ def hook(tmp_path): def set_branch_protection(self, mode): (state / "bp").write_text(mode) + def set_retarget(self, ref): + """Persistent retarget: every PR read after the first reports `ref`.""" + (state / "retarget").write_text(ref) + def branch_protection_was_fetched(self): return (state / "bp_called").exists() @@ -378,11 +404,16 @@ def test_a_SCALAR_status_row_asks_instead_of_killing_the_hook(hook): WHAT THIS TEST DOES *NOT* PROVE, stated because the mutation showed it. Restoring the scheduled-branch validation to its predecessor leaves this test GREEN, because the #632 - base-retarget block runs FIRST and already validates every member it consumes — so it catches - this payload and asks before the scheduled branch is ever reached. The two guards overlap, which - is the masking shape `duplicate guards mask each other` describes, and it means the member - validation added to the scheduled branch is DEFENCE IN DEPTH against a reordering or a removal - of the earlier block, not a fix for a reachable live defect. + base-retarget block runs FIRST and validates the members it consumes — so it catches THIS + payload and asks before the scheduled branch is reached. The two guards overlap, which is the + masking shape `duplicate guards mask each other` describes. + + The caveat is scoped to this payload, NOT to the clause. The earlier block validates `.context` + and `.description` but not `.status`, so an object row with a numeric status passes it and does + reach the new validator — `test_a_NON_STRING_status_reaches_the_scheduled_validator` covers that + and goes red when the clause is removed. So the clause is masked for scalar rows and load-bearing + for that one. An earlier draft called the whole clause defence-in-depth, understating it in the + opposite direction from this repo's usual error. So this asserts the OBSERVABLE contract — a decision is always emitted for this payload — which is true and worth pinning whichever guard supplies it. It is deliberately not offered as a @@ -394,3 +425,37 @@ def test_a_SCALAR_status_row_asks_instead_of_killing_the_hook(hook): "the hook emitted no decision at all for a malformed statuses payload — it neither granted, " "denied nor asked") assert decision["hookSpecificOutput"]["permissionDecision"] == "ask" + + +def test_a_PERSISTENT_retarget_after_the_first_read_denies(hook): + """The defect this guard had itself, found in the fifth cold-review round. + + `$base_ref` is captured from the PR snapshot at the top of the hook, and everything between + then and the branch-protection lookup is round trips — the file enumeration alone can be forty + pages. A retarget in that gap needs no ABA and no force-push: the lookup would name the OLD + base, confirm `review-verdict/h10` on a branch the PR no longer targets, and grant a scheduled + merge onto one that may require nothing. Checking a stale identifier is not checking, which is + the whole of `process.check-and-use-pins-a-version` — so the guard enforcing that rule had to + stop breaking it. + """ + hook.set_retarget("scratch") + reason = hook.reason() + assert "deny" in reason, ( + "the PR was retargeted mid-evaluation and the gate still granted on the original base") + assert "scratch" in reason and "main" in reason, ( + "the deny must name both branches; a reader cannot act on 'the base changed'") + + +def test_a_NON_STRING_status_reaches_the_scheduled_validator(hook): + """The reachable contribution of the scheduled-branch member validation, which the previous + caveat understated. + + The #632 block validates `.context` and `.description` but NOT `.status`, so an object row with + a numeric status passes it and arrives here. Without this clause it becomes `vstate=7` and falls + to the catch-all deny arm — fail-closed, but reported as "the verdict is '7'" rather than as a + payload that could not be read. So the clause is masked for scalar rows and load-bearing for + this one; the caveat on the scalar test is scoped accordingly. + """ + hook.set_branch_protection("NONSTRING-STATUS-ROW") + reason = hook.reason() + assert "ask" in reason, "a non-string .status produced a verdict-shaped decision"