From a13360ae68d712e90a48845db8f9b7a3fff97ca4 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Tue, 3 Nov 2026 11:28:18 +0000 Subject: [PATCH] fix(agent): keep a restored finished turn's Worked label (FRONT-3803) (#22791) ## Summary After a reload or reopening a chat from history, every finished Agent turn read "Changes not confirmed on your canvas", although the same turn read "Worked" live and its edits are on the canvas. Restored turns now read "Worked" again, as on 1.59.0.dev2. Fixes [FRONT-3803](https://linear.app/comfyorg/issue/FRONT-3803).
Full context for agent readers **Cause.** #20741 (`08b4792346`, the only canvas-claim change between 1.59.0.dev2 `864bfc939e` and 1.60.0.dev1 `9b73bee0a5`) made `parseToolCallEntry` in `agentTranscript.ts` set `canvasConfirmed: false` on every successful canvas-mutating call read from a stored row. A stored row carries no browser observation, so after a reload nothing could ever confirm those calls. `preserveCanvasConfirmation` upgraded them back only for calls the same page session had confirmed live, which is why in-session turns kept "Worked" until the reload. #20741 flipped `agentToolCallHistoryPersistence.spec.ts` to expect the unconfirmed label; this PR flips it back. **Fix.** - `agentTranscript.ts`: a stored row makes no canvas claim (no `canvasConfirmed`). - `agentConversationStore.ts`: when a turn settles in the session that ran it, the verdict its live calls reached carries onto the stored row, **true or false** (previously true only, because stored rows defaulted to false). Applied on both settle paths: the socket reconcile (`reconcileSettledTurn`) and the recovery read (`settleTurn`). Without the second one, a live denial settled by the recovery read would now drop to "Worked". This is the canvas half of [FRONT-3757](https://linear.app/comfyorg/issue/FRONT-3757); its tab-link half is unchanged. - A call still waiting for canvas catch-up when its turn settles is stored as not confirmed, which is the same verdict the transport's flush records. A verdict moves onto a stored tool only when the tool names match (review round 1). - Live behaviour from #20741 is unchanged: unobserved, timed-out or unavailable-canvas writes still read "not confirmed" live and after settle in that session. Only a restored row, which never had an observation, stops claiming one. No reconcile pass, no new store writes; this is display state on the message parts only (KEEP-ALIVE #14 untouched). **Backport.** #20741 is on `core/1.60`, `cloud/1.60`, `core/1.59`, `cloud/1.59` natively and was backported to `core/1.58` (#22062) and `cloud/1.58` (#21966). The frontend pin decision needs `core/1.60`; labels are set for all six lines.
## Verification - Black-box: `browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts` (reload, then reopen from history) restored to expect "Worked" with no unconfirmed marker. **Red on parent `a8c2ace17f`** (summary reads "Changes not confirmed on your canvas"), **green on this branch** (rerun at `2d96033bfc`). - Related canvas-claim specs, `--project=cloud`, local backend: `agentCanvasClaimNeedsObservation`, `agentCanvasLagsToolCallCompletion`, `agentRefusedDocSubscribe`, `agentGraphOpLostOnDisconnect`, `agentSchemaSkewTellsTheUser`, `agentZeroNodesOnUnreadableSchema`, `agentToolCallHistoryPersistence`: 15/15 pass. - Unit: store cases cover the live verdict (confirmed, denied, still pending) through both settle paths, plus a name-mismatch guard; each fails without the change that it covers. Agent suites: 194 files, 4914 pass. `pnpm typecheck`, `typecheck:browser`, oxlint, eslint, oxfmt pass via hooks. --- .../agentToolCallHistoryPersistence.spec.ts | 24 ++- .../services/agent/agentTranscript.test.ts | 3 +- .../agent/services/agent/agentTranscript.ts | 6 +- .../agent/agentConversationStore.test.ts | 146 +++++++++++++++++- .../stores/agent/agentConversationStore.ts | 75 +++++---- 5 files changed, 214 insertions(+), 40 deletions(-) diff --git a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts index 17f085ef1e5..6ce0b7a86b2 100644 --- a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts +++ b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts @@ -28,7 +28,7 @@ const TRACE = [ THINKING_ROUND_1, 'Search nodes', THINKING_ROUND_2, - enMessages.agent.toolNotConfirmedOnCanvas.replace('{tool}', 'Add node'), + 'Add node', THINKING_FINAL ] @@ -39,10 +39,16 @@ test( 'keeps a completed turn work summary, thinking included, after reload and chat switching', { tag: ['@cloud', '@ui'], - annotation: { - type: 'source', - description: 'https://linear.app/comfyorg/issue/PM-717' - } + annotation: [ + { + type: 'source', + description: 'https://linear.app/comfyorg/issue/PM-717' + }, + { + type: 'source', + description: 'https://linear.app/comfyorg/issue/FRONT-3803' + } + ] }, async ({ page, promptHistory, workflowSelection }) => { await new AgentPanel(page).open() @@ -148,10 +154,16 @@ test( await expect(reopenedPanel).toBeVisible({ timeout: 30_000 }) const summary = reopenedPanel.getByRole('button', { - name: enMessages.agent.workedNotConfirmedOnCanvas, + name: enMessages.agent.worked, exact: true }) await expect(summary).toBeVisible({ timeout: 10_000 }) + await expect( + reopenedPanel.getByText(enMessages.agent.workedNotConfirmedOnCanvas) + ).toHaveCount(0) + await expect( + reopenedPanel.getByTestId('agent-canvas-unconfirmed') + ).toHaveCount(0) await expect(summary).toHaveAttribute('aria-expanded', 'false') await expect(reopenedPanel.getByText('Search nodes')).toBeHidden() diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts index dfb81d6b93d..2d1bd732363 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts @@ -1070,8 +1070,7 @@ describe('normalizeAgentTranscript', () => { callId: 'call-2', name: 'add_node', state: 'done', - ok: true, - canvasConfirmed: false + ok: true }, { type: 'text', text: 'Done', state: 'done' } ]) diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.ts index 2fb58ff1f43..0830a0b2754 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.ts @@ -22,7 +22,6 @@ import { isOpenAskPart, toAskOrNoticePart } from './agentMessageParts' -import { toolCanMutateCanvas } from './agentToolClassification' type AttachmentKind = 'image' | 'video' | 'audio' @@ -312,10 +311,7 @@ function parseToolCallEntry( callId: toolCallId || id, name: toolName, state, - ...toolPartOptionalFields(ok, skill, durationMs), - ...(ok === true && toolCanMutateCanvas(toolName) - ? { canvasConfirmed: false } - : {}) + ...toolPartOptionalFields(ok, skill, durationMs) } } } diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index 8eb6140f94e..82e941bf3d0 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -2346,6 +2346,146 @@ describe('useAgentConversationStore', () => { expect(reconciledParts(store)).toEqual(storedParts()) }) + const verdictWorkflowId = 'f2b4c6d8-1a3e-4b5c-9d7e-0f1a2b3c4d5e' + const verdictTurn = { threadId: 'th', messageId: SNAPSHOT_ROW_ID } + function settleBySocket(store: ConversationStore) { + store.ingest(done(SNAPSHOT_ROW_ID)) + store.reconcileSettledTurn(verdictTurn, storedParts()) + } + function settleByRecoveryRead(store: ConversationStore) { + store.settleTurn(verdictTurn, storedParts()) + } + function liveCanvasEdit(gate: 'awaiting' | 'unavailable') { + const store = useAgentConversationStore() + store.setCanvasSyncGate(() => gate) + store.setThreadId('th') + store.hydrate(liveSnapshot()) + store.ingest( + toolCall(SNAPSHOT_ROW_ID, 'add_node', 'success', { + workflow_id: verdictWorkflowId, + op_ids: ['op-add'] + }) + ) + return store + } + function settledTool(store: ConversationStore) { + return reconciledParts(store)?.find((part) => part.type === 'tool') + } + + it.for([ + { through: 'socket', settle: settleBySocket }, + { through: 'recovery read', settle: settleByRecoveryRead } + ])( + 'keeps an observed canvas edit confirmed when the $through settles the turn', + ({ settle }) => { + const store = liveCanvasEdit('awaiting') + store.notifyCanvasCaughtUp( + liveCanvasEvidence(verdictWorkflowId, ['op-add']) + ) + + settle(store) + + expect(settledTool(store)).toMatchObject({ + callId: 'call-add_node', + canvasConfirmed: true + }) + } + ) + + it.for([ + { gate: 'unavailable', through: 'socket', settle: settleBySocket }, + { + gate: 'unavailable', + through: 'recovery read', + settle: settleByRecoveryRead + }, + { + gate: 'awaiting', + through: 'recovery read', + settle: settleByRecoveryRead + } + ] as const)( + 'keeps an unobserved canvas edit unconfirmed ($gate) when the $through settles the turn', + ({ gate, settle }) => { + const store = liveCanvasEdit(gate) + + settle(store) + + expect(settledTool(store)).toMatchObject({ + callId: 'call-add_node', + canvasConfirmed: false + }) + } + ) + + it('does not move a live canvas verdict onto a different stored tool', () => { + const store = useAgentConversationStore() + store.setCanvasSyncGate(() => 'unavailable') + store.setThreadId('th') + store.hydrate(liveSnapshot()) + store.ingest(toolCall(SNAPSHOT_ROW_ID, 'add_node', 'success')) + + store.settleTurn({ threadId: 'th', messageId: SNAPSHOT_ROW_ID }, [ + { + type: 'tool', + callId: 'row-fallback-id', + name: 'search_nodes', + state: 'done', + ok: true + } + ]) + + expect( + reconciledParts(store)?.find((part) => part.type === 'tool') + ).not.toHaveProperty('canvasConfirmed') + }) + + it('does not move a live canvas verdict onto a stored tool when the row omits a call', () => { + const workflowId = 'f2b4c6d8-1a3e-4b5c-9d7e-0f1a2b3c4d5e' + function liveAddNode(n: number): AgentChatEvent { + return chat({ + type: 'agent_tool_call', + data: { + tool_call_id: `live-${n}`, + tool_name: 'add_node', + status: 'success', + workflow_id: workflowId, + op_ids: [`op-${n}`], + message_id: SNAPSHOT_ROW_ID, + thread_id: 'th' + } + }) + } + const store = useAgentConversationStore() + store.setCanvasSyncGate(() => 'awaiting') + store.setThreadId('th') + store.hydrate(liveSnapshot()) + store.ingest(liveAddNode(1)) + store.ingest(liveAddNode(2)) + store.ingest(liveAddNode(3)) + store.notifyCanvasCaughtUp( + liveCanvasEvidence(workflowId, ['op-1', 'op-3']) + ) + function stored(callId: string): AssistantMessage['parts'][number] { + return { + type: 'tool', + callId, + name: 'add_node', + state: 'done', + ok: true + } + } + + store.settleTurn({ threadId: 'th', messageId: SNAPSHOT_ROW_ID }, [ + stored('row-1'), + stored('row-3') + ]) + + expect( + reconciledParts(store)?.filter((part) => part.type === 'tool') + ).toEqual([stored('row-1'), stored('row-3')]) + }) + it('waits for a held tool call to clear before announcing the turn', () => { const workflowId = 'f2b4c6d8-1a3e-4b5c-9d7e-0f1a2b3c4d5e' const store = useAgentConversationStore() @@ -2441,7 +2581,11 @@ describe('useAgentConversationStore', () => { store.retireAsk('turn-7:call-1', { status: 'retired' }) - expect(reconciledParts(store)).toEqual(storedParts()) + expect(reconciledParts(store)).toEqual( + storedParts().map((part) => + part.type === 'tool' ? { ...part, canvasConfirmed: false } : part + ) + ) }) it('keeps a re-registered held turn for its own read when an earlier read lands late', () => { diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 0fabc0ca78a..d96ab78a43b 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -22,7 +22,8 @@ import type { import { createAgentEventTransport } from '@/workbench/extensions/agent/services/agent/agentEventTransport' import type { AskUserResolution, - AssistantMessage + AssistantMessage, + ToolPart } from '@/workbench/extensions/agent/services/agent/agentMessageParts' import { askIdOf, @@ -292,41 +293,60 @@ function storedCallIdOf( } } -function preserveCanvasConfirmation( +function preserveCanvasVerdicts( liveParts: AssistantMessage['parts'], - persistedParts: AssistantMessage['parts'], - storedCallId: (callId: string) => string + persistedParts: AssistantMessage['parts'] ): AssistantMessage['parts'] { - const confirmedCallIds = new Set( - liveParts.flatMap((part) => - part.type === 'tool' && part.canvasConfirmed === true - ? [storedCallId(part.callId)] - : [] - ) - ) - return persistedParts.map((part) => - part.type === 'tool' && - part.ok === true && - confirmedCallIds.has(part.callId) - ? { ...part, canvasConfirmed: true } - : part + const offset = liveToolOffset(liveParts, persistedParts) + const storedCallId = storedCallIdOf(liveParts, persistedParts, offset) + const liveTools = toolPartsOf(liveParts) + const persistedCallIds = new Set(toolCallIdsOf(persistedParts)) + const liveCounts = toolNameCounts(liveTools) + const alignedCounts = toolNameCounts( + toolPartsOf(persistedParts).slice(offset, offset + liveTools.length) ) + const liveByStoredCallId = new Map() + for (const part of liveTools) { + if ( + !persistedCallIds.has(part.callId) && + liveCounts.get(part.name) !== alignedCounts.get(part.name) + ) + continue + liveByStoredCallId.set(storedCallId(part.callId), part) + } + return persistedParts.map((part) => { + if (part.type !== 'tool' || part.ok !== true) return part + const live = liveByStoredCallId.get(part.callId) + if (live?.name !== part.name) return part + const canvasConfirmed = liveCanvasVerdict(live) + return canvasConfirmed === undefined ? part : { ...part, canvasConfirmed } + }) +} + +function toolPartsOf(parts: AssistantMessage['parts']): ToolPart[] { + return parts.filter((part): part is ToolPart => part.type === 'tool') +} + +function toolNameCounts(tools: ToolPart[]): Map { + const counts = new Map() + for (const tool of tools) + counts.set(tool.name, (counts.get(tool.name) ?? 0) + 1) + return counts +} + +function liveCanvasVerdict(part: ToolPart): boolean | undefined { + if (part.canvasConfirmed !== undefined) return part.canvasConfirmed + return part.ok === true && part.state === 'streaming' ? false : undefined } function finishSnapshotTurn( message: AssistantMessage, persistedParts: AssistantMessage['parts'] ): void { - const liveParts = message.parts - const offset = liveToolOffset(liveParts, persistedParts) finishWithPersistedParts( message, - preserveCanvasConfirmation( - liveParts, - persistedParts, - storedCallIdOf(liveParts, persistedParts, offset) - ), - offset + persistedParts, + liveToolOffset(message.parts, persistedParts) ) } @@ -354,7 +374,10 @@ function finishWithPersistedParts( (part) => part.type === 'thinking' ) message.parts = interleaveLocalParts( - preserveLiveToolStartTimes(persistedParts, message.parts), + preserveLiveToolStartTimes( + preserveCanvasVerdicts(message.parts, persistedParts), + message.parts + ), anchorLocalParts(message.parts) .filter(({ part }) => !persistedHasThinking || part.type !== 'thinking') .map((anchor) => ({