From aac05b608eaf1a13b0bd9a3a3023d87f4a52df98 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Wed, 8 Apr 2026 12:52:32 -0700 Subject: [PATCH] fix: merge subblock values in auto-layout to prevent losing router context 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. Co-Authored-By: Claude Opus 4.6 --- .../w/[workflowId]/utils/auto-layout-utils.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/utils/auto-layout-utils.ts b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/utils/auto-layout-utils.ts index 69a15ec7d67..e9a203ef728 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/utils/auto-layout-utils.ts +++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/utils/auto-layout-utils.ts @@ -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') @@ -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(), } @@ -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, })