LCOV - code coverage report
Current view: top level - src/core/graph/subgraph - liftNodeErrorsToBoundary.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 96.2 % 53 51
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { groupBy, partition } from 'es-toolkit'
       2              : 
       3              : import type { LGraph } from '@/lib/litegraph/src/litegraph'
       4              : import type { NodeError } from '@comfyorg/api-client/types'
       5              : import { tryNormalizeNodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'
       6              : import type { NodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'
       7              : import { isNodeLevelValidationError } from '@/utils/executionErrorUtil'
       8              : import type { NodeValidationError } from '@/utils/executionErrorUtil'
       9              : import { getNodeByExecutionId } from '@/utils/graphTraversalUtil'
      10              : import { isSubgraph } from '@/utils/typeGuardUtil'
      11              : 
      12              : export interface LiftedErrorExtraInfo {
      13              :   input_name: string
      14              :   source_execution_id: string
      15              :   source_input_name: string
      16              : }
      17              : 
      18              : export interface LiftedSurface {
      19              :   hostExecId: NodeExecutionId
      20              :   hostInputName: string
      21              : }
      22              : 
      23              : interface ErrorPlacement {
      24              :   kind: 'own' | 'lifted'
      25              :   targetExecId: string
      26              :   error: NodeValidationError
      27              : }
      28              : 
      29           30 : export function getLiftedErrorSource(
      30              :   error: NodeValidationError
      31              : ): LiftedErrorExtraInfo | null {
      32           30 :   const extraInfo = error.extra_info
      33           30 :   if (!extraInfo) return null
      34              : 
      35           30 :   const { input_name, source_execution_id, source_input_name } = extraInfo
      36           30 :   if (
      37              :     typeof input_name !== 'string' ||
      38              :     typeof source_execution_id !== 'string' ||
      39              :     typeof source_input_name !== 'string'
      40              :   ) {
      41           30 :     return null
      42              :   }
      43              : 
      44            0 :   return { input_name, source_execution_id, source_input_name }
      45              : }
      46              : 
      47            2 : function getHostExecutionId(executionId: string): NodeExecutionId | null {
      48            2 :   const separatorIndex = executionId.lastIndexOf(':')
      49            2 :   if (separatorIndex <= 0) return null
      50            2 :   return tryNormalizeNodeExecutionId(executionId.slice(0, separatorIndex))
      51              : }
      52              : 
      53              : /**
      54              :  * Boundary surfaces that expose `(executionId, inputName)`, innermost first.
      55              :  * Walks one host per level and stops at the last resolvable surface, so an
      56              :  * unresolvable deeper host falls back to the shallower one (fail-open).
      57              :  */
      58           59 : export function resolveLiftChain(
      59              :   rootGraph: LGraph,
      60              :   executionId: string,
      61              :   inputName: string
      62              : ): LiftedSurface[] {
      63           59 :   const chain: LiftedSurface[] = []
      64           59 :   let currentExecId = executionId
      65           59 :   let currentInputName = inputName
      66              : 
      67           59 :   for (;;) {
      68           61 :     const node = getNodeByExecutionId(rootGraph, currentExecId)
      69           61 :     const graph = node?.graph
      70           61 :     if (!node || !graph || !isSubgraph(graph)) break
      71              : 
      72            4 :     const slot = node.inputs.find((input) => input.name === currentInputName)
      73           61 :     if (slot?.link == null) break
      74              : 
      75           61 :     const subgraphInput = graph.getLink(slot.link)?.resolve(graph).subgraphInput
      76           61 :     if (!subgraphInput) break
      77              : 
      78            2 :     const hostExecId = getHostExecutionId(currentExecId)
      79           61 :     if (!hostExecId || !getNodeByExecutionId(rootGraph, hostExecId)) break
      80              : 
      81            2 :     chain.push({ hostExecId, hostInputName: subgraphInput.name })
      82            2 :     currentExecId = hostExecId
      83            2 :     currentInputName = subgraphInput.name
      84              :   }
      85              : 
      86           59 :   return chain
      87              : }
      88              : 
      89           59 : function createEmptyNodeError(nodeError: NodeError): NodeError {
      90           59 :   return {
      91              :     ...nodeError,
      92              :     errors: []
      93              :   }
      94              : }
      95              : 
      96              : // Lifted host entries use the host title for display; SubgraphNode.type is a UUID.
      97            2 : function createLiftedHostEntry(
      98              :   rootGraph: LGraph,
      99              :   hostExecId: string
     100              : ): NodeError {
     101            2 :   return {
     102              :     class_type:
     103              :       getNodeByExecutionId(rootGraph, hostExecId)?.title ?? hostExecId,
     104              :     dependent_outputs: [],
     105              :     errors: []
     106              :   }
     107              : }
     108              : 
     109           61 : function toErrorPlacement(
     110              :   rootGraph: LGraph,
     111              :   executionId: string,
     112              :   error: NodeValidationError
     113              : ): ErrorPlacement {
     114           61 :   const inputName = error.extra_info?.input_name
     115           61 :   const surface =
     116              :     inputName && !isNodeLevelValidationError(error)
     117              :       ? resolveLiftChain(rootGraph, executionId, inputName).at(-1)
     118              :       : undefined
     119              : 
     120           61 :   if (!inputName || !surface) {
     121           59 :     return {
     122              :       kind: 'own',
     123              :       targetExecId: executionId,
     124              :       error
     125              :     }
     126              :   }
     127              : 
     128            2 :   const liftedExtraInfo: LiftedErrorExtraInfo = {
     129              :     input_name: surface.hostInputName,
     130              :     source_execution_id: executionId,
     131              :     source_input_name: inputName
     132              :   }
     133              : 
     134            2 :   return {
     135              :     kind: 'lifted',
     136              :     targetExecId: surface.hostExecId,
     137              :     error: {
     138              :       ...error,
     139              :       extra_info: {
     140              :         ...error.extra_info,
     141              :         ...liftedExtraInfo
     142              :       }
     143              :     }
     144              :   }
     145              : }
     146              : 
     147           61 : export function liftNodeErrorsToBoundary(
     148              :   rootGraph: LGraph,
     149              :   nodeErrors: Record<string, NodeError>
     150              : ): Record<string, NodeError> {
     151           61 :   const output: Record<string, NodeError> = {}
     152           61 :   const placements = Object.entries(nodeErrors).flatMap(
     153              :     ([executionId, nodeError]) =>
     154              :       nodeError.errors.map((error) =>
     155              :         toErrorPlacement(rootGraph, executionId, error)
     156              :       )
     157              :   )
     158              : 
     159           61 :   for (const [executionId, nodeError] of Object.entries(nodeErrors)) {
     160           61 :     if (nodeError.errors.length === 0) {
     161            0 :       output[executionId] = createEmptyNodeError(nodeError)
     162              :     }
     163              :   }
     164              : 
     165           61 :   const placementsByTarget = groupBy(
     166              :     placements,
     167              :     (placement) => placement.targetExecId
     168              :   )
     169              : 
     170           61 :   for (const [targetExecId, targetPlacements] of Object.entries(
     171              :     placementsByTarget
     172              :   )) {
     173           61 :     const existingEntry = Object.hasOwn(nodeErrors, targetExecId)
     174              :       ? nodeErrors[targetExecId]
     175              :       : undefined
     176           61 :     const baseEntry = existingEntry
     177              :       ? createEmptyNodeError(existingEntry)
     178              :       : createLiftedHostEntry(rootGraph, targetExecId)
     179              : 
     180           61 :     const [ownErrors, liftedErrors] = partition(
     181              :       targetPlacements,
     182              :       (placement) => placement.kind === 'own'
     183              :     )
     184              : 
     185           61 :     output[targetExecId] = {
     186              :       ...baseEntry,
     187              :       errors: [...ownErrors, ...liftedErrors].map(
     188              :         (placement) => placement.error
     189              :       )
     190              :     }
     191              :   }
     192              : 
     193           61 :   return output
     194              : }
        

Generated by: LCOV version 2.0-1