LCOV - code coverage report
Current view: top level - src/workbench/extensions/agent/crdt - restoreOpMinter.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 23.7 % 76 18
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : /**
       2              :  * Mints an undo/redo restore into the bound document. The ChangeTracker
       3              :  * replays history through `app.loadGraphData`, which litegraph runs under the
       4              :  * `load` intent source, so `docOpMinter` sees nothing of it and the document
       5              :  * keeps the state the human just undid; the next remote frame then puts it
       6              :  * back on the canvas (QAF-51).
       7              :  *
       8              :  * While the tracker is `_restoringState`, the graph is snapshotted at the
       9              :  * extension's `beforeLoadGraph` and `afterConfigureGraph` hooks and the
      10              :  * difference is expressed as semantic ops: node deletions with the links they
      11              :  * severed, node additions, changed values of widget keys present after the
      12              :  * restore, and link additions. Removed widget keys and standalone link
      13              :  * removals have no wire op; a standalone removal is surfaced, not dropped
      14              :  * silently (FE-2229).
      15              :  */
      16              : import type { WorkflowNode } from '@comfyorg/comfy-multi-player'
      17              : 
      18              : import type { LGraph } from '@/lib/litegraph/src/LGraph'
      19              : import type { LLink } from '@/lib/litegraph/src/LLink'
      20              : 
      21              : import { docInputIndex, wireNodeSnapshot } from './docOpMinter'
      22              : import type { GraphOperation } from './graphOperations'
      23              : 
      24              : export interface RestoreOpMinterDeps {
      25              :   isEnabled(): boolean
      26              :   isDocBound(): boolean
      27              :   enqueue(operations: GraphOperation[]): void
      28              :   /** The live root graph, or null when no workflow is open. */
      29              :   getGraph(): LGraph | null
      30              :   /** True while the active workflow's ChangeTracker replays an undo/redo. */
      31              :   isRestoringState(): boolean
      32              :   /** Input names in the bound document's stable order. */
      33              :   docInputNames(nodeId: string): readonly (string | undefined)[] | null
      34              : }
      35              : 
      36              : export interface RestoreOpMinter {
      37              :   detach(): void
      38              : }
      39              : 
      40              : interface RestoreSnapshot {
      41              :   nodes: Map<string, WorkflowNode>
      42              :   links: Map<string, LLink>
      43              :   /**
      44              :    * Nodes present in the graph whose `serialize()` threw. Absence from `nodes`
      45              :    * otherwise reads as "not in the graph", which the diff would turn into an
      46              :    * addition or a deletion of a node that is still on the canvas.
      47              :    */
      48              :   unserializableNodeIds: string[]
      49              : }
      50              : 
      51              : interface RestoreListener {
      52              :   onBeforeGraphLoad(): void
      53              :   onAfterGraphConfigure(): void
      54              :   onGraphLoadError(): void
      55              : }
      56              : 
      57         2355 : const activeMinters = new Set<RestoreListener>()
      58              : 
      59         3903 : export function notifyRestoreMintersBeforeGraphLoad(): void {
      60         3903 :   for (const minter of activeMinters) minter.onBeforeGraphLoad()
      61              : }
      62              : 
      63         3897 : export function notifyRestoreMintersAfterGraphConfigure(): void {
      64         3897 :   for (const minter of activeMinters) minter.onAfterGraphConfigure()
      65              : }
      66              : 
      67            6 : export function notifyRestoreMintersGraphLoadError(): void {
      68            6 :   for (const minter of activeMinters) minter.onGraphLoadError()
      69              : }
      70              : 
      71            0 : function snapshotGraph(graph: LGraph): RestoreSnapshot {
      72            0 :   const nodes = new Map<string, WorkflowNode>()
      73            0 :   const unserializableNodeIds: string[] = []
      74            0 :   for (const node of graph._nodes) {
      75            0 :     const serialized = wireNodeSnapshot(node)
      76            0 :     if (serialized) nodes.set(String(node.id), serialized)
      77            0 :     else unserializableNodeIds.push(String(node.id))
      78              :   }
      79            0 :   const links = new Map<string, LLink>()
      80            0 :   for (const link of graph.links.values()) links.set(String(link.id), link)
      81            0 :   return { nodes, links, unserializableNodeIds }
      82              : }
      83              : 
      84            0 : function widgetValuesOf(node: WorkflowNode): Record<string, unknown> {
      85            0 :   const values = node.widgets_values
      86            0 :   return values != null && typeof values === 'object' && !Array.isArray(values)
      87              :     ? values
      88              :     : {}
      89              : }
      90              : 
      91            0 : function removedNodeOperations(
      92              :   before: RestoreSnapshot,
      93              :   after: RestoreSnapshot
      94              : ): GraphOperation[] {
      95            0 :   return [...before.nodes.keys()].flatMap((id) => {
      96            0 :     if (after.nodes.has(id)) return []
      97              :     const removedLinks = [...before.links.values()]
      98              :       .filter(
      99              :         (link) => String(link.origin_id) === id || String(link.target_id) === id
     100              :       )
     101              :       .map((link) => link.id)
     102            0 :     return [{ op: 'delete_node', node_id: id, removed_links: removedLinks }]
     103              :   })
     104              : }
     105              : 
     106            0 : function addedNodeOperations(
     107              :   before: RestoreSnapshot,
     108              :   after: RestoreSnapshot
     109              : ): GraphOperation[] {
     110            0 :   return [...after.nodes].flatMap(([id, node]) =>
     111              :     before.nodes.has(id)
     112              :       ? []
     113              :       : [
     114              :           {
     115              :             op: 'add_node',
     116              :             node_id: id,
     117              :             class_type: node.type,
     118              :             pos: Array.isArray(node.pos) ? node.pos : [0, 0],
     119              :             node
     120              :           }
     121              :         ]
     122              :   )
     123              : }
     124              : 
     125            0 : function changedWidgetOperations(
     126              :   before: RestoreSnapshot,
     127              :   after: RestoreSnapshot
     128              : ): GraphOperation[] {
     129            0 :   return [...after.nodes].flatMap(([id, node]) => {
     130            0 :     const previous = before.nodes.get(id)
     131            0 :     if (!previous) return []
     132            0 :     const oldValues = widgetValuesOf(previous)
     133            0 :     return Object.entries(widgetValuesOf(node)).flatMap(([name, value]) => {
     134            0 :       const old = oldValues[name]
     135            0 :       return JSON.stringify(old) === JSON.stringify(value)
     136              :         ? []
     137              :         : [{ op: 'set_widget', node_id: id, widget: name, value }]
     138              :     })
     139              :   })
     140              : }
     141              : 
     142            0 : function addedLinkOperations(
     143              :   before: RestoreSnapshot,
     144              :   after: RestoreSnapshot,
     145              :   deps: RestoreOpMinterDeps
     146              : ): GraphOperation[] {
     147            0 :   return [...after.links].flatMap(([id, link]) => {
     148            0 :     if (before.links.has(id)) return []
     149            0 :     const targetId = String(link.target_id)
     150            0 :     const targetAdded = !before.nodes.has(targetId)
     151            0 :     const serializedInput =
     152              :       after.nodes.get(targetId)?.inputs?.[link.target_slot]
     153            0 :     const input =
     154              :       serializedInput &&
     155              :       typeof serializedInput === 'object' &&
     156              :       'name' in serializedInput &&
     157              :       typeof serializedInput.name === 'string'
     158              :         ? { name: serializedInput.name }
     159              :         : undefined
     160            0 :     const toSlot = targetAdded
     161              :       ? link.target_slot
     162              :       : docInputIndex(deps.docInputNames(targetId), input, link.target_slot)
     163            0 :     if (toSlot === undefined) return []
     164            0 :     return [
     165              :       {
     166              :         op: 'connect',
     167              :         link_id: link.id,
     168              :         from_node: link.origin_id,
     169              :         from_slot: link.origin_slot,
     170              :         to_node: link.target_id,
     171              :         to_slot: toSlot,
     172              :         link_type: String(link.type)
     173              :       }
     174              :     ]
     175              :   })
     176              : }
     177              : 
     178            0 : function reportDetachedLinks(
     179              :   before: RestoreSnapshot,
     180              :   after: RestoreSnapshot
     181              : ): void {
     182            0 :   for (const [id, link] of before.links) {
     183            0 :     if (after.links.has(id)) continue
     184              :     const endpointRemoved =
     185              :       !after.nodes.has(String(link.origin_id)) ||
     186              :       !after.nodes.has(String(link.target_id))
     187            0 :     if (!endpointRemoved) {
     188            0 :       console.error(
     189              :         '[agent-crdt] undo/redo restore removed link without its node; doc may diverge',
     190              :         id
     191              :       )
     192              :     }
     193              :   }
     194              : }
     195              : 
     196              : /**
     197              :  * An incomplete snapshot is not a smaller graph: a node that threw only after
     198              :  * the restore would read as deleted and mint `delete_node` for a node still on
     199              :  * the canvas (and the mirror case a duplicate `add_node`). Serialization
     200              :  * failure has no op either, so the whole diff is skipped loudly.
     201              :  */
     202            0 : function diffRestore(
     203              :   before: RestoreSnapshot,
     204              :   after: RestoreSnapshot,
     205              :   deps: RestoreOpMinterDeps
     206              : ): GraphOperation[] {
     207            0 :   const unserializable = [
     208              :     ...new Set([
     209              :       ...before.unserializableNodeIds,
     210              :       ...after.unserializableNodeIds
     211              :     ])
     212              :   ]
     213            0 :   if (unserializable.length > 0) {
     214            0 :     console.error(
     215              :       '[agent-crdt] undo/redo restore snapshot could not serialize every present node; skipping the restore diff so the doc is not told a still-present node was deleted',
     216              :       unserializable
     217              :     )
     218            0 :     return []
     219              :   }
     220            0 :   reportDetachedLinks(before, after)
     221            0 :   return [
     222              :     ...removedNodeOperations(before, after),
     223              :     ...addedNodeOperations(before, after),
     224              :     ...changedWidgetOperations(before, after),
     225              :     ...addedLinkOperations(before, after, deps)
     226              :   ]
     227              : }
     228              : 
     229            1 : export function attachRestoreOpMinter(
     230              :   deps: RestoreOpMinterDeps
     231              : ): RestoreOpMinter {
     232            1 :   let restoreSnapshot: RestoreSnapshot | null = null
     233              : 
     234            1 :   const listener: RestoreListener = {
     235              :     onBeforeGraphLoad() {
     236            1 :       restoreSnapshot = null
     237            1 :       if (!deps.isRestoringState() || !deps.isEnabled() || !deps.isDocBound())
     238            1 :         return
     239            0 :       const graph = deps.getGraph()
     240            0 :       if (graph) restoreSnapshot = snapshotGraph(graph)
     241              :     },
     242              :     onAfterGraphConfigure() {
     243            1 :       const before = restoreSnapshot
     244            1 :       restoreSnapshot = null
     245            1 :       if (!before) return
     246            0 :       const graph = deps.getGraph()
     247            0 :       if (!graph) return
     248            0 :       const operations = diffRestore(before, snapshotGraph(graph), deps)
     249            0 :       if (operations.length > 0) deps.enqueue(operations)
     250              :     },
     251              :     onGraphLoadError() {
     252            0 :       restoreSnapshot = null
     253              :     }
     254              :   }
     255            1 :   activeMinters.add(listener)
     256              : 
     257            1 :   return {
     258              :     detach() {
     259            0 :       activeMinters.delete(listener)
     260            0 :       restoreSnapshot = null
     261              :     }
     262              :   }
     263              : }
        

Generated by: LCOV version 2.0-1