From 8212e11f80689a61b0b894383e1fe2917205c2e9 Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Wed, 7 Oct 2026 02:18:37 +0000 Subject: [PATCH 1/2] fix(cursor-approve): approve-external refuses a PR labelled skip-cursor-review A caller whose workflow concurrency group is keyed on the label name cannot cancel an in-flight decide when `skip-cursor-review` lands mid-axes: the veto starts a run in a different slot, and decide runs under always(). So the veto is now re-checked where the approval is posted. approve-external reads `skip-cursor-review` from the live PR object it already re-reads for head/base/needs-human-review, and treats it exactly like needs-human-review: no APPROVE, this identity's own marked approvals withdrawn, and a new `vetoed` outcome the decide card renders. The post-write re-read withdraws an approval if the veto arrives during the POST. A PR payload with no label list fails closed rather than reading as "no veto". Co-Authored-By: Claude Opus 5.5 --- .github/cursor-approve/card.py | 2 + .github/cursor-approve/tests/test_card.py | 6 ++ .github/cursor-review/auto-approve.py | 43 +++++++++-- .../tests/test_approve_external.py | 73 ++++++++++++++++++- .github/workflows/cursor-approve.yml | 3 +- README.md | 2 +- docs/callers/cursor-approve.md | 5 +- 7 files changed, 121 insertions(+), 13 deletions(-) diff --git a/.github/cursor-approve/card.py b/.github/cursor-approve/card.py index 4d4cec9f..bb41a73e 100644 --- a/.github/cursor-approve/card.py +++ b/.github/cursor-approve/card.py @@ -46,6 +46,7 @@ OUTCOME_NOT_APPROVED = "not_approved" OUTCOME_SUPERSEDED = "superseded" OUTCOME_HUMAN = "needs_human" +OUTCOME_VETOED = "vetoed" OUTCOME_OWN_PR = "own_pr" OUTCOME_ERROR = "error" @@ -159,6 +160,7 @@ def render_decide(round_no, max_rounds, sha: str, axes: list, decision, outcome: else: why = { OUTCOME_HUMAN: ["the PR is labelled `needs-human-review`"], + OUTCOME_VETOED: ["vetoed: the PR is labelled `skip-cursor-review`"], OUTCOME_OWN_PR: ["the approver authored this PR"], OUTCOME_ERROR: ["the approval could not be posted (see the workflow run)"], }.get(outcome) diff --git a/.github/cursor-approve/tests/test_card.py b/.github/cursor-approve/tests/test_card.py index 902db840..99fcf267 100644 --- a/.github/cursor-approve/tests/test_card.py +++ b/.github/cursor-approve/tests/test_card.py @@ -68,6 +68,12 @@ def test_decide_not_approved_lists_reasons_and_missing_axis(self): self.assertIn("red on correctness", body) self.assertIn("no red, at most 0 yellow", body) + def test_decide_vetoed_names_the_skip_label(self): + body = card.render_decide("1", "5", SHA, ["correctness"], decision(correctness="green"), "vetoed", "0", "") + self.assertIn("**Result: ❌ Not approved.**", body) + self.assertIn("- vetoed: the PR is labelled `skip-cursor-review`", body) + self.assertNotIn("no decision was reached", body) + def test_malformed_decision_shows_no_result(self): body = card.render_decide("1", "5", SHA, ["correctness"], {"axes": "junk"}, "not_approved", "0", "") self.assertIn("| correctness | ⚠️ no result |", body) diff --git a/.github/cursor-review/auto-approve.py b/.github/cursor-review/auto-approve.py index 046b184f..1f6d754c 100644 --- a/.github/cursor-review/auto-approve.py +++ b/.github/cursor-review/auto-approve.py @@ -866,6 +866,13 @@ def pr_head_base(pr: dict) -> tuple: return _str_field(pr, "head", "sha"), _str_field(pr, "base", "ref") +def live_labels_readable(pr: dict) -> bool: + """Whether the PR payload carries a label list at all. `has_label` reads a + missing or shapeless `labels` as "no labels", which fails OPEN for a veto + label; a caller that must fail closed checks this first.""" + return isinstance(pr.get("labels"), list) + + def has_label(pr: dict, name: str) -> bool: # A truthy non-list `labels` (a number, `true`) is not iterable, and the # TypeError would escape every caller's `except (RuntimeError, ValueError)`. @@ -1466,9 +1473,15 @@ def cmd_round_cap(args) -> int: # base ride in the body's markers (so `dismiss-stale` withdraws it on a push or # retarget like any other auto-approval), this identity's own earlier approvals # are withdrawn on every non-approval, and a `needs-human-review` PR is never -# approved. The outcome is written as the step output `outcome` for the card. - -EXTERNAL_OUTCOMES = ("approved", "not_approved", "superseded", "needs_human", "own_pr", "error") +# approved. Nor is a PR labelled `skip-cursor-review`: a caller's label-keyed +# concurrency group cannot cancel an in-flight decide when that veto lands +# mid-axes, so decide re-reads it live. The outcome is written as the step +# output `outcome` for the card. + +EXTERNAL_OUTCOMES = ("approved", "not_approved", "superseded", "needs_human", "vetoed", "own_pr", "error") +# cursor-review.yml's veto label (its gate and concurrency slot hardcode it too). +SKIP_REVIEW_LABEL = "skip-cursor-review" +SKIP_REVIEW_MESSAGE = f"The PR was labelled `{SKIP_REVIEW_LABEL}` — cursor-approve approval withdrawn." EXTERNAL_AXES = ("business", "design", "correctness", "completeness", "conformance") @@ -1519,7 +1532,11 @@ def cmd_approve_external(args) -> int: try: pr = read_pr(args.repo, args.pr_number) live_head, live_base = pr_head_base(pr) + if not live_labels_readable(pr): + # The veto below must be read, not assumed absent. + raise ValueError("the PR payload carries no label list") human_review = has_label(pr, HUMAN_REVIEW_LABEL) + vetoed = has_label(pr, SKIP_REVIEW_LABEL) except (RuntimeError, ValueError) as e: print(f"::error::Could not read the PR state: {e}") withdraw_own_approvals(args) @@ -1532,6 +1549,8 @@ def cmd_approve_external(args) -> int: outcome, why = "superseded", "the PR base changed since the axes ran" elif human_review: outcome, why = "needs_human", f"the PR is labelled `{HUMAN_REVIEW_LABEL}`" + elif vetoed: + outcome, why = "vetoed", f"the PR is labelled `{SKIP_REVIEW_LABEL}`" elif not external_decision_approves(decision, axes): outcome, why = "not_approved", "the axes did not approve" else: @@ -1544,6 +1563,8 @@ def cmd_approve_external(args) -> int: # is stale against the LIVE head and base, so a decide that finishes # late cannot dismiss an approval that run already posted. return withdraw_own_approvals(args, STALE_MESSAGE, live_head, live_base) + if outcome == "vetoed": + return withdraw_own_approvals(args, SKIP_REVIEW_MESSAGE) return withdraw_own_approvals(args) body = render_external_body(args.card_url, args.commit_sha, args.base_ref) @@ -1568,14 +1589,22 @@ def cmd_approve_external(args) -> int: pr_now = read_pr(args.repo, args.pr_number) head_now, base_now = pr_head_base(pr_now) labelled_now = has_label(pr_now, HUMAN_REVIEW_LABEL) + # An unreadable label list cannot clear the veto: withdraw. + vetoed_now = not live_labels_readable(pr_now) or has_label(pr_now, SKIP_REVIEW_LABEL) except (RuntimeError, ValueError): head_now = base_now = None - labelled_now = False + labelled_now = vetoed_now = False moved = head_now != args.commit_sha or base_now != args.base_ref - if moved or labelled_now: - set_output("outcome", "superseded" if moved else "needs_human") + if moved or labelled_now or vetoed_now: + if moved: + outcome, message = "superseded", STALE_MESSAGE + elif labelled_now: + outcome, message = "needs_human", HUMAN_REVIEW_MESSAGE + else: + outcome, message = "vetoed", SKIP_REVIEW_MESSAGE + set_output("outcome", outcome) try: - dismiss(args.repo, args.pr_number, posted["id"], STALE_MESSAGE if moved else HUMAN_REVIEW_MESSAGE) + dismiss(args.repo, args.pr_number, posted["id"], message) except RuntimeError as e: print(f"::error::The PR changed while the approval was posted, and withdrawing it failed: {e}. {DISMISS_PERMISSION_HINT}") return 1 diff --git a/.github/cursor-review/tests/test_approve_external.py b/.github/cursor-review/tests/test_approve_external.py index 3a40c9d1..bca0d63d 100644 --- a/.github/cursor-review/tests/test_approve_external.py +++ b/.github/cursor-review/tests/test_approve_external.py @@ -19,9 +19,11 @@ class FakeGitHub: - def __init__(self, head=SHA, labels=(), reviews=(), head_after=None, base="main"): + def __init__(self, head=SHA, labels=(), reviews=(), head_after=None, base="main", + labels_after=None, read_error=False, pr_override=None): self.head, self.labels, self.reviews, self.base = head, list(labels), list(reviews), base - self.head_after = head_after + self.head_after, self.labels_after = head_after, labels_after + self.read_error, self.pr_override = read_error, pr_override self.posted, self.dismissed, self.reads = [], [], 0 def __call__(self, args, payload=None): @@ -40,9 +42,14 @@ def __call__(self, args, payload=None): return json.dumps({"data": {"repository": {"pullRequest": {"reviews": { "pageInfo": {"hasNextPage": False, "endCursor": None}, "nodes": nodes}}}}}) self.reads += 1 + if self.read_error: + raise RuntimeError("gh api: HTTP 502") + if self.pr_override is not None: + return json.dumps(self.pr_override) head = self.head_after if (self.head_after and self.reads > 1) else self.head + labels = self.labels_after if (self.labels_after is not None and self.reads > 1) else self.labels return json.dumps({"head": {"sha": head}, "base": {"ref": self.base}, - "labels": [{"name": n} for n in self.labels]}) + "labels": [{"name": n} for n in labels]}) def prior_approval(sha="c" * 40, rid=42): @@ -116,6 +123,66 @@ def test_needs_human_review_label_posts_nothing(self): self.assertEqual((rc, outcome), (0, "needs_human")) self.assertEqual(fake.posted, []) + # --- the skip-cursor-review veto, re-read live at decide time --- + + def test_skip_label_vetoes_all_green_and_withdraws_prior_approval(self): + fake = FakeGitHub(labels=["bug", aa.SKIP_REVIEW_LABEL], reviews=[prior_approval()]) + rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}, + threshold="low", poster="cr-bot[bot]") + self.assertEqual((rc, outcome), (0, "vetoed")) + self.assertEqual(fake.posted, []) + self.assertEqual(fake.dismissed, ["42"]) + self.assertEqual(self.resolve_calls, []) + + def test_skip_label_is_read_live_not_from_the_event_payload(self): + # The run started on `labeled: cursor-review`; the veto landed mid-axes, + # so only the live PR read carries it. + fake = FakeGitHub(labels=["cursor-review", aa.SKIP_REVIEW_LABEL]) + with tempfile.TemporaryDirectory() as tmp: + event = os.path.join(tmp, "event.json") + with open(event, "w", encoding="utf-8") as f: + json.dump({"action": "labeled", "label": {"name": "cursor-review"}, + "pull_request": {"number": 1, "head": {"sha": SHA}, + "labels": [{"name": "cursor-review"}]}}, f) + with mock.patch.dict(os.environ, {"GITHUB_EVENT_PATH": event}): + rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}) + self.assertEqual((rc, outcome), (0, "vetoed")) + self.assertEqual(fake.posted, []) + self.assertGreaterEqual(fake.reads, 1) + + def test_skip_label_match_is_exact(self): + fake = FakeGitHub(labels=["skip-cursor-review-later", "no-skip-cursor-review"]) + rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}) + self.assertEqual((rc, outcome), (0, "approved")) + self.assertEqual(len(fake.posted), 1) + + def test_label_read_failure_posts_nothing(self): + fake = FakeGitHub(read_error=True, reviews=[prior_approval()]) + rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}) + self.assertEqual((rc, outcome), (1, "error")) + self.assertEqual(fake.posted, []) + self.assertEqual(fake.dismissed, ["42"]) + + def test_missing_label_list_fails_closed(self): + # A PR payload with no `labels` cannot prove the veto absent. + for labels in (None, "skip-cursor-review", 1): + pr = {"head": {"sha": SHA}, "base": {"ref": "main"}} + if labels is not None: + pr["labels"] = labels + fake = FakeGitHub(pr_override=pr) + rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}) + self.assertEqual((rc, outcome), (1, "error"), labels) + self.assertEqual(fake.posted, [], labels) + + def test_skip_label_applied_during_post_withdraws(self): + fake = FakeGitHub(labels_after=[aa.SKIP_REVIEW_LABEL]) + rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}, + threshold="low", poster="cr-bot[bot]") + self.assertEqual((rc, outcome), (0, "vetoed")) + self.assertEqual(len(fake.posted), 1) + self.assertEqual(fake.dismissed, ["555"]) + self.assertEqual(self.resolve_calls, []) + def test_late_superseded_decide_keeps_an_approval_on_the_live_head(self): # An older run's decide finishing after a newer run approved the new head # must withdraw only what is stale against that head. diff --git a/.github/workflows/cursor-approve.yml b/.github/workflows/cursor-approve.yml index 7db11ba1..fde44b44 100644 --- a/.github/workflows/cursor-approve.yml +++ b/.github/workflows/cursor-approve.yml @@ -17,7 +17,8 @@ name: Cursor approve # the commit_sha it stamped), posts the approval through # .github/cursor-review/auto-approve.py approve-external # (head and base re-read, reviewed-SHA marker, own stale -# approvals withdrawn, never over needs-human-review) +# approvals withdrawn, never over needs-human-review or +# skip-cursor-review, both re-read live at decide time) # and rewrites the card with the verdicts. # # No job here checks out PR code; the only checkout is this repo at diff --git a/README.md b/README.md index ecb5d451..6b073b7f 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ complete, copy-pasteable caller. | [`detect-unreviewed-merge.yml`](.github/workflows/detect-unreviewed-merge.yml) | SOC 2 compliance — detects PRs merged without prior approval and opens a tracking issue in [`Comfy-Org/unreviewed-merges`](https://github.com/Comfy-Org/unreviewed-merges). | [detect-unreviewed-merge.md](docs/callers/detect-unreviewed-merge.md) | | [`linear-ticket.yml`](.github/workflows/linear-ticket.yml) | **Requires a Linear issue that Linear has LINKED to the exact PR when the PR targets a protected base branch** — not a `TEAM-123`-shaped string in author text. The validator reads GitHub's branch `protected` property, so any number of branches protected by branch protection or rulesets are covered without a caller-maintained list; unprotected targets publish no `Linear ticket` status and do not query Linear (a success status would be unsafe because GitHub scopes statuses to a SHA, not a PR). For protected targets, the only thing that passes is an attachment Linear returns for the PR's canonical `html_url` (`attachmentsForURL`, PR URL passed as a GraphQL **variable**) whose issue satisfies the configured team (`team-keys`, matched on the resolved API `team.key`, never a prefix) and state (`require-open-issue` rejects `completed`/`canceled` via `state.type`) policy; a PR linked to several tickets passes when at least one satisfies policy. Identifiers mined from branch/title/body are **diagnostics only** (capped at 20, one batched lookup) — they explain a red check, never turn it green. Fork- and Dependabot-safe by a two-stage split: an unprivileged, checks-out-nothing **signal** workflow on `pull_request`, then a privileged validator on `workflow_run` that GitHub grants the secret + write token. It publishes the required **`Linear ticket`** commit status on the refetched PR head SHA (its own default-branch job is *not* the merge check), refetches the head SHA and base branch before every terminal write so a superseded or retargeted run can't overwrite a newer result, maintains one marker diagnostic comment, and **fails closed** on unreadable branch protection or Linear auth/schema/timeout/rate-limit errors (an infrastructure failure, never a green pass) with bounded retry for Linear's async-link race. `enforce: false` is warn-only — a failing verdict never exits the job nonzero (a broken run still does), and by default (`soft-fail: true`) it still publishes the **red** status so the pilot is visible on the PR while blocking nothing, since blocking is branch protection requiring the context, not this workflow's call (`soft-fail: false` restores the silent always-green variant); one `exempt-label` (e.g. `linear-exempt`) waives it per-PR, and an opt-in `exempt-actors` list (empty by default) skips bot PRs (e.g. `dependabot[bot]`) with no built-in bypass. An opt-in `exempt-paths` list exempts a PR whose changed files **all** match (config-only chores arriving from many different authors); it is decided IN the validator — reading the GitHub-owned changed-file list, failing closed past that endpoint's silent 3000-file cap — precisely so an exempt PR still publishes the green required status, which a `paths-ignore:` on the signal workflow could not (an unpublished required context leaves the PR pending forever). Logic + hermetic tests live in [`scripts/linear-ticket/`](scripts/linear-ticket); `workflows_ref` is **required**, shape- and ancestry-checked before checkout. Requires `LINEAR_API_TOKEN` + the two-workflow caller. | [linear-ticket.md](docs/callers/linear-ticket.md) | | [`cursor-review.yml`](.github/workflows/cursor-review.yml) | Label-triggered multi-model code review. A 3-lab × 2-review-type cursor-agent panel runs adversarial + edge-case passes, a judge model consolidates them into one PR review with per-finding severity badges, and the triggerer gets Slack start/complete DMs. Advisory by default: it posts a review, it does not gate. Opt-in `blocking: true` adds a fail-closed **Blocking gate** check — red while any finding thread is unresolved and non-outdated, and red whenever the round that should have produced those threads did not land (the trigger job failed, the post exited zero without reaching the PR, every finding was demoted to the review body, the diff was over `diff_size_cap`, or the run was cancelled) — which blocks merge once the caller's ruleset marks it required (two switches; see [the setup guide](docs/callers/cursor-review.md)). Prompts and scripts live in [`.github/cursor-review/`](.github/cursor-review) — the single source of truth, so consumer repos carry only a thin caller. Generated code is excluded from BOTH the diff-size gate and the diff sent to the panel via the shared [`check-pr-size`](scripts/check-pr-size) classifier (base-ref `linguist-generated`, base-blob Go generated markers — a PR cannot self-exempt a file from review, lockfiles, plus `extra_generated_globs` / `extra_lockfiles`) — the same detection as `pr-size.yml`, so codegen never blows the review cap or wastes review signal. Blank and comment-only lines are also discounted from the size count by default (`ignore_comments`) so AI comment volume doesn't trip the cap and skip the review — comments still reach the panel, only the count ignores them. The panel model list is fixed by the reusable — there is no `models` input (`models` is a job *output*); an optional `runs_on` routes only the credential-free heavy jobs (`diff-size`, `preflight`, panel, judge) to a caller's runner pool, while every write-credential job stays on GitHub-hosted `ubuntu-latest`. Least privilege by construction: no job both checks out PR code and holds a write-scoped credential — every job that checks out PR code and runs `cursor-agent` over it (every panel cell and the judge) holds `contents: read` only, and hands its result to a separate, checkout-free `Post review` job via an artifact; that job and the over-the-cap comment job are the only holders of `pull-requests: write`, and neither checks out PR code. Requires `CURSOR_API_KEY` (+ optional `SLACK_BOT_TOKEN`). | [cursor-review.md](docs/callers/cursor-review.md) | -| [`cursor-approve.yml`](.github/workflows/cursor-approve.yml) + [`axis-correctness.yml`](.github/workflows/axis-correctness.yml) / [`axis-conformance.yml`](.github/workflows/axis-conformance.yml) | Per-axis approval after a passing cursor-review round (`approve_gate == 'pass'`). Each axis is its own reusable declaring only the secrets it needs (these two: `CURSOR_API_KEY` only), runs one read-only `cursor-agent` judgement over the PR head via the shared [`cursor-axis-base.yml`](.github/workflows/cursor-axis-base.yml), and uploads one verdict artifact. `cursor-approve.yml`, called with `phase: start` and `phase: decide`, keeps one live status card on the PR and approves through `auto-approve.py approve-external` (head re-read, reviewed-SHA marker, own stale approvals withdrawn, never over `needs-human-review`). Prompts and the aggregator live in [`.github/cursor-approve/`](.github/cursor-approve). Requires `CURSOR_API_KEY` + `APPROVER_TOKEN`. | [cursor-approve.md](docs/callers/cursor-approve.md) | +| [`cursor-approve.yml`](.github/workflows/cursor-approve.yml) + [`axis-correctness.yml`](.github/workflows/axis-correctness.yml) / [`axis-conformance.yml`](.github/workflows/axis-conformance.yml) | Per-axis approval after a passing cursor-review round (`approve_gate == 'pass'`). Each axis is its own reusable declaring only the secrets it needs (these two: `CURSOR_API_KEY` only), runs one read-only `cursor-agent` judgement over the PR head via the shared [`cursor-axis-base.yml`](.github/workflows/cursor-axis-base.yml), and uploads one verdict artifact. `cursor-approve.yml`, called with `phase: start` and `phase: decide`, keeps one live status card on the PR and approves through `auto-approve.py approve-external` (head re-read, reviewed-SHA marker, own stale approvals withdrawn, never over `needs-human-review` or `skip-cursor-review`, both re-read live). Prompts and the aggregator live in [`.github/cursor-approve/`](.github/cursor-approve). Requires `CURSOR_API_KEY` + `APPROVER_TOKEN`. | [cursor-approve.md](docs/callers/cursor-approve.md) | | [`cursor-review-auto-label.yml`](.github/workflows/cursor-review-auto-label.yml) | Companion to `cursor-review.yml`. On PR assignment, applies the review label for an opted-in reviewer (via the CLOUD_CODE_BOT app token, so the label actually triggers the review). The opt-in roster lives in the caller's `vars.CURSOR_REVIEW_OPTED_IN_LOGINS` — no roster is baked into the workflow. Requires `vars.APP_ID` + `CLOUD_CODE_BOT_PRIVATE_KEY`, or the optional `bot_app_id` + `BOT_APP_PRIVATE_KEY` pair to mint from a narrower App instead. | [cursor-review-auto-label.md](docs/callers/cursor-review-auto-label.md) | | [`assign-reviewers.yml`](.github/workflows/assign-reviewers.yml) | Assigns PR owners using configured paths and recent human approval history. Chooses complementary file coverage before workload, with an explained one-owner default fallback and no random substitutions. Optionally generates a shared history artifact on schedule/manual dispatch and reuses fresh snapshots across PRs. Reads configuration from the base SHA, respects manual assignments, and preserves opt-in `REVIEWER_AUTHOR_ALLOWLIST` scoping. Requires `vars.APP_ID` + `CLOUD_CODE_BOT_PRIVATE_KEY`; callers skip forks and Dependabot triggers. | [assign-reviewers.md](docs/callers/assign-reviewers.md) | | [`assign-prs-to-author.yml`](.github/workflows/assign-prs-to-author.yml) | Housekeeping — assigns every open PR with no assignees to its author (bot-authored PRs skipped by default). Run on a schedule from a thin caller; useful when a team tracks PR ownership via assignees. The calling job needs `pull-requests: write` and `issues: write`. | [assign-prs-to-author.md](docs/callers/assign-prs-to-author.md) | diff --git a/docs/callers/cursor-approve.md b/docs/callers/cursor-approve.md index 54a779d4..eeb30d08 100644 --- a/docs/callers/cursor-approve.md +++ b/docs/callers/cursor-approve.md @@ -27,7 +27,10 @@ the context axes (business, design, completeness) are not available yet. yellow than `max_yellow_axes` → no approval), reading each axis only from its own `axis-` artifact, then `auto-approve.py approve-external`: it re-reads the head and base and withholds when either moved since the axes - ran, never approves a PR labelled `needs-human-review`, + ran, never approves a PR labelled `needs-human-review` or + `skip-cursor-review` (both read live from the PR at decide time, so a veto + applied while the axes run still stops the approval — a caller's + label-keyed concurrency group cannot cancel that in-flight decide), records the reviewed SHA in the review body, and withdraws this identity's own earlier approvals whenever it does not approve. An approval is one line linking the card. The card is rewritten with the verdicts, the result and From 95ac2b88628b833cda77d8064a0c56eda24382da Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Wed, 7 Oct 2026 02:44:33 +0000 Subject: [PATCH 2/2] fix(cursor-approve): the skip-cursor-review veto outranks supersession and withdraws PR-wide Review round on the approve-external veto: - the veto is checked before head/base supersession (pre- and post-POST), so a late decide on a vetoed PR no longer keeps an approval standing on the live head; - a veto landing during the POST withdraws every own marked approval, not just the one just posted; - an unreadable post-POST label list withdraws under its own message and exits red instead of claiming the veto label was applied; - `live_labels_readable` rejects entries `has_label` would silently skip; - the review POST response is validated for an id, as cmd_decide does; - the live-vs-payload test now distinguishes the two sources in both directions. Co-Authored-By: Claude Opus 5.5 --- .github/cursor-review/auto-approve.py | 56 ++++++++++---- .../tests/test_approve_external.py | 76 ++++++++++++++++--- 2 files changed, 108 insertions(+), 24 deletions(-) diff --git a/.github/cursor-review/auto-approve.py b/.github/cursor-review/auto-approve.py index 1f6d754c..b4044cf3 100644 --- a/.github/cursor-review/auto-approve.py +++ b/.github/cursor-review/auto-approve.py @@ -867,10 +867,14 @@ def pr_head_base(pr: dict) -> tuple: def live_labels_readable(pr: dict) -> bool: - """Whether the PR payload carries a label list at all. `has_label` reads a - missing or shapeless `labels` as "no labels", which fails OPEN for a veto - label; a caller that must fail closed checks this first.""" - return isinstance(pr.get("labels"), list) + """Whether the PR payload carries a label list `has_label` can fully read. + `has_label` reads a missing or shapeless `labels` — or skips an entry that is + not a dict with a string `name` — as "no such label", which fails OPEN for a + veto label; a caller that must fail closed checks this first.""" + labels = pr.get("labels") + return isinstance(labels, list) and all( + isinstance(label, dict) and isinstance(label.get("name"), str) for label in labels + ) def has_label(pr: dict, name: str) -> bool: @@ -1482,6 +1486,9 @@ def cmd_round_cap(args) -> int: # cursor-review.yml's veto label (its gate and concurrency slot hardcode it too). SKIP_REVIEW_LABEL = "skip-cursor-review" SKIP_REVIEW_MESSAGE = f"The PR was labelled `{SKIP_REVIEW_LABEL}` — cursor-approve approval withdrawn." +LABELS_UNREADABLE_MESSAGE = ( + f"The PR's labels could not be read to rule out `{SKIP_REVIEW_LABEL}` — cursor-approve approval withdrawn." +) EXTERNAL_AXES = ("business", "design", "correctness", "completeness", "conformance") @@ -1541,7 +1548,12 @@ def cmd_approve_external(args) -> int: print(f"::error::Could not read the PR state: {e}") withdraw_own_approvals(args) return 1 - if live_head != args.commit_sha: + # The veto is PR-wide, so it outranks `superseded`: that path withdraws only + # what is stale against the live head, and would leave an approval already + # standing on the live head in place on a vetoed PR. + if vetoed: + outcome, why = "vetoed", f"the PR is labelled `{SKIP_REVIEW_LABEL}`" + elif live_head != args.commit_sha: outcome, why = "superseded", f"the PR head moved from {args.commit_sha[:7]} to {live_head[:7] or '?'}" elif live_base != args.base_ref: # Same head, different base: the axes diffed against a base the PR no @@ -1549,8 +1561,6 @@ def cmd_approve_external(args) -> int: outcome, why = "superseded", "the PR base changed since the axes ran" elif human_review: outcome, why = "needs_human", f"the PR is labelled `{HUMAN_REVIEW_LABEL}`" - elif vetoed: - outcome, why = "vetoed", f"the PR is labelled `{SKIP_REVIEW_LABEL}`" elif not external_decision_approves(decision, axes): outcome, why = "not_approved", "the axes did not approve" else: @@ -1575,7 +1585,11 @@ def cmd_approve_external(args) -> int: {"commit_id": args.commit_sha, "event": APPROVE, "body": body}, ) ) - except RuntimeError as e: + # As cmd_decide: the id is what a late veto or move dismisses by, so a + # `gh` that exits 0 without one is an ambiguous POST, not a success. + if not isinstance(posted, dict) or posted.get("id") is None: + raise ValueError(f"the review POST returned no review id ({type(posted).__name__})") + except (RuntimeError, ValueError) as e: if "own pull request" in str(e).lower(): set_output("outcome", "own_pr") emit(f"ℹ️ **cursor-approve: skipped** — the approver authored this PR ({e}).") @@ -1589,25 +1603,39 @@ def cmd_approve_external(args) -> int: pr_now = read_pr(args.repo, args.pr_number) head_now, base_now = pr_head_base(pr_now) labelled_now = has_label(pr_now, HUMAN_REVIEW_LABEL) - # An unreadable label list cannot clear the veto: withdraw. - vetoed_now = not live_labels_readable(pr_now) or has_label(pr_now, SKIP_REVIEW_LABEL) + # An unreadable label list cannot clear the veto: withdraw, but under + # its own cause rather than claiming a label nobody applied. + labels_unreadable = not live_labels_readable(pr_now) + vetoed_now = has_label(pr_now, SKIP_REVIEW_LABEL) except (RuntimeError, ValueError): head_now = base_now = None - labelled_now = vetoed_now = False + labelled_now = vetoed_now = labels_unreadable = False moved = head_now != args.commit_sha or base_now != args.base_ref - if moved or labelled_now or vetoed_now: - if moved: + if moved or labelled_now or vetoed_now or labels_unreadable: + # Veto first, as before the POST: it is PR-wide. + if vetoed_now: + outcome, message = "vetoed", SKIP_REVIEW_MESSAGE + elif moved: outcome, message = "superseded", STALE_MESSAGE elif labelled_now: outcome, message = "needs_human", HUMAN_REVIEW_MESSAGE else: - outcome, message = "vetoed", SKIP_REVIEW_MESSAGE + outcome, message = "error", LABELS_UNREADABLE_MESSAGE set_output("outcome", outcome) try: dismiss(args.repo, args.pr_number, posted["id"], message) except RuntimeError as e: print(f"::error::The PR changed while the approval was posted, and withdrawing it failed: {e}. {DISMISS_PERMISSION_HINT}") return 1 + if outcome in ("vetoed", "error"): + # Not only the review just posted: any other standing approval by + # this identity (an earlier run's, or one posted concurrently) would + # still count toward branch protection on a PR that cannot be cleared. + if withdraw_own_approvals(args, message): + return 1 + if outcome == "error": + print(f"::error::{LABELS_UNREADABLE_MESSAGE}") + return 1 emit("ℹ️ **cursor-approve: withdrawn** — the PR changed while the approval was being posted.") return 0 set_output("outcome", "approved") diff --git a/.github/cursor-review/tests/test_approve_external.py b/.github/cursor-review/tests/test_approve_external.py index bca0d63d..c983ec9a 100644 --- a/.github/cursor-review/tests/test_approve_external.py +++ b/.github/cursor-review/tests/test_approve_external.py @@ -20,18 +20,20 @@ class FakeGitHub: def __init__(self, head=SHA, labels=(), reviews=(), head_after=None, base="main", - labels_after=None, read_error=False, pr_override=None): + labels_after=None, read_error=False, pr_override=None, pr_after=None, post_reply=None): self.head, self.labels, self.reviews, self.base = head, list(labels), list(reviews), base self.head_after, self.labels_after = head_after, labels_after self.read_error, self.pr_override = read_error, pr_override - self.posted, self.dismissed, self.reads = [], [], 0 + self.pr_after, self.post_reply = pr_after, post_reply + self.posted, self.dismissed, self.dismiss_messages, self.reads = [], [], [], 0 def __call__(self, args, payload=None): if args[:3] == ["api", "-X", "POST"] and args[3].endswith("/reviews"): self.posted.append(payload) - return json.dumps({"id": 555}) + return json.dumps({"id": 555}) if self.post_reply is None else self.post_reply if args[:3] == ["api", "-X", "PUT"]: self.dismissed.append(args[3].split("/")[-2]) + self.dismiss_messages.append((payload or {}).get("message")) return "{}" if args[:2] == ["api", "graphql"]: # list_reviews reads through GraphQL (for `lastEditedAt`). @@ -44,6 +46,8 @@ def __call__(self, args, payload=None): self.reads += 1 if self.read_error: raise RuntimeError("gh api: HTTP 502") + if self.pr_after is not None and self.reads > 1: + return json.dumps(self.pr_after) if self.pr_override is not None: return json.dumps(self.pr_override) head = self.head_after if (self.head_after and self.reads > 1) else self.head @@ -134,21 +138,46 @@ def test_skip_label_vetoes_all_green_and_withdraws_prior_approval(self): self.assertEqual(fake.dismissed, ["42"]) self.assertEqual(self.resolve_calls, []) - def test_skip_label_is_read_live_not_from_the_event_payload(self): - # The run started on `labeled: cursor-review`; the veto landed mid-axes, - # so only the live PR read carries it. - fake = FakeGitHub(labels=["cursor-review", aa.SKIP_REVIEW_LABEL]) + def run_with_event_labels(self, fake, event_labels): with tempfile.TemporaryDirectory() as tmp: event = os.path.join(tmp, "event.json") with open(event, "w", encoding="utf-8") as f: json.dump({"action": "labeled", "label": {"name": "cursor-review"}, "pull_request": {"number": 1, "head": {"sha": SHA}, - "labels": [{"name": "cursor-review"}]}}, f) + "labels": [{"name": n} for n in event_labels]}}, f) with mock.patch.dict(os.environ, {"GITHUB_EVENT_PATH": event}): - rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}) + return self.run_cmd(fake, {"correctness": "green", "conformance": "green"}) + + def test_skip_label_is_read_live_not_from_the_event_payload(self): + # Live and payload labels disagree in both directions; only the live + # read may decide. The run started on `labeled: cursor-review` and the + # veto landed mid-axes: only the live read carries it. + fake = FakeGitHub(labels=["cursor-review", aa.SKIP_REVIEW_LABEL]) + self.assertEqual(self.run_with_event_labels(fake, ["cursor-review"]), (0, "vetoed")) + self.assertEqual(fake.posted, []) + # The veto was removed mid-axes: a stale payload must not keep it. + fake = FakeGitHub(labels=["cursor-review"]) + self.assertEqual(self.run_with_event_labels(fake, ["cursor-review", aa.SKIP_REVIEW_LABEL]), + (0, "approved")) + self.assertEqual(len(fake.posted), 1) + + def test_skip_label_outranks_a_moved_head(self): + # A late decide seeing both a moved head and the veto must withdraw the + # approval already standing on the live head, not only stale ones. + fake = FakeGitHub(head="d" * 40, labels=[aa.SKIP_REVIEW_LABEL], + reviews=[prior_approval(), prior_approval("d" * 40, 77)]) + rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}) self.assertEqual((rc, outcome), (0, "vetoed")) self.assertEqual(fake.posted, []) - self.assertGreaterEqual(fake.reads, 1) + self.assertEqual(sorted(fake.dismissed), ["42", "77"]) + + def test_malformed_label_entries_fail_closed(self): + # Entries `has_label` would skip cannot rule the veto out. + for labels in (["skip-cursor-review"], [{"name": None}], [{"name": "bug"}, 7]): + fake = FakeGitHub(pr_override={"head": {"sha": SHA}, "base": {"ref": "main"}, "labels": labels}) + rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}) + self.assertEqual((rc, outcome), (1, "error"), labels) + self.assertEqual(fake.posted, [], labels) def test_skip_label_match_is_exact(self): fake = FakeGitHub(labels=["skip-cursor-review-later", "no-skip-cursor-review"]) @@ -181,8 +210,35 @@ def test_skip_label_applied_during_post_withdraws(self): self.assertEqual((rc, outcome), (0, "vetoed")) self.assertEqual(len(fake.posted), 1) self.assertEqual(fake.dismissed, ["555"]) + self.assertEqual(fake.dismiss_messages, [aa.SKIP_REVIEW_MESSAGE]) self.assertEqual(self.resolve_calls, []) + def test_skip_label_during_post_withdraws_every_own_approval(self): + # The veto is PR-wide: an approval this identity already has standing on + # the same head goes too, not only the one just posted. + fake = FakeGitHub(labels_after=[aa.SKIP_REVIEW_LABEL], head_after="d" * 40, + reviews=[prior_approval(SHA, 77)]) + rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}) + self.assertEqual((rc, outcome), (0, "vetoed")) + self.assertEqual(fake.dismissed, ["555", "77"]) + + def test_unreadable_labels_after_post_withdraws_under_its_own_cause(self): + fake = FakeGitHub(pr_after={"head": {"sha": SHA}, "base": {"ref": "main"}}, + reviews=[prior_approval(SHA, 77)]) + rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}, + threshold="low", poster="cr-bot[bot]") + self.assertEqual((rc, outcome), (1, "error")) + self.assertEqual(fake.dismissed, ["555", "77"]) + self.assertEqual(fake.dismiss_messages, [aa.LABELS_UNREADABLE_MESSAGE] * 2) + self.assertEqual(self.resolve_calls, []) + + def test_post_without_a_review_id_withdraws_and_fails(self): + for reply in ("proxy error", "null", "{}"): + fake = FakeGitHub(post_reply=reply, reviews=[prior_approval(SHA, 77)]) + rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"}) + self.assertEqual((rc, outcome), (1, "error"), reply) + self.assertEqual(fake.dismissed, ["77"], reply) + def test_late_superseded_decide_keeps_an_approval_on_the_live_head(self): # An older run's decide finishing after a newer run approved the new head # must withdraw only what is stale against that head.