三面拆分⑫:网关去节点化 —— 分支即条件出边,汇合即多入边异或

模型(与 Mastra .branch 同构,条件挂在步与步之间):
- routing.selectEdges:多出边一律异或,不分节点种类 —— 条件命中优先(按声明序),
  无条件边作默认兜底,恰取一条;单出边直通保留(历史并行网关的遗留通道)
- closure:含网关即 gateway-deprecated(历史版本仅可读、不可激活);
  「隐式合并」禁令删除(多入边=异或汇合,上游互斥无歧义);
  新规:多出边节点的无条件边至多一条,超出报 multi-default-edge
- contracts:CLOSURE_VIOLATION_KINDS 去 gateway-unbalanced/join-ambiguous-source,
  进 multi-default-edge/gateway-deprecated(下游零引用,api-report 随行)

种子(按用户要求:只留一条流一个版本,图/节点/边全部对账):
- MVP_PACKAGE 重写:10 节点 14 边零网关 —— G1/G2 展开为 A1 的三条出边
  (选型/直接寻源/默认直接定价)+ A2 的两条,G2J/G3 整颗删除,
  A5 审批点的两个去向就是自己的两条 verdict 条件出边(驳回边与 reject 声明字字对齐)
- 手绘图:顶车道 y30 补画 A1→A3「直接寻源」—— 画布保存只认画出来的边,
  不画这条语义边会在首次存盘被静默丢掉(新增图边对账测试卡死这类漏画);
  「供应商底价表」标注钉进 y190 车道空档,不再被卡点压住
- 开发库外科手术:清运行数据 + 删 v1–v4,重种为唯一生效版 v1(闭合过闸)

UI 与文案同步:
- 元素盘/追加盘不再有网关件(画布仍认得历史图);图例删「判断/并行网关」「菱形=网关」
- 新建流程补激活一跳(保存不生效模型下,不激活「建出来就能起单」就是谎言)
- 总览四种编排要素、「画图不检查,激活才查完整」贯穿 blueprint/memory/process 文案

测试:closure 43 通过(gateway-deprecated + multi-default-edge 两用例),
seed-mvp-flow 8 通过(含图边对账),端到端八例真库全绿(网关从不建实例,断言原样存活);
五闸 typecheck/lint/depcruise/api:check/test 全过(退出码验)。

