diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.test.ts index 9e3b38c5c1d..2c62b0461d3 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.test.ts @@ -291,4 +291,29 @@ describe('Agent workflow resolution', () => { { id: 'updated', name: 'Current' } ]) }) + + it('rejects a stale binding that points a cloud id at a differently named saved tab', async () => { + const portrait = workflow('workflows/portrait.json', 'Portrait') + const { resolver, bindings, workflows } = setup( + [portrait], + [ + { id: 'cloud-zimage', name: 'image_z_image_turbo' }, + { id: 'cloud-portrait', name: 'Portrait' } + ] + ) + bindings.bind('cloud-zimage', 'workflows/portrait.json') + await resolver.refreshCloudWorkflowIds() + expect(resolver.cloudIdFor(portrait)).toBe('cloud-portrait') + expect(resolver.boundOrOpenWorkflowFor('cloud-zimage')).toBeNull() + expect(resolver.storedWorkflowFor('cloud-zimage')).toBeNull() + expect(resolver.openWorkflowFor('cloud-zimage')).toBeNull() + expect(resolver.boundOrOpenWorkflowFor('cloud-portrait')).toBe( + workflows.openWorkflows[0] + ) + expect(bindings.tabPathFor('cloud-zimage')).toBeUndefined() + expect(resolver.availableWorkflowReferences.value).toEqual([ + { id: 'cloud-portrait', name: 'Portrait' }, + { id: 'cloud-zimage', name: 'image_z_image_turbo' } + ]) + }) }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.ts index c19723c08b0..fe499222eba 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.ts @@ -21,7 +21,7 @@ type WorkflowResolverDeps = { > bindings: Pick< ReturnType, - 'workflowIdFor' | 'tabPathFor' | 'matchesWorkflow' + 'workflowIdFor' | 'tabPathFor' | 'matchesWorkflow' | 'unbind' > listCloudWorkflows: AgentRestClient['listCloudWorkflows'] } @@ -92,13 +92,38 @@ export function useAgentWorkflowResolver({ : undefined } + function indexedNameFor(workflowId: string): string | undefined { + for (const [name, id] of cloudIdsByName.value) { + if (id === workflowId) return name + } + return undefined + } + + /** + * A persisted binding is stale when the cloud index says `workflowId` is + * a different saved workflow than the tab it points at, and the tab's own + * name resolves to another cloud id. Applying mutations through such a + * binding would write one workflow's edits into another workflow's graph. + */ + function bindingIsStale(workflowId: string, bound: ComfyWorkflow): boolean { + if (bound.isTemporary) return false + const indexedName = indexedNameFor(workflowId) + const boundName = cloudWorkflowName(bound) + if (indexedName === undefined || indexedName === boundName) return false + const boundId = cloudIdsByName.value.get(boundName) + return boundId !== undefined && boundId !== workflowId + } + function resolveWorkflow( workflowId: string, nameCandidates: ComfyWorkflow[] ): ComfyWorkflow | null { const path = bindings.tabPathFor(workflowId) const bound = path === undefined ? null : workflows.getWorkflowByPath(path) - if (bound && bindings.matchesWorkflow(workflowId, bound)) return bound + if (bound && bindings.matchesWorkflow(workflowId, bound)) { + if (!bindingIsStale(workflowId, bound)) return bound + bindings.unbind(bound.path) + } for (const [name, id] of cloudIdsByName.value) { if (id !== workflowId) continue const matches = savedMatches(name, nameCandidates)