diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 77f27749f88..6cb39076fd0 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -898,6 +898,71 @@ function agentTabFilename(name: string | undefined): string | undefined { return cleaned.length === 0 ? undefined : `${cleaned}.json` } +type AgentTabTarget = NonNullable> + +async function openBoundAgentTab( + data: AgentActiveTabData, + bound: AgentTabTarget, + stale: () => boolean +): Promise { + const opened = await workflowService.openWorkflow(bound) + if (stale()) return + if (!opened) { + warnWorkflowUnavailable() + trackWorkflowOpenFailure('toast') + return + } + // Name resolution does not create the binding consumed downstream. + bindingStore.bind(data.workflow_id, bound.path) + if (status.value !== 'idle') tabActivity.setEditing(bound.path) + bindWorkflow(data.workflow_id) + useTelemetry()?.trackAgentWorkflowApplied({ + workflow_id: data.workflow_id, + target: 'active_tab_switch' + }) +} + +async function openNewAgentTab( + data: AgentActiveTabData, + stale: () => boolean +): Promise { + const creatingStartedAt = Date.now() + tabActivity.setCreating(true) + const remainingCreatingTime = + CREATING_TAB_MIN_DURATION_MS - (Date.now() - creatingStartedAt) + if (remainingCreatingTime > 0) + await new Promise((resolve) => setTimeout(resolve, remainingCreatingTime)) + if (stale()) return + + const tab = workflowStore.createNewTemporary( + agentTabFilename(data.name), + agentTabGraph + ) + tabActivity.setCreating(false) + let opened: boolean + try { + opened = await workflowService.openWorkflow(tab) + } catch (error) { + await workflowService.closeWorkflow(tab, { warnIfUnsaved: false }) + throw error + } + if (stale() || !opened) { + await workflowService.closeWorkflow(tab, { warnIfUnsaved: false }) + if (!stale()) { + warnWorkflowUnavailable() + trackWorkflowOpenFailure('toast') + } + return + } + if (status.value !== 'idle') tabActivity.setEditing(tab.path) + bindingStore.bind(data.workflow_id, tab.path) + bindWorkflow(data.workflow_id) + useTelemetry()?.trackAgentWorkflowApplied({ + workflow_id: data.workflow_id, + target: 'active_tab_open' + }) +} + async function onAgentActiveTab( data: AgentActiveTabData, generation: number @@ -906,59 +971,8 @@ async function onAgentActiveTab( if (stale()) return try { const bound = boundOrOpenWorkflowFor(data.workflow_id) - if (bound) { - const opened = await workflowService.openWorkflow(bound) - if (stale()) return - if (!opened) { - warnWorkflowUnavailable() - trackWorkflowOpenFailure('toast') - return - } - // boundOrOpenWorkflowFor can resolve by cloud name, which leaves no binding behind - // for everything downstream that only reads tabPathFor. - bindingStore.bind(data.workflow_id, bound.path) - if (status.value !== 'idle') tabActivity.setEditing(bound.path) - bindWorkflow(data.workflow_id) - useTelemetry()?.trackAgentWorkflowApplied({ - workflow_id: data.workflow_id, - target: 'active_tab_switch' - }) - return - } - const creatingStartedAt = Date.now() - tabActivity.setCreating(true) - const remainingCreatingTime = - CREATING_TAB_MIN_DURATION_MS - (Date.now() - creatingStartedAt) - if (remainingCreatingTime > 0) - await new Promise((resolve) => setTimeout(resolve, remainingCreatingTime)) - if (stale()) return - const tab = workflowStore.createNewTemporary( - agentTabFilename(data.name), - agentTabGraph - ) - tabActivity.setCreating(false) - let opened: boolean - try { - opened = await workflowService.openWorkflow(tab) - } catch (error) { - await workflowService.closeWorkflow(tab, { warnIfUnsaved: false }) - throw error - } - if (stale() || !opened) { - await workflowService.closeWorkflow(tab, { warnIfUnsaved: false }) - if (!stale()) { - warnWorkflowUnavailable() - trackWorkflowOpenFailure('toast') - } - return - } - if (status.value !== 'idle') tabActivity.setEditing(tab.path) - bindingStore.bind(data.workflow_id, tab.path) - bindWorkflow(data.workflow_id) - useTelemetry()?.trackAgentWorkflowApplied({ - workflow_id: data.workflow_id, - target: 'active_tab_open' - }) + if (bound) await openBoundAgentTab(data, bound, stale) + else await openNewAgentTab(data, stale) } catch (error) { if (stale()) return bindWorkflow(data.workflow_id) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index a6b2f9a2717..031f07cb61a 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -2784,7 +2784,11 @@ describe('app:agent_error telemetry (TEL-8)', () => { const [error, options] = vi.mocked(reportError).mock.calls[0] expect((error as Error).message).toBe('Malformed agent stream event') expect(options.tags?.event_type).toBe('agent_message_done') - expect(options.context?.issues).toEqual(expect.any(Array)) + expect(options.context?.issue_count).toEqual(expect.any(Number)) + expect(options.context?.issue_codes).toEqual( + expect.arrayContaining([expect.any(String)]) + ) + expect(options.context).not.toHaveProperty('issues') }) it('reports a rejected send to the unified error sinks', async () => { @@ -2876,7 +2880,7 @@ describe('app:agent_error telemetry (TEL-8)', () => { try { const ok = await session.sendMessage('make me a cat') - expect(ok).toBe(false) + expect(ok).toBe(true) } finally { setItem.mockRestore() } @@ -2901,7 +2905,7 @@ describe('app:agent_error telemetry (TEL-8)', () => { const session = useAgentSession({ rest, events: fakeEvents().source }) session.start() - await session.sendMessage('make me a cat') + expect(await session.sendMessage('make me a cat')).toBe(true) expect(telemetry.trackAgentError).toHaveBeenCalledWith({ error_class: 'request_failed', @@ -2916,7 +2920,6 @@ describe('app:agent_error telemetry (TEL-8)', () => { const rest = fakeRest({ postMessage: vi.fn(async () => { throw new AgentResponseUnreadableError( - '/agent/threads/new/messages', new SyntaxError('Unexpected end of JSON input') ) }) @@ -2924,7 +2927,7 @@ describe('app:agent_error telemetry (TEL-8)', () => { const session = useAgentSession({ rest, events: fakeEvents().source }) session.start() - await session.sendMessage('make me a cat') + expect(await session.sendMessage('make me a cat')).toBe(true) expect(telemetry.trackAgentError).toHaveBeenCalledWith({ error_class: 'request_failed', @@ -3060,6 +3063,18 @@ describe('app:agent_error telemetry (TEL-8)', () => { // The aborted turn, then the idle session the other nineteen arrived into. expect(telemetry.trackAgentError).toHaveBeenCalledTimes(2) + expect(session.notices.value).toHaveLength(2) + }) + + it('does not trust attacker-controlled message ids as telemetry keys', () => { + const { source, emit } = fakeEvents() + const session = useAgentSession({ rest: fakeRest(), events: source }) + session.start() + + for (let i = 0; i < 100; i++) + emit({ type: 'agent_message_delta', data: { message_id: `spoof-${i}` } }) + + expect(telemetry.trackAgentError).toHaveBeenCalledTimes(1) }) it('tracks malformed frames of event types that never reach the user', async () => { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 7b99be93204..b44a39a96ab 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -103,17 +103,20 @@ export interface AgentSessionDeps { const THREAD_STORAGE_KEY = 'Comfy.Agent.ThreadId' const PREPARE_TIMEOUT_MS = 3000 -const NON_RETRYABLE_REQUEST_STATUSES = new Set([ - 400, 401, 403, 404, 405, 409, 410, 422 -]) - export function isRetryableRequestFailure( error: unknown, accepted: boolean ): boolean { if (accepted) return false - if (error instanceof AgentApiError) - return !NON_RETRYABLE_REQUEST_STATUSES.has(error.status) + if ( + error instanceof ZodError || + error instanceof AgentResponseUnreadableError + ) + return false + if (error instanceof AgentApiError) { + if ([408, 425, 429].includes(error.status)) return true + return error.status < 400 || error.status >= 500 + } return true } @@ -214,15 +217,26 @@ export function useAgentSession(deps: AgentSessionDeps) { eventType: string, turnId: TurnId | null, uiTreatment: AgentErrorMetadata['ui_treatment'] - ): void { + ): boolean { const visible = uiTreatment !== 'none' const priorVisible = malformedStreamReports.get(turnId) - if (priorVisible === true || (priorVisible === false && !visible)) return + if (priorVisible === true || (priorVisible === false && !visible)) + return false + if ( + !malformedStreamReports.has(turnId) && + malformedStreamReports.size >= 32 + ) { + const oldest = malformedStreamReports.keys().next().value + if (oldest !== undefined) malformedStreamReports.delete(oldest) + } malformedStreamReports.set(turnId, visible) reportError(new Error('Malformed agent stream event'), { errorType: 'agent_malformed_stream_event', tags: { ui_treatment: uiTreatment, event_type: eventType }, - context: { issues: cause.issues } + context: { + issue_count: cause.issues.length, + issue_codes: cause.issues.slice(0, 10).map(({ code }) => code) + } }) trackAgentError( 'malformed_stream_event', @@ -230,6 +244,7 @@ export function useAgentSession(deps: AgentSessionDeps) { uiTreatment, { retryable: false } ) + return true } function start(): void { @@ -510,31 +525,34 @@ export function useAgentSession(deps: AgentSessionDeps) { } } - function recordSendError( - error: unknown, - text: string, - accepted: boolean - ): void { + function recordAdmissionSendError(error: unknown, text: string): boolean { const admission = parseAdmissionError(error) - if (admission?.reason === 'no_funds') { + if (admission === undefined) return false + if (admission.reason === 'no_funds') { conversationStore.recordPaywall( nextLocalErrorId(), text, admission.message ) - return - } - if (admission !== undefined) { - conversationStore.recordFailedSend( - nextLocalErrorId(), - text, - admission.message, - admission.reason === 'funds_unavailable' - ? admission.retryAfterSeconds - : undefined - ) - return + return true } + conversationStore.recordFailedSend( + nextLocalErrorId(), + text, + admission.message, + admission.reason === 'funds_unavailable' + ? admission.retryAfterSeconds + : undefined + ) + return true + } + + function recordSendError( + error: unknown, + text: string, + accepted: boolean + ): boolean { + if (recordAdmissionSendError(error, text)) return false const message = error instanceof AgentApiError ? error.message @@ -546,14 +564,16 @@ export function useAgentSession(deps: AgentSessionDeps) { text, `${i18n.global.t('agent.sendFailed')}: ${message}` ) - const turnAccepted = accepted || isUnreadableAckFailure(error) - reportError(error, { errorType: 'agent_send_message_failed' }) + const turnAccepted = accepted + if (!disownsWorkflow(error)) + reportError(error, { errorType: 'agent_send_message_failed' }) trackAgentError( 'request_failed', turnAccepted ? 'post_acceptance' : 'pre_acceptance', 'inline_notice', { retryable: isRetryableRequestFailure(error, turnAccepted) } ) + return turnAccepted } /** @@ -578,6 +598,23 @@ export function useAgentSession(deps: AgentSessionDeps) { if (rememberedWorkflowId === sent.id) rememberedWorkflowId = null } + function handleSendFailure( + error: unknown, + text: string, + generation: number, + sentContext: WorkflowTurnContext | undefined, + accepted: boolean, + requestStarted: boolean + ): boolean { + releaseDisownedWorkflow(sentContext, error) + if (generation !== loadGeneration) return false + return recordSendError( + error, + text, + accepted || (requestStarted && isUnreadableAckFailure(error)) + ) + } + async function performSend( text: string, attachments?: SentAttachment[], @@ -592,6 +629,7 @@ export function useAgentSession(deps: AgentSessionDeps) { originContext === undefined ? null : { tabPath: originContext.tabPath } let sentContext: WorkflowTurnContext | undefined let accepted = false + let requestStarted = false try { await prepareWorkflow() if (generation !== loadGeneration) return false @@ -601,6 +639,7 @@ export function useAgentSession(deps: AgentSessionDeps) { return false } sentContext = wfContext + requestStarted = true const ack = await postTurn( threadAtSend, text, @@ -612,17 +651,24 @@ export function useAgentSession(deps: AgentSessionDeps) { selectionWorkflowId ) accepted = true - if (generation !== loadGeneration) return false + // Navigation invalidates the originating submission before advancing the + // generation. The server still accepted this turn, so reporting success + // prevents a future caller from offering a duplicate non-idempotent send. + if (generation !== loadGeneration) return true acceptTurn(ack, text, wfContext, attachments, tags, workflowReferences) return true } catch (error) { // Before the generation guard: the binding store is page-global and // persisted, so a refusal that lands after newChat()/loadThread() has // moved on still has to release, or the dead id survives the reload. - releaseDisownedWorkflow(sentContext, error) - if (generation !== loadGeneration) return false - recordSendError(error, text, accepted) - return false + return handleSendFailure( + error, + text, + generation, + sentContext, + accepted, + requestStarted + ) } } @@ -771,32 +817,43 @@ export function useAgentSession(deps: AgentSessionDeps) { if (hydrated && isCurrent()) conversationStore.resumeBackgroundTurn() } + function malformedEventTurnId(raw: object): TurnId | undefined { + const messageId = (raw as { data?: { message_id?: unknown } }).data + ?.message_id + return typeof messageId === 'string' ? (messageId as TurnId) : undefined + } + + function settleMalformedDoneEvent( + type: string, + turnId: TurnId | undefined + ): AgentErrorMetadata['ui_treatment'] { + if (type !== 'agent_message_done') return 'none' + if (turnId === undefined || turnId === conversationStore.activeTurnId) { + conversationStore.abortActiveTurn() + return 'error_overlay' + } + if (conversationStore.hasPendingTurn(turnId)) { + conversationStore.settleBackgroundTurn(turnId) + } + return 'none' + } + function handleMalformedEvent( raw: object, type: string, error: ZodError ): void { - const messageId = (raw as { data?: { message_id?: unknown } }).data - ?.message_id - let reportedTurnId = - typeof messageId === 'string' - ? (messageId as TurnId) + const turnId = malformedEventTurnId(raw) + const reportedTurnId = + turnId !== undefined && conversationStore.hasPendingTurn(turnId) + ? turnId : conversationStore.activeTurnId - let uiTreatment: AgentErrorMetadata['ui_treatment'] = 'none' - if (type === 'agent_message_done') { - if ( - typeof messageId !== 'string' || - messageId === conversationStore.activeTurnId - ) { - conversationStore.abortActiveTurn() - pushError(i18n.global.t('agent.malformedEvent')) - uiTreatment = 'error_overlay' - } else { - reportedTurnId = - conversationStore.settleBackgroundTurn(messageId) ?? reportedTurnId - } - } - trackMalformedStreamEvent(error, type, reportedTurnId, uiTreatment) + const uiTreatment = settleMalformedDoneEvent(type, turnId) + if ( + trackMalformedStreamEvent(error, type, reportedTurnId, uiTreatment) && + uiTreatment === 'error_overlay' + ) + pushError(i18n.global.t('agent.malformedEvent')) console.warn('[agent] dropping malformed agent event', error) } diff --git a/src/workbench/extensions/agent/services/agent/agentRestClient.ts b/src/workbench/extensions/agent/services/agent/agentRestClient.ts index 8606663b585..62acb4569a8 100644 --- a/src/workbench/extensions/agent/services/agent/agentRestClient.ts +++ b/src/workbench/extensions/agent/services/agent/agentRestClient.ts @@ -49,8 +49,8 @@ export class AgentApiError extends Error { } export class AgentResponseUnreadableError extends Error { - constructor(route: string, cause: unknown) { - super(`Unreadable agent response body from ${route}`, { cause }) + constructor(cause: unknown) { + super('Unreadable agent response body', { cause }) this.name = 'AgentResponseUnreadableError' } } @@ -329,6 +329,24 @@ function asDelaySeconds(seconds: number): number | undefined { return Number.isSafeInteger(seconds) && seconds >= 0 ? seconds : undefined } +function buildPostMessageBody(req: PostMessageInput): AgentPostMessageRequest { + const body: AgentPostMessageRequest = { content: req.content } + if (req.workflowId !== undefined) body.workflow_id = req.workflowId + if (req.tabs !== undefined) { + body.open_tabs = req.tabs.open_tabs + if (req.tabs.current_tab !== undefined) + body.current_tab = req.tabs.current_tab + } + if (req.workflowReferences !== undefined) + body.workflow_references = req.workflowReferences + if (req.selection !== undefined) body.selection = req.selection + if (req.attachments !== undefined) body.attachments = req.attachments + if (req.draft !== undefined) body.draft = { content: req.draft.content } + if (req.currentTabUnbound !== undefined) + body.current_tab_unbound = req.currentTabUnbound + return body +} + export function createAgentRestClient() { async function toApiError(response: Response): Promise { const body = parseErrorBody(await response.text()) @@ -350,7 +368,8 @@ export function createAgentRestClient() { try { payload = await response.json() } catch (error) { - throw new AgentResponseUnreadableError(route, error) + if (error instanceof Error && error.name === 'AbortError') throw error + throw new AgentResponseUnreadableError(error) } return schema.parse(payload) } @@ -367,27 +386,17 @@ export function createAgentRestClient() { threadId: string, req: PostMessageInput ): Promise { - const body: AgentPostMessageRequest = { - content: req.content - } - if (req.workflowId !== undefined) body.workflow_id = req.workflowId - if (req.tabs !== undefined) { - body.open_tabs = req.tabs.open_tabs - if (req.tabs.current_tab !== undefined) - body.current_tab = req.tabs.current_tab + try { + return await request( + `/agent/threads/${encodeURIComponent(threadId)}/messages`, + jsonInit('POST', buildPostMessageBody(req)), + zAgentTurnAccepted + ) + } catch (error) { + if (error instanceof Error && error.name === 'AbortError') + throw new AgentResponseUnreadableError(error) + throw error } - if (req.workflowReferences !== undefined) - body.workflow_references = req.workflowReferences - if (req.selection !== undefined) body.selection = req.selection - if (req.attachments !== undefined) body.attachments = req.attachments - if (req.draft !== undefined) body.draft = { content: req.draft.content } - if (req.currentTabUnbound !== undefined) - body.current_tab_unbound = req.currentTabUnbound - return request( - `/agent/threads/${encodeURIComponent(threadId)}/messages`, - jsonInit('POST', body), - zAgentTurnAccepted - ) } async function getMessages(threadId: string): Promise { diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index 0d6cb684e7c..0ab30233c39 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -409,7 +409,7 @@ export const useAgentConversationStore = defineStore( return true } - function settleBackgroundTurn(turnId: string): TurnId | null { + function settleBackgroundTurn(turnId: string): void { for (const [key, entry] of backgroundTurns) { if (entry.messageId !== turnId) continue rememberDepartedTurn(key, entry.messageId, 'settled-turn') @@ -419,9 +419,15 @@ export const useAgentConversationStore = defineStore( // will keep it reachable afterwards, so flush its held parts now. entry.transport.dispose() backgroundTurns.delete(key) - return entry.messageId + return } - return null + } + + function hasPendingTurn(turnId: string): boolean { + if (activeTurnId.value === turnId) return true + for (const entry of backgroundTurns.values()) + if (entry.messageId === turnId) return true + return false } function dropBackgroundTurns(): void { @@ -601,6 +607,7 @@ export const useAgentConversationStore = defineStore( stashActiveTurn, resumeBackgroundTurn, settleBackgroundTurn, + hasPendingTurn, dropBackgroundTurns, reset, hydrate