diff --git a/.github/workflows/ci-oss-assets-validation.yaml b/.github/workflows/ci-oss-assets-validation.yaml index 9abb6f8f139..466c2d96d9c 100644 --- a/.github/workflows/ci-oss-assets-validation.yaml +++ b/.github/workflows/ci-oss-assets-validation.yaml @@ -117,7 +117,7 @@ jobs: if pnpx license-checker-rseidelsohn@4 \ --production \ --summary \ - --excludePackages '@comfyorg/comfyui-frontend;@comfyorg/account-core;@comfyorg/account-ui;@comfyorg/api-client;@comfyorg/billing-contract;@comfyorg/billing-core;@comfyorg/design-system;@comfyorg/graph-model;@comfyorg/ingest-types;@comfyorg/keybindings;@comfyorg/layer-editor;@comfyorg/mask-editor;@comfyorg/registry-types;@comfyorg/settings-core;@comfyorg/shared-frontend-utils;@comfyorg/tailwind-utils;@comfyorg/telemetry;@comfyorg/test-utils;@comfyorg/viewport3d;@comfyorg/comfyui-electron-types;@comfyorg/comfy-multi-player' \ + --excludePackages '@comfyorg/comfyui-frontend;@comfyorg/account-core;@comfyorg/comfyui-api-types;@comfyorg/account-ui;@comfyorg/api-client;@comfyorg/billing-contract;@comfyorg/billing-core;@comfyorg/design-system;@comfyorg/graph-model;@comfyorg/ingest-types;@comfyorg/keybindings;@comfyorg/layer-editor;@comfyorg/mask-editor;@comfyorg/registry-types;@comfyorg/settings-core;@comfyorg/shared-frontend-utils;@comfyorg/tailwind-utils;@comfyorg/telemetry;@comfyorg/test-utils;@comfyorg/viewport3d;@comfyorg/comfyui-electron-types;@comfyorg/comfy-multi-player' \ --clarificationsFile .github/license-clarifications.json \ --onlyAllow 'MIT;MIT*;Apache-2.0;BSD-2-Clause;BSD-3-Clause;ISC;0BSD;BlueOak-1.0.0;Python-2.0;CC0-1.0;Unlicense;(MIT OR Apache-2.0);(MIT OR GPL-3.0);(Apache-2.0 OR MIT);(MPL-2.0 OR Apache-2.0);MPL-2.0;CC-BY-4.0;CC-BY-3.0;GPL-3.0-only'; then echo '' diff --git a/browser_tests/tests/wsReconnectStaleJob.spec.ts b/browser_tests/tests/wsReconnectStaleJob.spec.ts index 7d26736e679..e467207d3df 100644 --- a/browser_tests/tests/wsReconnectStaleJob.spec.ts +++ b/browser_tests/tests/wsReconnectStaleJob.spec.ts @@ -99,6 +99,100 @@ async function triggerReconnect( } test.describe('WebSocket reconnect with stale job', { tag: '@ui' }, () => { + test('keeps the visible workflow running status when cleanup forgets one job', async ({ + comfyPage, + getWebSocket, + nextWebSocket + }) => { + const ws = await getWebSocket() + const exec = new ExecutionHelper(comfyPage, ws) + const topbar = comfyPage.menu.topbar + + const { orphanedIndicator, sharedIndicator, staleJobIds, runningJobId } = + await test.step('Strand one job per workflow behind a live sibling', async () => { + await comfyPage.workflow.waitForActiveWorkflow() + await comfyPage.workflow.waitForWorkflowIdle() + + const orphanedJobId = await exec.run() + exec.executionStart(orphanedJobId) + + await comfyPage.workflow.loadWorkflow('nodes/single_save_image_node') + await comfyPage.workflow.waitForWorkflowIdle() + + const sharedStaleJobId = await exec.run() + const runningJobId = await exec.run() + exec.executionStart(sharedStaleJobId) + exec.executionStart(runningJobId) + + await comfyPage.workflow.newBlankWorkflow() + await comfyPage.workflow.waitForWorkflowIdle() + await expect(topbar.tabs).toHaveCount(3) + + const orphanedIndicator = topbar + .getTab(0) + .getByRole('img', { name: 'Running' }) + const sharedIndicator = topbar + .getTab(1) + .getByRole('img', { name: 'Running' }) + await expect(orphanedIndicator).toBeVisible() + await expect(sharedIndicator).toBeVisible() + + return { + orphanedIndicator, + sharedIndicator, + staleJobIds: [orphanedJobId, sharedStaleJobId], + runningJobId + } + }) + + await test.step('Recover the remaining job after stale-job cleanup', async () => { + const remainingJobScenario: Scenario = { + name: 'preserves the remaining same-workflow job', + queueBody: () => + JSON.stringify( + jobsResponse([ + { id: runningJobId, status: 'in_progress', create_time: 0 } + ]) + ), + expectsActiveAfter: true + } + const staleJobReads = await Promise.all( + staleJobIds.map((staleJobId) => + mockJobsRoute( + comfyPage, + new RegExp(`/api/jobs/${staleJobId}$`), + '{}', + 404 + ) + ) + ) + + let socket = ws + for (let pass = 1; pass <= 3; pass++) { + const nextSocketPromise = nextWebSocket() + await triggerReconnect( + comfyPage, + socket, + remainingJobScenario, + runningJobId + ) + await expect + .poll(() => staleJobReads.map((reads) => reads() >= 2 * pass)) + .toEqual([true, true]) + socket = await nextSocketPromise + } + + await expect( + orphanedIndicator, + 'the workflow whose only job went unreadable must release its badge' + ).toBeHidden() + await expect( + sharedIndicator, + 'the same cleanup batch must leave the still-running sibling alone' + ).toBeVisible() + }) + }) + test.describe('app mode skeleton', () => { test.beforeEach(async ({ comfyPage }) => { await comfyPage.appMode.enterAppModeWithInputs([[KSAMPLER_NODE, 'seed']]) diff --git a/docs/adr/EXECUTION-ROUTING-0049-workflow-ownership-for-websocket-frames.md b/docs/adr/EXECUTION-ROUTING-0049-workflow-ownership-for-websocket-frames.md new file mode 100644 index 00000000000..efabcea5595 --- /dev/null +++ b/docs/adr/EXECUTION-ROUTING-0049-workflow-ownership-for-websocket-frames.md @@ -0,0 +1,55 @@ +# ADR-EXECUTION-ROUTING-0049: Workflow ownership for execution websocket frames + +Date: 2026-10-26 + +## Status + +Proposed + +## Context + +One `LGraph` and one `LGraphCanvas` serve every open workflow tab, so execution state written by node id is shared state. A run started in one tab painted its progress and outputs onto whichever tab was in front. Reported on #1293, #2660, #6730 and Comfy-Org/ComfyUI#6581, under the umbrella #3377. + +[ADR-GRAPH-DOCUMENT-0026](GRAPH-DOCUMENT-0026-frontend-document-model.md) D2 states the rule: a frame arriving on a push channel resolves its target document at ingress via the job map, never via the active pointer. It also states what to do at the boundary — "events with no job→uid resolution (page reloaded mid-run, jobs queued by another client) go to an explicit _unattributed_ bucket rather than being applied to the active document by default … misdelivery is not the fallback." + +`#21666` implemented D2's addressing rule for the Agent, keying the ownership gate on `jobIdToWorkflowInstanceId` and gating the node-output and preview paths. That shipped without unit coverage, and without the wire contract Comfy-Org/ComfyUI#16763 and #16809 were merged to provide: the frontend sent no `workflow_metadata`, so the only id the server could stamp came from `extra_pnginfo`. + +This ADR records what the resolution order is, why the graph id is its weakest signal, and that the boundary departs from D2. + +## Decision + +**Resolution order, strongest first.** The open tab's `instanceId`, then the session path, then the workflow id stamped on the frame. When none resolves, the `activeJobId` guard decides. + +`instanceId` is the only identifier unique per open tab. A path identifies one open workflow, so it is next. The frame's own workflow id is weakest, because every copy of a graph shares it: `ensureWorkflowId` keeps the id already present in a pasted graph, so copy a workflow, open a tab and paste gives two tabs carrying one id. Keying on it first would make each tab match the other's jobs. Its value is placing a job that no per-tab mapping knows about at all — one queued before a page reload, or by another client — which is the gap `instanceId` alone cannot close and the reason the wire contract exists. + +**The all-zero UUID is not an id.** `LGraph._id` defaults to it and the codebase reads it as "no id yet", replacing it on configure and on clear. It is not sent as a routing key, and an incoming one is discarded rather than compared, because comparing it as a real id matches nothing and rejected a fresh graph's own frames. Withholding it from the request does not keep it off the wire: the same graph goes out under `extra_pnginfo` and the server falls back to the id there. + +**The stamp is remembered per job, not re-read per frame.** The first stamped frame of a job records `jobId → workflow_id`, and the workflow-id leg reads it when the caller holds no frame. Three callers hold none: `adoptActiveWorkflowJob` on a tab switch, the `executed` events `useReconnectQueueRefresh` rebuilds by hand on reconnect, and `progress_text` when its backend supplies `prompt_id`. Without it those three skipped the leg and reached the `activeJobId` fallback, so a reconnect replay could paint a foreign job's output onto the visible graph — `handleExecutionStart` enrols a job in `queuedJobs` before the gate runs, and the replay follows every job in that map, so a rejected foreign job was still replayed unstamped. + +First stamp wins: core merges `workflow_metadata` at `send_sync` enqueue time rather than at publication, so a frame already queued for a job cannot be relabelled by the next prompt, and a later disagreeing stamp belongs to a different job. The map is separate from `jobIdToWorkflowId` on purpose — that one is reactive, exported, and backs `runErrorKeyForJob`, so recording a foreign job's stamp there would file that job's errors against a local tab whenever the two share a graph id, which is the copy-paste case the resolution order exists to handle. Being populated for foreign jobs, it carries its own FIFO bound rather than relying on the local-job eviction paths. + +**A rejection by a real identifier is not a diagnostic.** Only a rejection decided by the `activeJobId` fallback is reported, because that is the one leg that guesses. The resolver returns which leg decided; a mismatched instance, path or workflow id is a definite answer. Reporting every rejection raised one `warning_rejecting_unowned_execution_start` per foreign job start on ordinary traffic, since Cloud's per-user fan-out shows every open tab every other tab's job starts. + +**Unresolvable frames fall back to the active job.** This is the documented exception to D2's boundary rule. Such a frame is delivered to the tab in front when no job is active or the frame belongs to `activeJobId`, rather than quarantined. A frame with no `prompt_id` is always delivered. + +**What is gated is decided by the consumer's key, not by the frame being foreign.** Node-keyed writes are gated, because the visible graph may hold the same node ids. Job-keyed writes are not: `useJobPreviewStore().setPreviewUrl` feeds the queue panel thumbnail for a background run. Account-level signals are not, because a sign-in or credit precondition belongs to the account rather than to one workflow. + +## Alternatives considered + +**Quarantine unresolvable frames, as D2 prescribes.** Rejected. The common unresolvable case is a page reloaded mid-run, where the mappings are gone and the only open tab is the one being looked at. Quarantining would stop a single-tab user's own run updating after a refresh, a regression for the common case in service of a rule written for the multi-tab case. D2's boundary also assumes a surfacing UI for the unattributed bucket, which does not exist. + +**Gate on the workflow id alone.** This was tried on #20045 and was wrong, for the copy-paste reason above. Recorded because the id looks like the obvious key. + +The same id-first shape is still open on #11951, which proposes a `messageMatchesActiveWorkflow` resolver consulting the frame's workflow id ahead of session ownership, and it is the inverse of the order decided here. Two tabs holding one copied graph id would match each other's jobs under it, which is exactly what the instance and path legs exist to prevent. Its resolver is therefore superseded by this ADR; remaining work on that branch should be carried onto this gate rather than restoring the competing policy during integration. #20982 overlaps the same seam and is covered by the same ruling. Whichever of #22186's move of the API and execution contracts into `packages/api-client` lands second has to carry this PR's request field, preview decoding and stamped types into that package. + +**A `contextvars` owner on the backend.** Rejected on a mechanism error: `prompt_worker` is a daemon thread and `send_json` runs on the event loop, so a ContextVar set in the worker is unset at send time. The attribute merged in the body of `send_sync` is what shipped. + +## Consequences + +**The `activeJobId` fallback is load-bearing and is the weakest point.** Single-tab behaviour after a reload is preserved, and it is narrower than a blanket permissive rule, since a foreign job's unresolvable frame is dropped while another job is active. What remains is that with two tabs open and a frame unresolvable, it can land on the wrong one. + +**`status` and current core `progress_text` both stay unattributable.** Neither supplies a `prompt_id` to the gate. `status` carries none because one server-wide socket serves every workflow. `progress_text` is a packed binary frame that `send_sync` cannot stamp, and the frontend decodes a job id out of it only when the server sets `supports_progress_text_metadata`, which no core version does — tracked as Comfy-Org/ComfyUI#16887. With the flag absent the gate is called with no job id at all, so it returns before any remembered-stamp lookup, and #16887 is what unblocks the leg rather than merely improving its first frame. A backend that does supply `prompt_id` gets the remembered workflow stamp without having to add `workflow_id` to every text frame. + +**The two backends merge in opposite orders.** Core merges `{**workflow_metadata, **data}`, so a frame's own fields win. The Cloud gateway assigns `prompt_id` and `workflow_id` after the fact, so the backend wins, and it does not read `workflow_metadata` at all. Neither order should be relied on; the routing ids that arrive are authoritative whichever side set them. + +**Revisit if** an unattributed-frame surface is built, at which point D2's boundary becomes implementable and this exception should be removed rather than inherited. diff --git a/docs/architecture/domains/boundaries.json b/docs/architecture/domains/boundaries.json index dbf642eedf4..03832ebc54b 100644 --- a/docs/architecture/domains/boundaries.json +++ b/docs/architecture/domains/boundaries.json @@ -841,7 +841,9 @@ "src/stores/executionErrorStore.ts", "src/stores/executionErrorStore.test.ts", "src/stores/executionStore.ts", + "src/stores/executionStore.terminalRecovery.test.ts", "src/stores/executionStore.test.ts", + "src/stores/executionStore.workflowOwnership.test.ts", "src/stores/jobPreviewStore.ts", "src/stores/jobPreviewStore.test.ts", "src/stores/nodeOutputStore.ts", @@ -881,6 +883,8 @@ "src/scripts/api.getSettings.test.ts", "src/scripts/api.getShareableAssets.test.ts", "src/scripts/api.logs.test.ts", + "src/scripts/api.previewMetadata.test.ts", + "src/scripts/api.queuePrompt.test.ts", "src/scripts/api.responses.test.ts", "src/scripts/api.sessionOverride.test.ts", "src/scripts/api.socketReset.test.ts", diff --git a/packages/api-client/package.json b/packages/api-client/package.json index 636c7e5f288..20132bccbb7 100644 --- a/packages/api-client/package.json +++ b/packages/api-client/package.json @@ -19,6 +19,7 @@ }, "dependencies": { "@comfyorg/account-core": "workspace:*", + "@comfyorg/comfyui-api-types": "workspace:*", "@comfyorg/graph-model": "workspace:*", "@comfyorg/ingest-types": "workspace:*", "@comfyorg/object-info-parser": "workspace:*", diff --git a/packages/api-client/src/ComfyApi.ts b/packages/api-client/src/ComfyApi.ts index 63c63e1d77c..1cd0dedda99 100644 --- a/packages/api-client/src/ComfyApi.ts +++ b/packages/api-client/src/ComfyApi.ts @@ -11,6 +11,7 @@ import { zGetExtensionsResponse, zPostAssetsFromWorkflowResponse } from '@comfyorg/ingest-types/zod' +import type { PromptWorkflowMetadata } from '@comfyorg/comfyui-api-types' import axios from 'axios' import { get } from 'es-toolkit/compat' import { trimEnd } from 'es-toolkit' @@ -72,6 +73,7 @@ import type { JobAssetsResult, JobDetail, JobListItem } from './jobTypes' import type { ComfyNodeDef } from '@comfyorg/object-info-parser' import type { AuthCredential, AuthScheme } from './authTypes' import type { NodeExecutionId } from '@comfyorg/graph-model/nodeIdentification' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import { getFetchRouteTemplate } from './fetchRouteTemplate' import { retryingSend } from './fetchWithRetry' import { NO_RETRY_POLICY } from './retryPolicy' @@ -89,6 +91,7 @@ interface QueuePromptRequestBody { client_id: string prompt: ComfyApiWorkflow partial_execution_targets?: NodeExecutionId[] + workflow_metadata?: Pick extra_data: { extra_pnginfo: { workflow: ComfyWorkflowJSON @@ -213,6 +216,7 @@ interface BackendApiCalls { displayNodeId: string realNodeId: string jobId: string + workflowId?: string } progress_text: ProgressTextWsMessage progress_state: ProgressStateWsMessage @@ -1105,7 +1109,8 @@ export class ComfyApi extends EventTarget { displayNodeId: metadata.display_node_id, parentNodeId: metadata.parent_node_id, realNodeId: metadata.real_node_id, - jobId: metadata.prompt_id + jobId: metadata.prompt_id, + workflowId: metadata.workflow_id }) this.dispatchCustomEvent('b_preview', imageBlob4) @@ -1344,6 +1349,10 @@ export class ComfyApi extends EventTarget { ...(options?.partialExecutionTargets && { partial_execution_targets: options.partialExecutionTargets }), + ...(workflow.id && + workflow.id !== zeroUuid && { + workflow_metadata: { workflow_id: workflow.id } + }), extra_data: { auth_token_comfy_org: this.authToken, api_key_comfy_org: this.apiKey, diff --git a/packages/api-client/src/executionTypes.ts b/packages/api-client/src/executionTypes.ts index f380a6e186e..22639644394 100644 --- a/packages/api-client/src/executionTypes.ts +++ b/packages/api-client/src/executionTypes.ts @@ -1,3 +1,4 @@ +import type { PromptWorkflowMetadata } from '@comfyorg/comfyui-api-types' import type { PromptInfo } from '@comfyorg/ingest-types' import { z } from 'zod' import { zNodeId } from '@comfyorg/graph-model/workflowSchema' @@ -6,6 +7,22 @@ import { resultItemType } from './resultItemTypeSchema' export type JobId = string +/** + * Core's OpenAPI defines no websocket frame schemas. It documents that + * `workflow_metadata` is echoed onto every JSON frame carrying a `prompt_id`, + * so this field is the request contract read back rather than a separate one. + * + * What transfers from the spec is the map's VALUE type, not the map: core types + * `workflow_metadata` as an arbitrary string map, and the frame carries that + * map's contents merged into the frame object. So a `Pick` or `Partial` of the + * map would describe the wrong shape here even though it would compile. + * + * The generated package is bundled into the published declarations by + * `tsconfig.types.json`, so this reference is inlined rather than emitted as an + * import of a private package that an external consumer could not resolve. + */ +type StampedWorkflowId = { workflow_id?: PromptWorkflowMetadata[string] } + export const zResultItem = z.object({ filename: z.string().optional(), subfolder: z.string().optional(), @@ -39,7 +56,7 @@ export interface StatusWsMessage { status?: StatusWsMessageStatus | null sid?: string | null } -export interface ProgressWsMessage { +export interface ProgressWsMessage extends StampedWorkflowId { value: number max: number prompt_id: JobId @@ -55,11 +72,11 @@ export interface NodeProgressState { parent_node_id?: NodeId real_node_id?: NodeId } -export interface ProgressStateWsMessage { +export interface ProgressStateWsMessage extends StampedWorkflowId { prompt_id: JobId nodes: Record } -export interface ExecutingWsMessage { +export interface ExecutingWsMessage extends StampedWorkflowId { node: NodeId | null display_node?: NodeId prompt_id: JobId @@ -70,7 +87,7 @@ export interface ExecutedWsMessage extends ExecutingWsMessage { output: NodeExecutionOutput merge?: boolean } -interface ExecutionWsMessageBase { +interface ExecutionWsMessageBase extends StampedWorkflowId { prompt_id: JobId timestamp: number } diff --git a/packages/comfyui-api-types/LICENSE b/packages/comfyui-api-types/LICENSE new file mode 100644 index 00000000000..7ceb934bd26 --- /dev/null +++ b/packages/comfyui-api-types/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Comfy Org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/comfyui-api-types/package.json b/packages/comfyui-api-types/package.json index 4d9cf869566..4c67043fcd5 100644 --- a/packages/comfyui-api-types/package.json +++ b/packages/comfyui-api-types/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "private": true, "description": "ComfyUI server API TypeScript types, generated from a pinned core OpenAPI spec", + "license": "MIT", "type": "module", "exports": { ".": { diff --git a/packages/graph-model/src/uuid.ts b/packages/graph-model/src/uuid.ts index 77587cd4b30..c9500127464 100644 --- a/packages/graph-model/src/uuid.ts +++ b/packages/graph-model/src/uuid.ts @@ -1 +1,4 @@ export type UUID = string + +/** Special-case zero-UUID, consisting entirely of zeros. Used as a default value. */ +export const zeroUuid = '00000000-0000-0000-0000-000000000000' diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f68d58f1c0d..69b5ef5bf92 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1121,6 +1121,9 @@ importers: '@comfyorg/account-core': specifier: workspace:* version: link:../account-core + '@comfyorg/comfyui-api-types': + specifier: workspace:* + version: link:../comfyui-api-types '@comfyorg/graph-model': specifier: workspace:* version: link:../graph-model diff --git a/src/composables/node/useProgressTextPreviews.ts b/src/composables/node/useProgressTextPreviews.ts index 05631050fd4..017f170cf06 100644 --- a/src/composables/node/useProgressTextPreviews.ts +++ b/src/composables/node/useProgressTextPreviews.ts @@ -26,7 +26,7 @@ export function useProgressTextPreviews() { const { nodeId, text, prompt_id } = e.detail if (!text || !nodeId) return - if (!executionStore.belongsToActiveWorkflow(prompt_id)) return + if (!executionStore.rememberStampAndCheckActiveWorkflow(prompt_id)) return const executionId = String(nodeId) const currentId = executionId.includes(':') diff --git a/src/core/graph/nodeShell/nodeShellState.test.ts b/src/core/graph/nodeShell/nodeShellState.test.ts index 5a358effcdd..aa65ec95cc8 100644 --- a/src/core/graph/nodeShell/nodeShellState.test.ts +++ b/src/core/graph/nodeShell/nodeShellState.test.ts @@ -11,7 +11,8 @@ import { useNodeDataStore } from '@/stores/nodeDataStore' import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId' import { UNASSIGNED_NODE_ID } from '@comfyorg/graph-model/nodeId' import type { NodeState } from '@comfyorg/graph-model/nodeState' -import { createUuidv4, zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' +import { createUuidv4 } from '@/utils/uuid' import { createNodeShellState, unregisterNodeState } from './nodeShellState' diff --git a/src/core/graph/nodeShell/nodeShellState.ts b/src/core/graph/nodeShell/nodeShellState.ts index c84db7a4302..281242b04c1 100644 --- a/src/core/graph/nodeShell/nodeShellState.ts +++ b/src/core/graph/nodeShell/nodeShellState.ts @@ -10,7 +10,7 @@ import { useNodeDataStore } from '@/stores/nodeDataStore' import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId' import { UNASSIGNED_NODE_ID } from '@comfyorg/graph-model/nodeId' import { LGraphEventMode } from '@comfyorg/graph-model/globalEnums' -import { zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import type { INodeInputSlot, diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index e230aa7819a..de89009132a 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -28,7 +28,8 @@ import { registerDocBoundRootGraphProbe } from '@/lib/litegraph/src/docBoundGraphs' import type { UUID } from '@/utils/uuid' -import { createUuidv4, zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' +import { createUuidv4 } from '@/utils/uuid' import { useEntityIdStore } from '@/stores/entityIdStore' import { useLinkStore } from '@/stores/linkStore' import { useExecutionOrderStore } from '@/stores/executionOrderStore' diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 0b4faa22b84..018d7850fd6 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -19,7 +19,8 @@ import { detachNodeFromStores } from '@/core/graph/nodeShell/nodeShellLifecycle' import type { UUID } from '@/utils/uuid' -import { createUuidv4, zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' +import { createUuidv4 } from '@/utils/uuid' import { reportError } from '@comfyorg/telemetry/reportError' import { attachGroupLayout, diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 6c6e79675d8..4fa5f989995 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -69,7 +69,7 @@ import { } from '@/types/widgetVisibility' import { adjustColor } from '@comfyorg/shared-frontend-utils/colorUtil' import type { ColorAdjustOptions } from '@comfyorg/shared-frontend-utils/colorUtil' -import { zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import { commonType, isNodeBindable, diff --git a/src/lib/litegraph/src/LLink.ts b/src/lib/litegraph/src/LLink.ts index 872a72d80ad..217c742930e 100644 --- a/src/lib/litegraph/src/LLink.ts +++ b/src/lib/litegraph/src/LLink.ts @@ -18,7 +18,7 @@ import { toOwningGraphId } from '@comfyorg/graph-model/graphScopeId' import type { GraphScope } from '@comfyorg/graph-model/graphScopeId' -import { zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import { toLinkId } from '@comfyorg/graph-model/linkId' import { UNASSIGNED_NODE_ID, diff --git a/src/lib/litegraph/src/Reroute.ts b/src/lib/litegraph/src/Reroute.ts index 15b25c6d91b..1a6bcdc8557 100644 --- a/src/lib/litegraph/src/Reroute.ts +++ b/src/lib/litegraph/src/Reroute.ts @@ -12,7 +12,7 @@ import { toOwningGraphId } from '@comfyorg/graph-model/graphScopeId' import type { GraphScope } from '@comfyorg/graph-model/graphScopeId' -import { zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import type { FloatingRerouteSlot, RerouteChain diff --git a/src/lib/litegraph/src/subgraph/EmptySubgraphInput.ts b/src/lib/litegraph/src/subgraph/EmptySubgraphInput.ts index 0988164df7c..bca6edd1970 100644 --- a/src/lib/litegraph/src/subgraph/EmptySubgraphInput.ts +++ b/src/lib/litegraph/src/subgraph/EmptySubgraphInput.ts @@ -8,7 +8,7 @@ import type { RerouteId } from '@comfyorg/graph-model/rerouteId' import type { PointTuple as Point } from '@comfyorg/shared-frontend-utils/geometry' import type { INodeInputSlot } from '@comfyorg/graph-model/slots' import { nextUniqueName } from '@/lib/litegraph/src/strings' -import { zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import { SubgraphInput } from './SubgraphInput' diff --git a/src/lib/litegraph/src/subgraph/EmptySubgraphOutput.ts b/src/lib/litegraph/src/subgraph/EmptySubgraphOutput.ts index 9890a13e6e4..27e15abe73d 100644 --- a/src/lib/litegraph/src/subgraph/EmptySubgraphOutput.ts +++ b/src/lib/litegraph/src/subgraph/EmptySubgraphOutput.ts @@ -8,7 +8,7 @@ import type { RerouteId } from '@comfyorg/graph-model/rerouteId' import type { PointTuple as Point } from '@comfyorg/shared-frontend-utils/geometry' import type { INodeOutputSlot } from '@comfyorg/graph-model/slots' import { nextUniqueName } from '@/lib/litegraph/src/strings' -import { zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import { SubgraphOutput } from './SubgraphOutput' diff --git a/src/platform/workflow/core/services/workflowService.test.ts b/src/platform/workflow/core/services/workflowService.test.ts index f676a3e0471..97fe0e4975d 100644 --- a/src/platform/workflow/core/services/workflowService.test.ts +++ b/src/platform/workflow/core/services/workflowService.test.ts @@ -49,7 +49,7 @@ import { createTestSubgraph } from '@/lib/litegraph/src/subgraph/__fixtures__/su import { createMockChangeTracker } from '@/utils/__tests__/litegraphTestUtils' import type { AppMode } from '@comfyorg/graph-model/appMode' import { isValidUuid } from '@/utils/formatUtil' -import { zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import { t } from '@/i18n' import { stubFirebaseAuthHarness } from '@/utils/__tests__/stubAccountIdentityPort' diff --git a/src/platform/workflow/core/services/workflowService.ts b/src/platform/workflow/core/services/workflowService.ts index 24fb83ae34b..e3b883fa898 100644 --- a/src/platform/workflow/core/services/workflowService.ts +++ b/src/platform/workflow/core/services/workflowService.ts @@ -60,7 +60,8 @@ import { } from '@/utils/formatUtil' import type { AppMode } from '@comfyorg/graph-model/appMode' import type { UUID } from '@/utils/uuid' -import { ensureNonZeroUuid, zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' +import { ensureNonZeroUuid } from '@/utils/uuid' export function readWorkflowDraftLineage(path: string) { return useWorkflowDraftStoreV2().getDraft(path)?.lineage ?? null diff --git a/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue b/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue index b55c80c3b7e..da005329e35 100644 --- a/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue +++ b/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue @@ -51,7 +51,7 @@ import { useWidgetStore } from '@/stores/widgetStore' import { toNodeId } from '@comfyorg/graph-model/nodeId' import type { NodeState } from '@comfyorg/graph-model/nodeState' import type { WidgetValue } from '@/types/simplifiedWidget' -import { zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import { matchesWidgetName } from '@/utils/widgetBinding' import type { WidgetValueBinding } from '@/utils/widgetBinding' import { cn } from '@comfyorg/tailwind-utils' diff --git a/src/scripts/api.previewMetadata.test.ts b/src/scripts/api.previewMetadata.test.ts new file mode 100644 index 00000000000..f33f529f464 --- /dev/null +++ b/src/scripts/api.previewMetadata.test.ts @@ -0,0 +1,95 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import { ComfyApi } from '@comfyorg/api-client/ComfyApi' + +class FakeWebSocket { + static readonly OPEN = 1 + static instances: FakeWebSocket[] = [] + + readyState = FakeWebSocket.OPEN + binaryType = '' + send = vi.fn() + removeEventListener = vi.fn() + handlers: Record void> = {} + addEventListener = vi.fn( + (event: string, handler: (event: unknown) => void) => { + this.handlers[event] = handler + } + ) + close = vi.fn() + + constructor(readonly url: string) { + FakeWebSocket.instances.push(this) + } + + receiveBinary(data: ArrayBuffer) { + this.handlers['message']?.({ data }) + } +} + +function previewFrame( + metadata: Record, + imageBytes: number[] +): ArrayBuffer { + const json = new TextEncoder().encode(JSON.stringify(metadata)) + const buffer = new ArrayBuffer(8 + json.byteLength + imageBytes.length) + const view = new DataView(buffer) + view.setUint32(0, 4) + view.setUint32(4, json.byteLength) + const bytes = new Uint8Array(buffer) + bytes.set(json, 8) + bytes.set(imageBytes, 8 + json.byteLength) + return buffer +} + +describe('ComfyApi type-4 preview metadata', () => { + let api: ComfyApi + + beforeEach(async () => { + FakeWebSocket.instances = [] + vi.stubGlobal('WebSocket', FakeWebSocket) + api = new ComfyApi({ + isCloud: false, + featureFlagOverride: () => undefined, + onFreeMemory: () => {} + }) + await api.init() + }) + + afterEach(() => { + api.socket = null + }) + + function dispatchedPreview(metadata: Record) { + const details: NonNullable< + Parameters>[1] + >[] = [] + api.addEventListener('b_preview_with_metadata', (event) => { + details.push(event.detail) + }) + FakeWebSocket.instances[0].receiveBinary(previewFrame(metadata, [1, 2, 3])) + return details[0] + } + + const baseMetadata = { + node_id: '1', + display_node_id: '1', + parent_node_id: '1', + real_node_id: '1', + prompt_id: 'job-1', + image_type: 'image/png' + } + + it('decodes the stamped workflow id onto the preview event', () => { + expect( + dispatchedPreview({ ...baseMetadata, workflow_id: 'graph-7' }) + ).toMatchObject({ jobId: 'job-1', workflowId: 'graph-7' }) + }) + + it('leaves the workflow id undefined when the frame carries none', () => { + const detail = dispatchedPreview(baseMetadata) + + expect(detail).toMatchObject({ jobId: 'job-1' }) + expect(detail.workflowId).toBeUndefined() + }) +}) diff --git a/src/scripts/api.queuePrompt.test.ts b/src/scripts/api.queuePrompt.test.ts new file mode 100644 index 00000000000..056fc37c590 --- /dev/null +++ b/src/scripts/api.queuePrompt.test.ts @@ -0,0 +1,69 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { MockInstance } from 'vitest' + +import type { ComfyWorkflowJSON } from '@comfyorg/graph-model/workflowSchema' +import { zeroUuid } from '@comfyorg/graph-model/uuid' +import { api } from '@/scripts/api' + +function promptResponse() { + return Response.json({ prompt_id: 'job-1', node_errors: {} }) +} + +function workflowWithId(id: string | undefined): ComfyWorkflowJSON { + return { + id, + revision: 0, + last_node_id: 0, + last_link_id: 0, + nodes: [], + links: [], + groups: [], + extra: {}, + version: 0.4 + } +} + +describe('api queuePrompt workflow_metadata', () => { + let fetchApiSpy: MockInstance + + beforeEach(() => { + fetchApiSpy = vi.spyOn(api, 'fetchApi').mockResolvedValue(promptResponse()) + }) + + async function postedBody(id: string | undefined) { + await api.queuePrompt(0, { + output: {}, + workflow: workflowWithId(id) + }) + const [, init] = fetchApiSpy.mock.calls[0] + if (typeof init?.body !== 'string') + throw new Error('Expected JSON request body') + return JSON.parse(init.body) + } + + it.for([ + { + name: 'a routable workflow id', + id: '11111111-2222-3333-4444-555555555555' + }, + { name: 'an id the server did not normalise', id: 'graph-7' } + ])('sends $name as workflow_metadata', async ({ id }) => { + expect(await postedBody(id)).toMatchObject({ + workflow_metadata: { workflow_id: id } + }) + }) + + it.for([ + { name: 'the all-zero sentinel', id: zeroUuid }, + { name: 'an absent id', id: undefined }, + { name: 'an empty id', id: '' } + ])('omits workflow_metadata for $name', async ({ id }) => { + expect(await postedBody(id)).not.toHaveProperty('workflow_metadata') + }) + + it('still carries the graph under extra_pnginfo when the id is withheld', async () => { + const body = await postedBody(zeroUuid) + + expect(body.extra_data.extra_pnginfo.workflow.id).toBe(zeroUuid) + }) +}) diff --git a/src/scripts/app.test.ts b/src/scripts/app.test.ts index 00d1fb2bffd..d97e71f1213 100644 --- a/src/scripts/app.test.ts +++ b/src/scripts/app.test.ts @@ -122,7 +122,7 @@ import { extractFilesFromDragEvent } from '@/utils/eventUtils' import { MIME_ASSET_INFO } from '@/platform/assets/schemas/mediaAssetSchema' import { reportError } from '@comfyorg/telemetry/reportError' import { useCanvasScheduler } from '@/renderer/core/canvas/useCanvasScheduler' -import { zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import type { importA1111 } from './pnginfo' type WorkflowService = ReturnType @@ -4357,6 +4357,79 @@ describe('ComfyApp', () => { ).toEqual(['blob:1']) }) + it('routes unmapped outputs and previews by the frame workflow id alone', () => { + Reflect.set(app, 'rootGraphInternal', new LGraph()) + const workflowA = markLoaded( + new ComfyWorkflow({ path: 'workflows/a.json', modified: 0, size: 0 }) + ) + workflowA.changeTracker.activeState = workflowGraphData(workflowAId) + useWorkflowStore().activeWorkflow = workflowA + + vi.spyOn(URL, 'createObjectURL').mockImplementation( + (blob) => `blob:${blob instanceof Blob ? blob.size : 0}` + ) + const addEventListener = vi + .spyOn(api, 'addEventListener') + .mockImplementation(() => {}) + vi.spyOn(api, 'init').mockResolvedValue() + Reflect.apply(Reflect.get(app, 'addApiUpdateHandlers'), app, []) + function handlerFor(event: Parameters[0]) { + const handler = addEventListener.mock.calls.find( + ([name]) => name === event + )?.[1] + if (typeof handler !== 'function') + throw new Error('Expected registered function handler') + return handler + } + function executedEvent(jobId: string, workflowId: string) { + return new CustomEvent('executed', { + detail: { + node: '1', + display_node: '1', + prompt_id: jobId, + workflow_id: workflowId, + output: { text: [jobId] } + } + }) + } + function previewEvent( + jobId: string, + blobText: string, + workflowId: string + ) { + return new CustomEvent('b_preview_with_metadata', { + detail: { + blob: new Blob([blobText]), + nodeId: '1', + parentNodeId: '1', + displayNodeId: '1', + realNodeId: '1', + jobId, + workflowId + } + }) + } + + handlerFor('executed')(executedEvent('job-own-output', workflowAId)) + handlerFor('b_preview_with_metadata')( + previewEvent('job-own-preview', 'a', workflowAId) + ) + handlerFor('executed')(executedEvent('job-foreign-output', workflowBId)) + handlerFor('b_preview_with_metadata')( + previewEvent('job-foreign-preview', 'bb', workflowBId) + ) + + const nodeOutputStore = useNodeOutputStore() + const executionId = createNodeExecutionId([1]) + expect( + nodeOutputStore.getNodeOutputByExecutionId(executionId), + "the later foreign output must not overwrite the visible tab's" + ).toEqual({ text: ['job-own-output'] }) + expect( + nodeOutputStore.getNodePreviewImagesByExecutionId(executionId) + ).toEqual(['blob:1']) + }) + it('restores the failed run state when returning to a workflow tab', async () => { const workflowService = await useRealWorkflowService() const graph = new LGraph() diff --git a/src/scripts/app.ts b/src/scripts/app.ts index 639b22cca5e..c6fbd692b2a 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -776,7 +776,13 @@ export class ComfyApp implements IComfyApp { }) api.addEventListener('executed', ({ detail }) => { - if (!useExecutionStore().belongsToActiveWorkflow(detail.prompt_id)) return + if ( + !useExecutionStore().rememberStampAndCheckActiveWorkflow( + detail.prompt_id, + detail.workflow_id + ) + ) + return const nodeOutputStore = useNodeOutputStore() const executionId = tryNormalizeNodeExecutionId( detail.display_node || detail.node @@ -811,14 +817,19 @@ export class ComfyApp implements IComfyApp { }) api.addEventListener('b_preview_with_metadata', ({ detail }) => { - const { blob, displayNodeId, jobId } = detail + const { blob, displayNodeId, jobId, workflowId } = detail const { setNodePreviewsByExecutionId, revokePreviewsByExecutionId } = useNodeOutputStore() const displayNodeExecutionId = tryNormalizeNodeExecutionId(displayNodeId) if (!displayNodeExecutionId) return const blobUrl = createSharedObjectUrl(blob) useJobPreviewStore().setPreviewUrl(jobId, blobUrl, displayNodeId) - if (!useExecutionStore().belongsToActiveWorkflow(jobId)) { + if ( + !useExecutionStore().rememberStampAndCheckActiveWorkflow( + jobId, + workflowId + ) + ) { releaseSharedObjectUrl(blobUrl) return } diff --git a/src/stores/executionErrorStore.ts b/src/stores/executionErrorStore.ts index 4ea94d071f5..1d0651a3852 100644 --- a/src/stores/executionErrorStore.ts +++ b/src/stores/executionErrorStore.ts @@ -39,7 +39,7 @@ import { getNodeByExecutionId } from '@/utils/graphTraversalUtil' import type { UUID } from '@/utils/uuid' -import { zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import { SIMPLE_ERROR_TYPES, errorsForSlot, diff --git a/src/stores/executionStore.terminalRecovery.test.ts b/src/stores/executionStore.terminalRecovery.test.ts new file mode 100644 index 00000000000..3f70d6c44d3 --- /dev/null +++ b/src/stores/executionStore.terminalRecovery.test.ts @@ -0,0 +1,170 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import type { LoadedComfyWorkflow } from '@/platform/workflow/management/stores/comfyWorkflow' +import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' +import { api } from '@/scripts/api' +import { useExecutionStore } from '@/stores/executionStore' +import { createMockLoadedWorkflow } from '@/utils/__tests__/litegraphTestUtils' + +vi.mock(import('@/composables/useAppMode')) +vi.mock(import('@comfyorg/telemetry')) +vi.mock(import('@comfyorg/telemetry/reportError')) +vi.mock(import('@/platform/distribution/types'), () => ({ isCloud: true })) + +const apiEvents: EventTarget = api + +function workflow(path: string): LoadedComfyWorkflow { + return createMockLoadedWorkflow({ + path, + instanceId: `instance-${path}`, + activeState: { id: `graph-${path}` }, + initialState: { id: `graph-${path}` } + }) +} + +const workflowA = workflow('workflows/a.json') +const workflowB = workflow('workflows/b.json') + +function openTabs(...workflows: LoadedComfyWorkflow[]) { + const workflowStore = useWorkflowStore() + for (const wf of workflows) { + if (workflowStore.isOpen(wf)) continue + workflowStore.attachWorkflow(wf, workflowStore.openWorkflows.length) + } +} + +describe('executionStore terminal-frame recovery', () => { + let store: ReturnType + + beforeEach(() => { + openTabs(workflowA, workflowB) + useWorkflowStore().activeWorkflow = workflowA + store = useExecutionStore() + store.bindExecutionEvents() + }) + + afterEach(() => { + store.unbindExecutionEvents() + }) + + function queueJob(jobId: string, wf: LoadedComfyWorkflow) { + store.storeJob({ + nodes: ['1'], + id: jobId, + promptOutput: { '1': { inputs: {}, class_type: 'TestNode' } }, + workflow: wf, + mode: 'graph' + }) + return jobId + } + + function startedJobWithNoTerminalFrame( + jobId: string, + wf: LoadedComfyWorkflow + ) { + queueJob(jobId, wf) + apiEvents.dispatchEvent( + new CustomEvent('execution_start', { + detail: { prompt_id: jobId, timestamp: 1 } + }) + ) + apiEvents.dispatchEvent( + new CustomEvent('progress_state', { + detail: { + prompt_id: jobId, + nodes: { + '1': { + value: 3, + max: 10, + state: 'running', + node_id: '1', + prompt_id: jobId + } + } + } + }) + ) + return jobId + } + + it('releases the tab status of a job it forgets', () => { + const jobId = startedJobWithNoTerminalFrame('job-b', workflowB) + expect(store.getWorkflowStatus(workflowB)).toBe('running') + + store.forgetFinishedJobs([jobId]) + + expect( + store.getWorkflowStatus(workflowB), + 'the badge must not keep claiming the tab is running' + ).toBeUndefined() + }) + + it('does not disturb a second tab while forgetting one job', () => { + startedJobWithNoTerminalFrame('job-a', workflowA) + const jobB = startedJobWithNoTerminalFrame('job-b', workflowB) + + store.forgetFinishedJobs([jobB]) + + expect( + store.getWorkflowStatus(workflowA), + 'A never went terminal, so forgetting B must not release it' + ).toBe('running') + }) + + const siblingCases: { + name: string + registerSibling: () => string + expectedStatus: 'running' | undefined + }[] = [ + { + name: 'started', + registerSibling: () => + startedJobWithNoTerminalFrame('job-b-2', workflowB), + expectedStatus: 'running' + }, + { + name: 'pending', + registerSibling: () => queueJob('job-b-2', workflowB), + expectedStatus: undefined + } + ] + + it.for(siblingCases)( + 'keeps the tab running only while a $name sibling holds it', + ({ registerSibling, expectedStatus }) => { + const firstJob = startedJobWithNoTerminalFrame('job-b-1', workflowB) + registerSibling() + + store.forgetFinishedJobs([firstJob]) + + expect(store.getWorkflowStatus(workflowB)).toBe(expectedStatus) + expect(store.queuedJobs[firstJob]).toBeUndefined() + expect(store.queuedJobs['job-b-2']).toBeDefined() + } + ) + + it('keeps a newer terminal badge when an older unreadable job is forgotten', () => { + const staleJob = startedJobWithNoTerminalFrame('job-b-stale', workflowB) + const completedJob = startedJobWithNoTerminalFrame( + 'job-b-completed', + workflowB + ) + apiEvents.dispatchEvent( + new CustomEvent('execution_success', { + detail: { prompt_id: completedJob, timestamp: 2 } + }) + ) + expect(store.getWorkflowStatus(workflowB)).toBe('completed') + expect( + store.queuedJobs[completedJob], + 'the completed job is already forgotten, so it cannot hold the badge' + ).toBeUndefined() + + store.forgetFinishedJobs([staleJob]) + + expect( + store.getWorkflowStatus(workflowB), + 'only an orphaned running badge may be released' + ).toBe('completed') + }) +}) diff --git a/src/stores/executionStore.ts b/src/stores/executionStore.ts index f083d338bcb..8f52d0eda3e 100644 --- a/src/stores/executionStore.ts +++ b/src/stores/executionStore.ts @@ -40,6 +40,7 @@ import { tryNormalizeNodeExecutionId } from '@comfyorg/graph-model/nodeIdentific import type { NodeLocatorId } from '@comfyorg/graph-model/nodeIdentification' import type { AppMode } from '@comfyorg/graph-model/appMode' import { isAppModeValue } from '@comfyorg/graph-model/appMode' +import { zeroUuid } from '@comfyorg/graph-model/uuid' import { classifyCloudValidationError } from '@/utils/executionErrorUtil' import { executionIdToNodeLocatorId } from '@/utils/graphTraversalUtil' import { createRafCoalescer } from '@/utils/rafBatch' @@ -167,6 +168,9 @@ export const useExecutionStore = defineStore('execution', () => { const jobIdToSessionWorkflowPath = shallowRef>(new Map()) const jobIdToWorkflowInstanceId = new Map() + const jobIdToStampedWorkflowId = new Map() + const MAX_STAMPED_WORKFLOW_ID_ENTRIES = 4000 + const initializingJobIds = ref>(new Set()) const workflowStatus = shallowRef< @@ -474,22 +478,98 @@ export const useExecutionStore = defineStore('execution', () => { pendingWorkflowStatusByJobId.clear() pendingExecutionErrorsByJobId.clear() jobIdToWorkflow.clear() + jobIdToStampedWorkflowId.clear() cancelPendingProgressUpdates() } - /** - * Whether frames for this job may paint the active workflow's canvas. - * Jobs whose owner is unknown are admitted unless another job is active. - */ - function belongsToActiveWorkflow(jobId: JobId | undefined): boolean { - if (!jobId) return true + function routableWorkflowId(id: string | undefined): string | undefined { + return !id || id === zeroUuid ? undefined : id + } + + function rememberStampedWorkflowId( + jobId: JobId, + workflowId: string | undefined + ): void { + const stamped = routableWorkflowId(workflowId) + if (!stamped || jobIdToStampedWorkflowId.has(jobId)) return + jobIdToStampedWorkflowId.set(jobId, stamped) + while (jobIdToStampedWorkflowId.size > MAX_STAMPED_WORKFLOW_ID_ENTRIES) { + const oldest = jobIdToStampedWorkflowId.keys().next().value + if (oldest === undefined) break + jobIdToStampedWorkflowId.delete(oldest) + } + } + + type OwnershipLeg = 'instance' | 'path' | 'workflow-id' | 'active-job' + + interface WorkflowOwnership { + owned: boolean + leg: OwnershipLeg + } + + function ownershipByInstance( + jobId: JobId, + activeWorkflow: ComfyWorkflow + ): WorkflowOwnership | undefined { const ownerInstanceId = jobIdToWorkflowInstanceId.get(jobId) - const activeInstanceId = workflowStore.activeWorkflow?.instanceId - if (ownerInstanceId && activeInstanceId) { - return ownerInstanceId === activeInstanceId + if (!ownerInstanceId || !activeWorkflow.instanceId) return + return { + owned: ownerInstanceId === activeWorkflow.instanceId, + leg: 'instance' } - return !activeJobId.value || jobId === activeJobId.value + } + + function ownershipByPath( + jobId: JobId, + activeWorkflow: ComfyWorkflow + ): WorkflowOwnership | undefined { + const ownerPath = jobIdToSessionWorkflowPath.value.get(jobId) + if (!ownerPath || !activeWorkflow.path) return + return { owned: ownerPath === activeWorkflow.path, leg: 'path' } + } + + function ownershipByWorkflowId( + jobId: JobId, + activeWorkflow: ComfyWorkflow, + workflowId: string | undefined + ): WorkflowOwnership | undefined { + const activeGraphId = routableWorkflowId( + activeWorkflow.activeState?.id ?? activeWorkflow.initialState?.id + ) + const frameWorkflowId = + routableWorkflowId(workflowId) ?? jobIdToStampedWorkflowId.get(jobId) + if (!frameWorkflowId || !activeGraphId) return + return { owned: frameWorkflowId === activeGraphId, leg: 'workflow-id' } + } + + function resolveWorkflowOwnership( + jobId: JobId, + workflowId?: string + ): WorkflowOwnership { + const ownedByActiveJob = (): WorkflowOwnership => ({ + owned: !activeJobId.value || jobId === activeJobId.value, + leg: 'active-job' + }) + + const activeWorkflow = workflowStore.activeWorkflow + if (!activeWorkflow) return ownedByActiveJob() + + return ( + ownershipByInstance(jobId, activeWorkflow) ?? + ownershipByPath(jobId, activeWorkflow) ?? + ownershipByWorkflowId(jobId, activeWorkflow, workflowId) ?? + ownedByActiveJob() + ) + } + + function rememberStampAndCheckActiveWorkflow( + jobId: JobId | undefined, + workflowId?: string + ): boolean { + if (!jobId) return true + rememberStampedWorkflowId(jobId, workflowId) + return resolveWorkflowOwnership(jobId, workflowId).owned } function handleExecutionStart(e: CustomEvent) { @@ -509,8 +589,12 @@ export const useExecutionStore = defineStore('execution', () => { executionStartedAt: queuedJobs.value[jobId].executionStartedAt }) - if (!belongsToActiveWorkflow(jobId)) { - reportAmbiguousExecutionStartRejection(jobId) + rememberStampedWorkflowId(jobId, e.detail.workflow_id) + const ownership = resolveWorkflowOwnership(jobId, e.detail.workflow_id) + if (!ownership.owned) { + if (ownership.leg === 'active-job') { + reportAmbiguousExecutionStartRejection(jobId) + } return } executionIdToLocatorCache.clear() @@ -664,8 +748,8 @@ export const useExecutionStore = defineStore('execution', () => { } function applyProgressState(detail: ProgressStateWsMessage) { - const { nodes, prompt_id: jobId } = detail - const isOwned = belongsToActiveWorkflow(jobId) + const { nodes, prompt_id: jobId, workflow_id: workflowId } = detail + const isOwned = rememberStampAndCheckActiveWorkflow(jobId, workflowId) if (isOwned) { revokeStartedNodePreviews( @@ -954,20 +1038,36 @@ export const useExecutionStore = defineStore('execution', () => { if (id && !activeJobIds.has(id)) resetExecutionState(id) } - /** Stops following queued jobs that ended without a terminal frame. */ function forgetFinishedJobs(jobIds: Iterable) { for (const jobId of jobIds) { if (jobId === activeJobId.value || !(jobId in queuedJobs.value)) continue + const workflow = jobIdToWorkflow.get(jobId) forgetJob(jobId) + if ( + workflow && + getWorkflowStatus(workflow) === 'running' && + !hasStartedJobForWorkflow(workflow) + ) { + clearWorkflowStatus(workflow) + } } } + function hasStartedJobForWorkflow(workflow: ComfyWorkflow): boolean { + return Object.entries(queuedJobs.value).some( + ([jobId, job]) => + job.executionStartedAt !== undefined && + jobIdToWorkflow.get(jobId) === workflow + ) + } + function forgetJob(jobId: JobId) { const progress = { ...nodeProgressStatesByJob.value } delete progress[jobId] nodeProgressStatesByJob.value = progress useJobPreviewStore().clearPreview(jobId) jobIdToWorkflow.delete(jobId) + jobIdToStampedWorkflowId.delete(jobId) if (jobId in queuedJobs.value) void jobReset.trigger(queuedJobs.value[jobId]) delete queuedJobs.value[jobId] @@ -1167,7 +1267,9 @@ export const useExecutionStore = defineStore('execution', () => { const startedJobIds = Object.keys(queuedJobs.value).filter( (jobId) => queuedJobs.value[jobId].executionStartedAt !== undefined ) - const ownedJobId = startedJobIds.findLast(belongsToActiveWorkflow) + const ownedJobId = startedJobIds.findLast((jobId) => + rememberStampAndCheckActiveWorkflow(jobId) + ) executionIdToLocatorCache.clear() if (!ownedJobId) { nodeProgressStates.value = {} @@ -1208,7 +1310,7 @@ export const useExecutionStore = defineStore('execution', () => { runningJobIds, runningWorkflowCount, initializingJobIds, - belongsToActiveWorkflow, + rememberStampAndCheckActiveWorkflow, isActiveWorkflowRunning, isJobInitializing, clearInitializationByJobId, diff --git a/src/stores/executionStore.workflowOwnership.test.ts b/src/stores/executionStore.workflowOwnership.test.ts new file mode 100644 index 00000000000..336382622f5 --- /dev/null +++ b/src/stores/executionStore.workflowOwnership.test.ts @@ -0,0 +1,275 @@ +import { reportError } from '@comfyorg/telemetry/reportError' +import { nextTick } from 'vue' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import type { LoadedComfyWorkflow } from '@/platform/workflow/management/stores/comfyWorkflow' +import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' +import { api } from '@/scripts/api' +import { useExecutionStore } from '@/stores/executionStore' +import { zeroUuid } from '@comfyorg/graph-model/uuid' +import { createMockLoadedWorkflow } from '@/utils/__tests__/litegraphTestUtils' + +vi.mock(import('@/composables/useAppMode')) +vi.mock(import('@comfyorg/telemetry')) +vi.mock(import('@comfyorg/telemetry/reportError')) +vi.mock(import('@/platform/distribution/types'), () => ({ isCloud: true })) + +const apiEvents: EventTarget = api + +function workflow(path: string): LoadedComfyWorkflow { + return createMockLoadedWorkflow({ + path, + instanceId: `instance-${path}`, + activeState: { id: 'shared-graph-id' }, + initialState: { id: 'shared-graph-id' } + }) +} + +const workflowA = workflow('workflows/a.json') +const workflowB = workflow('workflows/b.json') + +function promptNode() { + return { inputs: {}, class_type: 'TestNode', _meta: { title: 'Node' } } +} + +function openTabs(...workflows: LoadedComfyWorkflow[]) { + const workflowStore = useWorkflowStore() + for (const wf of workflows) { + if (workflowStore.isOpen(wf)) continue + workflowStore.attachWorkflow(wf, workflowStore.openWorkflows.length) + } +} + +describe('executionStore workflow ownership', () => { + let store: ReturnType + + beforeEach(() => { + openTabs(workflowA, workflowB) + useWorkflowStore().activeWorkflow = workflowA + store = useExecutionStore() + store.bindExecutionEvents() + }) + + afterEach(() => { + store.unbindExecutionEvents() + }) + + function queueJobFrom(jobId: string, wf: LoadedComfyWorkflow) { + store.storeJob({ + nodes: ['1'], + id: jobId, + promptOutput: { '1': promptNode() }, + workflow: wf, + mode: 'graph' + }) + } + + function fire(event: string, detail: Record) { + apiEvents.dispatchEvent(new CustomEvent(event, { detail })) + } + + it('does not separate two tabs by their shared graph id', () => { + queueJobFrom('job-a', workflowA) + queueJobFrom('job-b', workflowB) + + expect(store.rememberStampAndCheckActiveWorkflow('job-a')).toBe(true) + expect(store.rememberStampAndCheckActiveWorkflow('job-b')).toBe(false) + }) + + it('accepts a frame that names no job at all', () => { + expect(store.rememberStampAndCheckActiveWorkflow(undefined)).toBe(true) + }) + + describe('the session path, when no tab instance is known', () => { + it('places a job by the path it was queued from', () => { + store.ensureSessionWorkflowPath('job-b', workflowB.path) + + expect(store.rememberStampAndCheckActiveWorkflow('job-b')).toBe(false) + useWorkflowStore().activeWorkflow = workflowB + expect(store.rememberStampAndCheckActiveWorkflow('job-b')).toBe(true) + }) + + it('loses to the tab instance when the two disagree', () => { + queueJobFrom('job-a', workflowA) + store.ensureSessionWorkflowPath( + 'job-a', + workflowB.path, + workflowA.instanceId + ) + + useWorkflowStore().activeWorkflow = workflowB + expect( + store.rememberStampAndCheckActiveWorkflow('job-a'), + 'the instance still says A, so B does not own it' + ).toBe(false) + }) + }) + + describe("the frame's own workflow id, when no per-tab mapping exists", () => { + it('places a job queued before this page load', () => { + expect( + store.rememberStampAndCheckActiveWorkflow( + 'job-from-a-previous-page', + 'other-graph' + ) + ).toBe(false) + expect( + store.rememberStampAndCheckActiveWorkflow( + 'job-from-a-previous-page', + 'shared-graph-id' + ) + ).toBe(true) + }) + + it('ignores the all-zero sentinel rather than comparing it', () => { + expect( + store.rememberStampAndCheckActiveWorkflow( + 'job-on-a-fresh-graph', + zeroUuid + ) + ).toBe(true) + }) + + it('reaches the gate from a real progress_state frame', async () => { + fire('progress_state', { + prompt_id: 'job-elsewhere', + workflow_id: 'other-graph', + nodes: { + '1': { + value: 3, + max: 10, + state: 'running', + node_id: '1', + prompt_id: 'job-elsewhere' + } + } + }) + await vi.waitFor(() => + expect(store.nodeProgressStatesByJob['job-elsewhere']).toBeDefined() + ) + + expect( + store.nodeProgressStates['1'], + "a frame stamped with another graph's id does not reach the visible tab" + ).toBeUndefined() + }) + + it('loses to the session path when the two disagree', () => { + store.ensureSessionWorkflowPath('job-b', workflowB.path) + + expect( + store.rememberStampAndCheckActiveWorkflow('job-b', 'shared-graph-id'), + 'the path says B owns it, even though the graph id matches A' + ).toBe(false) + }) + }) + + describe('the stamp remembered from an earlier frame of the same job', () => { + function foreignJobStarted(jobId: string, workflowId?: string) { + fire('execution_start', { + prompt_id: jobId, + workflow_id: workflowId, + timestamp: 1 + }) + return jobId + } + + it('places a job for a caller that holds no frame', () => { + foreignJobStarted('job-elsewhere', 'other-graph') + + expect( + store.rememberStampAndCheckActiveWorkflow('job-elsewhere'), + 'the stamp seen on execution_start still decides when no id is passed' + ).toBe(false) + }) + + it('keeps the first stamp when a later frame disagrees', () => { + foreignJobStarted('job-elsewhere', 'other-graph') + + store.rememberStampAndCheckActiveWorkflow( + 'job-elsewhere', + 'shared-graph-id' + ) + + expect( + store.rememberStampAndCheckActiveWorkflow('job-elsewhere'), + 'the later stamp must not relabel the job onto the visible tab' + ).toBe(false) + }) + + it('loses to the tab instance, which is still the strongest leg', () => { + queueJobFrom('job-a', workflowA) + store.rememberStampAndCheckActiveWorkflow('job-a', 'other-graph') + + useWorkflowStore().activeWorkflow = workflowB + expect( + store.rememberStampAndCheckActiveWorkflow('job-a'), + 'the instance says A, so the remembered stamp must not outrank it' + ).toBe(false) + useWorkflowStore().activeWorkflow = workflowA + expect(store.rememberStampAndCheckActiveWorkflow('job-a')).toBe(true) + }) + + it('loses to the session path', () => { + store.ensureSessionWorkflowPath('job-b', workflowB.path) + store.rememberStampAndCheckActiveWorkflow('job-b', 'shared-graph-id') + + expect( + store.rememberStampAndCheckActiveWorkflow('job-b'), + 'the path says B owns it, even though the remembered stamp matches A' + ).toBe(false) + }) + + it('stops a tab switch from adopting a foreign job', async () => { + foreignJobStarted('job-elsewhere', 'other-graph') + expect(store.queuedJobs['job-elsewhere']).toBeDefined() + + useWorkflowStore().activeWorkflow = workflowB + await nextTick() + + expect(store.activeJobId).toBeNull() + }) + + it('is forgotten with the job', () => { + foreignJobStarted('job-elsewhere', 'other-graph') + + store.forgetFinishedJobs(['job-elsewhere']) + + expect( + store.rememberStampAndCheckActiveWorkflow('job-elsewhere'), + 'nothing is left to place the job, so the fallback admits it again' + ).toBe(true) + }) + }) + + describe('reporting a rejected execution_start', () => { + function rejectionWarnings() { + return vi + .mocked(reportError) + .mock.calls.filter( + ([, options]) => + options.errorType === 'warning_rejecting_unowned_execution_start' + ) + } + + it('does not report a rejection decided by the frame workflow id', () => { + fire('execution_start', { + prompt_id: 'job-elsewhere', + workflow_id: 'other-graph', + timestamp: 1 + }) + + expect(store.activeJobId, 'the frame was still rejected').toBeNull() + expect(rejectionWarnings()).toHaveLength(0) + }) + + it('does not report a rejection decided by the session path', () => { + store.ensureSessionWorkflowPath('job-b', workflowB.path) + + fire('execution_start', { prompt_id: 'job-b', timestamp: 1 }) + + expect(store.activeJobId).toBeNull() + expect(rejectionWarnings()).toHaveLength(0) + }) + }) +}) diff --git a/src/utils/__tests__/litegraphTestUtils.ts b/src/utils/__tests__/litegraphTestUtils.ts index 0bc82e8a950..6f8208a19a1 100644 --- a/src/utils/__tests__/litegraphTestUtils.ts +++ b/src/utils/__tests__/litegraphTestUtils.ts @@ -30,7 +30,7 @@ import { toNodeId } from '@comfyorg/graph-model/nodeId' import type { NodeState } from '@comfyorg/graph-model/nodeState' import { usePreviewExposureStore } from '@/stores/previewExposureStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' -import { zeroUuid } from '@/utils/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' /** Creates a node shell state with minimal required fields. */ export function createNodeState(overrides: Partial = {}): NodeState { diff --git a/src/utils/uuid.ts b/src/utils/uuid.ts index f44552ca9a7..e8bb8c41420 100644 --- a/src/utils/uuid.ts +++ b/src/utils/uuid.ts @@ -1,10 +1,8 @@ // Using a template string for this is resulting in complex type workarounds. No current benefit beyond dev reading. import type { UUID } from '@comfyorg/graph-model/uuid' +import { zeroUuid } from '@comfyorg/graph-model/uuid' export type { UUID } from '@comfyorg/graph-model/uuid' -/** Special-case zero-UUID, consisting entirely of zeros. Used as a default value. */ -export const zeroUuid = '00000000-0000-0000-0000-000000000000' - /** Pre-allocated storage for uuid random values. */ const randomStorage = new Uint32Array(31) diff --git a/tsconfig.types.json b/tsconfig.types.json index 422f4f3c256..914224ad83c 100644 --- a/tsconfig.types.json +++ b/tsconfig.types.json @@ -21,6 +21,9 @@ "@comfyorg/graph-model/*": ["./packages/graph-model/src/*"], "@comfyorg/keybindings/*": ["./packages/keybindings/src/*"], "@comfyorg/api-client/*": ["./packages/api-client/src/*"], + "@comfyorg/comfyui-api-types": [ + "./packages/comfyui-api-types/src/index.ts" + ], "@comfyorg/settings-core/*": ["./packages/settings-core/src/*"], "@comfyorg/telemetry/*": ["./packages/telemetry/src/*"], "@comfyorg/shared-frontend-utils/geometry": [ @@ -33,6 +36,7 @@ "packages/graph-model/src/**/*.ts", "packages/keybindings/src/**/*.ts", "packages/api-client/src/**/*.ts", + "packages/comfyui-api-types/src/**/*.ts", "packages/settings-core/src/**/*.ts", "packages/telemetry/src/types.ts", "packages/shared-frontend-utils/src/geometry.ts",