@@ -298,6 +298,15 @@ interface DbBlockRef {
298298 data : unknown
299299}
300300
301+ /** Every block of the workflow with the fields the lock checks read, keyed by id. */
302+ async function loadLockBlocks ( tx : DbOrTx , workflowId : string ) : Promise < Record < string , DbBlockRef > > {
303+ const rows = await tx
304+ . select ( { id : workflowBlocks . id , locked : workflowBlocks . locked , data : workflowBlocks . data } )
305+ . from ( workflowBlocks )
306+ . where ( eq ( workflowBlocks . workflowId , workflowId ) )
307+ return Object . fromEntries ( rows . map ( ( row ) => [ row . id , row ] ) )
308+ }
309+
301310/**
302311 * Finds all descendant block IDs of a container (recursive).
303312 * Works with raw DB block arrays.
@@ -413,8 +422,21 @@ export async function getWorkflowState(workflowId: string) {
413422 }
414423}
415424
416- export async function persistWorkflowOperation ( workflowId : string , operation : any ) {
425+ /** What a persisted operation means for the clients that applied its payload optimistically. */
426+ export interface PersistedWorkflowOperation {
427+ /**
428+ * True when the stored graph kept lock-protected state the payload tried to change, so the
429+ * payload no longer describes what was saved and every client must re-sync from the server.
430+ */
431+ reconcile : boolean
432+ }
433+
434+ export async function persistWorkflowOperation (
435+ workflowId : string ,
436+ operation : any
437+ ) : Promise < PersistedWorkflowOperation > {
417438 const startTime = Date . now ( )
439+ let reconcile = false
418440 try {
419441 const { operation : op , target, payload, timestamp, userId } = operation
420442
@@ -451,7 +473,7 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
451473 await handleBlockOperationTx ( tx , workflowId , op , payload )
452474 break
453475 case OPERATION_TARGETS . BLOCKS :
454- await handleBlocksOperationTx ( tx , workflowId , op , payload )
476+ reconcile = ( await handleBlocksOperationTx ( tx , workflowId , op , payload ) ) === true
455477 break
456478 case OPERATION_TARGETS . EDGE :
457479 await handleEdgeOperationTx ( tx , workflowId , op , payload )
@@ -469,7 +491,7 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
469491 await handleVariableOperationTx ( tx , workflowId , op , payload )
470492 break
471493 case OPERATION_TARGETS . WORKFLOW :
472- await handleWorkflowOperationTx ( tx , workflowId , op , payload )
494+ reconcile = await handleWorkflowOperationTx ( tx , workflowId , op , payload )
473495 break
474496 default :
475497 throw new Error ( `Unknown operation target: ${ target } ` )
@@ -495,6 +517,7 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
495517 workflowId : `${ workflowId . substring ( 0 , 8 ) } ...` ,
496518 } )
497519 }
520+ return { reconcile }
498521 } catch ( error ) {
499522 const duration = Date . now ( ) - startTime
500523 logger . error (
@@ -949,36 +972,31 @@ async function handleBlocksOperationTx(
949972 parallelCount : Object . keys ( parallels || { } ) . length ,
950973 } )
951974
952- if ( blocks && blocks . length > 0 ) {
953- const existingBlocks : DbBlockRef [ ] = await tx
954- . select ( {
955- id : workflowBlocks . id ,
956- locked : workflowBlocks . locked ,
957- data : workflowBlocks . data ,
958- } )
959- . from ( workflowBlocks )
960- . where ( eq ( workflowBlocks . workflowId , workflowId ) )
961- const blocksById = Object . fromEntries ( existingBlocks . map ( ( b ) => [ b . id , b ] ) )
975+ const lockBlocks = await loadLockBlocks ( tx , workflowId )
976+ const isProtected = ( id : string ) => isWorkflowBlockProtected ( id , lockBlocks )
962977
963- // Lock state is admin-only, so an add never overwrites a protected block
964- // nor places one inside a locked container.
978+ // Lock state is admin-only, so an add never writes the lock flag, overwrites a protected
979+ // block or its loop/parallel config, nor places a block inside a locked container.
980+ let reconcile = false
981+
982+ if ( blocks && blocks . length > 0 ) {
965983 const allowedBlocks = ( blocks as Array < Record < string , unknown > > ) . filter ( ( block ) => {
966984 const parentId = ( block . data as Record < string , unknown > | null ) ?. parentId as
967985 | string
968986 | undefined
969- if (
970- isWorkflowBlockProtected ( block . id as string , blocksById ) ||
971- ( parentId && isWorkflowBlockProtected ( parentId , blocksById ) )
972- ) {
987+ if ( isProtected ( block . id as string ) || ( parentId && isProtected ( parentId ) ) ) {
973988 logger . info ( `Skipping block ${ block . id } - it or its parent is locked` )
974989 return false
975990 }
976991 return true
977992 } )
993+ reconcile =
994+ allowedBlocks . length < blocks . length ||
995+ ( blocks as Array < Record < string , unknown > > ) . some ( ( block ) => block . locked )
978996
979997 if ( allowedBlocks . length === 0 ) {
980998 logger . info ( 'All blocks filtered out due to locks, skipping add' )
981- break
999+ return reconcile
9821000 }
9831001
9841002 const blockValues = allowedBlocks . map ( ( block : Record < string , unknown > ) => {
@@ -1115,36 +1133,37 @@ async function handleBlocksOperationTx(
11151133 }
11161134 }
11171135
1118- if ( loops && Object . keys ( loops ) . length > 0 ) {
1119- const loopValues = Object . entries ( loops ) . map ( ( [ id , loop ] ) => ( {
1136+ const subflowEntries = [
1137+ ...Object . entries ( loops || { } ) . map ( ( [ id , config ] ) => ( { id, type : 'loop' , config } ) ) ,
1138+ ...Object . entries ( parallels || { } ) . map ( ( [ id , config ] ) => ( {
11201139 id,
1140+ type : 'parallel' ,
1141+ config,
1142+ } ) ) ,
1143+ ]
1144+ const subflowValues = subflowEntries
1145+ . filter ( ( subflow ) => ! isProtected ( subflow . id ) )
1146+ . map ( ( subflow ) => ( {
1147+ ...subflow ,
11211148 workflowId,
1122- type : 'loop' ,
1123- config : loop as Record < string , unknown > ,
1149+ config : subflow . config as Record < string , unknown > ,
11241150 } ) )
1125-
1126- await upsertWorkflowRows ( tx , workflowSubflows , workflowId , loopValues , SUBFLOW_UPSERT_SET )
1151+ if ( subflowValues . length < subflowEntries . length ) {
1152+ logger . info ( 'Skipping loop/parallel config of locked containers' )
1153+ reconcile = true
11271154 }
1128-
1129- if ( parallels && Object . keys ( parallels ) . length > 0 ) {
1130- const parallelValues = Object . entries ( parallels ) . map ( ( [ id , parallel ] ) => ( {
1131- id,
1132- workflowId,
1133- type : 'parallel' ,
1134- config : parallel as Record < string , unknown > ,
1135- } ) )
1136-
1155+ if ( subflowValues . length > 0 ) {
11371156 await upsertWorkflowRows (
11381157 tx ,
11391158 workflowSubflows ,
11401159 workflowId ,
1141- parallelValues ,
1160+ subflowValues ,
11421161 SUBFLOW_UPSERT_SET
11431162 )
11441163 }
11451164
11461165 logger . info ( `Successfully batch added blocks to workflow ${ workflowId } ` )
1147- break
1166+ return reconcile
11481167 }
11491168
11501169 case BLOCKS_OPERATIONS . BATCH_REMOVE_BLOCKS : {
@@ -1816,6 +1835,13 @@ async function handleSubflowOperationTx(
18161835 throw new Error ( 'Missing required fields for update subflow operation' )
18171836 }
18181837
1838+ if ( isWorkflowBlockProtected ( payload . id , await loadLockBlocks ( tx , workflowId ) ) ) {
1839+ logger . info (
1840+ `Skipping update of subflow ${ payload . id } - it is locked or in a locked container`
1841+ )
1842+ break
1843+ }
1844+
18191845 logger . debug ( `Updating subflow ${ payload . id } with config:` , payload . config )
18201846
18211847 // Read-modify-write merge so partial config payloads never wipe other fields
@@ -2128,13 +2154,13 @@ async function handleVariableOperationTx(
21282154 }
21292155}
21302156
2131- /** Replaces the complete workflow state. */
2157+ /** Replaces the complete workflow state; resolves whether clients must re-sync from the server . */
21322158async function handleWorkflowOperationTx (
21332159 tx : any ,
21342160 workflowId : string ,
21352161 operation : string ,
21362162 payload : any
2137- ) {
2163+ ) : Promise < boolean > {
21382164 switch ( operation ) {
21392165 case WORKFLOW_OPERATIONS . REPLACE_STATE : {
21402166 if ( ! payload . state ) {
@@ -2261,7 +2287,14 @@ async function handleWorkflowOperationTx(
22612287 }
22622288
22632289 logger . info ( `Successfully replaced workflow state for ${ workflowId } ` )
2264- break
2290+ // A graph holding protected blocks may have been kept over the payload; comparing the
2291+ // stored rows against it field by field is not worth the precision, so re-sync.
2292+ return (
2293+ merged . protectedBlockIds . size > 0 ||
2294+ Object . values ( ( blocks || { } ) as Record < string , { locked ?: unknown } > ) . some (
2295+ ( block ) => block . locked
2296+ )
2297+ )
22652298 }
22662299
22672300 default :
0 commit comments