From 4b5a0562c92c5ffe762786f8e0d923cfc5bd223a Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Sun, 11 Oct 2026 21:16:26 -0700 Subject: [PATCH 1/3] feat(cursor-review): link every auto-approve review to the cursor-approve card (BE-20246) Every review the approver posts (decide's APPROVE, REQUEST_CHANGES and standing block, and approve-external's standing block) now carries a link to the status card. approve-external's approval already linked it. The link has to be in the review when it is posted. Editing a review afterwards would break it: an edited approval reads as stale, and the bot never withdraws an edited request for changes. So on a PR's first round, before any card exists, decide creates a pending card (no_decision/none) just before the review. The round's card replaces it in the same step, by comment id rather than a re-list, so a lagging listing cannot produce a second card. Rounds that post no review create no pending card, and neither does a PR the approver authored. A card that cannot be found or created only costs the review its link. --- .github/cursor-approve/card.py | 62 ++++++- .github/cursor-approve/tests/test_card.py | 96 +++++++++++ .github/cursor-review/README.md | 2 +- .github/cursor-review/auto-approve.py | 92 ++++++++++- .../tests/test_approve_external.py | 27 ++- .../cursor-review/tests/test_auto_approve.py | 155 +++++++++++++++++- docs/callers/cursor-approve.md | 12 +- docs/callers/cursor-review.md | 10 ++ 8 files changed, 433 insertions(+), 23 deletions(-) diff --git a/.github/cursor-approve/card.py b/.github/cursor-approve/card.py index 7c63db5f..d09cf1ba 100644 --- a/.github/cursor-approve/card.py +++ b/.github/cursor-approve/card.py @@ -33,6 +33,13 @@ ``capped``) the caller's ``if:`` let through. On a ``pass`` the start phase then overwrites it with the axes table, as before. +Every review that decide posts links the card (BE-20246), and so must exist +before it: a review cannot be edited afterwards to add the link, because +auto-approve.py treats an edited approval as stale and never withdraws an +edited request for changes. So on a PR's first round decide calls ``ensure`` +just before its review, which creates the card from ``render_pending`` when +there is none, and the round's ``render_round`` replaces that in the same step. + Every card carries two machine-readable markers on the lines right after ``CARD_MARKER`` — ``STATE_MARKER`` and ``NEXT_MARKER`` — a stable contract for agents, documented in docs/callers/cursor-approve.md. @@ -496,6 +503,25 @@ def _gating_row(finding: dict) -> str: return f"- **{sev}** — {ref} — {link}" + (f" — gated: {why}" if why else "") +PENDING_TEXT = ("Deciding this round. This card is rewritten with the result in a moment; if it still says " + "this, the card was not updated, so this round's latest auto-approve review is the decision and " + "the workflow run says why.") + + +def render_pending(round_no, max_rounds, sha: str, run_url: str) -> str: + """The card decide creates on a PR's first round, just before posting the + review that links to it (BE-20246); ``render_round`` replaces it seconds + later, in the same step. + + ``no_decision``/``none``: nothing has been decided yet, and nothing is asked + of an agent that reads the card in that window. A ``next`` with a remedy in + it would have an agent push or relabel under a round that may be approving. + """ + lines = card_head(STATE_NO_DECISION, NEXT_NONE) + [heading(round_no, max_rounds, sha), "", f"**{PENDING_TEXT}**"] + lines += ["", _reviewed_line(sha, run_url)] + return "\n".join(lines) + "\n" + + def render_round(round_no, max_rounds, sha: str, state: str, next_step: str, headline: str, reasons: list, next_text: str, run_url: str, gating=None, threshold: str = "") -> str: """The card cursor-review's decide writes for the round it just decided. @@ -722,13 +748,39 @@ def list_comments(repo: str, pr_number) -> list: return [c for page in pages for c in page] if pages and isinstance(pages[0], list) else pages -def upsert(repo: str, pr_number, login: str, body: str, entry=None, keep=None) -> dict: +def _comment(text: str) -> dict: + """A comment write's response, which must name the comment it wrote.""" + comment = json.loads(text) + if not isinstance(comment, dict) or comment.get("id") is None: + raise ValueError(f"the comment write returned no comment id ({type(comment).__name__})") + return comment + + +def ensure(repo: str, pr_number, login: str, body: str) -> tuple: + """(card, created): this PR's card, created with `body` when there is none. + + For a review that links the card, which needs the comment's URL before it + is posted: this file's ``ensure`` phase, and cursor-review's decide on a + PR's first round (BE-20246). + """ + card = find_card(list_comments(repo, pr_number), login) + if card is not None: + return card, False + return _comment(gh(["api", "-X", "POST", f"repos/{repo}/issues/{pr_number}/comments", "--input", "-"], + {"body": body})), True + + +def upsert(repo: str, pr_number, login: str, body: str, entry=None, keep=None, card=None) -> dict: """Edit the existing card in place, or create it when there is none. The card's axes sentinel is carried into `body` (see `with_axes_history`); `entry` and `keep` are decide's; `keep` stays None for every other writer. + `card` is a card this run created seconds ago (``ensure``): it is edited + without listing the comments again, since a listing taken that soon after + the write could miss it and post a second card. """ - card = find_card(list_comments(repo, pr_number), login) + if card is None: + card = find_card(list_comments(repo, pr_number), login) body = with_axes_history(body, card.get("body") if card is not None else "", entry, keep) if card is not None: return json.loads(gh(["api", "-X", "PATCH", f"repos/{repo}/issues/comments/{card['id']}", "--input", "-"], @@ -795,10 +847,8 @@ def main(argv=None) -> int: body = render_start(args.round, args.max_rounds, args.commit_sha, axes, args.approve_gate, args.run_url) elif args.cmd == "ensure": try: - card = find_card(list_comments(args.repo, args.pr_number), args.login) - if card is None: - body = render_start(args.round, args.max_rounds, args.commit_sha, axes, "", args.run_url) - card = upsert(args.repo, args.pr_number, args.login, body) + card, _ = ensure(args.repo, args.pr_number, args.login, + render_start(args.round, args.max_rounds, args.commit_sha, axes, "", args.run_url)) except (RuntimeError, ValueError) as e: print(f"::warning::Could not find or create the cursor-approve card: {e}") return 0 diff --git a/.github/cursor-approve/tests/test_card.py b/.github/cursor-approve/tests/test_card.py index 4ae8a5d9..3945341b 100644 --- a/.github/cursor-approve/tests/test_card.py +++ b/.github/cursor-approve/tests/test_card.py @@ -129,6 +129,17 @@ def test_start_pass(self): self.assertIn(f"Reviewed commit: `{SHA}`", body) self.assertNotIn("Next step", body) + def test_pending_asks_nothing_of_an_agent(self): + # BE-20246: on screen for the seconds between a first round's review + # and its real card. A remedy in `next` would have an agent push or + # relabel under a round that may be approving. + body = card.render_pending("1", "5", SHA, RUN) + self.assertEqual(markers(body), ("no_decision", "none")) + self.assertIn("Cursor approve · round 1 of 5 · `aaaaaaa`", body) + self.assertIn(f"**{card.PENDING_TEXT}**", body) + self.assertIn(f"Reviewed commit: `{SHA}` · [workflow run]({RUN})", body) + self.assertNotIn("Next step", body) + def test_start_capped(self): body = card.render_start("5", "5", SHA, ["correctness"], "capped", RUN) self.assertEqual(markers(body), ("capped", "human")) @@ -393,6 +404,91 @@ def test_no_card_creates_one(self): self.assertEqual(len(writes), 1) self.assertEqual(writes[0][0][2], "POST") + def test_a_card_this_run_created_is_edited_without_listing_again(self): + # BE-20246: a listing taken seconds after `ensure` created the card + # could miss it, and upsert would then post a second card. + calls = [] + + def fake_gh(args, payload=None): + calls.append((args, payload)) + return json.dumps({"id": 9}) + + with mock.patch.object(card, "gh", fake_gh): + card.upsert("o/r", 1, LOGIN, card.CARD_MARKER + "\nnew", + card={"id": 9, "user": {"login": LOGIN}, "body": card.render_pending("1", "5", SHA, RUN)}) + self.assertEqual([c[0][:4] for c in calls], [["api", "-X", "PATCH", "repos/o/r/issues/comments/9"]]) + self.assertEqual(calls[0][1], {"body": card.CARD_MARKER + "\nnew"}) + + +class Ensure(unittest.TestCase): + """`ensure`: the card a review links to, created first when there is none.""" + + URL = "https://github.com/o/r/pull/1#issuecomment-9" + + def run_ensure(self, comments, reply=None): + self.writes = [] + + def fake_gh(args, payload=None): + if "--paginate" in args: + return json.dumps([comments]) + self.writes.append((args[2], args[3], payload)) + return json.dumps({"id": 9, "html_url": self.URL}) if reply is None else reply + + with mock.patch.object(card, "gh", fake_gh): + return card.ensure("o/r", 1, LOGIN, "PENDING") + + def test_an_existing_card_is_returned_untouched(self): + existing = {"id": 7, "user": {"login": LOGIN}, "body": card.CARD_MARKER + "\nold", + "html_url": "https://github.com/o/r/pull/1#issuecomment-7"} + self.assertEqual(self.run_ensure([existing]), (existing, False)) + self.assertEqual(self.writes, []) + + def test_no_card_creates_one_from_the_body_given(self): + found, created = self.run_ensure([]) + self.assertTrue(created) + self.assertEqual(found["html_url"], self.URL) + self.assertEqual(self.writes, [("POST", "repos/o/r/issues/1/comments", {"body": "PENDING"})]) + + def test_a_marker_from_another_author_is_not_the_card(self): + _, created = self.run_ensure([{"id": 7, "user": {"login": "attacker"}, "body": card.CARD_MARKER}]) + self.assertTrue(created) + + def test_a_reply_without_a_comment_id_is_an_error(self): + for reply in ("null", "[]", "{}", '{"html_url": "https://github.com/o/r/pull/1"}'): + with self.subTest(reply=reply), self.assertRaises(ValueError): + self.run_ensure([], reply=reply) + + def test_the_ensure_phase_prints_the_url_it_found_or_created(self): + for comments, want in (([], self.URL), + ([{"id": 7, "user": {"login": LOGIN}, "body": card.CARD_MARKER, + "html_url": "https://github.com/o/r/pull/1#issuecomment-7"}], + "https://github.com/o/r/pull/1#issuecomment-7")): + with self.subTest(want=want), tempfile.TemporaryDirectory() as tmp: + out = os.path.join(tmp, "out") + + def fake_gh(args, payload=None, comments=comments): + if "--paginate" in args: + return json.dumps([comments]) + return json.dumps({"id": 9, "html_url": self.URL}) + + with mock.patch.object(card, "gh", fake_gh), mock.patch.dict(os.environ, {"GITHUB_OUTPUT": out}): + self.assertEqual(card.main(["ensure", "--repo", "o/r", "--pr-number", "1", "--login", LOGIN, + "--commit-sha", SHA, "--axes", "correctness"]), 0) + with open(out, encoding="utf-8") as f: + self.assertEqual(f.read(), f"card_url={want}\n") + + def test_the_ensure_phase_never_fails_the_job(self): + with tempfile.TemporaryDirectory() as tmp: + out = os.path.join(tmp, "out") + open(out, "w").close() + with mock.patch.object(card, "gh", side_effect=RuntimeError("gh api failed: HTTP 502")), \ + mock.patch.dict(os.environ, {"GITHUB_OUTPUT": out}), \ + contextlib.redirect_stdout(io.StringIO()): + self.assertEqual(card.main(["ensure", "--repo", "o/r", "--pr-number", "1", "--login", LOGIN, + "--commit-sha", SHA, "--axes", "correctness"]), 0) + with open(out, encoding="utf-8") as f: + self.assertEqual(f.read(), "") + def _secrets_declared(path): """Names under `on.workflow_call.secrets` — text-parsed (stdlib only).""" diff --git a/.github/cursor-review/README.md b/.github/cursor-review/README.md index 54c91610..40835384 100644 --- a/.github/cursor-review/README.md +++ b/.github/cursor-review/README.md @@ -249,7 +249,7 @@ the consolidated review's panel table. See [Panel integrity](#panel-integrity). | [`review-output-mcp.py`](review-output-mcp.py) | Dependency-free stdio MCP server. Reviewers record individual findings and finish; the judge submits the final findings array. It validates and atomically writes the normalized records consumed by later jobs. | | [`stop-on-submit.py`](stop-on-submit.py) | Wraps each panel cell's `cursor-agent` and polls its `findings.json`. Once the file parses as a JSON object with `"status": "ok"`, the agent gets 15s to exit by itself, then SIGTERM to its process group, then SIGKILL 10s later, so a cell that has submitted exits **within 30 seconds** of submitting instead of idling to the 15-minute step cap. It only reads the file, so the artifact is byte-for-byte what the MCP tool wrote, and the stop exits 0, so the leg stays green. A missing, partial, unparseable or non-`ok` file never triggers a stop, so a cell that never submits still runs to the cap and fails its leg as before. The step cap's and a run cancel's signals reach it through the step shell's traps and are forwarded to the agent's group, which is SIGKILLed at most 5s later (exit 128+signal). Every exit path, including an agent that exits by itself, ends with SIGKILL to the group, so nothing it left running can rewrite the findings after the upload. Only a regular file of at most 8 MiB is read (no FIFO/symlink/device). It also retries Cursor's transient capacity error: an agent that exits non-zero with `[resource_exhausted]` / `RetriableError` in the tail of its stderr (never its stdout, the model's own text, which the PR under review can steer) is re-run after `--retry-delays` (30s, then 90s in the workflow), each retry logged as one `::warning::` on a line of its own, naming the cell. Every attempt reads the whole prompt: the wrapper puts its stdin (`< /tmp/prompt.txt`) back at its starting offset before each retry, so the prompt must arrive as a file redirect; a stdin that cannot seek (a pipe, a tty) rules retries out. A retry needs the findings file to still be an unsubmitted record with **no** recorded findings (the MCP tool appends, so a re-run would duplicate them) and must start within `--retry-window` (300s), so the last attempt still fits the step cap. Exit 0, any other error, a signal death and a forwarded signal are never retried. | | [`post-review.py`](post-review.py) | Reads the judge's consolidated findings and posts **one** PR review with line-anchored inline comments and severity badges. | -| [`auto-approve.py`](auto-approve.py) | Opt-in auto-approve (`approve_max_severity`): `decide` (in `post-review`) approves / requests changes / reaches no decision (withdrawing its own earlier approvals and leaving one standing REQUEST_CHANGES with the reasons and the next step, replaced by the next no-decision round and dismissed by the next approving one; none on a `capped` round, which withdraws the earlier one) per its fail-closed rules, then writes cursor-approve's status card for the outcome (via [`../cursor-approve/card.py`](../cursor-approve/card.py); no card for an author `approve_authors` does not list); `dismiss-stale` (the **Dismiss stale auto-approval** job) withdraws the bot's own approvals that are stale against the PR's live head and base (a push, a reopen, a retarget) — and every one of them while the PR carries `skip-cursor-review` or `needs-human-review` — plus its own standing REQUEST_CHANGES while the PR carries either label (re-read after the reviews are listed) or `approve_max_severity` is empty (then only once none of its approvals still stands), on every event of an open PR, whether or not `approve_max_severity` is still set; `round-cap` (the **Round cap** job, ahead of the panel) enforces `max_rounds`. On an APPROVE that survived its post-write re-check, `decide` also resolves (re-reading each first) and then replies on the posting identity's own at-or-below-threshold threads nobody else commented in (nothing, if any live thread is above the threshold); [`build-ledger.py`](build-ledger.py) never counts that reply as an answer. `decide` also writes the `approve_gate` output, and `round-cap` feeds the `round` and `max_rounds` outputs. Reads open-thread severity through `gate-unresolved.py`'s query. See [`docs/callers/cursor-review.md`](../../docs/callers/cursor-review.md#auto-approve). | +| [`auto-approve.py`](auto-approve.py) | Opt-in auto-approve (`approve_max_severity`): `decide` (in `post-review`) approves / requests changes / reaches no decision (withdrawing its own earlier approvals and leaving one standing REQUEST_CHANGES with the reasons and the next step, replaced by the next no-decision round and dismissed by the next approving one; none on a `capped` round, which withdraws the earlier one) per its fail-closed rules, then writes cursor-approve's status card for the outcome (via [`../cursor-approve/card.py`](../cursor-approve/card.py); no card for an author `approve_authors` does not list), and every review it posts links that card (on a PR's first round it creates a pending card just before the review, since a review cannot be edited to add the link later); `dismiss-stale` (the **Dismiss stale auto-approval** job) withdraws the bot's own approvals that are stale against the PR's live head and base (a push, a reopen, a retarget) — and every one of them while the PR carries `skip-cursor-review` or `needs-human-review` — plus its own standing REQUEST_CHANGES while the PR carries either label (re-read after the reviews are listed) or `approve_max_severity` is empty (then only once none of its approvals still stands), on every event of an open PR, whether or not `approve_max_severity` is still set; `round-cap` (the **Round cap** job, ahead of the panel) enforces `max_rounds`. On an APPROVE that survived its post-write re-check, `decide` also resolves (re-reading each first) and then replies on the posting identity's own at-or-below-threshold threads nobody else commented in (nothing, if any live thread is above the threshold); [`build-ledger.py`](build-ledger.py) never counts that reply as an answer. `decide` also writes the `approve_gate` output, and `round-cap` feeds the `round` and `max_rounds` outputs. Reads open-thread severity through `gate-unresolved.py`'s query. See [`docs/callers/cursor-review.md`](../../docs/callers/cursor-review.md#auto-approve). | | [`gate-unresolved.py`](gate-unresolved.py) | The opt-in blocking gate (`blocking: true` → the **Blocking gate** job): queries the PR's review threads and exits non-zero while any cursor-review finding thread is unresolved and non-outdated. Dropped from `cursor-review.yml` by accident in #31 and restored by BE-4691 — see [the blocking section](#optional-make-the-review-blocking). Double-billed: [`build-ledger.py`](build-ledger.py) also imports it for `CONSOLIDATED_MARKER`, the paging `reviewThreads` GraphQL query and the `iter_threads` / `is_cursor_thread` helpers, so the gate and the ledger can never disagree about which threads are ours. | | [`slack-notify.sh`](slack-notify.sh) | Sends the start/complete Slack DMs to the triggerer (no-ops without a token). | | [`install-cursor-cli.sh`](install-cursor-cli.sh) | Installs the Cursor agent CLI from the versioned, sha256-pinned release artifact — not `curl cursor.com/install \| bash`. Used by all three CLI-using jobs; the pin (`CURSOR_CLI_VERSION` / `CURSOR_CLI_SHA256`) lives in `cursor-review.yml`'s top-level `env:`. | diff --git a/.github/cursor-review/auto-approve.py b/.github/cursor-review/auto-approve.py index e6c1ed44..e454b6c3 100644 --- a/.github/cursor-review/auto-approve.py +++ b/.github/cursor-review/auto-approve.py @@ -12,7 +12,11 @@ (see below). The review is pinned (`commit_id`) to the commit the panel reviewed. With ``--card true`` it then writes cursor-approve's status card (``.github/cursor-approve/card.py``) for the outcome — every outcome except - an author ``approve_authors`` does not list. + an author ``approve_authors`` does not list. Every review it posts links + that card (BE-20246). On a PR's first round there is no card yet, so it + creates a pending one just before the review (``review_card_url``): a + review edited afterwards to add the link would read as stale, or never be + withdrawn. ``dismiss-stale`` (in `dismiss-stale-approval`, on every same-repo `pull_request` event of an open PR; ``--all-approvals`` on a base retarget) @@ -62,6 +66,7 @@ An outcome that withholds (``EXTERNAL_BLOCK_OUTCOMES``) leaves the same standing REQUEST_CHANGES a no-decision ``decide`` does (BE-19489); one that hands the PR to a human (``needs_human``, ``vetoed``) withdraws it instead. + The approval and that block both link the card ``--card-url`` names. ``withdraw`` (in cursor-approve.yml's start phase) Withdraw this identity's own marked approvals while the axes run — see @@ -1526,8 +1531,73 @@ def resolve_eligible_threads(repo: str, pr, poster_login: str, threshold: str, c return counts +# What a review body may link as the status card: an https URL with nothing in +# it that could close the markdown link or start another one. +CARD_URL_RE = re.compile(r"https://[A-Za-z0-9.-]+/[A-Za-z0-9_./#-]+") + + +def safe_card_url(url) -> str: + """`url` when it is safe to put in a review body's markdown link, else "".""" + return url if isinstance(url, str) and CARD_URL_RE.fullmatch(url) else "" + + +def card_link_line(url) -> str: + """The line every auto-approve review carries to the status card (BE-20246), + or "" when there is no card URL safe to link.""" + url = safe_card_url(url) + return f"Live status: the [cursor-approve card]({url}), updated in place every round." if url else "" + + +def review_card_url(args) -> str: + """The status card's URL for the review about to be posted, or "". + + approve-external is handed it (``--card-url``, from cursor-approve.yml's + ``ensure`` step). decide looks it up the first time it needs it and keeps it + for the rest of the run. On a PR's first round there is no card yet, so it + creates one from ``card.render_pending`` first: the link cannot be added + after the review is posted, because an edited approval reads as stale and + an edited request for changes is never withdrawn (see ``_review_node``). + The round's card replaces the pending one in the same step, after the + review, as before. + + Never fatal: a card that cannot be found or created costs the review its + link, nothing else. + """ + url = getattr(args, "card_url", None) + if url is None: + url = args.card_url = _ensure_card_url(args) + return safe_card_url(url) + + +def _ensure_card_url(args) -> str: + login = (getattr(args, "approver_login", "") or "").strip() + # The same switch write_round_card obeys: without it decide writes no card, + # so there is nothing to link. + if not login or (getattr(args, "card", "") or "").strip().lower() != "true": + return "" + # GitHub refuses the approver a review of its own PR, so there is nothing + # to link, and cmd_decide returns on that refusal without writing the card + # that would replace a pending one. + if (getattr(args, "pr_author", "") or "").strip().lower() == login.lower(): + return "" + try: + card = _load_card() + found, created = card.ensure(args.repo, args.pr_number, login, + card.render_pending(getattr(args, "round", ""), getattr(args, "max_rounds", ""), + args.commit_sha, getattr(args, "run_url", "") or "")) + if created: + # write_round_card edits this comment by id: a listing taken + # seconds after it was created could miss it and post a second card. + args.created_card = found + return found.get("html_url") or "" + except Exception as e: # noqa: BLE001 — a link is a convenience; it must never cost the review + print(f"::warning::Could not find or create the cursor-approve status card, so this review does not " + f"link it: {annotation_cause(e, 'unknown error')}") + return "" + + def render_body(event: str, reasons: list, threshold: str, blocking: list, reviewed_sha: str = "", - base_ref: str = "") -> str: + base_ref: str = "", card_url: str = "") -> str: lines = [APPROVE_MARKER] if re.fullmatch(r"[0-9a-f]{40}", (reviewed_sha or "").lower()): lines.append(f"") @@ -1552,6 +1622,9 @@ def render_body(event: str, reasons: list, threshold: str, blocking: list, revie line = str(line) if isinstance(line, int) and not isinstance(line, bool) else "?" path = f.get("file") if isinstance(f.get("file"), str) else "?" lines.append(f"- **{sev}** — {render_code_ref(path[:300], line)}") + link = card_link_line(card_url) + if link: + lines.append(f"\n{link}") # The scope note is scope_note()'s own sentence, so it is not routed # through card.reason_text: an unloadable card.py must not cost the APPROVE. if scope_note_of(reasons): @@ -1564,7 +1637,7 @@ def render_body(event: str, reasons: list, threshold: str, blocking: list, revie def render_standing_body(headline: str, reasons: list, next_text: str, threshold: str, reviewed_sha: str = "", base_ref: str = "", prerendered: bool = False, - retry: bool = False) -> str: + retry: bool = False, card_url: str = "") -> str: """The standing REQUEST_CHANGES a no-decision round leaves (BE-19489). Same markers as every other auto-approve review, so dismiss_own_change_requests @@ -1588,6 +1661,9 @@ def render_standing_body(headline: str, reasons: list, next_text: str, threshold # is already markdown-safe (axis reasons sanitized as on the card). lines += [f"- {r if prerendered else reason_text(r)}" for r in reasons if isinstance(r, str) and r.strip()] lines.append(f"\n**Next step:** {next_text}") + link = card_link_line(card_url) + if link: + lines.append(f"\n{link}") if threshold in ALLOWED_THRESHOLDS: lines.append(f"\n_Threshold: `{threshold}` (set by this repo's `approve_max_severity`)._") return "\n".join(lines) @@ -1640,9 +1716,10 @@ def post_standing_change_request(args, headline: str, reasons: list, next_text: the body's reviewed-SHA marker still naming what was reviewed. `result`, when given, receives the posted review's `id`. """ + card_url = review_card_url(args) try: body = render_standing_body(headline, reasons, next_text, threshold, args.commit_sha, args.base_ref, - prerendered, retry) + prerendered, retry, card_url) except Exception as e: # noqa: BLE001 — card.py failing to load must not traceback here print(f"::error::Could not render the standing request for changes: {annotation_cause(e, 'unknown error')}") return 1 @@ -1690,7 +1767,8 @@ def write_round_card(args, state: str, next_step: str, headline: str, reasons: l body = card.render_round(getattr(args, "round", ""), getattr(args, "max_rounds", ""), args.commit_sha, state, next_step, headline, reasons, next_text, getattr(args, "run_url", "") or "", gating, threshold) - card.upsert(args.repo, args.pr_number, login, body) + # The pending card review_card_url created for this round's review, if it did. + card.upsert(args.repo, args.pr_number, login, body, card=getattr(args, "created_card", None)) except Exception as e: # noqa: BLE001 — a report: it runs after the decision landed, never undoes it print(f"::warning::Could not write the cursor-approve status card: {annotation_cause(e, 'unknown error')}") @@ -1918,7 +1996,7 @@ def cmd_decide(args) -> int: why, next_text, threshold) return rc - body = render_body(event, reasons, threshold, blocking, args.commit_sha, args.base_ref) + body = render_body(event, reasons, threshold, blocking, args.commit_sha, args.base_ref, review_card_url(args)) if event == APPROVE and not REVIEWED_SHA_RE.search(body): # The recorded SHA is the only staleness anchor an approval has (see # REVIEWED_SHA_RE), and render_body drops the marker for anything that is @@ -2972,7 +3050,7 @@ def render_external_body(card_url: str, reviewed_sha: str, base_ref: str) -> str lines.append(f"") if base_ref: lines.append(f"") - if re.fullmatch(r"https://[A-Za-z0-9.-]+/[A-Za-z0-9_./#-]+", card_url or ""): + if safe_card_url(card_url): lines.append(f"Approved, see the [cursor-approve card]({card_url}).") else: lines.append("Approved, see the cursor-approve card.") diff --git a/.github/cursor-review/tests/test_approve_external.py b/.github/cursor-review/tests/test_approve_external.py index 118a6cbe..88a410d6 100644 --- a/.github/cursor-review/tests/test_approve_external.py +++ b/.github/cursor-review/tests/test_approve_external.py @@ -17,6 +17,7 @@ SHA = "b" * 40 LOGIN = "approver-bot" AXES = "correctness,conformance" +CARD_URL = "https://github.com/o/r/pull/1#issuecomment-9" class FakeGitHub: @@ -75,7 +76,8 @@ def handoff_reviews(): class ApproveExternal(unittest.TestCase): - def run_cmd(self, fake, verdicts, event=None, login=LOGIN, base_ref="main", threshold="", poster="", **extra): + def run_cmd(self, fake, verdicts, event=None, login=LOGIN, base_ref="main", threshold="", poster="", + card_url=CARD_URL, **extra): if event is None: event = "APPROVE" if all(v in ("green", "yellow") for v in verdicts.values()) and verdicts else "NONE" with tempfile.TemporaryDirectory() as tmp: @@ -84,7 +86,7 @@ def run_cmd(self, fake, verdicts, event=None, login=LOGIN, base_ref="main", thre json.dump({"event": event, "verdicts": verdicts, "axes": {}, "reasons": []}, f) out = os.path.join(tmp, "out") args = argparse.Namespace(repo="o/r", pr_number="1", commit_sha=SHA, axes=AXES, decision=path, - approver_login=login, base_ref=base_ref, card_url="https://github.com/o/r/pull/1#issuecomment-9", + approver_login=login, base_ref=base_ref, card_url=card_url, threshold=threshold, poster_login=poster, **extra) self.resolve_calls = [] with mock.patch.object(aa, "gh", fake), mock.patch.dict(os.environ, {"GITHUB_OUTPUT": out}), \ @@ -137,6 +139,27 @@ def test_a_red_axis_posts_a_standing_request_for_changes(self): self.assertIn(f"**Next step:** {card.DECIDE_NEXT_TEXT['not_approved']}", review["body"]) self.assertIn("_Threshold: `low`", review["body"]) + def test_the_block_links_the_card_the_ensure_step_found(self): + # BE-20246: its next step says "Address the axis verdicts above", and + # the verdicts are on the card, so the block has to link it. + for outcome, fake in (("not_approved", FakeGitHub()), ("superseded", FakeGitHub(head="d" * 40)), + ("error", FakeGitHub(read_error=True))): + with self.subTest(outcome=outcome): + self.run_cmd(fake, {"correctness": "red", "conformance": "green"}) + self.assertEqual([p["event"] for p in fake.posted], ["REQUEST_CHANGES"]) + self.assertIn(f"Live status: the [cursor-approve card]({CARD_URL}), updated in place every round.", + fake.posted[0]["body"].split("\n")) + + def test_an_unsafe_card_url_is_never_linked(self): + url = "https://github.com/o/r/pull/1) [forged](https://evil.example" + fake = FakeGitHub() + self.run_cmd(fake, {"correctness": "green", "conformance": "green"}, card_url=url) + self.assertIn("Approved, see the cursor-approve card.", fake.posted[0]["body"]) + fake = FakeGitHub() + self.run_cmd(fake, {"correctness": "red", "conformance": "green"}, card_url=url) + for p in fake.posted: + self.assertNotIn("](", p["body"]) + def test_the_block_carries_the_axis_reasons_sanitized(self): fake = FakeGitHub() with tempfile.TemporaryDirectory() as tmp: diff --git a/.github/cursor-review/tests/test_auto_approve.py b/.github/cursor-review/tests/test_auto_approve.py index 40ecd2a3..6da1e62b 100644 --- a/.github/cursor-review/tests/test_auto_approve.py +++ b/.github/cursor-review/tests/test_auto_approve.py @@ -1273,6 +1273,41 @@ def test_model_values_cannot_inject_markdown(self): self.assertNotIn("pwn", body) self.assertIn(":?", body) + URL = "https://github.com/o/r/pull/1#issuecomment-7" + LINK = f"Live status: the [cursor-approve card]({URL}), updated in place every round." + + def test_every_review_body_links_the_card_and_keeps_its_markers(self): + # BE-20246. The markers sit above the link and are what everything + # parses back: staleness, the recorded base, the retry budget. + bodies = { + "approve": AA.render_body(AA.APPROVE, ["every finding is at or below `low`"], "low", [], + SHA, "main", self.URL), + "changes": AA.render_body(AA.REQUEST_CHANGES, ["1 finding(s) above `low`"], "low", + [finding("high")], SHA, "main", self.URL), + "standing": AA.render_standing_body(AA.NO_DECISION_HEADLINE, ["the judge errored"], "Re-run the round.", + "low", SHA, "main", retry=True, card_url=self.URL), + } + for name, body in bodies.items(): + with self.subTest(name): + self.assertIn(self.LINK, body.split("\n")) + self.assertTrue(body.startswith(AA.APPROVE_MARKER + "\n")) + self.assertEqual(AA.REVIEWED_SHA_RE.search(body).group(1), SHA) + self.assertEqual(AA.recorded_base(body), "main") + self.assertTrue(body.endswith("(set by this repo's `approve_max_severity`)._")) + self.assertIn(AA.AUTO_RETRY_MARKER, bodies["standing"].split("\n")) + + def test_an_unsafe_or_missing_card_url_is_never_linked(self): + for url in ("", None, 42, "http://github.com/o/r/pull/1", "javascript:alert(1)", + "https://github.com/o/r/pull/1) [forged](https://evil.example", + "https://github.com/o/r/pull/1#x\n## forged", "https://github.com/o/r/pull/1 @someone"): + with self.subTest(url=url): + self.assertEqual((AA.safe_card_url(url), AA.card_link_line(url)), ("", "")) + for body in (AA.render_body(AA.APPROVE, ["ok"], "low", [], SHA, "main", url), + AA.render_standing_body("h", ["r"], "n", "low", SHA, "main", card_url=url), + AA.render_external_body(url, SHA, "main")): + self.assertNotIn("](", body) + self.assertNotIn("Live status", body) + def read_outputs(path): """$GITHUB_OUTPUT as a dict; a later write of a key wins, as in Actions.""" @@ -2515,12 +2550,18 @@ class StatusCardAndStandingBlockTest(unittest.TestCase): def run_decide(self, findings=(), panel=PANEL_OK, judge="ok", delivered="true", diff=DIFF, reviews=(), comments=(), threads=(), labels=(), defer="", author_enabled="", head=SHA, card="true", review_label="", refuse_pin=False, head_seq=None, - fail_first_post=False): + fail_first_post=False, comments_fail=False, post_error="", pr_author="someone"): self.reviews_posted, self.dismissed, self.card_writes = [], [], [] + # Every write, in order, and every comment listing (BE-20246). + self.writes, self.listings = [], 0 failed = [] def fake_gh(args, payload=None): + if args[:2] == ["api", "-X"]: + self.writes.append((args[2], args[3])) if args[:3] == ["api", "-X", "POST"] and args[3].endswith("/reviews"): + if post_error: + raise RuntimeError(post_error) if refuse_pin and "commit_id" in payload and payload["event"] == AA.REQUEST_CHANGES: raise RuntimeError("gh api failed: HTTP 422: No commit found for SHA") if fail_first_post and not failed: @@ -2535,6 +2576,9 @@ def fake_gh(args, payload=None): self.card_writes.append((args[2], args[3], payload["body"])) return json.dumps({"id": 7, "html_url": "https://github.com/o/r/pull/1#issuecomment-7"}) if args[:2] == ["api", "--paginate"]: + self.listings += 1 + if comments_fail: + raise RuntimeError("gh api failed: HTTP 502") return json.dumps([list(comments)]) if args[:2] == ["api", "graphql"]: return graphql_reviews(list(reviews)) @@ -2558,7 +2602,7 @@ def fake_threads(repo, pr, sink=None): commit_sha=SHA, judge_status=judge, delivered=delivered, ungated="0", approver_login=APPROVER, reviewed_diff=dpath, base_ref="main", poster_login=POSTER, defer_approval=defer, - author_enabled=author_enabled, pr_author="someone", + author_enabled=author_enabled, pr_author=pr_author, card=card, round="2", max_rounds="5", review_label=review_label, run_url="https://github.com/o/r/actions/runs/1") with mock.patch.dict(os.environ, {"GITHUB_OUTPUT": out}), \ @@ -2566,13 +2610,24 @@ def fake_threads(repo, pr, sink=None): mock.patch.object(AA, "open_thread_severities", fake_threads), \ mock.patch.object(AA, "resolve_eligible_threads", lambda *a: None), \ mock.patch.object(AA, "emit", lambda *a: None), \ - mock.patch("builtins.print", lambda *a, **k: None): + mock.patch("builtins.print", lambda *a, **k: self.printed.append(" ".join(map(str, a)))): + self.printed = [] rc = AA.cmd_decide(args) return rc, read_outputs(out).get("approve_gate") def card_body(self): - self.assertEqual(len(self.card_writes), 1, self.card_writes) - return self.card_writes[0][2] + """The card this round ended on. Every write lands on ONE comment: only + the first may create it (the pending card a first round links its + review to, BE-20246), and nothing re-creates it — this fake's listing + never shows the comment it created, so a writer that re-listed would + POST a second card here.""" + self.assertIn(len(self.card_writes), (1, 2), self.card_writes) + self.assertNotIn("POST", [m for m, _, _ in self.card_writes[1:]], self.card_writes) + if len(self.card_writes) == 2: + self.assertEqual(self.card_writes[1][:2], ("PATCH", "repos/o/r/issues/comments/7")) + self.assertIn("\n", + self.card_writes[0][2]) + return self.card_writes[-1][2] def assert_markers(self, body, state, nxt): lines = body.splitlines() @@ -2781,6 +2836,81 @@ def test_the_next_step_names_the_callers_review_label(self): self.assertIn("remove and re-add the review label", self.card_body()) self.assertNotIn("\n", + self.assertIn("\n", self.card_writes[0][2]) return self.card_writes[-1][2] diff --git a/docs/callers/cursor-approve.md b/docs/callers/cursor-approve.md index c6e47c65..1e2bb784 100644 --- a/docs/callers/cursor-approve.md +++ b/docs/callers/cursor-approve.md @@ -113,7 +113,7 @@ Read them from the start of the comment body. A released value is not renamed (t | State | Written by | Card says | `next` | Next-step line | |---|---|---|---|---| -| `pass` | cursor-review decide, then this workflow's start phase | Approved (or passed the severity gate, axes pending), then the axes table | `none` | none | +| `pass` | cursor-review decide, then this workflow's start phase | Approved (or passed the severity gate, axes pending), then the axes table; or, for a few seconds on a first round, "Deciding this round" (the pending card, below) | `none` | none | | `changes_requested` | cursor-review decide | "Not approved: N finding(s) above `` gate this round", one line per gating finding (severity, `file:line`, thread link, why it gated: inside this round's changes, a live re-raise, High/Critical anywhere, or `full` scope), or the earlier open threads that held it | `resolve_then_relabel` | Fix or reply to the gating threads, resolve them, then start a new round: remove and re-add the `cursor-review` label. | | `no_decision` | cursor-review decide | "No decision this round", with `decide_gate`'s reasons verbatim (e.g. "2/6 panel reviewers did not complete") | `relabel` when the head moved (the one transient cause a relabel alone clears); `push_then_relabel` for a transient cause that left the head where it was (a reviewer or the judge errored, the PR could not be read, the base alone was retargeted); `human` for a structural one (the findings did not land as threads, the reviewed diff is empty) | Re-run the round: remove and re-add the `cursor-review` label. / Nothing — the round is being re-run automatically (see below). / Push a commit so the head moves, then re-run the round. / A human is needed: . | | `capped` | cursor-review decide, or this workflow's start phase | Needs a human (`needs-human-review`, or the round limit) | `human` | A human is needed: the bot withdraws its own request for changes on this hand-off, so a human's review decides this PR — if one still shows, its withdrawal failed (see the run log) and it needs dismissing by hand. Removing the `needs-human-review` label resets the round count. | @@ -121,11 +121,14 @@ Read them from the start of the comment body. A released value is not renamed (t **The pending card (BE-20246).** Every auto-approve review links the card, so the card has to exist before the review does. On a PR's first round, cursor-review's decide therefore creates it a few seconds early, just before -posting its review. That card reads "Deciding this round" with -`no_decision` / `none`, and the round's card replaces it in the same step. -`none` means wait. A remedy there would have an agent push or relabel during a -round that may be approving. If the pending card stays, the card write failed: -the latest auto-approve review is the decision, and the workflow run says why. +posting its review. That card reads "Deciding this round" and carries +`pass` / `none`, the same pair the start phase writes while the axes are +pending: a round in progress, nothing to do. The round's card replaces it in +the same step. Any other pair would ask something of an agent that reads the +card in that window: `no_decision` / `none` reads as "hand to a human", and a +remedy would have it push or relabel during a round that may be approving. If +the pending card stays, the card write failed: the latest auto-approve review +is the decision, and the workflow run says why. The decide phase maps its outcome onto the same contract: `approved` → `pass`/`none`; `not_approved` (an axis was red, missing, had no headline, too diff --git a/docs/callers/cursor-review.md b/docs/callers/cursor-review.md index 60601889..98093ed1 100644 --- a/docs/callers/cursor-review.md +++ b/docs/callers/cursor-review.md @@ -867,8 +867,9 @@ reach the live state in one click. The link has to be in the review when it is posted. Editing a review later would break it: an edited approval is treated as stale, and the bot never withdraws an edited request for changes. So on a PR's first round, before any card exists, `decide` creates one just before the -review. That card is pending (`no_decision` / `none`, "Deciding this round"), -and the round's card replaces it in the same step (see +review. That card is pending ("Deciding this round", with the start phase's +`pass` / `none` pair: a round in progress, nothing to do), and the round's +card replaces it in the same step (see [cursor-approve.md](cursor-approve.md#the-status-card-contract)). **Tolerating an errored reviewer.** By default a single panel cell that errors From 7e285f21870be475b7693115c457033c9caff087 Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Mon, 12 Oct 2026 04:32:12 +0000 Subject: [PATCH 3/3] fix(cursor-review): never strand the pending card a first round creates (BE-20246) Review round on #412: - decide renders the review body before it looks up or creates the card, so a body that fails to render creates no pending card. - A review GitHub refuses as the approver's own PR deletes the pending card it created. An empty or stale --pr-author gets past the up-front check. - A review that could not be withdrawn after the head/base moved, or after a hand-off, now writes a no_decision/human card. The pending card would otherwise keep pointing readers at that review as the decision. - card.ensure looks again after a create that failed ambiguously, so the caller edits a card that did land instead of posting a second one. Co-Authored-By: Claude Opus 5.5 --- .github/cursor-approve/card.py | 13 +++++-- .github/cursor-approve/tests/test_card.py | 27 ++++++++++++++ .github/cursor-review/auto-approve.py | 36 +++++++++++++++++-- .../cursor-review/tests/test_auto_approve.py | 32 ++++++++++++++++- docs/callers/cursor-approve.md | 9 +++-- 5 files changed, 108 insertions(+), 9 deletions(-) diff --git a/.github/cursor-approve/card.py b/.github/cursor-approve/card.py index 61730966..4eb268aa 100644 --- a/.github/cursor-approve/card.py +++ b/.github/cursor-approve/card.py @@ -769,8 +769,17 @@ def ensure(repo: str, pr_number, login: str, body: str) -> tuple: card = find_card(list_comments(repo, pr_number), login) if card is not None: return card, False - return _comment(gh(["api", "-X", "POST", f"repos/{repo}/issues/{pr_number}/comments", "--input", "-"], - {"body": body})), True + try: + return _comment(gh(["api", "-X", "POST", f"repos/{repo}/issues/{pr_number}/comments", "--input", "-"], + {"body": body})), True + except (RuntimeError, ValueError): + # A POST that failed ambiguously (a timeout, a reply without an id) + # may have written the card anyway. Look once more, so the caller edits + # that card by id rather than posting a second one. + card = find_card(list_comments(repo, pr_number), login) + if card is None: + raise + return card, True def upsert(repo: str, pr_number, login: str, body: str, entry=None, keep=None, card=None) -> dict: diff --git a/.github/cursor-approve/tests/test_card.py b/.github/cursor-approve/tests/test_card.py index c49873a2..3fe1eac0 100644 --- a/.github/cursor-approve/tests/test_card.py +++ b/.github/cursor-approve/tests/test_card.py @@ -461,6 +461,33 @@ def test_a_reply_without_a_comment_id_is_an_error(self): with self.subTest(reply=reply), self.assertRaises(ValueError): self.run_ensure([], reply=reply) + def test_an_ambiguous_create_that_landed_is_found_and_returned_as_created(self): + # A timeout or a reply without an id may still have written the card: + # the caller must edit that one by id, not post a second. + landed = {"id": 9, "user": {"login": LOGIN}, "body": card.CARD_MARKER + "\nPENDING", "html_url": self.URL} + for failure in (RuntimeError("gh api failed: timed out"), "{}"): + with self.subTest(failure=failure): + listings = iter([[], [landed]]) + + def fake_gh(args, payload=None, failure=failure): + if "--paginate" in args: + return json.dumps([next(listings)]) + if isinstance(failure, Exception): + raise failure + return failure + + with mock.patch.object(card, "gh", fake_gh): + self.assertEqual(card.ensure("o/r", 1, LOGIN, "PENDING"), (landed, True)) + + def test_a_failed_create_with_no_card_after_it_still_raises(self): + def fake_gh(args, payload=None): + if "--paginate" in args: + return json.dumps([[]]) + raise RuntimeError("gh api failed: HTTP 502") + + with mock.patch.object(card, "gh", fake_gh), self.assertRaises(RuntimeError): + card.ensure("o/r", 1, LOGIN, "PENDING") + def test_the_ensure_phase_prints_the_url_it_found_or_created(self): for comments, want in (([], self.URL), ([{"id": 7, "user": {"login": LOGIN}, "body": card.CARD_MARKER, diff --git a/.github/cursor-review/auto-approve.py b/.github/cursor-review/auto-approve.py index e454b6c3..fe46257b 100644 --- a/.github/cursor-review/auto-approve.py +++ b/.github/cursor-review/auto-approve.py @@ -1576,8 +1576,8 @@ def _ensure_card_url(args) -> str: if not login or (getattr(args, "card", "") or "").strip().lower() != "true": return "" # GitHub refuses the approver a review of its own PR, so there is nothing - # to link, and cmd_decide returns on that refusal without writing the card - # that would replace a pending one. + # to link. cmd_decide deletes a pending card on that refusal (an empty or + # stale --pr-author gets past this check); this check saves the write. if (getattr(args, "pr_author", "") or "").strip().lower() == login.lower(): return "" try: @@ -1596,6 +1596,23 @@ def _ensure_card_url(args) -> str: return "" +def discard_pending_card(args) -> None: + """Delete the pending card ``review_card_url`` created this run, when + GitHub then refused the review outright (the approver's own PR, which a + stale or missing ``--pr-author`` lets through to the POST). No card of this + round replaces it on that path, so it would say "Deciding this round" for + good. Never fatal.""" + created = getattr(args, "created_card", None) + if not created: + return + try: + gh(["api", "-X", "DELETE", f"repos/{args.repo}/issues/comments/{created['id']}"]) + args.created_card = None + except RuntimeError as e: + print(f"::warning::Could not delete the pending cursor-approve status card: " + f"{annotation_cause(e, 'unknown error')}") + + def render_body(event: str, reasons: list, threshold: str, blocking: list, reviewed_sha: str = "", base_ref: str = "", card_url: str = "") -> str: lines = [APPROVE_MARKER] @@ -1996,7 +2013,13 @@ def cmd_decide(args) -> int: why, next_text, threshold) return rc - body = render_body(event, reasons, threshold, blocking, args.commit_sha, args.base_ref, review_card_url(args)) + # Rendered once before the card is looked up: a body that cannot be + # rendered posts no review, and a first round's pending card created for + # it would never be replaced by this round's. + body = render_body(event, reasons, threshold, blocking, args.commit_sha, args.base_ref) + card_url = review_card_url(args) + if card_url: + body = render_body(event, reasons, threshold, blocking, args.commit_sha, args.base_ref, card_url) if event == APPROVE and not REVIEWED_SHA_RE.search(body): # The recorded SHA is the only staleness anchor an approval has (see # REVIEWED_SHA_RE), and render_body drops the marker for anything that is @@ -2027,6 +2050,7 @@ def cmd_decide(args) -> int: # who authored the PR, not a broken review — report it, don't go red. if "own pull request" in str(e).lower(): emit(f"ℹ️ **Auto-approve: skipped** — the approver authored this PR ({e}).") + discard_pending_card(args) return 0 # No review landed, so this round's verdict stands behind nothing: the # gate must not read pass/fail, and an earlier approval must not keep @@ -2071,6 +2095,12 @@ def cmd_decide(args) -> int: dismiss(args.repo, args.pr_number, posted["id"], HUMAN_REVIEW_MESSAGE if labelled_now else STALE_MESSAGE) except RuntimeError as e: print(f"::error::{why[0].upper()}{why[1:]} while the {event} review was posted, and withdrawing it failed: {annotation_cause(e, 'unknown error')}. {DISMISS_PERMISSION_HINT}") + # Not the pending card's "the latest review is the decision": that + # review is the one this run could not withdraw. + write_round_card(args, CARD_NO_DECISION, CARD_NEXT_HUMAN, NO_DECISION_HEADLINE, + [f"{why} while the {event} review was being posted, and withdrawing it failed"], + f"A human is needed: dismiss this round's {event} review. {DISMISS_PERMISSION_HINT}", + threshold) return 1 emit(f"ℹ️ **Auto-approve: withdrawn** — {why} while the {event} review was being posted.") if not labelled_now: diff --git a/.github/cursor-review/tests/test_auto_approve.py b/.github/cursor-review/tests/test_auto_approve.py index b9225ed4..06550882 100644 --- a/.github/cursor-review/tests/test_auto_approve.py +++ b/.github/cursor-review/tests/test_auto_approve.py @@ -2550,7 +2550,8 @@ class StatusCardAndStandingBlockTest(unittest.TestCase): def run_decide(self, findings=(), panel=PANEL_OK, judge="ok", delivered="true", diff=DIFF, reviews=(), comments=(), threads=(), labels=(), defer="", author_enabled="", head=SHA, card="true", review_label="", refuse_pin=False, head_seq=None, - fail_first_post=False, comments_fail=False, post_error="", pr_author="someone"): + fail_first_post=False, comments_fail=False, post_error="", pr_author="someone", + dismiss_error=""): self.reviews_posted, self.dismissed, self.card_writes = [], [], [] # Every write, in order, and every comment listing (BE-20246). self.writes, self.listings = [], 0 @@ -2570,8 +2571,12 @@ def fake_gh(args, payload=None): self.reviews_posted.append(payload) return json.dumps({"id": 99}) if args[:3] == ["api", "-X", "PUT"]: + if dismiss_error: + raise RuntimeError(dismiss_error) self.dismissed.append((int(args[3].split("/")[-2]), payload["message"])) return "{}" + if args[:3] == ["api", "-X", "DELETE"]: + return "" if args[:3] == ["api", "-X", "PATCH"] or (args[:3] == ["api", "-X", "POST"] and "/comments" in args[3]): self.card_writes.append((args[2], args[3], payload["body"])) return json.dumps({"id": 7, "html_url": "https://github.com/o/r/pull/1#issuecomment-7"}) @@ -2903,6 +2908,31 @@ def test_the_approvers_own_pr_gets_no_pending_card(self): self.assertEqual(rc, 0) self.assertEqual((self.listings, self.card_writes), (0, [])) + def test_a_refused_review_on_the_approvers_own_pr_deletes_the_pending_card(self): + # An empty (or stale) --pr-author lets the own-PR check pass, so the + # pending card is created; nothing of this round would replace it. + rc, _ = self.run_decide(pr_author="", + post_error="gh api failed: HTTP 422: Can not approve your own pull request") + self.assertEqual(rc, 0) + self.assertEqual(self.writes, [("POST", "repos/o/r/issues/1/comments"), + ("POST", "repos/o/r/pulls/1/reviews"), + ("DELETE", "repos/o/r/issues/comments/7")]) + + def test_a_body_that_cannot_be_rendered_creates_no_pending_card(self): + with mock.patch.object(AA, "_load_post_review", side_effect=RuntimeError("post-review.py failed to load")), \ + self.assertRaises(RuntimeError): + self.run_decide(findings=[finding("high")]) + self.assertEqual((self.card_writes, self.reviews_posted), ([], [])) + + def test_a_review_that_cannot_be_withdrawn_replaces_the_pending_card(self): + # The pending card says the latest review is the decision; that review + # is the one the run failed to withdraw. + rc, _ = self.run_decide(head_seq=iter([SHA, "b" * 40]), dismiss_error="gh api failed: HTTP 403") + self.assertEqual(rc, 1) + body = self.card_body() + self.assert_markers(body, "no_decision", "human") + self.assertIn("withdrawing it failed", body) + def test_rounds_that_post_no_review_create_no_pending_card(self): for kwargs in ({"defer": "true"}, {"labels": ["needs-human-review"]}): with self.subTest(case=str(kwargs)): diff --git a/docs/callers/cursor-approve.md b/docs/callers/cursor-approve.md index 1e2bb784..9aa4f295 100644 --- a/docs/callers/cursor-approve.md +++ b/docs/callers/cursor-approve.md @@ -126,9 +126,12 @@ posting its review. That card reads "Deciding this round" and carries pending: a round in progress, nothing to do. The round's card replaces it in the same step. Any other pair would ask something of an agent that reads the card in that window: `no_decision` / `none` reads as "hand to a human", and a -remedy would have it push or relabel during a round that may be approving. If -the pending card stays, the card write failed: the latest auto-approve review -is the decision, and the workflow run says why. +remedy would have it push or relabel during a round that may be approving. +decide creates it only once the review's body has rendered, deletes it if +GitHub then refuses the review as the approver's own PR, and replaces it with +a `no_decision` / `human` card when it cannot withdraw a review it must +withdraw. If the pending card stays, the card write failed: the latest +auto-approve review is the decision, and the workflow run says why. The decide phase maps its outcome onto the same contract: `approved` → `pass`/`none`; `not_approved` (an axis was red, missing, had no headline, too