From 943b07b2d99f92e4bcdbd4ef96aa70ddfddfc68c Mon Sep 17 00:00:00 2001 From: glary-bot Date: Sat, 26 Sep 2026 03:09:05 +0000 Subject: [PATCH 01/36] fix(agent): restore an approval ask the socket drop swallowed (PM-1738) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A turn parked on a run approval stays `streaming` until the user answers, so a socket drop that swallowed the `agent_ask` frame left the server waiting on a card the panel never drew: the turn read as hung and every follow-up message came back 409, recoverable only by reloading the tab. Turn recovery already polls the persisted row, and the row carries the unanswered ask in `pending_ask` — the same payload the lost frame held. Re-deliver it through the transport path the frame would have taken, and report it, since an ask that did arrive is already on the message. --- .../fixtures/agentTurnLockFixture.ts | 40 +++++- .../agentApprovalSurvivesSocketDrop.spec.ts | 115 ++++++++++++++++++ .../composables/agent/useAgentSession.test.ts | 109 +++++++++++++++++ .../composables/agent/useAgentSession.ts | 52 +++++++- .../agent/agentConversationStore.test.ts | 45 +++++++ .../stores/agent/agentConversationStore.ts | 30 +++++ 6 files changed, 386 insertions(+), 5 deletions(-) create mode 100644 browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts index 6fc191088d7..b37d19c9eb1 100644 --- a/browser_tests/fixtures/agentTurnLockFixture.ts +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -93,6 +93,12 @@ const FOREIGN_TURN_DONE_EVENT: AgentWsEvent = { const RUN_APPROVAL_ASK_ID = `${TURN_ID}:call-run-workflow` +/** + * The workflow this ask names. Exported so a spec can assert the card is the + * one the ask described without restating the literal. + */ +export const APPROVAL_WORKFLOW_NAME = 'Unsaved Workflow' + /** * The option ids this ask offers. Declared separately because `AgentWsEvent` * types `data` loosely, so reading them back off the event would be an @@ -100,6 +106,9 @@ const RUN_APPROVAL_ASK_ID = `${TURN_ID}:call-run-workflow` */ const RUN_APPROVAL_OPTION_IDS = ['run', 'cancel'] as const +/** The option id the panel's Run button answers with. */ +export const APPROVAL_RUN_OPTION_ID = RUN_APPROVAL_OPTION_IDS[0] + /** * The frame the server sends when a turn parks waiting for the user to approve * a run. Shaped after `pendingRunApproval`'s reader and cloud's `asks` writer: @@ -113,8 +122,11 @@ export const RUN_APPROVAL_EVENT: AgentWsEvent = { thread_id: THREAD_ID, ask_id: RUN_APPROVAL_ASK_ID, kind: 'run_approval', - prompt: 'Run workflow “Unsaved Workflow”?', - context: { workflow_id: WORKFLOW_ID, workflow_name: 'Unsaved Workflow' }, + prompt: `Run workflow “${APPROVAL_WORKFLOW_NAME}”?`, + context: { + workflow_id: WORKFLOW_ID, + workflow_name: APPROVAL_WORKFLOW_NAME + }, options: [ { id: RUN_APPROVAL_OPTION_IDS[0], @@ -506,6 +518,7 @@ export class AgentTurnLockHarness { public readonly sendButton: Locator public readonly stopButton: Locator public readonly runApprovalButton: Locator + public readonly approvalCard: Locator public readonly workSummary: Locator public readonly workingRow: Locator public readonly liveProgressRow: Locator @@ -538,6 +551,14 @@ export class AgentTurnLockHarness { name: enMessages.agent.runApproval.run, exact: true }) + // RunApprovalCard.vue has no landmark of its own, and its lead line varies + // with whether the workflow name is shown. The question line is the one + // element every variant renders exactly once per card, so counting it + // counts cards -- which is what the no-duplicate-card assertion needs. + this.approvalCard = this.panel.getByText( + enMessages.agent.runApproval.question, + { exact: true } + ) // WorkSummary.vue renders three labels off the elapsed total: `worked` // alone, `workedForSeconds`, or `workedForMinutes`. Anchoring on the // shared `worked` stem matches all three, so the negative assertions on @@ -661,6 +682,21 @@ export class AgentTurnLockHarness { this.server.recordAsk(event) } + /** + * Parks the live turn on a run-approval ask the way the service does while + * it waits for the user: the assistant row stays `streaming` (so posts keep + * returning 409) and starts carrying `pending_ask`. No frame is pushed -- + * that models the `agent_ask` a dropped socket never delivered. + */ + parkOnApproval(): void { + this.primePendingAsk(RUN_APPROVAL_EVENT) + } + + /** The frame the server pushes once it accepts an answer to the approval. */ + resolveApproval(ws: WebSocketRoute): void { + this.push(ws, RUN_APPROVAL_RESOLVED_EVENT) + } + /** True only when a test explicitly primed transcript-based recovery. */ pendingAskIsPrimed(): boolean { return this.server.askIsPending diff --git a/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts new file mode 100644 index 00000000000..8b2e9f14c2e --- /dev/null +++ b/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts @@ -0,0 +1,115 @@ +import { expect } from '@playwright/test' + +import { + APPROVAL_RUN_OPTION_ID, + APPROVAL_WORKFLOW_NAME, + RUN_APPROVAL_EVENT, + TURN_IN_PROGRESS_MESSAGE, + agentTurnLockTest as test +} from '@e2e/fixtures/agentTurnLockFixture' + +// PM-1738, filed under PM-1199. Reported from the agent panel while building an +// img2img and then img2video workflow: "after generate workflow, agent stopped, +// I couldn't run but need to restart agent tab. Session was remained so that I +// could run again with existing session." +// +// The service side was never hung. Its logs show the turn accepted, then +// twenty-three minutes of silence, then two posts answered 409, then a GET of +// the transcript (the tab restart) and an `asks/.../answer` four seconds later +// — the user approving a tool call the moment a reload finally drew it. A turn +// parked on a run approval stays `streaming` for as long as the user takes to +// answer, so the socket drop that swallowed the `agent_ask` frame left the +// server waiting on a card the panel never drew: no way forward, and every +// follow-up message rejected by the single-active-turn guard. +// +// Distinct from agentTurnSurvivesSocketDrop.spec.ts, which drops the socket +// around a turn the server finishes on its own. Here the server cannot finish +// without an answer, so keeping the turn live is not enough — the ask itself +// has to come back. +test.describe.configure({ timeout: 120_000 }) +test.use({ connectWebSocketToServer: false }) + +test.describe( + 'Agent approval ask across a transient websocket reconnect', + { tag: ['@cloud', '@ui'] }, + () => { + const PROMPT = 'build an img2img workflow and then turn it into img2video' + + test.beforeEach(async ({ turnLock }) => { + await turnLock.openOnBlankWorkflow() + await turnLock.startTurn(PROMPT) + }) + + test('surfaces an approval whose frame the drop swallowed, and resumes on it', async ({ + turnLock + }) => { + const nextPrompt = 'now raise the video length to 48 frames' + + const reconnected = + await test.step('the turn parks on an approval whose frame never arrives', async () => { + turnLock.parkOnApproval() + await expect(turnLock.approvalCard).toHaveCount(0) + return turnLock.dropSocket() + }) + + await test.step('the approval surfaces without a reload', async () => { + await expect(turnLock.approvalCard).toBeVisible({ timeout: 30_000 }) + await expect( + turnLock.panel.getByText(APPROVAL_WORKFLOW_NAME) + ).toBeVisible() + await expect(turnLock.stopButton).toBeVisible() + }) + + await test.step('answering it reaches the server', async () => { + await turnLock.runApprovalButton.click() + + // The fake's answer route 404s any ask id but the parked one, so a + // recorded answer proves the panel answered the ask it restored -- + // and this reads the server, not the DOM, so it proves the answer + // actually left the client. + await expect + .poll(() => turnLock.answeredAsks()) + .toEqual([APPROVAL_RUN_OPTION_ID]) + + turnLock.resolveApproval(reconnected) + await expect(turnLock.approvalCard).toHaveCount(0) + }) + + await test.step('the thread is usable again', async () => { + turnLock.finishTurn(reconnected) + await expect(turnLock.sendButton).toBeVisible() + + await turnLock.composer.fill(nextPrompt) + await turnLock.sendButton.click() + + await expect(turnLock.userBubbles).toHaveCount(2) + await expect(turnLock.userBubbles.last()).toHaveText(nextPrompt) + await expect( + turnLock.panel + .getByRole('alert') + .filter({ hasText: TURN_IN_PROGRESS_MESSAGE }) + ).toHaveCount(0) + expect(turnLock.rejectedPosts()).toBe(0) + }) + }) + + // The recovery path re-delivers from the persisted row on every poll, so + // without a guard an ask the socket did deliver would be drawn twice. + test('does not redraw an approval the socket already delivered', async ({ + turnLock, + getWebSocket + }) => { + turnLock.parkOnApproval() + turnLock.push(await getWebSocket(), RUN_APPROVAL_EVENT) + await expect(turnLock.approvalCard).toBeVisible() + + await turnLock.dropSocket() + + await expect(turnLock.approvalCard).toBeVisible() + await expect(turnLock.runApprovalButton).toHaveCount(1) + // Outlasts the first two recovery polls (0s, then 1s), so a duplicate + // card drawn by a later poll cannot slip past a one-shot assertion. + await expect(turnLock.approvalCard).toHaveCount(1, { timeout: 5_000 }) + }) + } +) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 5fe7c5c0787..9721abdd156 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4338,6 +4338,115 @@ describe('useAgentSession (v1 composition root)', () => { expect(conversationStore.liveTurns()).toHaveLength(1) }) + // PM-1738. A turn parked on a run approval stays `streaming` for as long as + // the user takes to answer, so the drop that swallowed its `agent_ask` frame + // leaves the server waiting on a card the panel never drew. Recovery already + // polls the row, and the row carries the unanswered ask. + const parkedOnApprovalRest = () => + fakeRest({ + getMessages: vi.fn( + async (): Promise => [ + historyRow(1, 'user', 'msg-1', 'go'), + { + ...historyRow(2, 'assistant', 'msg-1', '', 'msg-1'), + content: {}, + status: 'streaming', + pending_ask: { + message_id: 'msg-1', + ask_id: 'turn-1:call-1', + kind: 'run_approval', + context: { + workflow_id: 'workflow-1', + workflow_name: 'Portrait workflow' + }, + prompt: 'Run it?', + options: [ + { id: 'run', label: 'Run' }, + { id: 'cancel', label: 'Cancel' } + ], + min_selections: 1, + max_selections: 1, + allow_other: false + } + } + ] + ) + }) + + const approvalParts = (session: ReturnType) => { + const assistant = session.entries.value.at(-1) + assert(assistant !== undefined && 'parts' in assistant) + return assistant.parts.filter((part) => part.type === 'runApproval') + } + + it('(g27) an approval the drop swallowed is restored from the row and reported once', async () => { + vi.useFakeTimers() + try { + const rest = parkedOnApprovalRest() + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + + await session.sendMessage('go') + emit(delta('msg-1', 'partial')) + expect(approvalParts(session)).toHaveLength(0) + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(0) + + expect(approvalParts(session)).toEqual([ + { + type: 'runApproval', + askId: 'turn-1:call-1', + workflowId: 'workflow-1', + workflowName: 'Portrait workflow' + } + ]) + expect(session.isStreaming.value).toBe(true) + + await vi.advanceTimersByTimeAsync(31_000) + + expect(approvalParts(session)).toHaveLength(1) + expect(reportError).toHaveBeenCalledExactlyOnceWith(expect.any(Error), { + errorType: 'failure_delivering_agent_approval_ask', + tags: { feature_area: 'agent', operation: 'recovery' }, + context: { + threadId: 'th-1', + messageId: 'msg-1', + askId: 'turn-1:call-1' + } + }) + } finally { + vi.useRealTimers() + } + }) + + it('(g28) an approval the socket did deliver is neither redrawn nor reported', async () => { + vi.useFakeTimers() + try { + const rest = parkedOnApprovalRest() + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + + await session.sendMessage('go') + emit(runApproval('msg-1')) + expect(approvalParts(session)).toHaveLength(1) + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(31_000) + + expect(approvalParts(session)).toHaveLength(1) + expect(reportError).not.toHaveBeenCalled() + } finally { + vi.useRealTimers() + } + }) + it('(h) attachments pass through to the postMessage wire body', async () => { const rest = fakeRest() const { source } = fakeEvents() diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index ba9c4dc79ba..689c20e8f2a 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -178,10 +178,12 @@ const TURN_RECOVERY_DELAYS_MS = [0, 1000, 2000, 4000, 8000, 16000] /** Upper bound on one recovery job, including any history fetch still in flight. */ const TURN_RECOVERY_DEADLINE_MS = 60_000 +type PendingAsk = NonNullable + type TurnOutcome = | { kind: 'terminal'; parts: AssistantMessage['parts'] | undefined } | { kind: 'thread-missing' } - | { kind: 'streaming' } + | { kind: 'streaming'; pendingAsk: PendingAsk | undefined } | { kind: 'error'; message: string } function isTerminalTurnStatus( @@ -498,6 +500,7 @@ export function useAgentSession(deps: AgentSessionDeps) { let ownedGeneration = 0 let connection: SocketConnection = 'initial' const recoveringTurns = new Map() + const reportedMissingAsks = new Set() function pushError(text: string): void { notices.value.push({ level: 'error', text }) @@ -1837,9 +1840,43 @@ export function useAgentSession(deps: AgentSessionDeps) { if (outcome.kind === 'thread-missing' && consecutiveThreadMissing < 2) continue if (settleFinishedTurn(turn, outcome)) return + if (outcome.kind === 'streaming') + restoreMissingApproval(turn, outcome.pendingAsk) } } + /** + * PM-1738: a turn parked on a tool-call approval stays `streaming` until the + * user answers, so a socket drop that swallowed the `agent_ask` frame leaves + * the server waiting on a card the panel never drew -- the turn reads as + * hung and every follow-up post comes back 409. The polled row still carries + * the unanswered ask, so re-deliver it down the transport path the lost + * frame would have taken. An ask that did arrive is already on the message, + * so reaching the report means a frame was genuinely lost. + */ + function restoreMissingApproval( + turn: LiveTurn, + pendingAsk: PendingAsk | undefined + ): void { + if (pendingAsk?.kind !== 'run_approval') return + if (conversationStore.isApprovalShown(turn, pendingAsk.ask_id)) return + conversationStore.ingest({ + type: 'agent_ask', + data: { ...pendingAsk, thread_id: turn.threadId } + }) + if (reportedMissingAsks.has(pendingAsk.ask_id)) return + reportedMissingAsks.add(pendingAsk.ask_id) + reportError(new Error('Agent approval ask never reached the panel'), { + errorType: 'failure_delivering_agent_approval_ask', + tags: { feature_area: 'agent', operation: 'recovery' }, + context: { + threadId: turn.threadId, + messageId: turn.messageId, + askId: pendingAsk.ask_id + } + }) + } + function isTurnLive(turn: LiveTurn, generation: number): boolean { return ( generation === sessionGeneration && @@ -1903,7 +1940,7 @@ export function useAgentSession(deps: AgentSessionDeps) { const anchor = history.find( (entry) => entry.role === 'assistant' && entry.id === turn.messageId ) - if (!anchor) return { kind: 'streaming' } + if (!anchor) return { kind: 'streaming', pendingAsk: undefined } const rows = history .filter( (entry) => @@ -1911,7 +1948,16 @@ export function useAgentSession(deps: AgentSessionDeps) { ) .sort((a, b) => a.seq - b.seq) if (rows.some((row) => !isTerminalTurnStatus(row.status))) - return { kind: 'streaming' } + // PM-1738: a turn parked on an approval persists the unanswered ask on + // whichever of its rows is still open, which is the last one to carry + // one -- not necessarily the anchor the turn is keyed by. + return { + kind: 'streaming', + pendingAsk: rows.reduce( + (found, row) => row.pending_ask ?? found, + undefined + ) + } return { kind: 'terminal', parts: terminalRecoveryParts(rows) diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index 135511c9bf6..bed15892905 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -1636,6 +1636,51 @@ describe('useAgentConversationStore', () => { expect(store.liveTurns()).toEqual([]) }) + it.for([ + { + name: 'the displayed turn', + turn: { threadId: 'th-front', messageId: T1 }, + askId: 'front-ask' + }, + { + name: 'a stashed background turn', + turn: { threadId: 'th-back', messageId: T2 }, + askId: 'back-ask' + } + ])( + 'reads back the approval showing on $name, and only that one', + ({ turn, askId }) => { + const store = useAgentConversationStore() + store.setThreadId('th-back') + store.startTurn(T2) + store.recordUser(T2, 'background prompt') + store.ingest(runApproval('t2', 'back-ask')) + store.stashActiveTurn() + store.setThreadId('th-front') + store.hydrate([]) + store.startTurn(T1) + store.recordUser(T1, 'front prompt') + store.ingest(runApproval('t1', 'front-ask')) + + expect(store.isApprovalShown(turn, askId)).toBe(true) + expect(store.isApprovalShown(turn, 'never-delivered')).toBe(false) + } + ) + + it('stops reporting an approval once its turn has settled', () => { + const store = useAgentConversationStore() + const turn = { threadId: 'th', messageId: T1 } + store.setThreadId('th') + store.startTurn(T1) + store.recordUser(T1, 'go') + store.ingest(runApproval('t1', 'ask-1')) + expect(store.isApprovalShown(turn, 'ask-1')).toBe(true) + + store.settleTurn(turn, 'persisted final') + + expect(store.isApprovalShown(turn, 'ask-1')).toBe(false) + }) + it.for([ { name: 'the displayed turn', diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index c9391f9a785..9bb89704520 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -1032,6 +1032,35 @@ export const useAgentConversationStore = defineStore( rememberDepartedTurn(slot.threadId, slot.turnId, reason) } + /** + * The mutable message a live turn is written into, not the snapshot copy + * in `messages`: transports emit snapshots, so only this object carries + * parts applied since the last emit. + */ + function liveTurnMessage(turn: LiveTurn): AssistantMessage | null { + const slot = activeSlot.value + if ( + slot !== null && + turn.threadId === slot.threadId && + turn.messageId === slot.turnId + ) + return slot.message + const entry = backgroundTurns.get(turn.messageId) + if (!entry || entry.messageId !== turn.messageId || entry.settled) + return null + return entry.message + } + + /** Guards re-delivery: a delayed ask must not draw a second card. */ + function isApprovalShown(turn: LiveTurn, askId: string): boolean { + const message = liveTurnMessage(turn) + return ( + message?.parts.some( + (part) => part.type === 'runApproval' && part.askId === askId + ) ?? false + ) + } + function clearActive(): void { activeSlot.value = null } @@ -1307,6 +1336,7 @@ export const useAgentConversationStore = defineStore( turnUsesSkill, historySkillNames, settleTurn, + isApprovalShown, reset, hydrate } From 7ea3070e386b1c85f2ed62b7eb03fcb8128b8898 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 1 Oct 2026 15:34:44 -0700 Subject: [PATCH 02/36] fix(agent): keep a restored approval single and never resurrect an answered one Carries the remaining four commits of this branch onto the rebased `fix/agent-reconcile-hydrated-streaming-turn` base, where they would not replay: each was written against the recovery loop as it stood before on the same files. What they add is unchanged: - `deliveredAsks` ledger, so a poll whose snapshot predates the answer cannot redraw a card the user is done with. - `handleAskEvent` drops an `agent_ask` whose `askId` is already on the message, since the server writes the ask row before publishing the frame and the two can arrive in either order. - Inbound asks route through `deliverAsk`, which only ledgers a frame once its card is actually on screen. - The report separates a frame the socket really lost (`error`, after a grace window for a late broadcast) from one a hydrate merely raced (`warning`), tagged with `recovery_cause`. Adapted where the base had moved on: the cause rides as a `RecoveryCause` rather than a second delay schedule, because the base now has one schedule; `(g31)`/`(g35)` assert the same hydrate-vs-reconnect outcomes against that single schedule; and the report carries the `surface` the current `reportError` contract requires. Unit: 2555 passed across src/workbench/extensions/agent. Typecheck, lint and oxfmt clean. --- .../fixtures/agentTurnLockFixture.ts | 12 + .../agentApprovalSurvivesSocketDrop.spec.ts | 22 +- .../composables/agent/useAgentSession.test.ts | 301 ++++++++++++++++-- .../composables/agent/useAgentSession.ts | 192 +++++++++-- .../services/agent/agentEventTransport.ts | 12 + .../agent/agentConversationStore.test.ts | 13 +- .../stores/agent/agentConversationStore.ts | 8 +- 7 files changed, 496 insertions(+), 64 deletions(-) diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts index b37d19c9eb1..97563406485 100644 --- a/browser_tests/fixtures/agentTurnLockFixture.ts +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -189,6 +189,7 @@ class TurnLockServer { private foreignPrompt: string | undefined private rejected = 0 private posts = 0 + private transcripts = 0 private readonly answered: string[][] = [] private pendingAsk: AgentPendingAsk | undefined private heldTranscript: @@ -214,6 +215,11 @@ class TurnLockServer { return this.posts } + /** Every transcript GET served, so a spec can wait out a recovery poll. */ + get transcriptFetches(): number { + return this.transcripts + } + countPost(): void { this.posts++ } @@ -272,6 +278,7 @@ class TurnLockServer { } transcript(threadId = THREAD_ID): AgentMessage[] { + this.transcripts++ if (threadId === OTHER_THREAD_ID) return [ { @@ -600,6 +607,11 @@ export class AgentTurnLockHarness { return this.server.postAttempts } + /** Every transcript GET served, so a spec can wait out a recovery poll. */ + transcriptFetches(): number { + return this.server.transcriptFetches + } + /** Opens the panel on a blank workflow and points the composer at that tab. */ async openOnBlankWorkflow(): Promise { await expect( diff --git a/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts index 8b2e9f14c2e..7e577f783ae 100644 --- a/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts +++ b/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts @@ -82,6 +82,11 @@ test.describe( await turnLock.composer.fill(nextPrompt) await turnLock.sendButton.click() + // The post has to have actually reached the server before the absence + // of a turn-in-progress notice means anything. + await expect + .poll(() => turnLock.postAttempts()) + .toBeGreaterThanOrEqual(2) await expect(turnLock.userBubbles).toHaveCount(2) await expect(turnLock.userBubbles.last()).toHaveText(nextPrompt) await expect( @@ -93,8 +98,9 @@ test.describe( }) }) - // The recovery path re-delivers from the persisted row on every poll, so - // without a guard an ask the socket did deliver would be drawn twice. + // Recovery re-reads the persisted row on every poll, so an ask the socket + // did deliver has to be recognised as already on screen rather than + // restored again. test('does not redraw an approval the socket already delivered', async ({ turnLock, getWebSocket @@ -102,14 +108,18 @@ test.describe( turnLock.parkOnApproval() turnLock.push(await getWebSocket(), RUN_APPROVAL_EVENT) await expect(turnLock.approvalCard).toBeVisible() + const pollsBefore = turnLock.transcriptFetches() await turnLock.dropSocket() - await expect(turnLock.approvalCard).toBeVisible() + // toHaveCount returns as soon as it passes, so the count alone would + // assert nothing before recovery has even polled. Wait for two polls to + // have actually served the parked row, then hold the panel to one card. + await expect + .poll(() => turnLock.transcriptFetches(), { timeout: 30_000 }) + .toBeGreaterThanOrEqual(pollsBefore + 2) + await expect(turnLock.approvalCard).toHaveCount(1) await expect(turnLock.runApprovalButton).toHaveCount(1) - // Outlasts the first two recovery polls (0s, then 1s), so a duplicate - // card drawn by a later poll cannot slip past a one-shot assertion. - await expect(turnLock.approvalCard).toHaveCount(1, { timeout: 5_000 }) }) } ) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 9721abdd156..bde58715f88 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4342,33 +4342,43 @@ describe('useAgentSession (v1 composition root)', () => { // the user takes to answer, so the drop that swallowed its `agent_ask` frame // leaves the server waiting on a card the panel never drew. Recovery already // polls the row, and the row carries the unanswered ask. + const PARKED_ASK = { + message_id: 'msg-1', + ask_id: 'turn-1:call-1', + kind: 'run_approval', + context: { + workflow_id: 'workflow-1', + workflow_name: 'Portrait workflow' + }, + prompt: 'Run it?', + options: [ + { id: 'run', label: 'Run' }, + { id: 'cancel', label: 'Cancel' } + ], + min_selections: 1, + max_selections: 1, + allow_other: false + } + + const parkedRow = (): AgentMessages[number] => ({ + ...historyRow(2, 'assistant', 'msg-1', '', 'msg-1'), + content: {}, + status: 'streaming', + pending_ask: PARKED_ASK + }) + + const unparkedRow = (): AgentMessages[number] => ({ + ...historyRow(2, 'assistant', 'msg-1', '', 'msg-1'), + content: {}, + status: 'streaming' + }) + const parkedOnApprovalRest = () => fakeRest({ getMessages: vi.fn( async (): Promise => [ historyRow(1, 'user', 'msg-1', 'go'), - { - ...historyRow(2, 'assistant', 'msg-1', '', 'msg-1'), - content: {}, - status: 'streaming', - pending_ask: { - message_id: 'msg-1', - ask_id: 'turn-1:call-1', - kind: 'run_approval', - context: { - workflow_id: 'workflow-1', - workflow_name: 'Portrait workflow' - }, - prompt: 'Run it?', - options: [ - { id: 'run', label: 'Run' }, - { id: 'cancel', label: 'Cancel' } - ], - min_selections: 1, - max_selections: 1, - allow_other: false - } - } + parkedRow() ] ) }) @@ -4410,8 +4420,14 @@ describe('useAgentSession (v1 composition root)', () => { expect(approvalParts(session)).toHaveLength(1) expect(reportError).toHaveBeenCalledExactlyOnceWith(expect.any(Error), { + surface: 'agent', errorType: 'failure_delivering_agent_approval_ask', - tags: { feature_area: 'agent', operation: 'recovery' }, + level: 'error', + tags: { + feature_area: 'agent', + operation: 'recovery', + recovery_cause: 'reconnect' + }, context: { threadId: 'th-1', messageId: 'msg-1', @@ -4447,6 +4463,245 @@ describe('useAgentSession (v1 composition root)', () => { } }) + it('(g29) a poll whose snapshot predates the answer does not resurrect the card', async () => { + vi.useFakeTimers() + try { + const rest = parkedOnApprovalRest() + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + + await session.sendMessage('go') + emit(runApproval('msg-1')) + status(false) + status(true) + await vi.advanceTimersByTimeAsync(0) + expect(approvalParts(session)).toHaveLength(1) + + emit(askResolved('msg-1')) + expect(approvalParts(session)).toHaveLength(0) + + await vi.advanceTimersByTimeAsync(31_000) + + expect(approvalParts(session)).toHaveLength(0) + expect(reportError).not.toHaveBeenCalled() + } finally { + vi.useRealTimers() + } + }) + + it('(g30) a broadcast that merely lagged the poll renders one card and is not reported', async () => { + vi.useFakeTimers() + try { + const rest = parkedOnApprovalRest() + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + + await session.sendMessage('go') + status(false) + status(true) + await vi.advanceTimersByTimeAsync(0) + expect(approvalParts(session)).toHaveLength(1) + + emit(runApproval('msg-1')) + await vi.advanceTimersByTimeAsync(31_000) + + expect(approvalParts(session)).toHaveLength(1) + expect(reportError).not.toHaveBeenCalled() + } finally { + vi.useRealTimers() + } + }) + + it('(g32) an ask answered elsewhere is not resurrected by a stale poll', async () => { + vi.useFakeTimers() + try { + const rest = parkedOnApprovalRest() + vi.mocked(rest.answerAsk).mockRejectedValue( + new AgentApiError('already answered', 409, undefined) + ) + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + + await session.sendMessage('go') + emit(runApproval('msg-1')) + await session.answerAsk('turn-1:call-1', 'run') + expect(approvalParts(session)).toHaveLength(0) + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(31_000) + + expect(approvalParts(session)).toHaveLength(0) + expect(reportError).not.toHaveBeenCalled() + } finally { + vi.useRealTimers() + } + }) + + // The 409 branch resolves the ask straight on the store, so the ledger only + // learns of a hydrated card's ask from `answerAsk` itself. Without that the + // next poll, whose row still carries the ask the server has not cleared yet, + // would draw the card back over a reply the user had already released. + it('(g33) answering a hydrated ask keeps a stale poll from drawing it again', async () => { + vi.useFakeTimers() + try { + const rest = fakeRest({ + getMessages: vi.fn( + async (): Promise => [ + historyRow(1, 'user', 'msg-1', 'go'), + parkedRow() + ] + ) + }) + vi.mocked(rest.answerAsk).mockRejectedValue( + new AgentApiError('already answered', 409, undefined) + ) + const { source, status } = fakeEvents() + localStorage.setItem(StorageKeys.agentThread('personal'), 'th-1') + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + await vi.advanceTimersByTimeAsync(0) + expect(approvalParts(session)).toHaveLength(1) + + await session.answerAsk('turn-1:call-1', 'run') + expect(approvalParts(session)).toHaveLength(0) + + await vi.advanceTimersByTimeAsync(31_000) + + expect(approvalParts(session)).toHaveLength(0) + expect(reportError).not.toHaveBeenCalled() + } finally { + vi.useRealTimers() + } + }) + + // A frame published while the first history fetch is still in flight has no + // turn in memory to reach: `ingest` drops it. Recovery is the only thing + // left that can draw the card, so the drop must not be recorded as a + // delivery. + it('(g34) a frame the store could not route yet is still restored by recovery', async () => { + vi.useFakeTimers() + try { + let releaseHistory = (): void => {} + const history = new Promise((resolve) => { + releaseHistory = resolve + }) + let served = 0 + const rest = fakeRest({ + getMessages: vi.fn(async (): Promise => { + // The first response is the one that predates the ask row, so + // hydration has nothing to draw the card from. + if (served++ === 0) { + await history + return [historyRow(1, 'user', 'msg-1', 'go'), unparkedRow()] + } + return [historyRow(1, 'user', 'msg-1', 'go'), parkedRow()] + }) + }) + const { source, emit, status } = fakeEvents() + localStorage.setItem(StorageKeys.agentThread('personal'), 'th-1') + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + + emit(runApproval('msg-1')) + expect(session.entries.value).toHaveLength(0) + + releaseHistory() + await vi.advanceTimersByTimeAsync(31_000) + + expect(approvalParts(session)).toHaveLength(1) + } finally { + vi.useRealTimers() + } + }) + + it('(g35) a drop while a hydrate job is polling files the ask as a lost frame', async () => { + vi.useFakeTimers() + try { + // The row parks only once the socket is down, so the hydrate job's own + // polls see nothing and the restore can only come from the reconnect + // job that takes it over. Keyed on the drop rather than on a call count + // because hydrate and its first poll share the 0ms head of the schedule. + let parked = false + const rest = fakeRest({ + getMessages: vi.fn( + async (): Promise => [ + historyRow(1, 'user', 'msg-1', 'go'), + parked ? parkedRow() : unparkedRow() + ] + ) + }) + const { source, status } = fakeEvents() + localStorage.setItem(StorageKeys.agentThread('personal'), 'th-1') + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + await vi.advanceTimersByTimeAsync(0) + expect(approvalParts(session)).toHaveLength(0) + + parked = true + status(false) + status(true) + await vi.advanceTimersByTimeAsync(31_000) + + expect(approvalParts(session)).toHaveLength(1) + expect(reportError).toHaveBeenCalledExactlyOnceWith( + expect.any(Error), + expect.objectContaining({ + level: 'error', + tags: expect.objectContaining({ recovery_cause: 'reconnect' }) + }) + ) + } finally { + vi.useRealTimers() + } + }) + + it('(g31) an ask the server parks during a hydrate fetch is restored as a warning', async () => { + vi.useFakeTimers() + try { + const getMessages = vi + .fn<() => Promise>() + .mockResolvedValueOnce([ + historyRow(1, 'user', 'msg-1', 'go'), + unparkedRow() + ]) + .mockResolvedValue([historyRow(1, 'user', 'msg-1', 'go'), parkedRow()]) + const rest = fakeRest({ getMessages }) + const { source, status } = fakeEvents() + localStorage.setItem(StorageKeys.agentThread('personal'), 'th-1') + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + + // Hydrate and its first recovery poll share the 0ms head of the + // schedule, so the restore lands in the same tick as the hydrate that + // missed the ask -- and it is filed as the benign publish race, not as + // a lost frame, because no socket ever dropped here. + await vi.advanceTimersByTimeAsync(0) + + expect(approvalParts(session)).toHaveLength(1) + expect(reportError).toHaveBeenCalledExactlyOnceWith( + expect.any(Error), + expect.objectContaining({ + errorType: 'failure_delivering_agent_approval_ask', + level: 'warning', + tags: expect.objectContaining({ recovery_cause: 'hydrate' }) + }) + ) + } finally { + vi.useRealTimers() + } + }) + it('(h) attachments pass through to the postMessage wire body', async () => { const rest = fakeRest() const { source } = fakeEvents() diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 689c20e8f2a..49b025bc49d 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -177,6 +177,15 @@ const MAX_HYDRATION_MAILBOXES = 32 const TURN_RECOVERY_DELAYS_MS = [0, 1000, 2000, 4000, 8000, 16000] /** Upper bound on one recovery job, including any history fetch still in flight. */ const TURN_RECOVERY_DEADLINE_MS = 60_000 +/** How long a restored ask waits for its own broadcast before it counts as lost. */ +const LATE_ASK_FRAME_GRACE_MS = 2000 + +/** + * Why a recovery job is running, which is what separates an ask the socket + * genuinely dropped from one that merely had not been published when a + * hydrate's history fetch went out. Only the former is a defect. + */ +type RecoveryCause = 'reconnect' | 'hydrate' type PendingAsk = NonNullable @@ -499,8 +508,21 @@ export function useAgentSession(deps: AgentSessionDeps) { let unsubscribeStatus: (() => void) | null = null let ownedGeneration = 0 let connection: SocketConnection = 'initial' - const recoveringTurns = new Map() - const reportedMissingAsks = new Set() + interface RunningRecovery { + controller: AbortController + cause: RecoveryCause + } + const recoveringTurns = new Map() + /** + * Asks recovery must not re-deliver: one it already restored, one a frame + * has delivered, and any the user has answered or the server has resolved. + * Without the latter, a poll whose snapshot predates the answer would + * resurrect a card the user is done with, splitting the reply that has + * since resumed streaming. Deliberately outlives `newChat`/`loadThread`: + * an `ask_id` carries its `message_id`, so it is never reused. + */ + const deliveredAsks = new Set() + const lateAskReports = new Map>() function pushError(text: string): void { notices.value.push({ level: 'error', text }) @@ -794,7 +816,7 @@ export function useAgentSession(deps: AgentSessionDeps) { if (conversationStore.threadId !== threadId || !isCurrent()) return false conversationStore.hydrate(history) rememberSnapshotTurn(conversationStore.activeTurnId) - reconcileLiveTurns() + reconcileLiveTurns('hydrate') readyThreadId.value = threadId drainHydration(buffer) const workflowReady = await workflow?.restored?.( @@ -844,8 +866,10 @@ export function useAgentSession(deps: AgentSessionDeps) { unsubscribeStatus?.() unsubscribe = null unsubscribeStatus = null - for (const recovery of recoveringTurns.values()) recovery.abort() + for (const running of recoveringTurns.values()) running.controller.abort() recoveringTurns.clear() + for (const scheduled of lateAskReports.values()) clearTimeout(scheduled) + lateAskReports.clear() const stoppedGeneration = ownedGeneration queueMicrotask(() => { if (stoppedGeneration !== sessionGeneration) return @@ -1456,6 +1480,7 @@ export function useAgentSession(deps: AgentSessionDeps) { conversationStore.setAskAnswering(askId, true) try { await sendAnswer(currentThreadId, askId, selection) + deliveredAsks.add(askId) conversationStore.commitAsk(askId, currentThreadId) return true } catch (error) { @@ -1486,6 +1511,10 @@ export function useAgentSession(deps: AgentSessionDeps) { ) pushError(i18n.global.t('agent.runApproval.answerFailed')) } + // The card is retired for good here, by resolution (409) or because + // this client could never answer it. Either way a later recovery poll + // must not draw it again. + deliveredAsks.add(askId) conversationStore.retireAsk(askId, currentThreadId) return false } @@ -1752,6 +1781,7 @@ export function useAgentSession(deps: AgentSessionDeps) { // owning turn's transport, and the turn is gone in exactly the case that // matters, so on its own it would re-enable a card it cannot remove. conversationStore.retireAsk(event.data.ask_id, event.data.thread_id) + deliveredAsks.add(event.data.ask_id) onAskResolved?.(event.data.ask_id) } for (const turn of conversationStore.liveTurns()) observeSkillTurn(turn) @@ -1761,6 +1791,11 @@ export function useAgentSession(deps: AgentSessionDeps) { messageId: toTurnId(event.data.message_id) }) } + if (event.type === 'agent_ask') { + deliverAsk(event) + for (const turn of conversationStore.liveTurns()) observeSkillTurn(turn) + return + } conversationStore.ingest(event) for (const turn of conversationStore.liveTurns()) observeSkillTurn(turn) if (event.type === 'agent_active_tab') handleActiveTab(event) @@ -1787,27 +1822,36 @@ export function useAgentSession(deps: AgentSessionDeps) { const reconnected = connection === 'dropped' connection = 'live' if (!reconnected) return - reconcileLiveTurns() + reconcileLiveTurns('reconnect') } - function reconcileLiveTurns(): void { - const turns = conversationStore - .liveTurns() - .filter((turn) => !recoveringTurns.has(recoveryKey(turn))) - for (const turn of turns) void reconcileTurn(turn) + function reconcileLiveTurns(cause: RecoveryCause): void { + for (const turn of conversationStore.liveTurns()) + void reconcileTurn(turn, cause) } - async function reconcileTurn(turn: LiveTurn): Promise { + async function reconcileTurn( + turn: LiveTurn, + cause: RecoveryCause + ): Promise { observeSkillTurn(turn) const key = recoveryKey(turn) + const running = recoveringTurns.get(key) + // A hydrate job files its ask reports as the benign publish race. Once the + // socket has actually dropped that reading is wrong, so a reconnect takes + // the job over; anything else defers to the job already running. + if (running) { + if (cause !== 'reconnect' || running.cause === 'reconnect') return + running.controller.abort() + } const recovery = new AbortController() - recoveringTurns.set(key, recovery) + recoveringTurns.set(key, { controller: recovery, cause }) const deadline = setTimeout( () => recovery.abort(), TURN_RECOVERY_DEADLINE_MS ) try { - await recoverTurn(turn, ownedGeneration, recovery.signal) + await recoverTurn(turn, cause, ownedGeneration, recovery.signal) } catch (error) { // `onStatus` floats this job (`void reconcileTurn(turn)`), so a rethrow // would land as an `unhandledrejection` the session never sees. Abort is @@ -1820,12 +1864,14 @@ export function useAgentSession(deps: AgentSessionDeps) { }) } finally { clearTimeout(deadline) - if (recoveringTurns.get(key) === recovery) recoveringTurns.delete(key) + if (recoveringTurns.get(key)?.controller === recovery) + recoveringTurns.delete(key) } } async function recoverTurn( turn: LiveTurn, + cause: RecoveryCause, generation: number, signal: AbortSignal ): Promise { @@ -1841,7 +1887,7 @@ export function useAgentSession(deps: AgentSessionDeps) { continue if (settleFinishedTurn(turn, outcome)) return if (outcome.kind === 'streaming') - restoreMissingApproval(turn, outcome.pendingAsk) + restoreMissingApproval(turn, outcome.pendingAsk, cause) } } @@ -1850,31 +1896,117 @@ export function useAgentSession(deps: AgentSessionDeps) { * user answers, so a socket drop that swallowed the `agent_ask` frame leaves * the server waiting on a card the panel never drew -- the turn reads as * hung and every follow-up post comes back 409. The polled row still carries - * the unanswered ask, so re-deliver it down the transport path the lost - * frame would have taken. An ask that did arrive is already on the message, - * so reaching the report means a frame was genuinely lost. + * the unanswered ask, so re-deliver it through the store, which routes it to + * the same transport the lost frame would have reached, and mirror the one + * other thing the socket branch does with an ask. + * + * Reported at `error` only after a reconnect, where the frame really is + * gone. A hydrate's poll can instead be racing a broadcast the server had + * not published when its history fetch went out, so that reads as a warning. */ function restoreMissingApproval( turn: LiveTurn, - pendingAsk: PendingAsk | undefined + pendingAsk: PendingAsk | undefined, + cause: RecoveryCause ): void { if (pendingAsk?.kind !== 'run_approval') return + if (deliveredAsks.has(pendingAsk.ask_id)) return if (conversationStore.isApprovalShown(turn, pendingAsk.ask_id)) return + deliveredAsks.add(pendingAsk.ask_id) conversationStore.ingest({ type: 'agent_ask', - data: { ...pendingAsk, thread_id: turn.threadId } - }) - if (reportedMissingAsks.has(pendingAsk.ask_id)) return - reportedMissingAsks.add(pendingAsk.ask_id) - reportError(new Error('Agent approval ask never reached the panel'), { - errorType: 'failure_delivering_agent_approval_ask', - tags: { feature_area: 'agent', operation: 'recovery' }, - context: { - threadId: turn.threadId, - messageId: turn.messageId, - askId: pendingAsk.ask_id + data: { + ...pendingAsk, + thread_id: turn.threadId, + message_id: turn.messageId } }) + markStoppedTurnReady(turn) + reportRestoredApproval(turn, pendingAsk.ask_id, cause) + } + + /** + * The server writes the ask row before it publishes the frame, so a poll + * can restore an ask whose broadcast is merely in flight. Only a reconnect + * claims a frame was lost, and only once its own broadcast has had a grace + * window to turn up -- `withdrawLateAskReport` cancels this when it does. + * A hydrate races the same way but never had a dropped socket to blame, so + * it files as a warning immediately. + */ + function reportRestoredApproval( + turn: LiveTurn, + askId: string, + cause: RecoveryCause + ): void { + if (cause !== 'reconnect') { + sendRestoredApprovalReport(turn, askId, cause, 'warning') + return + } + lateAskReports.set( + askId, + setTimeout(() => { + lateAskReports.delete(askId) + sendRestoredApprovalReport(turn, askId, cause, 'error') + }, LATE_ASK_FRAME_GRACE_MS) + ) + } + + /** + * Routes an inbound `agent_ask`. One already in the ledger is an ask + * recovery has supplied, or one the user has finished with; dropping it on + * the id rather than on the rendered parts is what stops a frame delayed + * past its own resolution from drawing a card over a resumed reply. + * + * A frame is only ledgered once its card is actually on screen. `ingest` + * has nowhere to route one that arrives before its turn is in memory -- the + * window between subscribing and the first history fetch landing -- and + * ledgering a frame the store dropped would convince recovery the ask had + * been delivered, leaving the panel with no card at all. + */ + function deliverAsk( + event: Extract + ): void { + const askId = event.data.ask_id + if (deliveredAsks.has(askId)) { + withdrawLateAskReport(askId) + return + } + const turn = { + threadId: event.data.thread_id, + messageId: toTurnId(event.data.message_id) + } + conversationStore.ingest(event) + if (conversationStore.isApprovalShown(turn, askId)) deliveredAsks.add(askId) + markStoppedTurnReady(turn) + } + + function withdrawLateAskReport(askId: string): void { + const scheduled = lateAskReports.get(askId) + if (scheduled === undefined) return + clearTimeout(scheduled) + lateAskReports.delete(askId) + } + + function sendRestoredApprovalReport( + turn: LiveTurn, + askId: string, + cause: RecoveryCause, + level: 'error' | 'warning' + ): void { + reportError( + new Error('Agent approval ask was missing from the panel when polled'), + { + surface: 'agent', + errorType: 'failure_delivering_agent_approval_ask', + level, + tags: { + feature_area: 'agent', + operation: 'recovery', + recovery_cause: cause + }, + context: { threadId: turn.threadId, messageId: turn.messageId, askId } + } + ) } function isTurnLive(turn: LiveTurn, generation: number): boolean { diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts index c3ee91791a3..c1823e17758 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts @@ -334,6 +334,12 @@ export function createAgentEventTransport( * the server parks waiting for an answer — the same dead-panel outcome as an * ask dropped in routing, so it is reported the same way. Generated-contract * kinds without a client renderer are tagged separately from unknown input. + * + * An ask already on the message also returns `false`, and is NOT reported: + * turn recovery restores an unanswered ask off the persisted row, and the + * server writes that row before it publishes the frame, so the two can + * arrive in either order for the same `ask_id`. The second arrival is a + * duplicate the user already saw, not an undeliverable ask. */ function handleAskEvent(data: AgentAskEvent['data']): boolean { if (data.kind !== 'run_approval') { @@ -343,6 +349,12 @@ export function createAgentEventTransport( ) return false } + if ( + message.parts.some( + (part) => part.type === 'runApproval' && part.askId === data.ask_id + ) + ) + return false dropDraft() closeOpenText() closeOpenThinking() diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index bed15892905..3841e6019e7 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -1640,16 +1640,18 @@ describe('useAgentConversationStore', () => { { name: 'the displayed turn', turn: { threadId: 'th-front', messageId: T1 }, - askId: 'front-ask' + askId: 'front-ask', + otherAskId: 'back-ask' }, { name: 'a stashed background turn', turn: { threadId: 'th-back', messageId: T2 }, - askId: 'back-ask' + askId: 'back-ask', + otherAskId: 'front-ask' } ])( 'reads back the approval showing on $name, and only that one', - ({ turn, askId }) => { + ({ turn, askId, otherAskId }) => { const store = useAgentConversationStore() store.setThreadId('th-back') store.startTurn(T2) @@ -1663,6 +1665,7 @@ describe('useAgentConversationStore', () => { store.ingest(runApproval('t1', 'front-ask')) expect(store.isApprovalShown(turn, askId)).toBe(true) + expect(store.isApprovalShown(turn, otherAskId)).toBe(false) expect(store.isApprovalShown(turn, 'never-delivered')).toBe(false) } ) @@ -1676,7 +1679,9 @@ describe('useAgentConversationStore', () => { store.ingest(runApproval('t1', 'ask-1')) expect(store.isApprovalShown(turn, 'ask-1')).toBe(true) - store.settleTurn(turn, 'persisted final') + store.settleTurn(turn, [ + { type: 'text', text: 'persisted final', state: 'done' } + ]) expect(store.isApprovalShown(turn, 'ask-1')).toBe(false) }) diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 9bb89704520..2cdb7dce380 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -1051,7 +1051,13 @@ export const useAgentConversationStore = defineStore( return entry.message } - /** Guards re-delivery: a delayed ask must not draw a second card. */ + /** + * Whether this turn is already showing `askId`. Turn recovery asks before + * re-delivering an ask off a persisted row: the card a hydrate drew is on + * screen without the session's ask ledger ever having seen a frame for + * it, and only this catches that one. Reads the mutable message, so it + * answers for the current moment and takes no reactive dependency. + */ function isApprovalShown(turn: LiveTurn, askId: string): boolean { const message = liveTurnMessage(turn) return ( From 85be037544caa5625325f19ec784d80bf207a204 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 1 Oct 2026 15:39:57 -0700 Subject: [PATCH 03/36] test(agent): anchor the restored-approval card on role and name Carries the spec onto the rebased base's turn-lock fixture, which had independently grown the richer half of what this spec needs: `primePendingAsk` parks a row on an unanswered ask without publishing a frame, `RUN_APPROVAL_EVENT`/`RUN_APPROVAL_RESOLVED_EVENT` are exported, and `answeredAsks()` reads the answers off the fake server. So the fixture keeps that version and gains only what was missing: a card locator, the workflow link, `resolveApproval`, `parkOnApproval`, and a transcript-GET counter the no-duplicate-card case waits on. `panel.getByText(APPROVAL_WORKFLOW_NAME)` cannot be used on this base: the ask names the same workflow the panel's picker and tab already name, so it resolves to three elements and fails strict mode. The card's workflow link is anchored on role + accessible name instead, which is also the assertion a screen-reader user's view supports. Verified: both cases green against a `build:cloud` dist served by ComfyUI (`--front-end-root`), project `cloud`. --- browser_tests/fixtures/agentTurnLockFixture.ts | 16 +++++++++++++--- .../agentApprovalSurvivesSocketDrop.spec.ts | 5 +---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts index 97563406485..620bafacdb9 100644 --- a/browser_tests/fixtures/agentTurnLockFixture.ts +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -94,10 +94,12 @@ const FOREIGN_TURN_DONE_EVENT: AgentWsEvent = { const RUN_APPROVAL_ASK_ID = `${TURN_ID}:call-run-workflow` /** - * The workflow this ask names. Exported so a spec can assert the card is the - * one the ask described without restating the literal. + * The workflow this ask names. Not exported: it is the same workflow the + * panel's picker and tab already name, so a spec that matched it as text + * would resolve to three elements. `approvalWorkflowLink` below is the + * locator a spec should assert on instead. */ -export const APPROVAL_WORKFLOW_NAME = 'Unsaved Workflow' +const APPROVAL_WORKFLOW_NAME = 'Unsaved Workflow' /** * The option ids this ask offers. Declared separately because `AgentWsEvent` @@ -526,6 +528,7 @@ export class AgentTurnLockHarness { public readonly stopButton: Locator public readonly runApprovalButton: Locator public readonly approvalCard: Locator + public readonly approvalWorkflowLink: Locator public readonly workSummary: Locator public readonly workingRow: Locator public readonly liveProgressRow: Locator @@ -566,6 +569,13 @@ export class AgentTurnLockHarness { enMessages.agent.runApproval.question, { exact: true } ) + // The workflow the ask names, as the card offers it: a link the user can + // open. Anchored on role + accessible name rather than on the text, which + // also appears on the panel's workflow picker and inside this link. + this.approvalWorkflowLink = this.panel.getByRole('button', { + name: APPROVAL_WORKFLOW_NAME, + exact: true + }) // WorkSummary.vue renders three labels off the elapsed total: `worked` // alone, `workedForSeconds`, or `workedForMinutes`. Anchoring on the // shared `worked` stem matches all three, so the negative assertions on diff --git a/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts index 7e577f783ae..8f996bdf0e9 100644 --- a/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts +++ b/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts @@ -2,7 +2,6 @@ import { expect } from '@playwright/test' import { APPROVAL_RUN_OPTION_ID, - APPROVAL_WORKFLOW_NAME, RUN_APPROVAL_EVENT, TURN_IN_PROGRESS_MESSAGE, agentTurnLockTest as test @@ -54,9 +53,7 @@ test.describe( await test.step('the approval surfaces without a reload', async () => { await expect(turnLock.approvalCard).toBeVisible({ timeout: 30_000 }) - await expect( - turnLock.panel.getByText(APPROVAL_WORKFLOW_NAME) - ).toBeVisible() + await expect(turnLock.approvalWorkflowLink).toBeVisible() await expect(turnLock.stopButton).toBeVisible() }) From 2d49daa2edebedd73f8026789a885495a887a420 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 2 Oct 2026 00:16:40 -0700 Subject: [PATCH 04/36] fix(agent): read a restored approval only from an open row The recovery poll reduced `pending_ask` across every row of the turn, so a row the server had already closed could hand back an ask that was already spent -- drawing a card nobody can answer and filing the restore as a lost frame. The server clears `pending_ask` behind the answer rather than with it, which `(g33)` already relies on, so a closed row carrying one is expected rather than exceptional. Take the ask only from a row that is still open, which is what the branch already said it did, and matches `applyAssistantRow` gating the hydrate path on the same `row.status` authority. (g36) covers it: red here before the narrowing, with the spent card drawn and reported. --- .../composables/agent/useAgentSession.test.ts | 44 +++++++++++++++++++ .../composables/agent/useAgentSession.ts | 11 ++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index bde58715f88..287b4f0bc12 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4582,6 +4582,50 @@ describe('useAgentSession (v1 composition root)', () => { } }) + // The same lag `(g33)` describes, read from the other side: the server + // clears `pending_ask` behind the answer, so a row it has already closed can + // still be carrying a spent one. A later row keeps the turn open, so + // recovery still polls -- and must take the ask only from the open row, or + // the turn's own closed row resurrects a card nobody can answer. The + // hydrate path is already gated this way (`applyAssistantRow`). + it('(g36) a closed row still carrying a spent ask does not restore a card', async () => { + vi.useFakeTimers() + try { + const rest = fakeRest({ + getMessages: vi.fn( + async (): Promise => [ + historyRow(1, 'user', 'msg-1', 'go'), + { + ...parkedRow(), + status: 'interrupted' + }, + { + ...historyRow(3, 'assistant', 'msg-1', '', 'row-3'), + content: {}, + status: 'streaming' + } + ] + ) + }) + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + + await session.sendMessage('go') + emit(delta('msg-1', 'partial')) + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(31_000) + + expect(approvalParts(session)).toHaveLength(0) + expect(reportError).not.toHaveBeenCalled() + } finally { + vi.useRealTimers() + } + }) + // A frame published while the first history fetch is still in flight has no // turn in memory to reach: `ingest` drops it. Recovery is the only thing // left that can draw the card, so the drop must not be recorded as a diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 49b025bc49d..b51ea516b75 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -2083,10 +2083,19 @@ export function useAgentSession(deps: AgentSessionDeps) { // PM-1738: a turn parked on an approval persists the unanswered ask on // whichever of its rows is still open, which is the last one to carry // one -- not necessarily the anchor the turn is keyed by. + // + // Only an open row is read. The server clears `pending_ask` behind the + // answer rather than with it (see `(g33)`), so a row it has already + // closed can still be carrying one that is spent, and restoring that + // would draw a card over a resolved ask. `applyAssistantRow` gates the + // hydrate path on the same `row.status` authority. return { kind: 'streaming', pendingAsk: rows.reduce( - (found, row) => row.pending_ask ?? found, + (found, row) => + isTerminalTurnStatus(row.status) + ? found + : (row.pending_ask ?? found), undefined ) } From 571bf467f2394186742eadf4cef8bd2150048ab6 Mon Sep 17 00:00:00 2001 From: bymyself Date: Tue, 6 Oct 2026 19:39:42 +0000 Subject: [PATCH 05/36] fix(agent): keep uncertain approvals retired --- .../composables/agent/useAgentSession.test.ts | 44 +++++++++++++++++++ .../composables/agent/useAgentSession.ts | 1 + 2 files changed, 45 insertions(+) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 287b4f0bc12..6e35b0af2ea 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4582,6 +4582,50 @@ describe('useAgentSession (v1 composition root)', () => { } }) + it('(g37) an uncertain hydrated answer keeps a stale poll from restoring its card', async () => { + vi.useFakeTimers() + try { + let resolveStalePoll: ((history: AgentMessages) => void) | undefined + const rest = fakeRest({ + getMessages: vi + .fn() + .mockResolvedValueOnce([ + historyRow(1, 'user', 'msg-1', 'go'), + parkedRow() + ]) + .mockImplementationOnce( + () => + new Promise((resolve) => { + resolveStalePoll = resolve + }) + ), + answerAsk: vi + .fn() + .mockRejectedValue(new AgentApiError('backend blip', 500, undefined)) + }) + const { source, status } = fakeEvents() + localStorage.setItem(StorageKeys.agentThread('personal'), 'th-1') + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + await vi.advanceTimersByTimeAsync(0) + expect(approvalParts(session)).toHaveLength(1) + await vi.waitFor(() => expect(rest.getMessages).toHaveBeenCalledTimes(2)) + + const answered = session.answerAsk('turn-1:call-1', 'run') + await vi.advanceTimersByTimeAsync(5_000) + await answered + expect(approvalParts(session)).toHaveLength(0) + + resolveStalePoll?.([historyRow(1, 'user', 'msg-1', 'go'), parkedRow()]) + await vi.advanceTimersByTimeAsync(0) + + expect(approvalParts(session)).toHaveLength(0) + } finally { + vi.useRealTimers() + } + }) + // The same lag `(g33)` describes, read from the other side: the server // clears `pending_ask` behind the answer, so a row it has already closed can // still be carrying a spent one. A later row keeps the turn open, so diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index b51ea516b75..393d9e47383 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -1533,6 +1533,7 @@ export function useAgentSession(deps: AgentSessionDeps) { // 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. + deliveredAsks.add(askId) conversationStore.retireAsk(askId, currentThreadId) pushError(i18n.global.t('agent.runApproval.answerUncertain')) return false From c9ace51886a2161cf5ed6b63715a59ec26960b88 Mon Sep 17 00:00:00 2001 From: bymyself Date: Tue, 6 Oct 2026 20:30:56 +0000 Subject: [PATCH 06/36] fix(agent): harden approval recovery lifecycle --- .../composables/agent/useAgentSession.test.ts | 51 ++++++++++++++++++- .../composables/agent/useAgentSession.ts | 43 ++++++++++++---- .../agent/agentEventTransport.test.ts | 18 +++++++ .../services/agent/agentEventTransport.ts | 15 +++--- .../stores/agent/agentConversationStore.ts | 5 ++ 5 files changed, 112 insertions(+), 20 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 6e35b0af2ea..e4abfaa8d84 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -3855,6 +3855,21 @@ describe('useAgentSession (v1 composition root)', () => { expect(session.editableTurnId.value).toBe('msg-1') }) + it('(g6b) an approval does not make a still-streaming stopped turn editable', async () => { + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest: fakeRest(), events: source }) + session.start() + status(true) + + await session.sendMessage('go') + emit(delta('msg-1', 'partial')) + await session.stopTurn() + emit(runApproval('msg-1')) + + expect(session.isStreaming.value).toBe(true) + expect(session.editableTurnId.value).toBeNull() + }) + it('(g7) a flapping socket starts one recovery job per turn, not one per reconnect', async () => { const rest = streamingTurnRest() const { source, emit, status } = fakeEvents() @@ -4626,6 +4641,36 @@ describe('useAgentSession (v1 composition root)', () => { } }) + it('(g38) a remount does not restore an ask the prior session retired', async () => { + vi.useFakeTimers() + try { + const rest = parkedOnApprovalRest() + vi.mocked(rest.answerAsk).mockRejectedValue( + new AgentApiError('already answered', 409, undefined) + ) + const firstEvents = fakeEvents() + localStorage.setItem(StorageKeys.agentThread('personal'), 'th-1') + const first = useAgentSession({ rest, events: firstEvents.source }) + first.start() + firstEvents.status(true) + await vi.advanceTimersByTimeAsync(0) + expect(approvalParts(first)).toHaveLength(1) + + await first.answerAsk('turn-1:call-1', 'run') + first.stop() + + const secondEvents = fakeEvents() + const second = useAgentSession({ rest, events: secondEvents.source }) + second.start() + secondEvents.status(true) + await vi.advanceTimersByTimeAsync(31_000) + + expect(approvalParts(second)).toHaveLength(0) + } finally { + vi.useRealTimers() + } + }) + // The same lag `(g33)` describes, read from the other side: the server // clears `pending_ask` behind the answer, so a row it has already closed can // still be carrying a spent one. A later row keeps the turn open, so @@ -4693,13 +4738,15 @@ describe('useAgentSession (v1 composition root)', () => { return [historyRow(1, 'user', 'msg-1', 'go'), parkedRow()] }) }) - const { source, emit, status } = fakeEvents() + const { source, status } = fakeEvents() localStorage.setItem(StorageKeys.agentThread('personal'), 'th-1') const session = useAgentSession({ rest, events: source }) + useAgentConversationStore().ingest( + zAgentWsEvent.parse(runApproval('msg-1')) + ) session.start() status(true) - emit(runApproval('msg-1')) expect(session.entries.value).toHaveLength(0) releaseHistory() diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 393d9e47383..e8b320b9b6c 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -318,6 +318,7 @@ const TERMINAL_ANSWER_STATUSES = new Set([403, 404, 409]) * the shared 60s request deadline it replaces. */ const ANSWER_RETRY_BACKOFF_MS = [300] +const MAX_DELIVERED_ASKS = 128 /** * A rejected fetch never reached the server, and 5xx is the status the server @@ -524,6 +525,18 @@ export function useAgentSession(deps: AgentSessionDeps) { const deliveredAsks = new Set() const lateAskReports = new Map>() + function recordDeliveredAsk(askId: string): void { + deliveredAsks.add(askId) + if (deliveredAsks.size <= MAX_DELIVERED_ASKS) return + const oldest = deliveredAsks.values().next().value + if (oldest !== undefined) deliveredAsks.delete(oldest) + } + + function clearLateAskReports(): void { + for (const scheduled of lateAskReports.values()) clearTimeout(scheduled) + lateAskReports.clear() + } + function pushError(text: string): void { notices.value.push({ level: 'error', text }) } @@ -868,8 +881,7 @@ export function useAgentSession(deps: AgentSessionDeps) { unsubscribeStatus = null for (const running of recoveringTurns.values()) running.controller.abort() recoveringTurns.clear() - for (const scheduled of lateAskReports.values()) clearTimeout(scheduled) - lateAskReports.clear() + clearLateAskReports() const stoppedGeneration = ownedGeneration queueMicrotask(() => { if (stoppedGeneration !== sessionGeneration) return @@ -1468,6 +1480,7 @@ export function useAgentSession(deps: AgentSessionDeps) { pushError(i18n.global.t('agent.runApproval.answerFailed')) // Nothing can ever answer this card, so retire it rather than let every // further click raise another toast and another telemetry event. + recordDeliveredAsk(askId) conversationStore.retireAsk(askId) return false } @@ -1480,7 +1493,7 @@ export function useAgentSession(deps: AgentSessionDeps) { conversationStore.setAskAnswering(askId, true) try { await sendAnswer(currentThreadId, askId, selection) - deliveredAsks.add(askId) + recordDeliveredAsk(askId) conversationStore.commitAsk(askId, currentThreadId) return true } catch (error) { @@ -1514,7 +1527,7 @@ export function useAgentSession(deps: AgentSessionDeps) { // The card is retired for good here, by resolution (409) or because // this client could never answer it. Either way a later recovery poll // must not draw it again. - deliveredAsks.add(askId) + recordDeliveredAsk(askId) conversationStore.retireAsk(askId, currentThreadId) return false } @@ -1533,7 +1546,7 @@ export function useAgentSession(deps: AgentSessionDeps) { // 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. - deliveredAsks.add(askId) + recordDeliveredAsk(askId) conversationStore.retireAsk(askId, currentThreadId) pushError(i18n.global.t('agent.runApproval.answerUncertain')) return false @@ -1599,6 +1612,7 @@ export function useAgentSession(deps: AgentSessionDeps) { source?: Exclude ): void { readyThreadId.value = null + clearLateAskReports() loadGeneration++ promptEditState.value = { phase: 'idle' } conversationStore.stashActiveTurn() @@ -1622,6 +1636,7 @@ export function useAgentSession(deps: AgentSessionDeps) { isNavigationCurrent: () => boolean = () => true ): Promise { readyThreadId.value = null + clearLateAskReports() const generation = ++loadGeneration promptEditState.value = { phase: 'idle' } const isCurrent = () => @@ -1782,7 +1797,7 @@ export function useAgentSession(deps: AgentSessionDeps) { // owning turn's transport, and the turn is gone in exactly the case that // matters, so on its own it would re-enable a card it cannot remove. conversationStore.retireAsk(event.data.ask_id, event.data.thread_id) - deliveredAsks.add(event.data.ask_id) + recordDeliveredAsk(event.data.ask_id) onAskResolved?.(event.data.ask_id) } for (const turn of conversationStore.liveTurns()) observeSkillTurn(turn) @@ -1881,6 +1896,7 @@ export function useAgentSession(deps: AgentSessionDeps) { await delay(ms, { signal }) if (!isTurnLive(turn, generation)) return const outcome = await fetchTurnOutcome(turn, signal) + signal.throwIfAborted() if (!isTurnLive(turn, generation)) return consecutiveThreadMissing = outcome.kind === 'thread-missing' ? consecutiveThreadMissing + 1 : 0 @@ -1912,8 +1928,8 @@ export function useAgentSession(deps: AgentSessionDeps) { ): void { if (pendingAsk?.kind !== 'run_approval') return if (deliveredAsks.has(pendingAsk.ask_id)) return + if (conversationStore.isAskRetired(pendingAsk.ask_id, turn.threadId)) return if (conversationStore.isApprovalShown(turn, pendingAsk.ask_id)) return - deliveredAsks.add(pendingAsk.ask_id) conversationStore.ingest({ type: 'agent_ask', data: { @@ -1922,7 +1938,8 @@ export function useAgentSession(deps: AgentSessionDeps) { message_id: turn.messageId } }) - markStoppedTurnReady(turn) + if (!conversationStore.isApprovalShown(turn, pendingAsk.ask_id)) return + recordDeliveredAsk(pendingAsk.ask_id) reportRestoredApproval(turn, pendingAsk.ask_id, cause) } @@ -1968,7 +1985,10 @@ export function useAgentSession(deps: AgentSessionDeps) { event: Extract ): void { const askId = event.data.ask_id - if (deliveredAsks.has(askId)) { + if ( + deliveredAsks.has(askId) || + conversationStore.isAskRetired(askId, event.data.thread_id) + ) { withdrawLateAskReport(askId) return } @@ -1977,8 +1997,8 @@ export function useAgentSession(deps: AgentSessionDeps) { messageId: toTurnId(event.data.message_id) } conversationStore.ingest(event) - if (conversationStore.isApprovalShown(turn, askId)) deliveredAsks.add(askId) - markStoppedTurnReady(turn) + if (conversationStore.isApprovalShown(turn, askId)) + recordDeliveredAsk(askId) } function withdrawLateAskReport(askId: string): void { @@ -2112,6 +2132,7 @@ export function useAgentSession(deps: AgentSessionDeps) { function forgetDeletedThread(turn: LiveTurn): void { skillTurnUsage.delete(recoveryKey(turn)) + clearLateAskReports() if (conversationStore.threadId !== turn.threadId) { conversationStore.settleTurn(turn, undefined) return diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts index 253b5fb5cd9..fbffd2be7da 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts @@ -606,6 +606,24 @@ describe('agentEventTransport run approval', () => { ]) }) + it('closes open transcript parts when a duplicate approval frame arrives', () => { + const message = drive([ + runApproval(), + delta('before the delayed frame'), + runApproval() + ]) + + expect(message.parts).toEqual([ + { + type: 'runApproval', + askId: 'turn-1:call-1', + workflowId: 'workflow-1', + workflowName: 'Portrait workflow' + }, + { type: 'text', text: 'before the delayed frame', state: 'done' } + ]) + }) + it('removes only the matching approval when the ask resolves', () => { const message = drive([ runApproval('ask-1'), diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts index c1823e17758..5d3516aefe8 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts @@ -335,7 +335,8 @@ export function createAgentEventTransport( * ask dropped in routing, so it is reported the same way. Generated-contract * kinds without a client renderer are tagged separately from unknown input. * - * An ask already on the message also returns `false`, and is NOT reported: + * An ask already on the message emits the closed transcript parts but is + * NOT reported: * turn recovery restores an unanswered ask off the persisted row, and the * server writes that row before it publishes the frame, so the two can * arrive in either order for the same `ask_id`. The second arrival is a @@ -349,17 +350,17 @@ export function createAgentEventTransport( ) return false } - if ( - message.parts.some( - (part) => part.type === 'runApproval' && part.askId === data.ask_id - ) - ) - return false dropDraft() closeOpenText() closeOpenThinking() message.thinking = false message.thinkingText = undefined + if ( + message.parts.some( + (part) => part.type === 'runApproval' && part.askId === data.ask_id + ) + ) + return true const part: RunApprovalPart = { type: 'runApproval', askId: data.ask_id, diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 2cdb7dce380..8bedc37c35f 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -460,6 +460,10 @@ export const useAgentConversationStore = defineStore( resolvedAskIds.set(key, retired) return retired } + + function isAskRetired(askId: string, owner?: string): boolean { + return retiredAsksFor(owner).has(askId) + } /** * PM-1658: which way this client answered each ask. The server takes a * second answer from anywhere with 202 while committing only the FIRST, so @@ -1329,6 +1333,7 @@ export const useAgentConversationStore = defineStore( submittedAskSelection, commitAsk, retireAsk, + isAskRetired, startTurn, ingest, setCanvasSyncGate, From 2e6313ab65ade9c1d846a377191d1c0ed312e120 Mon Sep 17 00:00:00 2001 From: bymyself Date: Tue, 6 Oct 2026 21:16:26 +0000 Subject: [PATCH 07/36] test(e2e): preserve allowed route handlers --- browser_tests/fixtures/networkIsolationFixture.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser_tests/fixtures/networkIsolationFixture.ts b/browser_tests/fixtures/networkIsolationFixture.ts index 00c5a839dad..b65152f94a9 100644 --- a/browser_tests/fixtures/networkIsolationFixture.ts +++ b/browser_tests/fixtures/networkIsolationFixture.ts @@ -136,7 +136,7 @@ export const networkIsolationFixture = base.extend<{ await context.route('**/*', async (route) => { const url = new URL(route.request().url()) if (origins.has(url.origin)) { - await route.continue() + await route.fallback() return } unexpected.add(getBlockedRequestViolation(url, route.request().method())) From ded03825220bb14bf3c0d92494bc8fa8abb53a28 Mon Sep 17 00:00:00 2001 From: bymyself Date: Tue, 6 Oct 2026 21:43:25 +0000 Subject: [PATCH 08/36] fix(agent): close approval recovery edge cases --- .../agentApprovalSurvivesSocketDrop.spec.ts | 29 +++++-- .../composables/agent/useAgentSession.test.ts | 15 +++- .../composables/agent/useAgentSession.ts | 78 ++++++++++++++----- .../stores/agent/agentConversationStore.ts | 2 +- 4 files changed, 94 insertions(+), 30 deletions(-) diff --git a/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts index 8f996bdf0e9..74a85e17734 100644 --- a/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts +++ b/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts @@ -100,21 +100,36 @@ test.describe( // restored again. test('does not redraw an approval the socket already delivered', async ({ turnLock, - getWebSocket + getWebSocket, + page }) => { turnLock.parkOnApproval() turnLock.push(await getWebSocket(), RUN_APPROVAL_EVENT) await expect(turnLock.approvalCard).toBeVisible() - const pollsBefore = turnLock.transcriptFetches() + let recoveryResponses = 0 + const secondRecoveryResponse = page.waitForResponse((response) => { + const request = response.request() + if ( + request.method() !== 'GET' || + !new URL(response.url()).pathname.endsWith('/messages') + ) + return false + return ++recoveryResponses === 2 + }) await turnLock.dropSocket() // toHaveCount returns as soon as it passes, so the count alone would - // assert nothing before recovery has even polled. Wait for two polls to - // have actually served the parked row, then hold the panel to one card. - await expect - .poll(() => turnLock.transcriptFetches(), { timeout: 30_000 }) - .toBeGreaterThanOrEqual(pollsBefore + 2) + // assert nothing before recovery has even polled. Wait until the browser + // receives two recovery responses, then let the app apply the second + // one before holding the panel to one card. + await secondRecoveryResponse + await page.evaluate( + () => + new Promise((resolve) => + requestAnimationFrame(() => requestAnimationFrame(() => resolve())) + ) + ) await expect(turnLock.approvalCard).toHaveCount(1) await expect(turnLock.runApprovalButton).toHaveCount(1) }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index e4abfaa8d84..163c12856f5 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -3866,6 +3866,7 @@ describe('useAgentSession (v1 composition root)', () => { await session.stopTurn() emit(runApproval('msg-1')) + expect(approvalParts(session)).toHaveLength(1) expect(session.isStreaming.value).toBe(true) expect(session.editableTurnId.value).toBeNull() }) @@ -4736,7 +4737,12 @@ describe('useAgentSession (v1 composition root)', () => { return [historyRow(1, 'user', 'msg-1', 'go'), unparkedRow()] } return [historyRow(1, 'user', 'msg-1', 'go'), parkedRow()] - }) + }), + answerAsk: vi + .fn() + .mockRejectedValue( + new AgentApiError('already answered', 409, undefined) + ) }) const { source, status } = fakeEvents() localStorage.setItem(StorageKeys.agentThread('personal'), 'th-1') @@ -4753,6 +4759,13 @@ describe('useAgentSession (v1 composition root)', () => { await vi.advanceTimersByTimeAsync(31_000) expect(approvalParts(session)).toHaveLength(1) + await session.answerAsk('turn-1:call-1', 'run') + expect(session.notices.value).toEqual([ + { + level: 'error', + text: 'This request was already answered somewhere else, so that answer was used instead of yours.' + } + ]) } finally { vi.useRealTimers() } diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index e8b320b9b6c..0b8c94beb8c 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -523,7 +523,11 @@ export function useAgentSession(deps: AgentSessionDeps) { * an `ask_id` carries its `message_id`, so it is never reused. */ const deliveredAsks = new Set() - const lateAskReports = new Map>() + const restoredAsks = new Set() + const lateAskReports = new Map< + string, + { threadId: string; timer: ReturnType } + >() function recordDeliveredAsk(askId: string): void { deliveredAsks.add(askId) @@ -532,9 +536,23 @@ export function useAgentSession(deps: AgentSessionDeps) { if (oldest !== undefined) deliveredAsks.delete(oldest) } - function clearLateAskReports(): void { - for (const scheduled of lateAskReports.values()) clearTimeout(scheduled) - lateAskReports.clear() + function recordRestoredAsk(askId: string): void { + restoredAsks.add(askId) + if (restoredAsks.size <= MAX_DELIVERED_ASKS) return + const oldest = restoredAsks.values().next().value + if (oldest !== undefined) restoredAsks.delete(oldest) + } + + function lateAskReportKey(threadId: string, askId: string): string { + return `${threadId}\u0000${askId}` + } + + function clearLateAskReports(threadId?: string): void { + for (const [key, scheduled] of lateAskReports) { + if (threadId !== undefined && scheduled.threadId !== threadId) continue + clearTimeout(scheduled.timer) + lateAskReports.delete(key) + } } function pushError(text: string): void { @@ -1493,6 +1511,7 @@ export function useAgentSession(deps: AgentSessionDeps) { conversationStore.setAskAnswering(askId, true) try { await sendAnswer(currentThreadId, askId, selection) + restoredAsks.delete(askId) recordDeliveredAsk(askId) conversationStore.commitAsk(askId, currentThreadId) return true @@ -1523,6 +1542,12 @@ export function useAgentSession(deps: AgentSessionDeps) { { retryable: isRetryableRequestFailure(error, false) } ) pushError(i18n.global.t('agent.runApproval.answerFailed')) + } else if (restoredAsks.delete(askId)) { + reportError(new Error('restored run approval was already answered'), { + surface: 'agent', + errorType: 'agent_ask_answer_superseded' + }) + pushError(i18n.global.t('agent.runApproval.answerSuperseded')) } // The card is retired for good here, by resolution (409) or because // this client could never answer it. Either way a later recovery poll @@ -1612,7 +1637,8 @@ export function useAgentSession(deps: AgentSessionDeps) { source?: Exclude ): void { readyThreadId.value = null - clearLateAskReports() + const departingThreadId = conversationStore.threadId + if (departingThreadId !== null) clearLateAskReports(departingThreadId) loadGeneration++ promptEditState.value = { phase: 'idle' } conversationStore.stashActiveTurn() @@ -1636,7 +1662,8 @@ export function useAgentSession(deps: AgentSessionDeps) { isNavigationCurrent: () => boolean = () => true ): Promise { readyThreadId.value = null - clearLateAskReports() + const departingThreadId = conversationStore.threadId + if (departingThreadId !== null) clearLateAskReports(departingThreadId) const generation = ++loadGeneration promptEditState.value = { phase: 'idle' } const isCurrent = () => @@ -1793,6 +1820,7 @@ export function useAgentSession(deps: AgentSessionDeps) { observeLiveDelivery(event) if (event.type === 'agent_ask_resolved') { reportSupersededAnswer(event.data.ask_id, event.data.selected) + restoredAsks.delete(event.data.ask_id) // Not just un-busying it: `ingest` below routes this frame through the // owning turn's transport, and the turn is gone in exactly the case that // matters, so on its own it would re-enable a card it cannot remove. @@ -1940,6 +1968,7 @@ export function useAgentSession(deps: AgentSessionDeps) { }) if (!conversationStore.isApprovalShown(turn, pendingAsk.ask_id)) return recordDeliveredAsk(pendingAsk.ask_id) + recordRestoredAsk(pendingAsk.ask_id) reportRestoredApproval(turn, pendingAsk.ask_id, cause) } @@ -1960,13 +1989,14 @@ export function useAgentSession(deps: AgentSessionDeps) { sendRestoredApprovalReport(turn, askId, cause, 'warning') return } - lateAskReports.set( - askId, - setTimeout(() => { - lateAskReports.delete(askId) + const key = lateAskReportKey(turn.threadId, askId) + lateAskReports.set(key, { + threadId: turn.threadId, + timer: setTimeout(() => { + lateAskReports.delete(key) sendRestoredApprovalReport(turn, askId, cause, 'error') }, LATE_ASK_FRAME_GRACE_MS) - ) + }) } /** @@ -1985,27 +2015,33 @@ export function useAgentSession(deps: AgentSessionDeps) { event: Extract ): void { const askId = event.data.ask_id - if ( - deliveredAsks.has(askId) || - conversationStore.isAskRetired(askId, event.data.thread_id) - ) { - withdrawLateAskReport(askId) + if (conversationStore.isAskRetired(askId, event.data.thread_id)) { + withdrawLateAskReport(event.data.thread_id, askId) return } const turn = { threadId: event.data.thread_id, messageId: toTurnId(event.data.message_id) } + if ( + deliveredAsks.has(askId) && + !conversationStore.isApprovalShown(turn, askId) + ) { + withdrawLateAskReport(event.data.thread_id, askId) + return + } conversationStore.ingest(event) + withdrawLateAskReport(event.data.thread_id, askId) if (conversationStore.isApprovalShown(turn, askId)) recordDeliveredAsk(askId) } - function withdrawLateAskReport(askId: string): void { - const scheduled = lateAskReports.get(askId) + function withdrawLateAskReport(threadId: string, askId: string): void { + const key = lateAskReportKey(threadId, askId) + const scheduled = lateAskReports.get(key) if (scheduled === undefined) return - clearTimeout(scheduled) - lateAskReports.delete(askId) + clearTimeout(scheduled.timer) + lateAskReports.delete(key) } function sendRestoredApprovalReport( @@ -2132,7 +2168,7 @@ export function useAgentSession(deps: AgentSessionDeps) { function forgetDeletedThread(turn: LiveTurn): void { skillTurnUsage.delete(recoveryKey(turn)) - clearLateAskReports() + clearLateAskReports(turn.threadId) if (conversationStore.threadId !== turn.threadId) { conversationStore.settleTurn(turn, undefined) return diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 8bedc37c35f..6d17790e414 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -462,7 +462,7 @@ export const useAgentConversationStore = defineStore( } function isAskRetired(askId: string, owner?: string): boolean { - return retiredAsksFor(owner).has(askId) + return resolvedAskIds.get(threadKey(owner))?.has(askId) ?? false } /** * PM-1658: which way this client answered each ask. The server takes a From 8e0823e972bb2d636fc53f33cba22a90943be143 Mon Sep 17 00:00:00 2001 From: bymyself Date: Tue, 6 Oct 2026 22:27:40 +0000 Subject: [PATCH 09/36] fix(agent): persist approval recovery provenance --- .../fixtures/agentTurnLockFixture.ts | 12 ---- .../composables/agent/useAgentSession.test.ts | 34 +++++++++- .../composables/agent/useAgentSession.ts | 65 ++++++++++++------- .../stores/agent/agentConversationStore.ts | 27 +++++++- 4 files changed, 100 insertions(+), 38 deletions(-) diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts index 620bafacdb9..f7cd03975c6 100644 --- a/browser_tests/fixtures/agentTurnLockFixture.ts +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -191,7 +191,6 @@ class TurnLockServer { private foreignPrompt: string | undefined private rejected = 0 private posts = 0 - private transcripts = 0 private readonly answered: string[][] = [] private pendingAsk: AgentPendingAsk | undefined private heldTranscript: @@ -217,11 +216,6 @@ class TurnLockServer { return this.posts } - /** Every transcript GET served, so a spec can wait out a recovery poll. */ - get transcriptFetches(): number { - return this.transcripts - } - countPost(): void { this.posts++ } @@ -280,7 +274,6 @@ class TurnLockServer { } transcript(threadId = THREAD_ID): AgentMessage[] { - this.transcripts++ if (threadId === OTHER_THREAD_ID) return [ { @@ -617,11 +610,6 @@ export class AgentTurnLockHarness { return this.server.postAttempts } - /** Every transcript GET served, so a spec can wait out a recovery poll. */ - transcriptFetches(): number { - return this.server.transcriptFetches - } - /** Opens the panel on a blank workflow and points the composer at that tab. */ async openOnBlankWorkflow(): Promise { await expect( diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 163c12856f5..008a62aba20 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4592,7 +4592,11 @@ describe('useAgentSession (v1 composition root)', () => { await vi.advanceTimersByTimeAsync(31_000) expect(approvalParts(session)).toHaveLength(0) - expect(reportError).not.toHaveBeenCalled() + expect(reportError).toHaveBeenCalledWith(expect.any(Error), { + surface: 'agent', + errorType: 'agent_ask_answer_superseded' + }) + expect(session.notices.value).toHaveLength(1) } finally { vi.useRealTimers() } @@ -4642,6 +4646,34 @@ describe('useAgentSession (v1 composition root)', () => { } }) + it('(g39) a 202 cannot silently confirm which answer won for a recovered card', async () => { + vi.useFakeTimers() + try { + const rest = parkedOnApprovalRest() + const { source, status } = fakeEvents() + localStorage.setItem(StorageKeys.agentThread('personal'), 'th-1') + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + await vi.advanceTimersByTimeAsync(0) + + await session.answerAsk('turn-1:call-1', 'run') + + expect(reportError).toHaveBeenCalledWith(expect.any(Error), { + surface: 'agent', + errorType: 'agent_ask_answer_unconfirmed' + }) + expect(session.notices.value).toEqual([ + { + level: 'error', + text: 'Could not confirm your answer reached the agent. It may still have been applied — reload the page to see where things stand.' + } + ]) + } finally { + vi.useRealTimers() + } + }) + it('(g38) a remount does not restore an ask the prior session retired', async () => { vi.useFakeTimers() try { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 0b8c94beb8c..1b480b2a487 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -523,7 +523,6 @@ export function useAgentSession(deps: AgentSessionDeps) { * an `ask_id` carries its `message_id`, so it is never reused. */ const deliveredAsks = new Set() - const restoredAsks = new Set() const lateAskReports = new Map< string, { threadId: string; timer: ReturnType } @@ -536,13 +535,6 @@ export function useAgentSession(deps: AgentSessionDeps) { if (oldest !== undefined) deliveredAsks.delete(oldest) } - function recordRestoredAsk(askId: string): void { - restoredAsks.add(askId) - if (restoredAsks.size <= MAX_DELIVERED_ASKS) return - const oldest = restoredAsks.values().next().value - if (oldest !== undefined) restoredAsks.delete(oldest) - } - function lateAskReportKey(threadId: string, askId: string): string { return `${threadId}\u0000${askId}` } @@ -836,6 +828,18 @@ export function useAgentSession(deps: AgentSessionDeps) { if (turnId !== null) snapshotTurns.add(turnId) } + function markRecoveredTranscriptAsks( + history: AgentMessages, + threadId: string + ): void { + for (const row of history) + if ( + row.status === 'streaming' && + row.pending_ask?.kind === 'run_approval' + ) + conversationStore.markAskRecovered(row.pending_ask.ask_id, threadId) + } + async function hydrateFromServer( threadId: string, isCurrent: () => boolean = () => true, @@ -845,6 +849,7 @@ export function useAgentSession(deps: AgentSessionDeps) { try { const history = await rest.getMessages(threadId) if (conversationStore.threadId !== threadId || !isCurrent()) return false + markRecoveredTranscriptAsks(history, threadId) conversationStore.hydrate(history) rememberSnapshotTurn(conversationStore.activeTurnId) reconcileLiveTurns('hydrate') @@ -1511,7 +1516,7 @@ export function useAgentSession(deps: AgentSessionDeps) { conversationStore.setAskAnswering(askId, true) try { await sendAnswer(currentThreadId, askId, selection) - restoredAsks.delete(askId) + reportAcceptedRecoveredAnswer(askId, currentThreadId) recordDeliveredAsk(askId) conversationStore.commitAsk(askId, currentThreadId) return true @@ -1542,13 +1547,7 @@ export function useAgentSession(deps: AgentSessionDeps) { { retryable: isRetryableRequestFailure(error, false) } ) pushError(i18n.global.t('agent.runApproval.answerFailed')) - } else if (restoredAsks.delete(askId)) { - reportError(new Error('restored run approval was already answered'), { - surface: 'agent', - errorType: 'agent_ask_answer_superseded' - }) - pushError(i18n.global.t('agent.runApproval.answerSuperseded')) - } + } else reportRejectedRecoveredAnswer(askId, currentThreadId) // The card is retired for good here, by resolution (409) or because // this client could never answer it. Either way a later recovery poll // must not draw it again. @@ -1578,6 +1577,30 @@ export function useAgentSession(deps: AgentSessionDeps) { } } + function reportAcceptedRecoveredAnswer( + askId: string, + threadId: string + ): void { + if (!conversationStore.isAskRecovered(askId, threadId)) return + reportError(new Error('accepted restored approval cannot be verified'), { + surface: 'agent', + errorType: 'agent_ask_answer_unconfirmed' + }) + pushError(i18n.global.t('agent.runApproval.answerUncertain')) + } + + function reportRejectedRecoveredAnswer( + askId: string, + threadId: string + ): void { + if (!conversationStore.isAskRecovered(askId, threadId)) return + reportError(new Error('restored run approval was already answered'), { + surface: 'agent', + errorType: 'agent_ask_answer_superseded' + }) + pushError(i18n.global.t('agent.runApproval.answerSuperseded')) + } + /** * PM-1658: the server commits the FIRST answer an ask receives and takes * every later one with 202 while replaying the stored selection, so another @@ -1637,8 +1660,6 @@ export function useAgentSession(deps: AgentSessionDeps) { source?: Exclude ): void { readyThreadId.value = null - const departingThreadId = conversationStore.threadId - if (departingThreadId !== null) clearLateAskReports(departingThreadId) loadGeneration++ promptEditState.value = { phase: 'idle' } conversationStore.stashActiveTurn() @@ -1662,8 +1683,6 @@ export function useAgentSession(deps: AgentSessionDeps) { isNavigationCurrent: () => boolean = () => true ): Promise { readyThreadId.value = null - const departingThreadId = conversationStore.threadId - if (departingThreadId !== null) clearLateAskReports(departingThreadId) const generation = ++loadGeneration promptEditState.value = { phase: 'idle' } const isCurrent = () => @@ -1820,7 +1839,6 @@ export function useAgentSession(deps: AgentSessionDeps) { observeLiveDelivery(event) if (event.type === 'agent_ask_resolved') { reportSupersededAnswer(event.data.ask_id, event.data.selected) - restoredAsks.delete(event.data.ask_id) // Not just un-busying it: `ingest` below routes this frame through the // owning turn's transport, and the turn is gone in exactly the case that // matters, so on its own it would re-enable a card it cannot remove. @@ -1968,7 +1986,7 @@ export function useAgentSession(deps: AgentSessionDeps) { }) if (!conversationStore.isApprovalShown(turn, pendingAsk.ask_id)) return recordDeliveredAsk(pendingAsk.ask_id) - recordRestoredAsk(pendingAsk.ask_id) + conversationStore.markAskRecovered(pendingAsk.ask_id, turn.threadId) reportRestoredApproval(turn, pendingAsk.ask_id, cause) } @@ -2025,7 +2043,8 @@ export function useAgentSession(deps: AgentSessionDeps) { } if ( deliveredAsks.has(askId) && - !conversationStore.isApprovalShown(turn, askId) + (!conversationStore.isApprovalShown(turn, askId) || + conversationStore.submittedAskSelection(askId) !== undefined) ) { withdrawLateAskReport(event.data.thread_id, askId) return diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 6d17790e414..297047b8d09 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -433,6 +433,7 @@ export const useAgentConversationStore = defineStore( for (const entry of backgroundTurns.values()) if (entry.threadId === key) entry.transport.dropAskPart(askId) retiredAsksFor(key).add(askId) + recoveredAskIds.get(key)?.delete(askId) clearAskResolutionWatchdog(askId) submittedAskSelections.delete(askId) setAskAnswering(askId, false) @@ -451,6 +452,7 @@ export const useAgentConversationStore = defineStore( * nothing here rests on an ask id being unique across threads. */ const resolvedAskIds = new Map>() + const recoveredAskIds = new Map>() const threadKey = (owner?: string) => owner ?? threadId.value ?? '' @@ -464,6 +466,17 @@ export const useAgentConversationStore = defineStore( function isAskRetired(askId: string, owner?: string): boolean { return resolvedAskIds.get(threadKey(owner))?.has(askId) ?? false } + + function markAskRecovered(askId: string, owner?: string): void { + const key = threadKey(owner) + const recovered = recoveredAskIds.get(key) ?? new Set() + recovered.add(askId) + recoveredAskIds.set(key, recovered) + } + + function isAskRecovered(askId: string, owner?: string): boolean { + return recoveredAskIds.get(threadKey(owner))?.has(askId) ?? false + } /** * PM-1658: which way this client answered each ask. The server takes a * second answer from anywhere with 202 while committing only the FIRST, so @@ -1050,7 +1063,12 @@ export const useAgentConversationStore = defineStore( ) return slot.message const entry = backgroundTurns.get(turn.messageId) - if (!entry || entry.messageId !== turn.messageId || entry.settled) + if ( + !entry || + entry.threadId !== turn.threadId || + entry.messageId !== turn.messageId || + entry.settled + ) return null return entry.message } @@ -1132,7 +1150,6 @@ export const useAgentConversationStore = defineStore( transcript: ReturnType ): void { const retired = retiredAsksFor() - if (retired.size === 0) return const named = new Set( transcript.messages.flatMap((message) => message.parts.flatMap((part) => @@ -1140,6 +1157,10 @@ export const useAgentConversationStore = defineStore( ) ) ) + const recovered = recoveredAskIds.get(threadKey()) + if (recovered !== undefined) + for (const askId of recovered) + if (!named.has(askId)) recovered.delete(askId) for (const askId of retired) if (!named.has(askId)) retired.delete(askId) for (const message of transcript.messages) message.parts = message.parts.filter( @@ -1334,6 +1355,8 @@ export const useAgentConversationStore = defineStore( commitAsk, retireAsk, isAskRetired, + markAskRecovered, + isAskRecovered, startTurn, ingest, setCanvasSyncGate, From ef597de72f82cd97a4f4e976cd98a396e055029f Mon Sep 17 00:00:00 2001 From: bymyself Date: Tue, 6 Oct 2026 23:09:10 +0000 Subject: [PATCH 10/36] fix(agent): reconcile approval recovery state --- .../composables/agent/useAgentSession.test.ts | 76 ++++++++++++------- .../composables/agent/useAgentSession.ts | 40 +++++----- .../stores/agent/agentConversationStore.ts | 45 ++++++++++- 3 files changed, 111 insertions(+), 50 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 008a62aba20..6ff676a0500 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4592,11 +4592,7 @@ describe('useAgentSession (v1 composition root)', () => { await vi.advanceTimersByTimeAsync(31_000) expect(approvalParts(session)).toHaveLength(0) - expect(reportError).toHaveBeenCalledWith(expect.any(Error), { - surface: 'agent', - errorType: 'agent_ask_answer_superseded' - }) - expect(session.notices.value).toHaveLength(1) + expect(reportError).not.toHaveBeenCalled() } finally { vi.useRealTimers() } @@ -4647,31 +4643,21 @@ describe('useAgentSession (v1 composition root)', () => { }) it('(g39) a 202 cannot silently confirm which answer won for a recovered card', async () => { - vi.useFakeTimers() - try { - const rest = parkedOnApprovalRest() - const { source, status } = fakeEvents() - localStorage.setItem(StorageKeys.agentThread('personal'), 'th-1') - const session = useAgentSession({ rest, events: source }) - session.start() - status(true) - await vi.advanceTimersByTimeAsync(0) + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest: fakeRest(), events: source }) + session.start() + status(true) + await session.sendMessage('go') + emit(runApproval('msg-1')) + status(false) - await session.answerAsk('turn-1:call-1', 'run') + await session.answerAsk('turn-1:call-1', 'run') - expect(reportError).toHaveBeenCalledWith(expect.any(Error), { - surface: 'agent', - errorType: 'agent_ask_answer_unconfirmed' - }) - expect(session.notices.value).toEqual([ - { - level: 'error', - text: 'Could not confirm your answer reached the agent. It may still have been applied — reload the page to see where things stand.' - } - ]) - } finally { - vi.useRealTimers() - } + expect(reportError).toHaveBeenCalledWith(expect.any(Error), { + surface: 'agent', + errorType: 'agent_ask_answer_unconfirmed' + }) + expect(session.notices.value).toHaveLength(1) }) it('(g38) a remount does not restore an ask the prior session retired', async () => { @@ -4845,6 +4831,40 @@ describe('useAgentSession (v1 composition root)', () => { } }) + it('(g40) recovery replaces a stale approval with the row current ask', async () => { + vi.useFakeTimers() + try { + const currentAskId = 'turn-1:call-2' + const rest = fakeRest({ + getMessages: vi.fn( + async (): Promise => [ + historyRow(1, 'user', 'msg-1', 'go'), + { + ...parkedRow(), + pending_ask: { ...PARKED_ASK, ask_id: currentAskId } + } + ] + ) + }) + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start({ restore: false }) + status(true) + await session.sendMessage('go') + emit(runApproval('msg-1')) + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(0) + + expect(approvalParts(session).map((part) => part.askId)).toEqual([ + currentAskId + ]) + } finally { + vi.useRealTimers() + } + }) + it('(g31) an ask the server parks during a hydrate fetch is restored as a warning', async () => { vi.useFakeTimers() try { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 1b480b2a487..7022dffb6bf 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -828,18 +828,6 @@ export function useAgentSession(deps: AgentSessionDeps) { if (turnId !== null) snapshotTurns.add(turnId) } - function markRecoveredTranscriptAsks( - history: AgentMessages, - threadId: string - ): void { - for (const row of history) - if ( - row.status === 'streaming' && - row.pending_ask?.kind === 'run_approval' - ) - conversationStore.markAskRecovered(row.pending_ask.ask_id, threadId) - } - async function hydrateFromServer( threadId: string, isCurrent: () => boolean = () => true, @@ -849,7 +837,6 @@ export function useAgentSession(deps: AgentSessionDeps) { try { const history = await rest.getMessages(threadId) if (conversationStore.threadId !== threadId || !isCurrent()) return false - markRecoveredTranscriptAsks(history, threadId) conversationStore.hydrate(history) rememberSnapshotTurn(conversationStore.activeTurnId) reconcileLiveTurns('hydrate') @@ -1531,10 +1518,9 @@ export function useAgentSession(deps: AgentSessionDeps) { 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. + // A 409 is a quiet double-click unless this client restored the ask + // after losing its resolution channel. The rest mean this client + // could never have answered, which the user has to be told. if (error.status !== 409) { reportError(error, { surface: 'agent', @@ -1879,6 +1865,7 @@ export function useAgentSession(deps: AgentSessionDeps) { function onStatus(live: boolean): void { if (!live) { connection = 'dropped' + conversationStore.markLiveApprovalsRecovered() return } const reconnected = connection === 'dropped' @@ -1949,11 +1936,20 @@ export function useAgentSession(deps: AgentSessionDeps) { if (outcome.kind === 'thread-missing' && consecutiveThreadMissing < 2) continue if (settleFinishedTurn(turn, outcome)) return - if (outcome.kind === 'streaming') - restoreMissingApproval(turn, outcome.pendingAsk, cause) + reconcileStreamingApproval(turn, outcome, cause) } } + function reconcileStreamingApproval( + turn: LiveTurn, + outcome: TurnOutcome, + cause: RecoveryCause + ): void { + if (outcome.kind !== 'streaming') return + conversationStore.reconcileApprovalParts(turn, outcome.pendingAsk?.ask_id) + restoreMissingApproval(turn, outcome.pendingAsk, cause) + } + /** * PM-1738: a turn parked on a tool-call approval stays `streaming` until the * user answers, so a socket drop that swallowed the `agent_ask` frame leaves @@ -2003,7 +1999,8 @@ export function useAgentSession(deps: AgentSessionDeps) { askId: string, cause: RecoveryCause ): void { - if (cause !== 'reconnect') { + const lostFrame = cause === 'reconnect' || connection === 'dropped' + if (!lostFrame) { sendRestoredApprovalReport(turn, askId, cause, 'warning') return } @@ -2012,7 +2009,7 @@ export function useAgentSession(deps: AgentSessionDeps) { threadId: turn.threadId, timer: setTimeout(() => { lateAskReports.delete(key) - sendRestoredApprovalReport(turn, askId, cause, 'error') + sendRestoredApprovalReport(turn, askId, 'reconnect', 'error') }, LATE_ASK_FRAME_GRACE_MS) }) } @@ -2188,6 +2185,7 @@ export function useAgentSession(deps: AgentSessionDeps) { function forgetDeletedThread(turn: LiveTurn): void { skillTurnUsage.delete(recoveryKey(turn)) clearLateAskReports(turn.threadId) + conversationStore.forgetRecoveredAsks(turn.threadId) if (conversationStore.threadId !== turn.threadId) { conversationStore.settleTurn(turn, undefined) return diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 297047b8d09..79146c11600 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -67,6 +67,7 @@ interface ActiveTurnSlot { * cannot leave the card disabled for the rest of the session. */ const ASK_RESOLUTION_GRACE_MS = 15_000 +const MAX_RECOVERED_ASK_THREADS = 128 function isLoadSkillPart(part: AssistantMessage['parts'][number]): boolean { return part.type === 'tool' && part.name === 'load_skill' @@ -433,7 +434,9 @@ export const useAgentConversationStore = defineStore( for (const entry of backgroundTurns.values()) if (entry.threadId === key) entry.transport.dropAskPart(askId) retiredAsksFor(key).add(askId) - recoveredAskIds.get(key)?.delete(askId) + const recovered = recoveredAskIds.get(key) + recovered?.delete(askId) + if (recovered?.size === 0) recoveredAskIds.delete(key) clearAskResolutionWatchdog(askId) submittedAskSelections.delete(askId) setAskAnswering(askId, false) @@ -472,11 +475,33 @@ export const useAgentConversationStore = defineStore( const recovered = recoveredAskIds.get(key) ?? new Set() recovered.add(askId) recoveredAskIds.set(key, recovered) + if (recoveredAskIds.size > MAX_RECOVERED_ASK_THREADS) { + const oldest = recoveredAskIds.keys().next().value + if (oldest !== undefined) recoveredAskIds.delete(oldest) + } } function isAskRecovered(askId: string, owner?: string): boolean { return recoveredAskIds.get(threadKey(owner))?.has(askId) ?? false } + + function forgetRecoveredAsks(owner: string): void { + recoveredAskIds.delete(threadKey(owner)) + } + + function markLiveApprovalsRecovered(): void { + const mark = (message: AssistantMessage, owner: string) => { + for (const part of message.parts) + if (part.type === 'runApproval') markAskRecovered(part.askId, owner) + } + const current = threadKey() + for (const message of messages.value) mark(message, current) + const slot = activeSlot.value + if (slot !== null && slot.threadId !== null) + mark(slot.message, slot.threadId) + for (const entry of backgroundTurns.values()) + mark(entry.message, entry.threadId) + } /** * PM-1658: which way this client answered each ask. The server takes a * second answer from anywhere with 202 while committing only the FIRST, so @@ -1073,6 +1098,20 @@ export const useAgentConversationStore = defineStore( return entry.message } + function reconcileApprovalParts( + turn: LiveTurn, + pendingAskId?: string + ): void { + const message = liveTurnMessage(turn) + if (message === null) return + const stale = message.parts.flatMap((part) => + part.type === 'runApproval' && part.askId !== pendingAskId + ? [part.askId] + : [] + ) + for (const askId of stale) retireAsk(askId, turn.threadId) + } + /** * Whether this turn is already showing `askId`. Turn recovery asks before * re-delivering an ask off a persisted row: the card a hydrate drew is on @@ -1161,6 +1200,7 @@ export const useAgentConversationStore = defineStore( if (recovered !== undefined) for (const askId of recovered) if (!named.has(askId)) recovered.delete(askId) + if (recovered?.size === 0) recoveredAskIds.delete(threadKey()) for (const askId of retired) if (!named.has(askId)) retired.delete(askId) for (const message of transcript.messages) message.parts = message.parts.filter( @@ -1357,6 +1397,9 @@ export const useAgentConversationStore = defineStore( isAskRetired, markAskRecovered, isAskRecovered, + forgetRecoveredAsks, + markLiveApprovalsRecovered, + reconcileApprovalParts, startTurn, ingest, setCanvasSyncGate, From d927c943b8f72f7b115381a1e15026704b04b078 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 7 Oct 2026 03:20:32 +0000 Subject: [PATCH 11/36] fix(agent): trust authoritative approval recovery state Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4201526232 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4201526239 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4201526245 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4201526255 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4201526261 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4201526269 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4201526278 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4201526283 --- .../composables/agent/useAgentSession.test.ts | 93 +++++++++++++++++-- .../composables/agent/useAgentSession.ts | 30 +++--- .../stores/agent/agentConversationStore.ts | 18 +++- 3 files changed, 112 insertions(+), 29 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 6ff676a0500..83472d1314f 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4778,12 +4778,7 @@ describe('useAgentSession (v1 composition root)', () => { expect(approvalParts(session)).toHaveLength(1) await session.answerAsk('turn-1:call-1', 'run') - expect(session.notices.value).toEqual([ - { - level: 'error', - text: 'This request was already answered somewhere else, so that answer was used instead of yours.' - } - ]) + expect(session.notices.value).toHaveLength(0) } finally { vi.useRealTimers() } @@ -4865,6 +4860,92 @@ describe('useAgentSession (v1 composition root)', () => { } }) + it('(g41) a missing turn row cannot retire a live approval', async () => { + vi.useFakeTimers() + try { + const rest = fakeRest({ + getMessages: vi.fn( + async (): Promise => [ + historyRow(1, 'user', 'msg-1', 'go') + ] + ) + }) + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start({ restore: false }) + status(true) + await session.sendMessage('go') + emit(runApproval('msg-1')) + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(31_000) + + expect(approvalParts(session)).toHaveLength(1) + } finally { + vi.useRealTimers() + } + }) + + it('(g42) the newest assistant row decides whether a turn is terminal', async () => { + vi.useFakeTimers() + try { + const rest = fakeRest({ + getMessages: vi.fn( + async (): Promise => [ + historyRow(1, 'user', 'msg-1', 'go'), + parkedRow(), + { + ...historyRow(3, 'assistant', 'msg-1', 'done', 'row-3'), + status: 'complete' + } + ] + ) + }) + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start({ restore: false }) + status(true) + await session.sendMessage('go') + emit(runApproval('msg-1')) + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(0) + + expect(session.isStreaming.value).toBe(false) + expect(approvalParts(session)).toHaveLength(0) + } finally { + vi.useRealTimers() + } + }) + + it('(g43) a confirmed current ask clears reconnect-only answer warnings', async () => { + vi.useFakeTimers() + try { + const rest = parkedOnApprovalRest() + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start({ restore: false }) + status(true) + await session.sendMessage('go') + emit(runApproval('msg-1')) + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(0) + await session.answerAsk('turn-1:call-1', 'run') + + expect(session.notices.value).toHaveLength(0) + expect(reportError).not.toHaveBeenCalledWith(expect.any(Error), { + surface: 'agent', + errorType: 'agent_ask_answer_unconfirmed' + }) + } finally { + vi.useRealTimers() + } + }) + it('(g31) an ask the server parks during a hydrate fetch is restored as a warning', async () => { vi.useFakeTimers() try { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 7022dffb6bf..02b53102703 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -1947,6 +1947,11 @@ export function useAgentSession(deps: AgentSessionDeps) { ): void { if (outcome.kind !== 'streaming') return conversationStore.reconcileApprovalParts(turn, outcome.pendingAsk?.ask_id) + if (outcome.pendingAsk?.kind === 'run_approval') + conversationStore.forgetAskRecovered( + outcome.pendingAsk.ask_id, + turn.threadId + ) restoreMissingApproval(turn, outcome.pendingAsk, cause) } @@ -2009,7 +2014,7 @@ export function useAgentSession(deps: AgentSessionDeps) { threadId: turn.threadId, timer: setTimeout(() => { lateAskReports.delete(key) - sendRestoredApprovalReport(turn, askId, 'reconnect', 'error') + sendRestoredApprovalReport(turn, askId, cause, 'error') }, LATE_ASK_FRAME_GRACE_MS) }) } @@ -2030,6 +2035,7 @@ export function useAgentSession(deps: AgentSessionDeps) { event: Extract ): void { const askId = event.data.ask_id + conversationStore.forgetAskRecovered(askId, event.data.thread_id) if (conversationStore.isAskRetired(askId, event.data.thread_id)) { withdrawLateAskReport(event.data.thread_id, askId) return @@ -2145,32 +2151,18 @@ export function useAgentSession(deps: AgentSessionDeps) { const anchor = history.find( (entry) => entry.role === 'assistant' && entry.id === turn.messageId ) - if (!anchor) return { kind: 'streaming', pendingAsk: undefined } + if (!anchor) return { kind: 'error', message: 'turn row is not visible' } const rows = history .filter( (entry) => entry.role === 'assistant' && entry.turn_id === anchor.turn_id ) .sort((a, b) => a.seq - b.seq) - if (rows.some((row) => !isTerminalTurnStatus(row.status))) - // PM-1738: a turn parked on an approval persists the unanswered ask on - // whichever of its rows is still open, which is the last one to carry - // one -- not necessarily the anchor the turn is keyed by. - // - // Only an open row is read. The server clears `pending_ask` behind the - // answer rather than with it (see `(g33)`), so a row it has already - // closed can still be carrying one that is spent, and restoring that - // would draw a card over a resolved ask. `applyAssistantRow` gates the - // hydrate path on the same `row.status` authority. + const latest = rows.at(-1) + if (latest && !isTerminalTurnStatus(latest.status)) return { kind: 'streaming', - pendingAsk: rows.reduce( - (found, row) => - isTerminalTurnStatus(row.status) - ? found - : (row.pending_ask ?? found), - undefined - ) + pendingAsk: latest.pending_ask } return { kind: 'terminal', diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 79146c11600..2cf017e7c52 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -474,6 +474,7 @@ export const useAgentConversationStore = defineStore( const key = threadKey(owner) const recovered = recoveredAskIds.get(key) ?? new Set() recovered.add(askId) + recoveredAskIds.delete(key) recoveredAskIds.set(key, recovered) if (recoveredAskIds.size > MAX_RECOVERED_ASK_THREADS) { const oldest = recoveredAskIds.keys().next().value @@ -485,6 +486,13 @@ export const useAgentConversationStore = defineStore( return recoveredAskIds.get(threadKey(owner))?.has(askId) ?? false } + function forgetAskRecovered(askId: string, owner?: string): void { + const key = threadKey(owner) + const recovered = recoveredAskIds.get(key) + recovered?.delete(askId) + if (recovered?.size === 0) recoveredAskIds.delete(key) + } + function forgetRecoveredAsks(owner: string): void { recoveredAskIds.delete(threadKey(owner)) } @@ -494,8 +502,6 @@ export const useAgentConversationStore = defineStore( for (const part of message.parts) if (part.type === 'runApproval') markAskRecovered(part.askId, owner) } - const current = threadKey() - for (const message of messages.value) mark(message, current) const slot = activeSlot.value if (slot !== null && slot.threadId !== null) mark(slot.message, slot.threadId) @@ -1103,9 +1109,12 @@ export const useAgentConversationStore = defineStore( pendingAskId?: string ): void { const message = liveTurnMessage(turn) - if (message === null) return + if (message === null || pendingAskId === undefined) return const stale = message.parts.flatMap((part) => - part.type === 'runApproval' && part.askId !== pendingAskId + part.type === 'runApproval' && + part.askId !== pendingAskId && + submittedAskSelection(part.askId) === undefined && + !answeringAskIds.value.has(part.askId) ? [part.askId] : [] ) @@ -1397,6 +1406,7 @@ export const useAgentConversationStore = defineStore( isAskRetired, markAskRecovered, isAskRecovered, + forgetAskRecovered, forgetRecoveredAsks, markLiveApprovalsRecovered, reconcileApprovalParts, From 815be7d1f69d9508423c7983298f6dca811fca02 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 7 Oct 2026 03:58:39 +0000 Subject: [PATCH 12/36] fix(agent): make approval reconciliation epoch-safe Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4202885776 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4202885782 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4202885785 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4202885786 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4202885788 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4202885794 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4202885797 --- .../composables/agent/useAgentSession.test.ts | 68 ++++++++++++++++++- .../composables/agent/useAgentSession.ts | 49 ++++++++++--- .../agent/agentEventTransport.test.ts | 4 +- .../services/agent/agentEventTransport.ts | 10 +-- .../stores/agent/agentConversationStore.ts | 9 +++ 5 files changed, 120 insertions(+), 20 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 83472d1314f..18731b4c656 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4778,7 +4778,12 @@ describe('useAgentSession (v1 composition root)', () => { expect(approvalParts(session)).toHaveLength(1) await session.answerAsk('turn-1:call-1', 'run') - expect(session.notices.value).toHaveLength(0) + expect(session.notices.value).toEqual([ + { + level: 'error', + text: 'This request was already answered somewhere else, so that answer was used instead of yours.' + } + ]) } finally { vi.useRealTimers() } @@ -4850,7 +4855,7 @@ describe('useAgentSession (v1 composition root)', () => { status(false) status(true) - await vi.advanceTimersByTimeAsync(0) + await vi.advanceTimersByTimeAsync(1_000) expect(approvalParts(session).map((part) => part.askId)).toEqual([ currentAskId @@ -4934,6 +4939,7 @@ describe('useAgentSession (v1 composition root)', () => { status(false) status(true) await vi.advanceTimersByTimeAsync(0) + emit(runApproval('msg-1')) await session.answerAsk('turn-1:call-1', 'run') expect(session.notices.value).toHaveLength(0) @@ -4946,6 +4952,64 @@ describe('useAgentSession (v1 composition root)', () => { } }) + it('(g44) repeated polls do not erase restored-approval provenance', async () => { + vi.useFakeTimers() + try { + const rest = parkedOnApprovalRest() + const { source, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start({ restore: false }) + status(true) + await session.sendMessage('go') + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(3_000) + await session.answerAsk('turn-1:call-1', 'run') + + expect(reportError).toHaveBeenCalledWith(expect.any(Error), { + surface: 'agent', + errorType: 'agent_ask_answer_unconfirmed' + }) + } finally { + vi.useRealTimers() + } + }) + + it('(g45) recovery reads the newest ask-bearing open row', async () => { + vi.useFakeTimers() + try { + const rest = fakeRest({ + getMessages: vi.fn( + async (): Promise => [ + historyRow(1, 'user', 'msg-1', 'go'), + parkedRow(), + { + ...historyRow(3, 'assistant', 'msg-1', '', 'row-3'), + content: {}, + status: 'streaming' + } + ] + ) + }) + const { source, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start({ restore: false }) + status(true) + await session.sendMessage('go') + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(0) + + expect(approvalParts(session).map((part) => part.askId)).toEqual([ + 'turn-1:call-1' + ]) + } finally { + vi.useRealTimers() + } + }) + it('(g31) an ask the server parks during a hydrate fetch is restored as a warning', async () => { vi.useFakeTimers() try { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 02b53102703..82bd66ebe85 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -195,6 +195,17 @@ type TurnOutcome = | { kind: 'streaming'; pendingAsk: PendingAsk | undefined } | { kind: 'error'; message: string } +function streamingPendingAskId(outcome: TurnOutcome): string | undefined { + return outcome.kind === 'streaming' ? outcome.pendingAsk?.ask_id : undefined +} + +function isRepeatedPendingAsk( + pendingAskId: string | undefined, + previousPendingAskId: string | undefined +): boolean { + return pendingAskId !== undefined && pendingAskId === previousPendingAskId +} + function isTerminalTurnStatus( status: AgentMessages[number]['status'] ): boolean { @@ -529,6 +540,7 @@ export function useAgentSession(deps: AgentSessionDeps) { >() function recordDeliveredAsk(askId: string): void { + deliveredAsks.delete(askId) deliveredAsks.add(askId) if (deliveredAsks.size <= MAX_DELIVERED_ASKS) return const oldest = deliveredAsks.values().next().value @@ -1864,8 +1876,9 @@ export function useAgentSession(deps: AgentSessionDeps) { function onStatus(live: boolean): void { if (!live) { + const dropped = connection === 'live' connection = 'dropped' - conversationStore.markLiveApprovalsRecovered() + if (dropped) conversationStore.markLiveApprovalsRecovered() return } const reconnected = connection === 'dropped' @@ -1890,7 +1903,7 @@ export function useAgentSession(deps: AgentSessionDeps) { // socket has actually dropped that reading is wrong, so a reconnect takes // the job over; anything else defers to the job already running. if (running) { - if (cause !== 'reconnect' || running.cause === 'reconnect') return + if (cause !== 'reconnect') return running.controller.abort() } const recovery = new AbortController() @@ -1925,6 +1938,7 @@ export function useAgentSession(deps: AgentSessionDeps) { signal: AbortSignal ): Promise { let consecutiveThreadMissing = 0 + let previousPendingAskId: string | undefined for (const ms of TURN_RECOVERY_DELAYS_MS) { await delay(ms, { signal }) if (!isTurnLive(turn, generation)) return @@ -1936,22 +1950,32 @@ export function useAgentSession(deps: AgentSessionDeps) { if (outcome.kind === 'thread-missing' && consecutiveThreadMissing < 2) continue if (settleFinishedTurn(turn, outcome)) return - reconcileStreamingApproval(turn, outcome, cause) + const pendingAskId = streamingPendingAskId(outcome) + const pendingAskConfirmed = isRepeatedPendingAsk( + pendingAskId, + previousPendingAskId + ) + reconcileStreamingApproval(turn, outcome, cause, pendingAskConfirmed) + previousPendingAskId = pendingAskId } } function reconcileStreamingApproval( turn: LiveTurn, outcome: TurnOutcome, - cause: RecoveryCause + cause: RecoveryCause, + pendingAskConfirmed: boolean ): void { if (outcome.kind !== 'streaming') return - conversationStore.reconcileApprovalParts(turn, outcome.pendingAsk?.ask_id) - if (outcome.pendingAsk?.kind === 'run_approval') - conversationStore.forgetAskRecovered( - outcome.pendingAsk.ask_id, - turn.threadId - ) + if (pendingAskConfirmed) + conversationStore.reconcileApprovalParts(turn, outcome.pendingAsk?.ask_id) + if ( + !pendingAskConfirmed && + outcome.pendingAsk?.kind === 'run_approval' && + !conversationStore.isApprovalShown(turn, outcome.pendingAsk.ask_id) && + conversationStore.hasApprovalShown(turn) + ) + return restoreMissingApproval(turn, outcome.pendingAsk, cause) } @@ -2162,7 +2186,10 @@ export function useAgentSession(deps: AgentSessionDeps) { if (latest && !isTerminalTurnStatus(latest.status)) return { kind: 'streaming', - pendingAsk: latest.pending_ask + pendingAsk: rows.findLast( + (row) => + !isTerminalTurnStatus(row.status) && row.pending_ask !== undefined + )?.pending_ask } return { kind: 'terminal', diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts index fbffd2be7da..c438e315364 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts @@ -606,7 +606,7 @@ describe('agentEventTransport run approval', () => { ]) }) - it('closes open transcript parts when a duplicate approval frame arrives', () => { + it('leaves open transcript parts unchanged for a duplicate approval frame', () => { const message = drive([ runApproval(), delta('before the delayed frame'), @@ -620,7 +620,7 @@ describe('agentEventTransport run approval', () => { workflowId: 'workflow-1', workflowName: 'Portrait workflow' }, - { type: 'text', text: 'before the delayed frame', state: 'done' } + { type: 'text', text: 'before the delayed frame', state: 'streaming' } ]) }) diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts index 5d3516aefe8..aa2e5f296a6 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts @@ -350,17 +350,17 @@ export function createAgentEventTransport( ) return false } - dropDraft() - closeOpenText() - closeOpenThinking() - message.thinking = false - message.thinkingText = undefined if ( message.parts.some( (part) => part.type === 'runApproval' && part.askId === data.ask_id ) ) return true + dropDraft() + closeOpenText() + closeOpenThinking() + message.thinking = false + message.thinkingText = undefined const part: RunApprovalPart = { type: 'runApproval', askId: data.ask_id, diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 2cf017e7c52..7008cf3c904 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -1137,6 +1137,14 @@ export const useAgentConversationStore = defineStore( ) } + function hasApprovalShown(turn: LiveTurn): boolean { + return ( + liveTurnMessage(turn)?.parts.some( + (part) => part.type === 'runApproval' + ) ?? false + ) + } + function clearActive(): void { activeSlot.value = null } @@ -1424,6 +1432,7 @@ export const useAgentConversationStore = defineStore( historySkillNames, settleTurn, isApprovalShown, + hasApprovalShown, reset, hydrate } From 44336344714285d8a24d72e64b5f72c015f3b857 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 7 Oct 2026 04:33:51 +0000 Subject: [PATCH 13/36] fix(agent): make recovery authority connection-aware Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4203062892 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4203062901 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4203062913 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4203062922 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4203062928 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4203062936 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4203062944 --- .../composables/agent/useAgentSession.test.ts | 11 +- .../composables/agent/useAgentSession.ts | 133 ++++++++++++------ .../services/agent/agentEventTransport.ts | 3 +- .../stores/agent/agentConversationStore.ts | 57 +------- 4 files changed, 100 insertions(+), 104 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 18731b4c656..ed32cf568f7 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4778,12 +4778,7 @@ describe('useAgentSession (v1 composition root)', () => { expect(approvalParts(session)).toHaveLength(1) await session.answerAsk('turn-1:call-1', 'run') - expect(session.notices.value).toEqual([ - { - level: 'error', - text: 'This request was already answered somewhere else, so that answer was used instead of yours.' - } - ]) + expect(session.notices.value).toHaveLength(0) } finally { vi.useRealTimers() } @@ -4952,7 +4947,7 @@ describe('useAgentSession (v1 composition root)', () => { } }) - it('(g44) repeated polls do not erase restored-approval provenance', async () => { + it('(g44) a restored approval answered on a live socket is not uncertain', async () => { vi.useFakeTimers() try { const rest = parkedOnApprovalRest() @@ -4967,7 +4962,7 @@ describe('useAgentSession (v1 composition root)', () => { await vi.advanceTimersByTimeAsync(3_000) await session.answerAsk('turn-1:call-1', 'run') - expect(reportError).toHaveBeenCalledWith(expect.any(Error), { + expect(reportError).not.toHaveBeenCalledWith(expect.any(Error), { surface: 'agent', errorType: 'agent_ask_answer_unconfirmed' }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 82bd66ebe85..283cb8e5755 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -520,11 +520,14 @@ export function useAgentSession(deps: AgentSessionDeps) { let unsubscribeStatus: (() => void) | null = null let ownedGeneration = 0 let connection: SocketConnection = 'initial' + let initiallyOffline = false interface RunningRecovery { controller: AbortController cause: RecoveryCause + rerun: boolean } const recoveringTurns = new Map() + const recoveryPendingAskIds = new Map() /** * Asks recovery must not re-deliver: one it already restored, one a frame * has delivered, and any the user has answered or the server has resolved. @@ -1515,7 +1518,7 @@ export function useAgentSession(deps: AgentSessionDeps) { conversationStore.setAskAnswering(askId, true) try { await sendAnswer(currentThreadId, askId, selection) - reportAcceptedRecoveredAnswer(askId, currentThreadId) + reportAcceptedRecoveredAnswer() recordDeliveredAsk(askId) conversationStore.commitAsk(askId, currentThreadId) return true @@ -1545,7 +1548,7 @@ export function useAgentSession(deps: AgentSessionDeps) { { retryable: isRetryableRequestFailure(error, false) } ) pushError(i18n.global.t('agent.runApproval.answerFailed')) - } else reportRejectedRecoveredAnswer(askId, currentThreadId) + } else reportRejectedRecoveredAnswer() // The card is retired for good here, by resolution (409) or because // this client could never answer it. Either way a later recovery poll // must not draw it again. @@ -1575,11 +1578,8 @@ export function useAgentSession(deps: AgentSessionDeps) { } } - function reportAcceptedRecoveredAnswer( - askId: string, - threadId: string - ): void { - if (!conversationStore.isAskRecovered(askId, threadId)) return + function reportAcceptedRecoveredAnswer(): void { + if (connection !== 'dropped') return reportError(new Error('accepted restored approval cannot be verified'), { surface: 'agent', errorType: 'agent_ask_answer_unconfirmed' @@ -1587,11 +1587,8 @@ export function useAgentSession(deps: AgentSessionDeps) { pushError(i18n.global.t('agent.runApproval.answerUncertain')) } - function reportRejectedRecoveredAnswer( - askId: string, - threadId: string - ): void { - if (!conversationStore.isAskRecovered(askId, threadId)) return + function reportRejectedRecoveredAnswer(): void { + if (connection !== 'dropped') return reportError(new Error('restored run approval was already answered'), { surface: 'agent', errorType: 'agent_ask_answer_superseded' @@ -1657,6 +1654,8 @@ export function useAgentSession(deps: AgentSessionDeps) { function newChat( source?: Exclude ): void { + if (conversationStore.threadId !== null) + clearLateAskReports(conversationStore.threadId) readyThreadId.value = null loadGeneration++ promptEditState.value = { phase: 'idle' } @@ -1680,6 +1679,8 @@ export function useAgentSession(deps: AgentSessionDeps) { threadId: string, isNavigationCurrent: () => boolean = () => true ): Promise { + if (conversationStore.threadId !== null) + clearLateAskReports(conversationStore.threadId) readyThreadId.value = null const generation = ++loadGeneration promptEditState.value = { phase: 'idle' } @@ -1876,15 +1877,18 @@ export function useAgentSession(deps: AgentSessionDeps) { function onStatus(live: boolean): void { if (!live) { - const dropped = connection === 'live' + if (connection !== 'live') { + initiallyOffline = connection === 'initial' + return + } connection = 'dropped' - if (dropped) conversationStore.markLiveApprovalsRecovered() return } const reconnected = connection === 'dropped' connection = 'live' - if (!reconnected) return - reconcileLiveTurns('reconnect') + if (reconnected) reconcileLiveTurns('reconnect') + else if (initiallyOffline) reconcileLiveTurns('hydrate') + initiallyOffline = false } function reconcileLiveTurns(cause: RecoveryCause): void { @@ -1898,22 +1902,21 @@ export function useAgentSession(deps: AgentSessionDeps) { ): Promise { observeSkillTurn(turn) const key = recoveryKey(turn) - const running = recoveringTurns.get(key) - // A hydrate job files its ask reports as the benign publish race. Once the - // socket has actually dropped that reading is wrong, so a reconnect takes - // the job over; anything else defers to the job already running. - if (running) { - if (cause !== 'reconnect') return - running.controller.abort() - } + if (deferToRunningRecovery(key, cause)) return const recovery = new AbortController() - recoveringTurns.set(key, { controller: recovery, cause }) + const state = { controller: recovery, cause, rerun: false } + recoveringTurns.set(key, state) const deadline = setTimeout( () => recovery.abort(), TURN_RECOVERY_DEADLINE_MS ) try { - await recoverTurn(turn, cause, ownedGeneration, recovery.signal) + state.rerun ||= await recoverTurn( + turn, + cause, + ownedGeneration, + recovery.signal + ) } catch (error) { // `onStatus` floats this job (`void reconcileTurn(turn)`), so a rethrow // would land as an `unhandledrejection` the session never sees. Abort is @@ -1926,9 +1929,32 @@ export function useAgentSession(deps: AgentSessionDeps) { }) } finally { clearTimeout(deadline) - if (recoveringTurns.get(key)?.controller === recovery) - recoveringTurns.delete(key) + finishRecovery(key, recovery, state, turn) + } + } + + function deferToRunningRecovery(key: string, cause: RecoveryCause): boolean { + const running = recoveringTurns.get(key) + if (running === undefined) return false + if (cause !== 'reconnect') return true + if (running.cause === 'reconnect') { + running.rerun = true + return true } + running.controller.abort() + return false + } + + function finishRecovery( + key: string, + recovery: AbortController, + state: RunningRecovery, + turn: LiveTurn + ): void { + if (recoveringTurns.get(key)?.controller !== recovery) return + recoveringTurns.delete(key) + if (state.rerun && isTurnLive(turn, ownedGeneration)) + void reconcileTurn(turn, 'reconnect') } async function recoverTurn( @@ -1936,39 +1962,68 @@ export function useAgentSession(deps: AgentSessionDeps) { cause: RecoveryCause, generation: number, signal: AbortSignal - ): Promise { + ): Promise { let consecutiveThreadMissing = 0 - let previousPendingAskId: string | undefined + let previousPendingAskId = recoveryPendingAskIds.get(recoveryKey(turn)) + let pendingAskConfirmed = false for (const ms of TURN_RECOVERY_DELAYS_MS) { await delay(ms, { signal }) - if (!isTurnLive(turn, generation)) return + if (!isTurnLive(turn, generation)) return false + const deliveredBeforePoll = new Set(deliveredAsks) const outcome = await fetchTurnOutcome(turn, signal) signal.throwIfAborted() - if (!isTurnLive(turn, generation)) return + if (!isTurnLive(turn, generation)) return false consecutiveThreadMissing = outcome.kind === 'thread-missing' ? consecutiveThreadMissing + 1 : 0 if (outcome.kind === 'thread-missing' && consecutiveThreadMissing < 2) continue - if (settleFinishedTurn(turn, outcome)) return + if (settleFinishedTurn(turn, outcome)) return false const pendingAskId = streamingPendingAskId(outcome) - const pendingAskConfirmed = isRepeatedPendingAsk( + pendingAskConfirmed = recordPendingAskObservation( + turn, pendingAskId, previousPendingAskId ) - reconcileStreamingApproval(turn, outcome, cause, pendingAskConfirmed) + const deliveredDuringPoll = new Set( + [...deliveredAsks].filter((askId) => !deliveredBeforePoll.has(askId)) + ) + reconcileStreamingApproval( + turn, + outcome, + cause, + pendingAskConfirmed, + deliveredDuringPoll + ) previousPendingAskId = pendingAskId } + return previousPendingAskId !== undefined && !pendingAskConfirmed + } + + function recordPendingAskObservation( + turn: LiveTurn, + pendingAskId: string | undefined, + previousPendingAskId: string | undefined + ): boolean { + const key = recoveryKey(turn) + if (pendingAskId === undefined) recoveryPendingAskIds.delete(key) + else recoveryPendingAskIds.set(key, pendingAskId) + return isRepeatedPendingAsk(pendingAskId, previousPendingAskId) } function reconcileStreamingApproval( turn: LiveTurn, outcome: TurnOutcome, cause: RecoveryCause, - pendingAskConfirmed: boolean + pendingAskConfirmed: boolean, + deliveredDuringPoll: ReadonlySet ): void { if (outcome.kind !== 'streaming') return if (pendingAskConfirmed) - conversationStore.reconcileApprovalParts(turn, outcome.pendingAsk?.ask_id) + conversationStore.reconcileApprovalParts( + turn, + outcome.pendingAsk?.ask_id, + deliveredDuringPoll + ) if ( !pendingAskConfirmed && outcome.pendingAsk?.kind === 'run_approval' && @@ -2011,7 +2066,6 @@ export function useAgentSession(deps: AgentSessionDeps) { }) if (!conversationStore.isApprovalShown(turn, pendingAsk.ask_id)) return recordDeliveredAsk(pendingAsk.ask_id) - conversationStore.markAskRecovered(pendingAsk.ask_id, turn.threadId) reportRestoredApproval(turn, pendingAsk.ask_id, cause) } @@ -2059,7 +2113,6 @@ export function useAgentSession(deps: AgentSessionDeps) { event: Extract ): void { const askId = event.data.ask_id - conversationStore.forgetAskRecovered(askId, event.data.thread_id) if (conversationStore.isAskRetired(askId, event.data.thread_id)) { withdrawLateAskReport(event.data.thread_id, askId) return @@ -2203,8 +2256,8 @@ export function useAgentSession(deps: AgentSessionDeps) { function forgetDeletedThread(turn: LiveTurn): void { skillTurnUsage.delete(recoveryKey(turn)) + recoveryPendingAskIds.delete(recoveryKey(turn)) clearLateAskReports(turn.threadId) - conversationStore.forgetRecoveredAsks(turn.threadId) if (conversationStore.threadId !== turn.threadId) { conversationStore.settleTurn(turn, undefined) return diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts index aa2e5f296a6..af535717a86 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts @@ -335,8 +335,7 @@ export function createAgentEventTransport( * ask dropped in routing, so it is reported the same way. Generated-contract * kinds without a client renderer are tagged separately from unknown input. * - * An ask already on the message emits the closed transcript parts but is - * NOT reported: + * An ask already on the message is a no-op and is not reported: * turn recovery restores an unanswered ask off the persisted row, and the * server writes that row before it publishes the frame, so the two can * arrive in either order for the same `ask_id`. The second arrival is a diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 7008cf3c904..cd4e6cb1516 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -67,7 +67,6 @@ interface ActiveTurnSlot { * cannot leave the card disabled for the rest of the session. */ const ASK_RESOLUTION_GRACE_MS = 15_000 -const MAX_RECOVERED_ASK_THREADS = 128 function isLoadSkillPart(part: AssistantMessage['parts'][number]): boolean { return part.type === 'tool' && part.name === 'load_skill' @@ -434,9 +433,6 @@ export const useAgentConversationStore = defineStore( for (const entry of backgroundTurns.values()) if (entry.threadId === key) entry.transport.dropAskPart(askId) retiredAsksFor(key).add(askId) - const recovered = recoveredAskIds.get(key) - recovered?.delete(askId) - if (recovered?.size === 0) recoveredAskIds.delete(key) clearAskResolutionWatchdog(askId) submittedAskSelections.delete(askId) setAskAnswering(askId, false) @@ -455,7 +451,6 @@ export const useAgentConversationStore = defineStore( * nothing here rests on an ask id being unique across threads. */ const resolvedAskIds = new Map>() - const recoveredAskIds = new Map>() const threadKey = (owner?: string) => owner ?? threadId.value ?? '' @@ -470,44 +465,6 @@ export const useAgentConversationStore = defineStore( return resolvedAskIds.get(threadKey(owner))?.has(askId) ?? false } - function markAskRecovered(askId: string, owner?: string): void { - const key = threadKey(owner) - const recovered = recoveredAskIds.get(key) ?? new Set() - recovered.add(askId) - recoveredAskIds.delete(key) - recoveredAskIds.set(key, recovered) - if (recoveredAskIds.size > MAX_RECOVERED_ASK_THREADS) { - const oldest = recoveredAskIds.keys().next().value - if (oldest !== undefined) recoveredAskIds.delete(oldest) - } - } - - function isAskRecovered(askId: string, owner?: string): boolean { - return recoveredAskIds.get(threadKey(owner))?.has(askId) ?? false - } - - function forgetAskRecovered(askId: string, owner?: string): void { - const key = threadKey(owner) - const recovered = recoveredAskIds.get(key) - recovered?.delete(askId) - if (recovered?.size === 0) recoveredAskIds.delete(key) - } - - function forgetRecoveredAsks(owner: string): void { - recoveredAskIds.delete(threadKey(owner)) - } - - function markLiveApprovalsRecovered(): void { - const mark = (message: AssistantMessage, owner: string) => { - for (const part of message.parts) - if (part.type === 'runApproval') markAskRecovered(part.askId, owner) - } - const slot = activeSlot.value - if (slot !== null && slot.threadId !== null) - mark(slot.message, slot.threadId) - for (const entry of backgroundTurns.values()) - mark(entry.message, entry.threadId) - } /** * PM-1658: which way this client answered each ask. The server takes a * second answer from anywhere with 202 while committing only the FIRST, so @@ -1106,13 +1063,15 @@ export const useAgentConversationStore = defineStore( function reconcileApprovalParts( turn: LiveTurn, - pendingAskId?: string + pendingAskId?: string, + protectedAskIds: ReadonlySet = new Set() ): void { const message = liveTurnMessage(turn) if (message === null || pendingAskId === undefined) return const stale = message.parts.flatMap((part) => part.type === 'runApproval' && part.askId !== pendingAskId && + !protectedAskIds.has(part.askId) && submittedAskSelection(part.askId) === undefined && !answeringAskIds.value.has(part.askId) ? [part.askId] @@ -1213,11 +1172,6 @@ export const useAgentConversationStore = defineStore( ) ) ) - const recovered = recoveredAskIds.get(threadKey()) - if (recovered !== undefined) - for (const askId of recovered) - if (!named.has(askId)) recovered.delete(askId) - if (recovered?.size === 0) recoveredAskIds.delete(threadKey()) for (const askId of retired) if (!named.has(askId)) retired.delete(askId) for (const message of transcript.messages) message.parts = message.parts.filter( @@ -1412,11 +1366,6 @@ export const useAgentConversationStore = defineStore( commitAsk, retireAsk, isAskRetired, - markAskRecovered, - isAskRecovered, - forgetAskRecovered, - forgetRecoveredAsks, - markLiveApprovalsRecovered, reconcileApprovalParts, startTurn, ingest, From 3a432f19fc515f37a2349b1c04aea4efb517774e Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 7 Oct 2026 05:20:26 +0000 Subject: [PATCH 14/36] fix(agent): close approval recovery races --- .../composables/agent/useAgentSession.test.ts | 3 +- .../composables/agent/useAgentSession.ts | 129 ++++++++++++------ 2 files changed, 88 insertions(+), 44 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index ed32cf568f7..ebde60261bc 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -2037,12 +2037,13 @@ describe('useAgentSession (v1 composition root)', () => { const answerAsk = vi .fn() .mockRejectedValue(new AgentApiError('already answered', 409, undefined)) - const { source, emit } = fakeEvents() + const { source, emit, status } = fakeEvents() const session = useAgentSession({ rest: fakeRest({ answerAsk }), events: source }) session.start() + status(true) await session.sendMessage('build it') emit(runApproval('msg-1')) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 283cb8e5755..0182ec149bc 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -199,11 +199,11 @@ function streamingPendingAskId(outcome: TurnOutcome): string | undefined { return outcome.kind === 'streaming' ? outcome.pendingAsk?.ask_id : undefined } -function isRepeatedPendingAsk( - pendingAskId: string | undefined, - previousPendingAskId: string | undefined +function isTransientMissingTurn( + outcome: TurnOutcome, + consecutiveThreadMissing: number ): boolean { - return pendingAskId !== undefined && pendingAskId === previousPendingAskId + return outcome.kind === 'thread-missing' && consecutiveThreadMissing < 2 } function isTerminalTurnStatus( @@ -520,14 +520,23 @@ export function useAgentSession(deps: AgentSessionDeps) { let unsubscribeStatus: (() => void) | null = null let ownedGeneration = 0 let connection: SocketConnection = 'initial' + let resolutionChannelEpoch = 0 let initiallyOffline = false interface RunningRecovery { controller: AbortController cause: RecoveryCause rerun: boolean + rerunCause: RecoveryCause } const recoveringTurns = new Map() - const recoveryPendingAskIds = new Map() + interface PendingAskObservation { + pendingAskId: string | undefined + deliveredAskIds: Set + } + const recoveryPendingAskObservations = new Map< + string, + PendingAskObservation + >() /** * Asks recovery must not re-deliver: one it already restored, one a frame * has delivered, and any the user has answered or the server has resolved. @@ -550,6 +559,23 @@ export function useAgentSession(deps: AgentSessionDeps) { if (oldest !== undefined) deliveredAsks.delete(oldest) } + function recordPendingAskObservation( + key: string, + pendingAskId: string | undefined + ): PendingAskObservation { + const observation = { + pendingAskId, + deliveredAskIds: new Set(deliveredAsks) + } + recoveryPendingAskObservations.delete(key) + recoveryPendingAskObservations.set(key, observation) + if (recoveryPendingAskObservations.size > MAX_DELIVERED_ASKS) { + const oldest = recoveryPendingAskObservations.keys().next().value + if (oldest !== undefined) recoveryPendingAskObservations.delete(oldest) + } + return observation + } + function lateAskReportKey(threadId: string, askId: string): string { return `${threadId}\u0000${askId}` } @@ -906,6 +932,7 @@ export function useAgentSession(deps: AgentSessionDeps) { unsubscribeStatus = null for (const running of recoveringTurns.values()) running.controller.abort() recoveringTurns.clear() + recoveryPendingAskObservations.clear() clearLateAskReports() const stoppedGeneration = ownedGeneration queueMicrotask(() => { @@ -1516,9 +1543,15 @@ export function useAgentSession(deps: AgentSessionDeps) { if (answeringAskIds.value.has(askId)) return false conversationStore.recordAskSelection(askId, selection) conversationStore.setAskAnswering(askId, true) + const answerChannelEpoch = resolutionChannelEpoch + const answerStartedWithoutResolutionChannel = connection !== 'live' + const resolutionChannelWasLost = () => + answerStartedWithoutResolutionChannel || + resolutionChannelEpoch !== answerChannelEpoch try { await sendAnswer(currentThreadId, askId, selection) - reportAcceptedRecoveredAnswer() + if (answeringAskIds.value.has(askId)) + reportAcceptedRecoveredAnswer(resolutionChannelWasLost()) recordDeliveredAsk(askId) conversationStore.commitAsk(askId, currentThreadId) return true @@ -1548,7 +1581,7 @@ export function useAgentSession(deps: AgentSessionDeps) { { retryable: isRetryableRequestFailure(error, false) } ) pushError(i18n.global.t('agent.runApproval.answerFailed')) - } else reportRejectedRecoveredAnswer() + } else reportRejectedRecoveredAnswer(resolutionChannelWasLost()) // The card is retired for good here, by resolution (409) or because // this client could never answer it. Either way a later recovery poll // must not draw it again. @@ -1578,8 +1611,10 @@ export function useAgentSession(deps: AgentSessionDeps) { } } - function reportAcceptedRecoveredAnswer(): void { - if (connection !== 'dropped') return + function reportAcceptedRecoveredAnswer( + resolutionChannelWasLost: boolean + ): void { + if (!resolutionChannelWasLost) return reportError(new Error('accepted restored approval cannot be verified'), { surface: 'agent', errorType: 'agent_ask_answer_unconfirmed' @@ -1587,8 +1622,10 @@ export function useAgentSession(deps: AgentSessionDeps) { pushError(i18n.global.t('agent.runApproval.answerUncertain')) } - function reportRejectedRecoveredAnswer(): void { - if (connection !== 'dropped') return + function reportRejectedRecoveredAnswer( + resolutionChannelWasLost: boolean + ): void { + if (!resolutionChannelWasLost) return reportError(new Error('restored run approval was already answered'), { surface: 'agent', errorType: 'agent_ask_answer_superseded' @@ -1656,6 +1693,7 @@ export function useAgentSession(deps: AgentSessionDeps) { ): void { if (conversationStore.threadId !== null) clearLateAskReports(conversationStore.threadId) + recoveryPendingAskObservations.clear() readyThreadId.value = null loadGeneration++ promptEditState.value = { phase: 'idle' } @@ -1681,6 +1719,7 @@ export function useAgentSession(deps: AgentSessionDeps) { ): Promise { if (conversationStore.threadId !== null) clearLateAskReports(conversationStore.threadId) + recoveryPendingAskObservations.clear() readyThreadId.value = null const generation = ++loadGeneration promptEditState.value = { phase: 'idle' } @@ -1882,6 +1921,7 @@ export function useAgentSession(deps: AgentSessionDeps) { return } connection = 'dropped' + resolutionChannelEpoch += 1 return } const reconnected = connection === 'dropped' @@ -1904,19 +1944,25 @@ export function useAgentSession(deps: AgentSessionDeps) { const key = recoveryKey(turn) if (deferToRunningRecovery(key, cause)) return const recovery = new AbortController() - const state = { controller: recovery, cause, rerun: false } + const state = { + controller: recovery, + cause, + rerun: false, + rerunCause: cause + } recoveringTurns.set(key, state) const deadline = setTimeout( () => recovery.abort(), TURN_RECOVERY_DEADLINE_MS ) try { - state.rerun ||= await recoverTurn( + const needsRerun = await recoverTurn( turn, cause, ownedGeneration, recovery.signal ) + state.rerun ||= needsRerun } catch (error) { // `onStatus` floats this job (`void reconcileTurn(turn)`), so a rethrow // would land as an `unhandledrejection` the session never sees. Abort is @@ -1936,9 +1982,13 @@ export function useAgentSession(deps: AgentSessionDeps) { function deferToRunningRecovery(key: string, cause: RecoveryCause): boolean { const running = recoveringTurns.get(key) if (running === undefined) return false - if (cause !== 'reconnect') return true + if (cause !== 'reconnect') { + running.rerun = true + return true + } if (running.cause === 'reconnect') { running.rerun = true + running.rerunCause = 'reconnect' return true } running.controller.abort() @@ -1954,7 +2004,7 @@ export function useAgentSession(deps: AgentSessionDeps) { if (recoveringTurns.get(key)?.controller !== recovery) return recoveringTurns.delete(key) if (state.rerun && isTurnLive(turn, ownedGeneration)) - void reconcileTurn(turn, 'reconnect') + void reconcileTurn(turn, state.rerunCause) } async function recoverTurn( @@ -1964,50 +2014,43 @@ export function useAgentSession(deps: AgentSessionDeps) { signal: AbortSignal ): Promise { let consecutiveThreadMissing = 0 - let previousPendingAskId = recoveryPendingAskIds.get(recoveryKey(turn)) - let pendingAskConfirmed = false + const key = recoveryKey(turn) + let previousObservation = recoveryPendingAskObservations.get(key) + let hasUnconfirmedObservation = false for (const ms of TURN_RECOVERY_DELAYS_MS) { await delay(ms, { signal }) if (!isTurnLive(turn, generation)) return false - const deliveredBeforePoll = new Set(deliveredAsks) const outcome = await fetchTurnOutcome(turn, signal) signal.throwIfAborted() if (!isTurnLive(turn, generation)) return false consecutiveThreadMissing = outcome.kind === 'thread-missing' ? consecutiveThreadMissing + 1 : 0 - if (outcome.kind === 'thread-missing' && consecutiveThreadMissing < 2) - continue - if (settleFinishedTurn(turn, outcome)) return false + if (isTransientMissingTurn(outcome, consecutiveThreadMissing)) continue + if (settleFinishedTurn(turn, outcome)) { + recoveryPendingAskObservations.delete(key) + return false + } + if (outcome.kind !== 'streaming') continue const pendingAskId = streamingPendingAskId(outcome) - pendingAskConfirmed = recordPendingAskObservation( - turn, - pendingAskId, - previousPendingAskId - ) - const deliveredDuringPoll = new Set( - [...deliveredAsks].filter((askId) => !deliveredBeforePoll.has(askId)) + const pendingAskConfirmed = + previousObservation !== undefined && + previousObservation.pendingAskId === pendingAskId + hasUnconfirmedObservation = !pendingAskConfirmed + const deliveredSincePreviousObservation = new Set( + [...deliveredAsks].filter( + (askId) => !previousObservation?.deliveredAskIds.has(askId) + ) ) reconcileStreamingApproval( turn, outcome, cause, pendingAskConfirmed, - deliveredDuringPoll + deliveredSincePreviousObservation ) - previousPendingAskId = pendingAskId + previousObservation = recordPendingAskObservation(key, pendingAskId) } - return previousPendingAskId !== undefined && !pendingAskConfirmed - } - - function recordPendingAskObservation( - turn: LiveTurn, - pendingAskId: string | undefined, - previousPendingAskId: string | undefined - ): boolean { - const key = recoveryKey(turn) - if (pendingAskId === undefined) recoveryPendingAskIds.delete(key) - else recoveryPendingAskIds.set(key, pendingAskId) - return isRepeatedPendingAsk(pendingAskId, previousPendingAskId) + return hasUnconfirmedObservation } function reconcileStreamingApproval( @@ -2256,7 +2299,7 @@ export function useAgentSession(deps: AgentSessionDeps) { function forgetDeletedThread(turn: LiveTurn): void { skillTurnUsage.delete(recoveryKey(turn)) - recoveryPendingAskIds.delete(recoveryKey(turn)) + recoveryPendingAskObservations.delete(recoveryKey(turn)) clearLateAskReports(turn.threadId) if (conversationStore.threadId !== turn.threadId) { conversationStore.settleTurn(turn, undefined) From afa9da395b57311988ce19aaf0f7b20880bcf1ee Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 7 Oct 2026 05:48:50 +0000 Subject: [PATCH 15/36] fix(agent): confirm recovered approval absence --- .../composables/agent/useAgentSession.test.ts | 28 +++++++++++++++++++ .../composables/agent/useAgentSession.ts | 17 ++++++++--- .../stores/agent/agentConversationStore.ts | 4 +-- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index ebde60261bc..c9d7f4e8582 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4861,6 +4861,34 @@ describe('useAgentSession (v1 composition root)', () => { } }) + it('(g46) a confirmed row with no pending ask retires a stale approval', async () => { + vi.useFakeTimers() + try { + const rest = fakeRest({ + getMessages: vi.fn( + async (): Promise => [ + historyRow(1, 'user', 'msg-1', 'go'), + unparkedRow() + ] + ) + }) + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start({ restore: false }) + status(true) + await session.sendMessage('go') + emit(runApproval('msg-1')) + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(1_000) + + expect(approvalParts(session)).toHaveLength(0) + } finally { + vi.useRealTimers() + } + }) + it('(g41) a missing turn row cannot retire a live approval', async () => { vi.useFakeTimers() try { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 0182ec149bc..f70d88b1cbe 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -561,11 +561,12 @@ export function useAgentSession(deps: AgentSessionDeps) { function recordPendingAskObservation( key: string, - pendingAskId: string | undefined + pendingAskId: string | undefined, + deliveredAskIds: Set ): PendingAskObservation { const observation = { pendingAskId, - deliveredAskIds: new Set(deliveredAsks) + deliveredAskIds } recoveryPendingAskObservations.delete(key) recoveryPendingAskObservations.set(key, observation) @@ -2020,12 +2021,16 @@ export function useAgentSession(deps: AgentSessionDeps) { for (const ms of TURN_RECOVERY_DELAYS_MS) { await delay(ms, { signal }) if (!isTurnLive(turn, generation)) return false + const deliveredBeforeFetch = new Set(deliveredAsks) const outcome = await fetchTurnOutcome(turn, signal) signal.throwIfAborted() if (!isTurnLive(turn, generation)) return false consecutiveThreadMissing = outcome.kind === 'thread-missing' ? consecutiveThreadMissing + 1 : 0 - if (isTransientMissingTurn(outcome, consecutiveThreadMissing)) continue + if (isTransientMissingTurn(outcome, consecutiveThreadMissing)) { + hasUnconfirmedObservation = true + continue + } if (settleFinishedTurn(turn, outcome)) { recoveryPendingAskObservations.delete(key) return false @@ -2048,7 +2053,11 @@ export function useAgentSession(deps: AgentSessionDeps) { pendingAskConfirmed, deliveredSincePreviousObservation ) - previousObservation = recordPendingAskObservation(key, pendingAskId) + previousObservation = recordPendingAskObservation( + key, + pendingAskId, + deliveredBeforeFetch + ) } return hasUnconfirmedObservation } diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index cd4e6cb1516..e6f46cb95ed 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -1067,10 +1067,10 @@ export const useAgentConversationStore = defineStore( protectedAskIds: ReadonlySet = new Set() ): void { const message = liveTurnMessage(turn) - if (message === null || pendingAskId === undefined) return + if (message === null) return const stale = message.parts.flatMap((part) => part.type === 'runApproval' && - part.askId !== pendingAskId && + (pendingAskId === undefined || part.askId !== pendingAskId) && !protectedAskIds.has(part.askId) && submittedAskSelection(part.askId) === undefined && !answeringAskIds.value.has(part.askId) From 13f352c9ce60b47774b78090881f3e22f6db253a Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 7 Oct 2026 06:27:26 +0000 Subject: [PATCH 16/36] fix(agent): bound approval recovery uncertainty --- .../composables/agent/useAgentSession.test.ts | 13 ++- .../composables/agent/useAgentSession.ts | 81 +++++++++++++------ .../stores/agent/agentConversationStore.ts | 6 +- 3 files changed, 64 insertions(+), 36 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index c9d7f4e8582..70d7a121e02 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -2037,13 +2037,12 @@ describe('useAgentSession (v1 composition root)', () => { const answerAsk = vi .fn() .mockRejectedValue(new AgentApiError('already answered', 409, undefined)) - const { source, emit, status } = fakeEvents() + const { source, emit } = fakeEvents() const session = useAgentSession({ rest: fakeRest({ answerAsk }), events: source }) session.start() - status(true) await session.sendMessage('build it') emit(runApproval('msg-1')) @@ -4011,7 +4010,7 @@ describe('useAgentSession (v1 composition root)', () => { ) }) - it('(g10) a failing recovery fetch stays silent and leaves the turn live for the socket', async () => { + it('(g10) a failing recovery fetch gets one bounded follow-up and leaves the turn live', async () => { const rest = fakeRest({ getMessages: vi.fn(async (): Promise => { throw new TypeError('Failed to fetch') @@ -4029,7 +4028,7 @@ describe('useAgentSession (v1 composition root)', () => { status(true) await vi.advanceTimersByTimeAsync(60_000) - expect(rest.getMessages).toHaveBeenCalledTimes(6) + expect(rest.getMessages).toHaveBeenCalledTimes(11) expect(session.notices.value).toEqual([]) expect(session.isStreaming.value).toBe(true) @@ -4916,7 +4915,7 @@ describe('useAgentSession (v1 composition root)', () => { } }) - it('(g42) the newest assistant row decides whether a turn is terminal', async () => { + it('(g42) an open approval keeps the turn live despite a newer terminal row', async () => { vi.useFakeTimers() try { const rest = fakeRest({ @@ -4942,8 +4941,8 @@ describe('useAgentSession (v1 composition root)', () => { status(true) await vi.advanceTimersByTimeAsync(0) - expect(session.isStreaming.value).toBe(false) - expect(approvalParts(session)).toHaveLength(0) + expect(session.isStreaming.value).toBe(true) + expect(approvalParts(session)).toHaveLength(1) } finally { vi.useRealTimers() } diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index f70d88b1cbe..f7252d0daaa 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -521,7 +521,6 @@ export function useAgentSession(deps: AgentSessionDeps) { let ownedGeneration = 0 let connection: SocketConnection = 'initial' let resolutionChannelEpoch = 0 - let initiallyOffline = false interface RunningRecovery { controller: AbortController cause: RecoveryCause @@ -537,6 +536,8 @@ export function useAgentSession(deps: AgentSessionDeps) { string, PendingAskObservation >() + const recoveryThreadMissingCounts = new Map() + const automaticRecoveryReruns = new Map() /** * Asks recovery must not re-deliver: one it already restored, one a frame * has delivered, and any the user has answered or the server has resolved. @@ -577,6 +578,17 @@ export function useAgentSession(deps: AgentSessionDeps) { return observation } + function recordThreadAvailability(key: string, outcome: TurnOutcome): number { + const previous = recoveryThreadMissingCounts.get(key) ?? 0 + if (outcome.kind === 'thread-missing') { + const consecutive = previous + 1 + recoveryThreadMissingCounts.set(key, consecutive) + return consecutive + } + if (outcome.kind === 'streaming') recoveryThreadMissingCounts.delete(key) + return previous + } + function lateAskReportKey(threadId: string, askId: string): string { return `${threadId}\u0000${askId}` } @@ -934,6 +946,8 @@ export function useAgentSession(deps: AgentSessionDeps) { for (const running of recoveringTurns.values()) running.controller.abort() recoveringTurns.clear() recoveryPendingAskObservations.clear() + recoveryThreadMissingCounts.clear() + automaticRecoveryReruns.clear() clearLateAskReports() const stoppedGeneration = ownedGeneration queueMicrotask(() => { @@ -1545,7 +1559,7 @@ export function useAgentSession(deps: AgentSessionDeps) { conversationStore.recordAskSelection(askId, selection) conversationStore.setAskAnswering(askId, true) const answerChannelEpoch = resolutionChannelEpoch - const answerStartedWithoutResolutionChannel = connection !== 'live' + const answerStartedWithoutResolutionChannel = connection === 'dropped' const resolutionChannelWasLost = () => answerStartedWithoutResolutionChannel || resolutionChannelEpoch !== answerChannelEpoch @@ -1695,6 +1709,8 @@ export function useAgentSession(deps: AgentSessionDeps) { if (conversationStore.threadId !== null) clearLateAskReports(conversationStore.threadId) recoveryPendingAskObservations.clear() + recoveryThreadMissingCounts.clear() + automaticRecoveryReruns.clear() readyThreadId.value = null loadGeneration++ promptEditState.value = { phase: 'idle' } @@ -1721,6 +1737,8 @@ export function useAgentSession(deps: AgentSessionDeps) { if (conversationStore.threadId !== null) clearLateAskReports(conversationStore.threadId) recoveryPendingAskObservations.clear() + recoveryThreadMissingCounts.clear() + automaticRecoveryReruns.clear() readyThreadId.value = null const generation = ++loadGeneration promptEditState.value = { phase: 'idle' } @@ -1883,6 +1901,7 @@ export function useAgentSession(deps: AgentSessionDeps) { // matters, so on its own it would re-enable a card it cannot remove. conversationStore.retireAsk(event.data.ask_id, event.data.thread_id) recordDeliveredAsk(event.data.ask_id) + withdrawLateAskReport(event.data.thread_id, event.data.ask_id) onAskResolved?.(event.data.ask_id) } for (const turn of conversationStore.liveTurns()) observeSkillTurn(turn) @@ -1917,19 +1936,16 @@ export function useAgentSession(deps: AgentSessionDeps) { function onStatus(live: boolean): void { if (!live) { - if (connection !== 'live') { - initiallyOffline = connection === 'initial' - return - } + if (connection !== 'live') return connection = 'dropped' resolutionChannelEpoch += 1 return } const reconnected = connection === 'dropped' + const firstLive = connection === 'initial' connection = 'live' if (reconnected) reconcileLiveTurns('reconnect') - else if (initiallyOffline) reconcileLiveTurns('hydrate') - initiallyOffline = false + else if (firstLive) reconcileLiveTurns('hydrate') } function reconcileLiveTurns(cause: RecoveryCause): void { @@ -1963,7 +1979,10 @@ export function useAgentSession(deps: AgentSessionDeps) { ownedGeneration, recovery.signal ) - state.rerun ||= needsRerun + if (needsRerun && (automaticRecoveryReruns.get(key) ?? 0) < 1) { + automaticRecoveryReruns.set(key, 1) + state.rerun = true + } } catch (error) { // `onStatus` floats this job (`void reconcileTurn(turn)`), so a rethrow // would land as an `unhandledrejection` the session never sees. Abort is @@ -2014,7 +2033,6 @@ export function useAgentSession(deps: AgentSessionDeps) { generation: number, signal: AbortSignal ): Promise { - let consecutiveThreadMissing = 0 const key = recoveryKey(turn) let previousObservation = recoveryPendingAskObservations.get(key) let hasUnconfirmedObservation = false @@ -2025,17 +2043,22 @@ export function useAgentSession(deps: AgentSessionDeps) { const outcome = await fetchTurnOutcome(turn, signal) signal.throwIfAborted() if (!isTurnLive(turn, generation)) return false - consecutiveThreadMissing = - outcome.kind === 'thread-missing' ? consecutiveThreadMissing + 1 : 0 + const consecutiveThreadMissing = recordThreadAvailability(key, outcome) if (isTransientMissingTurn(outcome, consecutiveThreadMissing)) { hasUnconfirmedObservation = true continue } if (settleFinishedTurn(turn, outcome)) { recoveryPendingAskObservations.delete(key) + recoveryThreadMissingCounts.delete(key) + automaticRecoveryReruns.delete(key) return false } - if (outcome.kind !== 'streaming') continue + if (outcome.kind !== 'streaming') { + hasUnconfirmedObservation = true + continue + } + automaticRecoveryReruns.delete(key) const pendingAskId = streamingPendingAskId(outcome) const pendingAskConfirmed = previousObservation !== undefined && @@ -2230,19 +2253,20 @@ export function useAgentSession(deps: AgentSessionDeps) { } function settleFinishedTurn(turn: LiveTurn, outcome: TurnOutcome): boolean { - switch (outcome.kind) { - case 'terminal': - if ( - outcome.parts?.some( - (part) => part.type === 'tool' && part.name === 'load_skill' + switch (outcome.kind) { + case 'terminal': + if ( + outcome.parts?.some( + (part) => part.type === 'tool' && part.name === 'load_skill' ) ) { observeSkillTurn(turn) const usage = skillTurnUsage.get(recoveryKey(turn)) - if (usage) usage.used = true - } - finishSkillTurn(turn) - conversationStore.settleTurn(turn, outcome.parts) + if (usage) usage.used = true + } + finishSkillTurn(turn) + clearLateAskReports(turn.threadId) + conversationStore.settleTurn(turn, outcome.parts) markStoppedTurnReady(turn) return true case 'thread-missing': @@ -2287,14 +2311,17 @@ export function useAgentSession(deps: AgentSessionDeps) { entry.role === 'assistant' && entry.turn_id === anchor.turn_id ) .sort((a, b) => a.seq - b.seq) + const openPendingAsk = rows.findLast( + (row) => + !isTerminalTurnStatus(row.status) && row.pending_ask !== undefined + )?.pending_ask + if (openPendingAsk !== undefined) + return { kind: 'streaming', pendingAsk: openPendingAsk } const latest = rows.at(-1) if (latest && !isTerminalTurnStatus(latest.status)) return { kind: 'streaming', - pendingAsk: rows.findLast( - (row) => - !isTerminalTurnStatus(row.status) && row.pending_ask !== undefined - )?.pending_ask + pendingAsk: undefined } return { kind: 'terminal', @@ -2309,6 +2336,8 @@ export function useAgentSession(deps: AgentSessionDeps) { function forgetDeletedThread(turn: LiveTurn): void { skillTurnUsage.delete(recoveryKey(turn)) recoveryPendingAskObservations.delete(recoveryKey(turn)) + recoveryThreadMissingCounts.delete(recoveryKey(turn)) + automaticRecoveryReruns.delete(recoveryKey(turn)) clearLateAskReports(turn.threadId) if (conversationStore.threadId !== turn.threadId) { conversationStore.settleTurn(turn, undefined) diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index e6f46cb95ed..91001c7f357 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -406,7 +406,7 @@ export const useAgentConversationStore = defineStore( * has to be told, or whichever one is asked to republish next puts the * card back. */ - function retireAsk(askId: string, owner?: string): void { + function retireAsk(askId: string, owner?: string, permanent = true): void { const key = threadKey(owner) const withoutAsk = (parts: AssistantMessage['parts']) => parts.filter( @@ -432,7 +432,7 @@ export const useAgentConversationStore = defineStore( // screen if this did not strip it. for (const entry of backgroundTurns.values()) if (entry.threadId === key) entry.transport.dropAskPart(askId) - retiredAsksFor(key).add(askId) + if (permanent) retiredAsksFor(key).add(askId) clearAskResolutionWatchdog(askId) submittedAskSelections.delete(askId) setAskAnswering(askId, false) @@ -1077,7 +1077,7 @@ export const useAgentConversationStore = defineStore( ? [part.askId] : [] ) - for (const askId of stale) retireAsk(askId, turn.threadId) + for (const askId of stale) retireAsk(askId, turn.threadId, false) } /** From 365e875703d8eaafaa40595ee38876c0f9eb4992 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 7 Oct 2026 06:55:21 +0000 Subject: [PATCH 17/36] fix(agent): align approval recovery ledgers --- .../composables/agent/useAgentSession.ts | 109 +++++++++++++----- .../agent/services/agent/agentTranscript.ts | 16 ++- .../stores/agent/agentConversationStore.ts | 9 +- 3 files changed, 100 insertions(+), 34 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index f7252d0daaa..ba7f5b7ad02 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -549,7 +549,11 @@ export function useAgentSession(deps: AgentSessionDeps) { const deliveredAsks = new Set() const lateAskReports = new Map< string, - { threadId: string; timer: ReturnType } + { + threadId: string + messageId: string + timer: ReturnType + } >() function recordDeliveredAsk(askId: string): void { @@ -585,10 +589,23 @@ export function useAgentSession(deps: AgentSessionDeps) { recoveryThreadMissingCounts.set(key, consecutive) return consecutive } - if (outcome.kind === 'streaming') recoveryThreadMissingCounts.delete(key) + recoveryThreadMissingCounts.delete(key) return previous } + function pruneRecoveryLedgers(): void { + for (const ledger of [ + recoveryThreadMissingCounts, + automaticRecoveryReruns + ]) { + while (ledger.size > MAX_DELIVERED_ASKS) { + const oldest = ledger.keys().next().value + if (oldest === undefined) break + ledger.delete(oldest) + } + } + } + function lateAskReportKey(threadId: string, askId: string): string { return `${threadId}\u0000${askId}` } @@ -1596,7 +1613,11 @@ export function useAgentSession(deps: AgentSessionDeps) { { retryable: isRetryableRequestFailure(error, false) } ) pushError(i18n.global.t('agent.runApproval.answerFailed')) - } else reportRejectedRecoveredAnswer(resolutionChannelWasLost()) + } else + reportRejectedRecoveredAnswerIfPending( + askId, + resolutionChannelWasLost() + ) // The card is retired for good here, by resolution (409) or because // this client could never answer it. Either way a later recovery poll // must not draw it again. @@ -1648,6 +1669,14 @@ export function useAgentSession(deps: AgentSessionDeps) { pushError(i18n.global.t('agent.runApproval.answerSuperseded')) } + function reportRejectedRecoveredAnswerIfPending( + askId: string, + resolutionChannelWasLost: boolean + ): void { + if (!answeringAskIds.value.has(askId)) return + reportRejectedRecoveredAnswer(resolutionChannelWasLost) + } + /** * PM-1658: the server commits the FIRST answer an ask receives and takes * every later one with 202 while replaying the stored selection, so another @@ -1981,8 +2010,9 @@ export function useAgentSession(deps: AgentSessionDeps) { ) if (needsRerun && (automaticRecoveryReruns.get(key) ?? 0) < 1) { automaticRecoveryReruns.set(key, 1) + pruneRecoveryLedgers() state.rerun = true - } + } else if (!needsRerun) automaticRecoveryReruns.delete(key) } catch (error) { // `onStatus` floats this job (`void reconcileTurn(turn)`), so a rethrow // would land as an `unhandledrejection` the session never sees. Abort is @@ -2044,6 +2074,7 @@ export function useAgentSession(deps: AgentSessionDeps) { signal.throwIfAborted() if (!isTurnLive(turn, generation)) return false const consecutiveThreadMissing = recordThreadAvailability(key, outcome) + pruneRecoveryLedgers() if (isTransientMissingTurn(outcome, consecutiveThreadMissing)) { hasUnconfirmedObservation = true continue @@ -2058,7 +2089,6 @@ export function useAgentSession(deps: AgentSessionDeps) { hasUnconfirmedObservation = true continue } - automaticRecoveryReruns.delete(key) const pendingAskId = streamingPendingAskId(outcome) const pendingAskConfirmed = previousObservation !== undefined && @@ -2094,11 +2124,7 @@ export function useAgentSession(deps: AgentSessionDeps) { ): void { if (outcome.kind !== 'streaming') return if (pendingAskConfirmed) - conversationStore.reconcileApprovalParts( - turn, - outcome.pendingAsk?.ask_id, - deliveredDuringPoll - ) + reconcileConfirmedApproval(turn, outcome, deliveredDuringPoll) if ( !pendingAskConfirmed && outcome.pendingAsk?.kind === 'run_approval' && @@ -2109,6 +2135,19 @@ export function useAgentSession(deps: AgentSessionDeps) { restoreMissingApproval(turn, outcome.pendingAsk, cause) } + function reconcileConfirmedApproval( + turn: LiveTurn, + outcome: Extract, + deliveredDuringPoll: ReadonlySet + ): void { + const hiddenAskIds = conversationStore.reconcileApprovalParts( + turn, + outcome.pendingAsk?.ask_id, + deliveredDuringPoll + ) + for (const askId of hiddenAskIds) deliveredAsks.delete(askId) + } + /** * PM-1738: a turn parked on a tool-call approval stays `streaming` until the * user answers, so a socket drop that swallowed the `agent_ask` frame leaves @@ -2165,6 +2204,7 @@ export function useAgentSession(deps: AgentSessionDeps) { const key = lateAskReportKey(turn.threadId, askId) lateAskReports.set(key, { threadId: turn.threadId, + messageId: turn.messageId, timer: setTimeout(() => { lateAskReports.delete(key) sendRestoredApprovalReport(turn, askId, cause, 'error') @@ -2253,20 +2293,28 @@ export function useAgentSession(deps: AgentSessionDeps) { } function settleFinishedTurn(turn: LiveTurn, outcome: TurnOutcome): boolean { - switch (outcome.kind) { - case 'terminal': - if ( - outcome.parts?.some( - (part) => part.type === 'tool' && part.name === 'load_skill' + switch (outcome.kind) { + case 'terminal': + if ( + outcome.parts?.some( + (part) => part.type === 'tool' && part.name === 'load_skill' ) ) { observeSkillTurn(turn) const usage = skillTurnUsage.get(recoveryKey(turn)) - if (usage) usage.used = true - } - finishSkillTurn(turn) - clearLateAskReports(turn.threadId) - conversationStore.settleTurn(turn, outcome.parts) + if (usage) usage.used = true + } + finishSkillTurn(turn) + for (const [key, scheduled] of lateAskReports) { + if ( + scheduled.threadId !== turn.threadId || + scheduled.messageId !== turn.messageId + ) + continue + clearTimeout(scheduled.timer) + lateAskReports.delete(key) + } + conversationStore.settleTurn(turn, outcome.parts) markStoppedTurnReady(turn) return true case 'thread-missing': @@ -2311,18 +2359,25 @@ export function useAgentSession(deps: AgentSessionDeps) { entry.role === 'assistant' && entry.turn_id === anchor.turn_id ) .sort((a, b) => a.seq - b.seq) - const openPendingAsk = rows.findLast( - (row) => - !isTerminalTurnStatus(row.status) && row.pending_ask !== undefined - )?.pending_ask - if (openPendingAsk !== undefined) - return { kind: 'streaming', pendingAsk: openPendingAsk } const latest = rows.at(-1) if (latest && !isTerminalTurnStatus(latest.status)) return { kind: 'streaming', - pendingAsk: undefined + pendingAsk: rows.findLast( + (row) => + !isTerminalTurnStatus(row.status) && row.pending_ask !== undefined + )?.pending_ask } + const openPendingAsk = rows.findLast( + (row) => + !isTerminalTurnStatus(row.status) && + row.pending_ask !== undefined && + conversationStore.submittedAskSelection(row.pending_ask.ask_id) === + undefined && + !conversationStore.isAskRetired(row.pending_ask.ask_id, turn.threadId) + )?.pending_ask + if (openPendingAsk !== undefined) + return { kind: 'streaming', pendingAsk: openPendingAsk } return { kind: 'terminal', parts: terminalRecoveryParts(rows) diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.ts index 13d4c76e8b1..25f397f7613 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.ts @@ -461,7 +461,8 @@ function liveTranscriptTurn( } export function normalizeAgentTranscript( - history: AgentMessages + history: AgentMessages, + isAskRetired: (askId: string) => boolean = () => false ): NormalizedAgentTranscript { const userTexts = new Map() const userAttachments = new Map() @@ -492,10 +493,17 @@ export function normalizeAgentTranscript( if (workflowId) latestWorkflowId = workflowId } if (row.role === 'assistant') { - pendingByTurn.set( - turnId, - recordAssistantRow(row, turnId, text, assistants) + const pending = recordAssistantRow(row, turnId, text, assistants) + const priorApproval = pendingByTurn + .get(turnId) + ?.message.parts.findLast((part) => part.type === 'runApproval') + if ( + pending === undefined && + priorApproval !== undefined && + !isAskRetired(priorApproval.askId) ) + continue + else pendingByTurn.set(turnId, pending) } } diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 91001c7f357..eb72ec68564 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -1065,9 +1065,9 @@ export const useAgentConversationStore = defineStore( turn: LiveTurn, pendingAskId?: string, protectedAskIds: ReadonlySet = new Set() - ): void { + ): string[] { const message = liveTurnMessage(turn) - if (message === null) return + if (message === null) return [] const stale = message.parts.flatMap((part) => part.type === 'runApproval' && (pendingAskId === undefined || part.askId !== pendingAskId) && @@ -1078,6 +1078,7 @@ export const useAgentConversationStore = defineStore( : [] ) for (const askId of stale) retireAsk(askId, turn.threadId, false) + return stale } /** @@ -1183,7 +1184,9 @@ export const useAgentConversationStore = defineStore( if (activeSlot.value) rememberDepartedActiveTurn('no-live-turn') disposeActiveAndSettledTransports() clearActive() - const transcript = normalizeAgentTranscript(history) + const transcript = normalizeAgentTranscript(history, (askId) => + isAskRetired(askId) + ) // Only the card is retired, never the turn: answering it is what lets // the turn RESUME, so it is still live and still needs a transport, or // every frame of the rest of it is dropped and the row stays "Working…" From d39c16a65debe37ae9b0e26c2bcb0dbe0c231c4b Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 8 Oct 2026 03:21:15 +0000 Subject: [PATCH 18/36] fix(agent): settle recovered approvals authoritatively Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4204281502 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4204281506 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4204281523 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4204281529 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4204281535 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4204281538 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4204281546 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4204281560 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4204281566 --- .../composables/agent/useAgentSession.test.ts | 6 +-- .../composables/agent/useAgentSession.ts | 47 ++++++++----------- .../services/agent/agentTranscript.test.ts | 34 ++++++++++++++ .../agent/services/agent/agentTranscript.ts | 12 ++--- .../agent/agentConversationStore.test.ts | 1 + .../stores/agent/agentConversationStore.ts | 19 ++++---- 6 files changed, 74 insertions(+), 45 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 70d7a121e02..a5e2e47e6b2 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4915,7 +4915,7 @@ describe('useAgentSession (v1 composition root)', () => { } }) - it('(g42) an open approval keeps the turn live despite a newer terminal row', async () => { + it('(g42) a newer terminal row retires an older pending approval', async () => { vi.useFakeTimers() try { const rest = fakeRest({ @@ -4941,8 +4941,8 @@ describe('useAgentSession (v1 composition root)', () => { status(true) await vi.advanceTimersByTimeAsync(0) - expect(session.isStreaming.value).toBe(true) - expect(approvalParts(session)).toHaveLength(1) + expect(session.isStreaming.value).toBe(false) + expect(approvalParts(session)).toHaveLength(0) } finally { vi.useRealTimers() } diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index ba7f5b7ad02..6a0158a4f1c 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -235,10 +235,11 @@ function mergeAdjacentTextParts( } function terminalRecoveryParts( - rows: AgentMessages + rows: AgentMessages, + isAskUnavailable: (askId: string) => boolean ): AssistantMessage['parts'] | undefined { const parts = mergeAdjacentTextParts( - normalizeAgentTranscript(rows).messages[0]?.parts ?? [] + normalizeAgentTranscript(rows, isAskUnavailable).messages[0]?.parts ?? [] ) if (parts.length > 0) return parts if (rows.every((row) => row.status !== 'error')) return undefined @@ -526,6 +527,7 @@ export function useAgentSession(deps: AgentSessionDeps) { cause: RecoveryCause rerun: boolean rerunCause: RecoveryCause + automaticRerun: boolean } const recoveringTurns = new Map() interface PendingAskObservation { @@ -586,6 +588,7 @@ export function useAgentSession(deps: AgentSessionDeps) { const previous = recoveryThreadMissingCounts.get(key) ?? 0 if (outcome.kind === 'thread-missing') { const consecutive = previous + 1 + recoveryThreadMissingCounts.delete(key) recoveryThreadMissingCounts.set(key, consecutive) return consecutive } @@ -1735,11 +1738,6 @@ export function useAgentSession(deps: AgentSessionDeps) { function newChat( source?: Exclude ): void { - if (conversationStore.threadId !== null) - clearLateAskReports(conversationStore.threadId) - recoveryPendingAskObservations.clear() - recoveryThreadMissingCounts.clear() - automaticRecoveryReruns.clear() readyThreadId.value = null loadGeneration++ promptEditState.value = { phase: 'idle' } @@ -1763,11 +1761,6 @@ export function useAgentSession(deps: AgentSessionDeps) { threadId: string, isNavigationCurrent: () => boolean = () => true ): Promise { - if (conversationStore.threadId !== null) - clearLateAskReports(conversationStore.threadId) - recoveryPendingAskObservations.clear() - recoveryThreadMissingCounts.clear() - automaticRecoveryReruns.clear() readyThreadId.value = null const generation = ++loadGeneration promptEditState.value = { phase: 'idle' } @@ -1984,17 +1977,20 @@ export function useAgentSession(deps: AgentSessionDeps) { async function reconcileTurn( turn: LiveTurn, - cause: RecoveryCause + cause: RecoveryCause, + automaticRerun = false ): Promise { observeSkillTurn(turn) const key = recoveryKey(turn) if (deferToRunningRecovery(key, cause)) return + if (!automaticRerun) automaticRecoveryReruns.delete(key) const recovery = new AbortController() const state = { controller: recovery, cause, rerun: false, - rerunCause: cause + rerunCause: cause, + automaticRerun: false } recoveringTurns.set(key, state) const deadline = setTimeout( @@ -2009,9 +2005,11 @@ export function useAgentSession(deps: AgentSessionDeps) { recovery.signal ) if (needsRerun && (automaticRecoveryReruns.get(key) ?? 0) < 1) { + automaticRecoveryReruns.delete(key) automaticRecoveryReruns.set(key, 1) pruneRecoveryLedgers() state.rerun = true + state.automaticRerun = true } else if (!needsRerun) automaticRecoveryReruns.delete(key) } catch (error) { // `onStatus` floats this job (`void reconcileTurn(turn)`), so a rethrow @@ -2054,7 +2052,7 @@ export function useAgentSession(deps: AgentSessionDeps) { if (recoveringTurns.get(key)?.controller !== recovery) return recoveringTurns.delete(key) if (state.rerun && isTurnLive(turn, ownedGeneration)) - void reconcileTurn(turn, state.rerunCause) + void reconcileTurn(turn, state.rerunCause, state.automaticRerun) } async function recoverTurn( @@ -2238,7 +2236,7 @@ export function useAgentSession(deps: AgentSessionDeps) { } if ( deliveredAsks.has(askId) && - (!conversationStore.isApprovalShown(turn, askId) || + (conversationStore.isApprovalShown(turn, askId) || conversationStore.submittedAskSelection(askId) !== undefined) ) { withdrawLateAskReport(event.data.thread_id, askId) @@ -2368,19 +2366,14 @@ export function useAgentSession(deps: AgentSessionDeps) { !isTerminalTurnStatus(row.status) && row.pending_ask !== undefined )?.pending_ask } - const openPendingAsk = rows.findLast( - (row) => - !isTerminalTurnStatus(row.status) && - row.pending_ask !== undefined && - conversationStore.submittedAskSelection(row.pending_ask.ask_id) === - undefined && - !conversationStore.isAskRetired(row.pending_ask.ask_id, turn.threadId) - )?.pending_ask - if (openPendingAsk !== undefined) - return { kind: 'streaming', pendingAsk: openPendingAsk } return { kind: 'terminal', - parts: terminalRecoveryParts(rows) + parts: terminalRecoveryParts( + rows, + (askId) => + conversationStore.isAskRetired(askId, turn.threadId) || + conversationStore.submittedAskSelection(askId) !== undefined + ) } } catch (error) { if (signal.aborted) throw error diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts index 9ef4987f798..a4281162dca 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts @@ -689,6 +689,40 @@ describe('normalizeAgentTranscript', () => { ]) }) + it('keeps an older available approval when the newest streaming row has none', () => { + const first = row(1, 'assistant', 'turn-a', '', 'row-1') + first.status = 'streaming' + first.pending_ask = { + message_id: 'row-1', + ask_id: 'ask-available', + kind: 'run_approval', + prompt: 'Run it?', + options: [], + min_selections: 1, + max_selections: 1, + allow_other: false + } + const second = row(2, 'assistant', 'turn-a', '', 'row-2') + second.status = 'streaming' + second.pending_ask = { + ...first.pending_ask, + message_id: 'row-2', + ask_id: 'ask-retired' + } + const latest = row(3, 'assistant', 'turn-a', '', 'row-3') + latest.status = 'streaming' + + const transcript = normalizeAgentTranscript( + [first, second, latest], + (askId) => askId === 'ask-retired' + ) + + expect(transcript.pending?.message.streaming).toBe(true) + expect(transcript.messages[0].parts).toContainEqual( + expect.objectContaining({ askId: 'ask-available' }) + ) + }) + // Only a transport can resolve an ask, and a demoted row is not getting one. // Kept, the card renders enabled and answering it posts against a turn that // is no longer active -- a button that silently does nothing. diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.ts index 25f397f7613..dbb088fffe7 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.ts @@ -462,7 +462,7 @@ function liveTranscriptTurn( export function normalizeAgentTranscript( history: AgentMessages, - isAskRetired: (askId: string) => boolean = () => false + isAskUnavailable: (askId: string) => boolean = () => false ): NormalizedAgentTranscript { const userTexts = new Map() const userAttachments = new Map() @@ -496,12 +496,10 @@ export function normalizeAgentTranscript( const pending = recordAssistantRow(row, turnId, text, assistants) const priorApproval = pendingByTurn .get(turnId) - ?.message.parts.findLast((part) => part.type === 'runApproval') - if ( - pending === undefined && - priorApproval !== undefined && - !isAskRetired(priorApproval.askId) - ) + ?.message.parts.some( + (part) => part.type === 'runApproval' && !isAskUnavailable(part.askId) + ) + if (row.status === 'streaming' && pending === undefined && priorApproval) continue else pendingByTurn.set(turnId, pending) } diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index 3841e6019e7..88b61969cbf 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -893,6 +893,7 @@ describe('useAgentConversationStore', () => { store.hydrate(parkedTranscript) store.retireAsk('turn-1:call-1') + store.hydrate(parkedTranscript) store.hydrate(parkedTranscript) expect( diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index eb72ec68564..a4c57f0f797 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -1163,14 +1163,15 @@ export const useAgentConversationStore = defineStore( * raised the card is left exactly as the transcript describes it. */ function dropResolvedAsks( - transcript: ReturnType + transcript: ReturnType, + history: AgentMessages ): void { const retired = retiredAsksFor() const named = new Set( - transcript.messages.flatMap((message) => - message.parts.flatMap((part) => - part.type === 'runApproval' ? [part.askId] : [] - ) + history.flatMap((row) => + row.pending_ask?.kind === 'run_approval' + ? [row.pending_ask.ask_id] + : [] ) ) for (const askId of retired) if (!named.has(askId)) retired.delete(askId) @@ -1184,14 +1185,16 @@ export const useAgentConversationStore = defineStore( if (activeSlot.value) rememberDepartedActiveTurn('no-live-turn') disposeActiveAndSettledTransports() clearActive() - const transcript = normalizeAgentTranscript(history, (askId) => - isAskRetired(askId) + const transcript = normalizeAgentTranscript( + history, + (askId) => + isAskRetired(askId) || submittedAskSelection(askId) !== undefined ) // Only the card is retired, never the turn: answering it is what lets // the turn RESUME, so it is still live and still needs a transport, or // every frame of the rest of it is dropped and the row stays "Working…" // with nothing able to settle it. - dropResolvedAsks(transcript) + dropResolvedAsks(transcript, history) messages.value = transcript.messages resolvedPaywallIds.value = new Set() userTexts.value = transcript.userTexts From 7959a7fb08c6f4890c26bb96a31ab6e63de9aa97 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 8 Oct 2026 03:57:29 +0000 Subject: [PATCH 19/36] refactor(agent): scope recovery reruns to each job Addresses Greenlight design and conformance feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#issuecomment-6002656120 --- .../composables/agent/useAgentSession.ts | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 6a0158a4f1c..be65c5fa930 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -539,7 +539,6 @@ export function useAgentSession(deps: AgentSessionDeps) { PendingAskObservation >() const recoveryThreadMissingCounts = new Map() - const automaticRecoveryReruns = new Map() /** * Asks recovery must not re-deliver: one it already restored, one a frame * has delivered, and any the user has answered or the server has resolved. @@ -597,15 +596,10 @@ export function useAgentSession(deps: AgentSessionDeps) { } function pruneRecoveryLedgers(): void { - for (const ledger of [ - recoveryThreadMissingCounts, - automaticRecoveryReruns - ]) { - while (ledger.size > MAX_DELIVERED_ASKS) { - const oldest = ledger.keys().next().value - if (oldest === undefined) break - ledger.delete(oldest) - } + while (recoveryThreadMissingCounts.size > MAX_DELIVERED_ASKS) { + const oldest = recoveryThreadMissingCounts.keys().next().value + if (oldest === undefined) break + recoveryThreadMissingCounts.delete(oldest) } } @@ -967,7 +961,6 @@ export function useAgentSession(deps: AgentSessionDeps) { recoveringTurns.clear() recoveryPendingAskObservations.clear() recoveryThreadMissingCounts.clear() - automaticRecoveryReruns.clear() clearLateAskReports() const stoppedGeneration = ownedGeneration queueMicrotask(() => { @@ -1978,12 +1971,11 @@ export function useAgentSession(deps: AgentSessionDeps) { async function reconcileTurn( turn: LiveTurn, cause: RecoveryCause, - automaticRerun = false + isAutomaticRerun = false ): Promise { observeSkillTurn(turn) const key = recoveryKey(turn) if (deferToRunningRecovery(key, cause)) return - if (!automaticRerun) automaticRecoveryReruns.delete(key) const recovery = new AbortController() const state = { controller: recovery, @@ -2004,13 +1996,10 @@ export function useAgentSession(deps: AgentSessionDeps) { ownedGeneration, recovery.signal ) - if (needsRerun && (automaticRecoveryReruns.get(key) ?? 0) < 1) { - automaticRecoveryReruns.delete(key) - automaticRecoveryReruns.set(key, 1) - pruneRecoveryLedgers() + if (needsRerun && !isAutomaticRerun) { state.rerun = true state.automaticRerun = true - } else if (!needsRerun) automaticRecoveryReruns.delete(key) + } } catch (error) { // `onStatus` floats this job (`void reconcileTurn(turn)`), so a rethrow // would land as an `unhandledrejection` the session never sees. Abort is @@ -2080,7 +2069,6 @@ export function useAgentSession(deps: AgentSessionDeps) { if (settleFinishedTurn(turn, outcome)) { recoveryPendingAskObservations.delete(key) recoveryThreadMissingCounts.delete(key) - automaticRecoveryReruns.delete(key) return false } if (outcome.kind !== 'streaming') { @@ -2385,7 +2373,6 @@ export function useAgentSession(deps: AgentSessionDeps) { skillTurnUsage.delete(recoveryKey(turn)) recoveryPendingAskObservations.delete(recoveryKey(turn)) recoveryThreadMissingCounts.delete(recoveryKey(turn)) - automaticRecoveryReruns.delete(recoveryKey(turn)) clearLateAskReports(turn.threadId) if (conversationStore.threadId !== turn.threadId) { conversationStore.settleTurn(turn, undefined) From a4f42bcdfa0d731ff8efb2975925586566ab01e7 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 8 Oct 2026 05:01:36 +0000 Subject: [PATCH 20/36] test(agent): bound each recovery job retry Addresses Greenlight completeness feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#issuecomment-6002656120 --- .../composables/agent/useAgentSession.test.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index a5e2e47e6b2..270e2f67e82 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4354,6 +4354,36 @@ describe('useAgentSession (v1 composition root)', () => { expect(conversationStore.liveTurns()).toHaveLength(1) }) + it('(g47) each reconnect gets one bounded follow-up recovery pass', async () => { + vi.useFakeTimers() + try { + const getMessages = vi.fn(async (): Promise => { + throw new AgentApiError('temporary failure', 500, undefined) + }) + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ + rest: fakeRest({ getMessages }), + events: source + }) + session.start({ restore: false }) + status(true) + await session.sendMessage('go') + emit(delta('msg-1', 'partial')) + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(64_000) + expect(getMessages).toHaveBeenCalledTimes(12) + + status(false) + status(true) + await vi.advanceTimersByTimeAsync(64_000) + expect(getMessages).toHaveBeenCalledTimes(24) + } finally { + vi.useRealTimers() + } + }) + // PM-1738. A turn parked on a run approval stays `streaming` for as long as // the user takes to answer, so the drop that swallowed its `agent_ask` frame // leaves the server waiting on a card the panel never drew. Recovery already From 88ff3186b9dd1bc7bdfe903afa66eedbf45bc4cb Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 8 Oct 2026 05:42:29 +0000 Subject: [PATCH 21/36] refactor(agent): keep terminal recovery canonical Addresses Greenlight design and conformance feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#issuecomment-6002656120 --- .../agent/composables/agent/useAgentSession.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index be65c5fa930..1b094607605 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -235,11 +235,10 @@ function mergeAdjacentTextParts( } function terminalRecoveryParts( - rows: AgentMessages, - isAskUnavailable: (askId: string) => boolean + rows: AgentMessages ): AssistantMessage['parts'] | undefined { const parts = mergeAdjacentTextParts( - normalizeAgentTranscript(rows, isAskUnavailable).messages[0]?.parts ?? [] + normalizeAgentTranscript(rows).messages[0]?.parts ?? [] ) if (parts.length > 0) return parts if (rows.every((row) => row.status !== 'error')) return undefined @@ -2356,12 +2355,7 @@ export function useAgentSession(deps: AgentSessionDeps) { } return { kind: 'terminal', - parts: terminalRecoveryParts( - rows, - (askId) => - conversationStore.isAskRetired(askId, turn.threadId) || - conversationStore.submittedAskSelection(askId) !== undefined - ) + parts: terminalRecoveryParts(rows) } } catch (error) { if (signal.aborted) throw error From 78f8cb3be518273835063b541f50bd7847f5f1e5 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 8 Oct 2026 07:14:20 +0000 Subject: [PATCH 22/36] chore(agent): refresh approval evidence From 57921cdac755ad53a80b38ae6e1eb287893da958 Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 9 Oct 2026 03:30:38 +0000 Subject: [PATCH 23/36] chore(agent): refresh approval review evidence From 974c697ba720a1110778bb756f61f020d82b4acc Mon Sep 17 00:00:00 2001 From: bymyself Date: Sat, 10 Oct 2026 03:26:55 +0000 Subject: [PATCH 24/36] test(agent): avoid wall-clock deadline race --- .../agent/composables/agent/useAgentSession.test.ts | 4 ++-- 1 file changed, 2 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 270e2f67e82..c3ba6ad1a14 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4171,13 +4171,13 @@ describe('useAgentSession (v1 composition root)', () => { status(false) status(true) - await vi.advanceTimersByTimeAsync(59_999) + await vi.advanceTimersByTimeAsync(59_000) expect(getMessages).toHaveBeenCalledTimes(1) const signal = getMessages.mock.calls[0]?.[1]?.signal assert.exists(signal) expect(signal.aborted).toBe(false) - await vi.advanceTimersByTimeAsync(1) + await vi.advanceTimersByTimeAsync(1_000) expect(signal.aborted).toBe(true) expect(session.isStreaming.value).toBe(true) expect(reportError).not.toHaveBeenCalled() From 6a653ebaafcc23bd4303d0b0af3e61f149adcb12 Mon Sep 17 00:00:00 2001 From: bymyself Date: Sat, 10 Oct 2026 03:29:28 +0000 Subject: [PATCH 25/36] refactor(agent): isolate recovery event bookkeeping --- .../composables/agent/useAgentSession.ts | 98 ++++++++++++------- 1 file changed, 60 insertions(+), 38 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 1b094607605..3a8a1dec541 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -1905,26 +1905,32 @@ export function useAgentSession(deps: AgentSessionDeps) { if (messageId !== undefined) snapshotTurns.delete(toTurnId(messageId)) } + function handleResolvedAsk( + event: Extract + ): void { + reportSupersededAnswer(event.data.ask_id, event.data.selected) + // The owning transport may already be gone, so retire the ask explicitly + // before ingest can otherwise leave its card enabled. + conversationStore.retireAsk(event.data.ask_id, event.data.thread_id) + recordDeliveredAsk(event.data.ask_id) + withdrawLateAskReport(event.data.thread_id, event.data.ask_id) + onAskResolved?.(event.data.ask_id) + } + + function observeSkillEvent(event: AgentWsEvent): void { + for (const turn of conversationStore.liveTurns()) observeSkillTurn(turn) + if (event.type !== 'agent_message_done') return + finishSkillTurn({ + threadId: event.data.thread_id, + messageId: toTurnId(event.data.message_id) + }) + } + function handleAgentEvent(event: AgentWsEvent): void { if (heldForHydration(event)) return observeLiveDelivery(event) - if (event.type === 'agent_ask_resolved') { - reportSupersededAnswer(event.data.ask_id, event.data.selected) - // Not just un-busying it: `ingest` below routes this frame through the - // owning turn's transport, and the turn is gone in exactly the case that - // matters, so on its own it would re-enable a card it cannot remove. - conversationStore.retireAsk(event.data.ask_id, event.data.thread_id) - recordDeliveredAsk(event.data.ask_id) - withdrawLateAskReport(event.data.thread_id, event.data.ask_id) - onAskResolved?.(event.data.ask_id) - } - for (const turn of conversationStore.liveTurns()) observeSkillTurn(turn) - if (event.type === 'agent_message_done') { - finishSkillTurn({ - threadId: event.data.thread_id, - messageId: toTurnId(event.data.message_id) - }) - } + if (event.type === 'agent_ask_resolved') handleResolvedAsk(event) + observeSkillEvent(event) if (event.type === 'agent_ask') { deliverAsk(event) for (const turn of conversationStore.liveTurns()) observeSkillTurn(turn) @@ -2277,30 +2283,46 @@ export function useAgentSession(deps: AgentSessionDeps) { ) } + function markRecoveredSkillUse( + turn: LiveTurn, + parts: Extract['parts'] + ): void { + if ( + !parts?.some((part) => part.type === 'tool' && part.name === 'load_skill') + ) + return + observeSkillTurn(turn) + const usage = skillTurnUsage.get(recoveryKey(turn)) + if (usage) usage.used = true + } + + function clearLateAskReportsForTurn(turn: LiveTurn): void { + for (const [key, scheduled] of lateAskReports) { + if ( + scheduled.threadId !== turn.threadId || + scheduled.messageId !== turn.messageId + ) + continue + clearTimeout(scheduled.timer) + lateAskReports.delete(key) + } + } + + function settleTerminalTurn( + turn: LiveTurn, + parts: Extract['parts'] + ): void { + markRecoveredSkillUse(turn, parts) + finishSkillTurn(turn) + clearLateAskReportsForTurn(turn) + conversationStore.settleTurn(turn, parts) + markStoppedTurnReady(turn) + } + function settleFinishedTurn(turn: LiveTurn, outcome: TurnOutcome): boolean { switch (outcome.kind) { case 'terminal': - if ( - outcome.parts?.some( - (part) => part.type === 'tool' && part.name === 'load_skill' - ) - ) { - observeSkillTurn(turn) - const usage = skillTurnUsage.get(recoveryKey(turn)) - if (usage) usage.used = true - } - finishSkillTurn(turn) - for (const [key, scheduled] of lateAskReports) { - if ( - scheduled.threadId !== turn.threadId || - scheduled.messageId !== turn.messageId - ) - continue - clearTimeout(scheduled.timer) - lateAskReports.delete(key) - } - conversationStore.settleTurn(turn, outcome.parts) - markStoppedTurnReady(turn) + settleTerminalTurn(turn, outcome.parts) return true case 'thread-missing': forgetDeletedThread(turn) From 86641ab3956701ed6b917ae0ad3dcc7e059e4cc0 Mon Sep 17 00:00:00 2001 From: bymyself Date: Sat, 10 Oct 2026 04:12:19 +0000 Subject: [PATCH 26/36] chore(agent): refresh greenlight evidence From 5dd28439e71b68d368164448aa5a2ba8fef702fd Mon Sep 17 00:00:00 2001 From: bymyself Date: Tue, 13 Oct 2026 03:21:00 +0000 Subject: [PATCH 27/36] fix(agent): preserve retired asks across pending kinds --- .../extensions/agent/stores/agent/agentConversationStore.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index a4c57f0f797..659b7d26c00 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -1169,9 +1169,7 @@ export const useAgentConversationStore = defineStore( const retired = retiredAsksFor() const named = new Set( history.flatMap((row) => - row.pending_ask?.kind === 'run_approval' - ? [row.pending_ask.ask_id] - : [] + row.pending_ask ? [row.pending_ask.ask_id] : [] ) ) for (const askId of retired) if (!named.has(askId)) retired.delete(askId) From fade5735ad395b025fc97bd5958196b177612138 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 14 Oct 2026 04:06:31 +0000 Subject: [PATCH 28/36] fix(agent): close recovery authority gaps --- .../composables/agent/useAgentSession.test.ts | 39 ++- .../composables/agent/useAgentSession.ts | 238 +++++++++++------- .../agent/services/agent/agentTranscript.ts | 30 ++- 3 files changed, 206 insertions(+), 101 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index c3ba6ad1a14..7369e2c4f74 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -2037,12 +2037,13 @@ describe('useAgentSession (v1 composition root)', () => { const answerAsk = vi .fn() .mockRejectedValue(new AgentApiError('already answered', 409, undefined)) - const { source, emit } = fakeEvents() + const { source, emit, status } = fakeEvents() const session = useAgentSession({ rest: fakeRest({ answerAsk }), events: source }) session.start() + status(true) await session.sendMessage('build it') emit(runApproval('msg-1')) @@ -2343,6 +2344,34 @@ describe('useAgentSession (v1 composition root)', () => { expect(cardOnScreen()).toBe(false) }) + it('warns from the canonical answer receipt when another selection won', async () => { + const answerAsk = vi + .fn() + .mockResolvedValue({ status: 'answered', selected: ['run'] }) + const events = fakeEvents() + const session = useAgentSession({ + rest: fakeRest({ answerAsk }), + events: events.source + }) + session.start() + events.status(true) + await session.sendMessage('build it and run it') + events.emit(runApproval('msg-1')) + + const answered = await session.answerAsk('turn-1:call-1', 'cancel') + + expect(answered).toBe(false) + expect(reportError).toHaveBeenCalledWith(expect.any(Error), { + surface: 'agent', + errorType: 'agent_ask_answer_superseded' + }) + expect(reportError).not.toHaveBeenCalledWith(expect.any(Error), { + surface: 'agent', + errorType: 'agent_ask_answer_unconfirmed' + }) + expect(session.notices.value).toHaveLength(1) + }) + // The resolution frame retires the card on its way through, so by the time // a slow request rejects there is nothing left to warn about — and telling // the user their answer might not have arrived, after the server has said @@ -4158,7 +4187,7 @@ describe('useAgentSession (v1 composition root)', () => { ) }) - it('(g18) a history fetch that never answers is abandoned at the recovery deadline', async () => { + it('(g18) a history fetch that never answers gets one deadline follow-up', async () => { const getMessages = vi.fn(hangingGetMessages) const rest = fakeRest({ getMessages }) const { source, emit, status } = fakeEvents() @@ -4181,11 +4210,7 @@ describe('useAgentSession (v1 composition root)', () => { expect(signal.aborted).toBe(true) expect(session.isStreaming.value).toBe(true) expect(reportError).not.toHaveBeenCalled() - - status(false) - status(true) - await vi.advanceTimersByTimeAsync(0) - expect(getMessages).toHaveBeenCalledTimes(2) + await vi.waitFor(() => expect(getMessages).toHaveBeenCalledTimes(2)) }) it('(g19) stopping the session cancels its in-flight history fetch', async () => { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 3a8a1dec541..72b8626ddc1 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -21,6 +21,7 @@ import { } from '@/platform/workflow/persistence/base/storageKeys' import { createUuidv4 } from '@/utils/uuid' import type { + AgentAnswerReceipt, AgentActiveTabData, AgentMessages, AgentTurnAccepted, @@ -177,6 +178,7 @@ const MAX_HYDRATION_MAILBOXES = 32 const TURN_RECOVERY_DELAYS_MS = [0, 1000, 2000, 4000, 8000, 16000] /** Upper bound on one recovery job, including any history fetch still in flight. */ const TURN_RECOVERY_DEADLINE_MS = 60_000 +const THREAD_MISSING_CONFIRMATION_INTERVAL_MS = 1000 /** How long a restored ask waits for its own broadcast before it counts as lost. */ const LATE_ASK_FRAME_GRACE_MS = 2000 @@ -528,6 +530,10 @@ export function useAgentSession(deps: AgentSessionDeps) { rerunCause: RecoveryCause automaticRerun: boolean } + interface MissingThreadObservation { + count: number + observedAt: number + } const recoveringTurns = new Map() interface PendingAskObservation { pendingAskId: string | undefined @@ -537,7 +543,10 @@ export function useAgentSession(deps: AgentSessionDeps) { string, PendingAskObservation >() - const recoveryThreadMissingCounts = new Map() + const recoveryThreadMissingCounts = new Map< + string, + MissingThreadObservation + >() /** * Asks recovery must not re-deliver: one it already restored, one a frame * has delivered, and any the user has answered or the server has resolved. @@ -547,6 +556,7 @@ export function useAgentSession(deps: AgentSessionDeps) { * an `ask_id` carries its `message_id`, so it is never reused. */ const deliveredAsks = new Set() + const supersededAnswerReports = new Set() const lateAskReports = new Map< string, { @@ -564,6 +574,16 @@ export function useAgentSession(deps: AgentSessionDeps) { if (oldest !== undefined) deliveredAsks.delete(oldest) } + function recordSupersededAnswerReport(askId: string): boolean { + if (supersededAnswerReports.has(askId)) return false + supersededAnswerReports.add(askId) + if (supersededAnswerReports.size > MAX_DELIVERED_ASKS) { + const oldest = supersededAnswerReports.values().next().value + if (oldest !== undefined) supersededAnswerReports.delete(oldest) + } + return true + } + function recordPendingAskObservation( key: string, pendingAskId: string | undefined, @@ -583,15 +603,23 @@ export function useAgentSession(deps: AgentSessionDeps) { } function recordThreadAvailability(key: string, outcome: TurnOutcome): number { - const previous = recoveryThreadMissingCounts.get(key) ?? 0 + const previous = recoveryThreadMissingCounts.get(key) if (outcome.kind === 'thread-missing') { - const consecutive = previous + 1 + const observedAt = Date.now() + const consecutive = + previous === undefined || + observedAt - previous.observedAt > TURN_RECOVERY_DEADLINE_MS + ? 1 + : observedAt - previous.observedAt >= + THREAD_MISSING_CONFIRMATION_INTERVAL_MS + ? previous.count + 1 + : previous.count recoveryThreadMissingCounts.delete(key) - recoveryThreadMissingCounts.set(key, consecutive) + recoveryThreadMissingCounts.set(key, { count: consecutive, observedAt }) return consecutive } recoveryThreadMissingCounts.delete(key) - return previous + return previous?.count ?? 0 } function pruneRecoveryLedgers(): void { @@ -1551,16 +1579,7 @@ export function useAgentSession(deps: AgentSessionDeps) { ): Promise { const currentThreadId = conversationStore.threadId if (currentThreadId === null) { - // PM-1658: the card is on screen, so a click on it is never a no-op. - reportError( - new Error('run approval answered with no thread to send on'), - { surface: 'agent', errorType: 'agent_ask_answer_unroutable' } - ) - pushError(i18n.global.t('agent.runApproval.answerFailed')) - // Nothing can ever answer this card, so retire it rather than let every - // further click raise another toast and another telemetry event. - recordDeliveredAsk(askId) - conversationStore.retireAsk(askId) + failUnroutableAsk(askId) return false } // PM-1658: deliberately NOT gated on an active turn. The endpoint is keyed @@ -1571,75 +1590,102 @@ export function useAgentSession(deps: AgentSessionDeps) { conversationStore.recordAskSelection(askId, selection) conversationStore.setAskAnswering(askId, true) const answerChannelEpoch = resolutionChannelEpoch - const answerStartedWithoutResolutionChannel = connection === 'dropped' + const answerStartedWithoutResolutionChannel = connection !== 'live' const resolutionChannelWasLost = () => answerStartedWithoutResolutionChannel || resolutionChannelEpoch !== answerChannelEpoch try { - await sendAnswer(currentThreadId, askId, selection) - if (answeringAskIds.value.has(askId)) - reportAcceptedRecoveredAnswer(resolutionChannelWasLost()) - recordDeliveredAsk(askId) - conversationStore.commitAsk(askId, currentThreadId) - return true + const receipt = await sendAnswer(currentThreadId, askId, selection) + return finishAcceptedAskAnswer( + askId, + currentThreadId, + selection, + receipt, + resolutionChannelWasLost() + ) } 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 a quiet double-click unless this client restored the ask - // after losing its resolution channel. The rest mean this client - // could never have answered, which the user has to be told. - 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')) - } else - reportRejectedRecoveredAnswerIfPending( - askId, - resolutionChannelWasLost() - ) - // The card is retired for good here, by resolution (409) or because - // this client could never answer it. Either way a later recovery poll - // must not draw it again. - recordDeliveredAsk(askId) - 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. + return handleAnswerFailure( + error, + askId, + currentThreadId, + resolutionChannelWasLost() + ) + } + } + + function finishAcceptedAskAnswer( + askId: string, + threadId: string, + selection: 'run' | 'cancel', + receipt: AgentAnswerReceipt, + resolutionChannelWasLost: boolean + ): boolean { + const committedSelection = receipt.selected ?? null + reportSupersededAnswer(askId, committedSelection) + if ( + (committedSelection === null || committedSelection.length === 0) && + answeringAskIds.value.has(askId) + ) + reportAcceptedRecoveredAnswer(resolutionChannelWasLost) + recordDeliveredAsk(askId) + conversationStore.commitAsk(askId, threadId) + return ( + committedSelection === null || + committedSelection.length === 0 || + committedSelection.includes(selection) + ) + } + + function failUnroutableAsk(askId: string): void { + reportError(new Error('run approval answered with no thread to send on'), { + surface: 'agent', + errorType: 'agent_ask_answer_unroutable' + }) + pushError(i18n.global.t('agent.runApproval.answerFailed')) + recordDeliveredAsk(askId) + conversationStore.retireAsk(askId) + } + + function handleAnswerFailure( + error: unknown, + askId: string, + threadId: string, + resolutionChannelWasLost: boolean + ): false { + if (!answeringAskIds.value.has(askId)) return false + if ( + error instanceof AgentApiError && + TERMINAL_ANSWER_STATUSES.has(error.status) + ) { + 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')) + } else + reportRejectedRecoveredAnswerIfPending(askId, resolutionChannelWasLost) recordDeliveredAsk(askId) - conversationStore.retireAsk(askId, currentThreadId) - pushError(i18n.global.t('agent.runApproval.answerUncertain')) + conversationStore.retireAsk(askId, threadId) return false } + reportError(error, { + surface: 'agent', + errorType: 'agent_ask_answer_failed' + }) + trackAgentError('ask_answer_failed', 'post_acceptance', 'error_overlay', { + retryable: isRetryableRequestFailure(error, false) + }) + recordDeliveredAsk(askId) + conversationStore.retireAsk(askId, threadId) + pushError(i18n.global.t('agent.runApproval.answerUncertain')) + return false } function reportAcceptedRecoveredAnswer( @@ -1690,7 +1736,8 @@ export function useAgentSession(deps: AgentSessionDeps) { submitted === undefined || settled === null || settled.length === 0 || - settled.includes(submitted) + settled.includes(submitted) || + !recordSupersededAnswerReport(askId) ) return reportError( @@ -1706,11 +1753,10 @@ export function useAgentSession(deps: AgentSessionDeps) { threadId: string, askId: string, selection: 'run' | 'cancel' - ): Promise { + ): Promise { for (let attempt = 0; ; attempt++) { try { - await rest.answerAsk(threadId, askId, [selection]) - return + return await rest.answerAsk(threadId, askId, [selection]) } catch (error) { if ( attempt >= ANSWER_RETRY_BACKOFF_MS.length || @@ -1990,10 +2036,15 @@ export function useAgentSession(deps: AgentSessionDeps) { automaticRerun: false } recoveringTurns.set(key, state) - const deadline = setTimeout( - () => recovery.abort(), - TURN_RECOVERY_DEADLINE_MS - ) + const deadline = setTimeout(() => { + if (recoveringTurns.get(key)?.controller !== recovery) return + recoveringTurns.delete(key) + recovery.abort() + if (!isTurnLive(turn, ownedGeneration)) return + if (state.rerun) + void reconcileTurn(turn, state.rerunCause, state.automaticRerun) + else if (!isAutomaticRerun) void reconcileTurn(turn, cause, true) + }, TURN_RECOVERY_DEADLINE_MS) try { const needsRerun = await recoverTurn( turn, @@ -2001,7 +2052,7 @@ export function useAgentSession(deps: AgentSessionDeps) { ownedGeneration, recovery.signal ) - if (needsRerun && !isAutomaticRerun) { + if (needsRerun && !isAutomaticRerun && !state.rerun) { state.rerun = true state.automaticRerun = true } @@ -2159,6 +2210,12 @@ export function useAgentSession(deps: AgentSessionDeps) { ): void { if (pendingAsk?.kind !== 'run_approval') return if (deliveredAsks.has(pendingAsk.ask_id)) return + if ( + conversationStore.submittedAskSelection(pendingAsk.ask_id) !== + undefined || + answeringAskIds.value.has(pendingAsk.ask_id) + ) + return if (conversationStore.isAskRetired(pendingAsk.ask_id, turn.threadId)) return if (conversationStore.isApprovalShown(turn, pendingAsk.ask_id)) return conversationStore.ingest({ @@ -2372,7 +2429,16 @@ export function useAgentSession(deps: AgentSessionDeps) { kind: 'streaming', pendingAsk: rows.findLast( (row) => - !isTerminalTurnStatus(row.status) && row.pending_ask !== undefined + !isTerminalTurnStatus(row.status) && + row.pending_ask !== undefined && + !conversationStore.isAskRetired( + row.pending_ask.ask_id, + turn.threadId + ) && + conversationStore.submittedAskSelection( + row.pending_ask.ask_id + ) === undefined && + !answeringAskIds.value.has(row.pending_ask.ask_id) )?.pending_ask } return { diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.ts index dbb088fffe7..94eb8ba1ada 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.ts @@ -426,6 +426,25 @@ function recordAssistantRow( return rowPending } +function keepAuthoritativeApproval( + message: AssistantMessage, + row: AgentMessages[number], + isAskUnavailable: (askId: string) => boolean +): void { + const rowAsk = + row.status === 'streaming' ? pendingRunApproval(row) : undefined + const availableAskId = + rowAsk !== undefined && !isAskUnavailable(rowAsk.askId) + ? rowAsk.askId + : undefined + message.parts = message.parts.filter( + (part) => + part.type !== 'runApproval' || + (!isAskUnavailable(part.askId) && + (availableAskId === undefined || part.askId === availableAskId)) + ) +} + /** * Strips the liveness a row's `streaming` status granted a message that turns * out not to be getting a transport. The parts matter as much as the flag, @@ -494,14 +513,9 @@ export function normalizeAgentTranscript( } if (row.role === 'assistant') { const pending = recordAssistantRow(row, turnId, text, assistants) - const priorApproval = pendingByTurn - .get(turnId) - ?.message.parts.some( - (part) => part.type === 'runApproval' && !isAskUnavailable(part.askId) - ) - if (row.status === 'streaming' && pending === undefined && priorApproval) - continue - else pendingByTurn.set(turnId, pending) + const message = assistants.get(turnId) + if (message) keepAuthoritativeApproval(message, row, isAskUnavailable) + pendingByTurn.set(turnId, pending) } } From 30365f23ca93ecfc373d0fc2380f6a0b858896bf Mon Sep 17 00:00:00 2001 From: bymyself Date: Sat, 17 Oct 2026 04:00:50 +0000 Subject: [PATCH 29/36] fix(agent): close cross-ask recovery gaps Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301169832 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301169842 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301169854 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301169862 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301169865 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301169870 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301169874 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301169880 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301169885 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301169890 --- src/locales/en/main.json | 1 + .../composables/agent/useAgentSession.test.ts | 32 +++- .../composables/agent/useAgentSession.ts | 150 +++++++++++++----- .../agent/agentConversationStore.test.ts | 9 ++ .../stores/agent/agentConversationStore.ts | 22 +-- 5 files changed, 156 insertions(+), 58 deletions(-) diff --git a/src/locales/en/main.json b/src/locales/en/main.json index ce5f0330d40..98adcdd3ad8 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -5672,6 +5672,7 @@ "run": "Run", "answerFailed": "Could not send your answer. Reload the page and try again.", "answerUncertain": "Could not confirm your answer reached the agent. It may still have been applied — reload the page to see where things stand.", + "answerAcceptedUnconfirmed": "Your answer reached the agent, but we could not confirm whether it was the answer applied. Reload the page to see where things stand.", "answerSuperseded": "This was already answered somewhere else, so that answer was used instead of yours." }, "askUnavailable": "The agent is waiting on a step this panel can't show.", diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index a5a0d273d68..6363e53637f 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -2298,6 +2298,24 @@ describe('useAgentSession (v1 composition root)', () => { ]) }) + it('accepts an explicitly empty answer committed by the server', async () => { + const { session } = await parkedOnQuestion({ + status: 'answered', + selected: [] + }) + + const committed = await session.answerAsk('turn-1:call-1', { + selected: [] + }) + await vi.advanceTimersByTimeAsync(15_000) + + expect(committed).toBe(true) + expect(card()).toMatchObject({ + resolution: { status: 'answered', selected: [] } + }) + expect(session.notices.value).toEqual([]) + }) + it('leaves a status-only 202 to the frame to report', async () => { const { session, emit } = await parkedOnQuestion() await session.answerAsk('turn-1:call-1', { selected: ['sdxl'] }) @@ -3958,7 +3976,7 @@ describe('useAgentSession (v1 composition root)', () => { expect(reportError).toHaveBeenCalledWith(expect.any(Error), { surface: 'agent', - errorType: 'agent_ask_answer_unconfirmed' + errorType: 'agent_ask_answer_winner_unconfirmed' }) }) @@ -4461,7 +4479,7 @@ describe('useAgentSession (v1 composition root)', () => { ) }) - it('(g10) a failing recovery fetch gets one bounded follow-up and leaves the turn live', async () => { + it('(g10) a failing recovery fetch does not amplify backend failure and leaves the turn live', async () => { const rest = fakeRest({ getMessages: vi.fn(async (): Promise => { throw new TypeError('Failed to fetch') @@ -4479,7 +4497,7 @@ describe('useAgentSession (v1 composition root)', () => { status(true) await vi.advanceTimersByTimeAsync(60_000) - expect(rest.getMessages).toHaveBeenCalledTimes(11) + expect(rest.getMessages).toHaveBeenCalledTimes(6) expect(session.notices.value).toEqual([]) expect(session.isStreaming.value).toBe(true) @@ -4801,7 +4819,7 @@ describe('useAgentSession (v1 composition root)', () => { expect(conversationStore.liveTurns()).toHaveLength(1) }) - it('(g47) each reconnect gets one bounded follow-up recovery pass', async () => { + it('(g47) each reconnect gets one bounded recovery pass', async () => { vi.useFakeTimers() try { const getMessages = vi.fn(async (): Promise => { @@ -4820,12 +4838,12 @@ describe('useAgentSession (v1 composition root)', () => { status(false) status(true) await vi.advanceTimersByTimeAsync(64_000) - expect(getMessages).toHaveBeenCalledTimes(12) + expect(getMessages).toHaveBeenCalledTimes(6) status(false) status(true) await vi.advanceTimersByTimeAsync(64_000) - expect(getMessages).toHaveBeenCalledTimes(24) + expect(getMessages).toHaveBeenCalledTimes(12) } finally { vi.useRealTimers() } @@ -5139,7 +5157,7 @@ describe('useAgentSession (v1 composition root)', () => { expect(reportError).toHaveBeenCalledWith(expect.any(Error), { surface: 'agent', - errorType: 'agent_ask_answer_unconfirmed' + errorType: 'agent_ask_answer_winner_unconfirmed' }) expect(session.notices.value).toHaveLength(1) }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index fa14d9db35d..d06037d86e6 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -587,6 +587,7 @@ export function useAgentSession(deps: AgentSessionDeps) { interface PendingAskObservation { pendingAskId: string | undefined deliveredAskIds: Set + observedAt: number } const recoveryPendingAskObservations = new Map< string, @@ -605,7 +606,10 @@ export function useAgentSession(deps: AgentSessionDeps) { * an `ask_id` carries its `message_id`, so it is never reused. */ const deliveredAsks = new Set() + const observedDeliveredAsks = new Set() const supersededAnswerReports = new Set() + const acceptedUnconfirmedReports = new Set() + const acceptedAsksAwaitingResolution = new Set() const lateAskReports = new Map< string, { @@ -616,11 +620,15 @@ export function useAgentSession(deps: AgentSessionDeps) { >() function recordDeliveredAsk(askId: string): void { + observedDeliveredAsks.delete(askId) + observedDeliveredAsks.add(askId) deliveredAsks.delete(askId) deliveredAsks.add(askId) - if (deliveredAsks.size <= MAX_DELIVERED_ASKS) return - const oldest = deliveredAsks.values().next().value - if (oldest !== undefined) deliveredAsks.delete(oldest) + for (const ledger of [deliveredAsks, observedDeliveredAsks]) { + if (ledger.size <= MAX_DELIVERED_ASKS) continue + const oldest = ledger.values().next().value + if (oldest !== undefined) ledger.delete(oldest) + } } function recordSupersededAnswerReport(askId: string): boolean { @@ -640,7 +648,8 @@ export function useAgentSession(deps: AgentSessionDeps) { ): PendingAskObservation { const observation = { pendingAskId, - deliveredAskIds + deliveredAskIds, + observedAt: Date.now() } recoveryPendingAskObservations.delete(key) recoveryPendingAskObservations.set(key, observation) @@ -1633,7 +1642,7 @@ export function useAgentSession(deps: AgentSessionDeps) { if (answeringAskIds.value.has(askId)) return false const runApprovalAnswer = typeof answer === 'string' const normalizedAnswer = runApprovalAnswer ? { selected: [answer] } : answer - conversationStore.recordAskAnswer(askId, normalizedAnswer) + conversationStore.recordAskAnswer(askId, normalizedAnswer, currentThreadId) conversationStore.setAskAnswering(askId, true) const answerChannelEpoch = resolutionChannelEpoch const answerStartedWithoutResolutionChannel = connection === 'dropped' @@ -1651,7 +1660,7 @@ export function useAgentSession(deps: AgentSessionDeps) { }) } catch (error) { if (answeringAskIds.value.has(askId)) - handleAnswerFailure( + return handleAnswerFailure( error, askId, currentThreadId, @@ -1674,7 +1683,7 @@ export function useAgentSession(deps: AgentSessionDeps) { receipt: AgentAnswerReceipt resolutionChannelWasLost: () => boolean }): boolean { - if (!receiptNamesCommittedAnswer(receipt)) + if (!receiptNamesCommittedAnswer(receipt, answer)) return commitStatusOnlyAskReceipt({ askId, answer, @@ -1682,6 +1691,7 @@ export function useAgentSession(deps: AgentSessionDeps) { resolutionChannelWasLost }) const committed = toAskResolution(receipt) + acceptedAsksAwaitingResolution.delete(askId) const superseded = committed.status === 'answered' && isSupersededAnswer(answer, committed) reportSupersededAnswer(askId, committed) @@ -1690,11 +1700,23 @@ export function useAgentSession(deps: AgentSessionDeps) { return !superseded } - function receiptNamesCommittedAnswer(receipt: { - selected?: string[] | null - other_text?: string - }): boolean { - return (receipt.selected?.length ?? 0) > 0 || Boolean(receipt.other_text) + function receiptNamesCommittedAnswer( + receipt: { + status?: string + selected?: string[] | null + other_text?: string + }, + submitted?: AgentAnswerRequest + ): boolean { + if (receipt.status !== undefined && receipt.status !== 'answered') + return false + if (receipt.other_text !== undefined) return true + if (receipt.selected === undefined || receipt.selected === null) + return false + const runApproval = + submitted?.selected.length === 1 && + (submitted.selected[0] === 'run' || submitted.selected[0] === 'cancel') + return !runApproval || receipt.selected.length > 0 } function commitStatusOnlyAskReceipt({ @@ -1707,16 +1729,18 @@ export function useAgentSession(deps: AgentSessionDeps) { answer: AgentAnswerRequest currentThreadId: string resolutionChannelWasLost: () => boolean - }): true { + }): boolean { const channelWasLost = resolutionChannelWasLost() const resolutionStillPending = conversationStore.submittedAskAnswer(askId) !== undefined - if (resolutionStillPending) reportAcceptedRecoveredAnswer(channelWasLost) + if (resolutionStillPending) acceptedAsksAwaitingResolution.add(askId) + if (resolutionStillPending) + reportAcceptedRecoveredAnswer(askId, channelWasLost) recordDeliveredAsk(askId) conversationStore.commitAsk(askId, currentThreadId, { status: 'retired' }) if (!channelWasLost && resolutionStillPending) - conversationStore.recordAskAnswer(askId, answer) - return true + conversationStore.recordAskAnswer(askId, answer, currentThreadId) + return !supersededAnswerReports.has(askId) } function failUnroutableAsk(askId: string): void { @@ -1730,14 +1754,17 @@ export function useAgentSession(deps: AgentSessionDeps) { } function reportAcceptedRecoveredAnswer( + askId: string, resolutionChannelWasLost: boolean ): void { - if (!resolutionChannelWasLost) return + if (!resolutionChannelWasLost || acceptedUnconfirmedReports.has(askId)) + return + acceptedUnconfirmedReports.add(askId) reportError(new Error('accepted restored approval cannot be verified'), { surface: 'agent', - errorType: 'agent_ask_answer_unconfirmed' + errorType: 'agent_ask_answer_winner_unconfirmed' }) - pushError(i18n.global.t('agent.runApproval.answerUncertain')) + pushError(i18n.global.t('agent.runApproval.answerAcceptedUnconfirmed')) } function handleAnswerFailure( @@ -1745,15 +1772,15 @@ export function useAgentSession(deps: AgentSessionDeps) { askId: string, threadId: string, resolutionChannelWasLost = connection === 'dropped' - ): void { + ): boolean { + acceptedAsksAwaitingResolution.delete(askId) if (error instanceof AgentApiError && error.status === 409) { - handleConflictAnswerFailure( + return handleConflictAnswerFailure( error, askId, threadId, resolutionChannelWasLost ) - return } const failure = classifyAnswerFailure(error) reportError(error, { @@ -1781,6 +1808,7 @@ export function useAgentSession(deps: AgentSessionDeps) { } } settle[failure]() + return false } function handleConflictAnswerFailure( @@ -1788,7 +1816,7 @@ export function useAgentSession(deps: AgentSessionDeps) { askId: string, threadId: string, resolutionChannelWasLost: boolean - ): void { + ): boolean { const conflict = zAgentAskConflict.safeParse(error.body) if ( conflict.success && @@ -1800,23 +1828,48 @@ export function useAgentSession(deps: AgentSessionDeps) { toAskResolution(conflict.data), threadId ) - return + return false } - if (conflict.success && receiptNamesCommittedAnswer(conflict.data)) { + const submitted = conversationStore.submittedAskAnswer(askId) + if ( + conflict.success && + receiptNamesCommittedAnswer(conflict.data, submitted) + ) { const committed = toAskResolution(conflict.data) + const superseded = + submitted !== undefined && + committed.status === 'answered' && + isSupersededAnswer(submitted, committed) reportSupersededAnswer(askId, committed) conversationStore.retireAsk(askId, committed, threadId) - return + return !superseded } + retireUnknownConflict( + error, + askId, + threadId, + submitted, + resolutionChannelWasLost + ) + return false + } + + function retireUnknownConflict( + error: AgentApiError, + askId: string, + threadId: string, + submitted: AgentAnswerRequest | undefined, + resolutionChannelWasLost: boolean + ): void { recordDeliveredAsk(askId) - const submitted = conversationStore.submittedAskAnswer(askId) conversationStore.retireAsk( askId, { status: resolutionChannelWasLost ? 'unconfirmed' : 'retired' }, threadId ) if (!resolutionChannelWasLost) { - if (submitted) conversationStore.recordAskAnswer(askId, submitted) + if (submitted) + conversationStore.recordAskAnswer(askId, submitted, threadId) return } reportError(error, { @@ -2051,6 +2104,7 @@ export function useAgentSession(deps: AgentSessionDeps) { function handleResolvedAsk( event: Extract ): void { + acceptedAsksAwaitingResolution.delete(event.data.ask_id) const resolution = toAskResolution(event.data) reportSupersededAnswer(event.data.ask_id, resolution) // The owning transport may already be gone, so retire the ask explicitly @@ -2105,13 +2159,18 @@ export function useAgentSession(deps: AgentSessionDeps) { if (!live) { if (connection === 'initial' && !receivedStatusSnapshot) { receivedStatusSnapshot = true - if (answeringAskIds.value.size > 0) resolutionChannelEpoch += 1 + connection = 'dropped' + resolutionChannelEpoch += 1 + for (const askId of acceptedAsksAwaitingResolution) + reportAcceptedRecoveredAnswer(askId, true) return } receivedStatusSnapshot = true if (connection === 'dropped') return connection = 'dropped' resolutionChannelEpoch += 1 + for (const askId of acceptedAsksAwaitingResolution) + reportAcceptedRecoveredAnswer(askId, true) return } receivedStatusSnapshot = true @@ -2215,8 +2274,9 @@ export function useAgentSession(deps: AgentSessionDeps) { signal: AbortSignal ): Promise { const key = recoveryKey(turn) - let previousObservation = recoveryPendingAskObservations.get(key) + let previousObservation = recentPendingAskObservation(key) let hasUnconfirmedObservation = false + let sawStreamingObservation = false for (const ms of TURN_RECOVERY_DELAYS_MS) { await delay(ms, { signal }) if (!isTurnLive(turn, generation)) return false @@ -2239,6 +2299,7 @@ export function useAgentSession(deps: AgentSessionDeps) { hasUnconfirmedObservation = true continue } + sawStreamingObservation = true const pendingAskId = streamingPendingAskId(outcome) const pendingAskConfirmed = previousObservation !== undefined && @@ -2262,7 +2323,20 @@ export function useAgentSession(deps: AgentSessionDeps) { deliveredBeforeFetch ) } - return hasUnconfirmedObservation + return hasUnconfirmedObservation && sawStreamingObservation + } + + function recentPendingAskObservation( + key: string + ): PendingAskObservation | undefined { + const observation = recoveryPendingAskObservations.get(key) + if ( + observation === undefined || + Date.now() - observation.observedAt <= TURN_RECOVERY_DEADLINE_MS + ) + return observation + recoveryPendingAskObservations.delete(key) + return undefined } function reconcileStreamingApproval( @@ -2330,6 +2404,7 @@ export function useAgentSession(deps: AgentSessionDeps) { return if (conversationStore.isAskRetired(pendingAsk.ask_id, turn.threadId)) return if (conversationStore.isAskShown(turn, pendingAsk.ask_id)) return + const wasDeliveredBefore = observedDeliveredAsks.has(pendingAsk.ask_id) conversationStore.ingest({ type: 'agent_ask', data: { @@ -2340,7 +2415,8 @@ export function useAgentSession(deps: AgentSessionDeps) { }) if (!conversationStore.isAskShown(turn, pendingAsk.ask_id)) return recordDeliveredAsk(pendingAsk.ask_id) - reportRestoredApproval(turn, pendingAsk.ask_id, cause) + if (!wasDeliveredBefore) + reportRestoredApproval(turn, pendingAsk.ask_id, cause) } /** @@ -2541,15 +2617,7 @@ export function useAgentSession(deps: AgentSessionDeps) { kind: 'streaming', pendingAsk: rows.findLast( (row) => - !isTerminalTurnStatus(row.status) && - row.pending_ask !== undefined && - !conversationStore.isAskPermanentlyRetired( - row.pending_ask.ask_id, - turn.threadId - ) && - conversationStore.submittedAskAnswer(row.pending_ask.ask_id) === - undefined && - !answeringAskIds.value.has(row.pending_ask.ask_id) + !isTerminalTurnStatus(row.status) && row.pending_ask !== undefined )?.pending_ask } return { diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index e36ddc7215e..9b2447b7a3b 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -1019,6 +1019,15 @@ describe('useAgentConversationStore', () => { ])[0] ] store.hydrate(deleteTranscript()) + store.retireAsk('turn-1:call-1', { status: 'retired' }, 'th', false) + store.confirmAskPending('turn-1:call-1', 'th') + expect( + store.messages.flatMap((message) => + message.parts.filter((part) => part.type === 'deleteApproval') + ) + ).toEqual([ + expect.not.objectContaining({ resolution: expect.anything() }) + ]) store.retireAsk( 'turn-1:call-1', { status: 'answered', selected: ['keep'] }, diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 2e6f0d099b0..bfcaa018942 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -456,22 +456,20 @@ export const useAgentConversationStore = defineStore( return retired } function isAskRetired(askId: string, owner?: string): boolean { + const key = threadKey(owner) return ( - retiredAsksFor(owner).has(askId) || - provisionalRetiredAsksFor(owner).has(askId) + retiredAsks.get(key)?.has(askId) === true || + provisionalRetiredAsks.get(key)?.has(askId) === true ) } - function isAskPermanentlyRetired(askId: string, owner?: string): boolean { - return retiredAsksFor(owner).has(askId) - } - function confirmAskPending(askId: string, owner?: string): void { provisionalRetiredAsksFor(owner).delete(askId) const key = threadKey(owner) const reopen = (message: AssistantMessage): void => { message.parts = message.parts.map((part) => - part.type === 'askUser' && part.askId === askId + (part.type === 'askUser' || part.type === 'deleteApproval') && + askIdOf(part) === askId ? { ...part, resolution: undefined } : part ) @@ -487,8 +485,12 @@ export const useAgentConversationStore = defineStore( { answer: AgentAnswerRequest; owner: string } >() - function recordAskAnswer(askId: string, answer: AgentAnswerRequest): void { - submittedAskAnswers.set(askId, { answer, owner: threadKey() }) + function recordAskAnswer( + askId: string, + answer: AgentAnswerRequest, + owner?: string + ): void { + submittedAskAnswers.set(askId, { answer, owner: threadKey(owner) }) } function submittedAskAnswer(askId: string): AgentAnswerRequest | undefined { @@ -1033,6 +1035,7 @@ export const useAgentConversationStore = defineStore( turn: LiveTurn, persistedParts: AssistantMessage['parts'] | undefined ): void { + provisionalRetiredAsks.delete(turn.threadId) const slot = activeSlot.value const isActive = slot !== null && @@ -1418,7 +1421,6 @@ export const useAgentConversationStore = defineStore( commitAsk, retireAsk, isAskRetired, - isAskPermanentlyRetired, confirmAskPending, reconcileApprovalParts, startTurn, From fb198172540a8d10cf4cd0314bb18820366697df Mon Sep 17 00:00:00 2001 From: bymyself Date: Sat, 17 Oct 2026 04:39:38 +0000 Subject: [PATCH 30/36] fix(agent): harden restored approval resolution Addresses review findings: - https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301368519 - https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301368529 - https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301368539 - https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301368544 - https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301368551 - https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301368561 - https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301368565 - https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301368568 --- .../composables/agent/useAgentSession.test.ts | 4 +- .../composables/agent/useAgentSession.ts | 71 ++++++++++--------- .../services/agent/agentEventSource.test.ts | 34 +++++++-- .../agent/services/agent/agentEventSource.ts | 11 ++- .../services/agent/agentTranscript.test.ts | 1 + .../agent/services/agent/agentTranscript.ts | 9 ++- .../stores/agent/agentConversationStore.ts | 44 +++++++++++- 7 files changed, 122 insertions(+), 52 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 6363e53637f..fc0a874f008 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -4627,7 +4627,7 @@ describe('useAgentSession (v1 composition root)', () => { ) }) - it('(g18) a history fetch that never answers gets one deadline follow-up', async () => { + it('(g18) a history fetch that never answers stops at its deadline', async () => { const getMessages = vi.fn(hangingGetMessages) const rest = fakeRest({ getMessages }) const { source, emit, status } = fakeEvents() @@ -4650,7 +4650,7 @@ describe('useAgentSession (v1 composition root)', () => { expect(signal.aborted).toBe(true) expect(session.isStreaming.value).toBe(true) expect(reportError).not.toHaveBeenCalled() - await vi.waitFor(() => expect(getMessages).toHaveBeenCalledTimes(2)) + await vi.waitFor(() => expect(getMessages).toHaveBeenCalledOnce()) }) it('(g19) stopping the session cancels its in-flight history fetch', async () => { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index d06037d86e6..78cd92c38ee 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -609,7 +609,7 @@ export function useAgentSession(deps: AgentSessionDeps) { const observedDeliveredAsks = new Set() const supersededAnswerReports = new Set() const acceptedUnconfirmedReports = new Set() - const acceptedAsksAwaitingResolution = new Set() + const conflictUnconfirmedReports = new Set() const lateAskReports = new Map< string, { @@ -1645,7 +1645,7 @@ export function useAgentSession(deps: AgentSessionDeps) { conversationStore.recordAskAnswer(askId, normalizedAnswer, currentThreadId) conversationStore.setAskAnswering(askId, true) const answerChannelEpoch = resolutionChannelEpoch - const answerStartedWithoutResolutionChannel = connection === 'dropped' + const answerStartedWithoutResolutionChannel = connection !== 'live' const resolutionChannelWasLost = () => answerStartedWithoutResolutionChannel || resolutionChannelEpoch !== answerChannelEpoch @@ -1656,6 +1656,7 @@ export function useAgentSession(deps: AgentSessionDeps) { answer: normalizedAnswer, currentThreadId, receipt, + runApprovalAnswer, resolutionChannelWasLost }) } catch (error) { @@ -1664,6 +1665,7 @@ export function useAgentSession(deps: AgentSessionDeps) { error, askId, currentThreadId, + runApprovalAnswer, resolutionChannelWasLost() ) return false @@ -1675,15 +1677,17 @@ export function useAgentSession(deps: AgentSessionDeps) { answer, currentThreadId, receipt, + runApprovalAnswer, resolutionChannelWasLost }: { askId: string answer: AgentAnswerRequest currentThreadId: string receipt: AgentAnswerReceipt + runApprovalAnswer: boolean resolutionChannelWasLost: () => boolean }): boolean { - if (!receiptNamesCommittedAnswer(receipt, answer)) + if (!receiptNamesCommittedAnswer(receipt, runApprovalAnswer)) return commitStatusOnlyAskReceipt({ askId, answer, @@ -1691,7 +1695,7 @@ export function useAgentSession(deps: AgentSessionDeps) { resolutionChannelWasLost }) const committed = toAskResolution(receipt) - acceptedAsksAwaitingResolution.delete(askId) + conversationStore.clearAskAwaitingResolution(askId) const superseded = committed.status === 'answered' && isSupersededAnswer(answer, committed) reportSupersededAnswer(askId, committed) @@ -1706,17 +1710,14 @@ export function useAgentSession(deps: AgentSessionDeps) { selected?: string[] | null other_text?: string }, - submitted?: AgentAnswerRequest + runApprovalAnswer = false ): boolean { if (receipt.status !== undefined && receipt.status !== 'answered') return false if (receipt.other_text !== undefined) return true if (receipt.selected === undefined || receipt.selected === null) return false - const runApproval = - submitted?.selected.length === 1 && - (submitted.selected[0] === 'run' || submitted.selected[0] === 'cancel') - return !runApproval || receipt.selected.length > 0 + return !runApprovalAnswer || receipt.selected.length > 0 } function commitStatusOnlyAskReceipt({ @@ -1733,9 +1734,10 @@ export function useAgentSession(deps: AgentSessionDeps) { const channelWasLost = resolutionChannelWasLost() const resolutionStillPending = conversationStore.submittedAskAnswer(askId) !== undefined - if (resolutionStillPending) acceptedAsksAwaitingResolution.add(askId) if (resolutionStillPending) - reportAcceptedRecoveredAnswer(askId, channelWasLost) + conversationStore.markAskAwaitingResolution(askId, 'accepted') + if (resolutionStillPending) + reportAcceptedRecoveredAnswer(askId, connection === 'dropped') recordDeliveredAsk(askId) conversationStore.commitAsk(askId, currentThreadId, { status: 'retired' }) if (!channelWasLost && resolutionStillPending) @@ -1767,18 +1769,30 @@ export function useAgentSession(deps: AgentSessionDeps) { pushError(i18n.global.t('agent.runApproval.answerAcceptedUnconfirmed')) } + function reportUnconfirmedConflict(askId: string): void { + if (conflictUnconfirmedReports.has(askId)) return + conflictUnconfirmedReports.add(askId) + reportError(new Error('restored approval conflict cannot be verified'), { + surface: 'agent', + errorType: 'agent_ask_answer_unconfirmed' + }) + pushError(i18n.global.t('agent.runApproval.answerUncertain')) + } + function handleAnswerFailure( error: unknown, askId: string, threadId: string, + runApprovalAnswer: boolean, resolutionChannelWasLost = connection === 'dropped' ): boolean { - acceptedAsksAwaitingResolution.delete(askId) + conversationStore.clearAskAwaitingResolution(askId) if (error instanceof AgentApiError && error.status === 409) { return handleConflictAnswerFailure( error, askId, threadId, + runApprovalAnswer, resolutionChannelWasLost ) } @@ -1815,6 +1829,7 @@ export function useAgentSession(deps: AgentSessionDeps) { error: AgentApiError, askId: string, threadId: string, + runApprovalAnswer: boolean, resolutionChannelWasLost: boolean ): boolean { const conflict = zAgentAskConflict.safeParse(error.body) @@ -1833,7 +1848,7 @@ export function useAgentSession(deps: AgentSessionDeps) { const submitted = conversationStore.submittedAskAnswer(askId) if ( conflict.success && - receiptNamesCommittedAnswer(conflict.data, submitted) + receiptNamesCommittedAnswer(conflict.data, runApprovalAnswer) ) { const committed = toAskResolution(conflict.data) const superseded = @@ -1868,8 +1883,10 @@ export function useAgentSession(deps: AgentSessionDeps) { threadId ) if (!resolutionChannelWasLost) { - if (submitted) + if (submitted) { conversationStore.recordAskAnswer(askId, submitted, threadId) + conversationStore.markAskAwaitingResolution(askId, 'conflict') + } return } reportError(error, { @@ -2104,7 +2121,7 @@ export function useAgentSession(deps: AgentSessionDeps) { function handleResolvedAsk( event: Extract ): void { - acceptedAsksAwaitingResolution.delete(event.data.ask_id) + conversationStore.clearAskAwaitingResolution(event.data.ask_id) const resolution = toAskResolution(event.data) reportSupersededAnswer(event.data.ask_id, resolution) // The owning transport may already be gone, so retire the ask explicitly @@ -2159,18 +2176,16 @@ export function useAgentSession(deps: AgentSessionDeps) { if (!live) { if (connection === 'initial' && !receivedStatusSnapshot) { receivedStatusSnapshot = true - connection = 'dropped' - resolutionChannelEpoch += 1 - for (const askId of acceptedAsksAwaitingResolution) - reportAcceptedRecoveredAnswer(askId, true) return } receivedStatusSnapshot = true if (connection === 'dropped') return connection = 'dropped' resolutionChannelEpoch += 1 - for (const askId of acceptedAsksAwaitingResolution) - reportAcceptedRecoveredAnswer(askId, true) + for (const awaiting of conversationStore.asksAwaitingResolution()) + if (awaiting.kind === 'accepted') + reportAcceptedRecoveredAnswer(awaiting.askId, true) + else reportUnconfirmedConflict(awaiting.askId) return } receivedStatusSnapshot = true @@ -2210,7 +2225,6 @@ export function useAgentSession(deps: AgentSessionDeps) { if (!isTurnLive(turn, ownedGeneration)) return if (state.rerun) void reconcileTurn(turn, state.rerunCause, state.automaticRerun) - else if (!isAutomaticRerun) void reconcileTurn(turn, cause, true) }, TURN_RECOVERY_DEADLINE_MS) try { const needsRerun = await recoverTurn( @@ -2541,25 +2555,12 @@ export function useAgentSession(deps: AgentSessionDeps) { if (usage) usage.used = true } - function clearLateAskReportsForTurn(turn: LiveTurn): void { - for (const [key, scheduled] of lateAskReports) { - if ( - scheduled.threadId !== turn.threadId || - scheduled.messageId !== turn.messageId - ) - continue - clearTimeout(scheduled.timer) - lateAskReports.delete(key) - } - } - function settleTerminalTurn( turn: LiveTurn, parts: Extract['parts'] ): void { markRecoveredSkillUse(turn, parts) finishSkillTurn(turn) - clearLateAskReportsForTurn(turn) conversationStore.settleTurn(turn, parts) markStoppedTurnReady(turn) } diff --git a/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts b/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts index 7f3d0545833..c715bfea7c9 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts @@ -7,9 +7,19 @@ import { createAgentEventSource } from './agentEventSource' function fakeHost(readyState?: number) { const target = new EventTarget() + const socketTarget = new EventTarget() const registered = new Set() const host: AgentEventHost = { - socket: readyState === undefined ? null : { readyState }, + socket: + readyState === undefined + ? null + : { + readyState, + addEventListener: (type, listener) => + socketTarget.addEventListener(type, listener), + removeEventListener: (type, listener) => + socketTarget.removeEventListener(type, listener) + }, addCustomEventListener(type, listener) { registered.add(type) target.addEventListener(type, listener as EventListener) @@ -28,7 +38,10 @@ function fakeHost(readyState?: number) { const emit = (type: string, data?: unknown): void => { target.dispatchEvent(new CustomEvent(type, { detail: data })) } - return { host, emit, registered } + const open = (): void => { + socketTarget.dispatchEvent(new Event('open')) + } + return { host, emit, open, registered } } describe('createAgentEventSource', () => { @@ -64,8 +77,7 @@ describe('createAgentEventSource', () => { const status = vi.fn() createAgentEventSource(host).onStatus?.(status) - expect(status).toHaveBeenCalledOnce() - expect(status).toHaveBeenLastCalledWith(false) + expect(status).not.toHaveBeenCalled() emit('reconnecting') expect(status).toHaveBeenLastCalledWith(false) @@ -89,11 +101,21 @@ describe('createAgentEventSource', () => { const status = vi.fn() const unsubscribe = createAgentEventSource(host).onStatus?.(status) - expect(status).toHaveBeenCalledOnce() - expect(status).toHaveBeenLastCalledWith(false) + expect(status).not.toHaveBeenCalled() unsubscribe?.() emit('reconnected') + expect(status).not.toHaveBeenCalled() + }) + + it('reports the first open of a socket that was connecting at bind time', () => { + const { host, open } = fakeHost(WebSocket.CONNECTING) + const status = vi.fn() + + createAgentEventSource(host).onStatus?.(status) + open() + expect(status).toHaveBeenCalledOnce() + expect(status).toHaveBeenCalledWith(true) }) }) diff --git a/src/workbench/extensions/agent/services/agent/agentEventSource.ts b/src/workbench/extensions/agent/services/agent/agentEventSource.ts index 7cffdc7e781..a91fb74f8a8 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventSource.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventSource.ts @@ -2,7 +2,11 @@ import type { AgentEventSource } from '../../composables/agent/useAgentSession' import { AGENT_WS_EVENT_TYPES } from '../../schemas/agentApiSchema' export interface AgentEventHost { - socket: { readyState: number } | null + socket: { + readyState: number + addEventListener?(type: 'open', listener: () => void): void + removeEventListener?(type: 'open', listener: () => void): void + } | null addCustomEventListener( type: string, listener: (event: CustomEvent) => void @@ -37,10 +41,13 @@ export function createAgentEventSource(host: AgentEventHost): AgentEventSource { onStatus(listener) { const onDown = (): void => listener(false) const onUp = (): void => listener(true) + const initialSocket = host.socket host.addEventListener('reconnecting', onDown) host.addEventListener('reconnected', onUp) - listener(host.socket?.readyState === OPEN) + if (initialSocket?.readyState === OPEN) listener(true) + else initialSocket?.addEventListener?.('open', onUp) return () => { + initialSocket?.removeEventListener?.('open', onUp) host.removeEventListener('reconnecting', onDown) host.removeEventListener('reconnected', onUp) } diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts index 4f1b728e9d3..dbf3779c924 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts @@ -777,6 +777,7 @@ describe('normalizeAgentTranscript', () => { const transcript = normalizeAgentTranscript( [first, second, latest], + (askId) => askId === 'ask-retired', (askId) => askId === 'ask-retired' ) diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.ts index 5e24dcaf6d8..d063295fc3d 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.ts @@ -537,15 +537,14 @@ function keepAuthoritativeAsk( !isAskUnavailable(askId) ) }) + // The snapshot's newest ask stays authoritative even while its answer is + // in flight. Falling back then would resurrect an older card. const authoritativeAskId = - rowAsk !== undefined && !isAskUnavailable(rowAsk.ask_id) + rowAsk !== undefined && !isAskRetired(rowAsk.ask_id) ? rowAsk.ask_id : ((priorAvailableAskId === undefined ? undefined - : askIdOf(priorAvailableAskId)) ?? - (rowAsk !== undefined && isAskRetired(rowAsk.ask_id) - ? rowAsk.ask_id - : undefined)) + : askIdOf(priorAvailableAskId)) ?? rowAsk?.ask_id) message.parts = message.parts.filter((part) => { if (!isOpenAskPart(part)) return true const askId = askIdOf(part) diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index bfcaa018942..8d15d2d6af7 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -464,8 +464,14 @@ export const useAgentConversationStore = defineStore( } function confirmAskPending(askId: string, owner?: string): void { - provisionalRetiredAsksFor(owner).delete(askId) const key = threadKey(owner) + const provisional = provisionalRetiredAsks.get(key) + if ( + provisional?.delete(askId) !== true || + retiredAsks.get(key)?.has(askId) === true + ) + return + if (provisional.size === 0) provisionalRetiredAsks.delete(key) const reopen = (message: AssistantMessage): void => { message.parts = message.parts.map((part) => (part.type === 'askUser' || part.type === 'deleteApproval') && @@ -482,7 +488,11 @@ export const useAgentConversationStore = defineStore( const submittedAskAnswers = new Map< string, - { answer: AgentAnswerRequest; owner: string } + { + answer: AgentAnswerRequest + owner: string + awaitingResolution?: 'accepted' | 'conflict' + } >() function recordAskAnswer( @@ -505,6 +515,31 @@ export const useAgentConversationStore = defineStore( return answer } + function markAskAwaitingResolution( + askId: string, + kind: 'accepted' | 'conflict' + ): void { + const submitted = submittedAskAnswers.get(askId) + if (submitted) submitted.awaitingResolution = kind + } + + function asksAwaitingResolution(): Array<{ + askId: string + kind: 'accepted' | 'conflict' + }> { + return [...submittedAskAnswers] + .filter(([, submitted]) => submitted.awaitingResolution !== undefined) + .map(([askId, submitted]) => ({ + askId, + kind: submitted.awaitingResolution! + })) + } + + function clearAskAwaitingResolution(askId: string): void { + const submitted = submittedAskAnswers.get(askId) + if (submitted) submitted.awaitingResolution = undefined + } + function discardDetachedAskState(): void { const retainedThreads = new Set( [...backgroundTurns.values()].map((turn) => turn.threadId) @@ -1036,6 +1071,8 @@ export const useAgentConversationStore = defineStore( persistedParts: AssistantMessage['parts'] | undefined ): void { provisionalRetiredAsks.delete(turn.threadId) + for (const [askId, submitted] of submittedAskAnswers) + if (submitted.owner === turn.threadId) submittedAskAnswers.delete(askId) const slot = activeSlot.value const isActive = slot !== null && @@ -1418,6 +1455,9 @@ export const useAgentConversationStore = defineStore( recordAskAnswer, submittedAskAnswer, takeSubmittedAskAnswer, + markAskAwaitingResolution, + asksAwaitingResolution, + clearAskAwaitingResolution, commitAsk, retireAsk, isAskRetired, From a161f8f7b1b071ab69dd3113e1b904442e953ac2 Mon Sep 17 00:00:00 2001 From: bymyself Date: Sat, 17 Oct 2026 05:36:10 +0000 Subject: [PATCH 31/36] fix(agent): preserve pending answer ownership --- .../composables/agent/useAgentSession.test.ts | 19 +++++++++++++++++-- .../composables/agent/useAgentSession.ts | 9 ++++++--- .../agent/agentConversationStore.test.ts | 14 ++++++++++++++ .../stores/agent/agentConversationStore.ts | 16 +++++++++------- 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index fc0a874f008..99f0934bbd8 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -2069,7 +2069,7 @@ describe('useAgentSession (v1 composition root)', () => { receipt: AgentAnswerReceipt = { status: 'answered' } ) => { const answerAsk = vi.fn(async () => receipt) - const { source, emit } = fakeEvents() + const { source, emit, status } = fakeEvents() const session = useAgentSession({ rest: fakeRest({ answerAsk }), events: source @@ -2077,7 +2077,7 @@ describe('useAgentSession (v1 composition root)', () => { session.start() await session.sendMessage('pick for me') emit(askUser('msg-1')) - return { session, answerAsk, emit } + return { session, answerAsk, emit, status } } const card = () => useAgentConversationStore().messages[0].parts.find( @@ -2332,6 +2332,21 @@ describe('useAgentSession (v1 composition root)', () => { ]) }) + it('keeps a pre-open accepted answer tracked until a later socket drop', async () => { + const { session, status } = await parkedOnQuestion() + await session.answerAsk('turn-1:call-1', { selected: ['sdxl'] }) + + status(true) + status(false) + + expect(session.notices.value).toEqual([ + { + level: 'error', + text: enMessages.agent.runApproval.answerAcceptedUnconfirmed + } + ]) + }) + it.for([400, 500])( 'marks the outcome unconfirmed when the answer may have landed (%i)', async (status) => { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 78cd92c38ee..597b831e787 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -1734,14 +1734,17 @@ export function useAgentSession(deps: AgentSessionDeps) { const channelWasLost = resolutionChannelWasLost() const resolutionStillPending = conversationStore.submittedAskAnswer(askId) !== undefined - if (resolutionStillPending) - conversationStore.markAskAwaitingResolution(askId, 'accepted') if (resolutionStillPending) reportAcceptedRecoveredAnswer(askId, connection === 'dropped') recordDeliveredAsk(askId) conversationStore.commitAsk(askId, currentThreadId, { status: 'retired' }) - if (!channelWasLost && resolutionStillPending) + if ( + resolutionStillPending && + (!channelWasLost || connection === 'initial') + ) { conversationStore.recordAskAnswer(askId, answer, currentThreadId) + conversationStore.markAskAwaitingResolution(askId, 'accepted') + } return !supersededAnswerReports.has(askId) } diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index 9b2447b7a3b..ff41fab4c75 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -1295,6 +1295,20 @@ describe('useAgentConversationStore', () => { expect(store.isAskRetired('ask-2', 'background')).toBe(true) }) + it('does not clear a live answer when a stale turn settlement arrives', () => { + const store = useAgentConversationStore() + store.setThreadId('th') + store.startTurn(T1) + store.recordAskAnswer('ask-1', { selected: ['run'] }, 'th') + + store.settleTurn( + { threadId: 'th', messageId: 'stale' as TurnId }, + undefined + ) + + expect(store.submittedAskAnswer('ask-1')).toEqual({ selected: ['run'] }) + }) + it('holds the thread id and clears it on reset', () => { const store = useAgentConversationStore() store.setThreadId('th-7') diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 8d15d2d6af7..7a907c155a0 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -466,12 +466,9 @@ export const useAgentConversationStore = defineStore( function confirmAskPending(askId: string, owner?: string): void { const key = threadKey(owner) const provisional = provisionalRetiredAsks.get(key) - if ( - provisional?.delete(askId) !== true || - retiredAsks.get(key)?.has(askId) === true - ) - return + if (provisional?.delete(askId) !== true) return if (provisional.size === 0) provisionalRetiredAsks.delete(key) + if (retiredAsks.get(key)?.has(askId) === true) return const reopen = (message: AssistantMessage): void => { message.parts = message.parts.map((part) => (part.type === 'askUser' || part.type === 'deleteApproval') && @@ -540,6 +537,11 @@ export const useAgentConversationStore = defineStore( if (submitted) submitted.awaitingResolution = undefined } + function clearSubmittedAskAnswers(owner: string): void { + for (const [askId, submitted] of submittedAskAnswers) + if (submitted.owner === owner) submittedAskAnswers.delete(askId) + } + function discardDetachedAskState(): void { const retainedThreads = new Set( [...backgroundTurns.values()].map((turn) => turn.threadId) @@ -1071,14 +1073,13 @@ export const useAgentConversationStore = defineStore( persistedParts: AssistantMessage['parts'] | undefined ): void { provisionalRetiredAsks.delete(turn.threadId) - for (const [askId, submitted] of submittedAskAnswers) - if (submitted.owner === turn.threadId) submittedAskAnswers.delete(askId) const slot = activeSlot.value const isActive = slot !== null && turn.threadId === slot.threadId && turn.messageId === slot.turnId if (isActive) { + clearSubmittedAskAnswers(turn.threadId) finishWithPersistedParts(slot.message, persistedParts) rememberDepartedActiveTurn('settled-turn') slot.transport.settle() @@ -1088,6 +1089,7 @@ export const useAgentConversationStore = defineStore( } const entry = backgroundTurns.get(turn.messageId) if (!entry || entry.messageId !== turn.messageId || entry.settled) return + clearSubmittedAskAnswers(turn.threadId) finishWithPersistedParts(entry.message, persistedParts) entry.transport.settle() entry.settled = true From ad7dc4d9d86ccd77ec0c88ca81c14db9fd52291e Mon Sep 17 00:00:00 2001 From: bymyself Date: Sat, 17 Oct 2026 20:37:12 +0000 Subject: [PATCH 32/36] fix(agent): close approval recovery gaps --- .../composables/agent/useAgentSession.test.ts | 26 +++++++++++++++++++ .../composables/agent/useAgentSession.ts | 9 +++++-- .../services/agent/agentEventSource.test.ts | 26 ++++++++++++++++++- .../agent/services/agent/agentEventSource.ts | 23 +++++++++++++--- .../stores/agent/agentConversationStore.ts | 18 +++++++++++++ 5 files changed, 95 insertions(+), 7 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 99f0934bbd8..5269de356ab 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -2810,6 +2810,32 @@ describe('useAgentSession (v1 composition root)', () => { expect(session.notices.value).toHaveLength(1) }) + it('treats an empty approval receipt as status-only for object answers', async () => { + const answerAsk = vi + .fn() + .mockResolvedValue({ status: 'answered', selected: [] }) + const events = fakeEvents() + const session = useAgentSession({ + rest: fakeRest({ answerAsk }), + events: events.source + }) + session.start() + events.status(true) + await session.sendMessage('build it and run it') + events.emit(runApproval('msg-1')) + + const answered = await session.answerAsk('turn-1:call-1', { + selected: ['run'] + }) + + expect(answered).toBe(true) + expect(reportError).not.toHaveBeenCalledWith(expect.any(Error), { + surface: 'agent', + errorType: 'agent_ask_answer_superseded' + }) + expect(session.notices.value).toEqual([]) + }) + // The resolution frame retires the card on its way through, so by the time // a slow request rejects there is nothing left to warn about — and telling // the user their answer might not have arrived, after the server has said diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 597b831e787..06e5b9e988f 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -1640,8 +1640,13 @@ export function useAgentSession(deps: AgentSessionDeps) { return false } if (answeringAskIds.value.has(askId)) return false - const runApprovalAnswer = typeof answer === 'string' - const normalizedAnswer = runApprovalAnswer ? { selected: [answer] } : answer + const legacyRunApprovalAnswer = typeof answer === 'string' + const runApprovalAnswer = + legacyRunApprovalAnswer || + conversationStore.askKind(askId) === 'runApproval' + const normalizedAnswer = legacyRunApprovalAnswer + ? { selected: [answer] } + : answer conversationStore.recordAskAnswer(askId, normalizedAnswer, currentThreadId) conversationStore.setAskAnswering(askId, true) const answerChannelEpoch = resolutionChannelEpoch diff --git a/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts b/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts index c715bfea7c9..6d7bb72f04d 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts @@ -41,7 +41,16 @@ function fakeHost(readyState?: number) { const open = (): void => { socketTarget.dispatchEvent(new Event('open')) } - return { host, emit, open, registered } + const attachSocket = (nextReadyState: number): void => { + host.socket = { + readyState: nextReadyState, + addEventListener: (type, listener) => + socketTarget.addEventListener(type, listener), + removeEventListener: (type, listener) => + socketTarget.removeEventListener(type, listener) + } + } + return { host, emit, open, attachSocket, registered } } describe('createAgentEventSource', () => { @@ -118,4 +127,19 @@ describe('createAgentEventSource', () => { expect(status).toHaveBeenCalledOnce() expect(status).toHaveBeenCalledWith(true) }) + + it('observes a socket created after status binding', () => { + vi.useFakeTimers() + const { host, attachSocket } = fakeHost() + const status = vi.fn() + + const unsubscribe = createAgentEventSource(host).onStatus?.(status) + attachSocket(WebSocket.OPEN) + vi.advanceTimersByTime(50) + + expect(status).toHaveBeenCalledOnce() + expect(status).toHaveBeenCalledWith(true) + unsubscribe?.() + vi.useRealTimers() + }) }) diff --git a/src/workbench/extensions/agent/services/agent/agentEventSource.ts b/src/workbench/extensions/agent/services/agent/agentEventSource.ts index a91fb74f8a8..a5f716b9e71 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventSource.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventSource.ts @@ -41,13 +41,28 @@ export function createAgentEventSource(host: AgentEventHost): AgentEventSource { onStatus(listener) { const onDown = (): void => listener(false) const onUp = (): void => listener(true) - const initialSocket = host.socket + let observedSocket = host.socket + let socketObserver: ReturnType | undefined + const bindSocket = (): void => { + const socket = host.socket + if (socket === observedSocket) return + observedSocket?.removeEventListener?.('open', onUp) + observedSocket = socket + if (socket?.readyState === OPEN) listener(true) + else socket?.addEventListener?.('open', onUp) + if (socket && socketObserver !== undefined) { + clearInterval(socketObserver) + socketObserver = undefined + } + } host.addEventListener('reconnecting', onDown) host.addEventListener('reconnected', onUp) - if (initialSocket?.readyState === OPEN) listener(true) - else initialSocket?.addEventListener?.('open', onUp) + if (observedSocket?.readyState === OPEN) listener(true) + else observedSocket?.addEventListener?.('open', onUp) + if (!observedSocket) socketObserver = setInterval(bindSocket, 50) return () => { - initialSocket?.removeEventListener?.('open', onUp) + if (socketObserver !== undefined) clearInterval(socketObserver) + observedSocket?.removeEventListener?.('open', onUp) host.removeEventListener('reconnecting', onDown) host.removeEventListener('reconnected', onUp) } diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 7a907c155a0..496850db523 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -1425,6 +1425,23 @@ export const useAgentConversationStore = defineStore( ) } + function askKind( + askId: string + ): 'runApproval' | 'askUser' | 'deleteApproval' | undefined { + for (const message of messages.value) { + const part = message.parts.find( + (candidate) => + isPendingAskPart(candidate) && candidate.askId === askId + ) + if ( + part?.type === 'runApproval' || + part?.type === 'askUser' || + part?.type === 'deleteApproval' + ) + return part.type + } + } + const activeMessageId = computed(() => activeMessage.value?.id ?? null) const isStreaming = computed(() => activeMessage.value?.streaming ?? false) const status = computed(() => { @@ -1460,6 +1477,7 @@ export const useAgentConversationStore = defineStore( markAskAwaitingResolution, asksAwaitingResolution, clearAskAwaitingResolution, + askKind, commitAsk, retireAsk, isAskRetired, From 4b404db0b94167a3a6d95c3ff7cb667e72f1d5d8 Mon Sep 17 00:00:00 2001 From: bymyself Date: Mon, 26 Oct 2026 09:19:12 +0000 Subject: [PATCH 33/36] fix(agent): report an unconfirmed winner after a mid-POST drop A status-only 202 that lands after the socket dropped during the POST now reports the accepted-but-unconfirmed outcome even when the socket has reconnected by the time the receipt arrives. The report keys on a real drop (the answer started while dropped, or the drop epoch moved), not on the socket state at receipt time. An answer started before the socket's first open that completes after it opens is tracked for a later drop instead of reported, since no drop happened. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301463988 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301648754 --- .../composables/agent/useAgentSession.test.ts | 68 +++++++++++++++++++ .../composables/agent/useAgentSession.ts | 29 ++++---- 2 files changed, 82 insertions(+), 15 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index aded52ee38e..d336e35fb92 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -5563,6 +5563,74 @@ describe('useAgentSession (v1 composition root)', () => { expect(session.notices.value).toHaveLength(1) }) + it('(g39b) a drop and reconnect during the POST cannot silently confirm which answer won', async () => { + let accept: (receipt: AgentAnswerAccepted) => void = () => {} + const rest = fakeRest({ + answerAsk: vi.fn( + () => + new Promise((resolve) => { + accept = resolve + }) + ) + }) + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + await session.sendMessage('go') + emit(runApproval('msg-1')) + + const answered = session.answerAsk('turn-1:call-1', { selected: ['run'] }) + status(false) + status(true) + accept({ status: 'answered', selected: [] }) + await answered + + expect(reportError).toHaveBeenCalledWith(expect.any(Error), { + surface: 'agent', + errorType: 'agent_ask_answer_winner_unconfirmed' + }) + expect(session.notices.value).toEqual([ + { + level: 'error', + text: enMessages.agent.runApproval.answerAcceptedUnconfirmed + } + ]) + }) + + it('(g39c) a first open during the POST tracks the accepted answer instead of reporting it', async () => { + let accept: (receipt: AgentAnswerAccepted) => void = () => {} + const rest = fakeRest({ + answerAsk: vi.fn( + () => + new Promise((resolve) => { + accept = resolve + }) + ) + }) + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start() + await session.sendMessage('go') + emit(runApproval('msg-1')) + + const answered = session.answerAsk('turn-1:call-1', { selected: ['run'] }) + status(true) + accept({ status: 'answered', selected: [] }) + await answered + + expect(session.notices.value).toEqual([]) + + status(false) + + expect(session.notices.value).toEqual([ + { + level: 'error', + text: enMessages.agent.runApproval.answerAcceptedUnconfirmed + } + ]) + }) + it('(g38) a remount does not restore an ask the prior session retired', async () => { vi.useFakeTimers() try { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 1039d754709..a0eaee2f6e5 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -1764,10 +1764,12 @@ export function useAgentSession(deps: AgentSessionDeps) { conversationStore.recordAskAnswer(askId, normalizedAnswer, currentThreadId) conversationStore.setAskAnswering(askId, true) const answerChannelEpoch = resolutionChannelEpoch - const answerStartedWithoutResolutionChannel = connection !== 'live' + const answerStartedBeforeFirstOpen = connection === 'initial' + const answerStartedDropped = connection === 'dropped' + const resolutionChannelDropped = () => + answerStartedDropped || resolutionChannelEpoch !== answerChannelEpoch const resolutionChannelWasLost = () => - answerStartedWithoutResolutionChannel || - resolutionChannelEpoch !== answerChannelEpoch + answerStartedBeforeFirstOpen || resolutionChannelDropped() try { const receipt = await sendAnswer(currentThreadId, askId, normalizedAnswer) return commitAskReceipt({ @@ -1776,7 +1778,7 @@ export function useAgentSession(deps: AgentSessionDeps) { currentThreadId, receipt, runApprovalAnswer, - resolutionChannelWasLost + resolutionChannelDropped }) } catch (error) { if (answeringAskIds.value.has(askId)) @@ -1797,21 +1799,21 @@ export function useAgentSession(deps: AgentSessionDeps) { currentThreadId, receipt, runApprovalAnswer, - resolutionChannelWasLost + resolutionChannelDropped }: { askId: string answer: AgentAnswerRequest currentThreadId: string receipt: AgentAnswerReceipt runApprovalAnswer: boolean - resolutionChannelWasLost: () => boolean + resolutionChannelDropped: () => boolean }): boolean { if (!receiptNamesCommittedAnswer(receipt, runApprovalAnswer)) return commitStatusOnlyAskReceipt({ askId, answer, currentThreadId, - resolutionChannelWasLost + resolutionChannelDropped }) const committed = toAskResolution(receipt) conversationStore.clearAskAwaitingResolution(askId) @@ -1844,24 +1846,21 @@ export function useAgentSession(deps: AgentSessionDeps) { askId, answer, currentThreadId, - resolutionChannelWasLost + resolutionChannelDropped }: { askId: string answer: AgentAnswerRequest currentThreadId: string - resolutionChannelWasLost: () => boolean + resolutionChannelDropped: () => boolean }): boolean { - const channelWasLost = resolutionChannelWasLost() + const channelDropped = resolutionChannelDropped() const resolutionStillPending = conversationStore.submittedAskAnswer(askId) !== undefined if (resolutionStillPending) - reportAcceptedRecoveredAnswer(askId, connection === 'dropped') + reportAcceptedRecoveredAnswer(askId, channelDropped) recordDeliveredAsk(askId) conversationStore.commitAsk(askId, currentThreadId, { status: 'retired' }) - if ( - resolutionStillPending && - (!channelWasLost || connection === 'initial') - ) { + if (resolutionStillPending && !channelDropped) { conversationStore.recordAskAnswer(askId, answer, currentThreadId) conversationStore.markAskAwaitingResolution(askId, 'accepted') } From 21b9f60478274217c9930012c974e614d400f417 Mon Sep 17 00:00:00 2001 From: bymyself Date: Mon, 26 Oct 2026 09:30:15 +0000 Subject: [PATCH 34/36] fix(agent): keep provisional tombstones through a stale settlement settleTurn cleared every provisional ask tombstone on the thread before it checked that the settlement matched the active slot or a background turn. A stale settlement could therefore let a delayed duplicate frame redraw a card that recovery had confirmed stale. The tombstones are now cleared together with the submitted answers, only after a turn matches. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301648767 --- .../stores/agent/agentConversationStore.test.ts | 15 ++++++++++++++- .../agent/stores/agent/agentConversationStore.ts | 8 ++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index 74e21378a03..5c6aad3d3ec 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -1415,11 +1415,12 @@ describe('useAgentConversationStore', () => { expect(store.isAskRetired('ask-2', 'background')).toBe(true) }) - it('does not clear a live answer when a stale turn settlement arrives', () => { + it('does not clear live ask state when a stale turn settlement arrives', () => { const store = useAgentConversationStore() store.setThreadId('th') store.startTurn(T1) store.recordAskAnswer('ask-1', { selected: ['run'] }, 'th') + store.retireAsk('ask-2', { status: 'retired' }, 'th', false) store.settleTurn( { threadId: 'th', messageId: 'stale' as TurnId }, @@ -1427,6 +1428,18 @@ describe('useAgentConversationStore', () => { ) expect(store.submittedAskAnswer('ask-1')).toEqual({ selected: ['run'] }) + expect(store.isAskRetired('ask-2', 'th')).toBe(true) + }) + + it('clears provisional tombstones when the matching turn settles', () => { + const store = useAgentConversationStore() + store.setThreadId('th') + store.startTurn(T1) + store.retireAsk('ask-2', { status: 'retired' }, 'th', false) + + store.settleTurn({ threadId: 'th', messageId: T1 }, undefined) + + expect(store.isAskRetired('ask-2', 'th')).toBe(false) }) it('holds the thread id and clears it on reset', () => { diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 410f6fac72b..a51481c07fd 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -637,7 +637,8 @@ export const useAgentConversationStore = defineStore( if (submitted) submitted.awaitingResolution = undefined } - function clearSubmittedAskAnswers(owner: string): void { + function clearSettledAskState(owner: string): void { + provisionalRetiredAsks.delete(owner) for (const [askId, submitted] of submittedAskAnswers) if (submitted.owner === owner) submittedAskAnswers.delete(askId) } @@ -1174,14 +1175,13 @@ export const useAgentConversationStore = defineStore( persistedParts: AssistantMessage['parts'] | undefined, ending: 'completed' | 'failed' = 'completed' ): void { - provisionalRetiredAsks.delete(turn.threadId) const slot = activeSlot.value const isActive = slot !== null && turn.threadId === slot.threadId && turn.messageId === slot.turnId if (isActive) { - clearSubmittedAskAnswers(turn.threadId) + clearSettledAskState(turn.threadId) finishWithPersistedParts(slot.message, persistedParts) rememberDepartedActiveTurn('settled-turn') slot.transport.settle() @@ -1192,7 +1192,7 @@ export const useAgentConversationStore = defineStore( } const entry = backgroundTurns.get(turn.messageId) if (!entry || entry.messageId !== turn.messageId || entry.settled) return - clearSubmittedAskAnswers(turn.threadId) + clearSettledAskState(turn.threadId) finishWithPersistedParts(entry.message, persistedParts) entry.transport.settle() updateBackgroundTurn(entry.messageId, { settled: true }) From 7a32fd6a726ce642465ab90d12d96a00271d51ae Mon Sep 17 00:00:00 2001 From: bymyself Date: Mon, 26 Oct 2026 09:32:33 +0000 Subject: [PATCH 35/36] fix(agent): cap the unconfirmed answer report ledgers The accepted-unconfirmed and conflict-unconfirmed report ledgers now share the oldest-first cap the superseded-answer ledger already had, so they no longer grow with every ask that hits an unconfirmed report. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301464008 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18949#discussion_r4301648780 --- .../composables/agent/useAgentSession.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index a0eaee2f6e5..599d6b340b0 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -673,12 +673,12 @@ export function useAgentSession(deps: AgentSessionDeps) { } } - function recordSupersededAnswerReport(askId: string): boolean { - if (supersededAnswerReports.has(askId)) return false - supersededAnswerReports.add(askId) - if (supersededAnswerReports.size > MAX_DELIVERED_ASKS) { - const oldest = supersededAnswerReports.values().next().value - if (oldest !== undefined) supersededAnswerReports.delete(oldest) + function recordAnswerReport(reports: Set, askId: string): boolean { + if (reports.has(askId)) return false + reports.add(askId) + if (reports.size > MAX_DELIVERED_ASKS) { + const oldest = reports.values().next().value + if (oldest !== undefined) reports.delete(oldest) } return true } @@ -1881,9 +1881,11 @@ export function useAgentSession(deps: AgentSessionDeps) { askId: string, resolutionChannelWasLost: boolean ): void { - if (!resolutionChannelWasLost || acceptedUnconfirmedReports.has(askId)) + if ( + !resolutionChannelWasLost || + !recordAnswerReport(acceptedUnconfirmedReports, askId) + ) return - acceptedUnconfirmedReports.add(askId) reportError(new Error('accepted restored approval cannot be verified'), { surface: 'agent', errorType: 'agent_ask_answer_winner_unconfirmed' @@ -1892,8 +1894,7 @@ export function useAgentSession(deps: AgentSessionDeps) { } function reportUnconfirmedConflict(askId: string): void { - if (conflictUnconfirmedReports.has(askId)) return - conflictUnconfirmedReports.add(askId) + if (!recordAnswerReport(conflictUnconfirmedReports, askId)) return reportError(new Error('restored approval conflict cannot be verified'), { surface: 'agent', errorType: 'agent_ask_answer_unconfirmed' @@ -2037,7 +2038,7 @@ export function useAgentSession(deps: AgentSessionDeps) { committed, Array.isArray(source.selected) ) || - !recordSupersededAnswerReport(askId) + !recordAnswerReport(supersededAnswerReports, askId) ) return reportError( From 5eb234a4355dc9690e1c45612e659084a92f2e1a Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 26 Oct 2026 03:34:06 -0700 Subject: [PATCH 36/36] test(agent): send approval frame on the Agent socket --- .../tests/agent/agentApprovalSurvivesSocketDrop.spec.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts index 74a85e17734..59c1318384e 100644 --- a/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts +++ b/browser_tests/tests/agent/agentApprovalSurvivesSocketDrop.spec.ts @@ -100,11 +100,10 @@ test.describe( // restored again. test('does not redraw an approval the socket already delivered', async ({ turnLock, - getWebSocket, page }) => { turnLock.parkOnApproval() - turnLock.push(await getWebSocket(), RUN_APPROVAL_EVENT) + turnLock.push(await turnLock.liveSocket(), RUN_APPROVAL_EVENT) await expect(turnLock.approvalCard).toBeVisible() let recoveryResponses = 0 const secondRecoveryResponse = page.waitForResponse((response) => {