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:
@@ -34,9 +34,10 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
*
|
||||
* <p>WS-B:第 5 参数为改写产物根目录(rewriterDir),传入即启用 onEvent 挂回。挂回不再读
|
||||
* full_auto_index.json,而是从每个模板源 XML 解析事件绑定(onselectedscript/onloadscript →
|
||||
* scriptKey)+ {@code <scripts>} 文档序(key→seq),按事件类型调用
|
||||
* {@link TemplateToAmisConverter#attachScriptOnEvent}(onselected→selectedChange / onload→inited),
|
||||
* 覆盖 NO_S3+FULL_AUTO 两类脚本。
|
||||
* scriptKey)+ scriptKey→seq(复刻改写器枚举:{@code //script} 文档序 + strip<5 跳过 + 1-based 连续),
|
||||
* 按事件类型调用 {@link TemplateToAmisConverter#attachScriptOnEvent}(onselected→selectedChange /
|
||||
* onload→inited),覆盖 NO_S3+FULL_AUTO 两类脚本。仅当盘上确有 {@code rewritten/<id>/<seq>.*.groovy}
|
||||
* 时才挂该绑定(fail-closed),并对「转换器算 keyed-script 数 vs 盘上文件数」做对齐校验告警。
|
||||
*
|
||||
* <p>用法(本地 eip4dev):
|
||||
* <pre>{@code
|
||||
@@ -58,7 +59,7 @@ public final class BatchConvertCli {
|
||||
private static final String DEFAULT_USER = "ceshi";
|
||||
private static final String DEFAULT_PASS = "ceshi";
|
||||
private static final String DEFAULT_OUT = "转换产物";
|
||||
/** 第 5 参数:改写产物根目录(含 full_auto_index.json),为空则不挂 onEvent。 */
|
||||
/** 第 5 参数:改写产物根目录(含 rewritten/<id>/<seq>.*.groovy),为空则不挂 onEvent。 */
|
||||
private static final String DEFAULT_REWRITER_DIR = "";
|
||||
|
||||
private BatchConvertCli() {
|
||||
@@ -126,7 +127,7 @@ public final class BatchConvertCli {
|
||||
}
|
||||
|
||||
/**
|
||||
* 从源模板 XML 解析 scriptKey→seq({@code <scripts>} 1-based 文档序,与 rewriter 落盘 seq 对齐)。
|
||||
* 从源模板 XML 解析 scriptKey→seq(复刻改写器枚举:{@code //script} 文档序 + strip<5 跳过 + 1-based 连续)。
|
||||
* 委托 {@link TemplateToAmisConverter#scriptKeyToSeq};XML 不可解析则返回空 map。
|
||||
*/
|
||||
static Map<String, Integer> parseKeyToSeq(String xml) {
|
||||
@@ -139,6 +140,56 @@ public final class BatchConvertCli {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从源模板 XML 算「占 seq 的脚本数」(= {@link TemplateToAmisConverter#countKeptScripts}),
|
||||
* 供与盘上文件数比对的对齐校验(WS-B C2)。XML 不可解析返回 -1(跳过校验)。
|
||||
*/
|
||||
static int parseCountKeptScripts(String xml) {
|
||||
try {
|
||||
Element root = DocumentHelper.parseText(xml).getRootElement();
|
||||
return new TemplateToAmisConverter(null).countKeptScripts(root);
|
||||
} catch (Exception e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描 {@code rewriterDir/<templateId>/} 下落盘的脚本文件,返回其 seq 集合
|
||||
* (文件名形如 {@code <seq>.<LABEL>.groovy},镜像运行时 {@code RewrittenScriptStore} 的 prefix+后缀匹配)。
|
||||
* 目录不存在/不可读返回空集合(→ 该模板所有绑定 fail-closed 不挂)。
|
||||
*/
|
||||
static java.util.Set<Integer> existingSeqs(Path rewriterDir, String templateId) {
|
||||
java.util.Set<Integer> seqs = new java.util.HashSet<>();
|
||||
if (rewriterDir == null || templateId == null) {
|
||||
return seqs;
|
||||
}
|
||||
Path dir = rewriterDir.resolve("rewritten").resolve(templateId);
|
||||
if (!Files.isDirectory(dir)) {
|
||||
// 兼容传入已是 rewritten 根的情形
|
||||
dir = rewriterDir.resolve(templateId);
|
||||
}
|
||||
if (!Files.isDirectory(dir)) {
|
||||
return seqs;
|
||||
}
|
||||
try (java.util.stream.Stream<Path> files = Files.list(dir)) {
|
||||
files.map(p -> p.getFileName().toString())
|
||||
.filter(n -> n.endsWith(".groovy"))
|
||||
.forEach(n -> {
|
||||
int dot = n.indexOf('.');
|
||||
if (dot > 0) {
|
||||
try {
|
||||
seqs.add(Integer.parseInt(n.substring(0, dot)));
|
||||
} catch (NumberFormatException ignore) {
|
||||
// 非 <seq>. 前缀的文件忽略
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
return seqs;
|
||||
}
|
||||
return seqs;
|
||||
}
|
||||
|
||||
static BatchSummary run(Connection conn, Path outDir) throws IOException, SQLException {
|
||||
return run(conn, outDir, null);
|
||||
}
|
||||
@@ -195,10 +246,18 @@ public final class BatchConvertCli {
|
||||
// 从源 XML 解析事件绑定(onselected/onload→scriptKey) + scripts 文档序(key→seq),
|
||||
// 按事件类型挂到正确 amis 事件(onselected→selectedChange / onload→inited),覆盖 NO_S3+FULL_AUTO。
|
||||
if (attachOnEvent) {
|
||||
// C2 对齐校验:转换器算出的 keyed-script 数 vs 盘上 rewritten/<id>/ 文件数,不一致告警。
|
||||
java.util.Set<Integer> seqsOnDisk = existingSeqs(rewriterDir, id);
|
||||
int keptByConverter = parseCountKeptScripts(xml);
|
||||
if (keptByConverter >= 0 && !seqsOnDisk.isEmpty() && keptByConverter != seqsOnDisk.size()) {
|
||||
System.out.println("[WS-B][对齐告警] " + id + " 转换器算 keyed-script 数=" + keptByConverter
|
||||
+ " ≠ 盘上文件数=" + seqsOnDisk.size() + "(seq 枚举可能与改写器偏差,请核对 //script 范围/长度阈值)");
|
||||
}
|
||||
List<TemplateToAmisConverter.EventBinding> bindings = parseEventBindings(xml);
|
||||
if (!bindings.isEmpty()) {
|
||||
Map<String, Integer> keyToSeq = parseKeyToSeq(xml);
|
||||
converter.attachScriptOnEvent(result.getSchema(), id, bindings, keyToSeq);
|
||||
// C1-a:只在盘上确有 rewritten/<id>/<seq>.*.groovy 时才挂该绑定(fail-closed)。
|
||||
converter.attachScriptOnEvent(result.getSchema(), id, bindings, keyToSeq, seqsOnDisk::contains);
|
||||
Object body = result.getSchema().get("body");
|
||||
if (body instanceof Map && ((Map<?, ?>) body).containsKey("onEvent")) {
|
||||
onEventAttached = true;
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentHelper;
|
||||
@@ -489,7 +490,11 @@ public class TemplateToAmisConverter {
|
||||
.filter(s -> !s.isBlank())
|
||||
.findFirst().orElse("");
|
||||
if (isTrueDeleteButton(btnName)) {
|
||||
// 真删除按钮:入 bulkActions(amis 批量操作槽,自动注入 ${ids}=选中行主键逗号串)
|
||||
// 真删除按钮:入 bulkActions(amis 批量操作槽,自动注入 ${ids}=选中行主键逗号串)。
|
||||
// WS-B M2 note:bulkActions 槽内按钮不设 amis id,故 enableButton("删除",…) 的
|
||||
// enabled/disabled 指令(componentId=按钮名)无法定位 bulkActions 删除按钮——但实测
|
||||
// 删除按钮极少被 onselectedscript 联动启停(联动几乎都针对 headerToolbar 业务按钮),
|
||||
// 暂不强制补 id(amis bulkActions 对自定义 id 的启停支持亦不稳)。
|
||||
Map<String, Object> del = new LinkedHashMap<>();
|
||||
del.put("type", "button");
|
||||
del.put("label", btnName);
|
||||
@@ -1713,24 +1718,45 @@ public class TemplateToAmisConverter {
|
||||
}
|
||||
}
|
||||
|
||||
/** rewriter 落盘的脚本最短文本长度阈值(strip 后 < 此值的 {@code <script>} 被跳过、不占 seq)。 */
|
||||
static final int SCRIPT_MIN_LEN = 5;
|
||||
|
||||
/**
|
||||
* 解析 {@code <scripts><script key=...>} 的 key→seq(1-based 文档序,与 rewriter 落盘 seq 对齐)。
|
||||
* 解析 {@code <script key=...>} 的 key→seq,<b>必须与改写器(tools/s3_rewriter)落盘枚举规则逐位对齐</b>。
|
||||
*
|
||||
* <p>rewriter 落盘路径为 {@code rewritten/<templateId>/<seq>.<LABEL>.groovy},seq 即
|
||||
* view_template.xml {@code <scripts>} 内 {@code <script>} 的 1-based 文档序;本方法重建该映射,
|
||||
* 供事件绑定 {@code onselectedscript="<scriptKey>"} 反查 seq。
|
||||
* <p>rewriter 落盘路径 {@code rewritten/<templateId>/<seq>.<LABEL>.groovy} 的 seq 由
|
||||
* {@code run_eval.py::load_corpus_db} + {@code run_rewrite.py} 共同确定,规则为:
|
||||
* <ol>
|
||||
* <li><b>全局范围</b>:抽 {@code xpath('//script/text()')}——文档内<b>所有</b> {@code <script>}(不限 {@code <scripts>} 直接子),按文档序;</li>
|
||||
* <li><b>长度过滤</b>:脚本文本 strip 后 {@code length < }{@value #SCRIPT_MIN_LEN} 的<b>跳过</b>({@code continue},不占号);</li>
|
||||
* <li><b>连续编号</b>:对保留者 1-based 连续编号(被跳过者不消耗 seq)。</li>
|
||||
* </ol>
|
||||
* 本方法复刻该枚举(dom4j {@code selectNodes("//script")} 文档序 + {@code getText()} 取文本),
|
||||
* 对每个保留脚本记 {@code @key}(非空)→seq。改写器只按位置编号、不认 key;本映射用 key 反查 seq,
|
||||
* 供事件绑定 {@code onselectedscript="<scriptKey>"} 找到对应落盘文件。
|
||||
*
|
||||
* <p>⚠️ 历史 bug(WS-B C2,已证伪 32 模板偏差/1 模板 key 错位):旧实现按 {@code <scripts>} 直接子、
|
||||
* 无长度过滤编号——遇 {@code <scripts>} 外的 script 漏号、遇空/极短 script 多占号,导致多脚本模板
|
||||
* 静默挂错 seq(运行时 load 返 null → 用户见「ajax 动作执行失败」)。
|
||||
*
|
||||
* @param root 模板 XML 根元素
|
||||
* @return key→1-based 连续 seq(仅含非空 key 的保留脚本),无 {@code <script>} 时返回空 map
|
||||
*/
|
||||
Map<String, Integer> scriptKeyToSeq(Element root) {
|
||||
Map<String, Integer> map = new LinkedHashMap<>();
|
||||
if (root == null) {
|
||||
return map;
|
||||
}
|
||||
Element scripts = root.element("scripts");
|
||||
if (scripts == null) {
|
||||
return map;
|
||||
}
|
||||
int seq = 0;
|
||||
for (Element s : scripts.elements("script")) {
|
||||
for (Object node : root.selectNodes("//script")) {
|
||||
if (!(node instanceof Element s)) {
|
||||
continue;
|
||||
}
|
||||
// getText():取直接文本节点(对纯文本脚本等价于 //script/text()),与 rewriter 取数同源。
|
||||
String text = s.getText();
|
||||
if (text == null || text.strip().length() < SCRIPT_MIN_LEN) {
|
||||
continue; // strip 后 <5 字符 → rewriter 跳过、不占 seq,本方法须一致跳过
|
||||
}
|
||||
seq++;
|
||||
String key = s.attributeValue("key");
|
||||
if (key != null && !key.isBlank()) {
|
||||
@@ -1740,27 +1766,56 @@ public class TemplateToAmisConverter {
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换器算出的「占 seq 的脚本数」(= {@link #scriptKeyToSeq} 的最大 seq),供 BatchConvertCli
|
||||
* 在重转期与盘上 {@code rewritten/<id>/} 文件数比对、发现残余偏差(WS-B C2 对齐校验)。
|
||||
*
|
||||
* @param root 模板 XML 根元素
|
||||
* @return 保留脚本数(strip 后 ≥{@value #SCRIPT_MIN_LEN} 的 {@code <script>} 数,含 keyless)
|
||||
*/
|
||||
int countKeptScripts(Element root) {
|
||||
if (root == null) {
|
||||
return 0;
|
||||
}
|
||||
int seq = 0;
|
||||
for (Object node : root.selectNodes("//script")) {
|
||||
if (node instanceof Element s) {
|
||||
String text = s.getText();
|
||||
if (text != null && text.strip().length() >= SCRIPT_MIN_LEN) {
|
||||
seq++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return seq;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归扫描根元素树,收集所有带 {@code onselectedscript}/{@code onloadscript} 属性的元素,
|
||||
* 产出事件绑定列表(全库 onselected 930 / onload 422 模板;onclick/onchange 为 0,只处理两类)。
|
||||
*
|
||||
* <p>WS-B I2:同一 {@code (eventType, scriptKey)} 在多个元素重复出现时<b>只收一次</b>(保文档序),
|
||||
* 避免同一脚本被挂进 onEvent 多次造成 ajax 双发/重放重复。
|
||||
*/
|
||||
List<EventBinding> extractEventBindings(Element root) {
|
||||
List<EventBinding> out = new ArrayList<>();
|
||||
if (root != null) {
|
||||
collectBindings(root, out);
|
||||
collectBindings(root, out, new LinkedHashSet<>());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private void collectBindings(Element el, List<EventBinding> out) {
|
||||
private void collectBindings(Element el, List<EventBinding> out, Set<String> seen) {
|
||||
for (String attr : EVENT_ATTRS) {
|
||||
String v = el.attributeValue(attr);
|
||||
if (v != null && !v.isBlank()) {
|
||||
out.add(new EventBinding(attr, v));
|
||||
// I2:按 (eventType,scriptKey) 去重——同绑定多处出现只挂一次
|
||||
if (seen.add(attr + " | ||||