: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
"""The scoring sweep: a scheduled Temporal workflow that scores the inbox reports that are due.
A report is due when its score is missing, older than its vector, or from another served model. The
served model reads only the report's vector, so the vector log is the change signal, not
`SignalReport.updated_at`: a scout edit saves `update_fields=["title", "summary"]` and leaves
`updated_at` alone, and the grouping pipeline moves `updated_at` with no text change. A new vector
lands only when a rendering's text changes.
A new manifest alone does not make a report due. Training publishes one every night because the
challenger models change, and a rescore of the whole window for each one makes new reports wait.
A challenger scores only the reports that become due after a manifest names it.
Nothing reads the `ranking_score` artefacts to order the inbox yet.
"""
import sys
import time
import uuid
import datetime
import resource
from collections import defaultdict
from collections.abc import Iterator, Mapping, Sequence
from typing import Any, cast
from django.conf import settings
from django.utils import timezone
import pydantic
import structlog
from temporalio import activity, workflow
from temporalio.common import RetryPolicy
from posthog.clickhouse.client import sync_execute
from posthog.clickhouse.client.connection import ClickHouseUser
from posthog.clickhouse.query_tagging import Feature, Product, tag_queries
from posthog.clickhouse.workload import Workload
from posthog.dataclasses import frozen
from posthog.ph_client import ph_scoped_capture
from posthog.sync import database_sync_to_async
from posthog.temporal.common.scoped import scoped_temporal
from posthog.temporal.common.utils import close_db_connections
from products.signals.backend.models import SignalReport, SignalReportArtefact
from products.signals.backend.ranking import scorer
from products.signals.backend.ranking.inventory import SCORABLE_STATUSES, spine_report_filter
from products.signals.backend.ranking.model_store import ModelLoadError, ServingSet, load_serving_set
from products.signals.backend.ranking.overrides import RankingOverrides, read_ranking_overrides
from products.signals.backend.ranking.serving_manifest import serving_project_vectors_date
from products.signals.backend.report_embedding_reader import REPORT_EMBEDDINGS_TABLE
from products.signals.backend.report_embeddings import EMBEDDING_DOCUMENT_TYPE, EMBEDDING_PRODUCT
logger = structlog.get_logger(__name__)
SCORING_WORKFLOW_NAME = "inbox-ranking-scoring-sweep"
# Report ids per Postgres lookup, so a large vector feed does not become one huge `IN` list.
_POSTGRES_BATCH_SIZE = 5000
_ACTIVITY_TIMEOUT = datetime.timedelta(minutes=10)
# The activity timeout does not stop the scoring thread, and the schedule's SKIP stops guarding it
# when the workflow closes. The pass starts no new team after this budget, which is below the
# timeout, so only the team in progress can run past it and into the next tick.
_TIME_BUDGET = datetime.timedelta(minutes=8)
# `timestamp` is the report's `created_at` and the partition key, so the lower bound prunes the
# read to a few weekly partitions. The query does not read the `embedding` column.
CANDIDATE_VECTORS_SQL = f"""
SELECT
team_id,
document_id,
max(inserted_at) AS vector_inserted_at
FROM {REPORT_EMBEDDINGS_TABLE}
WHERE product = %(product)s
AND document_type = %(document_type)s
AND rendering = %(rendering)s
AND timestamp >= %(since)s
GROUP BY team_id, document_id
HAVING argMax(JSONExtractBool(metadata, 'deleted'), inserted_at) = 0
"""
# The scan is cross-team, so it cannot use the `team_id` sort-key prefix, and its cost grows with the
# whole window. The time cap sits well inside `_TIME_BUDGET`, so a slow scan fails the tick with an
# error instead of running into the activity timeout. It throws because a partial GROUP BY would
# give a wrong newest-vector time.
CANDIDATE_VECTORS_QUERY_SETTINGS: dict[str, int | str] = {
"max_execution_time": 180,
"timeout_overflow_mode": "throw",
}
@frozen
class ScoringCandidate:
team_id: int
report_id: str
vector_inserted_at: datetime.datetime
@frozen
class ScoreInboxReportsInput:
# A manual run can override the per-tick cap.
limit: int | None = None
@frozen
class ScoreInboxReportsResult:
candidates: int = 0
scored: int = 0
no_vector: int = 0
teams: int = 0
failed_teams: int = 0
# Teams with reports not started because the time budget ran out. The next tick scores them.
deferred_teams: int = 0
deferred_reports: int = 0
manifest_version: str | None = None
# "disabled" or "no manifest". None when the pass ran.
skipped_reason: str | None = None
class _ScoreStamp(pydantic.BaseModel):
"""The fields of a `ranking_score` artefact that decide if the report is due.
The full `RankingScore` copies each model's metadata onto every result, so it is tens of KB per
report in memory. The sweep reads the latest score of every scored report in the window, so a
full parse grows with the number of scored reports until it exhausts the worker.
"""
scored_at: datetime.datetime
embedding_inserted_at: datetime.datetime | None = None
served_key: str
def _batches(items: Sequence[str], size: int = _POSTGRES_BATCH_SIZE) -> Iterator[Sequence[str]]:
for start in range(0, len(items), size):
yield items[start : start + size]
def _peak_rss_mb() -> int:
# The peak of the whole worker process, which other activities share, not of this pass only.
peak = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
return peak // (1024 * 1024) if sys.platform == "darwin" else peak // 1024
def _utc(value: datetime.datetime) -> datetime.datetime:
# ClickHouse returns a naive DateTime in UTC.
return value.replace(tzinfo=datetime.UTC) if value.tzinfo is None else value.astimezone(datetime.UTC)
def _live_vectors(since: datetime.datetime, rendering: str) -> dict[str, tuple[int, datetime.datetime]]:
"""Report id to `(team_id, newest vector time)`, for each report whose newest row is live."""
tag_queries(product=Product.SIGNALS, feature=Feature.ENRICHMENT, query_type="inbox_ranking_sweep_candidates")
rows = cast(
list[tuple[Any, ...]],
sync_execute(
CANDIDATE_VECTORS_SQL,
{
"product": EMBEDDING_PRODUCT,
"document_type": EMBEDDING_DOCUMENT_TYPE,
"rendering": rendering,
"since": since.astimezone(datetime.UTC).replace(tzinfo=None),
},
settings=CANDIDATE_VECTORS_QUERY_SETTINGS,
workload=Workload.OFFLINE,
ch_user=ClickHouseUser.SIGNALS,
)
or [],
)
vectors: dict[str, tuple[int, datetime.datetime]] = {}
for team_id, document_id, inserted_at in rows:
try:
report_id = str(uuid.UUID(str(document_id)))
except ValueError:
# A document id that is not a report UUID would fail the Postgres lookup.
continue
vectors[report_id] = (int(team_id), _utc(inserted_at))
return vectors
def _scorable_report_teams(
report_ids: Sequence[str], now: datetime.datetime, since: datetime.datetime
) -> dict[str, int]:
teams: dict[str, int] = {}
for batch in _batches(report_ids):
rows = (
# nosemgrep: idor-lookup-without-team (system sweep with no user input; report teams are checked against vector teams before scoring)
SignalReport.objects.filter(spine_report_filter(now))
.filter(id__in=batch, status__in=SCORABLE_STATUSES, created_at__gte=since)
.values_list("id", "team_id")
)
teams.update({str(report_id): team_id for report_id, team_id in rows})
return teams
def _latest_scores(report_ids_by_team: Mapping[int, Sequence[str]]) -> dict[str, _ScoreStamp | None]:
"""The latest `ranking_score` stamp of each report that has one. A row that no longer parses is None."""
scores: dict[str, _ScoreStamp | None] = {}
for team_id, report_ids in report_ids_by_team.items():
for batch in _batches(report_ids):
# Pick the newest id per report first, so Postgres reads `content` once per report, not once per score.
latest_ids = (
SignalReportArtefact.objects.filter(
team_id=team_id, report_id__in=batch, type=SignalReportArtefact.ArtefactType.RANKING_SCORE
)
.order_by("report_id", "-created_at")
.distinct("report_id")
.values("id")
)
rows = SignalReportArtefact.objects.filter(team_id=team_id, id__in=latest_ids).values_list(
"report_id", "content"
)
for report_id, content in rows:
try:
scores[str(report_id)] = _ScoreStamp.model_validate_json(content)
except pydantic.ValidationError:
scores[str(report_id)] = None
return scores
def _is_due(score: _ScoreStamp | None, vector_inserted_at: datetime.datetime, served_key: str) -> bool:
# A promotion or a `served` override changes the served key, so each one rescores every report.
return (
score is None
or score.embedding_inserted_at is None
or vector_inserted_at > score.embedding_inserted_at
or score.served_key != served_key
)
def reports_due_for_scoring(
now: datetime.datetime, *, served_key: str, rendering: str, limit: int
) -> list[ScoringCandidate]:
"""In-window scorable reports whose score is missing, older than their vector, or from another served model.
Unscored reports come first, then the oldest scores, so no report starves under the cap. A
report with no live vector is never a candidate, so a safety-suppressed or retracted report is
not retried every tick. It comes back when a new vector lands.
"""
since = now - datetime.timedelta(days=settings.INBOX_RANKING_SCORING_MAX_AGE_DAYS)
vectors = _live_vectors(since, rendering)
report_teams = _scorable_report_teams(list(vectors), now, since)
kept = [report_id for report_id, team_id in report_teams.items() if vectors[report_id][0] == team_id]
kept_by_team: dict[int, list[str]] = defaultdict(list)
for report_id in kept:
kept_by_team[report_teams[report_id]].append(report_id)
latest = _latest_scores(kept_by_team)
due: list[tuple[datetime.datetime | None, ScoringCandidate]] = []
for report_id in kept:
team_id, vector_inserted_at = vectors[report_id]
score = latest.get(report_id)
if not _is_due(score, vector_inserted_at, served_key):
continue
candidate = ScoringCandidate(team_id=team_id, report_id=report_id, vector_inserted_at=vector_inserted_at)
due.append((score.scored_at if score else None, candidate))
due.sort(key=lambda item: (item[0] is not None, item[0] or now, item[1].report_id))
return [candidate for _, candidate in due[:limit]]
def _load_serving(overrides: RankingOverrides) -> ServingSet | None:
"""The serving set with the `served` override applied when it can be served, else without it."""
serving = load_serving_set(overrides)
if serving is None or serving.served_override is None:
return serving
try:
scorer.served_rendering(serving)
except scorer.ScoringError as error:
logger.warning(
"inbox_ranking_override_rejected",
override_served=serving.served_override.served,
reason=str(error),
)
return load_serving_set()
return serving
def score_inbox_reports(limit: int | None = None) -> ScoreInboxReportsResult:
if not settings.INBOX_RANKING_SCORING_ENABLED:
logger.info("inbox_ranking_sweep_skipped", skipped_reason="disabled")
return ScoreInboxReportsResult(skipped_reason="disabled")
deadline = time.monotonic() + _TIME_BUDGET.total_seconds()
now = timezone.now()
# A served model that does not load raises here and aborts the run. A served override that
# does not load only logs.
serving = _load_serving(read_ranking_overrides(now))
if serving is None:
logger.info("inbox_ranking_sweep_skipped", skipped_reason="no manifest")
return ScoreInboxReportsResult(skipped_reason="no manifest")
manifest_version = serving.manifest.manifest_version
override = serving.served_override
candidates = reports_due_for_scoring(
now,
served_key=serving.manifest.served.key,
rendering=scorer.served_rendering(serving),
limit=limit or settings.INBOX_RANKING_SCORING_MAX_REPORTS_PER_TICK,
)
ids_by_team: dict[int, list[str]] = defaultdict(list)
for candidate in candidates:
ids_by_team[candidate.team_id].append(candidate.report_id)
# A pass that the worker kills logs no finish, so the start log is the only trace of it.
logger.info(
"inbox_ranking_sweep_started",
candidates=len(candidates),
teams=len(ids_by_team),
manifest_version=manifest_version,
peak_rss_mb=_peak_rss_mb(),
)
project_vectors = scorer.pass_project_vectors(serving, list(ids_by_team))
# A scorer call holds the vectors and matrices of all its reports, so one call scores at most one
# vector read of one team. Each call persists its scores, so a pass that stops keeps its progress.
chunks = [
(team_id, batch)
for team_id, report_ids in ids_by_team.items()
for batch in _batches(report_ids, settings.INBOX_RANKING_SCORING_BATCH_SIZE)
]
scored = no_vector = deferred_reports = 0
failed: set[int] = set()
deferred: set[int] = set()
with ph_scoped_capture() as capture:
for team_id, report_ids in chunks:
if time.monotonic() >= deadline:
deferred.add(team_id)
deferred_reports += len(report_ids)
continue
try:
outcomes = scorer.score_reports(
team_id,
list(report_ids),
persist=True,
now=now,
serving=serving,
capture=capture,
project_vectors=project_vectors,
)
except (scorer.ScoringError, ModelLoadError):
# A pass without a served score is worse than no pass.
raise
except Exception:
logger.exception("inbox_ranking_sweep_team_failed", team_id=team_id)
failed.add(team_id)
continue
finally:
# Send the events of each call when it ends, so a killed pass keeps the events of the scores it persisted.
capture.flush()
scored += sum(1 for outcome in outcomes if outcome.score is not None)
no_vector += sum(1 for outcome in outcomes if outcome.reason == scorer.NO_VECTOR)
# The day the pass read, so a stale mirror region day shows in the log.
vectors_date = (
serving_project_vectors_date(serving.manifest.project_vectors)
if project_vectors is not None and project_vectors.available and serving.manifest.project_vectors
else None
)
result = ScoreInboxReportsResult(
candidates=len(candidates),
scored=scored,
no_vector=no_vector,
teams=len(ids_by_team),
failed_teams=len(failed),
deferred_teams=len(deferred),
deferred_reports=deferred_reports,
manifest_version=manifest_version,
)
logger.info(
"inbox_ranking_sweep_finished",
candidates=result.candidates,
scored=result.scored,
no_vector=result.no_vector,
teams=result.teams,
failed_teams=result.failed_teams,
deferred_teams=result.deferred_teams,
deferred_reports=result.deferred_reports,
manifest_version=result.manifest_version,
project_vectors_date=vectors_date.isoformat() if vectors_date else None,
project_vectors_age_days=(now.date() - vectors_date).days if vectors_date else None,
override_served=override.served if override else None,
override_expires_at=override.expires_at.isoformat() if override and override.expires_at else None,
peak_rss_mb=_peak_rss_mb(),
)
return result
@activity.defn
@scoped_temporal()
@close_db_connections
async def score_inbox_reports_activity(input: ScoreInboxReportsInput) -> ScoreInboxReportsResult:
return await database_sync_to_async(score_inbox_reports, thread_sensitive=False)(input.limit)
@workflow.defn(name=SCORING_WORKFLOW_NAME)
class InboxRankingScoringWorkflow:
@workflow.run
async def run(self, inputs: ScoreInboxReportsInput) -> ScoreInboxReportsResult:
return await workflow.execute_activity(
score_inbox_reports_activity,
inputs,
start_to_close_timeout=_ACTIVITY_TIMEOUT,
# No retry: a timed-out attempt keeps running in its thread, so a second attempt could
# score the same batch at the same time. The next tick finds what this one left,
# because the vector comparison finds it again.
retry_policy=RetryPolicy(maximum_attempts=1),
)