: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
feat(signals): trigger activity summary updates on weighted views (#123218)
posthog[bot]ac70ab68b2bb
diff --git a/products/signals/backend/activity_summary/config.py b/products/signals/backend/activity_summary/config.pyindex 60fd0c8dd561..d2f4752d3602 100644--- a/products/signals/backend/activity_summary/config.py+++ b/products/signals/backend/activity_summary/config.py@@ -19,6 +19,7 @@ from products.signals.backend.activity_summary.logic import (MAX_VERSIONS,MIN_UNITS,MIN_UPDATE_INTERVAL,+ VIEW_UPDATE_INTERVAL,)from products.signals.backend.activity_summary.render import DEFAULT_LIMITS, RenderLimitsfrom products.signals.backend.scout_harness.team_limits import read_flag_payload@@ -28,6 +29,23 @@ logger = structlog.get_logger(__name__)ACTIVITY_SUMMARY_FLAG = "activity-summary"# Not a person: the flag must serve its payload to this id.ACTIVITY_SUMMARY_FLAG_DISTINCT_ID = "signals_activity_summary"+# One unit of a source never adds more than this to the pending score.+MAX_SOURCE_WEIGHT = 10.0+++@frozen+class SourceWeights:+ """The pending score that one unit of each queue source adds."""++ # An activity log row that passes the filters of `inputs.user_activity`.+ activity: float = 1.0+ tasks: float = 1.0+ # An object first opened after the pair's watermark: it has no view day on or before the watermark day.+ new_views: float = 0.5+ # A new day of opening an object that the pair also opened on or before the watermark day.+ repeat_view_days: float = 0.0+ # A new day of opening a product scene.+ scene_views: float = 0.0@frozen@@ -52,8 +70,15 @@ class ActivitySummaryConfig:max_versions: int = MAX_VERSIONSemit_embeddings: bool = Truecapture_events: bool = True- # Count viewed objects in the queue. Turn it on only when the update reads views as an input.+ # Count viewed objects in the queue. When off, the view weights and the view cold start do not apply.count_views: bool = False+ source_weights: SourceWeights = SourceWeights()+ # A pair with a current summary is due when its pending score reaches this.+ min_score: float = 3.0+ # The wait after an update that views led, in place of `min_update_interval_hours`.+ view_update_interval_hours: float = VIEW_UPDATE_INTERVAL.total_seconds() / 3600+ # A pair with no summary is due when it opened objects or scenes on this many days in the window.+ cold_start_min_active_days: int = 3# The summary age each tier aims for. A pair's staleness is its summary age over this interval.target_interval_days_paying: float = 3.0target_interval_days_free: float = 5.0@@ -104,6 +129,25 @@ def _bool(value: Any, default: bool) -> bool:return value if isinstance(value, bool) else default+def _weight(value: Any, default: float) -> float:+ if isinstance(value, bool) or not isinstance(value, int | float) or not 0 <= value <= MAX_SOURCE_WEIGHT:+ return default+ return float(value)+++def parse_source_weights(payload: Any) -> SourceWeights:+ """The `source_weights` object of the payload. A weight can be zero, so that source adds nothing."""+ defaults = SourceWeights()+ if not isinstance(payload, dict):+ return defaults+ return SourceWeights(+ **{+ field.name: _weight(payload.get(field.name), getattr(defaults, field.name))+ for field in dataclasses.fields(SourceWeights)+ }+ )++def _team_ids(value: Any) -> tuple[int, ...]:if not isinstance(value, list):return ()@@ -166,6 +210,14 @@ def parse_config(payload: Any) -> ActivitySummaryConfig:emit_embeddings=_bool(payload.get("emit_embeddings"), defaults.emit_embeddings),capture_events=_bool(payload.get("capture_events"), defaults.capture_events),count_views=_bool(payload.get("count_views"), defaults.count_views),+ source_weights=parse_source_weights(payload.get("source_weights")),+ min_score=_positive_number(payload.get("min_score"), defaults.min_score),+ view_update_interval_hours=_positive_number(+ payload.get("view_update_interval_hours"), defaults.view_update_interval_hours+ ),+ cold_start_min_active_days=_positive_int(+ payload.get("cold_start_min_active_days"), defaults.cold_start_min_active_days+ ),target_interval_days_paying=_positive_number(payload.get("target_interval_days_paying"), defaults.target_interval_days_paying),diff --git a/products/signals/backend/activity_summary/logic.py b/products/signals/backend/activity_summary/logic.pyindex d1c830566d32..e8d9307efe03 100644--- a/products/signals/backend/activity_summary/logic.py+++ b/products/signals/backend/activity_summary/logic.py@@ -35,6 +35,8 @@ from products.signals.backend.models import UserActivitySummary, UserActivitySum# Fewer rendered lines than this since the last update say too little to change the summary.MIN_UNITS = 3MIN_UPDATE_INTERVAL = datetime.timedelta(days=1)+# What someone opens changes slowly, so an update that views led waits longer.+VIEW_UPDATE_INTERVAL = datetime.timedelta(days=7)# The `UserActivitySummaryVersion` rows kept per summary. An update deletes the older ones.MAX_VERSIONS = 30# A pair that skipped or failed waits this long, so it does not take the queue head on every tick.diff --git a/products/signals/backend/activity_summary/queue.py b/products/signals/backend/activity_summary/queue.pyindex d169174ccde2..313700bf4c86 100644--- a/products/signals/backend/activity_summary/queue.py+++ b/products/signals/backend/activity_summary/queue.py@@ -1,17 +1,24 @@"""Pick the (team, user) pairs that the next activity summary tick updates, and measure the daily caps.Each pair has a queue state row. One cursor per input source reads the new rows of all enrolled teams-and adds them to the pending units of their pair. `select_pairs` then ranks the due pairs and fills the-tick budget in two passes: paying organizations first, then free organizations with the budget that is-left. Row counts here are only a queue estimate. `update_activity_summary` still applies the real drop-rules and `min_units`, and backs a pair off when it skips, so the pair leaves the queue head.+and adds them to the pending units and the weighted pending score of their pair. `select_pairs` then+ranks the due pairs and fills the tick budget in two passes: paying organizations first, then free+organizations with the budget that is left. Row counts here are only a queue estimate.+`update_activity_summary` still applies the real drop rules and `min_units`, and backs a pair off when+it skips, so the pair leaves the queue head.++A view log row keeps one object's latest view, and its `viewed_at` moves on every reopen. So the view+part of the score is not added up row by row. The feed computes it again from the view days of the+pair's rows, relative to the pair's watermark, so a reopen of a known object adds at most one repeat+day per day."""import mathimport uuidimport hashlibimport datetime-from collections.abc import Callable+import dataclasses+from collections.abc import Callable, Iterable, Sequencefrom typing import Anyfrom django.db import transaction@@ -38,7 +45,7 @@ from django.db.models.functions import Cast, Coalesce, Extract, Greatest, Ln, Rofrom posthog.dataclasses import frozenfrom posthog.models import ActivityLog, FileSystemViewLog, Team-from products.signals.backend.activity_summary.config import ActivitySummaryConfig+from products.signals.backend.activity_summary.config import ActivitySummaryConfig, SourceWeightsfrom products.signals.backend.activity_summary.inputs import TASK_ORIGIN_PRODUCTSfrom products.signals.backend.activity_summary.logic import EXCEPTION_ERROR_PREFIXfrom products.signals.backend.activity_summary.prompt import COMPUTED_MAX_CHARS, PROMPT_VERSION, system_prompt@@ -49,6 +56,7 @@ from products.signals.backend.activity_summary.render import (NOISY_ACTIVITY_SCOPES,RENDER_VERSION,SANDBOX_PROMPT_TITLE_PREFIX,+ SCENE_VIEW_TYPE,SCOUT_CLIENT_PREFIX,RenderLimits,)@@ -79,11 +87,23 @@ TIER_FREE = "free"# A summary younger than this many target intervals counts as fresh on the tick event._FRESH_STALENESS = 2+# Why the queue picked a pair.+TRIGGER_ACTIVITY = "activity"+# Views make up more than half of the pending score.+TRIGGER_VIEWS = "views"+# A pair with no summary and a pending score below `min_score`, due on its days with views.+TRIGGER_COLD_START_VIEWS = "cold_start_views"+TRIGGER_STALE = "stale"+TRIGGERS = (TRIGGER_ACTIVITY, TRIGGER_VIEWS, TRIGGER_COLD_START_VIEWS, TRIGGER_STALE)+VIEW_TRIGGERS = frozenset({TRIGGER_VIEWS, TRIGGER_COLD_START_VIEWS})+@frozen(order=True)class ActivitySummaryPair:team_id: intuser_id: int+ # Set only on the pairs that `select_pairs` returns. Has a default, so a plan recorded before it existed decodes.+ trigger: str | None = None@frozen@@ -101,12 +121,20 @@ class PairSelection:pairs: list[ActivitySummaryPair]due_pairs: inttiers: list[ActivitySummaryTierStats]+ due_by_trigger: dict[str, int] = dataclasses.field(default_factory=dict)@frozenclass _NewRows:units: intlast_at: datetime.datetime+ score: float = 0.0+++@frozen+class _ViewState:+ score: float+ active_days: int_Pending = dict[ActivitySummaryPair, _NewRows]@@ -275,12 +303,18 @@ def _parse_organization_ids(organization_ids: tuple[str, ...]) -> set[uuid.UUID]return parsed-def _add(pending: _Pending, pair: ActivitySummaryPair, units: int, at: datetime.datetime) -> None:+def _add(pending: _Pending, pair: ActivitySummaryPair, units: int, at: datetime.datetime, score: float = 0.0) -> None:current = pending.get(pair)if current is None:- pending[pair] = _NewRows(units=units, last_at=at)+ pending[pair] = _NewRows(units=units, last_at=at, score=score)else:- pending[pair] = _NewRows(units=current.units + units, last_at=max(current.last_at, at))+ pending[pair] = _NewRows(+ units=current.units + units, last_at=max(current.last_at, at), score=current.score + score+ )+++def _write_weight(weights: SourceWeights, source: str) -> float:+ return weights.activity if source == SOURCE_ACTIVITY_LOG else weights.tasksdef _watermarks(team_ids: list[int]) -> dict[ActivitySummaryPair, datetime.datetime | None]:@@ -379,6 +413,63 @@ def queue_sources(config: ActivitySummaryConfig) -> tuple[str, ...]:)+def _view_state(+ rows: Iterable[tuple[str, Sequence[datetime.date] | None, datetime.datetime]],+ watermark: datetime.datetime | None,+ first_day: datetime.date,+ weights: SourceWeights,+) -> _ViewState:+ """The view part of a pair's pending score, and its days with a view from `first_day` on.++ An object with a view day on or before the watermark day is known: it adds `repeat_view_days` for+ each day after the watermark day. Any other object opened after the watermark adds `new_views` once.+ A scene adds `scene_views` for each day after the watermark day. With no watermark, every object is new.+ """+ watermark_day = watermark.astimezone(datetime.UTC).date() if watermark is not None else None+ active: set[datetime.date] = set()+ score = 0.0+ for view_type, view_days, viewed_at in rows:+ days = set(view_days or ()) or {viewed_at.astimezone(datetime.UTC).date()}+ active |= {day for day in days if day >= first_day}+ if watermark is not None and viewed_at <= watermark:+ continue+ new_days = sum(1 for day in days if day >= first_day and (watermark_day is None or day > watermark_day))+ if view_type == SCENE_VIEW_TYPE:+ score += new_days * weights.scene_views+ elif watermark_day is not None and any(day <= watermark_day for day in days):+ score += new_days * weights.repeat_view_days+ else:+ score += weights.new_views+ return _ViewState(score=score, active_days=len(active))+++def _view_states(+ config: ActivitySummaryConfig, pairs: Iterable[ActivitySummaryPair], end: datetime.datetime+) -> dict[ActivitySummaryPair, _ViewState]:+ """The view state of each pair, from all its rows in the window before `end`, in two queries."""+ wanted = set(pairs)+ if not wanted:+ return {}+ team_ids = sorted({pair.team_id for pair in wanted})+ window_start = end - datetime.timedelta(days=config.limits.window_days)+ rows: dict[ActivitySummaryPair, list[tuple[str, Sequence[datetime.date] | None, datetime.datetime]]] = {}+ for team_id, user_id, view_type, view_days, viewed_at in (+ FileSystemViewLog.objects.filter(+ team_id__in=team_ids, user_id__in={pair.user_id for pair in wanted}, viewed_at__gte=window_start+ )+ .exclude(type="folder")+ .values_list("team_id", "user_id", "type", "view_days", "viewed_at")+ ):+ pair = ActivitySummaryPair(team_id=team_id, user_id=user_id)+ if pair in wanted:+ rows.setdefault(pair, []).append((view_type, view_days, viewed_at))+ watermarks = _watermarks(team_ids)+ return {+ pair: _view_state(rows.get(pair, []), watermarks.get(pair), window_start.date(), config.source_weights)+ for pair in wanted+ }++_STORED_FIELDS = ("next_eligible_at", "generated_at", "prompt_version", "render_version")@@ -409,16 +500,34 @@ def _create_missing_states(pairs: list[ActivitySummaryPair]) -> None:)-def _add_pending(pending: _Pending) -> None:+def _add_pending(pending: _Pending, views: dict[ActivitySummaryPair, _ViewState] | None = None) -> None:+ """Add the new rows to each pair's state. With `views`, set the view part of the score in place of adding it."""if not pending:return_create_missing_states(list(pending))for pair, rows in pending.items():- UserActivitySummaryQueueState.objects.for_team(pair.team_id).filter(user_id=pair.user_id).update(- pending_units=F("pending_units") + rows.units,+ fields: dict[str, Any] = {+ "pending_units": F("pending_units") + rows.units,+ "pending_score": F("pending_score") + rows.score,# Postgres GREATEST skips a null.- last_activity_at=Greatest("last_activity_at", rows.last_at),- )+ "last_activity_at": Greatest("last_activity_at", rows.last_at),+ }+ if views is not None:+ view = views[pair]+ fields |= {+ # Postgres reads the old `pending_view_score` on the right side of every assignment.+ "pending_score": F("pending_score") - F("pending_view_score") + view.score,+ "pending_view_score": view.score,+ "view_active_days": view.active_days,+ }+ UserActivitySummaryQueueState.objects.for_team(pair.team_id).filter(user_id=pair.user_id).update(**fields)+++def _weighted(pending: _Pending, weight: float) -> _Pending:+ return {+ pair: _NewRows(units=rows.units, last_at=rows.last_at, score=rows.units * weight)+ for pair, rows in pending.items()+ }def feed_queue(config: ActivitySummaryConfig, now: datetime.datetime, team_ids: list[int] | None = None) -> int:@@ -441,7 +550,10 @@ def feed_queue(config: ActivitySummaryConfig, now: datetime.datetime, team_ids:if stop <= cursor.position:continuepending = _READERS[source](team_ids, cursor.position, stop, False)- _add_pending(pending)+ if source == SOURCE_VIEWS:+ _add_pending(pending, _view_states(config, pending, stop))+ else:+ _add_pending(_weighted(pending, _write_weight(config.source_weights, source)))cursor.position = stopcursor.save(update_fields=["position", "updated_at"])fed += len(pending)@@ -451,23 +563,32 @@ def feed_queue(config: ActivitySummaryConfig, now: datetime.datetime, team_ids:def seed_queue(config: ActivitySummaryConfig, team_ids: list[int], end: datetime.datetime) -> int:"""Set the queue state of these teams from the rows in the window before `end`.- Pending units count the rows after each pair's watermark. Last activity counts every row, so a- stale summary with no new rows is still due.+ Pending units and the pending score count the rows after each pair's watermark. Last activity counts+ every row, so a stale summary with no new rows is still due.- Pending units of these teams that the window does not hold go to zero. Returns the pairs seeded.+ The pending state of these teams that the window does not hold goes to zero. Returns the pairs seeded."""if not team_ids:return 0window_start = end - datetime.timedelta(days=config.limits.window_days)pending: _Pending = {}+ viewed: _Pending = {}for source in queue_sources(config):for pair, rows in _READERS[source](team_ids, window_start, end, True).items():- _add(pending, pair, rows.units, rows.last_at)+ if source == SOURCE_VIEWS:+ viewed[pair] = rows+ else:+ score = rows.units * _write_weight(config.source_weights, source)+ _add(pending, pair, rows.units, rows.last_at, score)+ views = _view_states(config, viewed, end)with transaction.atomic():# nosemgrep: idor-lookup-without-team (operator backfill with no user input; filtered to the given teams)- UserActivitySummaryQueueState.all_teams.filter(team_id__in=team_ids).update(pending_units=0)+ UserActivitySummaryQueueState.all_teams.filter(team_id__in=team_ids).update(+ pending_units=0, pending_score=0.0, pending_view_score=0.0, view_active_days=0+ )_add_pending(pending)- return len(pending)+ _add_pending(viewed, views)+ return len(pending.keys() | viewed.keys())def move_cursors_to(config: ActivitySummaryConfig, end: datetime.datetime) -> None:@@ -484,7 +605,7 @@ def move_cursors_to(config: ActivitySummaryConfig, end: datetime.datetime) -> Nodef sync_pair_state(team_id: int, user_id: int) -> None:"""Copy the times and versions of the stored summary to the pair's queue state.- Pending units go to zero when the summary's watermark includes the newest pending row.+ Pending units and the pending score go to zero when the summary's watermark includes the newest pending row."""stored = (UserActivitySummary.objects.for_team(team_id)@@ -495,11 +616,12 @@ def sync_pair_state(team_id: int, user_id: int) -> None:if stored is None:returnwatermark = stored["input_watermark"]+ cleared = {+ name: F(name) if watermark is None else Case(When(last_activity_at__lte=watermark, then=zero), default=F(name))+ for name, zero in (("pending_units", 0), ("pending_score", 0.0), ("pending_view_score", 0.0))+ }UserActivitySummaryQueueState.objects.for_team(team_id).filter(user_id=user_id).update(- **_state_fields(stored),- pending_units=F("pending_units")- if watermark is None- else Case(When(last_activity_at__lte=watermark, then=0), default=F("pending_units")),+ **_state_fields(stored), **cleared)@@ -517,11 +639,12 @@ def _hours_since(now: datetime.datetime, at: Coalesce) -> Combinable:def _states(config: ActivitySummaryConfig, now: datetime.datetime, team_ids: list[int]) -> QuerySet:- """The queue states of these teams, with their tier, staleness and priority score.+ """The queue states of these teams, with their tier, staleness, pending score and priority score.Staleness is the summary age over the tier's target interval. A pair with no summary counts its age- from its first queued row. The score is log(1 + pending units) times staleness, plus a bonus for a- pair with no summary, or a bonus plus staleness for a stale summary.+ from its first queued row. The pending score leaves out the view part when views do not count. The+ priority score is log(1 + pending score) times staleness, plus a bonus for a pair with no summary,+ or a bonus plus staleness for a stale summary."""paying = Coalesce(F("team__organization__has_active_subscription"), Value(False))target_hours = Case(@@ -538,20 +661,37 @@ def _states(config: ActivitySummaryConfig, now: datetime.datetime, team_ids: lisdefault=Value(0.0),output_field=FloatField(),)+ write_score = F("pending_score") - F("pending_view_score")return (# nosemgrep: idor-lookup-without-team (system tick with no user input; filtered to the enrolled teams)UserActivitySummaryQueueState.all_teams.filter(team_id__in=team_ids).annotate(organization_id=F("team__organization_id"), paying=paying, stale=stale)- .annotate(staleness=age_hours / target_hours)- .annotate(score=Ln(F("pending_units") + 1) * F("staleness") + cold_bonus)+ .annotate(+ staleness=age_hours / target_hours,+ write_score=write_score,+ weighted_score=F("pending_score") if config.count_views else write_score,+ )+ .annotate(score=Ln(F("weighted_score") + 1) * F("staleness") + cold_bonus))def _due(states: QuerySet, config: ActivitySummaryConfig, now: datetime.datetime) -> QuerySet:+ """The due states, each with the trigger that made it due."""+ reason = Q(weighted_score__gte=config.min_score) | Q(stale=True)+ if config.count_views:+ reason |= Q(summarized_at__isnull=True, view_active_days__gte=config.cold_start_min_active_days)return states.filter(+ reason,Q(due_at__isnull=True) | Q(due_at__lte=now),- Q(pending_units__gte=max(1, config.min_units)) | Q(stale=True),last_activity_at__gte=now - datetime.timedelta(days=config.limits.window_days),+ ).annotate(+ trigger=Case(+ When(stale=True, then=Value(TRIGGER_STALE)),+ # A due pair below `min_score` that is not stale is due only on its days with views.+ When(weighted_score__lt=config.min_score, then=Value(TRIGGER_COLD_START_VIEWS)),+ When(write_score__lt=F("weighted_score") / 2, then=Value(TRIGGER_VIEWS)),+ default=Value(TRIGGER_ACTIVITY),+ ))@@ -565,7 +705,7 @@ def _cap_rebuilds(due: QuerySet, max_rebuilds: int) -> QuerySet:return due.filter(Q(stale=False) | Q(id__in=rebuild_ids))-def _fill_pass(due: QuerySet, limit: int, max_org_share: float) -> list[UserActivitySummaryQueueState]:+def _fill_pass(due: QuerySet, limit: int, max_org_share: float) -> list[dict[str, Any]]:"""The highest scores first, with at most `max_org_share` of `limit` from one organization."""if limit <= 0:return []@@ -574,10 +714,15 @@ def _fill_pass(due: QuerySet, limit: int, max_org_share: float) -> list[UserActidue.annotate(org_rank=Window(RowNumber(), partition_by=[F("organization_id")], order_by=order)).filter(org_rank__lte=max(1, int(max_org_share * limit))).order_by(*order)- .only("id", "team_id", "user_id")[:limit]+ .values("id", "team_id", "user_id", "trigger")[:limit])+def _due_by_trigger(due: QuerySet) -> dict[str, int]:+ counts = {row["trigger"]: row["pairs"] for row in due.values("trigger").annotate(pairs=Count("id"))}+ return {trigger: counts.get(trigger, 0) for trigger in TRIGGERS}++def _tier_stats(states: QuerySet, due: QuerySet, config: ActivitySummaryConfig, now: datetime.datetime) -> list[ActivitySummaryTierStats]:@@ -619,9 +764,10 @@ def select_pairs("""The due pairs of the enrolled teams, highest priority first, cut to `limit`.A pair is due when it has a row in the last `window_days` and its due time is empty or past. A pair- with a current summary also needs at least `min_units` pending units. One unit never comes from less- than one row, so a pair with fewer rows would only skip. A pair with a stale summary needs no pending- units, because its update reads the full window, and it ranks with the pairs with no summary.+ with a current summary also needs a pending score of at least `min_score`. When views count, a pair+ with no summary is also due once it opened objects or scenes on `cold_start_min_active_days` days.+ A pair with a stale summary needs no pending score, because its update reads the full window, and it+ ranks with the pairs with no summary. Each returned pair carries the trigger that made it due.Stale pairs take at most `rebuild_budget_share` of `limit`, paying pairs first, so a version changedoes not stop the other updates. The first pass ranks the paying pairs and the free pairs with atleast `max_overdue_factor` staleness, for `paying_budget_share` of `limit`. The second pass ranks the@@ -634,6 +780,7 @@ def select_pairs(states = _states(config, now, team_ids)due = _due(states, config, now)tiers = _tier_stats(states, due, config, now)+ due_by_trigger = _due_by_trigger(due)due = _cap_rebuilds(due, math.ceil(limit * config.rebuild_budget_share))first = _fill_pass(due.filter(Q(paying=True) | Q(staleness__gte=config.max_overdue_factor)),@@ -641,12 +788,16 @@ def select_pairs(config.max_org_share,)second = _fill_pass(- due.filter(paying=False).exclude(id__in=[state.id for state in first]),+ due.filter(paying=False).exclude(id__in=[row["id"] for row in first]),limit - len(first),config.max_org_share,)return PairSelection(- pairs=[ActivitySummaryPair(team_id=state.team_id, user_id=state.user_id) for state in first + second],+ pairs=[+ ActivitySummaryPair(team_id=row["team_id"], user_id=row["user_id"], trigger=row["trigger"])+ for row in first + second+ ],due_pairs=sum(tier.due_pairs for tier in tiers),tiers=tiers,+ due_by_trigger=due_by_trigger,)diff --git a/products/signals/backend/activity_summary/tick.py b/products/signals/backend/activity_summary/tick.pyindex 40090309b69b..724d4d66c687 100644--- a/products/signals/backend/activity_summary/tick.py+++ b/products/signals/backend/activity_summary/tick.py@@ -30,6 +30,7 @@ from products.signals.backend.activity_summary.config import read_configfrom products.signals.backend.activity_summary.logic import (MAX_SKIP_BACKOFF,MAX_VERSIONS,+ VIEW_UPDATE_INTERVAL,ActivitySummaryUpdate,UpdateAfterModelCallError,UpdateStatus,@@ -38,6 +39,8 @@ from products.signals.backend.activity_summary.logic import ()from products.signals.backend.activity_summary.prompt import PROMPT_VERSIONfrom products.signals.backend.activity_summary.queue import (+ TRIGGERS,+ VIEW_TRIGGERS,ActivitySummaryPair,ActivitySummaryTierStats,daily_usage,@@ -115,6 +118,8 @@ class ActivitySummaryTickPlan:max_skip_backoff_hours: float = MAX_SKIP_BACKOFF.total_seconds() / 3600tiers: list[ActivitySummaryTierStats] = dataclasses.field(default_factory=list)enrollment: EnrollmentCounts = dataclasses.field(default_factory=EnrollmentCounts)+ view_update_interval_hours: float = VIEW_UPDATE_INTERVAL.total_seconds() / 3600+ due_by_trigger: dict[str, int] = dataclasses.field(default_factory=dict)@frozen@@ -131,6 +136,9 @@ class ActivitySummaryPairInput:# Has a default, so the pair input of a workflow in flight during a deploy still decodes.max_versions: int = MAX_VERSIONSmax_skip_backoff_hours: float = MAX_SKIP_BACKOFF.total_seconds() / 3600+ # Why the queue picked the pair. A view trigger waits `view_update_interval_hours` after a good update.+ trigger: str | None = None+ view_update_interval_hours: float | None = None@frozen@@ -156,6 +164,8 @@ class ActivitySummaryTickResult:# Successful updates that made a pair's first summary, and successful updates of an existing summary.new_builds: int = 0refresh_builds: int = 0+ # Due pairs at the start of the tick, per trigger.+ due_by_trigger: dict[str, int] = dataclasses.field(default_factory=dict)def plan_tick(limit: int | None, now: datetime.datetime) -> ActivitySummaryTickPlan:@@ -203,12 +213,14 @@ def plan_tick(limit: int | None, now: datetime.datetime) -> ActivitySummaryTickPmax_concurrency=config.max_concurrency,min_units=config.min_units,min_update_interval_hours=config.min_update_interval_hours,+ view_update_interval_hours=config.view_update_interval_hours,emit_embeddings=config.emit_embeddings,capture_events=config.capture_events,limits=config.limits,max_error_rate=config.max_error_rate,max_skip_backoff_hours=config.max_skip_backoff_hours,tiers=selection.tiers if selection else [],+ due_by_trigger=selection.due_by_trigger if selection else {},enrollment=counts,stop_reason=stop_reason,rebuild_before=config.rebuild_before,@@ -242,7 +254,7 @@ def _emit_embedding(team_id: int, user_id: int) -> None:)-def _capture_generated(team_id: int, user_id: int, update: ActivitySummaryUpdate) -> None:+def _capture_generated(team_id: int, user_id: int, update: ActivitySummaryUpdate, trigger: str | None) -> None:team = Team.objects.only("uuid", "organization_id").get(id=team_id)# Counts and accounting only. The summary and the rendered input are user content.ph_background_capture()(@@ -263,6 +275,7 @@ def _capture_generated(team_id: int, user_id: int, update: ActivitySummaryUpdate"latency_ms": update.latency_ms,"inputs_used": update.inputs_used,"rebuilt": update.rebuilt,+ "trigger": trigger,},groups={"organization": str(team.organization_id), "project": str(team.uuid)},)@@ -279,12 +292,15 @@ def run_pair(input: ActivitySummaryPairInput, *, last_attempt: bool = True) -> Areturn ActivitySummaryPairResult(status=UpdateStatus.SKIPPED_NOT_ENROLLED.value, cost_usd=None, called_model=False)+ interval_hours = input.min_update_interval_hours+ if input.trigger in VIEW_TRIGGERS and input.view_update_interval_hours is not None:+ interval_hours = input.view_update_interval_hourstry:update = update_activity_summary(input.team_id,input.user_id,min_units=input.min_units,- min_update_interval=datetime.timedelta(hours=input.min_update_interval_hours),+ min_update_interval=datetime.timedelta(hours=interval_hours),limits=input.limits,rebuild_before=input.rebuild_before,max_versions=input.max_versions,@@ -310,7 +326,7 @@ def run_pair(input: ActivitySummaryPairInput, *, last_attempt: bool = True) -> Alogger.exception("activity_summary_embedding_failed", team_id=input.team_id, user_id=input.user_id)if update.status in (UpdateStatus.OK, UpdateStatus.FAILED) and input.capture_events:try:- _capture_generated(input.team_id, input.user_id, update)+ _capture_generated(input.team_id, input.user_id, update, input.trigger)except Exception:logger.exception("activity_summary_capture_failed", team_id=input.team_id, user_id=input.user_id)return ActivitySummaryPairResult(@@ -333,6 +349,7 @@ def capture_tick(result: ActivitySummaryTickResult) -> None:tiers=[dataclasses.asdict(tier) for tier in result.tiers],new_builds=result.new_builds,refresh_builds=result.refresh_builds,+ due_by_trigger=result.due_by_trigger,**dataclasses.asdict(result.enrollment),)ph_background_capture()(@@ -348,6 +365,7 @@ def capture_tick(result: ActivitySummaryTickResult) -> None:"stop_reason": result.stop_reason,"new_builds": result.new_builds,"refresh_builds": result.refresh_builds,+ **{f"due_{trigger}": result.due_by_trigger.get(trigger, 0) for trigger in TRIGGERS},**dataclasses.asdict(result.enrollment),**{f"{tier.tier}_{name}": getattr(tier, name)@@ -436,6 +454,8 @@ class ActivitySummaryTickWorkflow:rebuild_before=plan.rebuild_before,max_versions=plan.max_versions,max_skip_backoff_hours=plan.max_skip_backoff_hours,+ trigger=pair.trigger,+ view_update_interval_hours=plan.view_update_interval_hours,),start_to_close_timeout=_PAIR_TIMEOUT,heartbeat_timeout=_PAIR_HEARTBEAT_TIMEOUT,@@ -475,6 +495,7 @@ class ActivitySummaryTickWorkflow:enrollment=plan.enrollment,new_builds=builds[True],refresh_builds=builds[False],+ due_by_trigger=plan.due_by_trigger,)await workflow.execute_activity(capture_activity_summary_tick_activity,diff --git a/products/signals/backend/management/commands/backfill_activity_summary_queue.py b/products/signals/backend/management/commands/backfill_activity_summary_queue.pyindex 882aec17a1fe..32b6f377c825 100644--- a/products/signals/backend/management/commands/backfill_activity_summary_queue.py+++ b/products/signals/backend/management/commands/backfill_activity_summary_queue.py@@ -1,7 +1,8 @@"""Seed the activity summary queue state of the enrolled teams, in batches of teams.-Run it once before the tick reads the queue, and again after a large enrollment change. Without it,-the queue holds only the rows that the cursors read after their first tick.+Run it once before the tick reads the queue, and again after a large enrollment change or a change to+`source_weights` or `count_views`. Without it, the queue holds only the rows that the cursors read+after their first tick, and the pending scores keep the weights they were fed with."""import jsondiff --git a/products/signals/backend/migrations/0202_useractivitysummaryqueuestate_pending_score.py b/products/signals/backend/migrations/0202_useractivitysummaryqueuestate_pending_score.pynew file mode 100644index 000000000000..7d94a4c93e5a--- /dev/null+++ b/products/signals/backend/migrations/0202_useractivitysummaryqueuestate_pending_score.py@@ -0,0 +1,27 @@+# Generated by Django 5.2.18 on 2026-10-25 20:50++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("signals", "0201_anomalyscoringstate_artifacts"),+ ]++ operations = [+ migrations.AddField(+ model_name="useractivitysummaryqueuestate",+ name="pending_score",+ field=models.FloatField(default=0.0),+ ),+ migrations.AddField(+ model_name="useractivitysummaryqueuestate",+ name="pending_view_score",+ field=models.FloatField(default=0.0),+ ),+ migrations.AddField(+ model_name="useractivitysummaryqueuestate",+ name="view_active_days",+ field=models.IntegerField(default=0),+ ),+ ]diff --git a/products/signals/backend/migrations/max_migration.txt b/products/signals/backend/migrations/max_migration.txtindex 023db632f653..cec63b7df899 100644--- a/products/signals/backend/migrations/max_migration.txt+++ b/products/signals/backend/migrations/max_migration.txt@@ -1 +1 @@-0201_anomalyscoringstate_artifacts+0202_useractivitysummaryqueuestate_pending_scorediff --git a/products/signals/backend/models.py b/products/signals/backend/models.pyindex d8166f0151ab..de9ac2e7b2df 100644--- a/products/signals/backend/models.py+++ b/products/signals/backend/models.py@@ -3978,6 +3978,12 @@ class UserActivitySummaryQueueState(TeamScopedRootMixin, UUIDModel):# New input rows since the last good update. An estimate: the update applies the real filters.pending_units = models.IntegerField(default=0)+ # The new input rows weighted by source, with the view part included. The queue's due rule reads this.+ pending_score = models.FloatField(default=0.0)+ # The view part of `pending_score`. The feed computes it again from the view days, so a reopen counts once a day.+ pending_view_score = models.FloatField(default=0.0)+ # Distinct days with an opened object or scene in the window, for the first summary of a view-only pair.+ view_active_days = models.IntegerField(default=0)last_activity_at = models.DateTimeField(null=True, blank=True)# The pair is due when this is empty or past. A copy of `UserActivitySummary.next_eligible_at`.due_at = models.DateTimeField(null=True, blank=True)diff --git a/products/signals/backend/test/test_activity_summary_tick.py b/products/signals/backend/test/test_activity_summary_tick.pyindex 860102c96cdd..1c12e4d48569 100644--- a/products/signals/backend/test/test_activity_summary_tick.py+++ b/products/signals/backend/test/test_activity_summary_tick.py@@ -20,13 +20,15 @@ from temporalio.testing import WorkflowEnvironmentfrom temporalio.worker import UnsandboxedWorkflowRunner, Workerfrom posthog.models import ActivityLog, FileSystemViewLog, Organization, Team, User+from posthog.models.file_system.file_system_representation import FileSystemRepresentation+from posthog.models.file_system.file_system_view_log import log_file_system_viewfrom products.signals.backend.activity_summary import (schedule as tick_schedule,tick,)from products.signals.backend.activity_summary.computed import MAX_OWNED, MAX_WATCHED-from products.signals.backend.activity_summary.config import ActivitySummaryConfig, parse_config+from products.signals.backend.activity_summary.config import ActivitySummaryConfig, SourceWeights, parse_configfrom products.signals.backend.activity_summary.prompt import (MAX_HEADLINE_CHARS,MAX_LABEL_CHARS,@@ -122,6 +124,10 @@ _CONFIG_CASES: list[tuple[str, Any, ActivitySummaryConfig]] = ["max_error_rate": 0,"self_driving_organization_percentage": 101,"deny_organization_ids": "org-b",+ "source_weights": {"activity": -1, "tasks": 11, "new_views": True, "scene_views": "1"},+ "min_score": 0,+ "view_update_interval_hours": -5,+ "cold_start_min_active_days": 2.5,},ActivitySummaryConfig(enabled=True, organization_ids=("org-a",), skip_team_ids=(3,)),),@@ -146,6 +152,10 @@ _CONFIG_CASES: list[tuple[str, Any, ActivitySummaryConfig]] = ["max_org_share": 0.3,"self_driving_organization_percentage": 12.5,"deny_organization_ids": ["org-b", 4],+ "source_weights": {"new_views": 1, "repeat_view_days": 0.25},+ "min_score": 4.5,+ "view_update_interval_hours": 72,+ "cold_start_min_active_days": 5,},ActivitySummaryConfig(enabled=True,@@ -161,6 +171,10 @@ _CONFIG_CASES: list[tuple[str, Any, ActivitySummaryConfig]] = [max_org_share=0.3,self_driving_organization_percentage=12.5,deny_organization_ids=("org-b",),+ source_weights=SourceWeights(new_views=1.0, repeat_view_days=0.25),+ min_score=4.5,+ view_update_interval_hours=72.0,+ cold_start_min_active_days=5,),),(@@ -281,7 +295,7 @@ class TestActivitySummaryQueue(BaseTest):self._changes(user, units, days_ago=0.5)self._changes(cold, 3, days_ago=0.5)- assert self._select(self._config(min_units=1, max_org_share=1.0)) == [+ assert self._select(self._config(min_score=1, max_org_share=1.0)) == [(self.team.id, cold.id),(self.team.id, recent_busy.id),(self.team.id, old_quiet.id),@@ -645,7 +659,7 @@ class TestActivitySummaryQueue(BaseTest):capture_events=True,)# The queue picks a pair with fewer rows than the update's `min_units`, so the update skips it.- config = self._config(min_units=1)+ config = self._config(min_score=1)assert self._select(config) == [(self.team.id, self.user.id)]with (@@ -692,18 +706,79 @@ class TestActivitySummaryQueue(BaseTest):assert self._select(seed=False) == []assert daily_usage(NOW) == DailyUsage(updates=1, cost_usd=0.25)- @parameterized.expand([("counted", True, 1), ("not counted", False, 0)])- def test_viewed_objects_make_a_pair_due_when_views_count(self, _name: str, count_views: bool, due: int) -> None:- for index in range(3):- FileSystemViewLog.objects.create(- team=self.team,- user=self.user,- type="insight",- ref=str(index),- viewed_at=NOW - datetime.timedelta(hours=index + 1),+ def _view(self, ref: str, *days_ago: int, view_type: str = "insight") -> None:+ days = sorted({(NOW - datetime.timedelta(days=ago)).date() for ago in days_ago})+ FileSystemViewLog.objects.create(+ team=self.team,+ user=self.user,+ type=view_type,+ ref=ref,+ viewed_at=NOW - datetime.timedelta(days=min(days_ago), hours=1),+ view_days=days,+ )++ @parameterized.expand(+ [+ ("known objects reopened every day", True, True, [("a", [5, 2, 1, 0]), ("b", [5, 2, 1, 0])], None),+ ("known scenes reopened every day", True, True, [("scene:a", [5, 2, 1, 0])], None),+ ("too few new objects", True, True, [(str(index), [1]) for index in range(5)], None),+ ("enough new objects", True, True, [(str(index), [1]) for index in range(6)], "views"),+ ("new objects when views do not count", False, True, [(str(index), [1]) for index in range(6)], None),+ (+ "no summary and views on three days",+ True,+ False,+ [("a", [3]), ("b", [2]), ("c", [1])],+ "cold_start_views",+ ),+ ("no summary and views on two days", True, False, [("a", [2]), ("b", [1, 2])], None),+ ("no summary and views on three days when views do not count", False, False, [("a", [3, 2, 1])], None),+ ]+ )+ def test_views_make_a_pair_due_by_weighted_score_or_on_active_days(+ self,+ _name: str,+ count_views: bool,+ summarized: bool,+ views: list[tuple[str, list[int]]],+ trigger: str | None,+ ) -> None:+ if summarized:+ watermark = NOW - datetime.timedelta(days=4)+ self._summary(self.user, generated_at=watermark, input_watermark=watermark)+ for ref, days_ago in views:+ name = ref.removeprefix("scene:")+ self._view(name, *days_ago, view_type="scene" if ref.startswith("scene:") else "insight")+ config = self._config(count_views=count_views)+ self._seed(config)++ selection = select_pairs(config, NOW, 10)++ assert [(pair.user_id, pair.trigger) for pair in selection.pairs] == [(self.user.id, trigger)][: bool(trigger)]+ assert selection.due_by_trigger == {+ name: int(name == trigger) for name in ("activity", "views", "cold_start_views", "stale")+ }++ def test_the_feed_counts_one_repeat_day_for_many_reopens_of_a_known_object_in_a_day(self) -> None:+ watermark = NOW - datetime.timedelta(days=2)+ self._summary(self.user, generated_at=watermark, input_watermark=watermark)+ insight = FileSystemRepresentation(+ base_folder="Unfiled/Insights", type="insight", ref="1", name="Funnel", href="/insights/1", meta={}+ )+ log_file_system_view(+ user=self.user, obj=insight, team_id=self.team.id, viewed_at=NOW - datetime.timedelta(days=3)+ )+ config = self._config(count_views=True, source_weights=SourceWeights(repeat_view_days=1.0))+ feed_queue(config, NOW - datetime.timedelta(hours=3))+ for hours_ago in (2.5, 2, 1.5, 1):+ log_file_system_view(+ user=self.user, obj=insight, team_id=self.team.id, viewed_at=NOW - datetime.timedelta(hours=hours_ago))+ with patch("products.signals.backend.activity_summary.queue.list_user_task_times", return_value=[]):+ feed_queue(config, NOW - datetime.timedelta(hours=hours_ago) + datetime.timedelta(minutes=10))- assert self._select(self._config(count_views=count_views)) == [(self.team.id, self.user.id)][:due]+ state = UserActivitySummaryQueueState.objects.for_team(self.team.id).get(user_id=self.user.id)+ assert (state.pending_score, state.pending_view_score, state.view_active_days) == (1.0, 1.0, 2)def test_the_feed_reads_each_row_once_and_an_ok_update_clears_the_pending_units(self) -> None:config = self._config()@@ -740,7 +815,10 @@ class TestActivitySummaryQueue(BaseTest):feed_queue(config, later)assert self._select(seed=False, now=later) == [(self.team.id, self.user.id)]- def test_an_ok_update_emits_the_embedding_and_an_event_without_the_summary(self) -> None:+ @parameterized.expand([("activity", 24), ("views", 168), ("cold_start_views", 168), (None, 24)])+ def test_an_ok_update_emits_the_embedding_and_an_event_without_the_summary(+ self, trigger: str | None, wait_hours: int+ ) -> None:self._changes(self.user, 3)capture = MagicMock()with (@@ -756,14 +834,19 @@ class TestActivitySummaryQueue(BaseTest):min_update_interval_hours=24,emit_embeddings=True,capture_events=True,+ trigger=trigger,+ view_update_interval_hours=168,))assert (result.status, result.called_model, result.new_summary) == ("ok", True, True)+ stored = UserActivitySummary.objects.for_team(self.team.id).get(user_id=self.user.id)+ assert stored.next_eligible_at == NOW + datetime.timedelta(hours=wait_hours)assert emit.call_args.kwargs["document_id"] == f"{self.user.id}:2026-10-19"assert emit.call_args.kwargs["content"] == "Builds the signup funnel.\nThemes: Signup funnel"event = capture.call_args.kwargsassert event["event"] == "activity summary generated"+ assert event["properties"]["trigger"] == triggerassert "signup funnel" not in json.dumps(event["properties"]).lower()@@ -790,7 +873,7 @@ async def test_an_update_keeps_an_operator_pause() -> None:def _plan(pairs: int, **overrides: Any) -> ActivitySummaryTickPlan:fields: dict[str, Any] = {"enabled": True,- "pairs": [ActivitySummaryPair(team_id=1, user_id=user_id) for user_id in range(pairs)],+ "pairs": [ActivitySummaryPair(team_id=1, user_id=user_id, trigger="views") for user_id in range(pairs)],"candidates": pairs,"updates_left": 100,"cost_left_usd": 100.0,@@ -804,6 +887,7 @@ def _plan(pairs: int, **overrides: Any) -> ActivitySummaryTickPlan:"max_error_rate": 0.5,"tiers": [ActivitySummaryTierStats(tier="paying", due_pairs=pairs)],"enrollment": EnrollmentCounts(qualifying_organizations=3, enrolled_organizations=2, enrolled_teams=4),+ "due_by_trigger": {"activity": 1, "views": pairs - 1},"stop_reason": None,}return ActivitySummaryTickPlan(**{**fields, **overrides})@@ -811,9 +895,10 @@ def _plan(pairs: int, **overrides: Any) -> ActivitySummaryTickPlan:async def _run_tick(plan: ActivitySummaryTickPlan, pair_cost: float | None, pair_status: str = "ok"-) -> tuple[ActivitySummaryTickResult, int, list[ActivitySummaryTickResult]]:+) -> tuple[ActivitySummaryTickResult, int, list[ActivitySummaryTickResult], list[ActivitySummaryPairInput]]:running = peak = 0captured: list[ActivitySummaryTickResult] = []+ pair_inputs: list[ActivitySummaryPairInput] = []@activity.defn(name="plan_activity_summary_tick_activity")async def fake_plan(input: ActivitySummaryTickInput) -> ActivitySummaryTickPlan:@@ -822,6 +907,7 @@ async def _run_tick(@activity.defn(name="update_activity_summary_pair_activity")async def fake_pair(input: ActivitySummaryPairInput) -> ActivitySummaryPairResult:nonlocal running, peak+ pair_inputs.append(input)running += 1peak = max(peak, running)await asyncio.sleep(0.01)@@ -848,7 +934,7 @@ async def _run_tick(id=f"activity-summary-tick-{uuid.uuid4()}",task_queue="activity-summary-tick-test",)- return result, peak, captured+ return result, peak, captured, pair_inputs@pytest.mark.asyncio@@ -871,9 +957,11 @@ async def test_the_tick_bounds_concurrency_and_stops_at_the_daily_caps_and_the_estop_reason: str | None,builds: tuple[int, int],) -> None:- result, peak, captured = await _run_tick(plan, pair_cost, pair_status)+ result, peak, captured, pair_inputs = await _run_tick(plan, pair_cost, pair_status)assert peak <= plan.max_concurrency+ assert {(pair.trigger, pair.view_update_interval_hours) for pair in pair_inputs} == {("views", 168.0)}+ assert result.due_by_trigger == plan.due_by_triggerassert (result.picked, result.statuses, result.stop_reason) == (picked, {pair_status: picked}, stop_reason)assert result.tiers == plan.tiersassert (result.new_builds, result.refresh_builds) == builds@@ -883,7 +971,7 @@ async def test_the_tick_bounds_concurrency_and_stops_at_the_daily_caps_and_the_e@pytest.mark.asyncioasync def test_a_disabled_tick_runs_no_pair() -> None:- result, peak, captured = await _run_tick(_plan(0, enabled=False), 0.01)+ result, peak, captured, _ = await _run_tick(_plan(0, enabled=False), 0.01)assert (result, peak, captured) == (ActivitySummaryTickResult(), 0, [])