: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(ai): avoid scouts for one-time bug investigations (#119360)
Georgiy Tarasov83b4b293d00b
diff --git a/products/posthog_ai/backend/tests/test_turn_suggestions.py b/products/posthog_ai/backend/tests/test_turn_suggestions.pyindex 192d5d79a2aa..38617672c804 100644--- a/products/posthog_ai/backend/tests/test_turn_suggestions.py+++ b/products/posthog_ai/backend/tests/test_turn_suggestions.py@@ -61,6 +61,7 @@ from products.posthog_ai.backend.turn_suggestions.service import (MAX_TRANSCRIPT_LOG_BYTES,TURN_SUGGESTION_METHOD,TurnSuggestionOutcome,+ _capture_classified,generate_turn_suggestion,)from products.posthog_ai.backend.turn_suggestions.transcript import (@@ -248,6 +249,7 @@ def _verdict(offer: OfferKind = OfferKind.SCOUT, intent: TurnIntent = TurnIntentshow_probability=0.92,picked=offer,offer_probabilities={offer.value: 0.8},+ scout_recurrence_probability=0.9,draft=_DRAFTS.get(offer),)@@ -508,6 +510,7 @@ def _judgment(**overrides: Any) -> TurnJudgment:intent=TurnIntent.METRIC_STATE,offer=OfferKind.SCOUT,offer_probabilities={"scout": 0.7, "notebook": 0.3},+ scout_recurrence_probability=0.9,scout_mode=ScoutMode.REPORT,cadence=ScoutCadence.WEEKLY,notebook_template=NotebookTemplate.CONVERSATION,@@ -562,10 +565,16 @@ class TestJudgeTurn(SimpleTestCase):offer = questions["offer"]assert isinstance(offer, ChoiceQuestion) and set(offer.criteria) == ALL_OFFERS- def test_a_turn_with_one_possible_offer_skips_the_offer_question(self):+ @parameterized.expand([("recurring", 0.9), ("one_time", 0.1), ("missing", None)])+ def test_a_turn_with_one_possible_offer_skips_the_offer_question(+ self, _name: str, recurrence_probability: float | None+ ) -> None:transcript = build_turn_transcript(_metric_turn())only_scout = frozenset({OfferKind.SCOUT})- answers = _answers({"show_offer": 0.8}, {"intent": "metric_state", "scout_mode": "report", "cadence": "weekly"})+ nouls = {"show_offer": 0.8}+ if recurrence_probability is not None:+ nouls["scout_recurring"] = recurrence_probability+ answers = _answers(nouls, {"intent": "metric_state", "scout_mode": "report", "cadence": "weekly"})with patch(f"{JUDGMENT}.build_system_one_client", return_value=MagicMock(decide=MagicMock(return_value=answers))@@ -574,6 +583,9 @@ class TestJudgeTurn(SimpleTestCase):assert "offer" not in build_judge_questions(transcript, only_scout)assert judgment is not None and judgment.offer == OfferKind.SCOUT+ assert judgment.scout_recurrence_probability == (recurrence_probability or 0.0)+ expected = OfferKind.SCOUT if recurrence_probability == 0.9 else OfferKind.NONE+ assert pick_offer(judgment, only_scout) == expecteddef test_a_turn_with_more_issues_than_a_choice_takes_still_offers_the_one_the_answer_names(self):issues = [{"id": f"issue-{index}", "name": f"Error {index:03d}"} for index in range(300)]@@ -677,6 +689,31 @@ class TestPickOffer(SimpleTestCase):[("offers_what_jev_picked", {}, ALL_OFFERS, OfferKind.SCOUT),("below_the_show_threshold", {"show_probability": 0.3}, ALL_OFFERS, OfferKind.NONE),+ (+ "one_time_investigation",+ {+ "intent": TurnIntent.DIAGNOSTIC,+ "scout_mode": ScoutMode.INVESTIGATE,+ "scout_recurrence_probability": 0.1,+ },+ ALL_OFFERS,+ OfferKind.NONE,+ ),+ ("one_time_report", {"scout_recurrence_probability": 0.1}, ALL_OFFERS, OfferKind.NONE),+ ("below_recurrence_threshold", {"scout_recurrence_probability": 0.49}, ALL_OFFERS, OfferKind.NONE),+ ("at_recurrence_threshold", {"scout_recurrence_probability": 0.5}, ALL_OFFERS, OfferKind.SCOUT),+ (+ "repeatable_investigation",+ {"intent": TurnIntent.DIAGNOSTIC, "scout_mode": ScoutMode.INVESTIGATE},+ ALL_OFFERS,+ OfferKind.SCOUT,+ ),+ (+ "one_time_investigation_notebook",+ {"offer": OfferKind.NOTEBOOK, "scout_recurrence_probability": 0.1},+ ALL_OFFERS,+ OfferKind.NOTEBOOK,+ ),("pick_the_project_cannot_act_on", {}, frozenset({OfferKind.NOTEBOOK}), OfferKind.NONE),("alert_without_an_insight", {"offer": OfferKind.ALERT, "insight": None}, ALL_OFFERS, OfferKind.NONE),("alert_on_a_funnel", {"offer": OfferKind.ALERT, "insight": FUNNEL_INSIGHT}, ALL_OFFERS, OfferKind.NONE),@@ -872,6 +909,7 @@ class TestClassifyTurn(SimpleTestCase):)assert verdict is not None and verdict.draft == draft+ assert verdict.scout_recurrence_probability == 0.9assert draft_scout_mock.call_args.kwargs["mode"] == ScoutMode.WATCHassert draft_scout_mock.call_args.kwargs["cadence"] == ScoutCadence.DAILYassert card_copy(draft).title == "Get a Slack message when this changes"@@ -883,6 +921,16 @@ class TestClassifyTurn(SimpleTestCase):assert verdict.picked == OfferKind.SCOUTassert verdict.draft is None+ def test_a_one_time_investigation_is_not_drafted(self) -> None:+ verdict, draft_scout_mock, draft_notebook_mock = self._classify(+ _judgment(intent=TurnIntent.DIAGNOSTIC, scout_mode=ScoutMode.INVESTIGATE, scout_recurrence_probability=0.1)+ )++ assert verdict is not None and verdict.picked == OfferKind.NONE and verdict.draft is None+ assert verdict.scout_recurrence_probability == 0.1+ draft_scout_mock.assert_not_called()+ draft_notebook_mock.assert_not_called()+def test_a_failed_judgment_returns_none(self):verdict, draft_scout_mock, _ = self._classify(None)@@ -890,6 +938,38 @@ class TestClassifyTurn(SimpleTestCase):draft_scout_mock.assert_not_called()+class TestCaptureClassified(SimpleTestCase):+ @parameterized.expand(+ [+ ("accepted_scout", 0.9, True),+ ("rejected_scout", 0.1, False),+ ("zero_score", 0.0, False),+ ("failed_judgment", None, False),+ ]+ )+ def test_records_recurrence_probability(self, _name: str, probability: float | None, emitted: bool) -> None:+ verdict = (+ replace(+ _verdict(offer=OfferKind.SCOUT if emitted else OfferKind.NONE), scout_recurrence_probability=probability+ )+ if probability is not None+ else None+ )+ with patch(f"{SERVICE}.ph_scoped_capture") as capture:+ _capture_classified(+ MagicMock(team_id=7, task_id="task-1", id="run-1"),+ MagicMock(distinct_id="user-1"),+ verdict,+ emitted=emitted,+ turn_index=0,+ )++ event = capture.return_value.__enter__.return_value.call_args.kwargs+ assert event["event"] == "posthog ai turn suggestion classified"+ assert event["properties"]["scout_recurrence_probability"] == probability+ assert event["properties"]["emitted"] is emitted++def _gateway_reply(payload: dict) -> MagicMock:client = MagicMock()client.with_options.return_value = clientdiff --git a/products/posthog_ai/backend/turn_suggestions/benchmark_cases.yaml b/products/posthog_ai/backend/turn_suggestions/benchmark_cases.yamlindex c3aa1699772f..be817d2c13d4 100644--- a/products/posthog_ai/backend/turn_suggestions/benchmark_cases.yaml+++ b/products/posthog_ai/backend/turn_suggestions/benchmark_cases.yaml@@ -99,6 +99,27 @@tools: [query-retention, query-trends, query-funnel]answer: Mobile signups retain 18% at week 4 against 31% on desktop. Mobile users rarely finish the onboarding checklist (22% against 58%), and those who do finish retain about as well as desktop users.+- name: upload-bug-cause-found+ category: diagnostic+ acceptable: [notebook, none]+ question: Why did file uploads fail after Monday's deployment?+ tools: [query-error-tracking-issues-list, query-trends]+ answer: The deployment removed a required request header from the upload client. Failed uploads rose to 74 before the rollback restored the header. This explains the failures in that release.++- name: export-bug-still-unresolved+ category: diagnostic+ acceptable: [notebook, none]+ question: What caused the export failure in job sample-job-17?+ tools: [query-error-tracking-issues-list]+ answer: The job failed while reading an empty attachment. The error happened three times in that job, and no fix has shipped yet. The next step is to reproduce the failure with an empty file.++- name: checkout-recurring-investigation+ category: recurring_metric+ acceptable: [scout]+ question: Keep checking checkout conversion each week and repeat this investigation if it falls again.+ tools: [query-funnel, query-trends, query-session-recordings-list]+ answer: Measure the checkout_started to order_completed funnel over the last seven days against the preceding seven days. If conversion falls by more than 20%, compare browser breakdowns and inspect recordings of failed checkouts. Post the cause when a drop occurs and stay silent otherwise.+# Saved insights- name: saved-insight-alertcategory: saved_insightdiff --git a/products/posthog_ai/backend/turn_suggestions/classifier.py b/products/posthog_ai/backend/turn_suggestions/classifier.pyindex a53667c4a768..063a0e4e95a2 100644--- a/products/posthog_ai/backend/turn_suggestions/classifier.py+++ b/products/posthog_ai/backend/turn_suggestions/classifier.py@@ -35,6 +35,7 @@ logger = structlog.get_logger(__name__)# probabilities behind every decision.SHOW_THRESHOLD = 0.5ISSUE_RESOLVED_THRESHOLD = 0.5+SCOUT_RECURRENCE_THRESHOLD = 0.5@frozen@@ -51,6 +52,8 @@ def pick_offer(if judgment.show_probability < show_threshold or offer not in available:return OfferKind.NONEmatch offer:+ case OfferKind.SCOUT if judgment.scout_recurrence_probability < SCOUT_RECURRENCE_THRESHOLD:+ return OfferKind.NONEcase OfferKind.ALERT if judgment.insight is None or not judgment.insight.alertable:return OfferKind.NONEcase OfferKind.SUBSCRIPTION if judgment.insight is None:@@ -157,5 +160,6 @@ def classify_turn(show_probability=judgment.show_probability,picked=picked,offer_probabilities=judgment.offer_probabilities,+ scout_recurrence_probability=judgment.scout_recurrence_probability,draft=draft,)diff --git a/products/posthog_ai/backend/turn_suggestions/judgment.py b/products/posthog_ai/backend/turn_suggestions/judgment.pyindex 0e3199415db2..99be08c3324c 100644--- a/products/posthog_ai/backend/turn_suggestions/judgment.py+++ b/products/posthog_ai/backend/turn_suggestions/judgment.py@@ -136,6 +136,21 @@ _OFFER_CRITERIA: dict[OfferKind, JsonValue] = {},}+_SCOUT_RECURRING = NoulQuestion(+ instructions="Does the conversation establish an analysis that would produce useful new results from fresh data on future scheduled runs?",+ criteria_true=(+ "The conversation defines an ongoing metric or a scoped analysis that can run over a new relative time window. "+ "For an investigation, it identifies the metric to check and repeatable steps to run when that metric changes. "+ "An explicit request to keep monitoring that defined metric also qualifies."+ ),+ criteria_false=(+ "The turn only diagnoses or fixes a specific bug, incident, failed run, or historical event, even if it mentions "+ "numbers, a weekly comparison, or an unresolved problem. Reopening the same report or checking the same fix "+ "is not a recurring analysis. A report reference alone does not define a metric. "+ "The conversation does not establish what fresh data a scheduled run would check."+ ),+)+_SCOUT_MODE = ChoiceQuestion(instructions="If PostHog AI offered a scheduled agent that reruns the analysis in `latest_turn`, which kind of run would fit?",criteria={@@ -192,6 +207,7 @@ class TurnJudgment:intent: TurnIntentoffer: OfferKindoffer_probabilities: Mapping[str, float]+ scout_recurrence_probability: floatscout_mode: ScoutModecadence: ScoutCadencenotebook_template: NotebookTemplate@@ -306,6 +322,7 @@ def build_judge_questions(transcript: TurnTranscript, available: frozenset[Offercriteria={kind: _OFFER_CRITERIA[kind] for kind in OfferKind if kind in available},)if OfferKind.SCOUT in available:+ questions["scout_recurring"] = _SCOUT_RECURRINGquestions["scout_mode"] = _SCOUT_MODEif available & {OfferKind.SCOUT, OfferKind.SUBSCRIPTION}:questions["cadence"] = _CADENCE@@ -374,6 +391,7 @@ def read_judgment(result: SystemOneResult, transcript: TurnTranscript, availableintent=TurnIntent(choices["intent"].choice),offer=offer,offer_probabilities=offer_probabilities,+ scout_recurrence_probability=nouls["scout_recurring"].probability if "scout_recurring" in nouls else 0.0,scout_mode=ScoutMode(picked("scout_mode", ScoutMode.REPORT)),cadence=ScoutCadence(picked("cadence", ScoutCadence.WEEKLY)),notebook_template=NotebookTemplate(picked("notebook_template", NotebookTemplate.CONVERSATION)),diff --git a/products/posthog_ai/backend/turn_suggestions/service.py b/products/posthog_ai/backend/turn_suggestions/service.pyindex 835e79ca542a..d18d427019f5 100644--- a/products/posthog_ai/backend/turn_suggestions/service.py+++ b/products/posthog_ai/backend/turn_suggestions/service.py@@ -147,6 +147,7 @@ def _capture_classified("picked": verdict.picked.value if verdict else None,"show_probability": verdict.show_probability if verdict else None,"offer_probabilities": dict(verdict.offer_probabilities) if verdict else None,+ "scout_recurrence_probability": verdict.scout_recurrence_probability if verdict else None,"offer": verdict.picked.value if verdict and verdict.draft else None,"emitted": emitted,},diff --git a/products/posthog_ai/backend/turn_suggestions/verdict.py b/products/posthog_ai/backend/turn_suggestions/verdict.pyindex f6c885065a6d..fbc532bef34f 100644--- a/products/posthog_ai/backend/turn_suggestions/verdict.py+++ b/products/posthog_ai/backend/turn_suggestions/verdict.py@@ -136,4 +136,5 @@ class TurnVerdict:show_probability: floatpicked: OfferKindoffer_probabilities: Mapping[str, float]+ scout_recurrence_probability: floatdraft: Draft | None