Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,23 @@ function isRetryableAnswerFailure(error: unknown): boolean {
return error instanceof TypeError || error instanceof DOMException
}

type CommittedAnswerSource = Parameters<typeof toAskResolution>[0]

function namesCommittedAnswer({
selected,
other_text
}: CommittedAnswerSource): boolean {
return Array.isArray(selected) || Boolean(other_text)
}

function isSupersededAnswer(
submitted: AgentAnswerRequest,
committed: Extract<AskUserResolution, { status: 'answered' }>
committed: Extract<AskUserResolution, { status: 'answered' }>,
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
)
}
Expand Down Expand Up @@ -1529,8 +1540,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) {
Expand All @@ -1546,13 +1556,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)
Expand Down Expand Up @@ -1583,11 +1589,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(
Expand Down Expand Up @@ -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,
Expand Down
Loading