diff --git a/.agents/checks/follower-boundary.md b/.agents/checks/follower-boundary.md index c20839288ba..a824e5470f8 100644 --- a/.agents/checks/follower-boundary.md +++ b/.agents/checks/follower-boundary.md @@ -115,26 +115,34 @@ Flag: - **Branch-pinned catalog/vocabulary citation** — referencing the widget catalog by moving branch instead of a pinned SHA. -## Priority 4: distribution seam — one boundary, unified auth +## Priority 4: distribution seam — one socket, one credential branch -Per FOLLOWER the follower APPLY path is distribution-agnostic; surface differences -(endpoint, ingest-vs-direct route, auth) live behind one small distribution-resolved -boundary keyed on `DISTRIBUTION`. +Per the FOLLOWER amendment (2026-10-12) the follower APPLY path is +distribution-agnostic. Cloud ingest and the local agent serve the same contract: +agent events and document frames share `/api/agent/events`, whose path comes from +`api.apiURL`. The only distribution branch is the credential choice in `agentAuth.ts`. Flag: - **`DISTRIBUTION` / `isCloud` / `isDesktop` checks scattered through follower core** — - distribution conditionals inside the CRDT apply seam, `DocFrameClient`, or rendering. - They belong only in the agent connection/configuration boundary. -- **Auth reduced to `isCloud`** — resolving credentials by distribution instead of - delegating to the unified chain `authStore.getAuthHeader()`. Local and Desktop are - authenticated product surfaces, not "anonymous / model-key-only". + distribution conditionals inside the CRDT apply seam, `DocFrameClient`, the agent + socket transport, or rendering. They belong only in the credential branch in + `agentAuth.ts`. +- **A credential path other than the one `isCloud` branch** — in the cloud, agent REST + requests authenticate through `fetchApi` alone and the socket takes + `api.socketAuthParams()` (no socket when no one is signed in); adding the user's own + header there moves a request out of its workspace. Off-cloud, REST requests carry + `getUserAuthHeader()` and the socket carries the same credential as `?token=`. Flag + credential resolution outside `agentAuth.ts`, or any branch that leaves Local or + Desktop unauthenticated: they are authenticated product surfaces, not + "anonymous / model-key-only". - **Dev/Vite behavior treated as Local product behavior** — Vite proxy or dev credentials - placed in the Local or Desktop path. Dev-only wiring stays in Vite/dev config. -- **A hardcoded agent base URL** — an agent HTTP/WS endpoint string literal instead of a - centralized distribution-resolved resolver (the `AGENT_BASE_URL`-style seam). Today the - follower rides the centralized `api.socket`; a new direct-to-agent connection must - resolve its endpoint through the seam, not a literal. + placed in the Local or Desktop path. Dev-only wiring stays in Vite/dev config; the + browser learns only the build-time flag `VITE_DEV_AGENT_PROXY_CREDENTIAL`, never the + token. +- **A hardcoded agent base URL or a second agent socket** — an agent HTTP/WS endpoint + string literal instead of `api.apiURL`, or a transport other than + `/api/agent/events` (such as agent traffic on `/ws` or a separate local-agent socket). ## Enforcement expectation diff --git a/browser_tests/fixtures/agentAskTurnFixture.ts b/browser_tests/fixtures/agentAskTurnFixture.ts index e040f16dba7..a88d949ec20 100644 --- a/browser_tests/fixtures/agentAskTurnFixture.ts +++ b/browser_tests/fixtures/agentAskTurnFixture.ts @@ -1,4 +1,4 @@ -import type { Locator, Page, WebSocketRoute } from '@playwright/test' +import type { Locator, Page } from '@playwright/test' import { expect } from '@playwright/test' import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' @@ -16,6 +16,7 @@ import { bootAgentApp, mockWorkflowPersistence } from '@e2e/fixtures/agentPanelFixture' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' @@ -27,7 +28,6 @@ import { import { AGENT_COMPOSER_LABEL } from '@e2e/fixtures/utils/agentComposerLabel' import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' -const SOCKET_SID = '8e2f3a4b-5c6d-4e7f-9a01-2b3c4d5e6f70' const CATALOG: WidgetCatalog = { types: {} } const SEED: WorkflowJSON = { nodes: [], links: [] } @@ -46,10 +46,6 @@ export interface AskTurn { answer: (prompt: string, option: string) => Promise } -function isRecord(value: unknown): value is Record { - return typeof value === 'object' && value !== null -} - function isAnswerTo(askId: string): (url: URL) => boolean { return (url) => decodeURIComponent(url.pathname) === answerPath(askId) } @@ -59,12 +55,13 @@ async function startAskTurn( prompt: string, storedMode: AgentRunMode['mode'] ): Promise { - const host = new HostDoc(ASK_WORKFLOW_ID, SEED, CATALOG) - let socket: WebSocketRoute | null = null - const send = (frame: AgentWsEvent | AgentAskServerFrame | HostFrame) => { - if (!socket) throw new Error('the app has not opened /ws yet') - socket.send(JSON.stringify(frame)) - } + const hostSocket = new AgentFollowerHostSocket( + page, + ASK_WORKFLOW_ID, + new HostDoc(ASK_WORKFLOW_ID, SEED, CATALOG) + ) + const send = (frame: AgentWsEvent | AgentAskServerFrame | HostFrame) => + hostSocket.send(frame) const answerCalls: AnswerCall[] = [] const runMode: AgentRunMode = { mode: storedMode, credit_limit: null } @@ -92,29 +89,7 @@ async function startAskTurn( } return route.fulfill(jsonRoute([])) }) - await page.routeWebSocket(/\/ws/, (ws) => { - socket = ws - ws.send( - JSON.stringify({ - type: 'status', - data: { status: { exec_info: { queue_remaining: 0 } }, sid: SOCKET_SID } - }) - ) - ws.onMessage((raw) => { - const frame: unknown = JSON.parse(raw.toString()) - if (!isRecord(frame)) return - const { type, data } = frame - if (type !== 'doc_subscribe' || !isRecord(data)) return - const { workflow_id, state_vector_b64 } = data - if ( - workflow_id !== ASK_WORKFLOW_ID || - typeof state_vector_b64 !== 'string' - ) - return - send(host.subscribed()) - send(host.catchUp(state_vector_b64)) - }) - }) + await hostSocket.install() await bootAgentApp(page, true, { settings: { 'Comfy.Graph.CanvasInfo': false } @@ -138,7 +113,7 @@ async function startAskTurn( .getByRole('menuitemradio', { name: 'Unsaved Workflow', exact: true }) .click() await expect(picker).toHaveText('Unsaved Workflow') - await expect.poll(() => socket !== null).toBe(true) + await expect.poll(() => hostSocket.connectionCount()).toBeGreaterThan(0) await panel.getByRole('textbox', { name: AGENT_COMPOSER_LABEL }).fill(prompt) await panel.getByRole('button', { name: enMessages.agent.send }).click() diff --git a/browser_tests/fixtures/agentAutogrowHandWiredLinkFixture.ts b/browser_tests/fixtures/agentAutogrowHandWiredLinkFixture.ts index 5a854116b18..c3d017e05e7 100644 --- a/browser_tests/fixtures/agentAutogrowHandWiredLinkFixture.ts +++ b/browser_tests/fixtures/agentAutogrowHandWiredLinkFixture.ts @@ -50,7 +50,6 @@ const AGENT_RECONNECT_LINK_ID = 9004 const WORKFLOW_ID = '6a1c9f3e-4d2b-4a7c-8e1f-3b5d7c9a1f6b' const THREAD_ID = 'c3d4e5f6-1a2b-4c3d-8e4f-5a6b7c8d9e0f' const MESSAGE_ID = 'a1b2c3d4-5e6f-4a1b-8c2d-3e4f5a6b7c8d' -const SOCKET_SID = 'f1e2d3c4-b5a6-4978-8a9b-0c1d2e3f4a5b' // Trimmed `/object_info` entries: a plain IMAGE source, and an API node whose // only input is a `COMFY_AUTOGROW_V3` group named `model.images`, mirroring @@ -214,7 +213,6 @@ async function setUpFixture(page: Page) { page, WORKFLOW_ID, host, - SOCKET_SID, 'apply' ) await hostSocket.install() diff --git a/browser_tests/fixtures/agentAutogrowSpareInputFixture.ts b/browser_tests/fixtures/agentAutogrowSpareInputFixture.ts index cc4cbc60440..f6b3c3f4caf 100644 --- a/browser_tests/fixtures/agentAutogrowSpareInputFixture.ts +++ b/browser_tests/fixtures/agentAutogrowSpareInputFixture.ts @@ -176,12 +176,7 @@ async function wireAutogrowNodeAndSwitchTabs(page: Page) { ) const host = new HostDoc(WORKFLOW_ID, seed, catalog) - const hostSocket = new AgentFollowerHostSocket( - page, - WORKFLOW_ID, - host, - 'autogrow-e2e' - ) + const hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, host) await hostSocket.install() await bootAgentApp(page, true, { diff --git a/browser_tests/fixtures/agentConversationFixture.ts b/browser_tests/fixtures/agentConversationFixture.ts index 416f8bd4818..f2cc5de68df 100644 --- a/browser_tests/fixtures/agentConversationFixture.ts +++ b/browser_tests/fixtures/agentConversationFixture.ts @@ -27,6 +27,10 @@ import { ComfyPage } from '@e2e/fixtures/ComfyPage' import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' +import { + AGENT_FRAME_HANDLED_EVENT, + announceHandledAgentFrames +} from '@e2e/fixtures/agentSocket' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' import type { ClientDocFrame, @@ -62,7 +66,6 @@ const THREAD_ID = 'e9a2f3d1-7c44-4b2e-9a01-5f6d8c7b3a10' // One synthetic message id per turn; the recorded ids never reach the page. const turnId = (turn: number): string => `0c5b1e77-2d4a-4f9e-8b63-1a2c3d4e5${turn.toString(16).padStart(3, '0')}` -const SOCKET_SID = '7d1f2e3a-4b5c-4d6e-8f90-1a2b3c4d5e6f' const VUE_NODES_TAG = '@vue-nodes' const PANEL_MOUNT_TIMEOUT = 30_000 const CANCEL_TIMEOUT = 10_000 @@ -200,7 +203,6 @@ interface ReplayResponseOptions { waitForGraphOpsDelivery?: (ops: readonly RecordedGraphOperation[]) => boolean } -// Runs one recorded prompt/response through the real panel over a routed /ws socket. export class AgentConversationHarness { readonly panel: Locator readonly vueNodes: VueNodeHelpers @@ -235,7 +237,6 @@ export class AgentConversationHarness { page, workflow.id, this.host, - SOCKET_SID, humanOpsHost ) this.seenIds = new Set(workflow.seed.nodes.map((node) => String(node.id))) @@ -298,6 +299,7 @@ export class AgentConversationHarness { ): Promise { await this.mockAgentApi() await this.hostSocket.install() + await announceHandledAgentFrames(this.page) const objectInfo = this.page.waitForResponse((response) => new URL(response.url()).pathname.endsWith('/api/object_info') ) @@ -1206,27 +1208,33 @@ export class AgentConversationHarness { const receipt = crypto.randomUUID() await this.page.evaluate( - ({ receipt, workflowId, seq }) => { - const api = window.app!.api + ({ receipt, workflowId, seq, handledEvent }) => { const attribute = 'data-agent-crdt-update-receipt' const cleanupType = `agent-crdt-update-cleanup-${receipt}` - const removeReceiptListener = () => { - api.removeCustomEventListener('doc_update', recordReceipt) + function removeReceiptListener() { + document.removeEventListener(handledEvent, recordReceipt) document.removeEventListener(cleanupType, removeReceiptListener) } - const recordReceipt = (event: CustomEvent) => { - if (typeof event.detail !== 'object' || event.detail === null) return - if ( - !('workflow_id' in event.detail) || - event.detail.workflow_id !== workflowId || - !('seq' in event.detail) || - event.detail.seq !== seq + function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null + } + function isThisUpdate(detail: unknown): boolean { + if (typeof detail !== 'string') return false + const frame: unknown = JSON.parse(detail) + if (!isRecord(frame) || frame.type !== 'doc_update') return false + return ( + isRecord(frame.data) && + frame.data.workflow_id === workflowId && + frame.data.seq === seq ) + } + function recordReceipt(event: Event) { + if (!(event instanceof CustomEvent) || !isThisUpdate(event.detail)) return document.documentElement.setAttribute(attribute, receipt) removeReceiptListener() } - api.addCustomEventListener('doc_update', recordReceipt) + document.addEventListener(handledEvent, recordReceipt) document.addEventListener(cleanupType, removeReceiptListener, { once: true }) @@ -1234,7 +1242,8 @@ export class AgentConversationHarness { { receipt, workflowId: parsedFrame.data.workflowId, - seq: parsedFrame.data.seq + seq: parsedFrame.data.seq, + handledEvent: AGENT_FRAME_HANDLED_EVENT } ) try { diff --git a/browser_tests/fixtures/agentCrdtIdCollisionFixture.ts b/browser_tests/fixtures/agentCrdtIdCollisionFixture.ts index 5c5829dc501..51b3c0b0e21 100644 --- a/browser_tests/fixtures/agentCrdtIdCollisionFixture.ts +++ b/browser_tests/fixtures/agentCrdtIdCollisionFixture.ts @@ -193,7 +193,6 @@ export class IdCollisionHarness { page, WORKFLOW_ID, this.host, - 'id-collision-repro-sid', 'hold' ) } diff --git a/browser_tests/fixtures/agentFollowerHostSocket.test.ts b/browser_tests/fixtures/agentFollowerHostSocket.test.ts index a893c13a68b..e6d3b3d3804 100644 --- a/browser_tests/fixtures/agentFollowerHostSocket.test.ts +++ b/browser_tests/fixtures/agentFollowerHostSocket.test.ts @@ -6,6 +6,7 @@ import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' const WORKFLOW_ID = 'wf-1' +const AGENT_EVENTS_URL = 'ws://127.0.0.1:8188/api/agent/events?token=abc' // A fake `Page` whose `routeWebSocket` hands back a fake `WebSocketRoute` // synchronously, so `AgentFollowerHostSocket` runs against the real class @@ -27,10 +28,11 @@ function fakeRoutedPage(): { }) const page = fromPartial({ routeWebSocket: async ( - _url: string | RegExp, + url: string | RegExp, handler: (route: WebSocketRoute) => unknown ) => { - handler(socket) + if (url instanceof RegExp && url.test(AGENT_EVENTS_URL)) handler(socket) + else handler(fromPartial({ send: () => {} })) } }) return { @@ -40,6 +42,59 @@ function fakeRoutedPage(): { } } +describe('AgentFollowerHostSocket transport', () => { + it('answers a doc_subscribe with doc_subscribed and a catch-up doc_update', async () => { + const host = new HostDoc( + WORKFLOW_ID, + { nodes: [], links: [] }, + { types: {} } + ) + const { page, emitClientFrame, sentFrames } = fakeRoutedPage() + const hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, host) + await hostSocket.install() + + emitClientFrame({ + type: 'doc_subscribe', + data: { workflow_id: WORKFLOW_ID, state_vector_b64: 'AA==' } + }) + + expect(sentFrames().map((frame) => frame.type)).toEqual([ + 'doc_subscribed', + 'doc_update' + ]) + expect(hostSocket.subscribeCount()).toBe(1) + }) +}) + +describe('AgentFollowerHostSocket subscribe counts', () => { + it('counts a refused subscribe only as refused, then the accepted retry as a subscribe', async () => { + const host = new HostDoc( + WORKFLOW_ID, + { nodes: [], links: [] }, + { types: {} } + ) + const { page, emitClientFrame, sentFrames } = fakeRoutedPage() + const hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, host) + await hostSocket.install() + hostSocket.refuseSubscribes('overloaded', 1) + const subscribe = { + type: 'doc_subscribe', + data: { workflow_id: WORKFLOW_ID, state_vector_b64: 'AA==' } + } + + emitClientFrame(subscribe) + + expect(sentFrames().map((frame) => frame.type)).toEqual(['doc_subscribed']) + expect(hostSocket.refusedSubscribeCount()).toBe(1) + expect(hostSocket.subscribeCount()).toBe(0) + + emitClientFrame(subscribe) + + expect(hostSocket.refusedSubscribeCount()).toBe(1) + expect(hostSocket.subscribeCount()).toBe(1) + }) +}) + describe('AgentFollowerHostSocket human doc_ops handling', () => { it('answers a malformed doc_ops batch with ok:false and never reaches the applier', async () => { const host = new HostDoc( @@ -52,7 +107,6 @@ describe('AgentFollowerHostSocket human doc_ops handling', () => { page, WORKFLOW_ID, host, - 'sid-1', 'apply' ) await hostSocket.install() @@ -87,7 +141,6 @@ describe('AgentFollowerHostSocket human doc_ops handling', () => { page, WORKFLOW_ID, host, - 'sid-1', 'apply' ) await hostSocket.install() @@ -115,7 +168,7 @@ describe('AgentFollowerHostSocket human doc_ops handling', () => { expect(hostSocket.humanOpOutcomes()).toEqual([ { op_id: 'a'.repeat(32), outcome: 'applied' } ]) - expect(sentFrames().slice(1)).toEqual([ + expect(sentFrames()).toEqual([ { type: 'doc_ops_result', data: expect.objectContaining({ ok: true }) @@ -156,7 +209,6 @@ describe('AgentFollowerHostSocket human doc_ops handling', () => { page, WORKFLOW_ID, host, - 'sid-1', 'apply' ) await hostSocket.install() diff --git a/browser_tests/fixtures/agentFollowerHostSocket.ts b/browser_tests/fixtures/agentFollowerHostSocket.ts index 7d21aaf4ef1..33e5656acd3 100644 --- a/browser_tests/fixtures/agentFollowerHostSocket.ts +++ b/browser_tests/fixtures/agentFollowerHostSocket.ts @@ -1,5 +1,6 @@ import type { Page, WebSocketRoute } from '@playwright/test' import type { ApplyOutcome } from '@comfyorg/comfy-multi-player' +import type { AgentAskServerFrame } from '@comfyorg/ingest-types' import { DOC_PROTOCOL_VERSION, @@ -20,6 +21,7 @@ import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApi import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' import type { HostDoc, HostFrame } from '@e2e/fixtures/agentConversationHostDoc' +import { AGENT_SOCKET_URL } from '@e2e/fixtures/agentSocket' import { isValidDocOpsBatch, parseWireOps } from '@e2e/fixtures/agentWireFrame' import type { ParsedWireBatch, @@ -27,6 +29,7 @@ import type { } from '@e2e/fixtures/agentWireFrame' const SUBSCRIBE_TIMEOUT = 15_000 +const COMFY_SOCKET_SID = '7d1f2e3a-4b5c-4d6e-8f90-1a2b3c4d5e6f' /** * ComfyUI execution frames the fake host can emit alongside doc and agent @@ -58,9 +61,11 @@ export interface ClientDocFrame { atMs: number type: string workflowId: string | null + stateVector: string | null /** `op:node_id` per op for a `doc_ops` frame; empty otherwise. */ ops: string[] opIds: string[] + connection: number } interface ParsedClientDocFrame { @@ -103,14 +108,19 @@ function parseClientDocFrame( } } -/** Routed `/ws` host shared by black-box Agent follower fixtures. */ export class AgentFollowerHostSocket { private refuseReason: string | null = null private refusalsLeft = 0 private refusedSubscribes = 0 private socket: WebSocketRoute | null = null + private comfySocket: WebSocketRoute | null = null + private connections = 0 private subscribes = 0 + private subscribeAnswer = (stateVector: string): HostFrame[] => [ + this.host.subscribed(), + this.host.catchUp(stateVector) + ] private readonly createdAt = Date.now() private readonly clientFrames: ClientDocFrame[] = [] private readonly heldBatches: WireOpEnvelope[][] = [] @@ -124,48 +134,50 @@ export class AgentFollowerHostSocket { private readonly page: Page, private readonly workflowId: string, private readonly host: HostDoc, - private readonly socketSid: string, private readonly humanOpsHost: HumanOpsHost = 'hold', private readonly hostUpdateSink?: HostUpdateSink ) {} async install(): Promise { await this.page.routeWebSocket(/\/ws/, (socket) => { - this.socket = socket - socket.onMessage((raw) => this.onClientFrame(raw)) + this.comfySocket = socket socket.send( JSON.stringify({ type: 'status', data: { status: { exec_info: { queue_remaining: 0 } }, - sid: this.socketSid + sid: COMFY_SOCKET_SID } }) ) }) + await this.page.routeWebSocket(AGENT_SOCKET_URL, (socket) => { + this.connections += 1 + this.socket = socket + socket.onMessage((raw) => this.onClientFrame(raw)) + }) } - send(frame: AgentWsEvent | HostFrame): void { + send(frame: AgentWsEvent | AgentAskServerFrame | HostFrame): void { if (frame.type.startsWith('doc_') || frame.type === 'awareness') { if (parseServerDocFrame(frame) === null) throw new Error(`host frame ${frame.type} is not a valid doc frame`) } else if (!parseAgentWsEvent(frame).success) { throw new Error(`agent event ${frame.type} is not a valid agent event`) } - if (!this.socket) throw new Error('the app has not opened /ws yet') + if (!this.socket) + throw new Error('the panel has not opened the agent socket yet') this.socket.send(JSON.stringify(frame)) } - /** Emits a ComfyUI execution frame on the shared `/ws`. */ sendExecution(frame: ExecutionHostFrame): void { - if (!this.socket) throw new Error('the app has not opened /ws yet') - this.socket.send(JSON.stringify(frame)) + if (!this.comfySocket) throw new Error('the app has not opened /ws yet') + this.comfySocket.send(JSON.stringify(frame)) } - /** Emits a binary ComfyUI execution frame on the shared `/ws`. */ sendExecutionBinary(frame: Buffer): void { - if (!this.socket) throw new Error('the app has not opened /ws yet') - this.socket.send(frame) + if (!this.comfySocket) throw new Error('the app has not opened /ws yet') + this.comfySocket.send(frame) } async waitForSubscribe(): Promise { @@ -205,8 +217,10 @@ export class AgentFollowerHostSocket { atMs: Date.now() - this.createdAt, type: frame.type, workflowId: frame.workflowId, + stateVector: frame.stateVector, ops: ops.map((op) => opLabel(op)), - opIds: ops.map((op) => op.op_id) + opIds: ops.map((op) => op.op_id), + connection: this.connections }) } @@ -268,17 +282,19 @@ export class AgentFollowerHostSocket { this.refusalsLeft = times } + answerSubscribesWith(answer: (stateVector: string) => HostFrame[]): void { + this.subscribeAnswer = answer + } + private answerSubscribe(stateVector: string): void { if (this.refuseReason !== null && this.refusalsLeft > 0) { this.refusalsLeft -= 1 this.refusedSubscribes += 1 this.send(this.host.subscribeRefused(this.refuseReason)) - this.subscribes += 1 this.resolveSubscribed?.() return } - this.send(this.host.subscribed()) - this.send(this.host.catchUp(stateVector)) + for (const frame of this.subscribeAnswer(stateVector)) this.send(frame) this.subscribes += 1 this.resolveSubscribed?.() } @@ -340,13 +356,22 @@ export class AgentFollowerHostSocket { } } - /** Rises once per follower subscribe, after the catch-up frame was sent. */ + /** + * Rises once per subscribe the host accepted, after its answer was sent. + * A custom answer from {@link answerSubscribesWith} may carry no catch-up; + * refused subscribes count only in {@link refusedSubscribeCount}. + */ subscribeCount(): number { return this.subscribes } + connectionCount(): number { + return this.connections + } + async disconnect(): Promise { - if (!this.socket) throw new Error('the app has not opened /ws yet') + if (!this.socket) + throw new Error('the panel has not opened the agent socket yet') await this.socket.close() } diff --git a/browser_tests/fixtures/agentInputOrderFixture.ts b/browser_tests/fixtures/agentInputOrderFixture.ts index 78bd498f8de..7ed47aac468 100644 --- a/browser_tests/fixtures/agentInputOrderFixture.ts +++ b/browser_tests/fixtures/agentInputOrderFixture.ts @@ -17,19 +17,12 @@ import { } from '@e2e/fixtures/data/agent/inputOrder' import { loadSeedIntoActiveTab } from '@e2e/fixtures/utils/seedActiveTab' -const SOCKET_SID = '6a80fd06-c647-4b17-9f68-202366e468d8' - export const test = agentTest.extend<{ inputOrderHost: AgentFollowerHostSocket }>({ inputOrderHost: async ({ page }, use) => { const host = new HostDoc(workflowId, structuredClone(seed), catalog) - const socket = new AgentFollowerHostSocket( - page, - workflowId, - host, - SOCKET_SID - ) + const socket = new AgentFollowerHostSocket(page, workflowId, host) await socket.install() await mockAgentTurnApi(page, { diff --git a/browser_tests/fixtures/agentNonValueWidgetRig.ts b/browser_tests/fixtures/agentNonValueWidgetRig.ts index f2dba2f9cdf..7dfa50b0ae4 100644 --- a/browser_tests/fixtures/agentNonValueWidgetRig.ts +++ b/browser_tests/fixtures/agentNonValueWidgetRig.ts @@ -33,7 +33,6 @@ interface NonValueWidgetRigConfig { nodeDefs: Record samplerNodeId: number seed: WorkflowJSON - socketSid: string threadId: string visibleNodeId: number workflowId: string @@ -65,7 +64,6 @@ export class AgentNonValueWidgetRig { page, config.workflowId, host, - config.socketSid, 'apply' ) await hostSocket.install() diff --git a/browser_tests/fixtures/agentPanelFixture.ts b/browser_tests/fixtures/agentPanelFixture.ts index e31624e45ae..18e1d9dc7f1 100644 --- a/browser_tests/fixtures/agentPanelFixture.ts +++ b/browser_tests/fixtures/agentPanelFixture.ts @@ -17,6 +17,7 @@ import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/w import type { ComfyNodeDef } from '@/schemas/nodeDefSchema' import type { AgentTurnAccepted } from '@/workbench/extensions/agent/schemas/agentApiSchema' +import { mockAgentIdentity } from '@e2e/fixtures/agentSocket' import { cloudAppFixture, waitForCloudApp } from '@e2e/fixtures/cloudAppFixture' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' import { DEPLOY_ACTION_SEEN_SETTINGS } from '@e2e/fixtures/constants/workflowActions' @@ -90,6 +91,7 @@ async function mockAgentBoot( objectInfo }) await mockBilling(page, { billingStatus }) + await mockAgentIdentity(page) const storedConsent: GlobalSetting = { key: AGENT_CONSENT_SETTING_ID, value: true, diff --git a/browser_tests/fixtures/agentPromptHistoryFixture.ts b/browser_tests/fixtures/agentPromptHistoryFixture.ts index e2807f89f00..a3a83e99644 100644 --- a/browser_tests/fixtures/agentPromptHistoryFixture.ts +++ b/browser_tests/fixtures/agentPromptHistoryFixture.ts @@ -46,7 +46,7 @@ export const promptHistoryTest = base.extend<{ agentPanel: async ({ page }, use) => { await use(new AgentPanel(page)) }, - promptHistory: async ({ page, workflowSelection, getWebSocket }, use) => { + promptHistory: async ({ page, workflowSelection, getAgentSocket }, use) => { // Workflow selection boots the app before these agent-specific routes. void workflowSelection const requests: AgentPostMessageRequest[] = [] @@ -137,7 +137,7 @@ export const promptHistoryTest = base.extend<{ type: 'agent_message_done', data: { message_id: messageId, thread_id: threadId } } - const socket = await getWebSocket() + const socket = await getAgentSocket() socket.send(JSON.stringify(done)) } ) diff --git a/browser_tests/fixtures/agentSharedHostFixture.ts b/browser_tests/fixtures/agentSharedHostFixture.ts index 012dae8fb73..af8e30d092f 100644 --- a/browser_tests/fixtures/agentSharedHostFixture.ts +++ b/browser_tests/fixtures/agentSharedHostFixture.ts @@ -20,12 +20,11 @@ export class AgentSharedHostFixture { this.host = new HostDoc(workflowId, seed, catalog) } - async attach(page: Page, socketSid: string): Promise { + async attach(page: Page): Promise { const socket = new AgentFollowerHostSocket( page, this.workflowId, this.host, - socketSid, 'apply', (frame) => this.broadcast(frame) ) diff --git a/browser_tests/fixtures/agentSocket.ts b/browser_tests/fixtures/agentSocket.ts new file mode 100644 index 00000000000..b51b85e127b --- /dev/null +++ b/browser_tests/fixtures/agentSocket.ts @@ -0,0 +1,48 @@ +import type { AgentIdentity } from '@comfyorg/ingest-types' +import type { Page } from '@playwright/test' + +import { WEB_SESSION } from '@e2e/fixtures/data/webSession' +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' + +export const AGENT_SOCKET_URL = /\/api\/agent\/events(?:\?|$)/ + +export const AGENT_FRAME_HANDLED_EVENT = 'agent-socket-frame-handled' + +export async function announceHandledAgentFrames(page: Page): Promise { + await page.addInitScript( + ({ urlPattern, eventName }) => { + const agentSocketUrl = new RegExp(urlPattern) + class AnnouncingWebSocket extends window.WebSocket { + constructor(url: string | URL, protocols?: string | string[]) { + super(url, protocols) + if (!agentSocketUrl.test(String(url))) return + this.addEventListener('message', ({ data }) => { + setTimeout(() => + document.dispatchEvent( + new CustomEvent(eventName, { detail: data }) + ) + ) + }) + } + } + window.WebSocket = AnnouncingWebSocket + }, + { + urlPattern: AGENT_SOCKET_URL.source, + eventName: AGENT_FRAME_HANDLED_EVENT + } + ) +} + +export async function mockAgentIdentity( + page: Page, + userId = WEB_SESSION.user.id +): Promise { + const identity: AgentIdentity = { + workspace_id: 'ws-personal', + user_id: userId + } + await page.route('**/api/agent/identity', (route) => + route.fulfill(jsonRoute(identity)) + ) +} diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts index 6fc191088d7..96631a41c10 100644 --- a/browser_tests/fixtures/agentTurnLockFixture.ts +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -520,8 +520,8 @@ export class AgentTurnLockHarness { private readonly server: TurnLockServer, private readonly finishSave: (success: boolean) => void, private readonly savedPaths: () => number, - private readonly getWebSocket: () => Promise, - private readonly nextWebSocket: () => Promise + private readonly getAgentSocket: () => Promise, + private readonly nextAgentSocket: () => Promise ) { this.agentPanel = new AgentPanel(page) this.panel = this.agentPanel.root @@ -606,7 +606,7 @@ export class AgentTurnLockHarness { /** Sends a prompt and streams it to the point where a user sees work happening. */ async startTurn(prompt: string): Promise { - const live = await this.getWebSocket() + const live = await this.getAgentSocket() await this.composer.fill(prompt) await this.sendButton.click() // Stop is NOT an ack: Composer renders it from `isSending`, which @@ -765,7 +765,7 @@ export class AgentTurnLockHarness { /** The socket the client is currently on, with no drop. */ async liveSocket(): Promise { - return this.getWebSocket() + return this.getAgentSocket() } /** Drops the live socket and resolves with the one the client reconnects on. */ @@ -773,8 +773,8 @@ export class AgentTurnLockHarness { // Resolve the live route first: with no socket open yet both calls would // queue on the same waiter and hand back the same route, so the close // below would kill the one returned as the reconnect. - const live = await this.getWebSocket() - const reconnected = this.nextWebSocket() + const live = await this.getAgentSocket() + const reconnected = this.nextAgentSocket() await live.close() return reconnected } @@ -786,7 +786,7 @@ export const agentTurnLockTest = mergeTests( webSocketFixture ).extend<{ turnLock: AgentTurnLockHarness }>({ turnLock: async ( - { page, workflowSelection, getWebSocket, nextWebSocket }, + { page, workflowSelection, getAgentSocket, nextAgentSocket }, use ) => { const server = new TurnLockServer() @@ -797,8 +797,8 @@ export const agentTurnLockTest = mergeTests( server, workflowSelection.finishSave, () => workflowSelection.savedPaths.length, - getWebSocket, - nextWebSocket + getAgentSocket, + nextAgentSocket ) ) } diff --git a/browser_tests/fixtures/agentTwoSessionCrdtFixture.ts b/browser_tests/fixtures/agentTwoSessionCrdtFixture.ts index 84b12f75aac..fa733a83cad 100644 --- a/browser_tests/fixtures/agentTwoSessionCrdtFixture.ts +++ b/browser_tests/fixtures/agentTwoSessionCrdtFixture.ts @@ -24,6 +24,7 @@ import { } from '@e2e/fixtures/agentPanelFixture' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' +import { AGENT_SOCKET_URL } from '@e2e/fixtures/agentSocket' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' import { Topbar } from '@e2e/fixtures/components/Topbar' import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers' @@ -89,9 +90,11 @@ export class AgentTwoSessionCrdtHarness { async boot(): Promise { await this.mockAgentApi() - await this.page.routeWebSocket(/\/ws(\?|$)/, (socket) => { + await this.page.routeWebSocket(AGENT_SOCKET_URL, (socket) => { this.socket = socket socket.onMessage((raw) => this.onClientFrame(raw)) + }) + await this.page.routeWebSocket(/\/ws(\?|$)/, (socket) => { socket.send( JSON.stringify({ type: 'status', diff --git a/browser_tests/fixtures/data/agent/README.md b/browser_tests/fixtures/data/agent/README.md index dd3abe53823..891acd8590b 100644 --- a/browser_tests/fixtures/data/agent/README.md +++ b/browser_tests/fixtures/data/agent/README.md @@ -181,7 +181,9 @@ to bind the workflow through. ## Capture -The recorder keeps the `/ws` frames of the thread with their receipt times and, +The recorder keeps the agent-socket frames of the thread (what the panel reads +on `/api/agent/events`; the replay serves them there through +`AgentFollowerHostSocket`) with their receipt times and, after the last turn completes, reads each turn's audit rows itself: the parent tool-call rows with their child op ids and statuses, plus the draft as it stands at the end of the thread (`readRows` in `scripts/agentConversationRecord.ts` diff --git a/browser_tests/fixtures/data/agent/agentCrdtMultiAutogrowRealignFixture.ts b/browser_tests/fixtures/data/agent/agentCrdtMultiAutogrowRealignFixture.ts index 313e38e71f6..a445fe90f03 100644 --- a/browser_tests/fixtures/data/agent/agentCrdtMultiAutogrowRealignFixture.ts +++ b/browser_tests/fixtures/data/agent/agentCrdtMultiAutogrowRealignFixture.ts @@ -14,7 +14,6 @@ export const TARGET_ID = String(TARGET_NODE_ID) export const WORKFLOW_ID = '4c1e9f2a-6b3d-4a7e-8f01-2c3d4e5f6a7b' export const THREAD_ID = 'a2b3c4d5-6e7f-4a8b-9c0d-1e2f3a4b5c6d' export const MESSAGE_ID = 'f1e2d3c4-b5a6-4978-8c6d-5e4f3a2b1c0d' -export const SOCKET_SID = '3f1c9d7a-2b4e-4a6f-8c9d-0e1f2a3b4c5d' // Link ids, one per wire in the saved graph below -- internal to this // module; consumers read them back off `EXPECTED_TARGETS`. diff --git a/browser_tests/fixtures/helpers/AgentRemoteApplyHarness.ts b/browser_tests/fixtures/helpers/AgentRemoteApplyHarness.ts index 690b9efbc1b..243c9864161 100644 --- a/browser_tests/fixtures/helpers/AgentRemoteApplyHarness.ts +++ b/browser_tests/fixtures/helpers/AgentRemoteApplyHarness.ts @@ -25,7 +25,6 @@ import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' const WORKFLOW_ID = 'f2b4c6d8-1a3e-4b5c-9d7e-0f1a2b3c4d5e' const THREAD_ID = '5c4b3a29-8d7e-4f60-9a1b-2c3d4e5f6a7b' const MESSAGE_ID = '9f8e7d6c-5b4a-4392-8170-6f5e4d3c2b1a' -const SOCKET_SID = '3a2b1c0d-9e8f-4a7b-8c6d-5e4f3a2b1c0d' /** * Drives one agent turn that edits the canvas, with the doc host under the @@ -53,12 +52,7 @@ export class AgentRemoteApplyHarness { readonly workSummary: Locator constructor(private readonly page: Page) { - this.hostSocket = new AgentFollowerHostSocket( - page, - WORKFLOW_ID, - this.host, - SOCKET_SID - ) + this.hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, this.host) this.agentPanel = new AgentPanel(page) this.vueNodes = new VueNodeHelpers(page) this.panel = this.agentPanel.root diff --git a/browser_tests/fixtures/helpers/MultiAutogrowRealignHarness.ts b/browser_tests/fixtures/helpers/MultiAutogrowRealignHarness.ts index 9b1b8b155a4..bdad941c43b 100644 --- a/browser_tests/fixtures/helpers/MultiAutogrowRealignHarness.ts +++ b/browser_tests/fixtures/helpers/MultiAutogrowRealignHarness.ts @@ -37,7 +37,6 @@ import { EXPECTED_TARGETS, MESSAGE_ID, NODE_TYPE, - SOCKET_SID, SOURCE_NODE_ID, SOURCE_NODE_TYPE, SPARE_SLOTS, @@ -114,7 +113,6 @@ export class MultiAutogrowRealignHarness { page, WORKFLOW_ID, this.host, - SOCKET_SID, 'apply' ) this.topbar = new Topbar(page) diff --git a/browser_tests/fixtures/load3dAgentFixture.ts b/browser_tests/fixtures/load3dAgentFixture.ts index 283f7a1ca45..680c290f3b9 100644 --- a/browser_tests/fixtures/load3dAgentFixture.ts +++ b/browser_tests/fixtures/load3dAgentFixture.ts @@ -22,7 +22,6 @@ import { assetPath } from '@e2e/fixtures/utils/paths' import { Load3DHelper } from '@e2e/tests/load3d/Load3DHelper' const WORKFLOW_ID = 'a81718a4-02ae-41e6-ae85-c33b7bb880f6' -const SOCKET_SID = '5b0e2c9a-6f1d-4a83-9c27-3e4f5a6b7c8d' const NODE_ID = '1' const PANEL_MOUNT_TIMEOUT = 30_000 @@ -127,7 +126,7 @@ async function uploadedImageBytes(request: Request): Promise { /** * One Load3D node bound to an agent workflow, driven the way production is: - * the panel opens the agent's tab, the follower subscribes over `/ws`, and the + * the panel opens the agent's tab, the follower subscribes over the agent socket, and the * fake host applies recorded `set_widget` ops that reach the node through the * doc frames the real client parses. */ @@ -144,12 +143,7 @@ class Load3dAgentHarness { private readonly uploadsByName = new Map() constructor(private readonly page: Page) { - this.hostSocket = new AgentFollowerHostSocket( - page, - WORKFLOW_ID, - this.host, - SOCKET_SID - ) + this.hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, this.host) this.viewer = new Load3DHelper(page.locator(`[data-node-id="${NODE_ID}"]`)) } diff --git a/browser_tests/fixtures/ws.ts b/browser_tests/fixtures/ws.ts index 716e9272615..cbc38160e20 100644 --- a/browser_tests/fixtures/ws.ts +++ b/browser_tests/fixtures/ws.ts @@ -1,67 +1,87 @@ +import type { BrowserContext, WebSocketRoute } from '@playwright/test' + +import { AGENT_SOCKET_URL } from '@e2e/fixtures/agentSocket' import { networkIsolationFixture as base } from '@e2e/fixtures/networkIsolationFixture' -import type { WebSocketRoute } from '@playwright/test' /** - * The `/ws` connections the page opens, in order. A reconnect routes a second - * time, so a test that drops the socket needs the route the client opened - * *after* the drop, not the dead one it already holds. + * The connections the page opens on one routed socket URL, in order. A + * reconnect routes a second time, so a test that drops the socket needs the + * route the client opened *after* the drop, not the dead one it already holds. */ interface WebSocketTracker { current: () => WebSocketRoute | undefined waitForNext: () => Promise } -function createWebSocketRouteHandler( - connectWebSocketToServer: boolean, - onRouted: (ws: WebSocketRoute) => void -) { - return (ws: WebSocketRoute) => { - if (connectWebSocketToServer) { +async function trackWebSocket( + context: BrowserContext, + url: RegExp, + { connectToServer }: { connectToServer: boolean } +): Promise { + let current: WebSocketRoute | undefined + const waiters = new Set<(ws: WebSocketRoute) => void>() + + await context.routeWebSocket(url, (ws) => { + if (connectToServer) { const server = ws.connectToServer() server.onMessage((message) => { ws.send(message) }) + } else { + ws.onClose(() => { + if (current === ws) current = undefined + }) } + current = ws + const pending = [...waiters] + waiters.clear() + for (const resolve of pending) resolve(ws) + }) - onRouted(ws) + return { + current: () => current, + waitForNext: () => + new Promise((resolve) => waiters.add(resolve)) } } +function currentOrNext(tracker: WebSocketTracker): Promise { + const open = tracker.current() + return open ? Promise.resolve(open) : tracker.waitForNext() +} + export const webSocketFixture = base.extend<{ connectWebSocketToServer: boolean getWebSocket: () => Promise nextWebSocket: () => Promise webSocketTracker: WebSocketTracker + getAgentSocket: () => Promise + nextAgentSocket: () => Promise + agentSocketTracker: WebSocketTracker }>({ connectWebSocketToServer: [true, { option: true }], webSocketTracker: [ async ({ context, connectWebSocketToServer }, use) => { - let current: WebSocketRoute | undefined - const waiters = new Set<(ws: WebSocketRoute) => void>() - - await context.routeWebSocket( - /\/ws/, - createWebSocketRouteHandler(connectWebSocketToServer, (ws) => { - current = ws - const pending = [...waiters] - waiters.clear() - for (const resolve of pending) resolve(ws) + await use( + await trackWebSocket(context, /\/ws/, { + connectToServer: connectWebSocketToServer + }) + ) + }, + { auto: true } + ], + agentSocketTracker: [ + async ({ context }, use) => { + await use( + await trackWebSocket(context, AGENT_SOCKET_URL, { + connectToServer: false }) ) - - await use({ - current: () => current, - waitForNext: () => - new Promise((resolve) => waiters.add(resolve)) - }) }, { auto: true } ], getWebSocket: async ({ webSocketTracker }, use) => { - await use(() => { - const open = webSocketTracker.current() - return open ? Promise.resolve(open) : webSocketTracker.waitForNext() - }) + await use(() => currentOrNext(webSocketTracker)) }, /** * Resolves with the NEXT socket the page opens. Call it before closing the @@ -69,5 +89,11 @@ export const webSocketFixture = base.extend<{ */ nextWebSocket: async ({ webSocketTracker }, use) => { await use(() => webSocketTracker.waitForNext()) + }, + getAgentSocket: async ({ agentSocketTracker }, use) => { + await use(() => currentOrNext(agentSocketTracker)) + }, + nextAgentSocket: async ({ agentSocketTracker }, use) => { + await use(() => agentSocketTracker.waitForNext()) } }) diff --git a/browser_tests/tests/agent/agentCanvasBlanksDuringRunTurn.spec.ts b/browser_tests/tests/agent/agentCanvasBlanksDuringRunTurn.spec.ts index 582a8bf3398..4f0f7ed463e 100644 --- a/browser_tests/tests/agent/agentCanvasBlanksDuringRunTurn.spec.ts +++ b/browser_tests/tests/agent/agentCanvasBlanksDuringRunTurn.spec.ts @@ -1,4 +1,4 @@ -import type { Page, WebSocketRoute } from '@playwright/test' +import type { Page } from '@playwright/test' import { expect } from '@playwright/test' import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' @@ -10,7 +10,6 @@ import type { UserDataFullInfo } from '@/platform/remote/comfyui/types' import enMessages from '@/locales/en/main.json' with { type: 'json' } import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' -import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' import { STALE_AFTER_MS, SUBSCRIBE_CATCHUP_GRACE_MS @@ -20,6 +19,7 @@ import { agentTest as test, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' @@ -63,7 +63,6 @@ import { loadSeedIntoActiveTab } from '@e2e/fixtures/utils/seedActiveTab' const WORKFLOW_ID = 'b7e2f1a4-9c3d-4e5f-8a6b-1d2c3e4f5a6b' const THREAD_ID = 'd4c5b6a7-8e9f-4a1b-9c2d-3e4f5a6b7c8d' const MESSAGE_ID = '1a2b3c4d-5e6f-4a7b-8c9d-0e1f2a3b4c5e' -const SOCKET_SID = '9f8e7d6c-5b4a-4c3d-8e1f-2a3b4c5d6e7f' const CATALOG: WidgetCatalog = { types: { MarkdownNote: { widget_order: ['text'] } } @@ -103,42 +102,6 @@ const SEED: WorkflowJSON = { const SEND_LABEL = enMessages.agent.send -/** - * Parses a raw `/ws` message down to its `doc_subscribe` payload, or `null` - * if the frame is malformed or is some other frame type. Extracted from the - * mock's `onMessage` handler to keep that handler's branching to the - * post-parse decisions (reset-gating, catch-up dropping). - */ -function parseDocSubscribeData( - raw: Buffer | string -): Record | null { - const frame: unknown = JSON.parse(raw.toString()) - if (typeof frame !== 'object' || frame === null) return null - const { type, data } = frame as { type?: unknown; data?: unknown } - if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) - return null - return data as Record -} - -/** - * Validates a parsed `doc_subscribe` payload against the workflow under - * test and extracts the state vector the mock needs to build a catch-up - * frame. Returns `null` for any other workflow's subscribe (there are - * none in this suite, but the guard mirrors the shape a real backend would - * check). - */ -function parseDocSubscribeFields( - data: Record -): { stateVectorB64: string } | null { - const { workflow_id, state_vector_b64 } = data as { - workflow_id?: unknown - state_vector_b64?: unknown - } - if (workflow_id !== WORKFLOW_ID || typeof state_vector_b64 !== 'string') - return null - return { stateVectorB64: state_vector_b64 } -} - /** * The node carries a stamp written before the reset, on an attribute Vue does * not manage, so only a fresh element loses it. Both checks below read it; @@ -194,19 +157,18 @@ async function driveThroughDocReset( const host = new HostDoc(WORKFLOW_ID, SEED, CATALOG) const vueNodes = new VueNodeHelpers(page) - let socket: WebSocketRoute | null = null let resetSent = false let resubscribeCountAfterReset = 0 - const send = (frame: AgentWsEvent | HostFrame): void => { - if ( - (frame.type.startsWith('doc_') || frame.type === 'awareness') && - parseServerDocFrame(frame) === null - ) - throw new Error(`frame ${frame.type} is not a valid doc frame`) - if (!socket) throw new Error('the app has not opened /ws yet') - socket.send(JSON.stringify(frame)) - } + const hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, host) + hostSocket.answerSubscribesWith((stateVector) => { + if (!resetSent) return [host.subscribed(), host.catchUp(stateVector)] + resubscribeCountAfterReset += 1 + return dropCatchUpAfterReset(resubscribeCountAfterReset) + ? [host.subscribed()] + : [host.subscribed(), host.catchUp(stateVector)] + }) + const send = (frame: AgentWsEvent | HostFrame): void => hostSocket.send(frame) await page.route('**/api/agent/threads', (route) => route.fulfill(jsonRoute({ threads: [] })) @@ -228,32 +190,7 @@ async function driveThroughDocReset( await page.route('**/api/agent/threads/*/asks/*/answer', (route) => route.fulfill(jsonRoute({ ok: true })) ) - await page.routeWebSocket(/\/ws/, (ws) => { - socket = ws - ws.send( - JSON.stringify({ - type: 'status', - data: { - status: { exec_info: { queue_remaining: 0 } }, - sid: SOCKET_SID - } - }) - ) - ws.onMessage((raw) => { - const data = parseDocSubscribeData(raw) - if (data === null) return - const fields = parseDocSubscribeFields(data) - if (fields === null) return - send(host.subscribed()) - if (!resetSent) { - send(host.catchUp(fields.stateVectorB64)) - return - } - resubscribeCountAfterReset += 1 - if (!dropCatchUpAfterReset(resubscribeCountAfterReset)) - send(host.catchUp(fields.stateVectorB64)) - }) - }) + await hostSocket.install() await bootAgentApp(page, true, { settings: { @@ -316,7 +253,7 @@ async function driveThroughDocReset( .click() await expect(picker).toHaveText('Unsaved Workflow') - await expect.poll(() => socket !== null).toBe(true) + await expect.poll(() => hostSocket.connectionCount()).toBeGreaterThan(0) const composer = panel.getByRole('textbox', { name: AGENT_COMPOSER_LABEL }) await composer.fill('Run the workflow.') diff --git a/browser_tests/tests/agent/agentCanvasLagsToolCallCompletion.spec.ts b/browser_tests/tests/agent/agentCanvasLagsToolCallCompletion.spec.ts index 3ad0c660311..3f106fbead6 100644 --- a/browser_tests/tests/agent/agentCanvasLagsToolCallCompletion.spec.ts +++ b/browser_tests/tests/agent/agentCanvasLagsToolCallCompletion.spec.ts @@ -1,4 +1,4 @@ -import type { Page, WebSocketRoute } from '@playwright/test' +import type { Page } from '@playwright/test' import { expect } from '@playwright/test' import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' @@ -6,15 +6,13 @@ import type { WorkflowListResponse } from '@comfyorg/ingest-types' import type { UserDataFullInfo } from '@/platform/remote/comfyui/types' import enMessages from '@/locales/en/main.json' with { type: 'json' } -import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' -import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' import { agentTest as test, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' -import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers' import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' @@ -84,7 +82,6 @@ const THREAD_ID = 'f0e1d2c3-b4a5-4968-8172-6354a3b2c1d0' const MESSAGE_ID = '1e2d3c4b-5a69-4788-9061-52f3e4d5c6b7' const ADDED_NODE_ID = 4242 const NODE_TEXT = 'added while the doc broadcast lags' -const SOCKET_SID = 'a9b8c7d6-e5f4-4321-8a9b-0c1d2e3f4a5b' const CATALOG: WidgetCatalog = { types: { MarkdownNote: { widget_order: ['text'] } } @@ -93,22 +90,6 @@ const SEED: WorkflowJSON = { nodes: [], links: [] } const SEND_LABEL = enMessages.agent.send -/** The follower's `doc_subscribe` payload for the workflow under test, or null. */ -function subscribeStateVectorOf(raw: Buffer | string): string | null { - const frame: unknown = JSON.parse(raw.toString()) - if (typeof frame !== 'object' || frame === null) return null - const { type, data } = frame as { type?: unknown; data?: unknown } - if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) - return null - const { workflow_id, state_vector_b64 } = data as { - workflow_id?: unknown - state_vector_b64?: unknown - } - if (workflow_id !== WORKFLOW_ID || typeof state_vector_b64 !== 'string') - return null - return state_vector_b64 -} - /** * Drives one "add a note to the canvas" turn through to the agent reporting * it done -- the tool call's own completion AND the turn's "Worked for Xs" @@ -139,16 +120,7 @@ async function driveThroughToolCallDone( const host = new HostDoc(WORKFLOW_ID, SEED, CATALOG) const vueNodes = new VueNodeHelpers(page) - let socket: WebSocketRoute | null = null - const send = (frame: AgentWsEvent | HostFrame): void => { - if ( - (frame.type.startsWith('doc_') || frame.type === 'awareness') && - parseServerDocFrame(frame) === null - ) - throw new Error(`frame ${frame.type} is not a valid doc frame`) - if (!socket) throw new Error('the app has not opened /ws yet') - socket.send(JSON.stringify(frame)) - } + const hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, host) await page.route('**/api/agent/threads', (route) => route.fulfill(jsonRoute({ threads: [] })) @@ -167,24 +139,7 @@ async function driveThroughToolCallDone( } return route.fulfill(jsonRoute([])) }) - await page.routeWebSocket(/\/ws/, (ws) => { - socket = ws - ws.send( - JSON.stringify({ - type: 'status', - data: { - status: { exec_info: { queue_remaining: 0 } }, - sid: SOCKET_SID - } - }) - ) - ws.onMessage((raw) => { - const stateVector = subscribeStateVectorOf(raw) - if (stateVector === null) return - send(host.subscribed()) - send(host.catchUp(stateVector)) - }) - }) + await hostSocket.install() await bootAgentApp(page, true, { settings: { @@ -246,7 +201,7 @@ async function driveThroughToolCallDone( .click() await expect(picker).toHaveText('Unsaved Workflow') - await expect.poll(() => socket !== null).toBe(true) + await expect.poll(() => hostSocket.connectionCount()).toBeGreaterThan(0) const composer = panel.getByRole('textbox', { name: AGENT_COMPOSER_LABEL }) await composer.fill('Add a note to the canvas.') @@ -255,7 +210,7 @@ async function driveThroughToolCallDone( panel.getByText('Add a note to the canvas.').first() ).toBeVisible() - send({ + hostSocket.send({ type: 'agent_active_tab', data: { workflow_id: WORKFLOW_ID, @@ -296,12 +251,12 @@ async function driveThroughToolCallDone( // while the tool is still (from the chat frames' perspective) running -- // the healthy-doc-host case most reviewers flagged as the lost-wakeup risk // for notifyCanvasCaughtUp()'s edge triggering. - if (order === 'docUpdateBeforeToolCall') send(update) + if (order === 'docUpdateBeforeToolCall') hostSocket.send(update) // The tool-call-completion affordance: `agentEventTransport.ts`'s `ingest()` // flips this tool part `done` the instant this frame is read, purely from // `status`, with nothing checked on the CRDT doc side (see file header). - send({ + hostSocket.send({ type: 'agent_tool_call', data: { tool_call_id: 'call-add-node', @@ -312,7 +267,7 @@ async function driveThroughToolCallDone( message_id: MESSAGE_ID } }) - send({ + hostSocket.send({ type: 'agent_message_delta', data: { delta: 'Added a note to the canvas.', @@ -320,7 +275,7 @@ async function driveThroughToolCallDone( message_id: MESSAGE_ID } }) - send({ + hostSocket.send({ type: 'agent_message_done', data: { thread_id: THREAD_ID, message_id: MESSAGE_ID, usage: null } }) @@ -336,7 +291,7 @@ async function driveThroughToolCallDone( // A no-op once `order` already sent it above -- resending the same // update twice is not part of what either ordering means to exercise. sendDelayedDocUpdate: () => { - if (order !== 'docUpdateBeforeToolCall') send(update) + if (order !== 'docUpdateBeforeToolCall') hostSocket.send(update) } } } diff --git a/browser_tests/tests/agent/agentCrdtRejectedNodeAdd.spec.ts b/browser_tests/tests/agent/agentCrdtRejectedNodeAdd.spec.ts index 9d26378d562..bf886421c07 100644 --- a/browser_tests/tests/agent/agentCrdtRejectedNodeAdd.spec.ts +++ b/browser_tests/tests/agent/agentCrdtRejectedNodeAdd.spec.ts @@ -48,7 +48,6 @@ import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers' */ const WORKFLOW_ID = 'f18210000-0000-4000-8000-000000000001' -const SOCKET_SID = 'f18210000-0000-4000-8000-000000000002' const MESSAGE_ID = 'f18210000-0000-4000-8000-000000000003' const THREAD_ID = 'f18210000-0000-4000-8000-000000000004' @@ -284,7 +283,6 @@ test.describe( page, WORKFLOW_ID, host, - SOCKET_SID, 'hold' ) await hostSocket.install() diff --git a/browser_tests/tests/agent/agentCreditsExhaustionLifecycleMocks.ts b/browser_tests/tests/agent/agentCreditsExhaustionLifecycleMocks.ts index 9cbc82305bc..7ea547e7bfd 100644 --- a/browser_tests/tests/agent/agentCreditsExhaustionLifecycleMocks.ts +++ b/browser_tests/tests/agent/agentCreditsExhaustionLifecycleMocks.ts @@ -16,7 +16,7 @@ class AgentCreditsLifecycleFixture { private readonly page: Page, private readonly agentPanel: AgentPanel, private readonly acceptedTurns: AgentTurnAccepted[], - private readonly ws: WebSocketRoute, + private readonly getAgentSocket: () => Promise, private readonly setFundingState: (state: AgentFundingState) => void ) {} @@ -55,7 +55,8 @@ class AgentCreditsLifecycleFixture { } } } - this.ws.send(JSON.stringify(doneEvent)) + const ws = await this.getAgentSocket() + ws.send(JSON.stringify(doneEvent)) await billingRefresh } } @@ -66,16 +67,15 @@ export const test = base.extend<{ creditsLifecycle: AgentCreditsLifecycleFixture }>({ creditsLifecycle: async ( - { acceptedTurns, agentBilling, agentPanel, getWebSocket, page }, + { acceptedTurns, agentBilling, agentPanel, getAgentSocket, page }, use ) => { - const ws = await getWebSocket() await use( new AgentCreditsLifecycleFixture( page, agentPanel, acceptedTurns, - ws, + getAgentSocket, (state) => agentBilling.setFundingState(state) ) ) diff --git a/browser_tests/tests/agent/agentDuplicateInsertOpaqueWidgetWrite.spec.ts b/browser_tests/tests/agent/agentDuplicateInsertOpaqueWidgetWrite.spec.ts index ec6e50c530f..7a6de34a292 100644 --- a/browser_tests/tests/agent/agentDuplicateInsertOpaqueWidgetWrite.spec.ts +++ b/browser_tests/tests/agent/agentDuplicateInsertOpaqueWidgetWrite.spec.ts @@ -243,7 +243,7 @@ async function ksamplerNodeIds(page: Page): Promise { */ async function driveThroughDuplicateInsert( page: Page, - getWebSocket: () => Promise + getAgentSocket: () => Promise ): Promise { await page.setViewportSize({ width: 1920, height: 1280 }) await page.addInitScript(() => { @@ -262,12 +262,12 @@ async function driveThroughDuplicateInsert( } }) await loadIntoBootWorkflow(page, BLANK_WORKFLOW) - const socket = await getWebSocket() - const outboundFrames: string[] = [] - socket.onMessage((message) => outboundFrames.push(String(message))) const agentPanel = new AgentPanel(page) await agentPanel.open() + const socket = await getAgentSocket() + const outboundFrames: string[] = [] + socket.onMessage((message) => outboundFrames.push(String(message))) await agentPanel.selectWorkflow() await agentPanel.sendMessage('Create a z-turbo image workflow') @@ -326,9 +326,9 @@ interface RejectedWidgetEditHandles extends DuplicateInsertHandles { */ async function driveThroughRejectedWidgetEdit( page: Page, - getWebSocket: () => Promise + getAgentSocket: () => Promise ): Promise { - const handles = await driveThroughDuplicateInsert(page, getWebSocket) + const handles = await driveThroughDuplicateInsert(page, getAgentSocket) const { vueNodes, socket, outboundFrames, copyBNodeId } = handles // The two copies sit at IDENTICAL canvas coordinates (the overlap under @@ -411,10 +411,10 @@ test.describe( test('two insert_workflow calls with different op ids leave two fully overlapping, visually indistinguishable copies of the template', async ({ page, - getWebSocket + getAgentSocket }) => { const { vueNodes, copyANodeId, copyBNodeId } = - await driveThroughDuplicateInsert(page, getWebSocket) + await driveThroughDuplicateInsert(page, getAgentSocket) expect( await page.evaluate( @@ -464,10 +464,10 @@ test.describe( test('a rejected widget edit is reported and rolled back to the value the shared document kept', async ({ page, toast, - getWebSocket + getAgentSocket }) => { const { host, seedInput, copyBNodeId } = - await driveThroughRejectedWidgetEdit(page, getWebSocket) + await driveThroughRejectedWidgetEdit(page, getAgentSocket) // The refused register is put back from the document, so the widget // the human is looking at and the document a subsequent run would read diff --git a/browser_tests/tests/agent/agentDuplicateWidgetOccurrenceFollower.spec.ts b/browser_tests/tests/agent/agentDuplicateWidgetOccurrenceFollower.spec.ts index f0b0b7e829b..971e828d3d6 100644 --- a/browser_tests/tests/agent/agentDuplicateWidgetOccurrenceFollower.spec.ts +++ b/browser_tests/tests/agent/agentDuplicateWidgetOccurrenceFollower.spec.ts @@ -40,7 +40,6 @@ const TYPED_VALUE = 'typed by the user' const WORKFLOW_ID = '2d9f5a71-4c3b-4e8d-9a06-7b1c2d3e4f50' const THREAD_ID = '3e0a6b82-5d4c-4f9e-8b17-8c2d3e4f5a61' const MESSAGE_ID = '4f1b7c93-6e5d-4a0f-9c28-9d3e4f5a6b72' -const SOCKET_SID = '5a2c8d04-7f6e-4b10-8d39-0e4f5a6b7c83' const nodeDef: ComfyNodeDef = { name: NODE_TYPE, @@ -189,7 +188,6 @@ test.describe( page, WORKFLOW_ID, host, - SOCKET_SID, // The human's own edit must reach the document through the real // applier, not be held: the last step asserts what the document kept. 'apply' diff --git a/browser_tests/tests/agent/agentDynamicComboSetWidgetSilentlyDropped.spec.ts b/browser_tests/tests/agent/agentDynamicComboSetWidgetSilentlyDropped.spec.ts index fe8e2799849..eb58505a5b9 100644 --- a/browser_tests/tests/agent/agentDynamicComboSetWidgetSilentlyDropped.spec.ts +++ b/browser_tests/tests/agent/agentDynamicComboSetWidgetSilentlyDropped.spec.ts @@ -112,8 +112,6 @@ const seed: WorkflowJSON = { version: 0.4 } -const SOCKET_SID = '9c8b7a6f-5e4d-4c3b-8a1f-0d9e8c7b6a5f' - test.describe( 'Agent set_widget on a dynamic-combo widget', { tag: ['@cloud', '@agent', '@vue-nodes'] }, @@ -128,12 +126,7 @@ test.describe( ) const host = new HostDoc(WORKFLOW_ID, seed, catalog) - const hostSocket = new AgentFollowerHostSocket( - page, - WORKFLOW_ID, - host, - SOCKET_SID - ) + const hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, host) await hostSocket.install() const threadList: AgentThreadListResponse = { diff --git a/browser_tests/tests/agent/agentFirstMintResetSweepsTemplate.spec.ts b/browser_tests/tests/agent/agentFirstMintResetSweepsTemplate.spec.ts index f62e49e3358..4191fd7f900 100644 --- a/browser_tests/tests/agent/agentFirstMintResetSweepsTemplate.spec.ts +++ b/browser_tests/tests/agent/agentFirstMintResetSweepsTemplate.spec.ts @@ -1,15 +1,12 @@ -import type { WebSocketRoute } from '@playwright/test' import { expect } from '@playwright/test' import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' import type { DocResetFrame } from '@comfyorg/ingest-types' import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' -import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' -import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' -import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { agentTest as test, bootAgentApp, @@ -78,46 +75,6 @@ const TEMPLATE_GRAPH: ComfyWorkflowJSON = { const EMPTY_HOST_SEED: WorkflowJSON = { nodes: [], links: [] } const EMPTY_CATALOG: WidgetCatalog = { types: {} } -/** `true` for a `doc_subscribe` frame addressed to `WORKFLOW_ID`; never throws inside the socket mock. */ -function parseDocSubscribeForWorkflow(raw: Buffer | string): boolean { - let frame: unknown - try { - frame = JSON.parse(raw.toString()) - } catch { - return false - } - if (typeof frame !== 'object' || frame === null) return false - const { type, data } = frame as { type?: unknown; data?: unknown } - if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) - return false - const { workflow_id } = data as { workflow_id?: unknown } - return workflow_id === WORKFLOW_ID -} - -/** Awaits the Nth `doc_subscribe` for `WORKFLOW_ID`: 1 is the turn's binding, 2 the post-reset resubscribe. */ -function createSubscribeTracker(): { - recordSubscribe: () => void - waitForCount: (target: number) => Promise -} { - let count = 0 - const waiters: { target: number; resolve: () => void }[] = [] - return { - recordSubscribe: () => { - count++ - for (let i = waiters.length - 1; i >= 0; i--) { - if (count >= waiters[i].target) { - waiters[i].resolve() - waiters.splice(i, 1) - } - } - }, - waitForCount: (target) => { - if (count >= target) return Promise.resolve() - return new Promise((resolve) => waiters.push({ target, resolve })) - } - } -} - test.describe( 'Agent first turn on a template that has never had a CRDT doc', { tag: ['@cloud', '@agent', '@vue-nodes'] }, @@ -130,26 +87,9 @@ test.describe( const host = new HostDoc(WORKFLOW_ID, EMPTY_HOST_SEED, EMPTY_CATALOG) const vueNodes = new VueNodeHelpers(page) - let socket: WebSocketRoute | null = null - const subscribeTracker = createSubscribeTracker() - const send = (frame: AgentWsEvent | HostFrame): void => { - if ( - (frame.type.startsWith('doc_') || frame.type === 'awareness') && - parseServerDocFrame(frame) === null - ) - throw new Error(`frame ${frame.type} is not a valid doc frame`) - if (!socket) throw new Error('the app has not opened /ws yet') - socket.send(JSON.stringify(frame)) - } - - await page.routeWebSocket(/\/ws/, (ws) => { - socket = ws - ws.onMessage((raw) => { - if (!parseDocSubscribeForWorkflow(raw)) return - subscribeTracker.recordSubscribe() - send(host.subscribed()) - }) - }) + const hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, host) + hostSocket.answerSubscribesWith(() => [host.subscribed()]) + await hostSocket.install() await bootAgentApp(page, true, { settings: { @@ -181,7 +121,9 @@ test.describe( // A reset sent before the follower subscribed to WORKFLOW_ID is dropped // by the bridge, which would make the assertions below vacuous. - await subscribeTracker.waitForCount(1) + await expect + .poll(() => hostSocket.subscribeCount()) + .toBeGreaterThanOrEqual(1) await expect( vueNodes.getNodeLocator(String(TEMPLATE_NODE_A_ID)) @@ -190,7 +132,7 @@ test.describe( vueNodes.getNodeLocator(String(TEMPLATE_NODE_B_ID)) ).toBeVisible() - send({ + hostSocket.send({ type: 'agent_tool_call', data: { tool_call_id: 'call-print', @@ -202,7 +144,7 @@ test.describe( } }) - send({ + hostSocket.send({ type: 'doc_reset', data: { v: 1, @@ -215,7 +157,9 @@ test.describe( // The bridge resubscribes once it has dispatched the reset, so the // second subscribe proves the reset was processed. - await subscribeTracker.waitForCount(2) + await expect + .poll(() => hostSocket.subscribeCount()) + .toBeGreaterThanOrEqual(2) await page.screenshot({ path: test.info().outputPath('template-after-mint-reset.png') diff --git a/browser_tests/tests/agent/agentFollowerLateEndpointLink.spec.ts b/browser_tests/tests/agent/agentFollowerLateEndpointLink.spec.ts index 87df96fe6c9..3ee598a2e4f 100644 --- a/browser_tests/tests/agent/agentFollowerLateEndpointLink.spec.ts +++ b/browser_tests/tests/agent/agentFollowerLateEndpointLink.spec.ts @@ -99,7 +99,7 @@ test.describe( test('connects the wire once the missing endpoint node materializes', async ({ page, - getWebSocket + getAgentSocket }) => { test.setTimeout(90_000) await page.setViewportSize({ width: 1920, height: 1280 }) @@ -121,21 +121,22 @@ test.describe( }) await loadIntoBootWorkflow(page, BLANK_WORKFLOW) - const socket = await getWebSocket() const outboundFrames: string[] = [] - socket.onMessage((message) => outboundFrames.push(String(message))) - const vueNodes = new VueNodeHelpers(page) const agentPanel = new AgentPanel(page) - await test.step('bind the workflow so the follower subscribes', async () => { - await agentPanel.open() - await agentPanel.selectWorkflow() - await agentPanel.sendMessage('wire the source into the sink') - await expect - .poll(() => outboundFrames, { timeout: 15_000 }) - .toContainEqual(expect.stringContaining('doc_subscribe')) - }) + const socket = + await test.step('bind the workflow so the follower subscribes', async () => { + await agentPanel.open() + const opened = await getAgentSocket() + opened.onMessage((message) => outboundFrames.push(String(message))) + await agentPanel.selectWorkflow() + await agentPanel.sendMessage('wire the source into the sink') + await expect + .poll(() => outboundFrames, { timeout: 15_000 }) + .toContainEqual(expect.stringContaining('doc_subscribe')) + return opened + }) const host = new HostDoc(WORKFLOW_ID, seed, catalog) const sink = vueNodes.getNodeLocator(String(SINK_NODE_ID)) diff --git a/browser_tests/tests/agent/agentGraphOpLostOnDisconnect.spec.ts b/browser_tests/tests/agent/agentGraphOpLostOnDisconnect.spec.ts index c1200065c59..ea255b9377d 100644 --- a/browser_tests/tests/agent/agentGraphOpLostOnDisconnect.spec.ts +++ b/browser_tests/tests/agent/agentGraphOpLostOnDisconnect.spec.ts @@ -1,4 +1,3 @@ -import type { WebSocketRoute } from '@playwright/test' import { expect } from '@playwright/test' import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' @@ -6,17 +5,16 @@ import type { WorkflowListResponse } from '@comfyorg/ingest-types' import type { UserDataFullInfo } from '@/platform/remote/comfyui/types' import enMessages from '@/locales/en/main.json' with { type: 'json' } -import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' import { SUBSCRIBE_ACK_TIMEOUT_MS } from '@/workbench/extensions/agent/crdt/agentCrdtDocLifecycle' -import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' import { agentTest as test, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' +import type { ClientDocFrame } from '@e2e/fixtures/agentFollowerHostSocket' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' -import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers' import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' import { AGENT_COMPOSER_LABEL } from '@e2e/fixtures/utils/agentComposerLabel' @@ -43,7 +41,6 @@ const THREAD_ID = 'c9b6a9c0-9b1a-4b9b-8f0e-1a2b3c4d5e6f' const MESSAGE_ID = '0c5b1e77-2d4a-4f9e-8b63-1a2c3d4e5001' const ADDED_NODE_ID = 9001 const NODE_TEXT = 'created while disconnected' -const SOCKET_SID = '7d1f2e3a-4b5c-4d6e-8f90-1a2b3c4d5e6f' const CATALOG: WidgetCatalog = { types: { MarkdownNote: { widget_order: ['text'] } } @@ -53,32 +50,6 @@ const SEED: WorkflowJSON = { nodes: [], links: [] } const SEND_LABEL = enMessages.agent.send const STOP_LABEL = enMessages.agent.stop -interface SubscriptionFrame { - type: 'doc_subscribe' | 'doc_unsubscribe' - stateVector: string | null -} - -interface PostReconnectDocFrame extends SubscriptionFrame { - at: number -} - -const isRecord = (value: unknown): value is Record => - typeof value === 'object' && value !== null - -/** The follower's subscription frames for the workflow under test, or null. */ -function subscriptionFrameOf(raw: string | Buffer): SubscriptionFrame | null { - const frame: unknown = JSON.parse(raw.toString()) - if (!isRecord(frame) || !isRecord(frame.data)) return null - if (frame.type !== 'doc_subscribe' && frame.type !== 'doc_unsubscribe') - return null - if (frame.data.workflow_id !== WORKFLOW_ID) return null - const { state_vector_b64 } = frame.data - return { - type: frame.type, - stateVector: typeof state_vector_b64 === 'string' ? state_vector_b64 : null - } -} - test.describe( 'Agent graph op lost on disconnect', { tag: ['@cloud', '@agent'] }, @@ -90,21 +61,24 @@ test.describe( const host = new HostDoc(WORKFLOW_ID, SEED, CATALOG) const vueNodes = new VueNodeHelpers(page) - let socket: WebSocketRoute | null = null - let connectionCount = 0 - const postReconnectDocFrames: PostReconnectDocFrame[] = [] - const postReconnectSubscribes = (): PostReconnectDocFrame[] => - postReconnectDocFrames.filter(({ type }) => type === 'doc_subscribe') - - const send = (frame: AgentWsEvent | HostFrame): void => { - if ( - (frame.type.startsWith('doc_') || frame.type === 'awareness') && - parseServerDocFrame(frame) === null - ) - throw new Error(`frame ${frame.type} is not a valid doc frame`) - if (!socket) throw new Error('the app has not opened /ws yet') - socket.send(JSON.stringify(frame)) - } + const hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, host) + const postReconnectDocFrames = (): ClientDocFrame[] => + hostSocket + .clientDocFrames() + .filter( + ({ type, workflowId, connection }) => + connection > 1 && + workflowId === WORKFLOW_ID && + (type === 'doc_subscribe' || type === 'doc_unsubscribe') + ) + const postReconnectSubscribes = (): ClientDocFrame[] => + postReconnectDocFrames().filter(({ type }) => type === 'doc_subscribe') + hostSocket.answerSubscribesWith((stateVector) => + hostSocket.connectionCount() > 1 && + postReconnectSubscribes().length === 1 + ? [] + : [host.subscribed(), host.catchUp(stateVector)] + ) await page.route('**/api/agent/threads', (route) => route.fulfill(jsonRoute({ threads: [] })) @@ -123,38 +97,7 @@ test.describe( } return route.fulfill(jsonRoute([])) }) - await page.routeWebSocket(/\/ws/, (ws) => { - connectionCount += 1 - const isFirstConnection = connectionCount === 1 - socket = ws - ws.send( - JSON.stringify({ - type: 'status', - data: { - status: { exec_info: { queue_remaining: 0 } }, - sid: SOCKET_SID - } - }) - ) - ws.onMessage((raw) => { - const frame = subscriptionFrameOf(raw) - if (frame === null) return - if (!isFirstConnection) { - postReconnectDocFrames.push({ ...frame, at: Date.now() }) - } - if (frame.type !== 'doc_subscribe' || frame.stateVector === null) - return - // The reconnect's re-subscribe never gets an answer: the general - // realtime channel recovered (chat keeps flowing below), but the - // host swallows the doc subscription frame. Only a retry of that - // subscribe is answered. - const swallowed = - !isFirstConnection && postReconnectSubscribes().length === 1 - if (swallowed) return - send(host.subscribed()) - send(host.catchUp(frame.stateVector)) - }) - }) + await hostSocket.install() await bootAgentApp(page, true, { settings: { @@ -218,7 +161,7 @@ test.describe( // Setup checkpoint: the follower actually bound and subscribed once on // the first (and so far only) connection before anything goes wrong. - await expect.poll(() => connectionCount).toBe(1) + await expect.poll(() => hostSocket.connectionCount()).toBe(1) const composer = panel.getByRole('textbox', { name: AGENT_COMPOSER_LABEL @@ -229,7 +172,7 @@ test.describe( panel.getByText('Add a note to the canvas.').first() ).toBeVisible() - send({ + hostSocket.send({ type: 'agent_active_tab', data: { workflow_id: WORKFLOW_ID, @@ -238,7 +181,7 @@ test.describe( message_id: MESSAGE_ID } }) - send({ + hostSocket.send({ type: 'agent_tool_call', data: { tool_call_id: 'call-1', @@ -278,18 +221,18 @@ test.describe( // ... but the transport drops right as that update would go out: the // frame is never sent, and the socket closes under the follower. void update - await socket!.close() + await hostSocket.disconnect() // The main realtime channel recovers (exactly one reconnect: chat keeps // working below), so the agent can still report success. The follower's // re-subscribe went out on the new socket and is being ignored. - await expect.poll(() => connectionCount).toBe(2) - await expect.poll(() => postReconnectDocFrames.length).toBe(1) - expect(postReconnectDocFrames.map(({ type }) => type)).toEqual([ + await expect.poll(() => hostSocket.connectionCount()).toBe(2) + await expect.poll(() => postReconnectDocFrames().length).toBe(1) + expect(postReconnectDocFrames().map(({ type }) => type)).toEqual([ 'doc_subscribe' ]) - send({ + hostSocket.send({ type: 'agent_message_delta', data: { delta: 'Added a note to the canvas.', @@ -297,7 +240,7 @@ test.describe( message_id: MESSAGE_ID } }) - send({ + hostSocket.send({ type: 'agent_message_done', data: { thread_id: THREAD_ID, message_id: MESSAGE_ID, usage: null } }) @@ -327,13 +270,13 @@ test.describe( message: 'the follower never retried the unanswered doc_subscribe' }) .toBe(2) - expect(postReconnectDocFrames.map(({ type }) => type)).toEqual([ + expect(postReconnectDocFrames().map(({ type }) => type)).toEqual([ 'doc_subscribe', 'doc_subscribe' ]) const [silenced, retried] = postReconnectSubscribes() expect(retried.stateVector).toBe(silenced.stateVector) - expect(retried.at - silenced.at).toBeGreaterThanOrEqual( + expect(retried.atMs - silenced.atMs).toBeGreaterThanOrEqual( SUBSCRIBE_ACK_TIMEOUT_MS - 500 ) }) diff --git a/browser_tests/tests/agent/agentHarnessSmoke.spec.ts b/browser_tests/tests/agent/agentHarnessSmoke.spec.ts index e329926c7bd..87ff86293a1 100644 --- a/browser_tests/tests/agent/agentHarnessSmoke.spec.ts +++ b/browser_tests/tests/agent/agentHarnessSmoke.spec.ts @@ -1,9 +1,12 @@ +import type { Page } from '@playwright/test' import { expect } from '@playwright/test' -import enMessages from '@/locales/en/main.json' with { type: 'json' } +import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' import { comfyPageFixture as test } from '@e2e/fixtures/ComfyPage' +import { AGENT_SOCKET_URL } from '@e2e/fixtures/agentSocket' +import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' /** * BE-11470 C1.4 smoke: one real turn through /api/agent against the local @@ -24,50 +27,91 @@ import { comfyPageFixture as test } from '@e2e/fixtures/ComfyPage' * VITE_AGENT_STANDALONE, which is baked at build time and unset in the CI * dist, so this spec can only run under the harness. */ + +function collectAgentFrames(page: Page): { + docUpdates: () => number + doneMessageIds: string[] +} { + let docUpdates = 0 + const doneMessageIds: string[] = [] + page.on('websocket', (socket) => { + if (!AGENT_SOCKET_URL.test(socket.url())) return + socket.on('framereceived', ({ payload }) => { + const frame: unknown = JSON.parse(String(payload)) + const parsed = parseAgentWsEvent(frame) + if (parsed.success && parsed.data.type === 'agent_message_done') + doneMessageIds.push(parsed.data.data.message_id) + else if (parseServerDocFrame(frame)?.type === 'doc_update') + docUpdates += 1 + }) + }) + return { docUpdates: () => docUpdates, doneMessageIds } +} + +function runNonce(label: string): string { + const nonce = `${label}-${Date.now().toString(36)}-${Math.random() + .toString(36) + .slice(2, 10)}` + console.log(`[${label}] nonce=${nonce}`) + return nonce +} + +async function submitTurn(agentPanel: AgentPanel, text: string) { + await agentPanel.composer.fill(text) + await agentPanel.composer.press('Enter') +} + test.describe('Agent harness smoke', { tag: '@agent-harness' }, () => { test('one unmocked turn streams a reply the backend completes', async ({ comfyPage }) => { test.setTimeout(180_000) - const page = comfyPage.page - const doneFrames: string[] = [] - page.on('websocket', (socket) => { - if (!socket.url().includes('/api/agent/events')) return - socket.on('framereceived', ({ payload }) => { - const parsed = parseAgentWsEvent(JSON.parse(String(payload))) - if (parsed.success && parsed.data.type === 'agent_message_done') - doneFrames.push(parsed.data.data.message_id) - }) - }) - - const nonce = `agent-harness-smoke-${Date.now().toString(36)}-${Math.random() - .toString(36) - .slice(2, 10)}` - console.log(`[agent-harness-smoke] nonce=${nonce}`) - - await page - .getByRole('button', { name: enMessages.agent.entryButton, exact: true }) - .click() - const panel = page.locator('#agent-panel-root') - await expect(panel).toBeVisible() + const { doneMessageIds } = collectAgentFrames(comfyPage.page) + const nonce = runNonce('agent-harness-smoke') - const composer = panel.getByRole('textbox', { name: /^Describe ideas/ }) - await composer.fill( + const agentPanel = new AgentPanel(comfyPage.page) + const panel = await agentPanel.open() + await submitTurn( + agentPanel, `Reply with one short sentence acknowledging this session. Nonce: ${nonce}` ) - // Enter submits the composer; the CRDT dev-panel chip overlaps the Send - // button under the harness and intercepts pointer clicks on it. - await composer.press('Enter') // The turn has started once assistant text renders (the Stop affordance // is transient and a fast model turn can finish before it is observed). // A real model turn can be slow, so the completion window is generous. const assistantText = panel.getByTestId('markdown-stream').last() await expect(assistantText).toBeVisible({ timeout: 30_000 }) - await expect.poll(() => doneFrames, { timeout: 150_000 }).toHaveLength(1) - await expect( - panel.getByRole('button', { name: enMessages.agent.stop }) - ).toBeHidden() + await expect + .poll(() => doneMessageIds, { timeout: 150_000 }) + .toHaveLength(1) + await expect(agentPanel.stopButton).toBeHidden() await expect(assistantText).not.toHaveText('') }) + + test('lands an agent-added node on the canvas over the agent socket', async ({ + comfyPage + }) => { + test.setTimeout(180_000) + const { docUpdates, doneMessageIds } = collectAgentFrames(comfyPage.page) + const nonce = runNonce('agent-harness-canvas') + const nodeClass = 'EmptyLatentImage' + + const agentPanel = new AgentPanel(comfyPage.page) + await agentPanel.open() + const countNodes = async () => + (await comfyPage.nodeOps.getNodeRefsByType(nodeClass)).length + const before = await countNodes() + + await submitTurn( + agentPanel, + `Add one ${nodeClass} node to the canvas. Nonce: ${nonce}` + ) + + await expect + .poll(() => doneMessageIds, { timeout: 150_000 }) + .toHaveLength(1) + await expect.poll(docUpdates).toBeGreaterThan(0) + await expect.poll(countNodes).toBeGreaterThan(before) + await expect(agentPanel.stopButton).toBeHidden() + }) }) diff --git a/browser_tests/tests/agent/agentHistoryFocusesDraftTab.spec.ts b/browser_tests/tests/agent/agentHistoryFocusesDraftTab.spec.ts index e77893f56ae..2cf2e3b35f7 100644 --- a/browser_tests/tests/agent/agentHistoryFocusesDraftTab.spec.ts +++ b/browser_tests/tests/agent/agentHistoryFocusesDraftTab.spec.ts @@ -1,5 +1,4 @@ import { expect } from '@playwright/test' -import type { WebSocketRoute } from '@playwright/test' import type { AgentMessage, @@ -16,7 +15,10 @@ import { agentTest as test, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' +import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' +import { emptySeed } from '@e2e/fixtures/data/agent/agentRemoteApply' import { Topbar } from '@e2e/fixtures/components/Topbar' import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' @@ -136,16 +138,13 @@ test.describe( ], pagination: { offset: 0, limit: 100, total: 2, has_more: false } } - let socket: WebSocketRoute | undefined - await page.routeWebSocket(/\/ws/, (ws) => { - socket = ws - ws.send( - JSON.stringify({ - type: 'status', - data: { status: { exec_info: { queue_remaining: 0 } } } - }) - ) - }) + const hostSocket = new AgentFollowerHostSocket( + page, + DRAFT_WORKFLOW_ID, + new HostDoc(DRAFT_WORKFLOW_ID, emptySeed, { types: {} }) + ) + hostSocket.answerSubscribesWith(() => []) + await hostSocket.install() await bootAgentApp(page, agentFlagEnabled, { beforeNavigate: async (page) => { @@ -219,13 +218,10 @@ test.describe( await test.step('the agent announces the draft it minted and the panel opens a tab for it', async () => { await expect(topbar.tabs).toHaveCount(1) - if (!socket) throw new Error('the app never opened /ws') - socket.send( - JSON.stringify({ - type: 'agent_active_tab', - data: { workflow_id: DRAFT_WORKFLOW_ID, name: DRAFT_TAB_NAME } - }) - ) + hostSocket.send({ + type: 'agent_active_tab', + data: { workflow_id: DRAFT_WORKFLOW_ID, name: DRAFT_TAB_NAME } + }) await expect(topbar.getWorkflowTab(DRAFT_TAB_NAME)).toBeVisible() await expect(agentPanel.workflowPicker).toHaveText(DRAFT_TAB_NAME) await expect(unavailable).toBeHidden() diff --git a/browser_tests/tests/agent/agentInsertWorkflowCanvasResult.spec.ts b/browser_tests/tests/agent/agentInsertWorkflowCanvasResult.spec.ts index f095bb45274..16005451c9d 100644 --- a/browser_tests/tests/agent/agentInsertWorkflowCanvasResult.spec.ts +++ b/browser_tests/tests/agent/agentInsertWorkflowCanvasResult.spec.ts @@ -188,7 +188,7 @@ test.describe( test('renders a promoted widget, a cross-node link, and a subgraph from one insert_workflow tool call', async ({ page, - getWebSocket + getAgentSocket }, testInfo) => { test.setTimeout(60_000) await page.setViewportSize({ width: 1920, height: 1280 }) @@ -205,14 +205,14 @@ test.describe( } }) await loadIntoBootWorkflow(page, BLANK_WORKFLOW) - const socket = await getWebSocket() - const outboundFrames: string[] = [] - socket.onMessage((message) => outboundFrames.push(String(message))) // Drive the turn through the real, public Agent panel -- not a raw // CRDT call -- exactly as a user would trigger the tool. const agentPanel = new AgentPanel(page) await agentPanel.open() + const socket = await getAgentSocket() + const outboundFrames: string[] = [] + socket.onMessage((message) => outboundFrames.push(String(message))) await agentPanel.selectWorkflow() await agentPanel.sendMessage( 'Insert the CLIP-conditioned sampler subgraph onto this canvas' diff --git a/browser_tests/tests/agent/agentLoadedSkillActivity.spec.ts b/browser_tests/tests/agent/agentLoadedSkillActivity.spec.ts index bd2c77a3a73..87903711e38 100644 --- a/browser_tests/tests/agent/agentLoadedSkillActivity.spec.ts +++ b/browser_tests/tests/agent/agentLoadedSkillActivity.spec.ts @@ -13,14 +13,14 @@ test.describe('Loaded skill activity', { tag: ['@cloud', '@ui'] }, () => { test('keeps the visible skill name through completion', async ({ acceptedTurns, agentPanel, - getWebSocket + getAgentSocket }) => { await agentPanel.open() await agentPanel.selectWorkflow() await agentPanel.sendMessage('Use the comfy-director skill') await expect.poll(() => acceptedTurns).toHaveLength(1) - const ws = await getWebSocket() + const ws = await getAgentSocket() const event = (status: 'running' | 'success', skill?: string) => ({ type: 'agent_tool_call', diff --git a/browser_tests/tests/agent/agentLocalAssetUrls.spec.ts b/browser_tests/tests/agent/agentLocalAssetUrls.spec.ts index 3766719e11d..235c51798bb 100644 --- a/browser_tests/tests/agent/agentLocalAssetUrls.spec.ts +++ b/browser_tests/tests/agent/agentLocalAssetUrls.spec.ts @@ -22,7 +22,7 @@ test.describe('Agent reply image URLs', { tag: ['@cloud', '@ui'] }, () => { test('re-homes a loopback reply image onto the page API route', async ({ agentPanel, - getWebSocket, + getAgentSocket, page, postedMessages }) => { @@ -39,7 +39,7 @@ test.describe('Agent reply image URLs', { tag: ['@cloud', '@ui'] }, () => { await agentPanel.open() await agentPanel.selectWorkflow() - const ws = await getWebSocket() + const ws = await getAgentSocket() await agentPanel.sendMessage('Draw me a duck') await expect.poll(() => postedMessages).toHaveLength(1) diff --git a/browser_tests/tests/agent/agentMobileNewTabWorkflowBinding.spec.ts b/browser_tests/tests/agent/agentMobileNewTabWorkflowBinding.spec.ts index 4eb95033621..b2190961720 100644 --- a/browser_tests/tests/agent/agentMobileNewTabWorkflowBinding.spec.ts +++ b/browser_tests/tests/agent/agentMobileNewTabWorkflowBinding.spec.ts @@ -77,7 +77,7 @@ test.describe( test('follows the tab on screen rather than a saved workflow, and holds a hand-picked target across the next turn', async ({ page, workflowSelection, - getWebSocket + getAgentSocket }) => { test.setTimeout(60_000) @@ -103,7 +103,7 @@ test.describe( // waited 101s and 121s for a first reply). Close each turn over the // socket the way the server would. const finishTurn = async (messageId: string): Promise => { - const socket = await getWebSocket() + const socket = await getAgentSocket() socket.send( JSON.stringify({ type: 'agent_message_done', diff --git a/browser_tests/tests/agent/agentOverflowWidgetOnNodeCreate.spec.ts b/browser_tests/tests/agent/agentOverflowWidgetOnNodeCreate.spec.ts index 780fb175435..af5fc00591d 100644 --- a/browser_tests/tests/agent/agentOverflowWidgetOnNodeCreate.spec.ts +++ b/browser_tests/tests/agent/agentOverflowWidgetOnNodeCreate.spec.ts @@ -31,7 +31,6 @@ const NODE_ID = 503 const WORKFLOW_ID = '4b2d8f6a-1c3e-4a5b-9d7f-2e8c0a1b3d5f' const THREAD_ID = 'c7d8e9fa-0b1c-4d2e-8f3a-4b5c6d7e8f90' const MESSAGE_ID = 'f0e1d2c3-b4a5-4968-8778-9a0b1c2d3e4f' -const SOCKET_SID = '2a3b4c5d-6e7f-4809-9a1b-2c3d4e5f6071' function intSlider(value: number): InputSpec { return [ @@ -122,12 +121,7 @@ test.describe( ) const host = new HostDoc(WORKFLOW_ID, seed, catalog) - const hostSocket = new AgentFollowerHostSocket( - page, - WORKFLOW_ID, - host, - SOCKET_SID - ) + const hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, host) await hostSocket.install() const threadList: AgentThreadListResponse = { diff --git a/browser_tests/tests/agent/agentPanel.spec.ts b/browser_tests/tests/agent/agentPanel.spec.ts index 68bcbbe00fb..7c2eb694ec0 100644 --- a/browser_tests/tests/agent/agentPanel.spec.ts +++ b/browser_tests/tests/agent/agentPanel.spec.ts @@ -61,7 +61,7 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { test('shows the greeting, inserts a suggested prompt, and completes a chat turn', async ({ agentPanel, postedMessages, - getWebSocket + getAgentSocket }) => { test.setTimeout(30_000) @@ -86,7 +86,7 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { 'inserting a prompt must not POST a message' ).toHaveLength(0) - const ws = await getWebSocket() + const ws = await getAgentSocket() await sendButton.click() await expect.poll(() => postedMessages.length).toBeGreaterThanOrEqual(1) expect(postedMessages[0]).toContain(firstPrompt) @@ -225,13 +225,12 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { agentBilling, agentPanel, comfyPage, - getWebSocket, + getAgentSocket, postedMessages }) => { test.setTimeout(30_000) const page = comfyPage.page const paywall = page.getByTestId('agent-credits-exhausted-paywall') - const ws = await getWebSocket() await test.step('show the standing paywall after an accepted turn exhausts credits', async () => { await agentPanel.open() @@ -239,12 +238,12 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { agentBilling.setFundingState('exhausted') await agentPanel.sendMessage('Complete this workflow without a refusal') await expect.poll(() => postedMessages.length).toBe(1) - pushEvent(ws, THINKING_EVENT) + pushEvent(await getAgentSocket(), THINKING_EVENT) await expect( agentPanel.root.getByRole('button', { name: enMessages.agent.stop }) ).toBeVisible() await expect(paywall).toHaveCount(0) - pushEvent(ws, MESSAGE_DONE_EVENT) + pushEvent(await getAgentSocket(), MESSAGE_DONE_EVENT) await expect(paywall).toBeVisible() }) @@ -280,7 +279,7 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { const secondTurn = acceptedTurns.at(-1) assert(secondTurn) pushEvent( - ws, + await getAgentSocket(), zAgentWsEvent.parse({ ...THINKING_EVENT, data: { ...THINKING_EVENT.data, message_id: secondTurn.message_id } @@ -290,7 +289,7 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { agentPanel.root.getByRole('button', { name: enMessages.agent.stop }) ).toBeVisible() pushEvent( - ws, + await getAgentSocket(), zAgentWsEvent.parse({ ...MESSAGE_DONE_EVENT, data: { @@ -385,7 +384,7 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { test('copies retained tool metadata with privacy sources turned off', async ({ agentPanel, comfyPage, - getWebSocket + getAgentSocket }) => { await test.step('turn off every optional privacy source', async () => { await agentPanel.open() @@ -406,7 +405,7 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { await expect( agentPanel.root.getByTestId('user-message-bubble') ).toHaveText('private diagnostic prompt') - const ws = await getWebSocket() + const ws = await getAgentSocket() pushEvent(ws, THINKING_EVENT) await expect( agentPanel.root.getByText(THINKING_TEXT, { exact: true }) @@ -670,7 +669,7 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { test('edits and resubmits the last prompt after stopping its turn', async ({ agentPanel, postedMessages, - getWebSocket + getAgentSocket }) => { await agentPanel.open() await agentPanel.selectWorkflow() @@ -692,7 +691,7 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { panel.getByRole('button', { name: enMessages.g.edit }) ).toHaveCount(0) - pushEvent(await getWebSocket(), MESSAGE_DONE_EVENT) + pushEvent(await getAgentSocket(), MESSAGE_DONE_EVENT) const editButton = panel.getByRole('button', { name: enMessages.g.edit }) await expect(editButton).toHaveCount(1) await expect(editButton).toHaveCSS('pointer-events', 'none') diff --git a/browser_tests/tests/agent/agentPanelMocks.ts b/browser_tests/tests/agent/agentPanelMocks.ts index dad691266d9..c4e83631935 100644 --- a/browser_tests/tests/agent/agentPanelMocks.ts +++ b/browser_tests/tests/agent/agentPanelMocks.ts @@ -21,6 +21,7 @@ import type { } from '@/workbench/extensions/agent/schemas/agentApiSchema' import { zAgentAdmissionError } from '@/workbench/extensions/agent/schemas/agentApiSchema' +import { mockAgentIdentity } from '@e2e/fixtures/agentSocket' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' import { mockBilling } from '@e2e/fixtures/utils/cloudBillingMocks' import { mockCloudBootRoutes } from '@e2e/fixtures/utils/cloudBootMocks' @@ -361,6 +362,7 @@ async function mockAgentBoot( ) await mockBilling(page) + await mockAgentIdentity(page) await page.route('**/api/billing/status', (route) => agentBilling.fulfillStatus(route) ) diff --git a/browser_tests/tests/agent/agentPreviewTextWidgetNotMinted.spec.ts b/browser_tests/tests/agent/agentPreviewTextWidgetNotMinted.spec.ts index 1e69a053b82..687ef07450e 100644 --- a/browser_tests/tests/agent/agentPreviewTextWidgetNotMinted.spec.ts +++ b/browser_tests/tests/agent/agentPreviewTextWidgetNotMinted.spec.ts @@ -20,7 +20,6 @@ const SAMPLER_NODE_ID = 612 const WORKFLOW_ID = 'd5f3e6c4-0000-4000-8000-000000000070' const MESSAGE_ID = 'd5f3e6c4-0000-4000-8000-000000000071' const THREAD_ID = 'd5f3e6c4-0000-4000-8000-000000000072' -const SOCKET_SID = 'd5f3e6c4-0000-4000-8000-000000000073' const JOB_ID = 'd5f3e6c4-0000-4000-8000-000000000074' const PREVIEW_WIDGET = 'preview_text' @@ -115,7 +114,6 @@ const rigConfig = { }, samplerNodeId: SAMPLER_NODE_ID, seed, - socketSid: SOCKET_SID, threadId: THREAD_ID, visibleNodeId: PREVIEW_NODE_ID, workflowId: WORKFLOW_ID diff --git a/browser_tests/tests/agent/agentProgressTextWidgetNotMinted.spec.ts b/browser_tests/tests/agent/agentProgressTextWidgetNotMinted.spec.ts index 727e6fe9276..c5b2f5e0a45 100644 --- a/browser_tests/tests/agent/agentProgressTextWidgetNotMinted.spec.ts +++ b/browser_tests/tests/agent/agentProgressTextWidgetNotMinted.spec.ts @@ -19,7 +19,6 @@ const NODE_ID = 501 const WORKFLOW_ID = 'c4e2d5b3-0000-4000-8000-000000000060' const MESSAGE_ID = 'c4e2d5b3-0000-4000-8000-000000000061' const THREAD_ID = 'c4e2d5b3-0000-4000-8000-000000000062' -const SOCKET_SID = 'c4e2d5b3-0000-4000-8000-000000000063' const PREVIEW_WIDGET = '$$node-text-preview' const SEED_VALUE = 111111 @@ -78,7 +77,6 @@ const rigConfig = { nodeDefs: { [NODE_TYPE]: nodeDef }, samplerNodeId: NODE_ID, seed, - socketSid: SOCKET_SID, threadId: THREAD_ID, visibleNodeId: NODE_ID, workflowId: WORKFLOW_ID diff --git a/browser_tests/tests/agent/agentReplyDrafts.spec.ts b/browser_tests/tests/agent/agentReplyDrafts.spec.ts index bb86019e062..ee72d7a71f5 100644 --- a/browser_tests/tests/agent/agentReplyDrafts.spec.ts +++ b/browser_tests/tests/agent/agentReplyDrafts.spec.ts @@ -1,4 +1,4 @@ -import type { Locator, Page, WebSocketRoute } from '@playwright/test' +import type { Locator, Page } from '@playwright/test' import { expect } from '@playwright/test' import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' @@ -13,6 +13,7 @@ import { agentTest as test, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' @@ -22,7 +23,6 @@ import { AGENT_COMPOSER_LABEL } from '@e2e/fixtures/utils/agentComposerLabel' const WORKFLOW_ID = 'b4d7e1f2-8a3c-4d5e-9f60-7a1b2c3d4e5f' const THREAD_ID = 'd8c7b6a5-9e1f-4a2b-8c3d-4e5f6a7b8c9d' const MESSAGE_ID = '1e2d3c4b-5a69-4788-9a7b-6c5d4e3f2a1b' -const SOCKET_SID = '8e2f3a4b-5c6d-4e7f-9a01-2b3c4d5e6f70' const CATALOG: WidgetCatalog = { types: {} } const SEED: WorkflowJSON = { nodes: [], links: [] } @@ -58,12 +58,12 @@ interface Turn { // the document subscription the way the host does, so every frame after it is // the agent's. async function startTurn(page: Page, prompt: string): Promise { - const host = new HostDoc(WORKFLOW_ID, SEED, CATALOG) - let socket: WebSocketRoute | null = null - const send = (frame: AgentWsEvent | HostFrame): void => { - if (!socket) throw new Error('the app has not opened /ws yet') - socket.send(JSON.stringify(frame)) - } + const hostSocket = new AgentFollowerHostSocket( + page, + WORKFLOW_ID, + new HostDoc(WORKFLOW_ID, SEED, CATALOG) + ) + const send = (frame: AgentWsEvent | HostFrame): void => hostSocket.send(frame) await page.route('**/api/agent/threads', (route) => route.fulfill(jsonRoute({ threads: [] })) @@ -78,30 +78,7 @@ async function startTurn(page: Page, prompt: string): Promise { } return route.fulfill(jsonRoute([])) }) - await page.routeWebSocket(/\/ws/, (ws) => { - socket = ws - ws.send( - JSON.stringify({ - type: 'status', - data: { status: { exec_info: { queue_remaining: 0 } }, sid: SOCKET_SID } - }) - ) - ws.onMessage((raw) => { - const frame: unknown = JSON.parse(raw.toString()) - if (typeof frame !== 'object' || frame === null) return - const { type, data } = frame as { type?: unknown; data?: unknown } - if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) - return - const { workflow_id, state_vector_b64 } = data as { - workflow_id?: unknown - state_vector_b64?: unknown - } - if (workflow_id !== WORKFLOW_ID || typeof state_vector_b64 !== 'string') - return - send(host.subscribed()) - send(host.catchUp(state_vector_b64)) - }) - }) + await hostSocket.install() await bootAgentApp(page, true, { settings: { 'Comfy.Graph.CanvasInfo': false } @@ -165,7 +142,7 @@ async function startTurn(page: Page, prompt: string): Promise { .getByRole('menuitemradio', { name: 'Unsaved Workflow', exact: true }) .click() await expect(picker).toHaveText('Unsaved Workflow') - await expect.poll(() => socket !== null).toBe(true) + await expect.poll(() => hostSocket.connectionCount()).toBeGreaterThan(0) await panel.getByRole('textbox', { name: AGENT_COMPOSER_LABEL }).fill(prompt) await panel.getByRole('button', { name: SEND_LABEL }).click() diff --git a/browser_tests/tests/agent/agentSchemaSkewTellsTheUser.spec.ts b/browser_tests/tests/agent/agentSchemaSkewTellsTheUser.spec.ts index 09ab3119a09..c9a545a5cef 100644 --- a/browser_tests/tests/agent/agentSchemaSkewTellsTheUser.spec.ts +++ b/browser_tests/tests/agent/agentSchemaSkewTellsTheUser.spec.ts @@ -1,4 +1,3 @@ -import type { WebSocketRoute } from '@playwright/test' import { expect } from '@playwright/test' import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' @@ -6,15 +5,13 @@ import type { WorkflowListResponse } from '@comfyorg/ingest-types' import type { UserDataFullInfo } from '@/platform/remote/comfyui/types' import enMessages from '@/locales/en/main.json' with { type: 'json' } -import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' -import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' import { agentTest as test, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' -import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers' import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' @@ -24,7 +21,6 @@ const THREAD_ID = 'dac7bad1-ac2b-4cac-9a1f-2b3c4d5e6f81' const MESSAGE_ID = '1d6c2f88-3e5b-4a0f-9c74-2b3d4e5f6113' const ADDED_NODE_ID = 9103 const NODE_TEXT = 'the agent says it added this' -const SOCKET_SID = '8e2a3f4b-5c6d-4e7f-9a01-2b3c4d5e6f81' const CATALOG: WidgetCatalog = { types: { MarkdownNote: { widget_order: ['text'] } } @@ -66,18 +62,14 @@ test.describe( const host = new HostDoc(WORKFLOW_ID, SEED, CATALOG) const vueNodes = new VueNodeHelpers(page) - let socket: WebSocketRoute | null = null let committedNodeIds: string[] = [] - const send = (frame: AgentWsEvent | HostFrame): void => { - if ( - (frame.type.startsWith('doc_') || frame.type === 'awareness') && - parseServerDocFrame(frame) === null - ) - throw new Error(`frame ${frame.type} is not a valid doc frame`) - if (!socket) throw new Error('the app has not opened /ws yet') - socket.send(JSON.stringify(frame)) - } + const hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, host) + hostSocket.answerSubscribesWith((stateVector) => { + committedNodeIds = Object.keys(host.graph().nodes) + host.advanceSchemaVersionBeyondReader() + return [host.subscribed(), host.catchUp(stateVector)] + }) await page.route('**/api/agent/threads', (route) => route.fulfill(jsonRoute({ threads: [] })) @@ -96,42 +88,7 @@ test.describe( } return route.fulfill(jsonRoute([])) }) - await page.routeWebSocket(/\/ws/, (ws) => { - socket = ws - ws.send( - JSON.stringify({ - type: 'status', - data: { - status: { exec_info: { queue_remaining: 0 } }, - sid: SOCKET_SID - } - }) - ) - ws.onMessage((raw) => { - const frame: unknown = JSON.parse(raw.toString()) - if (typeof frame !== 'object' || frame === null) return - const { type, data } = frame as { type?: unknown; data?: unknown } - if ( - type !== 'doc_subscribe' || - typeof data !== 'object' || - data === null - ) - return - const { workflow_id, state_vector_b64 } = data as { - workflow_id?: unknown - state_vector_b64?: unknown - } - if ( - workflow_id !== WORKFLOW_ID || - typeof state_vector_b64 !== 'string' - ) - return - committedNodeIds = Object.keys(host.graph().nodes) - host.advanceSchemaVersionBeyondReader() - send(host.subscribed()) - send(host.catchUp(state_vector_b64)) - }) - }) + await hostSocket.install() await bootAgentApp(page, true, { settings: { @@ -200,7 +157,7 @@ test.describe( .click() await expect(picker).toHaveText('Unsaved Workflow') - await expect.poll(() => socket !== null).toBe(true) + await expect.poll(() => hostSocket.connectionCount()).toBeGreaterThan(0) const composer = panel.getByRole('textbox', { name: COMPOSER_LABEL }) await composer.fill('Rebuild the workflow.') @@ -209,7 +166,7 @@ test.describe( panel.getByText('Rebuild the workflow.').first() ).toBeVisible() - send({ + hostSocket.send({ type: 'agent_active_tab', data: { workflow_id: WORKFLOW_ID, @@ -218,7 +175,7 @@ test.describe( message_id: MESSAGE_ID } }) - send({ + hostSocket.send({ type: 'agent_tool_call', data: { tool_call_id: 'call-1', @@ -230,7 +187,7 @@ test.describe( } }) - send({ + hostSocket.send({ type: 'agent_message_delta', data: { delta: 'Rebuilt the workflow.', @@ -238,7 +195,7 @@ test.describe( message_id: MESSAGE_ID } }) - send({ + hostSocket.send({ type: 'agent_message_done', data: { thread_id: THREAD_ID, message_id: MESSAGE_ID, usage: null } }) diff --git a/browser_tests/tests/agent/agentStaleWorkflowTabBinding.spec.ts b/browser_tests/tests/agent/agentStaleWorkflowTabBinding.spec.ts index b6ceb8b343e..3fef9c76a26 100644 --- a/browser_tests/tests/agent/agentStaleWorkflowTabBinding.spec.ts +++ b/browser_tests/tests/agent/agentStaleWorkflowTabBinding.spec.ts @@ -1,5 +1,4 @@ import { expect } from '@playwright/test' -import type { WebSocketRoute } from '@playwright/test' import type { AgentMessage, @@ -18,6 +17,7 @@ import { agentTest as test, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' import { Topbar } from '@e2e/fixtures/components/Topbar' @@ -75,42 +75,12 @@ test.describe( ] as const ) - let socket: WebSocketRoute | undefined - await page.routeWebSocket(/\/ws/, (ws) => { - socket = ws - // Answers a doc_subscribe for the abandoned workflow with its real - // seeded content, the way the production doc host would. - ws.onMessage((raw) => { - const frame: unknown = JSON.parse(raw.toString()) - if (typeof frame !== 'object' || frame === null) return - const { type, data } = frame as { type?: unknown; data?: unknown } - if ( - type !== 'doc_subscribe' || - typeof data !== 'object' || - data === null - ) - return - const { workflow_id, state_vector_b64 } = data as { - workflow_id?: unknown - state_vector_b64?: unknown - } - if ( - workflow_id !== staleWorkflowId || - typeof state_vector_b64 !== 'string' - ) - return - ws.send(JSON.stringify(host.subscribed())) - ws.send(JSON.stringify(host.catchUp(state_vector_b64))) - }) - // The follower only re-drives a pending subscribe on a status frame, - // which every real connect sends. - ws.send( - JSON.stringify({ - type: 'status', - data: { status: { exec_info: { queue_remaining: 0 } } } - }) - ) - }) + const hostSocket = new AgentFollowerHostSocket( + page, + staleWorkflowId, + host + ) + await hostSocket.install() const history: AgentMessage[] = [ { @@ -237,7 +207,6 @@ test.describe( enMessages.agent.targetWorkflowUnavailable ) await expect(unavailable).toBeVisible() - if (!socket) throw new Error('the app never opened /ws') // The user continues the thread in the fresh tab. const workflowPicker = panel.getByRole('button', { diff --git a/browser_tests/tests/agent/agentStreamingScrollAnchor.spec.ts b/browser_tests/tests/agent/agentStreamingScrollAnchor.spec.ts index 9b48495df37..a5e5270af87 100644 --- a/browser_tests/tests/agent/agentStreamingScrollAnchor.spec.ts +++ b/browser_tests/tests/agent/agentStreamingScrollAnchor.spec.ts @@ -30,14 +30,14 @@ test.describe( test('keeps an earlier paragraph in view while more text streams', async ({ agentPanel, - getWebSocket, + getAgentSocket, page, postedMessages }) => { await agentPanel.open() await agentPanel.selectWorkflow() - const socket = await getWebSocket() + const socket = await getAgentSocket() await agentPanel.sendMessage('Explain this workflow in detail') await expect.poll(() => postedMessages).toHaveLength(1) await expect( diff --git a/browser_tests/tests/agent/agentSubgraphFollower.spec.ts b/browser_tests/tests/agent/agentSubgraphFollower.spec.ts index ec504a50d40..df3568d88e5 100644 --- a/browser_tests/tests/agent/agentSubgraphFollower.spec.ts +++ b/browser_tests/tests/agent/agentSubgraphFollower.spec.ts @@ -41,34 +41,38 @@ test.describe( test('materializes a linked host with nested definitions and applies a promoted widget edit', async ({ page, - getWebSocket + getAgentSocket }) => { - const socket = - await test.step('open the agent-enabled workflow', async () => { - await page.setViewportSize({ width: 1920, height: 1280 }) - await bootAgentApp(page, true, { - onboardingCompleted: true, - settings: { 'Comfy.VueNodes.Enabled': true }, - objectInfo: agentSubgraphNodeDefs, - beforeNavigate: async (page) => { - await mockAgentTurnApi(page, { - message_id: '3818ba00-d772-4a3f-98c1-9312725b577d', - thread_id: 'd4c016c4-3b8c-44cf-97de-1ae27e43e718', - workflow_id: AGENT_SUBGRAPH_WORKFLOW_ID - }) - await mockWorkflowPersistence(page, AGENT_SUBGRAPH_WORKFLOW_ID) - } - }) - return getWebSocket() + await test.step('open the agent-enabled workflow', async () => { + await page.setViewportSize({ width: 1920, height: 1280 }) + await bootAgentApp(page, true, { + onboardingCompleted: true, + settings: { 'Comfy.VueNodes.Enabled': true }, + objectInfo: agentSubgraphNodeDefs, + beforeNavigate: async (page) => { + await mockAgentTurnApi(page, { + message_id: '3818ba00-d772-4a3f-98c1-9312725b577d', + thread_id: 'd4c016c4-3b8c-44cf-97de-1ae27e43e718', + workflow_id: AGENT_SUBGRAPH_WORKFLOW_ID + }) + await mockWorkflowPersistence(page, AGENT_SUBGRAPH_WORKFLOW_ID) + } }) + }) const outboundFrames: string[] = [] - socket.onMessage((message) => outboundFrames.push(String(message))) - await test.step('select the workflow and send an agent turn', async () => { + const socket = await test.step('open the agent panel', async () => { await loadSeedIntoActiveTab(page, subgraphWorkflow) const agentPanel = new AgentPanel(page) await agentPanel.open() + const opened = await getAgentSocket() + opened.onMessage((message) => outboundFrames.push(String(message))) + return opened + }) + + await test.step('select the workflow and send an agent turn', async () => { + const agentPanel = new AgentPanel(page) await agentPanel.selectWorkflow() const composer = agentPanel.root.getByRole('textbox', { name: /^Describe ideas/ @@ -261,34 +265,38 @@ test.describe( test('applies interior widget edits to the subgraph and its host', async ({ page, - getWebSocket + getAgentSocket }) => { - const socket = - await test.step('open the agent-enabled workflow', async () => { - await page.setViewportSize({ width: 1920, height: 1280 }) - await bootAgentApp(page, true, { - onboardingCompleted: true, - settings: { 'Comfy.VueNodes.Enabled': true }, - objectInfo: agentSubgraphNodeDefs, - beforeNavigate: async (page) => { - await mockAgentTurnApi(page, { - message_id: '3818ba00-d772-4a3f-98c1-9312725b577d', - thread_id: 'd4c016c4-3b8c-44cf-97de-1ae27e43e718', - workflow_id: AGENT_SUBGRAPH_WORKFLOW_ID - }) - await mockWorkflowPersistence(page, AGENT_SUBGRAPH_WORKFLOW_ID) - } - }) - return getWebSocket() + await test.step('open the agent-enabled workflow', async () => { + await page.setViewportSize({ width: 1920, height: 1280 }) + await bootAgentApp(page, true, { + onboardingCompleted: true, + settings: { 'Comfy.VueNodes.Enabled': true }, + objectInfo: agentSubgraphNodeDefs, + beforeNavigate: async (page) => { + await mockAgentTurnApi(page, { + message_id: '3818ba00-d772-4a3f-98c1-9312725b577d', + thread_id: 'd4c016c4-3b8c-44cf-97de-1ae27e43e718', + workflow_id: AGENT_SUBGRAPH_WORKFLOW_ID + }) + await mockWorkflowPersistence(page, AGENT_SUBGRAPH_WORKFLOW_ID) + } }) + }) const outboundFrames: string[] = [] - socket.onMessage((message) => outboundFrames.push(String(message))) - await test.step('select the workflow and send an agent turn', async () => { + const socket = await test.step('open the agent panel', async () => { await loadSeedIntoActiveTab(page, subgraphWorkflow) const agentPanel = new AgentPanel(page) await agentPanel.open() + const opened = await getAgentSocket() + opened.onMessage((message) => outboundFrames.push(String(message))) + return opened + }) + + await test.step('select the workflow and send an agent turn', async () => { + const agentPanel = new AgentPanel(page) await agentPanel.selectWorkflow() const composer = agentPanel.root.getByRole('textbox', { name: /^Describe ideas/ @@ -387,7 +395,7 @@ test.describe( test('keeps promoted widget defaults when the host carries extra opaque values', async ({ page, - getWebSocket + getAgentSocket }) => { await page.setViewportSize({ width: 1920, height: 1280 }) await bootAgentApp(page, true, { @@ -403,10 +411,6 @@ test.describe( await mockWorkflowPersistence(page, AGENT_SUBGRAPH_DRIFT_WORKFLOW_ID) } }) - const socket = await getWebSocket() - const outboundFrames: string[] = [] - socket.onMessage((message) => outboundFrames.push(String(message))) - await loadSeedIntoActiveTab(page, { ...subgraphWorkflow, nodes: [], @@ -414,6 +418,9 @@ test.describe( }) const agentPanel = new AgentPanel(page) await agentPanel.open() + const socket = await getAgentSocket() + const outboundFrames: string[] = [] + socket.onMessage((message) => outboundFrames.push(String(message))) await agentPanel.selectWorkflow() const composer = agentPanel.root.getByRole('textbox', { name: /^Describe ideas/ diff --git a/browser_tests/tests/agent/agentTemplatePlacement.spec.ts b/browser_tests/tests/agent/agentTemplatePlacement.spec.ts index 7884e509e58..04314561f86 100644 --- a/browser_tests/tests/agent/agentTemplatePlacement.spec.ts +++ b/browser_tests/tests/agent/agentTemplatePlacement.spec.ts @@ -1,4 +1,4 @@ -import type { Page, WebSocketRoute } from '@playwright/test' +import type { Page } from '@playwright/test' import { expect } from '@playwright/test' import type { @@ -6,13 +6,13 @@ import type { AgentMessages, AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' -import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' import { agentTest as test, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' @@ -36,7 +36,6 @@ const THREAD_ID = 'a1b2c3d4-0000-4000-8000-000000000001' const MESSAGE_ID = 'a1b2c3d4-0000-4000-8000-000000000002' const WORKFLOW_ID = 'a1b2c3d4-0000-4000-8000-000000000003' const WORKFLOW_NAME = 'Image edit' -const SOCKET_SID = 'a1b2c3d4-0000-4000-8000-000000000004' // Stands in for the node the user just placed (e.g. Load Image) before // asking the agent to insert a template. @@ -114,11 +113,7 @@ function templateAddNodeOps() { // wiring, same real HostDoc/CRDT apply path, same real canvas. class TemplatePlacementHarness { private readonly host: HostDoc - private socket: WebSocketRoute | null = null - private resolveSubscribed: (() => void) | null = null - private readonly subscribed = new Promise((resolve) => { - this.resolveSubscribed = resolve - }) + private readonly hostSocket: AgentFollowerHostSocket constructor(private readonly page: Page) { this.host = new HostDoc( @@ -126,23 +121,12 @@ class TemplatePlacementHarness { SEED_WORKFLOW.seed, SEED_WORKFLOW.catalog ) + this.hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, this.host) } async boot(): Promise { await this.mockAgentApi() - await this.page.routeWebSocket(/\/ws/, (socket) => { - this.socket = socket - socket.onMessage((raw) => this.onClientFrame(raw)) - socket.send( - JSON.stringify({ - type: 'status', - data: { - status: { exec_info: { queue_remaining: 0 } }, - sid: SOCKET_SID - } - }) - ) - }) + await this.hostSocket.install() await bootAgentApp(this.page, true, { settings: { @@ -231,7 +215,7 @@ class TemplatePlacementHarness { data: { workflow_id: WORKFLOW_ID, name: WORKFLOW_NAME } }) ) - await this.waitForSubscribe() + await this.hostSocket.waitForSubscribe() // The subscribe catch-up (the user's pre-existing canvas) must be applied // before the template tool call arrives, as it is in production: sending // both back-to-back coalesces them into the follower's first frame, a @@ -308,34 +292,7 @@ class TemplatePlacementHarness { } private send(frame: AgentWsEvent | HostFrame): void { - if ( - (frame.type.startsWith('doc_') || frame.type === 'awareness') && - parseServerDocFrame(frame) === null - ) - throw new Error(`host frame ${frame.type} is not a valid doc frame`) - if (!this.socket) throw new Error('the app has not opened /ws yet') - this.socket.send(JSON.stringify(frame)) - } - - private onClientFrame(raw: string | Buffer): void { - const frame: unknown = JSON.parse(raw.toString()) - if (typeof frame !== 'object' || frame === null) return - const { type, data } = frame as { type?: unknown; data?: unknown } - if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) - return - const { workflow_id, state_vector_b64 } = data as { - workflow_id?: unknown - state_vector_b64?: unknown - } - if (workflow_id !== WORKFLOW_ID || typeof state_vector_b64 !== 'string') - return - this.send(this.host.subscribed()) - this.send(this.host.catchUp(state_vector_b64)) - this.resolveSubscribed?.() - } - - private waitForSubscribe(): Promise { - return this.subscribed + this.hostSocket.send(frame) } } diff --git a/browser_tests/tests/agent/agentTurnProgressIsLegible.spec.ts b/browser_tests/tests/agent/agentTurnProgressIsLegible.spec.ts index f69dc917eb5..e03a110cd6a 100644 --- a/browser_tests/tests/agent/agentTurnProgressIsLegible.spec.ts +++ b/browser_tests/tests/agent/agentTurnProgressIsLegible.spec.ts @@ -24,13 +24,13 @@ test.describe( test('shows a spinning in-progress row for a running tool call and settles it when the call finishes', async ({ agentPanel, - getWebSocket, + getAgentSocket, postedMessages }) => { await agentPanel.open() await agentPanel.selectWorkflow() - const ws = await getWebSocket() + const ws = await getAgentSocket() await agentPanel.sendMessage('Open a new tab for the upscale graph') await expect.poll(() => postedMessages.length).toBeGreaterThanOrEqual(1) @@ -65,13 +65,13 @@ test.describe( test('renders a multi-sentence thinking update in full instead of cutting it off', async ({ agentPanel, - getWebSocket, + getAgentSocket, postedMessages }) => { await agentPanel.open() await agentPanel.selectWorkflow() - const ws = await getWebSocket() + const ws = await getAgentSocket() await agentPanel.sendMessage('Plan the upscale pass before you edit') await expect.poll(() => postedMessages.length).toBeGreaterThanOrEqual(1) @@ -102,13 +102,13 @@ test.describe( test('names how long the finished turn took', async ({ agentPanel, - getWebSocket, + getAgentSocket, postedMessages }) => { await agentPanel.open() await agentPanel.selectWorkflow() - const ws = await getWebSocket() + const ws = await getAgentSocket() await agentPanel.sendMessage('Set the prompt to a red fox in the snow') await expect.poll(() => postedMessages.length).toBeGreaterThanOrEqual(1) diff --git a/browser_tests/tests/agent/agentTurnSurvivesPanelMinimize.spec.ts b/browser_tests/tests/agent/agentTurnSurvivesPanelMinimize.spec.ts index e630d44d3c4..ffbf1df782f 100644 --- a/browser_tests/tests/agent/agentTurnSurvivesPanelMinimize.spec.ts +++ b/browser_tests/tests/agent/agentTurnSurvivesPanelMinimize.spec.ts @@ -26,7 +26,7 @@ test.describe( // below only resolves once a turn has actually ended. test('runs a turn to completion after a minimize and restore', async ({ turnLock, - getWebSocket + getAgentSocket }) => { await test.step('minimize and restore an idle panel', async () => { await turnLock.openOnBlankWorkflow() @@ -38,7 +38,7 @@ test.describe( await turnLock.startTurn(PROMPT) await expect(turnLock.workSummary).toHaveCount(0) - turnLock.push(await getWebSocket(), TURN_DONE_EVENT) + turnLock.push(await getAgentSocket(), TURN_DONE_EVENT) await expect(turnLock.workSummary).toBeVisible() await expect(turnLock.sendButton).toBeVisible() @@ -74,7 +74,7 @@ test.describe( test('keeps rendering the turn after the panel is reopened', async ({ turnLock, - getWebSocket + getAgentSocket }) => { await test.step('minimize and reopen over the live turn', async () => { await turnLock.minimizePanel() @@ -84,7 +84,7 @@ test.describe( }) await test.step('a later frame for that turn still renders', async () => { - turnLock.push(await getWebSocket(), POST_RECONNECT_EVENT) + turnLock.push(await getAgentSocket(), POST_RECONNECT_EVENT) await expect( turnLock.panel.getByText(POST_RECONNECT_TEXT) @@ -95,7 +95,7 @@ test.describe( test('settles when completion arrives after hydration handoff expires', async ({ page, turnLock, - getWebSocket + getAgentSocket }) => { await page.clock.install() turnLock.holdNextTranscript() @@ -108,7 +108,7 @@ test.describe( }) await test.step('accept terminal delivery before stale hydration resolves', async () => { - turnLock.push(await getWebSocket(), TURN_DONE_EVENT) + turnLock.push(await getAgentSocket(), TURN_DONE_EVENT) turnLock.releaseHeldTranscript() await expect(turnLock.workSummary).toBeVisible() @@ -120,7 +120,7 @@ test.describe( test('delivers completion to a background turn before its hydration mailbox expires', async ({ page, turnLock, - getWebSocket + getAgentSocket }) => { await page.clock.install() @@ -132,7 +132,7 @@ test.describe( }) await test.step('complete the background turn and expire its mailbox', async () => { - turnLock.push(await getWebSocket(), TURN_DONE_EVENT) + turnLock.push(await getAgentSocket(), TURN_DONE_EVENT) await page.clock.fastForward(300_001) }) diff --git a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts index 063c01ac29e..ca421710b1a 100644 --- a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts +++ b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts @@ -16,8 +16,8 @@ import { // "Message failed to send: a turn is already in progress for this thread" — // with no refresh and no tab close anywhere in the journey. // -// The agent chat stream rides the shared ComfyUI websocket, so any transient -// close dispatches `reconnecting`. The server never sees the socket go away: +// The agent chat stream rides the agent socket (`/api/agent/events`), so any +// transient close reports it down. The server never sees the socket go away: // its assistant row stays `streaming`, it keeps broadcasting the same // message_id down the new socket, and it answers posts with 409 until the turn // really ends. These specs close one socket and hold the client to keeping the @@ -141,11 +141,11 @@ test.describe( // free; this shows it does resolve once a turn ends. test('summarises a turn that ends normally', async ({ turnLock, - getWebSocket + getAgentSocket }) => { await expect(turnLock.workSummary).toHaveCount(0) - turnLock.push(await getWebSocket(), TURN_DONE_EVENT) + turnLock.push(await getAgentSocket(), TURN_DONE_EVENT) await expect(turnLock.workSummary).toBeVisible() await expect(turnLock.sendButton).toBeVisible() @@ -158,9 +158,9 @@ test.describe( // product's player, so it does not clear the whole audio path. test('keeps the turn running while audio decoding runs alongside it', async ({ turnLock, - getWebSocket + getAgentSocket }) => { - const ws = await getWebSocket() + const ws = await getAgentSocket() await turnLock.decodeAudioLikeAPreview() await expect(turnLock.stopButton).toBeVisible() diff --git a/browser_tests/tests/agent/agentTwoContextConvergence.spec.ts b/browser_tests/tests/agent/agentTwoContextConvergence.spec.ts index adc2fa522a4..2570f27a879 100644 --- a/browser_tests/tests/agent/agentTwoContextConvergence.spec.ts +++ b/browser_tests/tests/agent/agentTwoContextConvergence.spec.ts @@ -62,8 +62,8 @@ test.describe( { types: { CLIPTextEncode: { widget_order: ['text'] } } } ) - await sharedHost.attach(pageA, 'shared-host-page-a') - await sharedHost.attach(pageB, 'shared-host-page-b') + await sharedHost.attach(pageA) + await sharedHost.attach(pageB) await bootSharedPage(pageA) await bootSharedPage(pageB) diff --git a/browser_tests/tests/agent/agentUndefinedSerializeWidgetNotMinted.spec.ts b/browser_tests/tests/agent/agentUndefinedSerializeWidgetNotMinted.spec.ts index 67e898f20a6..0e37c5b9037 100644 --- a/browser_tests/tests/agent/agentUndefinedSerializeWidgetNotMinted.spec.ts +++ b/browser_tests/tests/agent/agentUndefinedSerializeWidgetNotMinted.spec.ts @@ -27,7 +27,6 @@ const NODE_ID = 711 const WORKFLOW_ID = 'e7a4c6d2-0000-4000-8000-000000000080' const MESSAGE_ID = 'e7a4c6d2-0000-4000-8000-000000000081' const THREAD_ID = 'e7a4c6d2-0000-4000-8000-000000000082' -const SOCKET_SID = 'e7a4c6d2-0000-4000-8000-000000000083' const PREVIEW_WIDGET = '$$node-text-preview' const SEED_VALUE = 777777 @@ -91,7 +90,6 @@ const rigConfig = { nodeDefs: { [NODE_TYPE]: nodeDef }, samplerNodeId: NODE_ID, seed, - socketSid: SOCKET_SID, threadId: THREAD_ID, visibleNodeId: NODE_ID, workflowId: WORKFLOW_ID diff --git a/browser_tests/tests/agent/agentZeroNodesOnUnreadableSchema.spec.ts b/browser_tests/tests/agent/agentZeroNodesOnUnreadableSchema.spec.ts index c3f0f12f8b2..e5a212d3b14 100644 --- a/browser_tests/tests/agent/agentZeroNodesOnUnreadableSchema.spec.ts +++ b/browser_tests/tests/agent/agentZeroNodesOnUnreadableSchema.spec.ts @@ -1,4 +1,3 @@ -import type { WebSocketRoute } from '@playwright/test' import { expect } from '@playwright/test' import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' @@ -6,15 +5,13 @@ import type { WorkflowListResponse } from '@comfyorg/ingest-types' import type { UserDataFullInfo } from '@/platform/remote/comfyui/types' import enMessages from '@/locales/en/main.json' with { type: 'json' } -import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' -import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' import { agentTest as test, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' -import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers' import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' @@ -64,7 +61,6 @@ const THREAD_ID = 'c9b6a9c0-9b1a-4b9b-8f0e-1a2b3c4d5e70' const MESSAGE_ID = '0c5b1e77-2d4a-4f9e-8b63-1a2c3d4e5002' const ADDED_NODE_ID = 9002 const NODE_TEXT = 'added after the schema gate tripped' -const SOCKET_SID = '7d1f2e3a-4b5c-4d6e-8f90-1a2b3c4d5e70' const CATALOG: WidgetCatalog = { types: { MarkdownNote: { widget_order: ['text'] } } @@ -85,17 +81,15 @@ test.describe( const host = new HostDoc(WORKFLOW_ID, SEED, CATALOG) const vueNodes = new VueNodeHelpers(page) - let socket: WebSocketRoute | null = null - - const send = (frame: AgentWsEvent | HostFrame): void => { - if ( - (frame.type.startsWith('doc_') || frame.type === 'awareness') && - parseServerDocFrame(frame) === null - ) - throw new Error(`frame ${frame.type} is not a valid doc frame`) - if (!socket) throw new Error('the app has not opened /ws yet') - socket.send(JSON.stringify(frame)) - } + const hostSocket = new AgentFollowerHostSocket(page, WORKFLOW_ID, host) + hostSocket.answerSubscribesWith((stateVector) => { + host.corruptSchemaVersion() + return [ + host.subscribed(), + host.catchUp(stateVector), + host.repairSchemaVersion() + ] + }) await page.route('**/api/agent/threads', (route) => route.fulfill(jsonRoute({ threads: [] })) @@ -114,50 +108,7 @@ test.describe( } return route.fulfill(jsonRoute([])) }) - await page.routeWebSocket(/\/ws/, (ws) => { - socket = ws - ws.send( - JSON.stringify({ - type: 'status', - data: { - status: { exec_info: { queue_remaining: 0 } }, - sid: SOCKET_SID - } - }) - ) - ws.onMessage((raw) => { - const frame: unknown = JSON.parse(raw.toString()) - if (typeof frame !== 'object' || frame === null) return - const { type, data } = frame as { type?: unknown; data?: unknown } - if ( - type !== 'doc_subscribe' || - typeof data !== 'object' || - data === null - ) - return - const { workflow_id, state_vector_b64 } = data as { - workflow_id?: unknown - state_vector_b64?: unknown - } - if ( - workflow_id !== WORKFLOW_ID || - typeof state_vector_b64 !== 'string' - ) - return - // Simulates the unproven trigger this repro exercises (see file - // header): the catch-up frame's merged doc has no readable - // meta.schema_version at all. - host.corruptSchemaVersion() - send(host.subscribed()) - send(host.catchUp(state_vector_b64)) - // Sent as its own later-seq frame -- not folded silently into a - // later apply()'s delta -- so the fix that stops the latch from - // outliving one bad frame is exercised for real: this repair alone - // is enough for the gate to pass again before the add_node frame - // below arrives. - send(host.repairSchemaVersion()) - }) - }) + await hostSocket.install() await bootAgentApp(page, true, { settings: { @@ -228,7 +179,7 @@ test.describe( // Setup checkpoint, still expected to pass: the doc subscription round // trip (with its corrupted catch-up) actually completed. - await expect.poll(() => socket !== null).toBe(true) + await expect.poll(() => hostSocket.connectionCount()).toBeGreaterThan(0) const composer = panel.getByRole('textbox', { name: AGENT_COMPOSER_LABEL @@ -239,7 +190,7 @@ test.describe( panel.getByText('Add a note to the canvas.').first() ).toBeVisible() - send({ + hostSocket.send({ type: 'agent_active_tab', data: { workflow_id: WORKFLOW_ID, @@ -248,7 +199,7 @@ test.describe( message_id: MESSAGE_ID } }) - send({ + hostSocket.send({ type: 'agent_tool_call', data: { tool_call_id: 'call-1', @@ -262,7 +213,7 @@ test.describe( // The op lands on the host's own authoritative document, and is sent to // the browser the same way agentConversationFixture's replay does. - send( + hostSocket.send( host.apply([ { op: 'add_node', @@ -287,7 +238,7 @@ test.describe( ) expect(Object.keys(host.graph().nodes)).toContain(String(ADDED_NODE_ID)) - send({ + hostSocket.send({ type: 'agent_message_delta', data: { delta: 'Added a note to the canvas.', @@ -295,7 +246,7 @@ test.describe( message_id: MESSAGE_ID } }) - send({ + hostSocket.send({ type: 'agent_message_done', data: { thread_id: THREAD_ID, message_id: MESSAGE_ID, usage: null } }) diff --git a/build/devAgentConfig.ts b/build/devAgentConfig.ts index 71e2bc9e680..7b00eb88ef4 100644 --- a/build/devAgentConfig.ts +++ b/build/devAgentConfig.ts @@ -34,6 +34,8 @@ function isReadableDirectory(path: string): boolean { } } +const AGENT_SESSION_HEADER = 'X-Comfy-Agent-Session' + type AgentTokenSource = | { kind: 'static'; token: string } | { kind: 'discovery'; dataDir: string } @@ -87,15 +89,26 @@ export function createDevAgentConfig(env: NodeJS.ProcessEnv) { ? { kind: 'discovery', dataDir } : undefined + const proxy = + url && tokenSource + ? createAgentProxy(url, tokenSource, comfyToken) + : undefined return { host: !comfyToken && env.VITE_REMOTE_DEV === 'true' ? '0.0.0.0' : undefined, - proxy: - url && tokenSource - ? createAgentProxy(url, tokenSource, comfyToken) - : undefined + proxy, + proxySuppliesCredential: proxy !== undefined && Boolean(comfyToken) } } +function comfyCredentialHeaders( + comfyToken: string | undefined +): Record { + if (!comfyToken) return {} + return comfyToken.startsWith('comfyui-') + ? { 'X-API-KEY': comfyToken } + : { Authorization: `Bearer ${comfyToken}` } +} + function createAgentProxy( target: string, tokenSource: AgentTokenSource, @@ -107,9 +120,9 @@ function createAgentProxy( rewrite: (path: string) => path.replace(/^\/api/, ''), headers: { ...(tokenSource.kind === 'static' - ? { Authorization: `Bearer ${tokenSource.token}` } + ? { [AGENT_SESSION_HEADER]: tokenSource.token } : {}), - ...(comfyToken ? { 'X-Comfy-Token': comfyToken } : {}) + ...comfyCredentialHeaders(comfyToken) }, configure: (proxy) => { proxy.on('proxyReqWs', (proxyReq, req, socket) => { @@ -122,9 +135,9 @@ function createAgentProxy( const { dataDir } = tokenSource function authorize(proxyReq: ClientRequest) { - proxyReq.removeHeader('Authorization') + proxyReq.removeHeader(AGENT_SESSION_HEADER) const token = readAgentDiscoveryToken(dataDir) - if (token) proxyReq.setHeader('Authorization', `Bearer ${token}`) + if (token) proxyReq.setHeader(AGENT_SESSION_HEADER, token) } proxy.on('proxyReq', authorize) proxy.on('proxyReqWs', authorize) @@ -137,3 +150,15 @@ function createAgentProxy( } } } + +export function devServerAgentRoutes( + agentProxy: ProxyOptions | undefined, + eventsSocketProxy: ProxyOptions +): Record { + return { + ...(agentProxy + ? { '/api/workflows': agentProxy, '/api/agent': agentProxy } + : {}), + '/api/agent/events': { ...eventsSocketProxy, ws: true } + } +} diff --git a/build/devAgentTokenRotation.test.ts b/build/devAgentTokenRotation.test.ts index 3d6f501af7c..b169e0d5bc5 100644 --- a/build/devAgentTokenRotation.test.ts +++ b/build/devAgentTokenRotation.test.ts @@ -1,7 +1,7 @@ // node:http rather than fetch: vitest.network.setup.ts blocks fetch to http(s). import { randomBytes } from 'node:crypto' import { mkdtemp, rm, writeFile } from 'node:fs/promises' -import type { Server } from 'node:http' +import type { IncomingMessage, Server } from 'node:http' import { createServer, request } from 'node:http' import { tmpdir } from 'node:os' import { join, resolve } from 'node:path' @@ -19,15 +19,22 @@ function listeningPort(server: Pick | null): number { return address.port } +function sessionOf(req: IncomingMessage): string | undefined { + const session = req.headers['x-comfy-agent-session'] + return typeof session === 'string' ? session : undefined +} + class FakeStandaloneAgent { private token = randomBytes(32).toString('hex') private readonly server: Server + readonly requestSessions: Array = [] readonly requestAuthorizations: Array = [] - readonly upgradeAuthorizations: Array = [] + readonly upgradeSessions: Array = [] private constructor() { this.server = createServer((req, res) => { + this.requestSessions.push(sessionOf(req)) this.requestAuthorizations.push(req.headers.authorization) - if (!this.authorized(req.headers.authorization)) { + if (!this.authorized(sessionOf(req))) { res.statusCode = 401 res.end('unauthorized') return @@ -36,9 +43,9 @@ class FakeStandaloneAgent { res.end(JSON.stringify({ path: req.url })) }) this.server.on('upgrade', (req, socket) => { - this.upgradeAuthorizations.push(req.headers.authorization) + this.upgradeSessions.push(sessionOf(req)) socket.end( - this.authorized(req.headers.authorization) + this.authorized(sessionOf(req)) ? 'HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n' : 'HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n' ) @@ -53,8 +60,8 @@ class FakeStandaloneAgent { return agent } - private authorized(header: string | undefined): boolean { - return header === `Bearer ${this.token}` + private authorized(session: string | undefined): boolean { + return session === this.token } get url(): string { @@ -225,7 +232,19 @@ describe('dev agent proxy across an agent restart', { timeout: 15_000 }, () => { expect(await upgradeThroughProxy(server)).toBe(101) }) - it('does not forward an inbound credential while discovery is unavailable', async () => { + it('passes the signed-in user credential through beside the session', async () => { + const { agent, server } = await bringUpPair('discovery') + + expect( + await getThroughProxy(server, '/api/agent/threads', { + Authorization: 'Bearer user-id-token' + }) + ).toBe(200) + expect(agent.requestAuthorizations).toEqual(['Bearer user-id-token']) + expect(agent.requestSessions).toEqual([agent.currentToken]) + }) + + it('does not forward an inbound session while discovery is unavailable', async () => { const agent = await FakeStandaloneAgent.start() started.agent = agent ownedDataDir = await mkdtemp(join(tmpdir(), 'dev-agent-rotation-')) @@ -237,11 +256,11 @@ describe('dev agent proxy across an agent restart', { timeout: 15_000 }, () => { expect( await getThroughProxy(server, '/api/agent/threads', { - Authorization: `Bearer ${agent.currentToken}`, + 'X-Comfy-Agent-Session': agent.currentToken, 'Sec-Fetch-Site': 'same-origin' }) ).toBe(401) - expect(agent.requestAuthorizations).toEqual([undefined]) + expect(agent.requestSessions).toEqual([undefined]) }) it('keeps the credential off cross-origin requests after a rotation', async () => { @@ -256,18 +275,14 @@ describe('dev agent proxy across an agent restart', { timeout: 15_000 }, () => { ).toBe(403) await rejectCrossOriginUpgrade(server) - expect(agent.upgradeAuthorizations).toEqual([ - `Bearer ${agent.currentToken}` - ]) + expect(agent.upgradeSessions).toEqual([agent.currentToken]) }) it('keeps a static credential off a cross-origin upgrade', async () => { const { agent, server } = await bringUpPair('static') await rejectCrossOriginUpgrade(server) - expect(agent.upgradeAuthorizations).toEqual([ - `Bearer ${agent.currentToken}` - ]) + expect(agent.upgradeSessions).toEqual([agent.currentToken]) }) it('rejects a request that declares itself cross-site', async () => { @@ -289,7 +304,7 @@ describe('dev agent proxy across an agent restart', { timeout: 15_000 }, () => { 'Sec-Fetch-Site': 'cross-site' }) ).toBe(403) - expect(agent.requestAuthorizations).toEqual([]) + expect(agent.requestSessions).toEqual([]) }) it('serves a caller that sends no browser metadata at all', async () => { diff --git a/docs/adr/CRDT-FOLLOWER-0025-in-app-agent-crdt-follower-and-distribution-resolved-boundaries.md b/docs/adr/CRDT-FOLLOWER-0025-in-app-agent-crdt-follower-and-distribution-resolved-boundaries.md index 67768e5cff1..9f906720572 100644 --- a/docs/adr/CRDT-FOLLOWER-0025-in-app-agent-crdt-follower-and-distribution-resolved-boundaries.md +++ b/docs/adr/CRDT-FOLLOWER-0025-in-app-agent-crdt-follower-and-distribution-resolved-boundaries.md @@ -16,6 +16,10 @@ on it were deleted. The distribution-boundary decision, the one-way follower invariant, the human write path, and the product gate map are unchanged and carried forward below. +Amended 2026-10-12: one agent socket and one credential policy on every +backend replace the distribution-resolved connection module. See +"Amendment (2026-10-12)"; the passages it supersedes are marked. + ## Context The In-App Agent runs server-side and needs to read a user's live workflow and @@ -41,7 +45,8 @@ Only the boundaries around it differ: the transport endpoint (Cloud reaches the agent through same-origin ingest; Local and Desktop connect directly to the agent binary) and authentication (every surface uses the unified chain in `authStore.getAuthHeader()`; Cloud ingest additionally enforces M2M -server-side). The model provider is never a distribution fork. +server-side). The model provider is never a distribution fork. (Superseded +2026-10-12: every surface reaches the agent same-origin; see the amendment.) ### What went wrong with writing the stores first @@ -174,7 +179,10 @@ one applier, and it is not in the frontend. - **Whole-graph replace as the mutation primitive.** Clobbers concurrent agent edits mid-turn and kills op-log replay. -**Distribution boundaries.** Keep one branch and one follower implementation, +**Distribution boundaries.** _Superseded 2026-10-12 except for its first and +last rules: one follower with no distribution checks in its apply path or +rendering, and Local and Desktop stay authenticated surfaces (the amendment's +one `isCloud` credential branch replaces delegating to `authStore.getAuthHeader()`)._ Keep one branch and one follower implementation, with surface differences isolated behind a small distribution-resolved boundary (rejecting both separate per-surface branches and distribution checks scattered through the follower core). Introduce a narrow agent connection/configuration @@ -188,11 +196,13 @@ reduce auth to `isCloud` (Local and Desktop are authenticated product surfaces). Dev-only Vite proxy/credential behavior stays in Vite config and is never treated as Local product behavior. -Today the follower uses the centralized ComfyUI `api` transport (`api.socket` +_Superseded 2026-10-12._ Today the follower uses the centralized ComfyUI `api` transport (`api.socket` in `src/scripts/api.ts`), which is already distribution-resolved, so no `AGENT_BASE_URL` is wired yet; this ADR records the module as the shape to introduce when the follower stops using `api.socket`. +_Superseded 2026-10-12 (the diagram shows the original module):_ + ```text compile-time __DISTRIBUTION__ ┌────────────────────────┼────────────────────────┐ @@ -268,7 +278,8 @@ guard. Interior (`path`) widget writes are not held. A page reload still replays the whole document over an empty graph, so a field outside the op vocabulary (node color) reverts there. -- The largest distribution risk is unchanged: accidental cloud coupling in the +- _Superseded 2026-10-12 (see the amendment's remaining risk)._ The largest + distribution risk is unchanged: accidental cloud coupling in the same-origin `/ws` transport. Boundary tests plus at least one browser-observable E2E per shipping topology are required. @@ -358,6 +369,52 @@ dropped as own echoes. This frontend gate is not a server authorization boundary. No Local or Desktop transport is claimed operational solely because its feature gate is enabled. +## Amendment (2026-10-12): one agent socket on every backend + +This supersedes the "Today the follower uses `api.socket`" paragraph and the +`/ws` risk bullet above. The follower no longer uses `api.socket`. Cloud ingest +and the local agent now serve the same contract, so the connection module +described above is not needed: + +- **One socket.** Agent events and document frames share + `/api/agent/events`. Its path comes from `api.apiURL`, as every agent REST + path does. The transport has no distribution branch. +- **Identity from the backend.** Ops carry `human::` with the id + from `GET /api/agent/identity`. The Firebase uid is not the backend user id + for identity-mapped accounts. +- **Credential, one `isCloud` branch.** In the cloud, agent REST requests + authenticate through `fetchApi` alone, and the socket takes the auth params + `/ws` uses (`api.socketAuthParams`: the session cookie with `workspace_id`, + or the cloud token; no socket when no one is signed in). Ingest prefers a + header credential over the session cookie, so adding the user's own header + there would move a request out of its workspace. Off-cloud, `fetchApi` + attaches nothing, so REST requests carry the user's personal auth header + (`getUserAuthHeader`) and the socket carries the same credential as + `?token=`; a running turn re-sends it. Whether off-cloud agent calls should + bill the active team workspace, as `queuePrompt` does with + `getWorkspaceAuthToken`, is still open. +- **Development exception.** In development mode, a signed-out send skips the + sign-in dialog only when the dev proxy injects `DEV_AGENT_COMFY_TOKEN`. Vite + tells the browser so with the build-time flag + `VITE_DEV_AGENT_PROXY_CREDENTIAL`; the token itself stays in the proxy. + +Rejected: the distribution-resolved connection module above, an +`AGENT_BASE_URL` resolving same-origin ingest in the cloud and the agent +binary directly on Local and Desktop. Both backends now serve the same paths +behind the ComfyUI origin, so the module would have nothing to resolve. Also +rejected: keeping `/ws` in the cloud and adding a second socket for the local +agent, which keeps two transports in the follower. + +Accepted cost: Local and Desktop product wiring is still unshipped (the panel +mounts only in cloud bundles and the standalone harness), and Global Settings +still picks its URL and header on `isCloud`. + +The remaining risk is the agent socket itself. The Playwright agent suites +serve chat and document frames on `/api/agent/events` from a fake host +(`AgentFollowerHostSocket`); none runs against ingest or the local agent in +CI, and the `@agent-harness` smoke needs the local harness. At least one +browser-observable E2E per shipping topology is still required. + ## Notes This ADR mirrors two cross-repo workspace decisions (ADR-010 follower diff --git a/docs/architecture/domains/boundaries.json b/docs/architecture/domains/boundaries.json index a82d392945b..388d4bb4a63 100644 --- a/docs/architecture/domains/boundaries.json +++ b/docs/architecture/domains/boundaries.json @@ -99,7 +99,9 @@ "src/workbench/extensions/agent/services/agent/undeliverableAskReporter.ts", "src/workbench/extensions/agent/stores/**", "src/workbench/extensions/agent/utils/getPageVisibilityMetadata.test.ts", - "src/workbench/extensions/agent/utils/getPageVisibilityMetadata.ts" + "src/workbench/extensions/agent/utils/getPageVisibilityMetadata.ts", + "src/workbench/extensions/agent/services/agent/agentIdentity.ts", + "src/workbench/extensions/agent/services/agent/agentIdentity.test.ts" ] }, { @@ -107,7 +109,6 @@ "patterns": [ "src/workbench/extensions/agent/crdt/__fixtures__/docFrameClient.ts", "src/workbench/extensions/agent/crdt/agentCrdtTransport.ts", - "src/workbench/extensions/agent/crdt/apiTransport.test.ts", "src/workbench/extensions/agent/crdt/crdtDebugGate.test.ts", "src/workbench/extensions/agent/crdt/crdtDebugGate.ts", "src/workbench/extensions/agent/crdt/crdtDebugReport.test.ts", @@ -132,12 +133,17 @@ "src/workbench/extensions/agent/services/agent/agentEventSource.ts", "src/workbench/extensions/agent/services/agent/agentRestClient.test.ts", "src/workbench/extensions/agent/services/agent/agentRestClient.ts", - "src/workbench/extensions/agent/services/agent/standaloneAgentEventSource.test.ts", - "src/workbench/extensions/agent/services/agent/standaloneAgentEventSource.ts", "src/workbench/extensions/agent/utils/resolveReplyAssetDownload.test.ts", "src/workbench/extensions/agent/utils/resolveReplyAssetDownload.ts", "src/workbench/extensions/agent/utils/videoThumbnail.test.ts", - "src/workbench/extensions/agent/utils/videoThumbnail.ts" + "src/workbench/extensions/agent/utils/videoThumbnail.ts", + "src/workbench/extensions/agent/services/agent/agentAuth.ts", + "src/workbench/extensions/agent/services/agent/agentAuth.test.ts", + "src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts", + "src/workbench/extensions/agent/crdt/agentDocFrameTransport.ts", + "src/workbench/extensions/agent/crdt/agentDocFrameTransport.test.ts", + "src/workbench/extensions/agent/crdt/__fixtures__/agentSocket.ts", + "src/workbench/extensions/agent/crdt/agentCrdtTransport.test.ts" ] }, { @@ -4125,6 +4131,7 @@ "from": "agent/domain", "allow": [ "agent/domain", + "authentication/domain", "design-system/domain", "design-system/domain/outer-1", "unclassified" @@ -4136,9 +4143,13 @@ "agent/domain", "agent/application", "agent/infrastructure", + "authentication/domain", + "authentication/application", + "authentication/infrastructure", "design-system/domain", "design-system/application", "design-system/infrastructure", + "authentication/infrastructure/outer-1", "design-system/domain/outer-1", "unclassified" ] @@ -4149,9 +4160,13 @@ "agent/domain", "agent/application", "agent/infrastructure", + "authentication/domain", + "authentication/application", + "authentication/infrastructure", "design-system/domain", "design-system/application", "design-system/infrastructure", + "authentication/infrastructure/outer-1", "design-system/domain/outer-1", "unclassified" ] @@ -4162,6 +4177,9 @@ "agent/domain", "agent/application", "agent/presentation", + "authentication/domain", + "authentication/application", + "authentication/presentation", "design-system/domain", "design-system/application", "design-system/presentation", @@ -4178,11 +4196,17 @@ "agent/infrastructure", "agent/presentation", "agent/integration", + "authentication/domain", + "authentication/application", + "authentication/infrastructure", + "authentication/presentation", + "authentication/integration", "design-system/domain", "design-system/application", "design-system/infrastructure", "design-system/presentation", "agent/integration/outer-1", + "authentication/infrastructure/outer-1", "design-system/domain/outer-1", "design-system/presentation/outer-1", "unclassified" @@ -6046,14 +6070,25 @@ }, { "from": "settings/domain", - "allow": ["settings/domain", "unclassified"] + "allow": [ + "authentication/domain", + "dialog-host/domain", + "settings/domain", + "unclassified" + ] }, { "from": "settings/application", "allow": [ + "authentication/domain", + "authentication/application", + "authentication/infrastructure", + "dialog-host/domain", + "dialog-host/application", "settings/domain", "settings/application", "settings/infrastructure", + "authentication/infrastructure/outer-1", "settings/application/outer-1", "unclassified" ] @@ -6061,9 +6096,15 @@ { "from": "settings/infrastructure", "allow": [ + "authentication/domain", + "authentication/application", + "authentication/infrastructure", + "dialog-host/domain", + "dialog-host/application", "settings/domain", "settings/application", "settings/infrastructure", + "authentication/infrastructure/outer-1", "settings/application/outer-1", "unclassified" ] @@ -6071,22 +6112,38 @@ { "from": "settings/presentation", "allow": [ + "authentication/domain", + "authentication/application", + "authentication/presentation", + "dialog-host/domain", + "dialog-host/application", "settings/domain", "settings/application", "settings/presentation", "settings/application/outer-1", + "dialog-host/presentation/outer-2", "unclassified" ] }, { "from": "settings/integration", "allow": [ + "authentication/domain", + "authentication/application", + "authentication/infrastructure", + "authentication/presentation", + "authentication/integration", + "dialog-host/domain", + "dialog-host/application", + "dialog-host/integration", "settings/domain", "settings/application", "settings/infrastructure", "settings/presentation", "settings/integration", + "authentication/infrastructure/outer-1", "settings/application/outer-1", + "dialog-host/presentation/outer-2", "unclassified" ] }, @@ -6759,11 +6816,17 @@ "agent/infrastructure", "agent/presentation", "agent/integration", + "authentication/domain", + "authentication/application", + "authentication/infrastructure", + "authentication/presentation", + "authentication/integration", "design-system/domain", "design-system/application", "design-system/infrastructure", "design-system/presentation", "agent/integration/outer-1", + "authentication/infrastructure/outer-1", "design-system/domain/outer-1", "design-system/presentation/outer-1", "unclassified" @@ -7327,9 +7390,15 @@ { "from": "settings/application/outer-1", "allow": [ + "authentication/domain", + "authentication/application", + "authentication/infrastructure", + "dialog-host/domain", + "dialog-host/application", "settings/domain", "settings/application", "settings/infrastructure", + "authentication/infrastructure/outer-1", "settings/application/outer-1", "unclassified" ] diff --git a/docs/architecture/domains/records/agent.domain.json b/docs/architecture/domains/records/agent.domain.json index ffe3ea08281..9c17830ffce 100644 --- a/docs/architecture/domains/records/agent.domain.json +++ b/docs/architecture/domains/records/agent.domain.json @@ -263,10 +263,6 @@ "path": "src/workbench/extensions/agent/crdt/agentSubgraphInsertWorkflow.regression.test.ts", "role": "integration" }, - { - "path": "src/workbench/extensions/agent/crdt/apiTransport.test.ts", - "role": "infrastructure" - }, { "path": "src/workbench/extensions/agent/crdt/applierConformance.test.ts", "role": "domain" @@ -627,14 +623,6 @@ "path": "src/workbench/extensions/agent/services/agent/agentTranscript.ts", "role": "domain" }, - { - "path": "src/workbench/extensions/agent/services/agent/standaloneAgentEventSource.test.ts", - "role": "infrastructure" - }, - { - "path": "src/workbench/extensions/agent/services/agent/standaloneAgentEventSource.ts", - "role": "infrastructure" - }, { "path": "src/workbench/extensions/agent/services/agent/targetAwareAgentNavigation.test.ts", "role": "domain" @@ -762,10 +750,46 @@ { "path": "src/workbench/extensions/agent/utils/workflowReferenceText.ts", "role": "domain" + }, + { + "path": "src/workbench/extensions/agent/services/agent/agentAuth.ts", + "role": "infrastructure" + }, + { + "path": "src/workbench/extensions/agent/services/agent/agentAuth.test.ts", + "role": "infrastructure" + }, + { + "path": "src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts", + "role": "infrastructure" + }, + { + "path": "src/workbench/extensions/agent/services/agent/agentIdentity.ts", + "role": "application" + }, + { + "path": "src/workbench/extensions/agent/services/agent/agentIdentity.test.ts", + "role": "application" + }, + { + "path": "src/workbench/extensions/agent/crdt/agentDocFrameTransport.ts", + "role": "infrastructure" + }, + { + "path": "src/workbench/extensions/agent/crdt/agentDocFrameTransport.test.ts", + "role": "infrastructure" + }, + { + "path": "src/workbench/extensions/agent/crdt/__fixtures__/agentSocket.ts", + "role": "infrastructure" + }, + { + "path": "src/workbench/extensions/agent/crdt/agentCrdtTransport.test.ts", + "role": "infrastructure" } ], "publicEntryPoints": [], - "allowedDependencies": ["design-system"], + "allowedDependencies": ["authentication", "design-system"], "allowedConsumers": [], "characterizationScenarios": [ { diff --git a/docs/architecture/domains/records/authentication.domain.json b/docs/architecture/domains/records/authentication.domain.json index 55d1e0270a6..d4d1dca5d25 100644 --- a/docs/architecture/domains/records/authentication.domain.json +++ b/docs/architecture/domains/records/authentication.domain.json @@ -354,7 +354,7 @@ ], "publicEntryPoints": [], "allowedDependencies": [], - "allowedConsumers": [], + "allowedConsumers": ["agent", "settings"], "characterizationScenarios": [ { "name": "Firebase identity drives sign-in, sign-out, current-user projection, and auth headers", diff --git a/docs/architecture/domains/records/dialog-host.domain.json b/docs/architecture/domains/records/dialog-host.domain.json index be897d0f299..552bbc04a24 100644 --- a/docs/architecture/domains/records/dialog-host.domain.json +++ b/docs/architecture/domains/records/dialog-host.domain.json @@ -50,7 +50,7 @@ ], "publicEntryPoints": [], "allowedDependencies": [], - "allowedConsumers": [], + "allowedConsumers": ["settings"], "characterizationScenarios": [ { "name": "Global dialog renders stacked dialogs and restores body pointer events on close", diff --git a/docs/architecture/domains/records/settings.domain.json b/docs/architecture/domains/records/settings.domain.json index ad0801ec621..5d9697ae162 100644 --- a/docs/architecture/domains/records/settings.domain.json +++ b/docs/architecture/domains/records/settings.domain.json @@ -145,7 +145,7 @@ } ], "publicEntryPoints": [], - "allowedDependencies": [], + "allowedDependencies": ["authentication", "dialog-host"], "allowedConsumers": [], "characterizationScenarios": [ { diff --git a/docs/testing/agent-integration-development.md b/docs/testing/agent-integration-development.md index d77556baa94..f5c4a59658a 100644 --- a/docs/testing/agent-integration-development.md +++ b/docs/testing/agent-integration-development.md @@ -91,8 +91,9 @@ Paste the recorder command it prints, one `--prompt` per turn. DEV_AGENT_COMFY_TOKEN=comfyui-… pnpm tsx scripts/dev-agent-integration.ts ``` - Vite forwards it to the standalone agent as `X-Comfy-Token`. Without it, the panel - starts but model requests return 401. + Vite forwards it to the standalone agent: a token that starts with `comfyui-` as + `X-API-KEY`, anything else as a bearer token. The panel then sends without asking a + signed-out user to sign in. Without it, a signed-out send opens the sign-in dialog. The root dependency on `@comfyorg/comfy-multi-player` may use the published npm package or `workspace:`. The launcher warns that package source edits will not @@ -131,9 +132,12 @@ until the agent publishes `agent.json`, then succeed. ## How it works -The Vite proxy keeps the standalone session token server-side. Browser REST and event -WebSocket traffic use same-origin `/api/agent` routes, while all other `/api` and `/ws` -traffic continues to reach ComfyUI. +The Vite proxy keeps the standalone session token server-side and sends it as +`X-Comfy-Agent-Session`. Without `DEV_AGENT_COMFY_TOKEN`, the browser's own `Authorization` +or `X-API-KEY` reaches the agent as the user's credential; with it, the proxy's header +replaces the browser's header of the same name. Browser REST and event WebSocket traffic use +same-origin `/api/agent` routes, and the saved-workflow index uses `/api/workflows`, while +all other `/api` and `/ws` traffic continues to reach ComfyUI. ### Modes diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index 0c1a5f4cd64..0e3088d9cab 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -1543,7 +1543,7 @@ }, "src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts": { "comfy/no-reexports": { - "count": 3 + "count": 2 } }, "src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts": { diff --git a/scripts/agentConversationAssemble.ts b/scripts/agentConversationAssemble.ts index 6236fafe96a..15ddce41fb7 100644 --- a/scripts/agentConversationAssemble.ts +++ b/scripts/agentConversationAssemble.ts @@ -19,7 +19,7 @@ import { import type { HostDoc } from '../browser_tests/fixtures/agentConversationHostDoc' import type { AgentWsEvent } from '../src/workbench/extensions/agent/schemas/agentApiSchema' import { - AGENT_WS_EVENT_TYPES, + isAgentEvent, zAgentTurnAccepted, zAgentWsEvent } from '../src/workbench/extensions/agent/schemas/agentApiSchema' @@ -239,47 +239,26 @@ function turnIds(turn: RecordedTurn, label: string): TurnIds { return { threadId: ack.data.thread_id, messageId: ack.data.message_id } } -function keepTurnFrames( - frames: RecordedFrame[], - ids: TurnIds[], - seedTurn: TurnIds | null -): { kept: KeptFrame[][]; dropped: Record } { - const kept: KeptFrame[][] = ids.map(() => []) - const dropped: Record = {} - const drop = (bucket: string): void => { - dropped[bucket] = (dropped[bucket] ?? 0) + 1 - } - - for (const frame of frames) { - const { type, data } = frame - const turn = ids.findIndex( - (id) => data.thread_id === id.threadId && data.message_id === id.messageId - ) - // Unparseable payloads belong to no turn; the heartbeat carries no ids. - if (type === '__raw__' || type === 'draft_version') - drop(type === '__raw__' ? 'unparseable' : 'type:draft_version') - else if (turn === -1) - drop( - seedTurn !== null && - data.thread_id === seedTurn.threadId && - data.message_id === seedTurn.messageId - ? 'seed_turn' - : 'foreign' - ) - else if ((AGENT_WS_EVENT_TYPES as ReadonlySet).has(type)) - kept[turn].push({ - event: parseOrRefuse( - zAgentWsEvent, - { type, data }, - `${turnLabel(turn)} ${type} frame` - ), - at_ms: frame.at_ms - }) - else drop(`type:${type}`) - } +function isSameTurn(data: RecordedFrame['data'], id: TurnIds): boolean { + return data.thread_id === id.threadId && data.message_id === id.messageId +} - if (dropped.unparseable) - refuse(`${dropped.unparseable} unparseable socket payload(s) recorded`) +function dropBucket( + { type, data }: RecordedFrame, + turn: number, + seedTurn: TurnIds | null +): string | null { + // Unparseable payloads belong to no turn; the heartbeat carries no ids. + if (type === '__raw__') return 'unparseable' + if (type === 'draft_version') return 'type:draft_version' + if (turn === -1) + return seedTurn !== null && isSameTurn(data, seedTurn) + ? 'seed_turn' + : 'foreign' + return isAgentEvent(type) ? null : `type:${type}` +} + +function assertTurnsClosed(kept: KeptFrame[][]): void { for (const [index, turn] of kept.entries()) { const dones = turn.filter( ({ event }) => event.type === 'agent_message_done' @@ -294,6 +273,37 @@ function keepTurnFrames( `last kept frame of ${turnLabel(index)} is ${last?.event.type}, not agent_message_done` ) } +} + +function keepTurnFrames( + frames: RecordedFrame[], + ids: TurnIds[], + seedTurn: TurnIds | null +): { kept: KeptFrame[][]; dropped: Record } { + const kept: KeptFrame[][] = ids.map(() => []) + const dropped: Record = {} + + for (const frame of frames) { + const { type, data } = frame + const turn = ids.findIndex((id) => isSameTurn(data, id)) + const bucket = dropBucket(frame, turn, seedTurn) + if (bucket !== null) { + dropped[bucket] = (dropped[bucket] ?? 0) + 1 + continue + } + kept[turn].push({ + event: parseOrRefuse( + zAgentWsEvent, + { type, data }, + `${turnLabel(turn)} ${type} frame` + ), + at_ms: frame.at_ms + }) + } + + if (dropped.unparseable) + refuse(`${dropped.unparseable} unparseable socket payload(s) recorded`) + assertTurnsClosed(kept) return { kept, dropped } } diff --git a/scripts/vite-config-dev-agent.test.ts b/scripts/vite-config-dev-agent.test.ts index 0bb4b5667ee..5bb938e6ad0 100644 --- a/scripts/vite-config-dev-agent.test.ts +++ b/scripts/vite-config-dev-agent.test.ts @@ -7,13 +7,16 @@ import { promisify } from 'node:util' import { afterAll, describe, expect, it } from 'vitest' -import { createDevAgentConfig } from '../build/devAgentConfig' +import { + createDevAgentConfig, + devServerAgentRoutes +} from '../build/devAgentConfig' const execFileAsync = promisify(execFile) const agentDataDir = mkdtempSync(join(tmpdir(), 'vite-agent-config-')) afterAll(() => rmSync(agentDataDir, { force: true, recursive: true })) const printAgentConfig = - "import('./vite.config.mts').then(({ default: config }) => process.stdout.write(JSON.stringify({ headers: config.server?.proxy?.['/api/agent']?.headers, host: config.server?.host })))" + "import('./vite.config.mts').then(({ default: config }) => process.stdout.write(JSON.stringify({ headers: config.server?.proxy?.['/api/agent']?.headers, workflowsHeaders: config.server?.proxy?.['/api/workflows']?.headers, host: config.server?.host, proxyCredential: config.define?.['import.meta.env.VITE_DEV_AGENT_PROXY_CREDENTIAL'] })))" describe('dev agent proxy transport', () => { it.for([ @@ -29,7 +32,7 @@ describe('dev agent proxy transport', () => { expect(host).toBeUndefined() expect(proxy).toMatchObject({ target: url, - headers: { Authorization: 'Bearer test-session-token' } + headers: { 'X-Comfy-Agent-Session': 'test-session-token' } }) }) @@ -73,7 +76,7 @@ describe('dev agent proxy transport', () => { DEV_AGENT_DATA_DIR: '' }) expect(proxy).toMatchObject({ - headers: { Authorization: 'Bearer test-session-token' } + headers: { 'X-Comfy-Agent-Session': 'test-session-token' } }) }) @@ -107,7 +110,8 @@ describe('dev agent proxy transport', () => { it('leaves the proxy disabled when no target or token is set', () => { expect(createDevAgentConfig({})).toEqual({ host: undefined, - proxy: undefined + proxy: undefined, + proxySuppliesCredential: false }) }) }) @@ -124,12 +128,55 @@ describe('dev agent comfy credential', () => { expect(proxy).toMatchObject({ target: 'http://127.0.0.1:8095', headers: { - Authorization: 'Bearer test-session-token', - 'X-Comfy-Token': 'comfyui-test-key' + 'X-Comfy-Agent-Session': 'test-session-token', + 'X-API-KEY': 'comfyui-test-key' } }) }) + it('presents a non-key token as the bearer, the way ingest reads it', () => { + expect( + createDevAgentConfig({ + DEV_AGENT_URL: 'http://127.0.0.1:8095', + DEV_AGENT_SESSION_TOKEN: 'test-session-token', + DEV_AGENT_COMFY_TOKEN: 'a-firebase-jwt' + }).proxy?.headers + ).toEqual({ + 'X-Comfy-Agent-Session': 'test-session-token', + Authorization: 'Bearer a-firebase-jwt' + }) + }) + + it.for([ + { + label: 'a proxy with a comfy credential', + env: { + DEV_AGENT_URL: 'http://127.0.0.1:8095', + DEV_AGENT_SESSION_TOKEN: 'test-session-token', + DEV_AGENT_COMFY_TOKEN: 'comfyui-test-key' + }, + supplies: true + }, + { + label: 'a proxy without one', + env: { + DEV_AGENT_URL: 'http://127.0.0.1:8095', + DEV_AGENT_SESSION_TOKEN: 'test-session-token' + }, + supplies: false + }, + { + label: 'a comfy credential without a proxy', + env: { DEV_AGENT_COMFY_TOKEN: 'comfyui-test-key' }, + supplies: false + } + ])( + 'reports whether the proxy supplies a credential for $label', + ({ env, supplies }) => { + expect(createDevAgentConfig(env).proxySuppliesCredential).toBe(supplies) + } + ) + it('allows remote development without a comfy credential', () => { expect(createDevAgentConfig({ VITE_REMOTE_DEV: 'true' }).host).toBe( '0.0.0.0' @@ -152,13 +199,39 @@ describe('dev agent comfy credential', () => { DEV_AGENT_SESSION_TOKEN: 'test-session-token', DEV_AGENT_COMFY_TOKEN: 'comfyui-test-key', VITE_AGENT_STANDALONE: 'true', - VITE_REMOTE_DEV: 'true' + VITE_REMOTE_DEV: 'true', + DISTRIBUTION: undefined } } ) - expect(stdout).toBe( - '{"headers":{"Authorization":"Bearer test-session-token","X-Comfy-Token":"comfyui-test-key"}}' - ) + const headers = { + 'X-Comfy-Agent-Session': 'test-session-token', + 'X-API-KEY': 'comfyui-test-key' + } + expect(JSON.parse(stdout)).toEqual({ + headers, + workflowsHeaders: headers, + proxyCredential: '"true"' + }) } ) }) + +describe('dev server agent routes', () => { + const agentProxy = { target: 'http://127.0.0.1:8095', ws: true } + const backend = { target: 'https://cloud.example.com' } + + it('upgrades the events socket on the backend without a local agent', () => { + expect(devServerAgentRoutes(undefined, backend)).toEqual({ + '/api/agent/events': { target: 'https://cloud.example.com', ws: true } + }) + }) + + it('routes the agent and the saved-workflow index to a local agent first', () => { + expect(Object.entries(devServerAgentRoutes(agentProxy, backend))).toEqual([ + ['/api/workflows', agentProxy], + ['/api/agent', agentProxy], + ['/api/agent/events', { target: 'https://cloud.example.com', ws: true }] + ]) + }) +}) diff --git a/src/platform/settings/globalSettingsApi.test.ts b/src/platform/settings/globalSettingsApi.test.ts index 847301b2e33..2f515080f3d 100644 --- a/src/platform/settings/globalSettingsApi.test.ts +++ b/src/platform/settings/globalSettingsApi.test.ts @@ -1,11 +1,15 @@ +vi.mock(import('firebase/auth')) +vi.mock(import('@/services/dialogService')) import type { GlobalSetting } from '@comfyorg/ingest-types' import { beforeEach, describe, expect, it, vi } from 'vitest' import { api } from '@/scripts/api' +import { useAuthStore } from '@/stores/authStore' import { GlobalSettingsApiError, getGlobalSetting, + getGlobalSettingsAuthHeader, setGlobalSetting } from './globalSettingsApi' @@ -17,6 +21,8 @@ vi.mock(import('@/platform/distribution/types'), () => distribution) vi.mock(import('@/scripts/api')) const fetchWithUnifiedRemint = vi.hoisted(() => vi.fn()) vi.mock(import('@/platform/auth/unified/remintRetry'), () => ({ + attachUnifiedRemintInterceptor: vi.fn(), + provideUnifiedRemint: vi.fn(), fetchWithUnifiedRemint, shouldRemintCloudRequest: () => Promise.resolve(false) })) @@ -41,7 +47,7 @@ describe('Global Settings transport', () => { }) it.for([true, false])( - 'reads with captured workspace credentials (Cloud=%s)', + 'reads with the credential it is handed (Cloud=%s)', async (cloud) => { distribution.isCloud = cloud respondWith(stored) @@ -132,4 +138,25 @@ describe('Global Settings transport', () => { setGlobalSetting({ key, value: true }, authHeader) ).rejects.toMatchObject({ status: 500 }) }) + + it.for([ + { cloud: true, expected: 'Bearer workspace-token' }, + { cloud: false, expected: 'Bearer user-token' } + ])( + 'authenticates Cloud=$cloud with $expected', + async ({ cloud, expected }) => { + distribution.isCloud = cloud + const authStore = useAuthStore() + vi.mocked(authStore.getWorkspaceAuthHeader).mockResolvedValue({ + Authorization: 'Bearer workspace-token' + }) + vi.mocked(authStore.getUserAuthHeader).mockResolvedValue({ + Authorization: 'Bearer user-token' + }) + + await expect(getGlobalSettingsAuthHeader()).resolves.toEqual({ + Authorization: expected + }) + } + ) }) diff --git a/src/platform/settings/globalSettingsApi.ts b/src/platform/settings/globalSettingsApi.ts index 095d46db24c..905c5d458d5 100644 --- a/src/platform/settings/globalSettingsApi.ts +++ b/src/platform/settings/globalSettingsApi.ts @@ -13,6 +13,7 @@ import { import { isCloud } from '@/platform/distribution/types' import { api } from '@/scripts/api' import type { WebSessionSend } from '@/platform/auth/session/webSessionFetch' +import { useAuthStore } from '@/stores/authStore' import type { AuthHeader } from '@/types/authTypes' export class GlobalSettingsApiError extends Error { @@ -30,6 +31,13 @@ function globalSettingsUrl(key?: GlobalSettingKey): string { return isCloud ? api.apiURL(path) : `${getComfyApiBaseUrl()}/api${path}` } +export async function getGlobalSettingsAuthHeader(): Promise { + const authStore = useAuthStore() + return isCloud + ? authStore.getWorkspaceAuthHeader() + : authStore.getUserAuthHeader() +} + async function responseBody(response: Response): Promise { try { return await response.json() diff --git a/src/scripts/__mocks__/api.ts b/src/scripts/__mocks__/api.ts index f095725ef90..008c55746f3 100644 --- a/src/scripts/__mocks__/api.ts +++ b/src/scripts/__mocks__/api.ts @@ -77,6 +77,7 @@ const actions = vi.mockObject< | 'getLogs' | 'getRawLogs' | 'getShareableAssets' + | 'socketAuthParams' > >( { @@ -142,7 +143,8 @@ const actions = vi.mockObject< getRawLogs: async () => ({ entries: [], size: { cols: 80, row: 24 } }), async getShareableAssets() { assert.fail('Configure api.getShareableAssets for this test') - } + }, + socketAuthParams: async () => new URLSearchParams() }, { spy: true } ) diff --git a/src/scripts/api.fetchApi.test.ts b/src/scripts/api.fetchApi.test.ts index b25618a31a5..055d7ccbb1b 100644 --- a/src/scripts/api.fetchApi.test.ts +++ b/src/scripts/api.fetchApi.test.ts @@ -389,6 +389,47 @@ describe('api.fetchApi', () => { }) }) + describe('socketAuthParams', () => { + it.for([ + { + name: 'a web session takes its workspace and no token', + scope: { session: fromPartial({}), epoch: 1, workspaceId: 'ws-1' }, + token: 'cloud-token', + expected: 'workspace_id=ws-1' + }, + { + name: 'a web session with no one signed in opens nothing', + scope: undefined, + token: undefined, + expected: null + } + ])('$name', async ({ scope, token, expected }) => { + api.setAuthProvider({ + ...anonymousApiAuthProvider, + getAuthToken: vi.fn().mockResolvedValue(token) + }) + const release = provideWebSessionRequests( + fromPartial({ scope: async () => scope }) + ) + + try { + const params = await api.socketAuthParams() + expect(params === null ? null : params.toString()).toBe(expected) + } finally { + release() + } + }) + + it('takes the cloud token without a web session', async () => { + api.setAuthProvider({ + ...anonymousApiAuthProvider, + getAuthToken: vi.fn().mockResolvedValue('cloud-token') + }) + + expect((await api.socketAuthParams())?.get('token')).toBe('cloud-token') + }) + }) + describe('default options', () => { it('should set cache to no-cache by default', async () => { vi.mocked(global.fetch).mockResolvedValue(new Response()) @@ -430,6 +471,19 @@ describe('api.fetchApi', () => { }) ) }) + + it('forwards the redirect policy to fetch', async () => { + const mockFetch = vi + .mocked(global.fetch) + .mockResolvedValue(new Response()) + + await api.fetchApi('/test', { redirect: 'error' }) + + expect(mockFetch).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ redirect: 'error' }) + ) + }) }) describe('URL construction', () => { diff --git a/src/scripts/api.logs.test.ts b/src/scripts/api.logs.test.ts index 4a00224966b..b2149e47ecd 100644 --- a/src/scripts/api.logs.test.ts +++ b/src/scripts/api.logs.test.ts @@ -46,6 +46,7 @@ describe('api.getLogs', () => { workflowId: 'doc-1', updatesApplied: 0, lastFrameType: null, + terminal: null, outcomes: { received: 0, applied: 0, diff --git a/src/scripts/api.ts b/src/scripts/api.ts index 20a5bb0aca1..b0b77ca91d8 100644 --- a/src/scripts/api.ts +++ b/src/scripts/api.ts @@ -887,6 +887,12 @@ export class ComfyApi extends EventTarget { return !requests || params.has('token') } + /** The cloud socket's auth params; null when no one is signed in to open it. */ + async socketAuthParams(): Promise { + const params = new URLSearchParams() + return (await this.addSocketAuth(params)) ? params : null + } + /** * Creates and connects a WebSocket for realtime updates * @param {boolean} isReconnect If the socket is connection is a reconnect attempt diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index b9e57e851e0..2e64bd1aa6a 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -19,6 +19,7 @@ declare global { interface ImportMetaEnv { VITE_APP_VERSION?: string + VITE_DEV_AGENT_PROXY_CREDENTIAL?: string VITE_BILLING_WEB_URL?: string VITE_STAGING_API_BASE_URL?: string VITE_STAGING_CLOUD_BASE_URL?: string diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index c4e1173bb03..60b0f54c0d9 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -8,9 +8,10 @@ import type { BillingStatus, SubscriptionTier } from '@comfyorg/ingest-types' +import type { User } from 'firebase/auth' import { render, screen, waitFor, within } from '@testing-library/vue' import userEvent from '@testing-library/user-event' -import { assert, beforeEach, describe, expect, it, vi } from 'vitest' +import { afterEach, assert, beforeEach, describe, expect, it, vi } from 'vitest' import type { Mocked } from 'vitest' import { computed, @@ -23,8 +24,10 @@ import { } from 'vue' import type { Ref } from 'vue' import { useClipboard } from '@vueuse/core' +import { z } from 'zod' vi.mock(import('firebase/auth')) +vi.mock(import('@/composables/auth/useAuthDialogs')) import { i18n } from '@/i18n' import { useCurrentUser } from '@/composables/auth/useCurrentUser' @@ -50,9 +53,12 @@ import { useTelemetry } from '@/platform/telemetry' import type { AgentConsentTrigger } from '@/platform/telemetry/types' import { useBillingCapabilities } from '@/platform/workspace/composables/useBillingCapabilities' import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI' -import { app } from '@/scripts/app' import { api } from '@/scripts/api' +import { app } from '@/scripts/app' import NodeSelectionModeBanner from '@/components/graph/NodeSelectionModeBanner.vue' +import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore' +import { useAuthStore } from '@/stores/authStore' +import { useAuthDialogs } from '@/composables/auth/useAuthDialogs' import { useWorkflowTabActivityStore } from '@/stores/workflowTabActivityStore' import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore' import { useToast } from '@/components/ui/toast/toastStore' @@ -85,10 +91,12 @@ import { createTestDragAndScale } from '@/utils/__tests__/canvasTestUtils' -const apiEvents: EventTarget = api - +const getServerFeature = vi.hoisted(() => + vi.fn((_name: string, defaultValue?: unknown) => defaultValue) +) const focusNodeInstance = vi.hoisted(() => vi.fn()) -const socketSend = vi.fn() +const socketSend = vi.hoisted(() => vi.fn()) +let agentSocket: ReturnType const withConsent = vi.hoisted(() => vi.fn<(trigger: AgentConsentTrigger, onAccept: () => void) => Promise>( async (_trigger, onAccept) => onAccept() @@ -106,7 +114,43 @@ vi.mock(import('@/composables/canvas/useFocusNode'), () => ({ vi.mock(import('@/platform/telemetry/reportError')) vi.mock(import('./experiments/freeUsePlacement'), { spy: true }) -vi.mock(import('@/scripts/api')) +const ws = vi.hoisted(() => { + type Listener = (event: { detail?: unknown }) => void + const listeners = new Map>() + const add = (type: string, listener: Listener): void => { + const set = listeners.get(type) ?? new Set() + set.add(listener) + listeners.set(type, set) + } + const remove = (type: string, listener: Listener): void => { + listeners.get(type)?.delete(listener) + } + const emit = (type: string, data?: unknown): void => { + // The doc-frame pipeline ignores events that are not CustomEvent instances. + const event = new CustomEvent(type, { detail: data }) + for (const listener of listeners.get(type) ?? []) listener(event) + agentSocket.receive(type, data) + } + const clear = (): void => { + listeners.clear() + } + return { add, remove, emit, clear } +}) + +vi.mock(import('@/scripts/api'), () => ({ + api: { + apiURL: vi.fn((route: string) => `/api${route}`), + fetchApi: (route: string, options?: RequestInit) => + fetch(route.startsWith('/api') ? route : `/api${route}`, options), + getServerFeature, + socket: { readyState: 1, send: socketSend }, + dispatchCustomEvent: vi.fn(), + addEventListener: ws.add, + removeEventListener: ws.remove, + addCustomEventListener: ws.add, + removeCustomEventListener: ws.remove + } +})) const appMock = vi.hoisted(() => { const graph = { @@ -223,6 +267,9 @@ const paywallAgentScopedHasFunds = ref() vi.mock(import('@/platform/workspace/composables/useWorkspaceUI'), { spy: true }) +vi.mock(import('./services/agent/agentEventSource'), { spy: true }) +vi.mock(import('./services/agent/agentIdentity'), { spy: true }) +vi.mock(import('./crdt/devPanelLog'), { spy: true }) vi.mock(import('@/composables/billing/useBillingContext'), { spy: true }) vi.mock(import('@/platform/workspace/composables/useBillingCapabilities'), { spy: true @@ -243,13 +290,19 @@ import { useAgentGraphActivityStore } from './stores/agent/agentGraphActivitySto import { useAgentPanelStore } from './stores/agent/agentPanelStore' import { useAgentComposerStore } from './stores/agent/agentComposerStore' import { useAgentWorkflowTabBindingStore } from './stores/agent/agentWorkflowTabBindingStore' +import { createFakeAgentSocket } from './crdt/__fixtures__/agentSocket' +import { createAgentEventSource } from './services/agent/agentEventSource' +import { resolveAgentIdentity } from './services/agent/agentIdentity' +import type { AgentIdentityState } from './services/agent/agentIdentity' import { attachDocOpMinter } from './crdt/docOpMinter' +import { attachRestoreOpMinter } from './crdt/restoreOpMinter' import type { DocOpMinter, DocOpMinterDeps } from './crdt/docOpMinter' const docOpMinterDeps = vi.hoisted(() => ({ current: null as DocOpMinterDeps | null })) vi.mock(import('./crdt/docOpMinter'), { spy: true }) +vi.mock(import('./crdt/restoreOpMinter'), { spy: true }) // The mock replaces the real `attachDocOpMinter` body entirely. It only // captures `deps` for assertions below — it must NOT reproduce any of that @@ -273,10 +326,21 @@ function syncFakeSelection() { } beforeEach(() => { - api.socket = fromPartial({ readyState: 1, send: socketSend }) - vi.mocked(api.fetchApi).mockImplementation((route, options) => - fetch(route.startsWith('/api') ? route : `/api${route}`, options) - ) + agentSocket = createFakeAgentSocket() + agentSocket.send.mockImplementation((frame) => { + socketSend(frame) + return true + }) + vi.mocked(createAgentEventSource).mockReturnValue(agentSocket.socket) + vi.mocked(resolveAgentIdentity).mockReturnValue({ + state: ref({ status: 'resolved', userId: 'account-a' }), + reset: vi.fn(), + stop: () => {} + }) + useAuthStore().currentUser = fromPartial({ uid: 'account-a' }) + vi.spyOn(useAuthStore(), 'getUserAuthHeader').mockResolvedValue({ + Authorization: 'Bearer id-token' + }) let clientMessageIds = 0 nextClientMessageId.mockImplementation( () => `client-message-${++clientMessageIds}` @@ -392,6 +456,11 @@ beforeEach(() => { URL.revokeObjectURL = vi.fn() localStorage.clear() localStorage.setItem('Comfy.AgentPanel.onboarded', 'true') + ws.clear() + getServerFeature.mockReset() + getServerFeature.mockImplementation( + (_name: string, defaultValue?: unknown) => defaultValue + ) workflowStore.activeWorkflow = null setCanvasSelection([]) @@ -5993,6 +6062,52 @@ describe('AgentPanelRoot a11y id guard', () => { }) }) +function mockMessagesEndpoint( + ackWorkflowId: string | (() => string), + cloudWorkflows: + | { id: string; name: string }[] + | (() => { id: string; name: string }[]) = [], + threads: AgentThreadSummary[] = [] +): unknown[] { + const bodies: unknown[] = [] + const resolveAckWorkflowId = + typeof ackWorkflowId === 'function' ? ackWorkflowId : () => ackWorkflowId + vi.mocked(fetch).mockImplementation( + async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input) + + if (url.includes('/messages') && init?.method === 'POST') { + bodies.push(JSON.parse(String(init.body))) + return json(202, ack(resolveAckWorkflowId(), `m-${bodies.length}`)) + } + if (url.includes('/messages')) return json(200, []) + if (url.includes('/agent/threads')) { + return json(200, agentThreadList(threads)) + } + if (url.includes('/assets')) { + return json(200, { assets: [], total: 0, has_more: false }) + } + if (url.includes('/workflows')) { + const workflows = + typeof cloudWorkflows === 'function' + ? cloudWorkflows() + : cloudWorkflows + return json(200, { + data: workflows, + pagination: { + offset: 0, + limit: 100, + total: workflows.length, + has_more: false + } + }) + } + return new Response('{}', { status: 200 }) + } + ) + return bodies +} + describe('AgentPanelRoot workflow binding', () => { beforeEach(() => { useAgentPanelStore().enabled = true @@ -6175,7 +6290,9 @@ describe('AgentPanelRoot workflow binding', () => { // one on mount. The turn is pinned to the tab it started on, so the report // has to stay there too. let sending = false + let listings = 0 const bodies = mockMessagesEndpoint('wf-42', () => { + listings++ if (sending) useAgentPanelStore().setWorkflowTarget( fromPartial(other) @@ -6188,6 +6305,7 @@ describe('AgentPanelRoot workflow binding', () => { vi.mocked(useTelemetry())!.trackAgentMessageSent.mockClear() renderWithSelectedTarget() await screen.findByRole('textbox') + await vi.waitFor(() => expect(listings).toBeGreaterThan(0)) sending = true await sendFromComposer('build me a workflow') @@ -6263,52 +6381,6 @@ describe('AgentPanelRoot workflow binding', () => { return { target, references: referenceTabs } } - function mockMessagesEndpoint( - ackWorkflowId: string | (() => string), - cloudWorkflows: - | { id: string; name: string }[] - | (() => { id: string; name: string }[]) = [], - threads: AgentThreadSummary[] = [] - ): unknown[] { - const bodies: unknown[] = [] - const resolveAckWorkflowId = - typeof ackWorkflowId === 'function' ? ackWorkflowId : () => ackWorkflowId - vi.mocked(fetch).mockImplementation( - async (input: RequestInfo | URL, init?: RequestInit) => { - const url = String(input) - - if (url.includes('/messages') && init?.method === 'POST') { - bodies.push(JSON.parse(String(init.body))) - return json(202, ack(resolveAckWorkflowId(), `m-${bodies.length}`)) - } - if (url.includes('/messages')) return json(200, []) - if (url.includes('/agent/threads')) { - return json(200, agentThreadList(threads)) - } - if (url.includes('/assets')) { - return json(200, { assets: [], total: 0, has_more: false }) - } - if (url.includes('/workflows')) { - const workflows = - typeof cloudWorkflows === 'function' - ? cloudWorkflows() - : cloudWorkflows - return json(200, { - data: workflows, - pagination: { - offset: 0, - limit: 100, - total: workflows.length, - has_more: false - } - }) - } - return new Response('{}', { status: 200 }) - } - ) - return bodies - } - it('preserves active-turn graph activity across remount and delayed hydration', async () => { makeTab('wf-42') let resolveHistory!: (response: Response) => void @@ -8588,6 +8660,22 @@ describe('AgentPanelRoot workflow binding', () => { ).not.toBeNull() }) + it('does not refresh the agent credential while a cloud turn runs', async () => { + makeTab('wf-42') + mockMessagesEndpoint('wf-42') + vi.useFakeTimers({ shouldAdvanceTime: true }) + await renderAndSend('keep going') + vi.mocked(fetch).mockClear() + + await vi.advanceTimersByTimeAsync(4 * 60 * 1000 + 1) + + expect(screen.getByRole('button', { name: 'Stop' })).toBeInTheDocument() + const refreshes = vi + .mocked(fetch) + .mock.calls.filter(([input]) => String(input).includes('limit=1')) + expect(refreshes).toEqual([]) + }) + it('lowers the creating flag when a newer focus event supersedes the fetch', async () => { const bound = makeTab('wf-42') let resolveLookup: ((response: Response) => void) | undefined @@ -8786,11 +8874,8 @@ describe('AgentPanelRoot workflow binding', () => { workflowStore.activeWorkflow = scratch await sendFromComposer('second message') - expect(bodies[1]).toMatchObject({ - workflow_id: 'wf-42', - - draft: { content: { id: 'wf-42' } } - }) + expect(bodies[1]).toMatchObject({ workflow_id: 'wf-42' }) + expect(bodies[1]).not.toHaveProperty('draft') }) it('keeps an explicitly detached workflow after a remount', async () => { @@ -10688,6 +10773,117 @@ describe('AgentPanelRoot workflow binding', () => { expect(bodies[0]).not.toHaveProperty('current_tab') }) + describe('draft seed against the follower', () => { + async function bindAndSettle(): Promise { + makeTab('wf-42') + const bodies = mockMessagesEndpoint('wf-42') + await renderAndSend('first message') + ws.emit('agent_message_done', { message_id: 'm-1', thread_id: 'th-1' }) + await screen.findByRole('button', { name: 'Send' }) + return bodies + } + + function subscribedWorkflowIds(): unknown[] { + return socketSend.mock.calls + .map( + ([frame]) => + JSON.parse(String(frame)) as { + type: string + data: { workflow_id?: unknown } + } + ) + .filter(({ type }) => type === 'doc_subscribe') + .map(({ data }) => data.workflow_id) + } + + it('seeds the draft again once the follower has given up on a refused document', async () => { + const bodies = await bindAndSettle() + ws.emit('doc_subscribed', { + v: 1, + workflow_id: 'wf-42', + ok: false, + code: 'schema_version_mismatch' + }) + + await sendFromComposer('second message') + + expect(bodies[1]).toMatchObject({ + workflow_id: 'wf-42', + draft: { content: { id: 'wf-42' } } + }) + }) + + it('keeps withholding the draft across an ordinary reconnect', async () => { + const bodies = await bindAndSettle() + ws.emit('doc_subscribed', { + v: 1, + workflow_id: 'wf-42', + ok: true, + seq: 1 + }) + + agentSocket.open() + + await sendFromComposer('second message') + + expect(bodies[1]).toHaveProperty('workflow_id', 'wf-42') + expect(bodies[1]).not.toHaveProperty('draft') + }) + + it('withholds the draft for a bound document while the agent identity is still pending', async () => { + vi.mocked(resolveAgentIdentity).mockReturnValue({ + state: ref({ status: 'pending' }), + reset: vi.fn(), + stop: () => {} + }) + const bodies = await bindAndSettle() + + await sendFromComposer('second message') + + expect(bodies[1]).toHaveProperty('workflow_id', 'wf-42') + expect(bodies[1]).not.toHaveProperty('draft') + }) + + it('starts the follower without seeding a draft once a retried identity lookup resolves', async () => { + const state = ref({ status: 'pending' }) + vi.mocked(resolveAgentIdentity).mockReturnValue({ + state, + reset: vi.fn(), + stop: () => {} + }) + const bodies = await bindAndSettle() + await sendFromComposer('second message') + ws.emit('agent_message_done', { message_id: 'm-2', thread_id: 'th-1' }) + await screen.findByRole('button', { name: 'Send' }) + expect(subscribedWorkflowIds()).toEqual([]) + + state.value = { status: 'resolved', userId: 'account-a' } + + await vi.waitFor(() => expect(subscribedWorkflowIds()).toEqual(['wf-42'])) + await sendFromComposer('third message') + expect(bodies.slice(1)).toEqual([ + expect.not.objectContaining({ draft: expect.anything() }), + expect.not.objectContaining({ draft: expect.anything() }) + ]) + }) + + it('seeds the draft again once the agent identity lookup is rejected', async () => { + vi.mocked(resolveAgentIdentity).mockReturnValue({ + state: ref({ status: 'rejected' }), + reset: vi.fn(), + stop: () => {} + }) + const bodies = await bindAndSettle() + + await sendFromComposer('second message') + + expect(bodies[1]).toMatchObject({ + workflow_id: 'wf-42', + draft: { content: { id: 'wf-42' } } + }) + }) + }) + it('keeps the editable target when viewing an unbound tab without current_tab fallback', async () => { makeTab('wf-42') const bodies = mockMessagesEndpoint('wf-42') @@ -10729,11 +10925,8 @@ describe('AgentPanelRoot workflow binding', () => { await sendFromComposer('second message') - expect(bodies[1]).toMatchObject({ - workflow_id: 'wf-42', - - draft: { content: { id: 'wf-42' } } - }) + expect(bodies[1]).toMatchObject({ workflow_id: 'wf-42' }) + expect(bodies[1]).not.toHaveProperty('draft') expect(useAgentWorkflowTabBindingStore().tabPathFor('wf-42')).toBe( origin.path ) @@ -11469,7 +11662,9 @@ describe('AgentPanelRoot workflow binding', () => { it('sends only the remaining chip after one is dismissed', async () => { makeTab() - const bodies = mockMessagesEndpoint('wf-42') + const bodies = mockMessagesEndpoint('wf-42', [ + { id: 'wf-42', name: 'current' } + ]) appMock.graph.nodes = [ { id: 5, title: 'KSampler' }, { id: 7, title: 'VAEDecode' } @@ -11497,7 +11692,7 @@ describe('AgentPanelRoot workflow binding', () => { attachment_count: 0, node_tag_count: 1, thread_id: null, - workflow_id: null, + workflow_id: 'wf-42', client_message_id: 'client-message-1', input_method: 'typed', starter_prompt_id: null, @@ -12009,7 +12204,9 @@ describe('AgentPanelRoot workflow binding', () => { it('does not resend a canvas selection after its chip was consumed', async () => { makeTab() - const bodies = mockMessagesEndpoint('wf-42') + const bodies = mockMessagesEndpoint('wf-42', [ + { id: 'wf-42', name: 'current' } + ]) const state = setupNodeSelectionCanvas() renderWithSelectedTarget() @@ -12035,7 +12232,9 @@ describe('AgentPanelRoot workflow binding', () => { it('keeps normal graph selections out of the composer across a panel remount', async () => { makeTab() - const bodies = mockMessagesEndpoint('wf-42') + const bodies = mockMessagesEndpoint('wf-42', [ + { id: 'wf-42', name: 'current' } + ]) appMock.graph.nodes = [{ id: 7, title: 'KSampler' }] const panelStore = useAgentPanelStore() @@ -12253,7 +12452,9 @@ describe('AgentPanelRoot workflow binding', () => { it('X-03 / PM-680 / FE-1311 keeps displayed node chips identical to every sent node id', async () => { makeTab() - const bodies = mockMessagesEndpoint('wf-42') + const bodies = mockMessagesEndpoint('wf-42', [ + { id: 'wf-42', name: 'current' } + ]) const selection = await startVueNodeSelection() expect(canvasStore.isPickingNodes).toBe(true) @@ -12513,6 +12714,264 @@ describe('AgentPanelRoot workflow binding', () => { }) }) +describe('AgentPanelRoot sign-in and saved workflows', () => { + beforeEach(() => { + useAgentPanelStore().enabled = true + }) + + it('asks a signed-out user to sign in instead of sending, keeping the draft', async () => { + useAuthStore().currentUser = null + vi.mocked(useAuthStore().getUserAuthHeader).mockResolvedValue(null) + vi.mocked(useAuthDialogs().showSignInDialog).mockResolvedValue(false) + const tab = addTab('workflows/current.json', { isTemporary: true }) + workflowStore.activeWorkflow = tab + const bodies = mockMessagesEndpoint('wf-minted') + renderWithSelectedTarget() + + await userEvent.click(screen.getByRole('textbox')) + await userEvent.paste('build here') + await userEvent.click(screen.getByRole('button', { name: 'Send' })) + + await vi.waitFor(() => + expect(useAuthDialogs().showSignInDialog).toHaveBeenCalledOnce() + ) + await vi.waitFor(() => + expect(useAgentComposerStore().draft).toBe('build here') + ) + expect(bodies).toHaveLength(0) + }) + + it('sends a saved file under the id the workflow index gives it', async () => { + const activeState = fromPartial({ + nodes: [{ id: 1, type: 'LoadImage' }], + links: [] + }) + const tab = addTab('workflows/Portrait.json', { + isTemporary: false, + activeState + }) + workflowStore.activeWorkflow = tab + const bodies = mockMessagesEndpoint('wf-portrait', [ + { id: 'wf-portrait', name: 'Portrait' } + ]) + renderWithSelectedTarget() + + await userEvent.click(screen.getByRole('textbox')) + await userEvent.paste('build here') + await userEvent.click(screen.getByRole('button', { name: 'Send' })) + + await vi.waitFor(() => expect(bodies).toHaveLength(1)) + expect(bodies[0]).toMatchObject({ + content: 'build here', + workflow_id: 'wf-portrait', + draft: { content: activeState } + }) + expect(useWorkflowService().saveWorkflowAs).not.toHaveBeenCalled() + }) +}) + +interface AgentAccount { + firebase: string | null + apiKeyUser: string | null +} + +const zSentFrame = z.object({ + type: z.string(), + data: z.object({ workflow_id: z.string().optional() }).optional() +}) + +function signInAs({ firebase, apiKeyUser }: AgentAccount): void { + useAuthStore().currentUser = + firebase === null ? null : fromPartial({ uid: firebase }) + useApiKeyAuthStore().currentUser = + apiKeyUser === null ? null : { id: apiKeyUser } +} + +describe('AgentPanelRoot agent socket', () => { + it.for([ + { + change: 'a Firebase account switch', + before: { firebase: 'account-a', apiKeyUser: null }, + after: { firebase: 'account-b', apiKeyUser: null } + }, + { + change: 'a Firebase sign-out', + before: { firebase: 'account-a', apiKeyUser: null }, + after: { firebase: null, apiKeyUser: null } + }, + { + change: "an API key replaced by another account's", + before: { firebase: null, apiKeyUser: 'key-user-a' }, + after: { firebase: null, apiKeyUser: 'key-user-b' } + }, + { + change: 'an API key cleared', + before: { firebase: null, apiKeyUser: 'key-user-a' }, + after: { firebase: null, apiKeyUser: null } + } + ])( + 'reconnects the socket and looks the identity up again on $change', + async ({ before, after }) => { + const reset = vi.fn() + vi.mocked(resolveAgentIdentity).mockReturnValue({ + state: ref({ + status: 'resolved', + userId: 'account-a' + }), + reset, + stop: () => {} + }) + signInAs(before) + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + agentSocket.reconnect.mockClear() + + signInAs(after) + await nextTick() + + expect(agentSocket.reconnect).toHaveBeenCalledOnce() + expect(reset).toHaveBeenCalledOnce() + } + ) + + it('opens the events socket under the api base path', async () => { + vi.mocked(api.apiURL).mockImplementation((route) => `/comfy/api${route}`) + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + expect(createAgentEventSource).toHaveBeenCalledWith( + expect.objectContaining({ endpoint: '/comfy/api/agent/events' }) + ) + }) +}) + +describe('AgentPanelRoot agent socket identity', () => { + beforeEach(() => { + vi.mocked(resolveAgentIdentity).mockReset() + vi.mocked(reportError).mockImplementation(() => {}) + }) + + afterEach(() => { + Object.assign(appMock, { isGraphReady: undefined }) + }) + + function bindActiveTab(id: string): LoadedComfyWorkflow { + const tab = addTab('workflows/current.json', { + activeState: fromPartial({ id }) + }) + workflowStore.activeWorkflow = tab + useAgentWorkflowTabBindingStore().bind(id, tab.path) + return tab + } + + function identityResponse(): Response { + return json(200, { workspace_id: 'ws-local', user_id: 'local-user' }) + } + + function subscribeAttempts(): string[] { + return agentSocket.send.mock.calls + .map(([frame]) => zSentFrame.parse(JSON.parse(frame))) + .filter((frame) => frame.type === 'doc_subscribe') + .map((frame) => frame.data?.workflow_id ?? '') + } + + it('retries a failed identity lookup and activates the follower once it resolves', async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }) + const identity = vi + .fn<() => Response>() + .mockImplementationOnce(() => { + throw new Error('identity route down') + }) + .mockImplementation(identityResponse) + mockMessagesEndpoint('wf-42') + respondToFetch(/\/agent\/identity$/, async () => identity()) + bindActiveTab('wf-42') + useAgentPanelStore().enabled = true + + await renderAndSend('first message') + await vi.advanceTimersByTimeAsync(0) + + expect(identity).toHaveBeenCalledTimes(1) + expect(reportError).toHaveBeenCalledWith( + expect.any(Error), + expect.objectContaining({ + errorType: 'failure_resolving_agent_identity' + }) + ) + expect(subscribeAttempts()).toEqual([]) + + await vi.advanceTimersByTimeAsync(1_000) + + expect(identity).toHaveBeenCalledTimes(2) + expect(subscribeAttempts()).toEqual(['wf-42']) + }) + + it.for([ + { reason: 'the backend rejects the credential', status: 401 }, + { reason: 'the backend has no identity route', status: 404 } + ])( + 'looks the identity up once and seeds the draft when $reason', + async ({ status }) => { + vi.useFakeTimers({ shouldAdvanceTime: true }) + const identity = vi.fn(async () => json(status, { error: 'refused' })) + const bodies = mockMessagesEndpoint('wf-42') + respondToFetch(/\/agent\/identity$/, identity) + bindActiveTab('wf-42') + useAgentPanelStore().enabled = true + + await renderAndSend('first message') + await vi.advanceTimersByTimeAsync(60_000) + ws.emit('agent_message_done', { message_id: 'm-1', thread_id: 'th-1' }) + await screen.findByRole('button', { name: 'Send' }) + await sendFromComposer('second message') + + expect(identity).toHaveBeenCalledOnce() + expect(bodies[1]).toMatchObject({ + workflow_id: 'wf-42', + draft: { content: expect.anything() } + }) + } + ) + + it.for([ + { + minter: 'canvas edit', + isDocBound: () => docOpMinterDeps.current?.isDocBound() + }, + { + minter: 'restored node', + isDocBound: () => + vi.mocked(attachRestoreOpMinter).mock.lastCall?.[0].isDocBound() + } + ])( + 'does not mint a $minter until the identity resolves and the subscribe has gone out', + async ({ isDocBound }) => { + let resolveIdentity!: (response: Response) => void + mockMessagesEndpoint('wf-42') + respondToFetch( + /\/agent\/identity$/, + () => + new Promise((resolve) => { + resolveIdentity = resolve + }) + ) + agentSocket.send.mockReturnValue(false) + bindActiveTab('wf-42') + useAgentPanelStore().enabled = true + + await renderAndSend('first message') + expect(isDocBound()).toBe(false) + + resolveIdentity(identityResponse()) + await vi.waitFor(() => expect(subscribeAttempts()).toEqual(['wf-42'])) + expect(isDocBound()).toBe(false) + + agentSocket.send.mockReturnValue(true) + agentSocket.open() + + await vi.waitFor(() => expect(isDocBound()).toBe(true)) + } + ) +}) + function dispatchApiEvent(type: string, detail: unknown): void { - apiEvents.dispatchEvent(new CustomEvent(type, { detail })) + ws.emit(type, detail) } diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 6751a983a2e..bd1fd5a1f31 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -38,6 +38,8 @@ import { MIME_ASSET_INFO } from '@/platform/assets/schemas/mediaAssetSchema' import { fetchDroppedAsset, getDroppedAsset } from '@/utils/eventUtils' import type { DroppedAsset } from '@/utils/eventUtils' import { useAssetsStore } from '@/stores/assetsStore' +import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore' +import { useAuthStore } from '@/stores/authStore' import { AGENT_ATTACH_ACCEPT, agentAttachRefVerdict, @@ -125,6 +127,11 @@ import { import { useAgentDraftSubmission } from './composables/agent/useAgentDraftSubmission' import { useAgentWorkflowTabBindingStore } from './stores/agent/agentWorkflowTabBindingStore' import { createAgentRestClient } from './services/agent/agentRestClient' +import { + agentSocketAuthParams, + ensureSignedIn, + hasAgentCredential +} from './services/agent/agentAuth' import type { DraftSnapshot } from './services/agent/agentRestClient' import type { AgentPaywallAction } from './services/agent/agentPaywallPresentation' import { @@ -134,7 +141,7 @@ import { toAgentPaywallReason } from './services/agent/agentPaywallPresentation' import { createAgentEventSource } from './services/agent/agentEventSource' -import { createStandaloneAgentEventSource } from './services/agent/standaloneAgentEventSource' +import { resolveAgentIdentity } from './services/agent/agentIdentity' import { useAgentChatHistoryStore } from './stores/agent/agentChatHistoryStore' import { agentMessageText } from './utils/agentMessageText' import { @@ -153,6 +160,7 @@ import { import { attachDocOpMinter } from './crdt/docOpMinter' import { attachRestoreOpMinter } from './crdt/restoreOpMinter' import { useAgentCrdtFollower } from './crdt/useAgentCrdtFollower' +import { createAgentDocFrameTransport } from './crdt/agentDocFrameTransport' const CrdtDevPanel = defineAsyncComponent( () => import('./crdt/CrdtDevPanel.vue') @@ -209,10 +217,36 @@ const userName = computed( const rest = createAgentRestClient() -const events = - import.meta.env.VITE_AGENT_STANDALONE === 'true' - ? createStandaloneAgentEventSource() - : createAgentEventSource(api) +const events = createAgentEventSource({ + endpoint: api.apiURL('/agent/events'), + getAuthParams: agentSocketAuthParams +}) + +const docTransport = createAgentDocFrameTransport(events) +onBeforeUnmount(() => docTransport.destroy()) + +const agentIdentity = resolveAgentIdentity({ + getIdentity: () => rest.getIdentity(), + onSocketOpen: (listener) => docTransport.onSocketOpen(listener), + isRetryable: (error) => isRetryableRequestFailure(error, false), + onFailure: (error) => + reportError(error, { + surface: 'agent', + errorType: 'failure_resolving_agent_identity', + level: 'warning' + }) +}) +onBeforeUnmount(() => agentIdentity.stop()) + +const authStore = useAuthStore() +const apiKeyAuthStore = useApiKeyAuthStore() +const agentAccountId = computed( + () => authStore.userId ?? apiKeyAuthStore.currentUser?.id ?? null +) +watch(agentAccountId, () => { + events.reconnect() + agentIdentity.reset() +}) function onPaywallAction( action: AgentPaywallAction, @@ -705,11 +739,24 @@ function targetWorkflowDraft(origin?: TurnOrigin): DraftSnapshot | undefined { if (workflowDetached.value) return undefined const target = originWorkflow(origin) if (!target) return undefined + if (followerOwnsDocument(cloudIdFor(target))) return undefined const content = serializedCanvas(target) if (!content) return undefined return { content } } +// A pending identity still withholds the draft: the seed replaces the server +// document, and a canvas the follower has not caught up would undo agent edits. +// Pending is bounded; the lookup settles as `rejected` after its retry budget. +function followerOwnsDocument(documentId: string | undefined): boolean { + return ( + documentId !== undefined && + boundWorkflowId.value === documentId && + crdtStatus.value.terminal === null && + agentIdentity.state.value.status !== 'rejected' + ) +} + function canvasForWorkflow(workflowId: string): Record | null { const target = boundOrOpenWorkflowFor(workflowId) return target ? serializedCanvas(target) : null @@ -843,6 +890,7 @@ const { useTelemetry()?.trackAgentThreadStarted({ source }), onThreadActivated: (id) => history.setActive(id), onAskResolved: forgetApproval, + refreshCredentialDuringTurns: !isCloud, workflow: { initialize: agentPanelStore.initializeTargetTracking, current: targetWorkflowTurnContext, @@ -887,6 +935,15 @@ const isBoundWorkflowActive = computed(() => { // session's bound workflow while its tab is active. Suspending the background // subscription makes reopening pull state-vector catch-up only after the // workflow's serialized activeState has hydrated the transient stores. +const agentUserId = computed(() => { + const identity = agentIdentity.state.value + return identity.status === 'resolved' ? identity.userId : null +}) + +const isFollowerActive = computed( + () => isBoundWorkflowActive.value && agentUserId.value !== null +) + const { status: crdtStatus, debugSnapshot: crdtDebugSnapshot, @@ -894,16 +951,18 @@ const { docInputNames, docPromotedWidgets, mayWritePromotedHost, - notifyWidgetWriteRefused -} = useAgentCrdtFollower( - boundWorkflowId, - () => resolvedUserInfo.value?.id ?? null, - isBoundWorkflowActive, + notifyWidgetWriteRefused, + subscriptionSent +} = useAgentCrdtFollower(boundWorkflowId, { + transport: docTransport, + userId: () => agentUserId.value, + isTargetActive: isFollowerActive, // `app.isGraphReady` is a plain getter; reading `canvasStore.canvas` (set // right after `app.setup()`) makes the follower's graph watch fire once the // root graph exists. - () => (canvasStore.canvas && app.isGraphReady ? app.rootGraph : null), - { + getGraph: () => + canvasStore.canvas && app.isGraphReady ? app.rootGraph : null, + events: { onMaterialized({ workflowId, nodeIds }) { if (app.isGraphReady) { graphActivity.recordMaterialized( @@ -920,7 +979,7 @@ const { description: formatWorkflowSyncErrorDetail(t, message, code) }) }, - { + applierDeps: { getCanvas: () => app.canvas, withRemoteActor: (actor, fn) => layoutStore.withActor(actor, fn), viewportBounds() { @@ -930,8 +989,8 @@ const { return { x, y, width, height } } }, - canvasForWorkflow -) + canvasFor: canvasForWorkflow +}) // The bound document's serialized root graph id, independent of what is // currently on the canvas: `beforeLoadNewGraph` persists the outgoing // workflow's `activeState` before the shared renderer graph is rewritten, so @@ -943,9 +1002,12 @@ function boundRootGraphId(): RootGraphId | null { const id = boundOrOpenWorkflowFor(bound)?.activeState?.id return id === undefined ? null : toRootGraphId(id) } +function followerCanDeliver(): boolean { + return isFollowerActive.value && subscriptionSent() +} const docOpMinter = attachDocOpMinter({ isEnabled: () => agentPanelStore.enabled, - isDocBound: () => isBoundWorkflowActive.value, + isDocBound: followerCanDeliver, enqueue: enqueueHumanOperations, getGraph: () => (app.isGraphReady ? app.rootGraph : null), boundRootGraphId, @@ -956,7 +1018,7 @@ const docOpMinter = attachDocOpMinter({ }) const restoreOpMinter = attachRestoreOpMinter({ isEnabled: () => agentPanelStore.enabled, - isDocBound: () => isBoundWorkflowActive.value, + isDocBound: followerCanDeliver, enqueue: enqueueHumanOperations, getGraph: () => (app.isGraphReady ? app.rootGraph : null), docInputNames: (nodeId) => { @@ -1576,6 +1638,7 @@ const { submit: onSend } = useAgentDraftSubmission({ }, // fallow-ignore-next-line complexity -- Existing PR logic; this lane changes only the composing panel test. send: async (text, attachments, nodes, references, meta) => { + if (!hasAgentCredential() && !(await ensureSignedIn())) return false const submissionId = composerStore.submission?.id if ( !consentAccepted.value && diff --git a/src/workbench/extensions/agent/README.md b/src/workbench/extensions/agent/README.md index 457350d83f9..c114dba4429 100644 --- a/src/workbench/extensions/agent/README.md +++ b/src/workbench/extensions/agent/README.md @@ -3,8 +3,35 @@ The In-App Agent panel is a manager-pattern workbench extension. The panel lives entirely in this subtree and renders in a right dock registered by `src/extensions/core/agentPanel.ts`, so it shares the host pinia and vue-i18n -instances and wires every host dependency itself (REST client, `/ws` event source, -draft-to-canvas seam). +instances and wires every host dependency itself (REST client, agent events +socket, draft-to-canvas seam). + +## Backend contract + +The panel speaks one contract on the cloud and on the local agent: + +- **Events socket, `/api/agent/events`** (`services/agent/agentEventSource.ts`). + Agent events and CRDT doc frames share it in both directions; ComfyUI's + `/ws` carries neither. Its URL comes from `api.apiURL`. In the cloud it + takes the auth params `/ws` uses, and does not open when no one is signed + in; elsewhere the signed-in user's credential rides as `?token=`. Both are + read again on every connect. Reconnects back off with jitter; the backoff + resets once a socket delivers a frame or stays open for the maximum delay. +- **Identity, `GET /api/agent/identity`** (`services/agent/agentIdentity.ts`). + Canvas ops carry `human::` with the id the backend reports, + so the follower stays inactive until it answers. A failed lookup retries + with backoff, capped at 30 seconds and never given up, and at once when the + socket connects again. A rejected credential is not retried until the + account changes. +- **Saved workflows, `GET /api/workflows`**. Saved tabs resolve through the + saved-workflow index on every backend. +- **Credential** (`services/agent/agentAuth.ts`). In the cloud, agent requests + authenticate through `api.fetchApi` like every other request. Elsewhere they + carry the signed-in user's auth header, or the stored API key when no one is + signed in, and a running turn re-sends it. A signed-out send opens the sign-in + dialog, unless the dev proxy injects `DEV_AGENT_COMFY_TOKEN`. Signing out or + switching account (or API key) reconnects the socket and looks the identity + up again. ## Activation and consent @@ -20,8 +47,9 @@ Agent opens after the save succeeds. Skip, Escape and outside clicks dismiss without saving; failed loads or saves keep the panel closed and allow retry. Switching user or workspace invalidates the cached acceptance and loads the new scope. Desktop/Local sign-in continuation authenticates before saving; this -branch currently mounts the Agent extension only in Cloud builds (FE-1931 owns -the remaining distribution entry points). +branch mounts the Agent extension in Cloud builds and in builds with +`VITE_AGENT_STANDALONE=true` (the local agent harness, in any distribution); +FE-1931 owns the remaining Local and Desktop product entry points. ## First-use tour @@ -46,7 +74,9 @@ the Agent tour appear. The doc-host follower has no gate of its own: it mounts with the agent panel, so it runs only once the panel's activation and consent requirements above are satisfied. -The follower uses the existing same-origin `/ws` connection. To run against a +The follower's doc frames ride the events socket +(`crdt/agentDocFrameTransport.ts`), and every open of that socket resubscribes. +The dev server proxies `/api/agent/events` as a WebSocket. To run against a cloud ephemeral environment: ```bash @@ -55,9 +85,16 @@ DEV_SERVER_COMFYUI_URL=https:/// pnpm dev Incoming `doc_update` frames are decoded and applied incrementally with `Y.applyUpdate`; the follower never requests or fans out a full document for -each update. Human `doc_ops` transmission is implemented, but converting local -canvas commands to the shared semantic op vocabulary remains intentionally -unwired until the `@comfyorg/comfy-multi-player` applier is available. +each update. Human edits are minted into the shared op vocabulary and sent as +`doc_ops` frames on the same socket (`crdt/docOpMinter.ts`). + +While the follower holds the bound document, a turn sends no draft: the +document is the source of truth and an unversioned draft would overwrite it. +Once the follower gives up on that document (refused for good, every subscribe +unanswered, or unreadable), or the identity lookup is rejected, turns seed the +draft again. An identity lookup that is still retrying keeps withholding the +draft. A reconnect retries the document and withholds it until that attempt +settles. ## Ask cards diff --git a/src/workbench/extensions/agent/components/agent/Composer.test.ts b/src/workbench/extensions/agent/components/agent/Composer.test.ts index 66d30cd2605..24313db961a 100644 --- a/src/workbench/extensions/agent/components/agent/Composer.test.ts +++ b/src/workbench/extensions/agent/components/agent/Composer.test.ts @@ -33,6 +33,7 @@ setupInlinePromptEditorDom() vi.mock(import('@/composables/auth/useCurrentUser')) vi.mock(import('@/scripts/api')) vi.mock(import('@/platform/telemetry')) +vi.mock(import('../../services/agent/agentAuth')) const fetchApi = vi.mocked(api.fetchApi) const telemetryProvider = useTelemetry() assert.exists(telemetryProvider) diff --git a/src/workbench/extensions/agent/components/agent/DockedAgentPanel.test.ts b/src/workbench/extensions/agent/components/agent/DockedAgentPanel.test.ts index 7c0b607262f..e12b48ab54b 100644 --- a/src/workbench/extensions/agent/components/agent/DockedAgentPanel.test.ts +++ b/src/workbench/extensions/agent/components/agent/DockedAgentPanel.test.ts @@ -20,6 +20,7 @@ import DockedAgentPanel from './DockedAgentPanel.vue' vi.mock(import('@/platform/telemetry')) vi.mock(import('@/composables/billing/useBillingContext')) const billingContext = useBillingContext() +vi.mock(import('../../services/agent/agentAuth')) vi.mock(import('@/platform/telemetry/reportError')) const fetchApi = vi.hoisted(() => diff --git a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts index 84e228784a7..1111addefd1 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts @@ -130,8 +130,7 @@ describe('useAgentConsent', () => { Object.assign(useTeamWorkspaceStore(), { activeWorkspaceId: 'workspace-a' }) Object.assign(useTeamWorkspaceStore(), { workspaceTransitionGeneration: 0 }) vi.mocked(useTeamWorkspaceStore().initialize).mockResolvedValue(undefined) - vi.mocked(useAuthStore().getWorkspaceAuthHeader).mockReset() - vi.mocked(useAuthStore().getWorkspaceAuthHeader).mockResolvedValue({ + vi.mocked(useAuthStore().getUserAuthHeader).mockResolvedValue({ Authorization: 'Bearer account-a-token' }) vi.mocked(api.fetchApi).mockReset() @@ -627,7 +626,7 @@ describe('useAgentConsent', () => { const onOpen = vi.fn() const request = useAgentConsent().withConsent('button_click', onOpen) const dialog = await waitForConsentDialog() - vi.mocked(useAuthStore().getWorkspaceAuthHeader).mockResolvedValueOnce(null) + vi.mocked(useAuthStore().getUserAuthHeader).mockResolvedValueOnce(null) ;(dialog.contentProps.onAccept as () => void)() await vi.waitFor(() => diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 3dbb79d106e..45ec613e138 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -6,7 +6,7 @@ import type { } from '@comfyorg/ingest-types' import { zAgentPostMessageRequest } from '@comfyorg/ingest-types/zod' import { assert, beforeEach, describe, expect, it, vi } from 'vitest' -import { nextTick } from 'vue' +import { effectScope, nextTick } from 'vue' import enMessages from '@/locales/en/main.json' with { type: 'json' } import { useTelemetry } from '@/platform/telemetry' @@ -54,7 +54,9 @@ import type { AgentEventSource, TurnOrigin } from './useAgentSession' import { useAgentSession } from './useAgentSession' vi.mock(import('@/platform/telemetry/reportError')) -vi.mock(import('@/platform/distribution/types'), () => ({ isCloud: true })) +vi.mock(import('../../services/agent/agentAuth')) +const distribution = vi.hoisted(() => ({ isCloud: true })) +vi.mock(import('@/platform/distribution/types'), () => distribution) vi.mock(import('@/platform/telemetry')) const telemetryProvider = useTelemetry() assert.exists(telemetryProvider) @@ -69,6 +71,7 @@ function fakeRest(overrides: Partial = {}): AgentRestClient { workflow_id: 'wf-1' }) ), + getIdentity: vi.fn(async () => ({ userId: 'user-test' })), getMessages: vi.fn(async (): Promise => []), listThreads: vi.fn(async (): Promise => []), getRunMode: vi.fn( @@ -104,6 +107,7 @@ function fakeRest(overrides: Partial = {}): AgentRestClient { selected: [] }) ), + refreshCredential: vi.fn(async () => {}), uploadImage: vi.fn( async (): Promise => ({ name: 'n', @@ -7316,3 +7320,99 @@ describe('app:agent_error telemetry (TEL-8)', () => { expect(localStorage.getItem('Comfy.Agent.ThreadId')).toBeNull() }) }) + +describe('useAgentSession credential refresh', () => { + const REFRESH_INTERVAL_MS = 4 * 60 * 1000 + + function startSession( + refreshCredential: AgentRestClient['refreshCredential'], + refreshCredentialDuringTurns = true + ) { + const { source, emit } = fakeEvents() + const scope = effectScope() + const session = scope.run(() => + useAgentSession({ + rest: fakeRest({ refreshCredential }), + events: source, + refreshCredentialDuringTurns + }) + ) + assert.exists(session) + session.start() + return { scope, session, emit } + } + + it('re-sends the credential every interval while a turn runs and stops when it ends', async () => { + const refreshCredential = vi.fn(async () => {}) + const { scope, session, emit } = startSession(refreshCredential) + + await vi.advanceTimersByTimeAsync(REFRESH_INTERVAL_MS) + expect(refreshCredential).not.toHaveBeenCalled() + + await session.sendMessage('build it') + await vi.advanceTimersByTimeAsync(REFRESH_INTERVAL_MS * 2) + expect(refreshCredential).toHaveBeenCalledTimes(2) + + emit(done('msg-1')) + await vi.advanceTimersByTimeAsync(REFRESH_INTERVAL_MS * 2) + expect(refreshCredential).toHaveBeenCalledTimes(2) + scope.stop() + }) + + it('keeps refreshing for a turn a thread switch moved to the background', async () => { + const refreshCredential = vi.fn(async () => {}) + const { scope, session, emit } = startSession(refreshCredential) + + await session.sendMessage('build it') + session.newChat() + expect(useAgentConversationStore().isStreaming).toBe(false) + await vi.advanceTimersByTimeAsync(REFRESH_INTERVAL_MS * 2) + expect(refreshCredential).toHaveBeenCalledTimes(2) + + emit(done('msg-1')) + await vi.advanceTimersByTimeAsync(REFRESH_INTERVAL_MS * 2) + expect(refreshCredential).toHaveBeenCalledTimes(2) + scope.stop() + }) + + it('does not refresh when the host does not ask for it, as in the cloud', async () => { + const refreshCredential = vi.fn(async () => {}) + const { scope, session } = startSession(refreshCredential, false) + + await session.sendMessage('build it') + await vi.advanceTimersByTimeAsync(REFRESH_INTERVAL_MS * 2) + + expect(refreshCredential).not.toHaveBeenCalled() + scope.stop() + }) + + it('reports a credential refresh that fails', async () => { + const failure = new Error('refresh failed') + const refreshCredential = vi.fn(async () => { + throw failure + }) + const { scope, session } = startSession(refreshCredential) + + await session.sendMessage('build it') + await vi.advanceTimersByTimeAsync(REFRESH_INTERVAL_MS) + + expect(reportError).toHaveBeenCalledWith( + failure, + expect.objectContaining({ + errorType: 'failure_refreshing_agent_credential' + }) + ) + scope.stop() + }) + + it('stops refreshing when the panel scope is disposed mid-turn', async () => { + const refreshCredential = vi.fn(async () => {}) + const { scope, session } = startSession(refreshCredential) + await session.sendMessage('build it') + + scope.stop() + await vi.advanceTimersByTimeAsync(REFRESH_INTERVAL_MS * 2) + + expect(refreshCredential).not.toHaveBeenCalled() + }) +}) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index f1fecbb5bc1..2ee4bab59a5 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -1,5 +1,6 @@ import { zAgentAskConflict } from '@comfyorg/ingest-types/zod' import type { AgentAnswerRequest } from '@comfyorg/ingest-types' +import { useIntervalFn } from '@vueuse/core' import { delay, xor } from 'es-toolkit' import { computed, ref, watch } from 'vue' import { ZodError } from 'zod' @@ -116,6 +117,12 @@ export interface AgentSessionDeps { onThreadStarted?: (source: AgentSessionThreadStartSource) => void onThreadActivated?: (threadId: string | null) => void onAskResolved?: (askId: string) => void + /** + * Re-send the user's credential on an interval while a turn runs, so a long + * local turn never outlives its token. Off in the cloud, where fetchApi + * authenticates every request. + */ + refreshCredentialDuringTurns?: boolean workflow?: { /** Resolve fresh versus restored startup before asynchronous hydration. */ initialize?(hasThread: boolean): void @@ -371,6 +378,8 @@ function consumeStopPendingAck() { return pending } +const CREDENTIAL_REFRESH_INTERVAL_MS = 4 * 60 * 1000 + function parseAdmissionError(error: unknown) { if (!(error instanceof AgentApiError)) return undefined const parsed = zAgentAdmissionError.safeParse(error.body) @@ -427,6 +436,7 @@ export function useAgentSession(deps: AgentSessionDeps) { onThreadStarted, onThreadActivated, onAskResolved, + refreshCredentialDuringTurns = false, workflow } = deps const threadStorageKey = StorageKeys.agentThread(getWorkspaceId()) @@ -538,6 +548,18 @@ export function useAgentSession(deps: AgentSessionDeps) { 'first_open' ) + useIntervalFn(() => { + if (!refreshCredentialDuringTurns) return + if (conversationStore.liveTurns().length === 0) return + rest.refreshCredential().catch((error: unknown) => + reportError(error, { + surface: 'agent', + errorType: 'failure_refreshing_agent_credential', + level: 'warning' + }) + ) + }, CREDENTIAL_REFRESH_INTERVAL_MS) + function nextLocalErrorId(): TurnId { return toTurnId(`local-error-${createUuidv4()}`) } diff --git a/src/workbench/extensions/agent/composables/agent/useAttachment.transport.test.ts b/src/workbench/extensions/agent/composables/agent/useAttachment.transport.test.ts index 6d3cc1b8eb2..a0b6bba6f83 100644 --- a/src/workbench/extensions/agent/composables/agent/useAttachment.transport.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAttachment.transport.test.ts @@ -4,6 +4,8 @@ import { createAgentRestClient } from '../../services/agent/agentRestClient' import { useAttachment } from './useAttachment' import type { ComposerAttachment } from '../../types/composerAttachment' +vi.mock(import('../../services/agent/agentAuth')) + it('keeps a 4 MiB upload alive past 60 seconds and aborts it at 94 seconds', async () => { vi.useFakeTimers() onTestFinished(() => { diff --git a/src/workbench/extensions/agent/crdt/CrdtDevPanel.clipboard.test.ts b/src/workbench/extensions/agent/crdt/CrdtDevPanel.clipboard.test.ts index 6f71f8d89f2..c79bbfac9ff 100644 --- a/src/workbench/extensions/agent/crdt/CrdtDevPanel.clipboard.test.ts +++ b/src/workbench/extensions/agent/crdt/CrdtDevPanel.clipboard.test.ts @@ -37,6 +37,7 @@ vi.mock(import('@/scripts/app'), () => ({ const status: AgentCrdtStatus = { enabled: true, connected: true, + terminal: null, workflowId: 'doc-123', updatesApplied: 1, lastFrameType: 'doc_update', diff --git a/src/workbench/extensions/agent/crdt/CrdtDevPanel.test.ts b/src/workbench/extensions/agent/crdt/CrdtDevPanel.test.ts index a6bdf16754c..760c3f9e6a8 100644 --- a/src/workbench/extensions/agent/crdt/CrdtDevPanel.test.ts +++ b/src/workbench/extensions/agent/crdt/CrdtDevPanel.test.ts @@ -26,6 +26,7 @@ import type { AgentCrdtStatus } from './agentCrdtStatus' const STATUS: AgentCrdtStatus = { enabled: true, connected: true, + terminal: null, workflowId: 'doc-1', updatesApplied: 4, lastFrameType: 'doc_update', diff --git a/src/workbench/extensions/agent/crdt/__fixtures__/agentSocket.ts b/src/workbench/extensions/agent/crdt/__fixtures__/agentSocket.ts new file mode 100644 index 00000000000..b96be2913ca --- /dev/null +++ b/src/workbench/extensions/agent/crdt/__fixtures__/agentSocket.ts @@ -0,0 +1,47 @@ +import { vi } from 'vitest' + +import type { AgentEventSocket } from '../../services/agent/agentEventSource' +import { createAgentDocFrameTransport } from '../agentDocFrameTransport' + +export function createFakeAgentSocket() { + const listeners = new Set<(raw: unknown) => void>() + const statusListeners = new Set<(live: boolean) => void>() + const send = vi.fn<(frame: string) => boolean>(() => true) + const reconnect = vi.fn() + let live = false + + function setLive(next: boolean): void { + live = next + statusListeners.forEach((listener) => listener(next)) + } + + const socket: AgentEventSocket = { + reconnect, + send: (frame) => send(frame), + subscribe(listener) { + listeners.add(listener) + return () => listeners.delete(listener) + }, + onStatus(listener) { + statusListeners.add(listener) + listener(live) + return () => statusListeners.delete(listener) + } + } + return { + socket, + send, + reconnect, + transport: createAgentDocFrameTransport(socket), + open(): void { + setLive(true) + }, + drop(): void { + setLive(false) + }, + receive(type: string, data: unknown): void { + listeners.forEach((listener) => listener({ type, data })) + }, + listenerCount: () => listeners.size + } +} diff --git a/src/workbench/extensions/agent/crdt/agentCrdtDocLifecycle.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtDocLifecycle.test.ts index bbcd64f6d56..80ea90dbc94 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtDocLifecycle.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtDocLifecycle.test.ts @@ -414,7 +414,7 @@ describe('AgentCrdtDocLifecycle refusal exhaustion', () => { vi.advanceTimersByTime(10 * SUBSCRIBE_ACK_TIMEOUT_MS) expect(resubscribe).toHaveBeenCalledTimes(6) - expect(lifecycle.shouldDeferSubscribe()).toBe(false) + expect(lifecycle.refusalIsFinal()).toBe(true) expect(onGaveUp).not.toHaveBeenCalled() expect(reportError).not.toHaveBeenCalled() expect(devEvents().map(({ kind }) => kind)).toEqual([ @@ -425,6 +425,12 @@ describe('AgentCrdtDocLifecycle refusal exhaustion', () => { 'subscribe_retry', 'subscribe_retry' ]) + + lifecycle.onSubscribeSent(WORKFLOW_ID) + vi.advanceTimersByTime(SUBSCRIBE_ACK_TIMEOUT_MS) + + expect(resubscribe).toHaveBeenCalledTimes(6) + expect(onGaveUp).toHaveBeenCalledTimes(1) }) }) @@ -442,7 +448,6 @@ describe('AgentCrdtDocLifecycle schema_version_mismatch refusal', () => { expect(resubscribe).not.toHaveBeenCalled() expect(onGaveUp).toHaveBeenCalledTimes(1) - expect(lifecycle.shouldDeferSubscribe()).toBe(true) expect(reportError).toHaveBeenCalledExactlyOnceWith( expect.any(Error), GAVE_UP_REPORT @@ -453,6 +458,11 @@ describe('AgentCrdtDocLifecycle schema_version_mismatch refusal', () => { detail: { code: 'schema_version_mismatch', terminal: true } } ]) + + lifecycle.onSubscribeSent(WORKFLOW_ID) + lifecycle.onSubscribeRefused('doc_not_found') + vi.advanceTimersByTime(10 * SUBSCRIBE_ACK_TIMEOUT_MS) + expect(resubscribe).not.toHaveBeenCalled() }) it('a different refusal code still retries with backoff', () => { @@ -522,7 +532,6 @@ describe('AgentCrdtDocLifecycle schema_version_mismatch refusal', () => { expect(resubscribe).not.toHaveBeenCalled() expect(onGaveUp).toHaveBeenCalledTimes(1) - expect(lifecycle.shouldDeferSubscribe()).toBe(true) expect(reportError).toHaveBeenCalledExactlyOnceWith( expect.any(Error), GAVE_UP_REPORT @@ -533,6 +542,11 @@ describe('AgentCrdtDocLifecycle schema_version_mismatch refusal', () => { detail: { code, terminal: true } } ]) + + lifecycle.onSubscribeSent(WORKFLOW_ID) + lifecycle.onSubscribeRefused('doc_not_found') + vi.advanceTimersByTime(10 * SUBSCRIBE_ACK_TIMEOUT_MS) + expect(resubscribe).not.toHaveBeenCalled() } ) @@ -544,10 +558,14 @@ describe('AgentCrdtDocLifecycle schema_version_mismatch refusal', () => { vi.advanceTimersByTime(10 * SUBSCRIBE_ACK_TIMEOUT_MS) expect(resubscribe).not.toHaveBeenCalled() - expect(lifecycle.shouldDeferSubscribe()).toBe(true) expect(onGaveUp).not.toHaveBeenCalled() expect(reportError).not.toHaveBeenCalled() expect(devEvents()).toEqual([]) + + lifecycle.onSubscribeSent(WORKFLOW_ID) + lifecycle.onSubscribeRefused('doc_not_found') + vi.advanceTimersByTime(10 * SUBSCRIBE_ACK_TIMEOUT_MS) + expect(resubscribe).not.toHaveBeenCalled() }) it('a reconnect does not re-notify a permanent refusal for the same workflow', () => { @@ -605,7 +623,6 @@ describe('AgentCrdtDocLifecycle schema_version_mismatch refusal', () => { // Attempt 2 confirms first - the channel is healthy again. lifecycle.onSubscribeConfirmed() - expect(lifecycle.shouldDeferSubscribe()).toBe(false) // Attempt 1's answer finally lands, refusing permanently. It cannot be // answering attempt 2 (which already confirmed), so it must be @@ -615,7 +632,6 @@ describe('AgentCrdtDocLifecycle schema_version_mismatch refusal', () => { expect(notified).toBe(false) expect(onGaveUp).not.toHaveBeenCalled() expect(reportError).not.toHaveBeenCalled() - expect(lifecycle.shouldDeferSubscribe()).toBe(false) expect(devEvents()).toEqual([ { kind: 'subscribe_ack_timeout', @@ -647,3 +663,42 @@ describe('AgentCrdtDocLifecycle schema_version_mismatch refusal', () => { expect(reportError).toHaveBeenCalledTimes(2) }) }) + +describe('AgentCrdtDocLifecycle refusalIsFinal', () => { + it('is not final while a retry is scheduled', () => { + const { lifecycle } = wire() + lifecycle.onSubscribeSent(WORKFLOW_ID) + + lifecycle.onSubscribeRefused() + + expect(lifecycle.refusalIsFinal()).toBe(false) + }) + + it('is final after a permanent refusal', () => { + const { lifecycle } = wire() + lifecycle.onSubscribeSent(WORKFLOW_ID) + + lifecycle.onSubscribeRefused('schema_version_mismatch') + + expect(lifecycle.refusalIsFinal()).toBe(true) + }) + + it('is final when nothing is bound to retry', () => { + const { lifecycle, retarget } = wire() + retarget(null) + + lifecycle.onSubscribeRefused() + + expect(lifecycle.refusalIsFinal()).toBe(true) + }) + + it('is not final for a stale permanent refusal behind a confirm', () => { + const { lifecycle } = wire() + lifecycle.onSubscribeSent(WORKFLOW_ID) + lifecycle.onSubscribeConfirmed() + + lifecycle.onSubscribeRefused('schema_version_mismatch') + + expect(lifecycle.refusalIsFinal()).toBe(false) + }) +}) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtDocLifecycle.ts b/src/workbench/extensions/agent/crdt/agentCrdtDocLifecycle.ts index e9bcf289b37..3735657ce15 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtDocLifecycle.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtDocLifecycle.ts @@ -39,6 +39,8 @@ const DOC_ID_REFRESH_INTERVAL_MS = DOC_ID_TTL_MS / 2 const SUBSCRIBE_RETRY_BASE_MS = 500 const SUBSCRIBE_RETRY_MAX_ATTEMPTS = 6 +export type SubscribeGiveUpReason = 'refused' | 'timeout' + // PM-1604 / BE-11437: the doc-host's terminal classifications for a // document this build can never read back - unlike every other refusal // reason, no amount of backoff retry changes that outcome, so these codes @@ -217,7 +219,7 @@ export class AgentCrdtDocLifecycle { constructor( private readonly workflowId: () => string | null, private readonly resubscribe: () => void, - private readonly onGaveUp: () => void + private readonly onGaveUp: (reason: SubscribeGiveUpReason) => void ) {} readPersistedDocId(): string | null { @@ -264,6 +266,10 @@ export class AgentCrdtDocLifecycle { return false } + refusalIsFinal(): boolean { + return !this.confirmedSinceLastSend && this.subscribeRetryTimer === null + } + stopProbing(): void { this.clearAckTimer() this.clearSubscribeRetry() @@ -326,7 +332,7 @@ export class AgentCrdtDocLifecycle { } } - shouldDeferSubscribe(): boolean { + private shouldDeferSubscribe(): boolean { return this.gaveUp || this.subscribeRetryTimer !== null } @@ -423,7 +429,7 @@ export class AgentCrdtDocLifecycle { level: 'warning', tags: { feature_area: 'agent', operation: 'sync', outcome: 'gave_up' } }) - this.onGaveUp() + this.onGaveUp('refused') return true } @@ -444,7 +450,7 @@ export class AgentCrdtDocLifecycle { tags: { feature_area: 'agent', operation: 'sync', outcome: 'gave_up' } } ) - this.onGaveUp() + this.onGaveUp('timeout') } private clearSubscribeRetry(): void { diff --git a/src/workbench/extensions/agent/crdt/agentCrdtStatus.ts b/src/workbench/extensions/agent/crdt/agentCrdtStatus.ts index 6de8c8f940e..ad7776b3d93 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtStatus.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtStatus.ts @@ -1,3 +1,5 @@ +import type { SubscribeGiveUpReason } from './agentCrdtDocLifecycle' + /** * s5-metrics-1: per-outcome counters for every `doc_update` the composable's * listeners observe, replacing the single overloaded `updatesApplied` @@ -44,10 +46,16 @@ export interface AgentCrdtOutcomeCounters { dropped: number } +export type AgentCrdtTerminalState = + | SubscribeGiveUpReason + | 'schema_error' + | null + export interface AgentCrdtStatus { enabled: boolean connected: boolean workflowId: string | null + terminal: AgentCrdtTerminalState /** * Mirror of `bridge.follower.updatesApplied` (Yjs merges, reset to 0 on * `doc_reset` / `follower_replaced`). Not interchangeable with diff --git a/src/workbench/extensions/agent/crdt/apiTransport.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtTransport.test.ts similarity index 64% rename from src/workbench/extensions/agent/crdt/apiTransport.test.ts rename to src/workbench/extensions/agent/crdt/agentCrdtTransport.test.ts index 4ce9c70026e..4efdac3d626 100644 --- a/src/workbench/extensions/agent/crdt/apiTransport.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtTransport.test.ts @@ -1,60 +1,27 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' -import { fromPartial } from '@total-typescript/shoehorn' - -vi.mock(import('@/scripts/app'), () => ({ app: { graph: null } })) -vi.mock(import('@/scripts/api')) - -import { api } from '@/scripts/api' import { createLoggedTransport } from './agentCrdtTransport' import { setCrdtDebugEnabled, setCrdtLogLevel } from './crdtDebugGate' import { clearDevEvents, devEvents, stringifyDevEvents } from './devPanelLog' -import { apiTransport } from './useAgentCrdtFollower' - -describe('apiTransport.send', () => { - beforeEach(() => { - api.socket = null - }) - - it('reports failure instead of throwing when there is no socket yet', () => { - expect(() => apiTransport.send('{}')).not.toThrow() - expect(apiTransport.send('{}')).toBe(false) - }) - - it('reports failure instead of throwing while the socket is still CONNECTING', () => { - const send = vi.fn() - api.socket = fromPartial({ - readyState: WebSocket.CONNECTING, - send - }) - expect(apiTransport.send('{}')).toBe(false) - expect(send).not.toHaveBeenCalled() - }) - - it('sends and reports success once the socket is OPEN', () => { - const send = vi.fn() - api.socket = fromPartial({ readyState: WebSocket.OPEN, send }) - expect(apiTransport.send('frame')).toBe(true) - expect(send).toHaveBeenCalledWith('frame') - }) -}) +import type { DocFrameTransport } from './docFrameClient' describe('createLoggedTransport.send', () => { const frame = '{"type":"doc_ops"}' + const base: DocFrameTransport = { + send: () => true, + addEventListener: () => {}, + removeEventListener: () => {} + } beforeEach(() => { clearDevEvents() - api.socket = fromPartial({ - readyState: WebSocket.OPEN, - send: vi.fn() - }) }) it('leaves the frame unparsed while the debug instrument is off', () => { setCrdtDebugEnabled(false) const parse = vi.spyOn(JSON, 'parse') - expect(createLoggedTransport().send(frame)).toBe(true) + expect(createLoggedTransport(base).send(frame)).toBe(true) expect(parse).not.toHaveBeenCalled() parse.mockRestore() @@ -63,7 +30,7 @@ describe('createLoggedTransport.send', () => { it('records the parsed frame while the instrument is on', () => { setCrdtDebugEnabled(true) - expect(createLoggedTransport().send(frame)).toBe(true) + expect(createLoggedTransport(base).send(frame)).toBe(true) expect(devEvents.value.at(-1)?.detail).toEqual({ delivered: true, @@ -80,7 +47,7 @@ describe('createLoggedTransport.send', () => { data: { value: 'secret-prompt', signed_url: 'secret-url' } }) - expect(createLoggedTransport().send(sensitiveFrame)).toBe(true) + expect(createLoggedTransport(base).send(sensitiveFrame)).toBe(true) expect(vi.mocked(console.debug).mock.calls.at(-1)?.at(-1)).toEqual({ delivered: true, @@ -104,7 +71,7 @@ describe('createLoggedTransport.send', () => { it('keeps an unparsed outbound frame out of the buffer and the copied report', () => { setCrdtDebugEnabled(true) - const transport = createLoggedTransport() + const transport = createLoggedTransport(base) expect(transport.send('not json')).toBe(true) expect(transport.send('[1,2]')).toBe(true) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtTransport.ts b/src/workbench/extensions/agent/crdt/agentCrdtTransport.ts index 991e9c62c72..e0c8a104580 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtTransport.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtTransport.ts @@ -1,23 +1,7 @@ -import { api } from '@/scripts/api' - import { isCrdtDebugEnabled } from './crdtDebugGate' import { wireLog } from './crdtLog' import type { DocFrameTransport } from './docFrameClient' -export const apiTransport: DocFrameTransport = { - send(frame) { - if (api.socket?.readyState !== WebSocket.OPEN) return false - api.socket.send(frame) - return true - }, - addEventListener(type, listener) { - api.addCustomEventListener(type, listener) - }, - removeEventListener(type, listener) { - api.removeCustomEventListener(type, listener) - } -} - /** * Both of `wireLog.trace`'s sinks drop the detail unless the debug instrument * is on, so an ungated parse costs every user edit a full `JSON.parse` of the @@ -38,10 +22,12 @@ function traceableFrame(frame: string): Record | null { } } -export function createLoggedTransport(): DocFrameTransport { +export function createLoggedTransport( + base: DocFrameTransport +): DocFrameTransport { return { send(frame) { - const delivered = apiTransport.send(frame) + const delivered = base.send(frame) const parsed = traceableFrame(frame) wireLog.trace('ws_out', 'outbound frame', { delivered, @@ -51,10 +37,10 @@ export function createLoggedTransport(): DocFrameTransport { return delivered }, addEventListener(type, listener) { - apiTransport.addEventListener(type, listener) + base.addEventListener(type, listener) }, removeEventListener(type, listener) { - apiTransport.removeEventListener(type, listener) + base.removeEventListener(type, listener) } } } diff --git a/src/workbench/extensions/agent/crdt/agentDocFrameTransport.test.ts b/src/workbench/extensions/agent/crdt/agentDocFrameTransport.test.ts new file mode 100644 index 00000000000..a3a6794dd76 --- /dev/null +++ b/src/workbench/extensions/agent/crdt/agentDocFrameTransport.test.ts @@ -0,0 +1,63 @@ +import { describe, expect, it, vi } from 'vitest' + +import { createFakeAgentSocket } from './__fixtures__/agentSocket' + +function received( + transport: ReturnType['transport'], + type: string +): unknown[] { + const details: unknown[] = [] + transport.addEventListener(type, (event) => { + if (event instanceof CustomEvent) details.push(event.detail) + }) + return details +} + +describe('createAgentDocFrameTransport', () => { + it.for([ + 'doc_update', + 'doc_subscribed', + 'doc_ops_result', + 'doc_reset', + 'doc_reseed_result', + 'awareness' + ])('surfaces an inbound %s as an event carrying its data', (type) => { + const fake = createFakeAgentSocket() + const details = received(fake.transport, type) + + fake.receive(type, { workflow_id: 'wf-1', seq: 2 }) + + expect(details).toEqual([{ workflow_id: 'wf-1', seq: 2 }]) + }) + + it('does not surface chat frames to doc listeners', () => { + const fake = createFakeAgentSocket() + const details = received(fake.transport, 'agent_message_delta') + + fake.receive('agent_message_delta', { delta: 'hi' }) + + expect(details).toEqual([]) + }) + + it('reports each time the shared socket opens, so a dropped subscribe can be re-driven', () => { + const fake = createFakeAgentSocket() + const connected = vi.fn() + fake.transport.onSocketOpen(connected) + + fake.open() + fake.drop() + fake.open() + + expect(connected).toHaveBeenCalledTimes(2) + }) + + it('stops listening once destroyed', () => { + const fake = createFakeAgentSocket() + fake.transport.addEventListener('doc_update', vi.fn()) + expect(fake.listenerCount()).toBe(1) + + fake.transport.destroy() + + expect(fake.listenerCount()).toBe(0) + }) +}) diff --git a/src/workbench/extensions/agent/crdt/agentDocFrameTransport.ts b/src/workbench/extensions/agent/crdt/agentDocFrameTransport.ts new file mode 100644 index 00000000000..53d2139d78c --- /dev/null +++ b/src/workbench/extensions/agent/crdt/agentDocFrameTransport.ts @@ -0,0 +1,50 @@ +import type { AgentEventSocket } from '../services/agent/agentEventSource' +import { SERVER_DOC_FRAME_TYPES } from './docFrameCodes' +import type { DocFrameTransport } from './docFrameClient' + +const INBOUND_DOC_FRAMES = new Set(SERVER_DOC_FRAME_TYPES) + +export interface AgentDocFrameTransport extends DocFrameTransport { + onSocketOpen(listener: () => void): () => void + destroy(): void +} + +export function createAgentDocFrameTransport( + source: AgentEventSocket +): AgentDocFrameTransport { + const target = new EventTarget() + let unsubscribe: (() => void) | null = null + + function tap(): void { + if (unsubscribe !== null) return + unsubscribe = source.subscribe((raw) => { + if (typeof raw !== 'object' || raw === null || !('type' in raw)) return + const { type } = raw + if (typeof type !== 'string' || !INBOUND_DOC_FRAMES.has(type)) return + const detail = 'data' in raw ? raw.data : undefined + target.dispatchEvent(new CustomEvent(type, { detail })) + }) + } + + return { + send(frame) { + return source.send(frame) + }, + addEventListener(type, listener) { + tap() + target.addEventListener(type, listener) + }, + removeEventListener(type, listener) { + target.removeEventListener(type, listener) + }, + onSocketOpen(listener) { + return source.onStatus((live) => { + if (live) listener() + }) + }, + destroy() { + unsubscribe?.() + unsubscribe = null + } + } +} diff --git a/src/workbench/extensions/agent/crdt/crdtDebugReport.test.ts b/src/workbench/extensions/agent/crdt/crdtDebugReport.test.ts index 5dba21c6c74..f072a63fab2 100644 --- a/src/workbench/extensions/agent/crdt/crdtDebugReport.test.ts +++ b/src/workbench/extensions/agent/crdt/crdtDebugReport.test.ts @@ -47,6 +47,7 @@ const SNAPSHOT: CrdtDebugSnapshot = { status: { enabled: true, connected: true, + terminal: null, workflowId: 'doc-1', updatesApplied: 3, lastFrameType: 'doc_update', diff --git a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts index 51b1fd9ef9c..eeb8128cbce 100644 --- a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts +++ b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts @@ -3,15 +3,11 @@ import { defineComponent, nextTick, ref } from 'vue' import type { Ref } from 'vue' import { render } from '@testing-library/vue' -import { fromPartial } from '@total-typescript/shoehorn' -import { api } from '@/scripts/api' import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' import type { DocOp } from './docFrameClient' import type { GraphOperation } from './graphOperations' -const apiEvents: EventTarget = api - const bridgeState = vi.hoisted(() => { const transport = { up: true } class FakeBridge extends EventTarget { @@ -124,6 +120,7 @@ vi.mock(import('@/scripts/app'), () => ({ app: { graph: null, canvas: null } })) +import { createFakeAgentSocket } from './__fixtures__/agentSocket' import { useAgentCrdtFollower } from './useAgentCrdtFollower' import type { AgentCrdtStatus } from './agentCrdtStatus' @@ -140,14 +137,20 @@ function mountFollower(initial: string): { workflowId: Ref enqueue: (operations: GraphOperation[]) => Promise status: () => AgentCrdtStatus + reconnect: () => void } { + const agentSocket = createFakeAgentSocket() const workflowId = ref(initial) let enqueue!: (operations: GraphOperation[]) => Promise let exposedStatus!: () => AgentCrdtStatus const host = defineComponent({ setup() { - const { enqueueHumanOperations, status } = - useAgentCrdtFollower(workflowId) + const { enqueueHumanOperations, status } = useAgentCrdtFollower( + workflowId, + { + transport: agentSocket.transport + } + ) enqueue = async (operations) => { enqueueHumanOperations(operations) await Promise.resolve() @@ -158,7 +161,13 @@ function mountFollower(initial: string): { }) const { unmount } = render(host) onTestFinished(unmount) - return { unmount, workflowId, enqueue, status: exposedStatus } + return { + unmount, + workflowId, + enqueue, + status: exposedStatus, + reconnect: () => agentSocket.open() + } } async function switchWorkflow(workflowId: Ref, next: string) { @@ -177,7 +186,6 @@ function dispatchOpsResult(detail: unknown): void { } beforeEach(() => { - api.socket = fromPartial({ readyState: 1, send: vi.fn() }) useAgentPanelStore().enabled = true bridgeState.current = null bridgeState.transport.up = true @@ -421,7 +429,7 @@ function ackResubscribe(workflowId: string): void { */ describe('a human edit made while the document connection is down', () => { it('does not flush a pending batch on reconnect before the resubscribe ack', async () => { - const { enqueue } = mountFollower('wf-a') + const { enqueue, reconnect } = mountFollower('wf-a') clientState.transportUp = false await enqueue([deleteNode('edited-during-outage')]) @@ -433,7 +441,7 @@ describe('a human edit made while the document connection is down', () => { expect(clientState.sent).toHaveLength(0) clientState.transportUp = true - apiEvents.dispatchEvent(new Event('reconnected')) + reconnect() expect(bridge().reconnect).toHaveBeenCalledOnce() expect(clientState.sent).toHaveLength(0) }) @@ -458,7 +466,7 @@ describe('a human edit made while the document connection is down', () => { }) it('does not resend an acknowledged retry on a later reconnect', async () => { - const { enqueue } = mountFollower('wf-a') + const { enqueue, reconnect } = mountFollower('wf-a') clientState.transportUp = false await enqueue([deleteNode('edited-during-outage')]) @@ -482,7 +490,7 @@ describe('a human edit made while the document connection is down', () => { ops: [expect.objectContaining({ op_id: replayedOperationId })] }) ) - apiEvents.dispatchEvent(new Event('reconnected')) + reconnect() ackResubscribe('wf-a') expect(clientState.sent).toHaveLength(1) diff --git a/src/workbench/extensions/agent/crdt/docFrameClient.test.ts b/src/workbench/extensions/agent/crdt/docFrameClient.test.ts index 08f3dd75735..da7e23df437 100644 --- a/src/workbench/extensions/agent/crdt/docFrameClient.test.ts +++ b/src/workbench/extensions/agent/crdt/docFrameClient.test.ts @@ -1,15 +1,16 @@ import { SCHEMA_VERSION } from '@comfyorg/comfy-multi-player' -import { describe, expect, it, vi } from 'vitest' +import { describe, expect, expectTypeOf, it, vi } from 'vitest' import * as Y from 'yjs' import { reportError } from '@/platform/telemetry/reportError' -import type { DocFrameTransport } from './docFrameClient' +import type { DocFrameTransport, ServerDocFrame } from './docFrameClient' import { DocFrameClient, encodeBase64, parseServerDocFrame } from './docFrameClient' +import type { ServerDocFrameType } from './docFrameCodes' import { FollowerDoc } from './followerDoc' import { LayoutFollowerBridge } from './layoutFollowerBridge' @@ -35,6 +36,10 @@ function updateAfter(doc: Y.Doc, mutate: () => void): Uint8Array { } describe('doc frame client', () => { + it('names every server frame type in SERVER_DOC_FRAME_TYPES', () => { + expectTypeOf().toEqualTypeOf() + }) + it('decodes a base64 doc_update that converges through Y.applyUpdate', () => { const source = new Y.Doc() source.getMap('nodes').set('one', { x: 10 }) diff --git a/src/workbench/extensions/agent/crdt/docFrameClient.ts b/src/workbench/extensions/agent/crdt/docFrameClient.ts index dbb3f253534..0c66b19d241 100644 --- a/src/workbench/extensions/agent/crdt/docFrameClient.ts +++ b/src/workbench/extensions/agent/crdt/docFrameClient.ts @@ -3,6 +3,8 @@ import type { DocResetData } from '@comfyorg/ingest-types' import { reportError } from '@/platform/telemetry/reportError' +import { SERVER_DOC_FRAME_TYPES } from './docFrameCodes' + export const DOC_PROTOCOL_VERSION = 1 /** Keep this encoded-field cap aligned with cloud's `MaxDocFrameB64Len`. */ const MAX_DOC_UPDATE_B64_LENGTH = 8 << 20 @@ -476,14 +478,7 @@ export class DocFrameClient extends EventTarget { constructor(private readonly transport: DocFrameTransport) { super() const reportedTypes = new Set() - for (const type of [ - 'doc_update', - 'doc_subscribed', - 'doc_ops_result', - 'doc_reset', - 'doc_reseed_result', - 'awareness' - ]) { + for (const type of SERVER_DOC_FRAME_TYPES) { const listener: EventListener = (event) => { if (!(event instanceof CustomEvent)) return const parsed = parseServerDocFrame({ type, data: event.detail }) diff --git a/src/workbench/extensions/agent/crdt/docFrameCodes.ts b/src/workbench/extensions/agent/crdt/docFrameCodes.ts index 6d434b4273b..6f85a71ac7c 100644 --- a/src/workbench/extensions/agent/crdt/docFrameCodes.ts +++ b/src/workbench/extensions/agent/crdt/docFrameCodes.ts @@ -1,3 +1,14 @@ +export const SERVER_DOC_FRAME_TYPES = [ + 'doc_update', + 'doc_subscribed', + 'doc_ops_result', + 'doc_reset', + 'doc_reseed_result', + 'awareness' +] as const + +export type ServerDocFrameType = (typeof SERVER_DOC_FRAME_TYPES)[number] + export const STALE_SCHEMA_RESEED_REQUIRED = 'stale_schema_reseed_required' export const RESEED_CONFLICT = 'conflict' diff --git a/src/workbench/extensions/agent/crdt/followerProductGate.test.ts b/src/workbench/extensions/agent/crdt/followerProductGate.test.ts index b072f0a98bd..81ee362c5f2 100644 --- a/src/workbench/extensions/agent/crdt/followerProductGate.test.ts +++ b/src/workbench/extensions/agent/crdt/followerProductGate.test.ts @@ -1,20 +1,15 @@ -import { fromPartial } from '@total-typescript/shoehorn' import { render } from '@testing-library/vue' import { expect, it, onTestFinished, vi } from 'vitest' import { defineComponent, nextTick, ref } from 'vue' -import { api } from '@/scripts/api' import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' +import { createFakeAgentSocket } from './__fixtures__/agentSocket' import { useAgentCrdtFollower } from './useAgentCrdtFollower' it('gates real document transport and removes reconnect listeners on revocation', async () => { - const previousSocket = api.socket - const send = vi.fn<(frame: string) => void>() - api.socket = fromPartial({ readyState: WebSocket.OPEN, send }) - onTestFinished(() => { - api.socket = previousSocket - }) + const agentSocket = createFakeAgentSocket() + const { send } = agentSocket const store = useAgentPanelStore() store.enabled = false @@ -23,7 +18,9 @@ it('gates real document transport and removes reconnect listeners on revocation' const { unmount } = render( defineComponent({ setup() { - follower = useAgentCrdtFollower(workflowId) + follower = useAgentCrdtFollower(workflowId, { + transport: agentSocket.transport + }) return () => null } }) @@ -51,8 +48,7 @@ it('gates real document transport and removes reconnect listeners on revocation' type: 'doc_unsubscribe', data: { workflow_id: 'wf-1' } }) - api.dispatchCustomEvent('reconnected') - api.dispatchCustomEvent('status', null) + agentSocket.open() workflowId.value = 'wf-2' await nextTick() vi.advanceTimersByTime(60_000) diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.humanAddTabSwitch.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.humanAddTabSwitch.test.ts index 0a104bb8531..fd381e65087 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.humanAddTabSwitch.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.humanAddTabSwitch.test.ts @@ -5,15 +5,13 @@ import type { WorkflowJSON } from '@comfyorg/comfy-multi-player' import { render } from '@testing-library/vue' -import { fromPartial } from '@total-typescript/shoehorn' import { getActivePinia } from 'pinia' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import { defineComponent, nextTick, ref } from 'vue' import * as Y from 'yjs' import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' import type { ISerialisedGraph } from '@/lib/litegraph/src/types/serialisation' -import { api } from '@/scripts/api' import { useNodeDataStore } from '@/stores/nodeDataStore' import { graphScopeOf } from '@/types/graphScopeId' import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' @@ -21,6 +19,7 @@ import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/ag import { encodeBase64 } from './docFrameClient' import { attachDocOpMinter } from './docOpMinter' import { mintWireOps } from './opEnvelope' +import { createFakeAgentSocket } from './__fixtures__/agentSocket' import { useAgentCrdtFollower } from './useAgentCrdtFollower' const WORKFLOW_ID = 'wf-human-add' @@ -55,11 +54,10 @@ function toWorkflowJson({ nodes, ...rest }: ISerialisedGraph): WorkflowJSON { } } +let agentSocket = createFakeAgentSocket() + function deliver(type: string, data: unknown): void { - EventTarget.prototype.dispatchEvent.call( - api, - new CustomEvent(type, { detail: data }) - ) + agentSocket.receive(type, data) } function frames(sent: string[], type: string) { @@ -95,18 +93,13 @@ describe('a human-added node across a tab switch', () => { useAgentPanelStore().enabled = true LiteGraph.registerNodeType('TestSource', TestSource) LiteGraph.registerNodeType('TestVirtual', TestVirtual) - api.socket = fromPartial({ - readyState: 1, - send: vi.fn((frame) => { - if (typeof frame === 'string') sent.push(frame) - }) + agentSocket = createFakeAgentSocket() + agentSocket.send.mockImplementation((frame) => { + sent.push(frame) + return true }) }) - afterEach(() => { - api.socket = null - }) - function mountBoundFollower(graph: LGraph) { const host = mint(toWorkflowJson(graph.serialize()), CATALOG) const isTargetActive = ref(true) @@ -114,12 +107,11 @@ describe('a human-added node across a tab switch', () => { const view = render( defineComponent({ setup() { - followerApi = useAgentCrdtFollower( - ref(WORKFLOW_ID), - () => null, + followerApi = useAgentCrdtFollower(ref(WORKFLOW_ID), { + transport: agentSocket.transport, isTargetActive, - () => graph - ) + getGraph: () => graph + }) return () => null } }), diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.integration.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.integration.test.ts index ae5eedcd73b..8852ca3dac1 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.integration.test.ts @@ -1,14 +1,12 @@ import { mint, readSchemaVersion } from '@comfyorg/comfy-multi-player' -import { fromPartial } from '@total-typescript/shoehorn' import { getActivePinia } from 'pinia' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import { defineComponent, nextTick, ref, shallowRef } from 'vue' import * as Y from 'yjs' import { render } from '@testing-library/vue' import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' -import { api } from '@/scripts/api' import { toNodeId } from '@/types/nodeId' import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' @@ -18,6 +16,7 @@ import { DOC_HOST_SCHEMA_VERSION } from './__fixtures__/docHostMintedSink' import { encodeBase64 } from './docFrameClient' +import { createFakeAgentSocket } from './__fixtures__/agentSocket' import { useAgentCrdtFollower } from './useAgentCrdtFollower' const sent: string[] = [] @@ -30,11 +29,10 @@ class SinkNode extends LGraphNode { } } +let agentSocket = createFakeAgentSocket() + function deliver(type: string, data: unknown): void { - EventTarget.prototype.dispatchEvent.call( - api, - new CustomEvent(type, { detail: data }) - ) + agentSocket.receive(type, data) } describe('useAgentCrdtFollower over a live graph', () => { @@ -43,18 +41,13 @@ describe('useAgentCrdtFollower over a live graph', () => { vi.stubGlobal('WebSocket', { OPEN: 1 }) useAgentPanelStore().enabled = true LiteGraph.registerNodeType('Sink', SinkNode) - api.socket = fromPartial({ - readyState: 1, - send: vi.fn((frame) => { - if (typeof frame === 'string') sent.push(frame) - }) + agentSocket = createFakeAgentSocket() + agentSocket.send.mockImplementation((frame) => { + sent.push(frame) + return true }) }) - afterEach(() => { - api.socket = null - }) - it('applies a frame delivered before the graph existed once the graph appears', async () => { const graph = shallowRef(null) const host = mint( @@ -64,12 +57,10 @@ describe('useAgentCrdtFollower over a live graph', () => { const view = render( defineComponent({ setup() { - useAgentCrdtFollower( - ref(WORKFLOW_ID), - () => null, - ref(true), - () => graph.value - ) + useAgentCrdtFollower(ref(WORKFLOW_ID), { + transport: agentSocket.transport, + getGraph: () => graph.value + }) return () => null } }), @@ -115,12 +106,10 @@ describe('useAgentCrdtFollower over a live graph', () => { const view = render( defineComponent({ setup() { - useAgentCrdtFollower( - ref(WORKFLOW_ID), - () => null, - ref(true), - () => graph.value - ) + useAgentCrdtFollower(ref(WORKFLOW_ID), { + transport: agentSocket.transport, + getGraph: () => graph.value + }) return () => null } }), diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.rejectedHumanAdd.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.rejectedHumanAdd.test.ts index a7e16b7c7de..16af5d9c9af 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.rejectedHumanAdd.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.rejectedHumanAdd.test.ts @@ -1,14 +1,13 @@ -import { fromPartial } from '@total-typescript/shoehorn' import { render } from '@testing-library/vue' import { expect, it, onTestFinished, vi } from 'vitest' import { defineComponent, ref } from 'vue' import { reportError } from '@/platform/telemetry/reportError' -import { api } from '@/scripts/api' import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' import { parseWireOps } from '@e2e/fixtures/agentWireFrame' +import { createFakeAgentSocket } from './__fixtures__/agentSocket' import { useAgentCrdtFollower } from './useAgentCrdtFollower' // The frame the sender actually put on the wire, narrowed the same way a @@ -39,12 +38,8 @@ const WORKFLOW_ID = 'wf-1' // the class is absent from the pinned catalog, so its named widget values // cannot be projected and the add is rejected. it('surfaces a human add_node the doc host rejected instead of swallowing the result', async () => { - const previousSocket = api.socket - const send = vi.fn<(frame: string) => void>() - api.socket = fromPartial({ readyState: WebSocket.OPEN, send }) - onTestFinished(() => { - api.socket = previousSocket - }) + const agentSocket = createFakeAgentSocket() + const { send } = agentSocket const store = useAgentPanelStore() store.enabled = true onTestFinished(() => { @@ -54,7 +49,9 @@ it('surfaces a human add_node the doc host rejected instead of swallowing the re const { unmount } = render( defineComponent({ setup() { - follower = useAgentCrdtFollower(ref(WORKFLOW_ID)) + follower = useAgentCrdtFollower(ref(WORKFLOW_ID), { + transport: agentSocket.transport + }) return () => null } }) @@ -87,26 +84,20 @@ it('surfaces a human add_node the doc host rejected instead of swallowing the re const { type, op_id } = sentOp(send.mock.calls[1][0]) expect(type).toBe('doc_ops') - // The transport listens on `api`; a frame from the socket is a CustomEvent there. - EventTarget.prototype.dispatchEvent.call( - api, - new CustomEvent('doc_ops_result', { - detail: { - v: 1, - workflow_id: WORKFLOW_ID, - ok: false, - applied: [], - skipped: [], - failed: { - index: 0, - op_id, - code: 'uncatalogued_widget_write', - message: - 'add_node(Note): named widgets_values for a class absent from the pinned catalog cannot be projected' - } - } - }) - ) + agentSocket.receive('doc_ops_result', { + v: 1, + workflow_id: WORKFLOW_ID, + ok: false, + applied: [], + skipped: [], + failed: { + index: 0, + op_id, + code: 'uncatalogued_widget_write', + message: + 'add_node(Note): named widgets_values for a class absent from the pinned catalog cannot be projected' + } + }) expect(reportError).toHaveBeenCalledWith( expect.any(Error), diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.rejectedWidgetWrite.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.rejectedWidgetWrite.test.ts index 1fb3473b71f..60e66004940 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.rejectedWidgetWrite.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.rejectedWidgetWrite.test.ts @@ -1,4 +1,3 @@ -import { fromPartial } from '@total-typescript/shoehorn' import { render } from '@testing-library/vue' import { describe, expect, it, onTestFinished, vi } from 'vitest' import { defineComponent, ref } from 'vue' @@ -6,12 +5,12 @@ import { defineComponent, ref } from 'vue' import { useToast } from '@/components/ui/toast/toastStore' import { i18n } from '@/i18n' import { reportError } from '@/platform/telemetry/reportError' -import { api } from '@/scripts/api' import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' import { parseWireOps } from '@e2e/fixtures/agentWireFrame' import type { GraphOperation } from './graphOperations' +import { createFakeAgentSocket } from './__fixtures__/agentSocket' import { useAgentCrdtFollower } from './useAgentCrdtFollower' vi.mock(import('@/platform/telemetry/reportError')) @@ -74,12 +73,10 @@ function docOpsFrames(raw: string[]): string[][] { return raw.map(mintedOpIds).filter((ids) => ids.length > 0) } -/** The transport listens on `api`; a server frame is a CustomEvent there. */ +let agentSocket = createFakeAgentSocket() + function answerWithOpsResult(detail: Record): void { - EventTarget.prototype.dispatchEvent.call( - api, - new CustomEvent('doc_ops_result', { detail }) - ) + agentSocket.receive('doc_ops_result', detail) } /** @@ -90,12 +87,8 @@ function answerWithOpsResult(detail: Record): void { function mountFollower() { // The telemetry dedupe is per notifier and each mount makes a new one. vi.mocked(reportError).mockClear() - const previousSocket = api.socket - const send = vi.fn<(frame: string) => void>() - api.socket = fromPartial({ readyState: WebSocket.OPEN, send }) - onTestFinished(() => { - api.socket = previousSocket - }) + agentSocket = createFakeAgentSocket() + const { send } = agentSocket const store = useAgentPanelStore() store.enabled = true onTestFinished(() => { @@ -106,7 +99,9 @@ function mountFollower() { const { unmount } = render( defineComponent({ setup() { - follower = useAgentCrdtFollower(ref(WORKFLOW_ID)) + follower = useAgentCrdtFollower(ref(WORKFLOW_ID), { + transport: agentSocket.transport + }) return () => null } }) diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index 96f5e524ac9..5d7ec4e56cb 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -16,7 +16,6 @@ import { render } from '@testing-library/vue' import { fromPartial } from '@total-typescript/shoehorn' import type { LGraph } from '@/lib/litegraph/src/litegraph' -import { api } from '@/scripts/api' import { useTelemetry } from '@/platform/telemetry' import type { reportError as reportErrorFn } from '@/platform/telemetry/reportError' @@ -28,8 +27,7 @@ import type { DocNodeDelta, FrameOutcome } from './agentCrdtProjection' import type { DocFrameTransport } from './docFrameClient' import type { GraphOperation } from './graphOperations' import type { BatchOutcome, OpSenderDeps } from './opSender' - -const apiEvents: EventTarget = api +import { createFakeAgentSocket } from './__fixtures__/agentSocket' const bridgeState = vi.hoisted(() => { const liveSubscriptions = new Map>() @@ -171,7 +169,6 @@ vi.mock(import('@/platform/telemetry/reportError'), () => ({ })) vi.mock(import('@/platform/telemetry')) -vi.mock(import('@/scripts/api')) vi.mock(import('@/scripts/app'), () => ({ app: { graph: null, canvas: null } })) @@ -183,6 +180,7 @@ import { SUBSCRIBE_CATCHUP_GRACE_MS, useAgentCrdtFollower } from './useAgentCrdtFollower' +import type { AgentCrdtFollowerOptions } from './useAgentCrdtFollower' import type { AgentCrdtStatus } from './agentCrdtStatus' const DOC_ID_KEY = 'Comfy.Agent.CrdtDocId' @@ -217,9 +215,13 @@ function mountFollower( initial: string | null = null, initiallyActive = true, getGraph: () => LGraph | null = () => null, - events: Parameters[4] = {}, - applierDeps: Parameters[5] = {}, - canvasFor: Parameters[6] = () => null + events: AgentCrdtFollowerOptions['events'] = {}, + { + transport = agentSocket.transport, + canvasFor + }: Pick & { + transport?: AgentCrdtFollowerOptions['transport'] + } = {} ): { unmount: () => void workflowId: Ref @@ -235,12 +237,7 @@ function mountFollower( setup() { const { status, enqueueHumanOperations } = useAgentCrdtFollower( workflowId, - () => null, - isTargetActive, - getGraph, - events, - applierDeps, - canvasFor + { transport, isTargetActive, getGraph, events, canvasFor } ) exposedStatus = () => status.value as AgentCrdtStatus enqueue = enqueueHumanOperations @@ -263,13 +260,15 @@ function reportedTeardownErrors(): unknown[] { .map(([cause]) => cause) } +let agentSocket = createFakeAgentSocket() + function dispatchFrame(type: string, detail: unknown): void { bridge().dispatchEvent(new CustomEvent(type, { detail })) } describe('useAgentCrdtFollower', () => { beforeEach(() => { - api.socket = fromPartial({ readyState: 1, send: vi.fn() }) + agentSocket = createFakeAgentSocket() useAgentPanelStore().enabled = true sessionStorage.clear() bridgeState.current = null @@ -358,7 +357,7 @@ describe('useAgentCrdtFollower', () => { first.dispatchEvent( new CustomEvent('doc_update', { detail: { workflowId: 'wf-1', seq: 42 } }) ) - apiEvents.dispatchEvent(new Event('reconnected')) + agentSocket.open() vi.advanceTimersByTime(STALE_AFTER_MS * 2) workflowId.value = 'wf-2' await nextTick() @@ -513,6 +512,143 @@ describe('useAgentCrdtFollower', () => { }) }) + describe('terminal follower state', () => { + function refuse(): void { + dispatchFrame('doc_subscribed', { ok: false, workflow_id: 'wf-1' }) + } + + function refuseForGood(): void { + dispatchFrame('doc_subscribed', { + ok: false, + workflow_id: 'wf-1', + code: 'schema_version_mismatch' + }) + } + + it('stays non-terminal while a refused subscribe still has a retry behind it', () => { + const { unmount, status } = mountFollower('wf-1') + + refuse() + + expect(status().connected).toBe(false) + expect(status().terminal).toBeNull() + unmount() + }) + + it('reports a permanent refusal as terminal, and clears it on the next confirmed subscribe', () => { + const { unmount, status } = mountFollower('wf-1') + + refuseForGood() + + expect(status().terminal).toBe('refused') + expect(status().connected).toBe(false) + expect(status().workflowId).toBe('wf-1') + + dispatchFrame('doc_subscribed', { ok: true, workflow_id: 'wf-1', seq: 1 }) + + expect(status().terminal).toBeNull() + expect(status().connected).toBe(true) + unmount() + }) + + it('reports a schema error as terminal, cleared by the next confirmed subscribe', () => { + const { unmount, status } = mountFollower('wf-1') + dispatchFrame('doc_subscribed', { ok: true, workflow_id: 'wf-1', seq: 1 }) + + dispatchFrame('schema_error', { workflowId: 'wf-1', code: 'unreadable' }) + expect(status().terminal).toBe('schema_error') + expect(status().workflowId).toBe('wf-1') + + dispatchFrame('doc_subscribed', { ok: true, workflow_id: 'wf-1', seq: 2 }) + expect(status().terminal).toBeNull() + unmount() + }) + + it('a retarget clears the terminal state', async () => { + const { unmount, status, workflowId } = mountFollower('wf-1') + refuseForGood() + expect(status().terminal).toBe('refused') + + workflowId.value = 'wf-2' + await nextTick() + + expect(status().terminal).toBeNull() + expect(status().workflowId).toBe('wf-2') + unmount() + }) + + it('an ordinary reconnect drops connected without becoming terminal', () => { + const { unmount, status } = mountFollower('wf-1') + dispatchFrame('doc_subscribed', { ok: true, workflow_id: 'wf-1', seq: 1 }) + expect(status().connected).toBe(true) + + agentSocket.open() + + expect(status().connected).toBe(false) + expect(status().terminal).toBeNull() + unmount() + }) + + it('reports a terminal timeout once the silent-subscribe budget is spent', () => { + const { unmount, status } = mountFollower('wf-1') + dispatchFrame('doc_subscribe_sent', { workflowId: 'wf-1' }) + vi.advanceTimersByTime(SUBSCRIBE_ACK_TIMEOUT_MS) + dispatchFrame('doc_subscribe_sent', { workflowId: 'wf-1' }) + vi.advanceTimersByTime(SUBSCRIBE_ACK_TIMEOUT_MS) + dispatchFrame('doc_subscribe_sent', { workflowId: 'wf-1' }) + expect(status().terminal).toBeNull() + + vi.advanceTimersByTime(SUBSCRIBE_ACK_TIMEOUT_MS) + + expect(status().terminal).toBe('timeout') + expect(status().workflowId).toBe('wf-1') + unmount() + }) + + it('a reconnect clears a terminal refusal while the new subscribe is in flight', () => { + const { unmount, status } = mountFollower('wf-1') + dispatchFrame('doc_subscribed', { + ok: false, + workflow_id: 'wf-1', + code: 'schema_version_mismatch' + }) + expect(status().terminal).toBe('refused') + + agentSocket.open() + + expect(status().terminal).toBeNull() + expect(status().connected).toBe(false) + expect(status().workflowId).toBe('wf-1') + unmount() + }) + + it('marks the same permanent refusal terminal again after a reconnect', () => { + const { unmount, status } = mountFollower('wf-1') + refuseForGood() + agentSocket.open() + + refuseForGood() + + expect(status().terminal).toBe('refused') + unmount() + }) + + it('reports a refusal terminal once the retry budget is spent', () => { + const { unmount, status } = mountFollower('wf-1') + for (let attempt = 0; attempt < 6; attempt++) { + refuse() + vi.advanceTimersByTime(500 * 2 ** attempt) + } + expect(bridge().resubscribe).toHaveBeenCalledTimes(6) + expect(status().terminal).toBeNull() + + refuse() + + expect(status().terminal).toBe('refused') + unmount() + }) + }) + it('FE-2158: one follower retargets across a tab switch and releases across remount', async () => { // AgentPanelRoot.vue creates a SINGLE follower and drives it with the // reactive boundWorkflowId / isBoundWorkflowActive. Exercising the switch @@ -809,7 +945,7 @@ describe('useAgentCrdtFollower', () => { dispatchFrame('doc_subscribed', { ok: true }) expect(status().connected).toBe(true) - apiEvents.dispatchEvent(new Event('reconnected')) + agentSocket.open() expect(status().connected).toBe(false) expect(bridge().reconnect).toHaveBeenCalledOnce() @@ -895,28 +1031,6 @@ describe('useAgentCrdtFollower', () => { unmount() }) - it('re-drives subscription intent on every status frame', () => { - const { unmount } = mountFollower('wf-1') - - apiEvents.dispatchEvent(new Event('status')) - - expect(bridge().reconcile).toHaveBeenCalled() - unmount() - }) - - it('does not bypass refused-subscribe backoff on status frames', () => { - vi.useFakeTimers() - const { unmount } = mountFollower('wf-1') - dispatchFrame('doc_subscribed', { ok: false }) - - apiEvents.dispatchEvent(new Event('status')) - expect(bridge().reconcile).not.toHaveBeenCalled() - - vi.advanceTimersByTime(500) - expect(bridge().resubscribe).toHaveBeenCalledTimes(1) - unmount() - }) - it('PM-1604 / BE-11437: retries a retryable refusal code with backoff', () => { vi.useFakeTimers() const { unmount } = mountFollower('wf-1') @@ -941,7 +1055,6 @@ describe('useAgentCrdtFollower', () => { }) vi.advanceTimersByTime(60_000) - apiEvents.dispatchEvent(new Event('status')) expect(bridge().resubscribe).not.toHaveBeenCalled() expect(bridge().reconcile).not.toHaveBeenCalled() @@ -966,7 +1079,6 @@ describe('useAgentCrdtFollower', () => { }) vi.advanceTimersByTime(60_000) - apiEvents.dispatchEvent(new Event('status')) expect(bridge().resubscribe).not.toHaveBeenCalled() expect(bridge().reconcile).not.toHaveBeenCalled() @@ -991,7 +1103,6 @@ describe('useAgentCrdtFollower', () => { }) vi.advanceTimersByTime(60_000) - apiEvents.dispatchEvent(new Event('status')) expect(bridge().resubscribe).not.toHaveBeenCalled() expect(bridge().reconcile).not.toHaveBeenCalled() @@ -1975,7 +2086,9 @@ describe('useAgentCrdtFollower', () => { >['enqueueHumanOperations'] const host = defineComponent({ setup() { - const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId) + const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId, { + transport: agentSocket.transport + }) enqueue = enqueueHumanOperations return () => null } @@ -2228,7 +2341,9 @@ describe('useAgentCrdtFollower', () => { >['enqueueHumanOperations'] const host = defineComponent({ setup() { - const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId) + const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId, { + transport: agentSocket.transport + }) enqueue = enqueueHumanOperations return () => null } @@ -2263,7 +2378,9 @@ describe('useAgentCrdtFollower', () => { >['enqueueHumanOperations'] const host = defineComponent({ setup() { - const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId) + const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId, { + transport: agentSocket.transport + }) enqueue = enqueueHumanOperations return () => null } @@ -2300,13 +2417,10 @@ describe('useAgentCrdtFollower', () => { >['enqueueHumanOperations'] const host = defineComponent({ setup() { - const { enqueueHumanOperations } = useAgentCrdtFollower( - workflowId, - () => null, - ref(true), - () => null, - { onSyncError } - ) + const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId, { + transport: agentSocket.transport, + events: { onSyncError } + }) enqueue = enqueueHumanOperations return () => null } @@ -2346,7 +2460,9 @@ describe('useAgentCrdtFollower', () => { >['enqueueHumanOperations'] const host = defineComponent({ setup() { - const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId) + const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId, { + transport: agentSocket.transport + }) enqueue = enqueueHumanOperations return () => null } @@ -2405,7 +2521,9 @@ describe('useAgentCrdtFollower', () => { >['enqueueHumanOperations'] const host = defineComponent({ setup() { - const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId) + const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId, { + transport: agentSocket.transport + }) enqueue = enqueueHumanOperations return () => null } @@ -2511,11 +2629,10 @@ describe('useAgentCrdtFollower', () => { let enqueue!: Enqueue const host = defineComponent({ setup() { - const { enqueueHumanOperations } = useAgentCrdtFollower( - workflowId, - () => null, + const { enqueueHumanOperations } = useAgentCrdtFollower(workflowId, { + transport: agentSocket.transport, isTargetActive - ) + }) enqueue = enqueueHumanOperations return () => null } @@ -2775,7 +2892,7 @@ describe('useAgentCrdtFollower', () => { // recovery; the heartbeat stays disarmed until the next confirm. dispatchFrame('doc_subscribed', { ok: true }) bridge().resubscribe.mockClear() - apiEvents.dispatchEvent(new Event('reconnected')) + agentSocket.open() const reconnectResubscribes = bridge().resubscribe.mock.calls.length vi.advanceTimersByTime(STALE_AFTER_MS * 2) expect(bridge().resubscribe.mock.calls.length).toBe(reconnectResubscribes) @@ -2814,7 +2931,7 @@ describe('useAgentCrdtFollower', () => { vi.advanceTimersByTime(SUBSCRIBE_ACK_TIMEOUT_MS) expect(bridge().resubscribe).toHaveBeenCalledTimes(2) - apiEvents.dispatchEvent(new Event('reconnected')) + agentSocket.open() expect(bridge().reconnect).toHaveBeenCalledOnce() dispatchFrame('doc_subscribe_sent', { workflowId: 'wf-1' }) vi.advanceTimersByTime(SUBSCRIBE_ACK_TIMEOUT_MS) @@ -2852,10 +2969,7 @@ describe('useAgentCrdtFollower', () => { dispatchFrame('doc_subscribed', { ok: false }) vi.advanceTimersByTime(30_000) expect(bridge().resubscribe).toHaveBeenCalledTimes(2) - apiEvents.dispatchEvent(new Event('status')) - expect(bridge().reconcile).not.toHaveBeenCalled() - - apiEvents.dispatchEvent(new Event('reconnected')) + agentSocket.open() expect(bridge().reconnect).toHaveBeenCalledOnce() dispatchFrame('doc_subscribe_sent', { workflowId: 'wf-1' }) vi.advanceTimersByTime(SUBSCRIBE_ACK_TIMEOUT_MS) @@ -2863,12 +2977,26 @@ describe('useAgentCrdtFollower', () => { unmount() }) + function transportFailingToStop( + failure: unknown + ): AgentCrdtFollowerOptions['transport'] { + return { + ...agentSocket.transport, + onSocketOpen: () => () => { + throw failure + } + } + } + it('tears down totally on unmount, reporting every cleanup failure instead of throwing', () => { const hookErrors: unknown[] = [] + const listenerFailure = new Error('listener removal failed') + const clientFailure = new Error('client destroy failed') + const transport = transportFailingToStop(listenerFailure) const workflowId = ref('wf-1') const host = defineComponent({ setup() { - useAgentCrdtFollower(workflowId) + useAgentCrdtFollower(workflowId, { transport }) return () => null } }) @@ -2881,11 +3009,6 @@ describe('useAgentCrdtFollower', () => { } } }) - const listenerFailure = new Error('listener removal failed') - const clientFailure = new Error('client destroy failed') - vi.mocked(api.removeEventListener).mockImplementation((type) => { - if (type === 'reconnected') throw listenerFailure - }) clientState.destroy.mockImplementation(() => { throw clientFailure }) @@ -2900,17 +3023,20 @@ describe('useAgentCrdtFollower', () => { expect(projectionState.destroy).toHaveBeenCalled() expect(bridge().destroy).toHaveBeenCalled() expect(clientState.destroy).toHaveBeenCalled() - expect(api.removeEventListener).toHaveBeenCalledWith( - 'status', - expect.any(Function) - ) }) it('reports cleanup failures that throw a nullish value', () => { - const { unmount } = mountFollower('wf-1') - vi.mocked(api.removeEventListener).mockImplementation((type) => { - if (type === 'reconnected') throw null - if (type === 'status') throw undefined + const { unmount } = mountFollower( + 'wf-1', + true, + () => null, + {}, + { + transport: transportFailingToStop(null) + } + ) + clientState.destroy.mockImplementation(() => { + throw undefined }) unmount() @@ -2936,7 +3062,7 @@ describe('useAgentCrdtFollower', () => { canvasFor: (workflowId: string) => Record | null = () => canvas ) { - return mountFollower('wf-1', true, () => null, {}, {}, canvasFor) + return mountFollower('wf-1', true, () => null, {}, { canvasFor }) } function refuseAsStale(frame: Record = staleRefusal) { @@ -3027,7 +3153,6 @@ describe('useAgentCrdtFollower', () => { ok: false, code: 'stale_schema_reseed_refused' }) - apiEvents.dispatchEvent(new Event('status')) vi.advanceTimersByTime(10 * 60_000) expect(bridge().resubscribe).not.toHaveBeenCalled() @@ -3052,6 +3177,43 @@ describe('useAgentCrdtFollower', () => { unmount() }) + it('keeps the draft seed withheld while a reseed is in flight, and releases it once the reseed is refused for good', () => { + vi.useFakeTimers() + const { unmount, status } = mountWithCanvas() + + refuseAsStale() + expect(status().terminal).toBeNull() + + dispatchFrame('doc_reseed_result', { + workflowId: 'wf-1', + ok: false, + code: 'stale_schema_reseed_refused' + }) + expect(status().terminal).toBe('refused') + unmount() + }) + + it('a reconnect on the agent socket clears a refused reseed and lets the new session reseed once', () => { + vi.useFakeTimers() + const { unmount, status } = mountWithCanvas() + refuseAsStale() + dispatchFrame('doc_reseed_result', { + workflowId: 'wf-1', + ok: false, + code: 'stale_schema_reseed_refused' + }) + expect(status().terminal).toBe('refused') + + agentSocket.open() + + expect(bridge().reconnect).toHaveBeenCalledOnce() + expect(status().terminal).toBeNull() + refuseAsStale() + expect(bridge().reseed).toHaveBeenCalledTimes(2) + expect(status().terminal).toBeNull() + unmount() + }) + it('a retarget allows one reseed for the new binding', () => { vi.useFakeTimers() const { unmount, workflowId } = mountWithCanvas() diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 606ee6437df..ba58e0bdfc0 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -16,7 +16,6 @@ import type { LGraph } from '@/lib/litegraph/src/litegraph' import { useTelemetry } from '@/platform/telemetry' import { reportError } from '@/platform/telemetry/reportError' import type { AgentGraphProjectionStage } from '@/platform/telemetry/types' -import { api } from '@/scripts/api' import { parseNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' import { createUuidv4 } from '@/utils/uuid' @@ -24,6 +23,7 @@ import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/ag import type { AgentCrdtOutcomeCounters, + AgentCrdtTerminalState, AgentCrdtStatus } from './agentCrdtStatus' import { @@ -33,7 +33,8 @@ import { } from './agentCrdtDocLifecycle' import { AgentCrdtProjection } from './agentCrdtProjection' import type { DocNodeDelta, FrameOutcome } from './agentCrdtProjection' -import { apiTransport, createLoggedTransport } from './agentCrdtTransport' +import { createLoggedTransport } from './agentCrdtTransport' +import type { AgentDocFrameTransport } from './agentDocFrameTransport' import { recordDevEvent } from './devPanelLog' import type { CrdtDebugSnapshot } from './crdtSnapshot' import { readCrdtSnapshot } from './crdtSnapshot' @@ -50,7 +51,7 @@ import { createOpSender } from './opSender' import type { BatchOutcome, OpsResultView } from './opSender' import { createRejectedOpNotifier } from './rejectedOpNotice' -export { apiTransport, STALE_AFTER_MS, SUBSCRIBE_CATCHUP_GRACE_MS } +export { STALE_AFTER_MS, SUBSCRIBE_CATCHUP_GRACE_MS } interface ProjectionFrameId { opIds?: readonly string[] @@ -188,7 +189,7 @@ function opsResultDropReason( return workflowId === subscribedWorkflowId ? null : 'workflow_mismatch' } -export interface AgentCrdtFollowerEvents { +interface AgentCrdtFollowerEvents { onMaterialized?: (event: { workflowId: string actor: string | undefined @@ -228,7 +229,7 @@ function runFollowerTeardown(cleanups: readonly (() => void)[]): void { } } -export type AgentCrdtApplierDeps = Omit< +type AgentCrdtApplierDeps = Omit< LiveGraphApplierDeps, 'getGraph' | 'holdsLocalWrite' > @@ -258,19 +259,32 @@ function reportRejectedHumanOps( ) } -export function useAgentCrdtFollower( - workflowId: Ref, - userId: () => string | null = () => null, - isTargetActive: Ref = ref(true), +export interface AgentCrdtFollowerOptions { + transport: AgentDocFrameTransport + userId?: () => string | null + isTargetActive?: Ref /** * Live graph the document is applied to. Reactive reads inside the getter * are tracked, so a `null` → graph flip syncs the graph from the document * without waiting for the next remote frame. */ - getGraph: () => LGraph | null = () => null, - events: AgentCrdtFollowerEvents = {}, - applierDeps: AgentCrdtApplierDeps = {}, - canvasFor: (workflowId: string) => Record | null = () => null + getGraph?: () => LGraph | null + events?: AgentCrdtFollowerEvents + applierDeps?: AgentCrdtApplierDeps + canvasFor?: (workflowId: string) => Record | null +} + +export function useAgentCrdtFollower( + workflowId: Ref, + { + transport, + userId = () => null, + isTargetActive = ref(true), + getGraph = () => null, + events = {}, + applierDeps = {}, + canvasFor = () => null + }: AgentCrdtFollowerOptions ) { const productGate = useAgentPanelStore() const follower = shallowRef>() @@ -280,6 +294,7 @@ export function useAgentCrdtFollower( workflowId: null, updatesApplied: 0, lastFrameType: null, + terminal: null, outcomes: { received: 0, applied: 0, @@ -303,15 +318,15 @@ export function useAgentCrdtFollower( scope.stop() }) follower.value = scope.run(() => - startAgentCrdtFollower( - workflowId, + startAgentCrdtFollower(workflowId, { + transport, userId, isTargetActive, getGraph, events, applierDeps, canvasFor - ) + }) ) }, { immediate: true, flush: 'sync' } @@ -335,20 +350,25 @@ export function useAgentCrdtFollower( follower.value?.docPromotedWidgets(nodeId) ?? null, mayWritePromotedHost: (nodeId: NodeId) => follower.value?.mayWritePromotedHost(nodeId) ?? false, - notifyWidgetWriteRefused: () => follower.value?.notifyWidgetWriteRefused() + notifyWidgetWriteRefused: () => follower.value?.notifyWidgetWriteRefused(), + subscriptionSent: () => follower.value?.subscriptionSent() ?? false } } function startAgentCrdtFollower( workflowId: Ref, - userId: () => string | null, - isTargetActive: Ref, - getGraph: () => LGraph | null, - events: AgentCrdtFollowerEvents, - applierDeps: AgentCrdtApplierDeps, - canvasFor: (workflowId: string) => Record | null + { + transport, + userId, + isTargetActive, + getGraph, + events, + applierDeps, + canvasFor + }: Required ) { const connected = ref(false) + const terminal = ref(null) const updatesApplied = ref(0) const lastFrameType = ref(null) const subscribedWorkflowId = ref(null) @@ -363,13 +383,14 @@ function startAgentCrdtFollower( dropped: 0 }) - const client = new DocFrameClient(createLoggedTransport()) + const client = new DocFrameClient(createLoggedTransport(transport)) const bridge = new LayoutFollowerBridge(client) const lifecycle = new AgentCrdtDocLifecycle( () => subscribedWorkflowId.value, () => bridge.resubscribe(), - () => { + (reason) => { connected.value = false + terminal.value = reason } ) const tabId = createUuidv4() @@ -589,6 +610,7 @@ function startAgentCrdtFollower( if (detail.ok === true || code === RESEED_CONFLICT) return if (isRetryableReseedCode(code)) lifecycle.onSubscribeRefused(code) else lifecycle.stopProbing() + if (lifecycle.refusalIsFinal()) terminal.value = 'refused' } function handleRejectedSubscription( @@ -599,9 +621,11 @@ function startAgentCrdtFollower( message?: unknown } | null ) { - const refusal = tryReseed() + const reseeding = tryReseed() + const refusal = reseeding ? { shouldNotify: false } : handleSubscribeRefusal(detail, lifecycle) + if (!reseeding && lifecycle.refusalIsFinal()) terminal.value = 'refused' releaseHeldOps() sender.abortIfUnbound() if (refusal.shouldNotify) @@ -621,6 +645,7 @@ function startAgentCrdtFollower( lastFrameType.value = event.type recordDevEvent('doc_subscribed', event.detail ?? null) if (ok) { + terminal.value = null lifecycle.onSubscribeConfirmed() resumeHeldOpsIfSubscribed() } else { @@ -728,6 +753,7 @@ function startAgentCrdtFollower( // nothing was projected. Surface it as its own status rather than as a // generic "disconnected", which is indistinguishable from "never connected". connected.value = false + terminal.value = 'schema_error' lastFrameType.value = event.type lifecycle.clearStaleProbe() const detail = @@ -778,31 +804,13 @@ function startAgentCrdtFollower( if (typeof detail?.workflowId !== 'string') return lifecycle.onSubscribeSent(detail.workflowId) } - const onReconnected: EventListener = () => { + function onSocketOpen(): void { connected.value = false + terminal.value = null lifecycle.onReconnected() recordDevEvent('reconnected', null) bridge.reconnect() } - /** - * Re-drive subscription intent whenever the socket may have become usable. - * - * `reconnected` fires only on a RE-connect (`api.ts` guards the dispatch with - * `isReconnect`), so it can never repair a follower that mounted while the - * first socket was still being opened — `createSocket` awaits an auth token - * before `new WebSocket(...)`, and a panel mounted inside that window used to - * stay inert forever. The ComfyUI server sends a `status` frame immediately - * on every accepted connection, first one included, so it is the earliest - * signal available that the socket can now carry a frame. `reconcile()` is a - * no-op once intent and reality agree, so the extra `status` traffic costs - * nothing unless a refused subscribe has a scheduled retry. In that case, - * the retry timer owns the next attempt and its backoff. - */ - const onSocketActivity: EventListener = () => { - if (lifecycle.shouldDeferSubscribe()) return - bridge.reconcile() - resumeHeldOpsIfSubscribed() - } bridge.addEventListener('doc_subscribed', onSubscribed) bridge.addEventListener('doc_update', onUpdate) @@ -814,8 +822,7 @@ function startAgentCrdtFollower( bridge.addEventListener('doc_stale', onStale) bridge.addEventListener('doc_subscribe_sent', onSubscribeSent) bridge.addEventListener('doc_reseed_result', onReseedResult) - api.addEventListener('reconnected', onReconnected) - api.addEventListener('status', onSocketActivity) + const stopTransportConnected = transport.onSocketOpen(onSocketOpen) // FE-1902 (poc-3): distinguish the mount-time null (in-memory doc id died // with the previous mount — rebind from sessionStorage) from a later null @@ -951,6 +958,7 @@ function startAgentCrdtFollower( if (acknowledgesFollowedDoc(next, active, justActivated)) return lifecycle.clearForRetarget() connected.value = false + terminal.value = null pendingLiveNodeIds.clear() if (!active) { deactivateTarget(next, previous?.[0] ?? null) @@ -970,8 +978,7 @@ function startAgentCrdtFollower( // update twice after a remount. runFollowerTeardown([ () => lifecycle.destroy(), - () => api.removeEventListener('reconnected', onReconnected), - () => api.removeEventListener('status', onSocketActivity), + () => stopTransportConnected(), () => bridge.removeEventListener('doc_subscribed', onSubscribed), () => bridge.removeEventListener('doc_update', onUpdate), () => bridge.removeEventListener('doc_ops_result', onOpsResult), @@ -1000,6 +1007,7 @@ function startAgentCrdtFollower( enabled: true, connected: connected.value, workflowId: subscribedWorkflowId.value, + terminal: terminal.value, updatesApplied: updatesApplied.value, lastFrameType: lastFrameType.value, outcomes: outcomes.value @@ -1032,7 +1040,10 @@ function startAgentCrdtFollower( (sentAddsToCurrentDoc().has(String(nodeId)) && readDocPromotedWidgets(bridge.follower.doc, String(nodeId)) === null), notifyWidgetWriteRefused: () => - rejectedOpNotifier.notifyWidgetWriteRefused() + rejectedOpNotifier.notifyWidgetWriteRefused(), + subscriptionSent: () => + bridge.subscribedWorkflowId !== null && + bridge.subscribedWorkflowId === subscribedWorkflowId.value } } diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts index 07a6646115d..601d57f6432 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts @@ -1,7 +1,6 @@ import { assert, describe, expect, it } from 'vitest' import { - AGENT_WS_EVENT_TYPES, isAgentEvent, parseAgentWsEvent, zAgentCancelAccepted, @@ -347,21 +346,26 @@ describe('agentApiSchema contract subtleties', () => { ).toBe(false) }) - it('exposes the Agent event types, including the ask lifecycle', () => { - expect([...AGENT_WS_EVENT_TYPES]).toEqual( - expect.arrayContaining([ - 'agent_active_tab', - 'agent_ask', - 'agent_ask_resolved', - 'agent_message_delta', - 'agent_message_done', - 'agent_message_draft', - 'agent_thinking', - 'agent_tool_call' - ]) - ) + it.for([ + 'agent_active_tab', + 'agent_ask', + 'agent_ask_resolved', + 'agent_message_delta', + 'agent_message_done', + 'agent_message_draft', + 'agent_thinking', + 'agent_tool_call' + ])('recognizes %s as an Agent event', (type) => { + expect(isAgentEvent(type)).toBe(true) }) + it.for(['doc_update', 'doc_subscribed', 'status', 'executing', 'agent_'])( + 'does not recognize %s as an Agent event', + (type) => { + expect(isAgentEvent(type)).toBe(false) + } + ) + it('parses an agent_message_draft frame, including the empty draft that withdraws one', () => { for (const text of ['Here is your video', '']) { const parsed = parseAgentWsEvent({ diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.ts index 13c9159c2ea..8e20bf9ee0c 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.ts @@ -6,6 +6,7 @@ import { zAgentAttachmentRejected, zAgentCancelAccepted, zAgentError as zGeneratedAgentError, + zAgentIdentity as zGeneratedAgentIdentity, zAgentMessage as zGeneratedAgentMessage, zAgentPendingAskBase, zAgentRunMode as zGeneratedAgentRunMode, @@ -355,7 +356,7 @@ export const zAgentWsEvent = z.discriminatedUnion('type', [ ]) export type AgentWsEvent = z.infer -export const AGENT_WS_EVENT_TYPES: ReadonlySet = new Set( +const AGENT_WS_EVENT_TYPES: ReadonlySet = new Set( zAgentWsEvent.options.map((option) => option.shape.type.value) ) @@ -368,3 +369,7 @@ export function parseAgentWsEvent( ): z.ZodSafeParseResult { return zAgentWsEvent.safeParse(value) } + +export const zAgentIdentity = zGeneratedAgentIdentity.extend({ + user_id: z.string().min(1) +}) diff --git a/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts b/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts new file mode 100644 index 00000000000..83c918efb97 --- /dev/null +++ b/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts @@ -0,0 +1,22 @@ +import { vi } from 'vitest' + +import type { + agentSocketAuthParams as realAgentSocketAuthParams, + ensureSignedIn as realEnsureSignedIn, + hasAgentCredential as realHasAgentCredential, + withAgentAuth as realWithAgentAuth +} from '../agentAuth' + +export const withAgentAuth = vi.fn( + async (init) => init +) + +export const agentSocketAuthParams = vi.fn( + async () => new URLSearchParams() +) + +export const hasAgentCredential = vi.fn( + () => true +) + +export const ensureSignedIn = vi.fn(async () => true) diff --git a/src/workbench/extensions/agent/services/agent/agentAuth.test.ts b/src/workbench/extensions/agent/services/agent/agentAuth.test.ts new file mode 100644 index 00000000000..398b73b7977 --- /dev/null +++ b/src/workbench/extensions/agent/services/agent/agentAuth.test.ts @@ -0,0 +1,198 @@ +vi.mock(import('firebase/auth')) +vi.mock(import('@/composables/auth/useAuthDialogs')) +vi.mock(import('@/scripts/api')) +const distribution = vi.hoisted(() => ({ isCloud: false })) +vi.mock(import('@/platform/distribution/types'), () => distribution) +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { useAuthDialogs } from '@/composables/auth/useAuthDialogs' +import { api } from '@/scripts/api' +import { useAuthStore } from '@/stores/authStore' +import type { AuthCredential, AuthHeader, AuthScheme } from '@/types/authTypes' + +import { + agentSocketAuthParams, + ensureSignedIn, + withAgentAuth +} from './agentAuth' + +beforeEach(() => { + distribution.isCloud = false +}) + +function userHeader(header: AuthHeader | null): void { + vi.spyOn(useAuthStore(), 'getUserAuthHeader').mockResolvedValue(header) +} + +describe('withAgentAuth', () => { + it.for<{ + account: string + header: AuthHeader + name: string + value: string + credential: AuthCredential + }>([ + { + account: 'a signed-in session', + header: { Authorization: 'Bearer id-token' }, + name: 'Authorization', + value: 'Bearer id-token', + credential: 'bearer' + }, + { + account: 'an API-key session', + header: { 'X-API-KEY': 'comfyui-key' }, + name: 'X-API-KEY', + value: 'comfyui-key', + credential: 'api-key' + } + ])( + "sends $account's user auth header, keeping the request's own", + async ({ header, name, value, credential }) => { + userHeader(header) + const reported: AuthCredential[] = [] + const schemes: AuthScheme[] = [] + + const init = await withAgentAuth({ + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + onAuthScheme: (scheme) => schemes.push(scheme), + onAuthCredential: (kind) => reported.push(kind) + }) + init.onAuthScheme?.('none') + init.onAuthCredential?.('none') + + const headers = new Headers(init.headers) + expect(headers.get(name)).toBe(value) + expect(headers.get('Content-Type')).toBe('application/json') + expect(init.method).toBe('POST') + expect(init.redirect).toBe('error') + expect(reported).toEqual([credential]) + expect(schemes).toEqual(['cloud-auth-header']) + } + ) + + it('leaves a cloud request to fetchApi, never attaching the personal token', async () => { + distribution.isCloud = true + userHeader({ Authorization: 'Bearer personal-id-token' }) + + const init = await withAgentAuth({ method: 'GET' }) + + expect(new Headers(init.headers).has('Authorization')).toBe(false) + expect(init.redirect).toBeUndefined() + }) + + it('sends nothing and leaves the redirect policy alone for a signed-out user', async () => { + userHeader(null) + + const init = await withAgentAuth({ method: 'GET' }) + + expect(new Headers(init.headers).has('Authorization')).toBe(false) + expect(new Headers(init.headers).has('X-API-KEY')).toBe(false) + expect(init.redirect).toBeUndefined() + }) +}) + +describe('agentSocketAuthParams', () => { + it.for<{ label: string; header: AuthHeader; token: string }>([ + { + label: 'a bearer token', + header: { Authorization: 'Bearer id-token' }, + token: 'id-token' + }, + { + label: 'an API key', + header: { 'X-API-KEY': 'comfyui-key' }, + token: 'comfyui-key' + } + ])( + 'passes the user credential as the token off-cloud: $label', + async ({ header, token }) => { + userHeader(header) + + expect((await agentSocketAuthParams())?.get('token')).toBe(token) + } + ) + + it('connects without a token when signed out', async () => { + userHeader(null) + + expect((await agentSocketAuthParams())?.has('token')).toBe(false) + }) + + it.for([ + { + label: 'its auth params', + params: new URLSearchParams({ workspace_id: 'ws-1' }) + }, + { label: 'that no one can open it', params: null } + ])( + 'takes the cloud socket auth from the /ws auth seam, reporting $label', + async ({ params }) => { + distribution.isCloud = true + userHeader({ Authorization: 'Bearer personal-id-token' }) + vi.mocked(api.socketAuthParams).mockResolvedValue(params) + + await expect(agentSocketAuthParams()).resolves.toBe(params) + } + ) +}) + +describe('ensureSignedIn', () => { + it('lets a signed-in user send without asking', async () => { + userHeader({ Authorization: 'Bearer id-token' }) + + await expect(ensureSignedIn()).resolves.toBe(true) + expect(useAuthDialogs().showSignInDialog).not.toHaveBeenCalled() + }) + + it('asks a signed-out user to sign in and holds the turn if they decline', async () => { + userHeader(null) + vi.mocked(useAuthDialogs().showSignInDialog).mockResolvedValue(false) + + await expect(ensureSignedIn()).resolves.toBe(false) + expect(useAuthDialogs().showSignInDialog).toHaveBeenCalledOnce() + }) + + it('sends once the sign-in dialog produced a session', async () => { + const spy = vi + .spyOn(useAuthStore(), 'getUserAuthHeader') + .mockResolvedValue(null) + vi.mocked(useAuthDialogs().showSignInDialog).mockImplementation( + async () => { + spy.mockResolvedValue({ Authorization: 'Bearer fresh-token' }) + return true + } + ) + + await expect(ensureSignedIn()).resolves.toBe(true) + }) + + it('holds the turn when the sign-in dialog fails to open', async () => { + userHeader(null) + vi.mocked(useAuthDialogs().showSignInDialog).mockRejectedValue( + new Error('chunk failed') + ) + + await expect(ensureSignedIn()).resolves.toBe(false) + }) + + it.for([ + { mode: 'development', proxyCredential: 'true', asks: false }, + { mode: 'development', proxyCredential: '', asks: true }, + { mode: 'production', proxyCredential: 'true', asks: true } + ])( + 'asks a signed-out user to sign in: $asks (mode $mode, proxy credential "$proxyCredential")', + async ({ mode, proxyCredential, asks }) => { + vi.stubEnv('MODE', mode) + vi.stubEnv('VITE_DEV_AGENT_PROXY_CREDENTIAL', proxyCredential) + userHeader(null) + + await ensureSignedIn() + + expect(useAuthDialogs().showSignInDialog).toHaveBeenCalledTimes( + asks ? 1 : 0 + ) + } + ) +}) diff --git a/src/workbench/extensions/agent/services/agent/agentAuth.ts b/src/workbench/extensions/agent/services/agent/agentAuth.ts new file mode 100644 index 00000000000..45b86bc2d61 --- /dev/null +++ b/src/workbench/extensions/agent/services/agent/agentAuth.ts @@ -0,0 +1,67 @@ +import { useAuthDialogs } from '@/composables/auth/useAuthDialogs' +import { authCredentialOf } from '@/platform/auth/authCredential' +import { isCloud } from '@/platform/distribution/types' +import { api } from '@/scripts/api' +import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore' +import { useAuthStore } from '@/stores/authStore' +import type { AuthHeader } from '@/types/authTypes' + +type AgentRequestInit = NonNullable[1]> + +function credentialValue(header: AuthHeader): string { + return 'X-API-KEY' in header + ? header['X-API-KEY'] + : header.Authorization.slice('Bearer '.length) +} + +export async function withAgentAuth( + init: AgentRequestInit +): Promise { + if (isCloud) return init + const header = await useAuthStore().getUserAuthHeader() + if (!header) return init + const headers = new Headers(init.headers) + for (const [name, value] of Object.entries(header)) headers.set(name, value) + const attached = authCredentialOf(header) + return { + ...init, + headers, + redirect: 'error', + onAuthScheme: () => init.onAuthScheme?.('cloud-auth-header'), + onAuthCredential: () => init.onAuthCredential?.(attached) + } +} + +export async function agentSocketAuthParams(): Promise { + if (isCloud) return api.socketAuthParams() + const params = new URLSearchParams() + const header = await useAuthStore().getUserAuthHeader() + if (header) params.set('token', credentialValue(header)) + return params +} + +export function hasAgentCredential(): boolean { + return ( + useAuthStore().isAuthenticated || + useApiKeyAuthStore().getAuthHeader() !== null + ) +} + +function devProxySuppliesCredential(): boolean { + return ( + import.meta.env.MODE === 'development' && + import.meta.env.VITE_DEV_AGENT_PROXY_CREDENTIAL === 'true' + ) +} + +export async function ensureSignedIn(): Promise { + const authStore = useAuthStore() + if (await authStore.getUserAuthHeader()) return true + if (devProxySuppliesCredential()) return true + try { + if (!(await useAuthDialogs().showSignInDialog())) return false + } catch { + return false + } + return hasAgentCredential() || (await authStore.getUserAuthHeader()) !== null +} diff --git a/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts b/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts index 7f3d0545833..8a3d0bfbba3 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts @@ -1,99 +1,491 @@ -import { describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' -import { AGENT_WS_EVENT_TYPES } from '../../schemas/agentApiSchema' - -import type { AgentEventHost } from './agentEventSource' import { createAgentEventSource } from './agentEventSource' -function fakeHost(readyState?: number) { - const target = new EventTarget() - const registered = new Set() - const host: AgentEventHost = { - socket: readyState === undefined ? null : { readyState }, - addCustomEventListener(type, listener) { - registered.add(type) - target.addEventListener(type, listener as EventListener) - }, - removeCustomEventListener(type, listener) { - registered.delete(type) - target.removeEventListener(type, listener as EventListener) - }, - addEventListener(type, listener) { - target.addEventListener(type, listener) - }, - removeEventListener(type, listener) { - target.removeEventListener(type, listener) - } +class FakeSocket extends EventTarget { + static readonly CONNECTING = 0 + static readonly OPEN = 1 + static readonly CLOSED = 3 + static instances: FakeSocket[] = [] + + readyState = FakeSocket.CONNECTING + send = vi.fn() + + constructor(readonly url: string) { + super() + FakeSocket.instances.push(this) } - const emit = (type: string, data?: unknown): void => { - target.dispatchEvent(new CustomEvent(type, { detail: data })) + + open(): void { + this.readyState = FakeSocket.OPEN + this.dispatchEvent(new Event('open')) + } + + receive(frame: unknown): void { + this.dispatchEvent( + new MessageEvent('message', { data: JSON.stringify(frame) }) + ) + } + + receiveRaw(data: string): void { + this.dispatchEvent(new MessageEvent('message', { data })) } - return { host, emit, registered } + + error(): void { + this.dispatchEvent(new Event('error')) + } + + close(): void { + if (this.readyState === FakeSocket.CLOSED) return + this.readyState = FakeSocket.CLOSED + this.dispatchEvent(new Event('close')) + } +} + +async function connected(): Promise { + await vi.advanceTimersByTimeAsync(0) +} + +function sockets(): FakeSocket[] { + return FakeSocket.instances } +function lastSocket(): FakeSocket { + const socket = FakeSocket.instances.at(-1) + if (!socket) throw new Error('no socket was opened') + return socket +} + +function createSource( + options: { + endpoint?: string + getAuthParams?: () => Promise + random?: () => number + maxReconnectDelayMs?: number + } = {} +) { + return createAgentEventSource({ + endpoint: '/api/agent/events', + getAuthParams: async () => new URLSearchParams(), + random: () => 1, + ...options + }) +} + +function tokenParams(token: string): URLSearchParams { + return new URLSearchParams({ token }) +} + +beforeEach(() => { + vi.useFakeTimers({ shouldAdvanceTime: false }) + FakeSocket.instances = [] + vi.stubGlobal('WebSocket', FakeSocket) +}) + describe('createAgentEventSource', () => { - it('registers every agent event type and delivers frames as {type, data}', () => { - const { host, emit, registered } = fakeHost() - const seen = vi.fn() + it.for([ + ['http://agent.test/api/agent/events', 'ws:'], + ['https://agent.test/api/agent/events', 'wss:'] + ])( + 'transforms %s into a %s socket endpoint', + async ([endpoint, protocol]) => { + const source = createSource({ endpoint }) + + source.subscribe(vi.fn()) + await connected() + + const socketUrl = new URL(lastSocket().url) + expect(socketUrl.protocol).toBe(protocol) + expect(socketUrl.host).toBe('agent.test') + expect(socketUrl.pathname).toBe('/api/agent/events') + } + ) - createAgentEventSource(host).subscribe(seen) + it('delivers JSON frames', async () => { + const source = createSource() + const seen = vi.fn() - expect(registered).toEqual(new Set(AGENT_WS_EVENT_TYPES)) + source.subscribe(seen) + await connected() + lastSocket().open() + lastSocket().receive({ type: 'agent_message_delta', data: { delta: 'hi' } }) - emit('agent_message_delta', { delta: 'hi' }) expect(seen).toHaveBeenCalledWith({ type: 'agent_message_delta', data: { delta: 'hi' } }) }) - it('stops delivering after unsubscribe', () => { - const { host, emit, registered } = fakeHost() + it('reports the current state to a status listener that joins an open socket', async () => { + const source = createSource() + const status = vi.fn() + + source.subscribe(vi.fn()) + await connected() + lastSocket().open() + source.onStatus(status) + + expect(status.mock.calls).toEqual([[true]]) + }) + + it('forwards an empty text frame instead of dropping it', async () => { + const source = createSource() const seen = vi.fn() - const unsubscribe = createAgentEventSource(host).subscribe(seen) - unsubscribe() + source.subscribe(seen) + await connected() + lastSocket().open() + lastSocket().receiveRaw('') - emit('agent_message_delta', { delta: 'late' }) - expect(seen).not.toHaveBeenCalled() - expect(registered.size).toBe(0) + expect(seen).toHaveBeenCalledWith('') }) - it('maps reconnecting/reconnected to liveness', () => { - const { host, emit } = fakeHost() + it('reconnects exactly once when the upgrade fails before open', async () => { + const source = createSource() const status = vi.fn() - createAgentEventSource(host).onStatus?.(status) - expect(status).toHaveBeenCalledOnce() - expect(status).toHaveBeenLastCalledWith(false) + source.subscribe(vi.fn()) + await connected() + source.onStatus(status) + lastSocket().error() + await vi.advanceTimersByTimeAsync(999) - emit('reconnecting') - expect(status).toHaveBeenLastCalledWith(false) + expect(sockets()[0].readyState).toBe(FakeSocket.CLOSED) + expect(status.mock.calls).toEqual([[false], [false]]) + expect(sockets()).toHaveLength(1) - emit('reconnected') - expect(status).toHaveBeenLastCalledWith(true) + await vi.advanceTimersByTimeAsync(1) + expect(sockets()).toHaveLength(2) + + await vi.advanceTimersByTimeAsync(5000) + expect(sockets()).toHaveLength(2) }) - it('reports live once when the socket is already open at bind time', () => { - const { host } = fakeHost(WebSocket.OPEN) + it('reports live again once the replacement socket opens', async () => { + const source = createSource() const status = vi.fn() - createAgentEventSource(host).onStatus?.(status) + source.subscribe(vi.fn()) + await connected() + source.onStatus(status) + sockets()[0].open() + sockets()[0].close() + await vi.advanceTimersByTimeAsync(1000) + sockets()[1].open() + + expect(status.mock.calls.map(([live]) => live)).toEqual([ + false, + true, + false, + true + ]) + }) + + it('drops frames from a superseded socket after resubscribing', async () => { + const source = createSource() + const first = vi.fn() + const second = vi.fn() + + const unsubscribe = source.subscribe(first) + await connected() + sockets()[0].open() + unsubscribe() + source.subscribe(second) + await connected() + sockets()[1].open() + sockets()[0].receive({ + type: 'agent_message_delta', + data: { delta: 'old' } + }) + sockets()[1].receive({ + type: 'agent_message_delta', + data: { delta: 'new' } + }) + + expect(sockets()).toHaveLength(2) + expect(first).not.toHaveBeenCalled() + expect(second).toHaveBeenCalledTimes(1) + expect(second).toHaveBeenCalledWith({ + type: 'agent_message_delta', + data: { delta: 'new' } + }) + }) + + it('stops the socket and pending reconnect on unsubscribe', async () => { + const source = createSource() + + const unsubscribe = source.subscribe(vi.fn()) + await connected() + sockets()[0].open() + sockets()[0].close() + unsubscribe() + await vi.advanceTimersByTimeAsync(1000) - expect(status).toHaveBeenCalledTimes(1) - expect(status).toHaveBeenCalledWith(true) + expect(sockets()).toHaveLength(1) }) - it('stays quiet for a connecting socket and after status unsubscribe', () => { - const { host, emit } = fakeHost(WebSocket.CONNECTING) + it('reports the stream down when the last subscriber leaves', async () => { + const source = createSource() const status = vi.fn() + source.onStatus(status) + + const unsubscribe = source.subscribe(vi.fn()) + await connected() + sockets()[0].open() + unsubscribe() + + expect(status.mock.calls).toEqual([[false], [true], [false]]) + expect(sockets()[0].readyState).toBe(FakeSocket.CLOSED) + }) +}) + +describe('createAgentEventSource reconnect backoff', () => { + it('doubles the delay per refused connect and resets once a socket delivers a frame', async () => { + const source = createSource() + + source.subscribe(vi.fn()) + await connected() + sockets()[0].error() + await vi.advanceTimersByTimeAsync(1000) + sockets()[1].error() + await vi.advanceTimersByTimeAsync(1999) + expect(sockets()).toHaveLength(2) + await vi.advanceTimersByTimeAsync(1) + expect(sockets()).toHaveLength(3) + + sockets()[2].open() + sockets()[2].receive({ type: 'agent_message_delta', data: {} }) + sockets()[2].close() + await vi.advanceTimersByTimeAsync(1000) + expect(sockets()).toHaveLength(4) + }) + + it('keeps backing off when the server accepts the upgrade and then closes', async () => { + const source = createSource() + + source.subscribe(vi.fn()) + await connected() + for (const wait of [1000, 2000, 4000]) { + lastSocket().open() + lastSocket().close() + const opened = sockets().length + await vi.advanceTimersByTimeAsync(wait - 1) + expect(sockets()).toHaveLength(opened) + await vi.advanceTimersByTimeAsync(1) + expect(sockets()).toHaveLength(opened + 1) + } + }) + + it('reconnects promptly after a socket that stayed open closes without traffic', async () => { + const source = createSource() + source.subscribe(vi.fn()) + await connected() + sockets()[0].error() + await vi.advanceTimersByTimeAsync(1_000) + sockets()[1].error() + await vi.advanceTimersByTimeAsync(2_000) + + sockets()[2].open() + await vi.advanceTimersByTimeAsync(30_000) + sockets()[2].close() + await vi.advanceTimersByTimeAsync(1_000) + + expect(sockets()).toHaveLength(4) + }) + + it('caps the delay and spreads it with jitter', async () => { + const source = createSource({ maxReconnectDelayMs: 4000, random: () => 0 }) + + source.subscribe(vi.fn()) + await connected() + for (const wait of [500, 1000, 2000, 2000]) { + lastSocket().error() + await vi.advanceTimersByTimeAsync(wait) + } + + expect(sockets()).toHaveLength(5) + }) +}) + +describe('createAgentEventSource reconnect', () => { + it('drops the open socket and connects again with a freshly read credential', async () => { + const tokens = ['account-a', 'account-b'] + const source = createSource({ + getAuthParams: async () => tokenParams(tokens.shift() ?? '') + }) + source.subscribe(vi.fn()) + await connected() + sockets()[0].open() + + source.reconnect() + await connected() + + expect(sockets()[0].readyState).toBe(FakeSocket.CLOSED) + expect(sockets()).toHaveLength(2) + expect(new URL(sockets()[1].url).searchParams.get('token')).toBe( + 'account-b' + ) + }) + + it('opens the socket with the newer token when a reconnect lands while the first token is read', async () => { + const pending: ((token: string) => void)[] = [] + const source = createSource({ + getAuthParams: () => + new Promise((resolve) => + pending.push((token) => resolve(tokenParams(token))) + ) + }) + + source.subscribe(vi.fn()) + source.reconnect() + pending[0]('account-a') + await connected() + pending[1]('account-b') + await connected() + + expect( + sockets().map((socket) => new URL(socket.url).searchParams.get('token')) + ).toEqual(['account-b']) + }) + + it('opens nothing when no one is listening', async () => { + const source = createSource() + + source.reconnect() + await connected() + + expect(sockets()).toHaveLength(0) + }) +}) + +describe('createAgentEventSource token', () => { + it('presents the caller credential as ?token=, read afresh on every connect', async () => { + const tokens = ['first', 'second'] + const source = createSource({ + getAuthParams: async () => tokenParams(tokens.shift() ?? '') + }) + + source.subscribe(vi.fn()) + await connected() + sockets()[0].open() + sockets()[0].close() + await vi.advanceTimersByTimeAsync(1000) + + expect(new URL(sockets()[0].url).searchParams.get('token')).toBe('first') + expect(new URL(sockets()[1].url).searchParams.get('token')).toBe('second') + }) + + it('connects without a token when the read returns no credential', async () => { + const source = createSource({ + getAuthParams: async () => new URLSearchParams() + }) + + source.subscribe(vi.fn()) + await connected() + + expect(new URL(lastSocket().url).searchParams.has('token')).toBe(false) + }) + + it('opens no socket while the auth read keeps rejecting', async () => { + const getAuthParams = vi.fn(async (): Promise => { + throw new Error('token refresh failed') + }) + const source = createSource({ getAuthParams }) + + source.subscribe(vi.fn()) + await connected() + await vi.advanceTimersByTimeAsync(1000 + 2000 + 4000) + + expect(getAuthParams).toHaveBeenCalledTimes(4) + expect(sockets()).toHaveLength(0) + }) + + it('opens the socket with the credential once a later auth read succeeds', async () => { + const reads: (() => Promise)[] = [ + async () => { + throw new Error('token refresh failed') + }, + async () => tokenParams('recovered') + ] + const source = createSource({ + getAuthParams: () => reads.shift()?.() ?? Promise.resolve(null) + }) + const status = vi.fn() + source.onStatus(status) + + source.subscribe(vi.fn()) + await connected() + expect(sockets()).toHaveLength(0) + + await vi.advanceTimersByTimeAsync(1000) + sockets()[0].open() + + expect( + sockets().map((socket) => new URL(socket.url).searchParams.get('token')) + ).toEqual(['recovered']) + expect(status.mock.calls).toEqual([[false], [false], [true]]) + }) + + it('opens nothing when no one can authenticate the socket', async () => { + const source = createSource({ getAuthParams: async () => null }) + + source.subscribe(vi.fn()) + await connected() + + expect(sockets()).toHaveLength(0) + }) + + it('carries every auth param onto the socket URL', async () => { + const source = createAgentEventSource({ + endpoint: '/api/agent/events', + getAuthParams: async () => new URLSearchParams({ workspace_id: 'ws-1' }) + }) + + source.subscribe(vi.fn()) + await connected() + + const params = new URL(lastSocket().url).searchParams + expect(params.get('workspace_id')).toBe('ws-1') + expect(params.has('token')).toBe(false) + }) + + it('opens nothing when the last listener leaves while the token is read', async () => { + const source = createSource({ getAuthParams: async () => tokenParams('t') }) + + source.subscribe(vi.fn())() + await connected() + + expect(sockets()).toHaveLength(0) + }) + + it('opens exactly one socket when a listener returns while the token is read', async () => { + const source = createSource({ getAuthParams: async () => tokenParams('t') }) + + source.subscribe(vi.fn())() + source.subscribe(vi.fn()) + await connected() + + expect(sockets()).toHaveLength(1) + }) +}) + +describe('createAgentEventSource send', () => { + it('reports false before the socket is open and writes nothing', async () => { + const source = createSource() + source.subscribe(vi.fn()) + await connected() + + expect(source.send('{"type":"doc_subscribe"}')).toBe(false) + expect(lastSocket().send).not.toHaveBeenCalled() + }) - const unsubscribe = createAgentEventSource(host).onStatus?.(status) - expect(status).toHaveBeenCalledOnce() - expect(status).toHaveBeenLastCalledWith(false) + it('writes the frame verbatim on the open socket and reports true', async () => { + const source = createSource() + source.subscribe(vi.fn()) + await connected() + lastSocket().open() - unsubscribe?.() - emit('reconnected') - expect(status).toHaveBeenCalledOnce() + expect(source.send('{"type":"doc_subscribe"}')).toBe(true) + expect(lastSocket().send).toHaveBeenCalledWith('{"type":"doc_subscribe"}') }) }) diff --git a/src/workbench/extensions/agent/services/agent/agentEventSource.ts b/src/workbench/extensions/agent/services/agent/agentEventSource.ts index 7cffdc7e781..eb34b60296d 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventSource.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventSource.ts @@ -1,49 +1,161 @@ +import { useTimeoutFn } from '@vueuse/core' + import type { AgentEventSource } from '../../composables/agent/useAgentSession' -import { AGENT_WS_EVENT_TYPES } from '../../schemas/agentApiSchema' - -export interface AgentEventHost { - socket: { readyState: number } | null - addCustomEventListener( - type: string, - listener: (event: CustomEvent) => void - ): void - removeCustomEventListener( - type: string, - listener: (event: CustomEvent) => void - ): void - addEventListener( - type: 'reconnecting' | 'reconnected', - listener: () => void - ): void - removeEventListener( - type: 'reconnecting' | 'reconnected', - listener: () => void - ): void + +export interface AgentEventSocket extends AgentEventSource { + send(frame: string): boolean + reconnect(): void + onStatus(listener: (live: boolean) => void): () => void +} + +interface AgentEventSourceOptions { + endpoint: string + getAuthParams: () => Promise + reconnectDelayMs?: number + maxReconnectDelayMs?: number + random?: () => number } -const OPEN = 1 +type AuthRead = { ok: true; params: URLSearchParams | null } | { ok: false } + +export function createAgentEventSource({ + endpoint, + getAuthParams, + reconnectDelayMs = 1000, + maxReconnectDelayMs = 30_000, + random = Math.random +}: AgentEventSourceOptions): AgentEventSocket { + const listeners = new Set<(raw: unknown) => void>() + const statusListeners = new Set<(live: boolean) => void>() + let socket: WebSocket | null = null + let nextReconnectDelayMs = reconnectDelayMs + const reconnectTimer = useTimeoutFn( + () => void connect(), + () => nextReconnectDelayMs, + { + immediate: false + } + ) + let connectGeneration = 0 + let connecting = false + let failedConnects = 0 + + function eventUrl(authParams: URLSearchParams): string { + const url = new URL(endpoint, window.location.href) + url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:' + for (const [name, value] of authParams) url.searchParams.set(name, value) + return url.toString() + } + + function notifyStatus(live: boolean): void { + statusListeners.forEach((listener) => listener(live)) + } + + function scheduleReconnect(): void { + if (listeners.size === 0 || reconnectTimer.isPending.value) return + const ceiling = Math.min( + maxReconnectDelayMs, + reconnectDelayMs * 2 ** failedConnects + ) + failedConnects += 1 + nextReconnectDelayMs = ceiling * (0.5 + 0.5 * random()) + reconnectTimer.start() + } + + async function connect(): Promise { + if (listeners.size === 0 || socket !== null || connecting) return + connecting = true + const generation = connectGeneration + let auth: AuthRead + try { + auth = { ok: true, params: await getAuthParams() } + } catch { + auth = { ok: false } + } finally { + connecting = false + } + if (generation !== connectGeneration) { + void connect() + return + } + if (listeners.size === 0) return + if (!auth.ok) { + notifyStatus(false) + scheduleReconnect() + return + } + if (auth.params === null) return + open(eventUrl(auth.params)) + } + + function open(url: string): void { + const current = new WebSocket(url) + socket = current + let openedAt: number | null = null + current.addEventListener('open', () => { + if (socket !== current) return + openedAt = Date.now() + notifyStatus(true) + }) + current.addEventListener('message', (event) => { + if (socket !== current) return + failedConnects = 0 + let frame: unknown = event.data + if (typeof event.data === 'string') { + try { + frame = JSON.parse(event.data) + } catch { + frame = event.data + } + } + listeners.forEach((listener) => listener(frame)) + }) + current.addEventListener('error', () => { + if (socket === current) current.close() + }) + current.addEventListener('close', () => { + if (socket !== current) return + if (openedAt !== null && Date.now() - openedAt >= maxReconnectDelayMs) + failedConnects = 0 + socket = null + notifyStatus(false) + scheduleReconnect() + }) + } + + function disconnect(): void { + connectGeneration++ + reconnectTimer.stop() + const current = socket + socket = null + if (current === null) return + current.close() + notifyStatus(false) + } -export function createAgentEventSource(host: AgentEventHost): AgentEventSource { return { - subscribe(listener) { - const unbinders = [...AGENT_WS_EVENT_TYPES].map((type) => { - const onEvent = (event: CustomEvent): void => - listener({ type, data: event.detail }) - host.addCustomEventListener(type, onEvent) - return () => host.removeCustomEventListener(type, onEvent) - }) - return () => unbinders.forEach((unbind) => unbind()) + reconnect() { + disconnect() + failedConnects = 0 + void connect() }, - onStatus(listener) { - const onDown = (): void => listener(false) - const onUp = (): void => listener(true) - host.addEventListener('reconnecting', onDown) - host.addEventListener('reconnected', onUp) - listener(host.socket?.readyState === OPEN) + send(frame) { + if (socket?.readyState !== WebSocket.OPEN) return false + socket.send(frame) + return true + }, + subscribe(listener) { + listeners.add(listener) + void connect() return () => { - host.removeEventListener('reconnecting', onDown) - host.removeEventListener('reconnected', onUp) + listeners.delete(listener) + if (listeners.size === 0) disconnect() } + }, + onStatus(listener) { + statusListeners.add(listener) + listener(socket?.readyState === WebSocket.OPEN) + return () => statusListeners.delete(listener) } } } diff --git a/src/workbench/extensions/agent/services/agent/agentIdentity.test.ts b/src/workbench/extensions/agent/services/agent/agentIdentity.test.ts new file mode 100644 index 00000000000..140ce55bbc0 --- /dev/null +++ b/src/workbench/extensions/agent/services/agent/agentIdentity.test.ts @@ -0,0 +1,270 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { isRetryableRequestFailure } from '../../composables/agent/useAgentSession' +import { resolveAgentIdentity } from './agentIdentity' +import { AgentApiError } from './agentRestClient' + +function harness( + getIdentity: () => Promise<{ userId: string }>, + isRetryable: (error: unknown) => boolean = () => true +) { + const connected = new Set<() => void>() + const onFailure = vi.fn() + const identity = resolveAgentIdentity({ + getIdentity, + onSocketOpen(listener) { + connected.add(listener) + return () => connected.delete(listener) + }, + isRetryable, + onFailure + }) + return { + ...identity, + connect: () => connected.forEach((listener) => listener()), + onFailure + } +} + +async function flush(): Promise { + await vi.advanceTimersByTimeAsync(0) +} + +function failing(): () => Promise<{ userId: string }> { + return vi.fn(async () => { + throw new Error('down') + }) +} + +beforeEach(() => { + vi.useFakeTimers({ shouldAdvanceTime: false }) +}) + +describe('resolveAgentIdentity', () => { + it('forgets the identity on reset and looks it up again', async () => { + const getIdentity = vi + .fn<() => Promise<{ userId: string }>>() + .mockResolvedValueOnce({ userId: 'account-a' }) + .mockResolvedValueOnce({ userId: 'account-b' }) + const identity = harness(getIdentity) + await flush() + expect(identity.state.value).toEqual({ + status: 'resolved', + userId: 'account-a' + }) + + identity.reset() + expect(identity.state.value).toEqual({ status: 'pending' }) + await flush() + + expect(identity.state.value).toEqual({ + status: 'resolved', + userId: 'account-b' + }) + }) + + it('discards a lookup that was still in flight for the previous account', async () => { + let finishStale!: (value: { userId: string }) => void + const getIdentity = vi + .fn<() => Promise<{ userId: string }>>() + .mockImplementationOnce( + () => + new Promise((resolve) => { + finishStale = resolve + }) + ) + .mockResolvedValueOnce({ userId: 'account-b' }) + const identity = harness(getIdentity) + + identity.reset() + await flush() + finishStale({ userId: 'account-a' }) + await flush() + + expect(identity.state.value).toEqual({ + status: 'resolved', + userId: 'account-b' + }) + }) + + it('reports a failed lookup and retries it on its own while the socket stays open', async () => { + const failure = new Error('identity route down') + const getIdentity = vi + .fn<() => Promise<{ userId: string }>>() + .mockRejectedValueOnce(failure) + .mockRejectedValueOnce(failure) + .mockResolvedValue({ userId: 'local-user' }) + + const identity = harness(getIdentity) + await flush() + expect(identity.onFailure).toHaveBeenCalledWith(failure) + expect(identity.state.value).toEqual({ status: 'pending' }) + + await vi.advanceTimersByTimeAsync(1_000 + 2_000) + + expect(getIdentity).toHaveBeenCalledTimes(3) + expect(identity.state.value).toEqual({ + status: 'resolved', + userId: 'local-user' + }) + }) + + it('retries at once when the socket connects again after a failure', async () => { + const getIdentity = vi + .fn<() => Promise<{ userId: string }>>() + .mockRejectedValueOnce(new Error('agent still starting')) + .mockResolvedValue({ userId: 'local-user' }) + + const identity = harness(getIdentity) + await flush() + identity.connect() + await flush() + + expect(getIdentity).toHaveBeenCalledTimes(2) + expect(identity.state.value).toEqual({ + status: 'resolved', + userId: 'local-user' + }) + }) + + it('doubles the retry delay per failure up to the cap, so a hard failure cannot spin', async () => { + const getIdentity = failing() + const identity = harness(getIdentity) + await flush() + + const waits = [1_000, 2_000, 4_000, 8_000, 16_000, 30_000, 30_000] + for (const [index, wait] of waits.entries()) { + await vi.advanceTimersByTimeAsync(wait - 1) + expect(getIdentity).toHaveBeenCalledTimes(index + 1) + await vi.advanceTimersByTimeAsync(1) + expect(getIdentity).toHaveBeenCalledTimes(index + 2) + } + expect(identity.state.value).toEqual({ status: 'rejected' }) + }) + + it('looks the identity up once and ignores later connected edges', async () => { + const getIdentity = vi.fn(async () => ({ userId: 'local-user' })) + const identity = harness(getIdentity) + await flush() + + identity.connect() + await flush() + + expect(getIdentity).toHaveBeenCalledOnce() + expect(identity.state.value).toEqual({ + status: 'resolved', + userId: 'local-user' + }) + }) + + it('restarts the backoff on reset', async () => { + const getIdentity = failing() + const identity = harness(getIdentity) + await vi.advanceTimersByTimeAsync(1_000 + 2_000) + expect(getIdentity).toHaveBeenCalledTimes(3) + + identity.reset() + await flush() + await vi.advanceTimersByTimeAsync(1_000) + + expect(getIdentity).toHaveBeenCalledTimes(5) + }) + + it('does not retry a failure that is not retryable', async () => { + const getIdentity = failing() + harness(getIdentity, () => false) + + await vi.advanceTimersByTimeAsync(30_000) + + expect(getIdentity).toHaveBeenCalledOnce() + }) + + it('settles a failure that is not retryable as rejected until reset', async () => { + const getIdentity = vi + .fn<() => Promise<{ userId: string }>>() + .mockRejectedValueOnce(new Error('unauthorized')) + .mockResolvedValue({ userId: 'account-b' }) + const identity = harness(getIdentity, () => false) + await flush() + expect(identity.state.value).toEqual({ status: 'rejected' }) + + identity.reset() + expect(identity.state.value).toEqual({ status: 'pending' }) + await flush() + + expect(identity.state.value).toEqual({ + status: 'resolved', + userId: 'account-b' + }) + }) + + it('reports nothing for a lookup that fails after stop()', async () => { + let failLookup!: (error: Error) => void + const identity = harness( + () => + new Promise((_, reject) => { + failLookup = reject + }) + ) + + identity.stop() + failLookup(new Error('down')) + await flush() + + expect(identity.onFailure).not.toHaveBeenCalled() + }) + + it('starts no lookup after stop()', async () => { + const getIdentity = failing() + const identity = harness(getIdentity) + await flush() + + identity.stop() + identity.connect() + await vi.advanceTimersByTimeAsync(30_000) + + expect(getIdentity).toHaveBeenCalledOnce() + }) + it('settles as rejected when the identity route does not exist', async () => { + const getIdentity = vi.fn(async (): Promise<{ userId: string }> => { + throw new AgentApiError('not found', 404, null) + }) + const identity = harness(getIdentity, (error) => + isRetryableRequestFailure(error, false) + ) + await flush() + + expect(identity.state.value).toEqual({ status: 'rejected' }) + await vi.advanceTimersByTimeAsync(60_000) + expect(getIdentity).toHaveBeenCalledOnce() + }) + + it('settles as rejected once a transient failure outlasts the retry budget', async () => { + const getIdentity = failing() + const identity = harness(getIdentity) + + await vi.advanceTimersByTimeAsync(10 * 60_000) + + expect(identity.state.value).toEqual({ status: 'rejected' }) + expect(getIdentity).toHaveBeenCalledTimes(8) + expect(identity.onFailure).toHaveBeenCalledTimes(8) + }) + + it('looks the identity up again when the socket reopens after the budget ran out', async () => { + const getIdentity = vi + .fn<() => Promise<{ userId: string }>>() + .mockRejectedValue(new Error('agent still starting')) + const identity = harness(getIdentity) + await vi.advanceTimersByTimeAsync(10 * 60_000) + expect(identity.state.value).toEqual({ status: 'rejected' }) + + getIdentity.mockResolvedValue({ userId: 'local-user' }) + identity.connect() + await flush() + + expect(identity.state.value).toEqual({ + status: 'resolved', + userId: 'local-user' + }) + }) +}) diff --git a/src/workbench/extensions/agent/services/agent/agentIdentity.ts b/src/workbench/extensions/agent/services/agent/agentIdentity.ts new file mode 100644 index 00000000000..c9d8bb1bead --- /dev/null +++ b/src/workbench/extensions/agent/services/agent/agentIdentity.ts @@ -0,0 +1,121 @@ +import { readonly, ref } from 'vue' +import type { Ref } from 'vue' + +const RETRY_BASE_MS = 1_000 +const RETRY_MAX_MS = 30_000 +/** Lookups tried before a transient failure is treated as a rejection. */ +const MAX_ATTEMPTS = 8 + +interface AgentIdentityDeps { + getIdentity(): Promise<{ userId: string }> + onSocketOpen(listener: () => void): () => void + isRetryable(error: unknown): boolean + onFailure(error: unknown): void +} + +/** + * Outcome of the identity lookup. `rejected` holds until `reset()`, or until + * the socket reopens after the retry budget ran out. + */ +export type AgentIdentityState = + | { status: 'pending' } + | { status: 'resolved'; userId: string } + | { status: 'rejected' } + +interface ResolvedAgentIdentity { + reset(): void + state: Readonly> + stop(): void +} + +export function resolveAgentIdentity( + deps: AgentIdentityDeps +): ResolvedAgentIdentity { + const state = ref({ status: 'pending' }) + let inFlight = false + let stopped = false + let failures = 0 + let retryTimer: ReturnType | null = null + let generation = 0 + let exhausted = false + + function cancelRetry(): void { + if (retryTimer === null) return + clearTimeout(retryTimer) + retryTimer = null + } + + function retryDelay(): number { + return Math.min(RETRY_BASE_MS * 2 ** (failures - 1), RETRY_MAX_MS) + } + + function scheduleRetry(): void { + if (stopped) return + retryTimer = setTimeout(() => { + retryTimer = null + void attempt() + }, retryDelay()) + } + + function settleFailure(error: unknown): void { + failures += 1 + deps.onFailure(error) + if (!deps.isRetryable(error)) { + state.value = { status: 'rejected' } + return + } + if (failures >= MAX_ATTEMPTS) { + exhausted = true + state.value = { status: 'rejected' } + return + } + scheduleRetry() + } + + async function attempt(): Promise { + if (stopped || inFlight || state.value.status !== 'pending') return + cancelRetry() + inFlight = true + const started = generation + try { + const identity = await deps.getIdentity() + if (started === generation) + state.value = { status: 'resolved', userId: identity.userId } + } catch (error) { + if (started === generation) settleFailure(error) + } finally { + if (started === generation) inFlight = false + } + } + + function retryNow(): void { + if (exhausted) { + exhausted = false + failures = 0 + state.value = { status: 'pending' } + } else if (retryTimer === null) return + void attempt() + } + + const stopListening = deps.onSocketOpen(retryNow) + void attempt() + + return { + state: readonly(state), + reset() { + generation += 1 + state.value = { status: 'pending' } + failures = 0 + exhausted = false + inFlight = false + cancelRetry() + void attempt() + }, + stop() { + stopped = true + generation += 1 + stopListening() + cancelRetry() + } + } +} diff --git a/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts b/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts index b4d33cc5d86..974ec1a1d32 100644 --- a/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import { markErrorReported, @@ -8,10 +8,13 @@ import type { AuthCredential, AuthScheme } from '@/types/authTypes' import * as authRejection from '@/platform/auth/authRejection' import { api } from '@/scripts/api' +import { withAgentAuth } from './agentAuth' + import type { CloudWorkflowEntry } from '../../schemas/agentApiSchema' vi.mock(import('@/scripts/api')) vi.mock(import('@/platform/telemetry/reportError')) +vi.mock(import('./agentAuth')) import { AgentApiError, @@ -335,6 +338,25 @@ describe('agentRestClient route + method', () => { }) }) +describe('getIdentity', () => { + it('reads the identity the agent authenticated this client as', async () => { + respond( + jsonResponse(200, { workspace_id: 'w-local', user_id: 'local-user' }) + ) + + const identity = await makeClient().getIdentity() + + expect(identity).toEqual({ userId: 'local-user' }) + expect(lastCall().route).toBe('/agent/identity') + }) + + it('rejects a malformed identity rather than returning a partial one', async () => { + respond(jsonResponse(200, { workspace_id: 'w-local', user_id: '' })) + + await expect(makeClient().getIdentity()).rejects.toThrow() + }) +}) + // The one read that can tell a live version-less draft from a deleted // workflow: cloud's `GetByID` excludes soft-deleted rows but, unlike `List`, // not version-less ones. @@ -396,6 +418,20 @@ describe('postMessage wire body', () => { expect(contentType(init)).toBe('application/json') }) + it('sends current_tab_unbound when the turn comes from a tab with no workflow', async () => { + respond(jsonResponse(202, turnAccepted)) + await makeClient().postMessage('t1', { + content: 'add a node', + currentTabUnbound: true + }) + + expect(JSON.parse(String(lastCall().init.body))).toEqual({ + content: 'add a node', + ask_kinds: ['run_approval', 'ask_user', 'delete_approval'], + current_tab_unbound: true + }) + }) + it('omits absent optionals rather than sending them as undefined keys', async () => { respond(jsonResponse(202, turnAccepted)) await makeClient().postMessage('t1', { content: 'just text' }) @@ -1241,3 +1277,61 @@ describe('Retry-After contract', () => { ) }) }) + +const emptyThreadPage = { + threads: [], + pagination: { offset: 0, limit: 1, total: 0, has_more: false } +} + +describe('user auth on agent requests', () => { + beforeEach(() => { + vi.mocked(withAgentAuth).mockImplementation(async (init) => { + const headers = new Headers(init.headers) + headers.set('Authorization', 'Bearer id-token') + return { ...init, headers } + }) + }) + + const calls: [ + string, + (client: AgentRestClient) => Promise, + unknown + ][] = [ + [ + 'POST messages', + (c) => c.postMessage('new', { content: 'hi' }), + turnAccepted + ], + ['GET messages', (c) => c.getMessages('t1'), []], + [ + 'POST answer', + (c) => c.answerAsk('t1', 'ask-1', { selected: ['allow'] }), + { status: 'answered' } + ], + ['the credential refresh', (c) => c.refreshCredential(), emptyThreadPage] + ] + + it.for(calls)( + 'sends the user auth header with %s', + async ([, call, body]) => { + respond(jsonResponse(200, body)) + + await call(makeClient()) + + expect(new Headers(lastCall().init.headers).get('Authorization')).toBe( + 'Bearer id-token' + ) + } + ) + + it('sends no auth header for a signed-out user', async () => { + vi.mocked(withAgentAuth).mockImplementation(async (init) => init) + respond(jsonResponse(200, [])) + + await makeClient().getMessages('t1') + + expect(new Headers(lastCall().init.headers).has('Authorization')).toBe( + false + ) + }) +}) diff --git a/src/workbench/extensions/agent/services/agent/agentRestClient.ts b/src/workbench/extensions/agent/services/agent/agentRestClient.ts index 8ba8b8a80f1..0af2af4e538 100644 --- a/src/workbench/extensions/agent/services/agent/agentRestClient.ts +++ b/src/workbench/extensions/agent/services/agent/agentRestClient.ts @@ -8,7 +8,7 @@ import { zUploadImageResponse, zWorkflowResponse } from '@comfyorg/ingest-types/zod' -import type { z } from 'zod' +import { z } from 'zod' import type { AuthRejectionStatus, @@ -26,12 +26,14 @@ import { api } from '@/scripts/api' import type { AuthCredential, AuthScheme } from '@/types/authTypes' import { refusedAttachmentsMessage } from '../../utils/attachmentMessages' +import { withAgentAuth } from './agentAuth' import { RENDERED_ASK_KINDS, zAgentAnswerReceipt, zAgentCancelAccepted, zAgentError, + zAgentIdentity, zAgentMessages, zAgentRunMode, zAgentThreads, @@ -80,6 +82,7 @@ const ANSWER_ASK_TIMEOUT_MS = 15_000 type AgentApiOperation = | 'answer_thread_ask' | 'cancel_thread_message' + | 'get_agent_identity' | 'get_cloud_workflow' | 'get_run_mode' | 'get_thread_messages' @@ -87,6 +90,7 @@ type AgentApiOperation = | 'list_threads' | 'post_thread_message' | 'put_run_mode' + | 'refresh_credential' | 'upload_image' type ReportedAuthScheme = AuthScheme | 'unreported' @@ -532,15 +536,18 @@ export function createAgentRestClient() { ): Promise { let authScheme: ReportedAuthScheme = 'unreported' let credential: ReportedAuthCredential = 'unreported' - const response = await api.fetchApi(route, { - ...init, - onAuthScheme: (scheme) => { - authScheme = scheme - }, - onAuthCredential: (kind) => { - credential = kind - } - }) + const response = await api.fetchApi( + route, + await withAgentAuth({ + ...init, + onAuthScheme: (scheme) => { + authScheme = scheme + }, + onAuthCredential: (kind) => { + credential = kind + } + }) + ) if (!response.ok) { throw await toApiError(response, operation, authScheme, credential) } @@ -609,6 +616,16 @@ export function createAgentRestClient() { return page.threads } + async function getIdentity(): Promise<{ userId: string }> { + const identity = await request( + 'get_agent_identity', + '/agent/identity', + { method: 'GET' }, + zAgentIdentity + ) + return { userId: identity.user_id } + } + async function getRunMode(): Promise { return request( 'get_run_mode', @@ -690,6 +707,15 @@ export function createAgentRestClient() { ) } + async function refreshCredential(): Promise { + await request( + 'refresh_credential', + '/agent/threads?limit=1', + { method: 'GET' }, + z.unknown() + ) + } + async function uploadImage( image: Blob, filename: string, @@ -714,12 +740,14 @@ export function createAgentRestClient() { postMessage, getMessages, listThreads, + getIdentity, getRunMode, putRunMode, listCloudWorkflows, getCloudWorkflow, cancelMessage, answerAsk, + refreshCredential, uploadImage } } diff --git a/src/workbench/extensions/agent/services/agent/standaloneAgentEventSource.test.ts b/src/workbench/extensions/agent/services/agent/standaloneAgentEventSource.test.ts deleted file mode 100644 index 4716328a829..00000000000 --- a/src/workbench/extensions/agent/services/agent/standaloneAgentEventSource.test.ts +++ /dev/null @@ -1,196 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' - -import { createStandaloneAgentEventSource } from './standaloneAgentEventSource' - -class FakeSocket extends EventTarget { - readyState: number = WebSocket.CONNECTING - readonly url: string - - constructor(url: string) { - super() - this.url = url - } - - open(): void { - this.readyState = WebSocket.OPEN - this.dispatchEvent(new Event('open')) - } - - receive(frame: unknown): void { - this.dispatchEvent( - new MessageEvent('message', { data: JSON.stringify(frame) }) - ) - } - - receiveRaw(data: string): void { - this.dispatchEvent(new MessageEvent('message', { data })) - } - - error(): void { - this.dispatchEvent(new Event('error')) - } - - close(): void { - if (this.readyState === WebSocket.CLOSED) return - this.readyState = WebSocket.CLOSED - this.dispatchEvent(new Event('close')) - } -} - -function sourceHarness(endpoint = '/api/agent/events') { - const sockets: FakeSocket[] = [] - const source = createStandaloneAgentEventSource({ - endpoint, - createSocket(url) { - const socket = new FakeSocket(url) - sockets.push(socket) - return socket as unknown as WebSocket - } - }) - return { source, sockets } -} - -describe('createStandaloneAgentEventSource', () => { - it.for([ - ['http://agent.test/api/agent/events', 'ws:'], - ['https://agent.test/api/agent/events', 'wss:'] - ])('transforms %s into a %s socket endpoint', ([endpoint, protocol]) => { - const { source, sockets } = sourceHarness(endpoint) - - source.subscribe(vi.fn()) - - const socketUrl = new URL(sockets[0].url) - expect(socketUrl.protocol).toBe(protocol) - expect(socketUrl.host).toBe('agent.test') - expect(socketUrl.pathname).toBe('/api/agent/events') - }) - - it('delivers JSON frames from the same-origin agent proxy', () => { - const { source, sockets } = sourceHarness() - const seen = vi.fn() - const status = vi.fn() - - source.subscribe(seen) - source.onStatus?.(status) - sockets[0].open() - sockets[0].receive({ type: 'agent_message_delta', data: { delta: 'hi' } }) - - expect(status).toHaveBeenCalledWith(true) - expect(seen).toHaveBeenCalledWith({ - type: 'agent_message_delta', - data: { delta: 'hi' } - }) - }) - - it('forwards an empty text frame instead of dropping it', () => { - const { source, sockets } = sourceHarness() - const seen = vi.fn() - - source.subscribe(seen) - sockets[0].open() - sockets[0].receiveRaw('') - - expect(seen).toHaveBeenCalledWith('') - }) - - it('reconnects after a backend reload', () => { - vi.useFakeTimers() - const { source, sockets } = sourceHarness() - const status = vi.fn() - - source.subscribe(vi.fn()) - source.onStatus?.(status) - sockets[0].open() - sockets[0].close() - vi.advanceTimersByTime(1000) - - expect(status).toHaveBeenLastCalledWith(false) - expect(sockets).toHaveLength(2) - }) - - it('reconnects exactly once when the upgrade fails before open', () => { - vi.useFakeTimers() - const { source, sockets } = sourceHarness() - const status = vi.fn() - - source.subscribe(vi.fn()) - source.onStatus?.(status) - sockets[0].error() - vi.advanceTimersByTime(999) - - expect(sockets[0].readyState).toBe(WebSocket.CLOSED) - expect(status).toHaveBeenCalledTimes(1) - expect(status).toHaveBeenLastCalledWith(false) - expect(sockets).toHaveLength(1) - - vi.advanceTimersByTime(1) - expect(sockets).toHaveLength(2) - - vi.advanceTimersByTime(5000) - expect(sockets).toHaveLength(2) - }) - - it('reports live again once the replacement socket opens', () => { - vi.useFakeTimers() - const { source, sockets } = sourceHarness() - const status = vi.fn() - - source.subscribe(vi.fn()) - source.onStatus?.(status) - sockets[0].open() - sockets[0].close() - vi.advanceTimersByTime(1000) - sockets[1].open() - - expect(status.mock.calls.map(([live]) => live)).toEqual([true, false, true]) - }) - - it('drops frames from a superseded socket after resubscribing', () => { - vi.useFakeTimers() - const { source, sockets } = sourceHarness() - const first = vi.fn() - const second = vi.fn() - - const unsubscribe = source.subscribe(first) - sockets[0].open() - unsubscribe() - source.subscribe(second) - sockets[1].open() - sockets[0].receive({ type: 'agent_message_delta', data: { delta: 'old' } }) - sockets[1].receive({ type: 'agent_message_delta', data: { delta: 'new' } }) - - expect(sockets).toHaveLength(2) - expect(first).not.toHaveBeenCalled() - expect(second).toHaveBeenCalledTimes(1) - expect(second).toHaveBeenCalledWith({ - type: 'agent_message_delta', - data: { delta: 'new' } - }) - }) - - it('stops the socket and pending reconnect on unsubscribe', () => { - vi.useFakeTimers() - const { source, sockets } = sourceHarness() - - const unsubscribe = source.subscribe(vi.fn()) - sockets[0].open() - sockets[0].close() - unsubscribe() - vi.advanceTimersByTime(1000) - - expect(sockets).toHaveLength(1) - }) - - it('reports the stream down when the last subscriber leaves', () => { - const { source, sockets } = sourceHarness() - const status = vi.fn() - source.onStatus?.(status) - - const unsubscribe = source.subscribe(vi.fn()) - sockets[0].open() - unsubscribe() - - expect(status.mock.calls).toEqual([[true], [false]]) - expect(sockets[0].readyState).toBe(WebSocket.CLOSED) - }) -}) diff --git a/src/workbench/extensions/agent/services/agent/standaloneAgentEventSource.ts b/src/workbench/extensions/agent/services/agent/standaloneAgentEventSource.ts deleted file mode 100644 index b321fee3511..00000000000 --- a/src/workbench/extensions/agent/services/agent/standaloneAgentEventSource.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { useTimeoutFn } from '@vueuse/core' - -import type { AgentEventSource } from '../../composables/agent/useAgentSession' - -interface StandaloneAgentEventSourceOptions { - createSocket?: (url: string) => WebSocket - endpoint?: string - reconnectDelayMs?: number -} - -export function createStandaloneAgentEventSource({ - createSocket = (url) => new WebSocket(url), - endpoint = '/api/agent/events', - reconnectDelayMs = 1000 -}: StandaloneAgentEventSourceOptions = {}): AgentEventSource { - const listeners = new Set<(raw: unknown) => void>() - const statusListeners = new Set<(live: boolean) => void>() - let socket: WebSocket | null = null - const reconnect = useTimeoutFn(connect, reconnectDelayMs, { - immediate: false - }) - - function eventUrl(): string { - const url = new URL(endpoint, window.location.href) - url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:' - return url.toString() - } - - function notifyStatus(live: boolean): void { - statusListeners.forEach((listener) => listener(live)) - } - - function scheduleReconnect(): void { - if (listeners.size === 0 || reconnect.isPending.value) return - reconnect.start() - } - - function connect(): void { - if (listeners.size === 0 || socket !== null) return - const current = createSocket(eventUrl()) - socket = current - current.addEventListener('open', () => { - if (socket === current) notifyStatus(true) - }) - current.addEventListener('message', (event) => { - if (socket !== current) return - let frame: unknown = event.data - if (typeof event.data === 'string') { - try { - frame = JSON.parse(event.data) - } catch { - frame = event.data - } - } - listeners.forEach((listener) => listener(frame)) - }) - current.addEventListener('error', () => { - if (socket === current) current.close() - }) - current.addEventListener('close', () => { - if (socket !== current) return - socket = null - notifyStatus(false) - scheduleReconnect() - }) - } - - function disconnect(): void { - reconnect.stop() - const current = socket - socket = null - if (current === null) return - current.close() - notifyStatus(false) - } - - return { - subscribe(listener) { - listeners.add(listener) - connect() - return () => { - listeners.delete(listener) - if (listeners.size === 0) disconnect() - } - }, - onStatus(listener) { - statusListeners.add(listener) - if (socket?.readyState === WebSocket.OPEN) listener(true) - return () => statusListeners.delete(listener) - } - } -} diff --git a/src/workbench/extensions/agent/stores/agent/agentConsentStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConsentStore.test.ts index 6e1f4420fa0..f7ecd2c6105 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConsentStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConsentStore.test.ts @@ -1,6 +1,5 @@ vi.mock(import('firebase/auth')) import type { GlobalSetting } from '@comfyorg/ingest-types' -import { useAuthStore } from '@/stores/authStore' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' import { computed, ref } from 'vue' @@ -16,11 +15,13 @@ import { useAgentConsentStore } from './agentConsentStore' const accountApi = vi.hoisted(() => ({ get: vi.fn(), - set: vi.fn() + set: vi.fn(), + header: vi.fn() })) vi.mock(import('@/platform/settings/globalSettingsApi'), () => ({ getGlobalSetting: accountApi.get, - setGlobalSetting: accountApi.set + setGlobalSetting: accountApi.set, + getGlobalSettingsAuthHeader: accountApi.header })) vi.mock(import('@/composables/auth/useCurrentUser')) @@ -47,8 +48,7 @@ describe('agentConsentStore', () => { useTeamWorkspaceStore().isSwitching = false Object.assign(useTeamWorkspaceStore(), { workspaceTransitionGeneration: 0 }) vi.mocked(useTeamWorkspaceStore().initialize).mockResolvedValue(undefined) - vi.mocked(useAuthStore().getWorkspaceAuthHeader).mockReset() - vi.mocked(useAuthStore().getWorkspaceAuthHeader).mockResolvedValue({ + accountApi.header.mockResolvedValue({ Authorization: 'Bearer account-a-token' }) accountApi.get.mockReset() @@ -72,7 +72,7 @@ describe('agentConsentStore', () => { }) it('settles a failed check so the entry can offer a retry', async () => { - vi.mocked(useAuthStore().getWorkspaceAuthHeader).mockResolvedValueOnce(null) + accountApi.header.mockResolvedValueOnce(null) const store = useAgentConsentStore() await expect(store.load()).rejects.toThrow('authentication is required') expect(store).toMatchObject({ isChecking: false }) @@ -133,7 +133,7 @@ describe('agentConsentStore', () => { await store.load() await store.accept() - expect(useAuthStore().getWorkspaceAuthHeader).not.toHaveBeenCalled() + expect(accountApi.header).not.toHaveBeenCalled() expect(accountApi.get).toHaveBeenCalledWith( 'Comfy.AgentPanel.ConsentAccepted', expect.any(Function) @@ -291,14 +291,10 @@ describe('agentConsentStore', () => { it('does not write when workspace scope changes while acquiring a token', async () => { const token = deferred<{ Authorization: `Bearer ${string}` }>() - vi.mocked(useAuthStore().getWorkspaceAuthHeader).mockReturnValueOnce( - token.promise - ) + accountApi.header.mockReturnValueOnce(token.promise) const store = useAgentConsentStore() const request = store.accept() - await vi.waitFor(() => - expect(useAuthStore().getWorkspaceAuthHeader).toHaveBeenCalledOnce() - ) + await vi.waitFor(() => expect(accountApi.header).toHaveBeenCalledOnce()) Object.assign(useTeamWorkspaceStore(), { activeWorkspaceId: 'workspace-b' }) token.resolve({ Authorization: 'Bearer workspace-b-token' }) await expect(request).resolves.toBe(false) diff --git a/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts b/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts index 96f85ada225..ef01a2a970c 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts @@ -9,17 +9,16 @@ import { import { AGENT_CONSENT_SETTING_ID } from '@/platform/settings/constants/agent' import { getGlobalSetting, + getGlobalSettingsAuthHeader, setGlobalSetting } from '@/platform/settings/globalSettingsApi' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' -import { useAuthStore } from '@/stores/authStore' class AgentConsentAuthenticationError extends Error { override name = 'AgentConsentAuthenticationError' } export const useAgentConsentStore = defineStore('agentConsent', () => { - const authStore = useAuthStore() const workspaceStore = useTeamWorkspaceStore() const { resolvedUserInfo } = useCurrentUser() const loadedIdentity = ref(null) @@ -95,7 +94,7 @@ export const useAgentConsentStore = defineStore('agentConsent', () => { async function requireAuth(sessionId: number, identity: string) { const send = webSessionRequests() ? await webSessionSend() : undefined - const auth = send ?? (await authStore.getWorkspaceAuthHeader()) + const auth = send ?? (await getGlobalSettingsAuthHeader()) if (!stillOwns(sessionId, identity)) return null if (!auth) { throw new AgentConsentAuthenticationError( diff --git a/src/workbench/extensions/agent/stores/agent/agentRunModeStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentRunModeStore.test.ts index d2a66a40b5e..fd4fbb2d885 100644 --- a/src/workbench/extensions/agent/stores/agent/agentRunModeStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentRunModeStore.test.ts @@ -5,6 +5,7 @@ import { api } from '@/scripts/api' import { createMockLoadedWorkflow } from '@/utils/__tests__/litegraphTestUtils' vi.mock(import('@/scripts/api')) +vi.mock(import('../../services/agent/agentAuth')) import { useAgentComposerStore } from './agentComposerStore' import { useAgentRunModeStore } from './agentRunModeStore' @@ -167,6 +168,7 @@ describe('agentRunModeStore', () => { const first = store.save('auto_limited', 20) const second = store.save('auto', null) + await vi.waitFor(() => expect(api.fetchApi).toHaveBeenCalledTimes(2)) resolveSecond(jsonResponse(200, { mode: 'auto', credit_limit: null })) await second resolveFirst(jsonResponse(200, { mode: 'auto_limited', credit_limit: 20 })) @@ -196,6 +198,7 @@ describe('agentRunModeStore', () => { const first = store.save('auto_limited', 20) const second = store.save('auto', null) + await vi.waitFor(() => expect(api.fetchApi).toHaveBeenCalledTimes(2)) resolveSecond(jsonResponse(500, { error: 'boom' })) await expect(second).rejects.toThrow() diff --git a/vite.config.mts b/vite.config.mts index e21279ecf5b..2cdbd2bd58d 100644 --- a/vite.config.mts +++ b/vite.config.mts @@ -18,7 +18,10 @@ import type { ProxyOptions } from 'vite' import { createHtmlPlugin } from 'vite-plugin-html' import vueDevTools from 'vite-plugin-vue-devtools' -import { createDevAgentConfig } from './build/devAgentConfig.ts' +import { + createDevAgentConfig, + devServerAgentRoutes +} from './build/devAgentConfig.ts' import { comfyAPIPlugin } from './build/plugins/comfyAPIPlugin.ts' import { hasCompleteSentryUploadConfig, @@ -341,7 +344,10 @@ export default defineConfig({ } : {}), - ...(devAgentConfig.proxy ? { '/api/agent': devAgentConfig.proxy } : {}), + ...devServerAgentRoutes(devAgentConfig.proxy, { + target: DEV_SERVER_COMFYUI_URL, + ...cloudProxyConfig + }), '/api': { target: DEV_SERVER_COMFYUI_URL, @@ -798,7 +804,10 @@ export default defineConfig({ __ALGOLIA_API_KEY__: JSON.stringify(process.env.ALGOLIA_API_KEY || ''), __USE_PROD_CONFIG__: process.env.USE_PROD_CONFIG === 'true', __DISTRIBUTION__: JSON.stringify(DISTRIBUTION), - __IS_NIGHTLY__: JSON.stringify(IS_NIGHTLY) + __IS_NIGHTLY__: JSON.stringify(IS_NIGHTLY), + 'import.meta.env.VITE_DEV_AGENT_PROXY_CREDENTIAL': JSON.stringify( + devAgentConfig.proxySuppliesCredential ? 'true' : '' + ) }, resolve: {