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(autoresearch): select the latest prediction batch by prediction date (#110068)

posthog[bot]ceeb956efb37
diff --git a/products/autoresearch/backend/inference/scoring.py b/products/autoresearch/backend/inference/scoring.py
index 0fec1ceed245..2ebb280a0f82 100644
--- a/products/autoresearch/backend/inference/scoring.py
+++ b/products/autoresearch/backend/inference/scoring.py
@@ -23,6 +23,7 @@ Event shape:
$autoresearch_prediction_date: str (YYYY-MM-DD)
$autoresearch_features_hash: str (SHA-256 prefix of the feature row)
$autoresearch_person_id: str (the person_id every row is keyed on)
+ $autoresearch_run_id: str (UUID of the AutoresearchRun that emitted the batch)
"""
import json
@@ -224,7 +225,7 @@ def run_inference_for_pipeline(
acting_user = _acting_user(team=team, pipeline=pipeline, user=user)
scored = score_population(team=team, pipeline=pipeline, model=model, window=window, user=acting_user)
emitted = _emit_predictions(
- team=team, pipeline=pipeline, model=model, scored=scored, window=window, user=acting_user
+ team=team, pipeline=pipeline, model=model, run=run, scored=scored, window=window, user=acting_user
)
run.status = AutoresearchRun.Status.COMPLETED
@@ -380,6 +381,7 @@ def _emit_predictions(
team: Team,
pipeline: AutoresearchPipeline,
model: AutoresearchModel,
+ run: AutoresearchRun,
scored: ScoredPopulation,
window: ScoringWindow,
user: User,
@@ -435,6 +437,7 @@ def _emit_predictions(
"$autoresearch_prediction_date": prediction_date_str,
"$autoresearch_features_hash": features_hash,
"$autoresearch_person_id": person_id,
+ "$autoresearch_run_id": str(run.pk),
}
if attach_to_person and output_property:
props["$set"] = {output_property: row["p_y"]}
diff --git a/products/autoresearch/backend/inference/test_inference.py b/products/autoresearch/backend/inference/test_inference.py
index 2ec96cc96a33..fa2e5c6d616c 100644
--- a/products/autoresearch/backend/inference/test_inference.py
+++ b/products/autoresearch/backend/inference/test_inference.py
@@ -143,6 +143,8 @@ class TestRunInferencePipeline(TeamScopedTestMixin, BaseTest):
assert kwargs["process_person_profile"] is True
assert [e["event"] for e in events] == [PREDICTION_EVENT_NAME, PREDICTION_EVENT_NAME]
by_person = {e["properties"]["$autoresearch_person_id"]: e for e in events}
+ assert {e["properties"]["$autoresearch_prediction_date"] for e in events} == {run.metrics["prediction_date"]}
+ assert {e["properties"]["$autoresearch_run_id"] for e in events} == {str(run.pk)}
# A resolved person is attached to their real distinct_id and gets the output property;
# an unresolved one stays person-less so a UUID never becomes a person.
assert by_person["user-1"]["distinct_id"] == "real-distinct-id"
diff --git a/products/autoresearch/frontend/autoresearchPipelineLogic.ts b/products/autoresearch/frontend/autoresearchPipelineLogic.ts
index 00ec8b4ee211..d21d1d120349 100644
--- a/products/autoresearch/frontend/autoresearchPipelineLogic.ts
+++ b/products/autoresearch/frontend/autoresearchPipelineLogic.ts
@@ -809,7 +809,7 @@ export const autoresearchPipelineLogic = kea<autoresearchPipelineLogicType>([
null as ProbabilityBucket[] | null,
{
loadProbabilityDistribution: async () => {
- // A scoring run stamps its whole batch with one timestamp, so the latest batch is its exact timestamp.
+ // Capture can shift the timestamps of one batch apart, so the latest batch is its prediction date.
const response = await api.queryHogQL(
hogql`
SELECT least(floor(p * 10), 9) AS bucket, count() AS users
@@ -820,8 +820,9 @@ export const autoresearchPipelineLogic = kea<autoresearchPipelineLogicType>([
FROM events
WHERE event = 'autoresearch_prediction'
AND properties.$autoresearch_pipeline_id = ${props.id}
- AND timestamp = (
- SELECT max(timestamp)
+ AND timestamp >= now() - INTERVAL ${LATEST_BATCH_LOOKBACK_DAYS} DAY
+ AND properties.$autoresearch_prediction_date = (
+ SELECT max(properties.$autoresearch_prediction_date)
FROM events
WHERE event = 'autoresearch_prediction'
AND properties.$autoresearch_pipeline_id = ${props.id}
diff --git a/products/autoresearch/frontend/pipeline/ProbabilityUsersTable.tsx b/products/autoresearch/frontend/pipeline/ProbabilityUsersTable.tsx
index 997be317b936..276ecc0b445f 100644
--- a/products/autoresearch/frontend/pipeline/ProbabilityUsersTable.tsx
+++ b/products/autoresearch/frontend/pipeline/ProbabilityUsersTable.tsx
@@ -45,9 +45,9 @@ export function ProbabilityUsersTable({
source: {
kind: NodeKind.HogQLQuery,
query: `
- -- A scoring run stamps its whole batch with one timestamp, so the latest batch is its exact timestamp.
+ -- Capture can shift the timestamps of one batch apart, so the latest batch is its prediction date.
WITH latest AS (
- SELECT max(timestamp) AS t
+ SELECT max(properties.$autoresearch_prediction_date) AS prediction_date
FROM events
WHERE event = 'autoresearch_prediction'
AND properties.$autoresearch_pipeline_id = {pipeline_id}
@@ -61,7 +61,8 @@ export function ProbabilityUsersTable({
FROM events
WHERE event = 'autoresearch_prediction'
AND properties.$autoresearch_pipeline_id = {pipeline_id}
- AND timestamp = (SELECT t FROM latest)
+ AND timestamp >= now() - INTERVAL ${LATEST_BATCH_LOOKBACK_DAYS} DAY
+ AND properties.$autoresearch_prediction_date = (SELECT prediction_date FROM latest)
GROUP BY person_id
)
-- LEFT JOIN persons directly on the person UUID: the implicit person join goes via