diff --git a/src/scripts/app.test.ts b/src/scripts/app.test.ts index d97e71f1213..04cd45eda78 100644 --- a/src/scripts/app.test.ts +++ b/src/scripts/app.test.ts @@ -2505,6 +2505,26 @@ describe('ComfyApp', () => { } }) + it('reports prompt construction failures with an id-free fingerprint', async () => { + prepareEmptyPromptQueue() + const failure = new Error( + 'No output node found for id [632] slot [0] images' + ) + vi.spyOn(app, 'graphToPrompt').mockRejectedValue(failure) + + await expect(app.queuePrompt(0)).rejects.toThrow(failure) + expect(reportError).toHaveBeenCalledWith(failure, { + surface: 'graph', + errorType: 'error_building_prompt', + fingerprint: [ + 'error_building_prompt', + 'Error', + 'No output node found for id [] slot []' + ], + logToConsole: false + }) + }) + it('stores execution intent when workflow context collection fails', async () => { prepareEmptyPromptQueue() const registry = new TelemetryRegistry() diff --git a/src/scripts/app.ts b/src/scripts/app.ts index c6fbd692b2a..df75cbd3c55 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -154,6 +154,7 @@ import type { NodeExecutionId } from '@comfyorg/graph-model/nodeIdentification' import { getErrorMessage } from '@comfyorg/shared-frontend-utils/errorUtil' import { normalizePromptError } from '@/utils/executionErrorUtil' import { graphToPrompt, unwrapExportedWidgetValue } from '@/utils/executionUtil' +import { promptBuildErrorFingerprint } from '@/utils/promptBuildErrorFingerprint' import { parseJsonWithNonFinite } from '@/utils/jsonUtil' import { getCnrIdFromProperties } from '@/platform/workflowErrors/nodeReplacement/cnrIdUtil' import { useMissingNodesErrorStore } from '@/platform/workflowErrors/nodeReplacement/missingNodesErrorStore' @@ -1867,6 +1868,12 @@ export class ComfyApp implements IComfyApp { failureReason: 'prompt_build_failed', ...workflowExecutionIntent }) + reportError(error, { + surface: 'graph', + errorType: 'error_building_prompt', + fingerprint: promptBuildErrorFingerprint(error), + logToConsole: false + }) throw error } ) diff --git a/src/utils/promptBuildErrorFingerprint.producers.test.ts b/src/utils/promptBuildErrorFingerprint.producers.test.ts new file mode 100644 index 00000000000..020bc69eb89 --- /dev/null +++ b/src/utils/promptBuildErrorFingerprint.producers.test.ts @@ -0,0 +1,71 @@ +import { beforeEach, describe, expect, it } from 'vitest' + +import { + createTestSubgraph, + createTestSubgraphNode, + resetSubgraphFixtureState +} from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' +import { promptBuildErrorFingerprint } from '@/utils/promptBuildErrorFingerprint' + +describe('promptBuildErrorFingerprint on real prompt build throwers', () => { + beforeEach(() => { + resetSubgraphFixtureState() + }) + + function fingerprintOfThrow(run: () => void): string[] { + try { + run() + } catch (error) { + return promptBuildErrorFingerprint(error) + } + throw new Error('expected the prompt build thrower to throw') + } + + it('groups SubgraphNode.rootGraph NullGraphError across string node ids', () => { + function detachedRootGraphAccess(nodeId: string) { + const subgraphNode = createTestSubgraphNode(createTestSubgraph(), { + id: nodeId + }) + subgraphNode.graph = null + return fingerprintOfThrow(() => subgraphNode.rootGraph) + } + + expect(detachedRootGraphAccess('alpha')).toEqual([ + 'error_building_prompt', + 'NullGraphError', + 'SubgraphNode N has no graph' + ]) + expect(detachedRootGraphAccess('beta')).toEqual( + detachedRootGraphAccess('alpha') + ) + }) + + it('groups SubgraphNode.getInnerNodes RecursionError across ids, titles and subgraph names', () => { + function selfVisitingInnerNodes( + nodeId: string, + title: string, + subgraphName: string + ) { + const subgraphNode = createTestSubgraphNode( + createTestSubgraph({ name: subgraphName }), + { id: nodeId } + ) + subgraphNode.title = title + return fingerprintOfThrow(() => + subgraphNode.getInnerNodes(new Map(), [], [], new Set([subgraphNode])) + ) + } + + expect(selfVisitingInnerNodes('alpha', 'Load. Image', 'Detailer')).toEqual([ + 'error_building_prompt', + 'RecursionError', + 'Circular reference detected at depth N in node N of subgraph N' + ]) + expect(selfVisitingInnerNodes('beta', 'Upscale', 'Refiner')).toEqual( + selfVisitingInnerNodes('alpha', 'Load. Image', 'Detailer') + ) + expect( + selfVisitingInnerNodes('gamma', 'Load of node Image', 'Enhancer') + ).toEqual(selfVisitingInnerNodes('alpha', 'Load. Image', 'Detailer')) + }) +}) diff --git a/src/utils/promptBuildErrorFingerprint.test.ts b/src/utils/promptBuildErrorFingerprint.test.ts new file mode 100644 index 00000000000..3ab68ed1382 --- /dev/null +++ b/src/utils/promptBuildErrorFingerprint.test.ts @@ -0,0 +1,137 @@ +import { describe, expect, it } from 'vitest' + +import { promptBuildErrorFingerprint } from '@/utils/promptBuildErrorFingerprint' + +describe('promptBuildErrorFingerprint', () => { + it('groups missing output nodes across ids, slots and output names', () => { + const first = new Error('No output node found for id [632] slot [0] images') + const second = new Error('No output node found for id [12:7] slot [3] mask') + + expect(promptBuildErrorFingerprint(first)).toEqual( + promptBuildErrorFingerprint(second) + ) + }) + + it('keeps different failure kinds apart', () => { + const missingOutput = new Error( + 'No output node found for id [1] slot [0] x' + ) + const missingInput = new TypeError( + 'No input found for flattened id [1] slot [0]' + ) + + expect(promptBuildErrorFingerprint(missingOutput)).not.toEqual( + promptBuildErrorFingerprint(missingInput) + ) + }) + + it.for([ + { + thrower: 'ExecutableNodeDTO.resolveInput', + suffix: ' during input resolution' + }, + { thrower: 'SubgraphNode.rootGraph', suffix: '' } + ])( + 'groups NullGraphError across numeric and string subgraph node ids ($thrower)', + ({ suffix }) => { + function nullGraphError(nodeId: string) { + const error = new Error(`SubgraphNode ${nodeId} has no graph${suffix}`) + error.name = 'NullGraphError' + return error + } + + expect(promptBuildErrorFingerprint(nullGraphError('alpha'))).toEqual([ + 'error_building_prompt', + 'NullGraphError', + `SubgraphNode N has no graph${suffix}` + ]) + expect(promptBuildErrorFingerprint(nullGraphError('beta'))).toEqual( + promptBuildErrorFingerprint(nullGraphError('alpha')) + ) + expect(promptBuildErrorFingerprint(nullGraphError('42'))).toEqual( + promptBuildErrorFingerprint(nullGraphError('alpha')) + ) + } + ) + + it('keeps the two NullGraphError sites apart', () => { + const duringInput = new Error( + 'SubgraphNode alpha has no graph during input resolution' + ) + const rootGraph = new Error('SubgraphNode alpha has no graph') + + expect(promptBuildErrorFingerprint(duringInput)).not.toEqual( + promptBuildErrorFingerprint(rootGraph) + ) + }) + + it('groups subgraph hierarchy recursion errors across ids, titles, names and depths', () => { + function hierarchyRecursionError( + depth: number, + nodeInfo: string, + subgraphName: string + ) { + const error = new Error( + `Circular reference detected at depth ${depth} in node ${nodeInfo} of subgraph '${subgraphName}'. ` + + `This creates an infinite loop in the subgraph hierarchy.` + ) + error.name = 'RecursionError' + return error + } + + expect( + promptBuildErrorFingerprint( + hierarchyRecursionError(2, 'alpha (Load. Image)', 'Detailer') + ) + ).toEqual( + promptBuildErrorFingerprint( + hierarchyRecursionError(3, 'beta', 'Unnamed Subgraph') + ) + ) + }) + + it.for([ + { direction: 'input', path: '' }, + { direction: 'output', path: ' at path sub-a:sub-b' } + ])( + 'groups recursion errors across string node ids, titles and paths ($direction)', + ({ direction, path }) => { + function recursionError(nodeInfo: string, uniqueId: string) { + const error = new Error( + `Circular reference detected while resolving ${direction} 0 of node ${nodeInfo}${path}. ` + + `This creates an infinite loop in link resolution. UniqueID: [${uniqueId}]` + ) + error.name = 'RecursionError' + return error + } + + expect( + promptBuildErrorFingerprint( + recursionError('alpha (Load. Image)', 'x:alpha[I]0') + ) + ).toEqual( + promptBuildErrorFingerprint(recursionError('beta', 'y:beta[I]0')) + ) + } + ) + + it('groups errors that end with a string node id', () => { + expect( + promptBuildErrorFingerprint( + new TypeError('Node is not a subgraph node: alpha') + ) + ).toEqual( + promptBuildErrorFingerprint( + new TypeError('Node is not a subgraph node: beta') + ) + ) + }) + + it('uses the error name for values that are not errors', () => { + expect(promptBuildErrorFingerprint('boom 12')).toEqual([ + 'error_building_prompt', + 'Error', + 'boom N' + ]) + }) +}) diff --git a/src/utils/promptBuildErrorFingerprint.ts b/src/utils/promptBuildErrorFingerprint.ts new file mode 100644 index 00000000000..de8cc10804d --- /dev/null +++ b/src/utils/promptBuildErrorFingerprint.ts @@ -0,0 +1,27 @@ +import { toError } from '@comfyorg/shared-frontend-utils/errorUtil' + +const IDENTITY_POSITIONS: readonly (readonly [RegExp, string])[] = [ + [ + /^(Circular reference detected while resolving (?:input|output) \d+ of node) [\s\S]*$/, + '$1 N' + ], + [/ in node [\s\S]*? of subgraph [\s\S]*$/, ' in node N of subgraph N'], + [/\b(Subgraph)?Node \S+ has no graph/, '$1Node N has no graph'], + [ + /^(No output node found for id \[[^\]]*\] slot \[[^\]]*\])\s+[\s\S]+$/, + '$1' + ], + [/\[[^\]]*\]/g, '[]'], + [/: \S+$/, ': N'], + [/\d+/g, 'N'] +] + +/** Sentry fingerprint for a prompt build failure, with node ids, titles, subgraph names, paths and slot indexes removed. */ +export function promptBuildErrorFingerprint(error: unknown): string[] { + const { name, message } = toError(error) + const shape = IDENTITY_POSITIONS.reduce( + (text, [position, placeholder]) => text.replace(position, placeholder), + message + ) + return ['error_building_prompt', name, shape] +}