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): ship system-table default pre-checks for four product scouts (#115174)

posthog[bot]7e173fcf1071
diff --git a/products/signals/backend/test/test_scout_precheck.py b/products/signals/backend/test/test_scout_precheck.py
index 5f0e9e53ecd7..4e723d90130a 100644
--- a/products/signals/backend/test/test_scout_precheck.py
+++ b/products/signals/backend/test/test_scout_precheck.py
@@ -1,7 +1,14 @@
from datetime import UTC, datetime, timedelta
import time_machine
-from posthog.test.base import APIBaseTest, BaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_events
+from posthog.test.base import (
+ APIBaseTest,
+ BaseTest,
+ ClickhouseTestMixin,
+ NonAtomicBaseTest,
+ _create_event,
+ flush_persons_and_events,
+)
from unittest.mock import patch
from django.apps import apps
@@ -11,7 +18,9 @@ from parameterized import parameterized
from rest_framework import status
from posthog.models import OrganizationMembership, Team, User
+from posthog.models.scoping import team_scope
+from products.customer_analytics.backend.facade.testing import create_account
from products.signals.backend.models import SignalScoutConfig, SignalScoutRun
from products.signals.backend.scout_harness.lazy_seed import HARNESS_SEEDED_BY
from products.signals.backend.scout_harness.precheck import (
@@ -249,6 +258,83 @@ class TestSkillDefaultPrecheck(ClickhouseTestMixin, BaseTest):
capture.assert_not_called()
+# Non-atomic: ClickHouse reads `system.*` tables over its own Postgres connection, so rows must be committed.
+@time_machine.travel(NOW, tick=False)
+class TestSystemTableSkillDefaults(ClickhouseTestMixin, NonAtomicBaseTest):
+ CLASS_DATA_LEVEL_SETUP = False
+
+ def _seed_task_run(self, origin_product: str) -> None:
+ Channel = apps.get_model("tasks", "Channel")
+ Task = apps.get_model("tasks", "Task")
+ TaskRun = apps.get_model("tasks", "TaskRun")
+ with team_scope(self.team.pk):
+ channel = Channel.objects.create(team=self.team, name=f"space_{origin_product}")
+ task = Task.objects.create(
+ team=self.team, channel=channel, title="t", description="", origin_product=origin_product
+ )
+ TaskRun.objects.create(task=task, team=self.team)
+
+ def _seed_experiment(self, *, started: bool, days_since_update: int) -> None:
+ FeatureFlag = apps.get_model("feature_flags", "FeatureFlag")
+ Experiment = apps.get_model("experiments", "Experiment")
+ flag = FeatureFlag.objects.create(team=self.team, key=f"exp_flag_{started}_{days_since_update}")
+ experiment = Experiment.objects.create(
+ team=self.team, name="e", feature_flag=flag, start_date=NOW - timedelta(days=60) if started else None
+ )
+ Experiment.objects.filter(pk=experiment.pk).update(updated_at=NOW - timedelta(days=days_since_update))
+
+ @parameterized.expand(
+ [
+ ("customer_analytics_no_accounts", "signals-scout-customer-analytics", None, "skip"),
+ ("customer_analytics_accounts", "signals-scout-customer-analytics", "account", "run"),
+ ("feature_flags_absent", "signals-scout-feature-flags", None, "skip"),
+ ("feature_flags_roster", "signals-scout-feature-flags", "flag", "run"),
+ ("feature_flags_calls_only", "signals-scout-feature-flags", "flag_call", "run"),
+ ("experiments_absent", "signals-scout-experiments", None, "skip"),
+ ("experiments_old_draft", "signals-scout-experiments", "old_draft", "skip"),
+ ("experiments_recent_draft", "signals-scout-experiments", "recent_draft", "run"),
+ ("experiments_running", "signals-scout-experiments", "running", "run"),
+ ("tasks_absent", "signals-scout-tasks", None, "skip"),
+ ("tasks_scout_runs_only", "signals-scout-tasks", "scout_run", "skip"),
+ ("tasks_user_runs", "signals-scout-tasks", "user_run", "run"),
+ ]
+ )
+ def test_system_table_defaults_skip_only_on_absence(self, _name, skill, seed, outcome) -> None:
+ LLMSkill.objects.create(
+ team=self.team, name=skill, description="", body="", metadata={"seeded_by": HARNESS_SEEDED_BY}
+ )
+ SignalScoutConfig.all_teams.create(team=self.team, skill_name=skill)
+ if seed == "account":
+ create_account(team_id=self.team.pk, name="a", external_id="a")
+ elif seed == "flag":
+ apps.get_model("feature_flags", "FeatureFlag").objects.create(team=self.team, key="f")
+ elif seed == "flag_call":
+ _create_event(
+ team=self.team, event="$feature_flag_called", distinct_id="d1", timestamp=NOW - timedelta(days=6)
+ )
+ flush_persons_and_events()
+ elif seed == "old_draft":
+ self._seed_experiment(started=False, days_since_update=31)
+ elif seed == "recent_draft":
+ self._seed_experiment(started=False, days_since_update=29)
+ elif seed == "running":
+ self._seed_experiment(started=True, days_since_update=90)
+ elif seed == "scout_run":
+ self._seed_task_run("signals_scout")
+ elif seed == "user_run":
+ self._seed_task_run("user_created")
+
+ with (
+ patch(ROLLOUT_PERCENT, return_value=100),
+ patch("products.signals.backend.scout_harness.precheck.posthoganalytics.capture") as capture,
+ ):
+ result = evaluate_scout_precheck(self.team.pk, skill)
+
+ assert result is not None
+ assert (result.outcome, result.query_source) == (outcome, "skill_default")
+ assert capture.call_args.kwargs["properties"].get("error_type") is None
+
+
class TestResolveEffectivePrecheck(SimpleTestCase):
@parameterized.expand(
[
diff --git a/products/signals/skills/signals-scout-customer-analytics/SKILL.md b/products/signals/skills/signals-scout-customer-analytics/SKILL.md
index 6848853cff3c..da507c1cd489 100644
--- a/products/signals/skills/signals-scout-customer-analytics/SKILL.md
+++ b/products/signals/skills/signals-scout-customer-analytics/SKILL.md
@@ -16,6 +16,9 @@ allowed_tools:
metadata:
owner_team: signals
scope: customer_analytics
+scout-precheck-query: |
+ SELECT count() AS accounts
+ FROM system.accounts
---
# Signals scout: customer analytics (account health)
@@ -42,6 +45,8 @@ You author reports directly via the report channel (`scout-emit-report` / `scout
## Quick close-out: is there an account roster worth scoring?
+A scheduled run can start with a `<precheck_result>` block. Its one row counts the accounts in `system.accounts`, and a run starts only when that count is above zero. When the block is present, skip the empty-roster check. The `products_in_use` check and the join check still apply.
+
Close out empty (after one scratchpad entry) if any of these hold:
- `customer_analytics` is **not** in the profile's `products_in_use`, or `system.accounts` is empty (`SELECT count() FROM system.accounts` is 0) → `not-in-use:customer_analytics:team{team_id}`.
diff --git a/products/signals/skills/signals-scout-experiments/SKILL.md b/products/signals/skills/signals-scout-experiments/SKILL.md
index 78230e9dddcc..339eb81b244c 100644
--- a/products/signals/skills/signals-scout-experiments/SKILL.md
+++ b/products/signals/skills/signals-scout-experiments/SKILL.md
@@ -15,6 +15,13 @@ allowed_tools:
metadata:
owner_team: signals
scope: experiments
+scout-precheck-query: |
+ SELECT id, start_date, end_date, archived, updated_at
+ FROM system.experiments
+ WHERE (start_date IS NOT NULL AND end_date IS NULL AND archived = 0)
+ OR updated_at > {now} - INTERVAL 30 DAY
+ ORDER BY updated_at DESC
+ LIMIT 20
---
# Signals scout: experiments
@@ -38,6 +45,8 @@ If a history reader is unavailable or access is denied, stop using that reader f
## Quick close-out: are experiments even active?
+A scheduled run can start with a `<precheck_result>` block. Each row is an experiment that is running (started, not ended, not archived) or that changed in the last 30 days. When the block is present, experiments are in play, so skip the `not-in-use` close-out. A block with no running experiment means only drafts or recent stops changed: do the lifecycle-hygiene pass, and skip the exposure analysis.
+
Read `recent_experiments` off `scout-project-profile-get`. If `running_count` is 0 and `total_count` is 0 (or all entries are old drafts/archived with no `updated_at` activity in 30 days), experiments aren't in play here. Write one scratchpad entry:
- key: `not-in-use:experiments` (the scratchpad is already team-scoped — no id in the key)
diff --git a/products/signals/skills/signals-scout-feature-flags/SKILL.md b/products/signals/skills/signals-scout-feature-flags/SKILL.md
index d9b19a9615f1..305c8d8b53a1 100644
--- a/products/signals/skills/signals-scout-feature-flags/SKILL.md
+++ b/products/signals/skills/signals-scout-feature-flags/SKILL.md
@@ -17,6 +17,17 @@ scout-tags:
metadata:
owner_team: signals
scope: feature_flags
+scout-precheck-query: |
+ SELECT 'flags' AS source, count() AS total
+ FROM system.feature_flags
+ WHERE deleted = 0
+ HAVING total > 0
+ UNION ALL
+ SELECT '$feature_flag_called' AS source, count() AS total
+ FROM events
+ WHERE event = '$feature_flag_called'
+ AND timestamp > {now} - INTERVAL 7 DAY
+ HAVING total > 0
---
# Signals scout: feature flags
@@ -40,6 +51,8 @@ If a history reader is unavailable or access is denied, stop using that reader f
## Quick close-out: are flags even in use?
+A scheduled run can start with a `<precheck_result>` block. A `flags` row counts the flags that are not deleted. A `$feature_flag_called` row counts the calls of the last 7 days. A run starts only when at least one row is present, so the zero-roster, zero-calls case does not occur. Use the rows in place of the count query below to pick the case.
+
Read `recent_feature_flags` off `scout-project-profile-get`. Two caveats before shortcutting: `total_count` excludes deleted flags, and `top_events` is only the top 50 by volume — so confirm the traffic side with one cheap count rather than trusting either alone:
```sql
diff --git a/products/signals/skills/signals-scout-tasks/SKILL.md b/products/signals/skills/signals-scout-tasks/SKILL.md
index 864fa701191a..133d79e6453f 100644
--- a/products/signals/skills/signals-scout-tasks/SKILL.md
+++ b/products/signals/skills/signals-scout-tasks/SKILL.md
@@ -18,6 +18,13 @@ compatibility: >
metadata:
owner_team: signals
scope: tasks
+scout-precheck-query: |
+ SELECT t.origin_product AS origin_product, count() AS runs, max(r.created_at) AS last_run
+ FROM system.task_runs AS r
+ INNER JOIN system.tasks AS t ON r.task_id = t.id
+ WHERE t.origin_product NOT IN ('signals_scout', 'scout_suggestions')
+ AND r.created_at > {now} - INTERVAL 14 DAY
+ GROUP BY origin_product
---
# Signals scout: tasks
@@ -83,6 +90,8 @@ A run that forgets this exclusion is mostly measuring the scout fleet, and the d
## Quick close-out: does this project run tasks?
+A scheduled run can start with a `<precheck_result>` block. Each row counts one origin's runs of the last 14 days, without the fleet's own origins, so the project uses Tasks. When the block is present, skip the `not-in-use` close-out. The baseline close-out below still applies.
+
Close out on **runs, not task creation**.
A project that creates no new tasks can still be running old ones daily, and those runs are exactly what lens A exists to watch — closing out on a task-creation count would skip today's failures entirely.
So: if a 14-day count of _runs_ outside those two origins is ~0, this project isn't using Tasks.