From 61e8ad19dd416f5c53bc200ca7f06a3f47c3cd09 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 4 Nov 2026 12:55:52 +0000 Subject: [PATCH 1/2] PM-1975 fix(agent): report when an accepted turn is dropped on a thread switch Switching chats or starting a new chat while a send's POST is still in flight bumps loadGeneration, but the server's ack can still land afterward. performSend's generation check then skips acceptTurn() entirely -- the turn gets no local bookkeeping (no activeTurnId, nothing for stashActiveTurn to stash) even though the server keeps running it. agentSendGateStore's own comment already names this as the unclosed half of PM-1660: "stashActiveTurn() has no slot to stash before the ack and performSend's generation check drops the late one." This is the most likely source of PM-1975's report (a message seemingly sent twice, with the multichoice answer generated twice, after switching chats mid-send): a turn the server accepted and later finished resurfaces later with nothing in the client's history to reconcile against. The reporter's own debug-log attachments are CRDT document-sync traces, not a chat-turn trace, so they cannot pin the exact rendering mechanism from source alone. Report the drop through reportError instead of leaving it silent, so a live occurrence carries the thread/turn ids needed to confirm (or rule out) this path as the cause. --- .../composables/agent/useAgentSession.test.ts | 34 +++++++++++++++++++ .../composables/agent/useAgentSession.ts | 30 ++++++++++++++-- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 024279a471d..f94960fd1a9 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -7796,6 +7796,40 @@ describe('useAgentSession (v1 composition root)', () => { expect(disowned).toHaveBeenCalledWith('wf-dead') }) + // PM-1975: the server can admit a turn (ack received) after the user + // already switched chats/started a new chat, bumping the send generation. + // acceptTurn() never runs for it, so it previously vanished from local + // bookkeeping with no trace -- exactly the gap agentSendGateStore's own + // comment names as the unclosed half of PM-1660 ("stashActiveTurn() has no + // slot to stash before the ack and performSend's generation check drops + // the late one"). This asserts the drop is now reported instead of silent. + 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: 'agent_turn_accepted_discarded_on_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..46c03f3e58a 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,38 @@ 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) { + // PM-1975: the server admitted this turn (ack received), but the user + // switched chats/started a new chat before the ack landed, bumping + // loadGeneration. acceptTurn() below never runs, so the turn gets no + // local bookkeeping (no activeTurnId, nothing to stash) even though the + // server keeps running it -- it was previously dropped with no visible + // trace. agentSendGateStore documents this as the known-but-unclosed + // half of PM-1660: "stashActiveTurn() has no slot to stash before the + // ack and performSend's generation check drops the late one." + reportError( + new Error('agent turn accepted after the send generation moved on'), + { + surface: 'agent', + errorType: 'agent_turn_accepted_discarded_on_generation_change', + context: { + threadAtSend, + ackThreadId: ack.thread_id, + turnId: toTurnId(ack.message_id) + } + } + ) + } + return isCurrent } function workflowTargetChanged( From f6d792677c94dfec172644e30f7e9e41d061b40d Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 4 Nov 2026 13:25:27 +0000 Subject: [PATCH 2/2] Address CodeRabbit review: drop added comments, fix errorType naming Repo guideline blocks added comments beyond curt JSDoc on exported APIs; the rationale lives in the PR description instead. Also renames the new errorType to the category_operation_subject convention used elsewhere in this file (failure_recovering_agent_turn, failure_delivering_agent_approval_ask): failure (category) + tracking (present-participle operation) + agent_accepted_turn (subject) + generation_change (detail). --- .../agent/composables/agent/useAgentSession.test.ts | 9 +-------- .../agent/composables/agent/useAgentSession.ts | 10 +--------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index f94960fd1a9..53b3d8bb6a7 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -7796,13 +7796,6 @@ describe('useAgentSession (v1 composition root)', () => { expect(disowned).toHaveBeenCalledWith('wf-dead') }) - // PM-1975: the server can admit a turn (ack received) after the user - // already switched chats/started a new chat, bumping the send generation. - // acceptTurn() never runs for it, so it previously vanished from local - // bookkeeping with no trace -- exactly the gap agentSendGateStore's own - // comment names as the unclosed half of PM-1660 ("stashActiveTurn() has no - // slot to stash before the ack and performSend's generation check drops - // the late one"). This asserts the drop is now reported instead of silent. 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 () => { @@ -7820,7 +7813,7 @@ describe('useAgentSession (v1 composition root)', () => { expect(reportError).toHaveBeenCalledWith(expect.any(Error), { surface: 'agent', - errorType: 'agent_turn_accepted_discarded_on_generation_change', + errorType: 'failure_tracking_agent_accepted_turn_generation_change', context: { threadAtSend: 'new', ackThreadId: 'th-1', diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 46c03f3e58a..7c69db71cc8 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -1674,19 +1674,11 @@ export function useAgentSession(deps: AgentSessionDeps) { ) const isCurrent = generation === loadGeneration if (!isCurrent) { - // PM-1975: the server admitted this turn (ack received), but the user - // switched chats/started a new chat before the ack landed, bumping - // loadGeneration. acceptTurn() below never runs, so the turn gets no - // local bookkeeping (no activeTurnId, nothing to stash) even though the - // server keeps running it -- it was previously dropped with no visible - // trace. agentSendGateStore documents this as the known-but-unclosed - // half of PM-1660: "stashActiveTurn() has no slot to stash before the - // ack and performSend's generation check drops the late one." reportError( new Error('agent turn accepted after the send generation moved on'), { surface: 'agent', - errorType: 'agent_turn_accepted_discarded_on_generation_change', + errorType: 'failure_tracking_agent_accepted_turn_generation_change', context: { threadAtSend, ackThreadId: ack.thread_id,