: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(signals): scope ranking outcome timestamps to the report's tenant (#101640)
posthog[bot]eeedd7916ed9
diff --git a/products/signals/dags/inbox_ranking/AGENTS.md b/products/signals/dags/inbox_ranking/AGENTS.mdindex 5fa9a2d7ac91..d50b00ab6095 100644--- a/products/signals/dags/inbox_ranking/AGENTS.md+++ b/products/signals/dags/inbox_ranking/AGENTS.md@@ -40,6 +40,10 @@ Read `README.md` first for what the dataset is and how partitions behave. This f- `dt=` partitions are **immutable snapshots** with deterministic object keys; the only mutation ever applied is an idempotent re-run of the same partition. The exception is `inbox_signal_embeddings`, an emission log whose partition holds only that day's inserts — see the README's signal-grain section before touching it. Its re-run must stay **additive** (union with the existing object): the source drops rows it already archived, so a plain overwrite destroys history that exists nowhere else.- Label columns are **cumulative from `LABELS_EPOCH`**; never bake a maturity window or a rolling time bound into the SQL (the saved `inbox_ranking_*` views roll 90 days — that is exactly why their SQL is inlined here with explicit bounds instead of reused).- Every cumulative outcome count ships with the column holding the moment its first counted event arrived, declared as a pair in `OUTCOME_FIRST_EVENT_COLUMNS` (a test fails on an unpaired count). A count alone dates an outcome only to the whole cumulative window, so a horizon label or a time-to-outcome read has nothing but a proxy without that moment. The timestamp must use the same predicate as its count, or the pair describes different events.+- Every `STATUS_SQL` aggregate reads only the latest transition's tenant (`event_team_id = latest_event_team_id`).+ `report_id` and `team_id` come from event properties, so the tenant an event names is claimed and not proven.+ An unscoped aggregate lets an event that names another team set a label, and `label_provenance_ok` checks only the latest transition.+ A forged-event invariance test over `STATUS_COLUMNS` fails on an unscoped aggregate.- `latest/` must stay **monotonic** (snapshot-date metadata stamp); backfills must never overwrite it.- Schema changes: additive nullable columns bump `FEATURE_SCHEMA_VERSION`; breaking changes bump the `v1` path segment. The parquet schemas and the row assemblers must stay in exact key agreement — `pa.Table.from_pylist` silently drops unknown keys, and a test guards this.- Cross-team ClickHouse reads stay on the offline workload with explicit guards; team-2 label queries keep their event + timestamp bounds aligned with the events sort key.diff --git a/products/signals/dags/inbox_ranking/dataset/queries.py b/products/signals/dags/inbox_ranking/dataset/queries.pyindex fee2dc89e11b..737f09f670aa 100644--- a/products/signals/dags/inbox_ranking/dataset/queries.py+++ b/products/signals/dags/inbox_ranking/dataset/queries.py@@ -380,10 +380,14 @@ STATUS_SQL = ("""SELECTreport_id,- nullIf(minIf(first_timestamp, outcome = 'resolved'), fromUnixTimestamp(0)) AS first_resolved_at,- nullIf(minIf(first_timestamp, outcome = 'dismissed'), fromUnixTimestamp(0)) AS first_dismissed_server_at,- nullIf(minIf(first_timestamp, outcome = 'failed'), fromUnixTimestamp(0)) AS first_failed_at,- nullIf(minIf(first_timestamp, outcome = 'snoozed'), fromUnixTimestamp(0)) AS first_snoozed_at,+ -- Each restricted to the latest transition's tenant, like the reason and the count below: team_id+ -- rides on event properties, so an event naming another team would otherwise win these min()+ -- calls and date an outcome this tenant never had, while still passing the provenance check.+ -- Claimed is not proven — an event naming the report's real team passes.+ nullIf(minIf(first_timestamp, outcome = 'resolved' AND event_team_id = latest_event_team_id), fromUnixTimestamp(0)) AS first_resolved_at,+ nullIf(minIf(first_timestamp, outcome = 'dismissed' AND event_team_id = latest_event_team_id), fromUnixTimestamp(0)) AS first_dismissed_server_at,+ nullIf(minIf(first_timestamp, outcome = 'failed' AND event_team_id = latest_event_team_id), fromUnixTimestamp(0)) AS first_failed_at,+ nullIf(minIf(first_timestamp, outcome = 'snoozed' AND event_team_id = latest_event_team_id), fromUnixTimestamp(0)) AS first_snoozed_at,argMax(status, last_timestamp) AS latest_status_event,max(last_timestamp) AS latest_status_event_at,-- argMax skips NULL values, so this is the reason from the latest *reasoned* transition (the@@ -399,11 +403,6 @@ SELECT-- NULL rather than handing over the next dismissal's reason: a dismissal carries no reason-- whenever no artefact accompanies the transition, and this column has to describe the earliest-- dismissal itself.- --- -- Caveat until posthog#101565 lands: this reason is restricted to the latest transition's- -- tenant while first_dismissed_server_at above is not, so an earlier dismissal naming another- -- team can date that column while this one reads a later genuine dismissal. Treat the two as- -- separate reads, not as one event.nullIf(argMinIf(bucket_first_dismissal_reason,diff --git a/products/signals/dags/inbox_ranking/tests/test_dataset.py b/products/signals/dags/inbox_ranking/tests/test_dataset.pyindex 6daf625fa40b..a5789e5bfb45 100644--- a/products/signals/dags/inbox_ranking/tests/test_dataset.py+++ b/products/signals/dags/inbox_ranking/tests/test_dataset.py@@ -502,6 +502,25 @@ class TestStatusStream(ClickhouseTestMixin, BaseTest):assert row["first_dismissal_reason"] == reasonassert row["wrong_dismissal_count"] == 0assert row["first_wrong_dismissed_at"] is None+ # The forged dismissal is the only one in the later_bucket case, so an unscoped min() dates+ # a dismissal the report's tenant never made.+ assert row["first_dismissed_server_at"] != T1++ def test_no_status_column_reads_an_event_from_another_tenant(self):+ # Forged-event invariance: transitions naming another team, all earlier than the genuine+ # ones, must leave every status column exactly as the genuine transitions alone produce it.+ # Asserted over STATUS_COLUMNS rather than a list written out here, so an aggregate added+ # later is covered without being enumerated. The genuine transitions carry distinct+ # timestamps so the latest-wins columns have one unambiguous winner.+ statuses = ("resolved", "suppressed", "failed", "potential")+ for offset, status in enumerate(statuses):+ self._transition(T2 + datetime.timedelta(hours=offset), "ready", status)+ genuine_only = self._status_row()++ for status in statuses:+ self._transition(T1, "ready", status, "analysis_wrong", team_id=999)++ assert self._status_row() == genuine_onlydef test_tied_tenants_count_and_report_the_same_team(self):# Two tenants' buckets with the same last timestamp: whichever wins the tie, the count and