#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ AI 功能门槛闸门(2026-09-29 建立) **定位:这是"闸门"不是"报告"。** 报告可以过期,闸门不会 —— 新增 AI 功能不登记门槛就提交不过。 背景(实测,可复现): · `server/src/services/` 下 AI/LLM/解析/语音/图像/助手类文件 **53 个**; · 全仓 `grep -rln "goldenSet\\|golden_set\\|金标\\|评测集\\|benchmark"` ⇒ **0 命中**; · AI 门槛全项目**只有 1 条**(立项 §4.3 商品匹配 ≥95%),且它是 R5 逼出来的 —— 原文写「比对人工录单准确率」,**无阈值、判不了 go/no-go**。 ⇒ 一个 AI 系统在没有精度基线的情况下上了线,而它对的是客户真实订单。 本闸门读 `docs/design/20260929-AI门槛与计量登记表.md`,逐行校验: §一 每个 AI 功能的 8 个字段**都有值**(填「待补」也算有值 —— 本闸门不判数值真假,只判"有没有登记"); §一 每个功能的「主逻辑」文件**真实存在**(防止登记一个不存在的服务); §一 登记的功能集合 ⊇ 代码里 `llmService` 的消费方集合(**防止新增 AI 功能漏登记**)。 用法: python3 tools/design-consistency/check-ai-thresholds.py [-v] 退出码:0 = 登记完备;1 = 有缺项/漏登记;2 = 登记表缺失或结构不对。 """ import io import os import re import sys ROOT = os.environ.get("DC_ROOT") or os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) REG = "docs/design/20260929-AI门槛与计量登记表.md" VERBOSE = "-v" in sys.argv # AI 功能的技术边界 = 直接消费 llmService / arkService 的服务文件 # (这是"有没有漏登记"的判据:代码里有、表里没有 = 漏登记) CONSUMER_SCAN_DIRS = ["server/src/services"] LLM_IMPORT = re.compile( r"from\s+'(?:\./|\.\./mcp/)(?:llmService|arkService|llmChatWithTools)'") REQUIRED_COLS = ["ID", "功能", "入口", "主逻辑", "精度门槛", "分母定义", "样本量", "降级开关", "计量点"] EMPTY_MARKS = {"", "-", "—", "无", "n/a", "N/A"} def parse_md_table(text, first_col_name): """从 markdown 里取第一张"表头首列等于 first_col_name"的表,返回 (表头, 行列表)。""" lines = text.split("\n") for i, l in enumerate(lines): if not l.strip().startswith("|"): continue hdr = [c.strip() for c in l.strip().strip("|").split("|")] if not hdr or hdr[0] != first_col_name: continue if i + 1 >= len(lines) or not re.match(r"^\|[\s\-:|]+\|?\s*$", lines[i + 1] or ""): continue rows = [] j = i + 2 while j < len(lines) and lines[j].strip().startswith("|"): cells = [c.strip() for c in lines[j].strip().strip("|").split("|")] rows.append(cells) j += 1 return hdr, rows return None, [] # 文档里已有的 AI 门槛表(扫文档侧,防"文档写过、登记表没有 ⇒ 在门槛盘点里消失") V3_DOC = "docs/design/20260928-系统设计与改造方案-V3.md" AI_METRIC_WORDS = ("准确率", "识别率", "匹配率", "换算率") THRESHOLD_RE = re.compile(r"[>≥]\s*([0-9]{1,3})\s*%") def doc_side_thresholds(): """从 V3 里抽出"AI 指标 + 数字阈值"的行 → [(指标名, 阈值, 行号)]。 这是 R7-AI-05 指出的一半未闭合:旧判据**只校验登记表自身**, 于是 V3 §30.3 那 4 条门槛(语音>95/OCR>90/商品匹配>95/单位换算>99) 因为没被登记,反而在"门槛盘点"里整批消失。⇒ 必须同时扫文档侧。 """ fp = os.path.join(ROOT, V3_DOC) if not os.path.exists(fp): return None out = [] for i, line in enumerate(io.open(fp, encoding="utf-8", errors="ignore").read().split("\n"), 1): if not line.strip().startswith("|"): continue m = THRESHOLD_RE.search(line) if not m: continue if not any(w in line for w in AI_METRIC_WORDS): continue cells = [c.strip().strip("*` ") for c in line.strip().strip("|").split("|")] name = cells[0] if cells else "" out.append((name, m.group(1) + "%", i)) return out def discover_consumers(): """代码里真实消费 llmService/arkService 的服务文件(basename 集合)。""" found = {} for d in CONSUMER_SCAN_DIRS: p = os.path.join(ROOT, d) if not os.path.isdir(p): continue for fn in sorted(os.listdir(p)): if not fn.endswith(".ts") or fn.endswith(".test.ts"): continue if fn in ("llmService.ts", "arkService.ts"): continue t = io.open(os.path.join(p, fn), encoding="utf-8", errors="ignore").read() if LLM_IMPORT.search(t): found[fn] = os.path.join(d, fn) return found def main(): path = os.path.join(ROOT, REG) if not os.path.exists(path): print("❌ 登记表不存在:%s" % REG) print(" ⇒ 没有登记表就没有闸门。先建表,再谈门槛。") return 2 text = io.open(path, encoding="utf-8").read() hdr, rows = parse_md_table(text, "ID") if not rows: print("❌ 登记表里找不到「以 ID 为首列的表格」(§一 AI 功能门槛登记)") return 2 problems = [] missing_cols = [c for c in REQUIRED_COLS if c not in hdr] if missing_cols: problems.append(("登记表缺列", "、".join(missing_cols))) idx = {c: (hdr.index(c) if c in hdr else -1) for c in REQUIRED_COLS} registered_files = set() n_feat = 0 for cells in rows: if len(cells) < len(hdr): problems.append(("行字段数不足", " | ".join(cells)[:80])) continue fid = cells[idx["ID"]] name = cells[idx["功能"]] n_feat += 1 for col in REQUIRED_COLS[1:]: v = cells[idx[col]].strip() if v in EMPTY_MARKS: problems.append(("%s 的「%s」为空" % (fid, col), "空字段 = 没门槛。填不出真数时请写「待补」并注明负责人")) # 「降级开关」列必须是**可执行的东西**,不得填任务编号或愿景表述。 # 判据来源:R7-FDE-01 —— 本表初版把「回退人工录单(开关见 T5-06)」填进该列, # 而 T5-06 是「灰度:1 群+1 客户跑 2 周」的 1 人日任务,开关根本不存在 # (代码里 AI_FALLBACK / ai_fallback / FALLBACK_TO_MANUAL 零命中)。 # 旧版闸门只查"字段非空",于是这条**照过了** —— 又一次"字段有值 ≠ 事实成立"。 # ⚠️ 只对**实体值**判据:括号里的说明常引用那个错的写法来解释它错在哪 # (本表 AI-19 就是这么更正的)—— 第一版连说明一起判,把更正本身报成缺陷, # 又是"检查器口径错、不是文档错"。⇒ 先剥掉括号说明再判。 gate_full = cells[idx["降级开关"]] # 嵌套括号要**反复剥到不动点**:`(A(B)C)` 这种,正则一次只剥最内层, # 第一版就是这样把说明的一半留在实体值里、继续误报(同一天内第二次"口径错")。 gate = gate_full while True: nxt = re.sub(r"([^()]*)|\([^()]*\)", "", gate) if nxt == gate: break gate = nxt gate = gate.strip() # ⚠️ 判据要从**全文**找"转引任务编号"的句式,不能在剥完括号的实体值上找 —— # 因为 `回退人工录单(开关见 T5-06)` 剥掉括号后正好把 T5-06 一起剥掉了, # 判据永不触发(我自测时看到的"红"其实来自别的检查项,差点误判成判据有效)。 gate_flat = re.sub(r"[\s`*]", "", gate_full) if re.search(r"开关见|见任务|参见|详见", gate_flat) and re.search(r"[TPG]\d+-\d+", gate_flat): problems.append(("%s 的「降级开关」是**转引任务编号**,不是开关实体" % fid, "%s ⇒ 开关可能根本不存在。请填 配置键/环境变量/接口/代码位置," "不存在就写「不存在」" % gate_full[:70])) elif re.search(r"^[TPG]\d+-\d+$", gate.strip()): problems.append(("%s 的「降级开关」只填了任务编号" % fid, gate_full[:70])) # 「主逻辑」里的文件必须真实存在 cell = cells[idx["主逻辑"]] for m in re.finditer(r"`([^`]+\.(?:ts|md))`", cell): rel = m.group(1) registered_files.add(os.path.basename(rel)) if not os.path.exists(os.path.join(ROOT, rel)): problems.append(("%s 的主逻辑文件不存在" % fid, rel)) # 文档侧门槛必须都被登记(否则"门槛盘点"会漏掉更早、更权威的那些) doc_th = doc_side_thresholds() if doc_th is None: problems.append(("找不到文档侧门槛来源 %s" % V3_DOC, "**不判通过**(失败关闭)")) else: reg_text = text missing_in_reg = [] for name, thr, ln in doc_th: key = re.sub(r"[((].*?[))]", "", name).strip() hit = (key and key in reg_text) or (thr in reg_text and key[:4] in reg_text) if not hit: missing_in_reg.append("%s %s(V3 行 %d)" % (key, thr, ln)) if missing_in_reg: problems.append(("文档里已写的 AI 门槛,登记表里没有(**门槛盘点的漏网**)", ";".join(missing_in_reg))) else: print(" 文档侧 AI 门槛 %d 条,均已登记" % len(doc_th)) consumers = discover_consumers() unregistered = sorted(set(consumers) - registered_files - {"orderParseExtract.ts", "v9CompletionService.ts", "qualityReportService.ts"}) # 这三个是"被别的功能带出去"的中间件(见说明) if unregistered: problems.append(("代码里有、登记表里没有的 AI 消费方(漏登记)", "、".join(unregistered))) print("AI 功能登记 %d 行 | 主逻辑文件 %d 个 | 代码侧 AI 消费方 %d 个" % (n_feat, len(registered_files), len(consumers))) if VERBOSE: for c in sorted(consumers): print(" %s %s" % ("✅" if c in registered_files else "❓", c)) if problems: print("\n🔴 %d 处不满足:" % len(problems)) for kind, detail in problems[:40]: print(" - [%s] %s" % (kind, detail)) if len(problems) > 40: print(" ...(还有 %d 处)" % (len(problems) - 40)) print("\n判因(别直接填绿):") print(" ① 真的没门槛 → 补登记,填「待补」并写明谁在什么时候补;") print(" ② 登记表结构被我改坏了 → 改表;") print(" ③ 新增了 AI 功能没登记 → **这正是本闸门要拦的**,去补一行。") return 1 print("✅ 每个 AI 功能都登记了门槛字段,且代码侧消费方无漏登记") return 0 if __name__ == "__main__": sys.exit(main())