Files
dacli/src/commands/workflow/_auto_assign_admin.ts
T
sswandClaude 367d90e4d1 fix(dacli): R3 实战 3 bug 修复 (form create 残留/autoRelease 400/workflow --ai 兜底)
- form create --ai 失败留 state=0 残留 (R3 P0)
  - 加 src/commands/form/_cleanup.ts + cleanupResidueForms helper
  - 集成到 wrapCommand 4 个 ctx.fail 路径 (T3 回读失败 / release 重试失败 / 外层 catch)
  - 默认 ON, --no-cleanup-on-fail 关闭
- form create --autoRelease 报 400 (R1 P6.2 + R3 P1)
  - release 失败首次 400 时 GET 一次 formInfo 刷新后端缓存, 再 retry release
  - 现有 retry + 指数 backoff (500/1000/2000ms) 保留, additive safety net
- workflow create --ai lint 失败直接 throw (R1 P5 + R3 P2)
  - 加 src/commands/workflow/_auto_assign_admin.ts + autoAssignAdmin helper
  - semanticLint 失败全为 missing assignee/candidate 类时, 自动注入 admin 重 lint
  - 兜底后仍失败才 throw (非 assignee 类错保持原 throw)
- 2 vitest tests: form_create_cleanup_residue (8 cases), workflow_auto_assign_admin (7 cases)
  - 含端到端 R3 实战场景: 4 userTask 缺 PO → autoAssignAdmin → re-lint OK
- workflow CreateOpts 加 forceLint/keepOnFail 字段 (历史缺失, TS 兜底)
- form CreateOpts/DeleteOpts 加 force 字段 (历史缺失, TS 兜底)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
2026-07-29 17:39:39 +08:00

101 lines
4.5 KiB
TypeScript

/**
* workflow _auto_assign_admin.ts — AI 生成的 BPMN 缺 potentialOwner 自动兜底
*
* Bug 3 (tdd6 R3 2026-07-29 + R1 P5 历史踩坑):
* - 现象: `dacli workflow create --ai` 4 userTask 缺 potentialOwner, semanticLint 报 4 errors
* (msg = 'no assignee or candidate'), 直接 throw, 新流程不建
* - 根因: AI LLM 偶尔只输出 `camunda:assignee` 命名空间属性 (或根本不写), dacli semanticLint
* 检测时落入 'missing assignee and no candidate' 分支阻断 (见 _semantic_lint.ts:110-114)
* - 兜底: 对所有 userTask 检查是否已有 potentialOwner, 没有则注入
* `<bpmn:potentialOwner><bpmn:resourceAssignmentExpression>
* <bpmn:formalExpression>admin</bpmn:formalExpression>
* </bpmn:resourceAssignmentExpression></bpmn:potentialOwner>`
* 已有的保持不变 (避免覆盖用户显式候选人)
* - 集成: 在 workflow/create.ts:415 semanticLint 失败时, 若 errors 全为 'no assignee/candidate'
* 类, 跑 autoAssignAdmin 重写 flowXml, 再跑一次 semanticLint 验证, 通过则继续 save
*
* 设计要点:
* - 纯函数 (无外部依赖除 bpmn-moddle), 单测好覆盖
* - 不破坏既有 potentialOwner, 只补缺失 (additive safety net)
* - 优先 bpmn-moddle 序列化 (canonical `bpmn:` 前缀), 但保留原 namespace prefixes 防 web 端乱解析
*
* 边界:
* - 空 XML / 解析失败 → 返回原 XML, 不抛 (避免引入新故障模式)
* - 完全没有 userTask → 返回原 XML
*/
import { BpmnModdle } from 'bpmn-moddle'
/**
* 给定 BPMN XML, 给所有缺 potentialOwner 的 userTask 注入 admin candidate.
*
* @param flowXml 原始 BPMN 2.0 XML
* @returns 修改后的 XML (若没有任何需要注入的 userTask, 返回原 XML 字符串)
*/
export async function autoAssignAdmin(flowXml: string): Promise<string> {
if (!flowXml || !String(flowXml).trim()) return flowXml
const moddle = new BpmnModdle()
let rootElement: any
try {
const parsed = (await moddle.fromXML(flowXml)) as { rootElement: any }
rootElement = parsed.rootElement
} catch {
// 解析失败: 不破坏原 XML, 由上层 semanticLint 报错
return flowXml
}
const proc = rootElement.rootElements?.find((e: any) => e.$type === 'bpmn:Process')
if (!proc) return flowXml
const flowEls: any[] = proc.flowElements || []
const userTasks = flowEls.filter((e: any) => e.$type === 'bpmn:UserTask')
if (userTasks.length === 0) return flowXml
let injected = 0
for (const ut of userTasks) {
// 检查是否已有任何 potentialOwner (避免覆盖用户显式候选人)
const hasPO = (ut.resources || []).some((r: any) => r.$type === 'bpmn:PotentialOwner')
if (hasPO) continue
// 检查是否已有命名空间 assignee (camunda:assignee / flowable:assignee) — 这些语义上
// 是单候选人而非 "无候选人", 但 dacli semanticLint 在无 PO 时仍判 fail.
// 为兜底有效, 统一注入 admin potentialOwner; 既有 assignee 不删 (additive).
const $attrs = ut.$attrs || {}
const hasAnyAssignee =
Boolean(ut.assignee) ||
Boolean($attrs.assignee) ||
Boolean($attrs['camunda:assignee']) ||
Boolean($attrs['flowable:assignee'])
// 构造 bpmn:PotentialOwner 元素
// 注: bpmn-moddle schema 关键细节 (踩坑 2026-07-29):
// 1. bpmn:FormalExpression.body 是 isBody=true (元素文本, 不是属性)
// 2. bpmn:ResourceAssignmentExpression 的字段叫 'expression' (不是 formalExpression)
// 序列化时 moddle 会写成 <expression xsi:type="tFormalExpression">...</expression>
// 3. 直接 moddle.create 时, body 必须放在第二个参数里
const formalExpression = moddle.create('bpmn:FormalExpression', { body: 'admin' }) as any
const rae = moddle.create('bpmn:ResourceAssignmentExpression', {
expression: formalExpression,
}) as any
const po = moddle.create('bpmn:PotentialOwner', {
resourceAssignmentExpression: rae,
}) as any
// resources 是 flat ResourceRole 数组 (bpmn-moddle schema isMany=true)
ut.resources = [...(ut.resources || []), po]
injected++
// 注释: hasAnyAssignee 信息保留给上层日志 (避免破坏现有 schema)
void hasAnyAssignee
}
if (injected === 0) return flowXml
try {
const { xml } = await moddle.toXML(rootElement, { format: true })
return xml as string
} catch {
// 序列化失败: 兜底不可用, 返回原 XML 让上层报错
return flowXml
}
}