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 @@ -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<AgentRestClient['postMessage']>(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<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
Loading