P4-2 脚本迁移线:全量改写产出 + facade 动词表运行时实现

任务1 全量改写执行:
- run_rewrite.py 对客户库缓存 1,427 条逐条改写,按 template_id+脚本序号落盘
  output/rewritten/(497 FULL_AUTO + 30 PARTIAL + 85 HARD + 815 NO_S3)
- 人工作业单 115 份(PARTIAL 30 + HARD 85),每份附原文/已改部分/余留模式/建议改法/工时口径
- 自动化率 81.2% 复现 C4 实测,0 误改

任务2 facade 动词表运行时:
- ScriptFacade 把 31 改写目标动词 + bindField 作为 Groovy 闭包注入 Bindings
  (PoC enableButton 模式扩展);服务端语义动词直接求值,UI 语义动词捕获为
  amis 事件动作指令 JSON(actionType/componentId/args)
- 保真层纪律:全在 com.eip4 现代层,com.dsasp 源码零改,ArchUnit 复跑通过
- ScriptExecuteController 提供 /api/script/execute 与 /api/script/execute-template

验证:
- 单测 17 方法逐动词覆盖,均经真实 Groovy 过保真层引擎,BUILD SUCCESS
- 集成 6 条真实 FULL_AUTO 改写脚本经 8086 执行,amis 指令协议合规
- 回归 PoC script-test 通过,ArchUnit 架构纪律通过

报告 tools/s3_rewriter/P4脚本迁移执行报告.md;新坑入 docs/迁移手册.md 5.39/9.3

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 16:10:22 +08:00
parent 3a13c50a01
commit aefb0e2eb9
1552 changed files with 41290 additions and 2 deletions

View File

