fix(modeler): 保真扫描回归加固——refs袋+unresolved入门+规模门+stderr(评审I1/M1/M2/M5)

- I1 引用回归盲区:surface() 新增 refs 袋(sourceRef/targetRef/bpmnElement/
  processRef/attachedToRef/default,与 sanitizeIds 同覆盖面)并入比对键;normWarn
  的 unresolved reference 类 warning 计入硬门 failures(净化/往返破坏连线的直接信号)。
- M1:删除未使用的 realFail。
- M2:新增 EXPECTED_TOTAL 环境变量(默认 1512),实际语料数不符即判失败;
  decodeFailures.length 并入硬门(过滤集内本应 100% 干净 UTF8)。
- M5:崩溃分支透出子进程 e.stderr。
- M3/M4:命名空间字面前缀识别的语料前提、extElemText trim 口径各加注释声明。

重跑实测(node tools/modeler-fidelity-sweep.mjs):匹配 1512/1512 零丢失,
解析异常 0,解码失败 0,warnings=0,exit 0;EXPECTED_TOTAL=9999 时 exit 1
(规模门生效)。npx vitest run src/modeler 12/12 绿;npx tsc -b 干净(exit 0)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-02 10:15:49 +08:00
parent bd7c7fc697
commit b391ee9f16

View File

