: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(workflows): add the workflow idea model (#115803)
Daniel Marchukd8cfe301efb2
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yamlindex ad5296c79237..7b070b892247 100644--- a/.semgrep/rules/security/idor-team-scoped-models.yaml+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml@@ -467,6 +467,7 @@ rules:|WizardScan|WizardSession|WizardWorker+ |WorkflowIdea|WorkflowProposal)$- focus-metavariable: $SINK@@ -923,6 +924,7 @@ rules:|WizardScan|WizardSession|WizardWorker+ |WorkflowIdea|WorkflowProposal)$# Exclude when team filter is presentdiff --git a/products/workflows/backend/facade/enums.py b/products/workflows/backend/facade/enums.pyindex 3f2fa1ab6e92..3e8ddbd14167 100644--- a/products/workflows/backend/facade/enums.py+++ b/products/workflows/backend/facade/enums.py@@ -56,6 +56,17 @@ class WorkflowProposalStatus(LabeledStrEnum):APPLIED = "applied", "Applied"+class WorkflowIdeaStatus(LabeledStrEnum):+ SUGGESTED = "suggested", "Suggested"+ ACCEPTED = "accepted", "Accepted"+ DISMISSED = "dismissed", "Dismissed"+++class WorkflowIdeaSource(LabeledStrEnum):+ MANUAL = "manual", "Manual"+ SCOUT = "scout", "Scout"++class HogFlowState(LabeledStrEnum):DRAFT = "draft"ACTIVE = "active"diff --git a/products/workflows/backend/migrations/0033_workflowidea.py b/products/workflows/backend/migrations/0033_workflowidea.pynew file mode 100644index 000000000000..af7b9bf75572--- /dev/null+++ b/products/workflows/backend/migrations/0033_workflowidea.py@@ -0,0 +1,140 @@+# Generated by Django 5.2.18 on 2026-10-11 17:19++import django.db.models.deletion+from django.conf import settings+from django.db import migrations, models++import posthog.uuidt+++class Migration(migrations.Migration):+ dependencies = [+ ("posthog", "1396_events_retention_config"),+ ("workflows", "0032_teamworkflowsconfig_default_email_integration_id"),+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),+ ]++ operations = [+ migrations.CreateModel(+ name="WorkflowIdea",+ fields=[+ (+ "id",+ models.UUIDField(+ default=posthog.uuidt.uuid7,+ editable=False,+ primary_key=True,+ serialize=False,+ ),+ ),+ (+ "key",+ models.CharField(+ help_text="Stable name of the idea within the project.",+ max_length=200,+ ),+ ),+ (+ "title",+ models.CharField(+ help_text="Short name of the suggested workflow.",+ max_length=200,+ ),+ ),+ (+ "rationale",+ models.TextField(help_text="Why this workflow is worth running, in one or two sentences."),+ ),+ (+ "evidence",+ models.JSONField(+ blank=True,+ default=dict,+ help_text="The numbers behind the idea: trigger and goal events, the share who reach the goal with no message, the reachable audience, and when they were measured.",+ ),+ ),+ (+ "definition",+ models.JSONField(+ help_text="The workflow to create on accept, in the shape the workflows API takes: trigger, actions, edges, conversion and exit condition."+ ),+ ),+ (+ "source",+ models.CharField(+ choices=[("manual", "Manual"), ("scout", "Scout")],+ default="manual",+ max_length=20,+ ),+ ),+ (+ "source_ref",+ models.CharField(+ blank=True,+ help_text="The run or batch that produced the idea.",+ max_length=200,+ null=True,+ ),+ ),+ (+ "status",+ models.CharField(+ choices=[+ ("suggested", "Suggested"),+ ("accepted", "Accepted"),+ ("dismissed", "Dismissed"),+ ],+ default="suggested",+ max_length=20,+ ),+ ),+ (+ "dismiss_reason",+ models.TextField(blank=True, db_default="", default=""),+ ),+ ("created_at", models.DateTimeField(auto_now_add=True)),+ ("first_viewed_at", models.DateTimeField(blank=True, null=True)),+ ("notified_at", models.DateTimeField(blank=True, null=True)),+ ("resolved_at", models.DateTimeField(blank=True, null=True)),+ (+ "hog_flow",+ models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="+",+ to="workflows.hogflow",+ ),+ ),+ (+ "resolved_by",+ models.ForeignKey(+ blank=True,+ db_constraint=False,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="+",+ to=settings.AUTH_USER_MODEL,+ ),+ ),+ (+ "team",+ models.ForeignKey(+ db_constraint=False,+ on_delete=django.db.models.deletion.CASCADE,+ related_name="+",+ to="posthog.team",+ ),+ ),+ ],+ options={+ "indexes": [models.Index(fields=["team", "status"], name="workflow_idea_status_idx")],+ "constraints": [+ models.UniqueConstraint(fields=("team", "key"), name="unique_workflow_idea_key"),+ models.CheckConstraint(+ condition=models.Q(("key", ""), _negated=True), name="workflow_idea_key_not_blank"+ ),+ ],+ },+ ),+ ]diff --git a/products/workflows/backend/migrations/max_migration.txt b/products/workflows/backend/migrations/max_migration.txtindex 1c4435ec8d69..b8a3cae39e72 100644--- a/products/workflows/backend/migrations/max_migration.txt+++ b/products/workflows/backend/migrations/max_migration.txt@@ -1 +1 @@-0032_teamworkflowsconfig_default_email_integration_id+0033_workflowideadiff --git a/products/workflows/backend/models/__init__.py b/products/workflows/backend/models/__init__.pyindex 16d80dd740dc..bbc6b3a22a6c 100644--- a/products/workflows/backend/models/__init__.py+++ b/products/workflows/backend/models/__init__.py@@ -5,6 +5,7 @@ from .hog_flow_optimization import HogFlowOptimizationfrom .hog_flow_revision import HogFlowRevisionfrom .hog_flow_schedule.hog_flow_schedule import HogFlowSchedulefrom .team_workflows_config import TeamWorkflowsConfig+from .workflow_idea import WorkflowIdeafrom .workflow_proposal import WorkflowProposal__all__ = [@@ -15,5 +16,6 @@ __all__ = ["HogFlowSchedule","HogFlowTemplate","TeamWorkflowsConfig",+ "WorkflowIdea","WorkflowProposal",]diff --git a/products/workflows/backend/models/workflow_idea.py b/products/workflows/backend/models/workflow_idea.pynew file mode 100644index 000000000000..55696f0c48da--- /dev/null+++ b/products/workflows/backend/models/workflow_idea.py@@ -0,0 +1,84 @@+from typing import TYPE_CHECKING, Any++from django.db import models++from posthog.models.scoping.root_mixin import TeamScopedRootMixin+from posthog.models.utils import UUIDModel++from products.workflows.backend.facade.enums import WorkflowIdeaSource, WorkflowIdeaStatus++if TYPE_CHECKING:+ from posthog.models import Team+++class WorkflowIdea(TeamScopedRootMixin, UUIDModel):+ """A whole workflow PostHog suggests to a project, ready to become a draft.++ Accepting one creates a draft workflow from `definition`, so nothing reaches anyone until a person+ reviews the draft, picks a sender and enables it.+ """++ Status = WorkflowIdeaStatus+ Source = WorkflowIdeaSource++ class Meta:+ constraints = [+ # Suggesting the same idea twice to one project would undo a dismissal.+ models.UniqueConstraint(fields=["team", "key"], name="unique_workflow_idea_key"),+ models.CheckConstraint(condition=~models.Q(key=""), name="workflow_idea_key_not_blank"),+ ]+ indexes = [+ models.Index(fields=["team", "status"], name="workflow_idea_status_idx"),+ ]++ # db_constraint=False: a real FK constraint to a hot table takes a parent-table lock on creation.+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False, related_name="+")++ key = models.CharField(max_length=200, help_text="Stable name of the idea within the project.")+ title = models.CharField(max_length=200, help_text="Short name of the suggested workflow.")+ rationale = models.TextField(help_text="Why this workflow is worth running, in one or two sentences.")+ evidence = models.JSONField(+ default=dict,+ blank=True,+ help_text=(+ "The numbers behind the idea: trigger and goal events, the share who reach the goal with "+ "no message, the reachable audience, and when they were measured."+ ),+ )+ definition = models.JSONField(+ help_text=(+ "The workflow to create on accept, in the shape the workflows API takes: trigger, actions, "+ "edges, conversion and exit condition."+ )+ )+ source = models.CharField(max_length=20, choices=Source.choices, default=Source.MANUAL.value)+ source_ref = models.CharField(+ max_length=200, null=True, blank=True, help_text="The run or batch that produced the idea."+ )++ status = models.CharField(max_length=20, choices=Status.choices, default=Status.SUGGESTED.value)+ dismiss_reason = models.TextField(blank=True, default="", db_default="")+ hog_flow = models.ForeignKey(+ "workflows.HogFlow", on_delete=models.SET_NULL, null=True, blank=True, related_name="+"+ )++ created_at = models.DateTimeField(auto_now_add=True)+ first_viewed_at = models.DateTimeField(null=True, blank=True)+ notified_at = models.DateTimeField(null=True, blank=True)+ resolved_at = models.DateTimeField(null=True, blank=True)+ resolved_by = models.ForeignKey(+ "posthog.User", on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False, related_name="+"+ )++ def save(self, *args: Any, **kwargs: Any) -> None:+ if self.hog_flow is not None and _project_id(self.hog_flow.team) != _project_id(self.team):+ raise ValueError("A workflow idea can only link a workflow from its own project.")+ super().save(*args, **kwargs)++ def __str__(self) -> str:+ return f"WorkflowIdea {self.team_id}/{self.key} ({self.status})"+++def _project_id(team: "Team") -> int:+ # The idea's team is rewritten to the parent on save, while the workflow keeps its environment.+ return team.parent_team_id or team.iddiff --git a/products/workflows/backend/test/test_workflow_idea_model.py b/products/workflows/backend/test/test_workflow_idea_model.pynew file mode 100644index 000000000000..15a8f2aa4b3f--- /dev/null+++ b/products/workflows/backend/test/test_workflow_idea_model.py@@ -0,0 +1,34 @@+from posthog.test.base import BaseTest++from posthog.models import Team++from products.workflows.backend.models import HogFlow+from products.workflows.backend.models.workflow_idea import WorkflowIdea+++class TestWorkflowIdeaModel(BaseTest):+ def test_linking_a_workflow_from_another_project_is_refused(self):+ other_team = Team.objects.create(organization=self.organization, name="Other")+ idea = WorkflowIdea.objects.for_team(self.team.pk).create(+ team=self.team, key="activation:signed_up", title="Welcome", rationale="New users stall.", definition={}+ )+ idea.hog_flow = HogFlow.objects.create(team=other_team, name="Elsewhere")+ with self.assertRaises(ValueError):+ idea.save()+ idea.hog_flow = HogFlow.objects.create(team=self.team, name="Here")+ idea.save()+ assert WorkflowIdea.objects.for_team(self.team.pk).get(id=idea.id).hog_flow_id == idea.hog_flow.id++ def test_a_workflow_in_a_child_environment_can_be_linked_and_saved_again(self):+ child = Team.objects.create(organization=self.organization, name="Staging", parent_team=self.team)+ idea = WorkflowIdea.objects.for_team(self.team.pk).create(+ team=child,+ key="activation:signed_up",+ title="Welcome",+ rationale="New users stall.",+ definition={},+ hog_flow=HogFlow.objects.create(team=child, name="Child flow"),+ )+ assert idea.team_id == self.team.id+ idea.status = WorkflowIdea.Status.ACCEPTED+ idea.save()