From 9fda5aab18abf116151b2692b9f37019cda2ef1e Mon Sep 17 00:00:00 2001 From: elforeign <324243036+elforeign@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:56:00 -0600 Subject: [PATCH] agent queue: re-run the check only after an accepted verification The verify job re-ran the pull request check after any /verified comment, including refused ones: the author's own, a non-verifier's, a stale or unknown head or a malformed SHA. decide now marks only an accepted verification of the current head, and the job re-runs the check only when should-rerun confirms it. A test checks that what re-runs the check is exactly what the check counts. Replay decides a comment listed twice only once. Tests cover duplicate and out-of-order listings, and every arrival order of a five-claim burst grants exactly one of two overlapping claims. Fixes #210, fixes #211 Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --- .github/workflows/agent-pr-checks.yml | 3 ++ docs/coordination/agent-tasks.md | 7 ++-- scripts/agent_queue/cli.py | 4 +++ scripts/agent_queue/decide.py | 8 ++++- scripts/agent_queue/replay.py | 1 + tests/agent_queue/test_prcheck_summary.py | 40 ++++++++++++++++++++++- tests/agent_queue/test_replay.py | 24 ++++++++++++++ tests/agent_queue/test_workflows_cli.py | 14 ++++++++ 8 files changed, 96 insertions(+), 5 deletions(-) diff --git a/.github/workflows/agent-pr-checks.yml b/.github/workflows/agent-pr-checks.yml index d5e6d95..76d2809 100644 --- a/.github/workflows/agent-pr-checks.yml +++ b/.github/workflows/agent-pr-checks.yml @@ -83,6 +83,9 @@ jobs: python3 -m agent_queue gather --repo "$GH_REPO" --pr "$number" > "$RUNNER_TEMP/state.json" python3 -m agent_queue decide --event "$RUNNER_TEMP/event.json" --state "$RUNNER_TEMP/state.json" > "$RUNNER_TEMP/actions.json" python3 -m agent_queue apply --repo "$GH_REPO" --actions "$RUNNER_TEMP/actions.json" + # Re-run only after an accepted verification of the current head: refused, stale, + # malformed or unauthorized /verified comments get a reply and nothing else. + if ! python3 -m agent_queue should-rerun --actions "$RUNNER_TEMP/actions.json"; then exit 0; fi sha=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["pr"]["head_sha"])' "$RUNNER_TEMP/state.json") run=$(gh run list --workflow agent-pr-checks.yml --commit "$sha" --event pull_request_target --limit 1 --json databaseId -q '.[0].databaseId // empty') if [ -n "$run" ]; then gh run rerun "$run" || true; fi diff --git a/docs/coordination/agent-tasks.md b/docs/coordination/agent-tasks.md index 89d1e5d..ad2e779 100644 --- a/docs/coordination/agent-tasks.md +++ b/docs/coordination/agent-tasks.md @@ -51,7 +51,7 @@ done Claims are decided one at a time across the whole repository, so two overlapping claims cannot both win. GitHub keeps at most one waiting run in a concurrency group and cancels it when a newer one queues, so each run decides every command of the last `lease_hours` that has no bot reply yet, - oldest first. A command whose own run was cancelled is answered by the run that replaced it, or at + oldest first; a comment listed twice is decided once. A command whose own run was cancelled is answered by the run that replaced it, or at the latest by the hourly audit. Each reply carries a marker naming the comment it answers. 4. `scripts/agent-queue.sh start ` creates branch `task/-` from `upstream/main` in a separate worktree `../xenora-task-` (`--here` uses the current clean checkout instead). @@ -89,8 +89,9 @@ machine-readable summary (counts and lists per area, priority and state). `agent-pr-checks` fails a pull request when: it has no `Closes #N` for a claimed task; the task is held by someone else; it changes files outside the task's patterns; it changes a file that another open pull request also changes; the claim has lapsed without a push; or no eligible `/verified ` exists. After a `/verified` comment the check is re-run automatically; other pull request -comments do not re-run it. +head sha>` exists. After an accepted `/verified` (an eligible verifier, the current head) the check is re-run +automatically. Refused, stale, malformed or unauthorized `/verified` comments get a reply and +nothing else, and other pull request comments start no run. The check is advisory for now: a failure is shown as a warning and in the run summary, and the job still succeeds. It becomes blocking (fail on problems, then a required check) only after a live smoke diff --git a/scripts/agent_queue/cli.py b/scripts/agent_queue/cli.py index 023e4e7..4dc3f5c 100644 --- a/scripts/agent_queue/cli.py +++ b/scripts/agent_queue/cli.py @@ -38,6 +38,7 @@ def main(argv): s.add_argument("--recent-commands", action="store_true", help="include comments from the last lease_hours for replay") s = sub.add_parser("replay"); s.add_argument("--state", required=True) s = sub.add_parser("decide"); s.add_argument("--event", required=True); s.add_argument("--state", required=True) + s = sub.add_parser("should-rerun"); s.add_argument("--actions", required=True) s = sub.add_parser("apply"); s.add_argument("--repo", required=True); s.add_argument("--actions", required=True) s = sub.add_parser("audit"); s.add_argument("--state", required=True) s = sub.add_parser("summary"); s.add_argument("--state", required=True); s.add_argument("--text", action="store_true") @@ -79,6 +80,9 @@ def main(argv): _out(decide(_load(a.event), _load(a.state), cfg)) elif a.cmd == "replay": _out(replay.replay(_load(a.state), cfg, _now(a.now))) + elif a.cmd == "should-rerun": + # exit 0 only for an accepted /verified of the current head + return 0 if decide_mod.rerun_check(_load(a.actions)) else 1 elif a.cmd == "apply": ghio.apply_actions(a.repo, _load(a.actions)) elif a.cmd == "audit": diff --git a/scripts/agent_queue/decide.py b/scripts/agent_queue/decide.py index 5ee8d19..68c46e3 100644 --- a/scripts/agent_queue/decide.py +++ b/scripts/agent_queue/decide.py @@ -150,4 +150,10 @@ def _verified(event, state, cfg, arg): return _reply(n, "%s may not verify: only maintainers and qa contributors can." % actor) if len(sha) < 7 or not head.startswith(sha): return _reply(n, "Verification refused: `%s` is not the current head %s." % (sha or "(none)", head[:12])) - return _reply(n, "Verification of %s recorded for %s." % (head[:12], actor)) + # Only an accepted verification of the current head re-runs the pull request check. + return [dict(_act(n, comment="Verification of %s recorded for %s." % (head[:12], actor)), rerun_check=True)] + + +def rerun_check(actions): + """True when the decided actions record an accepted verification of the current head.""" + return any(a.get("rerun_check") for a in actions) diff --git a/scripts/agent_queue/replay.py b/scripts/agent_queue/replay.py index 6cb785f..a93e78e 100644 --- a/scripts/agent_queue/replay.py +++ b/scripts/agent_queue/replay.py @@ -45,6 +45,7 @@ def replay(state, cfg, now, bots=L.BOT): if (c.get("pull_request") or c["id"] in done or c["user"] in bots or parse_time(c["created_at"]) < since): continue + done.add(c["id"]) # a comment listed twice is decided once actions = decide({"kind": "issue", "number": c["issue"], "actor": c["user"], "body": c["body"]}, state, cfg) if not actions: continue diff --git a/tests/agent_queue/test_prcheck_summary.py b/tests/agent_queue/test_prcheck_summary.py index 56f2796..7279b14 100644 --- a/tests/agent_queue/test_prcheck_summary.py +++ b/tests/agent_queue/test_prcheck_summary.py @@ -4,7 +4,7 @@ import unittest from helpers import CFG, NOW, issue, state from agent_queue import lease as L -from agent_queue.decide import decide +from agent_queue.decide import decide, rerun_check from agent_queue.prcheck import check_pr from agent_queue.queue import summary @@ -104,6 +104,44 @@ class VerifiedCommand(unittest.TestCase): self.assertIn("not the current head", self.go("qaer", sha="1234567")) +class VerifiedRerun(unittest.TestCase): + """Only an accepted /verified of the current head may re-run the pull request check (#210).""" + + def rerun(self, actor, text, author="gpuer"): + st = state(issue(1, labels=HELD)) + st["pr"] = pr(author=author) + return rerun_check(decide({"kind": "pr", "number": 10, "actor": actor, "body": text}, st, CFG)) + + def test_accepted_current_head_reruns(self): + self.assertTrue(self.rerun("qaer", "/verified " + SHA[:12])) + self.assertTrue(self.rerun("maint", "/verified " + SHA + "\nran the acceptance command")) + + def test_ordinary_comments_do_not_rerun(self): + for text in ("looks good", "LGTM /verified " + SHA, "", "thanks\n/verified " + SHA[:12]): + self.assertFalse(self.rerun("qaer", text), text) + + def test_malformed_do_not_rerun(self): + for text in ("/verified", "/verified abc", "/verifiedaaaaaaa", "/verified " + SHA[:6], "/Verified " + SHA[:12]): + self.assertFalse(self.rerun("qaer", text), text) + + def test_stale_and_unknown_heads_do_not_rerun(self): + self.assertFalse(self.rerun("qaer", "/verified " + "b" * 12)) + self.assertFalse(self.rerun("qaer", "/verified " + SHA[:11] + "b")) + + def test_unauthorized_do_not_rerun(self): + self.assertFalse(self.rerun("gpuer", "/verified " + SHA[:12])) # author + self.assertFalse(self.rerun("gpuer", "/verified " + SHA[:12], author="maint")) # task holder + self.assertFalse(self.rerun("rt", "/verified " + SHA[:12])) # not a verifier + self.assertFalse(self.rerun("stranger", "/verified " + SHA[:12])) + + def test_rerun_agrees_with_the_check(self): + # Whatever re-runs the check must also be counted by it, and the other way round. + for actor in ("qaer", "maint", "gpuer", "rt"): + for sha in (SHA[:12], SHA[:6], "b" * 12): + counted = actor in run(pr(), [vc(actor, sha)])["verified_by"] + self.assertEqual(self.rerun(actor, "/verified " + sha), counted, (actor, sha)) + + class Summary(unittest.TestCase): def test_shape_and_json(self): st = state(issue(1), issue(2, labels=HELD, area="cpu", prio="low"), issue(3, labels=("bug",))) diff --git a/tests/agent_queue/test_replay.py b/tests/agent_queue/test_replay.py index e091976..12667b3 100644 --- a/tests/agent_queue/test_replay.py +++ b/tests/agent_queue/test_replay.py @@ -93,6 +93,30 @@ class Replay(unittest.TestCase): self.assertIn("file overlap with claimed task #1", actions[1]["comment"]) self.assertIn(R.done_marker(8), actions[1]["comment"]) + def test_out_of_order_listing_is_decided_in_comment_order(self): + # Listed newest first: the earlier claim on the shared file still wins. + actions = self.run_replay([cmd(9, "maint", "/claim", 2, "2026-01-10T11:00:09Z"), + cmd(5, "gpuer", "/claim", 1, "2026-01-10T11:00:01Z")]) + self.assertEqual(granted(actions), [(1, "gpuer")]) + self.assertEqual([a["issue"] for a in actions], [1, 2]) + + def test_duplicate_events_are_decided_once(self): + c = cmd(5, "gpuer", "/claim", 1) + actions = self.run_replay([c, dict(c), cmd(6, "maint", "/claim", 2, "2026-01-10T11:00:02Z")]) + self.assertEqual(granted(actions), [(1, "gpuer")]) + self.assertEqual(sum(R.done_marker(5) in a["comment"] for a in actions), 1) + + def test_no_burst_order_grants_overlapping_claims(self): + # Every arrival order of a burst of claims on #1 and #2 (shared file) grants exactly one of them. + import itertools + burst = [cmd(1, "gpuer", "/claim", 1), cmd(2, "maint", "/claim", 2), cmd(3, "maint", "/claim", 1), + cmd(4, "gpuer", "/claim", 2), cmd(5, "maint", "/claim", 3)] + for order in itertools.permutations(range(5)): + comments = [dict(burst[i], created_at="2026-01-10T11:00:%02dZ" % k) for k, i in enumerate(order)] + g = granted(self.run_replay(comments)) + self.assertEqual(len([x for x in g if x[0] in (1, 2)]), 1, (order, g)) + self.assertIn((3, "maint"), g) + def test_answered_commands_are_not_decided_again(self): first = self.run_replay([cmd(1, "gpuer", "/claim", 1)]) st = queue_state([cmd(1, "gpuer", "/claim", 1), diff --git a/tests/agent_queue/test_workflows_cli.py b/tests/agent_queue/test_workflows_cli.py index 2e5ff33..8f9e87c 100644 --- a/tests/agent_queue/test_workflows_cli.py +++ b/tests/agent_queue/test_workflows_cli.py @@ -87,6 +87,20 @@ class Workflows(unittest.TestCase): self.assertIn("--recent-commands", text, job) self.assertNotIn("agent_queue decide", self.job("agent-claims.yml", "command")) + def test_rerun_needs_an_accepted_verification(self): + text = self.job("agent-pr-checks.yml", "verify") + gate = text.index("agent_queue should-rerun") + self.assertLess(gate, text.index("gh run rerun")) + self.assertIn('if ! python3 -m agent_queue should-rerun --actions "$RUNNER_TEMP/actions.json"; then exit 0; fi', text) + + def test_should_rerun_exit_codes(self): + with tempfile.TemporaryDirectory() as d: + p = os.path.join(d, "a.json") + for actions, code in (([], 1), ([{"issue": 4, "add_labels": [], "remove_labels": [], "comment": "refused"}], 1), + ([{"issue": 4, "add_labels": [], "remove_labels": [], "comment": "ok", "rerun_check": True}], 0)): + dump(actions, p) + self.assertEqual(Cli.run_cli(self, "should-rerun", "--actions", p).returncode, code, actions) + def test_verify_runs_one_at_a_time_per_pull_request(self): text = self.job("agent-pr-checks.yml", "verify") self.assertRegex(text, r"group: agent-verify-\$\{\{ github\.event\.issue\.number \}\}\n\s+cancel-in-progress: false")