: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): fork a fresh report when a signal matches a failed report (#129742)
posthog[bot]69d397155942
diff --git a/products/signals/backend/emission/tests/test_emit_signals.py b/products/signals/backend/emission/tests/test_emit_signals.pyindex c4b1f5239bc2..217395443234 100644--- a/products/signals/backend/emission/tests/test_emit_signals.py+++ b/products/signals/backend/emission/tests/test_emit_signals.py@@ -966,12 +966,20 @@ class TestExistingSourceOnlyOutputs:link_target_id=recurrence_parent.id,)+ failed = SignalReport.objects.create(team=team, status=SignalReport.Status.FAILED)+closed = _reports_closed_to_source_state(team.id,- {str(resolved.id), str(fixed_dismissal.id), str(merged_source.id), str(recurrence_parent.id)},+ {+ str(resolved.id),+ str(fixed_dismissal.id),+ str(merged_source.id),+ str(recurrence_parent.id),+ str(failed.id),+ },)- assert closed == {str(resolved.id), str(fixed_dismissal.id)}+ assert closed == {str(resolved.id), str(fixed_dismissal.id), str(failed.id)}direct_reports = SignalReport.objects.bulk_create([SignalReport(team=team, status=SignalReport.Status.RESOLVED) for _ in range(20)]diff --git a/products/signals/backend/recurrence.py b/products/signals/backend/recurrence.pyindex a71e4951b2dc..d7ed9989b125 100644--- a/products/signals/backend/recurrence.py+++ b/products/signals/backend/recurrence.py@@ -1,7 +1,8 @@-"""Route new evidence after a fixed verdict or a rejected repository choice.+"""Route new evidence after a fixed verdict, a rejected repository choice, or a failed report.A resolved report and a report dismissed as fixed make the same factual claim: the issue is gone.-A wrong-repo dismissal leaves the issue unresolved. Both need a fresh report for later matching+A wrong-repo dismissal leaves the issue unresolved. A failed report never promotes again, so a+signal that joins it starts no research. All of them need a fresh report for later matchingsignals, but only fixed verdicts supply prior-fix context to research."""@@ -14,7 +15,7 @@ from products.signals.backend.artefact_schemas import DISMISSAL_REASON_WRONG_REPfrom products.signals.backend.enums import ReportLinkKindfrom products.signals.backend.models import SignalReport, SignalReportArtefactfrom products.signals.backend.repo_corrections import sanitized_repository-from products.signals.backend.typed_report_links import incoming_links, outgoing_links+from products.signals.backend.typed_report_links import has_open_or_merged_pull_request, incoming_links, outgoing_linksdef _latest_dismissal(*, team_id: int, report_id: str) -> SignalReportArtefact | None:@@ -94,19 +95,30 @@ def reports_fork_recurrence(*, team_id: int, reports: Collection[SignalReport])if report.status in {SignalReport.Status.RESOLVED, SignalReport.Status.CLOSED}or (report.status == SignalReport.Status.MONITORING and not monitoring_enabled)}+ failed_ids = {str(report.id) for report in reports if report.status == SignalReport.Status.FAILED}+ failed_ids -= has_open_or_merged_pull_request(team_id=team_id, report_ids=failed_ids)suppressed_ids = {str(report.id) for report in reports if report.status == SignalReport.Status.SUPPRESSED}reasons_by_report = dismissal_reasons_by_report(team_id=team_id, report_ids=suppressed_ids)- return terminal_ids | {- report_id- for report_id in suppressed_ids- if reasons_by_report.get(report_id, [None])[0] in (FIXED_DISMISSAL_REASONS | {DISMISSAL_REASON_WRONG_REPO})- }+ return (+ terminal_ids+ | failed_ids+ | {+ report_id+ for report_id in suppressed_ids+ if reasons_by_report.get(report_id, [None])[0] in (FIXED_DISMISSAL_REASONS | {DISMISSAL_REASON_WRONG_REPO})+ }+ )def forks_recurrence(report: SignalReport) -> bool:"""Whether a new signal on `report` starts a fresh recurrence report instead of joining it."""- if report.effective_status in {SignalReport.Status.RESOLVED, SignalReport.Status.CLOSED}:+ status = report.effective_status+ if status in {SignalReport.Status.RESOLVED, SignalReport.Status.CLOSED}:return True+ if status == SignalReport.Status.FAILED:+ # A pull request on a failed report is work that already exists, so new evidence joins it+ # rather than starting a second implementation.+ return not has_open_or_merged_pull_request(team_id=report.team_id, report_ids=[str(report.id)])return report.status == SignalReport.Status.SUPPRESSED and recurrence_dismissal_at(report) is not Nonediff --git a/products/signals/backend/temporal/grouping.py b/products/signals/backend/temporal/grouping.pyindex 7d3a6aadf0c1..db4b16b893d9 100644--- a/products/signals/backend/temporal/grouping.py+++ b/products/signals/backend/temporal/grouping.py@@ -932,7 +932,11 @@ async def assign_and_emit_signal_activity(input: AssignAndEmitSignalInput) -> Asreport = signal_target_report(report, lock=True)if forks_recurrence(report):parent_report = report- inherit_content = not _is_safety_suppressed(str(parent_report.id), input.team_id)+ # A failed report often holds only the matcher's first guess, so the fork keeps+ # its content only when a research pass completed.+ inherit_content = (+ parent_report.status != SignalReport.Status.FAILED or _has_completed_research(parent_report)+ ) and not _is_safety_suppressed(str(parent_report.id), input.team_id)report = SignalReport.objects.create(team_id=input.team_id,status=SignalReport.Status.POTENTIAL,@@ -986,6 +990,7 @@ async def assign_and_emit_signal_activity(input: AssignAndEmitSignalInput) -> As# Promotion rules by status:# - SUPPRESSED: never promoted. Fixed and wrong-repo dismissals spawn a fresh report above.+ # - FAILED: never promoted. Unless a pull request is open or merged, a new signal spawns a fresh report above.# - RESOLVED: terminal — never receives new signals (a recurrence spawns a fresh report above).# - POTENTIAL: promote once total_weight >= WEIGHT_THRESHOLD and signal_count >= signals_at_run# (snooze gate, defaults to 0). Uncapped — a report's first research always runs.diff --git a/products/signals/backend/test/test_assign_and_emit_signal.py b/products/signals/backend/test/test_assign_and_emit_signal.pyindex 944b551cc620..7445d0f3249c 100644--- a/products/signals/backend/test/test_assign_and_emit_signal.py+++ b/products/signals/backend/test/test_assign_and_emit_signal.py@@ -11,11 +11,12 @@ import pytest_asynciofrom asgiref.sync import sync_to_asyncfrom posthog.models import Organization, Team+from posthog.models.scoping import team_scopefrom posthog.sync import database_sync_to_asyncfrom products.signals.backend.agent_runtime import DEFAULT_RUNTIMEfrom products.signals.backend.artefact_attribution import ArtefactAttribution-from products.signals.backend.artefact_schemas import Dismissal, RelatedTo, ReportLink+from products.signals.backend.artefact_schemas import Dismissal, PullRequestLink, RelatedTo, ReportLinkfrom products.signals.backend.daily_limit import DailyReportLimitGatefrom products.signals.backend.enums import ReportLinkKind, SignalSourceProduct, SignalSourceTypefrom products.signals.backend.models import (@@ -23,6 +24,7 @@ from products.signals.backend.models import (SignalReport,SignalReportArtefact,SignalReportMonitoringAttempt,+ SignalReportPullRequest,SignalSpend,)from products.signals.backend.quota import SelfDrivingQuotaGate@@ -589,6 +591,82 @@ async def test_resolved_match_spawns_new_report_and_leaves_resolved_untouched(assert refreshed_resolved.signal_count == 2+ ("signals_researched", "pr_state", "forks", "inherits"),+ [+ (None, None, True, False),+ (2, None, True, True),+ (None, SignalReportPullRequest.State.CLOSED, True, False),+ (None, SignalReportPullRequest.State.OPEN, False, False),+ ],+)+async def test_failed_match_spawns_one_fork_that_later_signals_join(+ ateam: Team, signals_researched: int | None, pr_state: str | None, forks: bool, inherits: bool+) -> None:+ failed = await database_sync_to_async(SignalReport.objects.create)(+ team=ateam,+ status=SignalReport.Status.FAILED,+ total_weight=1.5,+ signal_count=2,+ signals_at_run=2 if signals_researched else 0,+ signals_researched=signals_researched,+ title="original title",+ summary="original summary",+ )+ if pr_state is not None:++ def _attach_pr() -> None:+ with team_scope(ateam.id):+ pr = SignalReportPullRequest.objects.create(+ team_id=ateam.id,+ repository="owner/repo",+ number=7,+ url="https://github.com/owner/repo/pull/7",+ state=pr_state,+ )+ link = SignalReportArtefact.add_log(+ team_id=ateam.id,+ report_id=str(failed.id),+ content=PullRequestLink(url=pr.url),+ attribution=ArtefactAttribution.system(),+ )+ link.pull_request = pr+ link.save(update_fields=["pull_request"])++ await database_sync_to_async(_attach_pr)()++ first = await assign_and_emit_signal_activity(+ _build_input(ateam.id, _existing_match(str(failed.id)), weight=WEIGHT_THRESHOLD * 0.5)+ )+ second = await assign_and_emit_signal_activity(+ _build_input(ateam.id, _existing_match(str(failed.id)), weight=WEIGHT_THRESHOLD * 0.5)+ )++ refreshed_failed = await database_sync_to_async(SignalReport.objects.get)(id=failed.id)+ assert refreshed_failed.status == SignalReport.Status.FAILED+ if not forks:+ assert first.report_id == second.report_id == str(failed.id)+ assert second.promoted is False+ assert refreshed_failed.signal_count == 4+ return++ assert first.report_id == second.report_id != str(failed.id)+ assert refreshed_failed.signal_count == 2+ fork = await database_sync_to_async(SignalReport.objects.get)(id=first.report_id)+ assert fork.signal_count == 2+ assert second.promoted is True+ assert fork.status == SignalReport.Status.CANDIDATE+ assert (fork.title, fork.summary) == (("original title", "original summary") if inherits else ("", ""))+ link = await database_sync_to_async(+ lambda: SignalReportArtefact.objects.get(report=fork, type=SignalReportArtefact.ArtefactType.REPORT_LINK)+ )()+ assert ReportLink.model_validate_json(link.content) == ReportLink(+ kind=ReportLinkKind.RECURRENCE_OF, report_id=str(failed.id)+ )++# ---------------------------------------------------------------------------# Recurrence after a dismissal that claimed the issue was fixed# ---------------------------------------------------------------------------@@ -1322,12 +1400,11 @@ async def test_potential_promotes_past_last_bucket_for_first_research(ateam):[SignalReport.Status.IN_PROGRESS,SignalReport.Status.PENDING_INPUT,- SignalReport.Status.FAILED,SignalReport.Status.SUPPRESSED,],)async def test_non_promoting_states_increment_counters_but_do_not_promote(ateam, starting_status):- """For IN_PROGRESS / PENDING_INPUT / FAILED / SUPPRESSED:+ """For IN_PROGRESS / PENDING_INPUT / SUPPRESSED:- weight + signal_count still update- status is unchanged- promoted=False so no workflow spawn attempt