Files
SupplierSystem/tools/design-consistency/check-structure.py
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库: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 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

168 lines
8.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
交付文档结构自检(2026-09-29 建立)
为什么要单独一个:`check.py` 查的是"结论有没有落到各展示面",**不查文档本身是否还成立**。
本项目文档被大量脚本化批量编辑,最容易出的结构性问题恰好不会被内容断言发现:
· 代码围栏 ```` ``` ```` 少写一个 ⇒ 后面半篇变成代码块
· 表格某行多/少一格、或单元格里有**未转义**的 `|` ⇒ 整张表错位
· 标题重复(**注意:V3 的重复是设计使然**,见下)
· 批量替换把表格行内容吃掉一半
用法:
python3 tools/design-consistency/check-structure.py
退出码:0 = 通过;1 = 有问题。
判定口径说明(这两条都是被假警报教出来的):
1. **`\\|` 是转义管道,不算分隔符**;``` `...` ``` 代码跨距里的 `|` 也不算。
直接 `line.count("|")` 会把大量正确行判成错的。
2. **只查 `##`/`###` 的编号重复,不查 `####`**:V3 是总册 + 11 册分册的合并物,
每册自带 `#### 1.1`/`#### 2.3` 这类**分册内小节号** ⇒ `####` 重复是结构使然。
**第一版脚本按 `#{2,4}` 查,一次报出 71 处、全部是假阳性** —— 这是"检查器口径错、
不是文档错"的实例,按要求改脚本而非改文档。真出错的是 `### 16.4` 那种章级小节号重复。
"""
import io
import os
import re
import sys
import collections
# ROOT 可用 DC_ROOT 覆盖(供 check-self-test.py 在临时目录里注入缺陷做**反向对照**)。
# 不设这个口子,自测就只能"相信脚本",而本项目的教训恰恰是"相信"不可靠。
ROOT = os.environ.get("DC_ROOT") or os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
FILES = {
"V3": "docs/design/20260928-系统设计与改造方案-V3.md",
"开工单": "docs/design/20260928-V3可施工任务分解(开工单).md",
"立项": "docs/design/20260928-立项-微信群AI自动录单.md",
"整体梳理": "docs/design/20260929-整体梳理(工作总览与验收).md",
"台账": "docs/audit/20260929-对抗式验证-缺陷台账.md",
"README": "docs/design/README.md",
}
def cells_count(line: str) -> int:
"""表格行的单元格数(转义管道与代码跨距内的管道都不算分隔符)。"""
tmp = line.replace("\\|", "\x00")
tmp = re.sub(r"`[^`]*`", "\x01", tmp)
return tmp.strip().strip("|").count("|") + 1
def check_file(name: str, path: str):
text = io.open(path, encoding="utf-8").read()
lines = text.split("\n")
problems = []
# ① 代码围栏必须配对
fences = [i for i, l in enumerate(lines, 1) if l.lstrip().startswith("```")]
if len(fences) % 2:
problems.append(("围栏不配对", f"共 {len(fences)} 个 ```(奇数);最后一个在行 {fences[-1]}"))
# ② 表格列数一致(逐表块校验,以**表头**为基准,再与分隔行互校)
#
# ⚠️ 2026-09-29 补:**旧版以"分隔行"为基准,查不出"表头格数与分隔行不一致"**。
# 真实盲区(我注入缺陷时才发现):把 `| # | 结论 | 性质 |` 改成 4 格而分隔行仍 3 格 ——
# 旧版第②条从**分隔行**开始查(data 行跟分隔行一致,PASS),
# 第③条只查"表头后面有没有分隔行"(有,PASS)⇒ **两处都放过,坏表提交成功**。
# 这条正是"门禁没被反向对照过就不能信"的实例。
i = 0
seen_sep = set()
while i < len(lines):
if lines[i].startswith("|") and i + 1 < len(lines) and re.match(r"^\|[\s\-:|]+\|?\s*$", lines[i + 1] or ""):
base = cells_count(lines[i]) # 基准 = 表头(不是分隔行)
sep_c = cells_count(lines[i + 1])
seen_sep.add(i + 1)
if sep_c != base:
problems.append(("表头与分隔行格数不符(表格渲染会错位)",
f"行 {i + 1}(表头 {base} 格)vs 行 {i + 2}(分隔行 {sep_c} 格) | {lines[i][:60]}"))
j = i + 2
while j < len(lines) and lines[j].startswith("|"):
c = cells_count(lines[j])
if c != base:
problems.append(("表格列数不符", f"行 {j + 1}:{c} 格 vs 表头 {base} 格 | {lines[j][:70]}"))
j += 1
i = j
else:
i += 1
# ②b 连续两个分隔行这种形态(真被批量脚本搞坏过):主循环只认了第一个,
# 第二个会成为"新表的表头"去扫下面的数据行 —— 这里单独与它上方的表头互校。
for k, l in enumerate(lines):
if k in seen_sep or not re.match(r"^\|[\s\-:|]+\|?\s*$", l or ""):
continue
if k == 0 or not re.match(r"^\|[\s\-:|]+\|?\s*$", lines[k - 1] or ""):
continue
if k >= 2 and lines[k - 2].startswith("|"):
a, b = cells_count(lines[k - 2]), cells_count(l)
if a != b:
problems.append(("表头与分隔行格数不符(表格渲染会错位)",
f"行 {k - 1}(表头 {a} 格)vs 行 {k + 1}(分隔行 {b} 格) | {lines[k - 2][:60]}"))
# ③ 表头必须**紧跟**分隔行 —— 否则表格不渲染
# 这条是 2026-09-29 补的:我用脚本往"表头与数据行之间"插入说明文字,
# 把 P1/P2/P3 三张表拆成了「表头 + 说明 + 分隔行 + 数据行」,
# 而**旧版检查器只查"表格列数一致",完全没发现** —— 表格在渲染时已经坏了。
for i, l in enumerate(lines):
if not l.startswith("|"):
continue
nxt = lines[i + 1] if i + 1 < len(lines) else ""
prev_looks_like_sep = re.match(r"^\|[\s\-:|]+\|?\s*$", l)
if prev_looks_like_sep:
continue
# 本行是表头(首行),下一行必须是分隔行
if not re.match(r"^\|[\s\-:|]+\|?\s*$", nxt):
# 排除"上一行也是数据行"的情况:只在表格块的首行要求
if i == 0 or not lines[i - 1].startswith("|"):
problems.append(("表头后缺分隔行(表格不渲染)",
f"行 {i + 1}:{l[:60]} | 下一行是「{nxt[:40]}」"))
# ④ 疑似重复编号标题(同级别 + 同编号,如两个 `### 16.4`)
# ⚠️ **只查 `##` / `###`**(2026-09-29 实测教训):V3 是 11 册分册的合并物,
# 每册自带 `#### 1.1`、`#### 2.3` 这类**分册内小节号** ⇒ `####` 编号重复是**结构使然**,
# 第一版脚本按 `#{2,4}` 查,一次报出 **71 处全部是假阳性**。
# 真出错的是 `### 16.4` 这种"章级小节号"重复(曾真出现过两个 `### 16.4`)。
heads = [(i, l.strip()) for i, l in enumerate(lines, 1) if re.match(r"^#{2,3} ", l)]
keyed = collections.defaultdict(list)
for ln, h in heads:
m = re.match(r"^(#{2,3})\s+([0-9]+(?:\.[0-9]+)*)", h)
if m:
keyed[(m.group(1), m.group(2))].append(ln)
for (lvl, num), lns in keyed.items():
if len(lns) > 1:
problems.append(("重复编号标题", f"{lvl} {num} 出现 {len(lns)} 次(行 {lns})"))
return problems
def main():
total = 0
for name, rel in FILES.items():
path = os.path.join(ROOT, rel)
if not os.path.exists(path):
print(f"缺少文件: {rel}")
total += 1
continue
probs = check_file(name, path)
lines = len(io.open(path, encoding="utf-8").read().split("\n"))
flag = "OK " if not probs else "FAIL"
print(f"{flag} {name:8s} {lines:6d} 行 问题 {len(probs)}")
for kind, detail in probs:
print(f" [{kind}] {detail}")
total += len(probs)
print(f"\n=== 结构问题合计 {total} 处 ===")
if total:
print("提示:先判断是「真结构错」还是「检查口径错」——"
"本脚本已排除转义管道与代码跨距;若仍有误报,改本脚本而不是改文档。")
return 1
return 0
if __name__ == "__main__":
sys.exit(main())