@@ -0,0 +1,280 @@
"""
run_rewrite.py — 全量改写产出落盘(P4 脚本迁移执行)
读 scripts_cache.json(客户库 dbname=postgres&currentSchema=eip3 落盘缓存,
已经 run_eval --from-db 锁定 81.2% 自动化率,离线复用,严禁连 ceshi 库),
对 1,427 条逐条跑 rewrite.rewrite_script,按 template_id + 脚本序号落盘:
output/rewritten/<template_id>/<seq>.<label>.groovy 改写后脚本库
- FULL_AUTO : 直接可用(头注释标 FULL_AUTO)
- PARTIAL : 已改写部分 + 余留 S3 行内联标注(头注释标 PARTIAL + 余留模式)
- HARD : 原文 + 人工标记(头注释标 HARD + 硬 S3 原因)
- NO_S3 : 原样(头注释标 NO_S3)
output/manual_workpackage/<seq>_<label>_<tid>.md 人工作业单
PARTIAL(30) + HARD(85) 共 115 条,每条附:
原文 / 已改写部分 / 余留模式 / 建议改法 / 工时口径(0.5 人日/条上界)
output/stats.json 全量产出统计(分类计数/自动化率/动词命中/人工包规模)
用法:
python3 run_rewrite.py
python3 run_rewrite.py --cache scripts_cache.json --out output
"""
import argparse
import json
import os
import re
import sys
from collections import Counter, defaultdict
from typing import Dict, Any, List
sys.path.insert(0, os.path.dirname(__file__))
from rewrite import rewrite_script, RewriteDetail
# 余留模式诊断(PARTIAL 余留分类,给人工作业单建议改法用)
_RESIDUAL_DIAG = [
(re.compile(r'\.comboBox\b'),
"raw .comboBox 组件访问",
"直达内嵌 Vaadin ComboBox;amis 侧改用 setValue/setOptions 事件动作,需人工确认语义等价"),
(re.compile(r'\.table\b'),
"raw .table 组件访问",
"操作原始 Table 列模型(getVisibleColumns/setVisibleColumns);amis 侧改 columnsTogglable/静态列配置"),
(re.compile(r'\.getItemProperty\('),
"Item 级属性访问",
"Vaadin Item 属性读写;若非 des 选项填充 idiom,需人工逐项映射到 setValue path"),
(re.compile(r'\.addItem\('),
"数据源 addItem(非标准选项 idiom)",
"非 addFieldOption 标准链;人工确认填充语义后映射为 setOptions 数组"),
(re.compile(r'\.getContainerDataSource\('),
"残留容器数据源链",
"未收敛到 facade 动词的容器操作;人工确认是否过滤/清空/取值后映射对应 facade"),
(re.compile(r'\.getPropertyDataSource\('),
"属性数据源链(非 setReadOnly 收尾)",
"非只读收尾的属性数据源链;人工确认目标语义"),
(re.compile(r'\.setFilter\('),
"残留 setFilter(参数无法静态解析)",
"过滤参数含运行时变量;人工补全 setFieldFilter 调用"),
(re.compile(r'\.parent\b'),
"组件树向上导航 .parent",
"Vaadin 组件树遍历;amis 无等价,需人工按业务意图重写"),
(re.compile(r'\.setData\('),
"任意数据附着 setData",
"组件附加数据;人工确认用途,多数可删或改 amis 上下文变量"),
(re.compile(r'\.getItemCaption\('),
"Item 标题查询",
"Item 级标题读取;人工映射到选项 label"),
(re.compile(r'group\.getField\('),
"残留字段句柄链(未覆盖动词)",
"字段句柄上挂了未登记动词;人工确认动词语义后补登记或手改"),
]
def diagnose_residual(rewritten: str) -> List[Dict[str, str]]:
"""对 PARTIAL 改写后文本诊断余留模式,返回 [{pattern, suggestion, lines}]。"""
found = []
for pat, name, suggestion in _RESIDUAL_DIAG:
hits = [ln for ln in rewritten.split('\n') if pat.search(ln)]
if hits:
found.append({
"pattern": name,
"suggestion": suggestion,
"sample_lines": hits[:3],
"count": len(hits),
})
return found
def slug(text: str) -> str:
return re.sub(r'[^\w一-鿿]+', '_', text)[:40].strip('_')
def main():
here = os.path.dirname(__file__)
p = argparse.ArgumentParser()
p.add_argument("--cache", default=os.path.join(here, "scripts_cache.json"))
p.add_argument("--out", default=os.path.join(here, "output"))
args = p.parse_args()
with open(args.cache, encoding="utf-8") as f:
records = json.load(f)
rewritten_dir = os.path.join(args.out, "rewritten")
wp_dir = os.path.join(args.out, "manual_workpackage")
os.makedirs(rewritten_dir, exist_ok=True)
os.makedirs(wp_dir, exist_ok=True)
label_counts = Counter()
rule_hits = Counter()
per_template_seq = defaultdict(int)
manual_items = [] # PARTIAL + HARD
full_auto_index = [] # FULL_AUTO 索引(供集成验证挑样)
for rec in records:
src = rec["source"]
name = rec["name"]
tid = name.split("_", 1)[0]
tname = name.split("_", 1)[1] if "_" in name else ""
out_text, details, label = rewrite_script(src)
label_counts[label] += 1
# 规则命中(与 run_eval 同口径,去 pass2/内联/HARD_DETECTION 噪声)
for d in details:
if d.rule_name in ("HARD_DETECTION", "inline_field_handle",
"inline_field_handle_pass2", "inline_datasource_handle"):
continue
rule_hits[d.rule_name.replace("_pass2", "")] += d.count
# 落盘改写后脚本
per_template_seq[tid] += 1
seq = per_template_seq[tid]
tdir = os.path.join(rewritten_dir, tid)
os.makedirs(tdir, exist_ok=True)
header = [
f"// === EIP4 P4 脚本迁移产出 ===",
f"// template_id : {tid}",
f"// template_name: {tname}",
f"// seq : {seq}",
f"// label : {label}",
]
hard_reasons = [d.original for d in details if d.rule_name == "HARD_DETECTION"]
residuals = []
if label == "HARD":
header.append(f"// 硬 S3 原因 : {'; '.join(hard_reasons)}")
header.append(f"// 处置 : 原文保留,进人工排期(amis dialog/service 重写)")
elif label == "PARTIAL":
residuals = diagnose_residual(out_text)
header.append(f"// 余留模式 : {'; '.join(r['pattern'] for r in residuals) or '(深层组件访问)'}")
header.append(f"// 处置 : 大部分已自动改写,余留行人工补全(见作业单)")
elif label == "FULL_AUTO":
header.append(f"// 处置 : 全自动改写完成,直接入 P4 转换流水线")
else:
header.append(f"// 处置 : 无 S3 模式,原样进 P4")
header.append("")
body = src if label == "HARD" else out_text
with open(os.path.join(tdir, f"{seq}.{label}.groovy"), "w", encoding="utf-8") as fh:
fh.write("\n".join(header) + body)
if label == "FULL_AUTO":
full_auto_index.append({
"template_id": tid, "template_name": tname, "seq": seq,
"rules": sorted({d.rule_name.replace("_pass2", "")
for d in details
if d.rule_name not in ("inline_field_handle",
"inline_field_handle_pass2",
"inline_datasource_handle")}),
})
# 人工作业单(PARTIAL + HARD)
if label in ("PARTIAL", "HARD"):
manual_items.append({
"label": label, "template_id": tid, "template_name": tname, "seq": seq,
"source": src, "rewritten": out_text,
"hard_reasons": hard_reasons, "residuals": residuals,
})
# 写人工作业单(每条一个 md,0.5 人日/条作业单形态)
manual_items.sort(key=lambda x: (0 if x["label"] == "HARD" else 1, x["template_id"]))
for i, it in enumerate(manual_items, 1):
fname = f"{i:03d}_{it['label']}_{it['template_id'][:8]}_{slug(it['template_name'])}.md"
lines = [
f"# 人工作业单 #{i:03d} — {it['label']}",
"",
f"- 模板ID : `{it['template_id']}`",
f"- 模板名 : {it['template_name']}",
f"- 脚本序 : {it['seq']}",
f"- 工时口径: 0.5 人日/条(上界;PARTIAL 多为「仅余几行」,实际远低于此)",
"",
"## 1. 原文(S3 原始 Groovy)",
"```groovy",
it["source"].rstrip(),
"```",
"",
]
if it["label"] == "HARD":
lines += [
"## 2. 硬 S3 原因",
"",
"\n".join(f"- {r}" for r in it["hard_reasons"]) or "- (硬 S3 特征命中)",
"",
"## 3. 已改写部分",
"",
"无(HARD 整条不自动改写,原样保留)。",
"",
"## 4. 余留模式",
"",
"整条为硬 S3:动态创建 Vaadin 组件 / showConfirmDialog 同步确认框 / UI.getCurrent()。",
"",
"## 5. 建议改法",
"",
"- 动态组件:在 amis 页面 schema 侧用条件渲染/Dialog 组件声明式表达,脚本仅下发数据。",
"- showConfirmDialog:改为 amis confirm 事件动作 + 回调链(异步),脚本中途交互拆为两段。",
"- UI.getCurrent():amis 无全局 UI 单例,改用事件上下文/广播动作。",
]
else:
lines += [
"## 2. 已改写部分(自动产出)",
"```groovy",
it["rewritten"].rstrip(),
"```",
"",
"## 3. 余留模式与建议改法",
"",
]
if it["residuals"]:
for r in it["residuals"]:
lines.append(f"### 余留:{r['pattern']}({r['count']} 行)")
lines.append("")
lines.append("样例行:")
lines.append("```groovy")
lines += [l.strip() for l in r["sample_lines"]]
lines.append("```")
lines.append(f"建议改法:{r['suggestion']}")
lines.append("")
else:
lines += ["未诊断出标准余留模式,需人工通读改写后脚本中残留的 `group.getField(` 行。", ""]
with open(os.path.join(wp_dir, fname), "w", encoding="utf-8") as fh:
fh.write("\n".join(lines) + "\n")
# 统计
s3_total = label_counts["FULL_AUTO"] + label_counts["PARTIAL"] + label_counts["HARD"]
auto_rate = round(label_counts["FULL_AUTO"] / s3_total * 100, 1) if s3_total else 0.0
stats = {
"total_scripts": len(records),
"label_counts": dict(label_counts),
"s3_total": s3_total,
"automation_rate": auto_rate,
"rule_hit_counts": dict(rule_hits.most_common()),
"manual_workpackage_count": len(manual_items),
"manual_partial": label_counts["PARTIAL"],
"manual_hard": label_counts["HARD"],
"full_auto_index_count": len(full_auto_index),
"templates_with_scripts": len(per_template_seq),
}
with open(os.path.join(args.out, "stats.json"), "w", encoding="utf-8") as fh:
json.dump(stats, fh, ensure_ascii=False, indent=2)
with open(os.path.join(args.out, "full_auto_index.json"), "w", encoding="utf-8") as fh:
json.dump(full_auto_index, fh, ensure_ascii=False, indent=2)
# 控制台摘要
print("=" * 56)
print("P4 全量改写产出完成")
print("=" * 56)
print(f"总脚本数 : {stats['total_scripts']}")
print(f"FULL_AUTO : {label_counts['FULL_AUTO']}")
print(f"PARTIAL : {label_counts['PARTIAL']}")
print(f"HARD : {label_counts['HARD']}")
print(f"NO_S3 : {label_counts['NO_S3']}")
print(f"S3 总数 : {s3_total}")
print(f"自动化率 : {auto_rate}% ({'≥80% 锁定' if auto_rate >= 80 else '未达线'})")
print(f"人工作业单 : {len(manual_items)} 条 (PARTIAL {label_counts['PARTIAL']} + HARD {label_counts['HARD']})")
print(f"承载模板数 : {len(per_template_seq)}")
print(f"落盘目录 : {args.out}/rewritten , {args.out}/manual_workpackage")
return 0 if auto_rate >= 80 else 1
if __name__ == "__main__":
sys.exit(main())