/** * 条件节点的空值安全校验(CONDITION_NULL_UNSAFE_COMPARE)。 * * ── 要解决的问题 ──────────────────────────────────────────────────── * * 条件节点里对一个「先经过列表取数」的字段做数值/日期比较时,如果上游列表 * 为空,取值结果是 null。而 MVEL 中 `null < 0` 返回的是 **null 而不是 false**, * 引擎对 condition 参数做 Boolean 强校验,于是直接抛: * * if 节点(xxx)的 condition 参数的解析结果必须为 Boolean 类型 * * 流程到这里**中断**,而不是安静地走 false 分支。真实案例里这类报错排查成本 * 极高,因为报错信息完全看不出是哪一次取值拿到 null。 * * ── 为什么不能靠 AND 兜住(实测结论)──────────────────────────────── * * 直觉写法是 `AND(列表非空, 额度 < 0)`,指望布尔短路跳过后面的比较。 * 但合思引擎是**逐叶子求值再合并**,不做短路:`null < 0` 照样被算出来, * 合并阶段类型校验失败,一样报错。 * * 唯一可靠的写法是把守卫**上提一层**,用流程控制短路: * * 条件节点A:结果列表 不为空 * └─(满足) 条件节点B:OR(差旅 < 0, 招待 < 0) * └─ 真正的处理动作 * * 列表为空时 A 为 false,B 根本不会被求值。 * * 本模块只做「有没有守卫」的静态判断,不尝试推断数据是否真的非空。 */ import type { WorkflowTask } from "../types.js"; import { extractLeafFilters } from "./every.js"; import { parseChoosePayloadLoose } from "../models/choose-serialization.js"; import { addDiagnostic } from "./diagnostics.js"; import type { WorkflowDiagnostic } from "./types.js"; /** 需要另一侧提供有序值的比较符;左边是 null 时结果不是布尔。 */ const ORDERING_COMPARES = new Set(["gt", "gte", "lt", "lte"]); /** 「为空 / 不为空」这一对只有它们不要求 value,也不产生 null 污染。 */ const NULL_CHECKS = new Set(["isnull", "notnull"]); interface ChoosePath { /** 取值链的根节点 key(如 action_5yt4ko) */ root: string; /** properties 链,按层级顺序 */ properties: Array<{ key?: string; type?: string; label?: string }>; /** 展示用的最后一段标签 */ label: string; } interface ChooseProperty { key?: unknown; type?: unknown; label?: unknown; } function unquote(raw: string): string | null { const text = raw.trim(); if (text.length < 2) return null; if (text.startsWith('"') && text.endsWith('"')) { try { const parsed = JSON.parse(text); return typeof parsed === "string" ? parsed : null; } catch { return null; } } if (text.startsWith("'") && text.endsWith("'")) { return text.slice(1, -1).replace(/\\'/g, "'"); } return null; } /** 解析 `@{choose('{...}')}` 里的描述对象;不是这个形态就返回 null。 */ export function readChoosePath(key: unknown): ChoosePath | null { if (typeof key !== "string") return null; const match = /^@\{\s*choose\s*\(([\s\S]*)\)\s*\}\s*$/.exec(key.trim()); if (!match) return null; const payloadText = unquote(match[1]); if (payloadText === null) return null; const normalized = parseChoosePayloadLoose(payloadText); if (!normalized) return null; let payload: Record; try { const parsed: unknown = JSON.parse(normalized); if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null; payload = parsed as Record; } catch { return null; } const root = typeof payload.key === "string" ? payload.key : ""; if (!root) return null; const properties = Array.isArray(payload.properties) ? payload.properties .filter((item): item is ChooseProperty => Boolean(item) && typeof item === "object") .map((item) => ({ key: typeof item.key === "string" ? item.key : undefined, type: typeof item.type === "string" ? item.type : undefined, label: typeof item.label === "string" ? item.label : undefined, })) : []; const label = typeof payload.label === "string" ? payload.label : properties.at(-1)?.label ?? properties.at(-1)?.key ?? root; return { root, properties, label }; } /** * 取值链是否「先穿过一个列表,再继续往下取」。 * * - `结果列表.首条数据.额度` → 穿过 DATA_LINK_LIST 后还有两层 → 有风险 * - `金额字段` → 单层取值 → 无风险 * - `结果列表`(只判列表本身)→ 列表在最后一层 → 无风险 */ export function goesThroughList(path: ChoosePath): boolean { const index = path.properties.findIndex((item) => item.type === "DATA_LINK_LIST"); return index !== -1 && index < path.properties.length - 1; } function compareOf(raw: unknown): string { return typeof raw === "string" ? raw.trim().toLowerCase() : ""; } function filterSourceOf(task: WorkflowTask): unknown { const raw = task as Record; return raw.combineFilters ?? raw.filters ?? raw.conditions; } /** 祖先链上是否存在「对同一列表做过不为空判断」的条件节点。 */ function hasAncestorListGuard(ancestors: WorkflowTask[], root: string): boolean { for (let i = ancestors.length - 1; i >= 0; i -= 1) { const ancestor = ancestors[i]; if (ancestor?.type !== "condition") continue; for (const leaf of extractLeafFilters(filterSourceOf(ancestor))) { if (compareOf(leaf.compare) !== "notnull") continue; const guarded = readChoosePath(leaf.key); if (guarded && guarded.root === root) return true; } } return false; } /** * 检查单个条件节点。命中「列表取数 + 有序比较 + 上游没有非空守卫」时产出软建议。 * * 刻意不产出硬门禁:静态分析无法确定上游列表一定为空,误判会让合法流程 * 无法发布;但这个消息本身必须把「AND 兜不住」讲清楚,否则作者会按直觉改错。 */ export function checkConditionNullSafety( task: WorkflowTask, path: string, ancestors: WorkflowTask[], diagnostics: WorkflowDiagnostic[] ): void { if (task.type !== "condition") return; const seen = new Set(); for (const leaf of extractLeafFilters(filterSourceOf(task))) { if (!ORDERING_COMPARES.has(compareOf(leaf.compare))) continue; if (NULL_CHECKS.has(compareOf(leaf.compare))) continue; const choosePath = readChoosePath(leaf.key); if (!choosePath || !goesThroughList(choosePath)) continue; if (hasAncestorListGuard(ancestors, choosePath.root)) continue; if (seen.has(choosePath.label)) continue; seen.add(choosePath.label); const compareLabel = { gt: "大于", gte: "大于等于", lt: "小于", lte: "小于等于" }[ compareOf(leaf.compare) as "gt" | "gte" | "lt" | "lte" ]; addDiagnostic(diagnostics, { severity: "warning", category: "soft_advice", code: "CONDITION_NULL_UNSAFE_COMPARE", path, message: `条件节点 [${task.label || task.name}] (${task.name}) 对字段 [${choosePath.label}] 做「${compareLabel}」比较,` + `而该字段的取值链要先穿过列表(${choosePath.properties.map((p) => p.label || p.key).join(" → ")})。` + `上游列表为空时取值是 null,null 参与比较返回的不是布尔,引擎会直接抛「condition 参数的解析结果必须为 Boolean 类型」并中断流程。` + `注意:把「列表不为空」放进同一个条件节点用 AND 组合没有用 —— 实测引擎逐叶子求值,不做短路,null 比较照样会被算出来。` + `请在该节点外层再套一个条件节点,先判断「列表不为空」,把本节点放进它的满足分支。`, }); } }