: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
import { MakeLogicType, actions, connect, kea, key, path, props, reducers, selectors } from 'kea'
import { isPropertyFilterWithOperator } from 'lib/components/PropertyFilters/utils'
import { FEATURE_FLAGS, INSTANTLY_AVAILABLE_PROPERTIES } from 'lib/constants'
import { featureFlagLogic as enabledFeaturesLogic } from 'lib/logic/featureFlagLogic'
import { BehavioralFilterKey } from 'scenes/cohorts/CohortFilters/types'
import { cohortsModel } from '~/models/cohortsModel'
import {
CohortCriteriaGroupFilter,
CohortType,
FeatureFlagGroupType,
FeatureFlagType,
PropertyFilterType,
} from '~/types'
import type { FeatureFlagsSet } from '../../lib/logic/featureFlagLogic'
import { featureFlagLogic, FeatureFlagLogicProps } from './featureFlagLogic'
export type FlagIntent = 'local-eval' | 'first-page-load'
function hasBehavioralCriteria(cohort: CohortType): boolean {
const values = cohort.filters?.properties?.values
if (!values) {
return false
}
return values.some((value) => {
if ('values' in value) {
const nested = value as CohortCriteriaGroupFilter
return nested.values.some((v) => 'type' in v && v.type === BehavioralFilterKey.Behavioral)
}
return 'type' in value && value.type === BehavioralFilterKey.Behavioral
})
}
function isGroupBroad(group: FeatureFlagGroupType): boolean {
const hasNoProperties = !group.properties || group.properties.length === 0
const isFullRollout =
group.rollout_percentage === null || group.rollout_percentage === undefined || group.rollout_percentage === 100
return hasNoProperties && isFullRollout
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featureFlagIntentWarningLogicValues {
cohortsById: Partial<Record<number | string, CohortType>> // cohortsModel
enabledFeatures: FeatureFlagsSet // enabledFeaturesLogic
featureFlag: FeatureFlagType // featureFlagLogic
flagIntent: FlagIntent | null // featureFlagLogic
intentIssues: string[]
issuesExpanded: boolean
unreachableGroups: Set<number>
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featureFlagIntentWarningLogicActions {
toggleIssuesExpanded: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featureFlagIntentWarningLogicMeta {
key: number | 'link' | 'new'
__keaTypeGenInternalSelectorTypes: {
unreachableGroups: (featureFlag: FeatureFlagType) => Set<number>
intentIssues: (
featureFlag: FeatureFlagType,
flagIntent: FlagIntent | null,
cohortsById: Partial<Record<number | string, CohortType>>,
enabledFeatures: FeatureFlagsSet
) => string[]
}
}
export type featureFlagIntentWarningLogicType = MakeLogicType<
featureFlagIntentWarningLogicValues,
featureFlagIntentWarningLogicActions,
FeatureFlagLogicProps,
featureFlagIntentWarningLogicMeta
>
export const featureFlagIntentWarningLogic = kea<featureFlagIntentWarningLogicType>([
path(['scenes', 'feature-flags', 'featureFlagIntentWarningLogic']),
props({} as FeatureFlagLogicProps),
key(({ id }) => id ?? 'unknown'),
connect((props: FeatureFlagLogicProps) => ({
values: [
featureFlagLogic(props),
['featureFlag', 'flagIntent'],
cohortsModel,
['cohortsById'],
enabledFeaturesLogic,
['featureFlags as enabledFeatures'],
],
})),
actions({
toggleIssuesExpanded: true,
}),
reducers({
issuesExpanded: [
false,
{
toggleIssuesExpanded: (expanded) => !expanded,
},
],
}),
selectors({
unreachableGroups: [
(s) => [s.featureFlag],
(featureFlag: FeatureFlagType): Set<number> => {
const groups = featureFlag?.filters?.groups
if (!groups || groups.length <= 1) {
return new Set()
}
// A broad condition only shadows later conditions targeting the same entity type.
// Mixed-targeting flags can have condition sets aimed at users (null) or different
// group types, which are evaluated independently when their context is provided.
const flagAggregation = featureFlag?.filters?.aggregation_group_type_index ?? null
const unreachable = new Set<number>()
const broadAggregations = new Set<number | null>()
for (let i = 0; i < groups.length; i++) {
const group = groups[i]
// Distinguish "not provided" (undefined → inherit flag-level) from "explicitly user-targeted" (null).
const aggregation =
group.aggregation_group_type_index !== undefined
? group.aggregation_group_type_index
: flagAggregation
if (broadAggregations.has(aggregation)) {
unreachable.add(i)
} else if (isGroupBroad(group)) {
broadAggregations.add(aggregation)
}
}
return unreachable
},
],
intentIssues: [
(s) => [s.featureFlag, s.flagIntent, s.cohortsById, s.enabledFeatures],
(
featureFlag: FeatureFlagType,
flagIntent: FlagIntent | null,
cohortsById: Partial<Record<string | number, CohortType>>,
enabledFeatures: Record<string, boolean | string>
): string[] => {
const intentsEnabled = !!enabledFeatures[FEATURE_FLAGS.FEATURE_FLAG_CREATION_INTENTS]
if (!intentsEnabled || !flagIntent) {
return []
}
const groups = featureFlag?.filters?.groups
if (!groups) {
return []
}
const issues = new Set<string>()
if (flagIntent === 'local-eval') {
if (featureFlag?.ensure_experience_continuity) {
issues.add(
'Persist across authentication requires server-side state that local evaluation cannot access'
)
}
for (const group of groups) {
for (const property of group.properties || []) {
if (property.type === PropertyFilterType.Cohort) {
// cohortId may be numeric or stringified depending on filter source
const cohortId = property.value
const cohort = cohortsById[cohortId] ?? cohortsById[String(cohortId)]
if (cohort?.is_static) {
issues.add('Static cohorts are not sent to SDKs')
} else if (cohort && hasBehavioralCriteria(cohort)) {
issues.add('Behavioral cohorts cannot be evaluated locally')
}
}
if (isPropertyFilterWithOperator(property) && property.operator === 'is_not_set') {
issues.add(
'"is not set" can\'t be evaluated locally because SDKs only receive properties you send, not the full list'
)
}
}
}
}
if (flagIntent === 'first-page-load') {
const allSlowKeys = new Set<string>()
let hasCohort = false
for (const group of groups) {
for (const property of group.properties || []) {
if (property.type === PropertyFilterType.Cohort) {
hasCohort = true
} else if (property.key && !INSTANTLY_AVAILABLE_PROPERTIES.includes(property.key)) {
allSlowKeys.add(property.key)
}
}
}
if (allSlowKeys.size === 1) {
const key = [...allSlowKeys][0]
issues.add(`The property "${key}" won't be available on first page load and can cause flicker`)
} else if (allSlowKeys.size > 1) {
issues.add(
`${allSlowKeys.size} properties won't be available on first page load and can cause flicker`
)
}
if (hasCohort) {
issues.add('Cohort membership is not available on first page load and can cause flicker')
}
}
return [...issues]
},
],
}),
])