Skip to content
Merged
Prev Previous commit
Next Next commit
fix: merge subblock values in auto-layout to prevent losing router co…
…ntext (#4055)

Auto-layout was reading from getWorkflowState() without merging subblock
store values, then persisting stale subblock data to the database. This
caused runtime-edited values (e.g. router_v2 context) to be overwritten
with their initial/empty values whenever auto-layout was triggered.
  • Loading branch information
waleedlatif1 authored and TheodoreSpeaks committed Apr 8, 2026
commit 096bb48e2c2f6f9baf3ec8c760814d9844ead9ca
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DEFAULT_LAYOUT_PADDING,
DEFAULT_VERTICAL_SPACING,
} from '@/lib/workflows/autolayout/constants'
import { mergeSubblockState } from '@/stores/workflows/utils'
import { useWorkflowStore } from '@/stores/workflows/workflow/store'

const logger = createLogger('AutoLayoutUtils')
Expand Down Expand Up @@ -109,10 +110,12 @@ export async function applyAutoLayoutAndUpdateStore(
return { success: false, error: errorMessage }
}

// Update workflow store immediately with new positions
const layoutedBlocks = result.data?.layoutedBlocks || blocks
const mergedBlocks = mergeSubblockState(layoutedBlocks, workflowId)

const newWorkflowState = {
...workflowStore.getWorkflowState(),
blocks: result.data?.layoutedBlocks || blocks,
blocks: mergedBlocks,
lastSaved: Date.now(),
}

Expand Down Expand Up @@ -167,9 +170,10 @@ export async function applyAutoLayoutAndUpdateStore(
})

// Revert the store changes since database save failed
const revertBlocks = mergeSubblockState(blocks, workflowId)
useWorkflowStore.getState().replaceWorkflowState({
...workflowStore.getWorkflowState(),
blocks,
blocks: revertBlocks,
lastSaved: workflowStore.lastSaved,
})

Expand Down