From a5740994550c8757a6ee1ae099506c82ccceb4b4 Mon Sep 17 00:00:00 2001 From: uytieu Date: Mon, 14 Sep 2026 15:07:25 -0700 Subject: [PATCH 01/19] feat(minimap): mark freshly agent-generated nodes Nothing recorded which nodes the agent authored, so the minimap drew them like any other. Stamp each node the CRDT follower creates, skipping frames a human authored and catch-up replays, and draw the stamp as a gold block that pops in, holds, then fades back to the node's ordinary fill. The animation runs on wall-clock time that no change digest can report, so it drives its own frames, and only while a mark is live. Co-Authored-By: Claude Opus 5 --- src/core/graph/graphMutations.test.ts | 25 ++++++ src/core/graph/graphMutations.ts | 24 ++++++ .../extensions/minimap/agentHighlight.test.ts | 39 ++++++++++ .../extensions/minimap/agentHighlight.ts | 40 ++++++++++ .../minimap/composables/useMinimap.test.ts | 4 + .../minimap/composables/useMinimap.ts | 29 ++++++- .../minimap/composables/useMinimapRenderer.ts | 60 +++++++++------ .../minimap/data/MinimapDataSource.ts | 5 +- .../minimap/minimapCanvasRenderer.test.ts | 77 +++++++++++++++++++ .../minimap/minimapCanvasRenderer.ts | 69 +++++++++++++++++ src/renderer/extensions/minimap/types.ts | 2 + src/stores/agentGeneratedNodesStore.ts | 50 ++++++++++++ 12 files changed, 399 insertions(+), 25 deletions(-) create mode 100644 src/renderer/extensions/minimap/agentHighlight.test.ts create mode 100644 src/renderer/extensions/minimap/agentHighlight.ts create mode 100644 src/stores/agentGeneratedNodesStore.ts diff --git a/src/core/graph/graphMutations.test.ts b/src/core/graph/graphMutations.test.ts index 5ba0bd0c778..156ffa1fe48 100644 --- a/src/core/graph/graphMutations.test.ts +++ b/src/core/graph/graphMutations.test.ts @@ -1,5 +1,6 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' +import { useAgentGeneratedNodesStore } from '@/stores/agentGeneratedNodesStore' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { useLinkStore } from '@/stores/linkStore' import { useNodeDataStore } from '@/stores/nodeDataStore' @@ -8,6 +9,7 @@ import { toOwningGraphId, toRootGraphId } from '@/types/graphScopeId' import type { RemoteMutationContext } from '@/types/graphMutationContext' import { toLinkId } from '@/types/linkId' import { toNodeId } from '@/types/nodeId' +import { createNodeLocatorId } from '@/types/nodeIdentification' import { widgetId } from '@/types/widgetId' import { createGraphMutations } from './graphMutations' @@ -79,6 +81,29 @@ describe('graphMutations', () => { ) }) + it('records agent-authored nodes so the minimap can mark them as new', () => { + mutations().addNode(node(7), context) + + expect( + useAgentGeneratedNodesStore().generatedAtFor( + createNodeLocatorId(null, toNodeId(7)) + ) + ).toBeTypeOf('number') + }) + + it('leaves human edits and catch-up frames unmarked', () => { + mutations().addNode(node(8), { ...context, actor: 'human:someone:tab-1' }) + mutations().addNode(node(9), { ...context, opId: 'replay' }) + + const agentNodes = useAgentGeneratedNodesStore() + expect( + agentNodes.generatedAtFor(createNodeLocatorId(null, toNodeId(8))) + ).toBeUndefined() + expect( + agentNodes.generatedAtFor(createNodeLocatorId(null, toNodeId(9))) + ).toBeUndefined() + }) + it('retains supplied link ids and atomically displaces the target occupant', () => { const graph = mutations() graph.batch(context, (batch) => { diff --git a/src/core/graph/graphMutations.ts b/src/core/graph/graphMutations.ts index 64d87fea562..63fae000e43 100644 --- a/src/core/graph/graphMutations.ts +++ b/src/core/graph/graphMutations.ts @@ -3,6 +3,7 @@ import type { ISerialisableNodeOutput, ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' +import { useAgentGeneratedNodesStore } from '@/stores/agentGeneratedNodesStore' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { useLinkStore } from '@/stores/linkStore' import { useNodeDataStore } from '@/stores/nodeDataStore' @@ -14,6 +15,7 @@ import { toLinkId } from '@/types/linkId' import type { LinkTopology } from '@/types/linkTopology' import type { NodeId } from '@/types/nodeId' import { toNodeId } from '@/types/nodeId' +import { createNodeLocatorId } from '@/types/nodeIdentification' import type { NodeState } from '@/types/nodeState' import type { WidgetValue } from '@/types/simplifiedWidget' import { isWidgetId, widgetId } from '@/types/widgetId' @@ -309,6 +311,27 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { const linkStore = useLinkStore() const linkPresentationStore = useLinkPresentationStore() const widgetStore = useWidgetValueStore() + const agentGeneratedNodes = useAgentGeneratedNodesStore() + + /** + * Frames a human authored are mirrored back through this same stream, and a + * catch-up frame replays nodes that landed long before this tab opened, so + * neither counts as the agent generating something now. + */ + function markAgentGenerated( + scope: GraphScope, + nodeId: NodeId, + context: RemoteMutationContext + ): void { + if (context.opId === 'replay' || context.actor.startsWith('human:')) return + + const subgraphUuid = + scope.owningGraphId === String(scope.rootGraphId) + ? null + : scope.owningGraphId + const locatorId = createNodeLocatorId(subgraphUuid, nodeId) + if (locatorId) agentGeneratedNodes.markGenerated(locatorId) + } function fail(message: string): false { console.error(`[agent-crdt] graph mutation rejected: ${message}`) @@ -672,6 +695,7 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { mutation.node.layout, context ) + markAgentGenerated(scope, mutation.node.state.id, context) } break } diff --git a/src/renderer/extensions/minimap/agentHighlight.test.ts b/src/renderer/extensions/minimap/agentHighlight.test.ts new file mode 100644 index 00000000000..388c936d0a8 --- /dev/null +++ b/src/renderer/extensions/minimap/agentHighlight.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from 'vitest' + +import { + AGENT_HIGHLIGHT_LIFETIME_MS, + agentHighlightAt +} from '@/renderer/extensions/minimap/agentHighlight' + +const CREATED_AT = 1_000_000 + +describe('agentHighlightAt', () => { + it('pops in from nothing to full size', () => { + const landed = agentHighlightAt(CREATED_AT, CREATED_AT) + const settling = agentHighlightAt(CREATED_AT, CREATED_AT + 100) + const settled = agentHighlightAt(CREATED_AT, CREATED_AT + 500) + + expect(landed?.pop).toBe(0) + expect(settling?.pop).toBeGreaterThan(0) + expect(settling?.pop).toBeLessThan(1) + expect(settled?.pop).toBe(1) + }) + + it('holds at full strength before easing away', () => { + const held = agentHighlightAt(CREATED_AT, CREATED_AT + 2000) + const fading = agentHighlightAt( + CREATED_AT, + CREATED_AT + AGENT_HIGHLIGHT_LIFETIME_MS - 500 + ) + + expect(held?.strength).toBe(1) + expect(fading?.strength).toBeGreaterThan(0) + expect(fading?.strength).toBeLessThan(1) + }) + + it('stops marking the node once the lifetime is up', () => { + expect( + agentHighlightAt(CREATED_AT, CREATED_AT + AGENT_HIGHLIGHT_LIFETIME_MS) + ).toBeNull() + }) +}) diff --git a/src/renderer/extensions/minimap/agentHighlight.ts b/src/renderer/extensions/minimap/agentHighlight.ts new file mode 100644 index 00000000000..5fe6b145174 --- /dev/null +++ b/src/renderer/extensions/minimap/agentHighlight.ts @@ -0,0 +1,40 @@ +/** Scale-up and halo of the entry animation. */ +const POP_MS = 260 +/** Full-strength hold once the node has landed. */ +const HOLD_MS = 4000 +/** Ease back to the node's ordinary fill. */ +const FADE_MS = 1800 + +export const AGENT_HIGHLIGHT_LIFETIME_MS = HOLD_MS + FADE_MS + +export interface AgentHighlight { + /** 0 the instant the node lands, 1 once it has finished popping in. */ + readonly pop: number + /** Opacity of the agent fill: 1 while held, easing to 0 as the mark expires. */ + readonly strength: number +} + +function clamp01(value: number): number { + return Math.min(1, Math.max(0, value)) +} + +function easeOutCubic(t: number): number { + return 1 - (1 - t) ** 3 +} + +/** + * The agent treatment for a node created at `generatedAt`, or null once the + * mark has expired and the node draws like any other. + */ +export function agentHighlightAt( + generatedAt: number, + now: number +): AgentHighlight | null { + const age = now - generatedAt + if (age >= AGENT_HIGHLIGHT_LIFETIME_MS) return null + + return { + pop: easeOutCubic(clamp01(age / POP_MS)), + strength: 1 - clamp01((age - HOLD_MS) / FADE_MS) + } +} diff --git a/src/renderer/extensions/minimap/composables/useMinimap.test.ts b/src/renderer/extensions/minimap/composables/useMinimap.test.ts index cdaa9f40d40..0b88b046bd0 100644 --- a/src/renderer/extensions/minimap/composables/useMinimap.test.ts +++ b/src/renderer/extensions/minimap/composables/useMinimap.test.ts @@ -249,6 +249,10 @@ describe('useMinimap', () => { } beforeEach(() => { + // Callbacks registered by an earlier test belong to a torn-down pinia, and + // triggerRAF() drives every entry in the map. + for (const id of Object.keys(rafCallbacks)) delete rafCallbacks[id] + setupVueUseMocks() registerMockLink(1, 'node2') diff --git a/src/renderer/extensions/minimap/composables/useMinimap.ts b/src/renderer/extensions/minimap/composables/useMinimap.ts index c451c788720..16030d00d1b 100644 --- a/src/renderer/extensions/minimap/composables/useMinimap.ts +++ b/src/renderer/extensions/minimap/composables/useMinimap.ts @@ -1,4 +1,4 @@ -import { useDocumentVisibility, useIntervalFn } from '@vueuse/core' +import { useDocumentVisibility, useIntervalFn, useRafFn } from '@vueuse/core' import { computed, nextTick, ref, shallowRef, watch } from 'vue' import type { ShallowRef } from 'vue' @@ -6,7 +6,9 @@ import type { LGraph } from '@/lib/litegraph/src/litegraph' import { useSettingStore } from '@/platform/settings/settingStore' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' +import { useAgentGeneratedNodesStore } from '@/stores/agentGeneratedNodesStore' +import { AGENT_HIGHLIGHT_LIFETIME_MS } from '../agentHighlight' import type { MinimapCanvas, MinimapSettingsKey } from '../types' import { useMinimapGraph } from './useMinimapGraph' import { useMinimapInteraction } from './useMinimapInteraction' @@ -27,6 +29,7 @@ export function useMinimap({ const canvasStore = useCanvasStore() const workflowStore = useWorkflowStore() const settingStore = useSettingStore() + const agentGeneratedNodes = useAgentGeneratedNodesStore() const minimapRef = ref(null) const canvasRef = canvasRefMaybe ?? shallowRef(null) @@ -146,6 +149,29 @@ export function useMinimap({ { immediate: true } ) + // Agent nodes animate on wall-clock time, which no digest can report: the + // graph is unchanged between the frame a node pops in and the frame it + // settles. Frames run only while a mark is live, and dropping the expired + // marks is what ends the loop. + const { pause: pauseAgentFrames, resume: resumeAgentFrames } = useRafFn( + () => { + agentGeneratedNodes.forgetMarksBefore( + Date.now() - AGENT_HIGHLIGHT_LIFETIME_MS + ) + renderer.renderFrame() + }, + { immediate: false } + ) + + watch( + () => shouldPoll.value && agentGeneratedNodes.hasMarks, + (animating) => { + if (animating) resumeAgentFrames() + else pauseAgentFrames() + }, + { immediate: true } + ) + const init = async () => { if (initialized.value) return @@ -174,6 +200,7 @@ export function useMinimap({ const destroy = () => { pauseChangeDetection() + pauseAgentFrames() viewport.stopViewportSync() graphManager.destroy() diff --git a/src/renderer/extensions/minimap/composables/useMinimapRenderer.ts b/src/renderer/extensions/minimap/composables/useMinimapRenderer.ts index b70e422ac59..61409b1cb49 100644 --- a/src/renderer/extensions/minimap/composables/useMinimapRenderer.ts +++ b/src/renderer/extensions/minimap/composables/useMinimapRenderer.ts @@ -25,43 +25,56 @@ export function useMinimapRenderer( const needsFullRedraw = ref(true) const needsBoundsUpdate = ref(true) - const renderMinimap = () => { + const draw = () => { const g = graph.value - if (!canvasRef.value || !g) return + if (!canvasRef.value || !g) return false const ctx = canvasRef.value.getContext('2d') - if (!ctx) return + if (!ctx) return false // Fast path for 0 nodes - just show background if (g._nodes.length === 0) { ctx.clearRect(0, 0, width, height) - return + return false } + renderMinimapToCanvas(canvasRef.value, g, { + bounds: bounds.value, + scale: scale.value, + settings: { + nodeColors: settings.nodeColors.value, + showLinks: settings.showLinks.value, + showGroups: settings.showGroups.value, + renderBypass: settings.renderBypass.value, + renderError: settings.renderError.value + }, + width, + height + }) + + return true + } + + const renderMinimap = () => { const needsRedraw = needsFullRedraw.value || updateFlags.value.nodes || updateFlags.value.connections + if (!needsRedraw) return - if (needsRedraw) { - renderMinimapToCanvas(canvasRef.value, g, { - bounds: bounds.value, - scale: scale.value, - settings: { - nodeColors: settings.nodeColors.value, - showLinks: settings.showLinks.value, - showGroups: settings.showGroups.value, - renderBypass: settings.renderBypass.value, - renderError: settings.renderError.value - }, - width, - height - }) - - needsFullRedraw.value = false - updateFlags.value.nodes = false - updateFlags.value.connections = false - } + if (!draw()) return + + needsFullRedraw.value = false + updateFlags.value.nodes = false + updateFlags.value.connections = false + } + + /** + * Repaint regardless of the change digests, for state that animates on its + * own clock rather than on an edit to the graph. + */ + const renderFrame = () => { + draw() } const updateMinimap = ( @@ -104,6 +117,7 @@ export function useMinimapRenderer( needsFullRedraw, needsBoundsUpdate, renderMinimap, + renderFrame, updateMinimap, forceFullRedraw } diff --git a/src/renderer/extensions/minimap/data/MinimapDataSource.ts b/src/renderer/extensions/minimap/data/MinimapDataSource.ts index 634b948404d..4add7efbd39 100644 --- a/src/renderer/extensions/minimap/data/MinimapDataSource.ts +++ b/src/renderer/extensions/minimap/data/MinimapDataSource.ts @@ -1,5 +1,6 @@ import type { LGraph } from '@/lib/litegraph/src/litegraph' import { calculateNodeBounds } from '@/renderer/core/spatial/boundsCalculator' +import { useAgentGeneratedNodesStore } from '@/stores/agentGeneratedNodesStore' import { useExecutionStore } from '@/stores/executionStore' import { useLinkStore } from '@/stores/linkStore' import { graphScopeOf } from '@/types/graphScopeId' @@ -27,6 +28,7 @@ export class MinimapDataSource { if (!graph) return [] const nodeProgressStates = useExecutionStore().nodeLocationProgressStates + const agentGeneratedNodes = useAgentGeneratedNodesStore() const containingSubgraphId = graph.isRootGraph ? null : graph.id return graph._nodes.map((node) => { @@ -45,7 +47,8 @@ export class MinimapDataSource { bgcolor: node.bgcolor, mode: node.mode, hasErrors: node.has_errors, - executionState + executionState, + agentGeneratedAt: agentGeneratedNodes.generatedAtFor(locatorId) } }) } diff --git a/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts b/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts index 873da9e2507..9fd67293a9b 100644 --- a/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts +++ b/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts @@ -3,8 +3,10 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import type { LGraph } from '@/lib/litegraph/src/litegraph' import { LGraphEventMode } from '@/lib/litegraph/src/litegraph' +import { AGENT_HIGHLIGHT_LIFETIME_MS } from '@/renderer/extensions/minimap/agentHighlight' import { renderMinimapToCanvas } from '@/renderer/extensions/minimap/minimapCanvasRenderer' import type { MinimapRenderContext } from '@/renderer/extensions/minimap/types' +import { useAgentGeneratedNodesStore } from '@/stores/agentGeneratedNodesStore' import { useLinkStore } from '@/stores/linkStore' import { adjustColor } from '@/utils/colorUtil' import { @@ -15,6 +17,7 @@ import { import { toOwningGraphId, toRootGraphId } from '@/types/graphScopeId' import { toLinkId } from '@/types/linkId' import { toNodeId } from '@/types/nodeId' +import { createNodeLocatorId } from '@/types/nodeIdentification' import type { UUID } from '@/utils/uuid' vi.mock(import('@/utils/colorUtil'), () => ({ @@ -346,4 +349,78 @@ describe('minimapCanvasRenderer', () => { // This affects node positioning expect(mockContext.fillRect).toHaveBeenCalled() }) + + describe('agent-generated nodes', () => { + const AGENT_COLOR_DARK = '#FDAB34' + + /** Fill colors in the order they were painted, one entry per fillRect. */ + function recordFills(): string[] { + const fills: string[] = [] + let currentStyle = '' + Object.defineProperty(mockContext, 'fillStyle', { + configurable: true, + get: () => currentStyle, + set: (value: string) => { + currentStyle = value + } + }) + vi.mocked(mockContext.fillRect).mockImplementation(() => { + fills.push(currentStyle) + }) + return fills + } + + function renderWithAgentNode(generatedAt: number): string[] { + const graph = createMockLGraph({ + _nodes: [ + createMockLGraphNode({ + id: '1', + pos: [100, 100], + size: [150, 80], + outputs: [] + }) + ], + _groups: [], + id: GRAPH_ID, + isRootGraph: true, + rootGraph: { id: GRAPH_ID } as LGraph, + getNodeById: vi.fn() + }) + useAgentGeneratedNodesStore().markGenerated( + createNodeLocatorId(null, toNodeId('1')), + generatedAt + ) + + const fills = recordFills() + renderMinimapToCanvas(mockCanvas, graph, { + bounds: { minX: 0, minY: 0, width: 500, height: 400 }, + scale: 0.5, + settings: { + nodeColors: false, + showLinks: false, + showGroups: false, + renderBypass: false, + renderError: false + }, + width: 250, + height: 200 + }) + return fills + } + + beforeEach(() => { + vi.useFakeTimers() + vi.setSystemTime(new Date('2026-01-01T00:00:00Z')) + useAgentGeneratedNodesStore().clear() + }) + + it('paints a freshly generated node in the agent color', () => { + expect(renderWithAgentNode(Date.now())).toContain(AGENT_COLOR_DARK) + }) + + it('leaves a node alone once its mark has expired', () => { + const expired = Date.now() - AGENT_HIGHLIGHT_LIFETIME_MS + expect(renderWithAgentNode(expired)).not.toContain(AGENT_COLOR_DARK) + }) + }) }) diff --git a/src/renderer/extensions/minimap/minimapCanvasRenderer.ts b/src/renderer/extensions/minimap/minimapCanvasRenderer.ts index 04367ab473f..d23ad1fed40 100644 --- a/src/renderer/extensions/minimap/minimapCanvasRenderer.ts +++ b/src/renderer/extensions/minimap/minimapCanvasRenderer.ts @@ -3,9 +3,19 @@ import { LGraphEventMode } from '@/lib/litegraph/src/litegraph' import { useColorPaletteStore } from '@/stores/workspace/colorPaletteStore' import { adjustColor } from '@/utils/colorUtil' +import { agentHighlightAt } from './agentHighlight' +import type { AgentHighlight } from './agentHighlight' import { MinimapDataSource } from './data/MinimapDataSource' import type { MinimapNodeData, MinimapRenderContext } from './types' +/** Screen pixels the halo travels beyond the node as it pops in. */ +const AGENT_HALO_PX = 5 +/** + * Agent nodes stay findable on a graph large enough to render every node as a + * sub-pixel speck, which is exactly when a new one is hardest to spot. + */ +const AGENT_MIN_MARKER_PX = 3 + /** * Get theme-aware colors for the minimap */ @@ -24,6 +34,7 @@ function getMinimapColors() { errorColor: '#FF0000', runningColor: '#00FF00', successColor: '#239B23', + agentColor: isLightTheme ? '#FD9903' : '#FDAB34', isLightTheme } } @@ -87,6 +98,53 @@ function renderGroups( } } +interface AgentHighlightEntry { + x: number + y: number + w: number + h: number + highlight: AgentHighlight +} + +/** + * Draw the agent treatment over the ordinary fill: a gold block that scales up + * behind an expanding halo, then holds and fades back to the node's own color. + */ +function renderAgentHighlights( + ctx: CanvasRenderingContext2D, + entries: readonly AgentHighlightEntry[], + colors: ReturnType +) { + ctx.fillStyle = colors.agentColor + ctx.strokeStyle = colors.agentColor + ctx.lineWidth = 1 + + for (const { x, y, w, h, highlight } of entries) { + const markerW = Math.max(w, AGENT_MIN_MARKER_PX) + const markerH = Math.max(h, AGENT_MIN_MARKER_PX) + const centerX = x + w / 2 + const centerY = y + h / 2 + const grown = markerW * (0.35 + 0.65 * highlight.pop) + const grownH = markerH * (0.35 + 0.65 * highlight.pop) + + ctx.globalAlpha = highlight.strength + ctx.fillRect(centerX - grown / 2, centerY - grownH / 2, grown, grownH) + + if (highlight.pop < 1) { + const halo = AGENT_HALO_PX * highlight.pop + ctx.globalAlpha = (1 - highlight.pop) * highlight.strength + ctx.strokeRect( + centerX - grown / 2 - halo, + centerY - grownH / 2 - halo, + grown + halo * 2, + grownH + halo * 2 + ) + } + } + + ctx.globalAlpha = 1 +} + /** * Render nodes on the minimap with performance optimizations */ @@ -103,6 +161,9 @@ function renderNodes( ctx.save() + const now = Date.now() + const agentEntries: AgentHighlightEntry[] = [] + // Group nodes by color for batch rendering (performance optimization) const nodesByColor = new Map< string, @@ -136,6 +197,12 @@ function renderNodes( hasErrors: node.hasErrors, executionState: node.executionState }) + + const highlight = + node.agentGeneratedAt === undefined + ? null + : agentHighlightAt(node.agentGeneratedAt, now) + if (highlight) agentEntries.push({ x, y, w, h, highlight }) } // Batch render nodes by color @@ -146,6 +213,8 @@ function renderNodes( } } + renderAgentHighlights(ctx, agentEntries, colors) + ctx.lineWidth = 0.3 for (const nodes of nodesByColor.values()) { for (const node of nodes) { diff --git a/src/renderer/extensions/minimap/types.ts b/src/renderer/extensions/minimap/types.ts index ef60a13133e..37feaf5a131 100644 --- a/src/renderer/extensions/minimap/types.ts +++ b/src/renderer/extensions/minimap/types.ts @@ -81,6 +81,8 @@ export interface MinimapNodeData { mode?: number hasErrors?: boolean executionState?: 'pending' | 'running' | 'finished' | 'error' | null + /** When the agent created this node, if it did. */ + agentGeneratedAt?: number } /** diff --git a/src/stores/agentGeneratedNodesStore.ts b/src/stores/agentGeneratedNodesStore.ts new file mode 100644 index 00000000000..fd8eca05a44 --- /dev/null +++ b/src/stores/agentGeneratedNodesStore.ts @@ -0,0 +1,50 @@ +import { defineStore } from 'pinia' +import { computed, ref } from 'vue' + +import type { NodeLocatorId } from '@/types/nodeIdentification' + +/** + * When the agent created each node, keyed the same way execution progress is, + * so a node in a subgraph stays distinct from the node of that id at the root. + * + * Recency is the whole point of the record: the minimap draws the entry + * animation from these stamps, and callers drop marks once they are older than + * whatever window they animate over. The store holds no opinion on that window. + */ +export const useAgentGeneratedNodesStore = defineStore( + 'agentGeneratedNodes', + () => { + const generatedAt = ref(new Map()) + + const hasMarks = computed(() => generatedAt.value.size > 0) + + function markGenerated( + locatorId: NodeLocatorId, + at: number = Date.now() + ): void { + generatedAt.value.set(locatorId, at) + } + + function generatedAtFor(locatorId: NodeLocatorId): number | undefined { + return generatedAt.value.get(locatorId) + } + + function forgetMarksBefore(cutoff: number): void { + for (const [locatorId, at] of generatedAt.value) { + if (at <= cutoff) generatedAt.value.delete(locatorId) + } + } + + function clear(): void { + generatedAt.value.clear() + } + + return { + hasMarks, + markGenerated, + generatedAtFor, + forgetMarksBefore, + clear + } + } +) From 81779fa8fd651f727f05237f8d33d6932d89cd18 Mon Sep 17 00:00:00 2001 From: uytieu Date: Mon, 14 Sep 2026 15:23:51 -0700 Subject: [PATCH 02/19] feat(minimap): hold the agent mark as provenance, not recency The gold treatment now stays for the life of the node, so the map answers which nodes the agent wrote and which a human placed rather than which ones arrived in the last few seconds. The pop-in stays; the hold-then-fade goes. Frames run only until the newest mark finishes popping. After that the treatment is static and ordinary change detection redraws it. Co-Authored-By: Claude Opus 5 --- .../extensions/minimap/agentHighlight.test.ts | 31 +++++-------------- .../extensions/minimap/agentHighlight.ts | 25 ++++----------- .../minimap/composables/useMinimap.ts | 28 ++++++++--------- .../minimap/minimapCanvasRenderer.test.ts | 29 +++++++++++------ .../minimap/minimapCanvasRenderer.ts | 23 ++++++++------ src/stores/agentGeneratedNodesStore.ts | 23 ++++++-------- 6 files changed, 71 insertions(+), 88 deletions(-) diff --git a/src/renderer/extensions/minimap/agentHighlight.test.ts b/src/renderer/extensions/minimap/agentHighlight.test.ts index 388c936d0a8..f944c5b9d06 100644 --- a/src/renderer/extensions/minimap/agentHighlight.test.ts +++ b/src/renderer/extensions/minimap/agentHighlight.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest' import { - AGENT_HIGHLIGHT_LIFETIME_MS, + AGENT_POP_MS, agentHighlightAt } from '@/renderer/extensions/minimap/agentHighlight' @@ -9,31 +9,16 @@ const CREATED_AT = 1_000_000 describe('agentHighlightAt', () => { it('pops in from nothing to full size', () => { - const landed = agentHighlightAt(CREATED_AT, CREATED_AT) - const settling = agentHighlightAt(CREATED_AT, CREATED_AT + 100) - const settled = agentHighlightAt(CREATED_AT, CREATED_AT + 500) + expect(agentHighlightAt(CREATED_AT, CREATED_AT).pop).toBe(0) - expect(landed?.pop).toBe(0) - expect(settling?.pop).toBeGreaterThan(0) - expect(settling?.pop).toBeLessThan(1) - expect(settled?.pop).toBe(1) - }) - - it('holds at full strength before easing away', () => { - const held = agentHighlightAt(CREATED_AT, CREATED_AT + 2000) - const fading = agentHighlightAt( - CREATED_AT, - CREATED_AT + AGENT_HIGHLIGHT_LIFETIME_MS - 500 - ) + const settling = agentHighlightAt(CREATED_AT, CREATED_AT + AGENT_POP_MS / 2) + expect(settling.pop).toBeGreaterThan(0) + expect(settling.pop).toBeLessThan(1) - expect(held?.strength).toBe(1) - expect(fading?.strength).toBeGreaterThan(0) - expect(fading?.strength).toBeLessThan(1) + expect(agentHighlightAt(CREATED_AT, CREATED_AT + AGENT_POP_MS).pop).toBe(1) }) - it('stops marking the node once the lifetime is up', () => { - expect( - agentHighlightAt(CREATED_AT, CREATED_AT + AGENT_HIGHLIGHT_LIFETIME_MS) - ).toBeNull() + it('keeps marking the node long after it landed', () => { + expect(agentHighlightAt(CREATED_AT, CREATED_AT + 60 * 60_000).pop).toBe(1) }) }) diff --git a/src/renderer/extensions/minimap/agentHighlight.ts b/src/renderer/extensions/minimap/agentHighlight.ts index 5fe6b145174..f7cb2fdea9e 100644 --- a/src/renderer/extensions/minimap/agentHighlight.ts +++ b/src/renderer/extensions/minimap/agentHighlight.ts @@ -1,17 +1,9 @@ /** Scale-up and halo of the entry animation. */ -const POP_MS = 260 -/** Full-strength hold once the node has landed. */ -const HOLD_MS = 4000 -/** Ease back to the node's ordinary fill. */ -const FADE_MS = 1800 - -export const AGENT_HIGHLIGHT_LIFETIME_MS = HOLD_MS + FADE_MS +export const AGENT_POP_MS = 260 export interface AgentHighlight { /** 0 the instant the node lands, 1 once it has finished popping in. */ readonly pop: number - /** Opacity of the agent fill: 1 while held, easing to 0 as the mark expires. */ - readonly strength: number } function clamp01(value: number): number { @@ -23,18 +15,13 @@ function easeOutCubic(t: number): number { } /** - * The agent treatment for a node created at `generatedAt`, or null once the - * mark has expired and the node draws like any other. + * The agent treatment for a node created at `generatedAt`. The mark is + * provenance, not recency: it holds for as long as the node exists, so this + * only ever describes where the node is in its entry animation. */ export function agentHighlightAt( generatedAt: number, now: number -): AgentHighlight | null { - const age = now - generatedAt - if (age >= AGENT_HIGHLIGHT_LIFETIME_MS) return null - - return { - pop: easeOutCubic(clamp01(age / POP_MS)), - strength: 1 - clamp01((age - HOLD_MS) / FADE_MS) - } +): AgentHighlight { + return { pop: easeOutCubic(clamp01((now - generatedAt) / AGENT_POP_MS)) } } diff --git a/src/renderer/extensions/minimap/composables/useMinimap.ts b/src/renderer/extensions/minimap/composables/useMinimap.ts index 16030d00d1b..edf808fb392 100644 --- a/src/renderer/extensions/minimap/composables/useMinimap.ts +++ b/src/renderer/extensions/minimap/composables/useMinimap.ts @@ -8,7 +8,7 @@ import { useWorkflowStore } from '@/platform/workflow/management/stores/workflow import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { useAgentGeneratedNodesStore } from '@/stores/agentGeneratedNodesStore' -import { AGENT_HIGHLIGHT_LIFETIME_MS } from '../agentHighlight' +import { AGENT_POP_MS } from '../agentHighlight' import type { MinimapCanvas, MinimapSettingsKey } from '../types' import { useMinimapGraph } from './useMinimapGraph' import { useMinimapInteraction } from './useMinimapInteraction' @@ -149,25 +149,25 @@ export function useMinimap({ { immediate: true } ) - // Agent nodes animate on wall-clock time, which no digest can report: the - // graph is unchanged between the frame a node pops in and the frame it - // settles. Frames run only while a mark is live, and dropping the expired - // marks is what ends the loop. - const { pause: pauseAgentFrames, resume: resumeAgentFrames } = useRafFn( + // A node pops in on wall-clock time, which no digest can report: the graph is + // unchanged between the frame the node lands and the frame it settles. Once + // the newest mark has finished popping the treatment is static again, and the + // ordinary change detection is enough to keep drawing it. + const agentFrames = useRafFn( () => { - agentGeneratedNodes.forgetMarksBefore( - Date.now() - AGENT_HIGHLIGHT_LIFETIME_MS - ) renderer.renderFrame() + if (Date.now() - agentGeneratedNodes.latestMarkAt >= AGENT_POP_MS) { + agentFrames.pause() + } }, { immediate: false } ) watch( - () => shouldPoll.value && agentGeneratedNodes.hasMarks, - (animating) => { - if (animating) resumeAgentFrames() - else pauseAgentFrames() + () => (shouldPoll.value ? agentGeneratedNodes.latestMarkAt : 0), + (latestMarkAt) => { + if (latestMarkAt > 0) agentFrames.resume() + else agentFrames.pause() }, { immediate: true } ) @@ -200,7 +200,7 @@ export function useMinimap({ const destroy = () => { pauseChangeDetection() - pauseAgentFrames() + agentFrames.pause() viewport.stopViewportSync() graphManager.destroy() diff --git a/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts b/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts index 9fd67293a9b..c119a1e219b 100644 --- a/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts +++ b/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts @@ -3,7 +3,6 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import type { LGraph } from '@/lib/litegraph/src/litegraph' import { LGraphEventMode } from '@/lib/litegraph/src/litegraph' -import { AGENT_HIGHLIGHT_LIFETIME_MS } from '@/renderer/extensions/minimap/agentHighlight' import { renderMinimapToCanvas } from '@/renderer/extensions/minimap/minimapCanvasRenderer' import type { MinimapRenderContext } from '@/renderer/extensions/minimap/types' import { useAgentGeneratedNodesStore } from '@/stores/agentGeneratedNodesStore' @@ -370,7 +369,10 @@ describe('minimapCanvasRenderer', () => { return fills } - function renderWithAgentNode(generatedAt: number): string[] { + function renderWithAgentNode( + generatedAt: number, + { marked = true }: { marked?: boolean } = {} + ): string[] { const graph = createMockLGraph({ _nodes: [ createMockLGraphNode({ @@ -386,10 +388,12 @@ describe('minimapCanvasRenderer', () => { rootGraph: { id: GRAPH_ID } as LGraph, getNodeById: vi.fn() }) - useAgentGeneratedNodesStore().markGenerated( - createNodeLocatorId(null, toNodeId('1')), - generatedAt - ) + if (marked) { + useAgentGeneratedNodesStore().markGenerated( + createNodeLocatorId(null, toNodeId('1')), + generatedAt + ) + } const fills = recordFills() renderMinimapToCanvas(mockCanvas, graph, { @@ -418,9 +422,16 @@ describe('minimapCanvasRenderer', () => { expect(renderWithAgentNode(Date.now())).toContain(AGENT_COLOR_DARK) }) - it('leaves a node alone once its mark has expired', () => { - const expired = Date.now() - AGENT_HIGHLIGHT_LIFETIME_MS - expect(renderWithAgentNode(expired)).not.toContain(AGENT_COLOR_DARK) + it('keeps marking the node long after the agent placed it', () => { + const anHourAgo = Date.now() - 60 * 60_000 + expect(renderWithAgentNode(anHourAgo)).toContain(AGENT_COLOR_DARK) + }) + + it('leaves nodes a human placed in the ordinary fill', () => { + useAgentGeneratedNodesStore().clear() + expect(renderWithAgentNode(Date.now(), { marked: false })).not.toContain( + AGENT_COLOR_DARK + ) }) }) }) diff --git a/src/renderer/extensions/minimap/minimapCanvasRenderer.ts b/src/renderer/extensions/minimap/minimapCanvasRenderer.ts index d23ad1fed40..8dde93aaa36 100644 --- a/src/renderer/extensions/minimap/minimapCanvasRenderer.ts +++ b/src/renderer/extensions/minimap/minimapCanvasRenderer.ts @@ -108,7 +108,8 @@ interface AgentHighlightEntry { /** * Draw the agent treatment over the ordinary fill: a gold block that scales up - * behind an expanding halo, then holds and fades back to the node's own color. + * behind an expanding halo and then holds, marking the node as the agent's for + * as long as it exists. */ function renderAgentHighlights( ctx: CanvasRenderingContext2D, @@ -127,22 +128,20 @@ function renderAgentHighlights( const grown = markerW * (0.35 + 0.65 * highlight.pop) const grownH = markerH * (0.35 + 0.65 * highlight.pop) - ctx.globalAlpha = highlight.strength ctx.fillRect(centerX - grown / 2, centerY - grownH / 2, grown, grownH) if (highlight.pop < 1) { const halo = AGENT_HALO_PX * highlight.pop - ctx.globalAlpha = (1 - highlight.pop) * highlight.strength + ctx.globalAlpha = 1 - highlight.pop ctx.strokeRect( centerX - grown / 2 - halo, centerY - grownH / 2 - halo, grown + halo * 2, grownH + halo * 2 ) + ctx.globalAlpha = 1 } } - - ctx.globalAlpha = 1 } /** @@ -198,11 +197,15 @@ function renderNodes( executionState: node.executionState }) - const highlight = - node.agentGeneratedAt === undefined - ? null - : agentHighlightAt(node.agentGeneratedAt, now) - if (highlight) agentEntries.push({ x, y, w, h, highlight }) + if (node.agentGeneratedAt !== undefined) { + agentEntries.push({ + x, + y, + w, + h, + highlight: agentHighlightAt(node.agentGeneratedAt, now) + }) + } } // Batch render nodes by color diff --git a/src/stores/agentGeneratedNodesStore.ts b/src/stores/agentGeneratedNodesStore.ts index fd8eca05a44..7f4bdea6292 100644 --- a/src/stores/agentGeneratedNodesStore.ts +++ b/src/stores/agentGeneratedNodesStore.ts @@ -1,5 +1,5 @@ import { defineStore } from 'pinia' -import { computed, ref } from 'vue' +import { ref } from 'vue' import type { NodeLocatorId } from '@/types/nodeIdentification' @@ -7,43 +7,40 @@ import type { NodeLocatorId } from '@/types/nodeIdentification' * When the agent created each node, keyed the same way execution progress is, * so a node in a subgraph stays distinct from the node of that id at the root. * - * Recency is the whole point of the record: the minimap draws the entry - * animation from these stamps, and callers drop marks once they are older than - * whatever window they animate over. The store holds no opinion on that window. + * A mark is provenance and holds for as long as the session does: the minimap + * draws agent-authored nodes differently from the ones a human placed. The + * stamp itself only feeds the entry animation. Nothing here survives a reload, + * since provenance is not part of the serialised workflow. */ export const useAgentGeneratedNodesStore = defineStore( 'agentGeneratedNodes', () => { const generatedAt = ref(new Map()) - const hasMarks = computed(() => generatedAt.value.size > 0) + /** Most recent stamp, or 0 when the agent has generated nothing. */ + const latestMarkAt = ref(0) function markGenerated( locatorId: NodeLocatorId, at: number = Date.now() ): void { generatedAt.value.set(locatorId, at) + latestMarkAt.value = Math.max(latestMarkAt.value, at) } function generatedAtFor(locatorId: NodeLocatorId): number | undefined { return generatedAt.value.get(locatorId) } - function forgetMarksBefore(cutoff: number): void { - for (const [locatorId, at] of generatedAt.value) { - if (at <= cutoff) generatedAt.value.delete(locatorId) - } - } - function clear(): void { generatedAt.value.clear() + latestMarkAt.value = 0 } return { - hasMarks, + latestMarkAt, markGenerated, generatedAtFor, - forgetMarksBefore, clear } } From aaa48fd2733e2bc9644bd51da62f529a2d2b6787 Mon Sep 17 00:00:00 2001 From: uytieu Date: Mon, 14 Sep 2026 15:34:39 -0700 Subject: [PATCH 03/19] feat(minimap): draw human-placed nodes in primary blue The default fill was an off-palette blue at 60% alpha. Use the primary token's value for each theme at full opacity, so the map reads as primary blue for what a human placed against gold for what the agent wrote. Co-Authored-By: Claude Opus 5 --- src/renderer/extensions/minimap/minimapCanvasRenderer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/extensions/minimap/minimapCanvasRenderer.ts b/src/renderer/extensions/minimap/minimapCanvasRenderer.ts index 8dde93aaa36..96af53246ee 100644 --- a/src/renderer/extensions/minimap/minimapCanvasRenderer.ts +++ b/src/renderer/extensions/minimap/minimapCanvasRenderer.ts @@ -24,7 +24,7 @@ function getMinimapColors() { const isLightTheme = colorPaletteStore.completedActivePalette.light_theme return { - nodeColor: isLightTheme ? '#3DA8E099' : '#0B8CE999', + nodeColor: isLightTheme ? '#31B9F4' : '#0B8CE9', nodeColorDefault: isLightTheme ? '#D9D9D9' : '#353535', linkColor: isLightTheme ? '#616161' : '#B3B3B3', slotColor: isLightTheme ? '#616161' : '#B3B3B3', From a14e8c81fced5fd6e52da6f05eaec2ed706a3607 Mon Sep 17 00:00:00 2001 From: uytieu Date: Mon, 14 Sep 2026 15:37:18 -0700 Subject: [PATCH 04/19] feat(minimap): bronze for human nodes, primary blue for the agent's Swaps the two fills: the majority of the map is what a human placed, so it takes the quieter bronze, and the agent's nodes carry the primary accent. Co-Authored-By: Claude Opus 5 --- src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts | 2 +- src/renderer/extensions/minimap/minimapCanvasRenderer.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts b/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts index c119a1e219b..57c1be78d80 100644 --- a/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts +++ b/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts @@ -350,7 +350,7 @@ describe('minimapCanvasRenderer', () => { }) describe('agent-generated nodes', () => { - const AGENT_COLOR_DARK = '#FDAB34' + const AGENT_COLOR_DARK = '#0B8CE9' /** Fill colors in the order they were painted, one entry per fillRect. */ function recordFills(): string[] { diff --git a/src/renderer/extensions/minimap/minimapCanvasRenderer.ts b/src/renderer/extensions/minimap/minimapCanvasRenderer.ts index 96af53246ee..12331429844 100644 --- a/src/renderer/extensions/minimap/minimapCanvasRenderer.ts +++ b/src/renderer/extensions/minimap/minimapCanvasRenderer.ts @@ -24,7 +24,7 @@ function getMinimapColors() { const isLightTheme = colorPaletteStore.completedActivePalette.light_theme return { - nodeColor: isLightTheme ? '#31B9F4' : '#0B8CE9', + nodeColor: isLightTheme ? '#C77700' : '#8C6A38', nodeColorDefault: isLightTheme ? '#D9D9D9' : '#353535', linkColor: isLightTheme ? '#616161' : '#B3B3B3', slotColor: isLightTheme ? '#616161' : '#B3B3B3', @@ -34,7 +34,7 @@ function getMinimapColors() { errorColor: '#FF0000', runningColor: '#00FF00', successColor: '#239B23', - agentColor: isLightTheme ? '#FD9903' : '#FDAB34', + agentColor: isLightTheme ? '#31B9F4' : '#0B8CE9', isLightTheme } } From ed2cdefd622648886aa4680078dd03135996803e Mon Sep 17 00:00:00 2001 From: uytieu Date: Mon, 14 Sep 2026 15:43:05 -0700 Subject: [PATCH 05/19] Revert "feat(minimap): bronze for human nodes, primary blue for the agent's" This reverts commit a14e8c81fc. Human-placed nodes go back to primary blue and the agent's back to gold. Co-Authored-By: Claude Opus 5 --- src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts | 2 +- src/renderer/extensions/minimap/minimapCanvasRenderer.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts b/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts index 57c1be78d80..c119a1e219b 100644 --- a/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts +++ b/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts @@ -350,7 +350,7 @@ describe('minimapCanvasRenderer', () => { }) describe('agent-generated nodes', () => { - const AGENT_COLOR_DARK = '#0B8CE9' + const AGENT_COLOR_DARK = '#FDAB34' /** Fill colors in the order they were painted, one entry per fillRect. */ function recordFills(): string[] { diff --git a/src/renderer/extensions/minimap/minimapCanvasRenderer.ts b/src/renderer/extensions/minimap/minimapCanvasRenderer.ts index 12331429844..96af53246ee 100644 --- a/src/renderer/extensions/minimap/minimapCanvasRenderer.ts +++ b/src/renderer/extensions/minimap/minimapCanvasRenderer.ts @@ -24,7 +24,7 @@ function getMinimapColors() { const isLightTheme = colorPaletteStore.completedActivePalette.light_theme return { - nodeColor: isLightTheme ? '#C77700' : '#8C6A38', + nodeColor: isLightTheme ? '#31B9F4' : '#0B8CE9', nodeColorDefault: isLightTheme ? '#D9D9D9' : '#353535', linkColor: isLightTheme ? '#616161' : '#B3B3B3', slotColor: isLightTheme ? '#616161' : '#B3B3B3', @@ -34,7 +34,7 @@ function getMinimapColors() { errorColor: '#FF0000', runningColor: '#00FF00', successColor: '#239B23', - agentColor: isLightTheme ? '#31B9F4' : '#0B8CE9', + agentColor: isLightTheme ? '#FD9903' : '#FDAB34', isLightTheme } } From 84ffb76ae5402baf69b1f363de34aff75bbd4f7b Mon Sep 17 00:00:00 2001 From: uytieu Date: Mon, 14 Sep 2026 15:56:24 -0700 Subject: [PATCH 06/19] fix(agent): mark generated nodes off the subscription's own state The relay sends agent frames with no op_ids, so frameContext fell back to the literal 'replay' for every one of them and the provenance mark read that fallback as "this is a catch-up frame". Nothing was ever marked, and the minimap drew agent nodes in the ordinary blue. Carry hydration explicitly instead: a subscription's first frame is its catch-up, and only that one replays state the tab did not witness arrive. Co-Authored-By: Claude Opus 5 --- src/core/graph/graphMutations.test.ts | 12 +- src/core/graph/graphMutations.ts | 6 +- src/types/graphMutationContext.ts | 5 + .../ecsFollowerAdapter.integration.test.ts | 62 ++++++++++ .../agent/crdt/ecsFollowerAdapter.ts | 112 ++++++++++-------- 5 files changed, 143 insertions(+), 54 deletions(-) diff --git a/src/core/graph/graphMutations.test.ts b/src/core/graph/graphMutations.test.ts index 156ffa1fe48..acbaf2b0024 100644 --- a/src/core/graph/graphMutations.test.ts +++ b/src/core/graph/graphMutations.test.ts @@ -93,7 +93,7 @@ describe('graphMutations', () => { it('leaves human edits and catch-up frames unmarked', () => { mutations().addNode(node(8), { ...context, actor: 'human:someone:tab-1' }) - mutations().addNode(node(9), { ...context, opId: 'replay' }) + mutations().addNode(node(9), { ...context, hydration: true }) const agentNodes = useAgentGeneratedNodesStore() expect( @@ -104,6 +104,16 @@ describe('graphMutations', () => { ).toBeUndefined() }) + it('marks a node whose frame carried no op ids', () => { + mutations().addNode(node(10), { ...context, opId: 'replay' }) + + expect( + useAgentGeneratedNodesStore().generatedAtFor( + createNodeLocatorId(null, toNodeId(10)) + ) + ).toBeTypeOf('number') + }) + it('retains supplied link ids and atomically displaces the target occupant', () => { const graph = mutations() graph.batch(context, (batch) => { diff --git a/src/core/graph/graphMutations.ts b/src/core/graph/graphMutations.ts index 63fae000e43..50c492f29b2 100644 --- a/src/core/graph/graphMutations.ts +++ b/src/core/graph/graphMutations.ts @@ -315,15 +315,15 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { /** * Frames a human authored are mirrored back through this same stream, and a - * catch-up frame replays nodes that landed long before this tab opened, so - * neither counts as the agent generating something now. + * subscription's catch-up replays nodes that landed long before this tab + * opened, so neither counts as the agent generating something now. */ function markAgentGenerated( scope: GraphScope, nodeId: NodeId, context: RemoteMutationContext ): void { - if (context.opId === 'replay' || context.actor.startsWith('human:')) return + if (context.hydration || context.actor.startsWith('human:')) return const subgraphUuid = scope.owningGraphId === String(scope.rootGraphId) diff --git a/src/types/graphMutationContext.ts b/src/types/graphMutationContext.ts index 9848afa0e4f..7149525962d 100644 --- a/src/types/graphMutationContext.ts +++ b/src/types/graphMutationContext.ts @@ -6,6 +6,11 @@ export interface RemoteMutationContext { readonly opId: string /** All effect identities when one replay frame folds several semantic ops. */ readonly opIds?: readonly string[] + /** + * This frame is the catch-up a fresh subscription receives, replaying state + * that already existed rather than reporting anything new. + */ + readonly hydration?: boolean } export function isRemoteMutationContext( diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts index f4f52bf7170..79542583015 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.integration.test.ts @@ -5,6 +5,7 @@ import * as Y from 'yjs' import { createGraphMutations } from '@/core/graph/graphMutations' import type { GraphMutations } from '@/core/graph/graphMutations' +import { useAgentGeneratedNodesStore } from '@/stores/agentGeneratedNodesStore' import { useLinkStore } from '@/stores/linkStore' import { useNodeDataStore } from '@/stores/nodeDataStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' @@ -12,6 +13,7 @@ import { toOwningGraphId, toRootGraphId } from '@/types/graphScopeId' import { toLinkId } from '@/types/linkId' import type { NodeId } from '@/types/nodeId' import { toNodeId } from '@/types/nodeId' +import { createNodeLocatorId } from '@/types/nodeIdentification' import { widgetId } from '@/types/widgetId' import type { DocUpdate } from './docFrameClient' @@ -1102,4 +1104,64 @@ describe('EcsFollowerAdapter integration', () => { expect(combined.originLinks).toEqual([toLinkId(9)]) expect(combined.targetLink).toEqual(toLinkId(9)) }) + + it('marks only the nodes the agent adds after the catch-up frame', () => { + const host = mint( + { nodes: [{ id: 1, type: 'Source', pos: [0, 0] }], links: [] }, + catalog + ) + const follower = new FollowerDoc() + const mutations = createGraphMutations({ + getScope: () => scope, + layout: { createNode: vi.fn(), deleteNodes: vi.fn() } + }) + const adapter = new EcsFollowerAdapter(mutations) + adapter.bind('wf', follower) + + // The relay sends no op ids on either frame, so nothing but the session's + // own hydration state separates the catch-up from live agent work. + const catchUp = Y.encodeStateAsUpdate(host) + follower.applyRemoteUpdate(catchUp) + adapter.applyFrame({ + workflowId: 'wf', + seq: 1, + update: catchUp, + actor: 'agent:relay:session' + }) + + const before = Y.encodeStateVector(host) + applyOps( + host, + [ + op('add-2', 2, { + op: 'add_node', + node_id: 2, + class_type: 'Sink', + pos: [300, 20], + node: { id: 2, type: 'Sink', pos: [300, 20] } + }) + ] as Parameters[1], + catalog + ) + const live = Y.encodeStateAsUpdate(host, before) + follower.applyRemoteUpdate(live) + adapter.applyFrame({ + workflowId: 'wf', + seq: 2, + update: live, + actor: 'agent:relay:session' + }) + + const agentNodes = useAgentGeneratedNodesStore() + expect( + agentNodes.generatedAtFor(createNodeLocatorId(null, toNodeId(1))) + ).toBeUndefined() + expect( + agentNodes.generatedAtFor(createNodeLocatorId(null, toNodeId(2))) + ).toBeTypeOf('number') + + adapter.destroy() + follower.destroy() + host.destroy() + }) }) diff --git a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts index 6f72c7f37fe..05f23a35bb7 100644 --- a/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts +++ b/src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts @@ -93,13 +93,17 @@ function readNodeSlots( : 'originOutputs'] } -function frameContext(update: DocUpdate): RemoteMutationContext { +function frameContext( + update: DocUpdate, + hydration: boolean +): RemoteMutationContext { const opIds = update.opIds?.filter((id) => id.length > 0) return { source: 'agent-remote', actor: update.actor ?? 'agent-replay', opId: opIds?.at(-1) ?? 'replay', - ...(opIds && opIds.length > 0 && { opIds }) + ...(opIds && opIds.length > 0 && { opIds }), + ...(hydration && { hydration: true }) } } @@ -114,6 +118,8 @@ interface TargetSession { readonly replacedWidgetMaps: Set readonly changedLinks: Set readonly frameQueue: DocUpdate[] + /** Cleared by the first frame this subscription receives: its catch-up. */ + hydrating: boolean onNodesChanged: (events: Y.YEvent>[]) => void onLinksChanged: (event: Y.YMapEvent) => void reconcileNextFrame: boolean @@ -204,6 +210,7 @@ export class EcsFollowerAdapter { replacedWidgetMaps: new Set(), changedLinks: new Set(), frameQueue: [], + hydrating: true, reconcileNextFrame: true, applying: false, onNodesChanged: (_events): void => undefined, @@ -223,6 +230,8 @@ export class EcsFollowerAdapter { const replacedWidgetMaps = new Set(session.replacedWidgetMaps) const changedLinkIds = new Set(session.changedLinks) const reconcile = session.reconcileNextFrame + const hydration = session.hydrating + session.hydrating = false this.discardSessionPending(session) const replacedNodeIds = new Set( @@ -246,60 +255,63 @@ export class EcsFollowerAdapter { const removedLinkIds = [...changedLinkIds].flatMap((id) => session.links.has(id) ? [] : [Number(id)] ) - const committed = session.mutations.batch(frameContext(update), (batch) => { - if (reconcile) { - const nodes = [...session.nodes.keys()].flatMap((id) => { - const payload = readSemanticNode(session.follower.doc, id) - return payload ? [payload] : [] - }) - const links = [...session.links.keys()].flatMap((id) => { - const link = readSemanticLink(session.follower.doc, id) - return link ? [link] : [] - }) - batch.removeMissing( - nodes.map(({ id }) => toNodeId(id)), - links.map(({ id }) => id) - ) - for (const payload of nodes) batch.reconcileNode(payload) - for (const link of links) batch.connect(link) - return - } + const committed = session.mutations.batch( + frameContext(update, hydration), + (batch) => { + if (reconcile) { + const nodes = [...session.nodes.keys()].flatMap((id) => { + const payload = readSemanticNode(session.follower.doc, id) + return payload ? [payload] : [] + }) + const links = [...session.links.keys()].flatMap((id) => { + const link = readSemanticLink(session.follower.doc, id) + return link ? [link] : [] + }) + batch.removeMissing( + nodes.map(({ id }) => toNodeId(id)), + links.map(({ id }) => id) + ) + for (const payload of nodes) batch.reconcileNode(payload) + for (const link of links) batch.connect(link) + return + } - batch.removeLinks(removedLinkIds) - for (const [id, action] of nodeActions) { - if (action === 'delete' || action === 'update') - batch.deleteNode(toNodeId(id)) - } - for (const [id, action] of nodeActions) { - if (action === 'delete') continue - const payload = readSemanticNode(session.follower.doc, id) - if (!payload) continue - batch.addNode(payload) - } - for (const id of replacedWidgetMaps) { - if (nodeActions.has(id)) continue - const payload = readSemanticNode(session.follower.doc, id) - if (payload) batch.reconcileNode(payload) - } - for (const [id, names] of changedWidgets) { - if (nodeActions.has(id) || replacedWidgetMaps.has(id)) continue - const node = session.nodes.get(id) - const widgets = node?.get('widgets') - if (!(widgets instanceof Y.Map)) continue - if ([...names].some((name) => !widgets.has(name))) { + batch.removeLinks(removedLinkIds) + for (const [id, action] of nodeActions) { + if (action === 'delete' || action === 'update') + batch.deleteNode(toNodeId(id)) + } + for (const [id, action] of nodeActions) { + if (action === 'delete') continue + const payload = readSemanticNode(session.follower.doc, id) + if (!payload) continue + batch.addNode(payload) + } + for (const id of replacedWidgetMaps) { + if (nodeActions.has(id)) continue const payload = readSemanticNode(session.follower.doc, id) if (payload) batch.reconcileNode(payload) - continue } - for (const name of names) { - batch.setWidget(toNodeId(id), name, plain(widgets.get(name))) + for (const [id, names] of changedWidgets) { + if (nodeActions.has(id) || replacedWidgetMaps.has(id)) continue + const node = session.nodes.get(id) + const widgets = node?.get('widgets') + if (!(widgets instanceof Y.Map)) continue + if ([...names].some((name) => !widgets.has(name))) { + const payload = readSemanticNode(session.follower.doc, id) + if (payload) batch.reconcileNode(payload) + continue + } + for (const name of names) { + batch.setWidget(toNodeId(id), name, plain(widgets.get(name))) + } + } + for (const id of changedLinkIds) { + const link = readSemanticLink(session.follower.doc, id) + if (link) batch.connect(link) } } - for (const id of changedLinkIds) { - const link = readSemanticLink(session.follower.doc, id) - if (link) batch.connect(link) - } - }) + ) // Only clear the reconciliation flag once the batch actually commits. // A rejected batch (no scope, or validation failure) must leave From b0d94dbd829e0bb2bb29c0a6dcb836c69cca163b Mon Sep 17 00:00:00 2001 From: uytieu Date: Mon, 14 Sep 2026 23:41:36 -0700 Subject: [PATCH 07/19] feat(agent): show graph activity while the agent writes nodes A bar and an edge shimmer mark the graph while the agent adds nodes, and a report gives the count once the turn ends, with framing onto the nodes it added. Both stay with the workflow tab that received them. Also fixes provenance for a workflow built from scratch: it arrives as the subscription's own catch-up frame, which was read as replayed history, so nothing was ever marked and no activity appeared. --- .../LiteGraphCanvasSplitterOverlay.vue | 5 +- .../graph/AgentGraphActivityBar.test.ts | 388 ++++++++++++++++++ .../graph/AgentGraphActivityBar.vue | 310 ++++++++++++++ src/components/graph/GraphCanvas.vue | 6 + src/core/graph/graphMutations.test.ts | 13 + src/core/graph/graphMutations.ts | 15 +- src/locales/en/main.json | 3 + .../minimap/minimapCanvasRenderer.test.ts | 2 +- src/stores/agentGeneratedNodesStore.test.ts | 47 +++ src/stores/agentGeneratedNodesStore.ts | 52 ++- src/stores/agentNodeSelectionStore.ts | 38 +- src/stores/workflowTabActivityStore.ts | 13 + src/utils/frameBoundsUtil.ts | 36 ++ .../extensions/agent/AgentPanelRoot.vue | 2 + src/workbench/extensions/agent/agentPanel.css | 29 -- src/workbench/extensions/agent/agentTheme.css | 78 ++++ 16 files changed, 962 insertions(+), 75 deletions(-) create mode 100644 src/components/graph/AgentGraphActivityBar.test.ts create mode 100644 src/components/graph/AgentGraphActivityBar.vue create mode 100644 src/stores/agentGeneratedNodesStore.test.ts create mode 100644 src/utils/frameBoundsUtil.ts diff --git a/src/components/LiteGraphCanvasSplitterOverlay.vue b/src/components/LiteGraphCanvasSplitterOverlay.vue index fc26b37bf6d..3f25b909cdf 100644 --- a/src/components/LiteGraphCanvasSplitterOverlay.vue +++ b/src/components/LiteGraphCanvasSplitterOverlay.vue @@ -65,7 +65,10 @@ - + ({ + getNodeByLocatorId: vi.fn() +})) + +vi.mock(import('@/renderer/core/canvas/useCanvasInteractions'), () => ({ + useCanvasInteractions: () => + fromPartial>({ + forwardEventToCanvas: vi.fn() + }) +})) + +function renderBar(props: { panelEl?: HTMLElement } = {}) { + const workspace = document.createElement('div') + workspace.className = 'workspace-panel' + document.body.append(workspace) + const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime }) + const result = render(AgentGraphActivityBar, { + props: { panelEl: undefined, ...props }, + global: { + plugins: [ + getActivePinia()!, + createI18n({ + legacy: false, + locale: 'en', + messages: { en: enMessages } + }) + ] + } + }) + return { user, workspace, ...result } +} + +const [VIEW_NODE, VIEW_NODES] = enMessages.agent.viewAddedNodes.split(' | ') + +/** The edge ring is what marks the graph as being written to. */ +const writingRing = () => screen.queryByTestId('agent-graph-edge-shimmer') +const writingBar = () => screen.queryByTestId('agent-graph-activity-bar') +const report = () => screen.queryByTestId('agent-graph-added-toast') + +/** A DOMRect derives its far edges, so a stub of one has to as well. */ +function rectOf(rect: Partial) { + const edges = { + right: (rect.left ?? 0) + (rect.width ?? 0), + bottom: (rect.top ?? 0) + (rect.height ?? 0) + } + return () => fromPartial({ ...edges, ...rect }) +} + +/** The canvas spans the window; the panel is the visible graph within it. */ +function stubLayout( + layout: { + panel: Partial + sidebar?: Partial + agentPanel?: Partial + } = { + panel: { left: 0, top: 0, width: 1000, height: 540 } + } +) { + const panelEl = document.createElement('div') + panelEl.getBoundingClientRect = rectOf(layout.panel) + const canvasEl = document.createElement('canvas') + canvasEl.getBoundingClientRect = rectOf({ + left: 0, + top: 0, + width: 1000, + height: 540 + }) + if (layout.sidebar) { + const sidebar = document.createElement('div') + sidebar.className = 'side-tool-bar-container' + sidebar.getBoundingClientRect = rectOf(layout.sidebar) + document.body.append(sidebar) + } + if (layout.agentPanel) { + const agentPanel = document.createElement('div') + agentPanel.className = 'docked-agent-panel' + agentPanel.getBoundingClientRect = rectOf(layout.agentPanel) + document.body.append(agentPanel) + } + const animateToBounds = vi.fn() + const canvas = fromPartial({ + animateToBounds, + graph: null, + canvas: canvasEl + }) + return { panelEl, animateToBounds, canvas } +} + +let nextNodeId = 0 + +async function agentAddsNodes(count = 1) { + const store = useAgentGeneratedNodesStore() + for (let i = 0; i < count; i++) { + const locatorId = createNodeLocatorId(null, toNodeId(++nextNodeId)) + if (locatorId) store.markGenerated(locatorId) + } + await nextTick() +} + +/** Past the turn-settle grace and the ring's floor together. */ +async function barsSettle() { + vi.advanceTimersByTime(2000) + await nextTick() +} + +async function turnRuns(running: boolean) { + useWorkflowTabActivityStore().setAgentRunning(running) + await nextTick() + // Idle has to hold before the turn counts as over. + if (!running) await barsSettle() +} + +/** A turn that starts, adds `count` nodes, and finishes. */ +async function turnAdds(count: number) { + await turnRuns(true) + await agentAddsNodes(count) + await turnRuns(false) +} + +describe('AgentGraphActivityBar', () => { + it('leaves the ring off while the agent runs but has not touched the graph', async () => { + renderBar() + await turnRuns(true) + + expect(writingRing()).not.toBeInTheDocument() + }) + + it('rings the graph once the agent adds a node to it', async () => { + renderBar() + await turnRuns(true) + await agentAddsNodes() + + expect(writingRing()).toBeInTheDocument() + }) + + it('says what the agent is doing while it writes', async () => { + renderBar() + await turnRuns(true) + await agentAddsNodes() + + expect(writingBar()).toHaveTextContent(enMessages.agent.updatingGraph) + }) + + it('leaves the ring off for nodes that land while no turn is running', async () => { + renderBar() + await agentAddsNodes() + + expect(writingRing()).not.toBeInTheDocument() + }) + + it('reports what the finished turn added and stays put', async () => { + renderBar() + await turnAdds(3) + + expect( + await screen.findByTestId('agent-graph-added-toast') + ).toHaveTextContent('3 nodes added to the graph by agent') + await waitFor(() => expect(writingRing()).not.toBeInTheDocument()) + + vi.advanceTimersByTime(60_000) + await nextTick() + + expect(report()).toBeInTheDocument() + }) + + it("rides out the idle between the turn's own messages", async () => { + const tabActivity = useWorkflowTabActivityStore() + renderBar() + await turnRuns(true) + await agentAddsNodes() + + tabActivity.setAgentRunning(false) + await nextTick() + vi.advanceTimersByTime(200) + tabActivity.setAgentRunning(true) + await nextTick() + + expect(writingRing()).toBeInTheDocument() + expect(report()).not.toBeInTheDocument() + }) + + it('holds the ring when a whole workflow lands in one frame', async () => { + renderBar() + await turnRuns(true) + await agentAddsNodes(5) + useWorkflowTabActivityStore().setAgentRunning(false) + await nextTick() + + expect(writingRing()).toBeInTheDocument() + expect(report()).not.toBeInTheDocument() + + await barsSettle() + + await waitFor(() => expect(report()).toBeInTheDocument()) + }) + + it('clears the report when the next turn begins', async () => { + renderBar() + await turnAdds(2) + await screen.findByTestId('agent-graph-added-toast') + + await turnRuns(true) + + await waitFor(() => expect(report()).not.toBeInTheDocument()) + }) + + it('counts only the nodes of the turn that just ended', async () => { + renderBar() + await turnAdds(2) + await screen.findByTestId('agent-graph-added-toast') + await turnAdds(1) + + await waitFor(() => + expect(report()).toHaveTextContent('1 node added to the graph by agent') + ) + expect( + screen.getByRole('button', { name: 'View node' }) + ).toBeInTheDocument() + }) + + it('reports nothing for a turn that never reached the graph', async () => { + renderBar() + await turnRuns(true) + await turnRuns(false) + await nextTick() + + expect(report()).not.toBeInTheDocument() + }) + + it('lets the report be dismissed', async () => { + const { user } = renderBar() + await turnAdds(1) + await screen.findByTestId('agent-graph-added-toast') + + await user.click( + screen.getByRole('button', { name: enMessages.agent.close }) + ) + + await waitFor(() => expect(report()).not.toBeInTheDocument()) + }) + + it('frames every node the turn added', async () => { + const queue = [ + createMockLGraphNode({ pos: [100, 100], size: [50, 40], graph: null }), + createMockLGraphNode({ pos: [400, 300], size: [50, 40], graph: null }) + ] + vi.mocked(getNodeByLocatorId).mockImplementation( + () => queue.shift() ?? null + ) + const { panelEl, animateToBounds, canvas } = stubLayout() + const { user } = renderBar({ panelEl }) + useCanvasStore().canvas = canvas + await turnAdds(2) + await screen.findByTestId('agent-graph-added-toast') + + await user.click(screen.getByRole('button', { name: VIEW_NODES })) + + // 40 graph units of padding around the union of (100,100) and (400,300). + expect(animateToBounds).toHaveBeenCalledWith( + [60, 60, 430, 320], + expect.anything() + ) + }) + + it('centres the framing on the visible graph, not the whole canvas', async () => { + vi.mocked(getNodeByLocatorId).mockReturnValue( + createMockLGraphNode({ pos: [0, 0], size: [10, 10], graph: null }) + ) + // A 300px sidebar and a 40px topbar cover a canvas spanning the window. + const { panelEl, animateToBounds, canvas } = stubLayout({ + panel: { left: 300, top: 40, width: 700, height: 500 } + }) + const { user } = renderBar({ panelEl }) + useCanvasStore().canvas = canvas + await turnAdds(1) + await screen.findByTestId('agent-graph-added-toast') + + await user.click(screen.getByRole('button', { name: VIEW_NODE })) + + expect(animateToBounds).toHaveBeenCalledWith(expect.anything(), { + viewport: [300, 40, 700, 500] + }) + }) + + it('keeps the framing clear of a side toolbar that overlays the panel', async () => { + vi.mocked(getNodeByLocatorId).mockReturnValue( + createMockLGraphNode({ pos: [0, 0], size: [10, 10], graph: null }) + ) + const { panelEl, animateToBounds, canvas } = stubLayout({ + panel: { left: 0, top: 0, width: 1000, height: 540 }, + sidebar: { left: 0, right: 64, width: 64 } + }) + const { user } = renderBar({ panelEl }) + useCanvasStore().canvas = canvas + await turnAdds(1) + await screen.findByTestId('agent-graph-added-toast') + + await user.click(screen.getByRole('button', { name: VIEW_NODE })) + + expect(animateToBounds).toHaveBeenCalledWith(expect.anything(), { + viewport: [64, 0, 936, 540] + }) + }) + + it('keeps the framing clear of the agent panel over the far edge', async () => { + vi.mocked(getNodeByLocatorId).mockReturnValue( + createMockLGraphNode({ pos: [0, 0], size: [10, 10], graph: null }) + ) + const { panelEl, animateToBounds, canvas } = stubLayout({ + panel: { left: 0, top: 0, width: 1000, height: 540 }, + sidebar: { left: 0, right: 64, width: 64 }, + agentPanel: { left: 700, right: 1000, width: 300 } + }) + const { user } = renderBar({ panelEl }) + useCanvasStore().canvas = canvas + await turnAdds(1) + await screen.findByTestId('agent-graph-added-toast') + + await user.click(screen.getByRole('button', { name: VIEW_NODE })) + + expect(animateToBounds).toHaveBeenCalledWith(expect.anything(), { + viewport: [64, 0, 636, 540] + }) + }) + + it('rings the workspace column while the agent writes', async () => { + const { workspace } = renderBar() + await turnRuns(true) + await agentAddsNodes() + + expect(workspace).toContainElement(writingRing()) + }) + + it('opens the minimap while nodes arrive and leaves it open', async () => { + const settingStore = useSettingStore() + settingStore.settingValues['Comfy.Minimap.Visible'] = false + renderBar() + + await turnRuns(true) + await agentAddsNodes() + expect(settingStore.get('Comfy.Minimap.Visible')).toBe(true) + + await turnRuns(false) + + expect(settingStore.get('Comfy.Minimap.Visible')).toBe(true) + }) + + it('stays on the tab the nodes landed in', async () => { + const workflowStore = useWorkflowStore() + const tab = (path: string) => fromPartial({ path }) + workflowStore.activeWorkflow = tab('agent.json') + renderBar() + await turnAdds(2) + await screen.findByTestId('agent-graph-added-toast') + + workflowStore.activeWorkflow = tab('other.json') + + await waitFor(() => expect(report()).not.toBeInTheDocument()) + + workflowStore.activeWorkflow = tab('agent.json') + + await waitFor(() => expect(report()).toBeInTheDocument()) + }) +}) diff --git a/src/components/graph/AgentGraphActivityBar.vue b/src/components/graph/AgentGraphActivityBar.vue new file mode 100644 index 00000000000..0b0f4a006e0 --- /dev/null +++ b/src/components/graph/AgentGraphActivityBar.vue @@ -0,0 +1,310 @@ + + + diff --git a/src/components/graph/GraphCanvas.vue b/src/components/graph/GraphCanvas.vue index 0e0b0b00ab5..1002a5b73e3 100644 --- a/src/components/graph/GraphCanvas.vue +++ b/src/components/graph/GraphCanvas.vue @@ -62,6 +62,11 @@ " class="pointer-events-auto" /> + + @@ -147,6 +152,7 @@ import AppBuilder from '@/components/builder/AppBuilder.vue' import VueNodeSwitchPopup from '@/components/builder/VueNodeSwitchPopup.vue' import ExtensionSlot from '@/components/common/ExtensionSlot.vue' import DomWidgets from '@/components/graph/DomWidgets.vue' +import AgentGraphActivityBar from '@/components/graph/AgentGraphActivityBar.vue' import GraphCanvasMenu from '@/components/graph/GraphCanvasMenu.vue' import { createNodeProgressCanvasSync } from '@/components/graph/nodeProgressCanvasSync' import LinkOverlayCanvas from '@/components/graph/LinkOverlayCanvas.vue' diff --git a/src/core/graph/graphMutations.test.ts b/src/core/graph/graphMutations.test.ts index acbaf2b0024..5d0ba21edf8 100644 --- a/src/core/graph/graphMutations.test.ts +++ b/src/core/graph/graphMutations.test.ts @@ -5,6 +5,7 @@ import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { useLinkStore } from '@/stores/linkStore' import { useNodeDataStore } from '@/stores/nodeDataStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' +import { useWorkflowTabActivityStore } from '@/stores/workflowTabActivityStore' import { toOwningGraphId, toRootGraphId } from '@/types/graphScopeId' import type { RemoteMutationContext } from '@/types/graphMutationContext' import { toLinkId } from '@/types/linkId' @@ -104,6 +105,18 @@ describe('graphMutations', () => { ).toBeUndefined() }) + it('marks a catch-up that lands while a turn is running', () => { + useWorkflowTabActivityStore().setAgentRunning(true) + + mutations().addNode(node(11), { ...context, hydration: true }) + + expect( + useAgentGeneratedNodesStore().generatedAtFor( + createNodeLocatorId(null, toNodeId(11)) + ) + ).toBeTypeOf('number') + }) + it('marks a node whose frame carried no op ids', () => { mutations().addNode(node(10), { ...context, opId: 'replay' }) diff --git a/src/core/graph/graphMutations.ts b/src/core/graph/graphMutations.ts index 50c492f29b2..6b39542f53b 100644 --- a/src/core/graph/graphMutations.ts +++ b/src/core/graph/graphMutations.ts @@ -4,6 +4,7 @@ import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' import { useAgentGeneratedNodesStore } from '@/stores/agentGeneratedNodesStore' +import { useWorkflowTabActivityStore } from '@/stores/workflowTabActivityStore' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { useLinkStore } from '@/stores/linkStore' import { useNodeDataStore } from '@/stores/nodeDataStore' @@ -312,25 +313,33 @@ export function createGraphMutations(deps: GraphMutationsDeps): GraphMutations { const linkPresentationStore = useLinkPresentationStore() const widgetStore = useWidgetValueStore() const agentGeneratedNodes = useAgentGeneratedNodesStore() + const tabActivity = useWorkflowTabActivityStore() /** * Frames a human authored are mirrored back through this same stream, and a * subscription's catch-up replays nodes that landed long before this tab - * opened, so neither counts as the agent generating something now. + * opened, so neither counts as the agent generating something now. A catch-up + * during a live turn is the agent's own work arriving: a workflow it builds + * from scratch opens its tab blank and lands entire in that first frame. */ function markAgentGenerated( scope: GraphScope, nodeId: NodeId, context: RemoteMutationContext ): void { - if (context.hydration || context.actor.startsWith('human:')) return + if (context.actor.startsWith('human:')) return + if (context.hydration && !tabActivity.agentRunning) return const subgraphUuid = scope.owningGraphId === String(scope.rootGraphId) ? null : scope.owningGraphId const locatorId = createNodeLocatorId(subgraphUuid, nodeId) - if (locatorId) agentGeneratedNodes.markGenerated(locatorId) + // A catch-up lands the whole workflow at once, so its nodes pop in sequence. + if (locatorId) + agentGeneratedNodes.markGenerated(locatorId, { + cascade: context.hydration === true + }) } function fail(message: string): false { diff --git a/src/locales/en/main.json b/src/locales/en/main.json index d05c5564453..50de383d1cf 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -5269,6 +5269,9 @@ "addPromptToSend": "Add a prompt to send", "send": "Send", "stop": "Stop", + "updatingGraph": "The agent is updating the graph", + "nodesAdded": "1 node added to the graph by agent | {count} nodes added to the graph by agent", + "viewAddedNodes": "View node | View nodes", "thinking": "Thinking...", "working": "Working...", "helpful": "Helpful", diff --git a/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts b/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts index c119a1e219b..a9b1346098f 100644 --- a/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts +++ b/src/renderer/extensions/minimap/minimapCanvasRenderer.test.ts @@ -391,7 +391,7 @@ describe('minimapCanvasRenderer', () => { if (marked) { useAgentGeneratedNodesStore().markGenerated( createNodeLocatorId(null, toNodeId('1')), - generatedAt + { at: generatedAt } ) } diff --git a/src/stores/agentGeneratedNodesStore.test.ts b/src/stores/agentGeneratedNodesStore.test.ts new file mode 100644 index 00000000000..7d1de772175 --- /dev/null +++ b/src/stores/agentGeneratedNodesStore.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from 'vitest' + +import { useAgentGeneratedNodesStore } from '@/stores/agentGeneratedNodesStore' +import { toNodeId } from '@/types/nodeId' +import type { NodeLocatorId } from '@/types/nodeIdentification' +import { createNodeLocatorId } from '@/types/nodeIdentification' + +function locator(id: number): NodeLocatorId { + const locatorId = createNodeLocatorId(null, toNodeId(id)) + if (!locatorId) throw new Error(`no locator for node ${id}`) + return locatorId +} + +describe('agentGeneratedNodesStore', () => { + it('spaces a cascade so a workflow that lands at once pops in sequence', () => { + const store = useAgentGeneratedNodesStore() + const landed = Date.now() + + for (const id of [1, 2, 3]) + store.markGenerated(locator(id), { at: landed, cascade: true }) + + const stamps = [1, 2, 3].map((id) => store.generatedAtFor(locator(id))) + expect(stamps[0]).toBe(landed) + expect(stamps).toStrictEqual([...stamps].sort((a = 0, b = 0) => a - b)) + expect(new Set(stamps).size).toBe(3) + }) + + it('stamps nodes added to a graph as they land', () => { + const store = useAgentGeneratedNodesStore() + const landed = Date.now() + + for (const id of [1, 2, 3]) store.markGenerated(locator(id), { at: landed }) + + const stamps = [1, 2, 3].map((id) => store.generatedAtFor(locator(id))) + expect(stamps).toStrictEqual([landed, landed, landed]) + }) + + it('leaves a cascade past nodes that arrive later on their own time', () => { + const store = useAgentGeneratedNodesStore() + const landed = Date.now() + + store.markGenerated(locator(1), { at: landed, cascade: true }) + store.markGenerated(locator(2), { at: landed + 500, cascade: true }) + + expect(store.generatedAtFor(locator(2))).toBe(landed + 500) + }) +}) diff --git a/src/stores/agentGeneratedNodesStore.ts b/src/stores/agentGeneratedNodesStore.ts index 7f4bdea6292..3563e88ad5a 100644 --- a/src/stores/agentGeneratedNodesStore.ts +++ b/src/stores/agentGeneratedNodesStore.ts @@ -1,8 +1,15 @@ import { defineStore } from 'pinia' -import { ref } from 'vue' +import { computed, ref } from 'vue' import type { NodeLocatorId } from '@/types/nodeIdentification' +interface MarkOptions { + /** When the node landed. Defaults to now. */ + at?: number + /** Space this mark off the last one, so a batch pops in sequence. */ + cascade?: boolean +} + /** * When the agent created each node, keyed the same way execution progress is, * so a node in a subgraph stays distinct from the node of that id at the root. @@ -20,12 +27,47 @@ export const useAgentGeneratedNodesStore = defineStore( /** Most recent stamp, or 0 when the agent has generated nothing. */ const latestMarkAt = ref(0) + /** + * The gap held between entry animations of a cascade. A workflow the agent + * builds from scratch arrives in a single frame, and stamping those nodes + * together pops them all in one flash; spacing the stamps cascades them + * instead. Nodes added to a graph already arrive spread across the turn, so + * they are stamped as they land. + */ + const POP_STAGGER_MS = 90 + + /** + * How far ahead of its arrival a stamp may be pushed. The minimap animates + * until the newest stamp has popped, so an unbounded cascade would hold it + * redrawing for 90ms x every node of a large workflow; past the budget the + * remainder pops together. + */ + const CASCADE_BUDGET_MS = 900 + + /** Marks standing this session. A turn reads its own work off this count. */ + const markCount = computed(() => generatedAt.value.size) + + /** + * The nodes marked after the first `skip`. Marks are only ever added, and + * the map keeps insertion order, so a turn's own work is the tail past the + * count it started with. + */ + function markedNodesAfter(skip: number): NodeLocatorId[] { + return [...generatedAt.value.keys()].slice(skip) + } + function markGenerated( locatorId: NodeLocatorId, - at: number = Date.now() + { at = Date.now(), cascade = false }: MarkOptions = {} ): void { - generatedAt.value.set(locatorId, at) - latestMarkAt.value = Math.max(latestMarkAt.value, at) + const stamp = cascade + ? Math.min( + Math.max(at, latestMarkAt.value + POP_STAGGER_MS), + at + CASCADE_BUDGET_MS + ) + : at + generatedAt.value.set(locatorId, stamp) + latestMarkAt.value = Math.max(latestMarkAt.value, stamp) } function generatedAtFor(locatorId: NodeLocatorId): number | undefined { @@ -39,6 +81,8 @@ export const useAgentGeneratedNodesStore = defineStore( return { latestMarkAt, + markCount, + markedNodesAfter, markGenerated, generatedAtFor, clear diff --git a/src/stores/agentNodeSelectionStore.ts b/src/stores/agentNodeSelectionStore.ts index 092eb2477f1..f915192fab1 100644 --- a/src/stores/agentNodeSelectionStore.ts +++ b/src/stores/agentNodeSelectionStore.ts @@ -7,6 +7,7 @@ import { useSettingStore } from '@/platform/settings/settingStore' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { useDialogStore } from '@/stores/dialogStore' import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore' +import { frameBounds } from '@/utils/frameBoundsUtil' const ACTION_BARS_TRANSITION_MS = 300 const BANNER_TRANSITION_MS = 150 @@ -23,43 +24,6 @@ const NODE_SELECTION_CLASS = 'node-selection-active' const MINIMAP_SETTING = 'Comfy.Minimap.Visible' -/* Graph units around the framed nodes so none sit flush with the edge. */ -const FRAME_PADDING = 40 - -/** - * Bounds are taken from `pos`/`size`, the geometry litegraph maintains for - * canvas and Vue nodes alike (`boundingRect` is a renderer cache that stays - * zeroed under Vue nodes). Structural rather than class-based so selected - * groups frame the same way selected nodes do. - */ -interface FramableItem { - pos?: ArrayLike - size?: ArrayLike -} - -function frameBounds( - items: readonly FramableItem[] -): [number, number, number, number] | null { - let minX = Infinity - let minY = Infinity - let maxX = -Infinity - let maxY = -Infinity - for (const item of items) { - if (!item.pos || !item.size) continue - minX = Math.min(minX, item.pos[0]) - minY = Math.min(minY, item.pos[1]) - maxX = Math.max(maxX, item.pos[0] + item.size[0]) - maxY = Math.max(maxY, item.pos[1] + item.size[1]) - } - if (minX === Infinity) return null - return [ - minX - FRAME_PADDING, - minY - FRAME_PADDING, - maxX - minX + FRAME_PADDING * 2, - maxY - minY + FRAME_PADDING * 2 - ] -} - export const useAgentNodeSelectionStore = defineStore( 'agentNodeSelection', () => { diff --git a/src/stores/workflowTabActivityStore.ts b/src/stores/workflowTabActivityStore.ts index 7c4ecb199d5..67807485347 100644 --- a/src/stores/workflowTabActivityStore.ts +++ b/src/stores/workflowTabActivityStore.ts @@ -12,11 +12,21 @@ export const useWorkflowTabActivityStore = defineStore( const editingTabPath = ref(null) const creatingTab = ref(false) const unseenModifiedPaths = ref>(new Set()) + /** + * Whether a turn is live, regardless of which tab it can be attributed to. + * `editingTabPath` drives the per-tab spinner and stays null for a turn the + * panel cannot pin to a tab, so the canvas cannot read activity from it. + */ + const agentRunning = ref(false) function setEditing(path: string | null): void { editingTabPath.value = path } + function setAgentRunning(running: boolean): void { + agentRunning.value = running + } + function setCreating(creating: boolean): void { creatingTab.value = creating } @@ -39,6 +49,7 @@ export const useWorkflowTabActivityStore = defineStore( function clearAgentActivity(): void { editingTabPath.value = null + agentRunning.value = false creatingTab.value = false unseenModifiedPaths.value.clear() } @@ -47,7 +58,9 @@ export const useWorkflowTabActivityStore = defineStore( editingTabPath, creatingTab, unseenModifiedPaths, + agentRunning, setEditing, + setAgentRunning, setCreating, markModified, markSeen, diff --git a/src/utils/frameBoundsUtil.ts b/src/utils/frameBoundsUtil.ts new file mode 100644 index 00000000000..1fa668c6dde --- /dev/null +++ b/src/utils/frameBoundsUtil.ts @@ -0,0 +1,36 @@ +/* Graph units around the framed items so none sit flush with the edge. */ +const FRAME_PADDING = 40 + +/** + * Bounds are taken from `pos`/`size`, the geometry litegraph maintains for + * canvas and Vue nodes alike (`boundingRect` is a renderer cache that stays + * zeroed under Vue nodes). Structural rather than class-based so groups frame + * the same way nodes do. + */ +interface FramableItem { + pos?: ArrayLike + size?: ArrayLike +} + +export function frameBounds( + items: readonly FramableItem[] +): [number, number, number, number] | null { + let minX = Infinity + let minY = Infinity + let maxX = -Infinity + let maxY = -Infinity + for (const item of items) { + if (!item.pos || !item.size) continue + minX = Math.min(minX, item.pos[0]) + minY = Math.min(minY, item.pos[1]) + maxX = Math.max(maxX, item.pos[0] + item.size[0]) + maxY = Math.max(maxY, item.pos[1] + item.size[1]) + } + if (minX === Infinity) return null + return [ + minX - FRAME_PADDING, + minY - FRAME_PADDING, + maxX - minX + FRAME_PADDING * 2, + maxY - minY + FRAME_PADDING * 2 + ] +} diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 0ed7d403de1..83ad6acd97a 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -541,6 +541,7 @@ function resumedTurnTabPath(): string | null { // primary spinner setters; the non-idle branch only re-arms it after the // stash/resume flip of a panel remount, where those setters never run. watch(status, (value) => { + tabActivity.setAgentRunning(value !== 'idle') if (value === 'idle') { const completedPath = tabActivity.editingTabPath tabActivity.setEditing(null) @@ -699,6 +700,7 @@ onBeforeUnmount(() => { exitNodeSelectionMode() stop() tabActivity.setEditing(null) + tabActivity.setAgentRunning(false) tabActivity.setCreating(false) }) diff --git a/src/workbench/extensions/agent/agentPanel.css b/src/workbench/extensions/agent/agentPanel.css index 51244da21e6..38352ab56d3 100644 --- a/src/workbench/extensions/agent/agentPanel.css +++ b/src/workbench/extensions/agent/agentPanel.css @@ -1,32 +1,3 @@ -.agent-shimmer-text { - background: linear-gradient( - 90deg, - var(--color-muted-foreground) 35%, - var(--color-base-foreground) 50%, - var(--color-muted-foreground) 65% - ); - background-size: 200% auto; - background-clip: text; - -webkit-text-fill-color: transparent; - animation: agent-shimmer 1.8s linear infinite; -} - -.disable-animations - :where(#agent-panel-root, .agent-scope) - .agent-shimmer-text { - animation: none; - color: var(--color-muted-foreground); - -webkit-text-fill-color: currentcolor; -} - -@media (prefers-reduced-motion: reduce) { - .agent-shimmer-text { - animation: none; - color: var(--color-muted-foreground); - -webkit-text-fill-color: currentcolor; - } -} - /* Tailwind Preflight is disabled host-wide (PrimeVue + litegraph coexistence), so raw form elements in the panel inherit UA chrome: a