: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
fix(signals): filter noisy activity rows before the read limit (#121379)
posthog[bot]9fd86a15c996
diff --git a/products/signals/backend/activity_summary/inputs.py b/products/signals/backend/activity_summary/inputs.pyindex 20aa154cb822..286f9db98c0b 100644--- a/products/signals/backend/activity_summary/inputs.py+++ b/products/signals/backend/activity_summary/inputs.py@@ -9,6 +9,9 @@ from posthog.models import ActivityLogfrom products.signals.backend.activity_summary.render import (MAX_ACTIVITY_ROWS_PER_PERSON,MAX_TASKS_PER_PERSON,+ NOISY_ACTIVITY_SCOPES,+ SANDBOX_PROMPT_TITLE_PREFIX,+ SCOUT_CLIENT_PREFIX,ActivityItem,TaskItem,)@@ -30,6 +33,7 @@ def user_tasks(team_id: int, user_id: int, start: datetime.datetime, end: datetiend=end,origin_products=TASK_ORIGIN_PRODUCTS,limit=MAX_TASKS_PER_PERSON,+ exclude_title_prefixes=(SANDBOX_PROMPT_TITLE_PREFIX,),)]@@ -37,11 +41,14 @@ def user_tasks(team_id: int, user_id: int, start: datetime.datetime, end: datetidef 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."""+ System rows and rows written while someone impersonated the user are not the user's. Noisy scopes+ and scout writes go before the limit, so a bulk write cannot push the rest of the window out."""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)+ .exclude(scope__in=NOISY_ACTIVITY_SCOPES)+ .exclude(client__startswith=SCOUT_CLIENT_PREFIX).order_by("-created_at").annotate(name=KeyTextTransform("name", "detail"),diff --git a/products/signals/backend/activity_summary/render.py b/products/signals/backend/activity_summary/render.pyindex 6d036b20ef97..7c169ab95af0 100644--- a/products/signals/backend/activity_summary/render.py+++ b/products/signals/backend/activity_summary/render.py@@ -21,14 +21,14 @@ from urllib.parse import urlsplitfrom 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+RENDER_VERSION = 4WINDOW_DAYS = 28RECENCY_HALF_LIFE_DAYS = 7CHARS_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.+# Rows read per person, newest first. They bound query and memory cost for very active people. The+# query applies the noise filters before this limit, so bulk agent writes cannot fill it.MAX_TASKS_PER_PERSON = 200MAX_ACTIVITY_ROWS_PER_PERSON = 1000@@ -59,7 +59,7 @@ TASKS_HEADER = "Tasks the person gave to PostHog AI or the Slack agent ([tags] gACTIVITY_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:")+SANDBOX_PROMPT_TITLE_PREFIX = "[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+")@@ -223,7 +223,7 @@ def task_units(tasks: Iterable[TaskItem], drops: Counter[str]) -> list[Unit]:templated: dict[str, list[datetime.datetime]] = {}for task in tasks:title = task.title.strip()- if SANDBOX_PROMPT_TITLE.match(title):+ if title.startswith(SANDBOX_PROMPT_TITLE_PREFIX):drops[Drop.SANDBOX_PROMPT_TASK] += 1continueif (label := _templated_label(title)) is not None:diff --git a/products/signals/backend/test/test_activity_summary_update.py b/products/signals/backend/test/test_activity_summary_update.pyindex ef295dd71dca..82ec2cae6902 100644--- a/products/signals/backend/test/test_activity_summary_update.py+++ b/products/signals/backend/test/test_activity_summary_update.py@@ -10,6 +10,7 @@ from parameterized import parameterizedfrom posthog.models import ActivityLog+from products.signals.backend.activity_summary.inputs import user_activityfrom products.signals.backend.activity_summary.logic import ActivitySummaryUpdate, UpdateStatus, update_activity_summaryfrom products.signals.backend.models import UserActivitySummaryfrom products.signals.backend.test.test_activity_summary_render import FakeLLM@@ -28,7 +29,9 @@ ANSWER = json.dumps(@time_machine.travel(NOW, tick=False)class TestUpdateActivitySummary(BaseTest):- def _change(self, name: str, *, days_ago: float, intent: str | None = None, **overrides: Any) -> None:+ def _change(+ self, name: str, *, days_ago: float, intent: str | None = None, scope: str = "Insight", **overrides: Any+ ) -> None:detail: dict[str, Any] = {"name": name}if intent:detail["trigger"] = {"job_type": "agent", "job_id": "", "payload": {"intent": intent}}@@ -36,7 +39,7 @@ class TestUpdateActivitySummary(BaseTest):team_id=self.team.id,organization_id=self.organization.id,user=self.user,- scope="Insight",+ scope=scope,activity="created",item_id=name,detail=detail,@@ -114,6 +117,16 @@ class TestUpdateActivitySummary(BaseTest):assert self._update(FakeLLM([ANSWER]), **kwargs).status == statusassert self._stored() is None+ @patch("products.signals.backend.activity_summary.inputs.MAX_ACTIVITY_ROWS_PER_PERSON", 1)+ def test_noisy_rows_do_not_use_the_read_limit(self) -> None:+ self._change("Signup funnel", days_ago=2)+ self._change("Tagged report", days_ago=1, scope="TaggedItem")+ self._change("Scout funnel", days_ago=1, client="scout:product-analytics")++ rows = user_activity(self.team.id, self.user.id, NOW - datetime.timedelta(days=28), NOW)++ assert [row.name for row in rows] == ["Signup funnel"]+def test_too_few_units_backs_off_without_touching_the_stored_summary(self) -> None:self._changes(2)llm = FakeLLM([ANSWER])diff --git a/products/tasks/backend/logic/user_task_summaries.py b/products/tasks/backend/logic/user_task_summaries.pyindex f1d434562ed2..2de1d2503b8e 100644--- a/products/tasks/backend/logic/user_task_summaries.py+++ b/products/tasks/backend/logic/user_task_summaries.py@@ -53,24 +53,28 @@ def list_user_task_summaries(end: datetime.datetime,origin_products: Collection[str],limit: int,+ exclude_title_prefixes: Collection[str] = (),) -> list[UserTaskSummary]:"""The newest `limit` non-deleted tasks one user created in these teams, in [start, end).+ Tasks whose title starts with one of `exclude_title_prefixes` do not count towards the limit.+One user per call, so a single busy person cannot take another person's share of a batch."""if not team_ids or not origin_products or limit <= 0:return []+ queryset = Task.objects.filter(+ team_id__in=list(team_ids),+ created_by_id=user_id,+ origin_product__in=list(origin_products),+ created_at__gte=start,+ created_at__lt=end,+ deleted=False,+ )+ for prefix in exclude_title_prefixes:+ queryset = queryset.exclude(title__startswith=prefix)tasks = list(- Task.objects.filter(- team_id__in=list(team_ids),- created_by_id=user_id,- origin_product__in=list(origin_products),- created_at__gte=start,- created_at__lt=end,- deleted=False,- )- .order_by("-created_at")- .values("id", "team_id", "created_at", "title", "origin_product")[:limit]+ queryset.order_by("-created_at").values("id", "team_id", "created_at", "title", "origin_product")[:limit])if not tasks:return []diff --git a/products/tasks/backend/tests/test_user_task_summaries.py b/products/tasks/backend/tests/test_user_task_summaries.pyindex cbcba4fb017f..d6762f3e5c9d 100644--- a/products/tasks/backend/tests/test_user_task_summaries.py+++ b/products/tasks/backend/tests/test_user_task_summaries.py@@ -31,6 +31,7 @@ class TestListUserTaskSummaries(BaseTest):self._task("scout run", origin="signals_scout")self._task("someone else's task", user=User.objects.create(email="[email protected]"))self._task("too old", days_ago=40)+ self._task("[sandbox_prompt:repo_selection] pick a repository", days_ago=1)summaries = list_user_task_summaries(team_ids=[self.team.id],@@ -38,7 +39,8 @@ class TestListUserTaskSummaries(BaseTest):start=NOW - timedelta(days=28),end=NOW,origin_products=["slack", "posthog_ai", "user_created"],- limit=10,+ limit=1,+ exclude_title_prefixes=["[sandbox_prompt:"],)assert [(s.title, s.summary, s.tags) for s in summaries] == [