: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): keep depot routing sticky after rollback (#103085)
Raúl Negrón-Otero04f9f8f04e4a
diff --git a/.github/scripts/ci_backend_route.py b/.github/scripts/ci_backend_route.pyindex 058f112eb586..740e16d3cdc0 100644--- a/.github/scripts/ci_backend_route.py+++ b/.github/scripts/ci_backend_route.py@@ -5,8 +5,9 @@ Only GitHub Actions runs this script. Its answer drives the `Hand off backend teto Depot CI` job, which Depot CI waits for before it runs anything, so Depot neverroutes on its own and the tests never run on both engines. Once that job has concludedfor a commit, every later run of the same commit repeats its answer, whatever the-percent or the labels say by then. A read of that record that keeps failing fails the-run, so nothing is routed anywhere.+percent or the labels say by then, even after the rollout variable is deleted. A read of+that record that keeps failing fails the run when the event would go to Depot, so+nothing is routed anywhere, and otherwise leaves the event on GitHub Actions."""import os@@ -160,29 +161,37 @@ def main() -> int:pr_number = int(pr_raw) if pr_raw.isdigit() else Noneis_fork = env.get("IS_FORK", "false") == "true"labels = json.loads(env.get("LABELS") or "null") or []- # Until the rollout variable exists, only a label can route a commit, so every other- # pull request skips the read and its API call, and cannot fail on it.- routing_possible = bool(env.get("PERCENT")) or bool({LABEL_FORCE_DEPOT, LABEL_FORCE_GITHUB} & set(labels))++ def route_with(prior_handoff: str | None) -> Decision:+ return decide(+ event=event,+ percent=parse_percent(env.get("PERCENT")),+ pr_number=pr_number,+ labels=labels,+ is_fork=is_fork,+ is_draft=env.get("IS_DRAFT", "false") == "true",+ prior_handoff=prior_handoff,+ head_ref=env.get("HEAD_REF", ""),+ )+prior_handoff = None- if event == "pull_request" and pr_number is not None and not is_fork and routing_possible:+ if event == "pull_request" and pr_number is not None and not is_fork:try:prior_handoff = handoff_conclusion(fetch_handoff_checks(env["REPO"], env["SHA"], env["GH_TOKEN"]), pr_number)except HandoffReadError as error:- sys.stdout.write(f"::error::Cannot read the earlier hand-off, so this event is not routed: {error}\n")- return 1- sys.stdout.write(f"::notice::Earlier hand-off on this commit: {prior_handoff or 'none'}\n")- decision = decide(- event=event,- percent=parse_percent(env.get("PERCENT")),- pr_number=pr_number,- labels=labels,- is_fork=is_fork,- is_draft=env.get("IS_DRAFT", "false") == "true",- prior_handoff=prior_handoff,- head_ref=env.get("HEAD_REF", ""),- )+ if route_with(None).engine == "depot":+ sys.stdout.write(f"::error::Cannot read the earlier hand-off, so this event is not routed: {error}\n")+ return 1+ # Staying on GitHub Actions never sends a commit to Depot twice. The worst case is+ # a GitHub rerun of a commit Depot already tested, which costs runners, not safety.+ sys.stdout.write(+ f"::warning::Cannot read the earlier hand-off, so this event stays on GitHub Actions: {error}\n"+ )+ else:+ sys.stdout.write(f"::notice::Earlier hand-off on this commit: {prior_handoff or 'none'}\n")+ decision = route_with(prior_handoff)sys.stdout.write(f"::notice::Backend CI engine: {decision.engine} ({decision.reason})\n")output_path = env.get("GITHUB_OUTPUT")if output_path:diff --git a/.github/scripts/test_ci_backend_route.py b/.github/scripts/test_ci_backend_route.pyindex 1d8adae98195..5c5142e4c538 100644--- a/.github/scripts/test_ci_backend_route.py+++ b/.github/scripts/test_ci_backend_route.py@@ -173,50 +173,57 @@ def test_main_writes_outputs(labels: str, tmp_path: Path, monkeypatch: pytest.Moassert output.read_text() == "engine=depot\nreason=bucket 30 < 50%\n"- "percent,labels,reads",- [- ("", "[]", 0),- ("0", "[]", 1),- ("", json.dumps(["ci-backend-depot"]), 1),- ("", json.dumps(["ci-backend-github"]), 1),- ],-)-def test_main_reads_the_handoff_only_when_routing_is_possible(- percent: str, labels: str, reads: int, tmp_path: Path, monkeypatch: pytest.MonkeyPatch[email protected]("percent", ["", "0", "5"])+def test_main_keeps_a_handed_off_commit_on_depot_after_rollback(+ percent: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:- calls: list[str] = []-def fetch(repo: str, sha: str, token: str) -> list[dict[str, Any]]:- calls.append(sha)- return []+ return [{"id": 1, "status": "completed", "conclusion": "success", "pull_requests": [{"number": 7}]}]- monkeypatch.setenv("GITHUB_OUTPUT", str(tmp_path / "out"))+ output = tmp_path / "out"+ monkeypatch.setenv("GITHUB_OUTPUT", str(output))monkeypatch.setenv("EVENT", "pull_request")monkeypatch.setenv("PERCENT", percent)monkeypatch.setenv("PR_NUMBER", "7")- monkeypatch.setenv("LABELS", labels)+ monkeypatch.setenv("LABELS", "[]")monkeypatch.setenv("REPO", "PostHog/posthog")monkeypatch.setenv("SHA", "abc")monkeypatch.setenv("GH_TOKEN", "t")monkeypatch.setattr(route, "fetch_handoff_checks", fetch)assert route.main() == 0- assert len(calls) == reads+ assert output.read_text().startswith("engine=depot\n")-def test_main_fails_when_the_earlier_handoff_cannot_be_read(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ "percent,labels,exit_code",+ [+ ("50", "[]", 1),+ ("", json.dumps(["ci-backend-depot"]), 1),+ ("5", "[]", 0),+ ("0", "[]", 0),+ ("", "[]", 0),+ ("50", json.dumps(["ci-backend-github"]), 0),+ ],+)+def test_main_fails_on_an_unreadable_handoff_only_when_depot_would_run(+ percent: str, labels: str, exit_code: int, tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:def failing(repo: str, sha: str, token: str) -> list[dict[str, Any]]:raise route.HandoffReadError("boom")- monkeypatch.setenv("GITHUB_OUTPUT", str(tmp_path / "out"))+ output = tmp_path / "out"+ monkeypatch.setenv("GITHUB_OUTPUT", str(output))monkeypatch.setenv("EVENT", "pull_request")- monkeypatch.setenv("PERCENT", "5")+ monkeypatch.setenv("PERCENT", percent)monkeypatch.setenv("PR_NUMBER", "7")- monkeypatch.setenv("LABELS", "[]")+ monkeypatch.setenv("LABELS", labels)monkeypatch.setenv("REPO", "PostHog/posthog")monkeypatch.setenv("SHA", "abc")monkeypatch.setenv("GH_TOKEN", "t")monkeypatch.setattr(route, "fetch_handoff_checks", failing)monkeypatch.setattr(route.time, "sleep", lambda seconds: None)- assert route.main() == 1- assert not (tmp_path / "out").exists()+ assert route.main() == exit_code+ if exit_code:+ assert not output.exists()+ else:+ assert output.read_text().startswith("engine=github\n")