forked from ChaiHao/BPOS-MVP
流程链按拓扑序读 —— 图纸坐标不配决定推进次序
任务详情里的「流程链」在 MVP 包上读作 **A1 → A7 → A2 → A6 → A3 → A5 → A4**。
不是随机:`toChain` 按 `layout.col` 排,而 MVP 图是**回形走线**(第 0 行左→右、
第 1 行右→左、第 2 行折回最左),A7 的列位(1)正好跟 A1 并列、A6(3)跟 A2 并列。
原注释还写着「列位是流程的推进次序(拓扑的可视化)」—— 那句话只对 v1 那种一行
从左画到右的包成立,换一张图就是错的。图纸声明为「不影响语义」,那它也不配决定次序。
改为按**边**做拓扑序,新增 `advanceOrder`(process 的 domain,纯函数零 IO):
· 回边(reopen)排除在外 —— 它是重新入场不是往前推进;留着 G4 → A4 就成环,
A4 会等自己的下游,拓扑序无从下手
· 真并行支(G2 分出的 A2 / A3)图上本无先后,按**节点编号**定 ——
同一个包在谁手上都读作同一条链
· 进不了拓扑序的节点(不可达、或排除回边后仍有环)按编号缀在末尾:少排序,不丢节点
实测链已读作 A1 → A2 → A3 → A4 → A5 → A6 → A7(A1 current)。
六条 domain 测试盯住它,第一条就是那张回形图。
顺带修了两处会互相传染的闸:
· `api-report.mjs` 在 tsc 失败那条路径不清 `.api-tmp`,半成品留在盘上,
eslint 扫到生成的 .d.ts 报 1004 条假错 —— 于是 tsc 一红 lint 跟着红。实测踩到。
· eslint 只忽略了 `api-report/**`,把同为产物的 `.api-tmp/**` 一并忽略
契约变更(加法,非破坏):①depcruise 反查六处下游无违规 ②CONTRACT.md 已注明
③快照 api-report/…/process/index.d.ts 仅多一个导出名。
typecheck ✓ lint ✓ depcruise 374 模块 ✓ 487 测试 ✓(以退出码验,不以 echo 验)
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KeDBx2PHLavbURXB8LJJ7m
This commit is contained in:
@@ -141,3 +141,52 @@ export function rejectRouteOf(
|
||||
export function startNodeOf(pkg: ProcessPackage): ProcessNode | undefined {
|
||||
return pkg.nodes.find((n) => n.kind === 'start');
|
||||
}
|
||||
|
||||
/**
|
||||
* 推进次序 —— 按**边**做拓扑序,不看图纸坐标。
|
||||
*
|
||||
* 「链」要回答的是「这一单接下来往哪走」,那是边的事实。曾经拿 `layout.col` 排,
|
||||
* 因为 v1 的包是一行从左画到右,列位恰好等于推进次序 —— 但 MVP 图是**回形走线**
|
||||
* (第 0 行左→右、第 1 行右→左、第 2 行折回最左),A7 的列位跟 A1 并列,链就读成了
|
||||
* A1 → A7 → A2 → A6 → A3(实测)。图纸声明为「不影响语义」,那它也不配决定次序。
|
||||
*
|
||||
* 回边(`reopen`)排除在外:它是**重新入场**,不是往前推进。留着它图上就有环,
|
||||
* 拓扑序无从下手(G4 → A4 会让 A4 等自己的下游)。
|
||||
*
|
||||
* 真并行的分支(G2 分出的 A2 / A3)在图上本无先后,链却必须挑一个顺序:
|
||||
* 按**节点编号**定,于是同样的包在谁手上都读作同一条链。
|
||||
* 进不了拓扑序的节点(不可达、或排除回边后仍有环)一律按编号缀在末尾 —— 少排序,不丢节点。
|
||||
*/
|
||||
export function advanceOrder(pkg: ProcessPackage): readonly ProcessNode[] {
|
||||
const byId = new Map(pkg.nodes.map((n) => [n.id, n]));
|
||||
const byNodeId = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0);
|
||||
|
||||
const forward = pkg.edges.filter((e) => e.reopen !== true && byId.has(e.from) && byId.has(e.to));
|
||||
const indegree = new Map(pkg.nodes.map((n) => [n.id, 0]));
|
||||
const next = new Map<ProcessNode['id'], ProcessNode['id'][]>();
|
||||
for (const e of forward) {
|
||||
indegree.set(e.to, (indegree.get(e.to) ?? 0) + 1);
|
||||
next.set(e.from, [...(next.get(e.from) ?? []), e.to]);
|
||||
}
|
||||
|
||||
// Kahn:每轮从「无未满足前驱」的节点里取编号最小的,顺序才与书写无关
|
||||
const ready = pkg.nodes.filter((n) => indegree.get(n.id) === 0).map((n) => n.id);
|
||||
const ordered: ProcessNode[] = [];
|
||||
const placed = new Set<string>();
|
||||
while (ready.length > 0) {
|
||||
ready.sort(byNodeId);
|
||||
const id = ready.shift()!;
|
||||
if (placed.has(id)) continue;
|
||||
placed.add(id);
|
||||
const node = byId.get(id);
|
||||
if (node !== undefined) ordered.push(node);
|
||||
for (const to of next.get(id) ?? []) {
|
||||
const left = (indegree.get(to) ?? 0) - 1;
|
||||
indegree.set(to, left);
|
||||
if (left === 0) ready.push(to);
|
||||
}
|
||||
}
|
||||
|
||||
const stranded = pkg.nodes.filter((n) => !placed.has(n.id)).sort((a, b) => byNodeId(a.id, b.id));
|
||||
return [...ordered, ...stranded];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user