@@ -87,6 +87,9 @@ function sanitizeIds(xml) {
}
// ── 命名空间嗅探:activiti 优先,其次 flowable,默认 activiti ─────────────────
// M3 前提:靠字面量 uri/前缀识别命名空间,依赖语料使用规范前缀 activiti/flowable。
// §0 实测本库确如此(activiti 1509 / flowable 3,无自定义前缀重映射),故成立;
// 若未来语料出现 xmlns:act="http://activiti.org/bpmn" 之类别名,此处需扩正则。
function sniffModdle(xml) {
const hasActiviti = xml.includes('activiti.org/bpmn') || /\bactiviti:/.test(xml);
const hasFlowable = xml.includes('flowable.org/bpmn') || /\bflowable:/.test(xml);
@@ -101,6 +104,9 @@ function sniffModdle(xml) {
const TAG_RE = /<([/!?]?)([\w:.-]+)((?:[^>"']|"[^"]*"|'[^']*')*)>/g;
const ATTR_RE = /([\w:.-]+)\s*=\s*"([^"]*)"/g;
const EXT_TEXT_RE = /<((?:activiti|flowable):[\w.-]+)((?:[^>"']|"[^"]*"|'[^']*')*)>([^<]*)<\//g;
// I1:引用类属性单独入袋,捕捉 round-trip/净化重写破坏的连线(sourceRef/targetRef/
// bpmnElement/processRef/attachedToRef/default)。与 sanitizeIds 的 ID_ATTR 同覆盖面。
const REF_ATTRS = new Set(['sourceRef', 'targetRef', 'bpmnElement', 'processRef', 'attachedToRef', 'default']);
function surface(xml) {
const extAttrs = []; // 原生元素上的 activiti:/flowable: 属性 p:name=value
@@ -108,6 +114,7 @@ function surface(xml) {
const extElemAttrs = [];// 扩展元素上的属性(含无前缀) tag@attr=value
const extElemText = []; // 扩展叶元素的文本 tag#text=value
const ids = []; // 全部 id="..."(含原生/DI/扩展)
const refs = []; // 引用类属性 an=value(连线/DI 指向完整性,I1)
let m;
TAG_RE.lastIndex = 0;
@@ -123,6 +130,7 @@ function surface(xml) {
const an = a[1];
const av = decodeEntities(a[2]);
if (an === 'id') ids.push(av);
else if (REF_ATTRS.has(an)) refs.push(`${an}=${av}`);
if (isExt) {
extElemAttrs.push(`${name}@${an}=${av}`);
} else if (/^(activiti|flowable):/.test(an)) {
@@ -132,13 +140,15 @@ function surface(xml) {
if (isExt) extElemTags.push(name);
}
// M4:extElemText 对叶元素文本做 trim 归一(去两端空白),故不比对纯空白/缩进差异。
// 扩展叶文本(监听器 field 值、表单选项 name 等)非空白敏感,且前后同样 trim,对称无偏。
EXT_TEXT_RE.lastIndex = 0;
while ((m = EXT_TEXT_RE.exec(xml))) {
const t = decodeEntities(m[3]).trim();
if (t) extElemText.push(`${m[1]}#text=${t}`);
}
return { extAttrs, extElemTags, extElemAttrs, extElemText, ids };
return { extAttrs, extElemTags, extElemAttrs, extElemText, ids, refs };
}
function bag(arr) {
@@ -206,9 +216,15 @@ async function main() {
continue;
}
const unresolvedRefs = [];
for (const w of warnings) {
const key = normWarn(String(w && w.message || w));
const msg = String(w && w.message || w);
const key = normWarn(msg);
warnCounts.set(key, (warnCounts.get(key) || 0) + 1);
if (key === 'unresolved reference') {
const mr = msg.match(/unresolved reference <([^>]+)>/i);
unresolvedRefs.push(mr ? mr[1] : msg.slice(0, 60));
}
}
// 5) 数据级零丢失比对:sanitized(前) vs out(后)
@@ -216,20 +232,31 @@ async function main() {
const after = surface(out);
const diffs = {};
let lost = false;
for (const k of ['extAttrs', 'extElemTags', 'extElemAttrs', 'extElemText', 'ids']) {
for (const k of ['extAttrs', 'extElemTags', 'extElemAttrs', 'extElemText', 'ids', 'refs']) {
const d = diffBag(before[k], after[k]);
if (d.missing.length || d.added.length) {
diffs[k] = d;
lost = true;
}
}
if (lost) failures.push({ id: doc.id, name: doc.name, ns, diffs });
else ok++;
// I1②:未解析引用计入硬门(净化/往返破坏连线的直接信号,不再只当 warning)
if (lost || unresolvedRefs.length) {
failures.push({ id: doc.id, name: doc.name, ns, diffs, unresolved: unresolvedRefs });
} else {
ok++;
}
}
report({ total: docs.length, ok, failures, decodeFailures, parseErrors, warnCounts, nsCounts });
// 硬门:真实 BPMN 全部零丢失(解码失败=非目标语料,单列不计入失败门)
process.exit(failures.length === 0 && parseErrors.length === 0 ? 0 : 1);
const expectedTotal = Number(process.env.EXPECTED_TOTAL || 1512);
report({ total: docs.length, ok, failures, decodeFailures, parseErrors, warnCounts, nsCounts, expectedTotal });
// 硬门:真实 BPMN 全部零丢失 + 无解析异常 + 无解码失败 + 语料规模符合预期。
// (M2:解码失败与规模不符均判失败——过滤集内本应 100% 干净 UTF8,且恒 EXPECTED_TOTAL 篇)
const gateOk =
failures.length === 0 &&
parseErrors.length === 0 &&
decodeFailures.length === 0 &&
docs.length === expectedTotal;
process.exit(gateOk ? 0 : 1);
}
// warning 归一:抽取属性/元素名做聚合键
@@ -246,10 +273,11 @@ function normWarn(msg) {
function report(r) {
const line = '─'.repeat(60);
process.stdout.write(`${line}\n保真扫描结果\n${line}\n`);
const realFail = r.failures.length + r.parseErrors.length;
const realTotal = r.total - r.decodeFailures.length;
process.stdout.write(`匹配 ${r.ok}/${realTotal}(真实 BPMN,已排除 ${r.decodeFailures.length} 解码失败)\n`);
process.stdout.write(`总文档 ${r.total}|零丢失 ${r.ok}|丢失 ${r.failures.length}|解析异常 ${r.parseErrors.length}|解码失败 ${r.decodeFailures.length}\n`);
process.stdout.write(`匹配 ${r.ok}/${r.total} 零丢失(真实 BPMN)\n`);
process.stdout.write(`总文档 ${r.total}(预期 ${r.expectedTotal})|零丢失 ${r.ok}|丢失 ${r.failures.length}|解析异常 ${r.parseErrors.length}|解码失败 ${r.decodeFailures.length}\n`);
if (r.total !== r.expectedTotal) {
process.stdout.write(`⚠ 语料规模 ${r.total} ≠ 预期 ${r.expectedTotal}(EXPECTED_TOTAL)→ 判失败\n`);
}
process.stdout.write(`\n命名空间加载分布:\n`);
for (const [ns, c] of [...r.nsCounts].sort((a, b) => b[1] - a[1])) {
@@ -280,6 +308,9 @@ function report(r) {
if (d.missing.length) process.stdout.write(` ${k} 丢失: ${d.missing.slice(0, 6).join(' ; ')}${d.missing.length > 6 ? ' …' : ''}\n`);
if (d.added.length) process.stdout.write(` ${k} 新增: ${d.added.slice(0, 6).join(' ; ')}${d.added.length > 6 ? ' …' : ''}\n`);
}
if (f.unresolved && f.unresolved.length) {
process.stdout.write(` 未解析引用(I1): ${f.unresolved.slice(0, 6).join(' ; ')}${f.unresolved.length > 6 ? ' …' : ''}\n`);
}
}
if (r.failures.length > 30) process.stdout.write(` … 另有 ${r.failures.length - 30} 个失败文档\n`);
}
@@ -287,6 +318,8 @@ function report(r) {
}
main().catch((e) => {
// M5:psql 等子进程失败时把其 stderr 透出来(execFileSync 把 stderr 挂在 e.stderr)
if (e && e.stderr) process.stderr.write(String(e.stderr));
process.stderr.write(`扫描崩溃: ${e && e.stack || e}\n`);
process.exit(2);
});