From dc765d3b81a092ade4e13a8b9845e14089efccf7 Mon Sep 17 00:00:00 2001 From: cheap-drain Date: Fri, 4 Sep 2026 08:42:42 +0000 Subject: [PATCH 01/11] fix(agent): ignore stale ask answer failures --- .../composables/agent/useAgentSession.test.ts | 32 +++++++++++++++++++ .../composables/agent/useAgentSession.ts | 4 +++ 2 files changed, 36 insertions(+) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index e3b9ae76002..073e27f7668 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -496,6 +496,38 @@ describe('useAgentSession (v1 composition root)', () => { ).toBe(false) }) + it('ignores a stale answer failure after switching threads', async () => { + let rejectAnswer: ((reason?: unknown) => void) | undefined + const answerAsk = vi.fn( + () => + new Promise((_, reject) => { + rejectAnswer = reject + }) + ) + const { source, emit } = fakeEvents() + const session = useAgentSession({ + rest: fakeRest({ answerAsk }), + events: source + }) + session.start() + await session.sendMessage('build it') + emit(runApproval('msg-1')) + + const pendingAnswer = session.answerAsk('turn-1:call-1', 'run') + await vi.waitFor(() => expect(answerAsk).toHaveBeenCalledOnce()) + await session.loadThread('th-2') + + const conversationStore = useAgentConversationStore() + const ingest = vi.spyOn(conversationStore, 'ingest') + const answeringStateAfterSwitch = session.answeringAskIds.value + rejectAnswer?.(new AgentApiError('already answered', 409, undefined)) + await pendingAnswer + + expect(session.answeringAskIds.value).toBe(answeringStateAfterSwitch) + expect(ingest).not.toHaveBeenCalled() + expect(conversationStore.threadId).toBe('th-2') + }) + it('retains and re-enables an approval after a non-409 answer failure', async () => { const answerAsk = vi .fn() diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 3279f75caeb..cfeb839bc2a 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -384,11 +384,15 @@ export function useAgentSession(deps: AgentSessionDeps) { answeringAskIds.value.has(askId) ) return + const generation = loadGeneration + const isCurrent = () => + generation === loadGeneration && ownedGeneration === sessionGeneration setAskAnswering(askId, true) try { await rest.answerAsk(currentThreadId, askId, [selection]) // Keep the actions disabled until the canonical resolution frame arrives. } catch (error) { + if (!isCurrent()) return setAskAnswering(askId, false) if (error instanceof AgentApiError && error.status === 409) { conversationStore.ingest({ From 5f720d0e9dc06bcbe6961755e134dbf76a7ba686 Mon Sep 17 00:00:00 2001 From: cheap-drain Date: Sat, 5 Sep 2026 00:46:21 +0000 Subject: [PATCH 02/11] fix(agent): clear answering flag on stale answer failure; report non-409s A failed answerAsk on a stale thread returned before setAskAnswering(askId, false), wedging the ask in answeringAskIds forever (retry silently dropped, approval rendered disabled). Clear the flag first, then gate only the 409 ingest and the thread-local pushError notice on freshness. reportError now fires for genuine non-409 failures regardless of UI freshness so the endpoint's outage stays visible in telemetry. Tests: stale 409 asserts membership cleared + no ingest/notice/report; new stale non-409 test asserts reportError fires while the new thread sees no ingest/notice mutation. --- .../composables/agent/useAgentSession.test.ts | 40 ++++++++++++++++++- .../composables/agent/useAgentSession.ts | 8 +++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 073e27f7668..d67dee0d503 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -519,11 +519,47 @@ describe('useAgentSession (v1 composition root)', () => { const conversationStore = useAgentConversationStore() const ingest = vi.spyOn(conversationStore, 'ingest') - const answeringStateAfterSwitch = session.answeringAskIds.value rejectAnswer?.(new AgentApiError('already answered', 409, undefined)) await pendingAnswer - expect(session.answeringAskIds.value).toBe(answeringStateAfterSwitch) + expect(session.answeringAskIds.value.has('turn-1:call-1')).toBe(false) + expect(ingest).not.toHaveBeenCalled() + expect(session.notices.value).toEqual([]) + expect(reportError).not.toHaveBeenCalled() + expect(conversationStore.threadId).toBe('th-2') + }) + + it('reports a stale non-409 answer failure without mutating the new thread', async () => { + let rejectAnswer: ((reason?: unknown) => void) | undefined + const answerAsk = vi.fn( + () => + new Promise((_, reject) => { + rejectAnswer = reject + }) + ) + const { source, emit } = fakeEvents() + const session = useAgentSession({ + rest: fakeRest({ answerAsk }), + events: source + }) + session.start() + await session.sendMessage('build it') + emit(runApproval('msg-1')) + + const pendingAnswer = session.answerAsk('turn-1:call-1', 'run') + await vi.waitFor(() => expect(answerAsk).toHaveBeenCalledOnce()) + await session.loadThread('th-2') + + const conversationStore = useAgentConversationStore() + const ingest = vi.spyOn(conversationStore, 'ingest') + rejectAnswer?.(new AgentApiError('backend blip', 500, undefined)) + await pendingAnswer + + expect(reportError).toHaveBeenCalledWith(expect.any(AgentApiError), { + errorType: 'agent_ask_answer_failed' + }) + expect(session.answeringAskIds.value.has('turn-1:call-1')).toBe(false) + expect(session.notices.value).toEqual([]) expect(ingest).not.toHaveBeenCalled() expect(conversationStore.threadId).toBe('th-2') }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index cfeb839bc2a..e5ebf1c78c5 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -392,9 +392,12 @@ export function useAgentSession(deps: AgentSessionDeps) { await rest.answerAsk(currentThreadId, askId, [selection]) // Keep the actions disabled until the canonical resolution frame arrives. } catch (error) { - if (!isCurrent()) return + // A failed answer must never wedge the ask in answeringAskIds, even when + // the failure lands on a thread the user already left. setAskAnswering(askId, false) if (error instanceof AgentApiError && error.status === 409) { + // Another path resolved the ask; only rehydrate the visible thread. + if (!isCurrent()) return conversationStore.ingest({ type: 'agent_ask_resolved', data: { @@ -407,7 +410,10 @@ export function useAgentSession(deps: AgentSessionDeps) { }) return } + // A backend failure to record the answer is real telemetry regardless of + // which thread is on screen; only the user-facing notice is thread-local. reportError(error, { errorType: 'agent_ask_answer_failed' }) + if (!isCurrent()) return pushError(error instanceof Error ? error.message : String(error)) } } From dba47f4a1f1d43e7525ffeee1194a8cfdd0e2118 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sat, 5 Sep 2026 05:04:28 +0000 Subject: [PATCH 03/11] fix(agent): retain stale-thread ask resolutions Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16949#discussion_r3938017150 --- .../agent/composables/agent/useAgentSession.test.ts | 13 +++++++++---- .../agent/composables/agent/useAgentSession.ts | 3 +-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index d67dee0d503..13fc4ebbfd6 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -496,7 +496,7 @@ describe('useAgentSession (v1 composition root)', () => { ).toBe(false) }) - it('ignores a stale answer failure after switching threads', async () => { + it('records a 409 resolution after switching threads', async () => { let rejectAnswer: ((reason?: unknown) => void) | undefined const answerAsk = vi.fn( () => @@ -517,16 +517,21 @@ describe('useAgentSession (v1 composition root)', () => { await vi.waitFor(() => expect(answerAsk).toHaveBeenCalledOnce()) await session.loadThread('th-2') - const conversationStore = useAgentConversationStore() - const ingest = vi.spyOn(conversationStore, 'ingest') rejectAnswer?.(new AgentApiError('already answered', 409, undefined)) await pendingAnswer + const conversationStore = useAgentConversationStore() expect(session.answeringAskIds.value.has('turn-1:call-1')).toBe(false) - expect(ingest).not.toHaveBeenCalled() expect(session.notices.value).toEqual([]) expect(reportError).not.toHaveBeenCalled() expect(conversationStore.threadId).toBe('th-2') + + await session.loadThread('th-1') + expect( + conversationStore.messages[0].parts.some( + (part) => part.type === 'runApproval' + ) + ).toBe(false) }) it('reports a stale non-409 answer failure without mutating the new thread', async () => { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index e5ebf1c78c5..df1dfd16cba 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -396,8 +396,7 @@ export function useAgentSession(deps: AgentSessionDeps) { // the failure lands on a thread the user already left. setAskAnswering(askId, false) if (error instanceof AgentApiError && error.status === 409) { - // Another path resolved the ask; only rehydrate the visible thread. - if (!isCurrent()) return + if (ownedGeneration !== sessionGeneration) return conversationStore.ingest({ type: 'agent_ask_resolved', data: { From d2c840b42eede4fc7eae913d4c5bfeff412c2495 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sat, 5 Sep 2026 05:07:21 +0000 Subject: [PATCH 04/11] fix(agent): invalidate stopped sessions Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16949#discussion_r3938017175 --- .../composables/agent/useAgentSession.test.ts | 29 +++++++++++++++++++ .../composables/agent/useAgentSession.ts | 3 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 13fc4ebbfd6..f06d1849d5d 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -534,6 +534,35 @@ describe('useAgentSession (v1 composition root)', () => { ).toBe(false) }) + it('ignores a 409 resolution after the session stops', async () => { + let rejectAnswer: ((reason?: unknown) => void) | undefined + const answerAsk = vi.fn( + () => + new Promise((_, reject) => { + rejectAnswer = reject + }) + ) + const { source, emit } = fakeEvents() + const session = useAgentSession({ + rest: fakeRest({ answerAsk }), + events: source + }) + session.start() + await session.sendMessage('build it') + emit(runApproval('msg-1')) + + const pendingAnswer = session.answerAsk('turn-1:call-1', 'run') + await vi.waitFor(() => expect(answerAsk).toHaveBeenCalledOnce()) + session.stop() + + const conversationStore = useAgentConversationStore() + const ingest = vi.spyOn(conversationStore, 'ingest') + rejectAnswer?.(new AgentApiError('already answered', 409, undefined)) + await pendingAnswer + + expect(ingest).not.toHaveBeenCalled() + }) + it('reports a stale non-409 answer failure without mutating the new thread', async () => { let rejectAnswer: ((reason?: unknown) => void) | undefined const answerAsk = vi.fn( diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index df1dfd16cba..26c8ffed5c6 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -212,7 +212,8 @@ export function useAgentSession(deps: AgentSessionDeps) { unsubscribeStatus?.() unsubscribe = null unsubscribeStatus = null - const stoppedGeneration = ownedGeneration + if (ownedGeneration !== sessionGeneration) return + const stoppedGeneration = ++sessionGeneration queueMicrotask(() => { if (stoppedGeneration !== sessionGeneration) return conversationStore.abortActiveTurn() From 5f842b740466402b85b21024e28a597aab90a83e Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sat, 5 Sep 2026 11:07:55 +0000 Subject: [PATCH 05/11] fix(agent): reject restarted ask resolutions --- .../agent/composables/agent/useAgentSession.test.ts | 1 + .../extensions/agent/composables/agent/useAgentSession.ts | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index f06d1849d5d..7b19ea5aa34 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -554,6 +554,7 @@ describe('useAgentSession (v1 composition root)', () => { const pendingAnswer = session.answerAsk('turn-1:call-1', 'run') await vi.waitFor(() => expect(answerAsk).toHaveBeenCalledOnce()) session.stop() + session.start() const conversationStore = useAgentConversationStore() const ingest = vi.spyOn(conversationStore, 'ingest') diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 26c8ffed5c6..cd295ce0bba 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -385,6 +385,8 @@ export function useAgentSession(deps: AgentSessionDeps) { answeringAskIds.value.has(askId) ) return + const answerOwnedGeneration = ownedGeneration + const answerSessionGeneration = sessionGeneration const generation = loadGeneration const isCurrent = () => generation === loadGeneration && ownedGeneration === sessionGeneration @@ -397,7 +399,11 @@ export function useAgentSession(deps: AgentSessionDeps) { // the failure lands on a thread the user already left. setAskAnswering(askId, false) if (error instanceof AgentApiError && error.status === 409) { - if (ownedGeneration !== sessionGeneration) return + if ( + answerOwnedGeneration !== ownedGeneration || + answerSessionGeneration !== sessionGeneration + ) + return conversationStore.ingest({ type: 'agent_ask_resolved', data: { From d604c88ebee65df009f182af936ae93e528c21e0 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 9 Sep 2026 19:40:19 -0700 Subject: [PATCH 06/11] test(agent): narrow stale ask mutation assertions Addresses review feedback:\nhttps://github.com/Comfy-Org/ComfyUI_frontend/pull/16949#discussion_r3938017159 --- .../agent/composables/agent/useAgentSession.test.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 7b19ea5aa34..ba5d9ab351c 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -561,7 +561,9 @@ describe('useAgentSession (v1 composition root)', () => { rejectAnswer?.(new AgentApiError('already answered', 409, undefined)) await pendingAnswer - expect(ingest).not.toHaveBeenCalled() + expect(ingest).not.toHaveBeenCalledWith( + expect.objectContaining({ type: 'agent_ask_resolved' }) + ) }) it('reports a stale non-409 answer failure without mutating the new thread', async () => { @@ -595,7 +597,9 @@ describe('useAgentSession (v1 composition root)', () => { }) expect(session.answeringAskIds.value.has('turn-1:call-1')).toBe(false) expect(session.notices.value).toEqual([]) - expect(ingest).not.toHaveBeenCalled() + expect(ingest).not.toHaveBeenCalledWith( + expect.objectContaining({ type: 'agent_ask_resolved' }) + ) expect(conversationStore.threadId).toBe('th-2') }) From 8cd54d27da39bd17ac61e1a7bf0c52277e284e6d Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Fri, 11 Sep 2026 10:47:16 -0700 Subject: [PATCH 07/11] refactor(agent): drop comments restating ask-failure cleanup Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16949#discussion_r3975188030 --- .../extensions/agent/composables/agent/useAgentSession.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index cd295ce0bba..88585bfef96 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -395,8 +395,6 @@ export function useAgentSession(deps: AgentSessionDeps) { await rest.answerAsk(currentThreadId, askId, [selection]) // Keep the actions disabled until the canonical resolution frame arrives. } catch (error) { - // A failed answer must never wedge the ask in answeringAskIds, even when - // the failure lands on a thread the user already left. setAskAnswering(askId, false) if (error instanceof AgentApiError && error.status === 409) { if ( @@ -416,8 +414,6 @@ export function useAgentSession(deps: AgentSessionDeps) { }) return } - // A backend failure to record the answer is real telemetry regardless of - // which thread is on screen; only the user-facing notice is thread-local. reportError(error, { errorType: 'agent_ask_answer_failed' }) if (!isCurrent()) return pushError(error instanceof Error ? error.message : String(error)) From 72d3c2cfb61793b3203b17933a948510a8216e3c Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Fri, 11 Sep 2026 10:48:49 -0700 Subject: [PATCH 08/11] fix(agent): gate stale ask notices on thread identity loadGeneration only counts navigations, so returning to the originating conversation suppressed a live failure: th-1 -> th-2 -> th-1 then a 500 cleared answeringAskIds and re-showed the approval with no notice. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16949#discussion_r3975188025 --- .../composables/agent/useAgentSession.test.ts | 38 +++++++++++++++++++ .../composables/agent/useAgentSession.ts | 16 ++++---- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index ba5d9ab351c..66be94dffeb 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -603,6 +603,44 @@ describe('useAgentSession (v1 composition root)', () => { expect(conversationStore.threadId).toBe('th-2') }) + it('surfaces a non-409 answer failure after returning to the originating thread', async () => { + let rejectAnswer: ((reason?: unknown) => void) | undefined + const answerAsk = vi.fn( + () => + new Promise((_, reject) => { + rejectAnswer = reject + }) + ) + const { source, emit } = fakeEvents() + const session = useAgentSession({ + rest: fakeRest({ answerAsk }), + events: source + }) + session.start() + await session.sendMessage('build it') + emit(runApproval('msg-1')) + + const pendingAnswer = session.answerAsk('turn-1:call-1', 'run') + await vi.waitFor(() => expect(answerAsk).toHaveBeenCalledOnce()) + await session.loadThread('th-2') + await session.loadThread('th-1') + + rejectAnswer?.(new AgentApiError('backend blip', 500, undefined)) + await pendingAnswer + + const conversationStore = useAgentConversationStore() + expect(conversationStore.threadId).toBe('th-1') + expect( + conversationStore.messages[0].parts.some( + (part) => part.type === 'runApproval' + ) + ).toBe(true) + expect(session.answeringAskIds.value.has('turn-1:call-1')).toBe(false) + expect(session.notices.value).toEqual([ + { level: 'error', text: 'backend blip' } + ]) + }) + it('retains and re-enables an approval after a non-409 answer failure', async () => { const answerAsk = vi .fn() diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 88585bfef96..aa83ba57aae 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -387,9 +387,11 @@ export function useAgentSession(deps: AgentSessionDeps) { return const answerOwnedGeneration = ownedGeneration const answerSessionGeneration = sessionGeneration - const generation = loadGeneration - const isCurrent = () => - generation === loadGeneration && ownedGeneration === sessionGeneration + const isAnswerSessionLive = () => + answerOwnedGeneration === ownedGeneration && + answerSessionGeneration === sessionGeneration + const isAnswerThreadOnScreen = () => + isAnswerSessionLive() && conversationStore.threadId === currentThreadId setAskAnswering(askId, true) try { await rest.answerAsk(currentThreadId, askId, [selection]) @@ -397,11 +399,7 @@ export function useAgentSession(deps: AgentSessionDeps) { } catch (error) { setAskAnswering(askId, false) if (error instanceof AgentApiError && error.status === 409) { - if ( - answerOwnedGeneration !== ownedGeneration || - answerSessionGeneration !== sessionGeneration - ) - return + if (!isAnswerSessionLive()) return conversationStore.ingest({ type: 'agent_ask_resolved', data: { @@ -415,7 +413,7 @@ export function useAgentSession(deps: AgentSessionDeps) { return } reportError(error, { errorType: 'agent_ask_answer_failed' }) - if (!isCurrent()) return + if (!isAnswerThreadOnScreen()) return pushError(error instanceof Error ? error.message : String(error)) } } From de5bfa5873c9dabd501a85141aa1f0ea0e4925cc Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 18 Sep 2026 02:19:42 -0700 Subject: [PATCH 09/11] refactor(agent): isolate ask failure handling --- browser_tests/tests/agent/agentPanel.spec.ts | 61 ++++++++++++ .../composables/agent/useAgentSession.ts | 95 ++++++++++++++----- 2 files changed, 133 insertions(+), 23 deletions(-) diff --git a/browser_tests/tests/agent/agentPanel.spec.ts b/browser_tests/tests/agent/agentPanel.spec.ts index e94a28228e6..ec512a5c5e5 100644 --- a/browser_tests/tests/agent/agentPanel.spec.ts +++ b/browser_tests/tests/agent/agentPanel.spec.ts @@ -171,6 +171,67 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { await expect(firstSummary).toHaveAttribute('aria-expanded', 'false') }) + test('does not surface an ask failure after starting a new chat', async ({ + comfyPage, + getWebSocket + }) => { + const page = comfyPage.page + let releaseAnswer!: () => void + let markAnswerRequested!: () => void + const answerReleased = new Promise((resolve) => { + releaseAnswer = resolve + }) + const answerRequested = new Promise((resolve) => { + markAnswerRequested = resolve + }) + await page.route('**/api/agent/threads/*/asks/*/answer', async (route) => { + markAnswerRequested() + await answerReleased + await route.fulfill({ + status: 500, + contentType: 'application/json', + body: JSON.stringify({ message: 'stale ask failure' }) + }) + }) + + await page.getByRole('button', { name: OPEN_AGENT_LABEL }).click() + const panel = page.locator('#agent-panel-root') + const composer = panel.getByRole('textbox', { name: /^Describe ideas/ }) + await composer.fill('Build a portrait workflow') + await panel.getByRole('button', { name: 'Send' }).click() + + const ws = await getWebSocket() + pushEvent(ws, { + type: 'agent_ask', + data: { + thread_id: 'd4c016c4-3b8c-44cf-97de-1ae27e43e718', + message_id: '3818ba00-d772-4a3f-98c1-9312725b577d', + ask_id: 'turn-1:call-1', + kind: 'run_approval', + context: { + workflow_id: 'workflow-1', + workflow_name: 'Portrait workflow' + }, + prompt: 'Run it?', + options: [ + { id: 'run', label: 'Run' }, + { id: 'cancel', label: 'Cancel' } + ], + min_selections: 1, + max_selections: 1, + allow_other: false + } + }) + await panel.getByRole('button', { name: 'Run' }).click() + await answerRequested + + await panel.getByRole('button', { name: enMessages.agent.newChat }).click() + releaseAnswer() + + await expect(panel.getByText('What do you want to make?')).toBeVisible() + await expect(page.getByText('stale ask failure')).toHaveCount(0) + }) + test.describe('composer sizing', () => { test.use({ viewport: { width: 1920, height: 1080 } }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index aa83ba57aae..dc5f37bd824 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -373,6 +373,70 @@ export function useAgentSession(deps: AgentSessionDeps) { } } + function isSessionGenerationCurrent( + expectedOwnedGeneration: number, + expectedSessionGeneration: number + ): boolean { + return ( + expectedOwnedGeneration === ownedGeneration && + expectedSessionGeneration === sessionGeneration + ) + } + + function isAnswerThreadOnScreen( + expectedOwnedGeneration: number, + expectedSessionGeneration: number, + expectedThreadId: string + ): boolean { + return ( + isSessionGenerationCurrent( + expectedOwnedGeneration, + expectedSessionGeneration + ) && conversationStore.threadId === expectedThreadId + ) + } + + function handleAnswerAskError( + error: unknown, + askId: string, + messageId: string, + threadId: string, + answerOwnedGeneration: number, + answerSessionGeneration: number + ): void { + setAskAnswering(askId, false) + if (error instanceof AgentApiError && error.status === 409) { + if ( + !isSessionGenerationCurrent( + answerOwnedGeneration, + answerSessionGeneration + ) + ) + return + conversationStore.ingest({ + type: 'agent_ask_resolved', + data: { + thread_id: threadId, + message_id: messageId, + ask_id: askId, + status: 'answered', + selected: null + } + }) + return + } + reportError(error, { errorType: 'agent_ask_answer_failed' }) + if ( + !isAnswerThreadOnScreen( + answerOwnedGeneration, + answerSessionGeneration, + threadId + ) + ) + return + pushError(error instanceof Error ? error.message : String(error)) + } + async function answerAsk( askId: string, selection: 'run' | 'cancel' @@ -387,34 +451,19 @@ export function useAgentSession(deps: AgentSessionDeps) { return const answerOwnedGeneration = ownedGeneration const answerSessionGeneration = sessionGeneration - const isAnswerSessionLive = () => - answerOwnedGeneration === ownedGeneration && - answerSessionGeneration === sessionGeneration - const isAnswerThreadOnScreen = () => - isAnswerSessionLive() && conversationStore.threadId === currentThreadId setAskAnswering(askId, true) try { await rest.answerAsk(currentThreadId, askId, [selection]) // Keep the actions disabled until the canonical resolution frame arrives. } catch (error) { - setAskAnswering(askId, false) - if (error instanceof AgentApiError && error.status === 409) { - if (!isAnswerSessionLive()) return - conversationStore.ingest({ - type: 'agent_ask_resolved', - data: { - thread_id: currentThreadId, - message_id: messageId, - ask_id: askId, - status: 'answered', - selected: null - } - }) - return - } - reportError(error, { errorType: 'agent_ask_answer_failed' }) - if (!isAnswerThreadOnScreen()) return - pushError(error instanceof Error ? error.message : String(error)) + handleAnswerAskError( + error, + askId, + messageId, + currentThreadId, + answerOwnedGeneration, + answerSessionGeneration + ) } } From b1a388bd07ec868b8256def106835ff5df90b661 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Sun, 20 Sep 2026 12:58:45 -0700 Subject: [PATCH 10/11] test(agent): pin stale-answer cleanup across stop and start `ignores a 409 resolution after the session stops` asserted only that the resolution is not ingested. Suppressing the resolution while leaving the ask in `answeringAskIds` would pass that, and would strand the approval disabled so a retry is impossible - the user-visible half of the same bug. Asserting the id is cleared closes that. Moving `setAskAnswering(askId, false)` inside the current-session branch of `handleAnswerAskError` now fails this case, where before the mutation slipped past it. --- .../agent/composables/agent/useAgentSession.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 15c808561ee..e6111dd0518 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -571,6 +571,10 @@ describe('useAgentSession (v1 composition root)', () => { expect(ingest).not.toHaveBeenCalledWith( expect.objectContaining({ type: 'agent_ask_resolved' }) ) + // Suppressing the resolution must not also strand the ask: cleanup runs + // across stop()/start(), so the approval is answerable again rather than + // left permanently disabled. + expect(session.answeringAskIds.value.has('turn-1:call-1')).toBe(false) }) it('reports a stale non-409 answer failure without mutating the new thread', async () => { From 68402700c5957d8628f2df0b39ed7e61944f2fc3 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Sun, 20 Sep 2026 17:36:59 -0700 Subject: [PATCH 11/11] test(agent): wait for the failed answer response before asserting its absence The new-chat regression released the 500 route and asserted the error text was absent in the same tick, so `toHaveCount(0)` could pass before `answerAsk()` had even received the response. Wait for the 500 on the answer endpoint first; the rejection handler runs in the page before the next assertion round-trip, so the negative now observes the post-handler state rather than the pre-response one. Addresses https://github.com/Comfy-Org/ComfyUI_frontend/pull/16949#discussion_r4045488152 --- browser_tests/tests/agent/agentPanel.spec.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/browser_tests/tests/agent/agentPanel.spec.ts b/browser_tests/tests/agent/agentPanel.spec.ts index e5638db4803..b2f679411d3 100644 --- a/browser_tests/tests/agent/agentPanel.spec.ts +++ b/browser_tests/tests/agent/agentPanel.spec.ts @@ -321,8 +321,18 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { await panel.getByRole('button', { name: 'Run' }).click() await answerRequested + // `answerAsk()` only reaches its error handler after the 500 is received, + // so the absence assertion has to wait for that response. Without this the + // negative could pass while the route was still held open. + const answerFailure = page.waitForResponse( + (response) => + /\/api\/agent\/threads\/[^/]+\/asks\/[^/]+\/answer$/.test( + response.url() + ) && response.status() === 500 + ) await panel.getByRole('button', { name: enMessages.agent.newChat }).click() releaseAnswer() + await answerFailure await expect(panel.getByText('What do you want to make?')).toBeVisible() await expect(page.getByText('stale ask failure')).toHaveCount(0)