Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
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.askComfyAgent,
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()
}
)
32 changes: 32 additions & 0 deletions src/workbench/extensions/agent/schemas/agentApiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ export function createAgentEventTransport(
let openText: TextPart | null = null
let openThinking: ThinkingPart | null = null
let openThinkingStartedAt = 0
const tools = new Map<string, ToolPart>()
// 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<string, ToolPart>(
message.parts
.filter((part): part is ToolPart => part.type === 'tool')
.map((part) => [part.callId, part])
)
let settled = false
let lastTabTargetKey: string | undefined

Expand Down
Loading
Loading