Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions browser_tests/tests/agent/agentGraphActivity.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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}`
)
})
}
)
14 changes: 12 additions & 2 deletions src/platform/canvas/minimapDecorationRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,25 @@ 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}`), {
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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([
Expand Down
85 changes: 83 additions & 2 deletions src/workbench/extensions/agent/AgentPanelRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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'
Expand Down Expand Up @@ -244,6 +252,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'
Expand Down Expand Up @@ -4318,6 +4327,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')
Expand Down
61 changes: 33 additions & 28 deletions src/workbench/extensions/agent/AgentPanelRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
defineAsyncComponent,
nextTick,
onBeforeUnmount,
onMounted,
provide,
readonly,
ref,
Expand Down Expand Up @@ -40,6 +41,7 @@ import { AGENT_ATTACH_ACCEPT, isAgentAttachable } from './utils/attachableFiles'
import { getNodeByLocatorId } from '@/utils/graphTraversalUtil'
// eslint-disable-next-line import-x/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.
Expand Down Expand Up @@ -406,33 +408,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()
Expand Down Expand Up @@ -1194,7 +1200,6 @@ onBeforeUnmount(() => {
stop()
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
Expand Down
Loading