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): validate negation against sibling groups under outer AND (#72382)

posthog[bot]e1963c8c2c55
diff --git a/frontend/src/scenes/cohorts/cohortEditLogic.ts b/frontend/src/scenes/cohorts/cohortEditLogic.ts
index 0ce1d357f7dd..9c9e250e5480 100644
--- a/frontend/src/scenes/cohorts/cohortEditLogic.ts
+++ b/frontend/src/scenes/cohorts/cohortEditLogic.ts
@@ -770,7 +770,13 @@ export const cohortEditLogic = kea<cohortEditLogicType>([
values:
is_static && values.staticCohortMode !== 'criteria'
? undefined
- : filters.properties.values.map(validateGroup),
+ : filters.properties.values.map((group, index, groups) =>
+ validateGroup(
+ group,
+ filters.properties.type,
+ groups.filter((_, i) => i !== index)
+ )
+ ),
},
},
}),
diff --git a/frontend/src/scenes/cohorts/cohortUtils.test.tsx b/frontend/src/scenes/cohorts/cohortUtils.test.tsx
index 529c4f8f61eb..4a2691b8294a 100644
--- a/frontend/src/scenes/cohorts/cohortUtils.test.tsx
+++ b/frontend/src/scenes/cohorts/cohortUtils.test.tsx
@@ -1,5 +1,5 @@
import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
-import { BehavioralFilterKey } from 'scenes/cohorts/CohortFilters/types'
+import { BehavioralFilterKey, CohortClientErrors } from 'scenes/cohorts/CohortFilters/types'
import { cleanBehavioralTypeCriteria, determineFilterType, validateGroup } from 'scenes/cohorts/cohortUtils'
import { AnyCohortCriteriaType, BehavioralEventType, CohortCriteriaGroupFilter, FilterLogicalOperator } from '~/types'
@@ -87,6 +87,63 @@ describe('validateGroup', () => {
// Exercises the `.join(', ')` and the `are` (vs `is a`) pluralization in the changed expression.
expect(errors.id).toContain("'legacy_unknown_type', 'Did not complete event' are negative cohort criteria")
})
+
+ const negationGroup: CohortCriteriaGroupFilter = {
+ type: FilterLogicalOperator.And,
+ values: [
+ {
+ type: BehavioralFilterKey.Behavioral,
+ value: BehavioralEventType.PerformEvent,
+ negation: true,
+ },
+ ],
+ }
+ const positiveSiblingGroup: CohortCriteriaGroupFilter = {
+ type: FilterLogicalOperator.And,
+ values: [
+ {
+ type: BehavioralFilterKey.Behavioral,
+ value: BehavioralEventType.PerformEvent,
+ negation: false,
+ },
+ ],
+ }
+
+ it('allows a negation alone in its group when a sibling group has a positive criterion and outer op is AND', () => {
+ // The reported bug: a "Did not complete event" negation in its own group, matched against a
+ // positive criterion in a sibling group under the top-level "Match all criteria" (AND) operator.
+ const errors = validateGroup(negationGroup, FilterLogicalOperator.And, [positiveSiblingGroup])
+
+ expect(errors.id).toBeUndefined()
+ })
+
+ it('still rejects a negation alone in its group when the outer operator is OR', () => {
+ // Under OR the sibling group is unioned, not intersected, so it cannot bound the negation.
+ const errors = validateGroup(negationGroup, FilterLogicalOperator.Or, [positiveSiblingGroup])
+
+ expect(errors.id).toContain(CohortClientErrors.NegationCriteriaMissingOther)
+ })
+
+ it('still rejects a negation alone in its group when no sibling group has a positive criterion', () => {
+ const errors = validateGroup(negationGroup, FilterLogicalOperator.And, [negationGroup])
+
+ expect(errors.id).toContain(CohortClientErrors.NegationCriteriaMissingOther)
+ })
+
+ it('still rejects a negation inside an inner OR group even when the outer operator is AND', () => {
+ // A negation only makes sense intersected with a positive set. An OR group unions the negation
+ // with its own members, so a positive sibling under outer AND must not rescue it.
+ const orGroupWithNegation: CohortCriteriaGroupFilter = {
+ type: FilterLogicalOperator.Or,
+ values: [
+ { type: BehavioralFilterKey.Behavioral, value: BehavioralEventType.PerformEvent, negation: true },
+ { type: BehavioralFilterKey.Behavioral, value: BehavioralEventType.PerformEvent, negation: false },
+ ],
+ }
+ const errors = validateGroup(orGroupWithNegation, FilterLogicalOperator.And, [positiveSiblingGroup])
+
+ expect(errors.id).toContain(CohortClientErrors.NegationCriteriaMissingOther)
+ })
})
describe('determineFilterType', () => {
diff --git a/frontend/src/scenes/cohorts/cohortUtils.tsx b/frontend/src/scenes/cohorts/cohortUtils.tsx
index 9edaf77a8faf..d7ffce019de0 100644
--- a/frontend/src/scenes/cohorts/cohortUtils.tsx
+++ b/frontend/src/scenes/cohorts/cohortUtils.tsx
@@ -182,8 +182,22 @@ export function createCohortFormData(
return cohortFormData
}
+/** Whether a group (or bare criterion) contributes at least one positive (non-negated) matching criterion. */
+function hasPositiveCriterion(group: CohortCriteriaGroupFilter | AnyCohortCriteriaType): boolean {
+ if (!isCohortCriteriaGroup(group)) {
+ return !group.negation
+ }
+ return (group.values as AnyCohortCriteriaType[]).filter((g) => !isCohortCriteriaGroup(g)).some((c) => !c.negation)
+}
+
export function validateGroup(
- group: CohortCriteriaGroupFilter | AnyCohortCriteriaType
+ group: CohortCriteriaGroupFilter | AnyCohortCriteriaType,
+ // The outer ("Match all/any criteria") operator and the other groups in the cohort. When the
+ // outer operator is AND, every group is intersected, so a negation in one group is bounded by a
+ // positive matching criterion in any sibling group — the whole cohort has to be considered, not
+ // just this group in isolation.
+ outerOperator?: FilterLogicalOperator,
+ siblingGroups: (CohortCriteriaGroupFilter | AnyCohortCriteriaType)[] = []
): DeepPartialMap<CohortCriteriaGroupFilter, ValidationErrorType> {
if (!isCohortCriteriaGroup(group)) {
return {}
@@ -196,12 +210,15 @@ export function validateGroup(
const negatedCriteria = criteria.filter((c) => !!c.negation)
const negatedCriteriaIndices = new Set(negatedCriteria.map((c) => c.index))
- if (
- // Negation criteria can only be used when matching ALL criteria
- (group.type !== FilterLogicalOperator.And && negatedCriteria.length > 0) ||
- // Negation criteria has at least one positive matching criteria
- (group.type === FilterLogicalOperator.And && negatedCriteria.length === criteria.length)
- ) {
+ // A negation is bounded by a positive matching criterion either within this AND group, or — when
+ // the cohort matches ALL criteria (outer AND) — within any sibling group.
+ const boundedWithinGroup = group.type === FilterLogicalOperator.And && negatedCriteria.length < criteria.length
+ const boundedBySibling =
+ group.type === FilterLogicalOperator.And &&
+ outerOperator === FilterLogicalOperator.And &&
+ siblingGroups.some((sibling) => hasPositiveCriterion(sibling))
+
+ if (negatedCriteria.length > 0 && !boundedWithinGroup && !boundedBySibling) {
const errorMsg = `${negatedCriteria
.map((c) => {
const behavioralFilterType = criteriaToBehavioralFilterType(c)