Skip to content

: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.

fix(engineering-analytics): resolve depot merge queue gate runs to the landed pr (#118586)

Georges-Antoine Assif9dbea952b37
diff --git a/products/engineering_analytics/SPEC.md b/products/engineering_analytics/SPEC.md
index 9d74724672f9..a3113a4f8ff5 100644
--- a/products/engineering_analytics/SPEC.md
+++ b/products/engineering_analytics/SPEC.md
@@ -196,7 +196,7 @@ Warehouse tables (GitHub source):
Warehouse table (Depot source):
-- `depot_job_attempts`: one row per Depot CI job attempt, for the repository the Depot source names. Reads keep only that repository's rows, because a source moved to another repository keeps what it synced before. Depot CI is its own engine, so these runs never reach the GitHub tables. `logic/views/depot_ci.py` reshapes the attempts into rows of the GitHub runs and jobs contracts and unions them onto those tables, so every builder reads both engines. Each Depot workflow is one runs row. A run with one workflow decodes its id to the integer `GITHUB_RUN_ID` Depot CI gives its jobs, which joins them to the CI test spans; a run with several workflows keys each by its own decoded workflow id, so no join fans out. The PR number comes from the run's `refs/pull/<n>/merge` ref, and the run's branch from that PR's snapshot. Job rows carry no branch, because a PR snapshot lookup in the jobs builder would add two full PR scans to every jobs read; a read that joins a job to its run takes the branch through `workflow_jobs.branch`, which falls back to the run's. Depot numbers attempts per job, not per run, so a job's last attempt is listed again under each later attempt of its run, as GitHub lists a job it did not re-run; the jobs builder flags those rows as `is_rerun_copy`. Depot's API reports no `runs-on`, so every attempt is costed as the default `depot-ubuntu-24.04` sandbox. Optional, and reads degrade to GitHub-only CI when unsynced.
+- `depot_job_attempts`: one row per Depot CI job attempt, for the repository the Depot source names. Reads keep only that repository's rows, because a source moved to another repository keeps what it synced before. Depot CI is its own engine, so these runs never reach the GitHub tables. `logic/views/depot_ci.py` reshapes the attempts into rows of the GitHub runs and jobs contracts and unions them onto those tables, so every builder reads both engines. Each Depot workflow is one runs row. A run with one workflow decodes its id to the integer `GITHUB_RUN_ID` Depot CI gives its jobs, which joins them to the CI test spans; a run with several workflows keys each by its own decoded workflow id, so no join fans out. The PR number comes from the run's `refs/pull/<n>/merge` ref, and the run's branch and actor from that PR's snapshot (its head ref and author). A merge-queue gate run's PR is the gate PR the queue bot opened, so the run resolves through `logic/merge_queue.py` to the PR being landed, like a GitHub gate run (§6). Job rows carry no branch, because a PR snapshot lookup in the jobs builder would add two full PR scans to every jobs read; a read that joins a job to its run takes the branch through `workflow_jobs.branch`, which falls back to the run's. Depot numbers attempts per job, not per run, so a job's last attempt is listed again under each later attempt of its run, as GitHub lists a job it did not re-run; the jobs builder flags those rows as `is_rerun_copy`. Depot's API reports no `runs-on`, so every attempt is costed as the default `depot-ubuntu-24.04` sandbox. Optional, and reads degrade to GitHub-only CI when unsynced.
- Depot ingestion replays seven days behind the creation-time watermark on every incremental sync and reconciles the schema's retained history weekly. The first sync after this policy deploys also reconciles retained history. The fixed `history_start` bounds new sources; a legacy source without that bound reconciles all available history. The durable reconciliation cursor advances only after extraction finishes and the pipeline makes the rows durable. Completed runs do not wait behind queued or stuck runs. Late retries older than the replay window can remain stale until the next reconciliation. Replayed attempts merge by `attempt_id`; append mode is unsupported, and an existing append sync stops before extraction with instructions to change its sync method. Depot's existing two-page-size pagination workaround still cannot guarantee recovery of every same-second cursor omission.
- The shared CI sources assign `ci_engine` from the warehouse source: `github_actions` or `depot_ci`. Runner provider is independent: GitHub Actions can use Depot runners. Managed job-history and job-cost views append `ci_engine`, `native_run_id`, `native_workflow_run_id`, `native_job_id`, and `native_attempt_id`. GitHub's native workflow execution ID is its run ID; Depot's is its workflow ID. These additive string IDs preserve source identity while the existing integer IDs remain compatible with test traces and API consumers. CI job-to-run joins, failure attribution, job run shares, and timeline paging qualify integer IDs with `ci_engine`. The friction view consumes the same normalized sources and resolves a Depot job's default branch through its run. Run and job APIs append engine and source-native IDs; per-run costs and UI caches qualify compatible integers with the engine. Detail, job, and log lookups accept `ci_engine`; an unqualified integer that names both engines fails with a 400. New failure logs and test traces stamp the engine. Recovery evidence groups by engine, native workflow identity, and commit SHA; historical untagged telemetry remains unknown and cannot establish cross-engine identity. Legacy logs are read only for integer IDs unique to one engine in the selected source. Depot workflows without a hand-off job remain visible, including default-branch pushes; their branch and commit come from `ref` and `sha` when no PR head is present.
- While a workflow moves to Depot CI, GitHub Actions routes each pull request to one engine, and both engines record a run of the commit. The union suppresses a successful Depot workflow whose `wait-for-handoff` job ran alone. A GitHub run is suppressed only when it succeeded, its `Hand off backend tests to Depot CI` job succeeded, its latest `Django Tests Pass` job succeeded, no synced job attempt decisively failed, and a successful Depot workflow took the hand-off for the same PR number and `head_sha`. Missing association or replacement data leaves the GitHub run visible. Failed, cancelled and unsettled GitHub relays and Depot waits stay visible. A relay that recovered retains its earlier failure history. Suppression applies to both runs and jobs; raw source rows remain available for full runner-cost accounting. Curated estimates cover the visible execution jobs; suppressed hand-off and relay overhead is excluded. These figures are not total runner spend or invoice reconciliation.
diff --git a/products/engineering_analytics/backend/logic/views/depot_ci.py b/products/engineering_analytics/backend/logic/views/depot_ci.py
index 61e25e9084eb..3569e5cc64c0 100644
--- a/products/engineering_analytics/backend/logic/views/depot_ci.py
+++ b/products/engineering_analytics/backend/logic/views/depot_ci.py
@@ -119,14 +119,19 @@ def _attempts(depot: DepotJobAttempts, pull_requests_table: str | None) -> str:
pr_number = "ifNull(toInt(extract(a.ref, '^refs/pull/([0-9]+)/')), 0)"
if pull_requests_table:
head_branch = "nullIf(pr.head_branch, '')"
+ actor = "nullIf(pr.author, '')"
branch_join = f"""
LEFT JOIN (
- SELECT number, any(JSONExtractString(ifNull(head, '{{}}'), 'ref')) AS head_branch
+ SELECT
+ number,
+ any(JSONExtractString(ifNull(head, '{{}}'), 'ref')) AS head_branch,
+ any(user) AS author
FROM {pull_requests_table}
GROUP BY number
) AS pr ON {pr_number} = pr.number"""
else:
head_branch = "NULL"
+ actor = "NULL"
branch_join = ""
# A push run's id carries a prefix outside the alphabet, and such a run holds no workflows.
return f"""(
@@ -136,6 +141,7 @@ def _attempts(depot: DepotJobAttempts, pull_requests_table: str | None) -> str:
{pr_number} AS pr_number,
coalesce(nullIf(extract(ifNull(a.ref, ''), '^refs/heads/(.+)$'), ''), {head_branch}) AS head_branch,
a.repo AS repo,
+ {actor} AS actor,
coalesce(nullIf(a.head_sha, ''), a.sha) AS head_sha,
a.run_id AS native_run_id,
a.workflow_id AS native_workflow_run_id,
@@ -181,7 +187,7 @@ def _runs(attempts: str) -> str:
) AS pull_requests,
concat('{{"id":{_REPOSITORY_ID},"full_name":"', any(repo), '"}}') AS repository,
NULL AS head_commit,
- NULL AS actor,
+ any(actor) AS actor,
{OPTIONAL_WORKFLOW_RUNS_COLUMNS["workflow_id"]} AS workflow_id,
{OPTIONAL_WORKFLOW_RUNS_COLUMNS["event"]} AS event,
'depot_ci' AS ci_engine,
diff --git a/products/engineering_analytics/backend/tests/test_views.py b/products/engineering_analytics/backend/tests/test_views.py
index 377191df5f0c..0f148ad3ca60 100644
--- a/products/engineering_analytics/backend/tests/test_views.py
+++ b/products/engineering_analytics/backend/tests/test_views.py
@@ -554,6 +554,74 @@ class TestEngineeringAnalyticsViews(ClickhouseTestMixin, BaseTest):
"WHERE native_attempt_id = '3v4pbsqvfc'"
) == [("depot_ci", "427q556wmn", "6n4tghls33", "3v4pbsqvfc")]
+ @parameterized.expand(
+ [
+ ("queue_bot_gate_pr", "trunk-io[bot]", 101991, 1),
+ ("spoofed_branch_shape", "mallory", 117864, 0),
+ ]
+ )
+ def test_depot_merge_queue_gate_run_resolves_to_the_landed_pr(
+ self, _name: str, gate_pr_author: str, expected_pr_number: int, expected_is_merge_queue: int
+ ) -> None:
+ runs_table = self._create_table(
+ "github_workflow_runs",
+ WORKFLOW_RUNS_COLUMNS,
+ [
+ _run_row(
+ 901, "Backend CI", "abc123", "completed", "success", "2026-09-24 14:50:00", "2026-09-24 15:05:00"
+ )
+ ],
+ )
+ prs_table = self._create_table(
+ "github_pull_requests",
+ PULL_REQUESTS_COLUMNS,
+ [
+ _pr_row(
+ 117864,
+ gate_pr_author,
+ "open",
+ 1,
+ "2026-09-24T14:00:00Z",
+ head_ref="trunk-merge/pr-101991/1b92e8cf-acbf-4658-8071-4cf622123da8",
+ )
+ ],
+ )
+ depot_table = self._create_table(
+ "depot_job_attempts",
+ DEPOT_JOB_ATTEMPTS_COLUMNS,
+ [
+ {
+ "run_id": "427q556wmn",
+ "run_workflow_count": 1,
+ "repo": "PostHog/posthog",
+ "ref": "refs/pull/117864/merge",
+ "head_sha": "abc123",
+ "sha": "",
+ "workflow_id": "6n4tghls33",
+ "workflow_name": "Backend CI on Depot",
+ "workflow_status": "failed",
+ "workflow_created_at": "2026-09-24T14:51:17.948Z",
+ "workflow_started_at": "2026-09-24T14:51:18.000Z",
+ "workflow_finished_at": "2026-09-24T15:01:18.000Z",
+ "job_id": "p4kd9tq2xs",
+ "job_key": "ci-backend.yml:django-tests",
+ "job_display_name": "Django Tests Pass on Depot",
+ "attempt_id": "zf6sbbn2wh",
+ "attempt": 1,
+ "attempt_status": "failed",
+ "attempt_started_at": "2026-09-24T14:53:00.000Z",
+ "attempt_finished_at": "2026-09-24T14:55:00.000Z",
+ "sandbox_id": "sandbox",
+ }
+ ],
+ )
+ depot = depot_ci.DepotJobAttempts(table=depot_table, repository="PostHog/posthog")
+ runs = depot_ci.with_depot_runs(runs_table, depot, prs_table, None)
+
+ assert self._select(
+ f"SELECT pr_number, is_merge_queue FROM ({workflow_runs.build_query(runs)}) AS r WHERE ci_engine = 'depot_ci'"
+ ) == [(expected_pr_number, expected_is_merge_queue)]
+
def test_pull_requests_view_handles_null_user(self) -> None:
# The real source lands user as Nullable(String), NULL for a PR by a deleted GitHub account.
# JSONExtractString over a NULL Nullable returns NULL, so the builder must ifNull-guard it to