From 096bfefeb5e1120f5db84158ec27c2cdfd0bd677 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 11 Sep 2026 20:59:14 -0700 Subject: [PATCH] fix(agent): drop stale workflow tab bindings before resolving graph scope A persisted Comfy.Agent.WorkflowTabBindings entry can map a cloud workflow id to a tab that now holds a differently named saved workflow. The resolver trusted that binding, so agent mutations for one cloud workflow were applied to another workflow's canvas and saved there (phantom LoadImage / ByteDance2ReferenceNodeV2 / SaveVideo nodes in text-to-image workflows). Detect the stale binding against the cloud name index, unbind it, and fall through to name-based resolution. --- .../agent/useAgentWorkflowResolver.test.ts | 25 ++++++++++++++++ .../agent/useAgentWorkflowResolver.ts | 29 +++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) 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)