: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(llma): use toString zero-UUID guard in generation sampling (#60632)
Andrew Maguireb169dbb40367
diff --git a/posthog/temporal/ai_observability/trace_summarization/sampling.py b/posthog/temporal/ai_observability/trace_summarization/sampling.pyindex 24c15c6894d3..db8f974cbdea 100644--- a/posthog/temporal/ai_observability/trace_summarization/sampling.py+++ b/posthog/temporal/ai_observability/trace_summarization/sampling.py@@ -144,8 +144,8 @@ async def sample_items_in_window_activity(inputs: BatchSummarizationInputs) -> lAND timestamp < toDateTime({end_ts}, 'UTC')AND properties.$ai_trace_id != ''GROUP BY trace_id- -- argMaxIf returns the zero UUID (not NULL) when no rows match- HAVING last_generation_id != toUUIDOrZero('')+ -- argMaxIf returns the zero UUID (not NULL) with no matches; toUUIDOrZero isn't in the HogQL allowlist.+ HAVING last_generation_id != toUUID('00000000-0000-0000-0000-000000000000')AND event_count <= {max_events}AND total_properties_size <= {max_properties_size}ORDER BY trace_first_timestamp DESCdiff --git a/posthog/temporal/ai_observability/trace_summarization/tests/test_workflow.py b/posthog/temporal/ai_observability/trace_summarization/tests/test_workflow.pyindex 3ee54c4dd811..7e9f39253b7c 100644--- a/posthog/temporal/ai_observability/trace_summarization/tests/test_workflow.py+++ b/posthog/temporal/ai_observability/trace_summarization/tests/test_workflow.py@@ -5,6 +5,11 @@ from contextlib import asynccontextmanagerimport pytestfrom unittest.mock import patch+from posthog.hogql.context import HogQLContext+from posthog.hogql.placeholders import replace_placeholders+from posthog.hogql.printer.utils import prepare_and_print_ast++from posthog.sync import database_sync_to_asyncfrom posthog.temporal.ai_observability.trace_summarization.models import BatchSummarizationInputs, SampledItemfrom posthog.temporal.ai_observability.trace_summarization.sampling import sample_items_in_window_activityfrom posthog.temporal.ai_observability.trace_summarization.workflow import BatchTraceSummarizationWorkflow@@ -217,10 +222,37 @@ class TestSampleItemsInWindowActivity:# argMaxIf with no matching rows returns the zero UUID (not NULL), so# HAVING must explicitly compare against it — IS NOT NULL doesn't catch it.- zero_uuid_guard = CallCollector("toUUIDOrZero")+ # toUUIDOrZero is not whitelisted; toUUID('...') is (constant-folded UUID compare).assert query.having is not None- zero_uuid_guard.visit(query.having)- assert zero_uuid_guard.found, "HAVING must guard against zero-UUID phantom generations"+ to_uuid_guard = CallCollector("toUUID")+ to_uuid_guard.visit(query.having)+ assert to_uuid_guard.found, "HAVING must guard against zero-UUID phantom generations"++ class ConstantFinder(TraversingVisitor):+ def __init__(self, value: str) -> None:+ self.value = value+ self.found = False++ def visit_constant(self, node: ast.Constant) -> None:+ if node.value == self.value:+ self.found = True++ zero_uuid_literal = ConstantFinder("00000000-0000-0000-0000-000000000000")+ zero_uuid_literal.visit(query.having)+ assert zero_uuid_literal.found, "HAVING must compare against the literal zero UUID"++ # Validate the query against the real HogQL printer — every function call+ # must be in the allowlist. Without this, a regression like toUUIDOrZero+ # (rejected at runtime as `QueryError: Unsupported function call`) ships.+ # Wrapped in database_sync_to_async because the printer loads the team+ # via the Django ORM, which is blocked from this async test context.+ placeholders = mock_execute.call_args.kwargs["placeholders"]+ materialized = replace_placeholders(query, placeholders)+ await database_sync_to_async(prepare_and_print_ast, thread_sensitive=False)(+ materialized,+ HogQLContext(team_id=mock_team.id, enable_select_queries=True),+ "clickhouse",+ )@pytest.mark.django_db(transaction=True)@pytest.mark.asyncio