From 4b506c2b347e65c107f80bb5e3080c9ec336cdc4 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Tue, 22 Sep 2026 22:39:24 -0700 Subject: [PATCH 1/2] feat: restore tool-call work summary on reload and chat switch (#18282) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Before Reloading the page or switching chats mid-thread dropped a completed turn's tool-call trace: the collapsible "Worked for Xs" work summary (with the tool names/durations inside it) that shows up live while the agent is running would not reappear once the transcript was rehydrated from history. Only the final reply text survived a refresh. ## After `GET /agent/threads/:id/messages` now returns each assistant message's `tool_calls`. This PR reads that field and folds it into the same `ToolPart` entries the live WebSocket `agent_tool_call` handler already builds, so a reloaded or chat-switched transcript renders through the exact same `WorkSummary`/`ActivityTrace` components a live turn uses — no parallel UI. The collapsible starts closed and expands on click identically for a historical trace as for a live one, since that behavior is owned by the (stateless) `CollapsibleRoot` the component already used. ### What changed - `agentApiSchema.ts`: adds `zPersistedToolCallSummary`, a validated FE-side schema for one `content.tool_calls` entry (`id`, `tool_name`, `status`, `duration_ms`), following the same `.extend()`/local-schema pattern this file already uses for `pending_ask`. `status` is deliberately `z.string()` rather than a closed enum, since an unrecognized value must still surface as a failed `ToolPart` rather than be dropped by schema validation. - `agentTranscript.ts`: `normalizeAgentTranscript()` now parses each assistant row's `content.tool_calls` through that schema into `ToolPart[]` and appends them to that turn's `AssistantMessage.parts`, ahead of the row's reply text — mirroring how the live event transport appends tool/text parts into the same array. A restored `pending`/`running` call clamps to a terminal `done`/`ok:false` state unless its row is the one actively backed by a live transport (the run_approval mid-ask case); `ok`/`success` map to success, everything else terminal maps to failure; duplicate `callId`s within one row are deduped; malformed `duration_ms` is rejected without dropping the whole entry. - `agentEventTransport.ts`: `createAgentEventTransport()` now seeds its `tool_call_id` map from any `ToolPart`s already on the message it's given, so a live frame for an already-restored call updates it in place instead of duplicating it. - No changes were needed in `agentConversationStore.ts`: `hydrate()` already assigns `transcript.messages` (which now carries the tool parts) straight into the store's `messages`, the same field the live turn path writes to and the same field the rendering components read. - Unit tests added to `agentTranscript.test.ts` (parsing, ordering, multiple calls, in-flight status, malformed/missing `tool_calls`, restore-without-transport clamping, broadened terminal vocabulary, duration guard, dedup) and `agentConversationStore.test.ts` (hydrate wiring into `store.messages`, and a chat-switch A→B→A test confirming a tool-call summary doesn't bleed across chats). - One Playwright e2e test added (`browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts`): sends a message, mocks the history GET to return a completed turn with two tool calls (using both the `ok` and `success` terminal literals), reloads, and asserts the same work-summary button appears with the exact expected duration label, stays collapsed by default, and expands to show both tool names. ### Known limitations The restored "Worked for Xs" headline is computed client-side from `totalDurationMs(parts)`, but only tool-call durations are restored — thinking-span durations are not persisted/parsed in this PR. A turn that spent significant time thinking can therefore show a different total live vs. after a reload (or across two tabs on the same conversation, one reloaded and one not). Persisting/serving the turn's worked-duration (or its thinking spans) so live and restored render from the same source is tracked as a follow-up rather than fixed here. ### Compatibility This is safe to merge independently of the backend change: when a response has no `tool_calls` key (today's shape, or a user message), parsing simply returns `undefined` and nothing is appended — covered by an explicit unit test. The full frontend behavior (tool history actually showing up after reload) only takes effect once the corresponding cloud backend change, [Comfy-Org/cloud#9960](https://github.com/Comfy-Org/cloud/pull/9960), lands. That PR's shared `persist.ToolCallWireStatus` translation emits `success`/`error` as the two terminal wire values for both the live and reload paths, and excludes `pending`/`running` rows from restored history entirely; this PR's parsing handles that vocabulary (plus the legacy `ok`/`error` pair and any other unrecognized terminal string, defensively) either way. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01RuB3pFMNyS3AE7JYNFNesb --- _Generated by [Claude Code](https://claude.ai/code/session_01RuB3pFMNyS3AE7JYNFNesb)_ --------- Co-authored-by: Christian Byrne --- .../agentToolCallHistoryPersistence.spec.ts | 137 ++++++++ .../agent/schemas/agentApiSchema.ts | 32 ++ .../agent/agentEventTransport.test.ts | 73 ++++ .../services/agent/agentEventTransport.ts | 10 +- .../services/agent/agentTranscript.test.ts | 322 ++++++++++++++++++ .../agent/services/agent/agentTranscript.ts | 300 +++++++++++++--- .../agent/agentConversationStore.test.ts | 49 +++ 7 files changed, 883 insertions(+), 40 deletions(-) create mode 100644 browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts diff --git a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts new file mode 100644 index 00000000000..fd85864eb10 --- /dev/null +++ b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts @@ -0,0 +1,137 @@ +import { expect } from '@playwright/test' + +import type { AgentMessage } from '@comfyorg/ingest-types' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } + +import { promptHistoryTest as test } from '@e2e/fixtures/agentPromptHistoryFixture' +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' + +// FE-1305: a completed turn's tool calls (the work-summary trace shown live, +// via ToolPart in agent_tool_call WebSocket events) used to vanish on +// reload/chat-switch because getMessages's response was never read back into +// that same trace. The real backend now serializes each assistant row's +// `content.tool_calls` ([]persist.ToolCallSummary) verbatim. +// normalizeAgentTranscript (agentTranscript.ts) reads that field and folds it +// into the same ToolPart parts the live agent_tool_call handler builds, so +// agentConversationStore.hydrate() restores the identical WorkSummary UI. +// 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. +test.describe.configure({ timeout: 120_000 }) +test.use({ connectWebSocketToServer: false }) + +test( + 'keeps a completed turn work summary after a browser refresh', + { tag: ['@cloud', '@ui'] }, + async ({ page, promptHistory, workflowSelection }) => { + await page + .getByRole('button', { name: enMessages.agent.entryButton, exact: true }) + .click() + await page + .getByRole('button', { + name: enMessages.sideToolbar.newBlankWorkflow, + exact: true + }) + .click() + const panel = page.locator('#agent-panel-root') + await expect(panel).toBeVisible() + // The composer gates Send behind an explicit workflow target + // (agent.selectWorkflowForAgent); without this the click below never + // reaches promptHistory. + await panel + .getByRole('button', { name: enMessages.agent.switchWorkflow }) + .click() + await page + .getByRole('menuitemradio', { name: 'Unsaved Workflow', exact: true }) + .click() + await expect.poll(() => workflowSelection.savedPaths.length).toBe(1) + workflowSelection.finishSave(true) + + const composer = panel.getByRole('textbox', { name: /^Describe ideas/ }) + await composer.pressSequentially('find a node for me') + await panel + .getByRole('button', { name: enMessages.agent.send, exact: true }) + .click() + await expect.poll(() => promptHistory.requests.length).toBe(1) + + // The real ingest API returns the message row's `content` map verbatim, + // including `tool_calls` once a turn's calls resolve server-side + // (services/agent/server/agent_handler.go's getMessages sets + // messageResponse.Content = m.Content directly). Stand in for that GET + // response here. + await page.route('**/api/agent/threads/*/messages', (route) => { + if (route.request().method() !== 'GET') return route.fallback() + const request = promptHistory.requests.at(0) + if (!request) return route.fallback() + const threadId = new URL(route.request().url()).pathname + .split('/') + .at(-2)! + const turnId = 'e2e-tool-call-turn' + const messages: AgentMessage[] = [ + { + id: 'e2e-tool-call-user', + thread_id: threadId, + turn_id: turnId, + seq: 1, + role: 'user', + status: 'complete', + workflow_id: request.workflow_id, + content: { text: request.content } + }, + { + id: turnId, + thread_id: threadId, + turn_id: turnId, + seq: 2, + role: 'assistant', + status: 'complete', + content: { + text: 'Found it.', + tool_calls: [ + { + id: 'call-1', + tool_name: 'search_nodes', + status: 'ok', + duration_ms: 420 + }, + { + // Deliberately 'success' (not 'ok', like call-1) to cover both terminal vocabularies. + id: 'call-2', + tool_name: 'add_node', + status: 'success', + duration_ms: 180 + } + ] + } + } + ] + return route.fulfill(jsonRoute(messages)) + }) + + // Settle the (WS-less) turn so the reload below is not racing a + // permanently-streaming assistant message. + await panel + .getByRole('button', { name: enMessages.agent.stop, exact: true }) + .click() + + await page.reload() + await expect( + page.getByTestId('integrated-tab-bar-actions') + ).toHaveAttribute('data-agent-gate-settled', 'true', { timeout: 30_000 }) + const reopenedPanel = page.locator('#agent-panel-root') + await expect(reopenedPanel).toBeVisible({ timeout: 30_000 }) + + const summary = reopenedPanel.getByRole('button', { + name: enMessages.agent.worked, + exact: true + }) + await expect(summary).toBeVisible({ timeout: 10_000 }) + await expect(summary).toHaveAttribute('aria-expanded', 'false') + await expect(reopenedPanel.getByText('Search nodes')).toBeHidden() + + await summary.click() + await expect(reopenedPanel.getByText('Search nodes')).toBeVisible() + await expect(reopenedPanel.getByText('Add node')).toBeVisible() + } +) diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.ts index b414dcd438e..01730507425 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.ts @@ -94,6 +94,38 @@ export const zAgentRunMode = zGeneratedAgentRunMode.superRefine( ) export type AgentRunModeValue = AgentRunModePreference['mode'] +/** + * One entry of a persisted assistant row's `content.tool_calls` (see + * `agentTranscript.ts`'s `parseToolCallEntry`), the reload-path counterpart + * to the live WebSocket's `zAgentToolCallData` above. `status` is + * deliberately `z.string()` rather than a closed enum: an unrecognized value + * must still surface as a failed `ToolPart` (`toolCallOk` treats anything + * other than `pending`/`running`/`ok`/`success` as failure), so schema + * validation should reject a malformed *entry* (missing `id`/`tool_name`), + * not an unfamiliar *status* string or a bad `duration_ms` — `duration_ms` is + * `z.unknown().optional()` so a NaN/Infinity/negative value there doesn't + * sink the whole entry; `parseToolCallEntry` narrows it separately and just + * omits it. `status` is likewise `.optional()`: an entry that omits it + * entirely must still survive validation (`toolCallPartState`/`toolCallOk` + * already treat `undefined` as terminal-and-failed, matching the old + * parser's behavior for a status-less call). + */ +export const zPersistedToolCallSummary = z + .object({ + id: z.string(), + // The provider tool-use id a LIVE `agent_tool_call` frame carries as + // `tool_call_id` (see `zAgentToolCallData` above). `parseToolCallEntry` + // prefers this over `id` when building `callId` so a restored `ToolPart` + // is keyed the same way a live frame for the same call will be, and can + // be updated in place rather than rendered as an unmatched duplicate. + // Optional: rows recorded before `tool_call_id` existed have none. + tool_call_id: z.string().optional(), + tool_name: z.string(), + status: z.string().optional(), + duration_ms: z.unknown().optional() + }) + .passthrough() + export const zAgentMessage = zGeneratedAgentMessage .extend({ pending_ask: zAgentPendingAsk.optional() diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts index 48fa1249225..534ddc261ac 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts @@ -1,6 +1,8 @@ import { describe, expect, it, vi } from 'vitest' +import type { AgentMessages } from '../../schemas/agentApiSchema' import { toTurnId, zAgentWsEvent } from '../../schemas/agentApiSchema' +import { normalizeAgentTranscript } from './agentTranscript' import type { AgentChatEvent } from './agentEventTransport' import { createAgentEventTransport } from './agentEventTransport' @@ -371,6 +373,77 @@ describe('agentEventTransport text and tool parts', () => { } ]) }) + + it('updates an already-restored tool part in place instead of duplicating it', () => { + const restored: ToolPart = { + type: 'tool', + callId: 'call-1', + name: 'run', + state: 'streaming' + } + const message = createAssistantMessage(T) + message.parts = [restored] + const emit = vi.fn<(m: AssistantMessage) => void>() + const transport = createAgentEventTransport(message, emit) + + transport.ingest(toolCall('run', 'success', 'call-1')) + + expect(toolParts(message)).toEqual([ + { + type: 'tool', + callId: 'call-1', + name: 'run', + state: 'done', + ok: true, + durationMs: undefined + } + ]) + }) + + it('updates a persisted-then-restored tool part in place when a live frame carries a different id than the audit row', () => { + // The audit row's own id and the provider tool_call_id it was recorded + // under are deliberately DIFFERENT here (unlike 'call-1' reused for + // both elsewhere in this file) so this test actually exercises the + // mismatch: a live agent_tool_call frame keys on tool_call_id, not the + // audit row's id, so callId must be derived from tool_call_id or the + // restored part and the live update never match. + const persistedRow: AgentMessages[number] = { + id: 'row-1', + thread_id: 'thread-1', + seq: 1, + role: 'assistant', + status: 'complete', + turn_id: 't1', + content: { + text: '', + tool_calls: [ + { + id: 'audit-row-uuid-1', + tool_call_id: 'call-1', + tool_name: 'run', + status: 'running' + } + ] + } + } + const { messages } = normalizeAgentTranscript([persistedRow]) + const message = messages[0] + const emit = vi.fn<(m: AssistantMessage) => void>() + const transport = createAgentEventTransport(message, emit) + + transport.ingest(toolCall('run', 'success', 'call-1')) + + expect(toolParts(message)).toEqual([ + { + type: 'tool', + callId: 'call-1', + name: 'run', + state: 'done', + ok: true, + durationMs: undefined + } + ]) + }) }) describe('agentEventTransport run approval', () => { diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts index 55eea9c5fd1..65347002c8f 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts @@ -35,7 +35,15 @@ export function createAgentEventTransport( let openText: TextPart | null = null let openThinking: ThinkingPart | null = null let openThinkingStartedAt = 0 - const tools = new Map() + // Seeded from any tool parts already on `message` (a hydrated pending row + // restores its tool_calls before this transport exists) so the next live + // `agent_tool_call` for an already-restored call updates that part in + // place instead of pushing a duplicate. + const tools = new Map( + message.parts + .filter((part): part is ToolPart => part.type === 'tool') + .map((part) => [part.callId, part]) + ) let settled = false let lastTabTargetKey: string | undefined diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts index bbc00cb5dfd..649e263652e 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts @@ -203,6 +203,328 @@ describe('normalizeAgentTranscript', () => { } ) + it('restores persisted tool calls as ToolPart entries ahead of the reply text', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [ + { + id: 'call-1', + tool_name: 'search_nodes', + status: 'ok', + duration_ms: 420 + } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toEqual([ + { + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: true, + durationMs: 420 + }, + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + + it('prefers tool_call_id over id when deriving callId, matching what live frames key on', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [ + { + id: 'audit-row-uuid-1', + tool_call_id: 'provider-call-1', + tool_name: 'search_nodes', + status: 'ok' + } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toEqual([ + { + type: 'tool', + callId: 'provider-call-1', + name: 'search_nodes', + state: 'done', + ok: true + }, + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + + it('falls back to id for callId when a row was recorded before tool_call_id existed', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [ + { id: 'audit-row-uuid-1', tool_name: 'search_nodes', status: 'ok' } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toContainEqual( + expect.objectContaining({ callId: 'audit-row-uuid-1' }) + ) + }) + + it('omits tool parts entirely when a message carries no tool_calls', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toEqual([ + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + + it('restores multiple tool calls per message in order', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [ + { id: 'call-1', tool_name: 'search_nodes', status: 'ok' }, + { id: 'call-2', tool_name: 'add_node', status: 'error' } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toEqual([ + { + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: true + }, + { + type: 'tool', + callId: 'call-2', + name: 'add_node', + state: 'done', + ok: false + }, + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + + it('clamps a restored pending/running tool call to done+ok:false with no live transport', () => { + const message = row(1, 'assistant', 'turn-a', '', 'row-1') + message.content = { + tool_calls: [ + { id: 'call-1', tool_name: 'search_nodes', status: 'running' } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toEqual([ + { + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: false + } + ]) + }) + + it('keeps a pending/running tool call streaming when its row is the live run_approval ask', () => { + const message = row(1, 'assistant', 'turn-a', '', 'row-1') + message.status = 'streaming' + message.content = { + tool_calls: [ + { id: 'call-1', tool_name: 'search_nodes', status: 'running' } + ] + } + message.pending_ask = { + message_id: 'row-1', + ask_id: 'ask-1', + kind: 'run_approval', + prompt: 'Run it?', + options: [], + min_selections: 1, + max_selections: 1, + allow_other: false + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toContainEqual({ + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'streaming' + }) + expect(transcript.pending?.messageId).toBe('row-1') + }) + + it.for(['success', 'failed', 'cancelled', 'timeout', 'unrecognized'])( + 'maps terminal tool-call status %s through the broadened vocabulary', + (status) => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [{ id: 'call-1', tool_name: 'search_nodes', status }] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toContainEqual({ + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: status === 'success' + }) + } + ) + + it.for([NaN, Infinity, -Infinity, -1])( + 'rejects a non-finite or negative durationMs (%s)', + (durationMs) => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [ + { + id: 'call-1', + tool_name: 'search_nodes', + status: 'ok', + duration_ms: durationMs + } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toContainEqual({ + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: true + }) + } + ) + + it('dedupes a repeated callId within one row, keeping the last entry at the first position', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [ + { id: 'call-1', tool_name: 'search_nodes', status: 'running' }, + { id: 'call-2', tool_name: 'add_node', status: 'ok' }, + { + id: 'call-1', + tool_name: 'search_nodes', + status: 'ok', + duration_ms: 420 + } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toEqual([ + { + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: true, + durationMs: 420 + }, + { + type: 'tool', + callId: 'call-2', + name: 'add_node', + state: 'done', + ok: true + }, + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + + it.for([ + { tool_calls: 'not-an-array' }, + { tool_calls: [null, 17, 'a-string'] }, + { tool_calls: [{ tool_name: 'no_id', status: 'ok' }] }, + { tool_calls: [{ id: 'call-1', status: 'ok' }] }, + { tool_calls: [] } + ])('ignores malformed tool_calls metadata: $tool_calls', (content) => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { text: 'Done', ...content } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toEqual([ + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + + it('dedupes a repeated callId across two assistant rows of the same turn', () => { + const first = row(1, 'assistant', 'turn-a', '', 'row-1') + first.content = { + tool_calls: [{ id: 'call-1', tool_name: 'search_nodes', status: 'ok' }] + } + const second = row(2, 'assistant', 'turn-a', 'Done', 'row-2') + second.content = { + text: 'Done', + tool_calls: [ + { + id: 'call-1', + tool_name: 'search_nodes', + status: 'error', + duration_ms: 900 + } + ] + } + + const transcript = normalizeAgentTranscript([first, second]) + + expect(transcript.messages[0].parts).toEqual([ + { + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: true + }, + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + + it('renders a persisted tool call with no status field as a failure, not a dropped entry', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [{ id: 'call-1', tool_name: 'search_nodes' }] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toEqual([ + { + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: false + }, + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + it('restores available and unavailable references without changing the latest workflow target', () => { const first = row(1, 'user', 'turn-a', 'Compare these', 'row-1') first.workflow_id = 'wf-target-a' diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.ts index c9aaa02d059..dc2e266fec8 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.ts @@ -1,7 +1,8 @@ import type { AgentMessages, TurnId } from '../../schemas/agentApiSchema' +import { zPersistedToolCallSummary } from '../../schemas/agentApiSchema' import type { WorkflowReference } from '../../types/workflowReference' import { parseWorkflowReferences } from '../../utils/workflowReferenceText' -import type { AssistantMessage } from './agentMessageParts' +import type { AssistantMessage, ToolPart } from './agentMessageParts' import { createAssistantMessage } from './agentMessageParts' /** @@ -100,6 +101,255 @@ function parseUserWorkflowReferences( : undefined } +/** + * A persisted tool-call status is `pending`/`running` while it was still in + * flight when the turn ended, and some terminal string otherwise (`ok`, + * `success`, `error`, `failed`, `cancelled`, `timeout`, ...) — the exact + * terminal vocabulary is not yet settled between the backend's persisted and + * live wire formats, so anything other than `pending`/`running` is treated as + * terminal here. + * + * A restored (non-live) row has no transport left to ever settle its tool + * parts, so a `pending`/`running` status there would otherwise spin forever; + * only `isLive` (the row is the one actively backed by a live transport — + * the run_approval mid-ask case) keeps it in `streaming` state. + */ +function toolCallPartState( + status: unknown, + isLive: boolean +): ToolPart['state'] { + const inProgress = status === 'pending' || status === 'running' + return inProgress && isLive ? 'streaming' : 'done' +} + +/** + * `undefined` while the call is still genuinely in progress (matching the + * live path, which omits `ok` until a terminal status arrives); once the + * part is in a `done` state, only `ok`/`success` counts as success — every + * other terminal string, including a restored `pending`/`running` call that + * had no live transport to finish it, reads as failure rather than being + * rendered as if it succeeded. + */ +function toolCallOk( + status: unknown, + state: ToolPart['state'] +): boolean | undefined { + if (state === 'streaming') return undefined + return status === 'ok' || status === 'success' +} + +/** + * Validates one `content.tool_calls` entry against `zPersistedToolCallSummary` + * and maps it onto the same `ToolPart` the live WebSocket path builds from + * `agent_tool_call` events, so a reloaded transcript renders through the + * identical work-summary UI as a live turn. `undefined` for anything that + * doesn't validate (missing `id`/`tool_name`, wrong types, ...). + */ +function parseToolCallEntry( + entry: unknown, + isLive: boolean +): ToolPart | undefined { + const parsed = zPersistedToolCallSummary.safeParse(entry) + if (!parsed.success) return undefined + const { + id, + tool_call_id: toolCallId, + tool_name: toolName, + status, + duration_ms: rawDuration + } = parsed.data + const state = toolCallPartState(status, isLive) + const ok = toolCallOk(status, state) + const durationMs = + typeof rawDuration === 'number' && + Number.isFinite(rawDuration) && + rawDuration >= 0 + ? rawDuration + : undefined + return { + type: 'tool', + // A live `agent_tool_call` frame keys its update on `tool_call_id`, not + // this row's own `id` — prefer it so a restored part matches a live + // frame that arrives for it later. Falls back to `id` only for rows + // recorded before `tool_call_id` existed. + callId: toolCallId ?? id, + name: toolName, + state, + ...(ok !== undefined ? { ok } : {}), + ...(durationMs !== undefined ? { durationMs } : {}) + } +} + +/** + * A persisted assistant row's `tool_calls` entries are parsed through + * `parseToolCallEntry`. A repeated `id` within the list is deduped, keeping + * the last entry's data at the first entry's position, matching how the + * live path updates a part in place rather than appending. + */ +function parseToolCalls( + content: Record | undefined, + isLive: boolean +): ToolPart[] | undefined { + const raw = content?.tool_calls + if (!Array.isArray(raw)) return undefined + const parts = new Map() + for (const entry of raw as unknown[]) { + const part = parseToolCallEntry(entry, isLive) + if (part) parts.set(part.callId, part) + } + return parts.size > 0 ? [...parts.values()] : undefined +} + +/** + * Appends a persisted assistant row's tool-call and text parts onto its + * running message. `isLive` is true only when this row is the one that will + * be handed a live `AgentEventTransport` (the run_approval mid-ask case), so + * its still-in-flight tool parts may legitimately stay `streaming`. + * + * `message.parts` is shared across every assistant row of one turn (via + * `assistants.get(turnId)` in `recordAssistantRow`), but `parseToolCalls` + * only dedupes within one row's own list — two rows of the same turn that + * each carry the same `callId` would otherwise still produce two separate + * `ToolPart`s. Dedupe is hoisted here to the message level — a parsed part + * whose `callId` already appears in `message.parts` is dropped — matching + * the live path's turn-wide keying. + */ +function appendAssistantContent( + message: AssistantMessage, + row: AgentMessages[number], + text: string, + isLive: boolean +): void { + const toolCalls = parseToolCalls(row.content, isLive) + if (toolCalls) { + const existingCallIds = new Set( + message.parts.flatMap((part) => + part.type === 'tool' ? [part.callId] : [] + ) + ) + const newToolCalls = toolCalls.filter( + (part) => !existingCallIds.has(part.callId) + ) + message.parts = [...message.parts, ...newToolCalls] + } + if (text) + message.parts = [...message.parts, { type: 'text', text, state: 'done' }] +} + +/** + * A row's `pending_ask` carries a `run_approval` context only while that row + * is still mid-ask; this reads it into the flat shape the `runApproval` part + * renders, or `undefined` once the ask is resolved or absent. + */ +function pendingRunApproval( + row: AgentMessages[number] +): { askId: string; workflowId?: string; workflowName?: string } | undefined { + const ask = row.pending_ask + if (ask?.kind !== 'run_approval') return undefined + return { + askId: ask.ask_id, + workflowId: ask.context?.workflow_id || undefined, + workflowName: ask.context?.workflow_name || undefined + } +} + +/** + * Applies one persisted assistant row onto its running message: appends any + * parsed tool-call parts and text part, then, when the row is mid-ask, + * attaches a `runApproval` part and marks the message still-streaming. + * Returns the `pending` entry to record when the row is mid-ask, or + * `undefined` otherwise. + */ +function applyAssistantRow( + row: AgentMessages[number], + message: AssistantMessage, + text: string +): NormalizedAgentTranscript['pending'] { + message.streaming = false + const runApproval = + row.status === 'streaming' ? pendingRunApproval(row) : undefined + appendAssistantContent(message, row, text, runApproval !== undefined) + + if (!runApproval) return undefined + + message.parts.push({ type: 'runApproval', ...runApproval }) + message.streaming = true + return { messageId: row.id as TurnId, message } +} + +/** + * A persisted user row's text/attachment/workflow-reference/workflow-target + * fields, resolved from `row.content` and ready for the caller to record + * onto its per-turn maps and `latestWorkflowId`. + */ +interface UserRowUpdate { + text: string + attachments?: UserAttachment[] + workflowReferences?: WorkflowReference[] + workflowId?: string +} + +function applyUserRow(row: AgentMessages[number], text: string): UserRowUpdate { + const referenceUpdate = parseUserWorkflowReferences( + text, + row.content?.workflow_references + ) + return { + text: referenceUpdate?.text ?? text, + attachments: parseUserAttachments(row.content), + workflowReferences: referenceUpdate?.references, + workflowId: row.workflow_id || undefined + } +} + +/** Adds `turnId` to the stable turn ordering the first time it is seen. */ +function recordTurnOrder( + turnId: TurnId, + seenTurns: Set, + turnOrder: TurnId[] +): void { + if (seenTurns.has(turnId)) return + seenTurns.add(turnId) + turnOrder.push(turnId) +} + +/** + * Resolves a user row's update and records it onto the per-turn maps. + * Returns the row's `workflow_id`, if any, for the caller to fold into + * `latestWorkflowId`. + */ +function recordUserRow( + row: AgentMessages[number], + turnId: TurnId, + text: string, + userTexts: Map, + userAttachments: Map, + userWorkflowReferences: Map +): string | undefined { + const update = applyUserRow(row, text) + userTexts.set(turnId, update.text) + if (update.attachments) userAttachments.set(turnId, update.attachments) + if (update.workflowReferences) + userWorkflowReferences.set(turnId, update.workflowReferences) + return update.workflowId +} + +/** + * Applies an assistant row onto its turn's running message and records it + * onto `assistants`. Returns the row's `pending` entry, if it is mid-ask. + */ +function recordAssistantRow( + row: AgentMessages[number], + turnId: TurnId, + text: string, + assistants: Map +): NormalizedAgentTranscript['pending'] { + const message = assistants.get(turnId) ?? createAssistantMessage(turnId) + const rowPending = applyAssistantRow(row, message, text) + assistants.set(turnId, message) + return rowPending +} + export function normalizeAgentTranscript( history: AgentMessages ): NormalizedAgentTranscript { @@ -116,50 +366,22 @@ export function normalizeAgentTranscript( for (const row of [...history].sort((a, b) => a.seq - b.seq)) { const turnId = row.turn_id as TurnId rowIds.add(row.id) - if (!seenTurns.has(turnId)) { - seenTurns.add(turnId) - turnOrder.push(turnId) - } + recordTurnOrder(turnId, seenTurns, turnOrder) const text = typeof row.content?.text === 'string' ? row.content.text : '' if (row.role === 'user') { - userTexts.set(turnId, text) - const attachments = parseUserAttachments(row.content) - if (attachments) userAttachments.set(turnId, attachments) - if (row.workflow_id) latestWorkflowId = row.workflow_id - const referenceUpdate = parseUserWorkflowReferences( + const workflowId = recordUserRow( + row, + turnId, text, - row.content?.workflow_references + userTexts, + userAttachments, + userWorkflowReferences ) - if (referenceUpdate) { - userTexts.set(turnId, referenceUpdate.text) - userWorkflowReferences.set(turnId, referenceUpdate.references) - } + if (workflowId) latestWorkflowId = workflowId } if (row.role === 'assistant') { - const message = assistants.get(turnId) ?? createAssistantMessage(turnId) - message.streaming = false - if (text) - message.parts = [ - ...message.parts, - { type: 'text', text, state: 'done' } - ] - if ( - row.status === 'streaming' && - row.pending_ask?.kind === 'run_approval' - ) { - message.parts.push({ - type: 'runApproval', - askId: row.pending_ask.ask_id, - workflowId: row.pending_ask.context?.workflow_id || undefined, - workflowName: row.pending_ask.context?.workflow_name || undefined - }) - message.streaming = true - pending = { - messageId: row.id as TurnId, - message - } - } - assistants.set(turnId, message) + const rowPending = recordAssistantRow(row, turnId, text, assistants) + if (rowPending) pending = rowPending } } diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index 895d109c902..ddcbc37c58d 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -493,6 +493,55 @@ describe('useAgentConversationStore', () => { }) }) + it('hydrates persisted tool calls into the same parts array the live work-summary UI reads', () => { + const assistant = historyRow(2, 'assistant', 'turn-a', 'Done') + assistant.content = { + text: 'Done', + tool_calls: [{ id: 'call-1', tool_name: 'search_nodes', status: 'ok' }] + } + const store = useAgentConversationStore() + + store.hydrate([historyRow(1, 'user', 'turn-a', 'Find a node'), assistant]) + + expect(store.messages[0].parts).toContainEqual({ + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: true + }) + }) + + it('does not bleed a tool-call summary onto a different chat, and restores it when switching back', () => { + const store = useAgentConversationStore() + const threadAAssistant = historyRow(2, 'assistant', 'turn-a', 'Done A') + threadAAssistant.content = { + text: 'Done A', + tool_calls: [{ id: 'call-a', tool_name: 'search_nodes', status: 'ok' }] + } + const threadA = [ + historyRow(1, 'user', 'turn-a', 'Find a node'), + threadAAssistant + ] + const threadB = [ + historyRow(1, 'user', 'turn-b', 'Just chat'), + historyRow(2, 'assistant', 'turn-b', 'Done B') + ] + const hasToolPart = () => + store.messages.some((message) => + message.parts.some((part) => part.type === 'tool') + ) + + store.hydrate(threadA) + expect(hasToolPart()).toBe(true) + + store.hydrate(threadB) + expect(hasToolPart()).toBe(false) + + store.hydrate(threadA) + expect(hasToolPart()).toBe(true) + }) + it('keeps hydrated turn identity stable when persisted row ids change', () => { const store = useAgentConversationStore() const firstRows = [ From 1fe42850172e2b7be41b5f6f762d32688396ed7a Mon Sep 17 00:00:00 2001 From: bymyself Date: Tue, 22 Sep 2026 23:42:19 -0700 Subject: [PATCH 2/2] fix: use 1.54 agent entry label --- .../tests/agent/agentToolCallHistoryPersistence.spec.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts index fd85864eb10..4e578736a42 100644 --- a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts +++ b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts @@ -26,7 +26,10 @@ test( { tag: ['@cloud', '@ui'] }, async ({ page, promptHistory, workflowSelection }) => { await page - .getByRole('button', { name: enMessages.agent.entryButton, exact: true }) + .getByRole('button', { + name: enMessages.agent.askComfyAgent, + exact: true + }) .click() await page .getByRole('button', {