fix(converter): scriptKeyToSeq 复刻改写器枚举(C2)+缺脚本 fail-closed/no-op(C1)+去重(I2)

re-review 修 WS-B 两 Critical + 两 Important。

C2(最关键,静默挂错脚本):scriptKeyToSeq 旧实现按 <scripts> 直接子、无长度过滤
1-based 编号,与改写器盘上 seq 枚举不一致 → 多脚本模板挂错 seq。改写器真实枚举
(run_eval.py::load_corpus_db + run_rewrite.py 核实):xpath('//script/text()') 全局
范围(非仅 <scripts> 子)+ strip 后 length<5 跳过(continue 不占号)+ 对保留者
1-based 连续编号。新实现用 selectNodes("//script") 文档序 + getText() + SCRIPT_MIN_LEN=5
过滤 + 连续编号逐位对齐。新增 countKeptScripts 供对齐校验。
- 全库实证(psql eip4dev,721 个有盘模板):新规则 key→seq 总数 701/701 == 盘上文件数
  (旧版 32 模板偏差/1 模板 key 错位 d1f64d77: K_FILTER 旧算 seq=3 盘上无→运行时「未找到」,
  新算 seq=2 命中);999 事件绑定 997 命中盘上文件、2 为悬挂/被过滤 key(C1 fail-closed)。

C1-a(有绑定但文件不存在不挂):attachScriptOnEvent 增 Predicate<Integer> seqExists
重载(converter 不碰 FS,由 BatchConvertCli 注入盘上 existingSeqs);seq 无对应
rewritten/<id>/<seq>.*.groovy 则跳过该绑定(fail-closed)。BatchConvertCli 接线
existingSeqs(镜像 RewrittenScriptStore prefix+.groovy 匹配)+ 重转期对齐校验告警
(转换器算 keyed-script 数 ≠ 盘上文件数则 System.out 提示)。

C1-b(运行时兜底):ScriptExecuteController.executeTemplateRewritten 当 load 返 null/blank
改返 {passed:true, commands:[], commandCount:0}(no-op)而非 {passed:false,error},残余
缺脚本退化为「无联动」而非用户可见 ajax 报错;保留 blank templateId→{passed:false}。

I2(去重):extractEventBindings 按 (eventType,scriptKey) 去重,免同脚本多处绑定致 ajax 双发。

M3/M2:清理 BatchConvertCli javadoc 的 full_auto_index/<scripts> 文档序漂移;bulkActions
删除按钮无 id(enableButton 定位不到)记 note(删除极少被联动启停,不强制修)。

测试:platform-converter 92 绿(TemplateToAmisConverterTest 83 含新增 C2 短脚本跳过/外部
script 计入/keyless 占号、C1-a seqExists、I2 去重;BatchConvertCliTest 9 新建:parseKeyToSeq/
parseEventBindings/parseCountKeptScripts/existingSeqs/attached-seq 命中盘上文件契约)。
platform-metadata ScriptExecuteControllerTest 5 绿(缺 templateId→false / 脚本未找到→no-op 拆分)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-14 15:21:04 +08:00
parent 79eb8e2087
commit 47d03b714b
6 changed files with 418 additions and 32 deletions

View File

@@ -138,8 +138,14 @@ public class ScriptExecuteController {
String script = rewrittenScriptStore.load(templateId, seq);
if (script == null || script.isBlank()) {
resp.put("passed", false);
resp.put("error", "改写后脚本未找到: templateId=" + templateId + " seq=" + seq);
// WS-B C1-b:脚本未找到(残余缺脚本/seq 偏差)退化为「无联动」no-op,而非用户可见 ajax 报错。
// 转换期 fail-closed 已尽量不挂缺文件的绑定;此处兜底任何残余,返空 commands → 前端无动作可重放。
resp.put("templateId", templateId);
resp.put("seq", seq);
resp.put("passed", true);
resp.put("commands", List.of());
resp.put("commandCount", 0);
resp.put("note", "改写后脚本未找到,按无联动处理(no-op): templateId=" + templateId + " seq=" + seq);
return resp;
}
resp.put("templateId", templateId);

View File

@@ -3,6 +3,7 @@ package com.eip4.platform.metadata.web;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
@@ -88,21 +89,33 @@ class ScriptExecuteControllerTest {
assertEquals("S-99", args.get("value")); // getFieldValue 从 item 求值闭环
}
/** 缺 templateId 是真错 → passed=false + error(保留)。 */
@Test
void executeTemplateRewritten_缺templateId或脚本未找到时passed为false(@TempDir Path root) {
void executeTemplateRewritten_缺templateId时passed为false(@TempDir Path root) {
ScriptExecuteController c = controller(root);
Map<String, Object> noTid = new LinkedHashMap<>();
Map<String, Object> r1 = c.executeTemplateRewritten(noTid);
assertEquals(Boolean.FALSE, r1.get("passed"));
assertNotNull(r1.get("error"));
assertTrue(((String) r1.get("error")).contains("templateId"));
}
/** WS-B C1-b:脚本未找到(残余缺脚本/seq 偏差)退化为 no-op(passed=true + 空 commands),不报 ajax 错。 */
@SuppressWarnings("unchecked")
@Test
void executeTemplateRewritten_脚本未找到时退化为noop(@TempDir Path root) {
ScriptExecuteController c = controller(root);
Map<String, Object> missing = new LinkedHashMap<>();
missing.put("templateId", "no-such");
missing.put("seq", 1);
Map<String, Object> r2 = c.executeTemplateRewritten(missing);
assertEquals(Boolean.FALSE, r2.get("passed"));
assertTrue(((String) r2.get("error")).contains("未找到"));
assertEquals(Boolean.TRUE, r2.get("passed")); // no-op,非用户可见报错
assertNull(r2.get("error")); // 不再返 error
List<Map<String, Object>> cmds = (List<Map<String, Object>>) r2.get("commands");
assertTrue(cmds.isEmpty()); // 空 commands → 前端无动作可重放
assertEquals(0, r2.get("commandCount"));
}
@SuppressWarnings("unchecked")