LCOV - code coverage report
Current view: top level - src/stores - executionStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 82.9 % 556 461
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { createEventHook } from '@vueuse/core'
       2              : import { defineStore } from 'pinia'
       3              : import { computed, ref, shallowRef, watch } from 'vue'
       4              : 
       5              : import { useAppMode } from '@/composables/useAppMode'
       6              : import { isCloud } from '@/platform/distribution/types'
       7              : import { resolveAccountPrecondition } from '@/platform/workflowErrors/errorCatalog/accountPreconditionRouting'
       8              : import { useTelemetry } from '@comfyorg/telemetry'
       9              : import { reportError } from '@comfyorg/telemetry/reportError'
      10              : import type {
      11              :   WorkflowExecutionContext,
      12              :   WorkflowExecutionFailureReason,
      13              :   WorkflowExecutionIntent
      14              : } from '@comfyorg/telemetry/types'
      15              : import type { ComfyWorkflow } from '@/platform/workflow/management/stores/workflowStore'
      16              : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
      17              : import type {
      18              :   ComfyApiWorkflow,
      19              :   WorkflowId
      20              : } from '@comfyorg/graph-model/workflowSchema'
      21              : import type {
      22              :   ExecutedWsMessage,
      23              :   ExecutionCachedWsMessage,
      24              :   ExecutionErrorWsMessage,
      25              :   ExecutionInterruptedWsMessage,
      26              :   ExecutionStartWsMessage,
      27              :   ExecutionSuccessWsMessage,
      28              :   JobId,
      29              :   NodeProgressState,
      30              :   NotificationWsMessage,
      31              :   ProgressStateWsMessage,
      32              :   ProgressWsMessage
      33              : } from '@comfyorg/api-client/executionTypes'
      34              : import type { zPromptOutputsSkippedData } from '@comfyorg/ingest-types/zod'
      35              : import type { NodeError } from '@comfyorg/api-client/types'
      36              : import { api } from '@/scripts/api'
      37              : import { useApp } from '@/scripts/appInstance'
      38              : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
      39              : import { useJobPreviewStore } from '@/stores/jobPreviewStore'
      40              : import { useExecutionErrorStore } from '@/stores/executionErrorStore'
      41              : import { tryNormalizeNodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'
      42              : import type { NodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
      43              : import type { AppMode } from '@comfyorg/graph-model/appMode'
      44              : import { isAppModeValue } from '@comfyorg/graph-model/appMode'
      45              : import { zeroUuid } from '@comfyorg/graph-model/uuid'
      46              : import {
      47              :   classifyCloudValidationError,
      48              :   normalizeRelayedNodeErrors
      49              : } from '@/utils/executionErrorUtil'
      50              : import { executionIdToNodeLocatorId } from '@/utils/graphTraversalUtil'
      51              : import { createRafCoalescer } from '@/utils/rafBatch'
      52              : import type { z } from 'zod'
      53              : 
      54              : type PromptOutputsSkippedData = z.infer<typeof zPromptOutputsSkippedData>
      55              : 
      56              : type RuntimeExecutionError = Omit<
      57              :   ExecutionErrorWsMessage,
      58              :   'node_id' | 'traceback'
      59              : > & {
      60              :   node_id?: ExecutionErrorWsMessage['node_id'] | null
      61              :   traceback?: ExecutionErrorWsMessage['traceback'] | null
      62              : }
      63              : 
      64              : interface ExecutionNodeInfo {
      65              :   title?: string | null
      66              :   type?: string | null
      67              : }
      68              : 
      69              : export interface QueuedJob {
      70              :   /**
      71              :    * The nodes that are queued to be executed. The key is the node id and the
      72              :    * value is a boolean indicating if the node has been executed.
      73              :    */
      74              :   nodes: Record<string, boolean>
      75              :   startTime?: number
      76              :   submissionAcceptedAt?: number
      77              :   executionStartedAt?: number
      78              :   outcomeTracked?: boolean
      79              :   workflowContext?: WorkflowExecutionContext
      80              :   workflowExecutionIntent?: WorkflowExecutionIntent
      81              :   /**
      82              :    * The workflow that is queued to be executed
      83              :    */
      84              :   workflow?: ComfyWorkflow
      85              :   /**
      86              :    * Queue-time node metadata keyed by execution ID.
      87              :    * This stays stable even if the user switches workflows or edits the canvas.
      88              :    */
      89              :   nodeLookup?: Record<string, ExecutionNodeInfo>
      90              :   /**
      91              :    * Share attribution snapshotted at queue time. Read this instead of
      92              :    * `workflow.shareId`, which can gain attribution after the job was queued.
      93              :    */
      94              :   shareId?: string
      95              :   /**
      96              :    * View-mode attribution snapshotted at queue time, so mode switches during
      97              :    * the run don't misattribute completion events.
      98              :    */
      99              :   viewMode?: AppMode
     100              :   isAppMode?: boolean
     101              : }
     102              : 
     103         2356 : const defaultWorkflowExecutionIntent: WorkflowExecutionIntent = {
     104              :   trigger_source: 'unknown'
     105              : }
     106              : 
     107          150 : function buildExecutionNodeLookup(
     108              :   promptOutput: ComfyApiWorkflow
     109              : ): Record<string, ExecutionNodeInfo> {
     110          150 :   return Object.fromEntries(
     111              :     Object.entries(promptOutput).map(([executionId, node]) => [
     112              :       executionId,
     113              :       {
     114              :         title: node._meta?.title ?? node.class_type,
     115              :         type: node.class_type
     116              :       }
     117              :     ])
     118              :   )
     119              : }
     120              : 
     121              : /**
     122              :  * Maximum number of job entries retained in {@link nodeProgressStatesByJob}.
     123              :  * When exceeded, the oldest entries (by insertion order) are evicted to
     124              :  * prevent unbounded memory growth in long-running sessions.
     125              :  */
     126         2356 : export const MAX_PROGRESS_JOBS = 1000
     127              : 
     128              : export type WorkflowExecutionStatus = 'running' | 'completed' | 'failed'
     129              : 
     130              : interface WorkflowStatusUpdate {
     131              :   status: WorkflowExecutionStatus
     132              :   executionStartedAt?: number
     133              :   endTime?: number
     134              :   failureReason?: WorkflowExecutionFailureReason
     135              :   showStatus?: boolean
     136              : }
     137              : 
     138              : interface PendingExecutionError {
     139              :   detail: ExecutionErrorWsMessage
     140              :   endTime: number
     141              : }
     142              : 
     143         2356 : export const WORKFLOW_STATUS_I18N_KEYS: Record<
     144              :   WorkflowExecutionStatus,
     145              :   string
     146              : > = {
     147              :   running: 'g.running',
     148              :   completed: 'g.completed',
     149              :   failed: 'g.failed'
     150              : }
     151              : 
     152         2356 : export const useExecutionStore = defineStore('execution', () => {
     153         2355 :   const workflowStore = useWorkflowStore()
     154         2355 :   const executionErrorStore = useExecutionErrorStore()
     155         2355 :   const { mode, isAppMode } = useAppMode()
     156              : 
     157         2355 :   const clientId = ref<string | null>(null)
     158         2355 :   const activeJobId = ref<JobId | null>(null)
     159         2355 :   const queuedJobs = ref<Record<JobId, QueuedJob>>({})
     160         2355 :   const jobReset = createEventHook<QueuedJob>()
     161              :   // This is the progress of all nodes in the currently executing workflow
     162         2355 :   const nodeProgressStates = ref<Record<string, NodeProgressState>>({})
     163         2355 :   const nodeProgressStatesByJob = ref<
     164              :     Record<JobId, Record<string, NodeProgressState>>
     165              :   >({})
     166              : 
     167              :   /**
     168              :    * Map of job ID to workflow ID for quick lookup across the app.
     169              :    */
     170         2355 :   const jobIdToWorkflowId = ref<Map<JobId, WorkflowId>>(new Map())
     171              : 
     172              :   /**
     173              :    * Map of job ID to workflow file path in the current session.
     174              :    * Only populated for jobs that are queued in this browser tab.
     175              :    */
     176         2355 :   const jobIdToSessionWorkflowPath = shallowRef<Map<JobId, string>>(new Map())
     177         2355 :   const jobIdToWorkflowInstanceId = new Map<JobId, string>()
     178              : 
     179         2355 :   const jobIdToStampedWorkflowId = new Map<JobId, string>()
     180         2355 :   const MAX_STAMPED_WORKFLOW_ID_ENTRIES = 4000
     181              : 
     182         2355 :   const initializingJobIds = ref<Set<JobId>>(new Set())
     183              : 
     184         2355 :   const workflowStatus = shallowRef<
     185              :     Map<ComfyWorkflow, WorkflowExecutionStatus>
     186              :   >(new Map())
     187              : 
     188         2355 :   const jobIdToWorkflow = new Map<string, ComfyWorkflow>()
     189              : 
     190              :   // Buffers statuses arriving before storeJob attaches the workflow.
     191              :   // FIFO-capped to bound growth if a matching storeJob never fires.
     192         2355 :   const pendingWorkflowStatusByJobId = new Map<string, WorkflowStatusUpdate>()
     193         2355 :   const pendingExecutionErrorsByJobId = new Map<string, PendingExecutionError>()
     194         2355 :   const pendingSkippedOutputsByJobId = new Map<
     195              :     string,
     196              :     Record<string, NodeError>
     197              :   >()
     198              : 
     199            4 :   function bufferPendingWorkflowStatus(
     200              :     jobId: string,
     201              :     update: WorkflowStatusUpdate
     202              :   ) {
     203            4 :     const existing = pendingWorkflowStatusByJobId.get(jobId)
     204            4 :     const executionStartedAt =
     205              :       update.executionStartedAt ??
     206              :       queuedJobs.value[jobId]?.executionStartedAt ??
     207              :       existing?.executionStartedAt
     208            4 :     pendingWorkflowStatusByJobId.delete(jobId)
     209            4 :     pendingWorkflowStatusByJobId.set(jobId, {
     210              :       ...update,
     211              :       ...(executionStartedAt !== undefined && { executionStartedAt })
     212              :     })
     213            4 :     while (pendingWorkflowStatusByJobId.size > MAX_PROGRESS_JOBS) {
     214            0 :       const oldest = pendingWorkflowStatusByJobId.keys().next().value
     215            0 :       if (oldest === undefined) break
     216            0 :       pendingWorkflowStatusByJobId.delete(oldest)
     217              :     }
     218              :   }
     219              : 
     220            2 :   function bufferByJobId<T>(buffer: Map<string, T>, jobId: string, value: T) {
     221            2 :     buffer.delete(jobId)
     222            2 :     buffer.set(jobId, value)
     223            2 :     while (buffer.size > MAX_PROGRESS_JOBS) {
     224            0 :       const oldest = buffer.keys().next().value
     225            0 :       if (oldest === undefined) break
     226            0 :       buffer.delete(oldest)
     227              :     }
     228              :   }
     229              : 
     230            2 :   function bufferPendingExecutionError(error: PendingExecutionError) {
     231            2 :     bufferByJobId(pendingExecutionErrorsByJobId, error.detail.prompt_id, error)
     232              :   }
     233              : 
     234          298 :   function mutateStatus(
     235              :     mutator: (map: Map<ComfyWorkflow, WorkflowExecutionStatus>) => void
     236              :   ) {
     237          298 :     const next = new Map(workflowStatus.value)
     238          298 :     mutator(next)
     239          298 :     workflowStatus.value = next
     240              :   }
     241              : 
     242          215 :   function applyWorkflowStatus(
     243              :     workflow: ComfyWorkflow,
     244              :     status: WorkflowExecutionStatus
     245              :   ) {
     246              :     // A late terminal event can arrive after the tab closed; don't resurrect
     247              :     // an entry (which also pins the workflow ref) for a closed workflow.
     248          215 :     if (!workflowStore.isOpen(workflow)) return
     249          215 :     mutateStatus((m) => m.set(workflow, status))
     250              :   }
     251              : 
     252          228 :   function trackExecutionOutcome(
     253              :     jobId: string,
     254              :     { status, endTime, failureReason }: WorkflowStatusUpdate
     255              :   ) {
     256          228 :     if (status === 'running' || endTime === undefined) return
     257          228 :     if (!(jobId in queuedJobs.value)) return
     258          104 :     const queuedJob = queuedJobs.value[jobId]
     259          104 :     const startTime = queuedJob.startTime
     260          104 :     const workflowExecutionIntent = queuedJob.workflowExecutionIntent
     261          228 :     if (
     262              :       queuedJob.outcomeTracked ||
     263              :       startTime === undefined ||
     264              :       workflowExecutionIntent === undefined
     265              :     )
     266           12 :       return
     267              : 
     268           92 :     queuedJob.outcomeTracked = true
     269          228 :     const metadata = {
     270              :       startTime,
     271              :       ...workflowExecutionIntent,
     272              :       ...(queuedJob.submissionAcceptedAt !== undefined && {
     273              :         submissionAcceptedAt: queuedJob.submissionAcceptedAt
     274              :       }),
     275              :       ...(queuedJob.executionStartedAt !== undefined && {
     276              :         executionStartedAt: queuedJob.executionStartedAt
     277              :       }),
     278              :       endTime,
     279              :       ...(queuedJob.workflowContext && {
     280              :         workflowContext: queuedJob.workflowContext
     281              :       })
     282              :     }
     283          228 :     const telemetry = useTelemetry()
     284          228 :     if (status === 'completed') {
     285           79 :       telemetry?.trackExecutionOutcome({
     286              :         ...metadata,
     287              :         success: true,
     288              :         failureReason: ''
     289              :       })
     290           79 :       return
     291              :     }
     292          228 :     telemetry?.trackExecutionOutcome({
     293              :       ...metadata,
     294              :       success: false,
     295              :       failureReason: failureReason ?? 'execution_failed'
     296              :     })
     297              :   }
     298              : 
     299          229 :   function setWorkflowStatus(jobId: string, update: WorkflowStatusUpdate) {
     300          229 :     const workflow = jobIdToWorkflow.get(jobId)
     301          229 :     if (!workflow) {
     302            4 :       bufferPendingWorkflowStatus(jobId, update)
     303            4 :       return
     304              :     }
     305          225 :     if (update.showStatus !== false) {
     306          212 :       applyWorkflowStatus(workflow, update.status)
     307              :     }
     308          225 :     trackExecutionOutcome(jobId, update)
     309              :   }
     310              : 
     311           83 :   function clearWorkflowStatus(workflow: ComfyWorkflow) {
     312           83 :     if (!workflowStatus.value.has(workflow)) return
     313           83 :     mutateStatus((m) => m.delete(workflow))
     314              :   }
     315              : 
     316        12942 :   function getWorkflowStatus(
     317              :     workflow: ComfyWorkflow | undefined | null
     318              :   ): WorkflowExecutionStatus | undefined {
     319        12942 :     if (!workflow) return undefined
     320         8621 :     return workflowStatus.value.get(workflow)
     321              :   }
     322              : 
     323              :   // Prune statuses for workflows that have been closed.
     324         2355 :   watch(
     325              :     () => workflowStore.openWorkflows,
     326              :     (openWorkflows) => {
     327         4039 :       if (workflowStatus.value.size === 0) return
     328           18 :       const openSet = new Set(openWorkflows)
     329           18 :       const filtered = new Map(
     330              :         [...workflowStatus.value].filter(([w]) => openSet.has(w))
     331              :       )
     332           30 :       if (filtered.size !== workflowStatus.value.size) {
     333            0 :         workflowStatus.value = filtered
     334              :       }
     335              :     }
     336              :   )
     337              : 
     338              :   /**
     339              :    * Cache for executionIdToNodeLocatorId lookups.
     340              :    * Avoids redundant graph traversals during a single execution run.
     341              :    * Cleared at execution start and end to ensure fresh graph state.
     342              :    */
     343         2355 :   const executionIdToLocatorCache = new Map<string, NodeLocatorId | undefined>()
     344              : 
     345          149 :   function cachedExecutionIdToLocator(
     346              :     executionId: string
     347              :   ): NodeLocatorId | undefined {
     348          149 :     if (executionIdToLocatorCache.has(executionId)) {
     349           59 :       return executionIdToLocatorCache.get(executionId)
     350              :     }
     351           90 :     const locatorId = executionIdToNodeLocatorId(
     352              :       useApp().rootGraph,
     353              :       executionId
     354              :     )
     355           90 :     executionIdToLocatorCache.set(executionId, locatorId)
     356           90 :     return locatorId
     357              :   }
     358              : 
     359         2355 :   const mergeExecutionProgressStates = (
     360              :     currentState: NodeProgressState | undefined,
     361              :     newState: NodeProgressState
     362              :   ): NodeProgressState => {
     363          149 :     if (currentState === undefined) {
     364          142 :       return newState
     365              :     }
     366              : 
     367            7 :     const mergedState = { ...currentState }
     368           32 :     if (mergedState.state === 'error') {
     369            0 :       return mergedState
     370            7 :     } else if (newState.state === 'running') {
     371            3 :       const newPerc = newState.max > 0 ? newState.value / newState.max : 0.0
     372            3 :       const oldPerc =
     373              :         mergedState.max > 0 ? mergedState.value / mergedState.max : 0.0
     374            3 :       if (
     375              :         mergedState.state !== 'running' ||
     376              :         oldPerc === 0.0 ||
     377              :         newPerc < oldPerc
     378              :       ) {
     379            3 :         mergedState.value = newState.value
     380            3 :         mergedState.max = newState.max
     381              :       }
     382            3 :       mergedState.state = 'running'
     383              :     }
     384              : 
     385            7 :     return mergedState
     386              :   }
     387              : 
     388         2355 :   const nodeLocationProgressStates = computed<
     389              :     Record<NodeLocatorId, NodeProgressState>
     390              :   >(() => {
     391         6328 :     const result: Record<NodeLocatorId, NodeProgressState> = {}
     392              : 
     393         6328 :     const states = nodeProgressStates.value // Apparently doing this inside `Object.entries` causes issues
     394         6328 :     for (const state of Object.values(states)) {
     395          125 :       const parts = String(state.display_node_id).split(':')
     396          125 :       for (let i = 0; i < parts.length; i++) {
     397              :         const executionId = parts.slice(0, i + 1).join(':')
     398          149 :         const locatorId = cachedExecutionIdToLocator(executionId)
     399          149 :         if (!locatorId) continue
     400              : 
     401          149 :         result[locatorId] = mergeExecutionProgressStates(
     402              :           result[locatorId],
     403              :           state
     404              :         )
     405              :       }
     406              :     }
     407              : 
     408         6328 :     return result
     409              :   })
     410              : 
     411              :   // Easily access all currently executing node IDs
     412         2355 :   const executingNodeIds = computed<string[]>(() => {
     413          220 :     return Object.entries(nodeProgressStates.value)
     414              :       .filter(([_, state]) => state.state === 'running')
     415              :       .map(([nodeId, _]) => nodeId)
     416              :   })
     417              : 
     418              :   // @deprecated For backward compatibility - stores the primary executing node ID
     419         2355 :   const executingNodeId = computed<string | null>(() => {
     420          220 :     return executingNodeIds.value[0] ?? null
     421              :   })
     422              : 
     423         2355 :   const uniqueExecutingNodeIdStrings = computed(
     424              :     () => new Set(executingNodeIds.value.map(String))
     425              :   )
     426              : 
     427              :   // For backward compatibility - returns the primary executing node info
     428         2355 :   const executingNode = computed<ExecutionNodeInfo | null>(() => {
     429          189 :     if (!executingNodeId.value) return null
     430              : 
     431          189 :     return activeJob.value?.nodeLookup?.[executingNodeId.value] ?? null
     432              :   })
     433              : 
     434              :   // This is the progress of the currently executing node (for backward compatibility)
     435         2355 :   const _executingNodeProgress = ref<ProgressWsMessage | null>(null)
     436         2355 :   const executingNodeProgress = computed(() =>
     437              :     _executingNodeProgress.value
     438              :       ? _executingNodeProgress.value.value / _executingNodeProgress.value.max
     439              :       : null
     440              :   )
     441              : 
     442         2355 :   const activeJob = computed<QueuedJob | undefined>(
     443              :     () => queuedJobs.value[activeJobId.value ?? '']
     444              :   )
     445              : 
     446         2355 :   const totalNodesToExecute = computed<number>(() => {
     447          236 :     if (!activeJob.value) return 0
     448          236 :     return Object.values(activeJob.value.nodes).length
     449              :   })
     450              : 
     451         2355 :   const isIdle = computed<boolean>(() => !activeJobId.value)
     452              : 
     453         2355 :   const nodesExecuted = computed<number>(() => {
     454          236 :     if (!activeJob.value) return 0
     455          236 :     return Object.values(activeJob.value.nodes).filter(Boolean).length
     456              :   })
     457              : 
     458         2355 :   const executionProgress = computed<number>(() => {
     459         2678 :     if (!activeJob.value) return 0
     460          236 :     const total = totalNodesToExecute.value
     461          236 :     const done = nodesExecuted.value
     462         2678 :     return total > 0 ? done / total : 0
     463              :   })
     464              : 
     465         2355 :   function bindExecutionEvents() {
     466         2355 :     api.addEventListener('notification', handleNotification)
     467         2355 :     api.addEventListener('execution_start', handleExecutionStart)
     468         2355 :     api.addEventListener('execution_cached', handleExecutionCached)
     469         2355 :     api.addEventListener('execution_interrupted', handleExecutionInterrupted)
     470         2355 :     api.addEventListener('execution_success', handleExecutionSuccess)
     471         2355 :     api.addEventListener('executed', handleExecuted)
     472         2355 :     api.addEventListener('executing', handleExecuting)
     473         2355 :     api.addEventListener('progress', handleProgress)
     474         2355 :     api.addEventListener('progress_state', handleProgressState)
     475         2355 :     api.addEventListener('status', handleStatus)
     476         2355 :     api.addEventListener('execution_error', handleExecutionError)
     477         2355 :     api.addEventListener('prompt_outputs_skipped', handlePromptOutputsSkipped)
     478              :   }
     479              : 
     480            0 :   function unbindExecutionEvents() {
     481            0 :     api.removeEventListener('notification', handleNotification)
     482            0 :     api.removeEventListener('execution_start', handleExecutionStart)
     483            0 :     api.removeEventListener('execution_cached', handleExecutionCached)
     484            0 :     api.removeEventListener('execution_interrupted', handleExecutionInterrupted)
     485            0 :     api.removeEventListener('execution_success', handleExecutionSuccess)
     486            0 :     api.removeEventListener('executed', handleExecuted)
     487            0 :     api.removeEventListener('executing', handleExecuting)
     488            0 :     api.removeEventListener('progress', handleProgress)
     489            0 :     api.removeEventListener('progress_state', handleProgressState)
     490            0 :     api.removeEventListener('status', handleStatus)
     491            0 :     api.removeEventListener('execution_error', handleExecutionError)
     492            0 :     api.removeEventListener(
     493              :       'prompt_outputs_skipped',
     494              :       handlePromptOutputsSkipped
     495              :     )
     496              : 
     497            0 :     if (workflowStatus.value.size > 0) workflowStatus.value = new Map()
     498            0 :     pendingWorkflowStatusByJobId.clear()
     499            0 :     pendingExecutionErrorsByJobId.clear()
     500            0 :     pendingSkippedOutputsByJobId.clear()
     501            0 :     jobIdToWorkflow.clear()
     502            0 :     jobIdToStampedWorkflowId.clear()
     503              : 
     504            0 :     cancelPendingProgressUpdates()
     505              :   }
     506              : 
     507          364 :   function routableWorkflowId(id: string | undefined): string | undefined {
     508          364 :     return !id || id === zeroUuid ? undefined : id
     509              :   }
     510              : 
     511          352 :   function rememberStampedWorkflowId(
     512              :     jobId: JobId,
     513              :     workflowId: string | undefined
     514              :   ): void {
     515          352 :     const stamped = routableWorkflowId(workflowId)
     516          352 :     if (!stamped || jobIdToStampedWorkflowId.has(jobId)) return
     517           55 :     jobIdToStampedWorkflowId.set(jobId, stamped)
     518          266 :     while (jobIdToStampedWorkflowId.size > MAX_STAMPED_WORKFLOW_ID_ENTRIES) {
     519            0 :       const oldest = jobIdToStampedWorkflowId.keys().next().value
     520            0 :       if (oldest === undefined) break
     521            0 :       jobIdToStampedWorkflowId.delete(oldest)
     522              :     }
     523              :   }
     524              : 
     525              :   type OwnershipLeg = 'instance' | 'path' | 'workflow-id' | 'active-job'
     526              : 
     527              :   interface WorkflowOwnership {
     528              :     owned: boolean
     529              :     leg: OwnershipLeg
     530              :   }
     531              : 
     532          352 :   function ownershipByInstance(
     533              :     jobId: JobId,
     534              :     activeWorkflow: ComfyWorkflow
     535              :   ): WorkflowOwnership | undefined {
     536          352 :     const ownerInstanceId = jobIdToWorkflowInstanceId.get(jobId)
     537          352 :     if (!ownerInstanceId || !activeWorkflow.instanceId) return
     538          346 :     return {
     539              :       owned: ownerInstanceId === activeWorkflow.instanceId,
     540              :       leg: 'instance'
     541              :     }
     542              :   }
     543              : 
     544            6 :   function ownershipByPath(
     545              :     jobId: JobId,
     546              :     activeWorkflow: ComfyWorkflow
     547              :   ): WorkflowOwnership | undefined {
     548            6 :     const ownerPath = jobIdToSessionWorkflowPath.value.get(jobId)
     549            6 :     if (!ownerPath || !activeWorkflow.path) return
     550            0 :     return { owned: ownerPath === activeWorkflow.path, leg: 'path' }
     551              :   }
     552              : 
     553            6 :   function ownershipByWorkflowId(
     554              :     jobId: JobId,
     555              :     activeWorkflow: ComfyWorkflow,
     556              :     workflowId: string | undefined
     557              :   ): WorkflowOwnership | undefined {
     558            6 :     const activeGraphId = routableWorkflowId(
     559              :       activeWorkflow.activeState?.id ?? activeWorkflow.initialState?.id
     560              :     )
     561            6 :     const frameWorkflowId =
     562              :       routableWorkflowId(workflowId) ?? jobIdToStampedWorkflowId.get(jobId)
     563            6 :     if (!frameWorkflowId || !activeGraphId) return
     564            3 :     return { owned: frameWorkflowId === activeGraphId, leg: 'workflow-id' }
     565              :   }
     566              : 
     567          352 :   function resolveWorkflowOwnership(
     568              :     jobId: JobId,
     569              :     workflowId?: string
     570              :   ): WorkflowOwnership {
     571          352 :     const ownedByActiveJob = (): WorkflowOwnership => ({
     572              :       owned: !activeJobId.value || jobId === activeJobId.value,
     573              :       leg: 'active-job'
     574              :     })
     575              : 
     576          352 :     const activeWorkflow = workflowStore.activeWorkflow
     577          352 :     if (!activeWorkflow) return ownedByActiveJob()
     578              : 
     579          352 :     return (
     580              :       ownershipByInstance(jobId, activeWorkflow) ??
     581              :       ownershipByPath(jobId, activeWorkflow) ??
     582              :       ownershipByWorkflowId(jobId, activeWorkflow, workflowId) ??
     583              :       ownedByActiveJob()
     584              :     )
     585              :   }
     586              : 
     587          267 :   function rememberStampAndCheckActiveWorkflow(
     588              :     jobId: JobId | undefined,
     589              :     workflowId?: string
     590              :   ): boolean {
     591          267 :     if (!jobId) return true
     592          227 :     rememberStampedWorkflowId(jobId, workflowId)
     593          227 :     return resolveWorkflowOwnership(jobId, workflowId).owned
     594              :   }
     595              : 
     596          125 :   function handleExecutionStart(e: CustomEvent<ExecutionStartWsMessage>) {
     597          125 :     const jobId = e.detail.prompt_id
     598          125 :     queuedJobs.value[jobId] ??= { nodes: {} }
     599          125 :     clearInitializationByJobId(jobId)
     600              : 
     601          125 :     if (!jobIdToSessionWorkflowPath.value.has(jobId)) {
     602            4 :       const workflow = queuedJobs.value[jobId]?.workflow
     603            4 :       if (workflow) {
     604            0 :         ensureSessionWorkflowPath(jobId, workflow.path, workflow.instanceId)
     605              :       }
     606              :     }
     607          125 :     queuedJobs.value[jobId].executionStartedAt ??= performance.now()
     608          125 :     setWorkflowStatus(jobId, {
     609              :       status: 'running',
     610              :       executionStartedAt: queuedJobs.value[jobId].executionStartedAt
     611              :     })
     612              : 
     613          125 :     rememberStampedWorkflowId(jobId, e.detail.workflow_id)
     614          125 :     const ownership = resolveWorkflowOwnership(jobId, e.detail.workflow_id)
     615          125 :     if (!ownership.owned) {
     616            6 :       if (ownership.leg === 'active-job') {
     617            0 :         reportAmbiguousExecutionStartRejection(jobId)
     618              :       }
     619            6 :       return
     620              :     }
     621          119 :     executionIdToLocatorCache.clear()
     622          119 :     executionErrorStore.clearExecutionStartErrors(runErrorKeyForJob(jobId))
     623          119 :     activeJobId.value = jobId
     624              :   }
     625              : 
     626            0 :   function reportAmbiguousExecutionStartRejection(jobId: JobId): void {
     627            0 :     if (jobIdToWorkflowInstanceId.get(jobId)) return
     628            0 :     reportError(
     629              :       new Error('execution_start rejected for a job with no known owner'),
     630              :       {
     631              :         surface: 'graph',
     632              :         errorType: 'warning_rejecting_unowned_execution_start',
     633              :         level: 'warning',
     634              :         context: {
     635              :           jobId,
     636              :           activeJobId: activeJobId.value,
     637              :           hasOwnWorkflowRecord: !!queuedJobs.value[jobId]?.workflow,
     638              :           activeWorkflowInstanceId: workflowStore.activeWorkflow?.instanceId
     639              :         }
     640              :       }
     641              :     )
     642              :   }
     643              : 
     644           55 :   function handleExecutionCached(e: CustomEvent<ExecutionCachedWsMessage>) {
     645           55 :     const jobId = e.detail.prompt_id
     646           55 :     if (!(jobId in queuedJobs.value)) return
     647           55 :     for (const n of e.detail.nodes) {
     648           29 :       queuedJobs.value[jobId].nodes[n] = true
     649              :     }
     650              :   }
     651              : 
     652            1 :   function handleExecutionInterrupted(
     653              :     e: CustomEvent<ExecutionInterruptedWsMessage>
     654              :   ) {
     655            1 :     const jobId = e.detail.prompt_id
     656            1 :     pendingExecutionErrorsByJobId.delete(jobId)
     657            1 :     setWorkflowStatus(jobId, {
     658              :       status: 'failed',
     659              :       endTime: performance.now(),
     660              :       failureReason: 'execution_interrupted',
     661              :       showStatus: false
     662              :     })
     663            1 :     const workflow = jobIdToWorkflow.get(jobId)
     664            1 :     if (workflow) clearWorkflowStatus(workflow)
     665            1 :     if (activeJobId.value) clearInitializationByJobId(activeJobId.value)
     666            1 :     resetExecutionState(jobId)
     667              :   }
     668              : 
     669          155 :   function handleExecuted(e: CustomEvent<ExecutedWsMessage>) {
     670          155 :     const jobId = e.detail.prompt_id
     671          155 :     if (!(jobId in queuedJobs.value)) return
     672          127 :     queuedJobs.value[jobId].nodes[e.detail.node] = true
     673              :   }
     674              : 
     675           79 :   function handleExecutionSuccess(e: CustomEvent<ExecutionSuccessWsMessage>) {
     676           79 :     const jobId = e.detail.prompt_id
     677           79 :     pendingExecutionErrorsByJobId.delete(jobId)
     678           79 :     setWorkflowStatus(jobId, {
     679              :       status: 'completed',
     680              :       endTime: performance.now()
     681              :     })
     682           79 :     const queuedJob = queuedJobs.value[jobId]
     683           79 :     const telemetry = useTelemetry()
     684           79 :     if (jobId in queuedJobs.value) {
     685           79 :       telemetry?.trackExecutionSuccess({
     686              :         jobId
     687              :       })
     688           79 :       if (queuedJob.shareId) {
     689            0 :         telemetry?.trackSharedWorkflowRun({
     690              :           job_id: jobId,
     691              :           share_id: queuedJob.shareId,
     692              :           view_mode: queuedJob.viewMode ?? mode.value,
     693              :           is_app_mode: queuedJob.isAppMode ?? isAppMode.value
     694              :         })
     695              :       }
     696              :     }
     697           79 :     resetExecutionState(jobId)
     698              :   }
     699              : 
     700          192 :   function handleExecuting(e: CustomEvent<string | number | null>): void {
     701          192 :     pendingProgressByJob.clear()
     702          192 :     progressCoalescer.cancel()
     703          192 :     if (e.detail == null) {
     704           61 :       pendingProgressStatesByJob.clear()
     705           61 :       progressStateCoalescer.cancel()
     706              :     }
     707          192 :     _executingNodeProgress.value = null
     708              : 
     709          192 :     if (!activeJob.value) return
     710              : 
     711          187 :     if (e.detail == null) {
     712           13 :       activeJobId.value = null
     713              :     }
     714              :   }
     715              : 
     716              :   /**
     717              :    * Evicts the oldest entries from {@link nodeProgressStatesByJob} when the
     718              :    * map exceeds {@link MAX_PROGRESS_JOBS}, preventing unbounded memory
     719              :    * growth in long-running sessions.
     720              :    *
     721              :    * Relies on ES2015+ object key insertion order: the first keys returned
     722              :    * by `Object.keys` are the oldest entries.
     723              :    *
     724              :    * @example
     725              :    * ```ts
     726              :    * // Given 105 entries, evicts the 5 oldest:
     727              :    * evictOldProgressJobs()
     728              :    * Object.keys(nodeProgressStatesByJob.value).length // => 100
     729              :    * ```
     730              :    */
     731           80 :   function evictOldProgressJobs() {
     732           80 :     const current = nodeProgressStatesByJob.value
     733           80 :     const keys = Object.keys(current)
     734           80 :     if (keys.length <= MAX_PROGRESS_JOBS) return
     735              : 
     736            0 :     const pruned: Record<string, Record<string, NodeProgressState>> = {}
     737            0 :     const keysToKeep = keys.slice(keys.length - MAX_PROGRESS_JOBS)
     738            0 :     for (const key of keysToKeep) {
     739            0 :       pruned[key] = current[key]
     740              :     }
     741            0 :     nodeProgressStatesByJob.value = pruned
     742              :   }
     743              : 
     744         2355 :   const pendingProgressStatesByJob = new Map<JobId, ProgressStateWsMessage>()
     745         2355 :   const progressStateCoalescer = createRafCoalescer<null>(() => {
     746           80 :     const updates = [...pendingProgressStatesByJob.values()]
     747           80 :     pendingProgressStatesByJob.clear()
     748           80 :     for (const detail of updates) applyProgressState(detail)
     749              :   }, 'raf:progress_state')
     750              : 
     751          242 :   function handleProgressState(e: CustomEvent<ProgressStateWsMessage>) {
     752          242 :     const jobId = e.detail.prompt_id
     753          242 :     pendingProgressStatesByJob.delete(jobId)
     754          242 :     pendingProgressStatesByJob.set(jobId, e.detail)
     755          242 :     progressStateCoalescer.push(null)
     756              :   }
     757              : 
     758           79 :   function revokeStartedNodePreviews(
     759              :     nodes: Record<string, NodeProgressState>,
     760              :     previousForJob: Record<string, NodeProgressState>
     761              :   ) {
     762           79 :     const { revokePreviewsByExecutionId } = useNodeOutputStore()
     763           79 :     for (const nodeId in nodes) {
     764          125 :       if (nodes[nodeId].state !== 'running' || nodeId in previousForJob)
     765           71 :         continue
     766           54 :       const executionId = tryNormalizeNodeExecutionId(nodeId)
     767           54 :       if (executionId) revokePreviewsByExecutionId(executionId)
     768              :     }
     769              :   }
     770              : 
     771           80 :   function applyProgressState(detail: ProgressStateWsMessage) {
     772           80 :     const { nodes, prompt_id: jobId, workflow_id: workflowId } = detail
     773           80 :     const isOwned = rememberStampAndCheckActiveWorkflow(jobId, workflowId)
     774              : 
     775           80 :     if (isOwned) {
     776           79 :       revokeStartedNodePreviews(
     777              :         nodes,
     778              :         nodeProgressStatesByJob.value[jobId] ?? {}
     779              :       )
     780              :     }
     781              : 
     782           80 :     nodeProgressStatesByJob.value = {
     783              :       ...nodeProgressStatesByJob.value,
     784              :       [jobId]: nodes
     785              :     }
     786           80 :     evictOldProgressJobs()
     787              : 
     788           80 :     if (!isOwned) return
     789           79 :     nodeProgressStates.value = nodes
     790              : 
     791           79 :     if (executingNodeId.value) {
     792           58 :       const nodeState = Object.hasOwn(nodes, executingNodeId.value)
     793              :         ? nodes[executingNodeId.value]
     794              :         : undefined
     795           58 :       if (!nodeState) return
     796           58 :       _executingNodeProgress.value = {
     797              :         value: nodeState.value,
     798              :         max: nodeState.max,
     799              :         prompt_id: nodeState.prompt_id,
     800              :         node: nodeState.display_node_id || nodeState.node_id
     801              :       }
     802              :     }
     803              :   }
     804              : 
     805         2355 :   const pendingProgressByJob = new Map<JobId, ProgressWsMessage>()
     806         2355 :   const progressCoalescer = createRafCoalescer<null>(() => {
     807           12 :     const updates = [...pendingProgressByJob.values()]
     808           12 :     pendingProgressByJob.clear()
     809           12 :     for (const detail of updates) {
     810           13 :       if (activeJobId.value && detail.prompt_id !== activeJobId.value) continue
     811           12 :       _executingNodeProgress.value = detail
     812              :     }
     813              :   }, 'raf:progress')
     814              : 
     815           22 :   function handleProgress(e: CustomEvent<ProgressWsMessage>) {
     816           22 :     const jobId = e.detail.prompt_id
     817           22 :     pendingProgressByJob.delete(jobId)
     818           22 :     pendingProgressByJob.set(jobId, e.detail)
     819           22 :     progressCoalescer.push(null)
     820              :   }
     821              : 
     822           98 :   function cancelPendingProgressUpdates(jobId?: JobId | null) {
     823           98 :     if (!jobId) {
     824            0 :       pendingProgressByJob.clear()
     825            0 :       pendingProgressStatesByJob.clear()
     826            0 :       progressCoalescer.cancel()
     827            0 :       progressStateCoalescer.cancel()
     828            0 :       return
     829              :     }
     830              : 
     831           98 :     pendingProgressByJob.delete(jobId)
     832           98 :     pendingProgressStatesByJob.delete(jobId)
     833           98 :     if (pendingProgressByJob.size === 0) progressCoalescer.cancel()
     834           98 :     if (pendingProgressStatesByJob.size === 0) progressStateCoalescer.cancel()
     835              :   }
     836              : 
     837         2353 :   function handleStatus() {
     838         2353 :     if (api.clientId) {
     839         2353 :       clientId.value = api.clientId
     840              : 
     841              :       // Once we've received the clientId we no longer need to listen
     842         2353 :       api.removeEventListener('status', handleStatus)
     843              :     }
     844              :   }
     845              : 
     846              :   /**
     847              :    * Queue and history polling supply a workflow id but no path, so the open
     848              :    * workflow carrying that root graph provides it. Two open workflows can share
     849              :    * a root graph id — an imported copy of one already on screen — and those runs
     850              :    * belong to different error buckets, so an ambiguous match resolves to
     851              :    * nothing rather than the wrong workflow.
     852              :    */
     853            0 :   function openWorkflowPathForGraph(graphId: WorkflowId): string | undefined {
     854            0 :     const matches = workflowStore.openWorkflows.filter(
     855              :       (w) => (w.activeState?.id ?? w.initialState?.id) === graphId
     856              :     )
     857            0 :     return matches.length === 1 ? matches[0].path : undefined
     858              :   }
     859              : 
     860              :   /**
     861              :    * Run-error key of the workflow that produced `jobId`, for filing its errors
     862              :    * against that workflow rather than whichever one is currently on screen.
     863              :    *
     864              :    * `jobIdToWorkflow` only covers jobs queued by this browser session and is
     865              :    * purged the moment a run ends, so `jobIdToWorkflowId` — also populated from
     866              :    * queue and history polling — backs it up for jobs from a previous page load.
     867              :    *
     868              :    * `null` means the job cannot be attributed to one known workflow, so
     869              :    * recording is suppressed rather than assigning it to whichever workflow is
     870              :    * currently visible.
     871              :    */
     872          230 :   function runErrorKeyForJob(jobId: string): string | null {
     873          230 :     const workflow = jobIdToWorkflow.get(jobId)
     874          230 :     const graphId =
     875              :       workflow?.activeState?.id ??
     876              :       workflow?.initialState?.id ??
     877              :       jobIdToWorkflowId.value.get(jobId)
     878          230 :     if (graphId === undefined) return null
     879              : 
     880          230 :     const path =
     881              :       workflow?.path ??
     882              :       jobIdToSessionWorkflowPath.value.get(jobId) ??
     883              :       openWorkflowPathForGraph(graphId)
     884          230 :     if (path === undefined) return null
     885              : 
     886          222 :     return executionErrorStore.runErrorKey(graphId, path)
     887              :   }
     888              : 
     889           13 :   function handleExecutionError(e: CustomEvent<ExecutionErrorWsMessage>) {
     890           13 :     const endTime = performance.now()
     891              :     // Resolved up front: resetExecutionState() drops the job's workflow entry
     892              :     // before the handlers below record anything.
     893           13 :     const runErrorKey = runErrorKeyForJob(e.detail.prompt_id)
     894           13 :     if (runErrorKey === null) {
     895            2 :       bufferPendingExecutionError({ detail: e.detail, endTime })
     896            2 :       resetExecutionState(e.detail.prompt_id)
     897            2 :       return
     898              :     }
     899              : 
     900           11 :     processExecutionError(e.detail, runErrorKey, endTime)
     901              :   }
     902              : 
     903            1 :   function handlePromptOutputsSkipped(
     904              :     e: CustomEvent<PromptOutputsSkippedData>
     905              :   ) {
     906            1 :     const nodeErrors = normalizeRelayedNodeErrors(e.detail.node_errors)
     907            1 :     if (Object.keys(nodeErrors).length === 0) return
     908            1 :     recordSkippedOutputs(e.detail.prompt_id, nodeErrors)
     909              :   }
     910              : 
     911            1 :   function recordSkippedOutputs(
     912              :     jobId: string,
     913              :     nodeErrors: Record<string, NodeError>
     914              :   ) {
     915            1 :     const runErrorKey = runErrorKeyForJob(jobId)
     916            1 :     if (runErrorKey === null) {
     917            0 :       bufferByJobId(pendingSkippedOutputsByJobId, jobId, nodeErrors)
     918            0 :       return
     919              :     }
     920            1 :     executionErrorStore.surfaceNodeErrors(nodeErrors, runErrorKey, jobId)
     921              :   }
     922              : 
     923          182 :   function flushPendingSkippedOutputs(jobId: string) {
     924          182 :     const pending = pendingSkippedOutputsByJobId.get(jobId)
     925          182 :     if (!pending) return
     926            0 :     pendingSkippedOutputsByJobId.delete(jobId)
     927            0 :     recordSkippedOutputs(jobId, pending)
     928              :   }
     929              : 
     930           12 :   function processExecutionError(
     931              :     detail: ExecutionErrorWsMessage,
     932              :     runErrorKey: string,
     933              :     endTime: number
     934              :   ) {
     935           12 :     setWorkflowStatus(detail.prompt_id, {
     936              :       status: 'failed',
     937              :       endTime,
     938              :       failureReason: 'execution_failed',
     939              :       showStatus: false
     940              :     })
     941           12 :     useTelemetry()?.trackExecutionError({
     942              :       jobId: detail.prompt_id,
     943              :       nodeId: String(detail.node_id),
     944              :       nodeType: detail.node_type,
     945              :       error: detail.exception_message
     946              :     })
     947              : 
     948              :     if (isCloud) {
     949              :       // Cloud wraps validation errors (400) in exception_message as embedded JSON.
     950              :       // Pre-flight validation isn't a runtime failure — no badge.
     951              :       if (handleCloudValidationError(detail, runErrorKey)) {
     952              :         executionErrorStore.showExecutionError(detail, runErrorKey)
     953              :         return
     954              :       }
     955              :     }
     956              : 
     957              :     // Account preconditions (sign-in, subscription, credits) open their own
     958              :     // modal and must stay out of the error panel and error count.
     959           12 :     if (handleAccountPreconditionError(detail)) return
     960              : 
     961              :     // Service-level errors (e.g. "Job has stagnated") have no associated node.
     962           12 :     if (handleServiceLevelError(detail, runErrorKey)) {
     963            0 :       executionErrorStore.showExecutionError(detail, runErrorKey)
     964            0 :       return
     965              :     }
     966              : 
     967           12 :     setWorkflowStatus(detail.prompt_id, {
     968              :       status: 'failed',
     969              :       endTime,
     970              :       failureReason: 'execution_failed'
     971              :     })
     972           12 :     executionErrorStore.recordExecutionError(detail, runErrorKey)
     973           12 :     executionErrorStore.showExecutionError(detail, runErrorKey)
     974           12 :     clearInitializationByJobId(detail.prompt_id)
     975           12 :     resetExecutionState(detail.prompt_id)
     976              :   }
     977              : 
     978           12 :   function handleAccountPreconditionError(
     979              :     detail: ExecutionErrorWsMessage
     980              :   ): boolean {
     981              :     const precondition = resolveAccountPrecondition({
     982              :       exceptionType: detail.exception_type,
     983              :       exceptionMessage: detail.exception_message
     984              :     })
     985           12 :     if (!precondition) return false
     986              : 
     987            0 :     const workflow = jobIdToWorkflow.get(detail.prompt_id)
     988            0 :     if (workflow) clearWorkflowStatus(workflow)
     989            0 :     clearInitializationByJobId(detail.prompt_id)
     990            0 :     resetExecutionState(detail.prompt_id)
     991            0 :     return true
     992              :   }
     993              : 
     994           12 :   function handleServiceLevelError(
     995              :     detail: RuntimeExecutionError,
     996              :     runErrorKey: string | null | undefined
     997              :   ): boolean {
     998           12 :     const { node_id: nodeId } = detail
     999           12 :     if (nodeId !== null && nodeId !== undefined && String(nodeId) !== '')
    1000           12 :       return false
    1001              : 
    1002            0 :     clearInitializationByJobId(detail.prompt_id)
    1003            0 :     resetExecutionState(detail.prompt_id)
    1004           12 :     executionErrorStore.recordPromptError(
    1005              :       {
    1006              :         type: detail.exception_type || 'error',
    1007              :         message: detail.exception_type
    1008              :           ? `${detail.exception_type}: ${detail.exception_message}`
    1009              :           : detail.exception_message || '',
    1010              :         details: detail.traceback?.join('\n') ?? ''
    1011              :       },
    1012              :       runErrorKey
    1013              :     )
    1014           12 :     return true
    1015              :   }
    1016              : 
    1017              :   function handleCloudValidationError(
    1018              :     detail: ExecutionErrorWsMessage,
    1019              :     runErrorKey: string | null | undefined
    1020              :   ): boolean {
    1021              :     const result = classifyCloudValidationError(detail.exception_message)
    1022              :     if (!result) return false
    1023              : 
    1024              :     clearInitializationByJobId(detail.prompt_id)
    1025              :     resetExecutionState(detail.prompt_id)
    1026              : 
    1027              :     if (result.kind === 'nodeErrors') {
    1028              :       executionErrorStore.recordNodeErrors(
    1029              :         result.nodeErrors,
    1030              :         runErrorKey,
    1031              :         detail.prompt_id
    1032              :       )
    1033              :     } else {
    1034              :       executionErrorStore.recordPromptError(result.promptError, runErrorKey)
    1035              :     }
    1036              :     return true
    1037              :   }
    1038              : 
    1039              :   /**
    1040              :    * Notification handler used for frontend/cloud initialization tracking.
    1041              :    * Marks a job as initializing when cloud notifies it is waiting for a machine.
    1042              :    */
    1043            0 :   function handleNotification(e: CustomEvent<NotificationWsMessage>) {
    1044            0 :     const payload = e.detail
    1045            0 :     const text = payload.value || ''
    1046            0 :     const id = payload.id ? payload.id : ''
    1047            0 :     if (!id) return
    1048              :     // Until cloud implements a proper message
    1049            0 :     if (text.includes('Waiting for a machine')) {
    1050            0 :       const next = new Set(initializingJobIds.value)
    1051            0 :       next.add(id)
    1052            0 :       initializingJobIds.value = next
    1053              :     }
    1054              :   }
    1055              : 
    1056          137 :   function clearInitializationByJobId(jobId: JobId | null) {
    1057          137 :     if (!jobId) return
    1058          137 :     if (!initializingJobIds.value.has(jobId)) return
    1059            0 :     const next = new Set(initializingJobIds.value)
    1060            0 :     next.delete(jobId)
    1061            0 :     initializingJobIds.value = next
    1062              :   }
    1063              : 
    1064         2581 :   function clearInitializationByJobIds(jobIds: JobId[]) {
    1065         2581 :     if (!jobIds.length) return
    1066            1 :     const current = initializingJobIds.value
    1067            1 :     const toRemove = jobIds.filter((id) => current.has(id))
    1068            1 :     if (!toRemove.length) return
    1069            0 :     const next = new Set(current)
    1070            0 :     for (const id of toRemove) {
    1071            0 :       next.delete(id)
    1072              :     }
    1073            0 :     initializingJobIds.value = next
    1074              :   }
    1075              : 
    1076         2580 :   function reconcileInitializingJobs(activeJobIds: Set<JobId>) {
    1077              :     const orphaned = [...initializingJobIds.value].filter(
    1078              :       (id) => !activeJobIds.has(id)
    1079              :     )
    1080         2580 :     clearInitializationByJobIds(orphaned)
    1081              :   }
    1082              : 
    1083              :   /**
    1084              :    * Clears the active job if the server's queue snapshot doesn't list it.
    1085              :    * Used after WS reconnect to recover from stale state when a job finished
    1086              :    * during the disconnect window.
    1087              :    */
    1088            6 :   function clearActiveJobIfStale(activeJobIds: Set<JobId>) {
    1089            6 :     const id = activeJobId.value
    1090            6 :     if (id && !activeJobIds.has(id)) resetExecutionState(id)
    1091              :   }
    1092              : 
    1093            6 :   function forgetFinishedJobs(jobIds: Iterable<JobId>) {
    1094            6 :     for (const jobId of jobIds) {
    1095            2 :       if (jobId === activeJobId.value || !(jobId in queuedJobs.value)) continue
    1096            2 :       const workflow = jobIdToWorkflow.get(jobId)
    1097            2 :       forgetJob(jobId)
    1098            2 :       if (
    1099              :         workflow &&
    1100              :         getWorkflowStatus(workflow) === 'running' &&
    1101              :         !hasStartedJobForWorkflow(workflow)
    1102              :       ) {
    1103            1 :         clearWorkflowStatus(workflow)
    1104              :       }
    1105              :     }
    1106              :   }
    1107              : 
    1108            2 :   function hasStartedJobForWorkflow(workflow: ComfyWorkflow): boolean {
    1109            2 :     return Object.entries(queuedJobs.value).some(
    1110              :       ([jobId, job]) =>
    1111              :         job.executionStartedAt !== undefined &&
    1112              :         jobIdToWorkflow.get(jobId) === workflow
    1113              :     )
    1114              :   }
    1115              : 
    1116           98 :   function forgetJob(jobId: JobId) {
    1117           98 :     const progress = { ...nodeProgressStatesByJob.value }
    1118           98 :     delete progress[jobId]
    1119           98 :     nodeProgressStatesByJob.value = progress
    1120           98 :     useJobPreviewStore().clearPreview(jobId)
    1121           98 :     jobIdToWorkflow.delete(jobId)
    1122           98 :     jobIdToStampedWorkflowId.delete(jobId)
    1123           98 :     if (jobId in queuedJobs.value)
    1124           97 :       void jobReset.trigger(queuedJobs.value[jobId])
    1125           98 :     delete queuedJobs.value[jobId]
    1126           98 :     cancelPendingProgressUpdates(jobId)
    1127              :   }
    1128              : 
    1129         2133 :   function isJobInitializing(jobId: JobId | number | undefined): boolean {
    1130         2133 :     if (!jobId) return false
    1131         2133 :     return initializingJobIds.value.has(String(jobId))
    1132              :   }
    1133              : 
    1134              :   /**
    1135              :    * Reset execution-related state after a run completes or is stopped.
    1136              :    */
    1137           96 :   function resetExecutionState(jobIdParam?: JobId | null) {
    1138           96 :     const jobId = jobIdParam ?? activeJobId.value ?? null
    1139           96 :     const runErrorKey = jobId ? runErrorKeyForJob(jobId) : undefined
    1140           96 :     if (jobId) forgetJob(jobId)
    1141            0 :     else cancelPendingProgressUpdates()
    1142           96 :     if (!activeJobId.value || jobId === activeJobId.value) {
    1143           95 :       executionIdToLocatorCache.clear()
    1144           95 :       nodeProgressStates.value = {}
    1145           95 :       activeJobId.value = null
    1146           95 :       _executingNodeProgress.value = null
    1147              :     }
    1148           96 :     executionErrorStore.clearPromptError(runErrorKey)
    1149              :   }
    1150              : 
    1151          150 :   function storeJob({
    1152              :     nodes,
    1153              :     id,
    1154              :     promptOutput,
    1155              :     startTime,
    1156              :     submissionAcceptedAt,
    1157              :     workflow,
    1158              :     mode,
    1159              :     workflowContext,
    1160              :     workflowExecutionIntent = defaultWorkflowExecutionIntent
    1161              :   }: {
    1162              :     nodes: string[]
    1163              :     id: JobId
    1164              :     promptOutput: ComfyApiWorkflow
    1165              :     startTime?: number
    1166              :     submissionAcceptedAt?: number
    1167              :     workflow: ComfyWorkflow
    1168              :     mode: AppMode
    1169              :     workflowContext?: WorkflowExecutionContext
    1170              :     workflowExecutionIntent?: WorkflowExecutionIntent
    1171              :   }) {
    1172          150 :     queuedJobs.value[id] ??= { nodes: {} }
    1173          150 :     const queuedJob = queuedJobs.value[id]
    1174          150 :     queuedJob.nodes = {
    1175              :       ...nodes.reduce((p: Record<string, boolean>, n) => {
    1176          568 :         p[n] = false
    1177          568 :         return p
    1178              :       }, {}),
    1179              :       ...queuedJob.nodes
    1180              :     }
    1181          150 :     queuedJob.nodeLookup = buildExecutionNodeLookup(promptOutput)
    1182          150 :     queuedJob.startTime = startTime
    1183          150 :     queuedJob.submissionAcceptedAt = submissionAcceptedAt
    1184          150 :     queuedJob.workflowContext = workflowContext
    1185          150 :     queuedJob.workflowExecutionIntent = workflowExecutionIntent
    1186          150 :     queuedJob.shareId = workflow.shareId
    1187          150 :     queuedJob.viewMode = mode
    1188          150 :     queuedJob.isAppMode = isAppModeValue(mode)
    1189          150 :     registerJobOwner(id, workflow)
    1190              :   }
    1191              : 
    1192              :   /** Routes a job's frames and outputs to `workflow`, including jobs queued elsewhere. */
    1193          150 :   function registerJobOwner(id: JobId, workflow: ComfyWorkflow) {
    1194          150 :     queuedJobs.value[id] ??= { nodes: {} }
    1195          150 :     const queuedJob = queuedJobs.value[id]
    1196          150 :     queuedJob.workflow = workflow
    1197          150 :     jobIdToWorkflow.set(id, workflow)
    1198          150 :     const wid = workflow.activeState?.id ?? workflow.initialState?.id
    1199          150 :     if (wid) {
    1200          150 :       jobIdToWorkflowId.value.set(id, wid)
    1201              :     }
    1202          150 :     if (workflow.path) {
    1203          150 :       ensureSessionWorkflowPath(id, workflow.path, workflow.instanceId)
    1204              :     }
    1205          150 :     flushPendingWorkflowStatus(id, workflow)
    1206          150 :     flushPendingExecutionError(id)
    1207          150 :     flushPendingSkippedOutputs(id)
    1208          150 :     if (queuedJob.executionStartedAt !== undefined) adoptActiveWorkflowJob()
    1209              :   }
    1210              : 
    1211          182 :   function flushPendingExecutionError(jobId: string) {
    1212          182 :     const pending = pendingExecutionErrorsByJobId.get(jobId)
    1213          182 :     if (!pending) return
    1214              : 
    1215            1 :     const runErrorKey = runErrorKeyForJob(jobId)
    1216            1 :     if (runErrorKey === null) return
    1217              : 
    1218            1 :     pendingExecutionErrorsByJobId.delete(jobId)
    1219            1 :     processExecutionError(pending.detail, runErrorKey, pending.endTime)
    1220              :   }
    1221              : 
    1222          150 :   function flushPendingWorkflowStatus(
    1223              :     jobId: string,
    1224              :     workflow: ComfyWorkflow | undefined
    1225              :   ) {
    1226          150 :     const pending = pendingWorkflowStatusByJobId.get(jobId)
    1227          150 :     if (pending === undefined || !workflow) return
    1228            3 :     pendingWorkflowStatusByJobId.delete(jobId)
    1229            3 :     queuedJobs.value[jobId].executionStartedAt = pending.executionStartedAt
    1230              :     // Don't let a stale 'running' overwrite a terminal status already set.
    1231          150 :     if (pending.status === 'running' && workflowStatus.value.has(workflow))
    1232            0 :       return
    1233            3 :     if (pending.showStatus !== false) {
    1234            3 :       applyWorkflowStatus(workflow, pending.status)
    1235              :     }
    1236            3 :     trackExecutionOutcome(jobId, pending)
    1237          150 :     if (pending.status === 'running' || activeJobId.value === jobId) return
    1238            0 :     delete queuedJobs.value[jobId]
    1239            0 :     jobIdToWorkflow.delete(jobId)
    1240              :   }
    1241              : 
    1242              :   // ~0.65 MB at capacity (32 char GUID key + 50 char path value)
    1243         2355 :   const MAX_SESSION_PATH_ENTRIES = 4000
    1244              : 
    1245          150 :   function ensureSessionWorkflowPath(
    1246              :     jobId: JobId,
    1247              :     path: string,
    1248              :     workflowInstanceId?: string
    1249              :   ) {
    1250          150 :     if (workflowInstanceId) {
    1251          150 :       jobIdToWorkflowInstanceId.set(jobId, workflowInstanceId)
    1252              :     }
    1253          150 :     if (jobIdToSessionWorkflowPath.value.get(jobId) === path) return
    1254          150 :     const next = new Map(jobIdToSessionWorkflowPath.value)
    1255          150 :     next.set(jobId, path)
    1256          150 :     while (next.size > MAX_SESSION_PATH_ENTRIES) {
    1257            0 :       const oldest = next.keys().next().value
    1258            0 :       if (oldest !== undefined) {
    1259            0 :         next.delete(oldest)
    1260            0 :         jobIdToWorkflowInstanceId.delete(oldest)
    1261            0 :       } else break
    1262              :     }
    1263          150 :     jobIdToSessionWorkflowPath.value = next
    1264              :   }
    1265              : 
    1266          100 :   function rewriteSessionWorkflowPaths(
    1267              :     workflowInstanceId: string,
    1268              :     newPath: string
    1269              :   ) {
    1270          100 :     let next: Map<string, string> | undefined
    1271          100 :     for (const [jobId, path] of jobIdToSessionWorkflowPath.value) {
    1272            3 :       if (path === newPath) continue
    1273            3 :       if (jobIdToWorkflowInstanceId.get(jobId) !== workflowInstanceId) continue
    1274            3 :       next ??= new Map(jobIdToSessionWorkflowPath.value)
    1275            3 :       next.set(jobId, newPath)
    1276              :     }
    1277          100 :     if (next) jobIdToSessionWorkflowPath.value = next
    1278              :   }
    1279              : 
    1280         2355 :   workflowStore.onWorkflowRenamed((workflow) =>
    1281              :     rewriteSessionWorkflowPaths(workflow.instanceId, workflow.path)
    1282              :   )
    1283              : 
    1284              :   /**
    1285              :    * Register or update a mapping from job ID to workflow ID.
    1286              :    */
    1287           32 :   function registerJobWorkflowIdMapping(jobId: JobId, workflowId: WorkflowId) {
    1288           32 :     if (!jobId || !workflowId) return
    1289           32 :     jobIdToWorkflowId.value.set(jobId, workflowId)
    1290           32 :     flushPendingExecutionError(jobId)
    1291           32 :     flushPendingSkippedOutputs(jobId)
    1292              :   }
    1293              : 
    1294              :   /**
    1295              :    * Convert a NodeLocatorId to an execution context ID
    1296              :    * @param locatorId The NodeLocatorId
    1297              :    * @returns The execution ID or null if conversion fails
    1298              :    */
    1299         2355 :   const nodeLocatorIdToExecutionId = (
    1300              :     locatorId: NodeLocatorId
    1301              :   ): string | null => {
    1302              :     const executionId = workflowStore.nodeLocatorIdToNodeExecutionId(locatorId)
    1303            0 :     return executionId
    1304              :   }
    1305              : 
    1306         2355 :   const runningJobIds = computed<JobId[]>(() => {
    1307            0 :     const result: JobId[] = []
    1308            0 :     for (const [pid, nodes] of Object.entries(nodeProgressStatesByJob.value)) {
    1309            0 :       if (Object.values(nodes).some((n) => n.state === 'running')) {
    1310            0 :         result.push(pid)
    1311              :       }
    1312              :     }
    1313            0 :     return result
    1314              :   })
    1315              : 
    1316         2355 :   const runningWorkflowCount = computed<number>(
    1317              :     () => runningJobIds.value.length
    1318              :   )
    1319              : 
    1320         3799 :   function adoptActiveWorkflowJob() {
    1321              :     const startedJobIds = Object.keys(queuedJobs.value).filter(
    1322              :       (jobId) => queuedJobs.value[jobId].executionStartedAt !== undefined
    1323              :     )
    1324         3799 :     const ownedJobId = startedJobIds.findLast((jobId) =>
    1325              :       rememberStampAndCheckActiveWorkflow(jobId)
    1326              :     )
    1327         3799 :     executionIdToLocatorCache.clear()
    1328         3799 :     if (!ownedJobId) {
    1329         3796 :       nodeProgressStates.value = {}
    1330         3796 :       return
    1331              :     }
    1332            3 :     activeJobId.value = ownedJobId
    1333         3799 :     nodeProgressStates.value = nodeProgressStatesByJob.value[ownedJobId] ?? {}
    1334         3799 :     if (_executingNodeProgress.value?.prompt_id !== ownedJobId) {
    1335            3 :       _executingNodeProgress.value = null
    1336              :     }
    1337              :   }
    1338              : 
    1339         2355 :   watch(() => workflowStore.activeWorkflow, adoptActiveWorkflowJob)
    1340              : 
    1341              :   const isActiveWorkflowRunning = computed(() => {
    1342          106 :     if (!activeJobId.value) return false
    1343          106 :     const path = workflowStore.activeWorkflow?.path
    1344          106 :     if (!path) return false
    1345           22 :     return jobIdToSessionWorkflowPath.value.get(activeJobId.value) === path
    1346              :   })
    1347              : 
    1348         2355 :   return {
    1349              :     isIdle,
    1350              :     clientId,
    1351              :     activeJobId,
    1352              :     queuedJobs,
    1353              :     executingNodeId,
    1354              :     executingNodeIds,
    1355              :     activeJob,
    1356              :     totalNodesToExecute,
    1357              :     nodesExecuted,
    1358              :     executionProgress,
    1359              :     executingNode,
    1360              :     executingNodeProgress,
    1361              :     nodeProgressStates,
    1362              :     nodeLocationProgressStates,
    1363              :     nodeProgressStatesByJob,
    1364              :     runningJobIds,
    1365              :     runningWorkflowCount,
    1366              :     initializingJobIds,
    1367              :     rememberStampAndCheckActiveWorkflow,
    1368              :     isActiveWorkflowRunning,
    1369              :     isJobInitializing,
    1370              :     clearInitializationByJobId,
    1371              :     clearInitializationByJobIds,
    1372              :     reconcileInitializingJobs,
    1373              :     clearActiveJobIfStale,
    1374              :     forgetFinishedJobs,
    1375              :     bindExecutionEvents,
    1376              :     unbindExecutionEvents,
    1377              :     onJobReset: jobReset.on,
    1378              :     storeJob,
    1379              :     registerJobOwner,
    1380              :     registerJobWorkflowIdMapping,
    1381              :     uniqueExecutingNodeIdStrings,
    1382              :     // Raw executing progress data for backward compatibility in ComfyApp.
    1383              :     _executingNodeProgress,
    1384              :     // NodeLocatorId conversion helpers
    1385              :     nodeLocatorIdToExecutionId,
    1386              :     jobIdToWorkflowId,
    1387              :     jobIdToSessionWorkflowPath,
    1388              :     ensureSessionWorkflowPath,
    1389              :     getWorkflowStatus,
    1390              :     clearWorkflowStatus
    1391              :   }
    1392              : })
        

Generated by: LCOV version 2.0-1