-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix(workflow-block): clearing child workflow input format field must lazy cascade parent workflow state deletion #2038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { db } from '@sim/db' | ||
| import { workflowBlocks } from '@sim/db/schema' | ||
| import { and, eq } from 'drizzle-orm' | ||
| import { createLogger } from '@/lib/logs/console/logger' | ||
|
|
||
| const logger = createLogger('LazyCleanup') | ||
|
|
||
| /** | ||
| * Extract valid field names from a child workflow's start block inputFormat | ||
| * | ||
| * @param childWorkflowBlocks - The blocks from the child workflow state | ||
| * @returns Set of valid field names defined in the child's inputFormat | ||
| */ | ||
| function extractValidInputFieldNames(childWorkflowBlocks: Record<string, any>): Set<string> | null { | ||
| const validFieldNames = new Set<string>() | ||
|
|
||
| const startBlock = Object.values(childWorkflowBlocks).find((block: any) => { | ||
| const blockType = block?.type | ||
| return blockType === 'start_trigger' || blockType === 'input_trigger' || blockType === 'starter' | ||
| }) | ||
|
|
||
| if (!startBlock) { | ||
| logger.debug('No start block found in child workflow') | ||
| return null | ||
| } | ||
|
|
||
| const inputFormat = | ||
| (startBlock as any)?.subBlocks?.inputFormat?.value ?? | ||
| (startBlock as any)?.config?.params?.inputFormat | ||
|
|
||
| if (!Array.isArray(inputFormat)) { | ||
| logger.debug('No inputFormat array found in child workflow start block') | ||
| return null | ||
| } | ||
|
|
||
| // Extract field names | ||
| for (const field of inputFormat) { | ||
| if (field?.name && typeof field.name === 'string') { | ||
| const fieldName = field.name.trim() | ||
| if (fieldName) { | ||
| validFieldNames.add(fieldName) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return validFieldNames | ||
| } | ||
|
|
||
| /** | ||
| * Clean up orphaned inputMapping fields that don't exist in child workflow's inputFormat. | ||
| * This is a lazy cleanup that only runs at execution time and only persists if changes are needed. | ||
| * | ||
| * @param parentWorkflowId - The parent workflow ID | ||
| * @param parentBlockId - The workflow block ID in the parent | ||
| * @param currentInputMapping - The current inputMapping value from the parent block | ||
| * @param childWorkflowBlocks - The blocks from the child workflow | ||
| * @returns The cleaned inputMapping (only different if cleanup was needed) | ||
| */ | ||
| export async function lazyCleanupInputMapping( | ||
| parentWorkflowId: string, | ||
| parentBlockId: string, | ||
| currentInputMapping: any, | ||
| childWorkflowBlocks: Record<string, any> | ||
| ): Promise<any> { | ||
| try { | ||
| if ( | ||
| !currentInputMapping || | ||
| typeof currentInputMapping !== 'object' || | ||
| Array.isArray(currentInputMapping) | ||
| ) { | ||
| return currentInputMapping | ||
| } | ||
|
|
||
| const validFieldNames = extractValidInputFieldNames(childWorkflowBlocks) | ||
|
|
||
| if (!validFieldNames || validFieldNames.size === 0) { | ||
| logger.debug('Child workflow has no inputFormat fields, skipping cleanup') | ||
| return currentInputMapping | ||
| } | ||
|
|
||
| const orphanedFields: string[] = [] | ||
| for (const fieldName of Object.keys(currentInputMapping)) { | ||
| if (!validFieldNames.has(fieldName)) { | ||
| orphanedFields.push(fieldName) | ||
| } | ||
| } | ||
|
|
||
| if (orphanedFields.length === 0) { | ||
| return currentInputMapping | ||
| } | ||
|
|
||
| const cleanedMapping: Record<string, any> = {} | ||
| for (const [fieldName, fieldValue] of Object.entries(currentInputMapping)) { | ||
| if (validFieldNames.has(fieldName)) { | ||
| cleanedMapping[fieldName] = fieldValue | ||
| } | ||
| } | ||
|
|
||
| logger.info( | ||
| `Lazy cleanup: Removing ${orphanedFields.length} orphaned field(s) from inputMapping in workflow ${parentWorkflowId}, block ${parentBlockId}: ${orphanedFields.join(', ')}` | ||
| ) | ||
|
|
||
| persistCleanedMapping(parentWorkflowId, parentBlockId, cleanedMapping).catch((error) => { | ||
| logger.error('Failed to persist cleaned inputMapping:', error) | ||
| }) | ||
|
|
||
| return cleanedMapping | ||
| } catch (error) { | ||
| logger.error('Error in lazy cleanup:', error) | ||
| return currentInputMapping | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Persist the cleaned inputMapping to the database | ||
| * | ||
| * @param workflowId - The workflow ID | ||
| * @param blockId - The block ID | ||
| * @param cleanedMapping - The cleaned inputMapping value | ||
| */ | ||
| async function persistCleanedMapping( | ||
| workflowId: string, | ||
| blockId: string, | ||
| cleanedMapping: Record<string, any> | ||
| ): Promise<void> { | ||
| try { | ||
| await db.transaction(async (tx) => { | ||
| const [block] = await tx | ||
| .select({ subBlocks: workflowBlocks.subBlocks }) | ||
| .from(workflowBlocks) | ||
| .where(and(eq(workflowBlocks.id, blockId), eq(workflowBlocks.workflowId, workflowId))) | ||
| .limit(1) | ||
|
|
||
| if (!block) { | ||
| logger.warn(`Block ${blockId} not found in workflow ${workflowId}, skipping persistence`) | ||
| return | ||
| } | ||
|
|
||
| const subBlocks = (block.subBlocks as Record<string, any>) || {} | ||
|
|
||
| if (subBlocks.inputMapping) { | ||
| subBlocks.inputMapping = { | ||
| ...subBlocks.inputMapping, | ||
| value: cleanedMapping, | ||
| } | ||
|
|
||
| // Persist updated subBlocks | ||
| await tx | ||
| .update(workflowBlocks) | ||
| .set({ | ||
| subBlocks: subBlocks, | ||
| updatedAt: new Date(), | ||
| }) | ||
| .where(and(eq(workflowBlocks.id, blockId), eq(workflowBlocks.workflowId, workflowId))) | ||
|
|
||
| logger.info(`Successfully persisted cleaned inputMapping for block ${blockId}`) | ||
| } | ||
| }) | ||
| } catch (error) { | ||
| logger.error('Error persisting cleaned mapping:', error) | ||
| throw error | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.