LCOV - code coverage report
Current view: top level - src/platform/workflowErrors/panel - useErrorGroups.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 89.4 % 322 288
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { computed, reactive, ref, toValue, watch } from 'vue'
       2              : import type { MaybeRefOrGetter } from 'vue'
       3              : import Fuse from 'fuse.js'
       4              : import type { IFuseOptions } from 'fuse.js'
       5              : import type { Positionable } from '@comfyorg/graph-model/interfaces'
       6              : 
       7              : import { useMissingModelStore } from '@/platform/workflowErrors/missingModel/missingModelStore'
       8              : import { useMissingMediaStore } from '@/platform/workflowErrors/missingMedia/missingMediaStore'
       9              : import { useExecutionErrorStore } from '@/stores/executionErrorStore'
      10              : import { useMissingNodesErrorStore } from '@/platform/workflowErrors/nodeReplacement/missingNodesErrorStore'
      11              : import { useComfyRegistryStore } from '@/stores/comfyRegistryStore'
      12              : import { app } from '@/scripts/app'
      13              : import { isCloud } from '@/platform/distribution/types'
      14              : import { useBootDeploymentLabel } from '@/platform/workspace/composables/useDeploymentLabels'
      15              : import { SubgraphNode } from '@/lib/litegraph/src/litegraph'
      16              : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
      17              : 
      18              : import {
      19              :   getNodeByExecutionId,
      20              :   getExecutionIdByNode
      21              : } from '@/utils/graphTraversalUtil'
      22              : import { createCancelToken } from '@/utils/createCancelToken'
      23              : import { resolveNodeDisplayName } from '@/utils/nodeTitleUtil'
      24              : import { isLGraphNode } from '@/utils/litegraphUtil'
      25              : import type { MissingNodeType } from '@/platform/workflowErrors/nodeReplacement/types'
      26              : import type { ErrorCardData, ErrorGroup, ErrorItem } from './types'
      27              : import { shouldRenderExecutionItemList } from './executionItemList'
      28              : import { someNodeTypeInSelection } from './selectionEmphasis'
      29              : import type { NodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'
      30              : import type { MissingModelGroup } from '@/platform/workflowErrors/missingModel/types'
      31              : import type { ResolvedCatalogErrorMessage } from '@/platform/workflowErrors/errorCatalog/types'
      32              : import type { MissingMediaGroup } from '@/platform/workflowErrors/missingMedia/types'
      33              : import {
      34              :   countMissingModels,
      35              :   groupMissingModelCandidates
      36              : } from '@/platform/workflowErrors/missingModel/missingModelGrouping'
      37              : import { groupCandidatesByMediaType } from '@/platform/workflowErrors/missingMedia/missingMediaScan'
      38              : import { countMissingMediaReferences } from '@/platform/workflowErrors/missingMedia/missingMediaGrouping'
      39              : import {
      40              :   resolveMissingErrorMessage,
      41              :   resolveRunErrorMessage
      42              : } from '@/platform/workflowErrors/errorCatalog/errorMessageResolver'
      43              : import {
      44              :   compareExecutionId,
      45              :   tryNormalizeNodeExecutionId
      46              : } from '@comfyorg/graph-model/nodeIdentification'
      47              : 
      48              : import type { MissingResourceAbsorption } from '@/utils/missingResourceAbsorption'
      49              : import { useErrorClassification } from './useErrorClassification'
      50              : 
      51         2355 : const PROMPT_CARD_ID = '__prompt__'
      52              : 
      53              : const AGENT_PROMPT_ERROR_TYPE_LIST = [
      54              :   'agent_api_failed',
      55              :   'op_rejected',
      56              :   'prefix_abort',
      57              :   'guard_trip',
      58              :   'apply_failed'
      59              : ] as const
      60              : 
      61              : type AgentPromptErrorType = (typeof AGENT_PROMPT_ERROR_TYPE_LIST)[number]
      62              : 
      63         2355 : const AGENT_PROMPT_ERROR_TYPES: ReadonlySet<string> = new Set(
      64              :   AGENT_PROMPT_ERROR_TYPE_LIST
      65              : )
      66              : 
      67            2 : function isAgentPromptErrorType(
      68              :   errorType: string
      69              : ): errorType is AgentPromptErrorType {
      70            2 :   return AGENT_PROMPT_ERROR_TYPES.has(errorType)
      71              : }
      72              : 
      73              : /** Sentinel: distinguishes "fetch in-flight" from "fetch done, pack not found (null)". */
      74         2355 : const RESOLVING = '__RESOLVING__'
      75              : 
      76              : export interface MissingPackGroup {
      77              :   packId: string | null
      78              :   nodeTypes: MissingNodeType[]
      79              :   isResolving: boolean
      80              : }
      81              : 
      82              : export interface SwapNodeGroup {
      83              :   type: string
      84              :   newNodeId: string | undefined
      85              :   nodeTypes: MissingNodeType[]
      86              : }
      87              : 
      88              : interface GroupEntry {
      89              :   type: 'execution'
      90              :   displayTitle: string
      91              :   displayMessage?: string
      92              :   priority: number
      93              :   cards: Map<string, ErrorCardData>
      94              : }
      95              : 
      96              : interface ErrorSearchItem {
      97              :   groupIndex: number
      98              :   cardIndex: number
      99              :   searchableNodeId: string
     100              :   searchableNodeTitle: string
     101              :   searchableRawMessage: string
     102              :   searchableRawDetails: string
     103              :   searchableMessage: string
     104              :   searchableDetails: string
     105              : }
     106              : 
     107              : type CataloguedErrorItem = ErrorItem & ResolvedCatalogErrorMessage
     108              : 
     109              : /** Resolve display info for a node by its execution ID. */
     110           77 : function resolveNodeInfo(nodeId: NodeExecutionId) {
     111           77 :   const rootGraph = app.rootGraphOrUndefined
     112           77 :   const graphNode = rootGraph ? getNodeByExecutionId(rootGraph, nodeId) : null
     113              : 
     114           77 :   return {
     115              :     title: resolveNodeDisplayName(graphNode, {
     116              :       emptyLabel: '',
     117              :       untitledLabel: ''
     118              :     }),
     119              :     graphNodeId: graphNode ? String(graphNode.id) : undefined
     120              :   }
     121              : }
     122              : 
     123           34 : function getOrCreateGroup(
     124              :   groupsMap: Map<string, GroupEntry>,
     125              :   groupKey: string,
     126              :   displayTitle = groupKey,
     127              :   priority = 1,
     128              :   displayMessage?: string
     129              : ): Map<string, ErrorCardData> {
     130           34 :   let entry = groupsMap.get(groupKey)
     131           34 :   if (!entry) {
     132           34 :     entry = {
     133              :       type: 'execution',
     134              :       displayTitle,
     135              :       displayMessage,
     136              :       priority,
     137              :       cards: new Map()
     138              :     }
     139           34 :     groupsMap.set(groupKey, entry)
     140            0 :   } else if (!entry.displayMessage && displayMessage) {
     141            0 :     entry.displayMessage = displayMessage
     142              :   }
     143           34 :   return entry.cards
     144              : }
     145              : 
     146           32 : function createErrorCard(
     147              :   nodeId: NodeExecutionId,
     148              :   classType: string,
     149              :   idPrefix: string
     150              : ): ErrorCardData {
     151           32 :   const nodeInfo = resolveNodeInfo(nodeId)
     152           32 :   return {
     153              :     id: `${idPrefix}-${nodeId}`,
     154              :     title: classType,
     155              :     nodeId,
     156              :     nodeTitle: nodeInfo.title,
     157              :     graphNodeId: nodeInfo.graphNodeId,
     158              :     errors: []
     159              :   }
     160              : }
     161              : 
     162            0 : function compareNodeId(a: ErrorCardData, b: ErrorCardData): number {
     163            0 :   return compareExecutionId(a.nodeId, b.nodeId)
     164              : }
     165              : 
     166           35 : function countExecutionCards(cards: ErrorCardData[]): number {
     167           35 :   if (shouldRenderExecutionItemList(cards)) {
     168           17 :     return cards.reduce((count, card) => count + card.errors.length, 0)
     169              :   }
     170              : 
     171           18 :   return cards.length
     172              : }
     173              : 
     174          589 : function toSortedGroups(groupsMap: Map<string, GroupEntry>): ErrorGroup[] {
     175          589 :   return Array.from(groupsMap.entries())
     176              :     .map(([rawGroupKey, groupData]) => {
     177           34 :       const cards = Array.from(groupData.cards.values()).sort(compareNodeId)
     178           34 :       return {
     179              :         type: 'execution' as const,
     180              :         severity: 'error' as const,
     181              :         groupKey: `execution:${rawGroupKey}`,
     182              :         catalogId: rawGroupKey,
     183              :         displayTitle: groupData.displayTitle,
     184              :         displayMessage: groupData.displayMessage,
     185              :         count: countExecutionCards(cards),
     186              :         cards,
     187              :         priority: groupData.priority,
     188              :         blockedLastRun: false
     189              :       }
     190              :     })
     191              :     .sort((a, b) => {
     192            0 :       if (a.priority !== b.priority) return a.priority - b.priority
     193            0 :       return a.displayTitle.localeCompare(b.displayTitle)
     194              :     })
     195              : }
     196              : 
     197          107 : function searchErrorGroups(groups: ErrorGroup[], query: string) {
     198          107 :   if (!query) return groups
     199              : 
     200            1 :   const searchableList: ErrorSearchItem[] = []
     201            1 :   for (let gi = 0; gi < groups.length; gi++) {
     202            1 :     const group = groups[gi]
     203            1 :     if (group.type !== 'execution') continue
     204            1 :     for (let ci = 0; ci < group.cards.length; ci++) {
     205            1 :       const card = group.cards[ci]
     206            1 :       searchableList.push({
     207              :         groupIndex: gi,
     208              :         cardIndex: ci,
     209              :         searchableNodeId: card.nodeId ?? '',
     210              :         searchableNodeTitle: card.nodeTitle ?? '',
     211              :         searchableRawMessage: card.errors.map((e) => e.message).join(' '),
     212              :         searchableRawDetails: card.errors
     213              :           .map((e) => e.details)
     214              :           .filter(Boolean)
     215              :           .join(' '),
     216              :         searchableMessage: card.errors
     217              :           .map((e) =>
     218              :             [e.displayTitle, e.displayMessage, e.message]
     219              :               .filter(Boolean)
     220              :               .join(' ')
     221              :           )
     222              :           .join(' '),
     223              :         searchableDetails: card.errors
     224              :           .map((e) => [e.displayDetails, e.details].filter(Boolean).join(' '))
     225              :           .join(' ')
     226              :       })
     227              :     }
     228              :   }
     229              : 
     230              :   const fuseOptions: IFuseOptions<ErrorSearchItem> = {
     231              :     keys: [
     232              :       { name: 'searchableRawMessage', weight: 0.3 },
     233              :       { name: 'searchableNodeId', weight: 0.2 },
     234              :       { name: 'searchableNodeTitle', weight: 0.2 },
     235              :       { name: 'searchableMessage', weight: 0.2 },
     236              :       { name: 'searchableRawDetails', weight: 0.1 },
     237              :       { name: 'searchableDetails', weight: 0.1 }
     238              :     ],
     239              :     threshold: 0.3
     240              :   }
     241              : 
     242              :   const fuse = new Fuse(searchableList, fuseOptions)
     243            1 :   const results = fuse.search(query)
     244              : 
     245            1 :   const matchedCardKeys = new Set(
     246              :     results.map((r) => `${r.item.groupIndex}:${r.item.cardIndex}`)
     247              :   )
     248              : 
     249            1 :   return groups
     250              :     .map((group, gi) => {
     251            1 :       if (group.type !== 'execution') return group
     252            1 :       const cards = group.cards.filter((_: ErrorCardData, ci: number) =>
     253              :         matchedCardKeys.has(`${gi}:${ci}`)
     254              :       )
     255            1 :       return {
     256              :         ...group,
     257              :         cards,
     258              :         count: countExecutionCards(cards)
     259              :       }
     260              :     })
     261              :     .filter((group) => group.type !== 'execution' || group.cards.length > 0)
     262              : }
     263              : 
     264         2244 : export function useErrorGroups(
     265              :   searchQuery: MaybeRefOrGetter<string>,
     266              :   selectedItems: MaybeRefOrGetter<readonly Positionable[]> = []
     267              : ) {
     268         2244 :   const executionErrorStore = useExecutionErrorStore()
     269         2244 :   const missingNodesStore = useMissingNodesErrorStore()
     270         2244 :   const missingModelStore = useMissingModelStore()
     271         2244 :   const missingMediaStore = useMissingMediaStore()
     272         2244 :   const { inferPackFromNodeName } = useComfyRegistryStore()
     273         2244 :   const collapseState = reactive<Record<string, boolean>>({})
     274              : 
     275         2244 :   const errorClassification = useErrorClassification()
     276              :   const deploymentLabel = isCloud ? useBootDeploymentLabel() : undefined
     277              : 
     278         2244 :   const selectedNodeInfo = computed(() => {
     279          112 :     const rootGraph = app.rootGraphOrUndefined
     280          112 :     const items = toValue(selectedItems)
     281          112 :     const nodeIds = new Set<string>()
     282          112 :     const containerExecutionIds = new Set<NodeExecutionId>()
     283              : 
     284          112 :     for (const item of items) {
     285           25 :       if (!isLGraphNode(item)) continue
     286           25 :       nodeIds.add(String(item.id))
     287           25 :       if (rootGraph && item instanceof SubgraphNode) {
     288            3 :         const execId = getExecutionIdByNode(rootGraph, item)
     289            3 :         if (execId) containerExecutionIds.add(execId)
     290              :       }
     291              :     }
     292              : 
     293          112 :     return {
     294              :       nodeIds: nodeIds.size > 0 ? nodeIds : null,
     295              :       containerExecutionIds
     296              :     }
     297              :   })
     298              : 
     299         2244 :   const hasSelection = computed(() => selectedNodeInfo.value.nodeIds !== null)
     300              : 
     301         2244 :   const selectedNodeCount = computed(
     302              :     () => selectedNodeInfo.value.nodeIds?.size ?? 0
     303              :   )
     304              : 
     305         2244 :   const selectedNodeTitle = computed(() => {
     306          111 :     if (selectedNodeCount.value !== 1) return null
     307           19 :     const node = toValue(selectedItems).find(isLGraphNode)
     308           63 :     if (!node) return null
     309          111 :     return (
     310              :       resolveNodeDisplayName(node, {
     311              :         emptyLabel: '',
     312              :         untitledLabel: ''
     313              :       }) || null
     314              :     )
     315              :   })
     316              : 
     317         2244 :   const errorNodeCache = computed(() => {
     318            0 :     const map = new Map<string, LGraphNode>()
     319            0 :     const rootGraph = app.rootGraphOrUndefined
     320            0 :     if (!rootGraph) return map
     321            0 :     for (const execId of executionErrorStore.allErrorExecutionIds) {
     322            0 :       const node = getNodeByExecutionId(rootGraph, execId)
     323            0 :       if (node) map.set(execId, node)
     324              :     }
     325            0 :     return map
     326              :   })
     327              : 
     328         2244 :   const missingNodeCache = computed(() => {
     329           23 :     const map = new Map<string, LGraphNode>()
     330           23 :     const rootGraph = app.rootGraphOrUndefined
     331           23 :     if (!rootGraph) return map
     332           23 :     const nodeTypes =
     333              :       missingNodesStore.visibleMissingNodesError?.nodeTypes ?? []
     334           23 :     for (const nodeType of nodeTypes) {
     335            6 :       if (typeof nodeType === 'string') continue
     336            6 :       if (nodeType.nodeId == null) continue
     337            6 :       const nodeId = String(nodeType.nodeId)
     338            6 :       const node = getNodeByExecutionId(rootGraph, nodeId)
     339            6 :       if (node) map.set(nodeId, node)
     340              :     }
     341           23 :     return map
     342              :   })
     343              : 
     344            0 :   function isErrorInSelection(executionNodeId: NodeExecutionId): boolean {
     345            0 :     const nodeIds = selectedNodeInfo.value.nodeIds
     346            0 :     if (!nodeIds) return true
     347              : 
     348            0 :     const graphNode = errorNodeCache.value.get(executionNodeId)
     349            0 :     if (graphNode && nodeIds.has(String(graphNode.id))) return true
     350              : 
     351            0 :     for (const containerExecId of selectedNodeInfo.value
     352              :       .containerExecutionIds) {
     353            0 :       if (executionNodeId.startsWith(`${containerExecId}:`)) return true
     354              :     }
     355              : 
     356            0 :     return false
     357              :   }
     358              : 
     359           32 :   function addNodeErrorToGroup(
     360              :     groupsMap: Map<string, GroupEntry>,
     361              :     nodeId: NodeExecutionId,
     362              :     classType: string,
     363              :     idPrefix: string,
     364              :     error: CataloguedErrorItem,
     365              :     filterBySelection = false
     366              :   ) {
     367           32 :     if (filterBySelection && !isErrorInSelection(nodeId)) return
     368           32 :     const cards = getOrCreateGroup(
     369              :       groupsMap,
     370              :       error.catalogId,
     371              :       error.displayTitle ?? classType,
     372              :       1,
     373              :       error.displayMessage
     374              :     )
     375           32 :     if (!cards.has(nodeId)) {
     376           32 :       cards.set(nodeId, createErrorCard(nodeId, classType, idPrefix))
     377              :     }
     378           32 :     const card = cards.get(nodeId)
     379           32 :     if (!card) return
     380           32 :     card.errors.push(error)
     381              :   }
     382              : 
     383            0 :   function addUnlocatedErrorToGroup(
     384              :     groupsMap: Map<string, GroupEntry>,
     385              :     cardId: string,
     386              :     classType: string,
     387              :     error: CataloguedErrorItem,
     388              :     rawNodeId?: string
     389              :   ) {
     390            0 :     const cards = getOrCreateGroup(
     391              :       groupsMap,
     392              :       error.catalogId,
     393              :       error.displayTitle ?? classType,
     394              :       1,
     395              :       error.displayMessage
     396              :     )
     397            0 :     if (!cards.has(cardId)) {
     398            0 :       cards.set(cardId, {
     399              :         id: cardId,
     400              :         rawNodeId,
     401              :         title: classType,
     402              :         errors: []
     403              :       })
     404              :     }
     405            0 :     cards.get(cardId)?.errors.push(error)
     406              :   }
     407              : 
     408          589 :   function processPromptError(
     409              :     groupsMap: Map<string, GroupEntry>,
     410              :     filterBySelection = false
     411              :   ): boolean {
     412          589 :     const classifiedPromptError = errorClassification.value.promptError
     413          589 :     if (
     414              :       (filterBySelection && selectedNodeInfo.value.nodeIds) ||
     415              :       !classifiedPromptError
     416              :     )
     417          584 :       return false
     418              : 
     419            5 :     const { error, isAbsorbed } = classifiedPromptError
     420           59 :     if (isAbsorbed) return true
     421              : 
     422            2 :     const resolvedDisplay = resolveRunErrorMessage({
     423              :       kind: 'prompt',
     424              :       error,
     425              :       isCloud
     426              :     })
     427          589 :     const groupDisplayTitle = resolvedDisplay.displayTitle ?? error.message
     428              :     const cards = getOrCreateGroup(
     429              :       groupsMap,
     430              :       `prompt:${error.type}`,
     431              :       groupDisplayTitle,
     432              :       0,
     433              :       resolvedDisplay.displayMessage
     434              :     )
     435              : 
     436              :     // Prompt errors are not tied to a node, so they bypass addNodeErrorToGroup.
     437          589 :     cards.set(PROMPT_CARD_ID, {
     438              :       id: PROMPT_CARD_ID,
     439              :       title: groupDisplayTitle,
     440              :       errors: [
     441              :         {
     442              :           message: error.message,
     443              :           ...(isAgentPromptErrorType(error.type)
     444              :             ? { details: error.details }
     445              :             : {}),
     446              :           ...resolvedDisplay
     447              :         }
     448              :       ]
     449              :     })
     450          589 :     return false
     451              :   }
     452              : 
     453          589 :   function processNodeErrors(
     454              :     groupsMap: Map<string, GroupEntry>,
     455              :     filterBySelection = false
     456              :   ): Set<MissingResourceAbsorption> {
     457          589 :     const blockedMissingGroups = new Set<MissingResourceAbsorption>()
     458              : 
     459          589 :     for (const classifiedNodeError of errorClassification.value.nodeErrors) {
     460           30 :       const { rawNodeId, nodeId, nodeError } = classifiedNodeError
     461           30 :       if (filterBySelection && !nodeId) continue
     462           30 :       const nodeDisplayName = nodeId
     463              :         ? resolveNodeInfo(nodeId).title || nodeError.class_type
     464              :         : nodeError.class_type
     465              : 
     466           30 :       for (const { error, absorption } of classifiedNodeError.errors) {
     467           30 :         if (absorption) {
     468           13 :           blockedMissingGroups.add(absorption)
     469           13 :           continue
     470              :         }
     471              : 
     472           17 :         const cataloguedError = {
     473              :           message: error.message,
     474              :           details: error.details,
     475              :           ...resolveRunErrorMessage({
     476              :             kind: 'node_validation',
     477              :             error,
     478              :             nodeDisplayName
     479              :           })
     480              :         }
     481           17 :         if (!nodeId) {
     482            0 :           addUnlocatedErrorToGroup(
     483              :             groupsMap,
     484              :             `node-${rawNodeId}`,
     485              :             nodeError.class_type,
     486              :             cataloguedError,
     487              :             rawNodeId
     488              :           )
     489            0 :           continue
     490              :         }
     491           17 :         addNodeErrorToGroup(
     492              :           groupsMap,
     493              :           nodeId,
     494              :           nodeError.class_type,
     495              :           'node',
     496              :           cataloguedError,
     497              :           filterBySelection
     498              :         )
     499              :       }
     500              :     }
     501              : 
     502          589 :     return blockedMissingGroups
     503              :   }
     504              : 
     505          589 :   function processExecutionError(
     506              :     groupsMap: Map<string, GroupEntry>,
     507              :     filterBySelection = false
     508              :   ) {
     509          589 :     const classifiedExecutionError = errorClassification.value.executionError
     510          589 :     if (!classifiedExecutionError) return
     511              : 
     512           15 :     const { error, nodeId, rawNodeId } = classifiedExecutionError
     513          589 :     if (filterBySelection && !nodeId) return
     514          589 :     const cataloguedError = {
     515              :       message: `${error.exception_type}: ${error.exception_message}`,
     516              :       details: error.traceback.join('\n'),
     517              :       isRuntimeError: true,
     518              :       exceptionType: error.exception_type,
     519              :       ...resolveRunErrorMessage({
     520              :         kind: 'execution',
     521              :         error,
     522              :         nodeDisplayName: nodeId
     523              :           ? resolveNodeInfo(nodeId).title || error.node_type
     524              :           : error.node_type
     525              :       })
     526              :     }
     527          589 :     if (nodeId) {
     528           15 :       addNodeErrorToGroup(
     529              :         groupsMap,
     530              :         nodeId,
     531              :         error.node_type,
     532              :         'exec',
     533              :         cataloguedError,
     534              :         filterBySelection
     535              :       )
     536              :     } else {
     537            0 :       addUnlocatedErrorToGroup(
     538              :         groupsMap,
     539              :         `exec-${error.node_id}`,
     540              :         error.node_type,
     541              :         cataloguedError,
     542              :         rawNodeId
     543              :       )
     544              :     }
     545              :   }
     546              : 
     547              :   // Async pack-ID resolution for missing node types that lack a cnrId
     548         2244 :   const asyncResolvedIds = ref<Map<string, string | null>>(new Map())
     549              : 
     550         2244 :   const pendingTypes = computed(() =>
     551              :     (missingNodesStore.missingNodesError?.nodeTypes ?? []).filter(
     552              :       (n): n is Exclude<MissingNodeType, string> =>
     553              :         typeof n !== 'string' && !n.cnrId
     554              :     )
     555              :   )
     556              : 
     557         2244 :   watch(
     558              :     pendingTypes,
     559              :     async (pending, _, onCleanup) => {
     560         2322 :       const toResolve = pending.filter(
     561              :         (n) => asyncResolvedIds.value.get(n.type) === undefined
     562              :       )
     563         2322 :       if (!toResolve.length) return
     564              : 
     565           81 :       const resolvingTypes = toResolve.map((n) => n.type)
     566           81 :       const { cancel, isCancelled } = createCancelToken()
     567           81 :       onCleanup(() => {
     568           40 :         cancel()
     569           40 :         const next = new Map(asyncResolvedIds.value)
     570           40 :         for (const type of resolvingTypes) {
     571           52 :           if (next.get(type) === RESOLVING) next.delete(type)
     572              :         }
     573           40 :         asyncResolvedIds.value = next
     574              :       })
     575              : 
     576           81 :       const updated = new Map(asyncResolvedIds.value)
     577          902 :       for (const type of resolvingTypes) updated.set(type, RESOLVING)
     578           81 :       asyncResolvedIds.value = updated
     579              : 
     580           81 :       const results = await Promise.allSettled(
     581              :         toResolve.map(async (n) => ({
     582              :           type: n.type,
     583              :           packId: (await inferPackFromNodeName.call(n.type))?.id ?? null
     584              :         }))
     585              :       )
     586         1927 :       if (isCancelled()) return
     587              : 
     588           80 :       const final = new Map(asyncResolvedIds.value)
     589           94 :       for (const r of results) {
     590          103 :         if (r.status === 'fulfilled') {
     591          103 :           final.set(r.value.type, r.value.packId)
     592              :         } else {
     593            0 :           console.warn('Failed to resolve pack ID:', r.reason)
     594              :         }
     595              :       }
     596              :       // Clear any remaining RESOLVING markers for failed lookups
     597           94 :       for (const type of resolvingTypes) {
     598          103 :         if (final.get(type) === RESOLVING) final.set(type, null)
     599              :       }
     600           80 :       asyncResolvedIds.value = final
     601              :     },
     602              :     { immediate: true }
     603              :   )
     604              : 
     605              :   // Evict stale entries when missing nodes are cleared
     606         2244 :   watch(
     607              :     () => missingNodesStore.missingNodesError,
     608              :     (error) => {
     609           78 :       if (!error && asyncResolvedIds.value.size > 0) {
     610           19 :         asyncResolvedIds.value = new Map()
     611              :       }
     612              :     }
     613              :   )
     614              : 
     615         2244 :   const missingPackGroups = computed<MissingPackGroup[]>(() => {
     616          434 :     const nodeTypes =
     617              :       missingNodesStore.visibleMissingNodesError?.nodeTypes ?? []
     618          434 :     const map = new Map<
     619              :       string | null,
     620              :       { nodeTypes: MissingNodeType[]; isResolving: boolean }
     621              :     >()
     622          434 :     const resolvingKeys = new Set<string | null>()
     623              : 
     624          434 :     for (const nodeType of nodeTypes) {
     625          133 :       if (typeof nodeType !== 'string' && nodeType.isReplaceable) continue
     626              : 
     627           79 :       let packId: string | null
     628              : 
     629           79 :       if (typeof nodeType === 'string') {
     630            0 :         packId = null
     631           79 :       } else if (nodeType.cnrId) {
     632            6 :         packId = nodeType.cnrId
     633              :       } else {
     634           73 :         const resolved = asyncResolvedIds.value.get(nodeType.type)
     635           73 :         if (resolved === undefined || resolved === RESOLVING) {
     636           35 :           packId = null
     637           35 :           resolvingKeys.add(null)
     638              :         } else {
     639           38 :           packId = resolved
     640              :         }
     641              :       }
     642              : 
     643           79 :       const existing = map.get(packId)
     644           79 :       if (existing) {
     645            4 :         existing.nodeTypes.push(nodeType)
     646              :       } else {
     647           75 :         map.set(packId, { nodeTypes: [nodeType], isResolving: false })
     648              :       }
     649              :     }
     650              : 
     651          434 :     for (const key of resolvingKeys) {
     652           34 :       const group = map.get(key)
     653           34 :       if (group) group.isResolving = true
     654              :     }
     655              : 
     656          434 :     return Array.from(map.entries())
     657              :       .sort(([packIdA], [packIdB]) => {
     658              :         // null (Unknown Pack) always goes last
     659            0 :         if (packIdA === null) return 1
     660            0 :         if (packIdB === null) return -1
     661            0 :         return packIdA.localeCompare(packIdB)
     662              :       })
     663              :       .map(([packId, { nodeTypes, isResolving }]) => ({
     664              :         packId,
     665              :         nodeTypes: [...nodeTypes].sort((a, b) => {
     666            4 :           const typeA = typeof a === 'string' ? a : a.type
     667            4 :           const typeB = typeof b === 'string' ? b : b.type
     668            4 :           const typeCmp = typeA.localeCompare(typeB)
     669            4 :           if (typeCmp !== 0) return typeCmp
     670            4 :           const idA = typeof a === 'string' ? '' : String(a.nodeId ?? '')
     671            4 :           const idB = typeof b === 'string' ? '' : String(b.nodeId ?? '')
     672            4 :           return idA.localeCompare(idB, undefined, { numeric: true })
     673              :         }),
     674              :         isResolving
     675              :       }))
     676              :   })
     677              : 
     678         2244 :   const swapNodeGroups = computed<SwapNodeGroup[]>(() => {
     679          399 :     const nodeTypes =
     680              :       missingNodesStore.visibleMissingNodesError?.nodeTypes ?? []
     681          399 :     const map = new Map<string, SwapNodeGroup>()
     682              : 
     683          399 :     for (const nodeType of nodeTypes) {
     684           97 :       if (typeof nodeType === 'string' || !nodeType.isReplaceable) continue
     685              : 
     686           54 :       const typeName = nodeType.type
     687           54 :       const existing = map.get(typeName)
     688           54 :       if (existing) {
     689            4 :         existing.nodeTypes.push(nodeType)
     690              :       } else {
     691           50 :         map.set(typeName, {
     692              :           type: typeName,
     693              :           newNodeId: nodeType.replacement?.new_node_id,
     694              :           nodeTypes: [nodeType]
     695              :         })
     696              :       }
     697              :     }
     698              : 
     699          399 :     return Array.from(map.values()).sort((a, b) => a.type.localeCompare(b.type))
     700              :   })
     701              : 
     702              :   /**
     703              :    * Builds ErrorGroups from missingNodesError. Returns [] when none present.
     704              :    * `includeGroup` narrows which swap/pack groups are counted (used to scope
     705              :    * emphasis to the canvas selection); groups reduced to zero are omitted.
     706              :    */
     707          589 :   function buildMissingNodeGroups(
     708              :     blockedLastRun: boolean,
     709              :     includeGroup: (nodeTypes: MissingNodeType[]) => boolean = () => true
     710              :   ): ErrorGroup[] {
     711          589 :     const error = missingNodesStore.visibleMissingNodesError
     712          589 :     if (!error) return []
     713              : 
     714          117 :     const groups: ErrorGroup[] = []
     715          117 :     const swapCount = swapNodeGroups.value.filter((group) =>
     716              :       includeGroup(group.nodeTypes)
     717              :     ).length
     718          117 :     const packCount = missingPackGroups.value.filter((group) =>
     719              :       includeGroup(group.nodeTypes)
     720              :     ).length
     721              : 
     722          205 :     if (swapCount > 0) {
     723           38 :       groups.push({
     724              :         type: 'swap_nodes' as const,
     725              :         severity: 'missing' as const,
     726              :         groupKey: 'swap_nodes',
     727              :         count: swapCount,
     728              :         priority: 0,
     729              :         blockedLastRun,
     730              :         ...resolveMissingErrorMessage({
     731              :           kind: 'swap_nodes',
     732              :           nodeTypes: error.nodeTypes,
     733              :           count: swapCount,
     734              :           isCloud
     735              :         })
     736              :       })
     737              :     }
     738              : 
     739          149 :     if (packCount > 0) {
     740           78 :       groups.push({
     741              :         type: 'missing_node' as const,
     742              :         severity: 'missing' as const,
     743              :         groupKey: 'missing_node',
     744              :         count: packCount,
     745              :         priority: 1,
     746              :         blockedLastRun,
     747              :         ...resolveMissingErrorMessage({
     748              :           kind: 'missing_node',
     749              :           nodeTypes: error.nodeTypes,
     750              :           count: packCount,
     751              :           isCloud,
     752              :           deploymentLabel: deploymentLabel?.value
     753              :         })
     754              :       })
     755              :     }
     756              : 
     757          117 :     return groups.sort((a, b) => a.priority - b.priority)
     758              :   }
     759              : 
     760         2244 :   const missingModelGroups = computed<MissingModelGroup[]>(() => {
     761          512 :     return groupMissingModelCandidates(
     762              :       missingModelStore.visibleMissingModelCandidates,
     763              :       isCloud
     764              :     )
     765              :   })
     766              : 
     767          567 :   function buildMissingModelGroups(blockedLastRun: boolean): ErrorGroup[] {
     768          567 :     if (!missingModelGroups.value.length) return []
     769          396 :     const count = countMissingModels(missingModelGroups.value)
     770          396 :     return [
     771              :       {
     772              :         type: 'missing_model' as const,
     773              :         severity: 'missing' as const,
     774              :         groupKey: 'missing_model',
     775              :         count,
     776              :         priority: 2,
     777              :         blockedLastRun,
     778              :         ...resolveMissingErrorMessage({
     779              :           kind: 'missing_model',
     780              :           groups: missingModelGroups.value,
     781              :           count,
     782              :           isCloud,
     783              :           deploymentLabel: deploymentLabel?.value
     784              :         })
     785              :       }
     786              :     ]
     787              :   }
     788              : 
     789         2244 :   const missingMediaGroups = computed<MissingMediaGroup[]>(() => {
     790          373 :     const candidates = missingMediaStore.visibleMissingMediaCandidates
     791          373 :     if (!candidates?.length) return []
     792           33 :     return groupCandidatesByMediaType(candidates)
     793              :   })
     794              : 
     795          567 :   function buildMissingMediaGroups(blockedLastRun: boolean): ErrorGroup[] {
     796          567 :     if (!missingMediaGroups.value.length) return []
     797           38 :     const totalRows = countMissingMediaReferences(missingMediaGroups.value)
     798           38 :     return [
     799              :       {
     800              :         type: 'missing_media' as const,
     801              :         severity: 'missing' as const,
     802              :         groupKey: 'missing_media',
     803              :         count: totalRows,
     804              :         priority: 3,
     805              :         blockedLastRun,
     806              :         ...resolveMissingErrorMessage({
     807              :           kind: 'missing_media',
     808              :           groups: missingMediaGroups.value,
     809              :           count: totalRows,
     810              :           isCloud
     811              :         })
     812              :       }
     813              :     ]
     814              :   }
     815              : 
     816           59 :   function isAssetErrorInSelection(executionNodeId: NodeExecutionId): boolean {
     817           59 :     const nodeIds = selectedNodeInfo.value.nodeIds
     818           59 :     if (!nodeIds) return true
     819              : 
     820              :     // Try missing node cache first
     821           59 :     const cachedNode = missingNodeCache.value.get(executionNodeId)
     822           59 :     if (cachedNode && nodeIds.has(String(cachedNode.id))) return true
     823              : 
     824              :     // Resolve from graph for model/media candidates
     825           57 :     const rootGraph = app.rootGraphOrUndefined
     826           59 :     const graphNode = rootGraph
     827              :       ? getNodeByExecutionId(rootGraph, executionNodeId)
     828              :       : null
     829           59 :     if (graphNode && nodeIds.has(String(graphNode.id))) return true
     830              : 
     831           37 :     for (const containerExecId of selectedNodeInfo.value
     832              :       .containerExecutionIds) {
     833            4 :       if (executionNodeId.startsWith(`${containerExecId}:`)) return true
     834              :     }
     835              : 
     836           11 :     return false
     837              :   }
     838              : 
     839           59 :   function isAssetCandidateInSelection(nodeId: string | number): boolean {
     840           59 :     const executionNodeId = tryNormalizeNodeExecutionId(nodeId)
     841           59 :     return executionNodeId ? isAssetErrorInSelection(executionNodeId) : false
     842              :   }
     843              : 
     844         2244 :   const missingModelCandidatesForSelection = computed(() => {
     845           22 :     if (!hasSelection.value) return []
     846           22 :     const candidates = missingModelStore.visibleMissingModelCandidates
     847           22 :     if (!candidates?.length) return []
     848           15 :     return candidates.filter(
     849              :       (c) =>
     850              :         (c.nodeId != null && isAssetCandidateInSelection(c.nodeId)) ||
     851              :         (c.sourceExecutionId != null &&
     852              :           isAssetCandidateInSelection(c.sourceExecutionId)) ||
     853              :         c.promotedSources?.some((source) =>
     854              :           isAssetCandidateInSelection(source.executionId)
     855              :         )
     856              :     )
     857              :   })
     858              : 
     859              :   /** Model groups narrowed to the selection, for emphasis derivation only. */
     860         2244 :   const missingModelGroupsForSelection = computed(() =>
     861              :     groupMissingModelCandidates(
     862              :       missingModelCandidatesForSelection.value,
     863              :       isCloud
     864              :     )
     865              :   )
     866              : 
     867              :   /** Media groups narrowed to the selection, for emphasis derivation only. */
     868         2244 :   const missingMediaGroupsForSelection = computed(() => {
     869           21 :     if (!hasSelection.value) return []
     870           21 :     const candidates = missingMediaStore.visibleMissingMediaCandidates
     871           21 :     if (!candidates?.length) return []
     872            5 :     const matched = candidates.filter((c) =>
     873              :       isAssetCandidateInSelection(c.nodeId)
     874              :     )
     875            5 :     if (!matched.length) return []
     876            5 :     return groupCandidatesByMediaType(matched)
     877              :   })
     878              : 
     879           22 :   function buildMissingModelGroupsForSelection(
     880              :     blockedLastRun: boolean
     881              :   ): ErrorGroup[] {
     882           22 :     if (!missingModelGroupsForSelection.value.length) return []
     883           15 :     const count = countMissingModels(missingModelGroupsForSelection.value)
     884           15 :     return [
     885              :       {
     886              :         type: 'missing_model' as const,
     887              :         severity: 'missing' as const,
     888              :         groupKey: 'missing_model',
     889              :         count,
     890              :         priority: 2,
     891              :         blockedLastRun,
     892              :         ...resolveMissingErrorMessage({
     893              :           kind: 'missing_model',
     894              :           groups: missingModelGroupsForSelection.value,
     895              :           count,
     896              :           isCloud,
     897              :           deploymentLabel: deploymentLabel?.value
     898              :         })
     899              :       }
     900              :     ]
     901              :   }
     902              : 
     903           22 :   function buildMissingMediaGroupsForSelection(
     904              :     blockedLastRun: boolean
     905              :   ): ErrorGroup[] {
     906           22 :     if (!missingMediaGroupsForSelection.value.length) return []
     907            5 :     const totalRows = countMissingMediaReferences(
     908              :       missingMediaGroupsForSelection.value
     909              :     )
     910            5 :     return [
     911              :       {
     912              :         type: 'missing_media' as const,
     913              :         severity: 'missing' as const,
     914              :         groupKey: 'missing_media',
     915              :         count: totalRows,
     916              :         priority: 3,
     917              :         blockedLastRun,
     918              :         ...resolveMissingErrorMessage({
     919              :           kind: 'missing_media',
     920              :           groups: missingMediaGroupsForSelection.value,
     921              :           count: totalRows,
     922              :           isCloud
     923              :         })
     924              :       }
     925              :     ]
     926              :   }
     927              : 
     928         2244 :   const allErrorGroups = computed<ErrorGroup[]>(() => {
     929          567 :     const groupsMap = new Map<string, GroupEntry>()
     930              : 
     931          567 :     const promptErrorAbsorbed = processPromptError(groupsMap)
     932          567 :     const blockedMissingGroups = processNodeErrors(groupsMap)
     933          567 :     processExecutionError(groupsMap)
     934              : 
     935          567 :     return [
     936              :       ...toSortedGroups(groupsMap),
     937              :       ...buildMissingNodeGroups(promptErrorAbsorbed),
     938              :       ...buildMissingModelGroups(blockedMissingGroups.has('missing_model')),
     939              :       ...buildMissingMediaGroups(blockedMissingGroups.has('missing_media'))
     940              :     ]
     941              :   })
     942              : 
     943              :   /**
     944              :    * The subset of error groups whose errors belong to the current canvas
     945              :    * selection. Empty when nothing is selected. Display always shows all
     946              :    * groups; this subset only drives selection emphasis (auto-expand, card
     947              :    * highlight, context strip).
     948              :    */
     949         2244 :   const selectionScopedGroups = computed<ErrorGroup[]>(() => {
     950           22 :     if (!hasSelection.value) return []
     951              : 
     952           22 :     const groupsMap = new Map<string, GroupEntry>()
     953           22 :     void processPromptError(groupsMap, true)
     954           22 :     void processNodeErrors(groupsMap, true)
     955           22 :     processExecutionError(groupsMap, true)
     956              : 
     957              :     // Selection groups drive emphasis only; blockedLastRun cannot be computed
     958              :     // selection-accurately here (absorption is a graph-wide fact), so it is
     959              :     // uniformly false rather than a graph-wide flag masquerading as scoped.
     960           22 :     return [
     961              :       ...toSortedGroups(groupsMap),
     962              :       ...buildMissingNodeGroups(false, (nodeTypes) =>
     963              :         someNodeTypeInSelection(nodeTypes, selectionMatchedAssetNodeIds.value)
     964              :       ),
     965              :       ...buildMissingModelGroupsForSelection(false),
     966              :       ...buildMissingMediaGroupsForSelection(false)
     967              :     ]
     968              :   })
     969              : 
     970              :   /**
     971              :    * Execution node ids referenced by any missing-asset candidate (models,
     972              :    * media, missing node types).
     973              :    */
     974         2244 :   const assetNodeIdsWithError = computed<string[]>(() => {
     975           92 :     const candidateIds = [
     976              :       ...(missingModelStore.visibleMissingModelCandidates ?? []),
     977              :       ...(missingMediaStore.visibleMissingMediaCandidates ?? [])
     978              :     ].map((candidate) => candidate.nodeId)
     979           92 :     const missingNodeTypeIds = (
     980              :       missingNodesStore.visibleMissingNodesError?.nodeTypes ?? []
     981              :     ).map((nodeType) =>
     982              :       typeof nodeType === 'string' ? undefined : nodeType.nodeId
     983              :     )
     984           92 :     return [...candidateIds, ...missingNodeTypeIds]
     985              :       .filter((nodeId) => nodeId != null)
     986              :       .map(String)
     987              :   })
     988              : 
     989              :   /**
     990              :    * Asset node ids that belong to the current selection. Drives row-level
     991              :    * highlighting inside the missing-* cards.
     992              :    */
     993         2244 :   const selectionMatchedAssetNodeIds = computed<Set<string>>(() => {
     994          104 :     if (!hasSelection.value) return new Set()
     995           22 :     const matched = new Set(
     996              :       assetNodeIdsWithError.value.filter(isAssetCandidateInSelection)
     997              :     )
     998           39 :     for (const candidate of missingModelCandidatesForSelection.value) {
     999           19 :       if (candidate.nodeId != null) matched.add(String(candidate.nodeId))
    1000              :     }
    1001           22 :     return matched
    1002              :   })
    1003              : 
    1004         2244 :   const selectionMatchedGroupKeys = computed<Set<string>>(() => {
    1005           22 :     if (!hasSelection.value) return new Set()
    1006           22 :     return new Set(selectionScopedGroups.value.map((group) => group.groupKey))
    1007              :   })
    1008              : 
    1009         2244 :   const selectionMatchedCardIds = computed<Set<string>>(() => {
    1010           29 :     if (!hasSelection.value) return new Set()
    1011           22 :     return new Set(
    1012              :       selectionScopedGroups.value
    1013              :         .flatMap((group) => (group.type === 'execution' ? group.cards : []))
    1014              :         .map((card) => card.id)
    1015              :     )
    1016              :   })
    1017              : 
    1018         2244 :   const selectionErrorCount = computed(() => {
    1019           22 :     if (!hasSelection.value) return 0
    1020           22 :     return selectionScopedGroups.value.reduce(
    1021              :       (sum, group) => sum + group.count,
    1022              :       0
    1023              :     )
    1024              :   })
    1025              : 
    1026              :   /** Distinct nodes affected by any error (workflow-level summary). */
    1027         2244 :   const errorNodeCount = computed(() => {
    1028          101 :     const executionNodeIds = allErrorGroups.value
    1029              :       .flatMap((group) => (group.type === 'execution' ? group.cards : []))
    1030              :       .map((card) => card.nodeId ?? card.rawNodeId)
    1031              :       .filter((nodeId) => nodeId != null)
    1032          101 :     return new Set([...executionNodeIds, ...assetNodeIdsWithError.value]).size
    1033              :   })
    1034              : 
    1035              :   const filteredGroups = computed<ErrorGroup[]>(() => {
    1036          107 :     const query = toValue(searchQuery).trim()
    1037          107 :     return searchErrorGroups(allErrorGroups.value, query)
    1038              :   })
    1039              : 
    1040         2244 :   return {
    1041              :     allErrorGroups,
    1042              :     filteredGroups,
    1043              :     collapseState,
    1044              :     errorNodeCache,
    1045              :     missingNodeCache,
    1046              :     missingPackGroups,
    1047              :     missingModelGroups,
    1048              :     missingMediaGroups,
    1049              :     swapNodeGroups,
    1050              :     hasSelection,
    1051              :     selectedNodeCount,
    1052              :     selectedNodeTitle,
    1053              :     selectionMatchedGroupKeys,
    1054              :     selectionMatchedCardIds,
    1055              :     selectionMatchedAssetNodeIds,
    1056              :     selectionErrorCount,
    1057              :     errorNodeCount
    1058              :   }
    1059              : }
        

Generated by: LCOV version 2.0-1