From 8db6a2c59247b7525c16498ff3b8ccacd8ef787c Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 5 Nov 2026 04:36:27 +0000 Subject: [PATCH 1/7] fix(workflow): load a tab before saving it instead of failing Saving a tab whose content was not loaded threw "Cannot save workflow ... before it is loaded" (the guard from #21326 that stops a null write). On cloud production this came from the close dialog's Save, the agent workflow picker and the bulk save, for saved files and for unsaved tabs a reload restored without opening. - saveWorkflow / saveWorkflowAs load an unloaded tab first, picking up its draft when one is usable. The guard in ComfyWorkflow.save stays as the backstop. - Workflow sync no longer unloads a modified background tab whose file changed remotely. Its draft is older than the new file, so reloading it dropped the edits and left the modified mark, which is the state the close-dialog Save then failed on. - A modified saved file that reaches save unloaded is still reported (static message, no path), so any other source stays visible. - The guard's error message no longer carries the workflow path: alerts showed user file names in their titles. --- .../core/services/workflowService.test.ts | 105 ++++++++++++++++++ .../workflow/core/services/workflowService.ts | 21 ++++ .../management/stores/comfyWorkflow.ts | 3 +- .../management/stores/workflowStore.test.ts | 61 +++++++++- .../management/stores/workflowStore.ts | 7 ++ .../agent/useAgentWorkflowSelection.test.ts | 8 +- 6 files changed, 197 insertions(+), 8 deletions(-) diff --git a/src/platform/workflow/core/services/workflowService.test.ts b/src/platform/workflow/core/services/workflowService.test.ts index 97fe0e4975d..24bdb42f053 100644 --- a/src/platform/workflow/core/services/workflowService.test.ts +++ b/src/platform/workflow/core/services/workflowService.test.ts @@ -51,6 +51,7 @@ import type { AppMode } from '@comfyorg/graph-model/appMode' import { isValidUuid } from '@/utils/formatUtil' import { zeroUuid } from '@comfyorg/graph-model/uuid' import { t } from '@/i18n' +import { api } from '@/scripts/api' import { stubFirebaseAuthHarness } from '@/utils/__tests__/stubAccountIdentityPort' vi.mock(import('firebase/auth'), { spy: true }) @@ -3971,6 +3972,110 @@ describe('useWorkflowService', () => { expect(workflowStore.saveWorkflow).toHaveBeenCalledWith(workflow) }) }) + + describe('saving a tab that is not loaded', () => { + let workflowStore: ReturnType + let service: ReturnType + + beforeEach(() => { + workflowStore = useWorkflowStore() + service = useWorkflowService() + vi.spyOn(api, 'getUserData').mockImplementation( + async () => new Response(JSON.stringify(defaultGraph)) + ) + vi.spyOn(api, 'storeUserData').mockImplementation( + async (path) => + new Response(JSON.stringify({ path, modified: 300, size: 2 })) + ) + vi.mocked(useDialogService().confirm).mockResolvedValue(true) + resetReportedWorkflowStateAnomaliesForTests() + mockAssert.mockClear() + }) + + // The shape Sentry reports: a saved tab whose content was dropped while it + // still carries its modified mark (isPersisted, !isLoaded). + function unloadedModifiedTab(path: string): ComfyWorkflow { + const tab = new ComfyWorkflowClass({ path, modified: 100, size: 1 }) + workflowStore.attachWorkflow(tab) + workflowStore.openWorkflowsInBackground({ right: [tab.path] }) + tab.isModified = true + return tab + } + + function storedContent(path: string): ComfyWorkflowJSON { + const call = vi + .mocked(api.storeUserData) + .mock.calls.find(([stored]) => stored === path) + assert(call, `nothing was stored at ${path}`) + return JSON.parse(call[1] as string) as ComfyWorkflowJSON + } + + it('loads and saves it when Save is chosen in the close dialog', async () => { + const tab = unloadedModifiedTab('workflows/background.json') + + expect(await service.closeWorkflow(tab)).toBe(true) + + expect(storedContent('workflows/background.json').nodes).toHaveLength( + defaultGraph.nodes.length + ) + expect(workflowStore.openWorkflows).not.toContain(tab) + expect(mockAssert).toHaveBeenCalledWith( + false, + 'Modified saved workflow was not loaded when saved', + expect.objectContaining({ + method: 'workflowService.loadForSave', + isPersisted: true, + isLoaded: false + }) + ) + }) + + it('saves the draft the user edited, not the stored file', async () => { + useSettingStore().settingsById['Comfy.Workflow.Persist'] = { + id: 'Comfy.Workflow.Persist', + name: 'Persist workflow state', + type: 'boolean', + defaultValue: true + } + const draft = { ...defaultGraph, extra: { editedInDraft: true } } + vi.mocked(useWorkflowDraftStoreV2().getDraft).mockReturnValue({ + data: JSON.stringify(draft), + name: 'background.json', + isTemporary: false, + updatedAt: 200 + }) + const tab = unloadedModifiedTab('workflows/background.json') + + expect(await service.saveWorkflow(tab)).toBe(true) + + expect(storedContent('workflows/background.json').extra).toEqual({ + editedInDraft: true + }) + expect(tab.isModified).toBe(false) + }) + + it('saves an unsaved tab that a reload restored without opening it', async () => { + const restored = workflowStore.createTemporary('Restored.json', { + ...blankGraph, + id: '7b1e2c3d-4f5a-4b6c-8d7e-9f0a1b2c3d4e' + }) + workflowStore.openWorkflowsInBackground({ right: [restored.path] }) + restored.isModified = true + expect(restored.isLoaded).toBe(false) + vi.mocked(useDialogService().prompt).mockResolvedValue('Restored') + + expect(await service.closeWorkflow(restored)).toBe(true) + + expect(storedContent('workflows/Restored.json').id).toBe( + '7b1e2c3d-4f5a-4b6c-8d7e-9f0a1b2c3d4e' + ) + expect(mockAssert).not.toHaveBeenCalledWith( + false, + 'Modified saved workflow was not loaded when saved', + expect.anything() + ) + }) + }) }) vi.mock(import('@vueuse/router'), async () => { diff --git a/src/platform/workflow/core/services/workflowService.ts b/src/platform/workflow/core/services/workflowService.ts index e3b883fa898..22b7b8ecea7 100644 --- a/src/platform/workflow/core/services/workflowService.ts +++ b/src/platform/workflow/core/services/workflowService.ts @@ -332,6 +332,25 @@ export const useWorkflowService = () => { if (!file) return downloadBlob(file, blob) } + /** + * A tab can lose its loaded content and keep its modified mark: a reload + * restores unsaved tabs without opening them, and a superseded open drops + * what it fetched. Saving needs the content back first; loading picks up + * the tab's draft when one is still usable. + */ + const loadForSave = async (workflow: ComfyWorkflow): Promise => { + if (workflow.isLoaded) return + // A restored unsaved tab is expected here. A saved file that is modified + // but unloaded means something dropped in-memory edits: keep it visible. + if (workflow.isPersisted && workflow.isModified) + reportWorkflowStateAnomaly( + 'Modified saved workflow was not loaded when saved', + 'workflowService.loadForSave', + workflow + ) + await loadWorkflowInBackground(workflow) + } + /** * Save a workflow as a new file * @param workflow The workflow to save @@ -341,6 +360,7 @@ export const useWorkflowService = () => { workflow: ComfyWorkflow, options: { filename?: string; isApp?: boolean } = {} ): Promise => { + await loadForSave(workflow) const newFilename = options.filename ?? (await workflow.promptSave()) if (!newFilename) return false @@ -396,6 +416,7 @@ export const useWorkflowService = () => { return await saveWorkflowAs(workflow) } + await loadForSave(workflow) workflow.changeTracker?.prepareForSave() const isApp = workflow.initialMode === 'app' const expectedPath = diff --git a/src/platform/workflow/management/stores/comfyWorkflow.ts b/src/platform/workflow/management/stores/comfyWorkflow.ts index b8145067d9e..edbafc052f0 100644 --- a/src/platform/workflow/management/stores/comfyWorkflow.ts +++ b/src/platform/workflow/management/stores/comfyWorkflow.ts @@ -280,7 +280,8 @@ function requireActiveStateForSave( method, workflow ) - throw new Error(`Cannot save workflow '${workflow.path}' before it is loaded`) + // No path in the message: error trackers group by it and show it in alerts. + throw new Error('Cannot save a workflow before it is loaded') } export class ComfyWorkflow extends UserFile { diff --git a/src/platform/workflow/management/stores/workflowStore.test.ts b/src/platform/workflow/management/stores/workflowStore.test.ts index 2c330ba534b..0e6533cff83 100644 --- a/src/platform/workflow/management/stores/workflowStore.test.ts +++ b/src/platform/workflow/management/stores/workflowStore.test.ts @@ -169,6 +169,61 @@ describe('useWorkflowStore', () => { expect(workflow.size).toBe(2) }) + it('keeps the unsaved edits of a background tab whose file changed remotely', async () => { + await syncRemoteWorkflowsWithMeta([ + { path: 'edited.json', modified: 100, size: 1 }, + { path: 'active.json', modified: 100, size: 1 } + ]) + const edited = store.getWorkflowByPath('workflows/edited.json')! + vi.mocked(api.getUserData).mockResolvedValue({ + status: 200, + text: () => Promise.resolve(defaultGraphJSON) + } as Response) + await store.openWorkflow(edited) + edited.changeTracker!.activeState = { + ...edited.changeTracker!.activeState, + extra: { unsaved: true } + } + edited.isModified = true + await store.openWorkflow( + store.getWorkflowByPath('workflows/active.json')! + ) + + await syncRemoteWorkflowsWithMeta([ + { path: 'edited.json', modified: 200, size: 2 }, + { path: 'active.json', modified: 100, size: 1 } + ]) + + expect(edited.isLoaded).toBe(true) + expect(edited.isModified).toBe(true) + expect(edited.activeState?.extra).toEqual({ unsaved: true }) + expect(edited.lastModified).toBe(200) + }) + + it('still drops the cached content of an unmodified background tab whose file changed', async () => { + await syncRemoteWorkflowsWithMeta([ + { path: 'clean.json', modified: 100, size: 1 }, + { path: 'active.json', modified: 100, size: 1 } + ]) + const clean = store.getWorkflowByPath('workflows/clean.json')! + vi.mocked(api.getUserData).mockResolvedValue({ + status: 200, + text: () => Promise.resolve(defaultGraphJSON) + } as Response) + await store.openWorkflow(clean) + await store.openWorkflow( + store.getWorkflowByPath('workflows/active.json')! + ) + + await syncRemoteWorkflowsWithMeta([ + { path: 'clean.json', modified: 200, size: 2 }, + { path: 'active.json', modified: 100, size: 1 } + ]) + + expect(clean.isLoaded).toBe(false) + expect(clean.isModified).toBe(false) + }) + describe('node widget histories', () => { const rootGraphId = '00000000-0000-4000-8000-0000000000a1' @@ -1664,7 +1719,11 @@ describe('useWorkflowStore', () => { ? workflow.save() : workflow.saveAs('workflows/copy.json') - await expect(attempt).rejects.toThrow('before it is loaded') + // Static message: the path would split the error into one group per + // file and put user file names in alert titles. + await expect(attempt).rejects.toThrow( + /^Cannot save a workflow before it is loaded$/ + ) expect(api.storeUserData).not.toHaveBeenCalled() expect(workflow.content).toBe(contentBefore) expectReported('Workflow saved without an active state', { diff --git a/src/platform/workflow/management/stores/workflowStore.ts b/src/platform/workflow/management/stores/workflowStore.ts index a1d636ed939..f952acb27e9 100644 --- a/src/platform/workflow/management/stores/workflowStore.ts +++ b/src/platform/workflow/management/stores/workflowStore.ts @@ -461,6 +461,13 @@ export const useWorkflowStore = defineStore('workflow', () => { return } + // A modified tab holds edits that exist only in memory, as the + // active one does. Its draft is older than the new remote file, so + // a reload would drop them and leave the modified mark behind. + if (existingWorkflow.isModified) { + return + } + existingWorkflow.unload() }, /* exclude */ (workflow) => workflow.isTemporary, diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.test.ts index 7d79d463e5f..9021a2a3f93 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.test.ts @@ -899,9 +899,7 @@ describe('picking a target tab', () => { vi.mocked(useWorkflowService().saveWorkflowAs).mockImplementationOnce( async (tab) => { if (!tab.isLoaded) - throw new Error( - `Cannot save workflow '${tab.path}' before it is loaded` - ) + throw new Error('Cannot save a workflow before it is loaded') saved = true return true } @@ -965,9 +963,7 @@ describe('picking a target tab', () => { vi.mocked(useWorkflowService().saveWorkflowAs).mockImplementationOnce( async (tab) => { if (!tab.isLoaded) - throw new Error( - `Cannot save workflow '${tab.path}' before it is loaded` - ) + throw new Error('Cannot save a workflow before it is loaded') saved = true return true } From a4c24c589bbdd40cd574e62db60d3c04d25067ac Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 5 Nov 2026 04:44:07 +0000 Subject: [PATCH 2/7] fix(workflow): say whether the unloaded tab was active in the save report --- src/platform/workflow/core/services/workflowService.test.ts | 3 ++- src/platform/workflow/core/services/workflowService.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/platform/workflow/core/services/workflowService.test.ts b/src/platform/workflow/core/services/workflowService.test.ts index 24bdb42f053..299472e45f1 100644 --- a/src/platform/workflow/core/services/workflowService.test.ts +++ b/src/platform/workflow/core/services/workflowService.test.ts @@ -4025,7 +4025,8 @@ describe('useWorkflowService', () => { expect.objectContaining({ method: 'workflowService.loadForSave', isPersisted: true, - isLoaded: false + isLoaded: false, + isActive: false }) ) }) diff --git a/src/platform/workflow/core/services/workflowService.ts b/src/platform/workflow/core/services/workflowService.ts index 22b7b8ecea7..9d8729e4715 100644 --- a/src/platform/workflow/core/services/workflowService.ts +++ b/src/platform/workflow/core/services/workflowService.ts @@ -346,7 +346,8 @@ export const useWorkflowService = () => { reportWorkflowStateAnomaly( 'Modified saved workflow was not loaded when saved', 'workflowService.loadForSave', - workflow + workflow, + { isActive: workflowStore.isActive(workflow) } ) await loadWorkflowInBackground(workflow) } From 45147094a511aae2b135e452ec2e759610104d5a Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 5 Nov 2026 05:06:08 +0000 Subject: [PATCH 3/7] fix(workflow): do not save a tab whose edits are gone; keep a kept tab's draft usable Review follow-ups: - When loading an unloaded saved tab finds no usable draft, its edits were already lost: write nothing, clear the modified mark, tell the user, and report it. A plain write would have reported Save as done. - An active tab with no loaded content still hits the guard instead of being saved from a background copy that is not the canvas. - Save As loads after the name prompt, so a cancelled prompt costs nothing. - Sync leaves a modified background tab's metadata alone too, so its draft stays newer than the file and survives a reload. - Drop code comments in the touched functions. --- src/locales/en/main.json | 4 +- .../core/services/workflowService.test.ts | 90 +++++++++++++------ .../workflow/core/services/workflowService.ts | 42 +++++---- .../management/stores/comfyWorkflow.ts | 1 - .../management/stores/workflowStore.test.ts | 4 +- .../management/stores/workflowStore.ts | 11 +-- 6 files changed, 95 insertions(+), 57 deletions(-) diff --git a/src/locales/en/main.json b/src/locales/en/main.json index a38cca7c9d2..873c22018e9 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -1364,7 +1364,9 @@ "enterFilenamePrompt": "Enter the filename:", "saveWorkflow": "Save workflow", "savedAsApp": "Converted to app workflow", - "savedAsWorkflow": "Converted to node graph only workflow" + "savedAsWorkflow": "Converted to node graph only workflow", + "unsavedChangesLostTitle": "Unsaved changes were not saved", + "unsavedChangesLost": "The unsaved changes to {name} were no longer available, so the saved file was left as it was." }, "subgraphStore": { "confirmDeleteTitle": "Delete blueprint?", diff --git a/src/platform/workflow/core/services/workflowService.test.ts b/src/platform/workflow/core/services/workflowService.test.ts index 299472e45f1..9133469654e 100644 --- a/src/platform/workflow/core/services/workflowService.test.ts +++ b/src/platform/workflow/core/services/workflowService.test.ts @@ -3992,8 +3992,6 @@ describe('useWorkflowService', () => { mockAssert.mockClear() }) - // The shape Sentry reports: a saved tab whose content was dropped while it - // still carries its modified mark (isPersisted, !isLoaded). function unloadedModifiedTab(path: string): ComfyWorkflow { const tab = new ComfyWorkflowClass({ path, modified: 100, size: 1 }) workflowStore.attachWorkflow(tab) @@ -4010,41 +4008,83 @@ describe('useWorkflowService', () => { return JSON.parse(call[1] as string) as ComfyWorkflowJSON } - it('loads and saves it when Save is chosen in the close dialog', async () => { + function enableDrafts() { + useSettingStore().settingsById['Comfy.Workflow.Persist'] = { + id: 'Comfy.Workflow.Persist', + name: 'Persist workflow state', + type: 'boolean', + defaultValue: true + } + } + + function draftOf(extra: Record, updatedAt: number) { + vi.mocked(useWorkflowDraftStoreV2().getDraft).mockReturnValue({ + data: JSON.stringify({ ...defaultGraph, extra }), + name: 'background.json', + isTemporary: false, + updatedAt + }) + } + + it('loads its draft and saves it when Save is chosen in the close dialog', async () => { + enableDrafts() + draftOf({ editedInDraft: true }, 200) const tab = unloadedModifiedTab('workflows/background.json') expect(await service.closeWorkflow(tab)).toBe(true) - expect(storedContent('workflows/background.json').nodes).toHaveLength( - defaultGraph.nodes.length - ) + expect(storedContent('workflows/background.json').extra).toEqual({ + editedInDraft: true + }) expect(workflowStore.openWorkflows).not.toContain(tab) + expect(mockAssert).not.toHaveBeenCalledWith( + false, + 'Modified saved workflow lost its edits before saving', + expect.anything() + ) + }) + + it('writes nothing and says so when the edits are no longer available', async () => { + enableDrafts() + draftOf({ editedInDraft: true }, 50) + const tab = unloadedModifiedTab('workflows/background.json') + + expect(await service.closeWorkflow(tab)).toBe(true) + + expect(api.storeUserData).not.toHaveBeenCalled() + expect(tab.isModified).toBe(false) + expect(useToast().toasts).toEqual([ + expect.objectContaining({ + kind: 'warning', + title: 'Unsaved changes were not saved' + }) + ]) expect(mockAssert).toHaveBeenCalledWith( false, - 'Modified saved workflow was not loaded when saved', + 'Modified saved workflow lost its edits before saving', expect.objectContaining({ method: 'workflowService.loadForSave', - isPersisted: true, - isLoaded: false, - isActive: false + isPersisted: true }) ) }) - it('saves the draft the user edited, not the stored file', async () => { - useSettingStore().settingsById['Comfy.Workflow.Persist'] = { - id: 'Comfy.Workflow.Persist', - name: 'Persist workflow state', - type: 'boolean', - defaultValue: true - } - const draft = { ...defaultGraph, extra: { editedInDraft: true } } - vi.mocked(useWorkflowDraftStoreV2().getDraft).mockReturnValue({ - data: JSON.stringify(draft), - name: 'background.json', - isTemporary: false, - updatedAt: 200 - }) + it('keeps refusing to save an active tab that has no loaded content', async () => { + const tab = unloadedModifiedTab('workflows/active.json') + workflowStore.activeWorkflow = fromAny(tab) + const load = vi.spyOn(tab, 'load') + + await expect(service.saveWorkflow(tab)).rejects.toThrow( + 'Cannot save a workflow before it is loaded' + ) + + expect(load).not.toHaveBeenCalled() + expect(api.storeUserData).not.toHaveBeenCalled() + }) + + it('saves the draft the user edited when saved directly', async () => { + enableDrafts() + draftOf({ editedInDraft: true }, 200) const tab = unloadedModifiedTab('workflows/background.json') expect(await service.saveWorkflow(tab)).toBe(true) @@ -4072,7 +4112,7 @@ describe('useWorkflowService', () => { ) expect(mockAssert).not.toHaveBeenCalledWith( false, - 'Modified saved workflow was not loaded when saved', + 'Modified saved workflow lost its edits before saving', expect.anything() ) }) diff --git a/src/platform/workflow/core/services/workflowService.ts b/src/platform/workflow/core/services/workflowService.ts index 9d8729e4715..9399cc98b3d 100644 --- a/src/platform/workflow/core/services/workflowService.ts +++ b/src/platform/workflow/core/services/workflowService.ts @@ -332,24 +332,31 @@ export const useWorkflowService = () => { if (!file) return downloadBlob(file, blob) } + const lostUnsavedEdits = (workflow: ComfyWorkflow): boolean => + workflow.isPersisted && + workflow.isModified && + workflow.content === workflow.originalContent + /** - * A tab can lose its loaded content and keep its modified mark: a reload - * restores unsaved tabs without opening them, and a superseded open drops - * what it fetched. Saving needs the content back first; loading picks up - * the tab's draft when one is still usable. + * Loads a tab that is not loaded so it can be saved, applying its draft. + * Returns false when its unsaved edits are gone and nothing is left to save. */ - const loadForSave = async (workflow: ComfyWorkflow): Promise => { - if (workflow.isLoaded) return - // A restored unsaved tab is expected here. A saved file that is modified - // but unloaded means something dropped in-memory edits: keep it visible. - if (workflow.isPersisted && workflow.isModified) - reportWorkflowStateAnomaly( - 'Modified saved workflow was not loaded when saved', - 'workflowService.loadForSave', - workflow, - { isActive: workflowStore.isActive(workflow) } - ) + const loadForSave = async (workflow: ComfyWorkflow): Promise => { + if (workflow.isLoaded || workflowStore.isActive(workflow)) return true await loadWorkflowInBackground(workflow) + if (!lostUnsavedEdits(workflow)) return true + reportWorkflowStateAnomaly( + 'Modified saved workflow lost its edits before saving', + 'workflowService.loadForSave', + workflow + ) + workflow.isModified = false + toast.warning(t('workflowService.unsavedChangesLostTitle'), { + description: t('workflowService.unsavedChangesLost', { + name: workflow.filename + }) + }) + return false } /** @@ -361,9 +368,9 @@ export const useWorkflowService = () => { workflow: ComfyWorkflow, options: { filename?: string; isApp?: boolean } = {} ): Promise => { - await loadForSave(workflow) const newFilename = options.filename ?? (await workflow.promptSave()) if (!newFilename) return false + if (!(await loadForSave(workflow))) return false const isApp = options.isApp ?? workflow.initialMode === 'app' const newPath = @@ -384,7 +391,6 @@ export const useWorkflowService = () => { if (isSelfOverwrite) { workflow.changeTracker?.prepareForSave() - // Call workflowStore.saveWorkflow directly: saveWorkflowAs emits its own is_new:true event below, so delegating to saveWorkflow() would also fire is_new:false and run prepareForSave a second time. await workflowStore.saveWorkflow(workflow) } else { let target: ComfyWorkflow @@ -417,7 +423,7 @@ export const useWorkflowService = () => { return await saveWorkflowAs(workflow) } - await loadForSave(workflow) + if (!(await loadForSave(workflow))) return true workflow.changeTracker?.prepareForSave() const isApp = workflow.initialMode === 'app' const expectedPath = diff --git a/src/platform/workflow/management/stores/comfyWorkflow.ts b/src/platform/workflow/management/stores/comfyWorkflow.ts index edbafc052f0..0b2cbdbc808 100644 --- a/src/platform/workflow/management/stores/comfyWorkflow.ts +++ b/src/platform/workflow/management/stores/comfyWorkflow.ts @@ -280,7 +280,6 @@ function requireActiveStateForSave( method, workflow ) - // No path in the message: error trackers group by it and show it in alerts. throw new Error('Cannot save a workflow before it is loaded') } diff --git a/src/platform/workflow/management/stores/workflowStore.test.ts b/src/platform/workflow/management/stores/workflowStore.test.ts index 0e6533cff83..eda98fbdf69 100644 --- a/src/platform/workflow/management/stores/workflowStore.test.ts +++ b/src/platform/workflow/management/stores/workflowStore.test.ts @@ -197,7 +197,7 @@ describe('useWorkflowStore', () => { expect(edited.isLoaded).toBe(true) expect(edited.isModified).toBe(true) expect(edited.activeState?.extra).toEqual({ unsaved: true }) - expect(edited.lastModified).toBe(200) + expect(edited.lastModified).toBe(100) }) it('still drops the cached content of an unmodified background tab whose file changed', async () => { @@ -1719,8 +1719,6 @@ describe('useWorkflowStore', () => { ? workflow.save() : workflow.saveAs('workflows/copy.json') - // Static message: the path would split the error into one group per - // file and put user file names in alert titles. await expect(attempt).rejects.toThrow( /^Cannot save a workflow before it is loaded$/ ) diff --git a/src/platform/workflow/management/stores/workflowStore.ts b/src/platform/workflow/management/stores/workflowStore.ts index f952acb27e9..e4e164b2918 100644 --- a/src/platform/workflow/management/stores/workflowStore.ts +++ b/src/platform/workflow/management/stores/workflowStore.ts @@ -437,6 +437,8 @@ export const useWorkflowStore = defineStore('workflow', () => { const isActiveWorkflow = activeWorkflow.value?.path === existingWorkflow.path + if (!isActiveWorkflow && existingWorkflow.isModified) return + const nextLastModified = Math.max( existingWorkflow.lastModified, file.modified @@ -451,23 +453,14 @@ export const useWorkflowStore = defineStore('workflow', () => { existingWorkflow.size = file.size } - // Never unload the active workflow - it may contain unsaved in-memory edits. if (isActiveWorkflow) { return } - // If nothing changed, keep any loaded content cached. if (isMetadataUnchanged) { return } - // A modified tab holds edits that exist only in memory, as the - // active one does. Its draft is older than the new remote file, so - // a reload would drop them and leave the modified mark behind. - if (existingWorkflow.isModified) { - return - } - existingWorkflow.unload() }, /* exclude */ (workflow) => workflow.isTemporary, From b1b1597bc08ad83a83673cea517de8a20c2f7345 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 5 Nov 2026 05:57:50 +0000 Subject: [PATCH 4/7] refactor(workflow): declare the save helpers as functions; cover Save As of an unloaded saved tab --- .../core/services/workflowService.test.ts | 19 +++++++++++++++++++ .../workflow/core/services/workflowService.ts | 18 +++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/platform/workflow/core/services/workflowService.test.ts b/src/platform/workflow/core/services/workflowService.test.ts index c52b989a5dc..bad1cf83659 100644 --- a/src/platform/workflow/core/services/workflowService.test.ts +++ b/src/platform/workflow/core/services/workflowService.test.ts @@ -4374,6 +4374,25 @@ describe('useWorkflowService', () => { expect(tab.isModified).toBe(false) }) + it('saves the draft of an unloaded saved tab under a new name', async () => { + enableDrafts() + draftOf({ editedInDraft: true }, 200) + const tab = unloadedModifiedTab('workflows/background.json') + + expect( + await service.saveWorkflowAs(tab, { filename: 'background copy' }) + ).toBe(true) + + expect(storedContent('workflows/background copy.json').extra).toEqual({ + editedInDraft: true + }) + expect( + vi + .mocked(api.storeUserData) + .mock.calls.some(([path]) => path === 'workflows/background.json') + ).toBe(false) + }) + it('saves an unsaved tab that a reload restored without opening it', async () => { const restored = workflowStore.createTemporary('Restored.json', { ...blankGraph, diff --git a/src/platform/workflow/core/services/workflowService.ts b/src/platform/workflow/core/services/workflowService.ts index 01ba9388ec0..75843a6db4e 100644 --- a/src/platform/workflow/core/services/workflowService.ts +++ b/src/platform/workflow/core/services/workflowService.ts @@ -345,16 +345,16 @@ export const useWorkflowService = () => { if (!file) return downloadBlob(file, blob) } - const lostUnsavedEdits = (workflow: ComfyWorkflow): boolean => - workflow.isPersisted && - workflow.isModified && - workflow.content === workflow.originalContent - /** - * Loads a tab that is not loaded so it can be saved, applying its draft. - * Returns false when its unsaved edits are gone and nothing is left to save. - */ - const loadForSave = async (workflow: ComfyWorkflow): Promise => { + function lostUnsavedEdits(workflow: ComfyWorkflow): boolean { + return ( + workflow.isPersisted && + workflow.isModified && + workflow.content === workflow.originalContent + ) + } + + async function loadForSave(workflow: ComfyWorkflow): Promise { if (workflow.isLoaded || workflowStore.isActive(workflow)) return true await loadWorkflowInBackground(workflow) if (!lostUnsavedEdits(workflow)) return true From 053a63bc7c9851b2f4e5efe042e0a2cb9041817a Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 5 Nov 2026 06:23:43 +0000 Subject: [PATCH 5/7] test(workflow): return a real Response from the sync test loads --- .../management/stores/workflowStore.test.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/platform/workflow/management/stores/workflowStore.test.ts b/src/platform/workflow/management/stores/workflowStore.test.ts index 790e3b0f752..1190d2a6ceb 100644 --- a/src/platform/workflow/management/stores/workflowStore.test.ts +++ b/src/platform/workflow/management/stores/workflowStore.test.ts @@ -178,10 +178,9 @@ describe('useWorkflowStore', () => { { path: 'active.json', modified: 100, size: 1 } ]) const edited = store.getWorkflowByPath('workflows/edited.json')! - vi.mocked(api.getUserData).mockResolvedValue({ - status: 200, - text: () => Promise.resolve(defaultGraphJSON) - } as Response) + vi.mocked(api.getUserData).mockImplementation( + async () => new Response(defaultGraphJSON, { status: 200 }) + ) await store.openWorkflow(edited) edited.changeTracker!.activeState = { ...edited.changeTracker!.activeState, @@ -209,10 +208,9 @@ describe('useWorkflowStore', () => { { path: 'active.json', modified: 100, size: 1 } ]) const clean = store.getWorkflowByPath('workflows/clean.json')! - vi.mocked(api.getUserData).mockResolvedValue({ - status: 200, - text: () => Promise.resolve(defaultGraphJSON) - } as Response) + vi.mocked(api.getUserData).mockImplementation( + async () => new Response(defaultGraphJSON, { status: 200 }) + ) await store.openWorkflow(clean) await store.openWorkflow( store.getWorkflowByPath('workflows/active.json')! From 7ebefd9fa25f416f97d716ea622e0fbff01828bf Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 5 Nov 2026 06:53:55 +0000 Subject: [PATCH 6/7] test(workflow): give only the source tab a draft in the save tests Save As loads the destination too, so a draft returned for every path could let the Save As test pass while copying the stale source. --- .../core/services/workflowService.test.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/platform/workflow/core/services/workflowService.test.ts b/src/platform/workflow/core/services/workflowService.test.ts index bad1cf83659..009bd850d4e 100644 --- a/src/platform/workflow/core/services/workflowService.test.ts +++ b/src/platform/workflow/core/services/workflowService.test.ts @@ -4297,12 +4297,17 @@ describe('useWorkflowService', () => { } function draftOf(extra: Record, updatedAt: number) { - vi.mocked(useWorkflowDraftStoreV2().getDraft).mockReturnValue({ - data: JSON.stringify({ ...defaultGraph, extra }), - name: 'background.json', - isTemporary: false, - updatedAt - }) + vi.mocked(useWorkflowDraftStoreV2().getDraft).mockImplementation( + (path) => + path === 'workflows/background.json' + ? { + data: JSON.stringify({ ...defaultGraph, extra }), + name: 'background.json', + isTemporary: false, + updatedAt + } + : null + ) } it('loads its draft and saves it when Save is chosen in the close dialog', async () => { From 406d070dedf1f6b52186eb26b091a68a97c77967 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 5 Nov 2026 06:56:25 +0000 Subject: [PATCH 7/7] [automated] Apply ESLint and Oxfmt fixes --- .../core/services/workflowService.test.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/platform/workflow/core/services/workflowService.test.ts b/src/platform/workflow/core/services/workflowService.test.ts index 009bd850d4e..012c55680ee 100644 --- a/src/platform/workflow/core/services/workflowService.test.ts +++ b/src/platform/workflow/core/services/workflowService.test.ts @@ -4297,16 +4297,15 @@ describe('useWorkflowService', () => { } function draftOf(extra: Record, updatedAt: number) { - vi.mocked(useWorkflowDraftStoreV2().getDraft).mockImplementation( - (path) => - path === 'workflows/background.json' - ? { - data: JSON.stringify({ ...defaultGraph, extra }), - name: 'background.json', - isTemporary: false, - updatedAt - } - : null + vi.mocked(useWorkflowDraftStoreV2().getDraft).mockImplementation((path) => + path === 'workflows/background.json' + ? { + data: JSON.stringify({ ...defaultGraph, extra }), + name: 'background.json', + isTemporary: false, + updatedAt + } + : null ) }