@@ -34,6 +34,7 @@ import {
3434 getWorkflowEdgeScopeDropReason ,
3535 isKnownWorkflowTriggerBlock ,
3636 isWorkflowAnnotationOnlyBlockType ,
37+ isWorkflowBlockAncestorLocked ,
3738 isWorkflowBlockProtected ,
3839 normalizeWorkflowEdgeSourceHandle ,
3940 normalizeWorkflowEdgeTargetHandle ,
@@ -978,21 +979,28 @@ async function handleBlocksOperationTx(
978979 // Lock state is admin-only, so an add never writes the lock flag, overwrites a protected
979980 // block or its loop/parallel config, nor places a block inside a locked container.
980981 let reconcile = false
982+ const rejectedBlockIds = new Set < string > ( )
981983
982984 if ( blocks && blocks . length > 0 ) {
983- const allowedBlocks = ( blocks as Array < Record < string , unknown > > ) . filter ( ( block ) => {
984- const parentId = ( block . data as Record < string , unknown > | null ) ?. parentId as
985- | string
986- | undefined
987- if ( isProtected ( block . id as string ) || ( parentId && isProtected ( parentId ) ) ) {
988- logger . info ( `Skipping block ${ block . id } - it or its parent is locked` )
989- return false
990- }
991- return true
992- } )
993- reconcile =
994- allowedBlocks . length < blocks . length ||
995- ( blocks as Array < Record < string , unknown > > ) . some ( ( block ) => block . locked )
985+ const incoming = blocks as Array < Record < string , unknown > >
986+ // Added blocks land unlocked, so one is refused exactly when it is protected now or
987+ // would sit under a locked ancestor once the whole batch lands.
988+ const landed : Record < string , DbBlockRef > = { ...lockBlocks }
989+ for ( const block of incoming ) {
990+ const id = block . id as string
991+ if ( ! isProtected ( id ) ) landed [ id ] = { id, locked : false , data : block . data }
992+ }
993+ for ( const block of incoming ) {
994+ const id = block . id as string
995+ if ( isProtected ( id ) || isWorkflowBlockAncestorLocked ( id , landed ) ) rejectedBlockIds . add ( id )
996+ }
997+ const allowedBlocks = incoming . filter ( ( block ) => ! rejectedBlockIds . has ( block . id as string ) )
998+ if ( rejectedBlockIds . size > 0 ) {
999+ logger . info ( 'Skipping blocks that are locked or inside a locked container' , {
1000+ count : rejectedBlockIds . size ,
1001+ } )
1002+ }
1003+ reconcile = rejectedBlockIds . size > 0 || incoming . some ( ( block ) => block . locked )
9961004
9971005 if ( allowedBlocks . length === 0 ) {
9981006 logger . info ( 'All blocks filtered out due to locks, skipping add' )
@@ -1118,6 +1126,7 @@ async function handleBlocksOperationTx(
11181126 droppedCyclic,
11191127 } )
11201128 }
1129+ if ( droppedCounts [ 'protected target block' ] ) reconcile = true
11211130
11221131 if ( safeEdges . length > 0 ) {
11231132 const edgeValues = safeEdges . map ( ( edge ) => ( {
@@ -1142,7 +1151,7 @@ async function handleBlocksOperationTx(
11421151 } ) ) ,
11431152 ]
11441153 const subflowValues = subflowEntries
1145- . filter ( ( subflow ) => ! isProtected ( subflow . id ) )
1154+ . filter ( ( subflow ) => ! isProtected ( subflow . id ) && ! rejectedBlockIds . has ( subflow . id ) )
11461155 . map ( ( subflow ) => ( {
11471156 ...subflow ,
11481157 workflowId,
0 commit comments