From cd5a608b59e96ddea314b8fb65d2496abece39e6 Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:27:48 +0000 Subject: [PATCH 1/2] ci(controller): label PRs on their lifecycle events The schedule is throttled to 2-3 runs a day here, so a new PR can wait hours for `agent-pr`. Run the controller on `pull_request` opened, ready_for_review and reopened as well; the cron stays as the fallback. A fork head's `pull_request` run gets a read-only token, so both jobs skip it rather than fail on the first label write; fork PRs keep being labelled by the cron. Fixes #1187 --- .github/workflows/controller.yml | 18 ++++++++++++++- changelog.d/1187-controller-pr-trigger.md | 6 +++++ tests/test_process_controller.py | 27 ++++++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 changelog.d/1187-controller-pr-trigger.md diff --git a/.github/workflows/controller.yml b/.github/workflows/controller.yml index 1e0bd275..8481a8ab 100644 --- a/.github/workflows/controller.yml +++ b/.github/workflows/controller.yml @@ -11,7 +11,14 @@ name: controller # else -> dry run, the plan goes to the job summary. # AGENT_PAUSE `true` -> kill switch: nothing is read, proposed or changed. # To turn it off entirely, disable this workflow in the Actions tab. +# +# It also runs on the three PR lifecycle events, so a new PR is labelled now +# rather than at the next cron tick (the schedule is throttled and can be +# hours late). A `pull_request` run from a fork head carries a read-only +# token, so both jobs skip it: the fallback cron still labels fork PRs. on: + pull_request: + types: [opened, ready_for_review, reopened] schedule: - cron: '7,37 * * * *' workflow_dispatch: @@ -27,6 +34,11 @@ jobs: name: plan (read only) runs-on: ubuntu-latest timeout-minutes: 10 + # A read-only token is all this job needs, but a fork head's run must not + # hand `apply` a live plan, so it is skipped like `apply` is. + if: >- + github.event_name != 'pull_request' + || github.event.pull_request.head.repo.full_name == github.repository permissions: contents: read issues: read @@ -50,7 +62,11 @@ jobs: apply: name: apply labels (live only) needs: plan - if: needs.plan.outputs.live == 'true' && vars.CONTROLLER_LIVE == 'true' && vars.AGENT_PAUSE != 'true' + if: >- + needs.plan.outputs.live == 'true' && vars.CONTROLLER_LIVE == 'true' + && vars.AGENT_PAUSE != 'true' + && (github.event_name != 'pull_request' + || github.event.pull_request.head.repo.full_name == github.repository) runs-on: ubuntu-latest timeout-minutes: 10 permissions: diff --git a/changelog.d/1187-controller-pr-trigger.md b/changelog.d/1187-controller-pr-trigger.md new file mode 100644 index 00000000..13e9e29d --- /dev/null +++ b/changelog.d/1187-controller-pr-trigger.md @@ -0,0 +1,6 @@ +Changed +The label-only controller now also runs on the `pull_request` `opened`, +`ready_for_review` and `reopened` events, so a new PR is labelled immediately +instead of waiting for the throttled `schedule` tick; the cron stays as the +fallback, and a `pull_request` run from a fork head is skipped by both jobs +because it cannot hold a write token (#1187). diff --git a/tests/test_process_controller.py b/tests/test_process_controller.py index 1b4384b7..322cb2b7 100644 --- a/tests/test_process_controller.py +++ b/tests/test_process_controller.py @@ -18,7 +18,12 @@ ROOT = Path(__file__).resolve().parents[1] SCRIPTS = ROOT / ".github" / "scripts" WORKFLOW = ROOT / ".github" / "workflows" / "controller.yml" +WORKFLOWS = ROOT / ".github" / "workflows" CONFIG = json.loads((ROOT / ".github" / "agent" / "controller.json").read_text()) +# A `pull_request` run on a fork head gets a read-only GITHUB_TOKEN, so neither +# job may run for it: `plan` would hand `apply` a live plan it cannot carry out. +SAME_REPO_GUARD = ("github.event_name != 'pull_request' " + "|| github.event.pull_request.head.repo.full_name == github.repository") def _mod(): @@ -268,8 +273,10 @@ def test_workflow_shape(): text = WORKFLOW.read_text(encoding="utf-8") wf = yaml.safe_load(text) on = wf.get("on", wf.get(True)) - assert set(on) == {"schedule", "workflow_dispatch"} + assert set(on) == {"schedule", "workflow_dispatch", "pull_request"} assert on["schedule"] == [{"cron": "7,37 * * * *"}], "every 30 minutes" + assert on["pull_request"] == {"types": ["opened", "ready_for_review", "reopened"]}, \ + "label a PR when it becomes reviewable, not on every push to its head" assert wf["permissions"] == {} plan, apply = wf["jobs"]["plan"], wf["jobs"]["apply"] assert plan["permissions"] == {"contents": "read", "issues": "read", "pull-requests": "read"} @@ -282,6 +289,24 @@ def test_workflow_shape(): assert "${{" not in runs, "values reach the shell only through env" +def test_a_fork_head_never_reaches_the_label_jobs(): + wf = yaml.safe_load(WORKFLOW.read_text(encoding="utf-8")) + for name in ("plan", "apply"): + assert SAME_REPO_GUARD in wf["jobs"][name]["if"], ( + f"{name} must skip a pull_request from a fork: that token cannot write" + ) + + +def test_no_workflow_uses_pull_request_target(): + offenders = [] + for path in sorted(WORKFLOWS.glob("*.yml")): + wf = yaml.safe_load(path.read_text(encoding="utf-8")) + on = wf.get("on", wf.get(True)) or {} + if "pull_request_target" in on: + offenders.append(path.name) + assert not offenders, f"pull_request_target runs with a writable token on untrusted code: {offenders}" + + def test_labels_json_has_the_labels_the_controller_manages(): labels = ROOT / ".github" / "labels.json" if not labels.exists(): From b505832634066035cca5117da83c2ba9cdbcdf33 Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Fri, 20 Nov 2026 17:04:35 +0000 Subject: [PATCH 2/2] ci(controller): six-hourly cron backstop, PR events carry immediacy --- .github/workflows/controller.yml | 10 ++++++---- changelog.d/1187-controller-pr-trigger.md | 5 +++-- tests/test_process_controller.py | 5 ++++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.github/workflows/controller.yml b/.github/workflows/controller.yml index 8481a8ab..5238552a 100644 --- a/.github/workflows/controller.yml +++ b/.github/workflows/controller.yml @@ -13,14 +13,16 @@ name: controller # To turn it off entirely, disable this workflow in the Actions tab. # # It also runs on the three PR lifecycle events, so a new PR is labelled now -# rather than at the next cron tick (the schedule is throttled and can be -# hours late). A `pull_request` run from a fork head carries a read-only -# token, so both jobs skip it: the fallback cron still labels fork PRs. +# rather than at the next cron tick. The old half-hourly schedule declared 48 +# slots a day and in practice ran twice (2026-11-19: 3 runs; 2026-11-20: 2), +# so immediacy comes from these events and manual dispatch, and the cron is a +# four-a-day backstop only. A `pull_request` run from a fork head carries a +# read-only token, so both jobs skip it: the fallback cron still labels fork PRs. on: pull_request: types: [opened, ready_for_review, reopened] schedule: - - cron: '7,37 * * * *' + - cron: '17 */6 * * *' workflow_dispatch: concurrency: diff --git a/changelog.d/1187-controller-pr-trigger.md b/changelog.d/1187-controller-pr-trigger.md index 13e9e29d..ea83c15a 100644 --- a/changelog.d/1187-controller-pr-trigger.md +++ b/changelog.d/1187-controller-pr-trigger.md @@ -1,6 +1,7 @@ Changed The label-only controller now also runs on the `pull_request` `opened`, `ready_for_review` and `reopened` events, so a new PR is labelled immediately -instead of waiting for the throttled `schedule` tick; the cron stays as the -fallback, and a `pull_request` run from a fork head is skipped by both jobs +instead of waiting for a `schedule` tick. The cron moves from every 30 minutes +(48 declared slots, in practice two or three runs a day) to `17 */6 * * *`, a +backstop only; a `pull_request` run from a fork head is skipped by both jobs because it cannot hold a write token (#1187). diff --git a/tests/test_process_controller.py b/tests/test_process_controller.py index 322cb2b7..1fa80312 100644 --- a/tests/test_process_controller.py +++ b/tests/test_process_controller.py @@ -274,7 +274,10 @@ def test_workflow_shape(): wf = yaml.safe_load(text) on = wf.get("on", wf.get(True)) assert set(on) == {"schedule", "workflow_dispatch", "pull_request"} - assert on["schedule"] == [{"cron": "7,37 * * * *"}], "every 30 minutes" + assert on["schedule"] == [{"cron": "17 */6 * * *"}], ( + "four slots a day: the half-hourly cron only ran two or three times a day, " + "so immediacy comes from the pull_request events, not the schedule" + ) assert on["pull_request"] == {"types": ["opened", "ready_for_review", "reopened"]}, \ "label a PR when it becomes reviewable, not on every push to its head" assert wf["permissions"] == {}