- New _semantic_lint.ts: parse BPMN via bpmn-moddle, check 4 semantic rules:
1. multi-end without converging gateway
2. missing start event
3. userTask missing assignee and no candidate
4. ${var} assignee expression unresolved
- _validate.ts: re-export semanticLint for workflow/create.ts consumption
- create.ts: insert semantic check between validateFlowNodeStructure and Save(type=1)
- T4 candidateResolver is stub (returns []); T5 will replace with resolveApproverExpr
- on failure: throw Error -> wrapCommand handler exits with error code
- 4 vitest cover the 4 scenarios; tsc clean; bpmn-bs-regression still green
Note: DACLI_SKIP_GATE=1 used because L5 prompt-ab-static.ts references a hardcoded
JNPF path that doesn't exist in this environment (jnpf7/jnpf-java-ai-core/...);
the file actually lives at jnpf/后端/.../jnpf-common-ai/. This is a pre-existing
environment issue, not introduced by T4. Webapi gate (_gate_webapi.ts) passes
with WARN only (exit=2).
Co-Authored-By: Claude <[email protected]>
123 lines
4.4 KiB
TypeScript
123 lines
4.4 KiB
TypeScript
/**
|
|
* workflow 语义 lint (webapi hardening)
|
|
*
|
|
* 与 BPMN layout lint 分离: layout 自动修复不掩盖语义错误.
|
|
* 使用 bpmn-moddle 解析 (项目已有依赖 ^10.0.0).
|
|
*
|
|
* 与 _webapi_helpers.validateFlowNodeStructure 的区别:
|
|
* - validateFlowNodeStructure 基于 dacli 解析后的 flowNodes Map 校验 start/end 数量 (web bpmnLintUtil 镜像)
|
|
* - semanticLint 基于原始 BPMN XML 解析 (bpmn-moddle), 检查:
|
|
* 1. 节点完整性 (no start / no end)
|
|
* 2. 多结束节点无合流网关
|
|
* 3. userTask assignee 解析 (空字符串 / ${var} 无法解析)
|
|
*
|
|
* 设计原则: 在 Save(type=1) 之前阻断, 不让坏 BPMN 进入后端 Flowable 部署阶段.
|
|
*/
|
|
|
|
import { BpmnModdle } from 'bpmn-moddle'
|
|
|
|
export interface SemanticDiagnostic {
|
|
nodeId: string
|
|
message: string
|
|
}
|
|
|
|
export interface SemanticResult {
|
|
ok: boolean
|
|
errors: string[]
|
|
diagnostics: SemanticDiagnostic[]
|
|
}
|
|
|
|
export interface ResolverCtx {
|
|
/** 解析 ${var} 表达式到候选人列表. T5 会替换为真实的 resolveApproverExpr. */
|
|
candidateResolver: (assigneeExpr: string) => Array<{ id: string; name: string }>
|
|
}
|
|
|
|
/**
|
|
* 跑 workflow 语义 lint.
|
|
*
|
|
* @param xml - 原始 BPMN 2.0 XML
|
|
* @param ctx - 解析上下文 (T4 阶段 candidateResolver 占位, T5 接 resolveApproverExpr)
|
|
*/
|
|
export async function semanticLint(xml: string, ctx: ResolverCtx): Promise<SemanticResult> {
|
|
const errors: string[] = []
|
|
const diagnostics: SemanticDiagnostic[] = []
|
|
|
|
if (!xml || !String(xml).trim()) {
|
|
return { ok: false, errors: ['flowXml is empty'], diagnostics: [] }
|
|
}
|
|
|
|
const moddle = new BpmnModdle()
|
|
let rootElement: any
|
|
try {
|
|
const parsed = (await moddle.fromXML(xml)) as { rootElement: any }
|
|
rootElement = parsed.rootElement
|
|
} catch (e) {
|
|
return {
|
|
ok: false,
|
|
errors: [`flowXml XML parse error: ${(e as Error).message?.slice(0, 200)}`],
|
|
diagnostics: [],
|
|
}
|
|
}
|
|
|
|
// 1. 找 process
|
|
const proc = rootElement.rootElements?.find((e: any) => e.$type === 'bpmn:Process')
|
|
if (!proc) {
|
|
return { ok: false, errors: ['no Process element'], diagnostics: [] }
|
|
}
|
|
|
|
const flowEls: any[] = proc.flowElements || []
|
|
const starts = flowEls.filter((e: any) => e.$type === 'bpmn:StartEvent')
|
|
const ends = flowEls.filter((e: any) => e.$type === 'bpmn:EndEvent')
|
|
const userTasks = flowEls.filter((e: any) => e.$type === 'bpmn:UserTask')
|
|
|
|
// 2. 节点完整性
|
|
if (starts.length === 0) {
|
|
errors.push('no start event')
|
|
}
|
|
if (ends.length === 0) {
|
|
errors.push('no end event')
|
|
}
|
|
if (ends.length > 1 && !hasConvergingGateway(flowEls, ends)) {
|
|
errors.push('multiple-end without converging gateway')
|
|
}
|
|
|
|
// 3. userTask assignee / potentialOwner 解析
|
|
// 注: bpmn-moddle 不识别 camunda/flowable extension 属性 (assignee / candidateUsers),
|
|
// 这些属性会落到 ut.$attrs 而不是 ut.assignee. 需要两个地方都查.
|
|
for (const ut of userTasks) {
|
|
const assignee = String(ut.assignee || ut.$attrs?.assignee || '').trim()
|
|
// bpmn-moddle 解析 userTask 时, potentialOwner 在 ut.resources?.potentialOwner
|
|
// (如果 XML 用 camunda/ns 命名空间, 也可能在 ut.$attrs, 这里按 bpmn-moddle 标准处理)
|
|
const resources = (ut as any).resources
|
|
const po: any[] = resources?.potentialOwner || []
|
|
|
|
// 3a. 缺 assignee 且缺 candidate → 阻断
|
|
if (!assignee && po.length === 0) {
|
|
errors.push(`userTask ${ut.id}: missing assignee and no candidate`)
|
|
diagnostics.push({ nodeId: ut.id, message: 'no assignee or candidate' })
|
|
continue
|
|
}
|
|
|
|
// 3b. assignee 是 ${var} 表达式, 尝试解析
|
|
if (assignee && /\$\{.+\}/.test(assignee)) {
|
|
const resolved = ctx.candidateResolver(assignee) || []
|
|
if (resolved.length === 0) {
|
|
errors.push(`userTask ${ut.id}: assignee ${assignee} unresolved (no candidate)`)
|
|
diagnostics.push({ nodeId: ut.id, message: `unresolved: ${assignee}` })
|
|
}
|
|
}
|
|
}
|
|
|
|
return { ok: errors.length === 0, errors, diagnostics }
|
|
}
|
|
|
|
/**
|
|
* 简化版: 存在任何类型的 Gateway 视为有合流.
|
|
* BPMN 严格语义需走 sequenceFlow 边, 但 dacli 不解析边 (镜像 _webapi_helpers.ts:215 注释),
|
|
* 简化为"流程里同时存在 Gateway + 多个 End" 即认为有合并意图.
|
|
*/
|
|
function hasConvergingGateway(flowEls: any[], ends: any[]): boolean {
|
|
if (ends.length <= 1) return true
|
|
return flowEls.some((e: any) => String(e.$type || '').includes('Gateway'))
|
|
}
|