diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 024279a471d..53b3d8bb6a7 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -7796,6 +7796,33 @@ describe('useAgentSession (v1 composition root)', () => { expect(disowned).toHaveBeenCalledWith('wf-dead') }) + it('(k5) reports a discarded accepted turn when the send generation moved on before the ack', async () => { + let abandon: () => void = () => {} + const postMessage = vi.fn(async () => { + abandon() + return { thread_id: 'th-1', message_id: 'msg-1' } + }) + const session = useAgentSession({ + rest: fakeRest({ postMessage }), + events: fakeEvents().source + }) + session.start() + abandon = () => session.newChat() + + expect(await session.sendMessage('run it')).toBe(false) + + expect(reportError).toHaveBeenCalledWith(expect.any(Error), { + surface: 'agent', + errorType: 'failure_tracking_agent_accepted_turn_generation_change', + context: { + threadAtSend: 'new', + ackThreadId: 'th-1', + turnId: toTurnId('msg-1') + } + }) + expect(session.entries.value).toEqual([]) + }) + it('(k) a failed POST records the user text plus a settled error reply and returns false', async () => { const postMessage = vi .fn< diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index f008d3b326d..7c69db71cc8 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -1637,7 +1637,8 @@ export function useAgentSession(deps: AgentSessionDeps) { ) const ack = await postTurn(threadAtSend, input, wfContext) accepted = true - if (!observeAcceptedTurn(generation, onAccepted)) return false + if (!observeAcceptedTurn(generation, ack, threadAtSend, onAccepted)) + return false acceptTurn( ack, text, @@ -1663,13 +1664,30 @@ export function useAgentSession(deps: AgentSessionDeps) { function observeAcceptedTurn( generation: number, + ack: AgentTurnAccepted, + threadAtSend: string, onAccepted?: () => void ): boolean { runAcceptedTurnSideEffect( () => onAccepted?.(), 'agent_turn_accepted_callback_failed' ) - return generation === loadGeneration + const isCurrent = generation === loadGeneration + if (!isCurrent) { + reportError( + new Error('agent turn accepted after the send generation moved on'), + { + surface: 'agent', + errorType: 'failure_tracking_agent_accepted_turn_generation_change', + context: { + threadAtSend, + ackThreadId: ack.thread_id, + turnId: toTurnId(ack.message_id) + } + } + ) + } + return isCurrent } function workflowTargetChanged(