From 2c93d0dcb5d0e667050a631dc557f6f668245222 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 21 Oct 2026 23:35:49 +0000 Subject: [PATCH 1/4] fix(agent): restore a completed turn's thinking on reload and chat switch (PM-717) (#21600) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Reopening a chat from history lost the thinking. Tool calls stayed. - The transcript now restores `content.thinking` into the Worked trace, each entry at its round. - Needs Comfy-Org/cloud#14903 to persist the field. Until that lands this is a no-op. Tracks [PM-717](https://linear.app/comfyorg/issue/PM-717) (fresh Windows QA report, case Q4: "Thinking and tool call summary survive a chat switch").
Full context for agent readers **Before.** `hydrate()` replaces the conversation with `normalizeAgentTranscript(history)`. An assistant row contributed tool parts from `content.tool_calls` (FE #18282) and its reply text, and nothing else. The thinking the live turn streamed through `agent_thinking` existed only in memory, so after New Chat → reopen, or after a reload, the Worked trace showed tool rows only. That matches the reporter's screenshots. **After.** Comfy-Org/cloud#14903 stores each round's live thinking text on the completed row: ```json "thinking": [{ "text": "...", "tool_call_id": "call-1" }, { "text": "..." }] ``` `appendAssistantContent` restores each entry as a done `ThinkingPart` in front of the tool part whose `callId` matches `tool_call_id`. An entry with no anchor, or one whose call is not in the transcript, goes after the calls and before the reply. Entries are parsed one at a time with `zPersistedThinkingEntry.safeParse`. A malformed or blank entry is skipped, and the row still loads. No rendering change: `WorkSummary`/`ActivityTrace` already render thinking parts. - **Black-box harness, red → green.** `agentToolCallHistoryPersistence.spec.ts` now mocks the history row with `content.thinking` (the cloud#14903 contract). It asserts the trace reads `thinking, Search nodes, thinking, Add node, thinking` after a reload and again after New Chat → reopen from history. - On parent `7178296cf5`: **fails**. Received only `Search nodes, Add node`. - On this head: **passes**. - Unit: one new `agentTranscript.test.ts` case covers a malformed or blank entry being skipped, and a missing-call anchor falling after the calls. - `vitest run src/workbench/extensions/agent/`: 178 files, 4038 passed, 1 expected fail. - `pnpm typecheck`, eslint, oxfmt, pre-commit, pre-push (knip/fallow): clean. - Local setup: cloud-distribution dev server with `VITE_USE_LEGACY_DEFAULT_GRAPH=true`, own backend container (ci image 0.0.28), `--project=cloud --retries=0`, Node 26.10, Chromium. - Screenshots were inspected locally. Before: the expanded Worked trace holds two tool rows and no thinking. After the chat switch: thinking rows sit between and after the tool rows, and the layout is intact.
(cherry picked from commit 5d448355b2cfb92df9e516fea0f3642cae779644) --- .../agentToolCallHistoryPersistence.spec.ts | 30 +++++++-- .../agent/schemas/agentApiSchema.ts | 9 +++ .../services/agent/agentTranscript.test.ts | 64 +++++++++++++++++++ .../agent/services/agent/agentTranscript.ts | 33 +++++++++- .../agent/agentConversationStore.test.ts | 31 +++++++++ .../stores/agent/agentConversationStore.ts | 7 +- 6 files changed, 165 insertions(+), 9 deletions(-) diff --git a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts index 41f1cfcb5ed..167770f7307 100644 --- a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts +++ b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts @@ -20,21 +20,32 @@ 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. +// Release note: on this branch the restored Add node call 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 +109,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..d1d69b31cb5 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.ts @@ -143,6 +143,15 @@ 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 = z.object({ + text: z.string(), + 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..4f01c90c3b1 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts @@ -427,6 +427,70 @@ 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('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' + ) ) } From d68f13f1ae6a73eb96ca2e3a32109f5cac39eddf Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 28 Oct 2026 01:54:31 +0000 Subject: [PATCH 2/4] refactor(agent): derive the persisted thinking entry from the generated schema zPersistedThinkingEntry now extends ingest-types' zAgentThinkingEntry, overriding only tool_call_id to .nullish(), the same way zPersistedToolCallSummary extends zToolCallSummary. A unit case pins the explicit-null tool_call_id that the override exists for. (cherry picked from commit f3ae8d8c1c4c8440cb78ad510ed5a97adbb7bca8) --- .../extensions/agent/schemas/agentApiSchema.ts | 7 +++++-- .../agent/services/agent/agentTranscript.test.ts | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.ts index d1d69b31cb5..f38907a6884 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 @@ -147,8 +148,10 @@ 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 = z.object({ - text: z.string(), +export const zPersistedThinkingEntry = zAgentThinkingEntry.extend({ + // `.nullish()`, matching `zPersistedToolCallSummary` above: a Go pointer + // field serialized without `omitempty` sends an explicit `null`, which the + // generated `.optional()` rejects, dropping that round's thinking. tool_call_id: z.string().nullish() }) diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts index 4f01c90c3b1..f5b5cea29c6 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts @@ -463,6 +463,21 @@ describe('normalizeAgentTranscript', () => { ]) }) + 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 = { From 13fc2e3335987bf560aa3df0e45817dd40bb2fca Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 28 Oct 2026 02:43:03 +0000 Subject: [PATCH 3/4] refactor(agent): pick only text from the generated thinking entry Drop the inline comment (repo rule) and narrow the generated schema to the field the reader uses, so a later regen that adds a required field does not drop historical thinking rows on reload. (cherry picked from commit 498373f635) --- src/workbench/extensions/agent/schemas/agentApiSchema.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.ts index f38907a6884..5b415f920eb 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.ts @@ -148,12 +148,9 @@ 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.extend({ - // `.nullish()`, matching `zPersistedToolCallSummary` above: a Go pointer - // field serialized without `omitempty` sends an explicit `null`, which the - // generated `.optional()` rejects, dropping that round's thinking. - tool_call_id: z.string().nullish() -}) +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 From 6a14b9c330522b5a841390da329cf467925629b6 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 28 Oct 2026 02:45:10 +0000 Subject: [PATCH 4/4] test(agent): drop the release-note comment from the backported spec Repo rule: no added non-JSDoc comments. The branch-specific trace rationale is in the PR description. --- .../tests/agent/agentToolCallHistoryPersistence.spec.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts index 167770f7307..17f085ef1e5 100644 --- a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts +++ b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts @@ -20,8 +20,6 @@ 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: on this branch the restored Add node call 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.'