fix(version): 就绪看目标提交的顶部版本 + 禁用自动发布路径
build / build (push) Successful in 52s

补两处残余(本轮不推、不打真实 tag、不部署;全部用临时仓库与 spy 验):

1) version_in_commit 必须比"目标提交 CHANGELOG 的**顶部**已发布版本",不是"历史里出现过":
   - 新增 top_version_in_commit(),version_in_commit() 直接比它;预检输出把该提交的顶部版本打出来
     (诊断可审计:例如"顶部是 `## [0.3.0]`,不是 0.2.0"),阻断信息里也带上真实顶部版本。
   - 顺带兜住"最新在最上面 / 版本只增不减"的排序要求。
   - 负例(自检 + 夹具各一遍):顶部 0.3.0、历史里 0.2.0 ⇒ 候选 0.2.0 拒绝;候选 0.3.0 通过。

2) 按只读约定**禁用 --execute 自动发布路径**:
   - 删掉写路径(打 tag / 推 tag / 建 Release 的代码整段移除),`--execute` / `--approve-version` /
     `--approve-sha` 只保留参数兼容(不报 unknown argument),传了就**明确拒绝**(rc=2)。
   - 拒绝发生在收集事实之前 ⇒ **零写操作、零 API 调用**;自检用 spy 断言拒绝路径
     "一个子进程都没跑、一个 API 都没发",并扫描生产路径不许出现直接执行 git 写命令的行。
   - 输出里也不再推荐自动路径;RELEASE_RUNBOOK 第 4 步改为由人执行。

验证:release_preflight --self-test 绿(就绪 20 分支 + 顶部/历史两态 + --execute 拒绝零写 +
源码写路径扫描);反向夹具 84/0(含 --execute rc=2 且跑完仓里 tag 不变);make verify 15/15 success。
This commit is contained in:
姚章浩
2026-10-13 15:48:50 +08:00
parent 7a9d51cc70
commit 97f4b29661
3 changed files with 148 additions and 73 deletions
+36 -4
View File
@@ -686,15 +686,15 @@ def seed(d):
for args in (["init", "-q", "-b", "main"], ["add", "-A"], ["commit", "-q", "-m", "chore: 夹具"]):
subprocess.run(["git", *args], cwd=d, check=True, capture_output=True)
def pre(d):
p = subprocess.run(["python3", "scripts/release_preflight.py", "--version", "0.2.0", "--no-api"],
def pre(d, *args):
p = subprocess.run(["python3", "scripts/release_preflight.py", *args, "--no-api"],
cwd=d, capture_output=True, text=True)
return p.returncode, (p.stdout + p.stderr)
cases = []
with tempfile.TemporaryDirectory() as tmp:
d = pathlib.Path(tmp) / "r"; d.mkdir(); seed(d)
rc1, t1 = pre(d)
rc1, t1 = pre(d, "--version", "0.2.0")
cases.append(("记录只在工作区(没提交)⇒ 明说『还没写进目标提交』且不就绪",
rc1 == 1 and "还没写进目标提交" in t1 and "现在**不能**发" in t1))
cases.append(("就绪判定里含『准备建议』与『发布就绪判定』两段(不是混在一起)",
@@ -709,7 +709,7 @@ with tempfile.TemporaryDirectory() as tmp:
subprocess.run(["git", "add", "-A"], cwd=d, check=True, capture_output=True)
subprocess.run(["git", "commit", "-q", "-m", "chore(release): 0.2.0 记录"], cwd=d, check=True,
capture_output=True)
rc2, t2 = pre(d)
rc2, t2 = pre(d, "--version", "0.2.0")
cases.append(("记录进了提交 ⇒ 『还没写进目标提交』这条阻塞**消失**",
"还没写进目标提交" not in t2))
cases.append(("仍然不就绪(没核对远端 / 没有 CI 证据)⇒ 不许给可发结论",
@@ -717,11 +717,43 @@ with tempfile.TemporaryDirectory() as tmp:
cases.append(("候选记录存在 ≠ 已发布:不该出现『已存在』类误判",
"已存在" not in t2.replace("不存在(可发)", "")))
# 候选版本必须“**是**目标提交里的顶部版本”:只在历史里出现过不算
d2 = pathlib.Path(tmp) / "r2"; d2.mkdir()
(d2 / "scripts").mkdir(parents=True); (d2 / "docs").mkdir(); (d2 / ".gitea").mkdir()
for n in ("check_changelog.py", "release_preflight.py"):
(d2 / "scripts" / n).write_text(pathlib.Path("scripts", n).read_text(encoding="utf-8"),
encoding="utf-8")
(d2 / "docs" / "RELEASE_RUNBOOK.md").write_text("# 发布说明\n", encoding="utf-8")
(d2 / ".gitea" / "required-contexts.txt").write_text("build / build (push)\n", encoding="utf-8")
(d2 / "CHANGELOG.md").write_text(
"# Changelog\n\n## [0.3.0] - 2026-03-01\n\n### Added\n\n- 新\n\n"
"## [0.2.0] - 2026-02-01\n\n### Fixed\n\n- 旧\n", encoding="utf-8")
(d2 / "no-hooks").mkdir(exist_ok=True)
for args in (["init", "-q", "-b", "main"], ["add", "-A"], ["commit", "-q", "-m", "chore: 夹具"]):
subprocess.run(["git", *args], cwd=d2, check=True, capture_output=True)
_, t3 = pre(d2, "--version", "0.2.0")
cases.append(("候选版本只在**历史**里(顶部是 0.3.0)⇒ 明确『还没写进目标提交』",
"还没写进目标提交" in t3))
_, t4 = pre(d2, "--version", "0.3.0")
cases.append(("候选版本就是该提交的**顶部**版本 ⇒ 该条阻塞消失", "还没写进目标提交" not in t4))
bad = [n for n, okv in cases if not okv]
for n, okv in cases:
print(("✅ " if okv else "❌ ") + n)
raise SystemExit(1 if bad else 0)
PY
pycheck "预检:--execute 自动发布路径已禁用(明确拒绝、rc=2、零写操作:跑完仓里 tag 不变)" <<'PY'
import subprocess
before = subprocess.run(["git", "tag", "-l"], capture_output=True, text=True).stdout.split()
p = subprocess.run(["python3", "scripts/release_preflight.py", "--execute",
"--approve-version", "9.9.9", "--approve-sha", "0" * 40],
capture_output=True, text=True)
after = subprocess.run(["git", "tag", "-l"], capture_output=True, text=True).stdout.split()
okv = (p.returncode == 2 and "已禁用" in (p.stderr or "") and not p.stdout.strip()
and before == after)
print(("✅ " if okv else "❌ ") + f"rc={p.returncode}|tag {before} → {after}|输出为空={not p.stdout.strip()}")
raise SystemExit(0 if okv else 1)
PY
if grep -qE "git tag -a|--data-binary|POST" /tmp/kg-pf.log; then
no "预检输出里出现了写操作迹象"
else