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.

feat(workflows): email reputation snapshot schema (#72543)

Meikel Ratz7002035e9582
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index 031b70fbc983..f28f9572e94a 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -123,6 +123,7 @@ rules:
|DeploymentProject
|EarlyAccessFeature
|EmailMessageMapping
+ |EmailReputationSnapshot
|EvaluationContext
|Edge
|Endpoint
@@ -435,6 +436,7 @@ rules:
|DeploymentProject
|EarlyAccessFeature
|EmailMessageMapping
+ |EmailReputationSnapshot
|EvaluationContext
|Edge
|Endpoint
diff --git a/products/workflows/backend/migrations/0010_email_reputation_snapshot.py b/products/workflows/backend/migrations/0010_email_reputation_snapshot.py
new file mode 100644
index 000000000000..a04c26700e49
--- /dev/null
+++ b/products/workflows/backend/migrations/0010_email_reputation_snapshot.py
@@ -0,0 +1,105 @@
+# Generated by Django 5.2.14 on 2026-07-10 08:00
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+import posthog.models.utils
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1247_oauthaccesstoken_token_idx"),
+ ("workflows", "0009_hogflow_action_redirects"),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="EmailReputationSnapshot",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.models.utils.uuid7,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ (
+ "scope",
+ models.CharField(
+ choices=[("workflow", "Workflow"), ("team", "Team")],
+ max_length=20,
+ ),
+ ),
+ (
+ "state",
+ models.CharField(
+ choices=[
+ ("insufficient_data", "Insufficient Data"),
+ ("healthy", "Healthy"),
+ ("warning", "Warning"),
+ ("critical", "Critical"),
+ ],
+ max_length=20,
+ ),
+ ),
+ ("bounce_rate", models.FloatField(default=0.0)),
+ ("complaint_rate", models.FloatField(default=0.0)),
+ ("emails_sent", models.BigIntegerField(default=0)),
+ ("evaluated_at", models.DateTimeField()),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ (
+ "hog_flow",
+ models.ForeignKey(
+ blank=True,
+ null=True,
+ on_delete=django.db.models.deletion.CASCADE,
+ to="workflows.hogflow",
+ ),
+ ),
+ (
+ "team",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ to="posthog.team",
+ ),
+ ),
+ ],
+ options={
+ "db_table": "posthog_emailreputationsnapshot",
+ "indexes": [
+ models.Index(
+ fields=["team", "evaluated_at"],
+ name="posthog_ema_team_id_a35f59_idx",
+ ),
+ models.Index(
+ condition=models.Q(("emails_sent__gt", 0), ("hog_flow__isnull", True)),
+ fields=["evaluated_at"],
+ name="eml_rep_snapshot_plan_idx",
+ ),
+ ],
+ "constraints": [
+ models.UniqueConstraint(
+ condition=models.Q(("hog_flow__isnull", False)),
+ fields=("team", "hog_flow", "evaluated_at"),
+ name="unique_workflow_reputation_snapshot",
+ ),
+ models.UniqueConstraint(
+ condition=models.Q(("hog_flow__isnull", True)),
+ fields=("team", "evaluated_at"),
+ name="unique_team_reputation_snapshot",
+ ),
+ models.CheckConstraint(
+ condition=models.Q(
+ models.Q(("hog_flow__isnull", True), ("scope", "team")),
+ models.Q(("hog_flow__isnull", False), ("scope", "workflow")),
+ _connector="OR",
+ ),
+ name="email_rep_snapshot_scope_matches_target",
+ ),
+ ],
+ },
+ ),
+ ]
diff --git a/products/workflows/backend/migrations/max_migration.txt b/products/workflows/backend/migrations/max_migration.txt
index acd817ac2b67..52bbaf43cfc1 100644
--- a/products/workflows/backend/migrations/max_migration.txt
+++ b/products/workflows/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0009_hogflow_action_redirects
+0010_email_reputation_snapshot
diff --git a/products/workflows/backend/models/__init__.py b/products/workflows/backend/models/__init__.py
index 09dbd5f9b503..4001c992589d 100644
--- a/products/workflows/backend/models/__init__.py
+++ b/products/workflows/backend/models/__init__.py
@@ -1,7 +1,15 @@
+from .email_reputation import EmailReputationSnapshot
from .hog_flow.hog_flow import HogFlow
from .hog_flow.hog_flow_template import HogFlowTemplate
from .hog_flow_batch_job import HogFlowBatchJob
from .hog_flow_schedule.hog_flow_schedule import HogFlowSchedule
from .team_workflows_config import TeamWorkflowsConfig
-__all__ = ["HogFlow", "HogFlowBatchJob", "HogFlowSchedule", "HogFlowTemplate", "TeamWorkflowsConfig"]
+__all__ = [
+ "EmailReputationSnapshot",
+ "HogFlow",
+ "HogFlowBatchJob",
+ "HogFlowSchedule",
+ "HogFlowTemplate",
+ "TeamWorkflowsConfig",
+]
diff --git a/products/workflows/backend/models/email_reputation.py b/products/workflows/backend/models/email_reputation.py
new file mode 100644
index 000000000000..dff4eaa6ebdc
--- /dev/null
+++ b/products/workflows/backend/models/email_reputation.py
@@ -0,0 +1,81 @@
+from django.db import models
+
+from posthog.models.scoping.root_mixin import TeamScopedRootMixin
+from posthog.models.utils import UUIDModel
+
+
+class EmailReputationSnapshot(TeamScopedRootMixin, UUIDModel):
+ """
+ Append-only daily snapshots of per-workflow and per-tenant email deliverability reputation
+ (bounce/complaint rates from app_metrics2), written by the Node Temporal evaluator via raw SQL.
+
+ Rates are volume-based, mirroring AWS SES: each snapshot covers the target's most recent sends
+ up to a configured representative volume, not a fixed time window — ``emails_sent`` is that
+ evaluated volume (0 for a carry-forward row written when a recently active team goes silent).
+
+ One row per workflow (``hog_flow`` set, ``scope=WORKFLOW``) plus one aggregate row per team
+ (``hog_flow`` null, ``scope=TEAM``) per evaluation run, so the table doubles as a time series
+ for trend dashboards. Calculation only — enforcement (pausing bad senders) ships separately.
+ """
+
+ class Meta:
+ db_table = "posthog_emailreputationsnapshot"
+ constraints = [
+ # One snapshot per target per run; the Node evaluator inserts ON CONFLICT DO NOTHING
+ # against these so Temporal activity retries are idempotent.
+ models.UniqueConstraint(
+ fields=["team", "hog_flow", "evaluated_at"],
+ condition=models.Q(hog_flow__isnull=False),
+ name="unique_workflow_reputation_snapshot",
+ ),
+ models.UniqueConstraint(
+ fields=["team", "evaluated_at"],
+ condition=models.Q(hog_flow__isnull=True),
+ name="unique_team_reputation_snapshot",
+ ),
+ # scope is denormalized from hog_flow nullability for readers; keep them coherent so
+ # a row can't classify as one target by scope and the other by hog_flow.
+ models.CheckConstraint(
+ check=models.Q(scope="team", hog_flow__isnull=True)
+ | models.Q(scope="workflow", hog_flow__isnull=False),
+ name="email_rep_snapshot_scope_matches_target",
+ ),
+ ]
+ indexes = [
+ models.Index(fields=["team", "evaluated_at"], name="posthog_ema_team_id_a35f59_idx"),
+ # Serves the evaluator's daily plan query (recent team rows with sends, no team filter).
+ models.Index(
+ fields=["evaluated_at"],
+ condition=models.Q(hog_flow__isnull=True, emails_sent__gt=0),
+ name="eml_rep_snapshot_plan_idx",
+ ),
+ ]
+
+ class Scope(models.TextChoices):
+ WORKFLOW = "workflow"
+ TEAM = "team"
+
+ class State(models.TextChoices):
+ INSUFFICIENT_DATA = "insufficient_data"
+ HEALTHY = "healthy"
+ WARNING = "warning"
+ CRITICAL = "critical"
+
+ # db_constraint=False: posthog_team is a hot table; a real FK constraint would lock it on deploy.
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False)
+ # Null hog_flow marks the team-level aggregate row.
+ hog_flow = models.ForeignKey("workflows.HogFlow", on_delete=models.CASCADE, null=True, blank=True)
+ scope = models.CharField(max_length=20, choices=Scope)
+ state = models.CharField(max_length=20, choices=State)
+
+ bounce_rate = models.FloatField(default=0.0)
+ complaint_rate = models.FloatField(default=0.0)
+ emails_sent = models.BigIntegerField(default=0)
+
+ # End of the evaluated rolling window; shared by all rows of one evaluator run.
+ evaluated_at = models.DateTimeField()
+ created_at = models.DateTimeField(auto_now_add=True)
+
+ def __str__(self) -> str:
+ target = f"hog_flow {self.hog_flow_id}" if self.hog_flow_id else "team"
+ return f"EmailReputationSnapshot({target}, {self.state}, {self.evaluated_at})"