LCOV - code coverage report
Current view: top level - src/stores - executionErrorStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 90.0 % 320 288
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { defineStore } from 'pinia'
       2              : import { computed, ref, toRaw, watch } from 'vue'
       3              : import { whenever } from '@vueuse/core'
       4              : 
       5              : import { classifyPanelErrors } from '@/utils/errorSeverityClassification'
       6              : import { classifyValidationErrorAbsorption } from '@/utils/missingResourceAbsorption'
       7              : import type { MissingResourceAbsorption } from '@/utils/missingResourceAbsorption'
       8              : 
       9              : import { useNodeErrorFlagSync } from '@/composables/graph/useNodeErrorFlagSync'
      10              : import {
      11              :   getLiftedErrorSource,
      12              :   liftNodeErrorsToBoundary,
      13              :   resolveLiftChain
      14              : } from '@/core/graph/subgraph/liftNodeErrorsToBoundary'
      15              : import type { LGraphNode, LGraph } from '@/lib/litegraph/src/litegraph'
      16              : import { useMissingModelStore } from '@/platform/workflowErrors/missingModel/missingModelStore'
      17              : import { useMissingMediaStore } from '@/platform/workflowErrors/missingMedia/missingMediaStore'
      18              : import type { MissingModelCandidate } from '@/platform/workflowErrors/missingModel/types'
      19              : import type { MissingMediaCandidate } from '@/platform/workflowErrors/missingMedia/types'
      20              : import { useSettingStore } from '@/platform/settings/settingStore'
      21              : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
      22              : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
      23              : import { useApp } from '@/scripts/appInstance'
      24              : import { ChangeTrackerAccess } from '@/scripts/changeTrackerAccess'
      25              : import { useDialogService } from '@/services/dialogService'
      26              : import type { ExecutionErrorWsMessage } from '@comfyorg/api-client/executionTypes'
      27              : import type { NodeError, PromptError } from '@comfyorg/api-client/types'
      28              : import {
      29              :   getAncestorExecutionIds,
      30              :   tryNormalizeNodeExecutionId
      31              : } from '@comfyorg/graph-model/nodeIdentification'
      32              : import type {
      33              :   NodeExecutionId,
      34              :   NodeLocatorId
      35              : } from '@comfyorg/graph-model/nodeIdentification'
      36              : import {
      37              :   executionIdToNodeLocatorId,
      38              :   getExecutionIdByNode,
      39              :   getNodeByExecutionId
      40              : } from '@/utils/graphTraversalUtil'
      41              : import type { UUID } from '@/utils/uuid'
      42              : import { zeroUuid } from '@comfyorg/graph-model/uuid'
      43              : import {
      44              :   SIMPLE_ERROR_TYPES,
      45              :   errorsForSlot,
      46              :   getInputConfigBounds,
      47              :   hasErrorForSlot,
      48              :   isMissingNodePromptError,
      49              :   isValueStillOutOfRange
      50              : } from '@/utils/executionErrorUtil'
      51              : import type { NodeValidationError } from '@/utils/executionErrorUtil'
      52              : import { useMissingNodesErrorStore } from '@/platform/workflowErrors/nodeReplacement/missingNodesErrorStore'
      53              : 
      54              : interface SlotNodeErrorClearTarget {
      55              :   executionId: NodeExecutionId
      56              :   slotName: string
      57              :   /** Interior targets validate against the bounds recorded on their errors. */
      58              :   useRecordedBounds?: boolean
      59              : }
      60              : 
      61              : interface ValidationErrorSurface {
      62              :   executionId: NodeExecutionId
      63              :   error: NodeValidationError
      64              : }
      65              : 
      66              : // undefined means no verdict yet; null or [] means a verified empty result.
      67              : interface MissingResourceCandidates {
      68              :   models?: MissingModelCandidate[] | null
      69              :   media?: MissingMediaCandidate[] | null
      70              : }
      71              : 
      72              : interface RunErrorState {
      73              :   absorbedValidationErrors: WeakMap<
      74              :     NodeValidationError,
      75              :     MissingResourceAbsorption
      76              :   >
      77              :   nodeErrors: Record<string, NodeError> | null
      78              :   nodeErrorsJobId: string | null
      79              :   executionError: ExecutionErrorWsMessage | null
      80              :   promptError: PromptError | null
      81              : }
      82              : 
      83              : /** Execution error state: node errors, runtime errors, prompt errors, and missing assets. */
      84         2356 : export const useExecutionErrorStore = defineStore('executionError', () => {
      85         2355 :   const workflowStore = useWorkflowStore()
      86         2355 :   const canvasStore = useCanvasStore()
      87         2355 :   const missingModelStore = useMissingModelStore()
      88         2355 :   const missingNodesStore = useMissingNodesErrorStore()
      89         2355 :   const missingMediaStore = useMissingMediaStore()
      90              : 
      91              :   /**
      92              :    * Run errors belong to the workflow that produced them, so they are parked
      93              :    * under its path and root graph id and follow that graph back into view
      94              :    * rather than being discarded when another workflow is loaded.
      95              :    */
      96         2355 :   const runErrorsByWorkflow = ref(new Map<string, RunErrorState>())
      97         2355 :   const activeRunErrorKey = ref<string | null>(`:${zeroUuid}`)
      98         2355 :   const isErrorOverlayOpen = ref(false)
      99              : 
     100         2355 :   const activeRunErrors = computed<RunErrorState | undefined>(() =>
     101              :     activeRunErrorKey.value === null
     102              :       ? undefined
     103              :       : runErrorsByWorkflow.value.get(activeRunErrorKey.value)
     104              :   )
     105              : 
     106         2355 :   const lastNodeErrors = computed(
     107              :     () => activeRunErrors.value?.nodeErrors ?? null
     108              :   )
     109         2355 :   const lastExecutionError = computed(
     110              :     () => activeRunErrors.value?.executionError ?? null
     111              :   )
     112         2355 :   const lastPromptError = computed(
     113              :     () => activeRunErrors.value?.promptError ?? null
     114              :   )
     115              : 
     116          615 :   function updateRunErrors(patch: Partial<RunErrorState>, key: string | null) {
     117          615 :     if (key === null) return
     118              : 
     119          609 :     const next: RunErrorState = {
     120              :       absorbedValidationErrors: new WeakMap(),
     121              :       nodeErrors: null,
     122              :       nodeErrorsJobId: null,
     123              :       executionError: null,
     124              :       promptError: null,
     125              :       ...runErrorsByWorkflow.value.get(key),
     126              :       ...patch
     127              :     }
     128              : 
     129          615 :     if (!next.nodeErrors && !next.executionError && !next.promptError) {
     130          547 :       runErrorsByWorkflow.value.delete(key)
     131              :     } else {
     132           62 :       runErrorsByWorkflow.value.set(key, next)
     133              :     }
     134              :   }
     135              : 
     136         8234 :   function runErrorKey(graphId: UUID, workflowPath?: string) {
     137         8234 :     return `${workflowPath ?? workflowStore.activeWorkflow?.path ?? ''}:${graphId}`
     138              :   }
     139              : 
     140          100 :   function moveRunErrors(
     141              :     graphId: UUID,
     142              :     oldWorkflowPath: string,
     143              :     newWorkflowPath: string
     144              :   ) {
     145          100 :     const oldKey = runErrorKey(graphId, oldWorkflowPath)
     146          100 :     const newKey = runErrorKey(graphId, newWorkflowPath)
     147          100 :     if (oldKey === newKey) return
     148              : 
     149           99 :     const runErrors = runErrorsByWorkflow.value.get(oldKey)
     150           99 :     if (runErrors) runErrorsByWorkflow.value.set(newKey, runErrors)
     151           97 :     else runErrorsByWorkflow.value.delete(newKey)
     152           99 :     runErrorsByWorkflow.value.delete(oldKey)
     153              : 
     154           99 :     if (activeRunErrorKey.value === oldKey) {
     155           98 :       activeRunErrorKey.value = newKey
     156              :     }
     157              :   }
     158              : 
     159         3978 :   function captureRunErrorKey() {
     160         3978 :     return activeRunErrorKey.value
     161              :   }
     162              : 
     163              :   /**
     164              :    * Point the store at the run errors of `graphId`. `null` detaches it, so a
     165              :    * discarded graph shows nothing until the next one is loaded. The overlay is
     166              :    * dismissed on every move so it only ever reopens for the graph in front.
     167              :    */
     168        15614 :   function setActiveGraph(graphId: UUID | null, workflowPath?: string) {
     169        15614 :     const key = graphId === null ? null : runErrorKey(graphId, workflowPath)
     170        15614 :     if (key === activeRunErrorKey.value) return
     171        11803 :     activeRunErrorKey.value = key
     172        11803 :     isErrorOverlayOpen.value = false
     173              :   }
     174              : 
     175         2355 :   const pendingAddedNodeScans = new WeakMap<
     176              :     LGraph,
     177              :     Map<NodeExecutionId, number>
     178              :   >()
     179         2355 :   const pendingAddedNodeScanRevision = ref(0)
     180              :   // Preserve the lifted host identity so retirement still matches after node deletion.
     181         2355 :   const validationErrorSurfaces = new WeakMap<
     182              :     NodeValidationError,
     183              :     ValidationErrorSurface[]
     184              :   >()
     185              : 
     186           30 :   function getValidationErrorSurfaces(
     187              :     rootGraph: LGraph | undefined,
     188              :     rawNodeId: NodeExecutionId,
     189              :     nodeError: NodeError,
     190              :     error: NodeValidationError
     191              :   ): ValidationErrorSurface[] {
     192           30 :     const rawErrorRecord = {
     193              :       [rawNodeId]: { ...nodeError, errors: [error] }
     194              :     }
     195           30 :     const surfacedErrorRecord = rootGraph
     196              :       ? liftNodeErrorsToBoundary(rootGraph, rawErrorRecord)
     197              :       : rawErrorRecord
     198              : 
     199           30 :     return Object.entries(surfacedErrorRecord).flatMap(
     200              :       ([surfaceNodeId, surfaceNodeError]) => {
     201           30 :         const executionId = tryNormalizeNodeExecutionId(surfaceNodeId)
     202           30 :         if (!executionId) return []
     203           30 :         return surfaceNodeError.errors.map((error) => ({ executionId, error }))
     204              :       }
     205              :     )
     206              :   }
     207              : 
     208           30 :   function captureValidationErrorSurfaces(
     209              :     nodeErrors: Record<string, NodeError>
     210              :   ): void {
     211           30 :     const rootGraph = useApp().rootGraphOrUndefined
     212           30 :     if (!rootGraph) return
     213              : 
     214           30 :     for (const [rawNodeId, nodeError] of Object.entries(nodeErrors)) {
     215           30 :       const executionId = tryNormalizeNodeExecutionId(rawNodeId)
     216           30 :       if (!executionId) continue
     217           30 :       for (const error of nodeError.errors) {
     218           30 :         validationErrorSurfaces.set(
     219              :           toRaw(error),
     220              :           getValidationErrorSurfaces(rootGraph, executionId, nodeError, error)
     221              :         )
     222              :       }
     223              :     }
     224              :   }
     225              : 
     226           31 :   function resolveValidationErrorSurfaces(
     227              :     executionId: NodeExecutionId,
     228              :     nodeError: NodeError,
     229              :     error: NodeValidationError
     230              :   ): ValidationErrorSurface[] {
     231           31 :     const rawError = toRaw(error)
     232           31 :     const cached = validationErrorSurfaces.get(rawError)
     233           31 :     if (cached) return cached
     234              : 
     235            0 :     const rootGraph = useApp().rootGraphOrUndefined
     236            0 :     const surfaces = getValidationErrorSurfaces(
     237              :       rootGraph,
     238              :       executionId,
     239              :       nodeError,
     240              :       error
     241              :     )
     242            0 :     if (rootGraph) validationErrorSurfaces.set(rawError, surfaces)
     243            0 :     return surfaces
     244              :   }
     245              : 
     246          436 :   function beginAddedNodeErrorScan(
     247              :     rootGraph: LGraph,
     248              :     executionId: NodeExecutionId
     249              :   ): () => void {
     250          436 :     const scansForGraph = pendingAddedNodeScans.get(rootGraph) ?? new Map()
     251          436 :     scansForGraph.set(executionId, (scansForGraph.get(executionId) ?? 0) + 1)
     252          436 :     pendingAddedNodeScans.set(rootGraph, scansForGraph)
     253          436 :     pendingAddedNodeScanRevision.value++
     254              : 
     255          436 :     let finished = false
     256          436 :     return () => {
     257          436 :       if (finished) return
     258          436 :       finished = true
     259              : 
     260          436 :       const remaining = (scansForGraph.get(executionId) ?? 1) - 1
     261          436 :       if (remaining > 0) scansForGraph.set(executionId, remaining)
     262          436 :       else scansForGraph.delete(executionId)
     263          436 :       pendingAddedNodeScanRevision.value++
     264              :     }
     265              :   }
     266              : 
     267            9 :   function hasPendingAddedNodeErrorScan(
     268              :     rootGraph: LGraph,
     269              :     executionId: NodeExecutionId
     270              :   ): boolean {
     271              :     // Subscribe callers to changes in the non-reactive WeakMap.
     272            9 :     void pendingAddedNodeScanRevision.value
     273            9 :     return (pendingAddedNodeScans.get(rootGraph)?.get(executionId) ?? 0) > 0
     274              :   }
     275              : 
     276              :   /**
     277              :    * Also called after a graph load settles: the sync watcher below skips
     278              :    * transitions that happen while a load is in flight, and no later
     279              :    * transition reruns it.
     280              :    */
     281         3905 :   function retireResolvedMissingNodePromptError() {
     282         3905 :     if (missingNodesStore.missingNodesError?.nodeTypes.length) return
     283         3855 :     if (!isMissingNodePromptError(lastPromptError.value)) return
     284            0 :     updateRunErrors({ promptError: null }, activeRunErrorKey.value)
     285              :   }
     286              : 
     287         2355 :   whenever(
     288              :     () => !missingNodesStore.missingNodesError?.nodeTypes.length,
     289              :     () => {
     290           23 :       if (ChangeTrackerAccess.isLoadingGraph) return
     291           18 :       retireResolvedMissingNodePromptError()
     292              :     },
     293              :     // The missing-node scan and prompt recording share a synchronous block; clear before recording the new error.
     294              :     { flush: 'sync' }
     295              :   )
     296              : 
     297           31 :   function classifyMissingResource(
     298              :     surfaces: ValidationErrorSurface[],
     299              :     { models, media }: MissingResourceCandidates
     300              :   ): MissingResourceAbsorption | null {
     301           31 :     for (const { executionId, error } of surfaces) {
     302           31 :       const kind = classifyValidationErrorAbsorption(
     303              :         models ?? null,
     304              :         media ?? null,
     305              :         error,
     306              :         executionId
     307              :       )
     308           31 :       if (kind) return kind
     309              :     }
     310           17 :     return null
     311              :   }
     312              : 
     313              :   /**
     314              :    * The model/media analog of the missing-node retirement above: when a
     315              :    * candidate leaves the store (installed + refreshed, node deleted or
     316              :    * bypassed), the validation errors it was absorbing must retire with it —
     317              :    * otherwise resolving the resource resurfaces them as blocking errors.
     318              :    * Matching is by content, so a rescan that rebuilds equivalent candidates
     319              :    * retires nothing.
     320              :    */
     321        12293 :   function reconcileAbsorbedErrors(
     322              :     { models, media }: MissingResourceCandidates,
     323              :     key: string | null,
     324              :     previous?: MissingResourceCandidates
     325              :   ) {
     326        12293 :     if (
     327              :       ChangeTrackerAccess.isLoadingGraph ||
     328              :       key === null ||
     329              :       key !== activeRunErrorKey.value
     330              :     )
     331            3 :       return
     332        12290 :     const state = activeRunErrors.value
     333        12293 :     if (!state?.nodeErrors) return
     334           31 :     const record = state.nodeErrors
     335              : 
     336        12293 :     const possiblyMissingModels =
     337              :       models?.map((candidate) =>
     338              :         candidate.isMissing === undefined
     339              :           ? { ...candidate, isMissing: true }
     340              :           : candidate
     341              :       ) ?? null
     342        12293 :     const possiblyMissingMedia =
     343              :       media?.map((candidate) =>
     344              :         candidate.isMissing === undefined
     345              :           ? { ...candidate, isMissing: true }
     346              :           : candidate
     347              :       ) ?? null
     348              : 
     349        12293 :     const current = { models, media }
     350        12293 :     const possiblyMissing = {
     351              :       models: possiblyMissingModels,
     352              :       media: possiblyMissingMedia
     353              :     }
     354        12293 :     const updated = filterNodeErrors(record, (surfaces, error) => {
     355           31 :       const rawError = toRaw(error)
     356           31 :       const kind = classifyMissingResource(surfaces, current)
     357           31 :       if (kind) {
     358           14 :         state.absorbedValidationErrors.set(rawError, kind)
     359           14 :         return true
     360              :       }
     361           31 :       const wasAbsorbed =
     362              :         state.absorbedValidationErrors.get(rawError) ??
     363              :         (previous ? classifyMissingResource(surfaces, previous) : null)
     364           31 :       if (!wasAbsorbed) return true
     365              :       const wasVerified =
     366              :         wasAbsorbed === 'missing_model'
     367              :           ? models !== undefined
     368              :           : media !== undefined
     369           31 :       return (
     370              :         !wasVerified ||
     371              :         classifyMissingResource(surfaces, possiblyMissing) !== null
     372              :       )
     373              :     })
     374        12293 :     if (updated !== record) updateRunErrors({ nodeErrors: updated }, key)
     375              :   }
     376              : 
     377           31 :   function filterNodeErrors(
     378              :     record: Record<string, NodeError>,
     379              :     keepAndRecord: (
     380              :       surfaces: ValidationErrorSurface[],
     381              :       error: NodeValidationError
     382              :     ) => boolean
     383              :   ): Record<string, NodeError> | null {
     384           31 :     let changed = false
     385           31 :     const updated: Record<string, NodeError> = {}
     386           31 :     for (const [rawNodeId, nodeError] of Object.entries(record)) {
     387           31 :       const executionId = tryNormalizeNodeExecutionId(rawNodeId)
     388           31 :       if (!executionId) {
     389            0 :         updated[rawNodeId] = nodeError
     390            0 :         continue
     391              :       }
     392           31 :       const remaining = nodeError.errors.filter((error) =>
     393              :         keepAndRecord(
     394              :           resolveValidationErrorSurfaces(executionId, nodeError, error),
     395              :           error
     396              :         )
     397              :       )
     398           31 :       if (remaining.length === nodeError.errors.length) {
     399           31 :         updated[rawNodeId] = nodeError
     400              :       } else {
     401            0 :         changed = true
     402            0 :         if (remaining.length > 0)
     403            0 :           updated[rawNodeId] = { ...nodeError, errors: remaining }
     404              :       }
     405              :     }
     406           31 :     if (!changed) return record
     407           31 :     return Object.keys(updated).length ? updated : null
     408              :   }
     409              : 
     410         6473 :   function retireResolvedMissingResourceErrors(
     411              :     candidates: MissingResourceCandidates,
     412              :     key: string | null = activeRunErrorKey.value
     413              :   ) {
     414         6473 :     reconcileAbsorbedErrors(candidates, key)
     415              :   }
     416              : 
     417         2355 :   watch(
     418              :     () => ({
     419              :       key: activeRunErrorKey.value,
     420              :       models: missingModelStore.missingModelCandidates,
     421              :       media: missingMediaStore.missingMediaCandidates
     422              :     }),
     423              :     (next, previous) => {
     424        13776 :       if (next.key !== previous.key) return
     425         5790 :       reconcileAbsorbedErrors(next, next.key, previous)
     426              :     }
     427              :   )
     428              : 
     429              :   /**
     430              :    * Replaces the full record; empty or null means the run produced no errors.
     431              :    *
     432              :    * `key` files the errors against the workflow that produced them, which is
     433              :    * not necessarily the one on screen. Build it with `runErrorKey`. It defaults
     434              :    * to the visible workflow for callers that record synchronously while
     435              :    * submitting it.
     436              :    */
     437          377 :   function recordNodeErrors(
     438              :     nodeErrors: Record<string, NodeError> | null,
     439              :     key: string | null = activeRunErrorKey.value,
     440              :     jobId: string | null = null
     441              :   ) {
     442          377 :     const record =
     443              :       nodeErrors && Object.keys(nodeErrors).length > 0 ? nodeErrors : null
     444          377 :     updateRunErrors(
     445              :       {
     446              :         nodeErrors: record,
     447              :         nodeErrorsJobId: record ? jobId : null,
     448              :         absorbedValidationErrors: new WeakMap()
     449              :       },
     450              :       key
     451              :     )
     452          377 :     if (record && key !== null && key === activeRunErrorKey.value) {
     453           30 :       captureValidationErrorSurfaces(record)
     454           30 :       reconcileAbsorbedErrors(
     455              :         {
     456              :           models: missingModelStore.missingModelCandidates,
     457              :           media: missingMediaStore.missingMediaCandidates
     458              :         },
     459              :         key
     460              :       )
     461              :     }
     462              :   }
     463              : 
     464              :   /**
     465              :    * Records submission `jobId`'s node errors under `key`. An empty response
     466              :    * keeps errors already recorded for `jobId` and clears any other job's.
     467              :    */
     468          150 :   function recordSubmissionNodeErrors(
     469              :     nodeErrors: Record<string, NodeError> | null | undefined,
     470              :     key: string | null,
     471              :     jobId: string | undefined
     472              :   ) {
     473          150 :     if (nodeErrors && Object.keys(nodeErrors).length > 0) {
     474            1 :       recordNodeErrors(nodeErrors, key, jobId ?? null)
     475            1 :       return
     476              :     }
     477          150 :     const recordedJobId =
     478              :       key === null ? null : runErrorsByWorkflow.value.get(key)?.nodeErrorsJobId
     479          150 :     if (jobId === undefined || recordedJobId !== jobId) {
     480          149 :       recordNodeErrors(null, key)
     481              :     }
     482              :   }
     483              : 
     484              :   /**
     485              :    * Records `nodeErrors` of job `jobId` under `key` and opens the error
     486              :    * overlay when that run is on screen.
     487              :    */
     488            1 :   function surfaceNodeErrors(
     489              :     nodeErrors: Record<string, NodeError>,
     490              :     key: string,
     491              :     jobId: string
     492              :   ) {
     493            1 :     recordNodeErrors(nodeErrors, key, jobId)
     494            1 :     if (
     495              :       key === activeRunErrorKey.value &&
     496              :       hasNodeError.value &&
     497              :       useSettingStore().get('Comfy.RightSidePanel.ShowErrorsTab')
     498              :     ) {
     499            1 :       showErrorOverlay()
     500              :     }
     501              :   }
     502              : 
     503           12 :   function recordExecutionError(
     504              :     detail: ExecutionErrorWsMessage,
     505              :     key: string | null = activeRunErrorKey.value
     506              :   ) {
     507           12 :     updateRunErrors({ executionError: detail }, key)
     508              :   }
     509              : 
     510           12 :   function showExecutionError(
     511              :     detail: ExecutionErrorWsMessage,
     512              :     key: string | null = activeRunErrorKey.value
     513              :   ) {
     514           12 :     if (key === null || key !== activeRunErrorKey.value) return
     515              : 
     516           11 :     if (useSettingStore().get('Comfy.RightSidePanel.ShowErrorsTab')) {
     517            7 :       showErrorOverlay()
     518              :     } else {
     519            4 :       useDialogService().showExecutionErrorDialog(detail)
     520              :     }
     521              :   }
     522              : 
     523            7 :   function recordPromptError(
     524              :     promptError: PromptError,
     525              :     key: string | null = activeRunErrorKey.value
     526              :   ) {
     527            7 :     updateRunErrors({ promptError }, key)
     528              :   }
     529              : 
     530          256 :   function showErrorOverlay() {
     531          256 :     isErrorOverlayOpen.value = true
     532              :   }
     533              : 
     534           92 :   function dismissErrorOverlay() {
     535           92 :     isErrorOverlayOpen.value = false
     536              :   }
     537              : 
     538              :   /** Clear error state produced by a run. Missing-resource state describes the
     539              :    *  loaded graph rather than the run, so only replacing or discarding the
     540              :    *  graph invalidates it. */
     541          193 :   function clearRunErrors() {
     542          193 :     if (activeRunErrorKey.value !== null) {
     543          193 :       runErrorsByWorkflow.value.delete(activeRunErrorKey.value)
     544              :     }
     545          193 :     isErrorOverlayOpen.value = false
     546              :   }
     547              : 
     548          119 :   function clearExecutionStartErrors(
     549              :     key: string | null = activeRunErrorKey.value
     550              :   ) {
     551          119 :     updateRunErrors({ executionError: null, promptError: null }, key)
     552          119 :     if (key === activeRunErrorKey.value && !lastNodeErrors.value) {
     553          109 :       isErrorOverlayOpen.value = false
     554              :     }
     555              :   }
     556              : 
     557              :   /** Clear only prompt-level errors. Called during resetExecutionState. */
     558           96 :   function clearPromptError(key: string | null = activeRunErrorKey.value) {
     559           96 :     updateRunErrors({ promptError: null }, key)
     560              :   }
     561              : 
     562            6 :   function clearSimpleNodeErrorsFromRecord(
     563              :     nodeErrors: Record<string, NodeError>,
     564              :     executionId: NodeExecutionId,
     565              :     slotName?: string
     566              :   ): Record<string, NodeError> | null {
     567            6 :     if (!(executionId in nodeErrors)) return null
     568            6 :     const nodeError = nodeErrors[executionId]
     569              : 
     570            6 :     const isSlotScoped = slotName !== undefined
     571            6 :     const relevantErrors = isSlotScoped
     572              :       ? errorsForSlot(nodeError.errors, slotName)
     573              :       : nodeError.errors
     574              : 
     575            6 :     if (relevantErrors.length === 0) return null
     576            4 :     if (!relevantErrors.every((e) => SIMPLE_ERROR_TYPES.has(e.type))) {
     577            0 :       return null
     578              :     }
     579              : 
     580            4 :     const updated = { ...nodeErrors }
     581              : 
     582            4 :     if (isSlotScoped) {
     583            4 :       const remainingErrors = nodeError.errors.filter(
     584              :         (error) => !relevantErrors.includes(error)
     585              :       )
     586            4 :       if (remainingErrors.length === 0) {
     587            4 :         delete updated[executionId]
     588              :       } else {
     589            0 :         updated[executionId] = {
     590              :           ...nodeError,
     591              :           errors: remainingErrors
     592              :         }
     593              :       }
     594              :     } else {
     595            0 :       delete updated[executionId]
     596              :     }
     597              : 
     598            4 :     return updated
     599              :   }
     600              : 
     601              :   /**
     602              :    * Raw interior sources of lifted errors whose boundary chain passes through
     603              :    * `(executionId, slotName)`, so a fix at any host level — final surface or
     604              :    * intermediate — clears the error at its raw key.
     605              :    */
     606        80721 :   function getLiftedErrorSourceTargets(
     607              :     executionId: NodeExecutionId,
     608              :     slotName: string
     609              :   ): SlotNodeErrorClearTarget[] {
     610        80721 :     const surfaced = surfacedNodeErrors.value
     611        80721 :     const rootGraph = useApp().rootGraphOrUndefined
     612        80721 :     if (!surfaced || !rootGraph) return []
     613              : 
     614            6 :     return Object.values(surfaced).flatMap((surface) =>
     615              :       surface.errors.flatMap((error): SlotNodeErrorClearTarget[] => {
     616            6 :         const source = getLiftedErrorSource(error)
     617            6 :         if (!source) return []
     618              : 
     619            0 :         const sourceExecutionId = tryNormalizeNodeExecutionId(
     620              :           source.source_execution_id
     621              :         )
     622            0 :         if (!sourceExecutionId) return []
     623              : 
     624              :         const clearsThisError = resolveLiftChain(
     625              :           rootGraph,
     626              :           sourceExecutionId,
     627              :           source.source_input_name
     628              :         ).some(
     629              :           (level) =>
     630              :             level.hostExecId === executionId && level.hostInputName === slotName
     631              :         )
     632            6 :         return clearsThisError
     633              :           ? [
     634              :               {
     635              :                 executionId: sourceExecutionId,
     636              :                 slotName: source.source_input_name,
     637              :                 useRecordedBounds: true
     638              :               }
     639              :             ]
     640              :           : []
     641              :       })
     642              :     )
     643              :   }
     644              : 
     645              :   /** Raw targets are keys into lastNodeErrors, not surfacedNodeErrors. */
     646        80721 :   function getRawClearTargets(
     647              :     executionId: NodeExecutionId,
     648              :     slotName: string
     649              :   ): SlotNodeErrorClearTarget[] {
     650        80721 :     return [
     651              :       { executionId, slotName },
     652              :       ...getLiftedErrorSourceTargets(executionId, slotName)
     653              :     ]
     654              :   }
     655              : 
     656              :   /**
     657              :    * Bounds recorded on the error win only for interior lifted targets, where
     658              :    * the caller's options describe the host widget rather than the interior
     659              :    * input. The base target keeps the caller's live widget bounds, which stay
     660              :    * authoritative when node definitions change after validation.
     661              :    */
     662            0 :   function getTargetRangeOptions(
     663              :     errors: NodeError['errors'],
     664              :     fallback: { min?: number; max?: number }
     665              :   ): { min?: number; max?: number } {
     666            0 :     for (const error of errors) {
     667            0 :       const { min, max } = getInputConfigBounds(error)
     668            0 :       if (min === undefined && max === undefined) continue
     669            0 :       return {
     670              :         min: typeof min === 'number' ? min : fallback.min,
     671              :         max: typeof max === 'number' ? max : fallback.max
     672              :       }
     673              :     }
     674            0 :     return fallback
     675              :   }
     676              : 
     677            1 :   function isTargetStillOutOfRange(
     678              :     nodeErrors: Record<string, NodeError>,
     679              :     target: SlotNodeErrorClearTarget,
     680              :     value: number,
     681              :     callerOptions: { min?: number; max?: number }
     682              :   ): boolean {
     683            1 :     if (!(target.executionId in nodeErrors)) return false
     684            1 :     const nodeError = nodeErrors[target.executionId]
     685              : 
     686            1 :     const errors = errorsForSlot(nodeError.errors, target.slotName)
     687            1 :     const options = target.useRecordedBounds
     688              :       ? getTargetRangeOptions(errors, callerOptions)
     689              :       : callerOptions
     690              : 
     691            1 :     return isValueStillOutOfRange(value, errors, options)
     692              :   }
     693              : 
     694        80721 :   function clearTargets(
     695              :     targets: { executionId: NodeExecutionId; slotName?: string }[]
     696              :   ): void {
     697        80721 :     if (!lastNodeErrors.value) return
     698              : 
     699            6 :     let updated = lastNodeErrors.value
     700            6 :     for (const target of targets) {
     701            6 :       updated =
     702              :         clearSimpleNodeErrorsFromRecord(
     703              :           updated,
     704              :           target.executionId,
     705              :           target.slotName
     706              :         ) ?? updated
     707              :     }
     708              : 
     709          158 :     if (updated === lastNodeErrors.value) return
     710        80721 :     updateRunErrors(
     711              :       { nodeErrors: Object.keys(updated).length > 0 ? updated : null },
     712              :       activeRunErrorKey.value
     713              :     )
     714              :   }
     715              : 
     716              :   /**
     717              :    * Removes a node's errors if they consist entirely of simple, auto-resolvable
     718              :    * types. When `slotName` is provided, only errors for that slot are checked
     719              :    * and boundary-lifted errors are also cleared through their raw interior
     720              :    * source. Node-scoped calls (no `slotName`) operate on the raw record key
     721              :    * only.
     722              :    */
     723        80715 :   function clearSimpleNodeErrors(
     724              :     executionId: NodeExecutionId,
     725              :     slotName?: string
     726              :   ): void {
     727        80715 :     clearTargets(
     728              :       slotName === undefined
     729              :         ? [{ executionId }]
     730              :         : getRawClearTargets(executionId, slotName)
     731              :     )
     732              :   }
     733              : 
     734              :   /**
     735              :    * Attempts to clear an error for a given widget, but avoids clearing it if
     736              :    * the error is a range violation and the new value is still out of bounds.
     737              :    * The base target validates against the caller's live widget bounds; each
     738              :    * interior lifted target validates against its own recorded bounds, so a
     739              :    * boundary input fanning out to inputs with different constraints clears
     740              :    * only the targets the new value satisfies.
     741              :    *
     742              :    * Note: `value_not_in_list` errors are optimistically cleared without
     743              :    * list-membership validation because combo widgets constrain choices to
     744              :    * valid values at the UI level, and the valid-values source varies
     745              :    * (asset system vs objectInfo) making runtime validation non-trivial.
     746              :    */
     747          447 :   function clearSlotErrorsWithRangeCheck(
     748              :     executionId: NodeExecutionId,
     749              :     widgetName: string,
     750              :     newValue: unknown,
     751              :     options?: { min?: number; max?: number }
     752              :   ): void {
     753          447 :     const nodeErrors = lastNodeErrors.value
     754          447 :     if (!nodeErrors) return
     755              : 
     756            6 :     const targets = getRawClearTargets(executionId, widgetName)
     757              :     const clearableTargets =
     758              :       typeof newValue === 'number'
     759              :         ? targets.filter(
     760              :             (target) =>
     761              :               !isTargetStillOutOfRange(
     762              :                 nodeErrors,
     763              :                 target,
     764              :                 newValue,
     765              :                 options ?? {}
     766              :               )
     767              :           )
     768              :         : targets
     769              : 
     770          447 :     clearTargets(clearableTargets)
     771              :   }
     772              : 
     773              :   /**
     774              :    * Clears both validation errors and missing model state for a widget.
     775              :    *
     776              :    * @param errorInputName Name matched against `error.extra_info.input_name`.
     777              :    * @param widgetName Widget name used for missing model/media lookup.
     778              :    */
     779          447 :   function clearWidgetRelatedErrors(
     780              :     executionId: NodeExecutionId,
     781              :     errorInputName: string,
     782              :     widgetName: string,
     783              :     newValue: unknown,
     784              :     options?: { min?: number; max?: number }
     785              :   ): void {
     786          447 :     clearSlotErrorsWithRangeCheck(
     787              :       executionId,
     788              :       errorInputName,
     789              :       newValue,
     790              :       options
     791              :     )
     792          447 :     missingModelStore.removeMissingModelByWidget(executionId, widgetName)
     793          447 :     missingMediaStore.removeMissingMediaByWidget(executionId, widgetName)
     794              :   }
     795              : 
     796              :   /** Set missing models and optionally open the error overlay. */
     797         3730 :   function surfaceMissingModels(
     798              :     models: MissingModelCandidate[],
     799              :     options?: { silent?: boolean }
     800              :   ) {
     801         3730 :     missingModelStore.setMissingModels(models)
     802         3730 :     if (
     803              :       !options?.silent &&
     804              :       missingModelStore.hasMissingModels &&
     805              :       useSettingStore().get('Comfy.RightSidePanel.ShowErrorsTab')
     806              :     ) {
     807          145 :       showErrorOverlay()
     808              :     }
     809              :   }
     810              : 
     811              :   /** Set missing media and optionally open the error overlay. */
     812          140 :   function surfaceMissingMedia(
     813              :     media: MissingMediaCandidate[],
     814              :     options?: { silent?: boolean }
     815              :   ) {
     816          140 :     missingMediaStore.setMissingMedia(media)
     817          140 :     if (
     818              :       !options?.silent &&
     819              :       missingMediaStore.hasMissingMedia &&
     820              :       useSettingStore().get('Comfy.RightSidePanel.ShowErrorsTab')
     821              :     ) {
     822           15 :       showErrorOverlay()
     823              :     }
     824              :   }
     825              : 
     826         2355 :   const lastExecutionErrorNodeLocatorId = computed(() => {
     827          807 :     const err = lastExecutionError.value
     828          807 :     if (!err) return null
     829            1 :     return executionIdToNodeLocatorId(
     830              :       useApp().rootGraphOrUndefined,
     831              :       String(err.node_id)
     832              :     )
     833              :   })
     834              : 
     835         2355 :   const lastExecutionErrorNodeId = computed(() => {
     836            0 :     const locator = lastExecutionErrorNodeLocatorId.value
     837            0 :     if (!locator) return null
     838              :     const localId = workflowStore.nodeLocatorIdToNodeId(locator)
     839            0 :     return localId
     840              :   })
     841              : 
     842         2355 :   const hasExecutionError = computed(() => !!lastExecutionError.value)
     843              : 
     844         2355 :   const hasPromptError = computed(() => !!lastPromptError.value)
     845              : 
     846         2355 :   const hasNodeError = computed(() => lastNodeErrors.value !== null)
     847              : 
     848              :   // Re-lifts only when the record changes; topology is assumed stable while errors are displayed.
     849         2355 :   const surfacedNodeErrors = computed(() => {
     850         4748 :     const rootGraph = useApp().rootGraphOrUndefined
     851         4748 :     return lastNodeErrors.value && rootGraph
     852              :       ? liftNodeErrorsToBoundary(rootGraph, lastNodeErrors.value)
     853              :       : lastNodeErrors.value
     854              :   })
     855              : 
     856         2355 :   const errorClassification = computed(() =>
     857              :     classifyPanelErrors({
     858              :       promptError: lastPromptError.value,
     859              :       executionError: lastExecutionError.value,
     860              :       nodeErrors: surfacedNodeErrors.value,
     861              :       missingModels: missingModelStore.visibleMissingModelCandidates,
     862              :       missingMedia: missingMediaStore.visibleMissingMediaCandidates,
     863              :       hasMissingNodes: missingNodesStore.hasMissingNodes
     864              :     })
     865              :   )
     866              : 
     867         2355 :   const blockingErrorNodeLocatorIds = computed(() => {
     868         3399 :     const classification = errorClassification.value
     869         3399 :     const executionIds = classification.nodeErrors
     870              :       .filter(({ errors }) => errors.some(({ absorption }) => !absorption))
     871              :       .map(({ rawNodeId }) => rawNodeId)
     872         3399 :     if (classification.executionError?.rawNodeId != null) {
     873            2 :       executionIds.push(classification.executionError.rawNodeId)
     874              :     }
     875              : 
     876         3399 :     const locators = new Set<NodeLocatorId>()
     877         3399 :     for (const executionId of executionIds) {
     878           11 :       for (const ancestorId of getAncestorExecutionIds(executionId)) {
     879           13 :         const locator = executionIdToNodeLocatorId(
     880              :           useApp().rootGraphOrUndefined,
     881              :           ancestorId
     882              :         )
     883           13 :         if (locator) locators.add(locator)
     884              :       }
     885              :     }
     886         3399 :     return locators
     887              :   })
     888              : 
     889         2355 :   const hasMissingError = computed(
     890              :     () =>
     891              :       missingNodesStore.hasMissingNodes ||
     892              :       missingModelStore.hasMissingModels ||
     893              :       missingMediaStore.hasMissingMedia
     894              :   )
     895              : 
     896         2355 :   const hasAnyError = computed(
     897              :     () =>
     898              :       hasExecutionError.value ||
     899              :       hasPromptError.value ||
     900              :       hasNodeError.value ||
     901              :       hasMissingError.value
     902              :   )
     903              : 
     904         2355 :   const allErrorExecutionIds = computed<string[]>(() => {
     905          833 :     const ids: string[] = []
     906          833 :     if (surfacedNodeErrors.value) {
     907            3 :       ids.push(...Object.keys(surfacedNodeErrors.value))
     908              :     }
     909          833 :     if (lastExecutionError.value) {
     910            1 :       const nodeId = lastExecutionError.value.node_id
     911            1 :       if (nodeId != null) {
     912            1 :         ids.push(String(nodeId))
     913              :       }
     914              :     }
     915          833 :     return ids
     916              :   })
     917              : 
     918              :   /** Graph node IDs (as strings) that have errors in the current graph scope. */
     919         2355 :   const activeGraphErrorNodeIds = computed<Set<string>>(() => {
     920           41 :     const ids = new Set<string>()
     921           41 :     const rootGraph = useApp().rootGraphOrUndefined
     922           41 :     if (!rootGraph) return ids
     923              : 
     924              :     // Fall back to rootGraph when currentGraph hasn't been initialized yet
     925           41 :     const activeGraph = canvasStore.currentGraph ?? rootGraph
     926              : 
     927           41 :     if (surfacedNodeErrors.value) {
     928            0 :       for (const executionId of Object.keys(surfacedNodeErrors.value)) {
     929            0 :         const graphNode = getNodeByExecutionId(rootGraph, executionId)
     930            0 :         if (graphNode?.graph === activeGraph) {
     931            0 :           ids.add(String(graphNode.id))
     932              :         }
     933              :       }
     934              :     }
     935              : 
     936           41 :     if (lastExecutionError.value) {
     937            0 :       const execNodeId = String(lastExecutionError.value.node_id)
     938            0 :       const graphNode = getNodeByExecutionId(rootGraph, execNodeId)
     939            0 :       if (graphNode?.graph === activeGraph) {
     940            0 :         ids.add(String(graphNode.id))
     941              :       }
     942              :     }
     943              : 
     944           41 :     return ids
     945              :   })
     946              : 
     947              :   /** Map of node errors indexed by locator ID. */
     948         2355 :   const nodeErrorsByLocatorId = computed<Record<NodeLocatorId, NodeError>>(
     949              :     () => {
     950          823 :       if (!surfacedNodeErrors.value) return {}
     951              : 
     952            8 :       const map: Record<NodeLocatorId, NodeError> = {}
     953              : 
     954            8 :       for (const [executionId, nodeError] of Object.entries(
     955              :         surfacedNodeErrors.value
     956              :       )) {
     957            8 :         const locatorId = executionIdToNodeLocatorId(
     958              :           useApp().rootGraphOrUndefined,
     959              :           executionId
     960              :         )
     961            8 :         if (locatorId) {
     962            8 :           map[locatorId] = nodeError
     963              :         }
     964              :       }
     965              : 
     966            8 :       return map
     967              :     }
     968              :   )
     969              : 
     970              :   /** Get node errors by locator ID. */
     971         2355 :   const getNodeErrors = (
     972              :     nodeLocatorId: NodeLocatorId
     973              :   ): NodeError | undefined => {
     974        32804 :     return nodeErrorsByLocatorId.value[nodeLocatorId]
     975              :   }
     976              : 
     977              :   /** Check if a specific slot has validation errors. */
     978         2355 :   const slotHasError = (
     979              :     nodeLocatorId: NodeLocatorId,
     980              :     slotName: string
     981              :   ): boolean => {
     982        11176 :     const nodeError = getNodeErrors(nodeLocatorId)
     983        11176 :     if (!nodeError) return false
     984              : 
     985           17 :     return hasErrorForSlot(nodeError.errors, slotName)
     986              :   }
     987              : 
     988              :   /**
     989              :    * Set of all execution ID prefixes derived from active error nodes,
     990              :    * including the error nodes themselves.
     991              :    *
     992              :    * Example: error at "65:70:63" → Set { "65", "65:70", "65:70:63" }
     993              :    */
     994         2355 :   const errorAncestorExecutionIds = computed<Set<NodeExecutionId>>(() => {
     995          816 :     const ids = new Set<NodeExecutionId>()
     996          816 :     for (const executionId of allErrorExecutionIds.value) {
     997            4 :       for (const id of getAncestorExecutionIds(executionId)) {
     998            4 :         ids.add(id)
     999              :       }
    1000              :     }
    1001          816 :     return ids
    1002              :   })
    1003              : 
    1004              :   /** True if the node has errors inside it at any nesting depth. */
    1005        19610 :   function isContainerWithInternalError(node: LGraphNode): boolean {
    1006        19610 :     const rootGraph = useApp().rootGraphOrUndefined
    1007        19610 :     if (!rootGraph) return false
    1008        19610 :     const execId = getExecutionIdByNode(rootGraph, node)
    1009        19610 :     if (!execId) return false
    1010        19307 :     return errorAncestorExecutionIds.value.has(execId)
    1011              :   }
    1012              : 
    1013         2355 :   useNodeErrorFlagSync(
    1014              :     surfacedNodeErrors,
    1015              :     missingModelStore,
    1016              :     missingMediaStore,
    1017              :     missingNodesStore
    1018              :   )
    1019              : 
    1020         2355 :   return {
    1021              :     // Read-only state
    1022              :     lastNodeErrors,
    1023              :     lastExecutionError,
    1024              :     lastPromptError,
    1025              : 
    1026              :     // Recording
    1027              :     recordNodeErrors,
    1028              :     recordSubmissionNodeErrors,
    1029              :     surfaceNodeErrors,
    1030              :     recordExecutionError,
    1031              :     recordPromptError,
    1032              : 
    1033              :     // Workflow scoping
    1034              :     captureRunErrorKey,
    1035              :     runErrorKey,
    1036              :     moveRunErrors,
    1037              :     setActiveGraph,
    1038              : 
    1039              :     // Clearing
    1040              :     clearRunErrors,
    1041              :     clearExecutionStartErrors,
    1042              :     clearPromptError,
    1043              :     retireResolvedMissingNodePromptError,
    1044              :     retireResolvedMissingResourceErrors,
    1045              : 
    1046              :     // Overlay UI
    1047              :     isErrorOverlayOpen,
    1048              :     showExecutionError,
    1049              :     showErrorOverlay,
    1050              :     dismissErrorOverlay,
    1051              : 
    1052              :     // Derived state
    1053              :     surfacedNodeErrors,
    1054              :     hasExecutionError,
    1055              :     hasPromptError,
    1056              :     hasNodeError,
    1057              :     errorClassification,
    1058              :     blockingErrorNodeLocatorIds,
    1059              :     hasMissingError,
    1060              :     hasAnyError,
    1061              :     allErrorExecutionIds,
    1062              :     lastExecutionErrorNodeId,
    1063              :     lastExecutionErrorNodeLocatorId,
    1064              :     activeGraphErrorNodeIds,
    1065              : 
    1066              :     // Added-node scan coordination
    1067              :     beginAddedNodeErrorScan,
    1068              :     hasPendingAddedNodeErrorScan,
    1069              : 
    1070              :     // Clearing (targeted)
    1071              :     clearSimpleNodeErrors,
    1072              :     clearWidgetRelatedErrors,
    1073              : 
    1074              :     // Missing model coordination (delegates to missingModelStore)
    1075              :     surfaceMissingModels,
    1076              : 
    1077              :     // Missing media coordination (delegates to missingMediaStore)
    1078              :     surfaceMissingMedia,
    1079              : 
    1080              :     // Lookup helpers
    1081              :     getNodeErrors,
    1082              :     slotHasError,
    1083              :     isContainerWithInternalError
    1084              :   }
    1085              : })
        

Generated by: LCOV version 2.0-1