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.

fix(signals): forward refund feedback to the authoring scout (#77027)

Alex LaGuardia98fcccec1367
diff --git a/products/signals/backend/test/test_dismissal_scout_notes.py b/products/signals/backend/test/test_dismissal_scout_notes.py
index ff17a6c2c88a..226c7bab3801 100644
--- a/products/signals/backend/test/test_dismissal_scout_notes.py
+++ b/products/signals/backend/test/test_dismissal_scout_notes.py
@@ -3,7 +3,9 @@ from __future__ import annotations
import json
from collections.abc import Callable, Iterator
from contextlib import contextmanager
+from datetime import UTC, datetime
+from freezegun import freeze_time
from posthog.test.base import APIBaseTest
from unittest.mock import patch
@@ -12,6 +14,9 @@ from django.apps import apps
from parameterized import parameterized
from rest_framework import status
+# Load the API URLconf (and its pydantic.v1 import chain) before any freeze_time window:
+# first-importing date-subclassing modules under freezegun's fake date raises a metaclass conflict.
+import posthog.api.rest_router # noqa: F401
from posthog.models.personal_api_key import PersonalAPIKey
from posthog.models.team.team import Team
from posthog.models.utils import generate_random_token_personal, hash_key_value
@@ -26,10 +31,14 @@ from products.signals.backend.models import (
SignalScoutNote,
SignalScoutRun,
)
+from products.signals.backend.test.test_billing import _make_pr_run
from products.skills.backend.models.skills import LLMSkill
SCOUT_SKILL = "signals-scout-error-tracking"
UNFORWARDED_NOTE = "feedback that never made it to a note"
+_REFUND_PERIOD = ["2026-06-01T00:00:00Z", "2026-07-01T00:00:00Z"]
+_REFUND_NOW = "2026-06-15T12:00:00Z"
+_PR_RUN_AT = datetime(2026, 6, 10, tzinfo=UTC)
class TestDismissalScoutNotes(APIBaseTest):
@@ -39,6 +48,15 @@ class TestDismissalScoutNotes(APIBaseTest):
def _bulk_state_url(self) -> str:
return f"/api/projects/{self.team.id}/signals/reports/bulk-state/"
+ def _refund(self, report: SignalReport, **body):
+ # Refund eligibility is scored against the org's current billing period, so the tests that
+ # use this run inside `_REFUND_NOW` with the period that contains it.
+ self.organization.usage = {"period": _REFUND_PERIOD}
+ self.organization.save()
+ return self.client.post(
+ f"/api/projects/{self.team.id}/signals/reports/{report.id}/refund/", body, format="json"
+ )
+
def _create_report(self, title: str = "Checkout errors spiked") -> SignalReport:
return SignalReport.objects.create(
team=self.team,
@@ -169,6 +187,88 @@ class TestDismissalScoutNotes(APIBaseTest):
# The artefact is the record of truth either way, so a resolve still keeps the feedback.
assert self._dismissal_notes_on(report) == ["context the scout should have"]
+ @freeze_time(_REFUND_NOW)
+ @patch("posthoganalytics.feature_enabled", return_value=True)
+ def test_refund_feedback_reaches_the_authoring_scout(self, _flag) -> None:
+ self._create_scout_skill()
+ report = self._create_report()
+ self._create_run(emitted_report_ids=[str(report.id)])
+ _make_pr_run(self.team, report, created_at=_PR_RUN_AT)
+
+ response = self._refund(report, reason="pr_incorrect", note="the PR edits a different endpoint")
+
+ assert response.status_code == status.HTTP_200_OK, response.json()
+ # Exactly one: a refund suppresses the report through its own transition rather than the
+ # `state` action, so routing it back through that action would forward the same judgement
+ # twice and hand the scout a duplicate.
+ assert len(self._notes()) == 1
+ note = self._notes()[0]
+ assert note.skill_name == SCOUT_SKILL
+ assert note.origin == SignalScoutNote.Origin.REPORT_DISMISSAL
+ assert "the PR edits a different endpoint" in note.content
+ # The refund's own reason rather than the flat `refunded` code the artefact carries, because
+ # `pr_incorrect` is what tells the scout its report promised something the PR did not deliver.
+ assert "pr_incorrect" in note.content
+
+ @freeze_time(_REFUND_NOW)
+ @patch("posthoganalytics.feature_enabled", return_value=True)
+ def test_a_repeat_refund_does_not_forward_a_second_time(self, _flag) -> None:
+ self._create_scout_skill()
+ report = self._create_report()
+ self._create_run(emitted_report_ids=[str(report.id)])
+ _make_pr_run(self.team, report, created_at=_PR_RUN_AT)
+
+ first = self._refund(report, reason="pr_incorrect", note="the PR edits a different endpoint")
+ second = self._refund(report, reason="pr_incorrect", note="the PR edits a different endpoint")
+
+ assert first.status_code == status.HTTP_200_OK, first.json()
+ assert second.status_code == status.HTTP_200_OK, second.json()
+ assert second.json()["already_refunded"] is True
+ # The refund is idempotent by row lock, and forwarding has to inherit that: a double-clicked
+ # Refund button must not teach the scout the same verdict twice.
+ assert len(self._notes()) == 1
+
+ @freeze_time(_REFUND_NOW)
+ @patch("posthoganalytics.feature_enabled", return_value=True)
+ def test_refund_without_a_note_forwards_nothing(self, _flag) -> None:
+ self._create_scout_skill()
+ report = self._create_report()
+ self._create_run(emitted_report_ids=[str(report.id)])
+ _make_pr_run(self.team, report, created_at=_PR_RUN_AT)
+
+ response = self._refund(report, reason="pr_not_useful")
+
+ assert response.status_code == status.HTTP_200_OK, response.json()
+ # The note is what carries a reason into the steering channel, so a refund with no prose
+ # forwards nothing even though its reason code is structured. The artefact still records it.
+ assert self._notes() == []
+ assert self._dismissal_notes_on(report) == [None]
+
+ @freeze_time(_REFUND_NOW)
+ @patch("posthoganalytics.feature_enabled", return_value=True)
+ def test_no_note_when_a_refund_leaves_a_merged_pr_report_resolved(self, _flag) -> None:
+ self._create_scout_skill()
+ report = self._create_report()
+ report.status = SignalReport.Status.RESOLVED
+ report.save(update_fields=["status"])
+ self._create_run(emitted_report_ids=[str(report.id)])
+ _make_pr_run(
+ self.team,
+ report,
+ created_at=_PR_RUN_AT,
+ output={"pr_url": "https://github.com/x/y/pull/1", "pr_merged": True},
+ )
+
+ response = self._refund(report, reason="duplicate", note="the checkout scout already filed this")
+
+ assert response.status_code == status.HTTP_200_OK, response.json()
+ # A merged PR leaves the report RESOLVED, and a resolve says the report did its job rather
+ # than that filing it was wrong, so the refund has nothing to teach the authoring scout.
+ report.refresh_from_db()
+ assert report.status == SignalReport.Status.RESOLVED
+ assert self._notes() == []
+ assert self._dismissal_notes_on(report) == ["the checkout scout already filed this"]
+
def test_bulk_dismissal_writes_one_note_per_scout_not_one_per_report(self) -> None:
self._create_scout_skill()
authored = [self._create_report(title=f"Report {index}") for index in range(3)]
diff --git a/products/signals/backend/views.py b/products/signals/backend/views.py
index 2fd9920ad9af..1dc2c986e701 100644
--- a/products/signals/backend/views.py
+++ b/products/signals/backend/views.py
@@ -2286,6 +2286,18 @@ class SignalReportViewSet(
refund_id = str(refund.id)
transaction.on_commit(lambda: sync_signals_refund_credit.delay(refund_id))
+ # A refund suppresses the report, which is a judgement the authoring scout should hear, so it
+ # forwards on the same channel as any other dismissal. `refund.reason` rather than the
+ # `refunded` code the artefact records, because `pr_incorrect` tells a scout the PR missed
+ # what its report promised while `refunded` only says money moved. Outside the atomic block
+ # because forwarding writes rows of its own and must not be able to roll back the refund it
+ # reports; a report left RESOLVED by a merged PR is dropped by the forwarding path itself.
+ self._forward_dismissal_note(
+ reports=[report],
+ dismissal_reason=refund.reason,
+ dismissal_note=note,
+ )
+
report_user_action(
user,
"signals_pr_refund_created",