From 0035129ae05f4fc38733eb066c2d338efd2a0dec Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Sat, 21 Nov 2026 13:56:28 +0800 Subject: [PATCH] fix(pr-review): verify the approval closeout comparison Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/capabilities/pr_review_queue/README.md | 3 ++ .../pr_review_queue/approval_closeout.py | 9 ++++- .../test_pr_review_approval_closeout.py | 40 ++++++++++++++++++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/loopx/capabilities/pr_review_queue/README.md b/loopx/capabilities/pr_review_queue/README.md index 65f88f114d..5986d03056 100644 --- a/loopx/capabilities/pr_review_queue/README.md +++ b/loopx/capabilities/pr_review_queue/README.md @@ -901,6 +901,9 @@ The read-only command paginates GitHub review history and uses the latest submit opinion per reviewer; a comment/pending review does not erase a blocker, and a dismissed review does not resurrect older history. Its typed TS read model reports `clear`, `verification_required`, or `hold`; these are not merge decisions. +Closeout checks that the base commit and changed-file count are stable across +the history read when the head is unchanged. If that comparison changes, +retry closeout with the current PR snapshot. Review age or a different commit only identifies a finding to inspect, never proof of resolution. The host independently verifies every old finding and inline comment, checks owner authorization and GitHub/branch dismissal permissions, rechecks the diff --git a/loopx/capabilities/pr_review_queue/approval_closeout.py b/loopx/capabilities/pr_review_queue/approval_closeout.py index db6dbecfb4..ca17b8f797 100644 --- a/loopx/capabilities/pr_review_queue/approval_closeout.py +++ b/loopx/capabilities/pr_review_queue/approval_closeout.py @@ -68,11 +68,14 @@ def read_github_approval_closeout(*, repository: str, exact_head: str) -> dict[s login = resolve_current_github_login() if not login: raise ValueError("approval closeout requires authenticated reviewer identity") - fields = "number,headRefOid,state,reviewDecision,author,files,changedFiles" + fields = "number,headRefOid,baseRefOid,state,reviewDecision,author,files,changedFiles" args = ["pr", "view", str(number), "--repo", repository, "--json", fields] pr = run_gh_json(args) if not isinstance(pr, dict): raise ValueError("pull-request readback is incomplete") + base_oid = pr.get("baseRefOid") + if not isinstance(base_oid, str) or not base_oid.strip(): + raise ValueError("pull-request comparison base is unavailable") expected_files = pr.get("changedFiles") if not isinstance(expected_files, int) or expected_files < 0: raise ValueError("changed-file readback is incomplete") @@ -101,6 +104,10 @@ def read_github_approval_closeout(*, repository: str, exact_head: str) -> dict[s after = run_gh_json(args) if not isinstance(after, dict) or after.get("number") != number or after.get("state") != pr.get("state"): raise ValueError("pull-request identity or lifecycle changed during readback") + if after.get("headRefOid") == pr.get("headRefOid") and ( + after.get("baseRefOid") != base_oid or after.get("changedFiles") != expected_files + ): + raise ValueError("pull-request comparison changed during readback; retry closeout") return plan_approval_closeout({"repository": repository, "expected_exact_head": exact_head, "pull_request": pr, "readback_head": after.get("headRefOid"), "reviews": reviews, "reviews_complete": True, "review_conclusions": conclusions}) diff --git a/tests/capabilities/test_pr_review_approval_closeout.py b/tests/capabilities/test_pr_review_approval_closeout.py index f24682e6dd..b2e9046b26 100644 --- a/tests/capabilities/test_pr_review_approval_closeout.py +++ b/tests/capabilities/test_pr_review_approval_closeout.py @@ -190,7 +190,7 @@ def read(args, **_): assert "--paginate" in args and "--slurp" in args return [[review(1, "CHANGES_REQUESTED")], [own]] assert "statusCheckRollup" not in args[-1] - return {"number": 42, "headRefOid": HEAD, "state": "OPEN", + return {"number": 42, "headRefOid": HEAD, "baseRefOid": OLD, "state": "OPEN", "author": {"login": "contributor"}, "reviewDecision": "CHANGES_REQUESTED", "files": [{"path": "loopx/runtime.py"}], "changedFiles": 1} @@ -206,6 +206,44 @@ def read(args, **_): assert "body" not in json.dumps(result) +@pytest.mark.parametrize("comparison_change", [ + {"baseRefOid": "c" * 40}, + {"changedFiles": 2}, + {"baseRefOid": None}, +]) +def test_live_adapter_rejects_comparison_drift_at_unchanged_head( + monkeypatch, capsys, comparison_change, +): + from loopx.cli import main + import loopx.pr_review as pr_module + from loopx.capabilities.pr_review_queue import github_source + + body = (Path(__file__).parents[2] / "examples/fixtures/pr-review.body.md").read_text() + body = body.replace("HEAD_OID", HEAD).replace("VERDICT", "APPROVE") + approval = review(3, "APPROVED", login="maintainer", head=HEAD) | {"body": body} + snapshot = { + "number": 42, "headRefOid": HEAD, "baseRefOid": OLD, "state": "OPEN", + "author": {"login": "contributor"}, "reviewDecision": "APPROVED", + "files": [{"path": "loopx/runtime.py"}], "changedFiles": 1, + } + readbacks = iter((snapshot, snapshot | comparison_change)) + + def read(args, **_): + if args[0] == "api": + return [[approval]] + fields = args[args.index("--json") + 1].split(",") + current = next(readbacks) + return {key: current[key] for key in fields if key in current} + + monkeypatch.setattr(github_source, "run_gh_json", read) + monkeypatch.setattr(pr_module, "resolve_current_github_login", lambda: "maintainer") + result = main(["--format", "json", "pr-review", "--repo", "owner/repo", + "--check-approval-closeout", f"42@{HEAD}"]) + payload = json.loads(capsys.readouterr().out) + assert result == 1, payload + assert "comparison changed during readback" in json.dumps(payload) + + def test_cli_rejects_mixed_modes_before_github_read(monkeypatch, capsys): from loopx.cli import main from loopx.capabilities.pr_review_queue import github_source