: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): resolve scout finding→report links (HogQL alias collision) (#63983)
Andrew Maguire7c102da0349b
diff --git a/products/signals/backend/temporal/signal_queries.py b/products/signals/backend/temporal/signal_queries.pyindex dfe964747a6d..50fba598c5a6 100644--- a/products/signals/backend/temporal/signal_queries.py+++ b/products/signals/backend/temporal/signal_queries.py@@ -54,7 +54,33 @@ def _deduped_signals_subquery(*, include_embedding: bool = False, extra_where: sselected_columns.append("argMax(timestamp, inserted_at) as timestamp")selected_columns_sql = ",\n ".join(selected_columns)- extra_where_clause = f"\n AND {extra_where}" if extra_where else ""+ if extra_where:+ # `extra_where` filters on the raw `metadata` JSON, but this SELECT also exposes+ # `metadata` as an `argMax(...)` alias. HogQL resolves the name in WHERE to that+ # aggregate alias and rejects the query ("aggregate function ... found in WHERE"),+ # so any caller that filtered on `metadata` silently failed. Apply the predicate in+ # a non-aggregating inner scan so it binds to the raw column, then dedupe in the+ # outer aggregate. Pushing the filter down here (vs. the caller's outer query) keeps+ # the dedup scan bounded to the matching rows.+ raw_columns = ["document_id", "content", "metadata"]+ if include_embedding:+ raw_columns.append("embedding")+ raw_columns.extend(["inserted_at", "timestamp"])+ raw_columns_sql = ",\n ".join(raw_columns)+ return f"""+ SELECT+ {selected_columns_sql}+ FROM (+ SELECT+ {raw_columns_sql}+ FROM document_embeddings+ WHERE model_name = {{model_name}}+ AND product = 'signals'+ AND document_type = 'signal'+ AND {extra_where}+ )+ GROUP BY document_id+ """return f"""SELECT@@ -62,7 +88,7 @@ def _deduped_signals_subquery(*, include_embedding: bool = False, extra_where: sFROM document_embeddingsWHERE model_name = {{model_name}}AND product = 'signals'- AND document_type = 'signal'{extra_where_clause}+ AND document_type = 'signal'GROUP BY document_id"""diff --git a/products/signals/backend/test/test_scout_harness_api.py b/products/signals/backend/test/test_scout_harness_api.pyindex af2a7cc4ff48..a7719ce2378a 100644--- a/products/signals/backend/test/test_scout_harness_api.py+++ b/products/signals/backend/test/test_scout_harness_api.py@@ -25,6 +25,7 @@ from products.signals.backend.models import ()from products.signals.backend.scout_harness.lazy_seed import HARNESS_SEEDED_BYfrom products.signals.backend.scout_harness.tools.profile import compute_project_profile+from products.signals.backend.temporal.signal_queries import fetch_report_ids_for_source_idsfrom products.skills.backend.models.skills import LLMSkillfrom products.tasks.backend.models import Task, TaskRun@@ -322,6 +323,19 @@ class TestScoutHarnessEmissionReportsAPI(APIBaseTest):assert response.status_code == status.HTTP_200_OKassert response.json()[0]["report"] is None+ def test_report_lookup_query_compiles_without_alias_collision(self) -> None:+ # Regression: the lookup pushes the `source_id` filter into the shared dedup subquery,+ # which exposes `metadata` as an `argMax(...)` alias. HogQL resolved the `metadata`+ # reference in that pushed-down WHERE to the aggregate alias and rejected the whole+ # query ("aggregate function ... found in WHERE"), so `fetch_report_ids_for_source_ids`+ # raised on every call. The view swallowed it, degrading *every* finding to+ # `report: null` — the chip never rendered for anyone. The other tests in this class+ # mock the helper, so they never exercised the query and the break shipped silently.+ # Run the real query (no mock) against an empty ClickHouse: it must compile and return+ # an empty map rather than raise.+ result = fetch_report_ids_for_source_ids(self.team, ["run:00000000-0000-0000-0000-000000000000:finding:f-a"])+ assert result == {}+def test_empty_run_returns_empty_and_skips_clickhouse(self) -> None:run = _make_run(self.team)with patch(_FETCH_REPORT_IDS) as mock_fetch: