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): always allow disabling a workflow (#122110)

Meikel Ratzc57bc592c2ee
diff --git a/products/workflows/frontend/Workflows/WorkflowActions.tsx b/products/workflows/frontend/Workflows/WorkflowActions.tsx
index 0ddd37ee01bc..4816f4e88e66 100644
--- a/products/workflows/frontend/Workflows/WorkflowActions.tsx
+++ b/products/workflows/frontend/Workflows/WorkflowActions.tsx
@@ -31,6 +31,7 @@ export function WorkflowActions(props: WorkflowSceneLogicProps): JSX.Element {
publishDisabledReason,
discardDisabledReason,
showDraftActions,
+ statusToggleDisabledReason,
} = useValues(workflowLogic)
const { saveWorkflowPartial, submitWorkflow, discardChanges, publishDraft, discardDraft } =
useActions(workflowLogic)
@@ -85,7 +86,7 @@ export function WorkflowActions(props: WorkflowSceneLogicProps): JSX.Element {
status: workflow?.status === 'draft' ? 'active' : 'draft',
})
}
- disabledReason={hasUnsavedChanges ? 'Save changes first' : undefined}
+ disabledReason={statusToggleDisabledReason}
className="transition-colors duration-300 ease-in-out"
data-attr="workflow-launch"
>
@@ -109,7 +110,7 @@ export function WorkflowActions(props: WorkflowSceneLogicProps): JSX.Element {
})
}
size="small"
- disabledReason={hasUnsavedChanges ? 'Save changes first' : undefined}
+ disabledReason={statusToggleDisabledReason}
className="transition-colors duration-300 ease-in-out"
data-attr="workflow-launch"
>
diff --git a/products/workflows/frontend/Workflows/WorkflowSceneHeader.test.tsx b/products/workflows/frontend/Workflows/WorkflowSceneHeader.test.tsx
index 1c06d4312cd6..9cb81ab26eff 100644
--- a/products/workflows/frontend/Workflows/WorkflowSceneHeader.test.tsx
+++ b/products/workflows/frontend/Workflows/WorkflowSceneHeader.test.tsx
@@ -5,6 +5,7 @@ import { BindLogic, Provider } from 'kea'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
+import { AccessControlLevel } from '~/types'
import { HogFlow } from './hogflows/types'
import { workflowLogic } from './workflowLogic'
@@ -46,6 +47,7 @@ const ACTIVE_WITH_DRAFT: HogFlow = {
updated_at: '2026-05-01T00:00:00.000Z',
draft: { name: 'Header test', actions: [], edges: [] },
draft_updated_at: '2026-05-01T00:01:00.000Z',
+ user_access_level: AccessControlLevel.Editor,
}
// The label and order of the buttons a person points at, as rendered.
@@ -98,4 +100,22 @@ describe('WorkflowSceneHeader', () => {
// The pointer has not moved, so neither has the button under it.
expect(toolbar()).toEqual(clean)
})
+
+ it('keeps Disable clickable on an active workflow while the form is dirty', () => {
+ render(
+ <Provider>
+ <BindLogic logic={workflowLogic} props={{ id: WORKFLOW_ID }}>
+ <WorkflowSceneHeader id={WORKFLOW_ID} />
+ </BindLogic>
+ </Provider>
+ )
+
+ act(() => {
+ logic.actions.setWorkflowValue('name', 'Still typing')
+ })
+
+ const launch = document.querySelector('[data-attr="workflow-launch"]')
+ expect(launch).toHaveTextContent('Disable')
+ expect(launch).not.toHaveAttribute('aria-disabled', 'true')
+ })
})
diff --git a/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts b/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
index 8380fefd6899..545086703c61 100644
--- a/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
+++ b/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
@@ -429,6 +429,44 @@ describe('workflowLogic auto-save', () => {
expect(patchBodies[0].status).toBe('draft')
})
+ it('disables with unsaved edits in the form, without saving or dropping them', async () => {
+ useMocks(activeMocks(activeWorkflow))
+ initKeaTests()
+ logic = workflowLogic({ id: WORKFLOW_ID })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadWorkflowSuccess'])
+
+ let scheduleWrites = 0
+ useMocks({
+ patch: {
+ '/api/environments/:team_id/hog_flows/:id/schedules/:schedule_id/': () => {
+ scheduleWrites += 1
+ return [200, {}]
+ },
+ },
+ })
+ logic.actions.setSchedules([
+ { id: 'schedule-1', rrule: 'FREQ=DAILY', starts_at: '2026-05-02T09:00:00.000Z', timezone: 'UTC' },
+ ])
+ logic.actions.setScheduleStartsAt('2026-05-03T09:00:00.000Z')
+ logic.actions.setWorkflowValues({
+ name: 'Unsaved name',
+ actions: renameExit(logic.values.workflow.actions, 'Unsaved exit'),
+ })
+ expect(logic.values.statusToggleDisabledReason).toBeUndefined()
+
+ await expectLogic(logic, () => {
+ logic.actions.saveWorkflowPartial({ status: 'draft' })
+ }).toDispatchActions(['saveWorkflowSuccess', 'resetWorkflow'])
+
+ expect(patchBodies[0]).toMatchObject({ status: 'draft', name: 'Autosave test' })
+ expect(logic.values.workflow).toMatchObject({ status: 'draft', name: 'Unsaved name' })
+ expect(logic.values.workflow.actions.find((a) => a.id === 'exit_node')?.name).toBe('Unsaved exit')
+ expect(scheduleWrites).toBe(0)
+ expect(logic.values.pendingSchedule).toMatchObject({ starts_at: '2026-05-03T09:00:00.000Z' })
+ expect(logic.values.hasUnsavedChanges).toBe(true)
+ })
+
it('an edit to a disabled workflow with a leftover draft clears the draft and keeps the edit', async () => {
useMocks(
activeMocks({
diff --git a/products/workflows/frontend/Workflows/workflowLogic.schedule.test.ts b/products/workflows/frontend/Workflows/workflowLogic.schedule.test.ts
index 9e3e2f030a66..f3825b508398 100644
--- a/products/workflows/frontend/Workflows/workflowLogic.schedule.test.ts
+++ b/products/workflows/frontend/Workflows/workflowLogic.schedule.test.ts
@@ -154,12 +154,19 @@ describe('workflowLogic schedule reducers', () => {
})
describe('hasUnsavedChanges', () => {
- it('is false after loading a saved schedule', async () => {
- await expectLogic(logic, () => {
- logic.actions.setSchedules([makeSchedule()])
- }).toMatchValues({
- hasUnsavedChanges: false,
- })
+ // Schedules saved through the API or MCP keep the rrule as written, which is often not the
+ // exact string the picker regenerates.
+ it.each([
+ ['the picker format', WEEKLY_MONDAY_RRULE],
+ ['no interval', 'FREQ=WEEKLY;BYDAY=MO'],
+ ['a different part order', 'FREQ=WEEKLY;BYDAY=MO;INTERVAL=1'],
+ ['a bare frequency', 'FREQ=DAILY'],
+ ])('is false after loading a saved schedule with %s, and after clearing changes', (_, rrule) => {
+ logic.actions.setSchedules([makeSchedule({ rrule })])
+ expect(logic.values.hasUnsavedChanges).toBe(false)
+
+ logic.actions.resetWorkflow(logic.values.workflow)
+ expect(logic.values.hasUnsavedChanges).toBe(false)
})
it.each([
diff --git a/products/workflows/frontend/Workflows/workflowLogic.ts b/products/workflows/frontend/Workflows/workflowLogic.ts
index 30c1c98c0ce6..d57675e5e4b7 100644
--- a/products/workflows/frontend/Workflows/workflowLogic.ts
+++ b/products/workflows/frontend/Workflows/workflowLogic.ts
@@ -249,6 +249,17 @@ interface SaveContext {
moveToBroadcastIds: string[]
// How many of them moved. The save sets it before it finishes.
movedToBroadcasts?: number
+ // A status change made while the form had unsaved edits. The edits stay in the form, unsaved.
+ keepsFormEdits: boolean
+}
+
+/** The schedule editor state, kept across a status change so unsaved schedule edits survive it. */
+interface ScheduleEdits {
+ scheduleState: ScheduleState
+ scheduleStartsAt: string | null
+ scheduleTimezone: string
+ isScheduleRepeating: boolean
+ scheduleConfigSources: { picker: boolean; natural_language: boolean }
}
function omitWorkflowContent(workflow: HogFlow): Partial<HogFlow> {
@@ -318,6 +329,7 @@ export interface workflowLogicValues {
schedules: HogFlowSchedule[]
showDraftActions: boolean
showWorkflowErrors: boolean
+ statusToggleDisabledReason: string | undefined
triggerAction: TriggerAction | null
workflow: HogFlow
workflowAllErrors: Record<string, any>
@@ -2519,6 +2531,9 @@ export interface workflowLogicActions {
resetWorkflow: (values?: HogFlow) => {
values?: HogFlow
}
+ restoreScheduleEdits: (edits: ScheduleEdits) => {
+ edits: ScheduleEdits
+ }
resumeEmailSending: () => {
value: true
}
@@ -3191,6 +3206,7 @@ export interface workflowLogicMeta {
hasUnsavedChanges: boolean,
draftActionPending: 'discard' | 'publish' | null
) => string | undefined
+ statusToggleDisabledReason: (workflow: HogFlow, hasUnsavedChanges: boolean) => string | undefined
}
}
@@ -3264,6 +3280,7 @@ export const workflowLogic = kea<workflowLogicType>([
setScheduleTimezone: (timezone: string, previousTimezone?: string) => ({ timezone, previousTimezone }),
setScheduleRepeating: (repeating: boolean) => ({ repeating }),
setSchedules: (schedules: HogFlowSchedule[]) => ({ schedules }),
+ restoreScheduleEdits: (edits: ScheduleEdits) => ({ edits }),
saveWorkflowPartial: (workflow: Partial<HogFlow>) => ({ workflow }),
markSaveAttempted: (actionIds: string[]) => ({ actionIds }),
triggerManualWorkflow: (variables: Record<string, any>) => ({
@@ -3362,13 +3379,17 @@ export const workflowLogic = kea<workflowLogicType>([
// The pending schedule rides along because an earlier save's reset clears it,
// which would drop a schedule change the user asked a manual save to persist.
const saveContexts = (cache.saveContexts ??= []) as SaveContext[]
+ const keepsFormEdits = cache.nextSaveKeepsFormEdits === true
const saveContext: SaveContext = {
initiatedByAutoSave,
- pendingSchedule: values.pendingSchedule,
+ // A status change keeps schedule edits unsaved, like the other edits in the form.
+ pendingSchedule: keepsFormEdits ? false : values.pendingSchedule,
moveToBroadcastIds: (cache.nextSaveMovesWorkflowIds ?? []) as string[],
+ keepsFormEdits,
}
saveContexts.push(saveContext)
cache.nextSaveMovesWorkflowIds = []
+ cache.nextSaveKeepsFormEdits = false
// Whether this save means to change the lifecycle. Only the enable and disable
// control does, and it says so when it dispatches. Comparing the payload's
// status against the stored one cannot tell the difference: that control does
@@ -3624,6 +3645,7 @@ export const workflowLogic = kea<workflowLogicType>([
{ ...DEFAULT_STATE } as ScheduleState,
{
setScheduleState: (_, { scheduleState }) => scheduleState,
+ restoreScheduleEdits: (_, { edits }) => edits.scheduleState,
setSchedules: (_, { schedules }) => {
const schedule = schedules[0]
if (schedule && !isOneTimeSchedule(schedule.rrule)) {
@@ -3637,6 +3659,7 @@ export const workflowLogic = kea<workflowLogicType>([
null as string | null,
{
setScheduleStartsAt: (_, { startsAt }) => startsAt,
+ restoreScheduleEdits: (_, { edits }) => edits.scheduleStartsAt,
setSchedules: (_, { schedules }) => schedules[0]?.starts_at ?? null,
},
],
@@ -3644,6 +3667,7 @@ export const workflowLogic = kea<workflowLogicType>([
dayjs.tz.guess() as string,
{
setScheduleTimezone: (_, { timezone }) => timezone,
+ restoreScheduleEdits: (_, { edits }) => edits.scheduleTimezone,
setSchedules: (_, { schedules }) => schedules[0]?.timezone ?? dayjs.tz.guess(),
},
],
@@ -3651,6 +3675,7 @@ export const workflowLogic = kea<workflowLogicType>([
false as boolean,
{
setScheduleRepeating: (_, { repeating }) => repeating,
+ restoreScheduleEdits: (_, { edits }) => edits.isScheduleRepeating,
setSchedules: (_, { schedules }) => {
const schedule = schedules[0]
return !!schedule && !isOneTimeSchedule(schedule.rrule)
@@ -3663,6 +3688,7 @@ export const workflowLogic = kea<workflowLogicType>([
{ picker: false, natural_language: false } as { picker: boolean; natural_language: boolean },
{
setScheduleState: (state, { source }) => ({ ...state, [source]: true }),
+ restoreScheduleEdits: (_, { edits }) => edits.scheduleConfigSources,
setSchedules: () => ({ picker: false, natural_language: false }),
},
],
@@ -3822,8 +3848,12 @@ export const workflowLogic = kea<workflowLogicType>([
return newSchedule
}
- const savedRRule = currentSchedule.rrule
const savedStartsAt = currentSchedule.starts_at
+ // An rrule saved through the API or MCP is stored as written. Compare it in the form
+ // the picker regenerates, or an untouched schedule reads as an unsaved change.
+ const savedRRule = isOneTimeSchedule(currentSchedule.rrule)
+ ? ONE_TIME_RRULE
+ : stateToRRule(parseRRuleToState(currentSchedule.rrule), savedStartsAt)
const savedTimezone = currentSchedule.timezone ?? dayjs.tz.guess()
if (rrule === savedRRule && scheduleStartsAt === savedStartsAt && scheduleTimezone === savedTimezone) {
@@ -4173,6 +4203,18 @@ export const workflowLogic = kea<workflowLogicType>([
return hasStagedDraft ? undefined : 'No changes staged to discard'
},
],
+
+ statusToggleDisabledReason: [
+ (s) => [s.workflow, s.hasUnsavedChanges],
+ (workflow: HogFlow, hasUnsavedChanges: boolean): string | undefined => {
+ // Disabling is always allowed: it changes only the status and keeps the edits in the
+ // form. Enabling would run the saved version, not the edits on screen.
+ if (workflow?.status !== 'active' && hasUnsavedChanges) {
+ return 'Save changes first'
+ }
+ return undefined
+ },
+ ],
}),
listeners(({ actions, values, props, cache }) => ({
setScheduleStartsAtFromPicker: ({ pickerDate }) => {
@@ -4351,7 +4393,11 @@ export const workflowLogic = kea<workflowLogicType>([
// Recorded before the error guard: an enable attempt on an invalid draft must still
// reveal the per-field step messages even though the save itself is aborted.
actions.markSaveAttempted(values.workflow.actions.map((action) => action.id))
- const merged = { ...values.workflow, ...workflow }
+ // A status change on a dirty form sends the stored copy with the new status, so the edits
+ // on screen are not saved by accident. The success listener puts them back in the form.
+ const stored = (cache.lastSavedWorkflow as HogFlow | undefined) ?? values.originalWorkflow
+ const keepsFormEdits = 'status' in workflow && values.hasUnsavedChanges && !!stored
+ const merged = { ...(keepsFormEdits && stored ? withStagedDraft(stored) : values.workflow), ...workflow }
if (merged.status === 'active' && values.workflowHasActionErrors) {
lemonToast.error('Fix all errors before enabling')
return
@@ -4360,6 +4406,7 @@ export const workflowLogic = kea<workflowLogicType>([
// as it handles the action below, so it describes this save and no other.
cache.nextSaveChangesStatus = 'status' in workflow
cache.enablingWorkflow = workflow.status === 'active' && values.originalWorkflow?.status !== 'active'
+ cache.nextSaveKeepsFormEdits = keepsFormEdits
actions.saveWorkflow(merged)
},
loadWorkflowSuccess: async ({ originalWorkflow }) => {
@@ -4539,9 +4586,22 @@ export const workflowLogic = kea<workflowLogicType>([
// A staged save's response carries the live config plus the new draft blob: rebaseline the
// form on the merged view, or the reset would wipe the just-saved edits off the canvas.
const editedDuringSave =
- cache.saveEditVersion !== undefined && values.workflowEditVersion !== cache.saveEditVersion
+ !!saveContext?.keepsFormEdits ||
+ (cache.saveEditVersion !== undefined && values.workflowEditVersion !== cache.saveEditVersion)
const editsDuringSave = editedDuringSave ? pickWorkflowEdits(values.workflow) : null
+ const scheduleEdits: ScheduleEdits | null = saveContext?.keepsFormEdits
+ ? {
+ scheduleState: values.scheduleState,
+ scheduleStartsAt: values.scheduleStartsAt,
+ scheduleTimezone: values.scheduleTimezone,
+ isScheduleRepeating: values.isScheduleRepeating,
+ scheduleConfigSources: values.scheduleConfigSources,
+ }
+ : null
actions.resetWorkflow(withStagedDraft(originalWorkflow))
+ if (scheduleEdits) {
+ actions.restoreScheduleEdits(scheduleEdits)
+ }
actions.markAutoSave(false)
if (editsDuringSave) {
// The response only reflects the payload that was sent. Anything typed while it was