: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
"""Build one person's ranked inbox list with one LLM call, store it and record one event.
The call goes through `build_openai_client`, as the activity summary does. Any failure of the call or
of its answer gives the list in model order with a `fallback_reason`, so a build never raises because
of the model. A person with no ready activity summary gets the list in model order with no call, because
the model has nothing to personalize it with.
"""
import json
import time
import hashlib
import datetime
import dataclasses
from typing import Any, Literal
from django.utils import timezone
import structlog
from openai import APITimeoutError, BadRequestError, Omit, OpenAI, omit
from openai.types.chat import ChatCompletion, ChatCompletionMessageParam
from posthog.dataclasses import frozen
from posthog.llm.gateway_client import build_openai_client, trace_id_from_seed
from posthog.models import Team, User
from posthog.ph_client import ph_scoped_capture
from products.signals.backend.activity_summary.read import ActivitySummaryStatus, get_summary
from products.signals.backend.models import PersonalInboxRanking
from products.signals.backend.personal_ranking.candidates import Candidate, gather_candidates
from products.signals.backend.personal_ranking.config import PersonalRankingConfig, ReasoningEffort, read_config
from products.signals.backend.personal_ranking.prompt import (
PROMPT_VERSION,
RESPONSE_FORMAT,
TIERS,
render_person,
system_prompt,
user_message,
)
from products.signals.backend.personal_ranking.validation import (
FoldedEntry,
MalformedRankingError,
RankedEntry,
ValidationCounts,
cap,
model_order,
validate_ranking,
)
logger = structlog.get_logger(__name__)
# (model, effort) pairs the API rejected in this process. Later calls with that pair leave the effort out.
_REJECTED_REASONING_EFFORTS: set[tuple[str, str]] = set()
AI_PRODUCT = "personal_inbox_ranking"
# Not a person: the gateway attributes every call to the pipeline.
PIPELINE_DISTINCT_ID = "signals_personal_inbox_ranking"
BUILT_EVENT = "personal inbox ranking built"
TRUNCATED_FINISH_REASON = "length"
# The most old rows one build deletes. Each build deletes again, so a larger backlog shrinks over later builds.
PRUNE_BATCH_SIZE = 100
FALLBACK_LLM_ERROR = "llm_error"
FALLBACK_TIMEOUT = "timeout"
FALLBACK_MALFORMED = "malformed_output"
FALLBACK_TRUNCATED = "truncated_output"
FALLBACK_NO_CANDIDATES = "no_candidates"
FALLBACK_AI_NOT_APPROVED = "ai_not_approved"
FALLBACK_NO_ACTIVITY_SUMMARY = "no_activity_summary"
# A later build with the same input hash can rank the list. AI approval is not part of the hash.
RETRYABLE_FALLBACK_REASONS = frozenset(
{FALLBACK_LLM_ERROR, FALLBACK_TIMEOUT, FALLBACK_MALFORMED, FALLBACK_TRUNCATED, FALLBACK_AI_NOT_APPROVED}
)
# The input parts that `input_parts` hashes one by one. The report parts have one hash for each candidate.
REPORT_PARTS = ("text", "status", "priority", "pull_request_state", "needs_you", "scores")
INPUT_PARTS = ("candidate_set", *REPORT_PARTS, "activity_summary_version", "prompt_version", "focus")
# The person had no list before this build.
CHANGED_NO_PREVIOUS = "no_previous_list"
# The previous list has no part hashes, so the build cannot tell which part changed.
CHANGED_UNKNOWN = "unknown"
_PART_HASH_CHARS = 16
BuildStatus = Literal["ok", "fallback", "empty", "disabled"]
# What started a build, on the build event and the trace, so cost can be split by it.
BuildTrigger = Literal["on_demand", "tick"]
_MODEL_ORDER_REASON = "Kept in inbox order."
_NEEDS_YOU_REASONS = {
"waiting_on_input": "Waiting on your input.",
"claimed": "You claimed this report.",
"suggested_reviewer": "You're a suggested reviewer.",
}
@frozen
class _ModelCall:
content: str | None
tokens_in: int | None
tokens_out: int | None
latency_ms: int
fallback_reason: str | None
@frozen
class InputChanges:
"""How the inputs of a build differ from the inputs of the person's previous list."""
# Names from `INPUT_PARTS`, or only `CHANGED_NO_PREVIOUS` or `CHANGED_UNKNOWN`.
changed_parts: tuple[str, ...]
# The reports in both lists with a changed report part. Null when the parts are not comparable.
changed_report_count: int | None = None
# The largest probability move of a report in both lists. Null when no probability is in both lists.
max_score_move: float | None = None
joined_count: int | None = None
left_count: int | None = None
# The reports that left and were in the first `rank_top` entries of the previous list.
left_top_count: int | None = None
def properties(self) -> dict[str, Any]:
return {
"changed_parts": list(self.changed_parts),
"changed_report_count": self.changed_report_count,
"max_score_move": self.max_score_move,
"candidates_joined": self.joined_count,
"candidates_left": self.left_count,
"candidates_left_top": self.left_top_count,
}
@frozen
class PersonalRankingBuild:
status: BuildStatus
personalized: bool
fallback_reason: str | None
entries: tuple[RankedEntry, ...]
folded: tuple[FoldedEntry, ...]
candidates: tuple[Candidate, ...]
counts: ValidationCounts
limit: int
input_hash: str | None
activity_summary_version: int | None
model: str
model_called: bool = False
tokens_in: int | None = None
tokens_out: int | None = None
latency_ms: int | None = None
# None for a dry run or a disabled build, which store nothing.
ranking_id: str | None = None
# The stored `input_parts`. None for a disabled build.
input_parts: dict[str, Any] | None = None
# None for a dry run or a disabled build.
input_changes: InputChanges | None = None
def result_json(self) -> dict[str, Any]:
"""The stored `result`: every candidate by report id, with its rank, tier and reason."""
by_ref = {candidate.ref: candidate for candidate in self.candidates}
return {
"limit": self.limit,
"ranked": [
{
"report_id": by_ref[entry.ref].report_id,
"rank": entry.rank,
"tier": entry.tier,
"reason": entry.reason,
"next_step": entry.next_step,
"matched": dataclasses.asdict(entry.matched) if entry.matched else None,
"ranked_by": entry.ranked_by,
"pools": list(by_ref[entry.ref].pools),
}
for entry in self.entries
],
"folded": [
{
"report_id": by_ref[fold.ref].report_id,
"into_report_id": by_ref[fold.into].report_id,
"reason": fold.reason,
}
for fold in self.folded
],
"counts": dataclasses.asdict(self.counts),
}
def model_order_reason(candidate: Candidate) -> str:
return next((_NEEDS_YOU_REASONS[kind] for kind in candidate.needs_you), _MODEL_ORDER_REASON)
def may_call_model(team: Team, *, summary_ready: bool) -> bool:
"""Whether a build with candidates calls the model, as `build_personal_ranking` decides it.
Only such a build spends the shared daily limit.
"""
return summary_ready and team.organization.is_ai_data_processing_approved is True
def normalize_focus(focus: str | None, max_chars: int) -> str | None:
text = cap(focus, max_chars) if focus else ""
return text or None
def input_hash(candidates: list[Candidate], *, activity_summary_version: int | None, focus: str | None) -> str:
"""Changes when a candidate, its text, status, priority, PR state, needs-you facts or score, the summary, the prompt or the focus changes."""
rows = sorted(
[
candidate.report_id,
# The prompt shows this text, so an edit makes the stored reasons out of date.
candidate.title,
candidate.summary_lead,
candidate.status,
candidate.priority,
candidate.pull_request_state,
list(candidate.needs_you),
candidate.predictions.status if candidate.predictions else None,
[[entry.outcome, round(entry.probability, 2)] for entry in candidate.predictions.predictions]
if candidate.predictions
else [],
]
for candidate in candidates
)
payload = {
"candidates": rows,
"activity_summary_version": activity_summary_version,
"prompt_version": PROMPT_VERSION,
"focus": focus,
}
return hashlib.sha256(json.dumps(payload, sort_keys=True, default=str).encode()).hexdigest()
def _part_hash(value: Any) -> str:
return hashlib.sha256(json.dumps(value, sort_keys=True, default=str).encode()).hexdigest()[:_PART_HASH_CHARS]
def _report_parts(candidate: Candidate) -> dict[str, Any]:
predictions = candidate.predictions
return {
"text": [candidate.title, candidate.summary_lead],
"status": candidate.status,
"priority": candidate.priority,
"pull_request_state": candidate.pull_request_state,
"needs_you": list(candidate.needs_you),
"scores": [
predictions.status if predictions else None,
[[entry.outcome, round(entry.probability, 2)] for entry in predictions.predictions] if predictions else [],
],
}
def input_parts(
candidates: list[Candidate], *, activity_summary_version: int | None, focus: str | None
) -> dict[str, Any]:
"""The inputs of `input_hash` as one hash per part, so a later build can name the parts that changed.
The report parts have one hash for each report. `scores` keeps the probabilities, so a later build can measure the move.
"""
return {
"parts": {
"activity_summary_version": _part_hash(activity_summary_version),
"prompt_version": _part_hash(PROMPT_VERSION),
"focus": _part_hash(focus),
},
"reports": {
candidate.report_id: {part: _part_hash(value) for part, value in _report_parts(candidate).items()}
for candidate in candidates
},
"scores": {
candidate.report_id: {
entry.outcome: round(entry.probability, 4) for entry in candidate.predictions.predictions
}
for candidate in candidates
if candidate.predictions
},
}
def _max_score_move(previous: dict[str, dict[str, float]], current: dict[str, dict[str, float]]) -> float | None:
moves = [
abs(probability - previous[report_id][outcome])
for report_id, scores in current.items()
if report_id in previous
for outcome, probability in scores.items()
if outcome in previous[report_id]
]
return round(max(moves), 4) if moves else None
def compare_inputs(previous: PersonalInboxRanking | None, current: dict[str, Any], *, rank_top: int) -> InputChanges:
"""The parts that differ between the previous list and the current inputs. Never raises."""
if previous is None:
return InputChanges(changed_parts=(CHANGED_NO_PREVIOUS,))
stored = previous.input_parts
try:
if not stored:
return InputChanges(changed_parts=(CHANGED_UNKNOWN,))
stored_reports: dict[str, dict[str, str]] = stored["reports"]
current_reports: dict[str, dict[str, str]] = current["reports"]
joined = current_reports.keys() - stored_reports.keys()
left = stored_reports.keys() - current_reports.keys()
changed = {"candidate_set"} if joined or left else set()
changed |= {part for part in current["parts"] if current["parts"][part] != stored["parts"].get(part)}
changed_reports = 0
for report_id in current_reports.keys() & stored_reports.keys():
report_changed = {
part
for part in REPORT_PARTS
if current_reports[report_id].get(part) != stored_reports[report_id].get(part)
}
changed |= report_changed
changed_reports += bool(report_changed)
top_ids = {entry["report_id"] for entry in previous.result.get("ranked", [])[:rank_top]}
return InputChanges(
changed_parts=tuple(part for part in INPUT_PARTS if part in changed),
changed_report_count=changed_reports,
max_score_move=_max_score_move(stored.get("scores", {}), current["scores"]),
joined_count=len(joined),
left_count=len(left),
left_top_count=len(left & top_ids),
)
except (KeyError, TypeError, AttributeError):
logger.warning("signals.personal_ranking.unreadable_input_parts", ranking_id=str(previous.id))
return InputChanges(changed_parts=(CHANGED_UNKNOWN,))
def latest_row(team: Team, user: User) -> PersonalInboxRanking | None:
return PersonalInboxRanking.objects.for_team(team.id).filter(user_id=user.id).order_by("-created_at", "-id").first()
def ready_summary_version(team: Team, user: User) -> int | None:
summary = get_summary(team, user.id)
return summary.version if summary is not None and summary.status == ActivitySummaryStatus.READY else None
def record_check(team: Team, row: PersonalInboxRanking, now: datetime.datetime) -> None:
"""Record that the list still matches the current inputs, so it counts as fresh again."""
PersonalInboxRanking.objects.for_team(team.id).filter(id=row.id).update(checked_at=now)
@frozen
class InputCheck:
unchanged: bool
calls_model: bool
def check_inputs(team: Team, user: User, config: PersonalRankingConfig, *, now: datetime.datetime) -> InputCheck:
"""Compare the latest list with the current inputs. A list that still matches counts as fresh again."""
latest = latest_row(team, user)
candidates = gather_candidates(team, user, config, now=now, with_sources=False)
summary_version = ready_summary_version(team, user)
current_hash = input_hash(candidates, activity_summary_version=summary_version, focus=None)
if (
latest is not None
and latest.input_hash == current_hash
and latest.fallback_reason not in RETRYABLE_FALLBACK_REASONS
):
record_check(team, latest, now)
return InputCheck(unchanged=True, calls_model=False)
return InputCheck(
unchanged=False,
calls_model=bool(candidates) and may_call_model(team, summary_ready=summary_version is not None),
)
def _client(
team: Team, user: User, now: datetime.datetime, config: PersonalRankingConfig, trigger: BuildTrigger
) -> OpenAI:
client = build_openai_client(
"signals",
ai_product=AI_PRODUCT,
trace_id=trace_id_from_seed(f"{AI_PRODUCT}:{team.id}:{user.id}:{now.isoformat()}"),
properties={
"team_id": str(team.id),
"user_id": str(user.id),
"prompt_version": str(PROMPT_VERSION),
"trigger": trigger,
},
distinct_id=PIPELINE_DISTINCT_ID,
)
# One attempt: a retry would run past the timeout, and the fallback list is always there.
return client.with_options(timeout=config.timeout_seconds, max_retries=0)
def _create_completion(
client: OpenAI, config: PersonalRankingConfig, messages: list[ChatCompletionMessageParam]
) -> ChatCompletion:
def create(reasoning_effort: ReasoningEffort | Omit = omit) -> ChatCompletion:
return client.chat.completions.create(
model=config.model,
messages=messages,
user=PIPELINE_DISTINCT_ID,
response_format=RESPONSE_FORMAT,
max_completion_tokens=config.max_completion_tokens,
reasoning_effort=reasoning_effort,
)
if (config.model, config.reasoning_effort) in _REJECTED_REASONING_EFFORTS:
return create()
try:
return create(config.reasoning_effort)
except BadRequestError as error:
if "reasoning_effort" not in str(error):
raise
# A rejected parameter fails fast, so the retry stays inside the timeout.
_REJECTED_REASONING_EFFORTS.add((config.model, config.reasoning_effort))
logger.warning(
"signals.personal_ranking.reasoning_effort_rejected",
model=config.model,
reasoning_effort=config.reasoning_effort,
)
return create()
def _call_model(
team: Team,
user: User,
now: datetime.datetime,
config: PersonalRankingConfig,
messages: list[ChatCompletionMessageParam],
trigger: BuildTrigger,
) -> _ModelCall:
started = time.monotonic()
def elapsed_ms() -> int:
return round((time.monotonic() - started) * 1000)
try:
response = _create_completion(_client(team, user, now, config, trigger), config, messages)
except APITimeoutError:
return _ModelCall(
content=None, tokens_in=None, tokens_out=None, latency_ms=elapsed_ms(), fallback_reason=FALLBACK_TIMEOUT
)
except Exception:
logger.exception("signals.personal_ranking.llm_call_failed", team_id=team.id, user_id=user.id)
return _ModelCall(
content=None, tokens_in=None, tokens_out=None, latency_ms=elapsed_ms(), fallback_reason=FALLBACK_LLM_ERROR
)
usage = response.usage
choice = response.choices[0] if response.choices else None
truncated = choice is not None and choice.finish_reason == TRUNCATED_FINISH_REASON
return _ModelCall(
content=choice.message.content if choice is not None else None,
tokens_in=usage.prompt_tokens if usage is not None else None,
tokens_out=usage.completion_tokens if usage is not None else None,
latency_ms=elapsed_ms(),
fallback_reason=FALLBACK_TRUNCATED if truncated else None,
)
def _capture(
team: Team,
user: User,
build: PersonalRankingBuild,
config: PersonalRankingConfig,
*,
has_focus: bool,
trigger: BuildTrigger,
) -> None:
# Ids, counts and accounting only. Report text, reasons and the summary are user content.
try:
pools: dict[str, int] = {}
for candidate in build.candidates:
for pool in candidate.pools:
pools[pool] = pools.get(pool, 0) + 1
# The build runs in a Celery task, where the global client can drop events before it flushes.
with ph_scoped_capture() as capture:
capture(
distinct_id=user.distinct_id or PIPELINE_DISTINCT_ID,
event=BUILT_EVENT,
properties={
"team_id": team.id,
"user_id": user.id,
"ranking_id": build.ranking_id,
"status": build.status,
"personalized": build.personalized,
"fallback_reason": build.fallback_reason,
"model_called": build.model_called,
"model": build.model,
"prompt_version": PROMPT_VERSION,
"activity_summary_version": build.activity_summary_version,
"has_focus": has_focus,
"trigger": trigger,
"limit": build.limit,
"rank_top": config.rank_top,
"reasoning_effort": config.reasoning_effort,
"candidate_count": len(build.candidates),
"ranked_by_llm_count": sum(1 for entry in build.entries if entry.ranked_by == "llm"),
"folded_count": len(build.folded),
"tier_counts": {tier: sum(1 for entry in build.entries if entry.tier == tier) for tier in TIERS},
"pool_counts": pools,
**dataclasses.asdict(build.counts),
"tokens_in": build.tokens_in,
"tokens_out": build.tokens_out,
"latency_ms": build.latency_ms,
**(build.input_changes.properties() if build.input_changes is not None else {}),
},
groups={"organization": str(team.organization_id), "project": str(team.uuid)},
)
except Exception:
logger.exception("signals.personal_ranking.capture_failed", team_id=team.id, user_id=user.id)
def _store(team: Team, user: User, build: PersonalRankingBuild) -> PersonalRankingBuild:
status = PersonalInboxRanking.Status(build.status)
row = PersonalInboxRanking.objects.for_team(team.id).create(
team_id=team.id,
user_id=user.id,
input_hash=build.input_hash or "",
input_parts=build.input_parts,
candidate_ids=[candidate.report_id for candidate in build.candidates],
result=build.result_json(),
personalized=build.personalized,
activity_summary_version=build.activity_summary_version,
model=build.model,
prompt_version=PROMPT_VERSION,
tokens_in=build.tokens_in,
tokens_out=build.tokens_out,
latency_ms=build.latency_ms,
status=status,
fallback_reason=build.fallback_reason,
)
return dataclasses.replace(build, ranking_id=str(row.id))
def _prune(team: Team, user: User, keep: int) -> None:
"""Delete one batch of the person's rows older than the newest `keep`, so the table stays bounded."""
rows = PersonalInboxRanking.objects.for_team(team.id).filter(user_id=user.id)
stale_ids = list(rows.order_by("-created_at", "-id").values_list("id", flat=True)[keep : keep + PRUNE_BATCH_SIZE])
if stale_ids:
rows.filter(id__in=stale_ids).delete()
def build_personal_ranking(
team: Team,
user: User,
*,
focus: str | None = None,
dry_run: bool = False,
limit: int | None = None,
config: PersonalRankingConfig | None = None,
trigger: BuildTrigger = "on_demand",
) -> PersonalRankingBuild:
"""Rank the person's candidate reports. `dry_run` calls the model but stores no row and records no event."""
config = config or read_config()
limit = min(max(limit or config.default_limit, 1), config.max_limit)
if not config.enabled:
return PersonalRankingBuild(
status="disabled",
personalized=False,
fallback_reason=None,
entries=(),
folded=(),
candidates=(),
counts=ValidationCounts(),
limit=limit,
input_hash=None,
activity_summary_version=None,
model=config.model,
)
now = timezone.now()
focus = normalize_focus(focus, config.focus_max_chars)
summary = get_summary(team, user.id)
ready_summary = summary if summary is not None and summary.status == ActivitySummaryStatus.READY else None
summary_version = ready_summary.version if ready_summary is not None else None
candidates = gather_candidates(team, user, config, now=now)
refs = [candidate.ref for candidate in candidates]
model_reasons = {candidate.ref: model_order_reason(candidate) for candidate in candidates}
status: BuildStatus = "empty"
fallback_reason: str | None = FALLBACK_NO_CANDIDATES
validated = None
call = None
if candidates and team.organization.is_ai_data_processing_approved is not True:
# Report text never goes to the model without the organization's approval, so the list stays in model order.
status, fallback_reason = "fallback", FALLBACK_AI_NOT_APPROVED
elif candidates and ready_summary is None:
# With no summary the model cannot personalize the list, so the build skips the paid call.
status, fallback_reason = "fallback", FALLBACK_NO_ACTIVITY_SUMMARY
elif candidates and ready_summary is not None:
messages: list[ChatCompletionMessageParam] = [
{"role": "system", "content": system_prompt(config, limit)},
{
"role": "user",
"content": user_message(
render_person(ready_summary, config),
candidates,
focus=focus,
now=now,
limit=limit,
rank_top=config.rank_top,
),
},
]
call = _call_model(team, user, now, config, messages, trigger)
fallback_reason = call.fallback_reason
if fallback_reason is None:
try:
validated = validate_ranking(
call.content,
refs=refs,
model_reasons=model_reasons,
needs_you={candidate.ref: candidate.needs_you for candidate in candidates},
limit=limit,
rank_top=config.rank_top,
top_max=config.top_max,
worth_a_look_max=config.worth_a_look_max,
reason_limits=config.reason_limits,
next_step_max_chars=config.next_step_max_chars,
)
except MalformedRankingError as error:
logger.warning(
"signals.personal_ranking.malformed_output", team_id=team.id, user_id=user.id, error=str(error)
)
fallback_reason = FALLBACK_MALFORMED
status = "ok" if validated is not None else "fallback"
parts = input_parts(candidates, activity_summary_version=summary_version, focus=focus)
build = PersonalRankingBuild(
status=status,
personalized=validated is not None,
fallback_reason=fallback_reason,
entries=validated.entries
if validated is not None
else model_order(refs, model_reasons, min(limit, config.worth_a_look_max)),
folded=validated.folded if validated is not None else (),
candidates=tuple(candidates),
counts=validated.counts if validated is not None else ValidationCounts(),
limit=limit,
input_hash=input_hash(candidates, activity_summary_version=summary_version, focus=focus),
input_parts=parts,
activity_summary_version=summary_version,
model=config.model,
model_called=call is not None,
tokens_in=call.tokens_in if call is not None else None,
tokens_out=call.tokens_out if call is not None else None,
latency_ms=call.latency_ms if call is not None else None,
)
if dry_run:
return build
changes = compare_inputs(latest_row(team, user), parts, rank_top=config.rank_top)
build = _store(team, user, dataclasses.replace(build, input_changes=changes))
try:
_prune(team, user, config.max_rows_per_person)
except Exception:
logger.exception("signals.personal_ranking.prune_failed", team_id=team.id, user_id=user.id)
_capture(team, user, build, config, has_focus=focus is not None, trigger=trigger)
return build