: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
feat(flags): warn when a fractional rollout breaks local evaluation (#85597)
Patricio9e21435e7925
diff --git a/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditions.tsx b/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditions.tsxindex b7478d7eeab5..5496b55ef6de 100644--- a/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditions.tsx+++ b/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditions.tsx@@ -43,6 +43,8 @@ import {PropertyOperator,} from '~/types'+import { FractionalRolloutWarning } from 'products/feature_flags/frontend/FractionalRolloutWarning'+import { resolveAggregationGroupTypeIndex } from './aggregation'import { EARLY_ACCESS_GROUP_TARGETING_DISABLED_REASON, MATCHING_ESTIMATE_TOOLTIP } from './constants'import { featureFlagLogic } from './featureFlagLogic'@@ -583,6 +585,7 @@ export function FeatureFlagReleaseConditions({it.</LemonBanner>)}+ <FractionalRolloutWarning filterGroups={filterGroups} className="mb-3" />{!readOnly &&!filterGroups.every((group) => filterGroups.filter((g) => g.variant === group.variant && g.variant !== null).length < 2diff --git a/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsCollapsible.tsx b/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsCollapsible.tsxindex 53b4b84832b3..a86f72ee9b85 100644--- a/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsCollapsible.tsx+++ b/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsCollapsible.tsx@@ -71,6 +71,7 @@ import {} from '~/types'import { INTENT_METADATA } from 'products/feature_flags/frontend/featureFlagTemplateConstants'+import { FractionalRolloutWarning } from 'products/feature_flags/frontend/FractionalRolloutWarning'import { resolveAggregationGroupTypeIndex } from './aggregation'import { EARLY_ACCESS_GROUP_TARGETING_DISABLED_REASON, MATCHING_ESTIMATE_TOOLTIP } from './constants'@@ -1120,6 +1121,8 @@ export function FeatureFlagReleaseConditionsCollapsible({<FeatureFlagConditionWarning properties={properties} evaluationRuntime={evaluationRuntime} />+ <FractionalRolloutWarning filterGroups={filterGroups} />+{flagId && <IntentWarningsBanner flagId={flagId} />}{!hideMatchOptions && matchByOptions.length > 1 && (diff --git a/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsReadonly.tsx b/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsReadonly.tsxindex a06c248f8eb8..4e7e32e35170 100644--- a/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsReadonly.tsx+++ b/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsReadonly.tsx@@ -18,6 +18,8 @@ import {PropertyFilterType,} from '~/types'+import { FractionalRolloutWarning } from 'products/feature_flags/frontend/FractionalRolloutWarning'+import { EarlyExitIndicator } from './EarlyExitIndicator'import { FeatureFlagConditionWarning } from './FeatureFlagConditionWarning'import {@@ -150,6 +152,8 @@ export function FeatureFlagReleaseConditionsReadonly({<FeatureFlagConditionWarning properties={properties} evaluationRuntime={evaluationRuntime} />+ <FractionalRolloutWarning filterGroups={filterGroups} />+<div className={isDisabled ? 'opacity-60' : ''}>{filterGroups.map((group, index) => (<div key={group.sort_key ?? index}>diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 3e0b62b55f8b..5ea2e48671e7 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -3272,6 +3272,9 @@ importers:'@posthog/icons':specifier: 'catalog:'+ '@testing-library/jest-dom':+ specifier: '*'+ version: 5.17.0'@types/react':specifier: 18.3.27version: 18.3.27@@ -3300,6 +3303,9 @@ importers:'@storybook/react':specifier: 'catalog:'version: 10.4.6(@types/[email protected](@types/[email protected]))(@types/[email protected])([email protected]([email protected]))([email protected])([email protected](@testing-library/[email protected])(@types/[email protected])([email protected])([email protected]([email protected]))([email protected]))([email protected])+ '@testing-library/react':+ specifier: ^14.3.1+ version: 14.3.1(@types/[email protected])([email protected]([email protected]))([email protected])kea-test-utils:specifier: 'catalog:'version: 0.2.4([email protected](patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected]))diff --git a/products/feature_flags/frontend/FractionalRolloutWarning.test.tsx b/products/feature_flags/frontend/FractionalRolloutWarning.test.tsxnew file mode 100644index 000000000000..7db6d03d8aab--- /dev/null+++ b/products/feature_flags/frontend/FractionalRolloutWarning.test.tsx@@ -0,0 +1,89 @@+import '@testing-library/jest-dom'++import { cleanup, render } from '@testing-library/react'+import { Provider } from 'kea'++import { initKeaTests } from '~/test/init'+import { FeatureFlagGroupType } from '~/types'++import { FractionalRolloutWarning, fractionalRolloutPercentages } from './FractionalRolloutWarning'++function group(rollout_percentage: number | null, variant: string | null = null): FeatureFlagGroupType {+ return { properties: [], rollout_percentage, variant, sort_key: `group-${rollout_percentage}-${variant}` }+}++// A condition group can carry a variant override. The group's own rollout is what breaks the parse,+// so the override must not change whether the warning fires.+const variantOverrideCases: [number, boolean][] = [+ [0.5, true],+ [50, false],+]++// Flags saved through the API carry more precision than the editor's two decimal places allows.+const precisionCases: [number, string][] = [+ [33.333333333333336, '(33.33%)'],+ [0.00015, '(0.00015%)'],+]++function renderWarning(filterGroups: FeatureFlagGroupType[]): HTMLElement {+ const { container } = render(+ <Provider>+ <FractionalRolloutWarning filterGroups={filterGroups} />+ </Provider>+ )+ return container+}++describe('FractionalRolloutWarning', () => {+ beforeEach(() => {+ initKeaTests()+ })++ afterEach(() => {+ cleanup()+ })++ describe('fractionalRolloutPercentages', () => {+ it('picks out only percentages that are not whole numbers', () => {+ expect(fractionalRolloutPercentages([group(100), group(0.5), group(0), group(33.33)])).toEqual([0.5, 33.33])+ })++ it('ignores null and missing rollout percentages', () => {+ expect(fractionalRolloutPercentages([group(null), {}])).toEqual([])+ expect(fractionalRolloutPercentages(undefined)).toEqual([])+ })+ })++ describe('rendering', () => {+ it('warns and names the offending percentage', () => {+ renderWarning([group(100), group(0.5)])++ expect(document.body).toHaveTextContent('This flag has a fractional rollout percentage (0.5%)')+ expect(document.body).toHaveTextContent('every flag in the project')+ })++ it('pluralizes and lists every percentage when several condition sets are fractional', () => {+ renderWarning([group(0.5), group(33.33)])++ expect(document.body).toHaveTextContent('This flag has fractional rollout percentages (0.5%, 33.33%)')+ })++ it('stays silent when every rollout percentage is a whole number', () => {+ expect(renderWarning([group(100), group(0), group(50)])).toBeEmptyDOMElement()+ })++ it.each(variantOverrideCases)('with rollout %s on a group targeting a variant, warns: %s', (rollout, warns) => {+ const container = renderWarning([group(rollout, 'test')])++ if (warns) {+ expect(container).toHaveTextContent('fractional rollout percentage')+ } else {+ expect(container).toBeEmptyDOMElement()+ }+ })++ it.each(precisionCases)('trims %s for display without rounding it to zero', (rollout, expected) => {+ expect(renderWarning([group(rollout)])).toHaveTextContent(expected)+ })+ })+})diff --git a/products/feature_flags/frontend/FractionalRolloutWarning.tsx b/products/feature_flags/frontend/FractionalRolloutWarning.tsxnew file mode 100644index 000000000000..8f2855aefa62--- /dev/null+++ b/products/feature_flags/frontend/FractionalRolloutWarning.tsx@@ -0,0 +1,62 @@+import { LemonBanner } from 'lib/lemon-ui/LemonBanner'+import { Link } from 'lib/lemon-ui/Link'++import { FeatureFlagGroupType } from '~/types'++/** First SDK releases that deserialize a fractional rollout percentage. */+const MIN_DOTNET_VERSION = '2.13.3'+const MIN_JAVA_VERSION = '2.12.1'++/** Release-condition rollout percentages that aren't whole numbers.+ *+ * Ignores multivariate variant rollouts on purpose: the SDKs that typed rollout percentages as+ * integers only did so for the condition group field, so fractional variant splits, which an even+ * three-way split can't avoid, parse everywhere. */+export function fractionalRolloutPercentages(filterGroups: FeatureFlagGroupType[] | undefined): number[] {+ return (filterGroups ?? [])+ .map((group) => group.rollout_percentage)+ .filter((percentage): percentage is number => typeof percentage === 'number' && !Number.isInteger(percentage))+}++/** Trims a stored rollout to something readable. A flag saved through the API can carry far more+ * precision than the editor's two decimal places, and 33.333333333333336 reads as a bug rather+ * than a warning. Significant digits rather than fixed decimals, so a sub-0.01 rollout such as+ * 0.00015 doesn't render as 0. */+function formatPercentage(percentage: number): string {+ return `${Number(percentage.toPrecision(4))}%`+}++/** Warns that a fractional release-condition rollout breaks local evaluation on older SDKs.+ *+ * Not gated on the flag's evaluation runtime: the local evaluation payload carries client-only flags+ * too (SDKs filter by runtime only after parsing), so even a client-only flag breaks the parse. */+export function FractionalRolloutWarning({+ filterGroups,+ className,+}: {+ filterGroups: FeatureFlagGroupType[] | undefined+ className?: string+}): JSX.Element | null {+ const percentages = fractionalRolloutPercentages(filterGroups)+ if (percentages.length === 0) {+ return null+ }++ const formatted = percentages.map(formatPercentage).join(', ')++ return (+ <LemonBanner type="warning" className={className}>+ {percentages.length === 1+ ? `This flag has a fractional rollout percentage (${formatted}).`+ : `This flag has fractional rollout percentages (${formatted}).`}{' '}+ The .NET and Java server-side SDKs read rollout percentages as whole numbers before .NET{' '}+ {MIN_DOTNET_VERSION} and Java {MIN_JAVA_VERSION}. On an older version the flag definitions payload fails to+ parse, so local evaluation stops working for <strong>every flag in the project</strong> and each evaluation+ goes to the <code>/flags</code> endpoint instead. Upgrade those SDKs, or change the rollout to a whole+ number.{' '}+ <Link to="https://posthog.com/docs/feature-flags/local-evaluation" target="_blank">+ Learn more about local evaluation+ </Link>+ </LemonBanner>+ )+}diff --git a/products/feature_flags/package.json b/products/feature_flags/package.jsonindex 91fc501c84f5..80fef8eecd79 100644--- a/products/feature_flags/package.json+++ b/products/feature_flags/package.json@@ -11,11 +11,14 @@},"devDependencies": {"@storybook/react": "catalog:",+ "@testing-library/react": "^14.3.1","kea-test-utils": "catalog:"},"peerDependencies": {"@posthog/brand": "catalog:","@posthog/icons": "catalog:",+ "@testing-library/jest-dom": "*",+ "@testing-library/react": "*","@types/react": "*","clsx": "*","fuse.js": "*",