Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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 @@ -2095,6 +2095,43 @@ describe('useAgentSession (v1 composition root)', () => {
).toBe(false)
})

it.for([
{ returnsToOrigin: false, notices: 0 },
{ returnsToOrigin: true, notices: 1 }
])(
'reports a late answer failure but notifies only on its originating thread: %o',
async ({ returnsToOrigin, notices }) => {
let rejectAnswer: ((reason: unknown) => void) | undefined
const answerAsk = vi.fn(
() =>
new Promise<AgentAnswerAccepted>((_resolve, 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 answered = session.answerAsk('turn-1:call-1', 'run')
await vi.waitFor(() => expect(answerAsk).toHaveBeenCalledOnce())
await session.loadThread('th-2')
if (returnsToOrigin) await session.loadThread('th-1')
rejectAnswer?.(new AgentApiError('backend blip', 400, undefined))

expect(await answered).toBe(false)
expect(reportError).toHaveBeenCalledWith(expect.any(AgentApiError), {
surface: 'agent',
errorType: 'agent_ask_answer_failed'
})
expect(session.notices.value).toHaveLength(notices)
}
)

// PM-1658. A live->down socket transition aborts the active turn, which
// nulls activeTurnId and disposes the transport, while the turn's parts -- a
// pending consent card among them -- stay on screen. Most cases below put
Expand Down
111 changes: 64 additions & 47 deletions src/workbench/extensions/agent/composables/agent/useAgentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,63 @@ export function useAgentSession(deps: AgentSessionDeps) {
}
}

function handleAnswerFailure(
error: unknown,
askId: string,
threadId: string,
notify: boolean
): void {
// A resolution frame can land while this request is still out, and it
// retires the card on the way through. The ask is settled and gone, so
// whatever this rejection says about delivery is no longer news the user
// can act on — any mismatch worth telling them about has already been
// raised by reportSupersededAnswer.
if (!answeringAskIds.value.has(askId)) return
if (
error instanceof AgentApiError &&
TERMINAL_ANSWER_STATUSES.has(error.status)
) {
// A 409 is the ordinary double-click, and the ask really is resolved,
// so it needs neither telemetry nor a notice. The rest mean this
// client could never have answered, which the user has to be told
// about or the card simply vanishes as though it had worked.
if (error.status !== 409) {
reportError(error, {
surface: 'agent',
errorType: 'agent_ask_answer_refused'
})
trackAgentError(
'ask_answer_failed',
'post_acceptance',
'error_overlay',
{
retryable: isRetryableRequestFailure(error, false)
}
)
if (notify) pushError(i18n.global.t('agent.runApproval.answerFailed'))
}
conversationStore.retireAsk(askId, threadId)
return
}
reportError(error, {
surface: 'agent',
errorType: 'agent_ask_answer_failed'
})
trackAgentError('ask_answer_failed', 'post_acceptance', 'error_overlay', {
retryable: isRetryableRequestFailure(error, false)
})
// Every re-drive above has been spent. The card cannot simply go back
// into service: the server CASes an answer onto the row BEFORE it wakes
// the turn and reports 500 for the wake alone, so this may already have
// authorized the run, and it answers any later click by replaying THIS
// selection while the card disappears as though the new one had taken
// effect. On a spend authorization that is the wrong way to be wrong, so
// retire the card and say the outcome is unknown rather than show a raw
// transport string next to a card that is about to vanish.
conversationStore.retireAsk(askId, threadId)
if (notify) pushError(i18n.global.t('agent.runApproval.answerUncertain'))
}

async function answerAsk(
askId: string,
selection: 'run' | 'cancel'
Expand All @@ -1449,60 +1506,20 @@ export function useAgentSession(deps: AgentSessionDeps) {
// parks a run approval for days, so a card can still be answered long
// after the turn that raised it stopped streaming to this client.
if (answeringAskIds.value.has(askId)) return false
const answerOwnedGeneration = ownedGeneration
const answerSessionGeneration = sessionGeneration
const isAnswerOnScreen = () =>
answerOwnedGeneration === ownedGeneration &&
answerSessionGeneration === sessionGeneration &&
conversationStore.threadId === currentThreadId
conversationStore.recordAskSelection(askId, selection)
conversationStore.setAskAnswering(askId, true)
try {
await sendAnswer(currentThreadId, askId, selection)
conversationStore.commitAsk(askId, currentThreadId)
return true
} catch (error) {
// A resolution frame can land while this request is still out, and it
// retires the card on the way through. The ask is settled and gone, so
// whatever this rejection says about delivery is no longer news the user
// can act on — any mismatch worth telling them about has already been
// raised by reportSupersededAnswer.
if (!answeringAskIds.value.has(askId)) return false
if (
error instanceof AgentApiError &&
TERMINAL_ANSWER_STATUSES.has(error.status)
) {
// A 409 is the ordinary double-click, and the ask really is resolved,
// so it needs neither telemetry nor a notice. The rest mean this
// client could never have answered, which the user has to be told
// about or the card simply vanishes as though it had worked.
if (error.status !== 409) {
reportError(error, {
surface: 'agent',
errorType: 'agent_ask_answer_refused'
})
trackAgentError(
'ask_answer_failed',
'post_acceptance',
'error_overlay',
{ retryable: isRetryableRequestFailure(error, false) }
)
pushError(i18n.global.t('agent.runApproval.answerFailed'))
}
conversationStore.retireAsk(askId, currentThreadId)
return false
}
reportError(error, {
surface: 'agent',
errorType: 'agent_ask_answer_failed'
})
trackAgentError('ask_answer_failed', 'post_acceptance', 'error_overlay', {
retryable: isRetryableRequestFailure(error, false)
})
// Every re-drive above has been spent. The card cannot simply go back
// into service: the server CASes an answer onto the row BEFORE it wakes
// the turn and reports 500 for the wake alone, so this may already have
// authorized the run, and it answers any later click by replaying THIS
// selection while the card disappears as though the new one had taken
// effect. On a spend authorization that is the wrong way to be wrong, so
// retire the card and say the outcome is unknown rather than show a raw
// transport string next to a card that is about to vanish.
conversationStore.retireAsk(askId, currentThreadId)
pushError(i18n.global.t('agent.runApproval.answerUncertain'))
handleAnswerFailure(error, askId, currentThreadId, isAnswerOnScreen())
return false
}
}
Expand Down
Loading