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 edits queued behind a slow auto-save (#126908)

Daniel Marchuk1df14a1b82c7
diff --git a/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts b/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
index c2f86dff159c..d4c57d6aaced 100644
--- a/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
+++ b/products/workflows/frontend/Workflows/workflowLogic.autosave.test.ts
@@ -780,6 +780,33 @@ describe('workflowLogic auto-save', () => {
await new Promise((resolve) => setTimeout(resolve, 300))
})
+ it.each([2, 3])('keeps the latest edit when %i saves queue behind a slow one', async (saves) => {
+ for (let i = 1; i <= saves; i++) {
+ logic.actions.setWorkflowValue('name', `Edit ${i}`)
+ logic.actions.markAutoSave(true)
+ logic.actions.saveWorkflow(logic.values.workflow)
+ if (i === 1) {
+ await firstPatchSeen
+ }
+ }
+
+ releaseFirstPatch?.()
+ await expectLogic(logic).toDispatchActions(['saveWorkflowSuccess'])
+
+ // The first response carries "Edit 1" only. Resetting the form to it puts that stale
+ // copy back on the canvas, which the live email editor then reloads.
+ expect(logic.values.workflow.name).toBe(`Edit ${saves}`)
+
+ // Once the queue drains, the form matches what was saved, so no further save starts.
+ await new Promise((resolve) => setTimeout(resolve, 300))
+ expect({
+ name: logic.values.workflow.name,
+ rejected,
+ patches: events.filter((event) => event === 'patch').length,
+ workflowChanged: logic.values.workflowChanged,
+ }).toEqual({ name: `Edit ${saves}`, rejected: 0, patches: saves, workflowChanged: false })
+ })
+
it('keeps the form submitting until the queued manual save lands', async () => {
logic.actions.setWorkflowValue('name', 'Renamed by me')
await expectLogic(logic).toDispatchActions(['saveWorkflow'])
diff --git a/products/workflows/frontend/Workflows/workflowLogic.ts b/products/workflows/frontend/Workflows/workflowLogic.ts
index d843b1caeca6..3c066f25cb8b 100644
--- a/products/workflows/frontend/Workflows/workflowLogic.ts
+++ b/products/workflows/frontend/Workflows/workflowLogic.ts
@@ -255,6 +255,19 @@ interface SaveContext {
movedToBroadcasts?: number
// A status change made while the form had unsaved edits. The edits stay in the form, unsaved.
keepsFormEdits: boolean
+ // The form's edit version when this save was dispatched. Its response compares against it to
+ // tell whether the user kept editing, including while an earlier save was still in flight.
+ editVersion: number
+}
+
+// Putting the edits back adds no new content, so a queued save that already sent them stays current.
+// Without this, every save in the queue would look edited when it lands.
+function keepQueuedSavesCurrent(queuedSaves: SaveContext[], fromVersion: number, toVersion: number): void {
+ for (const queued of queuedSaves) {
+ if (queued.editVersion === fromVersion) {
+ queued.editVersion = toVersion
+ }
+ }
}
/** The schedule editor state, kept across a status change so unsaved schedule edits survive it. */
@@ -3430,6 +3443,7 @@ export const workflowLogic = kea<workflowLogicType>([
scheduleId: values.currentSchedule?.id,
moveToBroadcastIds: (cache.nextSaveMovesWorkflowIds ?? []) as string[],
keepsFormEdits,
+ editVersion: values.workflowEditVersion,
}
saveContexts.push(saveContext)
cache.nextSaveMovesWorkflowIds = []
@@ -4529,9 +4543,6 @@ export const workflowLogic = kea<workflowLogicType>([
loadWorkflowFailure: () => {
actions.replayDeferredResourceEdited()
},
- saveWorkflow: () => {
- cache.saveEditVersion = values.workflowEditVersion
- },
saveWorkflowFailure: () => {
// Keep the queue aligned with the saves still in flight.
;(cache.saveContexts as SaveContext[] | undefined)?.shift()
@@ -4694,7 +4705,7 @@ export const workflowLogic = kea<workflowLogicType>([
// form on the merged view, or the reset would wipe the just-saved edits off the canvas.
const editedDuringSave =
!!saveContext?.keepsFormEdits ||
- (cache.saveEditVersion !== undefined && values.workflowEditVersion !== cache.saveEditVersion)
+ (saveContext !== undefined && values.workflowEditVersion !== saveContext.editVersion)
const editsDuringSave = editedDuringSave ? pickWorkflowEdits(values.workflow) : null
const scheduleEdits: ScheduleEdits | null = !wroteSchedule
? {
@@ -4714,8 +4725,14 @@ export const workflowLogic = kea<workflowLogicType>([
// The response only reflects the payload that was sent. Anything typed while it was
// in flight (the live email editor writes on every pause) must survive the reset and
// stay dirty, or it vanishes from the form and the canvas reloads the stale version.
+ const versionBeforeReapply = values.workflowEditVersion
actions.setWorkflowValues(editsDuringSave)
- actions.autoSaveWorkflow()
+ const queuedSaves = (cache.saveContexts as SaveContext[] | undefined) ?? []
+ keepQueuedSavesCurrent(queuedSaves, versionBeforeReapply, values.workflowEditVersion)
+ // A queued save runs this same check when it lands, so only the last one starts another.
+ if (queuedSaves.length === 0) {
+ actions.autoSaveWorkflow()
+ }
}
actions.replayDeferredResourceEdited()
},