: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
fix(ci): relay a racing event's depot run (#106889)
Raúl Negrón-Oteroae2d634b96e3
diff --git a/.agents/skills/depot-ci/references/posthog-check-run-semantics.md b/.agents/skills/depot-ci/references/posthog-check-run-semantics.mdindex 1bbf30ba647e..40f63c680680 100644--- a/.agents/skills/depot-ci/references/posthog-check-run-semantics.md+++ b/.agents/skills/depot-ci/references/posthog-check-run-semantics.md@@ -75,6 +75,16 @@ In the following cancelled runs, an unstarted job received a `cancelled` check wCheck times and `pull_requests` did not pick out the Depot run in these cases, so the event goes into a check name. The Depot wait job's name ends with `(PR <number>, event <pull_request.updated_at>)`, and Depot renders expressions in job names into the check name. `.github/scripts/ci_backend_relay.py` builds the same name from the GitHub run's payload, reads that check, takes the Depot workflow id from its `details_url`, and reads the gate or migration check of that workflow only. Measured 2026-09-24 on PR 105886: Depot posted check `107656013423` named `… (PR 105886, event 2026-09-24T13:33:14Z)`, and GitHub relay job `107655803961` used `EVENT_AT: 2026-09-24T13:33:14Z` and relayed a successful gate.+### Racing events++Two events of one commit that arrive within a second or two race the concurrency cancel on both engines, and neither engine reliably keeps the newer event. A stack push shows it: it force-pushes a pull request's head and its base about one second apart, and each push starts a pull request event. Measured 2026-09-25 on a stack push at 15:17Z:++- PR 106429: GitHub Actions cancelled the run of the newer event (15:17:40Z) and kept the older one (15:17:39Z). Depot kept 15:17:40Z and cancelled Depot run `rtl932mrbc` with "Cancelled due to the workflow concurrency policy".+- PR 106435: GitHub Actions kept the newer event (15:17:41Z). Depot kept the older one (15:17:40Z) and cancelled `nzb661zql2`.+- Neither cancelled Depot run posted a wait check, so each surviving GitHub relay found no run for its event and failed after the grace period, although Depot's run for the other event passed on the same commit.++GitHub Actions alone does the same. Among the 946 `ci-backend.yml` pull request runs created on 2026-09-25 between 11:30Z and 16:00Z, 56 same-commit pairs were created within 30 seconds of each other, 52 of them 0 to 2 seconds apart. GitHub kept the older run in 6 pairs, all 0 to 1 second apart. So after its grace period, the relay follows the Depot run of an event of the same pull request up to 2 seconds away (`RACING_EVENT_SECONDS`), newest first.+The newest check per name tells you the verdict. It does not tell you whether the PR can merge. A cancelled run on the same head keeps its checks, and when one of them is a required check, GitHub's merge box and Trunk keep the PR blocked until that run is rerun. `/merging-prs` has the recipe.## Depot CI CLI recipesdiff --git a/.github/scripts/ci_backend_relay.py b/.github/scripts/ci_backend_relay.pyindex fde59524daff..a8c847dd2c60 100644--- a/.github/scripts/ci_backend_relay.py+++ b/.github/scripts/ci_backend_relay.py@@ -18,6 +18,11 @@ So the Depot wait job's check name carries the event: the pull request number anevent's run, and every other check of the run is matched by the Depot workflow id in itsdetails URL.+Two events of one commit that arrive within a second or two race the concurrency cancel of+both engines, and each engine can keep a different event. GitHub Actions alone can also keep+either event of such a pair, so when this event has no live Depot run, the relay follows the+run that Depot kept for the racing event.+Standard library only: the relay job runs this with the runner's python3 before any install."""@@ -32,6 +37,7 @@ import urllib.parseimport urllib.requestfrom collections.abc import Callable, Iterable, Sequencefrom dataclasses import dataclass+from datetime import datetime, timedeltafrom enum import Enumfrom typing import Any, Protocol@@ -40,6 +46,9 @@ 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.EVENT_SUFFIX = " (PR {pr}, event {event_at})"+# How a pull request event payload renders `updated_at`.+EVENT_TIME = "%Y-%m-%dT%H:%M:%SZ"+RACING_EVENT_SECONDS = 2GATE_CHECK = f"{DEPOT_WORKFLOW} / Django Tests Pass on Depot"MIGRATION_CHECK = f"{DEPOT_WORKFLOW} / Validate migrations"CHANGES_CHECK = f"{DEPOT_WORKFLOW} / Determine need to run backend and migration checks"@@ -246,6 +255,19 @@ class Event:event_at: str+def racing_wait(reader: CheckReader, event: Event, followed: set[str]) -> str | None:+ """The newest racing wait check whose job took the hand-off or is pending, skipping `followed`."""+ event_at = datetime.strptime(event.event_at, EVENT_TIME)+ for offset in range(RACING_EVENT_SECONDS, -RACING_EVENT_SECONDS - 1, -1):+ name = wait_check_name(event.pr_number, (event_at + timedelta(seconds=offset)).strftime(EVENT_TIME))+ if name in followed:+ continue+ wait = newest_live(reader.read(name))+ if progress(wait, ()).phase in (Phase.STARTING, Phase.RUNNING):+ return name+ return None++def wait_for_handoff(reader: CheckReader,event: Event,@@ -278,9 +300,11 @@ def poll(A duplicate run of the same event can replace a cancelled one. The migration reportgives an absent or cancelled run the full deadline; the required gate uses a shorter grace period.+ After the grace period, the run of a racing event stands in for an absent or cancelled one."""start = clock()event_name = wait_check_name(event.pr_number, event.event_at)+ followed = {event_name}while True:wait = newest_live(reader.read(event_name))checks = reader.read(check_name) if wait and wait.state == "success" else []@@ -293,7 +317,13 @@ def poll(if current.phase in (Phase.FINISHED, Phase.DECLINED):return currentif current.phase in (Phase.ABSENT, Phase.CANCELLED) and elapsed >= absent_minutes * 60:- return current+ racing = racing_wait(reader, event, followed)+ if racing is None:+ return current+ sys.stdout.write(f"Depot kept a racing event of this commit instead. Following: {racing}\n")+ followed.add(racing)+ event_name = racing+ continueif elapsed >= deadline_minutes * 60:return current# The checked job only posts its check when the matrix is done, so once Depot hasdiff --git a/.github/scripts/test_ci_backend_relay.py b/.github/scripts/test_ci_backend_relay.pyindex 9a1774226e4c..a789becfc293 100644--- a/.github/scripts/test_ci_backend_relay.py+++ b/.github/scripts/test_ci_backend_relay.py@@ -23,6 +23,8 @@ EVENT_AT = "2026-09-24T09:54:20Z"EVENT = relay.Event(repo="PostHog/posthog", sha="a8a3755cf964", pr_number=PR, event_at=EVENT_AT)EVENT_WAIT = relay.wait_check_name(PR, EVENT_AT)PLAIN_WAIT = f"{relay.DEPOT_WORKFLOW} / {relay.WAIT_JOB}"+OLDER_RACING_WAIT = relay.wait_check_name(PR, "2026-09-24T09:54:19Z")+NEWER_RACING_WAIT = relay.wait_check_name(PR, "2026-09-24T09:54:22Z")def run(@@ -158,6 +160,76 @@ class FakeClock:id="a cancelled run fails only after the grace window",),pytest.param([{}], (relay.Phase.ABSENT, ""), 15, id="no run fails after the grace window"),+ pytest.param(+ [+ {+ OLDER_RACING_WAIT: [run(1, "success", workflow="racing")],+ relay.GATE_CHECK: [run(10, "failure", workflow="racing")],+ }+ ],+ (relay.Phase.FINISHED, "failure"),+ 15,+ id="an older racing event's run stands in for an absent run",+ ),+ pytest.param(+ [+ {+ EVENT_WAIT: [run(1, "cancelled")],+ NEWER_RACING_WAIT: [run(2, "success", workflow="racing")],+ relay.GATE_CHECK: [run(10, "success", workflow="racing")],+ }+ ],+ (relay.Phase.FINISHED, "success"),+ 15,+ id="a newer racing event's run stands in for a cancelled run",+ ),+ pytest.param(+ [+ {+ NEWER_RACING_WAIT: [run(1, "success", workflow="newer")],+ OLDER_RACING_WAIT: [run(2, "success", workflow="older")],+ relay.GATE_CHECK: [run(10, "cancelled", workflow="newer"), run(11, "success", workflow="older")],+ }+ ],+ (relay.Phase.FINISHED, "success"),+ 15,+ id="the next racing event's run stands in when the followed one is cancelled",+ ),+ pytest.param(+ [+ {+ NEWER_RACING_WAIT: [run(1, "success", workflow="newer")],+ OLDER_RACING_WAIT: [run(2, "success", workflow="older")],+ relay.GATE_CHECK: [run(10, "success", workflow="newer"), run(11, "failure", workflow="older")],+ }+ ],+ (relay.Phase.FINISHED, "success"),+ 15,+ id="the newer of two racing runs stands in",+ ),+ pytest.param(+ [+ {+ NEWER_RACING_WAIT: [run(1, "skipped", workflow="declined")],+ OLDER_RACING_WAIT: [run(2, "success", workflow="older")],+ relay.GATE_CHECK: [run(10, "success", workflow="older")],+ }+ ],+ (relay.Phase.FINISHED, "success"),+ 15,+ id="a racing run that declined the hand-off does not stand in",+ ),+ pytest.param(+ [+ {+ relay.wait_check_name(PR, "2026-09-24T09:54:17Z"): [run(1, "success", workflow="earlier")],+ relay.GATE_CHECK: [run(10, "success", workflow="earlier")],+ }+ ],+ (relay.Phase.ABSENT, ""),+ 15,+ id="an event outside the race window does not stand in",+ ),pytest.param([{EVENT_WAIT: [run(1, "success")]}],(relay.Phase.RUNNING, ""),@@ -264,18 +336,34 @@ def test_migration_report_stops_when_depot_did_not_receive_the_handoff() -> Noneassert clock.now == 0-def test_migration_report_waits_for_a_late_replacement_of_a_cancelled_run() -> None:+ "late_poll,workflow",+ [+ pytest.param(+ {+ EVENT_WAIT: [run(1, "cancelled"), run(2, "success", workflow="replacement")],+ relay.MIGRATION_CHECK: [run(3, "success", workflow="replacement")],+ },+ "replacement",+ id="a late replacement of this event's run",+ ),+ pytest.param(+ {+ EVENT_WAIT: [run(1, "cancelled")],+ OLDER_RACING_WAIT: [run(2, "success", workflow="racing")],+ relay.MIGRATION_CHECK: [run(3, "success", workflow="racing")],+ },+ "racing",+ id="a racing event's run at the deadline",+ ),+ ],+)+def test_migration_report_finds_the_run_that_stands_in_for_a_cancelled_one(+ late_poll: dict[str, list[Any]], workflow: str+) -> None:clock = FakeClock()result = relay.poll(- FakeReader(- [{EVENT_WAIT: [run(1, "cancelled")]}] * 25- + [- {- EVENT_WAIT: [run(1, "cancelled"), run(2, "success", workflow="replacement")],- relay.MIGRATION_CHECK: [run(3, "success", workflow="replacement")],- }- ]- ),+ FakeReader([{EVENT_WAIT: [run(1, "cancelled")]}] * 25 + [late_poll]),EVENT,relay.MIGRATION_CHECK,deadline_minutes=70,@@ -283,7 +371,7 @@ def test_migration_report_waits_for_a_late_replacement_of_a_cancelled_run() -> Nclock=clock,sleep=clock.sleep,)- assert (result.phase, result.state) == (relay.Phase.FINISHED, "success")+ assert relay.report_migrations(result)[2] == {"migration_state": "success", "workflow_id": workflow}assert clock.now > 10 * 60