diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 7434bf0237b..e753b7f6571 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -52,7 +52,11 @@ import { useAgentSendGateStore } from '../../stores/agent/agentSendGateStore' import { useAgentWorkflowTabBindingStore } from '../../stores/agent/agentWorkflowTabBindingStore' import type { SelectedNode } from './useCanvasSelection' -import type { AgentEventSource, TurnOrigin } from './useAgentSession' +import type { + AgentEventSource, + TurnOrigin, + WorkflowTurnContext +} from './useAgentSession' import { useAgentSession } from './useAgentSession' vi.mock(import('@/platform/telemetry/reportError')) @@ -4787,6 +4791,71 @@ describe('useAgentSession (v1 composition root)', () => { expect(session.notices.value).toEqual([]) }) + it('(g11a) a deleted thread invalidates an in-flight send before its acknowledgement', async () => { + let resolvePost: ((ack: AgentTurnAccepted) => void) | undefined + const postMessage = vi + .fn() + .mockResolvedValueOnce({ + thread_id: 'th-1', + message_id: 'msg-1', + workflow_id: 'wf-bound' + }) + .mockImplementationOnce( + () => + new Promise((resolve) => { + resolvePost = resolve + }) + ) + .mockResolvedValueOnce({ + thread_id: 'th-2', + message_id: 'msg-3', + workflow_id: 'wf-fresh' + }) + const rest = fakeRest({ + postMessage, + getMessages: vi.fn(async (): Promise => { + throw new AgentApiError('gone', 404, undefined) + }) + }) + const { source, emit, status } = fakeEvents() + let target: WorkflowTurnContext = { + id: 'wf-bound', + tabPath: 'workflows/bound.json' + } + const session = useAgentSession({ + rest, + events: source, + workflow: { + current: () => target, + adopted: vi.fn(), + draft: () => ({ content: { nodes: [{ id: 1 }], links: [] } }) + } + }) + session.start() + status(true) + + await session.sendMessage('first') + emit(delta('msg-1', 'partial')) + target = { tabPath: 'workflows/Unsaved Workflow.json' } + const staleSend = session.sendMessage('second') + await vi.waitFor(() => expect(postMessage).toHaveBeenCalledTimes(2)) + + status(false) + status(true) + await vi.waitFor(() => expect(session.threadId.value).toBeNull()) + resolvePost?.({ + thread_id: 'th-1', + message_id: 'msg-2', + workflow_id: 'wf-minted' + }) + + await expect(staleSend).resolves.toBe(false) + expect(session.threadId.value).toBeNull() + await session.sendMessage('start over') + expect(postMessage.mock.calls[2][0]).toBe('new') + expect(postMessage.mock.calls[2][1].currentTabUnbound).toBe(true) + }) + it('(g15) a deleted team-workspace thread is forgotten without touching another workspace', async () => { sessionStorage.setItem( 'Comfy.Workspace.Current', @@ -5375,6 +5444,7 @@ describe('useAgentSession (v1 composition root)', () => { draft: () => DraftSnapshot | undefined tabs?: () => OpenTabsSnapshot | undefined adoptIntoStore?: boolean + currentTabPath?: () => string }) { const postMessage = vi.fn() options.acks.forEach((workflowId, index) => @@ -5386,12 +5456,13 @@ describe('useAgentSession (v1 composition root)', () => { ) const bindingStore = useAgentWorkflowTabBindingStore() bindingStore.bind('wf-other-tab', 'workflows/other.json') - const tabPath = 'workflows/Unsaved Workflow.json' + const defaultTabPath = 'workflows/Unsaved Workflow.json' const session = useAgentSession({ rest: fakeRest({ postMessage }), events: fakeEvents().source, workflow: { current: () => { + const tabPath = options.currentTabPath?.() ?? defaultTabPath const id = bindingStore.workflowIdFor(tabPath) return id === undefined ? { tabPath } : { id, tabPath } }, @@ -5404,14 +5475,15 @@ describe('useAgentSession (v1 composition root)', () => { } }) session.start() - return { session, postMessage } + return { session, postMessage, bindingStore } } it('(h10) a continuing turn re-flags a tab that never adopted the workflow another tab holds, with its draft', async () => { const draftSnapshot = { content: { nodes: [], links: [] } } + const draft = vi.fn(() => draftSnapshot) const { session, postMessage } = unboundTabSession({ acks: ['wf-other-tab', 'wf-minted'], - draft: () => draftSnapshot + draft }) await session.sendMessage('add a text input node') @@ -5421,21 +5493,22 @@ describe('useAgentSession (v1 composition root)', () => { expect(second).not.toHaveProperty('workflowId') expect(second.currentTabUnbound).toBe(true) expect(second.draft).toEqual(draftSnapshot) + expect(draft).toHaveBeenCalledTimes(2) }) - it('(h10b) a continuing turn with no draft is not re-flagged, so it cannot mint an empty workflow', async () => { + it('(h10b) a continuing turn with no canvas is held instead of retargeting the remembered workflow', async () => { let draft: DraftSnapshot | undefined = { content: { nodes: [], links: [] } } const { session, postMessage } = unboundTabSession({ - acks: ['wf-other-tab', 'wf-other-tab'], + acks: ['wf-other-tab'], draft: () => draft }) await session.sendMessage('add a text input node') draft = undefined - await session.sendMessage('and a load image node') + const sent = await session.sendMessage('and a load image node') - expect(postMessage.mock.calls[1][1]).not.toHaveProperty('currentTabUnbound') - expect(postMessage.mock.calls[1][1]).not.toHaveProperty('draft') + expect(sent).toBe(false) + expect(postMessage).toHaveBeenCalledTimes(1) }) it('(h10c) once the re-flagged mint is adopted, the next turn names it instead of minting again', async () => { @@ -5455,9 +5528,9 @@ describe('useAgentSession (v1 composition root)', () => { expect(third.workflowId).toBe('wf-minted') }) - it('(h10d) a continuing turn is not re-flagged when the bound workflow is open in no tab', async () => { + it('(h10d) a mint the tab could not adopt is not minted again', async () => { const { session, postMessage } = unboundTabSession({ - acks: ['wf-closed', 'wf-closed'], + acks: ['wf-minted', 'wf-minted'], draft: () => ({ content: { nodes: [], links: [] } }), tabs: () => ({ open_tabs: [] }) }) @@ -5469,6 +5542,213 @@ describe('useAgentSession (v1 composition root)', () => { expect(postMessage.mock.calls[1][1]).not.toHaveProperty('currentTabUnbound') }) + it('(h10e) a continuing turn re-flags the tab once the tab holding the remembered workflow has closed', async () => { + const draftSnapshot = { content: { nodes: [{ id: 1 }], links: [] } } + const { session, postMessage, bindingStore } = unboundTabSession({ + acks: ['wf-other-tab', 'wf-minted'], + draft: () => draftSnapshot, + tabs: () => ({ open_tabs: [] }) + }) + + await session.sendMessage('add a text input node') + bindingStore.unbindWorkflow('wf-other-tab') + await session.sendMessage('and a load image node') + + const second = postMessage.mock.calls[1][1] + expect(second).not.toHaveProperty('workflowId') + expect(second.currentTabUnbound).toBe(true) + expect(second.draft).toEqual(draftSnapshot) + }) + + it('(h10e2) a blank tab keeps the thread workflow once the tab holding it has closed', async () => { + const { session, postMessage, bindingStore } = unboundTabSession({ + acks: ['wf-other-tab', 'wf-other-tab'], + draft: () => ({ content: { nodes: [], links: [] } }), + tabs: () => ({ open_tabs: [] }) + }) + + await session.sendMessage('build a workflow') + bindingStore.unbindWorkflow('wf-other-tab') + await session.sendMessage('now add a sampler') + + expect(postMessage.mock.calls[1][1]).not.toHaveProperty('currentTabUnbound') + expect(postMessage.mock.calls[1][1]).not.toHaveProperty('draft') + }) + + it('(h10e2b) a tab holding only a group is not blank', async () => { + let currentTabPath = 'workflows/other.json' + const draftSnapshot = { + content: { nodes: [], links: [], groups: [{ id: 1 }] } + } + const { session, postMessage, bindingStore } = unboundTabSession({ + acks: ['wf-other-tab', 'wf-minted'], + draft: () => draftSnapshot, + tabs: () => ({ open_tabs: [] }), + currentTabPath: () => currentTabPath + }) + + await session.sendMessage('build a workflow') + currentTabPath = 'workflows/Unsaved Workflow.json' + bindingStore.unbindWorkflow('wf-other-tab') + await session.sendMessage('what is in the group?') + + expect(postMessage.mock.calls[1][1].currentTabUnbound).toBe(true) + }) + + it('(h10e3) a new tab reusing the path of a closed adopted tab mints for itself', async () => { + const draftSnapshot = { content: { nodes: [{ id: 1 }], links: [] } } + const { session, postMessage, bindingStore } = unboundTabSession({ + acks: ['wf-minted', 'wf-minted', 'wf-reused'], + draft: () => draftSnapshot, + adoptIntoStore: true + }) + + await session.sendMessage('add a text input node') + await session.sendMessage('and a load image node') + bindingStore.unbindWorkflow('wf-minted') + await session.sendMessage('and a sampler') + + const third = postMessage.mock.calls[2][1] + expect(third.currentTabUnbound).toBe(true) + expect(third.draft).toEqual(draftSnapshot) + }) + + it('(h10f) a reply without a workflow id does not mint again on every later turn', async () => { + const postMessage = vi.fn(async () => ({ + thread_id: 'th-9', + message_id: 'msg-1' + })) + const session = useAgentSession({ + rest: fakeRest({ postMessage }), + events: fakeEvents().source, + workflow: { + current: () => ({ tabPath: 'workflows/Unsaved Workflow.json' }), + adopted: vi.fn(), + draft: () => ({ content: { nodes: [], links: [] } }) + } + }) + session.start() + + await session.sendMessage('add a text input node') + await session.sendMessage('and a load image node') + + expect(postMessage.mock.calls[0][1].currentTabUnbound).toBe(true) + expect(postMessage.mock.calls[1][1]).not.toHaveProperty('currentTabUnbound') + }) + + function movableTabSession(acks: (string | undefined)[]) { + const postMessage = vi.fn() + acks.forEach((workflowId, index) => + postMessage.mockResolvedValueOnce({ + thread_id: 'th-9', + message_id: `msg-${index + 1}`, + ...(workflowId === undefined ? {} : { workflow_id: workflowId }) + }) + ) + const target: { context: WorkflowTurnContext } = { + context: { id: 'wf-a', tabPath: 'workflows/a.json' } + } + const canvas: { draft: DraftSnapshot | undefined } = { + draft: { content: { nodes: [{ id: 1 }], links: [] } } + } + const session = useAgentSession({ + rest: fakeRest({ postMessage }), + events: fakeEvents().source, + workflow: { + current: () => target.context, + adopted: vi.fn(), + draft: () => canvas.draft + } + }) + session.start() + return { session, postMessage, target, canvas } + } + + it('(h10f2) an id-less reply stops suppressing the mint once the binding moves', async () => { + const { session, postMessage, target } = movableTabSession([ + 'wf-a', + undefined, + 'wf-b', + 'wf-minted' + ]) + const unsaved = { tabPath: 'workflows/Unsaved Workflow.json' } + + await session.sendMessage('on a') + target.context = unsaved + await session.sendMessage('on the unsaved tab') + target.context = { id: 'wf-b', tabPath: 'workflows/b.json' } + await session.sendMessage('on b') + target.context = unsaved + await session.sendMessage('back on the unsaved tab') + + expect(postMessage.mock.calls[1][1].currentTabUnbound).toBe(true) + expect(postMessage.mock.calls[3][1].currentTabUnbound).toBe(true) + }) + + it('(h10f3) a tab with a recorded mint still holds a send with no canvas', async () => { + const { session, postMessage, target, canvas } = movableTabSession([ + 'wf-a', + undefined + ]) + + await session.sendMessage('on a') + target.context = { tabPath: 'workflows/Unsaved Workflow.json' } + await session.sendMessage('on the unsaved tab') + canvas.draft = undefined + const sent = await session.sendMessage('with no canvas') + + expect(sent).toBe(false) + expect(postMessage).toHaveBeenCalledTimes(2) + }) + + it('(h10f4) a host with no canvas provider still sends', async () => { + const postMessage = vi.fn(async () => ({ + thread_id: 'th-9', + message_id: 'msg-1', + workflow_id: 'wf-a' + })) + let context: WorkflowTurnContext = { + id: 'wf-a', + tabPath: 'workflows/a.json' + } + const session = useAgentSession({ + rest: fakeRest({ postMessage }), + events: fakeEvents().source, + workflow: { current: () => context, adopted: vi.fn() } + }) + session.start() + + await session.sendMessage('on a') + context = { tabPath: 'workflows/Unsaved Workflow.json' } + + expect(await session.sendMessage('on the unsaved tab')).toBe(true) + }) + + it('(h10g) a new chat forgets the mint a tab already requested', async () => { + const postMessage = vi.fn(async () => ({ + thread_id: 'th-9', + message_id: 'msg-1' + })) + const conversation = useAgentConversationStore() + const session = useAgentSession({ + rest: fakeRest({ postMessage }), + events: fakeEvents().source, + workflow: { + current: () => ({ tabPath: 'workflows/Unsaved Workflow.json' }), + adopted: vi.fn(), + draft: () => ({ content: { nodes: [], links: [] } }) + } + }) + session.start() + + await session.sendMessage('add a text input node') + session.newChat() + conversation.setThreadId('th-10') + await session.sendMessage('start over') + + expect(postMessage.mock.calls[1][1].currentTabUnbound).toBe(true) + }) + const LINKED_DRAFT: DraftSnapshot = { content: { nodes: [ diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index da1a88ed9eb..9de99eb6b7f 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -509,6 +509,8 @@ export function useAgentSession(deps: AgentSessionDeps) { */ const boundWorkflowId = ref(rememberedWorkflowId) + const unadoptedMintBinding = new Map() + /** * H8 reporting state, kept beside the binding it describes: the workflow * last reported for the current thread (suppresses the ack's re-report of a @@ -614,6 +616,12 @@ export function useAgentSession(deps: AgentSessionDeps) { ) } + function forgetBinding(): void { + boundWorkflowId.value = null + rememberedWorkflowId = null + unadoptedMintBinding.clear() + } + function initializeWorkflowContext(hasThread: boolean): void { workflow?.initialize?.(hasThread) // The binding only outlives a remount together with its thread: a page @@ -955,28 +963,24 @@ export function useAgentSession(deps: AgentSessionDeps) { ) } - function postTurn( - threadId: string, + function refusesTarget( text: string, - origin: TurnOrigin, + originContext: WorkflowTurnContext | undefined, wfContext: WorkflowTurnContext | undefined, - attachments?: SentAttachment[], - tags?: SentTag[], - workflowReferences?: WorkflowReference[], - selectionWorkflowId?: () => string | undefined, - clientMessageId?: string + draft: DraftSnapshot | undefined + ): boolean { + const refused = + workflowTargetChanged(originContext, wfContext) || + lacksCanvasForRemint(wfContext, draft) + if (refused) recordUnavailableTarget(text) + return refused + } + + function postTurn( + threadId: string, + input: PostMessageInput, + wfContext: WorkflowTurnContext | undefined ): Promise { - const input = buildPostInput( - threadId, - text, - origin, - wfContext, - attachments, - tags, - workflowReferences, - selectionWorkflowId, - clientMessageId - ) if (wfContext?.id === undefined) return rest.postMessage(threadId, input) return rest.postMessage(threadId, { ...input, workflowId: wfContext.id }) } @@ -986,13 +990,13 @@ export function useAgentSession(deps: AgentSessionDeps) { text: string, origin: TurnOrigin, wfContext: WorkflowTurnContext | undefined, + draft: DraftSnapshot | undefined, attachments?: SentAttachment[], tags?: SentTag[], workflowReferences?: WorkflowReference[], selectionWorkflowId?: () => string | undefined, clientMessageId?: string ): PostMessageInput { - const draft = workflow?.draft?.(origin) const tabs = workflow?.tabs?.(origin) const unboundTarget = isUnboundTarget( wfContext, @@ -1057,8 +1061,17 @@ export function useAgentSession(deps: AgentSessionDeps) { tabs: OpenTabsSnapshot | undefined ): boolean { if (wfContext === undefined || wfContext.id !== undefined) return false + if (mintAlreadyRequested(wfContext.tabPath, boundWorkflowId)) return false if (boundWorkflowId === null) return true - return draft !== undefined && isOpenInATab(boundWorkflowId, tabs) + if (draft === undefined) return false + return hasContent(draft) || isOpenInATab(boundWorkflowId, tabs) + } + + function hasContent(draft: DraftSnapshot): boolean { + const { nodes, groups } = draft.content + return [nodes, groups].some( + (items) => Array.isArray(items) && items.length > 0 + ) } function isOpenInATab( @@ -1068,6 +1081,38 @@ export function useAgentSession(deps: AgentSessionDeps) { return (tabs?.open_tabs ?? []).some((tab) => tab.workflow_id === workflowId) } + function mintAlreadyRequested( + tabPath: string, + boundWorkflowId: string | null + ): boolean { + return ( + unadoptedMintBinding.has(tabPath) && + unadoptedMintBinding.get(tabPath) === boundWorkflowId + ) + } + + function rememberMint( + input: PostMessageInput, + wfContext: WorkflowTurnContext | undefined, + workflowId: string | undefined + ): void { + if (!input.currentTabUnbound || wfContext === undefined) return + const adoptedOrHeld = + workflowId !== undefined && + bindingStore.tabPathFor(workflowId) !== undefined + if (!adoptedOrHeld) + unadoptedMintBinding.set(wfContext.tabPath, boundWorkflowId.value) + } + + function lacksCanvasForRemint( + wfContext: WorkflowTurnContext | undefined, + draft: DraftSnapshot | undefined + ): boolean { + if (wfContext === undefined || wfContext.id !== undefined) return false + if (boundWorkflowId.value === null) return false + return workflow?.draft !== undefined && draft === undefined + } + function buildTargetFields( threadId: string, wfContext: WorkflowTurnContext | undefined, @@ -1247,6 +1292,33 @@ export function useAgentSession(deps: AgentSessionDeps) { return adoptedActiveTurn } + async function recoverFailedSend({ + error, + text, + sentContext, + accepted, + threadAtSend, + generation + }: { + error: unknown + text: string + sentContext: WorkflowTurnContext | undefined + accepted: boolean + threadAtSend: string + generation: number + }): Promise { + releaseDisownedWorkflow(sentContext, error) + if (generation !== loadGeneration) return false + const reattached = await reattachRefusedTurn( + error, + threadAtSend, + generation + ) + if (generation !== loadGeneration) return false + recordCurrentSendError(error, text, accepted, reattached, threadAtSend) + return false + } + async function performSend( text: string, attachments?: SentAttachment[], @@ -1268,22 +1340,22 @@ export function useAgentSession(deps: AgentSessionDeps) { await prepareWorkflow() if (generation !== loadGeneration) return false const wfContext = workflow?.current(origin) - if (workflowTargetChanged(originContext, wfContext)) { - recordUnavailableTarget(text) - return false - } + const draft = workflow?.draft?.(origin) + if (refusesTarget(text, originContext, wfContext, draft)) return false sentContext = wfContext - const ack = await postTurn( + const input = buildPostInput( threadAtSend, text, origin, wfContext, + draft, attachments, tags, workflowReferences, selectionWorkflowId, clientMessageId ) + const ack = await postTurn(threadAtSend, input, wfContext) accepted = true if (!observeAcceptedTurn(generation, onAccepted)) return false acceptTurn( @@ -1295,21 +1367,17 @@ export function useAgentSession(deps: AgentSessionDeps) { workflowReferences, skillScopeAtSend ) + rememberMint(input, wfContext, ack.workflow_id) return true } catch (error) { - // Before the generation guard: the binding store is page-global and - // persisted, so a refusal that lands after newChat()/loadThread() has - // moved on still has to release, or the dead id survives the reload. - releaseDisownedWorkflow(sentContext, error) - if (generation !== loadGeneration) return false - const reattached = await reattachRefusedTurn( + return recoverFailedSend({ error, + text, + sentContext, + accepted, threadAtSend, generation - ) - if (generation !== loadGeneration) return false - recordCurrentSendError(error, text, accepted, reattached, threadAtSend) - return false + }) } } @@ -1638,8 +1706,7 @@ export function useAgentSession(deps: AgentSessionDeps) { conversationStore.reset() pruneSkillTurns() onThreadActivated?.(null) - boundWorkflowId.value = null - rememberedWorkflowId = null + forgetBinding() pendingWorkflowBind = null malformedStreamReports.clear() localStorage.removeItem(threadStorageKey) @@ -1663,8 +1730,7 @@ export function useAgentSession(deps: AgentSessionDeps) { isNavigationCurrent() const stashedTurn = conversationStore.activeTurnId !== null conversationStore.stashActiveTurn() - boundWorkflowId.value = null - rememberedWorkflowId = null + forgetBinding() pendingWorkflowBind = null malformedStreamReports.clear() conversationStore.setThreadId(threadId) @@ -1996,11 +2062,11 @@ export function useAgentSession(deps: AgentSessionDeps) { conversationStore.settleTurn(turn, undefined) return } + loadGeneration++ conversationStore.reset() pruneSkillTurns() onThreadActivated?.(null) - boundWorkflowId.value = null - rememberedWorkflowId = null + forgetBinding() localStorage.removeItem(threadStorageKey) }