diff --git a/browser_tests/tests/agent/agentHistoryUnresolvedWorkflowReported.spec.ts b/browser_tests/tests/agent/agentHistoryUnresolvedWorkflowReported.spec.ts new file mode 100644 index 00000000000..121faa51895 --- /dev/null +++ b/browser_tests/tests/agent/agentHistoryUnresolvedWorkflowReported.spec.ts @@ -0,0 +1,165 @@ +import { expect } from '@playwright/test' + +import type { + AgentMessage, + AgentThreadListResponse, + WorkflowListResponse +} from '@comfyorg/ingest-types' +import { REPORTED_ERROR_PREFIX } from '@comfyorg/shared-frontend-utils/telemetry' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } +import type { UserDataFullInfo } from '@/platform/remote/comfyui/types' + +import { + agentTest as test, + bootAgentApp +} from '@e2e/fixtures/agentPanelFixture' +import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' +import { collectConsoleErrors } from '@e2e/fixtures/utils/consoleErrorCollector' +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' + +const THREAD_ID = '3b7e2d1c-9a4f-4e6b-8c5d-1f2a3b4c5d6e' +const TURN_ID = '6c5d4e3f-2a1b-4c9d-8e7f-0a1b2c3d4e5f' +const WORKFLOW_ID = '0d9c8b7a-6f5e-4d3c-b2a1-f0e9d8c7b6a5' +const TWIN_WORKFLOW_ID = '1e0d9c8b-7a6f-4e5d-c3b2-a1f0e9d8c7b6' +const WORKFLOW_NAME = 'Image and video' +const THREAD_TITLE = 'Lighthouse clip' +const USER_REQUEST = 'Make a lighthouse image, then a short video of it' +const IMAGE_URL = + 'https://cloud.comfy.org/api/view?filename=lighthouse_00001_.png&subfolder=agent%2Foutputs&type=output' +const VIDEO_URL = + 'https://cloud.comfy.org/api/view?filename=lighthouse_00001_.mp4&subfolder=agent%2Foutputs&type=output' +const ASSISTANT_REPLY = `Here is the image: [lighthouse_00001_.png](${IMAGE_URL}) and the video: [lighthouse_00001_.mp4](${VIDEO_URL})` +const SAVED_AT = '2026-10-15T10:00:00Z' + +const THREADS: AgentThreadListResponse = { + threads: [ + { + id: THREAD_ID, + title: THREAD_TITLE, + preview: USER_REQUEST, + workflow_id: WORKFLOW_ID, + status: 'active', + message_count: 2, + created_at: SAVED_AT, + updated_at: SAVED_AT, + last_message_at: SAVED_AT + } + ], + pagination: { offset: 0, limit: 100, total: 1, has_more: false } +} +const TRANSCRIPT: AgentMessage[] = [ + { + id: 'lighthouse-user', + thread_id: THREAD_ID, + turn_id: TURN_ID, + seq: 1, + role: 'user', + status: 'complete', + workflow_id: WORKFLOW_ID, + content: { text: USER_REQUEST } + }, + { + id: TURN_ID, + thread_id: THREAD_ID, + turn_id: TURN_ID, + seq: 2, + role: 'assistant', + status: 'complete', + workflow_id: WORKFLOW_ID, + content: { + text: ASSISTANT_REPLY, + tool_calls: [ + { + id: 'call-image', + tool_call_id: 'toolu_image', + tool_name: 'run_workflow', + status: 'success', + duration_ms: 4200 + }, + { + id: 'call-video', + tool_call_id: 'toolu_video', + tool_name: 'run_workflow', + status: 'success', + duration_ms: 18400 + } + ] + } + } +] +const CLOUD_WORKFLOWS: WorkflowListResponse = { + data: [WORKFLOW_ID, TWIN_WORKFLOW_ID].map((id) => ({ + id, + name: WORKFLOW_NAME, + created_at: SAVED_AT, + updated_at: SAVED_AT, + created_by: 'test-user-e2e', + latest_version: 1 + })), + pagination: { offset: 0, limit: 100, total: 2, has_more: false } +} +const SAVED_FILES: UserDataFullInfo[] = [ + `${WORKFLOW_NAME}.json`, + `drafts/${WORKFLOW_NAME}.json` +].map((path) => ({ path, modified: Date.parse(SAVED_AT), size: 64 })) + +test.describe( + 'Agent history reports an unresolvable chat workflow', + { tag: ['@cloud', '@agent'] }, + () => { + test.beforeEach(async ({ page, agentFlagEnabled }) => { + await bootAgentApp(page, agentFlagEnabled, { + beforeNavigate: async (page) => { + await page.route('**/api/agent/threads?*', (route) => + route.fulfill(jsonRoute(THREADS)) + ) + await page.route('**/api/agent/threads/*/messages', (route) => + route.request().method() === 'GET' + ? route.fulfill(jsonRoute(TRANSCRIPT)) + : route.fallback() + ) + await page.route('**/api/workflows?*', (route) => + route.fulfill(jsonRoute(CLOUD_WORKFLOWS)) + ) + await page.route('**/api/userdata?*', (route) => { + const dir = new URL(route.request().url()).searchParams.get('dir') + if (dir !== 'workflows') return route.fallback() + return route.fulfill(jsonRoute(SAVED_FILES)) + }) + } + }) + }) + + test('logs why a chat whose workflow cannot be resolved does not open', async ({ + page + }) => { + const agentPanel = new AgentPanel(page) + const panel = agentPanel.root + const historyRow = panel.getByRole('button', { + name: THREAD_TITLE, + exact: true + }) + using consoleErrors = collectConsoleErrors(page) + + await agentPanel.open() + await panel + .getByRole('button', { name: enMessages.agent.showChatHistory }) + .click() + await historyRow.click() + + await expect(panel.getByRole('alert')).toHaveText( + enMessages.agent.historyOpenFailed + ) + await expect + .poll(() => + consoleErrors.errors.filter((error) => + error.startsWith( + `${REPORTED_ERROR_PREFIX}agent_workflow_restore_failed` + ) + ) + ) + .toHaveLength(1) + }) + } +) diff --git a/docs/adr/AGENT-HISTORY-0038-wait-for-chat-and-workflow-readiness.md b/docs/adr/AGENT-HISTORY-0038-wait-for-chat-and-workflow-readiness.md index 0bdff941d99..e38d17e2a24 100644 --- a/docs/adr/AGENT-HISTORY-0038-wait-for-chat-and-workflow-readiness.md +++ b/docs/adr/AGENT-HISTORY-0038-wait-for-chat-and-workflow-readiness.md @@ -24,7 +24,8 @@ are ready; a remount whose target is already decided has nothing to restore, so it preserves its current identity. A failed startup restoration leaves the session unmarked until a successful retry. Failures remain in history with a retry affordance and existing error details; -the selected row is their only feedback. A chat whose recorded workflow a +the selected row is their only on-screen feedback, and every workflow +restoration failure is still reported with its reason. A chat whose recorded workflow a successful Cloud listing no longer includes opens without a target, even if a stale local binding still names a tab, and the composer tip says the target workflow is no longer available until a workflow is diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.test.ts index a99241f2cf0..ca630124c2f 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.test.ts @@ -5,6 +5,7 @@ import { assert, beforeEach, describe, expect, it, vi } from 'vitest' import { defineComponent, nextTick } from 'vue' import { i18n } from '@/i18n' +import { reportError } from '@comfyorg/telemetry/reportError' import { useWorkflowService } from '@/platform/workflow/core/services/workflowService' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import { blankGraph } from '@/scripts/defaultGraph' @@ -19,6 +20,7 @@ import { useAgentWorkflowResolver } from './useAgentWorkflowResolver' import { useAgentWorkflowSelection } from './useAgentWorkflowSelection' vi.mock(import('@/platform/workflow/core/services/workflowService')) +vi.mock(import('@comfyorg/telemetry/reportError')) function setup({ tabsRestored = true }: { tabsRestored?: boolean } = {}) { const workflows = useWorkflowStore() @@ -367,6 +369,14 @@ describe('historical workflow restoration', () => { expect(panel.selectedWorkflow).toBeNull() expect(panel.targetUnavailable).toBe(false) expect(warnRestoreFailed).toHaveBeenCalledOnce() + expect(reportError).toHaveBeenCalledWith( + new Error('No saved workflow resolves to the chat target'), + { + surface: 'agent', + errorType: 'agent_workflow_restore_failed', + context: { workflowId: 'wf-saved' } + } + ) }) it.for([ @@ -410,20 +420,23 @@ describe('historical workflow restoration', () => { } ) + const openFailure = new Error('Cannot open') it.for([ { opening: 'returns false', - open: async () => false + open: async () => false, + cause: new Error('The chat target workflow did not open') }, { opening: 'throws', open: async (): Promise => { - throw new Error('Cannot open') - } + throw openFailure + }, + cause: openFailure } ])( 'reports a failed restore when opening the saved workflow $opening', - async ({ open }) => { + async ({ open, cause }) => { const { selection, workflows, panel, current, warnRestoreFailed } = setup() workflows.attachWorkflow( @@ -442,6 +455,11 @@ describe('historical workflow restoration', () => { expect(workflows.activeWorkflow?.path).toBe(current.path) expect(panel.targetUnavailable).toBe(false) expect(warnRestoreFailed).toHaveBeenCalledOnce() + expect(reportError).toHaveBeenCalledWith(cause, { + surface: 'agent', + errorType: 'agent_workflow_restore_failed', + context: { workflowId: 'wf-saved' } + }) } ) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.ts index 829140db6c1..328f5369c2a 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowSelection.ts @@ -4,6 +4,7 @@ import { computed, nextTick, onScopeDispose, ref } from 'vue' import { useI18n } from 'vue-i18n' import { useToast } from '@/components/ui/toast/toastStore' +import { reportError } from '@comfyorg/telemetry/reportError' import type { AgentWorkflowBindSource } from '@comfyorg/telemetry/types' import { useWorkflowService } from '@/platform/workflow/core/services/workflowService' import type { ComfyWorkflow } from '@/platform/workflow/management/stores/comfyWorkflow' @@ -328,25 +329,40 @@ export function useAgentWorkflowSelection({ } if (target === null) { panelStore.setWorkflowTarget(null) - warnRestoreFailed() + failRestore( + workflowId, + new Error('No saved workflow resolves to the chat target') + ) return RESTORE_ABANDONED } const opened = await workflowService.openWorkflow(target, { isCurrent }) if (!isCurrent()) return RESTORE_ABANDONED if (!opened) { panelStore.setWorkflowTarget(null) - warnRestoreFailed() + failRestore( + workflowId, + new Error('The chat target workflow did not open') + ) return RESTORE_ABANDONED } commitWorkflowTarget(target, workflowId, 'restored') return { ready: true, committedWorkflowId: workflowId } - } catch { + } catch (error) { if (!isCurrent()) return RESTORE_ABANDONED - warnRestoreFailed() + failRestore(workflowId, error) return RESTORE_ABANDONED } } + function failRestore(workflowId: string, cause: unknown): void { + reportError(cause, { + surface: 'agent', + errorType: 'agent_workflow_restore_failed', + context: { workflowId } + }) + warnRestoreFailed() + } + function adoptTarget(workflow: ComfyWorkflow, workflowId: string): void { commitWorkflowTarget(workflow, workflowId, 'restored') }