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(aio): hold generation backfill windows back from ai_events lag (#107603)

posthog[bot]d6225edecf52
diff --git a/products/ai_observability/backend/api/evaluation_backfills.py b/products/ai_observability/backend/api/evaluation_backfills.py
index fa514c061875..b9fc508b3f9f 100644
--- a/products/ai_observability/backend/api/evaluation_backfills.py
+++ b/products/ai_observability/backend/api/evaluation_backfills.py
@@ -330,7 +330,10 @@ class EvaluationBackfillViewSet(
def _clamped_window(self, evaluation: Evaluation, data: dict[str, Any]) -> BackfillWindow:
"""The requested window, bounded to the span whose verdicts can be read back."""
now = timezone.now()
- window_end: datetime = min(data["window_end"], now)
+ # Candidates come from `events`, but each generation is read back from `ai_events`, which a
+ # separate pipeline fills later. A generation that has not reached `ai_events` yet fails its
+ # run for good, so the window stops short of the newest events.
+ window_end: datetime = min(data["window_end"], now - timedelta(seconds=INGESTION_LAG_MARGIN_SECONDS))
settle_hold = settle_horizon(evaluation.target, evaluation.target_config)
if settle_hold:
# A trace or session is graded over `settle_hold` from its first event, so a unit any
diff --git a/products/ai_observability/backend/api/test/test_evaluation_backfills.py b/products/ai_observability/backend/api/test/test_evaluation_backfills.py
index b942957f3a10..4d99b4e19ffd 100644
--- a/products/ai_observability/backend/api/test/test_evaluation_backfills.py
+++ b/products/ai_observability/backend/api/test/test_evaluation_backfills.py
@@ -26,6 +26,7 @@ from posthog.rate_limit import (
AIObservabilityBackfillEstimateSustainedThrottle,
AIObservabilityBackfillEstimateThrottle,
)
+from posthog.temporal.ai_observability.run_aggregate_evaluation import INGESTION_LAG_MARGIN_SECONDS
from posthog.temporal.ai_observability.run_session_evaluation import AI_EVENTS_RETENTION_DAYS
from products.access_control.backend.models.access_control import AccessControl
@@ -374,14 +375,15 @@ class TestEvaluationBackfillsApi(APIBaseTest):
estimate = self.client.post(f"{self.url}/estimate/", body, format="json")
assert estimate.status_code == status.HTTP_200_OK, estimate.json()
- assert abs(datetime.fromisoformat(estimate.json()["window_end"]) - now) < timedelta(seconds=5)
+ expected_end = now - timedelta(seconds=INGESTION_LAG_MARGIN_SECONDS)
+ assert abs(datetime.fromisoformat(estimate.json()["window_end"]) - expected_end) < timedelta(seconds=5)
expected_start = now - timedelta(days=AI_EVENTS_RETENTION_DAYS)
assert abs(datetime.fromisoformat(estimate.json()["window_start"]) - expected_start) < timedelta(seconds=5)
created = self.client.post(f"{self.url}/", body, format="json")
assert created.status_code == status.HTTP_201_CREATED, created.json()
row = EvaluationBackfill.objects.unscoped().get(pk=created.json()["id"])
- assert abs(row.window_end - now) < timedelta(seconds=5)
+ assert abs(row.window_end - expected_end) < timedelta(seconds=5)
assert abs(row.window_start - expected_start) < timedelta(seconds=5)
@parameterized.expand(
@@ -419,14 +421,16 @@ class TestEvaluationBackfillsApi(APIBaseTest):
@patch(f"{API_MODULE}.count_backfill_candidates", return_value=_scope(3))
@patch(f"{API_MODULE}.sync_connect")
- def test_a_generation_backfill_reaches_up_to_now(self, connect, _count):
+ def test_a_generation_backfill_stops_short_of_the_ai_events_lag(self, connect, _count):
connect.return_value = _temporal_client()
- now = timezone.now()
+ margin = timedelta(seconds=INGESTION_LAG_MARGIN_SECONDS)
+ before = timezone.now()
estimate = self.client.post(f"{self.url}/estimate/", _body(), format="json")
+ after = timezone.now()
assert estimate.status_code == status.HTTP_200_OK, estimate.json()
- assert abs(datetime.fromisoformat(estimate.json()["window_end"]) - now) < timedelta(seconds=30)
+ assert before - margin <= datetime.fromisoformat(estimate.json()["window_end"]) <= after - margin
@patch(f"{API_MODULE}.count_backfill_candidates", return_value=_scope(3))
@patch(f"{API_MODULE}.sync_connect")