: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
feat(signals): Ądd management command to reingest specific signal reports (#68638)
Alex Lebedev1e38b1e79d5c
diff --git a/posthog/management/commands/test/test_set_recorder_script.py b/posthog/management/commands/test/test_set_recorder_script.pyindex ca867c618fe7..3b02d9acc9da 100644--- a/posthog/management/commands/test/test_set_recorder_script.py+++ b/posthog/management/commands/test/test_set_recorder_script.py@@ -106,9 +106,14 @@ class TestSetRecorderScriptCommand(BaseTest):assert "Sample rate must be between 0.0 and 1.0" in str(cm.exception)def test_sampling_is_consistent(self):- teams = []- for i in range(100):- teams.append(Team.objects.create(organization=self.organization, name=f"Team {i}"))+ # simple_hash maps consecutive decimal ids into a narrow band mod 10000, so a sequence-assigned+ # id block usually samples all-or-nothing; fixed ids give a deterministic 80/20 split at rate 0.5.+ Team.objects.bulk_create(+ [+ Team(id=9_400_000 + i, organization=self.organization, project=self.project, name=f"Team {i}")+ for i in range(100)+ ]+ )call_command("set_recorder_script",@@ -116,13 +121,13 @@ class TestSetRecorderScriptCommand(BaseTest):"--sample-rate=0.5",)- # Sampling is deterministic on team id, so the command must update exactly the teams- # that hash into the sample. Asserting a rough proportion is flaky: consecutive ids hash- # into a narrow band, so the count lands near 0 or near 100 rather than around 50.- expected_ids = {team.id for team in Team.objects.all() if sample_on_property(str(team.id), 0.5)}+ all_ids = set(Team.objects.values_list("id", flat=True))+ expected_ids = {team_id for team_id in all_ids if sample_on_property(str(team_id), 0.5)}updated_ids = set(Team.objects.filter(extra_settings__has_key="recorder_script").values_list("id", flat=True))assert updated_ids == expected_ids+ # Both sides non-empty proves the command actually filtered rather than updating none or all.+ assert updated_ids and all_ids - updated_idsdef test_bulk_updates_in_batches(self):# Use bulk_create with a shared project to avoid 2500 individualdiff --git a/products/signals/backend/management/AGENTS.md b/products/signals/backend/management/AGENTS.mdindex 841d4a150a46..40a8f5aa3987 100644--- a/products/signals/backend/management/AGENTS.md+++ b/products/signals/backend/management/AGENTS.md@@ -74,6 +74,18 @@ in `relevant_commit_hashes` to map to a user with a `SignalUserAutonomyConfig` wpriority threshold (personal or team default) covers the report's priority — otherwise thereport will be saved but no `Task` will be created.+## Re-ingesting reports++`reingest_signal_report` deletes specific reports and re-emits their signals through the active+pipeline (same `SignalReportReingestionWorkflow` as the API `reingest` action), so they regroup+and re-research from scratch:++```bash+python manage.py reingest_signal_report --team-id 1 <report-uuid> [<report-uuid> ...]+```++For a full-team wipe + reingest, use `reingest_team_signals --team-id 1` (add `--delete` for delete-only).+## Session summary (video-based)Test the SummarizeSingleSessionWorkflow with full video validation:diff --git a/products/signals/backend/management/commands/reingest_signal_report.py b/products/signals/backend/management/commands/reingest_signal_report.pynew file mode 100644index 000000000000..411f95c1222e--- /dev/null+++ b/products/signals/backend/management/commands/reingest_signal_report.py@@ -0,0 +1,74 @@+from datetime import timedelta+from typing import Any++from django.conf import settings+from django.core.exceptions import ValidationError+from django.core.management.base import BaseCommand, CommandError, CommandParser++from asgiref.sync import async_to_sync+from temporalio.common import RetryPolicy+from temporalio.exceptions import WorkflowAlreadyStartedError++from posthog.models import Team+from posthog.temporal.common.client import async_connect++from products.signals.backend.models import SignalReport+from products.signals.backend.temporal.reingestion import SignalReportReingestionWorkflow+from products.signals.backend.temporal.types import SignalReportReingestionWorkflowInputs+++async def _start_report_reingestion_workflow(team_id: int, report_id: str) -> None:+ client = await async_connect()+ await client.start_workflow(+ SignalReportReingestionWorkflow.run,+ SignalReportReingestionWorkflowInputs(team_id=team_id, report_id=report_id),+ id=SignalReportReingestionWorkflow.workflow_id_for(team_id, report_id),+ task_queue=settings.VIDEO_EXPORT_TASK_QUEUE,+ execution_timeout=timedelta(minutes=30),+ retry_policy=RetryPolicy(maximum_attempts=1),+ )+++class Command(BaseCommand):+ help = (+ "Re-ingest specific signal reports: delete each report and re-emit its signals "+ "through the active pipeline so they are regrouped and re-researched from scratch."+ )++ def add_arguments(self, parser: CommandParser) -> None:+ parser.add_argument("report_ids", nargs="+", help="One or more SignalReport UUIDs to reingest.")+ parser.add_argument("--team-id", type=int, required=True, help="The ID of the team the reports belong to.")++ def handle(self, *args: Any, **options: Any) -> None:+ team_id: int = options["team_id"]+ report_ids: list[str] = options["report_ids"]++ try:+ Team.objects.get(id=team_id)+ except Team.DoesNotExist as err:+ raise CommandError(f"Team {team_id} not found") from err++ for report_id in report_ids:+ try:+ report = SignalReport.objects.get(id=report_id, team_id=team_id)+ except (SignalReport.DoesNotExist, ValidationError) as err:+ raise CommandError(f"Report {report_id} not found in team {team_id}") from err++ workflow_id = SignalReportReingestionWorkflow.workflow_id_for(team_id, str(report.id))++ try:+ async_to_sync(_start_report_reingestion_workflow)(team_id, str(report.id))+ except WorkflowAlreadyStartedError:+ self.stdout.write(+ self.style.WARNING(+ f"Reingestion workflow already running for report {report.id} [workflow_id={workflow_id}]"+ )+ )+ continue++ self.stdout.write(+ self.style.SUCCESS(+ f"Started reingestion for report {report.id} (status={report.status}, "+ f"signals={report.signal_count}) [workflow_id={workflow_id}]"+ )+ )