## 背景
条件节点对「取值链要先穿过列表再往下取字段」的字段做大于/小于类比较时,上游
列表为空会让字段取到 `null`。MVEL 里 `null < 0` 返回的是 **null 而不是 false**,
引擎对 condition 做强 Boolean 校验,于是直接抛:
```
if 节点(xxx)的 condition 参数的解析结果必须为 Boolean 类型
```
流程在这里**中断**,而不是安静地走 false 分支。真实案例中一条链路跑到第 9 步
才炸,报错信息完全指不出是哪次取值拿到了 null。
## 关键实测结论:AND 不短路
直觉写法是 `AND(列表非空, 额度 < 0)`,指望布尔短路跳过后面的比较。实测证明
这条路走不通——合思引擎**逐叶子求值再合并**:
| 输入 | 条件 | 结果 |
|---|---|---|
| 列表为空 | `AND(列表 notNull, 额度 lt 0)` | FAILED,仍是同一个类型错误 |
| 额度 = -5 | 同上 | COMPLETED |
也就是说同一个条件节点里加「不为空」判断**完全无效**,null 比较照样会被算出来。
唯一可靠的是流程控制短路:外层套一个条件节点判「列表不为空」,把原节点放进它的
满足分支——列表为空时外层为 false,内层根本不被求值。
同时确认了判空本身的可用性:`notNull` 对空数组 `[]` 返回 false、对非空数组返回
true,且不需要 `value` 字段;条件为 false 时节点 COMPLETED、子节点跳过、流程继续。
## 实现
新增 `analysis/condition-null-safety.ts`:
- `readChoosePath()` 解析 `@{choose('{...}')}` 描述符,兼容前端多一个右花括号的形态,
取出取值链的根节点与 `properties` 层级;
- `goesThroughList()` 判定取值链是否「穿过 `DATA_LINK_LIST` 之后还有层级」——
单层字段取值和「只判列表本身」都不算风险;
- `hasAncestorListGuard()` 沿祖先链找对同一列表做过 `notNull` 判断的条件节点;
- 命中「有序比较(`gt/gte/lt/lte`)+ 穿过列表 + 无祖先守卫」时产出
`CONDITION_NULL_UNSAFE_COMPARE` 软提示,消息里明确否掉「用 AND 兜住」这个直觉解法,
并给出守卫上提的正确写法。
`analyzer.ts` 的遍历新增祖先链参数,在条件节点诊断段之后调用该检查。
## 为什么是软建议而不是硬门禁
静态分析无法确定上游列表一定为空(例如上游刚创建、或业务上保证有数据),硬阻断会
让合法流程无法发布。因此只提示、不改写配置、不推断数据非空。`eq`/`neq` 也不纳入
检查——null 与值做相等比较返回的是布尔,不会中断。
## 测试
新增 14 个用例:路径解析(含多花括号形态)、穿过列表判定、无守卫告警、同节点内
AND 组合仍告警、外层守卫不告警、守卫判断的是别的列表仍告警、单层取值不告警、
只判列表不告警、`gt/gte/lte` 覆盖、`eq` 不告警、同一路径去重。
另外用真实流程做了前后对照:改造前(无守卫)命中 2 条,加上守卫后命中 0 条。
`bun test` 1082 pass / 0 fail;`bun run check` 零错误;`check:readme` 通过。
195 lines
7.8 KiB
TypeScript
195 lines
7.8 KiB
TypeScript
/**
|
||
* 条件节点的空值安全校验(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<string, unknown>;
|
||
try {
|
||
const parsed: unknown = JSON.parse(normalized);
|
||
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
|
||
payload = parsed as Record<string, unknown>;
|
||
} 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<string, unknown>;
|
||
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<string>();
|
||
|
||
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 比较照样会被算出来。` +
|
||
`请在该节点外层再套一个条件节点,先判断「列表不为空」,把本节点放进它的满足分支。`,
|
||
});
|
||
}
|
||
}
|