dsh-project-dept 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,10 +18,15 @@
18
18
  - 路径一律相对项目根,项目根 = 配置文件所在目录。
19
19
  """
20
20
  import argparse
21
+ import hashlib
21
22
  import io
22
23
  import json
23
24
  import os
24
25
  import re
26
+
27
+ # v1.3.7:作废/替代标记的唯一识别口径——方括号 token,整行任意位置,大小写不敏感。
28
+ # 售后事故:合法表格行 `B2 | 相关长度 | [已作废] |`(无首竖线)曾 exit 0 静默放行。
29
+ RETIRED_MARK_RE = re.compile(r"\[\s*(?:已作废|superseded)\s*\]", re.IGNORECASE)
25
30
  import sys
26
31
 
27
32
  EXIT_PASS = 0
@@ -124,11 +129,28 @@ def classify_gate(ok_structural, open_debts):
124
129
  return EXIT_PASS
125
130
 
126
131
 
132
+ def _sha12(path):
133
+ """文件内容 sha256 前 12 位;文件不存在返回 MISSING。
134
+
135
+ P2(外部调研 dsh omni-router / Maat 的教训):复核是人与人之间传文件,
136
+ 若被验包在复核期间被改过,判决就不可复现。锚哈希让"验的是哪一版"可当场核对。
137
+ """
138
+ if not os.path.isfile(path):
139
+ return "MISSING"
140
+ try:
141
+ with open(path, "rb") as fh:
142
+ return hashlib.sha256(fh.read()).hexdigest()[:12]
143
+ except OSError:
144
+ return "UNREADABLE"
145
+
146
+
127
147
  def main(argv=None):
128
148
  ap = argparse.ArgumentParser(description="Project department pre-start gate")
129
149
  ap.add_argument("--config", help="path to dept.config.json (default: walk up from cwd)")
130
150
  ap.add_argument("--root", default=os.getcwd(), help="where to start looking for the config")
131
151
  ap.add_argument("--quiet", action="store_true", help="only print the verdict line")
152
+ ap.add_argument("--json", action="store_true",
153
+ help="print one final JSON line (code/verdict/anchor/counts) after the verdict")
132
154
  args = ap.parse_args(argv)
133
155
 
134
156
  cfg_path = find_config(args.root, args.config)
@@ -176,18 +198,25 @@ def main(argv=None):
176
198
  role_dir = os.path.join(ledger, cfg.get("role_dir", "岗位"))
177
199
  artefact_dir = os.path.join(ledger, cfg.get("artefact_dir", "out"))
178
200
 
201
+ # ---- anchor:被审快照的指纹(config + 四台账),用于复核可复现
202
+ _anchor_parts = ["config=%s" % _sha12(cfg_path)]
203
+ for _k in ("board", "flow", "register", "baseline"):
204
+ _anchor_parts.append("%s=%s" % (os.path.basename(files[_k]), _sha12(files[_k])))
205
+ anchor = " ".join(_anchor_parts)
206
+
179
207
  structural = True
180
208
  violations = []
181
209
  warnings = []
182
210
  ok = []
183
211
 
184
212
  def say(tag, msg):
185
- if not args.quiet:
213
+ if not args.quiet and not args.json:
186
214
  print(" [%s] %s" % (tag, msg))
187
215
 
188
- print("=" * 44)
189
- print(" DEPARTMENT GATE — %s" % cfg.get("project_name", os.path.basename(root)))
190
- print("=" * 44)
216
+ if not args.json and not args.quiet:
217
+ print("=" * 44)
218
+ print(" DEPARTMENT GATE — %s" % cfg.get("project_name", os.path.basename(root)))
219
+ print("=" * 44)
191
220
 
192
221
  # ---- 1 台账文件齐否(缺 = 结构性问题)
193
222
  missing = [k for k, p in files.items() if not os.path.isfile(p)]
@@ -300,6 +329,43 @@ def main(argv=None):
300
329
  for ln, l in untraced[:3]:
301
330
  print(" %s:%d %s" % (os.path.basename(files["baseline"]), ln, l[:110]))
302
331
 
332
+ # ---- 7b 基准作废/替代必须有变更记录(P-15-9:规则要挂在强制产出物上)
333
+ # 判定口径(v1.3.6 明确写死,别再引入未声明的长度阈值):
334
+ # * 作废行 = 行首标记(含大小写变体)或表格单元内的标记,两种写法同口径;
335
+ # * 「有记录」= 变更记录文件存在,且至少有一行非空、非 Markdown 标题的正文;
336
+ # * 有作废行却没配 baseline_changelog ⇒ 结构性阻断(否则 P-15-9 可以绕过)。
337
+ if base_text is not None:
338
+ retired = []
339
+ for i, l in enumerate(base_text.splitlines(), 1):
340
+ s = l.strip()
341
+ # v1.3.8:唯一口径 = 正则整行搜索,**不做任何前置字面量预筛**。
342
+ # 售后事故:v1.3.7 在正则前留了 `"[已作废]" in s or "[superseded]" in low` 预筛,
343
+ # 于是 `[ 已作废 ]`、`[superseded ]`、`[ superseded ]`(括号内含空白)被预筛挡掉、静默放行,
344
+ # 与 README「整行任意位置匹配」的声明不符(v1.3.7 档 A 复核人实测)。
345
+ if RETIRED_MARK_RE.search(s):
346
+ retired.append((i, s))
347
+ if retired:
348
+ cl_name = cfg.get("baseline_changelog") if isinstance(cfg, dict) else None
349
+ if not cl_name:
350
+ structural = False
351
+ violations.append(
352
+ "基准清单有 %d 条已作废/被替代条目,但 dept.config.json 没配 "
353
+ "baseline_changelog —— 作废必须留记录,先配这个键(P-15-9)" % len(retired))
354
+ else:
355
+ cl_path = os.path.join(ledger, cl_name)
356
+ cl_text = strip_fences(read_text(cl_path) or "")
357
+ cl_body = [l.strip() for l in cl_text.splitlines()
358
+ if l.strip() and not l.strip().startswith("#")]
359
+ if not cl_body:
360
+ structural = False
361
+ violations.append(
362
+ "基准清单有 %d 条已作废/被替代基准,但 %s 不存在或只有标题 —— 作废必须写明"
363
+ "旧值/新值或理由/superseded_by/哪些下游要重跑(P-15-9)"
364
+ % (len(retired), cl_name))
365
+ else:
366
+ ok.append("基准变更记录:%d 条作废/替代条目有记录(%s)"
367
+ % (len(retired), cl_name))
368
+
303
369
  # ---- 8 方向复盘小节是否还是模板
304
370
  pre = section_lines(board_text, r"方向复盘|pre-?mortem")
305
371
  if pre:
@@ -308,7 +374,9 @@ def main(argv=None):
308
374
  warnings.append("「方向复盘」小节还是模板 —— 收口前必须填三问(见 references/self-skepticism.md)")
309
375
 
310
376
  # ---- 输出
311
- if not args.quiet:
377
+ # --json 时 stdout 只能有 JSON:所有人类可读输出统一走 human 开关(复核人用 json 解析)
378
+ human = (not args.quiet) and (not args.json)
379
+ if human:
312
380
  for m in ok:
313
381
  say("i", m)
314
382
  for m in warnings:
@@ -316,7 +384,8 @@ def main(argv=None):
316
384
  for m in violations:
317
385
  say("x", m)
318
386
  code = classify_gate(structural, open_debts)
319
- print("-" * 44)
387
+ if human:
388
+ print("-" * 44)
320
389
  # V1/P7:以前无论有没有告警都印「(有告警)」,零告警时是假话。按实际条数输出。
321
390
  verdicts = {
322
391
  EXIT_PASS: ("结论:可以开工(exit 0,%s)。"
@@ -324,7 +393,21 @@ def main(argv=None):
324
393
  EXIT_BLOCKED: "结论:禁止开工(exit 1)—— 先修结构性问题。",
325
394
  EXIT_DECISION: "结论:需先决策(exit 2)—— 逐条「做掉 / 豁免(理由+复查日期)/ 降级」后再开工。",
326
395
  }
327
- print(verdicts[code])
396
+ if human:
397
+ print(" [anchor] %s" % anchor)
398
+ print(verdicts[code])
399
+ if args.json:
400
+ print(json.dumps({
401
+ "code": code,
402
+ "verdict": verdicts[code],
403
+ "anchor": anchor,
404
+ "config": cfg_path,
405
+ "warnings": len(warnings),
406
+ "warnings_list": warnings,
407
+ "violations": len(violations),
408
+ "violations_list": violations,
409
+ "open_debts": len(open_debts) if isinstance(open_debts, (list, tuple)) else open_debts,
410
+ }, ensure_ascii=False))
328
411
  return code
329
412
 
330
413