From 83bae393230a8413d7e22994eb67f5bc05b22fd2 Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:06:44 +0000 Subject: [PATCH] fix(controller): skip draft pull requests when labelling The cron labels every open PR, so a draft that nobody asked to review yet picked up `agent-pr` and `risk:T*` and sat in the review queue. `read_prs` already receives the pull list, which carries `draft`; it now records the field, and `plan` skips a draft with reason `draft` in the existing `skipped` shape, so the plan JSON gains no key and `apply` is unchanged. A skip is no add and no remove, so a draft keeps the labels it has; `hold` is still checked first. `draft: false` PRs are labelled exactly as before. AGENTS.md said the controller labels "open PRs"; it now says "open non-draft PRs", the one clause this change makes stale. Refs #1192 --- .github/scripts/controller.py | 9 +- AGENTS.md | 7 +- changelog.d/1192-draft-skip.md | 5 ++ tests/test_process_controller_draft.py | 116 +++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 changelog.d/1192-draft-skip.md create mode 100644 tests/test_process_controller_draft.py diff --git a/.github/scripts/controller.py b/.github/scripts/controller.py index e2e7b5a9..8a9df07c 100644 --- a/.github/scripts/controller.py +++ b/.github/scripts/controller.py @@ -10,7 +10,8 @@ risk gate's rules. - sets ``agent-pr`` on pull requests an agent opened, and ``risk:T0..T3`` - (the risk gate's tier) on every open PR not on ``hold`` + (the risk gate's tier) on every open PR that is neither on ``hold`` nor a + draft: a parked draft is not in the review queue yet (issue #1192) - removes ``agent-ready`` (and ``agent-working``) when someone who is not a maintainer labeled the issue, or edited its text after it was labeled - removes ``agent-working`` from a claim older than the TTL with no recent @@ -196,6 +197,11 @@ def push(kind: str, number: int, found: List[Dict[str, str]], reason: str) -> No if any(_same(l, "hold") for l in pr["labels"]): skipped.append({"number": pr["number"], "label": "*", "reason": "hold"}) continue + if pr.get("draft"): + # A draft is parked, not up for review: no add and no remove, so the + # labels it already carries stay (#1192). `hold` above still wins. + skipped.append({"number": pr["number"], "label": "*", "reason": "draft"}) + continue if not pr["tier"]: continue # the files could not be read; leave the labels as they are want = f"risk:{pr['tier']}" @@ -358,6 +364,7 @@ def read_prs(api: Any, repo: str, config: Dict[str, Any], gate: "risk_gate.Gate" out.append({ "number": pr["number"], "labels": [l["name"] for l in pr.get("labels", [])], + "draft": bool(pr.get("draft")), # already in this pull list; no extra request (#1192) "author": _login(pr.get("user")) or "", "headRef": pr["head"]["ref"], "sameRepo": head_repo.lower() == repo.lower(), diff --git a/AGENTS.md b/AGENTS.md index 06ee6c54..88d2fbc3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -105,9 +105,10 @@ question in a comment. Agents develop **only** `agent-ready` issues. [`.github/workflows/controller.yml`](.github/workflows/controller.yml) runs every 30 minutes with the built-in `GITHUB_TOKEN` only (no GitHub App, no secrets) and -changes labels and nothing else: it sets `risk:T*` and `agent-pr` on open PRs, -removes `agent-ready`/`agent-working` when a non-maintainer set or edited an -issue after it was approved, and expires a claim after 6 h with no linked push. +changes labels and nothing else: it sets `risk:T*` and `agent-pr` on open +non-draft PRs, removes `agent-ready`/`agent-working` when a non-maintainer set +or edited an issue after it was approved, and expires a claim after 6 h with no +linked push. It never adds `agent-ready`, never merges, comments or closes. | Repository variable | Effect | diff --git a/changelog.d/1192-draft-skip.md b/changelog.d/1192-draft-skip.md new file mode 100644 index 00000000..dacd28f2 --- /dev/null +++ b/changelog.d/1192-draft-skip.md @@ -0,0 +1,5 @@ +Fixed +The label controller skips a draft pull request instead of labelling it, so a +draft parked before it is ready no longer picks up `agent-pr` or a `risk:T*` +label and enter the review queue; a pull request marked ready is labelled as +before (#1192). diff --git a/tests/test_process_controller_draft.py b/tests/test_process_controller_draft.py new file mode 100644 index 00000000..ac914bb6 --- /dev/null +++ b/tests/test_process_controller_draft.py @@ -0,0 +1,116 @@ +"""A parked draft is not in the review queue (issue #1192). + +The controller runs on a cron and labels every open pull request, so a draft +that nobody asked to review yet used to get ``agent-pr`` and ``risk:T*`` and +appear in the queue. ``read_prs`` already fetches the pull list, which carries +``draft``; the plan skips such a PR with the ``skipped`` reason ``draft`` — +no add and no remove, so the labels it already has stay. ``hold`` still wins. +""" + +import importlib.util +import json +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +SCRIPTS = ROOT / ".github" / "scripts" +CONFIG = json.loads((ROOT / ".github" / "agent" / "controller.json").read_text()) + + +def _mod(): + sys.path.insert(0, str(SCRIPTS)) + try: + spec = importlib.util.spec_from_file_location("controller", SCRIPTS / "controller.py") + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + finally: + sys.path.remove(str(SCRIPTS)) + + +c = _mod() +REPO = "ThinkFlowLab/agentdescent" +ALL = ["agent-ready", "agent-working", "agent-pr", "hold", "risk:T0", "risk:T1", "risk:T2", "risk:T3"] +NOW = c.parse_time("2026-10-19T12:00:00Z") + + +def pr(number, labels=(), tier="T1", author="HugoXK", head="feat/x", same=True, body="", + push="2026-10-19T11:00:00Z", draft=False): + return {"number": number, "labels": list(labels), "tier": tier, "author": author, "headRef": head, + "sameRepo": same, "body": body, "lastPushAt": push, "draft": draft} + + +def run(prs=(), labels=ALL): + prs = list(prs) + return c.plan({"repo": REPO, "config": CONFIG, "now": NOW, "repoLabels": list(labels), + "issues": [], "prs": prs, "linked": c.linked_prs(prs, REPO)}) + + +class PullApi: + """The two GETs ``read_prs`` makes per PR, and nothing else.""" + + def __init__(self, pulls): + self.pulls, self.calls = pulls, [] + + def get(self, path): + self.calls.append(("GET", path)) + if path.startswith(f"repos/{REPO}/pulls?"): + return self.pulls + if "/commits/" in path: + return {"commit": {"committer": {"date": "2026-10-19T11:00:00Z"}}} + raise AssertionError(path) + + def list(self, path): + self.calls.append(("LIST", path)) + return [{"filename": "docs/x.md", "status": "modified", "additions": 1, "deletions": 0}] + + +class StubGate: + def judge(self, files): + return {"tier": "T1"} + + +def pull(number, draft): + return {"number": number, "draft": draft, "labels": [], "user": {"login": "HugoXK"}, + "head": {"ref": f"feat/{number}", "sha": f"{number:040x}", "repo": {"full_name": REPO}}, + "body": "", "created_at": "2026-10-19T11:00:00Z"} + + +def test_read_prs_records_the_draft_flag_it_already_fetches(): + api = PullApi([pull(41, True), pull(42, False)]) + prs = c.read_prs(api, REPO, CONFIG, StubGate()) + assert [(p["number"], p["draft"]) for p in prs] == [(41, True), (42, False)] + # The pull list already carries the field: a draft costs no extra request. + draft_calls = [x for x in api.calls if x[0] == "GET" and "/pulls/" in x[1] and "draft" in x[1]] + assert draft_calls == [] + assert len([x for x in api.calls if "/pulls/41" in x[1]]) == len([x for x in api.calls if "/pulls/42" in x[1]]) + + +def test_a_draft_pr_is_skipped_and_keeps_the_labels_it_has(): + parked = pr(20, ["agent-pr", "risk:T2"], tier="T3", head="agent/x", draft=True) + out = run(prs=[parked]) + assert out["ops"] == [], "a draft must not be labelled, and must not lose a label either" + assert {"number": 20, "label": "*", "reason": "draft"} in out["skipped"] + + +def test_a_ready_pr_next_to_it_is_labelled_normally(): + parked = pr(20, ["agent-pr", "risk:T2"], tier="T3", head="agent/x", draft=True) + ready = pr(21, tier="T1", head="agent/x", draft=False) + out = run(prs=[parked, ready]) + assert {(o["number"], o["op"], o["label"]) for o in out["ops"]} == { + (21, "add", "risk:T1"), (21, "add", "agent-pr")} + assert not any(s["number"] == 21 for s in out["skipped"]), "the control row stays untouched" + + +def test_hold_is_still_reported_first_on_a_draft(): + out = run(prs=[pr(22, ["hold"], tier="T2", draft=True)]) + assert out["ops"] == [] + assert out["skipped"] == [{"number": 22, "label": "*", "reason": "hold"}] + + +def test_the_draft_reason_needs_no_new_plan_key_and_renders(): + out = run(prs=[pr(30, draft=True, tier="T2")]) + assert set(out) == {"ops", "skipped", "notes"}, "apply/validate_plan see the same plan shape" + assert "draft" not in c.REASONS, "a skipped PR never becomes an operation" + text = c.render(dict(out, version=1), "dry-run") + assert "- #30 `?`: draft" in text