From 72577f037dc42fc06e001d2c25e3b41dedadc706 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 23 Oct 2026 21:15:49 +0000 Subject: [PATCH] fix(agent): report a free-text-only answer that won elsewhere (#21829) ## Summary Follow-up to #21320. That PR stopped a 409 with no selection from raising a false "answered somewhere else" notice, but its guard only looked at `selected`. The ask conflict schema also allows a committed free-text-only answer (`status: answered`, `selected` null or omitted, `other_text` set). In that case the 409 does name the winning answer, and the user got no superseded notice and no telemetry. ## Change - One predicate, `namesCommittedAnswer`: a body names the committed answer when it carries a `selected` array or non-empty `other_text`. - The check moved inside `reportSupersededAnswer`, so all three sources share it: the 202 receipt, the 409 conflict and the `agent_ask_resolved` frame. The frame path had no guard before, so an answered frame with a null selection also raised a false superseded report. - Removes the `//` comment #21320 added (AGENTS.md); the predicate name carries the reason. `selected: []` still counts as naming an answer, matching the existing `reports an empty committed answer that replaced this one` test and the schema ("empty when the answer was free text only"). ## Tests (written first) - `reports a 409 whose free text alone shows another answer won` (null and omitted `selected`): failed before, pass now. - `does not report an answered frame with a null selection as another answer winning`: failed before, passes now. - `reports / does not report a 202 that names only free text` and `... a free-text-only frame` (second commit, from review). Against main's source the 202 "reports" case fails; the others pin behavior that already held. - `useAgentSession.test.ts`: 261 pass. Agent suite: 4194 pass; the only failure is the known order-dependent `(g18)` test (FE-3485), which also fails on main. - typecheck, oxlint, eslint, format clean. ## Review A fresh Claude reviewer (adversarial-review, Skeptic + Minimalist; no cross-model CLI available on this host) found no blockers or majors. Its test-gap and duplicated-type notes are in the second commit. Not changed here: the superseded telemetry message (being rewritten in #21327), and an answered frame with `selected: null` still renders as an empty answer (out of contract per the schema; unchanged). ## Review threads on #21320 This addresses the Medium, the frame-path Low and the Nit. Replies on #21320 explain the other two Lows. ## Overlap https://github.com/Comfy-Org/ComfyUI_frontend/pull/21327 edits the body of `reportSupersededAnswer` below the new guard. Whichever lands second needs a small rebase; no behavior conflict. --- .../composables/agent/useAgentSession.test.ts | 129 ++++++++++++++++++ .../composables/agent/useAgentSession.ts | 39 ++++-- 2 files changed, 154 insertions(+), 14 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index adc65efafad..7277a954b8b 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -2293,6 +2293,135 @@ describe('useAgentSession (v1 composition root)', () => { } ) + it.for([ + [ + 'a null selection', + { + error: 'already answered', + status: 'answered', + selected: null, + other_text: 'someone else' + } + ], + [ + 'no selection', + { + error: 'already answered', + status: 'answered', + other_text: 'someone else' + } + ] + ] as const)( + 'reports a 409 whose free text alone shows another answer won (%s)', + async ([, body]) => { + const { session, answerAsk } = await parkedOnQuestion() + answerAsk.mockRejectedValue( + new AgentApiError('already answered', 409, body) + ) + + await session.answerAsk('turn-1:call-1', { selected: ['sdxl'] }) + + expect(card()).toMatchObject({ + resolution: { + status: 'answered', + selected: [], + otherText: 'someone else' + } + }) + expect(reportError).toHaveBeenCalledWith( + expect.any(Error), + expect.objectContaining({ errorType: 'agent_ask_answer_superseded' }) + ) + expect(session.notices.value).toEqual([ + { + level: 'error', + text: enMessages.agent.runApproval.answerSuperseded + } + ]) + } + ) + + it.for([ + ['reports', 'someone else', true], + ['does not report', 'a LoRA', false] + ] as const)( + '%s a 202 that names only free text (%s)', + async ([, otherText, superseded]) => { + const { session } = await parkedOnQuestion({ + status: 'answered', + other_text: otherText + }) + + await session.answerAsk('turn-1:call-1', { + selected: [], + other_text: 'a LoRA' + }) + + expect(session.notices.value).toHaveLength(superseded ? 1 : 0) + } + ) + + it.for([ + ['reports', 'someone else', true], + ['does not report', 'a LoRA', false] + ] as const)( + '%s a free-text-only frame (%s)', + async ([, otherText, superseded]) => { + const { session, emit } = await parkedOnQuestion() + await session.answerAsk('turn-1:call-1', { + selected: [], + other_text: 'a LoRA' + }) + + emit( + wire({ + type: 'agent_ask_resolved', + data: { + thread_id: 'th-1', + message_id: 'msg-1', + ask_id: 'turn-1:call-1', + status: 'answered', + selected: [], + other_text: otherText + } + }) + ) + + expect(session.notices.value).toHaveLength(superseded ? 1 : 0) + } + ) + + it('does not report a 409 naming only the same free text as another answer winning', async () => { + const { session, answerAsk } = await parkedOnQuestion() + answerAsk.mockRejectedValue( + new AgentApiError('already answered', 409, { + error: 'already answered', + status: 'answered', + other_text: 'a LoRA' + }) + ) + + await session.answerAsk('turn-1:call-1', { + selected: ['sdxl'], + other_text: 'a LoRA' + }) + + expect(session.notices.value).toEqual([]) + }) + + it('does not report an answered frame with a null selection as another answer winning', async () => { + const { session, emit } = await parkedOnQuestion() + await session.answerAsk('turn-1:call-1', { selected: ['sdxl'] }) + + emit(askResolved('msg-1', 'turn-1:call-1', null)) + + expect(reportError).not.toHaveBeenCalledWith( + expect.any(Error), + expect.objectContaining({ errorType: 'agent_ask_answer_superseded' }) + ) + expect(session.notices.value).toEqual([]) + }) + it('puts the card back in service when the server rejects the selection', async () => { const { session, answerAsk } = await parkedOnQuestion() answerAsk.mockRejectedValueOnce( diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 01bf9dde1dc..633d1891713 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -344,12 +344,23 @@ function isRetryableAnswerFailure(error: unknown): boolean { return error instanceof TypeError || error instanceof DOMException } +type CommittedAnswerSource = Parameters[0] + +function namesCommittedAnswer({ + selected, + other_text +}: CommittedAnswerSource): boolean { + return Array.isArray(selected) || Boolean(other_text) +} + function isSupersededAnswer( submitted: AgentAnswerRequest, - committed: Extract + committed: Extract, + selectionKnown: boolean ): boolean { return ( - xor(submitted.selected, committed.selected).length > 0 || + (selectionKnown && + xor(submitted.selected, committed.selected).length > 0) || (submitted.other_text || undefined) !== committed.otherText ) } @@ -1518,8 +1529,7 @@ export function useAgentSession(deps: AgentSessionDeps) { try { const receipt = await sendAnswer(currentThreadId, askId, answer) const committed = toAskResolution(receipt) - if (receipt.selected !== undefined) - reportSupersededAnswer(askId, committed) + reportSupersededAnswer(askId, receipt) conversationStore.commitAsk(askId, currentThreadId, committed) return true } catch (error) { @@ -1535,13 +1545,9 @@ export function useAgentSession(deps: AgentSessionDeps) { threadId: string ): void { if (error instanceof AgentApiError && error.status === 409) { - const conflict = zAgentAskConflict.safeParse(error.body).data - const committed = toAskResolution(conflict ?? {}) - // Same guard as the 202: a conflict that names no selection cannot - // show that another answer won. - if (Array.isArray(conflict?.selected)) - reportSupersededAnswer(askId, committed) - conversationStore.retireAsk(askId, committed, threadId) + const conflict = zAgentAskConflict.safeParse(error.body).data ?? {} + reportSupersededAnswer(askId, conflict) + conversationStore.retireAsk(askId, toAskResolution(conflict), threadId) return } const failure = classifyAnswerFailure(error) @@ -1572,11 +1578,16 @@ export function useAgentSession(deps: AgentSessionDeps) { function reportSupersededAnswer( askId: string, - committed: AskUserResolution + source: CommittedAnswerSource ): void { + if (!namesCommittedAnswer(source)) return + const committed = toAskResolution(source) if (committed.status !== 'answered') return const submitted = conversationStore.takeSubmittedAskAnswer(askId) - if (submitted === undefined || !isSupersededAnswer(submitted, committed)) + if ( + submitted === undefined || + !isSupersededAnswer(submitted, committed, Array.isArray(source.selected)) + ) return reportError( new Error( @@ -1793,7 +1804,7 @@ export function useAgentSession(deps: AgentSessionDeps) { observeLiveDelivery(event) if (event.type === 'agent_ask_resolved') { const resolution = toAskResolution(event.data) - reportSupersededAnswer(event.data.ask_id, resolution) + reportSupersededAnswer(event.data.ask_id, event.data) conversationStore.retireAsk( event.data.ask_id, resolution,