Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

ci: tame the merge train's fallback dispatch, re-runs and refused pushes (#476)

Ruben Lütkea5b21c92f5f4
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bf8bcfe95d..6e1e3765bc 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -69,10 +69,12 @@ on:
# organisation's concurrent-job limit — the newest run covers every commit
# before it. Every merge-queue entry gets its own group and is never
# cancelled: the queue itself drops entries it no longer needs, and a dropped
-# pending run would leave `CI gate` unreported until the queue times out. The
-# nightly run and manual dispatches get their own group.
+# pending run would leave `CI gate` unreported until the queue times out. A
+# macOS-free dispatch on main (the merge train's stand-in for a push its
+# GITHUB_TOKEN merge never fired) shares the push group. The nightly run and
+# other manual dispatches (release cuts included) get their own group.
concurrency:
- group: ci-${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || (github.event_name == 'merge_group' && format('merge-group-{0}', github.event.merge_group.head_ref)) || (github.event_name == 'workflow_dispatch' && github.ref_type == 'branch' && github.ref != 'refs/heads/main' && format('branch-{0}', github.ref)) || (github.event_name == 'push' && 'main-push') || github.run_id }}
+ group: ci-${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || (github.event_name == 'merge_group' && format('merge-group-{0}', github.event.merge_group.head_ref)) || (github.event_name == 'workflow_dispatch' && github.ref_type == 'branch' && github.ref != 'refs/heads/main' && format('branch-{0}', github.ref)) || ((github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main' && !inputs.include_macos)) && 'main-push') || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' || (github.event_name == 'workflow_dispatch' && github.ref_type == 'branch' && github.ref != 'refs/heads/main') }}
permissions:
diff --git a/scripts/ci/agent_integrate.py b/scripts/ci/agent_integrate.py
index 191755ba3b..5d67d5ba6f 100644
--- a/scripts/ci/agent_integrate.py
+++ b/scripts/ci/agent_integrate.py
@@ -689,6 +689,7 @@ def plan(repo: str, max_updates: int) -> dict[str, object]:
"action": action,
"run_id": run_id,
"note": note,
+ "labels": sorted(lb["name"] for lb in pr.get("labels", [])),
}
)
return {"repo": repo, "main": main_sha, "queue": queued is not None, "entries": entries}
@@ -850,7 +851,10 @@ def enqueue(entry: dict) -> int:
def dispatch_ci(ref: str) -> None:
- gh("workflow", "run", "ci.yml", "--ref", ref, "-f", "full=false")
+ """Stand-in for the push run a GITHUB_TOKEN merge never fires (direct-merge
+ fallback only; queue merges fire a real push). The dispatch input's
+ default would add every macOS lane (~10x runner cost) to each merge."""
+ gh("workflow", "run", "ci.yml", "--ref", ref, "-f", "full=false", "-f", "include_macos=false")
def _bundle_is_safe(sha: str, entry: dict, main_sha: str, checkout: Path) -> bool:
@@ -866,6 +870,35 @@ def _bundle_is_safe(sha: str, entry: dict, main_sha: str, checkout: Path) -> boo
return True
+_WORKFLOWS_BODY = (
+ "The merge train merged `main` into this branch, but could not push the result: "
+ "`main` changed files under `.github/workflows/`, and the train's `GITHUB_TOKEN` "
+ "may not push workflow changes. Merge `main` into this branch yourself (for example "
+ "`python scripts/agent_land.py --pr`), or give the repository an `INTEGRATION_TOKEN` "
+ "secret with workflow write access."
+)
+
+
+def push_rejection(stderr: str) -> str:
+ """Why a push was refused: ``workflows`` (the token lacks the permission
+ to touch .github/workflows) or ``moved`` (anything else, e.g. not a fast
+ forward because the author pushed meanwhile)."""
+ text = stderr.lower()
+ if "workflow" in text and ("permission" in text or "scope" in text):
+ return "workflows"
+ return "moved"
+
+
+def _report_once(entry: dict, repo: str, body: str, dry_run: bool) -> None:
+ """Comment and label a pull request the train cannot update - once: the
+ `needs-rebase` label marks it as told, so later ticks stay quiet."""
+ number = str(entry["number"])
+ if dry_run or "needs-rebase" in entry.get("labels", []):
+ return
+ gh("pr", "comment", number, "--repo", repo, "--body", body)
+ gh("pr", "edit", number, "--repo", repo, "--add-label", "needs-rebase")
+
+
def apply_update(
root: Path, data: dict, entry: dict, dry_run: bool, checkout: Path = REPO_ROOT
) -> str:
@@ -888,9 +921,7 @@ def apply_update(
"resolves generated files and lists what is left). The train retries on "
"the next push to this branch."
)
- if not dry_run:
- gh("pr", "comment", str(number), "--repo", repo, "--body", body)
- gh("pr", "edit", str(number), "--repo", repo, "--add-label", "needs-rebase")
+ _report_once(entry, repo, body, dry_run)
return "conflict - " + ", ".join(unresolved)
if status != "ready":
return f"update in unexpected state {status!r}; next tick"
@@ -910,6 +941,11 @@ def apply_update(
return "update bundle rejected (not the planned head plus main); next tick"
push = git("push", "origin", f"{sha}:refs/heads/{branch}", check=False, cwd=checkout)
if push.returncode != 0:
+ reason = push_rejection(push.stderr)
+ print(f"[train] #{number}: push rejected:\n{push.stderr.strip()[-2000:]}", flush=True)
+ if reason == "workflows":
+ _report_once(entry, repo, _WORKFLOWS_BODY, dry_run)
+ return "update needs the `workflows` permission (main changed .github/workflows)"
return "update rejected (the branch moved); retry next tick"
gh("pr", "edit", str(number), "--repo", repo, "--remove-label", "needs-rebase")
ai = [str(f) for f in state.get("ai_resolved") or []]
@@ -969,7 +1005,9 @@ def apply(root: Path, data: dict, dry_run: bool, token_is_bot: bool) -> int:
if action == "approve":
code = gh("api", "-X", "POST", f"repos/{repo}/actions/runs/{run_id}/approve")
else:
- code = gh("run", "rerun", str(run_id), "--repo", repo)
+ # Only the failed and cancelled jobs: a whole-run rerun of a
+ # full pipeline re-queues ~50 green jobs behind the runner limit.
+ code = gh("run", "rerun", str(run_id), "--failed", "--repo", repo)
if code == 0:
summary.append(f"#{number}: CI run {run_id} {action}d")
else:
diff --git a/tests/unit/ci/test_pipeline_scripts.py b/tests/unit/ci/test_pipeline_scripts.py
index 9edb7cdee8..e482b6bd25 100644
--- a/tests/unit/ci/test_pipeline_scripts.py
+++ b/tests/unit/ci/test_pipeline_scripts.py
@@ -636,6 +636,46 @@ def test_train_resolver_runs_between_merge_and_finish(tmp_path, repo, monkeypatc
assert any(call[:2] == ("pr", "comment") for call in fake.calls)
+def test_push_rejections_are_told_apart():
+ github = (
+ "! [remote rejected] abc -> claude/x (refusing to allow a GitHub App to create or "
+ "update workflow `.github/workflows/ci.yml` without `workflows` permission)"
+ )
+ assert agent_integrate.push_rejection(github) == "workflows"
+ assert agent_integrate.push_rejection("! [rejected] abc -> x (non-fast-forward)") == "moved"
+
+
+def test_a_workflow_permission_refusal_is_reported_once(tmp_path, repo, monkeypatch):
+ origin, integrate, applier, plan = _origin_with_feature(
+ tmp_path,
+ repo,
+ {"CHANGELOG.md": "# Changelog\n\n- main entry\n- base\n"},
+ {"CHANGELOG.md": "# Changelog\n\n- branch entry\n- base\n"},
+ )
+ hook = origin / "hooks" / "pre-receive"
+ hook.write_text(
+ "#!/bin/sh\necho 'refusing to allow a GitHub App to create or update workflow "
+ "`.github/workflows/ci.yml` without `workflows` permission' >&2\nexit 1\n",
+ encoding="utf-8",
+ newline="\n",
+ )
+ hook.chmod(0o755)
+ work = tmp_path / "train"
+ agent_integrate.integrate_merge(work, plan, integrate)
+ agent_integrate.integrate_finish(work, plan)
+ agent_integrate.cleanup_worktrees(work, integrate)
+ fake = _FakeGh()
+ monkeypatch.setattr(agent_integrate, "gh", fake)
+ outcome = agent_integrate.apply_update(work, plan, plan["entries"][0], False, applier)
+ assert "workflows" in outcome, outcome
+ assert any(c[:2] == ("pr", "comment") for c in fake.calls)
+ told = {**plan["entries"][0], "labels": ["needs-rebase"]}
+ quiet = _FakeGh()
+ monkeypatch.setattr(agent_integrate, "gh", quiet)
+ agent_integrate.apply_update(work, plan, told, False, applier)
+ assert not quiet.calls # already labelled: no comment on every tick
+
+
def test_train_reports_a_conflict_without_a_resolver(tmp_path, repo, monkeypatch):
origin, integrate, applier, plan = _origin_with_feature(
tmp_path, repo, {"app.py": "VALUE = 2\n"}, {"app.py": "VALUE = 3\n"}
@@ -696,6 +736,20 @@ def test_apply_enqueues_green_pull_requests_and_reports_the_rest(tmp_path, monke
assert not any(c[:2] == ("pr", "merge") or c[:2] == ("workflow", "run") for c in fake.calls)
+def test_apply_reruns_only_the_failed_jobs_of_a_cancelled_run(tmp_path, monkeypatch):
+ fake = _FakeGh()
+ monkeypatch.setattr(agent_integrate, "gh", fake)
+ plan = {
+ "repo": "example/project",
+ "main": "m" * 40,
+ "queue": True,
+ "entries": [{"number": 1, "branch": "claude/a", "head": "a" * 40, "id": "PR_a",
+ "action": "rerun", "run_id": 77}],
+ }
+ agent_integrate.apply(tmp_path, plan, False, True)
+ assert ("run", "rerun", "77", "--failed", "--repo", "example/project") in fake.calls
+
+
def test_apply_without_a_queue_merges_directly_and_pins_the_head(tmp_path, monkeypatch):
fake = _FakeGh()
monkeypatch.setattr(agent_integrate, "gh", fake)
@@ -710,7 +764,8 @@ def test_apply_without_a_queue_merges_directly_and_pins_the_head(tmp_path, monke
agent_integrate.apply(tmp_path, plan, False, True)
merge = next(c for c in fake.calls if c[:2] == ("pr", "merge"))
assert merge[-2:] == ("--match-head-commit", "a" * 40) and "--squash" in merge
- assert ("workflow", "run", "ci.yml", "--ref", "main", "-f", "full=false") in fake.calls
+ dispatch = ("workflow", "run", "ci.yml", "--ref", "main", "-f", "full=false")
+ assert dispatch + ("-f", "include_macos=false") in fake.calls # no macOS lanes per merge
# --------------------------------------------------------------------------- release