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.

feat(signals): add in-region activity summary core with a stored model (#120455)

posthog[bot]1525df536d03
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index 4b89785d9fca..19519f4c9b6b 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -1649,6 +1649,7 @@ rules:
|MCPServerInstallation
|Reminder
|SessionRecordingViewed
+ |UserActivitySummary
|UserFacetSettings
|UserHomeSettings
|UserScenePersonalisation
@@ -1722,6 +1723,7 @@ rules:
|KernelRuntime
|Reminder
|SessionRecordingViewed
+ |UserActivitySummary
|UserFacetSettings
|UserHomeSettings
|UserScenePersonalisation
diff --git a/products/signals/backend/activity_summary/inputs.py b/products/signals/backend/activity_summary/inputs.py
new file mode 100644
index 000000000000..20aa154cb822
--- /dev/null
+++ b/products/signals/backend/activity_summary/inputs.py
@@ -0,0 +1,62 @@
+"""Read one user's own tasks and activity log rows in one project, from in-region Postgres."""
+
+import datetime
+
+from django.db.models.fields.json import KeyTextTransform, KeyTransform
+
+from posthog.models import ActivityLog
+
+from products.signals.backend.activity_summary.render import (
+ MAX_ACTIVITY_ROWS_PER_PERSON,
+ MAX_TASKS_PER_PERSON,
+ ActivityItem,
+ TaskItem,
+)
+from products.tasks.backend.facade.user_task_summaries import list_user_task_summaries
+
+# Tasks a person started: in PostHog AI, in Slack, or by hand in the web or desktop app. Scouts,
+# reports and workflows start tasks under a person's name too, and those are not the person's intent.
+TASK_ORIGIN_PRODUCTS = ("posthog_ai", "slack", "user_created")
+
+
+def user_tasks(team_id: int, user_id: int, start: datetime.datetime, end: datetime.datetime) -> list[TaskItem]:
+ """The user's own tasks in [start, end), newest `MAX_TASKS_PER_PERSON` first."""
+ return [
+ TaskItem(at=task.created_at, title=task.title, summary=task.summary, tags=task.tags)
+ for task in list_user_task_summaries(
+ team_ids=[team_id],
+ user_id=user_id,
+ start=start,
+ end=end,
+ origin_products=TASK_ORIGIN_PRODUCTS,
+ limit=MAX_TASKS_PER_PERSON,
+ )
+ ]
+
+
+def user_activity(team_id: int, user_id: int, start: datetime.datetime, end: datetime.datetime) -> list[ActivityItem]:
+ """The user's own activity log rows in [start, end), newest `MAX_ACTIVITY_ROWS_PER_PERSON` first.
+
+ System rows and rows written while someone impersonated the user are not the user's."""
+ rows = (
+ ActivityLog.objects.filter(team_id=team_id, user_id=user_id, created_at__gte=start, created_at__lt=end)
+ .exclude(is_system=True)
+ .exclude(was_impersonated=True)
+ .order_by("-created_at")
+ .annotate(
+ name=KeyTextTransform("name", "detail"),
+ intent=KeyTextTransform("intent", KeyTransform("payload", KeyTransform("trigger", "detail"))),
+ )
+ .values("scope", "activity", "client", "created_at", "name", "intent")[:MAX_ACTIVITY_ROWS_PER_PERSON]
+ )
+ return [
+ ActivityItem(
+ at=row["created_at"],
+ scope=row["scope"],
+ activity=row["activity"],
+ name=row["name"],
+ client=row["client"],
+ intent=row["intent"],
+ )
+ for row in rows
+ ]
diff --git a/products/signals/backend/activity_summary/logic.py b/products/signals/backend/activity_summary/logic.py
new file mode 100644
index 000000000000..455e33c97b17
--- /dev/null
+++ b/products/signals/backend/activity_summary/logic.py
@@ -0,0 +1,151 @@
+"""One activity summary update for one (team, user): gather, filter, render, summarize, persist."""
+
+import datetime
+from enum import StrEnum
+from typing import Any
+
+from django.utils import timezone
+
+from posthog.dataclasses import frozen
+from posthog.models import Team
+
+from products.signals.backend.activity_summary.inputs import user_activity, user_tasks
+from products.signals.backend.activity_summary.prompt import PROMPT_VERSION, summary_text
+from products.signals.backend.activity_summary.render import RENDER_VERSION, WINDOW_DAYS, build_input, sampling_seed
+from products.signals.backend.activity_summary.summaries import (
+ DEFAULT_MODEL,
+ MODEL_PRICES,
+ cost_usd,
+ gateway_client,
+ summarize,
+)
+from products.signals.backend.models import UserActivitySummary
+
+# Fewer rendered lines than this since the last update say too little to change the summary.
+MIN_UNITS = 3
+MIN_UPDATE_INTERVAL = datetime.timedelta(days=1)
+# The watermark is inclusive in the stored summary, so the next read starts just after it.
+_WATERMARK_STEP = datetime.timedelta(microseconds=1)
+
+
+class UpdateStatus(StrEnum):
+ OK = "ok"
+ FAILED = "failed"
+ SKIPPED_AI_NOT_APPROVED = "skipped_ai_not_approved"
+ SKIPPED_NOT_ELIGIBLE_YET = "skipped_not_eligible_yet"
+ SKIPPED_TOO_FEW_UNITS = "skipped_too_few_units"
+
+
+@frozen
+class ActivitySummaryUpdate:
+ status: UpdateStatus
+ rendered_input: str | None = None
+ summary: dict[str, Any] | None = None
+ tokens_in: int | None = None
+ tokens_out: int | None = None
+ cost_usd: float | None = None
+ latency_ms: int | None = None
+ error: str | None = None
+ inputs_used: dict[str, Any] | None = None
+ input_watermark: datetime.datetime | None = None
+
+
+def _input_start(watermark: datetime.datetime | None, now: datetime.datetime) -> datetime.datetime:
+ earliest = now - datetime.timedelta(days=WINDOW_DAYS)
+ return earliest if watermark is None else max(earliest, watermark + _WATERMARK_STEP)
+
+
+def update_activity_summary(
+ team_id: int, user_id: int, *, dry_run: bool = False, force: bool = False
+) -> ActivitySummaryUpdate:
+ """Update the stored activity summary of one user in one project.
+
+ Reads only the inputs newer than the stored watermark, or the last `WINDOW_DAYS` on a first
+ run. A failed update records the error and keeps the stored summary and the watermark. With
+ `dry_run`, the model is still called but nothing is written. `force` ignores `next_eligible_at`.
+ """
+ team = Team.objects.select_related("organization").get(id=team_id)
+ if team.organization.is_ai_data_processing_approved is not True:
+ return ActivitySummaryUpdate(status=UpdateStatus.SKIPPED_AI_NOT_APPROVED)
+
+ now = timezone.now()
+ stored = UserActivitySummary.objects.for_team(team_id).filter(user_id=user_id).first()
+ if not force and stored is not None and stored.next_eligible_at is not None and stored.next_eligible_at > now:
+ return ActivitySummaryUpdate(status=UpdateStatus.SKIPPED_NOT_ELIGIBLE_YET)
+
+ start = _input_start(stored.input_watermark if stored else None, now)
+ tasks = user_tasks(team_id, user_id, start, now)
+ activity = user_activity(team_id, user_id, start, now)
+ rendered = build_input(tasks, activity, as_of=now, seed=sampling_seed(now.date().isoformat(), team_id, user_id))
+ if rendered is None or rendered.units < MIN_UNITS:
+ return ActivitySummaryUpdate(
+ status=UpdateStatus.SKIPPED_TOO_FEW_UNITS,
+ rendered_input=rendered.text if rendered else None,
+ inputs_used={"drops": rendered.drops} if rendered else None,
+ )
+
+ inputs_used: dict[str, Any] = {
+ "tasks": rendered.n_tasks,
+ "activity_lines": rendered.n_activity_lines,
+ "input_chars": rendered.input_chars,
+ "drops": rendered.drops,
+ }
+ watermark = max([task.at for task in tasks] + [item.at for item in activity])
+ previous = stored.summary if stored is not None else None
+ model = DEFAULT_MODEL
+ outcome = summarize(
+ gateway_client(team_id, user_id, now.date()),
+ rendered_input=rendered.text,
+ previous_summary=previous,
+ model=model,
+ as_of=now.date(),
+ )
+ cost = cost_usd(outcome.tokens_in, outcome.tokens_out, MODEL_PRICES.get(model))
+ result = ActivitySummaryUpdate(
+ status=UpdateStatus.OK if outcome.summary is not None else UpdateStatus.FAILED,
+ rendered_input=rendered.text,
+ summary=outcome.summary,
+ tokens_in=outcome.tokens_in,
+ tokens_out=outcome.tokens_out,
+ cost_usd=cost,
+ latency_ms=outcome.latency_ms,
+ error=outcome.error,
+ inputs_used=inputs_used,
+ input_watermark=watermark if outcome.summary is not None else None,
+ )
+ if dry_run:
+ return result
+
+ accounting: dict[str, Any] = {
+ "tokens_in": outcome.tokens_in,
+ "tokens_out": outcome.tokens_out,
+ "cost_usd": cost,
+ "latency_ms": outcome.latency_ms,
+ "inputs_used": inputs_used,
+ "error": outcome.error,
+ }
+ if outcome.summary is None:
+ UserActivitySummary.objects.for_team(team_id).update_or_create(
+ team_id=team_id,
+ user_id=user_id,
+ defaults={**accounting, "status": UserActivitySummary.Status.FAILED},
+ )
+ return result
+
+ UserActivitySummary.objects.for_team(team_id).update_or_create(
+ team_id=team_id,
+ user_id=user_id,
+ defaults={
+ **accounting,
+ "status": UserActivitySummary.Status.OK,
+ "summary": outcome.summary,
+ "summary_text": summary_text(outcome.summary),
+ "generated_at": now,
+ "model": model,
+ "prompt_version": PROMPT_VERSION,
+ "render_version": RENDER_VERSION,
+ "input_watermark": watermark,
+ "next_eligible_at": now + MIN_UPDATE_INTERVAL,
+ },
+ )
+ return result
diff --git a/products/signals/backend/activity_summary/prompt.py b/products/signals/backend/activity_summary/prompt.py
new file mode 100644
index 000000000000..8f8a6d325548
--- /dev/null
+++ b/products/signals/backend/activity_summary/prompt.py
@@ -0,0 +1,172 @@
+"""The summary prompt, the output contract and the normalization of the model's answer.
+
+`summary_text` is the text that gets embedded, so its format is part of the contract with every
+vector already written. Bump `PROMPT_VERSION` when the prompt, the normalization or `summary_text`
+changes.
+"""
+
+import json
+import datetime
+from typing import Any
+
+PROMPT_VERSION = 2
+
+SUMMARY_MAX_WORDS = 60
+MAX_FOCUS_AREAS = 6
+MAX_PRODUCTS = 10
+# Per-field caps, so one runaway field cannot make the stored summary large.
+MAX_TOPIC_CHARS = 80
+MAX_PRODUCT_CHARS = 40
+MAX_RECENT_SHIFT_CHARS = 300
+STRENGTHS = ("high", "medium", "low")
+DEFAULT_STRENGTH = "low"
+# Strings a model writes in place of a JSON null.
+_NULL_STRINGS = frozenset({"", "none", "null", "n/a", "na", "nil"})
+
+SYSTEM_PROMPT = """You maintain a short summary of what one person has been working on in one PostHog project.
+You get their previous summary, if any, and a sample of their recent activity in up to two sections: tasks they gave to PostHog AI or the Slack agent, and things they created or changed in PostHog, with the reason their coding agent gave when an agent made the change.
+Update the summary to reflect what they are focused on now. Keep what is still true, drop what has gone quiet, and add what is new. Weigh themes that repeat across sections above one-off items. Treat bulk changes of the same kind as one theme, not a list.
+Name concrete products, features, metrics, pipelines and workflows. Never write generic phrases like "data analysis".
+Write about the work, not the person: no names, emails, IDs, secrets, or quotes from the input. Open with the work itself.
+Return JSON: `summary` (at most 60 words), `focus_areas` (up to 6, each with `topic`, `strength` high, medium or low, and `last_seen` as YYYY-MM-DD), `products`, and `recent_shift` (one sentence, or null).
+Set `recent_shift` only when there is a previous summary and the activity shows a real change from it. Otherwise it is null."""
+
+# Strict structured output. The normalization below still checks every rule, because a provider
+# behind the gateway may not enforce the schema.
+RESPONSE_FORMAT: Any = {
+ "type": "json_schema",
+ "json_schema": {
+ "name": "activity_summary",
+ "strict": True,
+ "schema": {
+ "type": "object",
+ "properties": {
+ "summary": {"type": "string"},
+ "focus_areas": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "topic": {"type": "string"},
+ "strength": {"type": "string", "enum": list(STRENGTHS)},
+ "last_seen": {"type": "string"},
+ },
+ "required": ["topic", "strength", "last_seen"],
+ "additionalProperties": False,
+ },
+ },
+ "products": {"type": "array", "items": {"type": "string"}},
+ "recent_shift": {"type": ["string", "null"]},
+ },
+ "required": ["summary", "focus_areas", "products", "recent_shift"],
+ "additionalProperties": False,
+ },
+ },
+}
+
+
+class MalformedSummaryError(ValueError):
+ """The model's answer cannot be turned into a summary. The message never quotes the answer."""
+
+
+def user_message(rendered_input: str, previous_summary: dict[str, Any] | None, as_of: datetime.date) -> str:
+ """The user turn: the day the activity ends, the previous summary and the rendered input.
+
+ The rendered input comes from `build_input` as is, and the previous summary is this pipeline's
+ own stored output, so no other text reaches the model."""
+ previous = (
+ json.dumps(
+ {key: previous_summary.get(key) for key in ("summary", "focus_areas", "products")},
+ ensure_ascii=False,
+ sort_keys=True,
+ )
+ if previous_summary
+ else "None"
+ )
+ return f"Today is {as_of.isoformat()}.\n\nPrevious summary:\n{previous}\n\nRecent activity:\n{rendered_input}"
+
+
+def _clean(value: Any, max_chars: int | None = None) -> str | None:
+ if not isinstance(value, str):
+ return None
+ text = " ".join(value.split())
+ if text.lower() in _NULL_STRINGS:
+ return None
+ return text[:max_chars].strip() if max_chars is not None else text
+
+
+def cap_words(text: str, max_words: int = SUMMARY_MAX_WORDS) -> str:
+ words = text.split()
+ return text if len(words) <= max_words else " ".join(words[:max_words])
+
+
+def _last_seen(value: Any) -> str | None:
+ text = _clean(value)
+ if text is None:
+ return None
+ try:
+ return datetime.date.fromisoformat(text).isoformat()
+ except ValueError:
+ return None
+
+
+def _focus_areas(value: Any) -> list[dict[str, str | None]]:
+ if not isinstance(value, list):
+ return []
+ areas: list[dict[str, str | None]] = []
+ for item in value:
+ if not isinstance(item, dict) or (topic := _clean(item.get("topic"), MAX_TOPIC_CHARS)) is None:
+ continue
+ strength = (_clean(item.get("strength")) or "").lower()
+ areas.append(
+ {
+ "topic": topic,
+ "strength": strength if strength in STRENGTHS else DEFAULT_STRENGTH,
+ "last_seen": _last_seen(item.get("last_seen")),
+ }
+ )
+ if len(areas) == MAX_FOCUS_AREAS:
+ break
+ return areas
+
+
+def _products(value: Any) -> list[str]:
+ if not isinstance(value, list):
+ return []
+ products: list[str] = []
+ for item in value:
+ product = _clean(item, MAX_PRODUCT_CHARS)
+ if product is not None and product not in products:
+ products.append(product)
+ return products[:MAX_PRODUCTS]
+
+
+def normalize_summary(content: str | None, *, had_previous_summary: bool) -> dict[str, Any]:
+ """The model's JSON answer under the output rules, or `MalformedSummaryError`.
+
+ Only a missing or empty `summary` makes an answer malformed. Every other field degrades to an
+ empty or null value, so one bad focus area does not cost the row."""
+ if not content:
+ raise MalformedSummaryError("empty response")
+ try:
+ parsed = json.loads(content)
+ except json.JSONDecodeError as error:
+ raise MalformedSummaryError("response is not JSON") from error
+ if not isinstance(parsed, dict):
+ raise MalformedSummaryError("response is not a JSON object")
+ summary = _clean(parsed.get("summary"))
+ if summary is None:
+ raise MalformedSummaryError("response has no summary")
+ recent_shift = _clean(parsed.get("recent_shift"), MAX_RECENT_SHIFT_CHARS) if had_previous_summary else None
+ return {
+ "summary": cap_words(summary),
+ "focus_areas": _focus_areas(parsed.get("focus_areas")),
+ "products": _products(parsed.get("products")),
+ "recent_shift": recent_shift,
+ }
+
+
+def summary_text(summary: dict[str, Any]) -> str:
+ """The text that gets embedded: the summary, then the focus area topics in the model's order."""
+ topics = [area["topic"] for area in summary.get("focus_areas", [])]
+ return f"{summary['summary']}\nFocus areas: {'; '.join(topics)}" if topics else summary["summary"]
diff --git a/products/signals/backend/activity_summary/render.py b/products/signals/backend/activity_summary/render.py
new file mode 100644
index 000000000000..bb29f45c05ce
--- /dev/null
+++ b/products/signals/backend/activity_summary/render.py
@@ -0,0 +1,391 @@
+"""Pure functions that turn one person's recent activity in one project into the input of an activity summary.
+
+Nothing here reads a database. `inputs.py` fetches the rows, and this module filters, caps, samples
+and renders them.
+
+Most activity recorded under a person is not theirs: scouts, agents and scheduled jobs act with the
+person's identity. Each filter is a named rule in `Drop`, and `build_input` counts what each rule
+removed, so a rule that starts dropping everything, or nothing, shows up in the output.
+"""
+
+import re
+import math
+import random
+import hashlib
+import datetime
+from collections import Counter
+from collections.abc import Iterable, Sequence
+from enum import StrEnum
+from urllib.parse import urlsplit
+
+from posthog.dataclasses import frozen
+
+# Bump on any change to the filters, the caps, the sampling or the rendered layout, so summaries stay comparable.
+RENDER_VERSION = 3
+
+WINDOW_DAYS = 28
+RECENCY_HALF_LIFE_DAYS = 7
+CHARS_PER_TOKEN = 4
+
+# Rows read per person before any filter, newest first. They bound query and memory cost for very
+# active people; the filters and caps below work on what is left.
+MAX_TASKS_PER_PERSON = 200
+MAX_ACTIVITY_ROWS_PER_PERSON = 1000
+
+# Units one source may keep from a single UTC day, newest first, so one busy afternoon cannot fill a section.
+MAX_UNITS_PER_DAY = 3
+TASK_LINES = 12
+ACTIVITY_LINES = 15
+# Per-line caps, so one runaway title or intent cannot take the budget. With them a full prompt
+# section stays under SECTIONS_MAX_CHARS; the hard stop is the backstop if the caps change.
+TASK_LINE_CHARS = 260
+TASK_GOAL_CHARS = 180
+TASK_TITLE_CHARS = 100
+MAX_TAGS = 5
+MAX_TAG_CHARS = 30
+ACTIVITY_LINE_CHARS = 220
+ACTIVITY_NAME_CHARS = 70
+ACTIVITY_INTENT_CHARS = 100
+SECTIONS_MAX_CHARS = 8000
+
+# Masking and whitespace folding shorten a text, so the patterns read this many times its cap.
+RAW_TEXT_FACTOR = 4
+# Task summaries run to a few hundred characters; the cap bounds the regex work on a long one.
+MAX_SUMMARY_CHARS = 2000
+# A task title with no summary needs this many words to say anything ("root cause" does not).
+MIN_BARE_TITLE_WORDS = 8
+
+TASKS_HEADER = "Tasks the person gave to PostHog AI or the Slack agent ([tags] goal):"
+ACTIVITY_HEADER = "Things the person created or changed in PostHog (agent: why their coding agent did it):"
+
+# Helper tasks that a task spawns for itself, such as repository selection, carry the parent's origin.
+SANDBOX_PROMPT_TITLE = re.compile(r"^\[sandbox_prompt:")
+# Titles a button or a workflow writes, with only an id changing. Each collapses to one line with a count.
+TEMPLATED_TASK_TITLES: tuple[re.Pattern[str], ...] = (re.compile(r"^Fix failing CI checks in PR #\d+$"),)
+_TEMPLATE_ID = re.compile(r"#\d+")
+_SUMMARY_LABEL = re.compile(r"^(?:Goal|Asked|Question)\s*:\s*", re.IGNORECASE)
+_SENTENCE_END = re.compile(r"(?<=[.!?])\s+")
+
+# Scopes agents write in bulk, such as tags applied to every report they touch.
+NOISY_ACTIVITY_SCOPES: frozenset[str] = frozenset({"TaggedItem", "Tag"})
+# A scout run writes its changes with this client tag, which the caller cannot set.
+SCOUT_CLIENT_PREFIX = "scout:"
+# Names that a scheduled job stamps with a date, such as a notebook regenerated every day.
+_AUTO_NAME = re.compile(r"\b\d{4}-\d{2}-\d{2}\b|model report", re.IGNORECASE)
+
+_EMAIL = re.compile(r"[\w.+-]+@[\w-]+(?:\.[\w-]+)+")
+_URL = re.compile(r"<?https?://[^\s<>]+>?")
+# Long runs of word characters: a run that mixes letters and digits is an API key, session token,
+# hash or UUID. The digit and letter checks run in `_mask_token`, because lookaheads in the pattern
+# rescan the run at every word boundary inside it and take quadratic time on a long input.
+_WORD_RUN = re.compile(r"[\w-]{24,}")
+_DIGIT = re.compile(r"\d")
+_LETTER = re.compile(r"[A-Za-z]")
+_WHITESPACE = re.compile(r"\s+")
+
+
+class Drop(StrEnum):
+ """Why a unit did not reach the prompt."""
+
+ SANDBOX_PROMPT_TASK = "sandbox_prompt_task"
+ BARE_TITLE = "bare_title"
+ NOISY_SCOPE = "noisy_scope"
+ SCOUT_CLIENT = "scout_client"
+ AUTO_NAMED_OBJECT = "auto_named_object"
+ UNNAMED_OBJECT = "unnamed_object"
+ DUPLICATE = "duplicate"
+ BURST_CAP = "burst_cap"
+ SAMPLE_CAP = "sample_cap"
+ SECTIONS_BUDGET = "sections_budget"
+
+
+@frozen
+class TaskItem:
+ """A task the person started, with its latest run's summary and tags."""
+
+ at: datetime.datetime
+ title: str
+ summary: str | None
+ tags: tuple[str, ...]
+
+
+@frozen
+class ActivityItem:
+ """One activity log row the person made, with the intent their agent sent, if any."""
+
+ at: datetime.datetime
+ scope: str
+ activity: str
+ name: str | None
+ client: str | None
+ intent: str | None = None
+
+
+@frozen
+class Unit:
+ """One rendered line, dated by its latest activity."""
+
+ at: datetime.datetime
+ text: str
+
+
+@frozen
+class RenderedInput:
+ text: str
+ n_tasks: int
+ n_activity_lines: int
+ task_chars: int
+ activity_chars: int
+ input_chars: int
+ input_tokens_estimate: int
+ drops: dict[str, int]
+
+ @property
+ def units(self) -> int:
+ return self.n_tasks + self.n_activity_lines
+
+
+def _mask_token(match: re.Match[str]) -> str:
+ run = match.group()
+ return "<token>" if _DIGIT.search(run) and _LETTER.search(run) else run
+
+
+def _url_host(match: re.Match[str]) -> str:
+ """Only the host of a URL: a secret can sit in a path, a query string or a fragment."""
+ try:
+ host = urlsplit(match.group().strip("<>")).hostname
+ except ValueError:
+ host = None
+ return f"[link {host}]" if host else "[link]"
+
+
+def bounded_raw(text: str, max_chars: int) -> str:
+ """At most `max_chars` of the text, without a word that the cut splits.
+
+ A split secret is too short for the masking patterns to match, so the partial word goes too."""
+ if len(text) <= max_chars:
+ return text
+ end = max_chars
+ if not text[end].isspace():
+ while end > 0 and not text[end - 1].isspace():
+ end -= 1
+ return text[:end]
+
+
+def clean_text(text: str, max_chars: int) -> str:
+ """Replace emails, URLs and token-like strings, fold the text onto one line, and cut it to `max_chars`.
+
+ A URL keeps only its host. The cut comes after the masking, so it cannot leave part of a secret
+ unmasked. The patterns read a bounded prefix, because the free text has no length limit."""
+ text = bounded_raw(text, max_chars * RAW_TEXT_FACTOR)
+ text = _URL.sub(_url_host, text)
+ text = _EMAIL.sub("<email>", text)
+ text = _WORD_RUN.sub(_mask_token, text)
+ return _WHITESPACE.sub(" ", text).strip()[:max_chars].strip()
+
+
+def cap_line(text: str, max_chars: int) -> str:
+ """Cut at a word boundary and mark the cut."""
+ if len(text) <= max_chars:
+ return text
+ cut = text[: max_chars - 1]
+ return (cut.rsplit(" ", 1)[0] if " " in cut else cut) + "…"
+
+
+def _day(at: datetime.datetime) -> datetime.date:
+ return at.astimezone(datetime.UTC).date()
+
+
+def summary_goal(summary: str) -> str:
+ """The opening sentences of a task summary, which state what was asked, without the label."""
+ text = _SUMMARY_LABEL.sub("", clean_text(bounded_raw(summary, MAX_SUMMARY_CHARS), MAX_SUMMARY_CHARS))
+ goal = ""
+ for sentence in _SENTENCE_END.split(text):
+ if goal and len(goal) + len(sentence) + 1 > TASK_GOAL_CHARS:
+ break
+ goal = f"{goal} {sentence}".strip()
+ return cap_line(goal, TASK_GOAL_CHARS)
+
+
+def _templated_label(title: str) -> str | None:
+ for pattern in TEMPLATED_TASK_TITLES:
+ if pattern.match(title):
+ return _TEMPLATE_ID.sub("#N", title)
+ return None
+
+
+def task_units(tasks: Iterable[TaskItem], drops: Counter[str]) -> list[Unit]:
+ """One line per task, with templated titles collapsed to one line per template."""
+ units: list[Unit] = []
+ templated: dict[str, list[datetime.datetime]] = {}
+ for task in tasks:
+ title = task.title.strip()
+ if SANDBOX_PROMPT_TITLE.match(title):
+ drops[Drop.SANDBOX_PROMPT_TASK] += 1
+ continue
+ if (label := _templated_label(title)) is not None:
+ templated.setdefault(label, []).append(task.at)
+ continue
+ day = _day(task.at).isoformat()
+ if task.summary:
+ tags = [cap_line(clean_text(tag, MAX_TAG_CHARS * 2), MAX_TAG_CHARS) for tag in task.tags[:MAX_TAGS]]
+ tag_text = f"[{', '.join(tag for tag in tags if tag)}] " if any(tags) else ""
+ line = f"- {day} {tag_text}{summary_goal(task.summary)}"
+ elif len(title.split()) >= MIN_BARE_TITLE_WORDS:
+ line = f"- {day} {cap_line(clean_text(title, TASK_TITLE_CHARS * 2), TASK_TITLE_CHARS)}"
+ else:
+ drops[Drop.BARE_TITLE] += 1
+ continue
+ units.append(Unit(at=task.at, text=cap_line(line, TASK_LINE_CHARS)))
+ for label, times in templated.items():
+ latest = max(times)
+ units.append(
+ Unit(at=latest, text=cap_line(f"- {_day(latest).isoformat()} {label} (x{len(times)})", TASK_LINE_CHARS))
+ )
+ return units
+
+
+def activity_units(items: Iterable[ActivityItem], drops: Counter[str]) -> list[Unit]:
+ """Changes collapsed to one line per object, creates first, then the most changed, then the newest.
+
+ A line carries the newest agent intent of its rows. Lines are already collapsed per object, so
+ they skip the per-day burst cap: a busy day of changes to different objects lands on one date
+ and the cap would keep only a few of them."""
+ collapsed: dict[tuple[str, str, str], list[ActivityItem]] = {}
+ for item in items:
+ name = (item.name or "").strip()
+ if item.scope in NOISY_ACTIVITY_SCOPES:
+ drops[Drop.NOISY_SCOPE] += 1
+ elif (item.client or "").startswith(SCOUT_CLIENT_PREFIX):
+ drops[Drop.SCOUT_CLIENT] += 1
+ elif not name:
+ drops[Drop.UNNAMED_OBJECT] += 1
+ elif _AUTO_NAME.search(name):
+ drops[Drop.AUTO_NAMED_OBJECT] += 1
+ else:
+ collapsed.setdefault((item.scope, item.activity, name), []).append(item)
+ ranked = sorted(
+ collapsed.items(),
+ key=lambda entry: (
+ entry[0][1] != "created",
+ -len(entry[1]),
+ -max(row.at for row in entry[1]).timestamp(),
+ ),
+ )
+ if len(ranked) > ACTIVITY_LINES:
+ drops[Drop.SAMPLE_CAP] += len(ranked) - ACTIVITY_LINES
+ units: list[Unit] = []
+ for (scope, activity, name), rows in ranked[:ACTIVITY_LINES]:
+ latest = max(row.at for row in rows)
+ count = f" (x{len(rows)})" if len(rows) > 1 else ""
+ name_text = cap_line(clean_text(name, ACTIVITY_NAME_CHARS * 2), ACTIVITY_NAME_CHARS)
+ line = f'- {_day(latest).isoformat()} {activity} {scope} "{name_text}"{count}'
+ intents = [row for row in rows if row.intent and row.intent.strip()]
+ if intents:
+ intent = max(intents, key=lambda row: row.at).intent or ""
+ intent_text = cap_line(clean_text(intent, ACTIVITY_INTENT_CHARS * 2), ACTIVITY_INTENT_CHARS)
+ if intent_text:
+ line += f" (agent: {intent_text})"
+ units.append(Unit(at=latest, text=cap_line(line, ACTIVITY_LINE_CHARS)))
+ return sorted(units, key=lambda unit: (unit.at, unit.text))
+
+
+def dedupe_latest(units: Iterable[Unit], drops: Counter[str]) -> list[Unit]:
+ """One unit per distinct text, the latest one, in chronological order."""
+ latest: dict[str, Unit] = {}
+ for unit in units:
+ if unit.text in latest:
+ drops[Drop.DUPLICATE] += 1
+ if unit.text not in latest or unit.at > latest[unit.text].at:
+ latest[unit.text] = unit
+ return sorted(latest.values(), key=lambda unit: (unit.at, unit.text))
+
+
+def burst_cap(units: Sequence[Unit], drops: Counter[str]) -> list[Unit]:
+ """At most `MAX_UNITS_PER_DAY` units per UTC day, newest first, in chronological order."""
+ kept: list[Unit] = []
+ per_day: Counter[datetime.date] = Counter()
+ for unit in sorted(units, key=lambda unit: (unit.at, unit.text), reverse=True):
+ if per_day[_day(unit.at)] >= MAX_UNITS_PER_DAY:
+ drops[Drop.BURST_CAP] += 1
+ continue
+ per_day[_day(unit.at)] += 1
+ kept.append(unit)
+ return sorted(kept, key=lambda unit: (unit.at, unit.text))
+
+
+def sampling_seed(seed_key: str, team_id: int, user_id: int) -> int:
+ # Not hash(): Python salts it per process, and a re-run must pick the same units.
+ digest = hashlib.sha256(f"{seed_key}:{team_id}:{user_id}".encode()).digest()
+ return int.from_bytes(digest[:8], "big")
+
+
+def sample_units(units: Sequence[Unit], *, limit: int, as_of: datetime.datetime, seed: int) -> list[Unit]:
+ """Up to `limit` units without replacement, weighted towards recent ones, in chronological order.
+
+ Weighted reservoir keys (Efraimidis and Spirakis): each unit draws `u ** (1 / weight)` and the
+ largest keys win, so one busy afternoon cannot fill every slot."""
+ if len(units) <= limit:
+ return sorted(units, key=lambda unit: (unit.at, unit.text))
+ rng = random.Random(seed)
+ keyed: list[tuple[float, Unit]] = []
+ for unit in sorted(units, key=lambda unit: (unit.at, unit.text)):
+ age_days = max((as_of - unit.at).total_seconds(), 0.0) / 86400
+ weight = 0.5 ** (age_days / RECENCY_HALF_LIFE_DAYS)
+ keyed.append((rng.random() ** (1 / weight), unit))
+ chosen = sorted(keyed, key=lambda pair: pair[0], reverse=True)[:limit]
+ return sorted((unit for _, unit in chosen), key=lambda unit: (unit.at, unit.text))
+
+
+def estimate_tokens(chars: int) -> int:
+ return math.ceil(chars / CHARS_PER_TOKEN)
+
+
+def _section(header: str, units: Sequence[Unit]) -> str:
+ return "\n".join([header, *(unit.text for unit in units)]) if units else ""
+
+
+def _sections_chars(sections: Sequence[tuple[str, list[Unit]]]) -> int:
+ return sum(len(_section(header, units)) + 2 for header, units in sections)
+
+
+def enforce_budget(sections: list[tuple[str, list[Unit]]], drops: Counter[str]) -> None:
+ """Drop the oldest unit across sections until the rendered sections fit `SECTIONS_MAX_CHARS`."""
+ while _sections_chars(sections) > SECTIONS_MAX_CHARS:
+ oldest = min((units[0].at, index) for index, (_, units) in enumerate(sections) if units)
+ sections[oldest[1]][1].pop(0)
+ drops[Drop.SECTIONS_BUDGET] += 1
+
+
+def build_input(
+ tasks: Iterable[TaskItem],
+ activity: Iterable[ActivityItem],
+ *,
+ as_of: datetime.datetime,
+ seed: int,
+) -> RenderedInput | None:
+ """The rendered input of one (project, person) pair, or None when nothing survives the filters.
+
+ Order: the named filters, then the burst cap, then the recency-weighted sample, then the hard
+ budget, so the sample is drawn from clean units only."""
+ drops: Counter[str] = Counter()
+ task_pool = burst_cap(dedupe_latest(task_units(tasks, drops), drops), drops)
+ activity_lines = activity_units(activity, drops)
+ if not task_pool and not activity_lines:
+ return None
+ chosen_tasks = sample_units(task_pool, limit=TASK_LINES, as_of=as_of, seed=seed)
+ drops[Drop.SAMPLE_CAP] += len(task_pool) - len(chosen_tasks)
+ sections = [(TASKS_HEADER, chosen_tasks), (ACTIVITY_HEADER, activity_lines)]
+ enforce_budget(sections, drops)
+ rendered = [_section(header, units) for header, units in sections]
+ text = "\n\n".join(part for part in rendered if part)
+ return RenderedInput(
+ text=text,
+ n_tasks=len(sections[0][1]),
+ n_activity_lines=len(sections[1][1]),
+ task_chars=len(rendered[0]),
+ activity_chars=len(rendered[1]),
+ input_chars=len(text),
+ input_tokens_estimate=estimate_tokens(len(text)),
+ drops={str(reason): count for reason, count in sorted(drops.items()) if count},
+ )
diff --git a/products/signals/backend/activity_summary/summaries.py b/products/signals/backend/activity_summary/summaries.py
new file mode 100644
index 000000000000..9c279d645e2b
--- /dev/null
+++ b/products/signals/backend/activity_summary/summaries.py
@@ -0,0 +1,141 @@
+"""The LLM step of an activity summary update: one call, its tokens and its cost.
+
+The call goes through `build_openai_client`, so the deployment picks the gateway: the Go AI gateway
+when its settings are set, else the Python gateway on the `signals` route.
+"""
+
+import time
+import datetime
+from typing import Any
+
+from openai import OpenAI, OpenAIError
+from openai.types.chat import ChatCompletionMessageParam
+
+from posthog.dataclasses import frozen
+from posthog.llm.gateway_client import build_openai_client, trace_id_from_seed
+
+from products.signals.backend.activity_summary.prompt import (
+ RESPONSE_FORMAT,
+ SYSTEM_PROMPT,
+ MalformedSummaryError,
+ normalize_summary,
+ user_message,
+)
+
+AI_PRODUCT = "activity_summary"
+# Not a person: the gateway attributes every call to the pipeline.
+PIPELINE_DISTINCT_ID = "signals_activity_summary"
+DEFAULT_MODEL = "gpt-6-luna"
+# One retry on a malformed or truncated answer. The SDK already retries a failed request on its own.
+MAX_ATTEMPTS = 2
+REQUEST_TIMEOUT_SECONDS = 120.0
+# A full answer is about 300 tokens. The cap bounds the cost of a model that does not stop.
+MAX_COMPLETION_TOKENS = 600
+TRUNCATED_FINISH_REASON = "length"
+
+
+@frozen
+class SummaryPrices:
+ input_price_per_million_tokens: float | None
+ output_price_per_million_tokens: float | None
+
+
+# Per million tokens. A model that is not here gets a null cost, not a guess.
+MODEL_PRICES: dict[str, SummaryPrices] = {
+ DEFAULT_MODEL: SummaryPrices(input_price_per_million_tokens=0.10, output_price_per_million_tokens=0.50),
+}
+
+
+def cost_usd(tokens_in: int | None, tokens_out: int | None, prices: SummaryPrices | None) -> float | None:
+ """None without both token counts and both prices, so an unknown usage or price records no guess."""
+ if tokens_in is None or tokens_out is None or prices is None:
+ return None
+ if prices.input_price_per_million_tokens is None or prices.output_price_per_million_tokens is None:
+ return None
+ return (
+ tokens_in * prices.input_price_per_million_tokens + tokens_out * prices.output_price_per_million_tokens
+ ) / 1_000_000
+
+
+@frozen
+class SummaryOutcome:
+ summary: dict[str, Any] | None
+ # Null when no response reported its usage.
+ tokens_in: int | None
+ tokens_out: int | None
+ latency_ms: int
+ error: str | None
+
+
+def gateway_client(team_id: int, user_id: int, as_of: datetime.date) -> OpenAI:
+ return build_openai_client(
+ "signals",
+ ai_product=AI_PRODUCT,
+ trace_id=trace_id_from_seed(f"{AI_PRODUCT}:{team_id}:{user_id}:{as_of.isoformat()}"),
+ properties={"team_id": str(team_id)},
+ distinct_id=PIPELINE_DISTINCT_ID,
+ )
+
+
+def _add(total: int | None, value: int | None) -> int | None:
+ return value if total is None else total + (value or 0)
+
+
+def summarize(
+ client: OpenAI,
+ *,
+ rendered_input: str,
+ previous_summary: dict[str, Any] | None,
+ model: str,
+ as_of: datetime.date,
+) -> SummaryOutcome:
+ """One summary. Tokens and latency add up over the attempts, because the retry costs too."""
+ messages: list[ChatCompletionMessageParam] = [
+ {"role": "system", "content": SYSTEM_PROMPT},
+ {"role": "user", "content": user_message(rendered_input, previous_summary, as_of)},
+ ]
+ tokens_in: int | None = None
+ tokens_out: int | None = None
+ error: str | None = None
+ started = time.monotonic()
+ for _ in range(MAX_ATTEMPTS):
+ try:
+ response = client.chat.completions.create(
+ model=model,
+ messages=messages,
+ user=PIPELINE_DISTINCT_ID,
+ response_format=RESPONSE_FORMAT,
+ max_completion_tokens=MAX_COMPLETION_TOKENS,
+ timeout=REQUEST_TIMEOUT_SECONDS,
+ )
+ except OpenAIError as api_error:
+ error = f"api_error: {type(api_error).__name__}"
+ break
+ if response.usage is not None:
+ tokens_in = _add(tokens_in, response.usage.prompt_tokens)
+ tokens_out = _add(tokens_out, response.usage.completion_tokens)
+ choice = response.choices[0] if response.choices else None
+ try:
+ if choice is not None and choice.finish_reason == TRUNCATED_FINISH_REASON:
+ raise MalformedSummaryError("response was truncated")
+ summary = normalize_summary(
+ choice.message.content if choice is not None else None,
+ had_previous_summary=previous_summary is not None,
+ )
+ except MalformedSummaryError as malformed:
+ error = f"malformed_output: {malformed}"
+ continue
+ return SummaryOutcome(
+ summary=summary,
+ tokens_in=tokens_in,
+ tokens_out=tokens_out,
+ latency_ms=round((time.monotonic() - started) * 1000),
+ error=None,
+ )
+ return SummaryOutcome(
+ summary=None,
+ tokens_in=tokens_in,
+ tokens_out=tokens_out,
+ latency_ms=round((time.monotonic() - started) * 1000),
+ error=error,
+ )
diff --git a/products/signals/backend/management/commands/run_activity_summary.py b/products/signals/backend/management/commands/run_activity_summary.py
new file mode 100644
index 000000000000..2589b8b140cc
--- /dev/null
+++ b/products/signals/backend/management/commands/run_activity_summary.py
@@ -0,0 +1,40 @@
+"""Run one activity summary update for one (team, user) and print what went in and what came out.
+
+The local loop for the filters and the prompt: `--dry-run` calls the model but writes nothing.
+"""
+
+import json
+
+from django.core.management.base import BaseCommand
+
+from products.signals.backend.activity_summary.logic import update_activity_summary
+
+
+class Command(BaseCommand):
+ help = "Update the activity summary of one user in one project, and print the input, output and cost."
+
+ def add_arguments(self, parser):
+ parser.add_argument("--team-id", type=int, required=True)
+ parser.add_argument("--user-id", type=int, required=True)
+ parser.add_argument("--dry-run", action="store_true", help="Call the model, but write nothing.")
+ parser.add_argument("--force", action="store_true", help="Ignore next_eligible_at.")
+
+ def handle(self, *args, **options):
+ result = update_activity_summary(
+ options["team_id"], options["user_id"], dry_run=options["dry_run"], force=options["force"]
+ )
+ self.stdout.write(f"status: {result.status}")
+ if result.rendered_input is not None:
+ self.stdout.write(f"\nrendered input:\n{result.rendered_input}\n")
+ if result.summary is not None:
+ self.stdout.write(f"summary:\n{json.dumps(result.summary, indent=2, ensure_ascii=False)}\n")
+ if result.error is not None:
+ self.stdout.write(f"error: {result.error}")
+ self.stdout.write(
+ f"tokens in: {result.tokens_in}, tokens out: {result.tokens_out}, "
+ f"cost usd: {result.cost_usd}, latency ms: {result.latency_ms}"
+ )
+ if result.inputs_used is not None:
+ self.stdout.write(f"inputs used: {json.dumps(result.inputs_used, sort_keys=True)}")
+ if result.input_watermark is not None:
+ self.stdout.write(f"input watermark: {result.input_watermark.isoformat()}")
diff --git a/products/signals/backend/migrations/0187_useractivitysummary.py b/products/signals/backend/migrations/0187_useractivitysummary.py
new file mode 100644
index 000000000000..01e3c39d2fca
--- /dev/null
+++ b/products/signals/backend/migrations/0187_useractivitysummary.py
@@ -0,0 +1,89 @@
+# Generated by Django 5.2.18 on 2026-10-20 15:44
+
+import django.db.models.manager
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+import posthog.uuidt
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1404_organization_inactive_quota_index"),
+ ("signals", "0186_signalteamconfig_onboarding"),
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="UserActivitySummary",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.uuid7,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ ("summary", models.JSONField(blank=True, null=True)),
+ ("summary_text", models.TextField(blank=True, null=True)),
+ ("generated_at", models.DateTimeField(blank=True, null=True)),
+ ("model", models.CharField(blank=True, max_length=100, null=True)),
+ (
+ "prompt_version",
+ models.PositiveSmallIntegerField(blank=True, null=True),
+ ),
+ (
+ "render_version",
+ models.PositiveSmallIntegerField(blank=True, null=True),
+ ),
+ ("tokens_in", models.IntegerField(blank=True, null=True)),
+ ("tokens_out", models.IntegerField(blank=True, null=True)),
+ ("cost_usd", models.FloatField(blank=True, null=True)),
+ ("latency_ms", models.IntegerField(blank=True, null=True)),
+ ("inputs_used", models.JSONField(blank=True, default=dict)),
+ (
+ "status",
+ models.CharField(choices=[("ok", "Ok"), ("failed", "Failed")], max_length=20),
+ ),
+ ("error", models.CharField(blank=True, max_length=200, null=True)),
+ ("input_watermark", models.DateTimeField(blank=True, null=True)),
+ ("next_eligible_at", models.DateTimeField(blank=True, null=True)),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ ("updated_at", models.DateTimeField(auto_now=True)),
+ (
+ "team",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to="posthog.team",
+ ),
+ ),
+ (
+ "user",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to=settings.AUTH_USER_MODEL,
+ ),
+ ),
+ ],
+ options={
+ "default_manager_name": "all_teams",
+ "constraints": [
+ models.UniqueConstraint(
+ fields=("team", "user"),
+ name="signals_activity_summary_team_user",
+ )
+ ],
+ },
+ managers=[
+ ("all_teams", django.db.models.manager.Manager()),
+ ],
+ ),
+ ]
diff --git a/products/signals/backend/migrations/max_migration.txt b/products/signals/backend/migrations/max_migration.txt
index 3cae2362d06e..13afc68b1d76 100644
--- a/products/signals/backend/migrations/max_migration.txt
+++ b/products/signals/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0186_signalteamconfig_onboarding
+0187_useractivitysummary
diff --git a/products/signals/backend/models.py b/products/signals/backend/models.py
index 5e98ae513687..8018d03cce3b 100644
--- a/products/signals/backend/models.py
+++ b/products/signals/backend/models.py
@@ -3720,3 +3720,56 @@ class SignalSpend(TeamScopedRootMixin, UUIDModel):
indexes = [
models.Index(fields=["team", "signal_id"], name="signals_spend_signal_idx"),
]
+
+
+class UserActivitySummary(TeamScopedRootMixin, UUIDModel):
+ """One rolling LLM summary of what one user has been doing in one project.
+
+ `update_activity_summary` in `activity_summary/logic.py` writes it from the user's own tasks and
+ activity log rows. A failed update records the error and keeps the last good summary and the
+ watermark, so the next update reads the same inputs again.
+ """
+
+ class Status(models.TextChoices):
+ OK = "ok"
+ FAILED = "failed"
+
+ # See SignalReportRefund.all_teams for rationale.
+ all_teams = models.Manager() # noqa: DJ012
+
+ # FKs to the hot posthog_team / posthog_user tables use db_constraint=False so creating this
+ # table takes no lock on those parents. Django still cascades a delete of either parent.
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False, related_name="+")
+ user = models.ForeignKey("posthog.User", on_delete=models.CASCADE, db_constraint=False, related_name="+")
+
+ # Output: the normalized answer (summary, focus_areas, products, recent_shift) and its embeddable text.
+ summary = models.JSONField(null=True, blank=True)
+ summary_text = models.TextField(null=True, blank=True)
+ generated_at = models.DateTimeField(null=True, blank=True)
+ model = models.CharField(max_length=100, null=True, blank=True)
+ prompt_version = models.PositiveSmallIntegerField(null=True, blank=True)
+ render_version = models.PositiveSmallIntegerField(null=True, blank=True)
+
+ # Accounting of the latest update. Null when the response had no usage or the model has no price.
+ tokens_in = models.IntegerField(null=True, blank=True)
+ tokens_out = models.IntegerField(null=True, blank=True)
+ cost_usd = models.FloatField(null=True, blank=True)
+ latency_ms = models.IntegerField(null=True, blank=True)
+ # Line counts per section and drop counts per filter rule of the latest update.
+ inputs_used = models.JSONField(default=dict, blank=True)
+
+ status = models.CharField(max_length=20, choices=Status)
+ # One of the fixed error strings of `summaries.py`, never model output.
+ error = models.CharField(max_length=200, null=True, blank=True)
+ # The newest input timestamp the stored summary includes. The next update reads only later rows.
+ input_watermark = models.DateTimeField(null=True, blank=True)
+ next_eligible_at = models.DateTimeField(null=True, blank=True)
+
+ created_at = models.DateTimeField(auto_now_add=True)
+ updated_at = models.DateTimeField(auto_now=True)
+
+ class Meta:
+ default_manager_name = "all_teams"
+ constraints = [
+ models.UniqueConstraint(fields=["team", "user"], name="signals_activity_summary_team_user"),
+ ]
diff --git a/products/signals/backend/test/test_activity_summary_render.py b/products/signals/backend/test/test_activity_summary_render.py
new file mode 100644
index 000000000000..1a7824b02558
--- /dev/null
+++ b/products/signals/backend/test/test_activity_summary_render.py
@@ -0,0 +1,486 @@
+import json
+import datetime
+from collections import Counter
+from types import SimpleNamespace
+from typing import Any
+
+import pytest
+
+from openai import APIConnectionError
+from parameterized import parameterized
+
+from products.signals.backend.activity_summary.prompt import (
+ MAX_PRODUCT_CHARS,
+ MAX_RECENT_SHIFT_CHARS,
+ MAX_TOPIC_CHARS,
+ MalformedSummaryError,
+ normalize_summary,
+ summary_text,
+ user_message,
+)
+from products.signals.backend.activity_summary.render import (
+ ACTIVITY_HEADER,
+ ACTIVITY_LINE_CHARS,
+ ACTIVITY_LINES,
+ MAX_UNITS_PER_DAY,
+ SECTIONS_MAX_CHARS,
+ TASK_LINE_CHARS,
+ TASK_LINES,
+ TASKS_HEADER,
+ ActivityItem,
+ Drop,
+ TaskItem,
+ Unit,
+ activity_units,
+ build_input,
+ burst_cap,
+ clean_text,
+ sample_units,
+ sampling_seed,
+ task_units,
+)
+from products.signals.backend.activity_summary.summaries import (
+ MAX_COMPLETION_TOKENS,
+ PIPELINE_DISTINCT_ID,
+ SummaryPrices,
+ cost_usd,
+ summarize,
+)
+
+AS_OF = datetime.datetime(2026, 10, 19, tzinfo=datetime.UTC)
+PATH_SECRET = "https://hooks.example.com/services/T000/B000/s3cr3tPathValue?sig=abc"
+
+
+def _at(days_ago: float, minutes: int = 0) -> datetime.datetime:
+ return AS_OF - datetime.timedelta(days=days_ago) + datetime.timedelta(minutes=minutes)
+
+
+def _task(title: str, summary: str | None = None, tags: tuple[str, ...] = (), days_ago: float = 1) -> TaskItem:
+ return TaskItem(at=_at(days_ago), title=title, summary=summary, tags=tags)
+
+
+def _change(
+ scope: str,
+ activity: str,
+ name: str | None,
+ client: str | None = None,
+ days_ago: float = 1,
+ intent: str | None = None,
+) -> ActivityItem:
+ return ActivityItem(at=_at(days_ago), scope=scope, activity=activity, name=name, client=client, intent=intent)
+
+
+ [
+ (
+ "a summary gives the goal, without its label, and the tags",
+ _task("why", summary="Goal: find why weekly signups dropped. Done: found a broken form.", tags=("growth",)),
+ "- 2026-10-18 [growth] find why weekly signups dropped. Done: found a broken form.",
+ {},
+ ),
+ (
+ "a link in a summary keeps only its host",
+ _task("why", summary=f"Goal: post the alert to {PATH_SECRET} for the team."),
+ "- 2026-10-18 post the alert to [link hooks.example.com] for the team.",
+ {},
+ ),
+ (
+ "a helper task is dropped",
+ _task("[sandbox_prompt:repo_selection] You are a selection agent"),
+ None,
+ {Drop.SANDBOX_PROMPT_TASK: 1},
+ ),
+ ("a short title with no summary says nothing", _task("root cause"), None, {Drop.BARE_TITLE: 1}),
+ (
+ "a long title with no summary is kept",
+ _task("compare the retention of the two pricing plans over the last quarter"),
+ "- 2026-10-18 compare the retention of the two pricing plans over the last quarter",
+ {},
+ ),
+ ]
+)
+def test_task_units(_name: str, task: TaskItem, expected: str | None, expected_drops: dict[str, int]) -> None:
+ drops: Counter[str] = Counter()
+ units = task_units([task], drops)
+ assert [unit.text for unit in units] == ([expected] if expected else [])
+ assert dict(drops) == expected_drops
+
+
+def test_templated_task_titles_collapse_to_one_line_with_a_count() -> None:
+ tasks = [_task(f"Fix failing CI checks in PR #{number}", days_ago=number) for number in (1, 2, 3)]
+ units = task_units(tasks, Counter())
+ assert [unit.text for unit in units] == ["- 2026-10-18 Fix failing CI checks in PR #N (x3)"]
+
+
+def test_activity_units_filter_collapse_and_rank_creates_first() -> None:
+ drops: Counter[str] = Counter()
+ items = [
+ _change("TaggedItem", "created", "growth"),
+ _change("Insight", "created", "Signup funnel", client="scout:feature-flags"),
+ _change("Notebook", "updated", "Daily report 2026-10-18"),
+ _change("AlertConfiguration", "created", None),
+ _change("Notebook", "updated", "Pricing notes", days_ago=1),
+ _change("Notebook", "updated", "Pricing notes", days_ago=2),
+ _change("Dashboard", "created", "Growth overview", days_ago=3),
+ ]
+
+ units = activity_units(items, drops)
+
+ assert [unit.text for unit in units] == [
+ '- 2026-10-16 created Dashboard "Growth overview"',
+ '- 2026-10-18 updated Notebook "Pricing notes" (x2)',
+ ]
+ assert dict(drops) == {
+ Drop.NOISY_SCOPE: 1,
+ Drop.SCOUT_CLIENT: 1,
+ Drop.AUTO_NAMED_OBJECT: 1,
+ Drop.UNNAMED_OBJECT: 1,
+ }
+
+
+ [
+ ("no intent renders as before", [None], '- 2026-10-18 updated Insight "Signup funnel"'),
+ (
+ "an intent renders next to the object",
+ ["Split the funnel by plan"],
+ '- 2026-10-18 updated Insight "Signup funnel" (agent: Split the funnel by plan)',
+ ),
+ (
+ "collapsed rows carry the newest intent",
+ ["Add a breakdown", None, "Split the funnel by plan"],
+ '- 2026-10-18 updated Insight "Signup funnel" (x3) (agent: Add a breakdown)',
+ ),
+ (
+ "an intent keeps only the host of a link",
+ [f"Send the funnel to {PATH_SECRET}"],
+ '- 2026-10-18 updated Insight "Signup funnel" (agent: Send the funnel to [link hooks.example.com])',
+ ),
+ ]
+)
+def test_activity_units_render_the_agent_intent(_name: str, intents: list[str | None], expected: str) -> None:
+ items = [
+ _change("Insight", "updated", "Signup funnel", days_ago=1 + index * 0.01, intent=intent)
+ for index, intent in enumerate(intents)
+ ]
+ assert [unit.text for unit in activity_units(items, Counter())] == [expected]
+
+
+def test_burst_cap_keeps_the_newest_units_of_a_day() -> None:
+ drops: Counter[str] = Counter()
+ units = [Unit(at=_at(1, minutes=minute), text=f"unit {minute}") for minute in range(MAX_UNITS_PER_DAY + 2)]
+
+ kept = burst_cap(units, drops)
+
+ assert [unit.text for unit in kept] == [f"unit {minute}" for minute in range(2, MAX_UNITS_PER_DAY + 2)]
+ assert drops[Drop.BURST_CAP] == 2
+
+
+ [
+ ("email", "Ask [email protected] about it", "Ask <email> about it"),
+ (
+ "a url keeps only its host, without its path or query",
+ "Open https://example.com/insights/abc?token=xyz&a=1 now",
+ "Open [link example.com] now",
+ ),
+ ("a secret in a url path is gone", f"See {PATH_SECRET}", "See [link hooks.example.com]"),
+ ("an angle-bracketed slack link", "See <https://example.com/a/b> now", "See [link example.com] now"),
+ ("credentials in a url are gone", "Use https://user:[email protected]/x", "Use [link db.example.com]"),
+ ("token-like string", "Use key phx_9f8e7d6c5b4a3f2e1d0c9b8a7f6e5d4c", "Use key <token>"),
+ ("multiline text folds onto one line", "First line\n\n second\tline ", "First line second line"),
+ (
+ "ordinary text is untouched",
+ "Why did weekly signups drop in October?",
+ "Why did weekly signups drop in October?",
+ ),
+ ("long run without a digit is kept", "a-" * 20, ("a-" * 20).strip()),
+ ("text is cut to the cap", "x" * 100, "x" * 60),
+ (
+ "secret across the cap is masked, not cut",
+ "a" * 50 + " sk-proj-abcdefghijklmnop1234567890",
+ "a" * 50 + " <token>",
+ ),
+ ("word split by the read bound is dropped", " " * 230 + "ab sk-proj-abcdefghijklmnop1234567890", "ab"),
+ ]
+)
+def test_clean_text(_name: str, text: str, expected: str) -> None:
+ assert clean_text(text, 60) == expected
+
+
+def test_sampling_is_deterministic_and_favors_recent_units() -> None:
+ units = [Unit(at=_at(days_ago), text=f"unit {days_ago}") for days_ago in range(28)]
+ seed = sampling_seed("2026-10-18", 2, 7)
+
+ first = sample_units(units, limit=TASK_LINES, as_of=AS_OF, seed=seed)
+ assert first == sample_units(list(reversed(units)), limit=TASK_LINES, as_of=AS_OF, seed=seed)
+ assert len(first) == TASK_LINES
+ assert first == sorted(first, key=lambda unit: unit.at)
+
+ picked_ages = [
+ (AS_OF - unit.at).days
+ for partition in range(50)
+ for unit in sample_units(units, limit=TASK_LINES, as_of=AS_OF, seed=sampling_seed(str(partition), 2, 7))
+ ]
+ assert sum(1 for age in picked_ages if age < 7) > sum(1 for age in picked_ages if age >= 21)
+
+
+def test_build_input_renders_the_two_sections_in_order() -> None:
+ rendered = build_input(
+ [_task("why", summary="Asked: why did signups drop?", tags=("growth",), days_ago=3)],
+ [_change("Dashboard", "created", "Growth overview", days_ago=1)],
+ as_of=AS_OF,
+ seed=1,
+ )
+
+ assert rendered is not None
+ assert rendered.text == "\n".join(
+ [
+ TASKS_HEADER,
+ "- 2026-10-16 [growth] why did signups drop?",
+ "",
+ ACTIVITY_HEADER,
+ '- 2026-10-18 created Dashboard "Growth overview"',
+ ]
+ )
+ assert (rendered.n_tasks, rendered.n_activity_lines, rendered.units) == (1, 1, 2)
+ assert rendered.input_chars == len(rendered.text)
+ assert build_input([_task("root cause")], [], as_of=AS_OF, seed=1) is None
+
+
+def test_build_input_stays_inside_its_bounds_for_a_very_active_person() -> None:
+ runaway = "word " * 2000
+ tasks = [
+ _task(runaway, summary=f"{day} {n} {runaway}", tags=(runaway,) * 20, days_ago=day + 0.01 * n)
+ for day in range(28)
+ for n in range(20)
+ ]
+ activity = [_change("Insight", "created", f"{runaway} {n}", days_ago=n % 28, intent=runaway) for n in range(500)]
+
+ rendered = build_input(tasks, activity, as_of=AS_OF, seed=1)
+
+ assert rendered is not None
+ assert (rendered.n_tasks, rendered.n_activity_lines) == (TASK_LINES, ACTIVITY_LINES)
+ tasks_text, activity_text = rendered.text.split("\n\n")
+ assert max(len(line) for line in tasks_text.split("\n")[1:]) <= TASK_LINE_CHARS
+ assert max(len(line) for line in activity_text.split("\n")[1:]) <= ACTIVITY_LINE_CHARS
+ assert rendered.input_chars <= SECTIONS_MAX_CHARS
+ assert rendered.drops[Drop.BURST_CAP] > 0 and rendered.drops[Drop.SAMPLE_CAP] > 0
+
+
+PREVIOUS = {"summary": "Builds the signup funnel.", "focus_areas": [], "products": ["product_analytics"]}
+
+
[email protected]([("with a previous summary", PREVIOUS), ("cold start", None)])
+def test_user_message_sends_only_the_rendered_input_and_the_previous_summary(
+ _name: str, previous: dict[str, Any] | None
+) -> None:
+ message = user_message("- 2026-10-17 Build a funnel", previous, datetime.date(2026, 10, 18))
+ assert message.startswith("Today is 2026-10-18.")
+ assert message.endswith("Recent activity:\n- 2026-10-17 Build a funnel")
+ if previous:
+ assert json.dumps(PREVIOUS["summary"]) in message
+ else:
+ assert "Previous summary:\nNone" in message
+
+
+def _answer(**overrides: Any) -> str:
+ return json.dumps(
+ {
+ "summary": "Builds the signup funnel.",
+ "focus_areas": [{"topic": "signup funnel", "strength": "high", "last_seen": "2026-10-17"}],
+ "products": ["product_analytics"],
+ "recent_shift": "Moved from flags to funnels.",
+ **overrides,
+ }
+ )
+
+
+ [
+ ("recent_shift is dropped without a previous summary", _answer(), False, {"recent_shift": None}),
+ (
+ "recent_shift is kept with a previous summary",
+ _answer(),
+ True,
+ {"recent_shift": "Moved from flags to funnels."},
+ ),
+ ("a 'None' recent_shift becomes null", _answer(recent_shift="None"), True, {"recent_shift": None}),
+ ("a 'null' recent_shift becomes null", _answer(recent_shift=" null "), True, {"recent_shift": None}),
+ (
+ "the summary is cut to the word cap",
+ _answer(summary="word " * 80),
+ False,
+ {"summary": " ".join(["word"] * 60)},
+ ),
+ (
+ "at most six focus areas, an unknown strength is low and a bad date is null",
+ _answer(focus_areas=[{"topic": f"t{i}", "strength": "huge", "last_seen": "soon"} for i in range(8)]),
+ False,
+ {"focus_areas": [{"topic": f"t{i}", "strength": "low", "last_seen": None} for i in range(6)]},
+ ),
+ (
+ "a focus area without a topic is dropped",
+ _answer(focus_areas=[{"strength": "high"}]),
+ False,
+ {"focus_areas": []},
+ ),
+ (
+ "topic, product and recent_shift are cut to their caps",
+ _answer(
+ focus_areas=[{"topic": "t" * 500, "strength": "high", "last_seen": "2026-10-17"}],
+ products=["p" * 500],
+ recent_shift="s" * 2000,
+ ),
+ True,
+ {
+ "focus_areas": [{"topic": "t" * MAX_TOPIC_CHARS, "strength": "high", "last_seen": "2026-10-17"}],
+ "products": ["p" * MAX_PRODUCT_CHARS],
+ "recent_shift": "s" * MAX_RECENT_SHIFT_CHARS,
+ },
+ ),
+ ]
+)
+def test_normalize_summary(_name: str, content: str, had_previous: bool, expected: dict[str, Any]) -> None:
+ assert normalize_summary(content, had_previous_summary=had_previous).items() >= expected.items()
+
+
+ [
+ ("empty", None),
+ ("not JSON", "Here is the summary: ..."),
+ ("not an object", "[1, 2]"),
+ ("no summary", _answer(summary=" ")),
+ ]
+)
+def test_normalize_summary_rejects_a_malformed_answer(_name: str, content: str | None) -> None:
+ with pytest.raises(MalformedSummaryError):
+ normalize_summary(content, had_previous_summary=False)
+
+
+def test_summary_text_is_deterministic_and_keeps_the_focus_order() -> None:
+ summary = normalize_summary(
+ _answer(
+ focus_areas=[
+ {"topic": "b", "strength": "high", "last_seen": ""},
+ {"topic": "a", "strength": "low", "last_seen": ""},
+ ]
+ ),
+ had_previous_summary=False,
+ )
+ reloaded = json.loads(json.dumps(summary, sort_keys=True))
+ assert summary_text(summary) == summary_text(reloaded) == "Builds the signup funnel.\nFocus areas: b; a"
+ assert summary_text({**summary, "focus_areas": []}) == "Builds the signup funnel."
+
+
+_PRICES = SummaryPrices(input_price_per_million_tokens=0.1, output_price_per_million_tokens=0.5)
+
+
+ [
+ ("both prices", 1_000_000, 200_000, _PRICES, 0.2),
+ ("unknown usage", None, None, _PRICES, None),
+ (
+ "no output price",
+ 1_000,
+ 100,
+ SummaryPrices(input_price_per_million_tokens=0.1, output_price_per_million_tokens=None),
+ None,
+ ),
+ ("a model with no prices", 1_000, 100, None, None),
+ ]
+)
+def test_cost_usd(
+ _name: str, tokens_in: int | None, tokens_out: int | None, prices: SummaryPrices | None, expected: float | None
+) -> None:
+ assert cost_usd(tokens_in, tokens_out, prices) == (pytest.approx(expected) if expected is not None else None)
+
+
+class FakeLLM:
+ """Answers are a JSON string, `(content, finish_reason)`, or an exception to raise."""
+
+ def __init__(self, answers: list[Any], *, usage: bool = True):
+ self.answers = list(answers)
+ self.usage = usage
+ self.requests: list[dict[str, Any]] = []
+ self.chat = SimpleNamespace(completions=SimpleNamespace(create=self.create))
+
+ def create(self, **kwargs: Any) -> SimpleNamespace:
+ self.requests.append(kwargs)
+ answer = self.answers.pop(0) if len(self.answers) > 1 else self.answers[0]
+ if isinstance(answer, Exception):
+ raise answer
+ content, finish_reason = answer if isinstance(answer, tuple) else (answer, "stop")
+ return SimpleNamespace(
+ usage=SimpleNamespace(prompt_tokens=100, completion_tokens=20) if self.usage else None,
+ choices=[SimpleNamespace(message=SimpleNamespace(content=content), finish_reason=finish_reason)],
+ )
+
+
+_CONNECTION_ERROR = APIConnectionError(request=SimpleNamespace()) # type: ignore[arg-type]
+
+
+ [
+ ("a malformed answer is retried once", ["not json", _answer()], True, 2, None, (200, 40)),
+ (
+ "two malformed answers fail",
+ ["not json", "still not json"],
+ False,
+ 2,
+ "malformed_output: response is not JSON",
+ (200, 40),
+ ),
+ (
+ "a truncated answer is retried, then fails",
+ [(_answer(), "length")],
+ False,
+ 2,
+ "malformed_output: response was truncated",
+ (200, 40),
+ ),
+ ("a truncated answer then a full one succeeds", [(_answer(), "length"), _answer()], True, 2, None, (200, 40)),
+ (
+ "a failed request fails without a retry, with unknown usage",
+ [_CONNECTION_ERROR],
+ False,
+ 1,
+ "api_error: APIConnectionError",
+ (None, None),
+ ),
+ ]
+)
+def test_summarize_retries_a_malformed_or_truncated_answer_once(
+ _name: str,
+ answers: list[Any],
+ succeeds: bool,
+ attempts: int,
+ error: str | None,
+ tokens: tuple[int | None, int | None],
+) -> None:
+ llm = FakeLLM(answers)
+ outcome = summarize(
+ llm, # type: ignore[arg-type]
+ rendered_input="- a line",
+ previous_summary=None,
+ model="gpt-6-luna",
+ as_of=datetime.date(2026, 10, 18),
+ )
+
+ assert len(llm.requests) == attempts
+ assert {req["user"] for req in llm.requests} == {PIPELINE_DISTINCT_ID}
+ assert {req["max_completion_tokens"] for req in llm.requests} == {MAX_COMPLETION_TOKENS}
+ assert (outcome.summary is not None, outcome.error) == (succeeds, error)
+ assert (outcome.tokens_in, outcome.tokens_out) == tokens
+
+
+def test_summarize_reports_null_tokens_when_the_response_has_no_usage() -> None:
+ outcome = summarize(
+ FakeLLM([_answer()], usage=False), # type: ignore[arg-type]
+ rendered_input="- a line",
+ previous_summary=None,
+ model="gpt-6-luna",
+ as_of=datetime.date(2026, 10, 18),
+ )
+ assert outcome.summary is not None
+ assert (outcome.tokens_in, outcome.tokens_out) == (None, None)
diff --git a/products/signals/backend/test/test_activity_summary_update.py b/products/signals/backend/test/test_activity_summary_update.py
new file mode 100644
index 000000000000..9cecd472e001
--- /dev/null
+++ b/products/signals/backend/test/test_activity_summary_update.py
@@ -0,0 +1,131 @@
+import json
+import datetime
+from typing import Any
+
+import time_machine
+from posthog.test.base import BaseTest
+from unittest.mock import patch
+
+from parameterized import parameterized
+
+from posthog.models import ActivityLog
+
+from products.signals.backend.activity_summary.logic import ActivitySummaryUpdate, UpdateStatus, update_activity_summary
+from products.signals.backend.models import UserActivitySummary
+from products.signals.backend.test.test_activity_summary_render import FakeLLM
+
+NOW = datetime.datetime(2026, 10, 19, 12, tzinfo=datetime.UTC)
+PATH_SECRET = "https://hooks.example.com/services/T000/B000/s3cr3tPathValue"
+ANSWER = json.dumps(
+ {
+ "summary": "Builds the signup funnel.",
+ "focus_areas": [{"topic": "signup funnel", "strength": "high", "last_seen": "2026-10-18"}],
+ "products": ["product_analytics"],
+ "recent_shift": None,
+ }
+)
+
+
+@time_machine.travel(NOW, tick=False)
+class TestUpdateActivitySummary(BaseTest):
+ def _change(self, name: str, *, days_ago: float, intent: str | None = None, **overrides: Any) -> None:
+ detail: dict[str, Any] = {"name": name}
+ if intent:
+ detail["trigger"] = {"job_type": "agent", "job_id": "", "payload": {"intent": intent}}
+ ActivityLog.objects.create(
+ team_id=self.team.id,
+ organization_id=self.organization.id,
+ user=self.user,
+ scope="Insight",
+ activity="created",
+ item_id=name,
+ detail=detail,
+ created_at=NOW - datetime.timedelta(days=days_ago),
+ **overrides,
+ )
+
+ def _changes(self, count: int, *, days_ago: float = 1) -> None:
+ for index in range(count):
+ self._change(f"Funnel {index}", days_ago=days_ago + index / 100)
+
+ def _update(self, llm: FakeLLM, **kwargs: Any) -> ActivitySummaryUpdate:
+ with patch("products.signals.backend.activity_summary.logic.gateway_client", return_value=llm):
+ return update_activity_summary(self.team.id, self.user.id, **kwargs)
+
+ def _stored(self) -> UserActivitySummary | None:
+ return UserActivitySummary.objects.for_team(self.team.id).filter(user_id=self.user.id).first()
+
+ def test_a_successful_update_stores_the_summary_and_advances_the_watermark(self) -> None:
+ self._changes(2, days_ago=2)
+ self._change("Signup funnel", days_ago=1, intent=f"Post the funnel to {PATH_SECRET}")
+ llm = FakeLLM([ANSWER])
+
+ result = self._update(llm)
+
+ assert result.status == UpdateStatus.OK
+ sent = json.dumps(llm.requests[0]["messages"])
+ assert "s3cr3tPathValue" not in sent and "[link hooks.example.com]" in sent
+ stored = self._stored()
+ assert stored is not None
+ assert stored.status == UserActivitySummary.Status.OK
+ assert stored.summary_text == "Builds the signup funnel.\nFocus areas: signup funnel"
+ assert stored.input_watermark == NOW - datetime.timedelta(days=1)
+ assert stored.next_eligible_at == NOW + datetime.timedelta(days=1)
+ assert (stored.tokens_in, stored.tokens_out) == (100, 20)
+ assert stored.cost_usd is not None
+ assert stored.inputs_used["activity_lines"] == 3
+
+ self._changes(3, days_ago=0.5)
+ with time_machine.travel(NOW + datetime.timedelta(days=2), tick=False):
+ next_llm = FakeLLM([ANSWER])
+ assert self._update(next_llm).status == UpdateStatus.OK
+ next_sent = json.dumps(next_llm.requests[0]["messages"])
+ assert "Signup funnel" not in next_sent.split("Recent activity")[1]
+ assert "Builds the signup funnel." in next_sent
+
+ def test_a_failed_update_keeps_the_stored_summary_and_watermark(self) -> None:
+ self._changes(3, days_ago=2)
+ self._update(FakeLLM([ANSWER]))
+ before = self._stored()
+ assert before is not None
+ self._changes(3, days_ago=0.5)
+
+ result = self._update(FakeLLM([(ANSWER, "length")], usage=False), force=True)
+
+ assert result.status == UpdateStatus.FAILED
+ after = self._stored()
+ assert after is not None
+ assert after.status == UserActivitySummary.Status.FAILED
+ assert after.error == "malformed_output: response was truncated"
+ assert (after.summary, after.input_watermark) == (before.summary, before.input_watermark)
+ assert (after.tokens_in, after.tokens_out, after.cost_usd) == (None, None, None)
+
+ @parameterized.expand(
+ [
+ ("too few units", 2, {}, UpdateStatus.SKIPPED_TOO_FEW_UNITS),
+ ("a dry run", 3, {"dry_run": True}, UpdateStatus.OK),
+ ]
+ )
+ def test_writes_nothing(self, _name: str, changes: int, kwargs: dict[str, Any], status: UpdateStatus) -> None:
+ self._changes(changes)
+ self._change("Scout funnel", days_ago=1, client="scout:product-analytics")
+
+ assert self._update(FakeLLM([ANSWER]), **kwargs).status == status
+ assert self._stored() is None
+
+ def test_skips_until_next_eligible_unless_forced(self) -> None:
+ self._changes(3, days_ago=2)
+ self._update(FakeLLM([ANSWER]))
+ self._changes(3, days_ago=0.5)
+
+ assert self._update(FakeLLM([ANSWER])).status == UpdateStatus.SKIPPED_NOT_ELIGIBLE_YET
+ assert self._update(FakeLLM([ANSWER]), force=True).status == UpdateStatus.OK
+
+ def test_skips_when_ai_data_processing_is_not_approved(self) -> None:
+ self.organization.is_ai_data_processing_approved = False
+ self.organization.save()
+ self._changes(3)
+ llm = FakeLLM([ANSWER])
+
+ assert self._update(llm).status == UpdateStatus.SKIPPED_AI_NOT_APPROVED
+ assert llm.requests == []