diff --git a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts index 41f1cfcb5ed..17f085ef1e5 100644 --- a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts +++ b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts @@ -20,21 +20,30 @@ import { AGENT_COMPOSER_LABEL } from '@e2e/fixtures/utils/agentComposerLabel' // This test mimics that verbatim content pass-through (the shared fixture's // own GET mock does not carry tool_calls) and asserts the summary survives a // refresh. -// Release note: upstream's TRACE interleaves thinking rows, which this -// branch's trace does not carry. The row this fix changes is the same one: -// the restored Add node call now reads as an unconfirmed canvas write rather -// than a plain tool label. +const THINKING_ROUND_1 = + 'The user wants a loader node, so I will search for one.' +const THINKING_ROUND_2 = 'Found a match; adding it to the graph.' +const THINKING_FINAL = 'Both steps succeeded, so I can answer.' const TRACE = [ + THINKING_ROUND_1, 'Search nodes', - enMessages.agent.toolNotConfirmedOnCanvas.replace('{tool}', 'Add node') + THINKING_ROUND_2, + enMessages.agent.toolNotConfirmedOnCanvas.replace('{tool}', 'Add node'), + THINKING_FINAL ] test.describe.configure({ timeout: 120_000 }) test.use({ connectWebSocketToServer: false }) test( - 'keeps a completed turn work summary after reload and chat switching', - { tag: ['@cloud', '@ui'] }, + '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' + } + }, async ({ page, promptHistory, workflowSelection }) => { await new AgentPanel(page).open() await page @@ -98,6 +107,11 @@ test( status: 'complete', content: { text: 'Found it.', + thinking: [ + { text: THINKING_ROUND_1, tool_call_id: 'call-1' }, + { text: THINKING_ROUND_2, tool_call_id: 'call-2' }, + { text: THINKING_FINAL } + ], tool_calls: [ { id: 'call-1', diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.ts index f8cd89eee64..5b415f920eb 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.ts @@ -12,6 +12,7 @@ import { zAgentRunMode as zGeneratedAgentRunMode, zAgentThreadListResponse as zGeneratedAgentThreadListResponse, zAgentToolCallData as zGeneratedAgentToolCallData, + zAgentThinkingEntry, zAgentTurnAccepted as zGeneratedAgentTurnAccepted, zToolCallSummary, zWorkflowListResponse @@ -143,6 +144,14 @@ export const zPersistedToolCallSummary = zToolCallSummary.extend({ }) export type PersistedToolCallSummary = z.infer +/** + * One persisted `content.thinking` entry: a model round's thinking text, + * anchored to that round's first tool call when it made one. + */ +export const zPersistedThinkingEntry = zAgentThinkingEntry + .pick({ text: true }) + .extend({ tool_call_id: z.string().nullish() }) + /** * The message-parse boundary's `content.tool_calls` field is deliberately * `z.array(z.unknown())`, not the stricter `zPersistedToolCallSummary` diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts index ae5d2dfd648..f5b5cea29c6 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts @@ -427,6 +427,85 @@ describe('normalizeAgentTranscript', () => { ]) }) + it('skips malformed persisted thinking and keeps an entry whose call is missing after the calls', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + thinking: [ + { text: 42 }, + { text: ' ' }, + { text: 'Looking it up.', tool_call_id: 'call-1' }, + { text: 'The audit row is gone.', tool_call_id: 'call-missing' } + ], + tool_calls: [ + { + id: 'audit-row-uuid-1', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status: 'success' + } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toEqual([ + { type: 'thinking', text: 'Looking it up.', state: 'done' }, + { + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: true + }, + { type: 'thinking', text: 'The audit row is gone.', state: 'done' }, + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + + it('restores persisted thinking whose tool_call_id is an explicit null', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + thinking: [{ text: 'Final round.', tool_call_id: null }] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toEqual([ + { type: 'thinking', text: 'Final round.', state: 'done' }, + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + + it('keeps persisted thinking in order when an earlier entry names a call the transcript lacks', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + thinking: [ + { text: 'Round one.', tool_call_id: 'call-missing' }, + { text: 'Round two.', tool_call_id: 'call-2' }, + { text: 'Final round.' } + ], + tool_calls: [ + { + id: 'audit-row-uuid-2', + tool_call_id: 'call-2', + tool_name: 'add_node', + status: 'success' + } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect( + transcript.messages[0].parts.map((part) => + part.type === 'thinking' ? part.text : part.type + ) + ).toEqual(['Round one.', 'Round two.', 'tool', 'Final round.', 'text']) + }) + it('restores a persisted skill name', () => { const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') message.content = { diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.ts index fd22f14ea1c..7faf1fdaf19 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.ts @@ -1,13 +1,18 @@ import type { AgentMessages, TurnId } from '../../schemas/agentApiSchema' import { toTurnId, + zPersistedThinkingEntry, zPersistedToolCallSummary } from '../../schemas/agentApiSchema' import type { WorkflowReference } from '../../types/workflowReference' import type { SkillReference } from '../../types/skillReference' import { parseSkillReferenceText } from '../../utils/skillReferenceText' import { parseWorkflowReferences } from '../../utils/workflowReferenceText' -import type { AssistantMessage, ToolPart } from './agentMessageParts' +import type { + AssistantMessage, + ThinkingPart, + ToolPart +} from './agentMessageParts' import { createAssistantMessage, isOpenAskPart, @@ -331,6 +336,31 @@ function parseToolCalls( return parsedByCallId.size > 0 ? [...parsedByCallId.values()] : undefined } +function insertThinking( + parts: AssistantMessage['parts'], + content: Record | undefined +): AssistantMessage['parts'] { + const raw = content?.thinking + if (!Array.isArray(raw)) return parts + const next = [...parts] + let unplaced: ThinkingPart[] = [] + for (const entry of raw as unknown[]) { + const parsed = zPersistedThinkingEntry.safeParse(entry) + if (!parsed.success || !parsed.data.text.trim()) continue + unplaced.push({ type: 'thinking', text: parsed.data.text, state: 'done' }) + const anchor = parsed.data.tool_call_id + const index = anchor + ? next.findIndex( + (existing) => existing.type === 'tool' && existing.callId === anchor + ) + : -1 + if (index === -1) continue + next.splice(index, 0, ...unplaced) + unplaced = [] + } + return [...next, ...unplaced] +} + /** A persisted tool-call `status` of `pending`/`running` is still in flight; * anything else (`success`/`error`/`ok`, or an unrecognized string) is * terminal — settled for good, independent of `isLive` clamping. This @@ -408,6 +438,7 @@ function appendAssistantContent( } message.parts = appended.length > 0 ? [...parts, ...appended] : parts } + message.parts = insertThinking(message.parts, row.content) if (text) message.parts = [...message.parts, { type: 'text', text, state: 'done' }] } diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index beefe72d391..5aea3138bfb 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -2143,6 +2143,37 @@ describe('useAgentConversationStore', () => { } ) + it.for([ + { + name: 'persisted thinking replaces the streamed thinking', + persisted: [ + { type: 'thinking', text: 'planning the whole turn', state: 'done' }, + { type: 'text', text: 'done', state: 'done' } + ] satisfies AssistantMessage['parts'], + expected: ['planning the whole turn', 'done'] + }, + { + name: 'a row without thinking keeps the streamed thinking', + persisted: [ + { type: 'text', text: 'done', state: 'done' } + ] satisfies AssistantMessage['parts'], + expected: ['planning', 'done'] + } + ])('settles a recovered turn so $name', ({ persisted, expected }) => { + const store = useAgentConversationStore() + store.setThreadId('th') + store.startTurn(T1) + store.ingest(thinking('t1', 'planning')) + + store.settleTurn({ threadId: 'th', messageId: T1 }, persisted) + + expect( + store.messages[0].parts.map((part) => + part.type === 'thinking' || part.type === 'text' ? part.text : part.type + ) + ).toEqual(expected) + }) + it('splits persisted text around a local tab link when there is no tool', () => { const store = useAgentConversationStore() store.setThreadId('th') diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 84d3ee3f526..fdc9518c8e5 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -217,9 +217,14 @@ function finishWithPersistedParts( } return } + const persistedHasThinking = persistedParts.some( + (part) => part.type === 'thinking' + ) message.parts = interleaveLocalParts( persistedParts, - anchorLocalParts(message.parts) + anchorLocalParts(message.parts).filter( + ({ part }) => !persistedHasThinking || part.type !== 'thinking' + ) ) }