diff --git a/browser_tests/tests/workflowNullStoredContent.spec.ts b/browser_tests/tests/workflowNullStoredContent.spec.ts new file mode 100644 index 00000000000..0de4cc5f7d0 --- /dev/null +++ b/browser_tests/tests/workflowNullStoredContent.spec.ts @@ -0,0 +1,155 @@ +import type { Page } from '@playwright/test' + +import type { ComfyPage } from '@e2e/fixtures/ComfyPage' +import { + comfyExpect as expect, + comfyPageFixture as test +} from '@e2e/fixtures/ComfyPage' +import type { WorkspaceStore } from '@e2e/types/globals' + +/** + * Production crashes CLOUD-FRONTEND-PROD-2Z2, -2Z3, -28Y and -233 + * (`null is not an object (evaluating 'e.initialState.extra')` / + * `(evaluating 'e.nodes')`, frontend 1.56.3 and 1.57.0): a saved workflow + * whose stored content is JSON `null` is reused by name, which leaves the + * active workflow's change tracker with a null initial state. Every later + * canvas capture, graph load and app-mode refresh then throws. + */ + +const WORKFLOW_PATH = 'workflows/default.json' + +interface DraftCopy { + indexKey: string + draftKey: string + entry: Record + payloadKey: string + payload: string +} + +async function readDraft(page: Page): Promise { + return page.evaluate((path) => { + for (const indexKey of Object.keys(localStorage)) { + if (!indexKey.startsWith('Comfy.Workflow.DraftIndex.v2:')) continue + const index = JSON.parse(localStorage.getItem(indexKey)!) + for (const [draftKey, entry] of Object.entries(index.entries)) { + if ((entry as { path: string }).path !== path) continue + const workspace = indexKey.slice('Comfy.Workflow.DraftIndex.v2:'.length) + const payloadKey = `Comfy.Workflow.Draft.v2:${workspace}:${draftKey}` + const payload = localStorage.getItem(payloadKey) + if (!payload) return null + return { + indexKey, + draftKey, + entry: entry as Record, + payloadKey, + payload + } + } + } + return null + }, WORKFLOW_PATH) +} + +/** + * Leaves `workflows/default.json` closed, stored as JSON `null`, with a + * local draft newer than the stored file: the state the crashing sessions + * were in when they reused the workflow by name. + */ +async function seedNullStoredWorkflowWithDraft(comfyPage: ComfyPage) { + const { page } = comfyPage + await comfyPage.workflow.setupWorkflowsDirectory({}) + await comfyPage.menu.topbar.saveWorkflow('default') + + await page.evaluate(() => { + window.app!.graph.add(window.LiteGraph!.createNode('Note')) + ;( + window.app!.extensionManager as WorkspaceStore + ).workflow.activeWorkflow!.changeTracker.captureCanvasState() + }) + await expect + .poll(async () => (await readDraft(page))?.payload.includes('Note')) + .toBe(true) + const draft = await readDraft(page) + if (!draft) throw new Error(`No draft persisted for ${WORKFLOW_PATH}`) + + // Closing a tab discards its draft; the draft is restored below. + await comfyPage.menu.topbar.triggerTopbarCommand(['New']) + await comfyPage.menu.topbar.closeWorkflowTab('default') + + await page.evaluate( + async ({ path, draft }) => { + const api = window.app!.api + await api.storeUserData(path, 'null', { + overwrite: true, + stringify: false, + throwOnError: true + }) + const stored = (await api.listUserDataFullInfo('workflows')).find( + (file) => `workflows/${file.path}` === path + ) + if (!stored) throw new Error(`${path} was not stored`) + const updatedAt = Math.max(Date.now(), stored.modified) + 1 + const payload = JSON.parse(draft.payload) + localStorage.setItem( + draft.payloadKey, + JSON.stringify({ ...payload, updatedAt }) + ) + const index = JSON.parse(localStorage.getItem(draft.indexKey)!) + index.entries[draft.draftKey] = { ...draft.entry, updatedAt } + if (!index.order.includes(draft.draftKey)) + index.order.push(draft.draftKey) + index.updatedAt = updatedAt + localStorage.setItem(draft.indexKey, JSON.stringify(index)) + }, + { path: WORKFLOW_PATH, draft } + ) +} + +test.describe( + 'Workflow whose stored content is null', + { tag: '@workflow' }, + () => { + test('reusing it by name does not leave the app throwing on every edit and load', async ({ + comfyPage + }) => { + const { page } = comfyPage + await seedNullStoredWorkflowWithDraft(comfyPage) + + const pageErrors: string[] = [] + page.on('pageerror', (error) => pageErrors.push(error.message)) + await comfyPage.workflow.reloadAndWaitForApp() + + // Dropping default.json loads it under the name "default", which + // reuses the stored workflows/default.json. + await comfyPage.dragDrop.dragAndDropFile('default.json', { + waitForUpload: false + }) + await expect.poll(() => comfyPage.nodeOps.getGraphNodesCount()).toBe(7) + + // An ordinary canvas gesture: mouseup captures the canvas state. + const canvas = page.locator('#graph-canvas') + const box = await canvas.boundingBox() + if (!box) throw new Error('Canvas is not visible') + await page.mouse.move(box.x + box.width / 2, box.y + box.height - 40) + await page.mouse.down() + await page.mouse.move(box.x + box.width / 2 + 40, box.y + box.height - 20) + await page.mouse.up() + + // Loading another graph deactivates the current workflow and fires + // the graph's `configured` event. + await comfyPage.menu.topbar.triggerTopbarCommand(['New']) + await expect.poll(() => comfyPage.nodeOps.getGraphNodesCount()).toBe(0) + + expect(pageErrors).toEqual([]) + + // Not a silent wipe: the stored file is not rewritten and the local + // draft that holds the user's last edits survives. + const storedContent = await page.evaluate(async (path) => { + const response = await window.app!.api.getUserData(path) + return response.text() + }, WORKFLOW_PATH) + expect(storedContent).toBe('null') + expect(await readDraft(page)).not.toBeNull() + }) + } +) diff --git a/src/platform/workflow/core/services/workflowService.test.ts b/src/platform/workflow/core/services/workflowService.test.ts index e9d397257bc..0b50a8bd3d8 100644 --- a/src/platform/workflow/core/services/workflowService.test.ts +++ b/src/platform/workflow/core/services/workflowService.test.ts @@ -6,13 +6,18 @@ import { useDomWidgetStore } from '@/stores/domWidgetStore' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { fromAny } from '@total-typescript/shoehorn' + import { useTelemetry } from '@/platform/telemetry' import type { LoadedComfyWorkflow, PendingWarnings } from '@/platform/workflow/management/stores/comfyWorkflow' -import { ComfyWorkflow as ComfyWorkflowClass } from '@/platform/workflow/management/stores/comfyWorkflow' +import { + ComfyWorkflow as ComfyWorkflowClass, + resetReportedWorkflowStateAnomaliesForTests +} from '@/platform/workflow/management/stores/comfyWorkflow' import { useSettingStore } from '@/platform/settings/settingStore' import { defaultGraph } from '@/scripts/defaultGraph' import { useToast } from '@/components/ui/toast/toastStore' @@ -91,6 +96,12 @@ vi.mock(import('@/services/dialogService')) vi.mock(import('@/scripts/app')) +const mockAssert = vi.hoisted(() => vi.fn()) + +vi.mock(import('@/base/assert'), () => ({ + assert: mockAssert +})) + const storeThumbnailMock = vi.hoisted(() => vi.fn()) vi.mock( @@ -2180,6 +2191,149 @@ describe('useWorkflowService', () => { }) }) + describe('null-state hardening', () => { + let workflowStore: ReturnType + + const reportedContexts = () => + mockAssert.mock.calls + .filter(([condition]) => !condition) + .map(([, message, context]) => ({ message, context })) + + beforeEach(() => { + resetReportedWorkflowStateAnomaliesForTests() + workflowStore = useWorkflowStore() + }) + + describe('beforeLoadNewGraph', () => { + it('tolerates an active workflow with no change tracker and reports it', () => { + enableWarningSettings() + const activeWorkflow = createModeTestWorkflow({ loaded: false }) + workflowStore.activeWorkflow = activeWorkflow + + expect(() => useWorkflowService().beforeLoadNewGraph()).not.toThrow() + + expect(reportedContexts()).toEqual([ + { + message: + 'Active workflow has no change tracker before loading a new graph', + context: expect.objectContaining({ + method: 'beforeLoadNewGraph', + isActive: true, + isLoaded: false, + hasChangeTracker: false + }) + } + ]) + expect( + useSubgraphNavigationStore().saveCurrentViewport + ).toHaveBeenCalled() + }) + + it('deactivates the tracker of a loaded workflow without reporting', () => { + enableWarningSettings() + const activeWorkflow = createModeTestWorkflow() + workflowStore.activeWorkflow = activeWorkflow + + useWorkflowService().beforeLoadNewGraph() + + expect(activeWorkflow.changeTracker.deactivate).toHaveBeenCalledOnce() + expect(reportedContexts()).toEqual([]) + }) + }) + + describe('afterLoadNewGraph', () => { + beforeEach(() => { + vi.mocked(workflowStore.getWorkflowByPath).mockImplementation((path) => + path === 'workflows/repeat.json' ? current : null + ) + vi.mocked(workflowStore.isActive).mockReturnValue(true) + vi.mocked(workflowStore.openWorkflow).mockImplementation( + async () => current + ) + }) + + let current: LoadedComfyWorkflow + + it('falls back to the fresh load mode when the initial state is missing', async () => { + current = createModeTestWorkflow({ path: 'workflows/repeat.json' }) + current.changeTracker.initialState = fromAny( + null + ) + + await expect( + useWorkflowService().afterLoadNewGraph( + 'repeat', + makeWorkflowData({ linearMode: true }) + ) + ).resolves.toBeUndefined() + + expect(current.initialMode).toBe('app') + expect(current.changeTracker.reset).toHaveBeenCalled() + expect(reportedContexts()).toEqual([ + { + message: 'Activated workflow has no initial state', + context: expect.objectContaining({ + method: 'activateLoadedWorkflow.initialMode', + isActive: true, + hasChangeTracker: true, + hasInitialState: false + }) + } + ]) + }) + + it('reads the saved linearMode when the initial state is present', async () => { + current = createModeTestWorkflow({ path: 'workflows/repeat.json' }) + current.changeTracker.initialState.extra = { linearMode: false } + + await useWorkflowService().afterLoadNewGraph( + 'repeat', + makeWorkflowData({ linearMode: true }) + ) + + expect(current.initialMode).toBe('graph') + expect(reportedContexts()).toEqual([]) + }) + + it('skips the tracker reset instead of throwing when the workflow has no tracker', async () => { + current = createModeTestWorkflow({ + path: 'workflows/repeat.json', + loaded: false + }) + + await expect( + useWorkflowService().afterLoadNewGraph('repeat', makeWorkflowData()) + ).resolves.toBeUndefined() + + expect(reportedContexts().map(({ context }) => context)).toEqual([ + expect.objectContaining({ + method: 'activateLoadedWorkflow.initialMode' + }), + expect.objectContaining({ + method: 'activateLoadedWorkflow.resetTracker', + isActive: true, + hasChangeTracker: false + }) + ]) + }) + + it('skips the tracker reset when opening a workflow by reference without a tracker', async () => { + const unloaded = createModeTestWorkflow({ loaded: false }) + vi.mocked(workflowStore.openWorkflow).mockResolvedValue(unloaded) + + await expect( + useWorkflowService().afterLoadNewGraph(unloaded, makeWorkflowData()) + ).resolves.toBeUndefined() + + expect(reportedContexts().map(({ context }) => context)).toEqual([ + expect.objectContaining({ + method: 'activateLoadedWorkflow.resetTracker' + }) + ]) + }) + }) + }) + describe('afterLoadNewGraph', () => { let workflowStore: ReturnType let existingWorkflow: LoadedComfyWorkflow diff --git a/src/platform/workflow/core/services/workflowService.ts b/src/platform/workflow/core/services/workflowService.ts index d128b9cfadc..d631639c139 100644 --- a/src/platform/workflow/core/services/workflowService.ts +++ b/src/platform/workflow/core/services/workflowService.ts @@ -20,6 +20,7 @@ import { ComfyWorkflow, useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' +import { reportWorkflowStateAnomaly } from '@/platform/workflow/management/stores/comfyWorkflow' import { useTelemetry } from '@/platform/telemetry' import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' // oxlint-disable-next-line comfy/no-restricted-paths @@ -108,6 +109,33 @@ function activeStateFallbackId(workflow: ComfyWorkflow): string | undefined { return workflow.activeState?.id } +/** + * Resets the tracker of a workflow that `openWorkflow` just activated and + * restores its view state. Typed against `ComfyWorkflow` for the same reason as + * `activeStateFallbackId`: `LoadedComfyWorkflow` promises a tracker the + * unchecked cast cannot guarantee. Returns false (after reporting) when the + * workflow has none, so the caller skips the reset instead of throwing. + */ +function resetAndRestoreTracker( + workflow: ComfyWorkflow, + workflowData: ComfyWorkflowJSON, + isActive: boolean +): boolean { + const tracker = workflow.changeTracker + if (!tracker) { + reportWorkflowStateAnomaly( + 'Activated workflow has no change tracker', + 'activateLoadedWorkflow.resetTracker', + workflow, + { isActive } + ) + return false + } + tracker.reset(ensureWorkflowId(workflowData, activeStateFallbackId(workflow))) + tracker.restore() + return true +} + /** @internal Test-only: clears the module-level load queue between tests. */ export function resetWorkflowLoadQueueForTests(): { pendingLoads: number @@ -665,7 +693,19 @@ export const useWorkflowService = () => { const workflowStore = useWorkspaceStore().workflow const activeWorkflow = workflowStore.activeWorkflow if (activeWorkflow) { - activeWorkflow.changeTracker.deactivate() + // Typed as `ComfyWorkflow`: the active workflow's tracker can be absent + // despite `LoadedComfyWorkflow`'s declared type. + const tracker = (activeWorkflow as ComfyWorkflow).changeTracker + if (tracker) { + tracker.deactivate() + } else { + reportWorkflowStateAnomaly( + 'Active workflow has no change tracker before loading a new graph', + 'beforeLoadNewGraph', + activeWorkflow, + { isActive: true } + ) + } persistActiveWorkflowDraft(activeWorkflow) // Cache missing model/media/node state for restore on tab switch. // Always overwrite to reflect the current store state (e.g. after @@ -779,23 +819,30 @@ export const useWorkflowService = () => { if (loadedWorkflow.initialMode === undefined) { // Prefer the file's linearMode over the draft's since the file // is the authoritative saved state. + // `initialState` is null when the tracker never loaded. + const initialState = (loadedWorkflow as ComfyWorkflow).initialState + if (!initialState) { + reportWorkflowStateAnomaly( + 'Activated workflow has no initial state', + 'activateLoadedWorkflow.initialMode', + loadedWorkflow, + { isActive: workflowStore.isActive(loadedWorkflow) } + ) + } loadedWorkflow.initialMode = - linearModeToAppMode( - loadedWorkflow.initialState.extra?.linearMode - ) ?? freshLoadMode + linearModeToAppMode(initialState?.extra?.linearMode) ?? + freshLoadMode trackIfEnteringApp(loadedWorkflow) } if (shareId) { loadedWorkflow.shareId = shareId } loadedWorkflow.legacyId ??= getLegacyWorkflowId(workflowData.id) - loadedWorkflow.changeTracker.reset( - ensureWorkflowId( - workflowData, - activeStateFallbackId(loadedWorkflow) - ) + resetAndRestoreTracker( + loadedWorkflow, + workflowData, + workflowStore.isActive(loadedWorkflow) ) - loadedWorkflow.changeTracker.restore() return } } @@ -826,10 +873,11 @@ export const useWorkflowService = () => { trackIfEnteringApp(loadedWorkflow) } loadedWorkflow.legacyId ??= getLegacyWorkflowId(workflowData.id) - loadedWorkflow.changeTracker.reset( - ensureWorkflowId(workflowData, activeStateFallbackId(loadedWorkflow)) + resetAndRestoreTracker( + loadedWorkflow, + workflowData, + workflowStore.isActive(loadedWorkflow) ) - loadedWorkflow.changeTracker.restore() } /** diff --git a/src/platform/workflow/management/stores/comfyWorkflow.test.ts b/src/platform/workflow/management/stores/comfyWorkflow.test.ts new file mode 100644 index 00000000000..e4275aafd8e --- /dev/null +++ b/src/platform/workflow/management/stores/comfyWorkflow.test.ts @@ -0,0 +1,98 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { + classifyWorkflowContent, + reportWorkflowStateAnomaly, + resetReportedWorkflowStateAnomaliesForTests +} from './comfyWorkflow' + +const mockAssert = vi.hoisted(() => vi.fn()) + +vi.mock(import('@/base/assert'), () => ({ + assert: mockAssert +})) + +const workflow = { + path: 'workflows/private-name.json', + isLoaded: false, + isPersisted: true, + isTemporary: false, + changeTracker: null, + initialState: null, + activeState: null, + originalContent: 'null' +} + +describe('classifyWorkflowContent', () => { + it.for([ + [null, 'unavailable'], + [undefined, 'unavailable'], + ['', 'empty'], + [' \n', 'empty'], + ['null', 'null'], + ['{"nodes":[]}', 'object'], + ['[]', 'other'], + ['42', 'other'], + ['"text"', 'other'], + ['{not json', 'unparseable'] + ] as const)('classifies %j as %s', ([content, kind]) => { + expect(classifyWorkflowContent(content)).toBe(kind) + }) +}) + +describe('reportWorkflowStateAnomaly', () => { + beforeEach(() => { + resetReportedWorkflowStateAnomaliesForTests() + }) + + it('reports a static message with a structural context', () => { + reportWorkflowStateAnomaly('Something is missing', 'site.one', workflow, { + isActive: true + }) + + expect(mockAssert).toHaveBeenCalledExactlyOnceWith( + false, + 'Something is missing', + { + method: 'site.one', + isLoaded: false, + hasChangeTracker: false, + hasInitialState: false, + hasActiveState: false, + isPersisted: true, + isTemporary: false, + originalContentKind: 'null', + isActive: true + } + ) + }) + + it('never sends the workflow path or content', () => { + reportWorkflowStateAnomaly('Something is missing', 'site.one', workflow) + + expect(JSON.stringify(mockAssert.mock.calls)).not.toContain('private-name') + }) + + it('reports only the method when there is no workflow', () => { + reportWorkflowStateAnomaly('Something is missing', 'site.two', undefined, { + hasFirst: false + }) + + expect(mockAssert).toHaveBeenCalledWith(false, 'Something is missing', { + method: 'site.two', + hasFirst: false + }) + }) + + it('reports a given site and workflow once', () => { + reportWorkflowStateAnomaly('Something is missing', 'site.one', workflow) + reportWorkflowStateAnomaly('Something is missing', 'site.one', workflow) + reportWorkflowStateAnomaly('Something is missing', 'site.two', workflow) + reportWorkflowStateAnomaly('Something is missing', 'site.one', { + ...workflow, + path: 'workflows/other.json' + }) + + expect(mockAssert).toHaveBeenCalledTimes(3) + }) +}) diff --git a/src/platform/workflow/management/stores/comfyWorkflow.ts b/src/platform/workflow/management/stores/comfyWorkflow.ts index 3ebb6bf5f54..27181433ea1 100644 --- a/src/platform/workflow/management/stores/comfyWorkflow.ts +++ b/src/platform/workflow/management/stores/comfyWorkflow.ts @@ -104,12 +104,170 @@ function workflowRuntime(): WorkflowRuntime { return registeredRuntime } +/** + * Coarse shape of a workflow file's stored text. Never includes the content. + * - `unavailable`: the text was never loaded (`null` / `undefined`) + * - `empty`: only whitespace + * - `null`: parses to JSON `null` + * - `object`: parses to a plain JSON object (the healthy case) + * - `other`: parses to an array, string, number or boolean + * - `unparseable`: not valid JSON + */ +export type OriginalContentKind = + | 'unavailable' + | 'empty' + | 'null' + | 'object' + | 'other' + | 'unparseable' + +export function classifyWorkflowContent( + content: string | null | undefined +): OriginalContentKind { + if (content == null) return 'unavailable' + if (content.trim().length === 0) return 'empty' + let parsed: unknown + try { + parsed = JSON.parse(content) + } catch { + return 'unparseable' + } + if (parsed === null) return 'null' + if (typeof parsed === 'object' && !Array.isArray(parsed)) return 'object' + return 'other' +} + +/** The slice of a `ComfyWorkflow` that is safe to describe in telemetry. */ +interface WorkflowStateProbe { + path: string + isLoaded: boolean + isPersisted: boolean + isTemporary: boolean + changeTracker: unknown + initialState: unknown + activeState: unknown + originalContent: string | null +} + +const reportedAnomalies = new Set() + +/** @internal Test-only: forgets which anomalies were already reported. */ +export function resetReportedWorkflowStateAnomaliesForTests(): void { + reportedAnomalies.clear() +} + +/** + * Reports a workflow whose in-memory state was not what the caller assumed + * (for example an active workflow with no change tracker). Each call site + * recovers locally; this only makes the occurrence visible so the real trigger + * can be found. + * + * `message` must be a static description: reporters forward it to telemetry + * and group on it. Diagnostic values go in the context, which carries only + * booleans and enum-like strings and never workflow names, paths or content. + * Reports are deduplicated per call site and workflow. + * + * @param method Stable name of the call site, e.g. `ComfyWorkflow.save`. + * @param workflow The workflow involved, when there is one. + * @param extra Further booleans/enums, e.g. `{ isActive: true }`. + */ +export function reportWorkflowStateAnomaly( + message: string, + method: string, + workflow?: WorkflowStateProbe, + extra: Record = {} +): void { + const key = `${method}:${workflow?.path ?? ''}` + if (reportedAnomalies.has(key)) return + reportedAnomalies.add(key) + + assert(false, message, { + method, + ...(workflow && { + isLoaded: workflow.isLoaded, + hasChangeTracker: workflow.changeTracker != null, + hasInitialState: workflow.initialState != null, + hasActiveState: workflow.activeState != null, + isPersisted: workflow.isPersisted, + isTemporary: workflow.isTemporary, + originalContentKind: classifyWorkflowContent(workflow.originalContent) + }), + ...extra + }) +} + +/** + * The saved draft to overlay on a freshly loaded workflow, if there is a usable + * one. A stale or unparseable draft is removed. + */ +function readUsableDraft( + runtime: WorkflowRuntime, + workflow: ComfyWorkflow +): { state: ComfyWorkflowJSON; content: string } | null { + const draft = runtime.getDraft(workflow.path) + if (!draft) return null + if (draft.updatedAt < workflow.lastModified) { + runtime.removeDraft(workflow.path) + return null + } + try { + return { state: JSON.parse(draft.data), content: draft.data } + } catch (err) { + console.warn('Failed to parse workflow draft, clearing it', err) + runtime.removeDraft(workflow.path) + return null + } +} + +/** + * The workflow object parsed from the stored text. Anything else (nothing + * stored, empty text, JSON `null`, a number, an array) cannot back a change + * tracker: a tracker around `null` is what later reads as a missing state. + */ +function parseStoredWorkflow(workflow: ComfyWorkflow): ComfyWorkflowJSON { + const content = workflow.originalContent + const parsed: unknown = content?.trim() ? JSON.parse(content) : undefined + if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) { + return parsed as ComfyWorkflowJSON + } + // `undefined` means there was nothing to parse; anything else parsed to + // something that is not a workflow. + if (parsed !== undefined) { + reportWorkflowStateAnomaly( + 'Workflow content is not a JSON object', + 'ComfyWorkflow.load', + workflow + ) + } + throw new Error( + `Workflow content is ${parsed === undefined ? 'missing or empty' : 'not a JSON object'} for '${workflow.path}'` + ) +} + /** i18n keys for the dialog shown when a workflow is saved without a name. */ export interface SaveNamePrompt { title: string message: string } +/** + * The serialized state to persist. An unloaded workflow has no state, and + * `JSON.stringify(null)` would overwrite the stored file with `null`. + */ +function requireActiveStateForSave( + workflow: ComfyWorkflow, + method: string +): ComfyWorkflowJSON { + const state = workflow.activeState + if (state) return state + reportWorkflowStateAnomaly( + 'Workflow saved without an active state', + method, + workflow + ) + throw new Error(`Cannot save workflow '${workflow.path}' before it is loaded`) +} + export class ComfyWorkflow extends UserFile { static readonly basePath: string = 'workflows/' readonly tintCanvasBg?: string @@ -193,48 +351,20 @@ export class ComfyWorkflow extends UserFile { } const runtime = workflowRuntime() - let draft = + const draft = !force && runtime.isDraftPersistenceEnabled() - ? runtime.getDraft(this.path) + ? readUsableDraft(runtime, this) : null - let draftState: ComfyWorkflowJSON | null = null - let draftContent: string | null = null - - if (draft) { - if (draft.updatedAt < this.lastModified) { - runtime.removeDraft(this.path) - draft = null - } - } - - if (draft) { - try { - draftState = JSON.parse(draft.data) - draftContent = draft.data - } catch (err) { - console.warn('Failed to parse workflow draft, clearing it', err) - runtime.removeDraft(this.path) - } - } await super.load({ force }) - if (this.originalContent == null) { - throw new Error( - `[ASSERT] Workflow content should be loaded for '${this.path}'` - ) - } - if (this.originalContent.trim().length === 0) { - throw new Error(`Workflow content is empty for '${this.path}'`) - } - - const initialState = JSON.parse(this.originalContent) + const initialState = parseStoredWorkflow(this) this.changeTracker = markRaw( runtime.createChangeTracker(this, initialState) ) - if (draftState && draftContent) { - this.changeTracker.activeState = draftState - this.content = draftContent + if (draft?.state && draft.content) { + this.changeTracker.activeState = draft.state + this.content = draft.content this._isModified = true // Saved-workflow draft overlay path; direct persisted-draft restores // are touched in workflowDraftStoreV2.loadDraft(). @@ -252,7 +382,9 @@ export class ComfyWorkflow extends UserFile { } override async save() { - this.content = JSON.stringify(this.activeState) + this.content = JSON.stringify( + requireActiveStateForSave(this, 'ComfyWorkflow.save') + ) // Force save to ensure the content is updated in remote storage incase // the isModified state is screwed by changeTracker. const ret = await super.save({ force: true }) @@ -268,7 +400,9 @@ export class ComfyWorkflow extends UserFile { * @returns this */ override async saveAs(path: string) { - this.content = JSON.stringify(this.activeState) + this.content = JSON.stringify( + requireActiveStateForSave(this, 'ComfyWorkflow.saveAs') + ) const result = await super.saveAs(path) workflowRuntime().removeDraft(path) return result diff --git a/src/platform/workflow/management/stores/workflowStore.test.ts b/src/platform/workflow/management/stores/workflowStore.test.ts index cf067c5eb09..2c0b82421d0 100644 --- a/src/platform/workflow/management/stores/workflowStore.test.ts +++ b/src/platform/workflow/management/stores/workflowStore.test.ts @@ -6,6 +6,7 @@ import type { LGraph, Subgraph } from '@/lib/litegraph/src/litegraph' import { useSettingStore } from '@/platform/settings/settingStore' import type { LoadedComfyWorkflow } from '@/platform/workflow/management/stores/workflowStore' import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' +import { resetReportedWorkflowStateAnomaliesForTests } from '@/platform/workflow/management/stores/comfyWorkflow' import { ComfyWorkflow, useWorkflowBookmarkStore, @@ -51,6 +52,12 @@ vi.mock(import('@/utils/typeGuardUtil'), () => ({ vi.mock(import('@/utils/syncUtil'), { spy: true }) +const mockAssert = vi.hoisted(() => vi.fn()) + +vi.mock(import('@/base/assert'), () => ({ + assert: mockAssert +})) + describe('useWorkflowStore', () => { let store: ReturnType let bookmarkStore: ReturnType @@ -1400,6 +1407,23 @@ describe('useWorkflowStore', () => { expect(draftStore.getDraft('workflows/a.json')).toBeNull() }) + it('clears the modified mark of a closed persisted workflow so a bulk save skips it', async () => { + await syncRemoteWorkflows(['a.json']) + const workflow = store.getWorkflowByPath('workflows/a.json')! + vi.mocked(api.getUserData).mockResolvedValue({ + status: 200, + text: () => Promise.resolve(defaultGraphJSON) + } as Response) + await workflow.load() + workflow.isModified = true + + await store.closeWorkflow(workflow) + + expect(workflow.isLoaded).toBe(false) + expect(workflow.isModified).toBe(false) + expect(store.modifiedWorkflows).not.toContain(workflow) + }) + it('should remove draft for temporary workflows on close', async () => { const workflow = store.createTemporary('temp.json') const draftStore = saveV2Draft(workflow.path, { @@ -1413,4 +1437,167 @@ describe('useWorkflowStore', () => { expect(draftStore.getDraft(workflow.path)).toBeNull() }) }) + + describe('null-state hardening', () => { + const storedAs = (content: string) => + vi.mocked(api.getUserData).mockResolvedValue({ + status: 200, + text: () => Promise.resolve(content) + } as Response) + + const failedAssertions = () => + mockAssert.mock.calls.filter(([condition]) => !condition) + + const expectReported = (message: string, context: object) => + expect(mockAssert).toHaveBeenCalledWith( + false, + message, + expect.objectContaining(context) + ) + + beforeEach(() => { + resetReportedWorkflowStateAnomaliesForTests() + }) + + describe('load', () => { + it.for(['null', '42', '"text"', '[]'])( + 'rejects stored content %s', + async (content) => { + await syncRemoteWorkflows(['a.json']) + const workflow = store.getWorkflowByPath('workflows/a.json')! + storedAs(content) + + await expect(workflow.load()).rejects.toThrow('not a JSON object') + + expect(workflow.isLoaded).toBe(false) + expect(workflow.changeTracker).toBeNull() + } + ) + + // The production crashes (CLOUD-FRONTEND-PROD-2Z2/-2Z3) had a tracker + // with a null initial state and a draft as its active state: a stored + // `null` overlaid by a newer draft. + it('rejects stored null even when a newer draft exists, and keeps the draft', async () => { + enableWorkflowPersistence() + await syncRemoteWorkflowsWithMeta([ + { path: 'a.json', modified: 100, size: 1 } + ]) + const workflow = store.getWorkflowByPath('workflows/a.json')! + const draftStore = saveV2Draft(workflow.path, { + data: defaultGraphJSON, + name: 'a.json' + }) + storedAs('null') + + await expect(workflow.load()).rejects.toThrow('not a JSON object') + + expect(workflow.changeTracker).toBeNull() + expect(workflow.initialState).toBeNull() + expect(workflow.activeState).toBeNull() + expect(draftStore.getDraft(workflow.path)?.data).toBe(defaultGraphJSON) + }) + + it('reports the kind of content without including the path', async () => { + await syncRemoteWorkflows(['secret-name.json']) + const workflow = store.getWorkflowByPath('workflows/secret-name.json')! + storedAs('null') + + await expect(workflow.load()).rejects.toThrow() + + expectReported('Workflow content is not a JSON object', { + method: 'ComfyWorkflow.load', + isLoaded: false, + hasChangeTracker: false, + hasInitialState: false, + hasActiveState: false, + isPersisted: true, + isTemporary: false, + originalContentKind: 'null' + }) + expect(JSON.stringify(mockAssert.mock.calls)).not.toContain( + 'secret-name' + ) + }) + + it('does not report content that is a JSON object', async () => { + await syncRemoteWorkflows(['a.json']) + const workflow = store.getWorkflowByPath('workflows/a.json')! + storedAs(defaultGraphJSON) + + await workflow.load() + + expect(workflow.isLoaded).toBe(true) + expect(failedAssertions()).toHaveLength(0) + }) + }) + + describe.for(['save', 'saveAs'] as const)('%s', (method) => { + it('refuses to persist an unloaded workflow and reports it', async () => { + await syncRemoteWorkflows(['a.json']) + const workflow = store.getWorkflowByPath('workflows/a.json')! + const contentBefore = workflow.content + + const attempt = + method === 'save' + ? workflow.save() + : workflow.saveAs('workflows/copy.json') + + await expect(attempt).rejects.toThrow('before it is loaded') + expect(api.storeUserData).not.toHaveBeenCalled() + expect(workflow.content).toBe(contentBefore) + expectReported('Workflow saved without an active state', { + method: `ComfyWorkflow.${method}`, + isLoaded: false, + hasChangeTracker: false, + hasActiveState: false + }) + }) + }) + + describe('openWorkflow', () => { + it('loads a workflow that is active but was never loaded', async () => { + await syncRemoteWorkflows(['a.json']) + const workflow = store.getWorkflowByPath('workflows/a.json')! + store.activeWorkflow = workflow as LoadedComfyWorkflow + storedAs(defaultGraphJSON) + + const opened = await store.openWorkflow(workflow) + + expect(opened.isLoaded).toBe(true) + expect(opened.initialState).toEqual(defaultGraph) + expectReported( + 'openWorkflow called on an active workflow that is not loaded', + { + method: 'workflowStore.openWorkflow', + isActive: true, + isLoaded: false, + hasInitialState: false + } + ) + }) + + it('reports each affected workflow only once', async () => { + await syncRemoteWorkflows(['a.json']) + const workflow = store.getWorkflowByPath('workflows/a.json')! + storedAs(defaultGraphJSON) + + for (let attempt = 0; attempt < 2; attempt++) { + workflow.unload() + store.activeWorkflow = workflow as LoadedComfyWorkflow + await store.openWorkflow(workflow) + } + + expect(failedAssertions()).toHaveLength(1) + }) + + it('does not report an active workflow that is loaded', async () => { + const workflow = await store.createTemporary('test.json').load() + store.activeWorkflow = workflow + + await store.openWorkflow(workflow) + + expect(failedAssertions()).toHaveLength(0) + }) + }) + }) }) diff --git a/src/platform/workflow/management/stores/workflowStore.ts b/src/platform/workflow/management/stores/workflowStore.ts index 5765942c4ec..c2a38a6661f 100644 --- a/src/platform/workflow/management/stores/workflowStore.ts +++ b/src/platform/workflow/management/stores/workflowStore.ts @@ -40,7 +40,11 @@ import type { NodeId } from '@/types/nodeId' import { generateUUID, getPathDetails } from '@/utils/formatUtil' import { syncEntities } from '@/utils/syncUtil' import { isSubgraph } from '@/utils/typeGuardUtil' -import { ComfyWorkflow, registerWorkflowRuntime } from './comfyWorkflow' +import { + ComfyWorkflow, + registerWorkflowRuntime, + reportWorkflowStateAnomaly +} from './comfyWorkflow' import type { LoadedComfyWorkflow } from './comfyWorkflow' import type { WorkflowStore } from './workflowStoreTypes' export { ComfyWorkflow, type LoadedComfyWorkflow } @@ -183,7 +187,18 @@ export const useWorkflowStore = defineStore('workflow', () => { const openWorkflow = async ( workflow: ComfyWorkflow ): Promise => { - if (isActive(workflow)) return workflow as LoadedComfyWorkflow + if (isActive(workflow)) { + if (workflow.isLoaded) return workflow as LoadedComfyWorkflow + // `isActive` matches by path, so a workflow can be "active" while this + // instance never loaded (or has since unloaded) its change tracker. + // Returning it as loaded would hand callers a null `initialState`. + reportWorkflowStateAnomaly( + 'openWorkflow called on an active workflow that is not loaded', + 'workflowStore.openWorkflow', + workflow, + { isActive: true } + ) + } if (!openWorkflowPaths.value.includes(workflow.path)) { openWorkflowPaths.value.push(workflow.path) @@ -332,6 +347,11 @@ export const useWorkflowStore = defineStore('workflow', () => { delete workflowLookup.value[workflow.path] } else { workflow.unload() + // Closing discards the in-memory edits and their draft. A modified mark + // left behind keeps the workflow in `modifiedWorkflows`, where a later + // bulk save (sign-out) would write the unloaded state, `null`, over + // the stored file. + workflow.isModified = false } } diff --git a/src/scripts/changeTracker.test.ts b/src/scripts/changeTracker.test.ts index bee96772dc3..7d4fce36d51 100644 --- a/src/scripts/changeTracker.test.ts +++ b/src/scripts/changeTracker.test.ts @@ -1,4 +1,4 @@ -import { fromPartial } from '@total-typescript/shoehorn' +import { fromAny, fromPartial } from '@total-typescript/shoehorn' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import { useSubgraphNavigationStore } from '@/stores/subgraphNavigationStore' import { useNodeOutputStore } from '@/stores/nodeOutputStore' @@ -25,6 +25,8 @@ import { import { LGraphNode } from '@/lib/litegraph/src/litegraph' import type { Subgraph } from '@/lib/litegraph/src/LGraph' import type { ExportedSubgraph } from '@/lib/litegraph/src/types/serialisation' +import { resetReportedWorkflowStateAnomaliesForTests } from '@/platform/workflow/management/stores/comfyWorkflow' +import type { ComfyWorkflow } from '@/platform/workflow/management/stores/workflowStore' import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' import { validateComfyWorkflow } from '@/platform/workflow/validation/schemas/workflowSchema' import { useQueueSettingsStore } from '@/stores/queueSettingsStore' @@ -1921,6 +1923,95 @@ describe('ChangeTracker', () => { }) }) + describe('graphEqual', () => { + beforeEach(() => { + resetReportedWorkflowStateAnomaliesForTests() + }) + + it.for([ + ['first', null, createState(), { hasFirst: false, hasSecond: true }], + ['second', createState(), undefined, { hasFirst: true, hasSecond: false }] + ] as const)( + 'reports and returns false when the %s state is missing', + ([, a, b, context]) => { + expect(ChangeTracker.graphEqual(a, b)).toBe(false) + + expect(mockAssert).toHaveBeenCalledWith( + false, + 'ChangeTracker.graphEqual() called with a missing state', + { method: 'ChangeTracker.graphEqual', ...context } + ) + } + ) + + it('does not report when both states are present', () => { + const state = createState(1) + + expect(ChangeTracker.graphEqual(state, structuredClone(state))).toBe(true) + expect(ChangeTracker.graphEqual(state, createState(2))).toBe(false) + + expect(mockAssert).not.toHaveBeenCalled() + }) + }) + + describe('updateModified', () => { + beforeEach(() => { + resetReportedWorkflowStateAnomaliesForTests() + }) + + function storeWorkflowFor(tracker: ChangeTracker) { + const workflow = fromPartial({ + path: tracker.workflow.path, + isLoaded: false, + isPersisted: true, + isTemporary: false, + changeTracker: null, + initialState: null, + activeState: null, + originalContent: null, + isModified: false + }) + vi.mocked(useWorkflowStore().getWorkflowByPath).mockReturnValue(workflow) + return workflow + } + + it('marks the workflow modified when its state differs from the initial state', () => { + const tracker = createTracker(createState(1)) + const workflow = storeWorkflowFor(tracker) + tracker.activeState = createState(2) + + tracker.updateModified() + + expect(workflow.isModified).toBe(true) + expect(mockAssert).not.toHaveBeenCalled() + }) + + it('leaves isModified alone and reports when the tracker has no initial state', () => { + const tracker = createTracker(createState(1)) + const workflow = storeWorkflowFor(tracker) + tracker.initialState = fromAny(null) + + expect(() => tracker.updateModified()).not.toThrow() + + expect(workflow.isModified).toBe(false) + expect(mockAssert).toHaveBeenCalledWith( + false, + 'ChangeTracker has no initial state', + { + method: 'ChangeTracker.updateModified', + isLoaded: false, + hasChangeTracker: false, + hasInitialState: false, + hasActiveState: false, + isPersisted: true, + isTemporary: false, + originalContentKind: 'unavailable', + isActive: true + } + ) + }) + }) + describe('deactivate', () => { it('captures canvas state then stores viewport/outputs', () => { const tracker = createTracker(createState(1)) diff --git a/src/scripts/changeTracker.ts b/src/scripts/changeTracker.ts index 515bd3beca0..460abee5681 100644 --- a/src/scripts/changeTracker.ts +++ b/src/scripts/changeTracker.ts @@ -9,6 +9,7 @@ import type { ComfyWorkflow, WorkflowChangeTracker } from '@/platform/workflow/management/stores/comfyWorkflow' +import { reportWorkflowStateAnomaly } from '@/platform/workflow/management/stores/comfyWorkflow' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' import { @@ -461,10 +462,21 @@ export class ChangeTracker implements WorkflowChangeTracker { // `this.workflow` is not reactive. const workflow = useWorkflowStore().getWorkflowByPath(this.workflow.path) if (workflow) { - workflow.isModified = !ChangeTracker.graphEqual( - this.initialState, - this.activeState - ) + // Declared non-null, but a tracker can still lack it at runtime. + const initialState = this.initialState as ComfyWorkflowJSON | null + if (initialState) { + workflow.isModified = !ChangeTracker.graphEqual( + initialState, + this.activeState + ) + } else { + reportWorkflowStateAnomaly( + 'ChangeTracker has no initial state', + 'ChangeTracker.updateModified', + workflow, + { isActive: isActiveTracker(this) } + ) + } } const autoQueueGraphChanged = @@ -813,7 +825,19 @@ export class ChangeTracker implements WorkflowChangeTracker { return false } - static graphEqual(a: ComfyWorkflowJSON, b: ComfyWorkflowJSON) { + static graphEqual( + a: ComfyWorkflowJSON | null | undefined, + b: ComfyWorkflowJSON | null | undefined + ) { + if (!a || !b) { + reportWorkflowStateAnomaly( + 'ChangeTracker.graphEqual() called with a missing state', + 'ChangeTracker.graphEqual', + undefined, + { hasFirst: !!a, hasSecond: !!b } + ) + return false + } if (a === b) return true // Compare nodes ignoring array position and execution order