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 46a2ff8be0e..f1b161145b1 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' @@ -30,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' @@ -252,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' @@ -4488,6 +4497,78 @@ 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', () => { + const errorHandler = vi.fn() + const first = render(AgentPanelRoot, { + global: { plugins: [i18n], config: { errorHandler } } + }) + vi.spyOn( + useWorkflowTabActivityStore(), + 'setCreating' + ).mockImplementationOnce(() => { + throw new Error('teardown failed') + }) + + first.unmount() + 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() + + expect(reportError).not.toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + errorType: 'minimap_decoration_layer_duplicate' + }) + ) + }) + + 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') diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 0faf66e0462..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,33 +411,37 @@ watch( }, { immediate: true } ) -const agentMinimapLayer = registerMinimapDecorationLayer('agent.graph-activity') -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() @@ -1243,7 +1249,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