:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(ci): route merge queue backend ci to depot by its own percent (#110333)
Raúl Negrón-Otero1830cfd7be98
diff --git a/.agents/skills/debugging-ci-failures/SKILL.md b/.agents/skills/debugging-ci-failures/SKILL.mdindex bbc723edfede..f7a731b28b94 100644--- a/.agents/skills/debugging-ci-failures/SKILL.md+++ b/.agents/skills/debugging-ci-failures/SKILL.md@@ -153,6 +153,7 @@ practice that is most of the queue. So:The branch is ephemeral; the run and its logs stay on GitHub, and thewarehouse keeps its jobs under that `head_branch` (query 8 in the`investigating-ci-failures` references).+ Backend CI is the exception when `CI_BACKEND_DEPOT_MERGE_QUEUE_PERCENT` hands the batch to Depot CI: the backend tests then run on Depot, and the `Django Tests Pass` check links that run.- The PR's own checks can be green with the failing job **skipped** ornarrowed. On the PR, path filters see only that diff and the Django suite runsa selected subset; on the queue branch the diff is every carried PR's and thediff --git a/.depot/workflows/ci-backend.yml b/.depot/workflows/ci-backend.ymlindex 049c2374f151..1083032460b7 100644--- a/.depot/workflows/ci-backend.yml+++ b/.depot/workflows/ci-backend.yml@@ -129,7 +129,8 @@# fallback.# - No push trigger: canonical primes the shared schema cache on master, and Depot Cache# serves both engines. The `github.event_name != 'push'` guards stay to mirror canonical.-# Trunk merge-queue PRs are excluded because they would repeat a source PR's trial run.+# Trunk merge-queue PRs run here only when canonical's router hands them off, which its own+# CI_BACKEND_DEPOT_MERGE_QUEUE_PERCENT controls apart from source PRs.# - Fresh-master cache waiting is canonical-only: it waits on the GitHub Actions# warmer, which cannot signal readiness for Depot's separate cache channel.diff --git a/.github/scripts/ci_backend_relay.py b/.github/scripts/ci_backend_relay.pyindex cbeb09346c21..3dd4b1cf81de 100644--- a/.github/scripts/ci_backend_relay.py+++ b/.github/scripts/ci_backend_relay.py@@ -49,6 +49,14 @@ DEPOT_ORG = "ntsdt08fpt"# The PostHog tests GitHub App. Depot's wait and gate jobs post the same checks with it, because# Depot posts its own checks from a budget that runs out at peak and then delivers them late.MIRROR_APP_ID = 2492437+# The Trunk merge queue tests each batch through a draft pull request on this branch.+MERGE_QUEUE_PREFIX = "trunk-merge/"+++def is_merge_queue(head_ref: str) -> bool:+ return head_ref.startswith(MERGE_QUEUE_PREFIX)++DEPOT_WORKFLOW = "Backend CI on Depot"WAIT_JOB = "Wait for GitHub Actions to hand off backend tests"# Renders the same text as the wait job's name expression in .depot/workflows/ci-backend.yml.@@ -324,6 +332,7 @@ class Event:pr_number: int# The pull request's `updated_at` in this event's payload.event_at: str+ merge_queue: bool = Falsedef racing_wait(reader: CheckReader, event: Event, followed: set[str]) -> str | None:@@ -479,11 +488,22 @@ def retry_instructions(event: Event, details_url: str, run_id: str = "") -> listf" gh run rerun {run_id} --repo {event.repo} --failed","",]- return [+ lines = [f"Backend tests for {event.sha} ran on Depot CI, not GitHub Actions.",f"Depot run: {details_url or 'not found'}","",*(rerun if run_id else []),+ ]+ # Labels do not route a merge queue batch, and a push to its branch ends the queue attempt.+ if event.merge_queue:+ return [+ *lines,+ "Run on GitHub Actions instead: CI_BACKEND_DEPOT_MERGE_QUEUE_PERCENT=0 keeps new merge queue batches there.",+ f" gh variable set CI_BACKEND_DEPOT_MERGE_QUEUE_PERCENT --repo {event.repo} --body 0",+ "Then queue the pull request again.",+ ]+ return [+ *lines,"Run on GitHub Actions instead: the ci-backend-github label routes the next commit of this PR there.",f" gh pr edit {event.pr_number} --repo {event.repo} --add-label ci-backend-github"," git commit --allow-empty -m 'chore: retry backend ci on github actions' && git push",@@ -521,7 +541,13 @@ def main(argv: Sequence[str]) -> int:sys.stderr.write("usage: ci_backend_relay.py gate\n")return 2env = os.environ- event = Event(repo=env["REPO"], sha=env["SHA"], pr_number=int(env["PR_NUMBER"]), event_at=env["EVENT_AT"])+ event = Event(+ repo=env["REPO"],+ sha=env["SHA"],+ pr_number=int(env["PR_NUMBER"]),+ event_at=env["EVENT_AT"],+ merge_queue=is_merge_queue(env.get("HEAD_REF", "")),+ )reader = CheckRunReader(event.repo, event.sha, env["GH_TOKEN"], pr_number=event.pr_number)try:result = gate_verdict(reader, event, rerun=env.get("GITHUB_RUN_ATTEMPT", "1") != "1")diff --git a/.github/scripts/ci_backend_route.py b/.github/scripts/ci_backend_route.pyindex 52ff8bd18768..8e89908bd055 100644--- a/.github/scripts/ci_backend_route.py+++ b/.github/scripts/ci_backend_route.py@@ -14,6 +14,10 @@ until GitHub has updated that ref. When the ref is missing, Depot ends the run wworkflows; when it stays stale, Depot fails to compile. Depot also cancels some runs underits concurrency policy before they start. Each case leaves the event with no Depot run, soit stays here.++Merge queue batches have their own percent, CI_BACKEND_DEPOT_MERGE_QUEUE_PERCENT, so the+merge gate moves to Depot separately from source pull requests. Batches ignore the routing+labels, so setting that percent to 0 keeps every new batch on GitHub Actions."""import os@@ -37,13 +41,12 @@ from ci_backend_relay import (ReadFailedError,ReadRefusedError,event_check_name,+ is_merge_queue,)LABEL_FORCE_GITHUB = "ci-backend-github"LABEL_FORCE_DEPOT = "ci-backend-depot"PERCENT_VARIABLE = "CI_BACKEND_DEPOT_PERCENT"-# The Trunk merge queue tests each batch through a draft pull request on this branch.-MERGE_QUEUE_PREFIX = "trunk-merge/"HANDOFF_CHECK = "Hand off backend tests to Depot CI"GITHUB_ACTIONS_APP_ID = 15368ENGINE_BY_HANDOFF_CONCLUSION = {"success": "depot", "skipped": "github"}@@ -110,6 +113,15 @@ def handoff_conclusion(check_runs: list[dict], pr_number: int) -> str | None:return conclusions[0] if conclusions else None+def by_bucket(pr_number: int | None, percent: int, label: str) -> Decision:+ if pr_number is None:+ return Decision("github", "no pull request number to hash")+ bucket = bucket_of(pr_number)+ if bucket < percent:+ return Decision("depot", f"{label} {bucket} < {percent}%")+ return Decision("github", f"{label} {bucket} >= {percent}%")++def decide(event: str,percent: int,@@ -119,14 +131,15 @@ def decide(is_draft: bool,prior_handoff: str | None = None,head_ref: str = "",+ merge_queue_percent: int = 0,) -> Decision:if event != "pull_request":return Decision("github", f"{event} events stay on GitHub Actions")- if head_ref.startswith(MERGE_QUEUE_PREFIX):- return Decision("github", "merge queue batches stay on GitHub Actions")prior_engine = ENGINE_BY_HANDOFF_CONCLUSION.get(prior_handoff or "")if prior_engine:return Decision(prior_engine, f"an earlier run of this commit chose {prior_engine}")+ if is_merge_queue(head_ref):+ return by_bucket(pr_number, merge_queue_percent, "merge queue bucket")if LABEL_FORCE_GITHUB in labels:return Decision("github", f"label {LABEL_FORCE_GITHUB}")if is_fork:@@ -135,12 +148,7 @@ def decide(return Decision("github", "no-ci drafts skip on GitHub Actions and run nowhere else")if LABEL_FORCE_DEPOT in labels:return Decision("depot", f"label {LABEL_FORCE_DEPOT}")- if pr_number is None:- return Decision("github", "no pull request number to hash")- bucket = bucket_of(pr_number)- if bucket < percent:- return Decision("depot", f"bucket {bucket} < {percent}%")- return Decision("github", f"bucket {bucket} >= {percent}%")+ return by_bucket(pr_number, percent, "bucket")def fetch_handoff_checks(repo: str, sha: str, token: str, *, opener: Any = None) -> list[dict]:@@ -225,6 +233,7 @@ def main() -> int:is_draft=env.get("IS_DRAFT", "false") == "true",prior_handoff=prior_handoff,head_ref=env.get("HEAD_REF", ""),+ merge_queue_percent=parse_percent(env.get("MERGE_QUEUE_PERCENT")),)prior_handoff = Nonediff --git a/.github/scripts/test_ci_backend_relay.py b/.github/scripts/test_ci_backend_relay.pyindex 2bee38a2313f..0d40458cfb15 100644--- a/.github/scripts/test_ci_backend_relay.py+++ b/.github/scripts/test_ci_backend_relay.py@@ -1,6 +1,7 @@import reimport jsonimport subprocess+import dataclassesimport http.clientimport urllib.errorimport urllib.parse@@ -307,6 +308,22 @@ def test_relay_gate_fails_closed(result: Any, exit_code: int, first_line: str |assert first_line in lines[0]+ "merge_queue,present,absent",+ [+ (False, "--add-label ci-backend-github", "CI_BACKEND_DEPOT_MERGE_QUEUE_PERCENT"),+ (True, "CI_BACKEND_DEPOT_MERGE_QUEUE_PERCENT", "--add-label ci-backend-github"),+ ],+)+def test_retry_instructions_route_back_to_github_the_way_that_works(+ merge_queue: bool, present: str, absent: str+) -> None:+ event = dataclasses.replace(EVENT, merge_queue=merge_queue)+ text = "\n".join(relay.retry_instructions(event, "https://depot.dev/orgs/o/workflows/w1", "123"))+ assert present in text+ assert absent not in text++def shown(workflow: str, gate: str, executions: int) -> dict[str, Any]:return {"workflow": {"status": workflow},diff --git a/.github/scripts/test_ci_backend_route.py b/.github/scripts/test_ci_backend_route.pyindex b21a642732c1..24c2c8a28f46 100644--- a/.github/scripts/test_ci_backend_route.py+++ b/.github/scripts/test_ci_backend_route.py@@ -58,10 +58,20 @@ def test_non_pull_request_events_stay_on_github(event: str) -> None:assert route.decide(event, 100, None, ["ci-backend-depot"], False, False, "success").engine == "github"-def test_merge_queue_batches_stay_on_github() -> None:- queued = route.decide("pull_request", 100, 124, ["ci-backend-depot"], False, True, "success", "trunk-merge/pr-1/x")- assert queued.engine == "github"- assert route.decide("pull_request", 100, 124, [], False, False, None, "feature/trunk-merge").engine == "depot"+ "head_ref,labels,percent,queue_percent,prior,expected",+ [+ ("trunk-merge/pr-1/x", ["ci-backend-depot"], 100, 0, None, "github"),+ ("trunk-merge/pr-1/x", ["ci-backend-github"], 0, 100, None, "depot"),+ ("trunk-merge/pr-1/x", [], 0, 0, "success", "depot"),+ ("feature/trunk-merge", [], 100, 0, None, "depot"),+ ],+)+def test_merge_queue_batches_route_by_their_own_percent(+ head_ref: str, labels: list[str], percent: int, queue_percent: int, prior: str | None, expected: str+) -> None:+ decision = route.decide("pull_request", percent, 124, labels, False, True, prior, head_ref, queue_percent)+ assert decision.engine == expecteddef test_missing_pr_number_stays_on_github() -> None:@@ -162,22 +172,25 @@ def test_parse_percent_fails_closed(raw: str | None, expected: int) -> None:@pytest.mark.parametrize(- "labels,started,expected",+ "labels,started,head_ref,expected",[- (json.dumps(["other"]), True, "engine=depot\nreason=bucket 30 < 50%\n"),- ("null", True, "engine=depot\nreason=bucket 30 < 50%\n"),- ("", True, "engine=depot\nreason=bucket 30 < 50%\n"),- ("[]", False, "engine=github\nreason=Depot CI started no run for this event\n"),- (json.dumps(["ci-backend-depot"]), False, "engine=github\nreason=Depot CI started no run for this event\n"),+ (json.dumps(["other"]), True, "", "engine=depot\nreason=bucket 30 < 50%\n"),+ ("null", True, "", "engine=depot\nreason=bucket 30 < 50%\n"),+ ("", True, "", "engine=depot\nreason=bucket 30 < 50%\n"),+ ("[]", False, "", "engine=github\nreason=Depot CI started no run for this event\n"),+ (json.dumps(["ci-backend-depot"]), False, "", "engine=github\nreason=Depot CI started no run for this event\n"),+ ("[]", True, "trunk-merge/pr-1/x", "engine=github\nreason=merge queue bucket 30 >= 20%\n"),],)def test_main_writes_outputs(- labels: str, started: bool, expected: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch+ labels: str, started: bool, head_ref: str, expected: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:output = tmp_path / "out"monkeypatch.setenv("GITHUB_OUTPUT", str(output))monkeypatch.setenv("EVENT", "pull_request")monkeypatch.setenv("PERCENT", "50")+ monkeypatch.setenv("MERGE_QUEUE_PERCENT", "20")+ monkeypatch.setenv("HEAD_REF", head_ref)monkeypatch.setenv("PR_NUMBER", "7")monkeypatch.setenv("LABELS", labels)monkeypatch.setattr(route, "fetch_handoff_checks", lambda repo, sha, token: [])diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.ymlindex 4c8796d3e8ad..c461f6783f14 100644--- a/.github/workflows/ci-backend.yml+++ b/.github/workflows/ci-backend.yml@@ -522,6 +522,7 @@ jobs:if: github.event_name == 'pull_request'env:PERCENT: ${{ vars.CI_BACKEND_DEPOT_PERCENT }}+ MERGE_QUEUE_PERCENT: ${{ vars.CI_BACKEND_DEPOT_MERGE_QUEUE_PERCENT }}EVENT: ${{ github.event_name }}PR_NUMBER: ${{ github.event.pull_request.number }}LABELS: ${{ toJSON(github.event.pull_request.labels.*.name) }}@@ -4315,6 +4316,7 @@ jobs:SHA: ${{ github.event.pull_request.head.sha }}EVENT_AT: ${{ github.event.pull_request.updated_at }}PR_NUMBER: ${{ github.event.pull_request.number }}+ HEAD_REF: ${{ github.head_ref }}run: python3 .github/scripts/ci_backend_relay.py gate- name: Summarize dependency resultsif: needs.changes.outputs.engine != 'depot'