Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ older stashed link rather than clearing the stash: an older link cannot keep
the empty one from being rejected as invalid, and while a guard holds the empty
link, an earlier lookup still sees a newer link in flight and its cleanup
cannot cancel that navigation. While the stash holds a link the loader never
started and the URL does not show, the sweep makes no replace either, so that
link's navigation is not cancelled.
started and the URL does not show, no URL action after the workflow-id link
runs, the sweep included: any replace would cancel that link's navigation. The
actions before it still run, since at boot the stash can hold a link the
loader has not reached yet. A load the link no longer owns, such as one still
waiting on an extension hook when the next link arrives, stops before the graph
is configured and again before the workflow is activated.

The reply and the 409 body are validated with the generated
`@comfyorg/ingest-types` schemas. The spec types the 409 as `ErrorResponse` and
Expand All @@ -64,7 +68,8 @@ failing error's class name when it is a built-in JavaScript error class, or
`other` for any other name or a name that cannot be read. They never carry the
caught error, its message or a name it chose: a failed file read names the
file, a parser error quotes the reply, and custom node code that runs while the
workflow loads can throw an error under any name.
workflow loads can throw an error under any name. The workflow service leaves
a failed link open to the loader's report and does not report it itself.

## Alternatives considered

Expand Down
5 changes: 3 additions & 2 deletions src/composables/useUrlActionLoaders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ describe('useUrlActionLoaders', () => {
).toHaveBeenCalledOnce()
})

