@@ -714,7 +714,8 @@ export interface ResponseGraphBlock {
714714 parentId : string | null
715715 /**
716716 * Set on a condition or router block: the branches a run always takes one of, empty when a run
717- * can take none (a condition without an else branch, or branches never saved).
717+ * can take none (a condition without an else branch, or branches never saved). A condition's
718+ * end at its first else row: the rows after it never run.
718719 */
719720 branches ?: readonly string [ ]
720721}
@@ -729,13 +730,29 @@ export interface ResponseGraphEdge {
729730 port : string
730731}
731732
733+ /** Whether the graph of `successors` (with each target's incoming count in `pending`) has a cycle. */
734+ function hasCycle ( successors : Map < string , string [ ] > , pending : Map < string , number > ) : boolean {
735+ const ready = [ ...successors . keys ( ) ] . filter ( ( id ) => ! pending . has ( id ) )
736+ let visited = 0
737+ for ( let id = ready . pop ( ) ; id !== undefined ; id = ready . pop ( ) ) {
738+ visited += 1
739+ for ( const next of successors . get ( id ) ?? [ ] ) {
740+ const left = ( pending . get ( next ) ?? 0 ) - 1
741+ pending . set ( next , left )
742+ if ( left === 0 ) ready . push ( next )
743+ }
744+ }
745+ const nodes = new Set ( [ ...successors . keys ( ) , ...pending . keys ( ) ] )
746+ return visited < nodes . size
747+ }
748+
732749/**
733750 * Whether every run of a workflow ends at an enabled Response block at its root, so a caller's
734751 * `result` is always that block's output (`{ data, status, headers }`) rather than the output of
735752 * whichever block the run ended at. True when there is a root Response and every other root block
736753 * that runs always goes on to one that runs (its out edge, a container's end edge, or an edge from
737- * every branch it always takes one of): the root graph is acyclic, so every run then ends at a
738- * Response.
754+ * every branch it always takes one of), and the root graph is acyclic: every run then ends at a
755+ * Response. A root cycle never runs (its blocks wait on each other), so it makes this false.
739756 */
740757export function alwaysEndsAtResponse (
741758 blocks : Readonly < Record < string , ResponseGraphBlock > > ,
@@ -745,13 +762,22 @@ export function alwaysEndsAtResponse(
745762 const block = Object . hasOwn ( blocks , id ) ? blocks [ id ] : undefined
746763 return block ?. enabled === true && ! isWorkflowAnnotationOnlyBlockType ( block . type )
747764 }
765+ const isRoot = ( id : string ) => blocks [ id ] ?. parentId === null
748766 const portsBySource = new Map < string , Set < string > > ( )
767+ const successors = new Map < string , string [ ] > ( )
768+ const pending = new Map < string , number > ( )
749769 for ( const edge of edges ) {
750770 if ( ! runs ( edge . source ) || ! runs ( edge . target ) ) continue
751771 const ports = portsBySource . get ( edge . source ) ?? new Set < string > ( )
752772 ports . add ( edge . port )
753773 portsBySource . set ( edge . source , ports )
774+ if ( ! isRoot ( edge . source ) || ! isRoot ( edge . target ) ) continue
775+ const next = successors . get ( edge . source ) ?? [ ]
776+ next . push ( edge . target )
777+ successors . set ( edge . source , next )
778+ pending . set ( edge . target , ( pending . get ( edge . target ) ?? 0 ) + 1 )
754779 }
780+ if ( hasCycle ( successors , pending ) ) return false
755781 let responds = false
756782 for ( const [ id , block ] of Object . entries ( blocks ) ) {
757783 if ( ! runs ( id ) || block . parentId !== null ) continue
0 commit comments