Skip to content
Merged
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 @@ -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' }
])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type WorkflowResolverDeps = {
>
bindings: Pick<
ReturnType<typeof useAgentWorkflowTabBindingStore>,
'workflowIdFor' | 'tabPathFor' | 'matchesWorkflow'
'workflowIdFor' | 'tabPathFor' | 'matchesWorkflow' | 'unbind'
>
listCloudWorkflows: AgentRestClient['listCloudWorkflows']
}
Expand Down Expand Up @@ -92,13 +92,38 @@ export function useAgentWorkflowResolver({
: undefined
}

function indexedNameFor(workflowId: string): string | undefined {
for (const [name, id] of cloudIdsByName.value) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 correctness: cloudIdsByName deliberately omits every name that maps to multiple cloud IDs, so this reverse lookup returns undefined for each of those IDs. bindingIsStale then trusts the persisted binding even when its saved tab has a different, uniquely mapped cloud ID. A focused test with two image_z_image_turbo IDs and a stale cloud-zimage -> Portrait binding failed: boundOrOpenWorkflowFor("cloud-zimage") returned Portrait instead of null, leaving the cross-workflow mutation path open. Please derive the target ID's indexed name from cloudIndex while keeping the unique name-to-ID map for identifying the bound tab, and cover this duplicate-name case.

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)
Expand Down
Loading