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.

fix(cohorts): seed internal users cohort with ends_with (#91201)

Phil Haackb720ba1717cc
diff --git a/posthog/migrations/1330_alter_team_test_account_filters.py b/posthog/migrations/1330_alter_team_test_account_filters.py
new file mode 100644
index 000000000000..c0caa731c6cf
--- /dev/null
+++ b/posthog/migrations/1330_alter_team_test_account_filters.py
@@ -0,0 +1,27 @@
+# Generated by Django 5.2.17 on 2026-08-30 22:24
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1329_drop_cimd_metadata_url"),
+ ]
+
+ # `help_text` is in Django's `Field.non_db_attrs`, so the schema editor skips it and this emits
+ # no DDL - `sqlmigrate` prints "(no-op)". Kept state-only so it can't take the ACCESS EXCLUSIVE
+ # lock on posthog_team that HotTableAlterPolicy guards against.
+ operations = [
+ migrations.SeparateDatabaseAndState(
+ state_operations=[
+ migrations.AlterField(
+ model_name="team",
+ name="test_account_filters",
+ field=models.JSONField(
+ default=list,
+ help_text='Filters used to identify internal/test users. Each entry is a property filter.\n\n Supported entry types and the exact shape each accepts:\n\n # Person property — match (or exclude) by a person property\n {"key": "email", "type": "person", "value": "@example.com", "operator": "ends_with"}\n\n # Event property — match by an event property\n {"key": "$host", "type": "event", "value": "localhost", "operator": "icontains"}\n\n # Cohort membership — match (or exclude) members of a cohort.\n # Use operator "in" for inclusion and "not_in" for exclusion. Do NOT use a\n # `negation` field here — `negation` is specific to cohort *definitions*\n # (the inner sub-filters that build a cohort) and is rejected by the\n # property-filter schema.\n {"key": "id", "type": "cohort", "value": 8814, "operator": "not_in"}\n\n Common operators: "exact", "is_not", "icontains", "not_icontains", "starts_with",\n "not_starts_with", "ends_with", "not_ends_with", "regex", "not_regex", "gt", "lt",\n "gte", "lte", "is_set", "is_not_set", "in", "not_in".',
+ ),
+ ),
+ ],
+ ),
+ ]
diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt
index 10673305c79d..a87a559fc415 100644
--- a/posthog/migrations/max_migration.txt
+++ b/posthog/migrations/max_migration.txt
@@ -1 +1 @@
-1329_drop_cimd_metadata_url
+1330_alter_team_test_account_filters
diff --git a/posthog/models/team/team.py b/posthog/models/team/team.py
index 0914fd7e0ad6..16290882f03f 100644
--- a/posthog/models/team/team.py
+++ b/posthog/models/team/team.py
@@ -567,7 +567,7 @@ class Team(UUIDTClassicModel):
Supported entry types and the exact shape each accepts:
# Person property — match (or exclude) by a person property
- {"key": "email", "type": "person", "value": "@example.com", "operator": "icontains"}
+ {"key": "email", "type": "person", "value": "@example.com", "operator": "ends_with"}
# Event property — match by an event property
{"key": "$host", "type": "event", "value": "localhost", "operator": "icontains"}
@@ -579,8 +579,9 @@ class Team(UUIDTClassicModel):
# property-filter schema.
{"key": "id", "type": "cohort", "value": 8814, "operator": "not_in"}
- Common operators: "exact", "is_not", "icontains", "not_icontains", "regex",
- "not_regex", "gt", "lt", "gte", "lte", "is_set", "is_not_set", "in", "not_in".""",
+ Common operators: "exact", "is_not", "icontains", "not_icontains", "starts_with",
+ "not_starts_with", "ends_with", "not_ends_with", "regex", "not_regex", "gt", "lt",
+ "gte", "lte", "is_set", "is_not_set", "in", "not_in".""",
),
"project",
"admin",
diff --git a/posthog/test/test_team.py b/posthog/test/test_team.py
index 7c6620fc8a28..9b12708b089a 100644
--- a/posthog/test/test_team.py
+++ b/posthog/test/test_team.py
@@ -75,15 +75,24 @@ class TestTeam(BaseTest):
self.assertEqual(team.autocapture_web_vitals_allowed_metrics, None)
self.assertEqual(team.autocapture_exceptions_errors_to_ignore, None)
- def test_create_team_with_test_account_filters(self):
- team = Team.objects.create_with_data(initiating_user=self.user, organization=self.organization)
+ @parameterized.expand(
+ [
+ ("plain_domain", "[email protected]", "@posthog.com"),
+ ("hyphenated_domain", "[email protected]", "@my-company.com"),
+ ]
+ )
+ def test_create_team_with_test_account_filters(self, _name, signup_email, expected_domain_value):
+ user = User.objects.create(email=signup_email)
+ organization = Organization.objects.create()
+ organization.members.set([user])
+ team = Team.objects.create_with_data(initiating_user=user, organization=organization)
# An internal/test users cohort should be created
test_users_cohort = Cohort.objects.get(team=team, name=INTERNAL_TEST_USERS_COHORT_NAME)
self.assertEqual(test_users_cohort.kind, CohortKind.INTERNAL_TEST_USERS)
- # Cohort should have $internal_or_test_user filter AND email domain filter (posthog.com is not generic)
+ # Cohort should have $internal_or_test_user filter AND email domain filter (neither domain is generic)
self.assertEqual(
test_users_cohort.filters,
{
@@ -107,8 +116,8 @@ class TestTeam(BaseTest):
{
"key": "email",
"type": "person",
- "value": "@posthog.com",
- "operator": "icontains",
+ "value": expected_domain_value,
+ "operator": "ends_with",
}
],
},
diff --git a/products/cohorts/backend/models/cohort.py b/products/cohorts/backend/models/cohort.py
index 9b6dec8fc359..b6fc7761aae0 100644
--- a/products/cohorts/backend/models/cohort.py
+++ b/products/cohorts/backend/models/cohort.py
@@ -1347,11 +1347,14 @@ def get_or_create_internal_test_users_cohort(
}
]
- # Add email domain filter if the creating user has a non-generic domain
+ # Add email domain filter if the creating user has a non-generic domain.
+ # The value is anchored to the end of the email: an unanchored "@acme.com" also matches
+ # acme.com.br and acme.community, silently dropping real users from every insight. Anchoring costs
+ # subdomain matches (eu.acme.com), which a team can widen itself.
if initiating_user_email:
generic_emails = GenericEmails()
if not generic_emails.is_generic(initiating_user_email):
- match = re.search(r"@([\w.]+)", initiating_user_email)
+ match = re.search(r"@([\w.-]+)", initiating_user_email)
if match:
domain = match.group(1).lower()
filter_groups.append(
@@ -1362,7 +1365,7 @@ def get_or_create_internal_test_users_cohort(
"key": "email",
"type": "person",
"value": f"@{domain}",
- "operator": "icontains",
+ "operator": "ends_with",
}
],
}
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index f81a8d213011..61a494110d9a 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -66964,7 +66964,7 @@ export namespace Schemas {
* Supported entry types and the exact shape each accepts:
*
* # Person property — match (or exclude) by a person property
- * {"key": "email", "type": "person", "value": "@example.com", "operator": "icontains"}
+ * {"key": "email", "type": "person", "value": "@example.com", "operator": "ends_with"}
*
* # Event property — match by an event property
* {"key": "$host", "type": "event", "value": "localhost", "operator": "icontains"}
@@ -66976,8 +66976,9 @@ export namespace Schemas {
* # property-filter schema.
* {"key": "id", "type": "cohort", "value": 8814, "operator": "not_in"}
*
- * Common operators: "exact", "is_not", "icontains", "not_icontains", "regex",
- * "not_regex", "gt", "lt", "gte", "lte", "is_set", "is_not_set", "in", "not_in". */
+ * Common operators: "exact", "is_not", "icontains", "not_icontains", "starts_with",
+ * "not_starts_with", "ends_with", "not_ends_with", "regex", "not_regex", "gt", "lt",
+ * "gte", "lte", "is_set", "is_not_set", "in", "not_in". */
test_account_filters?: unknown;
/** @nullable */
test_account_filters_default_checked?: boolean | null;
@@ -84791,7 +84792,7 @@ export namespace Schemas {
* Supported entry types and the exact shape each accepts:
*
* # Person property — match (or exclude) by a person property
- * {"key": "email", "type": "person", "value": "@example.com", "operator": "icontains"}
+ * {"key": "email", "type": "person", "value": "@example.com", "operator": "ends_with"}
*
* # Event property — match by an event property
* {"key": "$host", "type": "event", "value": "localhost", "operator": "icontains"}
@@ -84803,8 +84804,9 @@ export namespace Schemas {
* # property-filter schema.
* {"key": "id", "type": "cohort", "value": 8814, "operator": "not_in"}
*
- * Common operators: "exact", "is_not", "icontains", "not_icontains", "regex",
- * "not_regex", "gt", "lt", "gte", "lte", "is_set", "is_not_set", "in", "not_in". */
+ * Common operators: "exact", "is_not", "icontains", "not_icontains", "starts_with",
+ * "not_starts_with", "ends_with", "not_ends_with", "regex", "not_regex", "gt", "lt",
+ * "gte", "lte", "is_set", "is_not_set", "in", "not_in". */
test_account_filters?: unknown;
/** @nullable */
test_account_filters_default_checked?: boolean | null;