债:M5-4 AND 并行待「并行组」声明(Mastra .parallel);M5-5 网关运行时兼容三处,
含网关在途流清零后随 schema 一并摘除。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KeDBx2PHLavbURXB8LJJ7m
This commit is contained in:
2026-07-28 17:56:20 +08:00
co-authored by Claude Fable 5
parent 7a065a50e0
commit 6854524784
17 changed files with 349 additions and 387 deletions
@@ -1,135 +1,109 @@
/**
* 流程包 —— 守四件事:拓扑齐、**审批点后面跟网关且两个去向都真的走**、
* 网关条件读得到判据、回边标了 reopen。
* 流程包(无网关版)—— 守四件事:拓扑齐且零网关、分支即条件出边、
* 审批点两个去向都由图表达、回边标了 reopen。
*/
import { describe, expect, it } from 'vitest';
import { MVP_PACKAGE } from '../seed-mvp-flow.ts';
describe('MVP_PACKAGE', () => {
it('15 语义节点 · 18 边 · 五网关一审批点双终点', () => {
expect(MVP_PACKAGE.nodes).toHaveLength(15);
expect(MVP_PACKAGE.edges).toHaveLength(18);
expect(MVP_PACKAGE.nodes.filter((n) => n.kind === 'end')).toHaveLength(2);
expect(MVP_PACKAGE.nodes.filter((n) => n.kind === 'gateway')).toHaveLength(5);
it('10 语义节点 · 14 边 · 零网关 · 一审批点双终点', () => {
expect(MVP_PACKAGE.nodes).toHaveLength(10);
expect(MVP_PACKAGE.edges).toHaveLength(14);
expect(MVP_PACKAGE.nodes.filter((n) => n.kind === 'gateway')).toHaveLength(0);
expect(MVP_PACKAGE.nodes.filter((n) => n.kind === 'approval')).toHaveLength(1);
});
it('分叉必有汇合:G2 的两条互斥分支并回 G2J —— 不给汇合点,A4 会被判隐式合并', () => {
const g2j = MVP_PACKAGE.nodes.find((n) => n.id === 'G2J');
expect(g2j?.kind).toBe('gateway');
if (g2j?.kind !== 'gateway') return;
expect(g2j.gateway).toBe('exclusive');
expect(g2j.mode).toBe('join');
expect(MVP_PACKAGE.edges.filter((e) => e.to === 'G2J')).toHaveLength(2);
/*
* A4 的**非回边**入边只剩一条 —— 这是发布闸能过的关键。
* A4 一共三条入边(G2J 正常来、G3 驳回、G4 有异议),后两条都是 reopen;
* 闭合校验的「汇合同名字段来源不明」只看并发入边(回边是重新入场、不参与判定),
* 所以 concurrent 长度 1、不触发。任一条回边漏标 reopen,发布立刻被拦。
*/
expect(MVP_PACKAGE.edges.filter((e) => e.to === 'A4' && !e.reopen)).toHaveLength(1);
expect(MVP_PACKAGE.edges.filter((e) => e.to === 'A4')).toHaveLength(3);
expect(MVP_PACKAGE.nodes.filter((n) => n.kind === 'end')).toHaveLength(2);
});
/*
* 审批点后面必然跟一个网关(BPMN 的常规)。
*
* 这条曾经不成立:G3 被我做掉了,通过走 A5 的唯一出边、驳回藏在 `reject` 声明里,
* 图上只有一枚牌子。补回 G3 时还有个坑 —— 若引擎不把裁决放进载荷,
* G3 的条件就没东西可读,只能在边上喂一枚常量 `verdict: "pass"` 骗过它;
* 那样条件恒真、驳回边永不可达,网关成了摆设。所以这里连**没有常量**一起守。
* 分支 = 源节点的多条条件出边,恰取一条;无条件边作默认兜底,至多一条。
* 原 G1/G2 合并展开成 A1 的三条出边:选型 / 直接寻源(复合条件)/ 直接定价(默认)。
*/
it('A5 后面跟着 G3,两个去向都由图表达,且判据不是喂进去的常量', () => {
it('A1 三条出边:两条带条件 + 一条默认兜底 —— 原 G1+G2 的展开', () => {
const outs = MVP_PACKAGE.edges.filter((e) => e.from === 'A1');
expect(outs.map((e) => `${e.to}:${e.condition ?? '(默认)'}`).sort()).toEqual([
'A2:needAlternative = true',
'A3:needAlternative = false and needSourcing = true',
'A4:(默认)',
]);
});
it('A5 审批点的两个去向就是它自己的两条条件出边;驳回边与 reject 声明字字对齐', () => {
const a5 = MVP_PACKAGE.nodes.find((n) => n.id === 'A5');
expect(a5?.kind).toBe('approval');
if (a5?.kind !== 'approval') return;
expect(a5.approver.position).toBe('PM');
if (a5?.kind !== 'approval' || a5.reject.kind !== 'reopen') return;
// A5 的唯一出边指向网关,而不是直接指向下一个活动
const outOfA5 = MVP_PACKAGE.edges.filter((e) => e.from === 'A5');
expect(outOfA5).toHaveLength(1);
expect(outOfA5[0]?.to).toBe('G3');
// 这条边不许带映射:判据由引擎在裁决时放进载荷,不是边喂的常量
expect(outOfA5[0]?.mapping).toEqual([]);
const g3 = MVP_PACKAGE.nodes.find((n) => n.id === 'G3');
expect(g3?.kind).toBe('gateway');
if (g3?.kind !== 'gateway') return;
expect(g3.gateway).toBe('exclusive');
expect(g3.mode).toBe('fork');
const outOfG3 = MVP_PACKAGE.edges.filter((e) => e.from === 'G3');
expect(outOfG3).toHaveLength(2);
// 异或分叉的每条出边都要带条件 —— 没有无条件兜底边可用
for (const e of outOfG3) expect(e.condition, `${e.id} 缺条件`).toBeDefined();
expect(outOfG3.map((e) => `${e.to}:${e.condition ?? ''}`).sort()).toEqual([
const outs = MVP_PACKAGE.edges.filter((e) => e.from === 'A5');
expect(outs.map((e) => `${e.to}:${e.condition ?? ''}`).sort()).toEqual([
'A4:verdict = "reject"',
'A6:verdict = "pass"',
]);
const rejectEdge = outs.find((e) => e.to === 'A4')!;
expect(rejectEdge.reopen).toBe(true);
expect(rejectEdge.to).toBe(a5.reject.nodeId);
expect(rejectEdge.mapping).toEqual(a5.reject.mapping);
});
it('G3 的驳回出边与 A5 的 reject 声明字字对齐 —— 引擎先看图,没画边的包才退回声明', () => {
const a5 = MVP_PACKAGE.nodes.find((n) => n.id === 'A5');
const edge = MVP_PACKAGE.edges.find((e) => e.id === 'e-g3-a4');
expect(a5?.kind).toBe('approval');
if (a5?.kind !== 'approval' || a5.reject.kind !== 'reopen') return;
expect(edge?.to).toBe(a5.reject.nodeId);
expect(edge?.reopen).toBe(true);
expect(edge?.mapping).toEqual(a5.reject.mapping);
/*
* A4 的多入边是**异或汇合**:上游互斥(A1/A2/A3 的条件路径),只会来一条,
* 同名映射天然无歧义 —— 这正是显式汇合点(G2J)被删掉的原因。
* 回边(A5 驳回 / A6 有异议)不算并发入边。
*/
it('A4 三条正向入边互斥合法,两条回边都标 reopen', () => {
const ins = MVP_PACKAGE.edges.filter((e) => e.to === 'A4');
expect(ins.filter((e) => !e.reopen).map((e) => e.from).sort()).toEqual(['A1', 'A2', 'A3']);
expect(ins.filter((e) => e.reopen).map((e) => e.from).sort()).toEqual(['A5', 'A6']);
});
it('两条回边都标了 reopen:审批驳回与议价有异议,都回 A4 重开', () => {
expect(MVP_PACKAGE.edges.filter((e) => e.reopen).map((e) => `${e.from}→${e.to}`).sort()).toEqual([
'G3→A4',
'G4→A4',
it('A6 三条条件出边 —— outcome 由放行选口给出', () => {
const outs = MVP_PACKAGE.edges.filter((e) => e.from === 'A6');
expect(outs.map((e) => `${e.to}:${e.condition ?? ''}`).sort()).toEqual([
'A4:outcome = "revise"',
'A7:outcome = "lost"',
'end_won:outcome = "won"',
]);
});
it('网关条件按判据写:G1/G2 读旗,G3 读裁决,G4 读 outcome', () => {
const cond = (from: string, to: string) => MVP_PACKAGE.edges.find((e) => e.from === from && e.to === to)?.condition;
expect(cond('G1', 'A2')).toBe('needAlternative = true');
expect(cond('G2', 'G2J')).toBe('needSourcing = false');
expect(cond('G3', 'A6')).toBe('verdict = "pass"');
expect(cond('G4', 'A7')).toBe('outcome = "lost"');
});
it('手绘图存证在包上 —— 15 语义节点 + 两枚卡点', () => {
expect(MVP_PACKAGE.diagram).toBeDefined();
it('手绘图存证:10 语义节点 + 4 卡点(直达两枚是正向边的呈现,回跳两枚照旧)', () => {
const nodes = (MVP_PACKAGE.diagram as { nodes: { id: string; kind: string }[] }).nodes;
expect(nodes.filter((n) => n.kind === 'callout')).toHaveLength(2);
expect(nodes).toHaveLength(17);
// 语义节点在图上一个不少(卡点是呈现,不入语义)
expect(nodes.filter((n) => n.kind === 'callout')).toHaveLength(4);
expect(nodes).toHaveLength(14);
const drawn = new Set(nodes.map((n) => n.id));
for (const n of MVP_PACKAGE.nodes) expect(drawn.has(n.id), `图上缺 ${n.id}`).toBe(true);
});
/*
* 几何自查:重排的收益要能验证,不能只写在注释里。
* 手绘尺寸见 `apps/web/src/shared/gov/flow/flow-layout.ts` 的 MANUAL_SIZES。
* 语义边必须**每条都画在图上**(直连线,或指向带 `to` 卡点的线)——
* 画布保存由画出来的边反推语义边,没画的那条会在第一次存盘时被静默丢掉
* (实测:A1→A3「直接寻源」漏画,mergeGraphIntoPackage 一过就没了)。
*/
it('几何:坐标吸附 10px 珊格,同带内框不重叠', () => {
it('图边对账:每条语义边都有画出来的对应(直连或经卡点)', () => {
const dia = MVP_PACKAGE.diagram as {
nodes: { id: string; kind: string; to?: string }[];
edges: { from: string; to: string }[];
};
const calloutTo = new Map(dia.nodes.filter((n) => n.kind === 'callout').map((n) => [n.id, n.to]));
const drawnPairs = new Set(dia.edges.map((e) => `${e.from}→${calloutTo.get(e.to) ?? e.to}`));
for (const e of MVP_PACKAGE.edges) {
expect(drawnPairs.has(`${e.from}→${e.to}`), `语义边 ${e.id}(${e.from}→${e.to})没画在图上`).toBe(true);
}
});
it('几何:坐标吸附 10px 珊格,框不重叠', () => {
const nodes = (MVP_PACKAGE.diagram as { nodes: { id: string; kind: string; x: number; y: number }[] }).nodes;
const size = (kind: string) =>
kind === 'gateway' ? { w: 40, h: 40 }
: kind === 'callout' ? { w: 100, h: 30 }
: kind === 'activity' ? { w: 100, h: 40 }
: { w: 20, h: 20 };
kind === 'callout' ? { w: 100, h: 30 } : kind === 'activity' ? { w: 100, h: 40 } : { w: 20, h: 20 };
for (const n of nodes) {
expect(n.x % 10, `${n.id} 的 x 未吸附珊格`).toBe(0);
expect(n.y % 10, `${n.id} 的 y 未吸附珊格`).toBe(0);
expect(n.x % 10, `${n.id} x`).toBe(0);
expect(n.y % 10, `${n.id} y`).toBe(0);
}
// 两两不重叠(轴对齐矩形相交判定)
for (let i = 0; i < nodes.length; i += 1) {
for (let j = i + 1; j < nodes.length; j += 1) {
const a = nodes[i]!;
const b = nodes[j]!;
const sa = size(a.kind);
const sb = size(b.kind);
const overlap =
a.x < b.x + sb.w && a.x + sa.w > b.x && a.y < b.y + sb.h && a.y + sa.h > b.y;
expect(overlap, `${a.id} 与 ${b.id} 的框重叠`).toBe(false);
const overlap = a.x < b.x + sb.w && a.x + sa.w > b.x && a.y < b.y + sb.h && a.y + sa.h > b.y;
expect(overlap, `${a.id} 与 ${b.id} 重叠`).toBe(false);
}
}
});