: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
chore(workflows): move proposal approval and the workflow enums off the model (#113315)
Harley Alexander4f2f04d493df
diff --git a/products/workflows/backend/facade/contracts.py b/products/workflows/backend/facade/contracts.pyindex ec47e75ea326..0ae3917fde33 100644--- a/products/workflows/backend/facade/contracts.py+++ b/products/workflows/backend/facade/contracts.py@@ -523,6 +523,10 @@ class WorkflowEditState:created_by: "User | None"updated_at: datetimetrigger: Any+ trigger_masking: dict[str, Any] | None+ conversion: dict[str, Any] | None+ exit_condition: str+ email_sending_rate_limit: dict[str, Any] | Noneedges: list[dict[str, Any]] | dict[str, Any]actions: list[dict[str, Any]] | dict[str, Any]abort_action: str | None@@ -552,6 +556,31 @@ class WorkflowWriteResult:schedules_paused: int = 0+class WorkflowProposalNotFound(Exception):+ pass+++class WorkflowProposalResolved(Exception):+ """The suggestion was approved or rejected already."""+++class WorkflowProposalConflicts(Exception):+ """Someone changed the steps or fields the suggestion is about since it was written."""++ def __init__(self, conflicts: list[str]) -> None:+ super().__init__(conflicts)+ self.conflicts = conflicts+++@frozen+class ProposalApproval:+ """The approved suggestion, and the workflow's fields from before and after its draft was staged."""++ proposal: "WorkflowProposalRecord"+ previous: Mapping[str, object]+ current: Mapping[str, object]++@frozenclass ProposalChanges:"""What approving a suggestion would stage. `conflicts` names the steps or fields someone elsediff --git a/products/workflows/backend/facade/enums.py b/products/workflows/backend/facade/enums.pyindex a31a0b1f5b2f..5ee29fad2e0e 100644--- a/products/workflows/backend/facade/enums.py+++ b/products/workflows/backend/facade/enums.py@@ -54,3 +54,30 @@ class WorkflowProposalStatus(LabeledStrEnum):APPROVED = "approved", "Approved"REJECTED = "rejected", "Rejected"APPLIED = "applied", "Applied"+++class HogFlowState(LabeledStrEnum):+ DRAFT = "draft"+ ACTIVE = "active"+ ARCHIVED = "archived"+++class HogFlowOriginProduct(LabeledStrEnum):+ """The product surface that owns a workflow. Null for workflows built in the workflows UI or the API."""++ LOOPS = "loops", "Loops"+ BROADCASTS = "broadcasts", "Broadcasts"+++class HogFlowEmailSendingPausedBy(LabeledStrEnum):+ AUTO = "auto", "auto"+ STAFF = "staff", "staff"+++class HogFlowExitCondition(LabeledStrEnum):+ # The same choices as HogFlowTemplateExitCondition, kept as a separate class so each owner keeps its+ # own enum and the shared OpenAPI component name does not change.+ CONVERSION = "exit_on_conversion"+ TRIGGER_NOT_MATCHED = "exit_on_trigger_not_matched"+ TRIGGER_NOT_MATCHED_OR_CONVERSION = "exit_on_trigger_not_matched_or_conversion"+ ONLY_AT_END = "exit_only_at_end"diff --git a/products/workflows/backend/facade/proposals.py b/products/workflows/backend/facade/proposals.pyindex d2e1131a382a..3599f90fd22e 100644--- a/products/workflows/backend/facade/proposals.py+++ b/products/workflows/backend/facade/proposals.py@@ -1,6 +1,7 @@"""Suggested changes to a workflow, the per-workflow opt-in that allows them, and what each change did."""from products.workflows.backend.metrics import HOG_FLOW_VERSION_APP_SOURCE+from products.workflows.backend.services.proposal_approval import approve_proposalfrom products.workflows.backend.services.workflow_proposals import (PROPOSAL_MERGE_BY_ID_FIELDS,as_the_serializer_stores_it,@@ -23,6 +24,7 @@ from products.workflows.backend.services.workflow_proposals import ()__all__ = [+ "approve_proposal","HOG_FLOW_VERSION_APP_SOURCE","PROPOSAL_MERGE_BY_ID_FIELDS","as_the_serializer_stores_it",diff --git a/products/workflows/backend/facade/workflows.py b/products/workflows/backend/facade/workflows.pyindex c46d990e4673..890772c1cf7b 100644--- a/products/workflows/backend/facade/workflows.py+++ b/products/workflows/backend/facade/workflows.py@@ -1,6 +1,14 @@"""Reading workflows: one workflow behind the reader's object access check, or a filtered page of them,each with its secret inputs masked."""+from products.workflows.backend.models.hog_flow.hog_flow import (+ BILLABLE_ACTION_TYPES,+ PERSON_DEPENDENT_ACTION_TYPES,+ ROW_SCOPED_TRIGGER_TYPES,+ SUPPORTED_ACTION_TYPES,+ TRIGGER_TYPES,+ WORKFLOW_SAFE_INTERNAL_EVENTS,+)from products.workflows.backend.services.hog_flow_reads import (BROADCAST_STATUSES,WORKFLOW_FIELD_FILTER_PARAMS,@@ -15,6 +23,12 @@ from products.workflows.backend.services.hog_flow_reads import ()__all__ = [+ "BILLABLE_ACTION_TYPES",+ "PERSON_DEPENDENT_ACTION_TYPES",+ "ROW_SCOPED_TRIGGER_TYPES",+ "SUPPORTED_ACTION_TYPES",+ "TRIGGER_TYPES",+ "WORKFLOW_SAFE_INTERNAL_EVENTS","BROADCAST_STATUSES","WORKFLOW_FIELD_FILTER_PARAMS","WORKFLOW_TYPES",diff --git a/products/workflows/backend/models/hog_flow/hog_flow.py b/products/workflows/backend/models/hog_flow/hog_flow.pyindex 7a32cad5c2fe..8802b946338b 100644--- a/products/workflows/backend/models/hog_flow/hog_flow.py+++ b/products/workflows/backend/models/hog_flow/hog_flow.py@@ -13,6 +13,12 @@ from posthog.models.utils import UUIDTModelfrom posthog.plugins.plugin_server_api import reload_hog_flows_on_workersfrom products.actions.backend.models.action import Action+from products.workflows.backend.facade.enums import (+ HogFlowEmailSendingPausedBy,+ HogFlowExitCondition,+ HogFlowOriginProduct,+ HogFlowState,+)if TYPE_CHECKING:pass@@ -123,26 +129,15 @@ class HogFlow(UUIDTModel):models.UniqueConstraint(fields=["team", "version", "id"], name="unique_version_per_flow"),]- class State(models.TextChoices):- DRAFT = "draft"- ACTIVE = "active"- ARCHIVED = "archived"-- class ExitCondition(models.TextChoices):- CONVERSION = "exit_on_conversion"- TRIGGER_NOT_MATCHED = "exit_on_trigger_not_matched"- TRIGGER_NOT_MATCHED_OR_CONVERSION = "exit_on_trigger_not_matched_or_conversion"- ONLY_AT_END = "exit_only_at_end"-- class OriginProduct(models.TextChoices):- LOOPS = "loops", "Loops"- BROADCASTS = "broadcasts", "Broadcasts"+ State = HogFlowState+ ExitCondition = HogFlowExitCondition+ OriginProduct = HogFlowOriginProductname = models.CharField(max_length=400, null=True, blank=True)description = models.TextField(blank=True, default="")version = models.IntegerField(default=1)team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, related_name="+")- status = models.CharField(max_length=20, choices=State, default=State.DRAFT)+ status = models.CharField(max_length=20, choices=State.choices, default=State.DRAFT.value)# The product surface that owns this workflow, so that surface can list only its own flows.# Null for workflows built directly in the workflows UI or over the API.origin_product = models.CharField(@@ -156,7 +151,9 @@ class HogFlow(UUIDTModel):trigger = models.JSONField(default=dict)trigger_masking = models.JSONField(null=True, blank=True)conversion = models.JSONField(null=True, blank=True)- exit_condition = models.CharField(max_length=100, choices=ExitCondition, default=ExitCondition.CONVERSION)+ exit_condition = models.CharField(+ max_length=100, choices=ExitCondition.choices, default=ExitCondition.CONVERSION.value+ )# Optional email pacing for deliverability: {"count": <int>, "period": "minute" | "hour"}.# Enforced per workflow by the email worker, which spreads sends instead of dropping them.@@ -173,9 +170,7 @@ class HogFlow(UUIDTModel):# re-trips on the feedback that caused the pause in the first place.email_sending_resumed_at = models.DateTimeField(null=True, blank=True)- class EmailSendingPausedBy(models.TextChoices):- AUTO = "auto", "auto"- STAFF = "staff", "staff"+ EmailSendingPausedBy = HogFlowEmailSendingPausedBy# Who paused it: "auto" for the deliverability detector, "staff" for a PostHog admin. A staff# pause is not customer-resumable, so the resume endpoint refuses it. Empty when not paused.diff --git a/products/workflows/backend/presentation/views/hog_flow.py b/products/workflows/backend/presentation/views/hog_flow.pyindex 529657f160b5..da753f96e9f3 100644--- a/products/workflows/backend/presentation/views/hog_flow.py+++ b/products/workflows/backend/presentation/views/hog_flow.py@@ -16,7 +16,6 @@ from django.core.signing import BadSignature, SignatureExpired, TimestampSignerfrom django.db import models, transactionfrom django.db.models import QuerySetfrom django.http import Http404, HttpResponse-from django.utils import timezoneimport requestsimport structlog@@ -70,7 +69,6 @@ from posthog.api.hog_invocation_results import (from posthog.api.log_entries import LogEntryMixinfrom posthog.api.routing import TeamAndOrgViewSetMixinfrom posthog.api.shared import UserBasicSerializer-from posthog.api.utils import log_activity_from_viewsetfrom posthog.auth import InternalAPIAuthenticationfrom posthog.cdp.filters import DATA_WAREHOUSE_SOURCES, compile_filters_exprfrom posthog.cdp.flag_gated_templates import FLAG_GATED_TEMPLATE_IDS, gated_template_enabled@@ -147,7 +145,10 @@ from products.workflows.backend.facade.contracts import (WorkflowListFiltersInvalid,WorkflowListQuery,WorkflowNotFound,+ WorkflowProposalConflicts,+ WorkflowProposalNotFound,WorkflowProposalRecord,+ WorkflowProposalResolved,WorkflowRef,WorkflowRevisionNotFound,WorkflowRevisionSummary,@@ -171,11 +172,19 @@ from products.workflows.backend.facade.email_health import (team_email_sending_allowance,verified_email_domains,)-from products.workflows.backend.facade.enums import HogFlowBatchJobState, HogFlowScheduleStatus, WorkflowProposalStatus+from products.workflows.backend.facade.enums import (+ HogFlowBatchJobState,+ HogFlowExitCondition,+ HogFlowOriginProduct,+ HogFlowScheduleStatus,+ HogFlowState,+ WorkflowProposalStatus,+)from products.workflows.backend.facade.message_assets import fetch_message_asset_html, fetch_message_assetsfrom products.workflows.backend.facade.proposals import (HOG_FLOW_VERSION_APP_SOURCE,PROPOSAL_MERGE_BY_ID_FIELDS,+ approve_proposal,as_the_serializer_stores_it,count_proposals,create_proposal,@@ -189,9 +198,7 @@ from products.workflows.backend.facade.proposals import (proposal_outcome,resolve_proposal,set_optimization_enabled,- staged_proposal_changes,target_metric_in,- unstage_workflow_proposals,version_outcome,)from products.workflows.backend.facade.revisions import count_revisions, get_revision, list_revisions, restore_revision@@ -226,8 +233,14 @@ from products.workflows.backend.facade.validation import (is_signed_duration,)from products.workflows.backend.facade.workflows import (+ BILLABLE_ACTION_TYPES,BROADCAST_STATUSES,+ PERSON_DEPENDENT_ACTION_TYPES,+ ROW_SCOPED_TRIGGER_TYPES,+ SUPPORTED_ACTION_TYPES,+ TRIGGER_TYPES,WORKFLOW_FIELD_FILTER_PARAMS,+ WORKFLOW_SAFE_INTERNAL_EVENTS,WORKFLOW_TYPES,get_team_workflow_edit_state,get_workflow,@@ -248,15 +261,7 @@ from products.workflows.backend.facade.writes import (trigger_has_audience,update_workflow,)-from products.workflows.backend.models.hog_flow.hog_flow import (- BILLABLE_ACTION_TYPES,- PERSON_DEPENDENT_ACTION_TYPES,- ROW_SCOPED_TRIGGER_TYPES,- SUPPORTED_ACTION_TYPES,- TRIGGER_TYPES,- WORKFLOW_SAFE_INTERNAL_EVENTS,- HogFlow,-)+from products.workflows.backend.models.hog_flow.hog_flow import HogFlowfrom products.workflows.backend.presentation.views.graph_operations import apply_graph_operationsfrom products.workflows.backend.presentation.views.graph_validation import validate_graphfrom products.workflows.backend.presentation.views.hog_flow_batch_job import (@@ -376,7 +381,7 @@ def _reject_clock_based_wait(config: dict, team: Team) -> None:)-def existing_secret_map(instance: "HogFlow", template_cache: Optional[TemplateCache] = None) -> dict[str, dict]:+def existing_secret_map(instance: WorkflowEditState, template_cache: Optional[TemplateCache] = None) -> dict[str, dict]:# Every stored secret a masked re-save may need to recover, later sources winning: legacy# plaintext (live, then draft), then the encrypted live map, then the encrypted draft map.result = plaintext_secret_map(instance.actions, template_cache)@@ -385,7 +390,7 @@ def existing_secret_map(instance: "HogFlow", template_cache: Optional[TemplateCareturn merge_secret_maps(result, instance.draft_encrypted_inputs)-def snapshot_flow_content(flow: HogFlow) -> dict:+def snapshot_flow_content(flow: WorkflowEditState) -> dict:return snapshot_content({field: getattr(flow, field) for field in DRAFT_CONTENT_FIELDS})@@ -837,7 +842,7 @@ def _has_exact_string_filter(filters: dict, key: str) -> bool:return False-def _existing_email_from_by_action(instance: "HogFlow") -> dict[str, list[dict]]:+def _existing_email_from_by_action(instance: WorkflowEditState) -> dict[str, list[dict]]:"""Stored email sender overrides keyed by action id, live and draft variants both kept.Which variant a save should be compared against depends on how the request routes (a@@ -2482,15 +2487,15 @@ class HogFlowMinimalSerializer(UserAccessControlSerializerMixin, serializers.Sername = serializers.CharField(read_only=True, allow_null=True)description = serializers.CharField(read_only=True)version = serializers.IntegerField(read_only=True)- status = serializers.ChoiceField(choices=HogFlow.State.choices, read_only=True)- origin_product = serializers.ChoiceField(choices=HogFlow.OriginProduct.choices, read_only=True, allow_null=True)+ status = serializers.ChoiceField(choices=HogFlowState.choices, read_only=True)+ origin_product = serializers.ChoiceField(choices=HogFlowOriginProduct.choices, read_only=True, allow_null=True)created_at = serializers.DateTimeField(read_only=True)created_by = UserBasicSerializer(read_only=True)updated_at = serializers.DateTimeField(read_only=True)trigger = serializers.JSONField(read_only=True)trigger_masking: serializers.Field = serializers.JSONField(read_only=True, allow_null=True)conversion: serializers.Field = serializers.JSONField(read_only=True, allow_null=True)- exit_condition = serializers.ChoiceField(choices=HogFlow.ExitCondition.choices, read_only=True)+ exit_condition = serializers.ChoiceField(choices=HogFlowExitCondition.choices, read_only=True)email_sending_rate_limit: serializers.Field = serializers.JSONField(read_only=True, allow_null=True)edges: serializers.Field = serializers.JSONField(read_only=True)actions: serializers.Field = serializers.JSONField(read_only=True)@@ -2552,8 +2557,8 @@ class HogFlowMinimalSerializer(UserAccessControlSerializerMixin, serializers.Serreturn {name: fields[name] for name in self.Meta.fields}@extend_schema_field(HogFlowLastRunSerializer(allow_null=True))- def get_last_run(self, instance: HogFlow) -> dict | None:- if instance.origin_product != HogFlow.OriginProduct.LOOPS:+ def get_last_run(self, instance: Workflow) -> dict | None:+ if instance.origin_product != HogFlowOriginProduct.LOOPS:return None# The list view batches this lookup for the whole page; other responses carry one flow.last_runs: dict[uuid_mod.UUID, WorkflowLastRunDTO] | None = self.context.get("workflow_last_runs")@@ -2565,12 +2570,12 @@ class HogFlowMinimalSerializer(UserAccessControlSerializerMixin, serializers.Serreturn HogFlowLastRunSerializer(last_run).data if last_run else None@extend_schema_field(serializers.IntegerField(allow_null=True))- def get_pending_suggestions(self, hog_flow: HogFlow) -> int | None:+ def get_pending_suggestions(self, hog_flow: Workflow) -> int | None:# Annotated on the list queryset only; the detail serializer leaves it out.return getattr(hog_flow, "pending_suggestions", None)@extend_schema_field(serializers.BooleanField(allow_null=True))- def get_suggestions_enabled(self, hog_flow: HogFlow) -> bool | None:+ def get_suggestions_enabled(self, hog_flow: Workflow) -> bool | None:# A workflow with suggestions on but none waiting is still worth telling apart in the list.return getattr(hog_flow, "suggestions_enabled", None)@@ -2596,15 +2601,14 @@ class HogFlowMinimalSerializer(UserAccessControlSerializerMixin, serializers.Ser# presence marker in the live actions, the trigger and, when present, the staged draft's# actions. Values come from the encrypted columns (or legacy plaintext).# The template cache lives on the context so a list render dedupes lookups across flows.- is_row = isinstance(instance, HogFlow)mask_workflow_fields(data,- live_actions=instance.actions if is_row else None,- live_trigger=instance.trigger if is_row else None,- encrypted_inputs=instance.encrypted_inputs if is_row else None,- draft_encrypted_inputs=instance.draft_encrypted_inputs if is_row else None,+ live_actions=None,+ live_trigger=None,+ encrypted_inputs=None,+ draft_encrypted_inputs=None,template_cache=self.context.setdefault("_hogflow_template_cache", {}),- mask_trigger=is_row,+ mask_trigger=False,)return data@@ -2633,7 +2637,7 @@ class HogFlowSummarySerializer(HogFlowMinimalSerializer):class HogFlowSerializer(HogFlowMinimalSerializer):origin_product = serializers.ChoiceField(- choices=HogFlow.OriginProduct.choices,+ choices=HogFlowOriginProduct.choices,required=False,allow_null=True,help_text="Product surface that owns this workflow (e.g. `loops` for Desktop loops). Set only when "@@ -2644,7 +2648,7 @@ class HogFlowSerializer(HogFlowMinimalSerializer):)description = serializers.CharField(required=False, allow_blank=True, default="", help_text="Optional description.")status = serializers.ChoiceField(- choices=HogFlow.State.choices,+ choices=HogFlowState.choices,required=False,help_text="draft (no execution), active (live), archived (disabled).",)@@ -2673,7 +2677,7 @@ class HogFlowSerializer(HogFlowMinimalSerializer):),)exit_condition = serializers.ChoiceField(- choices=HogFlow.ExitCondition.choices,+ choices=HogFlowExitCondition.choices,required=False,help_text=("exit_only_at_end: only at exit node (default). "@@ -2767,7 +2771,7 @@ class HogFlowSerializer(HogFlowMinimalSerializer):)@extend_schema_field(serializers.BooleanField())- def get_email_sending_pause_requires_support(self, hog_flow: HogFlow) -> bool:+ def get_email_sending_pause_requires_support(self, hog_flow: Workflow) -> bool:return pause_requires_staff(paused_at=hog_flow.email_sending_paused_at,paused_by=hog_flow.email_sending_paused_by,@@ -2792,7 +2796,7 @@ class HogFlowSerializer(HogFlowMinimalSerializer):def to_internal_value(self, data):# When used as a nested field (the `configuration` override on test invocations) DRF never# binds `self.instance`, so fall back to the flow passed in via context so recovery still works.- instance = cast(Optional[HogFlow], self.instance) or self.context.get("instance")+ instance = cast(Optional[WorkflowEditState], self.instance) or self.context.get("instance")# Who a "Create AI task" step runs as: the existing creator for an update, or the# requesting user for a brand-new flow (matches the `created_by` a create() actually@@ -2833,7 +2837,7 @@ class HogFlowSerializer(HogFlowMinimalSerializer):# unflagged team activate the step.self.context["stored_gated_template_action_ids"] = {action["id"]- for action in ((instance.actions if instance and instance.status == HogFlow.State.ACTIVE else None) or [])+ for action in ((instance.actions if instance and instance.status == HogFlowState.ACTIVE else None) or [])if isinstance(action, dict)and action.get("id")and (action.get("config") or {}).get("template_id") in FLAG_GATED_TEMPLATE_IDS@@ -2846,7 +2850,7 @@ class HogFlowSerializer(HogFlowMinimalSerializer):self.context["is_draft"] = Trueelif (instance is not None- and instance.status == HogFlow.State.ACTIVE+ and instance.status == HogFlowState.ACTIVEand isinstance(data, dict)and bool(set(data.keys()) & set(DRAFT_CONTENT_FIELDS))and self._stages_draft()@@ -2940,7 +2944,7 @@ class HogFlowSerializer(HogFlowMinimalSerializer):]def validate(self, data):- instance = cast(Optional[HogFlow], self.instance)+ instance = cast(Optional[WorkflowEditState], self.instance)is_draft = self.context.get("is_draft")# Reject duplicate action ids on any client-submitted actions array (create/update/graph), on@@ -2963,7 +2967,9 @@ class HogFlowSerializer(HogFlowMinimalSerializer):if not action_serializer.is_valid():# many=True yields a list of per-action errors despite the ReturnDict annotationaction_errors = cast(list[Any], action_serializer.errors)- raise serializers.ValidationError({"actions": _describe_action_errors(action_errors, instance.actions)})+ raise serializers.ValidationError(+ {"actions": _describe_action_errors(action_errors, cast(list[dict], instance.actions))}+ )actions = action_serializer.validated_data# Re-validation recovers stripped secret inputs back into these actions (mutating the# instance's in place). Route them through validated_data so the write re-strips them into@@ -2985,7 +2991,7 @@ class HogFlowSerializer(HogFlowMinimalSerializer):# exit_only_at_end since the other exit conditions re-evaluate trigger/conversion filters that# may reference person properties.if data["trigger"].get("type") in ROW_SCOPED_TRIGGER_TYPES:- data["exit_condition"] = HogFlow.ExitCondition.ONLY_AT_END+ data["exit_condition"] = HogFlowExitCondition.ONLY_AT_ENDif not is_draft:offending_types = sorted({@@ -3090,14 +3096,14 @@ class HogFlowSerializer(HogFlowMinimalSerializer):class HogFlowUpdateSerializer(HogFlowSerializer):origin_product = serializers.ChoiceField(- choices=HogFlow.OriginProduct.choices,+ choices=HogFlowOriginProduct.choices,read_only=True,allow_null=True,help_text="Product surface that owns this workflow. This value cannot change after creation.",)def validate(self, data: dict) -> dict:- instance = cast(Optional[HogFlow], self.instance)+ instance = cast(Optional[WorkflowEditState], self.instance)submitted_origin_product = self.initial_data.get("origin_product", serializers.empty)if (instance is not None@@ -3123,7 +3129,7 @@ def set_workflow_enabled(*, team_id: int, user_id: int, workflow_id: uuid_mod.UUhog_flow = get_team_workflow_edit_state(team_id=team_id, workflow_id=workflow_id, user_access_control=user_access_control, required_level="editor")- target = HogFlow.State.ACTIVE if enabled else HogFlow.State.DRAFT+ target = HogFlowState.ACTIVE if enabled else HogFlowState.DRAFTif hog_flow.status != target:validated_data: dict[str, Any] = {"status": target}if enabled:@@ -4063,8 +4069,8 @@ class HogFlowVersionMetricsRequestSerializer(AppMetricsRequestSerializer):OpenApiParameter("status",OpenApiTypes.STR,- enum=sorted(HogFlow.State.values),- description="\n".join(f"* `{value}` - {label}" for value, label in HogFlow.State.choices),+ enum=sorted(HogFlowState.values),+ description="\n".join(f"* `{value}` - {label}" for value, label in HogFlowState.choices),),OpenApiParameter("optimization_enabled",@@ -4089,7 +4095,7 @@ class HogFlowVersionMetricsRequestSerializer(AppMetricsRequestSerializer):OpenApiParameter("origin_product",OpenApiTypes.STR,- enum=HogFlow.OriginProduct.values,+ enum=HogFlowOriginProduct.values,description="Filter to workflows owned by a product surface, e.g. `loops` for Desktop loops.",),OpenApiParameter(@@ -4323,9 +4329,9 @@ class HogFlowViewSet()origin_product = params.get("origin_product")- if origin_product and origin_product not in HogFlow.OriginProduct.values:+ if origin_product and origin_product not in HogFlowOriginProduct.values:raise exceptions.ValidationError(- {"origin_product": f"Must be one of: {', '.join(HogFlow.OriginProduct.values)}"}+ {"origin_product": f"Must be one of: {', '.join(HogFlowOriginProduct.values)}"})trigger = self._trigger_filter()@@ -4374,7 +4380,7 @@ class HogFlowViewSet(# The MCP summary serializer has no last_run, so only the full list row pays for the lookup.if self.get_serializer_class() is HogFlowMinimalSerializer:# One lookup for every loop on the page, so each row does not query tasks on its own.- loop_ids = [flow.id for flow in page.results if flow.origin_product == HogFlow.OriginProduct.LOOPS]+ loop_ids = [flow.id for flow in page.results if flow.origin_product == HogFlowOriginProduct.LOOPS]self._workflow_last_runs = list_workflow_last_runs(self.team_id, request.user.id, loop_ids)paginator.request = request@@ -4544,7 +4550,7 @@ class HogFlowViewSet()return Response(data)- def _emit_resource_edited(self, instance: "HogFlow | Workflow") -> None:+ def _emit_resource_edited(self, instance: Workflow) -> None:# Realtime "edited elsewhere" signal so an open builder (or another tab) can refresh instead of# clobbering edits made via a different channel (UI/MCP/API). Fires for every channel; the# frontend dedupes its own echo by comparing updated_at. Transient — no inbox notification.@@ -4565,7 +4571,7 @@ class HogFlowViewSet(def _report_workflow_action(self,event: str,- instance: "HogFlow | Workflow | WorkflowRef | _DeletedWorkflow",+ instance: "Workflow | WorkflowRef | WorkflowEditState | _DeletedWorkflow",extra_properties: Optional[dict] = None,) -> None:# report_user_action injects source and MCP-client properties from the request, so usage is@@ -4588,7 +4594,7 @@ class HogFlowViewSet(logger.warning("Failed to capture workflow usage event", event=event, error=str(e))def perform_create(self, serializer):- if self._is_mcp_request(self.request) and serializer.validated_data.get("status") == HogFlow.State.ACTIVE:+ if self._is_mcp_request(self.request) and serializer.validated_data.get("status") == HogFlowState.ACTIVE:raise exceptions.ValidationError("You can't one-shot active workflows via MCP. ""Create as draft, test with workflows-test-run, then enable with workflows-enable."@@ -4643,9 +4649,9 @@ class HogFlowViewSet(# Content edits on an active workflow stage a draft rather than landing live. Status-only# PATCHes (the lifecycle tools) and metadata-only edits apply straight to the live row.- if serializer.instance.status == HogFlow.State.ACTIVE and has_non_status:+ if serializer.instance.status == HogFlowState.ACTIVE and has_non_status:route_to_draft = bool(keys & set(DRAFT_CONTENT_FIELDS))- elif serializer.instance.status == HogFlow.State.ACTIVE and self.request.data.get("stage_draft"):+ elif serializer.instance.status == HogFlowState.ACTIVE and self.request.data.get("stage_draft"):# The web builder opts into the same draft routing per request ("stage_draft" rides the# raw body like "base_updated_at"). Callers that don't send it keep deploy-on-save, so# existing raw-API automation is unaffected.@@ -4678,7 +4684,7 @@ class HogFlowViewSet(self._emit_resource_edited(serializer.instance)# PostHog capture for hog_flow activated (draft -> active)- if result.previous.get("status") == HogFlow.State.DRAFT and serializer.instance.status == HogFlow.State.ACTIVE:+ if result.previous.get("status") == HogFlowState.DRAFT and serializer.instance.status == HogFlowState.ACTIVE:self._report_workflow_action("hog_flow_activated",serializer.instance,@@ -4832,7 +4838,7 @@ class HogFlowViewSet(# the apply conflicts.rendered: Optional[_RenderedActionEmailDesign] = Noneif operations:- predicts_draft = self._is_mcp_request(request) and unlocked.status == HogFlow.State.ACTIVE+ predicts_draft = self._is_mcp_request(request) and unlocked.status == HogFlowState.ACTIVEif predicts_draft and unlocked.draft:render_base = list(unlocked.draft.get("actions") or [])else:@@ -5146,7 +5152,7 @@ class HogFlowViewSet(approves it, and only reaches the live config once someone publishes that draft."""self._require_self_optimising_enabled()- instance = self.get_object()+ instance = self._edit_state()# DRF routes HEAD to the GET handler, and the scope gate reads it as a read.if request.method in ("GET", "HEAD"):@@ -5155,7 +5161,7 @@ class HogFlowViewSet(raise exceptions.ValidationError({"status": f"Must be one of: {', '.join(WorkflowProposalStatus.values)}."})- page = self.paginate_queryset(cast(Sequence[Any], _ProposalPages(instance.pk, requested_status or None)))+ page = self.paginate_queryset(cast(Sequence[Any], _ProposalPages(instance.id, requested_status or None)))return self.get_paginated_response(WorkflowProposalSerializer(page, many=True, context={"hog_flow": instance}).data)@@ -5167,7 +5173,7 @@ class HogFlowViewSet(source_id = params.get("source_id") or None# A retry names the suggestion it already made, which is still in someone's queue whatever the switch says now.- retry_of = get_proposal_by_source_id(hog_flow_id=instance.pk, source_id=source_id) if source_id else None+ retry_of = get_proposal_by_source_id(hog_flow_id=instance.id, source_id=source_id) if source_id else Noneif retry_of:return Response(WorkflowProposalSerializer(retry_of, context={"hog_flow": instance}).data, status=status.HTTP_200_OK@@ -5180,9 +5186,9 @@ class HogFlowViewSet()# Reading the queue stays open while the flag is on; the workflow's opt-in only gates producing a new one.- if instance.status != HogFlow.State.ACTIVE:+ if instance.status != HogFlowState.ACTIVE:raise WorkflowNotLiveError()- if not is_optimization_enabled(instance.pk):+ if not is_optimization_enabled(instance.id):raise WorkflowNotOptimisedError()live_content = snapshot_flow_content(instance)@@ -5239,7 +5245,7 @@ class HogFlowViewSet(evidence["measured"] = measuredcreated = create_proposal(- hog_flow_id=instance.pk,+ hog_flow_id=instance.id,title=params["title"],rationale=params["rationale"],content=content,@@ -5271,12 +5277,12 @@ class HogFlowViewSet(@action(detail=True, methods=["GET"], url_path=r"proposals/(?P<proposal_id>[^/.]+)", filter_backends=[])def proposal_detail(self, request: Request, proposal_id: Optional[str] = None, *args, **kwargs):self._require_self_optimising_enabled()- instance = self.get_object()+ instance = self._workflow_ref()proposal = self._get_proposal_or_404(instance, proposal_id)return Response(WorkflowProposalSerializer(proposal, context={"hog_flow": instance}).data)def _get_proposal_or_404(- self, hog_flow: HogFlow | WorkflowRef, proposal_id: Optional[str]+ self, hog_flow: WorkflowRef | WorkflowEditState, proposal_id: Optional[str]) -> WorkflowProposalRecord:parsed = _parse_uuid_or_none(proposal_id)if parsed is None:@@ -5307,57 +5313,47 @@ class HogFlowViewSet(param_serializer = WorkflowProposalApproveRequestSerializer(data=request.data)param_serializer.is_valid(raise_exception=True)- instance = self.get_object()- with transaction.atomic():- # nosemgrep: idor-lookup-without-team (re-fetch of already-authorized instance, locked for update)- locked = HogFlow.objects.select_for_update().get(pk=instance.pk)- proposal = self._get_proposal_or_404(locked, proposal_id)- # The row lock serializes concurrent approvals; the second sees what the first wrote.- locked_proposal = lock_proposal(team_id=self.team_id, proposal_id=proposal.id)- if locked_proposal.status != WorkflowProposalStatus.SUGGESTED:- raise ProposalAlreadyResolvedError()- if locked.draft and not param_serializer.validated_data["overwrite"]:- raise DraftExistsError()- staged = staged_proposal_changes(- team_id=locked.team_id, hog_flow_id=locked.pk, proposal_id=locked_proposal.id- )- if staged.conflicts:- raise ProposalOutOfDateError(staged.conflicts)- expected_draft_updated_at = param_serializer.validated_data.get("expected_draft_updated_at")- if (- locked.draft- and expected_draft_updated_at is not None- and locked.draft_updated_at != expected_draft_updated_at- ):- raise StaleWorkflowUpdateError()- # nosemgrep: idor-lookup-without-team (re-fetch of already-authorized instance for activity logging)- before_update = HogFlow.objects.get(pk=instance.pk)- # The draft is a full snapshot (live plus what the suggestion changes), so publish stays- # a plain copy. A field the suggestion merely echoed is not staged, since writing it back- # would undo a later edit the conflict check let through.- merged = merge_proposal_content(snapshot_flow_content(locked), staged.changes)+ workflow_ref = self._workflow_ref()+ parsed_proposal_id = _parse_uuid_or_none(proposal_id)+ if parsed_proposal_id is None:+ raise exceptions.NotFound("No such suggestion for this workflow.")++ def check_runnable(merged: dict) -> None:try:- # Create validated the merge against the graph as it was then; it can have moved since.validate_graph(merged.get("actions") or [], merged.get("edges") or [], merged.get("abort_action"))except serializers.ValidationError as error:raise ProposalNotRunnableError(_flatten_graph_errors(error))- locked.draft = merged- locked.draft_updated_at = timezone.now()- # Proposal content carries no secrets, so the draft re-attaches them from live on publish.- locked.draft_encrypted_inputs = None- locked.save(update_fields=["draft", "draft_updated_at", "draft_encrypted_inputs"])-- # The new draft replaces what was staged; an earlier approval stays only if the draft still carries it.- unstage_workflow_proposals(team_id=locked.team_id, hog_flow_id=locked.pk)- locked_proposal = resolve_proposal(+ try:+ approval = approve_proposal(team_id=self.team_id,- proposal_id=locked_proposal.id,- status=WorkflowProposalStatus.APPROVED,+ workflow_team_id=workflow_ref.team_id,+ hog_flow_id=workflow_ref.id,+ proposal_id=parsed_proposal_id,+ overwrite=param_serializer.validated_data["overwrite"],+ expected_draft_updated_at=param_serializer.validated_data.get("expected_draft_updated_at"),resolved_by_id=request.user.pk if request.user.is_authenticated else None,+ check_runnable=check_runnable,)+ except WorkflowProposalNotFound:+ raise exceptions.NotFound("No such suggestion for this workflow.")+ except WorkflowProposalResolved:+ raise ProposalAlreadyResolvedError()+ except WorkflowDraftExists:+ raise DraftExistsError()+ except WorkflowProposalConflicts as conflicts:+ raise ProposalOutOfDateError(conflicts.conflicts)+ except WorkflowDraftChanged:+ raise StaleWorkflowUpdateError()+ locked = workflow_from_fields(+ fields=approval.current,+ user_access_control=None if is_service_auth(request) else self.user_access_control,+ )+ locked_proposal = approval.proposal- log_activity_from_viewset(self, locked, activity="proposal_approved", name=locked.name, previous=before_update)+ self._log_activity(+ locked.id, locked.name, "proposal_approved", previous=approval.previous, current=approval.current+ )self._emit_resource_edited(locked)self._report_workflow_action("hog_flow_proposal_approved", locked, {"proposal_id": str(locked_proposal.id)})return Response(WorkflowProposalSerializer(locked_proposal, context={"hog_flow": locked}).data)@@ -5397,7 +5393,7 @@ class HogFlowViewSet()def _measure_evidence(- self, hog_flow: HogFlow, base_version: int, step_id: Optional[str], evidence: dict+ self, hog_flow: WorkflowEditState, base_version: int, step_id: Optional[str], evidence: dict) -> Optional[dict]:"""PostHog's own reading of the metrics a suggestion is about, taken when it is filed.@@ -5412,7 +5408,7 @@ class HogFlowViewSet(tag_queries(product=ProductKey.WORKFLOWS, feature=Feature.QUERY)reading = version_outcome(team_id=self.team_id,- hog_flow_id=hog_flow.pk,+ hog_flow_id=hog_flow.id,versions=[base_version],after=after_date,step_id=step_id,@@ -5475,7 +5471,7 @@ class HogFlowViewSet(param_serializer = HogFlowOptimizationSerializer(data=request.data)param_serializer.is_valid(raise_exception=True)enabled = param_serializer.validated_data["enabled"]- if enabled and instance.status != HogFlow.State.ACTIVE:+ if enabled and instance.status != HogFlowState.ACTIVE:raise WorkflowNotLiveError()changed = set_optimization_enabled(hog_flow_id=instance.id, enabled=enabled)if changed:@@ -6070,7 +6066,7 @@ class HogFlowViewSet(# A batch run sends real messages, so only fire for an enabled workflow. The scheduler applies the# same gate (see internal_process_due_schedules) and the UI disables the manual trigger for non-active# workflows; enforce it here too so API/MCP callers can't start a run the consumer would only drop.- if hog_flow.status != HogFlow.State.ACTIVE:+ if hog_flow.status != HogFlowState.ACTIVE:raise exceptions.ValidationError("Workflow must be active to run a batch. Enable it first.")# A batch run is an irreversible mass send: interactive agent surfaces must prove they@@ -6199,7 +6195,7 @@ class HogFlowViewSet(# A draft's trigger can still be edited after the audience was sized, so a schedule# staged on a draft could fire on a broadened audience once enabled. Same rule the# MCP tool enforces, applied at the API boundary.- if hog_flow.status != HogFlow.State.ACTIVE:+ if hog_flow.status != HogFlowState.ACTIVE:raise exceptions.ValidationError("Workflow must be active before scheduling - a draft's audience can still be ""edited after previewing. Enable it with workflows-enable, then preview and "@@ -6275,7 +6271,7 @@ class HogFlowViewSet("""hog_flow = self._workflow_ref()- if hog_flow.status != HogFlow.State.ACTIVE:+ if hog_flow.status != HogFlowState.ACTIVE:raise exceptions.ValidationError("Workflow must be active to run. Enable it first.")trigger_type = hog_flow.trigger_typediff --git a/products/workflows/backend/services/hog_flow_reads.py b/products/workflows/backend/services/hog_flow_reads.pyindex de1569809fcf..a5456b394222 100644--- a/products/workflows/backend/services/hog_flow_reads.py+++ b/products/workflows/backend/services/hog_flow_reads.py@@ -118,6 +118,10 @@ def to_edit_state(flow: HogFlow) -> WorkflowEditState:created_by=flow.created_by,updated_at=flow.updated_at,trigger=flow.trigger,+ trigger_masking=flow.trigger_masking,+ conversion=flow.conversion,+ exit_condition=flow.exit_condition,+ email_sending_rate_limit=flow.email_sending_rate_limit,edges=flow.edges,actions=flow.actions,abort_action=flow.abort_action,diff --git a/products/workflows/backend/services/proposal_approval.py b/products/workflows/backend/services/proposal_approval.pynew file mode 100644index 000000000000..0eb9d89327b0--- /dev/null+++ b/products/workflows/backend/services/proposal_approval.py@@ -0,0 +1,90 @@+"""Approving a suggested change: stage the workflow's draft from the suggestion under the row lock."""++from collections.abc import Callable+from datetime import datetime+from typing import Optional+from uuid import UUID++from django.db import transaction+from django.utils import timezone++from products.workflows.backend.facade.contracts import (+ ProposalApproval,+ WorkflowDraftChanged,+ WorkflowDraftExists,+ WorkflowProposalConflicts,+ WorkflowProposalNotFound,+ WorkflowProposalResolved,+)+from products.workflows.backend.facade.enums import WorkflowProposalStatus+from products.workflows.backend.models.hog_flow.hog_flow import HogFlow+from products.workflows.backend.services.hog_flow_content import snapshot_flow_content+from products.workflows.backend.services.hog_flow_writes import field_values+from products.workflows.backend.services.workflow_proposals import (+ get_proposal,+ lock_proposal,+ merge_proposal_content,+ resolve_proposal,+ staged_proposal_changes,+ unstage_workflow_proposals,+)+++def approve_proposal(+ *,+ team_id: int,+ workflow_team_id: int,+ hog_flow_id: UUID,+ proposal_id: UUID,+ overwrite: bool,+ expected_draft_updated_at: Optional[datetime],+ resolved_by_id: Optional[int],+ check_runnable: Callable[[dict], None],+) -> ProposalApproval:+ """Stage the suggestion into the workflow's draft and mark it approved. Approval only stages, like a+ revision restore: publish still previews and confirms. `team_id` scopes the suggestion and+ `workflow_team_id` the workflow row. `check_runnable` receives the merged content and raises when+ the graph could not run."""+ with transaction.atomic():+ # The row lock serializes concurrent approvals; the second sees what the first wrote.+ locked = HogFlow.objects.select_for_update().get(pk=hog_flow_id, team_id=workflow_team_id)+ proposal = get_proposal(team_id=team_id, hog_flow_id=locked.id, proposal_id=proposal_id)+ if proposal is None:+ raise WorkflowProposalNotFound()+ locked_proposal = lock_proposal(team_id=team_id, proposal_id=proposal.id)+ if locked_proposal.status != WorkflowProposalStatus.SUGGESTED:+ raise WorkflowProposalResolved()+ if locked.draft and not overwrite:+ raise WorkflowDraftExists()+ staged = staged_proposal_changes(team_id=locked.team_id, hog_flow_id=locked.pk, proposal_id=locked_proposal.id)+ if staged.conflicts:+ raise WorkflowProposalConflicts(staged.conflicts)+ if (+ locked.draft+ and expected_draft_updated_at is not None+ and locked.draft_updated_at != expected_draft_updated_at+ ):+ raise WorkflowDraftChanged()+ previous = field_values(locked)+ # The draft is a full snapshot (live plus what the suggestion changes), so publish stays+ # a plain copy. A field the suggestion merely echoed is not staged, since writing it back+ # would undo a later edit the conflict check let through.+ merged = merge_proposal_content(snapshot_flow_content(locked), staged.changes)+ # Create validated the merge against the graph as it was then; it can have moved since.+ check_runnable(merged)+ locked.draft = merged+ locked.draft_updated_at = timezone.now()+ # Proposal content carries no secrets, so the draft re-attaches them from live on publish.+ locked.draft_encrypted_inputs = None+ locked.save(update_fields=["draft", "draft_updated_at", "draft_encrypted_inputs"])++ # The new draft replaces what was staged; an earlier approval stays only if the draft still carries it.+ unstage_workflow_proposals(team_id=locked.team_id, hog_flow_id=locked.pk)++ locked_proposal = resolve_proposal(+ team_id=team_id,+ proposal_id=locked_proposal.id,+ status=WorkflowProposalStatus.APPROVED,+ resolved_by_id=resolved_by_id,+ )+ return ProposalApproval(proposal=locked_proposal, previous=previous, current=field_values(locked))diff --git a/products/workflows/backend/tests/api/test_hog_flow_revisions.py b/products/workflows/backend/tests/api/test_hog_flow_revisions.pyindex dc010d46602e..6f76a7ebf134 100644--- a/products/workflows/backend/tests/api/test_hog_flow_revisions.py+++ b/products/workflows/backend/tests/api/test_hog_flow_revisions.py@@ -10,7 +10,7 @@ from posthog.models.activity_logging.activity_log import ActivityLogfrom products.cdp.backend.api.test.test_hog_function_templates import MOCK_NODE_TEMPLATESfrom products.workflows.backend.models.hog_flow.hog_flow import HogFlow-from products.workflows.backend.presentation.views.hog_flow import DRAFT_CONTENT_FIELDS, snapshot_flow_content+from products.workflows.backend.services.hog_flow_content import DRAFT_CONTENT_FIELDS, snapshot_flow_contentwebhook_template = MOCK_NODE_TEMPLATES[0]