From 4e8f6e4dda0ea3c7abfbcafb2a200099e2db7b39 Mon Sep 17 00:00:00 2001 From: Codex Date: Mon, 23 Nov 2026 20:31:11 +0800 Subject: [PATCH] fix(flow): block test run for disconnected nodes --- .../common/utils/flowValidationIssues.mjs | 154 +++++++++++++++++- am-web/src/cmvr/common/views/flow/index.vue | 16 +- 2 files changed, 152 insertions(+), 18 deletions(-) diff --git a/am-web/src/cmvr/common/utils/flowValidationIssues.mjs b/am-web/src/cmvr/common/utils/flowValidationIssues.mjs index 4189b74..0718b2c 100644 --- a/am-web/src/cmvr/common/utils/flowValidationIssues.mjs +++ b/am-web/src/cmvr/common/utils/flowValidationIssues.mjs @@ -17,8 +17,149 @@ export function validationMessages(...values) { return [...messages] } +/** + * Check whether every runnable flow node is wired into its execution scope. + * Visual selection groups are intentionally ignored; branch exits are checked + * individually because a branch node can have several independent outputs. + */ +export function flowConnectionIssues(nodes = [], edges = []) { + const runnableNodes = nodes.filter((node) => node?.type !== 'selectArea') + const byId = new Map(runnableNodes.map((node) => [String(node.id), node])) + const issues = [] + const incoming = new Map(runnableNodes.map((node) => [String(node.id), []])) + const outgoing = new Map(runnableNodes.map((node) => [String(node.id), []])) + const scopeOf = (node) => String(node?.properties?.parentId || '') + + for (const edge of edges) { + const sourceId = String(edge?.sourceNodeId || '') + const targetId = String(edge?.targetNodeId || '') + const source = byId.get(sourceId) + const target = byId.get(targetId) + if (!source || !target) { + issues.push({ + global: true, + message: `连线 ${edge?.id || ''} 的${!source ? '起点' : '终点'}节点不存在,请删除无效连线后重试` + }) + continue + } + if (scopeOf(source) !== scopeOf(target)) { + issues.push({ nodeId: sourceId, message: '连线跨越了流程/循环作用域,请重新连接' }) + issues.push({ nodeId: targetId, message: '连线跨越了流程/循环作用域,请重新连接' }) + continue + } + outgoing.get(sourceId).push(edge) + incoming.get(targetId).push(edge) + } + + const sourceOnly = new Set(['start', 'subStart']) + const targetOnly = new Set(['end', 'stopLoop', 'subEnd']) + for (const node of runnableNodes) { + const id = String(node.id) + const type = String(node.type || '') + const inEdges = incoming.get(id) || [] + const outEdges = outgoing.get(id) || [] + + if (!sourceOnly.has(type) && inEdges.length === 0) { + // A loop body has no synthetic start node, so its unconnected first node(s) + // are valid entries. Reachability below still catches isolated components. + const nestedScopeEntry = Boolean(scopeOf(node)) + if (!nestedScopeEntry) issues.push({ nodeId: id, message: '缺少输入连线,请连接上游节点' }) + } + if (sourceOnly.has(type) && inEdges.length > 0) { + issues.push({ nodeId: id, message: '起点节点不应连接输入端' }) + } + + if (!targetOnly.has(type) && type !== 'branch' && outEdges.length === 0) { + issues.push({ nodeId: id, message: '缺少输出连线,请连接下游节点' }) + } + if (targetOnly.has(type) && outEdges.length > 0) { + issues.push({ nodeId: id, message: '终止节点不应连接输出端' }) + } + + if (type === 'branch') { + if (inEdges.length === 0) issues.push({ nodeId: id, message: '分支节点缺少输入连线' }) + const reserved = new Set([`${id}_1`, `${id}_3`, `${id}_entry`]) + const anchors = (node.properties?.anchor || []).filter( + (anchor) => anchor?.id && !reserved.has(String(anchor.id)) + ) + const conditions = node.properties?.nodeParams || node.properties?.conditions || [] + if (!anchors.length) issues.push({ nodeId: id, message: '分支出口未配置' }) + for (const anchor of anchors) { + if (!outEdges.some((edge) => String(edge.sourceAnchorId || '') === String(anchor.id))) { + const conditionIndex = conditions.findIndex( + (condition) => String(condition?.id || '') === String(anchor.id) + ) + const condition = conditionIndex >= 0 ? conditions[conditionIndex] : null + const label = + condition?.label || + (anchor.id === `${id}_else` + ? 'Else' + : condition + ? `条件 ${conditionIndex + 1}` + : anchor.label || '分支') + issues.push({ nodeId: id, message: `“${label}”出口没有连线,请连接下游节点` }) + } + } + } + } + + // Node-by-node port checks miss a fully wired island (for example, a detached + // cycle). Verify that each graph scope is reachable from its entry node(s). + const scopes = new Map() + for (const node of runnableNodes) { + const key = scopeOf(node) + if (!scopes.has(key)) scopes.set(key, []) + scopes.get(key).push(node) + } + for (const [scopeId, scopeNodes] of scopes) { + const scopeIds = new Set(scopeNodes.map((node) => String(node.id))) + const starts = scopeNodes.filter((node) => node.type === 'start' || node.type === 'subStart') + const roots = scopeNodes.filter((node) => (incoming.get(String(node.id)) || []).length === 0) + if (!scopeId && starts.length === 0) { + issues.push({ global: true, message: '流程缺少开始节点,无法试运行' }) + } + for (const extraStart of starts.slice(1)) { + issues.push({ + nodeId: String(extraStart.id), + message: '流程作用域中存在多个开始节点,请保留一个入口' + }) + } + if (scopeId && starts.length === 0 && roots.length > 1) { + for (const root of roots) { + issues.push({ + nodeId: String(root.id), + message: '循环体存在多个入口,请检查是否有节点未接入循环流程' + }) + } + } + const entries = starts.length ? starts : scopeId ? roots.slice(0, 1) : [] + const reachable = new Set() + const pending = entries.map((node) => String(node.id)) + while (pending.length) { + const id = pending.pop() + if (reachable.has(id)) continue + reachable.add(id) + for (const edge of outgoing.get(id) || []) { + const targetId = String(edge.targetNodeId || '') + if (scopeIds.has(targetId)) pending.push(targetId) + } + } + for (const node of scopeNodes) { + const id = String(node.id) + if (!reachable.has(id)) { + issues.push({ + nodeId: id, + message: scopeId ? '该节点所在循环体与循环体入口不连通' : '该节点没有从流程开始节点连通' + }) + } + } + } + + return issues +} + export function nodeValidationIssues(nodes, errors) { - const byId = new Map(nodes.map(node => [node.id, node])) + const byId = new Map(nodes.map((node) => [node.id, node])) const issues = new Map() for (const error of errors) { const id = error.nodeId || error.id || (error.global ? '__flow__' : '') @@ -27,12 +168,17 @@ export function nodeValidationIssues(nodes, errors) { const previous = issues.get(id) issues.set(id, { id, - name: node?.properties?.name || node?.text?.value || (node?.type === 'start' ? '流程开始' : error.global ? '流程整体' : id), + name: + node?.properties?.name || + node?.text?.value || + (node?.type === 'start' ? '流程开始' : error.global ? '流程整体' : id), messages: validationMessages(previous?.messages, error.messages, error.message) }) } - return [...issues.values()].map(issue => ({ + return [...issues.values()].map((issue) => ({ ...issue, - messages: issue.messages.length ? issue.messages : ['参数未通过校验,请打开节点配置检查必填项及引用'] + messages: issue.messages.length + ? issue.messages + : ['参数未通过校验,请打开节点配置检查必填项及引用'] })) } diff --git a/am-web/src/cmvr/common/views/flow/index.vue b/am-web/src/cmvr/common/views/flow/index.vue index c2d893e..1a0260b 100644 --- a/am-web/src/cmvr/common/views/flow/index.vue +++ b/am-web/src/cmvr/common/views/flow/index.vue @@ -392,7 +392,7 @@ const ActionQueueSingleRun = defineAsyncComponent(() => import("./components/Act import { ElMessage, ElMessageBox } from "element-plus"; import { useFlowStore } from "@/cmvr/common/store/modules/flow"; import { emitter } from "@/cmvr/common/utils/eventBus"; -import { validationMessages, nodeValidationIssues as describeNodeValidationIssues } from '@/cmvr/common/utils/flowValidationIssues.mjs'; +import { flowConnectionIssues, validationMessages, nodeValidationIssues as describeNodeValidationIssues } from '@/cmvr/common/utils/flowValidationIssues.mjs'; import { convertToTree } from "@/cmvr/common/utils/flow"; import { actionRequiresDevice, @@ -953,21 +953,9 @@ const testRun = async () => { // 1、验证画布内所有的表单通过验证 const { nodes, edges } = lf.getGraphData(); - const isolatedNodes = nodes.filter((node) => { - const nodeId = node.id; - // 检查是否存在关联边 - return !edges.some( - (edge) => edge.sourceNodeId === nodeId || edge.targetNodeId === nodeId - ); - }); - - const gl = isolatedNodes.filter((item) => { - return !item.properties?.parentId && item.type !== "selectArea"; - }); - const resourceValidation = validateFlowResources({ nodes, edges }); preflightIssues.value = [ - ...gl.map(node => ({ nodeId: node.id, message: '节点没有连线,请接入流程后再试运行' })), + ...flowConnectionIssues(nodes, edges), ...resourceValidation.errors.map(error => ({ ...error, global: !error.nodeId, nodeId: error.nodeId || nodes.find(node => node.type === 'start')?.id })) ]; if (validationIssues.value.length) {