: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
fix(flags): order scheduled flag changes by scheduled date (#61026)
Phil Haack29f5646916e3
diff --git a/frontend/src/scenes/feature-flags/FeatureFlagSchedule.tsx b/frontend/src/scenes/feature-flags/FeatureFlagSchedule.tsxindex 5e69e564ae1f..d3d8f0578b98 100644--- a/frontend/src/scenes/feature-flags/FeatureFlagSchedule.tsx+++ b/frontend/src/scenes/feature-flags/FeatureFlagSchedule.tsx@@ -51,6 +51,7 @@ import {} from '~/types'import {+ byScheduledAt,describeCron,featureFlagLogic,PAIRED_PRESETS,@@ -1227,6 +1228,7 @@ function FeatureFlagScheduleLegacy(): JSX.Element {{title: 'Scheduled at',dataIndex: 'scheduled_at',+ sorter: byScheduledAt,render: function Render(_, scheduledChange: ScheduledChangeType) {const scheduledAt = dayjs(scheduledChange.scheduled_at).tz(tz)const tzShort = shortTimeZone(tz, scheduledAt.toDate()) ?? tzdiff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.test.tsindex 055770d45291..028b3ef87d0e 100644--- a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts@@ -1,4 +1,4 @@-import { MOCK_DEFAULT_PROJECT } from 'lib/api.mock'+import { MOCK_DEFAULT_BASIC_USER, MOCK_DEFAULT_PROJECT } from 'lib/api.mock'import { router } from 'kea-router'import { expectLogic, partial } from 'kea-test-utils'@@ -8,7 +8,14 @@ import { urls } from 'scenes/urls'import { useMocks } from '~/mocks/jest'import { initKeaTests } from '~/test/init'-import { FeatureFlagType, PropertyFilterType, PropertyOperator } from '~/types'+import {+ FeatureFlagType,+ PropertyFilterType,+ PropertyOperator,+ ScheduledChangeModels,+ ScheduledChangeOperationType,+ ScheduledChangeType,+} from '~/types'import { FeatureFlagFilters } from '~/types'import { TemplateKey } from 'products/feature_flags/frontend/featureFlagTemplateConstants'@@ -814,6 +821,141 @@ describe('featureFlagLogic', () => {testLogic.unmount()})})++ describe('schedule ordering', () => {+ const makeScheduledChange = (overrides: Partial<ScheduledChangeType>): ScheduledChangeType => ({+ id: 1,+ team_id: MOCK_DEFAULT_PROJECT.id,+ record_id: MOCK_FEATURE_FLAG.id,+ model_name: ScheduledChangeModels.FeatureFlag,+ payload: { operation: ScheduledChangeOperationType.UpdateStatus, value: true },+ scheduled_at: '2026-01-01T00:00:00Z',+ executed_at: null,+ failure_reason: null,+ created_at: '2026-01-01T00:00:00Z',+ created_by: MOCK_DEFAULT_BASIC_USER,+ is_recurring: false,+ recurrence_interval: null,+ cron_expression: null,+ last_executed_at: null,+ end_date: null,+ ...overrides,+ })++ const schedulesUrl = `/api/projects/${MOCK_DEFAULT_PROJECT.id}/scheduled_changes`++ it.each([+ {+ // Mirrors the issue: a recurring change created first must not float above sooner one-time changes.+ desc: 'interleaves recurring and one-time changes by next firing time, soonest first',+ results: [+ makeScheduledChange({ id: 1, scheduled_at: '2026-05-02T15:00:00Z', is_recurring: true }),+ makeScheduledChange({ id: 2, scheduled_at: '2026-05-01T14:00:00Z' }),+ makeScheduledChange({ id: 3, scheduled_at: '2026-07-04T14:58:00Z' }),+ makeScheduledChange({ id: 4, scheduled_at: '2026-06-13T18:59:00Z' }),+ ],+ expectedIds: [2, 1, 4, 3],+ },+ {+ desc: 'breaks ties by id when scheduled_at is equal',+ results: [+ makeScheduledChange({ id: 7, scheduled_at: '2026-05-01T00:00:00Z' }),+ makeScheduledChange({ id: 3, scheduled_at: '2026-05-01T00:00:00Z' }),+ makeScheduledChange({ id: 5, scheduled_at: '2026-05-01T00:00:00Z' }),+ ],+ expectedIds: [3, 5, 7],+ },+ {+ desc: 'excludes executed changes',+ results: [+ makeScheduledChange({ id: 1, scheduled_at: '2026-05-01T00:00:00Z' }),+ makeScheduledChange({+ id: 2,+ scheduled_at: '2026-04-01T00:00:00Z',+ executed_at: '2026-04-01T00:00:00Z',+ }),+ ],+ expectedIds: [1],+ },+ ])('$desc', async ({ results, expectedIds }) => {+ useMocks({ get: { [schedulesUrl]: () => [200, { results }] } })+ await expectLogic(logic, () => {+ logic.actions.loadScheduledChanges()+ }).toDispatchActions(['loadScheduledChangesSuccess'])++ await expectLogic(logic).toMatchValues({+ activeSchedules: expectedIds.map((id) => partial({ id })),+ })+ })++ it('orders completed changes most-recent first', async () => {+ useMocks({+ get: {+ [schedulesUrl]: () => [+ 200,+ {+ results: [+ makeScheduledChange({+ id: 1,+ scheduled_at: '2026-05-01T00:00:00Z',+ executed_at: '2026-05-01T00:00:00Z',+ }),+ makeScheduledChange({+ id: 2,+ scheduled_at: '2026-07-01T00:00:00Z',+ executed_at: '2026-07-01T00:00:00Z',+ }),+ makeScheduledChange({+ id: 3,+ scheduled_at: '2026-06-01T00:00:00Z',+ executed_at: '2026-06-01T00:00:00Z',+ }),+ ],+ },+ ],+ },+ })+ await expectLogic(logic, () => {+ logic.actions.loadScheduledChanges()+ }).toDispatchActions(['loadScheduledChangesSuccess'])++ await expectLogic(logic).toMatchValues({+ completedSchedules: [partial({ id: 2 }), partial({ id: 3 }), partial({ id: 1 })],+ })+ })++ it('orders completed changes by execution time, not scheduled time', async () => {+ useMocks({+ get: {+ [schedulesUrl]: () => [+ 200,+ {+ results: [+ // Scheduled first but, after a delay, executed last.+ makeScheduledChange({+ id: 1,+ scheduled_at: '2026-05-01T00:00:00Z',+ executed_at: '2026-05-03T00:00:00Z',+ }),+ makeScheduledChange({+ id: 2,+ scheduled_at: '2026-05-02T00:00:00Z',+ executed_at: '2026-05-02T00:00:00Z',+ }),+ ],+ },+ ],+ },+ })+ await expectLogic(logic, () => {+ logic.actions.loadScheduledChanges()+ }).toDispatchActions(['loadScheduledChangesSuccess'])++ await expectLogic(logic).toMatchValues({+ completedSchedules: [partial({ id: 1 }), partial({ id: 2 })],+ })+ })+ })})const createFilters = (overrides: Partial<FeatureFlagFilters> = {}): FeatureFlagFilters => ({diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.tsindex 6491e9d08b3f..4e2d9d7b027b 100644--- a/frontend/src/scenes/feature-flags/featureFlagLogic.ts+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.ts@@ -176,6 +176,34 @@ export function scheduleDateFromStoredISO(isoString: string, timezone: string):return dayjs(dayjs.utc(isoString).tz(timezone).format('YYYY-MM-DDTHH:mm:ss.SSS'))}+// Order scheduled changes by their `scheduled_at` time, soonest first. `scheduled_at` holds the+// next occurrence for recurring changes too, so it is a uniform sort key across all change types.+// Unparseable/missing dates sort last; ties break by `id` (creation order) for a stable order.+export function byScheduledAt(a: ScheduledChangeType, b: ScheduledChangeType): number {+ const epoch = (sc: ScheduledChangeType): number => {+ if (!sc.scheduled_at) {+ return Number.POSITIVE_INFINITY+ }+ const ms = dayjs(sc.scheduled_at).valueOf()+ return Number.isNaN(ms) ? Number.POSITIVE_INFINITY : ms+ }+ return epoch(a) - epoch(b) || a.id - b.id+}++// Order executed changes by `executed_at`, most recently executed first, so the history reads as a+// real execution timeline even when execution is delayed. Unparseable/missing dates sort last; ties+// break by `id` (creation order) for a stable order.+export function byExecutedAt(a: ScheduledChangeType, b: ScheduledChangeType): number {+ const epoch = (sc: ScheduledChangeType): number => {+ if (!sc.executed_at) {+ return Number.NEGATIVE_INFINITY+ }+ const ms = dayjs(sc.executed_at).valueOf()+ return Number.isNaN(ms) ? Number.NEGATIVE_INFINITY : ms+ }+ return epoch(b) - epoch(a) || b.id - a.id+}+export const PAIRED_PRESETS: Record<Exclude<PairedPresetKey, 'custom_pair'>, PairedPresetDefinition> = {business_hours: {label: 'Business hours',@@ -2495,15 +2523,14 @@ export const featureFlagLogic = kea<featureFlagLogicType>([],completedSchedules: [(s) => [s.scheduledChanges],- (scheduledChanges: ScheduledChangeType[]) => scheduledChanges.filter((sc) => !!sc.executed_at),+ (scheduledChanges: ScheduledChangeType[]) =>+ scheduledChanges.filter((sc) => !!sc.executed_at).sort(byExecutedAt),],activeSchedules: [(s) => [s.activeRecurringSchedules, s.pausedRecurringSchedules, s.upcomingOneTimeSchedules],- (activeRecurring, pausedRecurring, upcomingOneTime) => [- ...activeRecurring,- ...pausedRecurring,- ...upcomingOneTime,- ],+ // Interleave all schedule types so the upcoming list reads soonest first.+ (activeRecurring, pausedRecurring, upcomingOneTime) =>+ [...activeRecurring, ...pausedRecurring, ...upcomingOneTime].sort(byScheduledAt),],emailDomain: [(s) => [s.user], (user) => user?.email?.split('@')[1] || 'example.com'],templates: [