442 lines
22 KiB
Python
442 lines
22 KiB
Python
#!/usr/bin/env python3
|
||
"""每条流程的回头线都得说清楚:这一族「点退回却走到下一步」的问题不能只修一条流程。
|
||
|
||
PYTHONPATH=. python3 scripts/check_process_cycles.py
|
||
|
||
起因是费用报销那条「财务经理 --驳回--> 发起人」:图遍历把它当成一条普通的入口边,
|
||
于是整条下游都成了部门主管的"上游",退回候选按最近到达排序,点确定就把单子往前送。
|
||
修一处不够——同一个口径散在五个地方(退回候选、撤回收回哪一段、网关配对、审批人前瞻、
|
||
试跑预测),只要有一处还把回头线当正常出口,下一张画了驳回线的流程就会再犯一次。
|
||
|
||
这里把口径钉成三件事:
|
||
|
||
1. **一份定义**:``definition.BACK_DECISIONS``(退回 / 拒绝)是唯一的词表,
|
||
``upstream_ids`` / ``downstream_ids`` / ``forward_routes`` 与引擎的
|
||
``upstream_activities`` / ``downstream_activities``、``lookahead._routes`` 都走它;
|
||
2. **设计期就拦**:闭了环却没标语义的那条线、以及标了退回却指着前方那条线,
|
||
发布校验当场说清楚,别等运行时;
|
||
3. **全库扫一遍**:库里每一条流程、每一个人工环节,退回候选都必须落在自己前面,
|
||
而且不能同时是"往前走能到的环节"。
|
||
|
||
第四件是展示层:审批记录里那枚角标与流转轨迹图上的「退回」记号都跟着这份判据走。
|
||
绿底「驳回」和刚驳回过却整环画绿的轨迹图,都是同一件事在两处各判一遍判出来的。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import ast
|
||
import collections
|
||
import pathlib
|
||
import types
|
||
from datetime import datetime
|
||
|
||
from sqlalchemy import select
|
||
|
||
from app import models
|
||
from app.db import SessionLocal
|
||
from app.engine import definition as definition_module
|
||
from app.engine import engine, lookahead
|
||
from app.service import flowmap
|
||
from app.service import simulator
|
||
|
||
FAILS: list[str] = []
|
||
PASSED = 0
|
||
|
||
#: 静态扫库时替 goback_candidates 兜住的那张"假单据":它只读这三格
|
||
STAND_IN = types.SimpleNamespace(id="", work_create_type="surface", creator_identity="__audit__")
|
||
|
||
|
||
def check(label: str, condition, detail: str = "") -> None:
|
||
global PASSED
|
||
if condition:
|
||
PASSED += 1
|
||
print(f"[通过] {label}")
|
||
else:
|
||
FAILS.append(label)
|
||
print(f"[FAIL] {label}" + (f" -> {detail}" if detail else ""))
|
||
|
||
|
||
def design(nodes: list[dict], links: list[dict], pid: str) -> definition_module.ProcessDefinition:
|
||
"""内存里的环节 / 路由 -> 流程定义(``build`` 只读列,不落库)。"""
|
||
process = models.Process(id=pid, name=f"回头线校验流程 {pid}")
|
||
rows = [
|
||
models.Activity(id=item["id"], process=pid, type=item["type"], name=item["name"],
|
||
alias=item["name"], config=item.get("config") or {},
|
||
# 坐标的默认值要等落库才补,内存里的行是 None;轨迹图那几项会读它
|
||
x=float(index) * 240.0, y=0.0)
|
||
for index, item in enumerate(nodes)
|
||
]
|
||
routes = [
|
||
models.Route(id=item["id"], process=pid, name=item["name"], alias=item["name"],
|
||
from_activity=item["from"], to_activity=item["to"],
|
||
decision=item.get("decision", ""), order_number=item.get("order", 0))
|
||
for item in links
|
||
]
|
||
return definition_module.build(process, rows, routes)
|
||
|
||
|
||
def node(ident: str, name: str, kind: str = "manual", config: dict | None = None) -> dict:
|
||
return {"id": ident, "name": name, "type": kind, "config": config or {}}
|
||
|
||
|
||
def link(ident: str, src: str, name: str, dst: str, decision: str = "", order: int = 0) -> dict:
|
||
return {"id": ident, "from": src, "name": name, "to": dst, "decision": decision, "order": order}
|
||
|
||
|
||
def back_lines(d: definition_module.ProcessDefinition) -> list[str]:
|
||
return [
|
||
f"{d.activity(r.from_activity).name}-{r.name}->{d.activity(r.to_activity).name}"
|
||
for r in d.routes if definition_module.is_back_route(d, r)
|
||
]
|
||
|
||
|
||
# ----------------------------------------------------------------- 1. 一份口径
|
||
|
||
def check_shared_vocabulary() -> None:
|
||
check("回头线的词表只有退回与拒绝两种语义",
|
||
set(definition_module.BACK_DECISIONS) == {"goBack", "reject"},
|
||
str(sorted(definition_module.BACK_DECISIONS)))
|
||
check("引擎与定义层用的是同一个判定(不是各写一份)",
|
||
engine.is_back_route is definition_module.is_back_route)
|
||
|
||
loop = design(
|
||
[node("begin", "发起人", "begin"), node("dept", "部门主管"), node("fin", "财务经理"),
|
||
node("end", "归档", "end")],
|
||
[link("r1", "begin", "提交", "dept"), link("r2", "dept", "同意", "fin", "agree"),
|
||
link("r3", "fin", "同意", "end"), link("r4", "fin", "驳回", "begin", "reject")],
|
||
"vocab",
|
||
)
|
||
forward = {r.name for r in definition_module.forward_routes(loop, "fin")}
|
||
looked = {getattr(r, "name", "") for r in lookahead._routes(loop, loop.activity("fin"))}
|
||
# 试跑预测里的那一支:_forward 只读 definition 与 flow,拿个壳子就能真调
|
||
probe = types.SimpleNamespace(definition=loop, flow=engine)
|
||
probe._decision = lambda node, route: simulator._Simulator._decision(probe, node, route) # type: ignore[attr-defined]
|
||
walked = {r.name for r in simulator._Simulator._forward(probe, loop.activity("fin"))}
|
||
check("试跑/前瞻与图遍历剪掉的是同一批出口(驳回线不参与「后面还会走到哪」)",
|
||
forward == looked == walked == {"同意"}, f"{forward} / {looked} / {walked}")
|
||
check("环节上的 routeDecisions 覆盖也算回头线",
|
||
definition_module.is_back_route(
|
||
design(
|
||
[node("begin", "发起人", "begin"), node("a", "A"), node("b", "B", config={"routeDecisions": {"打回": "reject"}}),
|
||
node("end", "结束", "end")],
|
||
[link("x1", "begin", "提交", "a"), link("x2", "a", "同意", "b"),
|
||
link("x3", "b", "打回", "a"), link("x4", "b", "同意", "end")],
|
||
"cover",
|
||
),
|
||
definition_module.RouteNode(id="x3", name="打回", alias="打回", from_activity="b", to_activity="a"),
|
||
))
|
||
|
||
# 撤回要收回的那一段:回头线尽头不是自己被带出去的地方
|
||
check("驳回线尽头不算「我这一步带出去的那一段」(撤回不会连发起人的补正待办一起收)",
|
||
definition_module.downstream_ids(loop, "dept") == {"fin", "end"}
|
||
and engine.downstream_activities(loop, "dept") == {"fin", "end"},
|
||
str(definition_module.downstream_ids(loop, "dept")))
|
||
check("上游与下游不再重叠(环只由标了语义的回头线构成)",
|
||
not (definition_module.upstream_ids(loop, "dept") & definition_module.downstream_ids(loop, "dept")),
|
||
str(definition_module.upstream_ids(loop, "dept")))
|
||
|
||
|
||
# ------------------------------------------------------------- 2. 设计期就拦下来
|
||
|
||
def check_validate() -> None:
|
||
nodes = [node("begin", "发起人", "begin"), node("dept", "部门主管"), node("fin", "财务经理"),
|
||
node("end", "归档", "end")]
|
||
unmarked = design(
|
||
nodes,
|
||
[link("a1", "begin", "提交", "dept"), link("a2", "dept", "同意", "fin"),
|
||
link("a3", "fin", "同意", "end"), link("a4", "fin", "退回修改", "dept")],
|
||
"unmarked",
|
||
)
|
||
problems = [p for p in definition_module.validate(unmarked) if "路由动作" in p]
|
||
check("闭环却没标语义的线,发布校验当场点名",
|
||
len(problems) == 1 and "退回修改" in problems[0] and "财务经理" in problems[0],
|
||
str(problems))
|
||
|
||
marked = design(
|
||
nodes,
|
||
[link("a1", "begin", "提交", "dept"), link("a2", "dept", "同意", "fin"),
|
||
link("a3", "fin", "同意", "end"), link("a4", "fin", "退回修改", "dept", "goBack")],
|
||
"marked",
|
||
)
|
||
check("同一条线标上退回之后,校验不再报错(拦的是没说清楚,不是不许画回头线)",
|
||
not [p for p in definition_module.validate(marked) if "路由动作" in p or "画布这条线" in p],
|
||
str(definition_module.validate(marked)))
|
||
|
||
# 真忘了标也不能又把单子推到下一步:图遍历自己认得出闭环边(is_back_route 的兜底)
|
||
check("没标语义也拦得住运行时:闭环那条边不算上游,退回候选里不会有下一步",
|
||
definition_module.upstream_ids(unmarked, "dept") == {"begin"}
|
||
and "fin" not in definition_module.upstream_ids(unmarked, "dept"),
|
||
str(definition_module.upstream_ids(unmarked, "dept")))
|
||
check("没标语义的那条线也不参与前瞻与预测(往前走完的出口里没有它)",
|
||
[r.name for r in definition_module.forward_routes(unmarked, "fin")] == ["同意"],
|
||
str([r.name for r in definition_module.forward_routes(unmarked, "fin")]))
|
||
|
||
forward_marked = design(
|
||
nodes,
|
||
[link("a1", "begin", "提交", "dept"), link("a2", "dept", "同意", "fin"),
|
||
# 「退回」却画到归档:引擎按退回处理,落点是上游环节,这条线的终点运行时走不到
|
||
link("a3", "dept", "退回", "end", "goBack"), link("a4", "fin", "同意", "end"),
|
||
link("a5", "dept", "同意", "fin")],
|
||
"wrongmark",
|
||
)
|
||
wrong = [p for p in definition_module.validate(forward_marked) if "画布这条线" in p]
|
||
check("标了退回却把线画到前面去,校验说清运行时不会走到那里",
|
||
len(wrong) == 1 and "归档" in wrong[0], str(wrong))
|
||
|
||
loop = design(
|
||
nodes + [node("fill", "补正")],
|
||
[link("b1", "begin", "提交", "fill"), link("b2", "fill", "提交", "dept"),
|
||
link("b3", "dept", "同意", "fin"), link("b4", "fin", "同意", "end"),
|
||
link("b5", "fin", "退回补正", "fill", "goBack")],
|
||
"fillloop",
|
||
)
|
||
check("真正退回给补正环节的那条线不误伤(标了退回、终点确实在上游)",
|
||
not [p for p in definition_module.validate(loop) if "路由动作" in p or "画布这条线" in p],
|
||
str(definition_module.validate(loop)))
|
||
|
||
|
||
# ------------------------------------------------------- 3. 网关配对不被回头线带偏
|
||
|
||
def check_gateway_pairing() -> None:
|
||
"""两条分支都只经一条驳回线才够到汇聚环:那对的不是这个分叉的收尾。"""
|
||
d = design(
|
||
[node("begin", "发起人", "begin"), node("p", "并分", "parallel"), node("a1", "分支一"),
|
||
node("x1", "分支一后续"), node("a2", "分支二"), node("x2", "分支二后续"),
|
||
node("m", "汇聚", "merge"), node("end", "归档", "end")],
|
||
[link("c1", "begin", "提交", "p"), link("c2", "p", "分支一", "a1"), link("c3", "p", "分支二", "a2"),
|
||
link("c4", "a1", "同意", "x1"), link("c5", "x1", "驳回", "m", "reject"),
|
||
link("c6", "a2", "同意", "x2"), link("c7", "x2", "驳回", "m", "reject"),
|
||
link("c8", "m", "同意", "end")],
|
||
"gateway",
|
||
)
|
||
paired = engine.pair_gateways(d).get("p", {})
|
||
check("分支绕出去再经驳回线拐回来的汇聚环,不当成分叉的收尾",
|
||
paired.get("merge", "") == "", str(paired))
|
||
|
||
honest = design(
|
||
[node("begin", "发起人", "begin"), node("p", "并分", "parallel"), node("a1", "分支一"),
|
||
node("a2", "分支二"), node("m", "汇聚", "merge"), node("end", "归档", "end")],
|
||
[link("d1", "begin", "提交", "p"), link("d2", "p", "分支一", "a1"), link("d3", "p", "分支二", "a2"),
|
||
link("d4", "a1", "同意", "m"), link("d5", "a2", "同意", "m"), link("d6", "m", "同意", "end"),
|
||
# 汇聚之后还有一条驳回线,配对不受影响
|
||
link("d7", "m", "驳回", "p", "reject")],
|
||
"gateway_ok",
|
||
)
|
||
check("正常配对的并联照常找到汇聚环(没把筛子做过头)",
|
||
engine.pair_gateways(honest).get("p", {}).get("merge") == "m",
|
||
str(engine.pair_gateways(honest).get("p")))
|
||
|
||
|
||
# ---------------------------------------------------- 4. 展示层也认这条回头线
|
||
|
||
|
||
def _log(kind: str, origin: str, name: str, dest: str, dest_name: str, line: str, when: str):
|
||
"""一条流转日志的最小够用形状:``flowmap.build`` 只读这几格(时刻按引擎入参给 datetime)。"""
|
||
return types.SimpleNamespace(
|
||
type=kind,
|
||
from_activity=origin,
|
||
from_activity_name=name,
|
||
from_opinion="",
|
||
from_identity_name="",
|
||
from_time=datetime.fromisoformat(when),
|
||
arrived_activity=dest,
|
||
arrived_activity_name=dest_name,
|
||
arrived_activity_type="manual",
|
||
arrived_time=datetime.fromisoformat(when),
|
||
route=line,
|
||
)
|
||
|
||
|
||
def _walked() -> list:
|
||
"""发起人提交 → 部门主管批到财务经理 → 财务经理按驳回线打回发起人。"""
|
||
return [
|
||
_log("normal", "begin", "发起人", "dept", "部门主管", "r1", "2026-10-26 14:01:00"),
|
||
_log("normal", "dept", "部门主管", "fin", "财务经理", "r2", "2026-10-26 14:05:00"),
|
||
_log("normal", "fin", "财务经理", "begin", "发起人", "r4", "2026-10-26 14:06:00"),
|
||
]
|
||
|
||
|
||
def check_trajectory_marks_back_lines() -> None:
|
||
"""标成「拒绝 / reject」的驳回线,在轨迹图上也得把这一环标成退回。
|
||
|
||
引擎按普通流转记它(日志类型是 normal),从前只认 ``goBack`` 的轨迹图于是把刚驳回过
|
||
的环节画成绿色「已完成」,跟同一张单子在审批记录里那枚红底驳回对不上。
|
||
"""
|
||
loop = design(
|
||
[node("begin", "发起人", "begin"), node("dept", "部门主管"), node("fin", "财务经理"),
|
||
node("end", "归档", "end")],
|
||
[link("r1", "begin", "提交", "dept"), link("r2", "dept", "同意", "fin", "agree"),
|
||
link("r3", "fin", "同意", "end"), link("r4", "fin", "驳回", "begin", "reject")],
|
||
"traj",
|
||
)
|
||
row = types.SimpleNamespace(id="w1", status="running", activity="begin",
|
||
start_time=datetime.fromisoformat("2026-10-26 14:00:00"))
|
||
trail = flowmap.build(loop, row, tasks=[], logs=_walked())
|
||
fin = trail["nodes"]["fin"]
|
||
check("驳回线走过的那一环在轨迹图上是退回,不是已完成",
|
||
fin["state"] == flowmap.STATE_REJECTED and fin["rejected"], fin["state"])
|
||
check("退回落到哪儿写在环节上", fin["go_back_to"] == "发起人", fin["go_back_to"])
|
||
check("驳回那条线画成回头线(与点退回同一画法)", trail["edges"]["r4"]["back"] is True)
|
||
check("往前走的那条同意线不受影响",
|
||
trail["nodes"]["dept"]["state"] == flowmap.STATE_DONE
|
||
and not trail["nodes"]["dept"]["rejected"]
|
||
and trail["edges"]["r2"]["back"] is False,
|
||
str(trail["nodes"]["dept"]["state"]))
|
||
|
||
# 驳回之后重新提交、再批回同一环:这一环按这一轮上色,退回只留一个历史记号
|
||
again = flowmap.build(
|
||
loop,
|
||
types.SimpleNamespace(id="w1", status="running", activity="fin",
|
||
start_time=datetime.fromisoformat("2026-10-26 14:00:00")),
|
||
tasks=[],
|
||
logs=[
|
||
*_walked(),
|
||
_log("normal", "begin", "发起人", "dept", "部门主管", "r1", "2026-10-26 14:10:00"),
|
||
_log("normal", "dept", "部门主管", "fin", "财务经理", "r2", "2026-10-26 14:12:00"),
|
||
],
|
||
)
|
||
rewaked = again["nodes"]["fin"]
|
||
check("重新走过之后这一环照这一轮上色,只留「退回过」的记号",
|
||
rewaked["state"] == flowmap.STATE_DONE and rewaked["had_reject"], rewaked["state"])
|
||
|
||
|
||
# ------------------------------------------------------------ 5. 全库每一条流程
|
||
|
||
def _literal(node) -> str:
|
||
"""把字面量抠成一个可比较的名字:``"dept"`` 与 ``b("dept")`` 都算 ``dept``。"""
|
||
if node is None:
|
||
return ""
|
||
if isinstance(node, ast.Constant):
|
||
return str(node.value)
|
||
if isinstance(node, ast.Call) and node.args and isinstance(node.args[0], ast.Constant):
|
||
return str(node.args[0].value)
|
||
return ""
|
||
|
||
|
||
def _unmarked_back_edges(graph: dict) -> list[tuple[str, str, str, str]]:
|
||
"""这张画布上"闭了环却没标语义"的边:先摘掉标了退回/拒绝的,再深度优先找闭环边。"""
|
||
forward = {
|
||
node_id: [edge for edge in edges if edge[2] not in definition_module.BACK_DECISIONS]
|
||
for node_id, edges in graph.items()
|
||
}
|
||
state: dict[str, int] = {}
|
||
hits: list[tuple[str, str, str, str]] = []
|
||
for start in list(forward):
|
||
if state.get(start):
|
||
continue
|
||
state[start] = 1
|
||
path: list[list] = [[start, 0]]
|
||
while path:
|
||
frame = path[-1]
|
||
edges = forward.get(frame[0], [])
|
||
if frame[1] >= len(edges):
|
||
state[frame[0]] = 2
|
||
path.pop()
|
||
continue
|
||
src, dst, decision, name = edges[frame[1]]
|
||
frame[1] += 1
|
||
if state.get(dst) == 1:
|
||
hits.append((src, dst, decision, name))
|
||
elif not state.get(dst) and dst in forward:
|
||
state[dst] = 1
|
||
path.append([dst, 0])
|
||
return [edge for edge in hits if not edge[2]]
|
||
|
||
|
||
def check_design_builders() -> None:
|
||
"""演示 / 种子流程那些"按图纸建流程"的脚本,画布也得守同一条规矩。
|
||
|
||
这些脚本直接 POST 设计再点发布,将来谁拿它们当图纸抄,回头线没标语义就会把
|
||
同一个坑再踩一遍。这里不回库、不发请求,只把脚本里写的路由字面量抠出来,
|
||
按同一套 DFS 看有没有闭了环却没标语义的线。
|
||
"""
|
||
root = pathlib.Path(__file__).resolve().parent
|
||
builders = [p for p in sorted(root.rglob("*.py")) if "/publish" in p.read_text(encoding="utf-8")]
|
||
checked = 0
|
||
for path in builders:
|
||
tree = ast.parse(path.read_text(encoding="utf-8"))
|
||
functions = [node for node in tree.body if isinstance(node, ast.FunctionDef)]
|
||
# 函数体里的路由归各自那份图纸;模块顶层再走一遍时把它们跳过,省得一条线算两次
|
||
nested = {id(item) for fn in functions for item in ast.walk(fn)}
|
||
scopes = [(f"函数 {fn.name}", fn) for fn in functions] + [("模块顶层", tree)]
|
||
for where, scope in scopes:
|
||
graph: dict = collections.defaultdict(list)
|
||
for call in ast.walk(scope):
|
||
if not isinstance(call, ast.Call):
|
||
continue
|
||
if scope is tree and id(call) in nested:
|
||
continue
|
||
func_name = call.func.id if isinstance(call.func, ast.Name) else getattr(call.func, "attr", "")
|
||
kwargs = {kw.arg: kw.value for kw in call.keywords if kw.arg}
|
||
src = dst = None
|
||
decision = ""
|
||
if func_name in {"rt", "route", "add_route"} and len(call.args) >= 3:
|
||
src, dst = _literal(call.args[1]), _literal(call.args[2])
|
||
if "decision" in kwargs:
|
||
decision = _literal(kwargs["decision"])
|
||
elif len(call.args) >= 5:
|
||
decision = _literal(call.args[4])
|
||
elif func_name == "Route" and {"from_activity", "to_activity"} <= set(kwargs):
|
||
src, dst = _literal(kwargs["from_activity"]), _literal(kwargs["to_activity"])
|
||
decision = _literal(kwargs.get("decision"))
|
||
if not src or not dst:
|
||
continue
|
||
graph[src].append((src, dst, decision, _literal(call.args[0]) if call.args else ""))
|
||
graph.setdefault(dst, [])
|
||
hits = [f"{s} -{name or '未命名'}-> {d}" for s, d, _decision, name in _unmarked_back_edges(graph)]
|
||
if not graph:
|
||
continue
|
||
checked += 1
|
||
check(f"{path.relative_to(root.parent)}({where})的画布没有没标语义的回头线",
|
||
not hits, ";".join(hits[:3]))
|
||
print(f" 扫过 {checked} 份按图纸建流程的脚本")
|
||
|
||
|
||
def check_every_process(db) -> None:
|
||
rows = list(db.execute(select(models.Process)).scalars().all())
|
||
check("库里能读到流程定义", bool(rows), "一条流程都没有")
|
||
cycles = 0
|
||
for process in rows:
|
||
d = definition_module.load(db, process.id)
|
||
if not d.activities:
|
||
continue
|
||
lines = back_lines(d)
|
||
cycles += len(lines)
|
||
problems = definition_module.validate(d)
|
||
check(f"「{process.name}」的回头线在设计期挑不出毛病", not problems, ";".join(problems[:3]))
|
||
for item in d.manual_activities:
|
||
upstream = definition_module.upstream_ids(d, item.id)
|
||
downstream = definition_module.downstream_ids(d, item.id)
|
||
candidates = [n.id for n in engine.goback_candidates(db, STAND_IN, d, item)]
|
||
bad = [n for n in candidates if n in downstream]
|
||
check(f"「{process.name} / {item.name}」的退回候选都落在自己前面",
|
||
not bad and all(c in upstream for c in candidates),
|
||
f"候选={[d.activity(c).name for c in candidates]} 前方={[d.activity(n).name for n in bad]}")
|
||
print(f" 扫过 {len(rows)} 条流程,回头线 {cycles} 条")
|
||
|
||
|
||
def main() -> int:
|
||
check_shared_vocabulary()
|
||
check_validate()
|
||
check_gateway_pairing()
|
||
check_trajectory_marks_back_lines()
|
||
check_design_builders()
|
||
db = SessionLocal()
|
||
try:
|
||
check_every_process(db)
|
||
finally:
|
||
db.close()
|
||
|
||
print()
|
||
if FAILS:
|
||
print(f"{len(FAILS)} 条没对上:")
|
||
for item in FAILS:
|
||
print(f" - {item}")
|
||
else:
|
||
print(f"全部 {PASSED} 条对上。")
|
||
return 1 if FAILS else 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|