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(workflows): keep unsaved schedule edits when an auto-save lands (#123394)

Meikel Ratz42007bcaf5bd
diff --git a/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts b/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
index 545086703c61..b09fc8ebe5f3 100644
--- a/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
+++ b/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
@@ -7,10 +7,11 @@ import { initKeaTests } from '~/test/init'
import { resourceEditedLogic } from 'products/notifications/frontend/resourceEditedLogic'
import { saveEntrySource } from '../Broadcasts/broadcastUsage'
-import { HogFlow } from './hogflows/types'
+import { HogFlow, HogFlowSchedule } from './hogflows/types'
import { workflowLogic } from './workflowLogic'
const WORKFLOW_ID = 'wf-autosave-1'
+const STARTS_AT = '2026-05-04T09:00:00.000Z'
const makeWorkflow = (overrides: Partial<HogFlow> = {}): HogFlow => ({
id: WORKFLOW_ID,
@@ -147,6 +148,48 @@ describe('workflowLogic auto-save', () => {
expect(updateCalls).toBe(2)
})
+ it('keeps an unsaved schedule change when an auto-save lands', async () => {
+ const saved: HogFlowSchedule = {
+ id: 'schedule-1',
+ rrule: 'FREQ=WEEKLY;INTERVAL=1;BYDAY=MO',
+ starts_at: '2026-05-04T09:00:00.000Z',
+ timezone: 'UTC',
+ }
+ logic.actions.setSchedules([saved])
+ // Move the run from Monday to Wednesday. Only a manual save writes the schedule, so
+ // an auto-save landing in between must leave this edit staged and saveable.
+ logic.actions.setScheduleState({ ...logic.values.scheduleState, weekdays: [2] })
+ expect(logic.values.hasUnsavedChanges).toBe(true)
+
+ await expectLogic(logic, () => {
+ logic.actions.markAutoSave(true)
+ logic.actions.saveWorkflow(logic.values.workflow)
+ }).toDispatchActions(['saveWorkflowSuccess'])
+
+ expect(logic.values.scheduleState.weekdays).toEqual([2])
+ expect(logic.values.hasUnsavedChanges).toBe(true)
+ })
+
+ it('keeps the created schedule as the baseline when the list read says there is none', async () => {
+ useMocks({
+ get: { '/api/environments/:team_id/hog_flows/:id/schedules/': [] },
+ post: {
+ '/api/environments/:team_id/hog_flows/:id/schedules/': () => [
+ 201,
+ { id: 'schedule-created', rrule: 'FREQ=DAILY;COUNT=1', starts_at: STARTS_AT, timezone: 'UTC' },
+ ],
+ },
+ })
+ logic.actions.setScheduleStartsAt(STARTS_AT)
+
+ await expectLogic(logic, () => {
+ logic.actions.saveWorkflow(logic.values.workflow)
+ }).toDispatchActions(['saveWorkflowSuccess', 'setSchedules'])
+
+ // Without this the next save sees no schedule and creates a second one.
+ expect(logic.values.currentSchedule?.id).toBe('schedule-created')
+ })
+
it('does not re-save when nothing changed during the round-trip', async () => {
jest.useFakeTimers()
diff --git a/products/workflows/frontend/Workflows/workflowLogic.ts b/products/workflows/frontend/Workflows/workflowLogic.ts
index d57675e5e4b7..f1e0a23ab948 100644
--- a/products/workflows/frontend/Workflows/workflowLogic.ts
+++ b/products/workflows/frontend/Workflows/workflowLogic.ts
@@ -4461,6 +4461,10 @@ export const workflowLogic = kea<workflowLogicType>([
// second toast, and a second schedule write that can leave a duplicate schedule.
const saveContext = (cache.saveContexts as SaveContext[] | undefined)?.shift()
const isAutoSave = saveContext?.initiatedByAutoSave ?? values.isAutoSave
+ // Only a manual save writes the schedule, and only once the reload below confirms it.
+ // The reset at the end of this listener re-derives the schedule editor from the saved
+ // schedule, so every other save must put the user's unsaved edits back afterwards.
+ let wroteSchedule = false
if (cache.enablingWorkflow && originalWorkflow.status === 'active') {
cache.enablingWorkflow = false
@@ -4482,13 +4486,30 @@ export const workflowLogic = kea<workflowLogicType>([
if (hasScheduleChanges) {
try {
+ // Each write answers with the saved schedule, so the editor rebaselines from
+ // the response. Re-reading the list instead would leave a created schedule
+ // unknown whenever that read failed, and the next save would add a second one.
if (pendingSchedule === null && existingScheduleId) {
await api.hogFlows.deleteHogFlowSchedule(workflowId, existingScheduleId)
+ actions.setSchedules([])
} else if (pendingSchedule !== null && existingScheduleId) {
- await api.hogFlows.updateHogFlowSchedule(workflowId, existingScheduleId, pendingSchedule)
+ actions.setSchedules([
+ // The generated client answers with HogFlowScheduleApi, whose untyped
+ // `variables` does not satisfy the HogFlowSchedule the editor holds.
+ // nosemgrep: prefer-codegen-api-namespaced-workflows
+ await api.hogFlows.updateHogFlowSchedule(
+ workflowId,
+ existingScheduleId,
+ pendingSchedule
+ ),
+ ])
} else if (pendingSchedule !== null) {
- await api.hogFlows.createHogFlowSchedule(workflowId, pendingSchedule)
+ actions.setSchedules([
+ // nosemgrep: prefer-codegen-api-namespaced-workflows
+ await api.hogFlows.createHogFlowSchedule(workflowId, pendingSchedule),
+ ])
}
+ wroteSchedule = true
if (pendingSchedule !== null) {
posthog.capture('workflows schedule saved', {
@@ -4497,9 +4518,6 @@ export const workflowLogic = kea<workflowLogicType>([
configured_via_natural_language: values.scheduleConfigSources.natural_language,
})
}
-
- const schedules = await api.hogFlows.getHogFlowSchedules(workflowId)
- actions.setSchedules(schedules)
} catch (e) {
console.error('Failed to save schedule', e)
lemonToast.error('Workflow saved, but schedule could not be updated')
@@ -4589,7 +4607,7 @@ export const workflowLogic = kea<workflowLogicType>([
!!saveContext?.keepsFormEdits ||
(cache.saveEditVersion !== undefined && values.workflowEditVersion !== cache.saveEditVersion)
const editsDuringSave = editedDuringSave ? pickWorkflowEdits(values.workflow) : null
- const scheduleEdits: ScheduleEdits | null = saveContext?.keepsFormEdits
+ const scheduleEdits: ScheduleEdits | null = !wroteSchedule
? {
scheduleState: values.scheduleState,
scheduleStartsAt: values.scheduleStartsAt,