it('lets a newer workflow-id link held in a navigation guard land instead of cancelling it', async () => {
it('runs only the loaders before the workflow-id link while a newer one waits in a navigation guard, so it lands', async () => {
await router.replace({ query: { workflowId: 'workflow-a', topup: '1' } })
workflowLinkUrlLoader.openWorkflowLinkFromUrl.mockResolvedValue('opened')
workflowLinkUrlLoader.isLinkSettled.mockImplementation(
Expand All @@ -536,7 +536,8 @@ describe('useUrlActionLoaders', () => {
releaseGuard()
await newerLink

expect(mocks.loadTopUp).toHaveBeenCalledOnce()
expect(mocks.loadWorkspace).toHaveBeenCalledOnce()
expect(mocks.loadTopUp).not.toHaveBeenCalled()
expect(router.currentRoute.value.query).toEqual({
workflowId: 'workflow-b'
})
Expand Down
30 changes: 20 additions & 10 deletions src/composables/useUrlActionLoaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ function sweepStep(
return {
failureMessage: 'Failed to clear handled deep-link params from the URL:',
run: async () => {
if (loader?.hasLinkInFlight()) return
clearPreservedQuery(PRESERVED_QUERY_NAMESPACES.WORKFLOWS)
const query = { ...router.currentRoute.value.query }
const params = [
Expand All @@ -147,6 +146,16 @@ function sweepStep(
}
}

function unlessLinkInFlight(
loader: UrlActionLoaders['workflowLinkUrlLoader'],
steps: UrlActionStep[]
): UrlActionStep[] {
return steps.map(({ failureMessage, run }) => ({
failureMessage,
run: () => (loader?.hasLinkInFlight() ? undefined : run())
}))
}

/**
* Orders the deep-link loaders: the deployment link first, since with
* `?deployment` it owns `?workspace` too (it switches, then picks after the
Expand All @@ -169,58 +178,59 @@ function buildUrlActionSteps(
})
}
steps.push(...workflowLinkSteps(loaders.workflowLinkUrlLoader))
const laterSteps: UrlActionStep[] = []
if (loaders.inviteUrlLoader) {
const loader = loaders.inviteUrlLoader
steps.push({
laterSteps.push({
failureMessage: 'Failed to load invite from URL:',
run: () => loader.loadInviteFromUrl()
})
}
if (loaders.createWorkspaceUrlLoader) {
const loader = loaders.createWorkspaceUrlLoader
steps.push({
laterSteps.push({
failureMessage: 'Failed to load create workspace from URL:',
run: () => loader.loadCreateWorkspaceFromUrl()
})
}
if (loaders.pricingTableUrlLoader) {
const loader = loaders.pricingTableUrlLoader
steps.push({
laterSteps.push({
failureMessage: 'Failed to load pricing table from URL:',
run: () => loader.loadPricingTableFromUrl()
})
}
// Not gated on the team-workspaces flag: it also drives personal/legacy users.
if (loaders.topUpUrlLoader) {
const loader = loaders.topUpUrlLoader
steps.push({
laterSteps.push({
failureMessage: 'Failed to load top-up dialog from URL:',
run: () => loader.loadTopUpFromUrl()
})
}
if (loaders.settingsUrlLoader) {
const loader = loaders.settingsUrlLoader
steps.push({
laterSteps.push({
failureMessage: 'Failed to load settings panel from URL:',
run: () => loader.loadSettingsFromUrl()
})
}
if (loaders.assetsUrlLoader) {
const loader = loaders.assetsUrlLoader
steps.push({
laterSteps.push({
failureMessage: 'Failed to open assets panel from URL:',
run: () => loader.loadAssetsFromUrl()
})
}
if (loaders.localHandoffUrlLoader) {
const loader = loaders.localHandoffUrlLoader
steps.push({
laterSteps.push({
failureMessage: 'Failed to open the workflow handed over from local:',
run: () => loader.loadLocalHandoffFromUrl()
})
}

steps.push(sweepStep(loaders.workflowLinkUrlLoader, router))
laterSteps.push(sweepStep(loaders.workflowLinkUrlLoader, router))
steps.push(...unlessLinkInFlight(loaders.workflowLinkUrlLoader, laterSteps))

return steps
}
Expand Down
38 changes: 38 additions & 0 deletions src/platform/workflow/core/services/workflowService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,44 @@ describe('useWorkflowService', () => {
expect(loading.isLoaded).toBe(false)
})

it('drops a fetched workflow and keeps the current one when the graph load sees the open superseded', async () => {
const workflows = useWorkflowStore()
const current = createModeTestWorkflow({ path: 'workflows/current.json' })
const loading = createModeTestWorkflow({
path: 'workflows/closed.json',
loaded: false
})
workflows.activeWorkflow = current
vi.spyOn(loading, 'load').mockImplementation(async () => {
loading.originalContent = '{}'
loading.content = '{}'
loading.changeTracker = createMockChangeTracker()
return loading
})
let currentRequest = true
vi.mocked(app.loadGraphData).mockImplementationOnce(
async (_data, _clean, _restoreView, _workflow, options) => {
currentRequest = false
return options?.isCurrent?.() !== false
}
)

const opened = await useWorkflowService().openWorkflow(loading, {
isCurrent: () => currentRequest
})

expect(opened).toBe(false)
expect(loading.isLoaded).toBe(false)
expect(workflows.activeWorkflow.path).toBe(current.path)
expect(app.loadGraphData).toHaveBeenLastCalledWith(
expect.anything(),
true,
true,
current,
expect.anything()
)
})

it('leaves a saved workflow clean after a superseded load applied its draft', async () => {
const workflows = useWorkflowStore()
const current = createModeTestWorkflow({ path: 'workflows/current.json' })
Expand Down
97 changes: 64 additions & 33 deletions src/platform/workflow/core/services/workflowService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,20 @@ export function resetWorkflowLoadQueueForTests(): {

function queueWorkflowLoad<T>(
load: () => Promise<T>,
workflowPath?: string
{
workflowPath,
reportFailure = true
}: { workflowPath?: string; reportFailure?: boolean } = {}
): Promise<T> {
pendingWorkflowLoads++
const result = workflowLoadTail.then(load)
const settledResult = result
.catch((error) => {
// Keep fire-and-forget load failures observable.
reportError(error, {
surface: 'graph',
errorType: 'workflow_load_failure'
})
if (reportFailure)
reportError(error, {
surface: 'graph',
errorType: 'workflow_load_failure'
})
return undefined
})
.finally(() => {
Expand All @@ -233,6 +236,36 @@ function queueWorkflowLoad<T>(
return result
}

function loadWorkflowGraph(
workflow: ComfyWorkflow,
navigationIntentId: number,
loadFromRemote: boolean,
options: {
force?: boolean
isCurrent?: () => boolean
keepSavedBaseline?: boolean
}
) {
workflow.changeTracker?.prepareForSave()
const load = () =>
useApp().loadGraphData(
toRaw(workflow.activeState) as ComfyWorkflowJSON,
true,
true,
workflow,
{
checkForRerouteMigration: false,
deferWarnings: true,
skipAssetScans: !loadFromRemote && !options.force,
workflowNavigationId: navigationIntentId,
isCurrent: options.isCurrent
}
)
return options.keepSavedBaseline && workflow.isModified
? keepingSavedBaseline(workflow, load)
: load()
}

const unreadableWorkflowToastIds = new Map<string, ToastId>()
const unreadableWorkflows = new WeakSet<ComfyWorkflow>()

Expand Down Expand Up @@ -551,6 +584,7 @@ export const useWorkflowService = () => {
isCurrent?: () => boolean
initiator?: 'user'
keepSavedBaseline?: boolean
reportLoadFailure?: boolean
} = {}
): Promise<boolean> => {
if (options.isCurrent?.() === false) return Promise.resolve(false)
Expand All @@ -569,6 +603,10 @@ export const useWorkflowService = () => {
const navigationIntentId =
options.navigationIntentId ??
useSubgraphNavigationStore().beginWorkflowNavigation()
const queueOptions = {
workflowPath: workflow.path,
reportFailure: options.reportLoadFailure !== false
}
return queueWorkflowLoad(async () => {
let wasUnreadable = false
try {
Expand All @@ -588,27 +626,17 @@ export const useWorkflowService = () => {
return false
}

workflow.changeTracker?.prepareForSave()
const load = () =>
useApp().loadGraphData(
toRaw(workflow.activeState) as ComfyWorkflowJSON,
true,
true,
workflow,
{
checkForRerouteMigration: false,
deferWarnings: true,
skipAssetScans: !loadFromRemote && !options.force,
workflowNavigationId: navigationIntentId
}
)
const loaded =
options.keepSavedBaseline && workflow.isModified
? await keepingSavedBaseline(workflow, load)
: await load()
const loaded = await loadWorkflowGraph(
workflow,
navigationIntentId,
loadFromRemote,
options
)
if (loaded === false) {
useSubgraphNavigationStore().endWorkflowNavigation(navigationIntentId)
await restoreRetainedWorkflow(workflow)
if (loadFromRemote && options.isCurrent?.() === false)
discardSupersededLoad(workflow, wasModified)
return false
}
dismissUnreadableWorkflowToast(workflow)
Expand All @@ -629,7 +657,7 @@ export const useWorkflowService = () => {
if (wasUnreadable && !isUnreadableWorkflow(workflow))
dismissUnreadableWorkflowToast(workflow)
}
}, workflow.path)
}, queueOptions)
}

const markUnreadable = (workflow: ComfyWorkflow) => {
Expand Down Expand Up @@ -802,14 +830,17 @@ export const useWorkflowService = () => {
const loadWorkflowInBackground = (
workflow: ComfyWorkflow
): Promise<ComfyWorkflow> =>
queueWorkflowLoad(async () => {
const loaded: ComfyWorkflow = await workflow.load()
if (loaded.initialMode === undefined)
loaded.initialMode =
linearModeToAppMode(loaded.initialState?.extra?.linearMode) ??
linearModeToAppMode(loaded.activeState?.extra?.linearMode)
return loaded
}, workflow.path)
queueWorkflowLoad(
async () => {
const loaded: ComfyWorkflow = await workflow.load()
if (loaded.initialMode === undefined)
loaded.initialMode =
linearModeToAppMode(loaded.initialState?.extra?.linearMode) ??
linearModeToAppMode(loaded.activeState?.extra?.linearMode)
return loaded
},
{ workflowPath: workflow.path }
)

const renameWorkflow = async (workflow: ComfyWorkflow, newPath: string) => {
const oldPath = workflow.path
Expand Down
Loading
Loading