来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
188 lines
9.1 KiB
Python
188 lines
9.1 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
对外 LLM 出口闸门(2026-09-29 建立)
|
||
|
||
**它拦的是"文档声称了代码里不存在的合规能力"** —— 台账 RD-3 就是这种:
|
||
V3 §8.3 写「调用外部大模型前按同一脱敏规则处理载荷」,
|
||
而同一文档另一处自认 ❌ 无实现,且 AI 外发链路**脱敏引用 0 命中**
|
||
(实际按原样发往第三方代理域名)⇒ **投标/签约材料失实风险**。
|
||
|
||
本闸门读 `docs/design/20260929-AI门槛与计量登记表.md` §二,逐行校验:
|
||
1. 登记的「文件:行」**真实存在**,且该行附近确实有外发动作(fetch);
|
||
2. 登记的「目标域名」在**该文件里**出现;
|
||
3. 「脱敏实现」列写着**函数名**时,该函数**必须在代码里查得到**(防止写个不存在的函数名蒙过去);
|
||
写着 `未实现` 时,闸门**不判 FAIL** —— 但要求文档侧不得同时声称已脱敏(见第 4 条);
|
||
4. **静态扫描**:出现「经脱敏/按同一脱敏规则处理载荷」这类**声明**时,
|
||
若此刻**没有任何已登记出口**真的挂了脱敏实现,则该声明 FAIL(要么改代码、要么标明声明不成立)。
|
||
|
||
用法:
|
||
python3 tools/design-consistency/check-ai-exits.py [-v]
|
||
退出码:0 = 出口登记与代码一致;1 = 有不一致/失实声明;2 = 登记表缺失。
|
||
"""
|
||
|
||
import io
|
||
import os
|
||
import re
|
||
import subprocess
|
||
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
|
||
EXEMPT_MARK = "经审计非大模型出口"
|
||
NOT_DONE = "未实现"
|
||
|
||
# 会被扫描"是否声称已脱敏"的文档面
|
||
CLAIM_FILES = [
|
||
"docs/design/20260928-系统设计与改造方案-V3.md",
|
||
"docs/design/20260929-AI门槛与计量登记表.md",
|
||
"docs/design/20260928-立项-微信群AI自动录单.md",
|
||
]
|
||
# 声明句式(命中即要求"有出口真的挂了脱敏")
|
||
CLAIM_PAT = re.compile(
|
||
r"(调用外部大模型前[^。\n]{0,40}脱敏|按同一脱敏规则处理载荷|大模型外发[^。\n]{0,20}脱敏)")
|
||
NEGATION = ("不成立", "未实现", "无实现", "❌", "声称", "失实", "实际按原样", "做不到",
|
||
"不符", "更正", "待建能力", "没有实现", "0 命中")
|
||
|
||
|
||
def parse_md_table(text, 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("|"):
|
||
rows.append([c.strip() for c in lines[j].strip().strip("|").split("|")])
|
||
j += 1
|
||
return hdr, rows
|
||
return None, []
|
||
|
||
|
||
def code_has_symbol(name):
|
||
"""该函数名是否在源码里出现(定义或调用)。"""
|
||
p = subprocess.run(["grep", "-rq", "--include=*.ts", name, os.path.join(ROOT, "server/src")],
|
||
capture_output=True)
|
||
return p.returncode == 0
|
||
|
||
|
||
def main():
|
||
path = os.path.join(ROOT, REG)
|
||
if not os.path.exists(path):
|
||
print("❌ 登记表不存在:%s" % REG)
|
||
return 2
|
||
text = io.open(path, encoding="utf-8").read()
|
||
|
||
hdr, rows = parse_md_table(text, "出口 ID")
|
||
if not rows:
|
||
print("❌ 登记表里找不到「以 出口 ID 为首列的表格」(§二 对外 LLM 出口登记)")
|
||
return 2
|
||
|
||
problems = []
|
||
redacted_exits = 0
|
||
llm_exits = 0
|
||
print("对外出口登记 %d 行:" % len(rows))
|
||
for cells in rows:
|
||
if len(cells) < 5:
|
||
problems.append(("行字段数不足", " | ".join(cells)[:70]))
|
||
continue
|
||
eid, loc, domain, feats, redact = [c.strip() for c in cells[:5]]
|
||
m = re.match(r"`?([^`:\s]+\.ts)`?:(\d+)", loc)
|
||
if not m:
|
||
problems.append(("%s 的定位格式不对(应形如 `路径:行号`)" % eid, loc))
|
||
continue
|
||
rel, lineno = m.group(1), int(m.group(2))
|
||
fp = os.path.join(ROOT, rel)
|
||
if not os.path.exists(fp):
|
||
problems.append(("%s 登记的文件不存在" % eid, rel))
|
||
continue
|
||
src_lines = io.open(fp, encoding="utf-8", errors="ignore").read().split("\n")
|
||
if lineno > len(src_lines):
|
||
problems.append(("%s 登记的行号越界" % eid, "%s 只有 %d 行" % (rel, len(src_lines))))
|
||
continue
|
||
window = "\n".join(src_lines[max(0, lineno - 12):lineno + 12])
|
||
if "fetch(" not in window and "http" not in window.lower():
|
||
problems.append(("%s 登记位置附近看不到外发动作" % eid,
|
||
"%s:%d 前后 12 行无 fetch/http" % (rel, lineno)))
|
||
# 域名字段两种写法:`固定:<域名>`(必须在该文件里出现)/ `运行时配置:<配置键>`(静态查不到)
|
||
# ⚠️ 第一版把域名放在**窗口**里找,EX-01/02 被误报(域名常量在文件顶部、出口在 193/400 行)
|
||
# ⇒ **检查器口径错,改脚本不改文档**(本项目既定纪律)。
|
||
# ⚠️ 第二版又发现:EX-03/04 的目标域名来自运行时配置(实测 `platform_llm_config.baseUrl`
|
||
# 指向第三方代理域名),**静态根本查不到** ⇒ 不设这个写法,闸门会永远假红。
|
||
file_text = io.open(fp, encoding="utf-8", errors="ignore").read()
|
||
if domain.startswith("固定:"):
|
||
key = domain.split(":", 1)[1].split("(")[0].strip().strip("`").strip()
|
||
if key and key not in file_text:
|
||
seen = "、".join(sorted(set(re.findall(r"https?://([A-Za-z0-9._-]+)", file_text)))[:4])
|
||
problems.append(("%s 登记的固定域名在该文件里找不到" % eid,
|
||
"%s | 该文件里出现的域名:%s" % (key, seen or "(无)")))
|
||
elif domain.startswith("运行时配置:"):
|
||
cfg = domain.split(":", 1)[1].strip()
|
||
if not cfg:
|
||
problems.append(("%s 写了运行时配置但没给配置键" % eid, domain))
|
||
else:
|
||
problems.append(("%s 的目标域名须以「固定:」或「运行时配置:」开头" % eid, domain[:60]))
|
||
|
||
# 脱敏列判定
|
||
if redact == EXEMPT_MARK:
|
||
print(" · %s 非大模型出口(豁免)" % eid)
|
||
elif redact == NOT_DONE:
|
||
llm_exits += 1
|
||
print(" ⚠️ %s 路径=%s 脱敏=未实现" % (eid, rel))
|
||
else:
|
||
llm_exits += 1
|
||
syms = re.findall(r"`([A-Za-z_][A-Za-z0-9_]*)`", redact)
|
||
if not syms:
|
||
problems.append(("%s 的脱敏列既不是函数名也不是「未实现」" % eid, redact))
|
||
for s in syms:
|
||
if not code_has_symbol(s):
|
||
problems.append(("%s 声称用 `%s` 脱敏,但代码里查不到该符号" % (eid, s), rel))
|
||
else:
|
||
redacted_exits += 1
|
||
print(" ✅ %s 路径=%s 脱敏=%s(代码命中)" % (eid, rel, s))
|
||
|
||
# 第 4 条:文档是不是声称了已脱敏
|
||
claim_found = False
|
||
for rel in CLAIM_FILES:
|
||
fp = os.path.join(ROOT, rel)
|
||
if not os.path.exists(fp):
|
||
continue
|
||
txt = io.open(fp, encoding="utf-8").read()
|
||
for i, line in enumerate(txt.split("\n"), 1):
|
||
if not CLAIM_PAT.search(line):
|
||
continue
|
||
claim_found = True
|
||
para = txt.split("\n\n")
|
||
# 同一空行段落里是否有否定语(标明"该声明不成立")
|
||
para_hit = next((p for p in para if line in p), line)
|
||
negated = any(k in para_hit for k in NEGATION)
|
||
if VERBOSE or not negated:
|
||
print(" 声明 %s:%d %s" % (rel, i, line.strip()[:74]))
|
||
if not negated and redacted_exits == 0:
|
||
problems.append(("文档声称大模型外发已脱敏,但没有任何出口挂了脱敏实现",
|
||
"%s:%d" % (rel, i)))
|
||
|
||
print("\nLLM 出口 %d 个 | 其中挂了脱敏实现 %d 个 | 声明扫描 %d 处"
|
||
% (llm_exits, redacted_exits, 1 if claim_found else 0))
|
||
if problems:
|
||
print("\n🔴 %d 处不满足:" % len(problems))
|
||
for kind, detail in problems[:30]:
|
||
print(" - [%s] %s" % (kind, detail))
|
||
print("\n处置原则(**两条路都行,但不许「只改文档」**):")
|
||
print(" ① 代码侧:加一个统一脱敏函数并在每个出口调用 —— 涉及产品代码,需批准;")
|
||
print(" ② 文档侧:把那句声明改成「**该声明不成立**(实测脱敏引用 0 命中)」,")
|
||
print(" 并登记为对外材料风险 —— 这是 RD-3 要求的最低限度处置。")
|
||
return 1
|
||
print("✅ 出口登记与代码一致;文档未声称不存在的脱敏能力")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|