From cc7e1766b24023588466990acc7d703fa0858822 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 29 Oct 2026 16:56:29 -0700 Subject: [PATCH] fix(agent): carry the canvas on a continuing turn from a still-unbound tab (PM-1410) (#22310) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Agent reported a removed link as still connected. Fixed for the unsaved-tab case. - Unsaved tab + continuing chat: canvas now reaches the agent - Server already supports it; frontend-only - Two expected-failure pins flip to passing Linear: https://linear.app/comfyorg/issue/PM-1410
Full context for agent readers On a continuing chat, a selected unsaved tab (no cloud id) whose session is bound to a workflow that another open tab holds sent neither its canvas (`draft`) nor `current_tab_unbound`. The server fell back to the thread's remembered workflow, so the agent answered from a stale graph — the PM-1410 report: a link the user severed is still reported as connected. This is the live residual arm pinned by #21931 (unit `(h12)`, black-box `agentUnboundTabLinkSeverance.spec.ts`). - **What**: `isUnboundTarget` now also returns true on a continuing turn when the selected tab has no cloud id, a draft is in hand, and the session's bound workflow is open in another tab (it appears in the turn's `open_tabs`). The turn then carries `current_tab_unbound` plus the draft; the server mints a workflow seeded from this tab's canvas (`agent_handler.go`: the flag skips only the thread-workflow fallback; `TestPostMessageUnboundCurrentTabMintsInsteadOfReusingTheThreadWorkflow`), and adoption binds the mint to the tab, so the following turn names it. - **Unchanged on purpose**: no re-flag without a draft (would mint an empty workflow); no re-flag when the bound workflow is open in no tab (resumed thread, or a bound unsaved tab renamed, whose binding keeps the old path) — those keep today's thread fallback. `canSendDraft` is not relaxed, so a canvas is never posted against a workflow id it does not belong to (risk register steer: do not always post an unidentified canvas; do not reopen document-wins). - **Tests**: red on `main`, green here — `(h10)` re-flag with draft, `(h10c)` mint → adopt → next turn names the mint (no mint loop), `(h11)` the severed link reaches the agent. Guards green on both: `(h10b)` no draft → no flag, `(h10d)` bound workflow open nowhere → no flag. Black-box spec's `test.fail` marker removed and it now asserts `current_tab_unbound`; both entries dropped from `tools/expected-failures/baseline.json`. - Product call (made under the takeover's delegated authority): when a selected unsaved tab is the panel's target, the agent works on that tab, not on another tab's workflow the thread remembers. Accepted consequence: picking a blank unsaved tab as target on a continuing chat mints a fresh workflow for it (same as a first turn does today). - Pre-existing, unrelated local timing flakes seen under host load: `(g6)` and `(g18)` in the same test file fail on `main` locally too; CI is green on `main`. - Adversarial review (fresh Claude reviewer; cross-model reviewer unavailable on this host): findings on rename and resumed-thread over-minting, missing mint-loop test, and comment rules were all addressed in the second commit. Verification: `vitest useAgentSession.test.ts` (all PM-1410 cases), `AgentPanelRoot.test.ts`, `pnpm typecheck`, `pnpm typecheck:browser`, `tools/expected-failures` tests, oxlint/oxfmt. Black-box spec relies on CI.
(cherry picked from commit 4a737bdeff8f14be577fe20699a3af986ae992ce) --- .../composables/agent/useAgentSession.test.ts | 140 +++++++++++++++--- .../composables/agent/useAgentSession.ts | 54 +++---- 2 files changed, 139 insertions(+), 55 deletions(-) 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(