diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 80df08ada06..dce3181dde0 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -42,6 +42,8 @@ import { } from '../../services/agent/agentRestClient' import type { AgentRestClient, + DraftSnapshot, + OpenTabsSnapshot, PostMessageInput } from '../../services/agent/agentRestClient' import type { AskUserResolution } from '../../services/agent/agentMessageParts' @@ -5362,35 +5364,101 @@ describe('useAgentSession (v1 composition root)', () => { expect(body.draft).toEqual(draftSnapshot) }) - // Once a workflow has been bound this session (turn 1's ack), a still- - // unbound tab must NOT keep re-minting: the second turn falls back to - // today's pre-existing behaviour (no signal at all) rather than asking - // the server to mint yet another empty workflow every turn. - it('(h10) a second turn on a still-unbound tab does not re-flag it as unbound', async () => { - const postMessage = vi - .fn() - .mockResolvedValueOnce({ - thread_id: 'th-9', - message_id: 'msg-1', - workflow_id: 'wf-fresh' - }) - .mockResolvedValueOnce({ + const OTHER_TAB_TABS: OpenTabsSnapshot = { + open_tabs: [{ workflow_id: 'wf-other-tab', name: 'other.json' }] + } + + function unboundTabSession(options: { + acks: string[] + draft: () => DraftSnapshot | undefined + tabs?: () => OpenTabsSnapshot | undefined + adoptIntoStore?: boolean + }) { + const postMessage = vi.fn() + options.acks.forEach((workflowId, index) => + postMessage.mockResolvedValueOnce({ thread_id: 'th-9', - message_id: 'msg-2', - workflow_id: 'wf-fresh' + message_id: `msg-${index + 1}`, + workflow_id: workflowId }) - const rest = fakeRest({ postMessage }) - const { source } = fakeEvents() + ) + const bindingStore = useAgentWorkflowTabBindingStore() + bindingStore.bind('wf-other-tab', 'workflows/other.json') + const tabPath = 'workflows/Unsaved Workflow.json' const session = useAgentSession({ - rest, - events: source, + rest: fakeRest({ postMessage }), + events: fakeEvents().source, workflow: { - current: () => ({ tabPath: 'workflows/scratch.json' }), - adopted: vi.fn(), - draft: () => ({ content: { nodes: [], links: [] } }) + current: () => { + const id = bindingStore.workflowIdFor(tabPath) + return id === undefined ? { tabPath } : { id, tabPath } + }, + adopted: (workflowId, sent) => { + if (options.adoptIntoStore && sent && sent.id === undefined) + bindingStore.bind(workflowId, sent.tabPath) + }, + draft: options.draft, + tabs: options.tabs ?? (() => OTHER_TAB_TABS) } }) session.start() + return { session, postMessage } + } + + 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 { session, postMessage } = unboundTabSession({ + acks: ['wf-other-tab', 'wf-minted'], + draft: () => draftSnapshot + }) + + await session.sendMessage('add a text input node') + 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('(h10b) a continuing turn with no draft is not re-flagged, so it cannot mint an empty workflow', async () => { + let draft: DraftSnapshot | undefined = { content: { nodes: [], links: [] } } + const { session, postMessage } = unboundTabSession({ + acks: ['wf-other-tab', 'wf-other-tab'], + draft: () => draft + }) + + await session.sendMessage('add a text input node') + draft = undefined + 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') + }) + + it('(h10c) once the re-flagged mint is adopted, the next turn names it instead of minting again', async () => { + const { session, postMessage } = unboundTabSession({ + acks: ['wf-other-tab', 'wf-minted', 'wf-minted'], + draft: () => ({ content: { nodes: [], links: [] } }), + adoptIntoStore: true + }) + + await session.sendMessage('add a text input node') + await session.sendMessage('and a load image node') + await session.sendMessage('and a sampler') + + expect(postMessage.mock.calls[1][1].currentTabUnbound).toBe(true) + const third = postMessage.mock.calls[2][1] + expect(third).not.toHaveProperty('currentTabUnbound') + 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 () => { + const { session, postMessage } = unboundTabSession({ + acks: ['wf-closed', 'wf-closed'], + draft: () => ({ content: { nodes: [], links: [] } }), + tabs: () => ({ open_tabs: [] }) + }) await session.sendMessage('add a text input node') await session.sendMessage('and a load image node') @@ -5399,6 +5467,34 @@ describe('useAgentSession (v1 composition root)', () => { expect(postMessage.mock.calls[1][1]).not.toHaveProperty('currentTabUnbound') }) + const LINKED_DRAFT: DraftSnapshot = { + content: { + nodes: [ + { id: 1, type: 'CheckpointLoaderSimple' }, + { id: 2, type: 'CLIPTextEncode' } + ], + links: [[1, 1, 1, 2, 1, 'CLIP']] + } + } + const SEVERED_DRAFT: DraftSnapshot = { + content: { nodes: LINKED_DRAFT.content.nodes, links: [] } + } + + it('(h11) a continuing turn on a still-unbound tab carries the connection the user severed (PM-1410)', async () => { + let draft = LINKED_DRAFT + const { session, postMessage } = unboundTabSession({ + acks: ['wf-other-tab', 'wf-minted'], + draft: () => draft + }) + + await session.sendMessage('what is connected here?') + expect(postMessage.mock.calls[0][1].draft).toEqual(LINKED_DRAFT) + draft = SEVERED_DRAFT + await session.sendMessage('is it still connected?') + + expect(postMessage.mock.calls[1][1].draft).toEqual(SEVERED_DRAFT) + }) + it.for(['new-chat', 'history'])( 'cancels preparation after switching conversation: %s', async (context) => { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index ebba4b9e561..da1a88ed9eb 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -993,24 +993,23 @@ export function useAgentSession(deps: AgentSessionDeps) { clientMessageId?: string ): PostMessageInput { const draft = workflow?.draft?.(origin) - const unboundTarget = isUnboundTarget(wfContext, boundWorkflowId.value) - // Resolved here rather than at the call site: `buildPostInput` runs after - // `prepareWorkflow()`, so a tab whose cloud id was still unresolved on - // mount has one by now (QAF-19). + const tabs = workflow?.tabs?.(origin) + const unboundTarget = isUnboundTarget( + wfContext, + boundWorkflowId.value, + draft, + tabs + ) const selectedWorkflowId = selectionWorkflowId?.() return { content: serializeWorkflowReferences(text, workflowReferences ?? []), - tabs: workflow?.tabs?.(origin), + tabs, workflowReferences: serializeReferencedWorkflows( workflowReferences, wfContext?.id ), selection: selectedNodes(tags, selectedWorkflowId), attachments: attachments?.map((attachment) => attachment.ref), - // Carried through untouched so the server can echo it onto - // agent_turn_started: it is the same id this send reports on its own - // app:agent_message_sent event, and the only value that can appear on both - // sides of the message -> turn step. clientMessageId, ...buildTargetFields(threadId, wfContext, draft, unboundTarget) } @@ -1051,33 +1050,22 @@ export function useAgentSession(deps: AgentSessionDeps) { return threadId === 'new' || wfContext?.id !== undefined || unboundTarget } - // current_tab_unbound's own contract (see its generated doc comment) is - // a tab-level fact: this tab has no cloud id yet. wfContext with no id - // is exactly that (a saved tab whose cloud id failed to resolve makes - // wfContext undefined entirely instead - see targetWorkflowTurnContext). - // - // boundWorkflowId === null narrows WHEN we assert that fact, and is a - // client-side policy choice, not part of the field's own meaning: the - // server has no way yet to tell "this thread's remembered workflow is - // itself my own prior unbound mint for this same tab" apart from "an - // unrelated workflow from a different tab" (see - // TestPostMessageUnboundCurrentTabMintsInsteadOfReusingTheThreadWorkflow - // in the agent service), so asserting the flag on every turn a still- - // unbound tab is asked about would mint a fresh, contentless workflow - // each time. Once any turn this session has bound a workflow, that - // binding (or the thread's own remembered workflow) is a safer target - // than minting again. Telling the server this is a selected-but-unbound - // tab, not "nothing selected", is what keeps the seed from telling the - // model no workflow is selected - see PM-1429/PM-1430. function isUnboundTarget( wfContext: WorkflowTurnContext | undefined, - boundWorkflowId: string | null + boundWorkflowId: string | null, + draft: DraftSnapshot | undefined, + tabs: OpenTabsSnapshot | undefined ): boolean { - return ( - wfContext !== undefined && - wfContext.id === undefined && - boundWorkflowId === null - ) + if (wfContext === undefined || wfContext.id !== undefined) return false + if (boundWorkflowId === null) return true + return draft !== undefined && isOpenInATab(boundWorkflowId, tabs) + } + + function isOpenInATab( + workflowId: string, + tabs: OpenTabsSnapshot | undefined + ): boolean { + return (tabs?.open_tabs ?? []).some((tab) => tab.workflow_id === workflowId) } function buildTargetFields(