From f345078ec8de0e344aef9bf8cfd4049b59645fd1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 4 Oct 2026 05:16:42 +0000 Subject: [PATCH 1/3] fix(agent): release minimap graph-activity layer reliably on unmount The agent.graph-activity minimap layer was disposed as the last statement of a long onBeforeUnmount hook, so any earlier teardown step throwing left the id registered and the next mount reported a duplicate layer. Dispose it in its own hook registered next to the registration, and add a regression test. --- .../extensions/agent/AgentPanelRoot.test.ts | 26 +++++++++++++++++++ .../extensions/agent/AgentPanelRoot.vue | 5 +++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 46a2ff8be0e..574cc847884 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -4488,6 +4488,32 @@ describe('AgentPanelRoot lifecycle', () => { expect(urls.some((url) => url.endsWith('/cancel'))).toBe(false) }) + it('releases the minimap graph-activity layer even when another teardown step throws', () => { + vi.spyOn( + useWorkflowTabActivityStore(), + 'setCreating' + ).mockImplementationOnce(() => { + throw new Error('teardown failed') + }) + // Mirror production, where Vue reports a failing hook and carries on. + const errorHandler = vi.fn() + + const first = render(AgentPanelRoot, { + global: { plugins: [i18n], config: { errorHandler } } + }) + first.unmount() + expect(errorHandler).toHaveBeenCalledOnce() + + renderWithSelectedTarget().unmount() + + expect(reportError).not.toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + errorType: 'minimap_decoration_layer_duplicate' + }) + ) + }) + it('clears workflow activity when the panel unmounts', () => { const activity = useWorkflowTabActivityStore() activity.setEditing('workflows/active.json') diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 0faf66e0462..dde178f38f1 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -410,6 +410,10 @@ watch( { immediate: true } ) const agentMinimapLayer = registerMinimapDecorationLayer('agent.graph-activity') +// Released in its own hook, registered right next to the registration and +// ahead of every other teardown step, so a later step that throws cannot leave +// the id claimed and make the next mount report a duplicate layer. +onBeforeUnmount(() => agentMinimapLayer.dispose()) watch( () => graphActivity.state, (activity) => { @@ -1243,7 +1247,6 @@ onBeforeUnmount(() => { ++activeTabGeneration tabActivity.setEditing(null) tabActivity.setCreating(false) - agentMinimapLayer.dispose() // PM-1575: the store singleton outlives this component. Without resetting // the gate here, a remount's own setCanvasSyncGate() call is the only // thing standing between the old (now torn-down) follower's gate and a From 295ea3035a25d62728ac4c488829e0fadbc04089 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 4 Oct 2026 05:42:13 +0000 Subject: [PATCH 2/3] fix(agent): claim minimap graph-activity layer on mount Registering the layer during setup left the id claimed when setup threw before the component mounted, since no unmount hook ever ran. Register it in onMounted instead and keep the dispose in its own onBeforeUnmount hook. Tighten the unmount regression test to arm the throw right before unmount and assert it comes from the beforeUnmount hook, and add a test for a setup failure. --- .../extensions/agent/AgentPanelRoot.test.ts | 46 +++++++++++-- .../extensions/agent/AgentPanelRoot.vue | 64 ++++++++++--------- 2 files changed, 72 insertions(+), 38 deletions(-) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 574cc847884..9d78d14735f 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -10,7 +10,15 @@ 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 type { Mocked } from 'vitest' -import { computed, defineComponent, h, nextTick, reactive, ref } from 'vue' +import { + computed, + createApp, + defineComponent, + h, + nextTick, + reactive, + ref +} from 'vue' import type { Ref } from 'vue' import { useClipboard } from '@vueuse/core' @@ -4489,20 +4497,44 @@ describe('AgentPanelRoot lifecycle', () => { }) it('releases the minimap graph-activity layer even when another teardown step throws', () => { + const errorHandler = vi.fn() + const first = render(AgentPanelRoot, { + global: { plugins: [i18n], config: { errorHandler } } + }) vi.spyOn( useWorkflowTabActivityStore(), 'setCreating' ).mockImplementationOnce(() => { throw new Error('teardown failed') }) - // Mirror production, where Vue reports a failing hook and carries on. - const errorHandler = vi.fn() - const first = render(AgentPanelRoot, { - global: { plugins: [i18n], config: { errorHandler } } - }) first.unmount() - expect(errorHandler).toHaveBeenCalledOnce() + expect(errorHandler).toHaveBeenCalledWith( + expect.objectContaining({ message: 'teardown failed' }), + expect.anything(), + 'beforeUnmount hook' + ) + + renderWithSelectedTarget().unmount() + + expect(reportError).not.toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + errorType: 'minimap_decoration_layer_duplicate' + }) + ) + }) + + it('does not claim the minimap graph-activity layer when setup throws', () => { + vi.mocked(useFreeUsePlacement).mockImplementationOnce(() => { + throw new Error('setup failed') + }) + // Mounted without Testing Library, whose error handler lets a failed + // setup finish mounting; Vue itself aborts the mount. + const app = createApp(AgentPanelRoot).use(i18n) + expect(() => app.mount(document.createElement('div'))).toThrow( + 'setup failed' + ) renderWithSelectedTarget().unmount() diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index dde178f38f1..7aa3f745a26 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -9,6 +9,7 @@ import { defineAsyncComponent, nextTick, onBeforeUnmount, + onMounted, provide, readonly, ref, @@ -41,6 +42,7 @@ import { AGENT_ATTACH_ACCEPT, isAgentAttachable } from './utils/attachableFiles' import { getNodeByLocatorId } from '@/utils/graphTraversalUtil' // oxlint-disable-next-line comfy/no-restricted-paths import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' +import type { MinimapDecorationLayer } from '@/platform/canvas/minimapDecorationRegistry' import { registerMinimapDecorationLayer } from '@/platform/canvas/minimapDecorationRegistry' // The composition root injects the renderer-owned layout port; follower core // stays independent of renderer and LiteGraph runtime values. @@ -409,37 +411,37 @@ watch( }, { immediate: true } ) -const agentMinimapLayer = registerMinimapDecorationLayer('agent.graph-activity') -// Released in its own hook, registered right next to the registration and -// ahead of every other teardown step, so a later step that throws cannot leave -// the id claimed and make the next mount report a duplicate layer. -onBeforeUnmount(() => agentMinimapLayer.dispose()) -watch( - () => graphActivity.state, - (activity) => { - if (activity.phase === 'idle') { - agentMinimapLayer.replace([]) - return - } - const rootGraphId = toRootGraphId(activity.rootGraphId) - agentMinimapLayer.replace( - activity.nodeIds.map((nodeId) => ({ - target: { - rootGraphId, - owningGraphId: toOwningGraphId(activity.rootGraphId), - nodeId - }, - enter: 'pop' - })) - ) - if ( - activity.phase === 'running' && - !settingStore.get('Comfy.Minimap.Visible') - ) - void settingStore.set('Comfy.Minimap.Visible', true) - }, - { immediate: true } -) +// Claimed on mount rather than in setup so a setup failure cannot leave the id +// registered with no unmount to release it. +let agentMinimapLayer: MinimapDecorationLayer | undefined +function syncAgentMinimapLayer(activity: typeof graphActivity.state): void { + if (activity.phase === 'idle') { + agentMinimapLayer?.replace([]) + return + } + const rootGraphId = toRootGraphId(activity.rootGraphId) + agentMinimapLayer?.replace( + activity.nodeIds.map((nodeId) => ({ + target: { + rootGraphId, + owningGraphId: toOwningGraphId(activity.rootGraphId), + nodeId + }, + enter: 'pop' + })) + ) + if ( + activity.phase === 'running' && + !settingStore.get('Comfy.Minimap.Visible') + ) + void settingStore.set('Comfy.Minimap.Visible', true) +} +watch(() => graphActivity.state, syncAgentMinimapLayer, { immediate: true }) +onMounted(() => { + agentMinimapLayer = registerMinimapDecorationLayer('agent.graph-activity') + syncAgentMinimapLayer(graphActivity.state) +}) +onBeforeUnmount(() => agentMinimapLayer?.dispose()) const { accepted: consentAccepted } = storeToRefs(useAgentConsentStore()) const { withConsent } = useAgentConsent() const workspaceStore = useTeamWorkspaceStore() From 16dc864942dc80700733ff53b02679924105ceb5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 3 Oct 2026 23:07:30 -0700 Subject: [PATCH 3/3] fix(agent): let a contested minimap layer id go to the newest claimant Claiming the layer on mount closes the setup-failure window, but it does not cover the other order the registry can see: a replacement panel finishing its mount while the outgoing one still holds the id. The duplicate branch handed the newcomer a no-op layer, so the panel that survived the handover painted nothing on the minimap for the rest of the session and only a Sentry event showed it. Hand the id to the newest registrant instead, and mark the replaced layer disposed so the outgoing panel's own dispose() cannot delete its replacement's rows. The duplicate is still reported: one id held twice at once is worth seeing, and it is now a diagnostic rather than a broken minimap. Tests: a registry unit test for the handover, an AgentPanelRoot test asserting the surviving panel still paints (both fail without this change), and a black-box spec asserting the minimap's accessible summary reports the same nodes after the panel closes and reopens. Co-Authored-By: Claude Opus 5 (1M context) --- .../tests/agent/agentGraphActivity.spec.ts | 34 ++++++++++++++++++ .../canvas/minimapDecorationRegistry.ts | 14 ++++++-- .../minimap/minimapDecorationRegistry.test.ts | 36 ++++++++++++++++++- .../extensions/agent/AgentPanelRoot.test.ts | 25 ++++++++++++- 4 files changed, 105 insertions(+), 4 deletions(-) diff --git a/browser_tests/tests/agent/agentGraphActivity.spec.ts b/browser_tests/tests/agent/agentGraphActivity.spec.ts index 866093809e6..f3a79216080 100644 --- a/browser_tests/tests/agent/agentGraphActivity.spec.ts +++ b/browser_tests/tests/agent/agentGraphActivity.spec.ts @@ -1,6 +1,7 @@ import { expect } from '@playwright/test' import { agentConversationTest as test } from '@e2e/fixtures/agentConversationFixture' +import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' // Recorded full-stack source: the agent adds three nodes at x=1845, 2165 and // 2485, beyond the visible canvas. See the fixture provenance for cloud SHA, @@ -65,5 +66,38 @@ test.describe( .toEqual([]) }) }) + + test(`recorded ${SEQUENTIAL_CASE} reports the same nodes again after the panel closes and reopens`, async ({ + agentConversation, + page + }) => { + const added = agentConversation.addedNodeIds() + await agentConversation.runTurns() + + // The panel claims the minimap's agent decoration layer while it is + // mounted and gives it back when it unmounts. Nothing inside the panel + // shows whether that handover worked, so the accessible summary of the + // minimap is the only user-visible evidence: a layer the panel failed to + // release is still claimed on the next open, and the reopened panel then + // paints nothing for the rest of the session. + const minimap = page.getByTestId('minimap-container') + const panel = new AgentPanel(page) + await expect(minimap).toHaveAttribute( + 'aria-label', + `Minimap. Highlighted nodes: ${added.length}` + ) + + await panel.close() + await expect(minimap).toHaveAttribute( + 'aria-label', + 'Minimap. Highlighted nodes: 0' + ) + + await panel.open() + await expect(minimap).toHaveAttribute( + 'aria-label', + `Minimap. Highlighted nodes: ${added.length}` + ) + }) } ) diff --git a/src/platform/canvas/minimapDecorationRegistry.ts b/src/platform/canvas/minimapDecorationRegistry.ts index d62beb5aad5..5157606d12e 100644 --- a/src/platform/canvas/minimapDecorationRegistry.ts +++ b/src/platform/canvas/minimapDecorationRegistry.ts @@ -57,16 +57,26 @@ function invalidate(): void { export function registerMinimapDecorationLayer( id: string ): MinimapDecorationLayer { - if (layers.has(id)) { + const stale = layers.get(id) + if (stale) { + // Still reported: one id held twice at once is a producer bug worth seeing. + // But the newest registrant takes the id over, because handing it back a + // no-op layer left the *surviving* producer unable to paint anything for + // the rest of the session - the outgoing one is the replaced party, so + // keeping its layer kept the id claimed and the decoration dead. reportError(new Error(`Minimap decoration layer exists: ${id}`), { surface: 'graph', errorType: 'minimap_decoration_layer_duplicate' }) - return { replace() {}, dispose() {} } + // Marking it disposed is what makes the takeover safe: the outgoing + // producer's own `dispose()` then returns early instead of deleting the + // replacement's layer, and its `replace()` calls stop mutating anything. + stale.disposed = true } const layer: RegisteredLayer = { rows: new Map(), disposed: false } layers.set(id, layer) + if (stale) invalidate() return { replace(rows) { diff --git a/src/renderer/extensions/minimap/minimapDecorationRegistry.test.ts b/src/renderer/extensions/minimap/minimapDecorationRegistry.test.ts index 72afde24710..3b75e185673 100644 --- a/src/renderer/extensions/minimap/minimapDecorationRegistry.test.ts +++ b/src/renderer/extensions/minimap/minimapDecorationRegistry.test.ts @@ -1,12 +1,15 @@ -import { describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import { getMinimapDecorations, registerMinimapDecorationLayer } from '@/platform/canvas/minimapDecorationRegistry' +import { reportError } from '@/platform/telemetry/reportError' import { toOwningGraphId, toRootGraphId } from '@/types/graphScopeId' import { toNodeId } from '@/types/nodeId' +vi.mock(import('@/platform/telemetry/reportError')) + const scopeA = { rootGraphId: toRootGraphId('workflow-a'), owningGraphId: toOwningGraphId('workflow-a') @@ -17,6 +20,37 @@ const scopeB = { } describe('minimapDecorationRegistry', () => { + beforeEach(() => { + vi.mocked(reportError).mockClear() + }) + + it('hands a contested id to the newest registrant', () => { + const outgoing = registerMinimapDecorationLayer('test.takeover') + outgoing.replace([{ target: { ...scopeA, nodeId: toNodeId('1') } }]) + + // A replacement producer sets up before the outgoing one has torn down. + const incoming = registerMinimapDecorationLayer('test.takeover') + expect(reportError).toHaveBeenCalledWith( + expect.any(Error), + expect.objectContaining({ + errorType: 'minimap_decoration_layer_duplicate' + }) + ) + expect(getMinimapDecorations(scopeA)).toEqual([]) + + // The replaced producer is inert, and tearing it down must not take the + // replacement's layer with it. + outgoing.replace([{ target: { ...scopeA, nodeId: toNodeId('9') } }]) + outgoing.dispose() + incoming.replace([{ target: { ...scopeA, nodeId: toNodeId('2') } }]) + expect( + getMinimapDecorations(scopeA).map(({ target }) => target.nodeId) + ).toEqual(['2']) + + incoming.dispose() + expect(getMinimapDecorations(scopeA)).toEqual([]) + }) + it('atomically replaces graph-scoped rows and preserves entry time', () => { const layer = registerMinimapDecorationLayer('test.scope') layer.replace([ diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 9d78d14735f..f1b161145b1 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -38,7 +38,7 @@ import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspace import type { Subgraph } from '@/lib/litegraph/src/litegraph' import { LGraph, LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' import { createTestSubgraph } from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' -import { toRootGraphId } from '@/types/graphScopeId' +import { toOwningGraphId, toRootGraphId } from '@/types/graphScopeId' import { toNodeId } from '@/types/nodeId' import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' @@ -260,6 +260,7 @@ import { MAX_ATTACHMENT_BYTES } from './composables/agent/useAttachment' import type { AgentChatEvent } from './services/agent/agentEventTransport' import { useAgentChatHistoryStore } from './stores/agent/agentChatHistoryStore' import { useAgentConversationStore } from './stores/agent/agentConversationStore' +import { getMinimapDecorations } from '@/platform/canvas/minimapDecorationRegistry' import { useAgentGraphActivityStore } from './stores/agent/agentGraphActivityStore' import { useAgentPanelStore } from './stores/agent/agentPanelStore' import { useAgentComposerStore } from './stores/agent/agentComposerStore' @@ -4546,6 +4547,28 @@ describe('AgentPanelRoot lifecycle', () => { ) }) + it('still paints graph activity when a replacement panel sets up before the old one unmounts', async () => { + workflowStore.activeWorkflow = addTab('workflows/current.json') + const outgoing = renderWithSelectedTarget() + // The handover order the fix above cannot cover: a replacement host builds + // its panel while the outgoing one is still mounted and holding the id. + renderWithSelectedTarget() + outgoing.unmount() + + useAgentGraphActivityStore().recordMaterialized( + { workflowId: 'wf-42', rootGraphId: toRootGraphId('graph-1') }, + [toNodeId(301)] + ) + await nextTick() + + expect( + getMinimapDecorations({ + rootGraphId: toRootGraphId('graph-1'), + owningGraphId: toOwningGraphId('graph-1') + }).map(({ target }) => target.nodeId) + ).toEqual(['301']) + }) + it('clears workflow activity when the panel unmounts', () => { const activity = useWorkflowTabActivityStore() activity.setEditing('workflows/active.json')