#!/usr/bin/env python3 """check_command_drift.py — ssw-loop 模板命令漂移门 (P7, 2026-07-24) 模板里 `dacli --flag` 用到的选项, 必须在生成参考里存在。 参考 = references/command-reference.generated.md (由 dacli 命令树生成)。 漂移 (模板用了已删/改名/拼错的选项) → 告警。advisory: exit 0 (稳定后可改阻塞)。 跑: python3 .claude/skills/ssw-loop/scripts/check_command_drift.py """ import os, re, sys, glob HERE = os.path.dirname(os.path.abspath(__file__)) SKILL = os.path.dirname(HERE) REF = os.path.join(SKILL, 'references', 'command-reference.generated.md') FLAG = re.compile(r'--[A-Za-z][A-Za-z0-9-]*') # 参考里 `--flag` (backtick 包裹) FLAG_USED = re.compile(r'(?<=\s)--[A-Za-z][A-Za-z0-9-]*') # 模板里: 必须空格前导 (排除 CSS .a--b / 锚点 #x--y) if not os.path.exists(REF): print(f"⚠ 参考未生成: {REF}\n 先跑: npx tsx dacli/scripts/gen-command-reference.ts") sys.exit(0) # advisory # 1) 参考里的合法选项 (含 backtick 包裹的 `--flag`) ref_text = open(REF, encoding='utf-8').read() valid = set(FLAG.findall(ref_text)) # 2) 扫模板/参考里的 dacli 命令行选项 (排除生成文件本身) used = {} # flag -> [file:line] targets = (glob.glob(os.path.join(SKILL, 'templates', '**', '*.tmpl'), recursive=True) + glob.glob(os.path.join(SKILL, 'templates', '**', '*.md'), recursive=True) + glob.glob(os.path.join(SKILL, 'references', '*.md')) + [os.path.join(SKILL, 'SKILL.md')]) for f in targets: if os.path.abspath(f) == os.path.abspath(REF): continue for i, line in enumerate(open(f, encoding='utf-8'), 1): if 'dacli ' not in line: continue for m in FLAG_USED.findall(line): used.setdefault(m, []).append(f"{os.path.relpath(f, SKILL)}:{i}") # 3) 报告漂移 (模板用了参考里没有的选项) drift = {f: locs for f, locs in used.items() if f not in valid} if not drift: print(f"✓ 命令漂移门: 模板 {len(used)} 个选项全部在生成参考中 (无漂移)") sys.exit(0) print(f"⚠ 命令漂移门: {len(drift)} 个模板选项不在生成参考中 (可能已删/改名/拼错):") for f in sorted(drift): print(f" {f} ← {drift[f][0]}" + (f" (+{len(drift[f])-1})" if len(drift[f]) > 1 else "")) print(" 修复: 更新模板选项, 或重跑 gen-command-reference.ts 若 CLI 刚改过") sys.exit(0) # advisory (稳定后改 sys.exit(1) 阻塞)