diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 503d3a05d10..7d188ed2196 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -2305,6 +2305,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 0d425a98c2b..d1e2d408a87 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -347,12 +347,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 ) } @@ -1533,8 +1544,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) { @@ -1550,13 +1560,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) @@ -1587,11 +1593,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( @@ -1804,7 +1815,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,