From 7726524b6a9eedfce76d2598b1c7dc685187d4af Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 22 Oct 2026 06:41:42 +0000 Subject: [PATCH] fix(agent): refresh node definitions when ComfyUI reconnects after an agent restart (#21644) Nodes the local agent installs show as missing after it restarts ComfyUI. Since comfy-agent 0.1.0-rc2 ([cloud#14617](https://github.com/Comfy-Org/cloud/pull/14617)) a successful `restart_comfyui` only schedules the restart: the agent reports `success` while the old ComfyUI is still serving and restarts it after the turn ends. The refresh store from #20903 fetched `/object_info` on that frame, read the old catalog, cleared its stale flag, and ignored the reconnect that followed. The store now marks definitions stale on the `success` frame and refreshes on the next `/ws` `reconnected`. A failed refresh still retries on the following reconnect. Restarts the panel never sees (panel closed, user or Desktop restarts) stay out of scope, as in FE-3483. Refs BE-21866 (the regression is recorded in its 2026-10-22 comment). - **Reproduced on the shipping stack, 2 of 2 runs:** published `comfy-agent[go-nonfree]==0.1.0rc2` on Linux x86_64, ComfyUI master `46aad33e` with `--enable-agent --enable-manager`, frontend `v1.59.0`. Each time the agent wrote and installed a one-node pack, I picked Restart ComfyUI on the card, and the agent then placed the node. The page fetched `/object_info` about 1 s after the card answer, before the restart (no new class in it). It fetched nothing after `reconnected`. The placed node rendered as a red missing type with no widgets. Refresh Node Definitions registered the class, but the placed node stayed broken until a page reload. - **Same flow on this branch** (production build of this head, served with `--front-end-root`): nothing is fetched at the card answer. `/object_info` is fetched about 1 s after `reconnected` and contains the new class. The agent-placed node renders with its widget and no error. - **Browser test:** `agentRestartNodeRefresh.spec.ts` now delivers the restart while the old definitions are served, then drops and reopens `/ws` with the new node defined. On the parent it fails: `hasErrors: true, hasStepsWidget: false`. With this change it passes. - **Unit:** the store tests now reconnect after a restart. The first case, which checks that nothing refreshes until ComfyUI reconnects, fails on the parent. - typecheck, typecheck:browser, oxlint, oxfmt, plus the pre-commit and pre-push hooks pass. (cherry picked from commit 3821a0aedbfc463f746e4c4c4ff48ed56ec347ab) --- .../assets/agent/restart_node_refresh.json | 70 ++++++++ .../agent/agentRestartNodeRefresh.spec.ts | 150 ++++++++++++++++++ .../extensions/agent/AgentPanelRoot.test.ts | 7 +- src/workbench/extensions/agent/README.md | 17 +- .../agentNodeDefinitionRefreshStore.test.ts | 62 +++++++- .../agent/agentNodeDefinitionRefreshStore.ts | 51 ++++-- 6 files changed, 330 insertions(+), 27 deletions(-) create mode 100644 browser_tests/assets/agent/restart_node_refresh.json create mode 100644 browser_tests/tests/agent/agentRestartNodeRefresh.spec.ts diff --git a/browser_tests/assets/agent/restart_node_refresh.json b/browser_tests/assets/agent/restart_node_refresh.json new file mode 100644 index 00000000000..fa1254b6cca --- /dev/null +++ b/browser_tests/assets/agent/restart_node_refresh.json @@ -0,0 +1,70 @@ +{ + "last_node_id": 1, + "last_link_id": 0, + "nodes": [ + { + "id": 1, + "type": "BrowserProofNode", + "pos": [0, 30], + "size": { + "0": 315, + "1": 262 + }, + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [ + { + "name": "model", + "type": "MODEL", + "link": null + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": null + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": null + }, + { + "name": "latent_image", + "type": "LATENT", + "link": null + } + ], + "outputs": [ + { + "name": "LATENT", + "type": "LATENT", + "links": [], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "BrowserProofNode" + }, + "widgets_values": [ + 156680208700286, + "randomize", + 20, + 8, + "euler", + "normal", + 1 + ] + } + ], + "links": [], + "groups": [], + "config": {}, + "extra": { + "ds": { + "scale": 1, + "offset": [0, 0] + } + }, + "version": 0.4 +} diff --git a/browser_tests/tests/agent/agentRestartNodeRefresh.spec.ts b/browser_tests/tests/agent/agentRestartNodeRefresh.spec.ts new file mode 100644 index 00000000000..1da1e23ed11 --- /dev/null +++ b/browser_tests/tests/agent/agentRestartNodeRefresh.spec.ts @@ -0,0 +1,150 @@ +import { expect, mergeTests } from '@playwright/test' + +import { toNodeId } from '@/types/nodeId' +import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' + +import { webSocketFixture } from '@e2e/fixtures/ws' +import { agentReplayNodeDefs } from '@e2e/fixtures/data/agentReplayNodeDefs' +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' +import { agentTest, pushAgentEvent } from '@e2e/tests/agent/agentPanelMocks' + +const test = mergeTests(agentTest, webSocketFixture) +const browserProofNodeId = toNodeId(1) + +test.describe( + 'Agent restart node refresh', + { tag: ['@cloud', '@agent', '@canvas', '@node'] }, + () => { + test.use({ connectWebSocketToServer: false, objectInfo: 'server' }) + + let restarted = false + + test.beforeEach(async ({ comfyPage }) => { + const page = comfyPage.page + restarted = false + await page.route('**/object_info', async (route) => { + await route.fulfill( + jsonRoute( + restarted + ? { + ...agentReplayNodeDefs, + BrowserProofNode: { + ...agentReplayNodeDefs.KSampler, + name: 'BrowserProofNode', + display_name: 'Browser Proof Node' + } + } + : agentReplayNodeDefs + ) + ) + }) + + await comfyPage.workflow.loadWorkflow('agent/restart_node_refresh') + await expect + .poll(() => + page.evaluate((nodeId) => { + const node = window.app!.graph.getNodeById(nodeId) + return { + type: node?.type, + hasErrors: node?.has_errors === true, + hasStepsWidget: node?.widgets?.some( + (widget) => widget.name === 'steps' + ) + } + }, browserProofNodeId) + ) + .toEqual({ + type: 'BrowserProofNode', + hasErrors: true, + hasStepsWidget: false + }) + }) + + // The restart report and ComfyUI's reconnect travel on different sockets, + // so the page must recover in either order. + for (const reportFirst of [true, false]) { + test(`refreshes node definitions and reloads the active workflow when ${ + reportFirst ? 'the restart report' : 'the ComfyUI reconnect' + } arrives first`, async ({ + agentPanel, + comfyPage, + getAgentSocket, + getWebSocket, + nextWebSocket + }) => { + test.setTimeout(60_000) + const page = comfyPage.page + + await agentPanel.open() + await agentPanel.selectWorkflow('restart_node_refresh') + + const deliverRestartReport = () => + test.step('deliver the agent restart report', async () => { + const ws = await getAgentSocket() + const restart: AgentWsEvent = { + type: 'agent_tool_call', + data: { + tool_call_id: 'call-restart-browser-proof', + tool_name: 'restart_comfyui', + status: 'success', + message_id: 'message-restart-browser-proof', + thread_id: 'thread-restart-browser-proof' + } + } + pushAgentEvent(ws, restart) + }) + + const restartComfy = () => + test.step('restart ComfyUI with the new node loaded', async () => { + const comfySocket = await getWebSocket() + const pageReconnected = await page.evaluateHandle(() => ({ + done: new Promise((resolve) => + window.app!.api.addEventListener( + 'reconnected', + () => resolve(), + { once: true } + ) + ) + })) + const reconnected = nextWebSocket() + restarted = true + await comfySocket.close() + await reconnected + await pageReconnected.evaluate(({ done }) => done) + }) + + if (reportFirst) { + await deliverRestartReport() + await restartComfy() + } else { + await restartComfy() + await deliverRestartReport() + } + + await test.step('verify the active workflow was reconstructed', async () => { + await expect + .poll(() => + page.evaluate((nodeId) => { + const node = window.app!.graph.getNodeById(nodeId) + return { + type: node?.type, + hasErrors: node?.has_errors === true, + hasStepsWidget: node?.widgets?.some( + (widget) => widget.name === 'steps' + ), + seed: node?.widgets?.find((widget) => widget.name === 'seed') + ?.value + } + }, browserProofNodeId) + ) + .toEqual({ + type: 'BrowserProofNode', + hasErrors: false, + hasStepsWidget: true, + seed: 156680208700286 + }) + }) + }) + } + } +) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index bae935c40a0..628e597a4ef 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -6156,7 +6156,7 @@ describe('AgentPanelRoot node definition refresh', () => { vi.useFakeTimers() }) - it('refreshes node definitions when the agent restarts ComfyUI while the panel is open', async () => { + it('refreshes node definitions when ComfyUI reconnects after the agent restarts it while the panel is open', async () => { const refreshNodeDefinitions = vi.fn() useCommandStore().registerCommand({ id: 'Comfy.RefreshNodeDefinitions', @@ -6166,6 +6166,10 @@ describe('AgentPanelRoot node definition refresh', () => { ws.emit('agent_tool_call', restartSucceeded) await vi.advanceTimersByTimeAsync(0) + expect(refreshNodeDefinitions).not.toHaveBeenCalled() + + ws.emit('reconnected') + await vi.advanceTimersByTimeAsync(0) expect(refreshNodeDefinitions).toHaveBeenCalledOnce() }) @@ -6180,6 +6184,7 @@ describe('AgentPanelRoot node definition refresh', () => { unmount() ws.emit('agent_tool_call', restartSucceeded) + ws.emit('reconnected') await vi.advanceTimersByTimeAsync(0) expect(refreshNodeDefinitions).not.toHaveBeenCalled() diff --git a/src/workbench/extensions/agent/README.md b/src/workbench/extensions/agent/README.md index aa3322917ef..cff84900ce9 100644 --- a/src/workbench/extensions/agent/README.md +++ b/src/workbench/extensions/agent/README.md @@ -24,12 +24,17 @@ The panel speaks one contract on the cloud and on the local agent: socket connects again. A rejected credential is not retried until the account changes. - **ComfyUI restart** (`stores/agent/agentNodeDefinitionRefreshStore.ts`). A - successful `restart_comfyui` tool call refreshes node definitions and then - reloads the open workflow; the refresh shows its own "Update requested" and - "Node definitions updated" toasts. A failed refresh runs again only when - ComfyUI's `/ws` reconnects later; until then definitions stay stale, and - **Refresh Node Definitions** recovers them. A restart made while the panel is - closed is not observed. + successful `restart_comfyui` tool call means ComfyUI restarts once the turn + ends. Every `/ws` reconnect within 10 minutes of that call, before or after + it, refreshes node definitions and then reloads the open workflow, because + the call and the reconnect arrive on different sockets in either order. An + unrelated reconnect in that window refreshes too and leaves the restart's + own reconnect to refresh again. The refresh shows its own "Update requested" + and "Node definitions updated" toasts. A failed refresh runs again on the + next reconnect in the window. A reconnect outside the window does nothing, so + if none comes within it the store does nothing, and **Refresh Node + Definitions** recovers definitions. A restart made while the panel is closed + is not observed. - **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 diff --git a/src/workbench/extensions/agent/stores/agent/agentNodeDefinitionRefreshStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentNodeDefinitionRefreshStore.test.ts index c01268ad081..ea74baae0e6 100644 --- a/src/workbench/extensions/agent/stores/agent/agentNodeDefinitionRefreshStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentNodeDefinitionRefreshStore.test.ts @@ -28,6 +28,7 @@ function toolCall( } const restartSucceeded = toolCall('restart_comfyui', 'success') +const RESTART_WINDOW_MS = 10 * 60_000 function registerRefreshCommand( refresh = vi.fn<() => Promise>(async () => {}) @@ -40,7 +41,7 @@ function registerRefreshCommand( } describe('agentNodeDefinitionRefreshStore', () => { - it('refreshes node definitions before reloading the open workflow after a restart succeeds', async () => { + it('waits for ComfyUI to reconnect after a restart, then refreshes node definitions before reloading the open workflow', async () => { const order: string[] = [] registerRefreshCommand( vi.fn(async () => { @@ -56,6 +57,10 @@ describe('agentNodeDefinitionRefreshStore', () => { useAgentNodeDefinitionRefreshStore().observeAgentEvent(restartSucceeded) await vi.advanceTimersByTimeAsync(100) + expect(order).toEqual([]) + + api.dispatchCustomEvent('reconnected') + await vi.advanceTimersByTimeAsync(100) expect(order).toEqual(['refresh', 'reload']) }) @@ -81,6 +86,7 @@ describe('agentNodeDefinitionRefreshStore', () => { const refresh = registerRefreshCommand() useAgentNodeDefinitionRefreshStore().observeAgentEvent(event) + api.dispatchCustomEvent('reconnected') await vi.advanceTimersByTimeAsync(0) expect(refresh).not.toHaveBeenCalled() @@ -101,6 +107,7 @@ describe('agentNodeDefinitionRefreshStore', () => { arrange() useAgentNodeDefinitionRefreshStore().observeAgentEvent(restartSucceeded) + api.dispatchCustomEvent('reconnected') await vi.advanceTimersByTimeAsync(0) expect(reportError).toHaveBeenCalledExactlyOnceWith( @@ -121,6 +128,7 @@ describe('agentNodeDefinitionRefreshStore', () => { .mockResolvedValue(undefined) ) useAgentNodeDefinitionRefreshStore().observeAgentEvent(restartSucceeded) + api.dispatchCustomEvent('reconnected') await vi.advanceTimersByTimeAsync(0) api.dispatchCustomEvent('reconnected') @@ -140,18 +148,57 @@ describe('agentNodeDefinitionRefreshStore', () => { expect(refresh).not.toHaveBeenCalled() }) - it('does not refresh on a ComfyUI reconnect once the last restart refreshed', async () => { + it('refreshes when ComfyUI reconnected before the restart report arrived on the agent socket', async () => { + const refresh = registerRefreshCommand() + const store = useAgentNodeDefinitionRefreshStore() + api.dispatchCustomEvent('reconnected') + await vi.advanceTimersByTimeAsync(2_000) + + store.observeAgentEvent(restartSucceeded) + await vi.advanceTimersByTimeAsync(0) + + expect(refresh).toHaveBeenCalledOnce() + expect(useWorkflowService().reloadCurrentWorkflow).toHaveBeenCalledOnce() + }) + + it('refreshes again on the restart reconnect after an unrelated reconnect earlier in the turn', async () => { const refresh = registerRefreshCommand() useAgentNodeDefinitionRefreshStore().observeAgentEvent(restartSucceeded) + api.dispatchCustomEvent('reconnected') + await vi.advanceTimersByTimeAsync(30_000) + + api.dispatchCustomEvent('reconnected') await vi.advanceTimersByTimeAsync(0) + expect(refresh).toHaveBeenCalledTimes(2) + }) + + it('refreshes on a reconnect 10 minutes after the restart', async () => { + const refresh = registerRefreshCommand() + useAgentNodeDefinitionRefreshStore().observeAgentEvent(restartSucceeded) + await vi.advanceTimersByTimeAsync(RESTART_WINDOW_MS) + api.dispatchCustomEvent('reconnected') await vi.advanceTimersByTimeAsync(0) expect(refresh).toHaveBeenCalledOnce() }) - it('runs one more refresh for restarts that arrive while a refresh is in flight', async () => { + it('ignores reconnects more than 10 minutes before or after the restart', async () => { + const refresh = registerRefreshCommand() + const store = useAgentNodeDefinitionRefreshStore() + api.dispatchCustomEvent('reconnected') + await vi.advanceTimersByTimeAsync(RESTART_WINDOW_MS + 1) + + store.observeAgentEvent(restartSucceeded) + await vi.advanceTimersByTimeAsync(RESTART_WINDOW_MS + 1) + api.dispatchCustomEvent('reconnected') + await vi.advanceTimersByTimeAsync(0) + + expect(refresh).not.toHaveBeenCalled() + }) + + it('runs one more refresh for reconnects that arrive while a refresh is in flight', async () => { let finishFirstRefresh: () => void = () => undefined const refresh = registerRefreshCommand( vi @@ -165,10 +212,11 @@ describe('agentNodeDefinitionRefreshStore', () => { ) const store = useAgentNodeDefinitionRefreshStore() store.observeAgentEvent(restartSucceeded) + api.dispatchCustomEvent('reconnected') await vi.advanceTimersByTimeAsync(0) - store.observeAgentEvent(restartSucceeded) - store.observeAgentEvent(restartSucceeded) + api.dispatchCustomEvent('reconnected') + api.dispatchCustomEvent('reconnected') finishFirstRefresh() await vi.advanceTimersByTimeAsync(0) @@ -182,9 +230,11 @@ describe('agentNodeDefinitionRefreshStore', () => { ) const store = useAgentNodeDefinitionRefreshStore() store.observeAgentEvent(restartSucceeded) + api.dispatchCustomEvent('reconnected') await vi.advanceTimersByTimeAsync(0) store.observeAgentEvent(restartSucceeded) + api.dispatchCustomEvent('reconnected') await vi.advanceTimersByTimeAsync(0) expect(refresh).toHaveBeenCalledTimes(2) @@ -198,9 +248,11 @@ describe('agentNodeDefinitionRefreshStore', () => { }) const store = useAgentNodeDefinitionRefreshStore() store.observeAgentEvent(restartSucceeded) + api.dispatchCustomEvent('reconnected') await vi.advanceTimersByTimeAsync(0) store.observeAgentEvent(restartSucceeded) + api.dispatchCustomEvent('reconnected') await vi.advanceTimersByTimeAsync(0) expect(refresh).toHaveBeenCalledTimes(2) diff --git a/src/workbench/extensions/agent/stores/agent/agentNodeDefinitionRefreshStore.ts b/src/workbench/extensions/agent/stores/agent/agentNodeDefinitionRefreshStore.ts index e6c7d110e52..e39ec2d46af 100644 --- a/src/workbench/extensions/agent/stores/agent/agentNodeDefinitionRefreshStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentNodeDefinitionRefreshStore.ts @@ -41,27 +41,39 @@ async function reloadWorkflowWithoutRejecting(): Promise { } /** - * Refreshes node definitions and the open workflow after the agent restarts - * ComfyUI; a failed refresh runs again on ComfyUI's next reconnect. + * How far apart a successful restart and a ComfyUI `/ws` reconnect may be for + * the reconnect to count as that restart. The turn can run on after the tool + * reports success, and the agent then allows ComfyUI five minutes to go down + * and come back. + */ +const RESTART_WINDOW_MS = 10 * 60_000 + +/** + * Refreshes node definitions and the open workflow when ComfyUI reconnects + * after the agent restarts it. The agent reports the restart before ComfyUI + * goes down, but the report and the reconnect arrive on different sockets, so + * either may come first: every reconnect within the window refreshes, + * including one that came just before the report. An unrelated reconnect in + * the window refreshes too, which is harmless, and leaves the restart's own + * reconnect to refresh again. */ export const useAgentNodeDefinitionRefreshStore = defineStore( 'agentNodeDefinitionRefresh', () => { - let stale = false + let restartedAt = -Infinity + let reconnectedAt = -Infinity + let refreshQueued = false let queue = Promise.resolve() - async function refreshIfStale(): Promise { - if (!stale) return - stale = false - if (!(await refreshNodeDefinitions())) { - stale = true - return - } - await reloadWorkflowWithoutRejecting() + async function refresh(): Promise { + refreshQueued = false + if (await refreshNodeDefinitions()) await reloadWorkflowWithoutRejecting() } function enqueueRefresh(): void { - queue = queue.then(refreshIfStale).catch((error: unknown) => { + if (refreshQueued) return + refreshQueued = true + queue = queue.then(refresh).catch((error: unknown) => { reportError(error, { surface: 'agent', errorType: 'failure_running_agent_node_definition_refresh' @@ -69,13 +81,22 @@ export const useAgentNodeDefinitionRefreshStore = defineStore( }) } + function refreshIfRestartNearby(): void { + if (Math.abs(reconnectedAt - restartedAt) <= RESTART_WINDOW_MS) { + enqueueRefresh() + } + } + function observeAgentEvent(event: AgentWsEvent): void { if (!isSuccessfulRestart(event)) return - stale = true - enqueueRefresh() + restartedAt = Date.now() + refreshIfRestartNearby() } - useEventListener(api, 'reconnected', enqueueRefresh) + useEventListener(api, 'reconnected', () => { + reconnectedAt = Date.now() + refreshIfRestartNearby() + }) return { observeAgentEvent } }