teamsmith 0.0.0-stage → 0.1.2

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.
Files changed (245) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +158 -2
  3. package/bin/team.mjs +59 -0
  4. package/install.sh +94 -0
  5. package/package.json +37 -4
  6. package/skills/teamsmith/CHANGELOG.md +763 -0
  7. package/skills/teamsmith/SKILL.md +312 -0
  8. package/skills/teamsmith/extension/team-bg.ts +435 -0
  9. package/skills/teamsmith/extension/team-inbox-watch.ts +1412 -0
  10. package/skills/teamsmith/extension/team-notify.ts +663 -0
  11. package/skills/teamsmith/references/agent-adapters.md +643 -0
  12. package/skills/teamsmith/references/config.md +477 -0
  13. package/skills/teamsmith/references/meeting.md +124 -0
  14. package/skills/teamsmith/references/memory.md +133 -0
  15. package/skills/teamsmith/references/migration.md +227 -0
  16. package/skills/teamsmith/references/openspec.md +222 -0
  17. package/skills/teamsmith/references/philosophy.md +95 -0
  18. package/skills/teamsmith/references/protocol.md +712 -0
  19. package/skills/teamsmith/references/troubleshooting.md +1384 -0
  20. package/skills/teamsmith/references/workflows.md +448 -0
  21. package/skills/teamsmith/scripts/lib/cmd-agents.sh +1709 -0
  22. package/skills/teamsmith/scripts/lib/cmd-bg.sh +284 -0
  23. package/skills/teamsmith/scripts/lib/cmd-bootstrap.sh +311 -0
  24. package/skills/teamsmith/scripts/lib/cmd-config.sh +1156 -0
  25. package/skills/teamsmith/scripts/lib/cmd-death.sh +562 -0
  26. package/skills/teamsmith/scripts/lib/cmd-docs.sh +149 -0
  27. package/skills/teamsmith/scripts/lib/cmd-draft.sh +124 -0
  28. package/skills/teamsmith/scripts/lib/cmd-init.sh +212 -0
  29. package/skills/teamsmith/scripts/lib/cmd-meeting.sh +904 -0
  30. package/skills/teamsmith/scripts/lib/cmd-outbox.sh +196 -0
  31. package/skills/teamsmith/scripts/lib/cmd-project.sh +642 -0
  32. package/skills/teamsmith/scripts/lib/cmd-review.sh +1393 -0
  33. package/skills/teamsmith/scripts/lib/cmd-status.sh +1399 -0
  34. package/skills/teamsmith/scripts/lib/cmd-update.sh +191 -0
  35. package/skills/teamsmith/scripts/lib/cmd-watch.sh +1681 -0
  36. package/skills/teamsmith/scripts/lib/common.sh +4964 -0
  37. package/skills/teamsmith/scripts/lib/draft-entry.sh +51 -0
  38. package/skills/teamsmith/scripts/lib/outbox.sh +2042 -0
  39. package/skills/teamsmith/scripts/lib/tmux-argv.sh +165 -0
  40. package/skills/teamsmith/scripts/monitor.mjs +616 -0
  41. package/skills/teamsmith/scripts/panel/README.md +192 -0
  42. package/skills/teamsmith/scripts/panel/build.sh +47 -0
  43. package/skills/teamsmith/scripts/panel/bun.lock +109 -0
  44. package/skills/teamsmith/scripts/panel/draft-send.sh +68 -0
  45. package/skills/teamsmith/scripts/panel/package.json +24 -0
  46. package/skills/teamsmith/scripts/panel/panel.js +388 -0
  47. package/skills/teamsmith/scripts/panel/src/App.tsx +2150 -0
  48. package/skills/teamsmith/scripts/panel/src/clipboard.ts +154 -0
  49. package/skills/teamsmith/scripts/panel/src/compose.ts +691 -0
  50. package/skills/teamsmith/scripts/panel/src/data.ts +555 -0
  51. package/skills/teamsmith/scripts/panel/src/format.ts +50 -0
  52. package/skills/teamsmith/scripts/panel/src/layout.ts +2562 -0
  53. package/skills/teamsmith/scripts/panel/src/main.tsx +894 -0
  54. package/skills/teamsmith/scripts/panel/src/markdown.ts +340 -0
  55. package/skills/teamsmith/scripts/panel/src/sanitize.ts +42 -0
  56. package/skills/teamsmith/scripts/panel/src/settings.ts +154 -0
  57. package/skills/teamsmith/scripts/panel/src/strings/en.ts +416 -0
  58. package/skills/teamsmith/scripts/panel/src/strings/index.ts +65 -0
  59. package/skills/teamsmith/scripts/panel/src/strings/types.ts +9 -0
  60. package/skills/teamsmith/scripts/panel/src/strings/zh.ts +414 -0
  61. package/skills/teamsmith/scripts/panel/src/theme.ts +116 -0
  62. package/skills/teamsmith/scripts/panel/src/types.ts +652 -0
  63. package/skills/teamsmith/scripts/panel/src/width.ts +105 -0
  64. package/skills/teamsmith/scripts/panel/tsconfig.json +16 -0
  65. package/skills/teamsmith/scripts/shim/signal-gate +156 -0
  66. package/skills/teamsmith/scripts/shim/tmux +403 -0
  67. package/skills/teamsmith/scripts/team +143 -0
  68. package/skills/teamsmith/templates/AGENTS.section.md.tmpl +147 -0
  69. package/skills/teamsmith/templates/BOARD.md.tmpl +18 -0
  70. package/skills/teamsmith/templates/DECISIONS.md.tmpl +22 -0
  71. package/skills/teamsmith/templates/OWNERSHIP.md.tmpl +25 -0
  72. package/skills/teamsmith/templates/PROTOCOL.md.tmpl +84 -0
  73. package/skills/teamsmith/templates/ROADMAP.md.tmpl +29 -0
  74. package/skills/teamsmith/templates/config.sh.tmpl +106 -0
  75. package/skills/teamsmith/templates/memory-seed.md.tmpl +46 -0
  76. package/skills/teamsmith/templates/pm-prompt.md.tmpl +110 -0
  77. package/skills/teamsmith/templates/report.md.tmpl +36 -0
  78. package/skills/teamsmith/templates/task.md.tmpl +65 -0
  79. package/skills/teamsmith/templates/threads-README.md +17 -0
  80. package/skills/teamsmith/tests/bg-ledger-harness.mjs +682 -0
  81. package/skills/teamsmith/tests/bg-ledger.sh +307 -0
  82. package/skills/teamsmith/tests/checkout-shape-probe.sh +800 -0
  83. package/skills/teamsmith/tests/config-cli.sh +1562 -0
  84. package/skills/teamsmith/tests/container-tmux.sh +493 -0
  85. package/skills/teamsmith/tests/coverage-inventory.sh +173 -0
  86. package/skills/teamsmith/tests/death-cause.sh +564 -0
  87. package/skills/teamsmith/tests/delivery-truth.sh +1231 -0
  88. package/skills/teamsmith/tests/fake-tui.py +450 -0
  89. package/skills/teamsmith/tests/fixtures/delivery-truth-real/README.md +60 -0
  90. package/skills/teamsmith/tests/fixtures/delivery-truth-real/judge-second.py +139 -0
  91. package/skills/teamsmith/tests/fixtures/delivery-truth-real/mock-server.py +29 -0
  92. package/skills/teamsmith/tests/fixtures/delivery-truth-real/observe.ts +20 -0
  93. package/skills/teamsmith/tests/fixtures/delivery-truth-real/run-case.sh +79 -0
  94. package/skills/teamsmith/tests/fixtures/delivery-truth-real/scenario.sh +175 -0
  95. package/skills/teamsmith/tests/fixtures/p55/flip-p49.sh +229 -0
  96. package/skills/teamsmith/tests/fixtures/p71-incident/README.md +22 -0
  97. package/skills/teamsmith/tests/fixtures/p71-incident/dev2.seen +8 -0
  98. package/skills/teamsmith/tests/fixtures/p71-incident/dev2.wake +9 -0
  99. package/skills/teamsmith/tests/fixtures/p71-incident/pm-nudges.wake +3 -0
  100. package/skills/teamsmith/tests/fixtures/signal-lint-cases.txt +40 -0
  101. package/skills/teamsmith/tests/flip-glue.sh +162 -0
  102. package/skills/teamsmith/tests/flip-m12-26c.sh +129 -0
  103. package/skills/teamsmith/tests/flip-m12.sh +185 -0
  104. package/skills/teamsmith/tests/flip-m16.sh +166 -0
  105. package/skills/teamsmith/tests/flip-m17.sh +204 -0
  106. package/skills/teamsmith/tests/flip-m33.sh +284 -0
  107. package/skills/teamsmith/tests/flip-m34.sh +96 -0
  108. package/skills/teamsmith/tests/flip-m37.sh +185 -0
  109. package/skills/teamsmith/tests/flip-m4.3.sh +375 -0
  110. package/skills/teamsmith/tests/flip-m43.sh +128 -0
  111. package/skills/teamsmith/tests/flip-m44.sh +262 -0
  112. package/skills/teamsmith/tests/flip-m45.sh +244 -0
  113. package/skills/teamsmith/tests/flip-m46.sh +238 -0
  114. package/skills/teamsmith/tests/flip-m48.sh +304 -0
  115. package/skills/teamsmith/tests/flip-m6.2.sh +205 -0
  116. package/skills/teamsmith/tests/flip-m6.3.sh +283 -0
  117. package/skills/teamsmith/tests/flip-m6.4.sh +267 -0
  118. package/skills/teamsmith/tests/flip-m6.5.sh +113 -0
  119. package/skills/teamsmith/tests/flip-m7.1.sh +135 -0
  120. package/skills/teamsmith/tests/flip-m7.2.sh +321 -0
  121. package/skills/teamsmith/tests/flip-p144.sh +112 -0
  122. package/skills/teamsmith/tests/flip-p156.sh +133 -0
  123. package/skills/teamsmith/tests/flip-p158.sh +301 -0
  124. package/skills/teamsmith/tests/flip-p160.sh +182 -0
  125. package/skills/teamsmith/tests/flip-p171.sh +249 -0
  126. package/skills/teamsmith/tests/flip-p18.1.sh +95 -0
  127. package/skills/teamsmith/tests/flip-p20.sh +286 -0
  128. package/skills/teamsmith/tests/flip-p201.sh +249 -0
  129. package/skills/teamsmith/tests/flip-p210.sh +239 -0
  130. package/skills/teamsmith/tests/flip-p211.sh +152 -0
  131. package/skills/teamsmith/tests/flip-p217.sh +288 -0
  132. package/skills/teamsmith/tests/flip-p22.sh +123 -0
  133. package/skills/teamsmith/tests/flip-p23.sh +239 -0
  134. package/skills/teamsmith/tests/flip-p25.sh +165 -0
  135. package/skills/teamsmith/tests/flip-p71.sh +184 -0
  136. package/skills/teamsmith/tests/flip-p72.sh +202 -0
  137. package/skills/teamsmith/tests/frames/README.md +176 -0
  138. package/skills/teamsmith/tests/frames/p78-conversation-rule-below-box.txt +6 -0
  139. package/skills/teamsmith/tests/frames/p78-cursor-mid-draft.txt +6 -0
  140. package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor-line.txt +6 -0
  141. package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor.txt +6 -0
  142. package/skills/teamsmith/tests/frames/p78-draft-rule-blank-region.txt +6 -0
  143. package/skills/teamsmith/tests/frames/p78-draft-rule-only.txt +5 -0
  144. package/skills/teamsmith/tests/frames/p78-spinner-row-below-cursor.txt +6 -0
  145. package/skills/teamsmith/tests/frames/p78-wider-rule-below-cursor.txt +6 -0
  146. package/skills/teamsmith/tests/frames/p86-f1-mixed-width-disjoint-box.txt +8 -0
  147. package/skills/teamsmith/tests/frames/p86-f1-narrower-width-disjoint-box.txt +8 -0
  148. package/skills/teamsmith/tests/frames/p86-f1-spinner-top-disjoint-box.txt +8 -0
  149. package/skills/teamsmith/tests/frames/pi-0.85.1-update-banner.txt +30 -0
  150. package/skills/teamsmith/tests/frames/pi-0.87.0-draft-half-sentence.txt +30 -0
  151. package/skills/teamsmith/tests/frames/pi-0.87.0-empty-box.txt +30 -0
  152. package/skills/teamsmith/tests/frames/pi-0.87.0-one-line-draft.txt +30 -0
  153. package/skills/teamsmith/tests/frames/pi-0.87.0-project-trust-prompt.txt +30 -0
  154. package/skills/teamsmith/tests/frames/pi-0.99.2-empty-editor.txt +32 -0
  155. package/skills/teamsmith/tests/frames/pi-0.99.2-human-draft.txt +32 -0
  156. package/skills/teamsmith/tests/frames/pi-1.0.0-git-error-in-box.txt +32 -0
  157. package/skills/teamsmith/tests/gate-guard.sh +112 -0
  158. package/skills/teamsmith/tests/guard-matrix.sh +233 -0
  159. package/skills/teamsmith/tests/install-shape.sh +731 -0
  160. package/skills/teamsmith/tests/ledger-ban.sh +612 -0
  161. package/skills/teamsmith/tests/lib/box-judge.sh +68 -0
  162. package/skills/teamsmith/tests/lib/checkout-shape.sh +129 -0
  163. package/skills/teamsmith/tests/lib/loop-scan.awk +114 -0
  164. package/skills/teamsmith/tests/lib/pty-wait.sh +676 -0
  165. package/skills/teamsmith/tests/lib/section-guard.sh +855 -0
  166. package/skills/teamsmith/tests/lib/shell-lex.pl +261 -0
  167. package/skills/teamsmith/tests/lib/tmp-root.sh +528 -0
  168. package/skills/teamsmith/tests/lib/tmux-cap.sh +35 -0
  169. package/skills/teamsmith/tests/lib/tmux-iso-call-probe.sh +81 -0
  170. package/skills/teamsmith/tests/lib/tmux-iso-probe.sh +46 -0
  171. package/skills/teamsmith/tests/lib/tmux-iso.sh +381 -0
  172. package/skills/teamsmith/tests/load-experiment.sh +290 -0
  173. package/skills/teamsmith/tests/loop-inventory.tsv +84 -0
  174. package/skills/teamsmith/tests/panel-b2-slow-stub.sh +19 -0
  175. package/skills/teamsmith/tests/panel-b2.sh +1183 -0
  176. package/skills/teamsmith/tests/panel-b3-pty-mouse.py +240 -0
  177. package/skills/teamsmith/tests/panel-b3-pty-tmux-mouse.py +138 -0
  178. package/skills/teamsmith/tests/panel-b3-stub.sh +279 -0
  179. package/skills/teamsmith/tests/panel-b3.sh +1301 -0
  180. package/skills/teamsmith/tests/panel-choices.sh +572 -0
  181. package/skills/teamsmith/tests/panel-clipboard-model.ts +187 -0
  182. package/skills/teamsmith/tests/panel-compose-model.ts +290 -0
  183. package/skills/teamsmith/tests/panel-contrast.mjs +81 -0
  184. package/skills/teamsmith/tests/panel-cpu-premise.sh +173 -0
  185. package/skills/teamsmith/tests/panel-cpu.sh +391 -0
  186. package/skills/teamsmith/tests/panel-flip-m54.sh +207 -0
  187. package/skills/teamsmith/tests/panel-flip-p123.sh +278 -0
  188. package/skills/teamsmith/tests/panel-flip-p125.sh +318 -0
  189. package/skills/teamsmith/tests/panel-flip-p128.sh +259 -0
  190. package/skills/teamsmith/tests/panel-keyprobe-pty.py +99 -0
  191. package/skills/teamsmith/tests/panel-keyprobe-stub.sh +41 -0
  192. package/skills/teamsmith/tests/panel-keyprobe.sh +82 -0
  193. package/skills/teamsmith/tests/panel-keyprobe.tsx +116 -0
  194. package/skills/teamsmith/tests/panel-knobs.sh +83 -0
  195. package/skills/teamsmith/tests/panel-p21.sh +2181 -0
  196. package/skills/teamsmith/tests/panel-snapshots.sh +802 -0
  197. package/skills/teamsmith/tests/panel-strings.mjs +195 -0
  198. package/skills/teamsmith/tests/perf.sh +740 -0
  199. package/skills/teamsmith/tests/pm-box-real.sh +350 -0
  200. package/skills/teamsmith/tests/pulse-nudge-key.sh +563 -0
  201. package/skills/teamsmith/tests/routes.sh +1652 -0
  202. package/skills/teamsmith/tests/section-budgets.tsv +150 -0
  203. package/skills/teamsmith/tests/section-guard.sh +231 -0
  204. package/skills/teamsmith/tests/section-needs-audit.sh +94 -0
  205. package/skills/teamsmith/tests/section-paths.tsv +142 -0
  206. package/skills/teamsmith/tests/section-select.sh +655 -0
  207. package/skills/teamsmith/tests/signal-gate.sh +366 -0
  208. package/skills/teamsmith/tests/signal-lint-legacy.txt +14 -0
  209. package/skills/teamsmith/tests/signal-lint.pl +416 -0
  210. package/skills/teamsmith/tests/skill-load.mjs +65 -0
  211. package/skills/teamsmith/tests/smoke.sh +19711 -0
  212. package/skills/teamsmith/tests/snapshots/zh-dark-120-detail.txt +32 -0
  213. package/skills/teamsmith/tests/snapshots/zh-dark-120-p4.txt +32 -0
  214. package/skills/teamsmith/tests/snapshots/zh-dark-120.txt +32 -0
  215. package/skills/teamsmith/tests/snapshots/zh-dark-160-p4.txt +32 -0
  216. package/skills/teamsmith/tests/snapshots/zh-dark-160.txt +32 -0
  217. package/skills/teamsmith/tests/snapshots/zh-dark-59-detail.txt +32 -0
  218. package/skills/teamsmith/tests/snapshots/zh-dark-59-p4.txt +32 -0
  219. package/skills/teamsmith/tests/snapshots/zh-dark-59.txt +32 -0
  220. package/skills/teamsmith/tests/snapshots/zh-dark-99-p4.txt +32 -0
  221. package/skills/teamsmith/tests/snapshots/zh-dark-99.txt +32 -0
  222. package/skills/teamsmith/tests/snapshots/zh-light-120-detail.txt +32 -0
  223. package/skills/teamsmith/tests/snapshots/zh-light-120-p4.txt +32 -0
  224. package/skills/teamsmith/tests/snapshots/zh-light-120.txt +32 -0
  225. package/skills/teamsmith/tests/snapshots/zh-light-160-p4.txt +32 -0
  226. package/skills/teamsmith/tests/snapshots/zh-light-160.txt +32 -0
  227. package/skills/teamsmith/tests/snapshots/zh-light-59-detail.txt +32 -0
  228. package/skills/teamsmith/tests/snapshots/zh-light-59-p4.txt +32 -0
  229. package/skills/teamsmith/tests/snapshots/zh-light-59.txt +32 -0
  230. package/skills/teamsmith/tests/snapshots/zh-light-99-p4.txt +32 -0
  231. package/skills/teamsmith/tests/snapshots/zh-light-99.txt +32 -0
  232. package/skills/teamsmith/tests/spec-ledger-refs.tsv +78 -0
  233. package/skills/teamsmith/tests/spec-refs.sh +826 -0
  234. package/skills/teamsmith/tests/task-header-model.sh +267 -0
  235. package/skills/teamsmith/tests/team-bg-flip.sh +239 -0
  236. package/skills/teamsmith/tests/team-bg-harness.mjs +392 -0
  237. package/skills/teamsmith/tests/team-bg-stop.sh +434 -0
  238. package/skills/teamsmith/tests/team-inbox-watch-flip.sh +191 -0
  239. package/skills/teamsmith/tests/team-inbox-watch-harness.mjs +1559 -0
  240. package/skills/teamsmith/tests/tmp-hygiene.sh +1190 -0
  241. package/skills/teamsmith/tests/tmux-lint-legacy.txt +31 -0
  242. package/skills/teamsmith/tests/tmux-lint.pl +604 -0
  243. package/skills/teamsmith-init/SKILL.md +100 -0
  244. package/skills/teamsmith-init/references/bootstrap.md +74 -0
  245. package/skills/teamsmith-init/templates/bootstrap-prompt.md.tmpl +46 -0
@@ -0,0 +1,826 @@
1
+ #!/usr/bin/env bash
2
+ # spec-refs.sh — 公开契约的自洽走查(change: spec-rationale-self-contained · boundary#Specs are the contract)
3
+ #
4
+ # 用法:
5
+ # bash tests/spec-refs.sh --check [--root <dir>] [--table <file>] # 活体走查(默认 = 本仓库根)
6
+ # bash tests/spec-refs.sh --flips [--root <dir>] [--break=<stage>] # 双向自检(只写 owned tmp 家族)
7
+ # bash tests/spec-refs.sh --blocks [<cap>[#<requirement>]] # 有效文本的来源(base / change:<id>)
8
+ # bash tests/spec-refs.sh --list-declared # 回放声明表
9
+ #
10
+ # 判据(规范在 change 的 delta 里;这里是实现):
11
+ # - **有效文本** = 基线 `openspec/specs/<cap>/spec.md` + 未归档 change 的
12
+ # `openspec/changes/*/specs/<cap>/*.md`(`MODIFIED` 按 requirement 标题替换、`ADDED` 追加、
13
+ # `REMOVED` 丢弃)——走查判的是归档将写出的那份文本,不是今天磁盘上尚未归档的基线。
14
+ # - `docs/team/…` 引用必须命中声明表 `spec-ledger-refs.tsv`:槽位形状(引用自带 `<…>`/`*`)只由
15
+ # `slot` 行放行,确切引用只由**字符级相等**的 `ledger`/`example` 行放行 → 具体报告引用不会
16
+ # 被槽位模式吞掉。
17
+ # - **声明表自身的加载规则**(P185):每条非注释数据行必须恰好三列(`pattern`/`kind`/`basis`,
18
+ # kind 闭集 `slot|ledger|example`、basis 非空),且 `ledger`/`example` 行的 pattern 必须是
19
+ # **字面引用**(带 `<…>`/`*` 的行只属于 `slot`)—— 任一不成立就**读表即拒绝**并点名表名、
20
+ # 行号、行内容;静默丢弃或放行 = 假绿。
21
+ # - 被 pending change 退场的引用(基线里有、**每个** pending 版本都不再有)打印
22
+ # `retired <file>:<line> <ref> by <change>`,不判失败;其余未声明引用打印
23
+ # `undeclared <file>:<line> <ref>` 并 exit 1。
24
+ # - **id 族键**:读 `Id families:` 那一段,文本里用到的族(P/M/D/V/E/F,含 `V<n>-<letter><m>` 的
25
+ # findings 形状)必须被它点名,否则红。
26
+ # - `docs/team/` 整面缺席(产品面检出:没有账本可引用、也没有 pending change 可退场)时打印一条
27
+ # 显式 `skip` 行,其余判据照判 —— 绝不静默假绿。
28
+ #
29
+ # 纯文本:不开窗口、不起进程守卫、不联网;`--flips` 只写 `${TMPDIR:-/tmp}` 下的 owned 家族
30
+ # (tests/lib/tmp-root.sh),并在跑完后断言真树的 `openspec/specs/**` 与 `docs/team/inbox/**`
31
+ # 逐字节未动(反向守卫)。`--break=<stage>` 是给复验的翻转旋钮(见 --flips 的用例表)。
32
+ set -uo pipefail
33
+
34
+ SELF="${BASH_SOURCE[0]}"
35
+ SELF_DIR="$(cd -P "$(dirname "$SELF")" && pwd)"
36
+ REPO_ROOT="$(cd -P "$SELF_DIR/../../.." && pwd)"
37
+
38
+ usage() { sed -n '2,27p' "$SELF"; }
39
+
40
+ ROOT="$REPO_ROOT"
41
+ TABLE="$SELF_DIR/spec-ledger-refs.tsv"
42
+ MODE=""
43
+ CAPARG=""
44
+ BREAK="${SPEC_REFS_BREAK:-}"
45
+ while [ $# -gt 0 ]; do
46
+ case "$1" in
47
+ --check) [ -z "$MODE" ] || { printf 'spec-refs: 多个模式\n' >&2; exit 2; }; MODE=check; shift ;;
48
+ --flips) [ -z "$MODE" ] || { printf 'spec-refs: 多个模式\n' >&2; exit 2; }; MODE=flips; shift ;;
49
+ --blocks) [ -z "$MODE" ] || { printf 'spec-refs: 多个模式\n' >&2; exit 2; }; MODE=blocks; shift
50
+ if [ $# -gt 0 ] && [ "${1#--}" = "$1" ]; then CAPARG="$1"; shift; fi ;;
51
+ --list-declared) [ -z "$MODE" ] || { printf 'spec-refs: 多个模式\n' >&2; exit 2; }; MODE=list; shift ;;
52
+ --root) [ $# -ge 2 ] || { printf 'spec-refs: --root 需要目录\n' >&2; exit 2; }; ROOT="$2"; shift 2 ;;
53
+ --root=*) ROOT="${1#*=}"; shift ;;
54
+ --table) [ $# -ge 2 ] || { printf 'spec-refs: --table 需要文件\n' >&2; exit 2; }; TABLE="$2"; shift 2 ;;
55
+ --table=*) TABLE="${1#*=}"; shift ;;
56
+ --break=*) BREAK="${1#*=}"; shift ;;
57
+ -h|--help) usage; exit 0 ;;
58
+ *) printf 'spec-refs: 未知参数 %s(用法见 --help)\n' "$1" >&2; exit 2 ;;
59
+ esac
60
+ done
61
+ [ -n "$MODE" ] || MODE=check
62
+ case "$MODE" in
63
+ check|blocks) ROOT="$(cd -P "$ROOT" 2>/dev/null && pwd)" || { printf 'spec-refs: --root 目录不存在:%s\n' "$ROOT" >&2; exit 2; } ;;
64
+ esac
65
+ [ -r "$TABLE" ] || { printf 'spec-refs: 读不到声明表 %s\n' "$TABLE" >&2; exit 2; }
66
+
67
+ export SPEC_REFS_BREAK="$BREAK"
68
+
69
+ # ── 走查核心(纯文本;python3 只做文本处理,不碰 tmux / git / 网络)─────────────────────────────
70
+ walk() { # <root> <table> <mode> [caparg] → 走查输出;退出码 0 绿 / 1 红 / 2 用法或内部错
71
+ command -v python3 >/dev/null 2>&1 || { printf 'spec-refs: 需要 python3(文本走查的核心)\n' >&2; return 2; }
72
+ python3 - "$1" "$2" "$3" "${4:-}" <<'PY'
73
+ import os, re, sys
74
+
75
+ root, table, mode = sys.argv[1], sys.argv[2], sys.argv[3]
76
+ caparg = sys.argv[4] if len(sys.argv) > 4 else ""
77
+ breakstage = os.environ.get("SPEC_REFS_BREAK", "")
78
+
79
+ def die(msg):
80
+ sys.stderr.write("spec-refs: %s\n" % msg)
81
+ sys.exit(2)
82
+
83
+ # ---------------------------------------------------------------- 声明表
84
+ KINDS = ("slot", "ledger", "example")
85
+
86
+ def load_rows(path):
87
+ """读声明表。畸形行**拒绝加载**(点名表名/行号/行内容)—— 静默丢弃 = 假绿(P185)。
88
+
89
+ `--break=toleranttable` 把加载放回 P150 的宽松版本(缺列 continue、basis/kind 只点名模式、
90
+ 通配行照收),`--flips` 的声明表用例必须因此全部 BAD —— 这是「换掉实现 → 守卫必红」的旋钮。
91
+ """
92
+ rows = []
93
+ tolerant = breakstage == "toleranttable"
94
+ with open(path, encoding="utf-8") as fh:
95
+ for lineno, raw in enumerate(fh, 1):
96
+ if not raw.strip() or raw.lstrip().startswith("#"):
97
+ continue
98
+ line = raw.rstrip("\r\n")
99
+ parts = line.split("\t")
100
+ if tolerant:
101
+ if len(parts) < 2:
102
+ continue
103
+ pat, kind = parts[0], parts[1]
104
+ basis = parts[2] if len(parts) > 2 else ""
105
+ if kind not in KINDS:
106
+ die("声明表 kind 非法:%s(%s)" % (kind, pat))
107
+ if not basis.strip():
108
+ die("声明表行没有 basis:%s" % pat)
109
+ else:
110
+ if len(parts) != 3:
111
+ die("声明表 %s:%d 数据行不是三列 pattern<TAB>kind<TAB>basis:%s"
112
+ % (path, lineno, line))
113
+ pat, kind, basis = parts
114
+ if kind not in KINDS:
115
+ die("声明表 %s:%d kind 非法(只许 %s):%r(行:%s)"
116
+ % (path, lineno, "/".join(KINDS), kind, line))
117
+ if not basis.strip():
118
+ die("声明表 %s:%d basis 为空(行:%s)" % (path, lineno, line))
119
+ if kind in ("ledger", "example") and ("*" in pat or "<" in pat):
120
+ die("声明表 %s:%d %s 行必须是字面引用,通配/占位形状只属于 slot:%r(行:%s)"
121
+ % (path, lineno, kind, pat, line))
122
+ s = re.sub(r"<[^>]*>", "\x01", pat)
123
+ s = s.replace("**", "\x02").replace("*", "\x03")
124
+ s = re.escape(s)
125
+ s = s.replace("\x01", r"[^/\s]+").replace("\x02", ".*").replace("\x03", r"[^/\s]*")
126
+ rows.append((re.compile("^" + s + "$"), kind, pat, basis))
127
+ if not rows:
128
+ die("声明表没有数据行:%s" % path)
129
+ return rows
130
+
131
+ ROWS = load_rows(table)
132
+
133
+ def declared(ref):
134
+ """槽位形状的引用只由 slot 行放行;确切引用与 ledger/example 行**逐字相等**(宽松匹配 = 假绿)。"""
135
+ slot_shaped = ("<" in ref) or ("*" in ref)
136
+ if breakstage == "slotmatcher":
137
+ # 翻转:放回 P145 dryrun §1 的第一版匹配器(任何行匹配任何引用)——它会把具体引用
138
+ # docs/team/reports/P52-dev2.md 吞进槽位模式,正是 --flips 用例 1 要钉住的假绿。
139
+ return any(rx.match(ref) for rx, _, _, _ in ROWS)
140
+ if slot_shaped:
141
+ return any(kind == "slot" and rx.match(ref) for rx, kind, _, _ in ROWS)
142
+ # 字面相等:ledger/example 行读表时已保证不含 `<…>`/`*`,所以这是真正的字符串相等,不是正则。
143
+ return any(kind in ("ledger", "example") and pat == ref for _, kind, pat, _ in ROWS)
144
+
145
+ # ---------------------------------------------------------------- 文本解析
146
+ REQ_RE = re.compile(r"^### Requirement: (.+?)\s*$")
147
+ SEC_RE = re.compile(r"^##\s+(ADDED|MODIFIED|REMOVED)\s+Requirements\s*$")
148
+
149
+ def parse_file(path):
150
+ """→ (loose, reqs):loose = [(lineno, text)];reqs = [{'title','sec','lines':[(lineno,text)]}]"""
151
+ loose, reqs = [], []
152
+ cur = None
153
+ section = None
154
+ with open(path, encoding="utf-8", errors="replace") as fh:
155
+ text = fh.read()
156
+ for i, line in enumerate(text.split("\n"), 1):
157
+ m = REQ_RE.match(line)
158
+ if m:
159
+ if cur:
160
+ reqs.append(cur)
161
+ cur = {"title": m.group(1).strip(), "sec": section, "lines": [(i, line)]}
162
+ continue
163
+ if re.match(r"^##(?!#)", line) or re.match(r"^#(?!#)", line):
164
+ if cur:
165
+ reqs.append(cur)
166
+ cur = None
167
+ ms = SEC_RE.match(line.rstrip())
168
+ section = ms.group(1) if ms else None
169
+ loose.append((i, line))
170
+ continue
171
+ if cur is not None:
172
+ cur["lines"].append((i, line))
173
+ else:
174
+ loose.append((i, line))
175
+ if cur:
176
+ reqs.append(cur)
177
+ return loose, reqs
178
+
179
+ def rel(p):
180
+ return os.path.relpath(p, root)
181
+
182
+ def entry_exists(p):
183
+ """空目录 / 坏软链都算**存在**(P146:不许把空或破的内部面当成缺席换跳过)。"""
184
+ return os.path.lexists(p)
185
+
186
+ specs_dir = os.path.join(root, "openspec", "specs")
187
+ if not os.path.isdir(specs_dir):
188
+ die("找不到 %s(--root 指向一棵带 openspec/specs 的树)" % rel(specs_dir))
189
+
190
+ caps = sorted(d for d in os.listdir(specs_dir)
191
+ if os.path.isdir(os.path.join(specs_dir, d))
192
+ and os.path.isfile(os.path.join(specs_dir, d, "spec.md")))
193
+
194
+ changes_dir = os.path.join(root, "openspec", "changes")
195
+ changes = []
196
+ if os.path.isdir(changes_dir):
197
+ changes = sorted(c for c in os.listdir(changes_dir)
198
+ if c != "archive" and os.path.isdir(os.path.join(changes_dir, c)))
199
+
200
+ # cap → {'loose':[(relf,ln,txt)], 'reqs':[(title,[(relf,ln,txt)])],
201
+ # 'virt':{title:(provider_change,[(relf,ln,txt)])},
202
+ # 'versions':{title:[(change,sec,[(relf,ln,txt)])]}, 'added':[(change,title,[...])],
203
+ # 'deps':[(relf,title,provider,own)]}
204
+ model = {}
205
+ for cap in caps:
206
+ base = os.path.join(specs_dir, cap, "spec.md")
207
+ b_loose, b_reqs = parse_file(base)
208
+ loose = [(rel(base), ln, t) for ln, t in b_loose]
209
+ reqs, versions, added, virt, deps = [], {}, [], {}, []
210
+ for r in b_reqs:
211
+ lines = [(rel(base), ln, t) for ln, t in r["lines"]]
212
+ if r["title"] in versions:
213
+ # 同一基线里同名 requirement:OpenSpec 不允许;走查按声明顺序保留,不猜。
214
+ die("%s 里 requirement 标题重复:%s" % (rel(base), r["title"]))
215
+ versions[r["title"]] = []
216
+ reqs.append((r["title"], lines))
217
+ # 两趟解析:先把所有未归档 change 的块收齐,再按**依赖顺序**而不是目录字典序解析。
218
+ # 一个 change 的 MODIFIED/REMOVED 可以改另一个未归档 change 才 ADDED 的 requirement
219
+ # (归档顺序依赖:提供方先归档;D87 的实战形状)—— 取提供方的块当基线,逐条点名、不判失败。
220
+ pend = [] # [(change, relf, sec, title, lines)]
221
+ for ch in changes:
222
+ ddir = os.path.join(changes_dir, ch, "specs", cap)
223
+ if not os.path.isdir(ddir):
224
+ continue
225
+ for name in sorted(os.listdir(ddir)):
226
+ if not name.endswith(".md"):
227
+ continue
228
+ p = os.path.join(ddir, name)
229
+ d_loose, d_reqs = parse_file(p)
230
+ for r in d_reqs:
231
+ if r["sec"] is None:
232
+ die("%s:requirement %r 不在 ADDED/MODIFIED/REMOVED 段里" % (rel(p), r["title"]))
233
+ pend.append((ch, rel(p), r["sec"], r["title"],
234
+ [(rel(p), ln, t) for ln, t in r["lines"]]))
235
+ virt_added = {} # 未归档 change 的 ADDED 标题 → (change, lines):只有被改写的才进 virt
236
+ for ch, relf, sec, title, lines in pend:
237
+ if sec != "ADDED":
238
+ continue
239
+ added.append((ch, title, lines))
240
+ if title not in versions:
241
+ virt_added[title] = (ch, lines)
242
+ for ch, relf, sec, title, lines in pend:
243
+ if sec == "ADDED":
244
+ continue
245
+ if title not in versions and title in virt_added:
246
+ virt[title] = virt_added[title]
247
+ if virt_added[title][0] != ch: # 同一个 change 自己 ADDED 又 MODIFIED:不算跨 change 依赖
248
+ deps.append((relf, title, virt_added[title][0], ch))
249
+ elif title not in versions:
250
+ die("%s:%s 的 %r 在基线里不存在,也没有未归档 change 提供它(归档会失败)"
251
+ % (relf, sec, title))
252
+ versions.setdefault(title, []).append((ch, "MODIFIED" if sec == "MODIFIED" else "REMOVED", lines))
253
+ model[cap] = {"loose": loose, "reqs": reqs, "virt": virt, "versions": versions,
254
+ "added": added, "deps": deps}
255
+
256
+ # ---------------------------------------------------------------- --blocks:有效文本的来源
257
+ if mode == "blocks":
258
+ want_cap, want_req = None, None
259
+ if caparg:
260
+ if "#" in caparg:
261
+ want_cap, want_req = caparg.split("#", 1)
262
+ else:
263
+ want_cap = caparg
264
+ found = False
265
+ for cap in caps:
266
+ if want_cap and cap != want_cap:
267
+ continue
268
+ m = model[cap]
269
+ rows = []
270
+ for title, base_lines in m["reqs"]:
271
+ vers = m["versions"][title]
272
+ if not vers:
273
+ rows.append(("base", title, base_lines))
274
+ else:
275
+ for ch, sec, lines in vers:
276
+ rows.append(("change:%s%s" % (ch, " (REMOVED)" if sec == "REMOVED" else ""), title, lines))
277
+ for title, (prov, base_lines) in sorted(m["virt"].items()):
278
+ rows.append(("change:%s (ADDED, 基线提供方)" % prov, title, base_lines))
279
+ for ch, sec, lines in m["versions"][title]:
280
+ rows.append(("change:%s%s" % (ch, " (REMOVED)" if sec == "REMOVED" else ""), title, lines))
281
+ for ch, title, lines in m["added"]:
282
+ if title in m["virt"]:
283
+ continue # 它的 ADDED 是虚拟基线(会被改,不是最终追加块)
284
+ rows.append(("change:%s (ADDED)" % ch, title, lines))
285
+ for src, title, lines in rows:
286
+ if want_req and title != want_req:
287
+ continue
288
+ found = True
289
+ if want_req:
290
+ print("== %s#%s · source: %s (%s) ==" % (cap, title, lines[0][0], src))
291
+ for _, _, t in lines:
292
+ print(t)
293
+ print()
294
+ else:
295
+ print("%s\t%s\t%s" % (cap, src, title))
296
+ if caparg and not found:
297
+ die("--blocks 找不到 %s" % caparg)
298
+ sys.exit(0)
299
+
300
+ # ---------------------------------------------------------------- 引用扫描
301
+ REF_RE = re.compile(r"`docs/team/[^`]*`|docs/team/[A-Za-z0-9_*./<>-]*")
302
+
303
+ def refs_in(text):
304
+ out = []
305
+ for m in REF_RE.finditer(text):
306
+ r = m.group(0).strip("`").rstrip(".,)")
307
+ if r:
308
+ out.append(r)
309
+ return out
310
+
311
+ retired, undeclared = [], []
312
+
313
+ # 基线里非 requirement 的文本(Purpose / 标题等)永远参与判断
314
+ # 每个 requirement:未改写 → 基线文本;改写 → 每个 pending 版本各判一次(REMOVED 无文本)
315
+ judged = [] # [(relf, ln, text)]
316
+ all_deps = []
317
+ for cap in caps:
318
+ m = model[cap]
319
+ for relf, ln, t in m["loose"]:
320
+ judged.append((relf, ln, t))
321
+ for relf, title, prov, own in m["deps"]:
322
+ all_deps.append((relf, title, prov, own))
323
+ for title, base_lines in m["reqs"] + [(t, l) for t, (p, l) in sorted(m["virt"].items())]:
324
+ vers = m["versions"][title]
325
+ if not vers:
326
+ judged.extend(base_lines)
327
+ continue
328
+ # 退场:基线里有、每个 pending 版本都没有的引用
329
+ base_refs = []
330
+ for relf, ln, t in base_lines:
331
+ for r in refs_in(t):
332
+ base_refs.append((relf, ln, r))
333
+ pending_refs = set()
334
+ changes_touching = []
335
+ for ch, sec, lines in vers:
336
+ changes_touching.append(ch)
337
+ for _, _, t in lines:
338
+ pending_refs.update(refs_in(t))
339
+ for relf, ln, r in base_refs:
340
+ if r in pending_refs:
341
+ continue
342
+ if breakstage == "noretired":
343
+ # P200 的影子:关掉「退场引用被点名」(连同汇总里的 retired 计数)—— --flips 的
344
+ # retired-naming 用例必须因此 BAD。判红/判绿这两条路不受影响:retired 只是点名,
345
+ # 从来不是 undeclared(真树在任何归档状态下都不该因此变红)。
346
+ continue
347
+ bys = sorted({ch for (ch, sec, lines) in vers
348
+ if sec == "REMOVED" or r not in {x for _, _, t in lines for x in refs_in(t)}})
349
+ retired.append((relf, ln, r, ",".join(bys) if bys else ",".join(sorted(set(changes_touching)))))
350
+ for ch, sec, lines in vers:
351
+ if sec != "REMOVED":
352
+ judged.extend(lines)
353
+ for ch, title, lines in m["added"]:
354
+ if title in m["virt"]:
355
+ continue # 虚拟基线:判的是改写它的那个 change 的块,不是提供方的块
356
+ judged.extend(lines)
357
+
358
+ seen_undeclared = []
359
+ for relf, ln, t in judged:
360
+ for r in refs_in(t):
361
+ if declared(r):
362
+ continue
363
+ key = (relf, ln, r)
364
+ if key not in seen_undeclared:
365
+ seen_undeclared.append(key)
366
+
367
+ for relf, title, prov, own in all_deps:
368
+ print("pending-dependency %s:%r 的基线由未归档 change %s 提供(归档顺序:%s 先于 %s)"
369
+ % (relf, title, prov, prov, own))
370
+ for relf, ln, r, by in retired:
371
+ print("retired %s:%s %s by %s" % (relf, ln, r, by))
372
+ for relf, ln, r in seen_undeclared:
373
+ print("undeclared %s:%s %s" % (relf, ln, r))
374
+
375
+ # ---------------------------------------------------------------- id 族键
376
+ ID_LINE = re.compile(r"^Id families:")
377
+ FAM_RE = re.compile(r"\b([PMDVEF])(\d+)(?:-[A-Za-z]+\d+)?\b")
378
+ FAM_ROOT = re.compile(r"`([A-Za-z]+)<[^`]*>`")
379
+
380
+ named = set()
381
+ used = {} # family → (relf, ln, token)
382
+ key_seen = False
383
+ in_key = False
384
+ for relf, ln, t in judged:
385
+ if ID_LINE.match(t):
386
+ key_seen = True
387
+ in_key = True
388
+ for m in FAM_ROOT.finditer(t):
389
+ named.add(m.group(1)[0])
390
+ continue
391
+ if in_key:
392
+ if not t.strip() or re.match(r"^#", t):
393
+ in_key = False
394
+ else:
395
+ for m in FAM_ROOT.finditer(t):
396
+ named.add(m.group(1)[0])
397
+ continue
398
+ for m in FAM_RE.finditer(t):
399
+ fam = m.group(1)
400
+ if fam not in used:
401
+ used[fam] = (relf, ln, m.group(0))
402
+
403
+ family_bad = []
404
+ if not key_seen:
405
+ for fam in sorted(used):
406
+ relf, ln, tok = used[fam]
407
+ family_bad.append((relf, ln, tok, fam))
408
+ if not used:
409
+ family_bad.append(("openspec/specs", 0, "(no family token found)", "?"))
410
+ for fam in sorted(set(used) - named):
411
+ relf, ln, tok = used[fam]
412
+ family_bad.append((relf, ln, tok, fam))
413
+ seen_fb = []
414
+ for item in family_bad:
415
+ if item not in seen_fb:
416
+ seen_fb.append(item)
417
+ for relf, ln, tok, fam in seen_fb:
418
+ if ln:
419
+ print("family-unnamed %s:%s %s family=%s" % (relf, ln, tok, fam))
420
+ else:
421
+ print("family-unnamed %s %s family=%s" % (relf, tok, fam))
422
+ if not key_seen and not seen_fb:
423
+ print("family-key-missing openspec/specs — 契约要求一行 Id families:")
424
+
425
+ # ---------------------------------------------------------------- 账本/叠加层缺席(产品面检出)
426
+ # 两个内部面各自缺席时走查的可判范围不同,逐条点名(不静默):
427
+ # docs/team 缺席 — 引用能不能被读者打开无从核对;声明表与 id 族键照判,退场判据照判。
428
+ # openspec/changes 缺席 — 退场判据不可判:base 文本里被 pending change 退场的引用会按未声明报
429
+ # (这正是 boundary 要求里那条负向对照的形状:交付树导出时叠加层不在,归档后才自洽)。
430
+ if not entry_exists(os.path.join(root, "docs", "team")):
431
+ print("skip: no ledger under %s/docs/team — 没有账本可引用;声明表与 id 族键照判,"
432
+ "退场判据照判(pending change 仍在)" % root)
433
+ if not os.path.isdir(os.path.join(root, "openspec", "changes")):
434
+ print("skip: no openspec/changes under %s — 退场叠加层缺席,退场判据不可判;"
435
+ "被未归档 change 退场的引用在这里会按 undeclared 报(负向对照的形状)" % root)
436
+
437
+ distinct = len({r for _, _, t in judged for r in refs_in(t)})
438
+ print("spec-refs: judged %d reference(s) (%d distinct) in %d effective line(s); retired %d; undeclared %d; "
439
+ "id families used [%s] named [%s]"
440
+ % (sum(len(refs_in(t)) for _, _, t in judged), distinct, len(judged), len(retired),
441
+ len(seen_undeclared), ",".join(sorted(used)), ",".join(sorted(named))))
442
+
443
+ sys.exit(1 if (seen_undeclared or seen_fb) else 0)
444
+ PY
445
+ }
446
+
447
+ # ── --list-declared:原样回放声明表的数据行(pattern / kind / basis)──────────────────────────
448
+ list_declared() {
449
+ local line
450
+ while IFS= read -r line || [ -n "$line" ]; do
451
+ case "$line" in ''|'#'*) continue ;; esac
452
+ printf '%s\n' "$line"
453
+ done < "$TABLE"
454
+ }
455
+
456
+ # ── --flips:双向自检 ────────────────────────────────────────────────────────────────────────
457
+ # 每个变异都在 owned tmp 家族里的 scratch 副本上做;`--break=slotmatcher` 把匹配器放回宽松版本
458
+ # (用例 1 必须因此从「红」变成「没抓到」→ --flips 自己报 BAD 并非 0);`--break=toleranttable`
459
+ # 把声明表加载放回 P150 的宽松版本(通配行照收、缺列静默丢弃 → 声明表用例必须全部 BAD);
460
+ # `--break=writeroot` 把变异写进被检的那棵树(默认拒绝在真仓库根上跑)→ 反向守卫必须报红;
461
+ # `--break=noretired` 关掉「退场引用被点名」(连同汇总计数,P200)→ retired-naming 用例必须 BAD。
462
+ flips() {
463
+ # shellcheck source=tests/lib/tmp-root.sh
464
+ . "$SELF_DIR/lib/tmp-root.sh" 2>/dev/null || { printf 'spec-refs: 找不到 tests/lib/tmp-root.sh\n' >&2; return 2; }
465
+ local scratch=""
466
+ scratch="$(tmp_root_create spec-refs)" || return 2
467
+ trap 'tmp_root_reap_all' EXIT
468
+ local ok_n=0 bad_n=0
469
+
470
+ tree_hash() { # <dir> → 内容指纹(缺席 = absent)
471
+ if [ ! -e "$1" ]; then printf 'absent\n'; return 0; fi
472
+ ( cd "$1" 2>/dev/null && find . -type f -print | LC_ALL=C sort | while IFS= read -r f; do sha256sum -- "$f"; done ) \
473
+ | sha256sum | awk '{print $1}'
474
+ }
475
+ mk_scratch() { # <label> → 新 scratch 根(含真树 openspec 的副本)
476
+ local d
477
+ d="$(tmp_root_create "spec-refs-$1")" || return 1
478
+ cp -a "$ROOT/openspec" "$d/openspec" || return 1
479
+ printf '%s' "$d"
480
+ }
481
+ first_spec() { # 副本里第一个带 ## Purpose 的基线 spec(plant 用)
482
+ local f
483
+ for f in $(find "$1/openspec/specs" -name spec.md | LC_ALL=C sort); do
484
+ grep -q '^## Purpose' "$f" && { printf '%s' "$f"; return 0; }
485
+ done
486
+ return 1
487
+ }
488
+ key_line_files() { # 只找 spec 文本(design.md 之类不在有效文本里)
489
+ { grep -rl -E '^Id families:' "$1/openspec/specs" 2>/dev/null || true
490
+ grep -rl -E '^Id families:' "$1"/openspec/changes/*/specs 2>/dev/null || true
491
+ } | LC_ALL=C sort -u
492
+ }
493
+
494
+ red() { printf 'red %-32s %s\n' "$1" "$2"; ok_n=$((ok_n + 1)); }
495
+ clean() { printf 'clean %-32s %s\n' "$1" "$2"; ok_n=$((ok_n + 1)); }
496
+ flipbad() { printf 'BAD %-32s %s\n' "$1" "$2"; bad_n=$((bad_n + 1)); }
497
+
498
+ local before_specs before_inbox
499
+ before_specs="$(tree_hash "$ROOT/openspec/specs")"
500
+ before_inbox="$(tree_hash "$ROOT/docs/team/inbox")"
501
+
502
+ local log="$scratch/walk.log" rc=0
503
+
504
+ local flip_table=""
505
+ run_flip() { # <label> <root> → 全局 FLIP_RC / 日志
506
+ FLIP_RC=0
507
+ walk "$2" "${flip_table:-$TABLE}" check >"$log" 2>&1 || FLIP_RC=$?
508
+ }
509
+ mk_table() { # <标签> <追加行(\t 转义)→ 新 scratch 声明表(真表只读)
510
+ local t="$scratch/table-$1.tsv"
511
+ cp "$TABLE" "$t" || return 1
512
+ printf '%b\n' "$2" >> "$t" || return 1
513
+ printf '%s' "$t"
514
+ }
515
+ expect_refused() { # <label> <ERE>:声明表必须**拒绝加载**(rc≠0 且点名表名/行号/该行内容)
516
+ local label="$1" pat="$2" hit=""
517
+ hit="$(grep -m1 -E -- "$pat" "$log" || true)"
518
+ if [ "$FLIP_RC" -ne 0 ] && [ -n "$hit" ]; then
519
+ red "$label" "exit $FLIP_RC · $hit"
520
+ else
521
+ flipbad "$label" "要拒绝加载(exit≠0 且有一行匹配 [$pat]),实得 exit $FLIP_RC:$(tail -2 "$log" | tr '\n' ' ')"
522
+ fi
523
+ }
524
+ expect_red() { # <label> <ERE>:必须出现一条**判红**的行(undeclared / family-unnamed),不能只是被别的行提到
525
+ local label="$1" pat="$2" hit=""
526
+ hit="$(grep -m1 -E -- "$pat" "$log" || true)"
527
+ if [ "$FLIP_RC" -ne 0 ] && [ -n "$hit" ]; then
528
+ red "$label" "exit $FLIP_RC · $hit"
529
+ else
530
+ flipbad "$label" "要红(exit≠0 且有一行匹配 [$pat]),实得 exit $FLIP_RC:$(tail -2 "$log" | tr '\n' ' ')"
531
+ fi
532
+ }
533
+ expect_clean() { # <label> <root>
534
+ local label="$1"
535
+ if [ "$FLIP_RC" -eq 0 ] && grep -q '^spec-refs: judged ' "$log"; then
536
+ clean "$label" "$(grep -m1 '^spec-refs: judged ' "$log")"
537
+ else
538
+ flipbad "$label" "要绿,实得 exit $FLIP_RC:$(grep -E '^(undeclared|family-unnamed)' "$log" | head -2 | tr '\n' ' ')"
539
+ fi
540
+ }
541
+
542
+ # ① 红:具体报告引用落在 ## Purpose 里 —— 槽位模式不许吞掉它(P145 dryrun §1 的第一版就吞了)
543
+ # `--break=writeroot`:变异写进**被检的那棵树**(拒绝在真仓库根上跑)→ 反向守卫必须报红
544
+ local s="" f="" target="" tbl=""
545
+ s="$(mk_scratch plant-purpose)" || return 2
546
+ target="$(first_spec "$s")" || true
547
+ if [ "$BREAK" = "writeroot" ]; then
548
+ if [ "$ROOT" = "$REPO_ROOT" ]; then
549
+ printf 'spec-refs: --break=writeroot 拒绝在真仓库根上跑(那会改到真树)—— 用 --root <副本> 演示守卫灵敏度\n' >&2
550
+ bad_n=$((bad_n + 1)); target=""
551
+ else
552
+ target="$(first_spec "$ROOT")" || true
553
+ fi
554
+ fi
555
+ if [ -n "$target" ]; then
556
+ awk '{ print } /^## Purpose/ && !done { print "- planted citation: `docs/team/reports/P52-dev2.md`"; done=1 }' \
557
+ "$target" >"$target.tmp" && mv "$target.tmp" "$target"
558
+ run_flip plant-purpose-concrete "$s"
559
+ expect_red "plant-purpose-concrete" '^undeclared .*docs/team/reports/P52-dev2\.md$'
560
+ else
561
+ flipbad "plant-purpose-concrete" "找不到带 ## Purpose 的 spec(root=$ROOT)"
562
+ fi
563
+
564
+ # ② 红:一条形状没有任何槽位行的未声明引用
565
+ s="$(mk_scratch plant-undeclared)" || return 2
566
+ f="$(first_spec "$s")" || true
567
+ if [ -n "$f" ]; then
568
+ awk '{ print } /^## Purpose/ && !done { print "- planted: `docs/team/scratch/notes.md`"; done=1 }' \
569
+ "$f" >"$f.tmp" && mv "$f.tmp" "$f"
570
+ run_flip plant-undeclared-shape "$s"
571
+ expect_red "plant-undeclared-shape" '^undeclared .*docs/team/scratch/notes\.md$'
572
+ else
573
+ flipbad "plant-undeclared-shape" "副本里找不到 ## Purpose"
574
+ fi
575
+
576
+ # ③ 红:Id families: 键行整段删掉(文本里的族因此没有键)
577
+ s="$(mk_scratch drop-key)" || return 2
578
+ local kf="" kfs=""
579
+ kfs="$(key_line_files "$s")"
580
+ if [ -n "$kfs" ]; then
581
+ while IFS= read -r kf; do
582
+ [ -n "$kf" ] || continue
583
+ grep -v -E '^Id families:' "$kf" >"$kf.tmp" && mv "$kf.tmp" "$kf"
584
+ done <<< "$kfs"
585
+ run_flip delete-id-families-line "$s"
586
+ expect_red "delete-id-families-line" '^family-unnamed .*family=F$'
587
+ else
588
+ flipbad "delete-id-families-line" "副本里找不到 Id families: 行"
589
+ fi
590
+
591
+ # ④ 红:键行不再点名 F<n>,而文本里用着 F2(键与文本对不上)
592
+ s="$(mk_scratch drop-f-key)" || return 2
593
+ kfs="$(key_line_files "$s")"
594
+ if [ -n "$kfs" ]; then
595
+ while IFS= read -r kf; do
596
+ [ -n "$kf" ] || continue
597
+ sed 's/`F<n>`//' "$kf" >"$kf.tmp" && mv "$kf.tmp" "$kf"
598
+ done <<< "$kfs"
599
+ run_flip drop-F-key-keep-F2 "$s"
600
+ expect_red "drop-F-key-keep-F2" '^family-unnamed .*family=F$'
601
+ else
602
+ flipbad "drop-F-key-keep-F2" "副本里找不到 Id families: 行"
603
+ fi
604
+
605
+ # ⑤ 绿:声明的槽位(槽位形状只由 slot 行放行)
606
+ s="$(mk_scratch slot-ok)" || return 2
607
+ f="$(first_spec "$s")" || true
608
+ if [ -n "$f" ]; then
609
+ awk '{ print } /^## Purpose/ && !done { print "- declared slot: `docs/team/reports/<ID>-<agent>.md`"; done=1 }' \
610
+ "$f" >"$f.tmp" && mv "$f.tmp" "$f"
611
+ run_flip declared-slot "$s"
612
+ expect_clean "declared-slot" "$s"
613
+ else
614
+ flipbad "declared-slot" "副本里找不到 ## Purpose"
615
+ fi
616
+
617
+ # ⑥ 绿:声明的示例记录(确切引用由逐字相等的 example 行放行)
618
+ s="$(mk_scratch example-ok)" || return 2
619
+ f="$(first_spec "$s")" || true
620
+ if [ -n "$f" ]; then
621
+ awk '{ print } /^## Purpose/ && !done { print "- declared example: `docs/team/reports/T9-dev.md`"; done=1 }' \
622
+ "$f" >"$f.tmp" && mv "$f.tmp" "$f"
623
+ run_flip declared-example "$s"
624
+ expect_clean "declared-example" "$s"
625
+ else
626
+ flipbad "declared-example" "副本里找不到 ## Purpose"
627
+ fi
628
+
629
+ # ⑦ 绿:没动过的树(此处是调用方给的 --root)
630
+ run_flip untouched-tree "$ROOT"
631
+ expect_clean "untouched-tree" "$ROOT"
632
+
633
+ # ⑧ 红:声明表自己的加载规则(P185 F1)—— 通配/占位形状只属于 `slot` 行:`ledger`/`example` 行
634
+ # 带 `<…>`/`*` 必须**拒绝加载**并点名表名、行号、行内容;否则一条通配行会吞掉具体引用
635
+ # (F1 的现场:植入的具体引用 `docs/team/reports/P184-dev.md` 被 `…P184*.md` 放行)。
636
+ s="$(mk_scratch table-wildcard)" || return 2
637
+ f="$(first_spec "$s")" || true
638
+ if [ -n "$f" ]; then
639
+ awk '{ print } /^## Purpose/ && !done { print "- planted citation: docs/team/reports/P184-dev.md"; done=1 }' \
640
+ "$f" >"$f.tmp" && mv "$f.tmp" "$f"
641
+ tbl="$(mk_table wildcard-ledger 'docs/team/reports/P184*.md\tledger\tP185:通配行只许出现在 slot')" || return 2
642
+ flip_table="$tbl"; run_flip table-wildcard-ledger "$s"
643
+ expect_refused "table-wildcard-ledger" 'table-wildcard-ledger\.tsv:[0-9]+ .*P184\*\.md'
644
+ tbl="$(mk_table wildcard-example 'docs/team/reports/P184-<agent>.md\texample\tP185:占位形状只许出现在 slot')" || return 2
645
+ flip_table="$tbl"; run_flip table-wildcard-example "$s"
646
+ expect_refused "table-wildcard-example" 'table-wildcard-example\.tsv:[0-9]+ .*P184-<agent>\.md'
647
+ else
648
+ flipbad "table-wildcard-ledger" "副本里找不到 ## Purpose"
649
+ flipbad "table-wildcard-example" "副本里找不到 ## Purpose"
650
+ fi
651
+
652
+ # ⑨ 红:畸形数据行必须**拒绝加载并点名行号与行内容**(P185 F2)—— 缺列/多列/空 basis/非法 kind,
653
+ # 一条都不许静默丢弃(P150 的加载对 1 列行直接 continue)。
654
+ s="$(mk_scratch table-shapes)" || return 2
655
+ tbl="$(mk_table one-col 'docs/team/one-col.md')" || return 2
656
+ flip_table="$tbl"; run_flip table-one-col "$s"
657
+ expect_refused "table-one-col" 'table-one-col\.tsv:[0-9]+ .*docs/team/one-col\.md'
658
+ tbl="$(mk_table two-col 'docs/team/two-col.md\tledger')" || return 2
659
+ flip_table="$tbl"; run_flip table-two-col "$s"
660
+ expect_refused "table-two-col" 'table-two-col\.tsv:[0-9]+ .*docs/team/two-col\.md'
661
+ tbl="$(mk_table empty-basis 'docs/team/empty-basis.md\tledger\t')" || return 2
662
+ flip_table="$tbl"; run_flip table-empty-basis "$s"
663
+ expect_refused "table-empty-basis" 'table-empty-basis\.tsv:[0-9]+ .*docs/team/empty-basis\.md'
664
+ tbl="$(mk_table bad-kind 'docs/team/bad-kind.md\tbogus\tP185')" || return 2
665
+ flip_table="$tbl"; run_flip table-bad-kind "$s"
666
+ expect_refused "table-bad-kind" "table-bad-kind\\.tsv:[0-9]+ .*'bogus'.*docs/team/bad-kind\\.md"
667
+ tbl="$(mk_table extra-col 'docs/team/extra-col.md\tledger\tP185\textra')" || return 2
668
+ flip_table="$tbl"; run_flip table-extra-col "$s"
669
+ expect_refused "table-extra-col" 'table-extra-col\.tsv:[0-9]+ .*docs/team/extra-col\.md.*extra'
670
+ flip_table=""
671
+
672
+ # ⑩ 绿:确切引用照旧由 `ledger`/`example` 行放行(P185 反向,不许误伤):账本根 `docs/team/reports/`
673
+ # 与固定文件 `docs/team/DECISIONS.md` 都是字面行,逐字相等即放行。
674
+ s="$(mk_scratch ledger-ok)" || return 2
675
+ f="$(first_spec "$s")" || true
676
+ if [ -n "$f" ]; then
677
+ awk '{ print } /^## Purpose/ && !done { print "- declared ledger root: docs/team/reports/"; \
678
+ print "- declared ledger file: docs/team/DECISIONS.md"; done=1 }' \
679
+ "$f" >"$f.tmp" && mv "$f.tmp" "$f"
680
+ run_flip declared-ledger-rows "$s"
681
+ expect_clean "declared-ledger-rows" "$s"
682
+ else
683
+ flipbad "declared-ledger-rows" "副本里找不到 ## Purpose"
684
+ fi
685
+
686
+ # ⑪⑫ 归档顺序依赖:一个未归档 change 的 MODIFIED 改另一个未归档 change 才 ADDED 的 requirement。
687
+ # 取提供方的块当基线 → 绿且点名提供方与归档顺序;提供方不在 → 红且点名解析不了的标题。
688
+ # 夹具是自足的最小树(不拷真 openspec):结论不随 main 上哪些 change 还没归档而变。
689
+ mk_dep_tree() { # <根> <带提供方:1|0>
690
+ local d="$1" with="${2:-1}"
691
+ mkdir -p "$d/openspec/specs/boundary" "$d/openspec/changes/zz-consumer/specs/boundary"
692
+ [ "$with" = "1" ] && mkdir -p "$d/openspec/changes/zz-provider/specs/boundary"
693
+ cat > "$d/openspec/specs/boundary/spec.md" <<'SPEC'
694
+ # boundary Specification
695
+
696
+ ## Purpose
697
+
698
+ Fixture baseline for the walk's dependency case.
699
+
700
+ Id families: `P<n>` a task brief.
701
+
702
+ ## Requirements
703
+
704
+ ### Requirement: Base slot
705
+
706
+ The baseline text.
707
+
708
+ #### Scenario: Base scenario
709
+
710
+ - **WHEN** the fixture runs
711
+ - **THEN** it runs
712
+ SPEC
713
+ if [ "$with" = "1" ]; then
714
+ cat > "$d/openspec/changes/zz-provider/specs/boundary/spec.md" <<'SPEC'
715
+ ## ADDED Requirements
716
+
717
+ ### Requirement: Proof-of-life slot
718
+
719
+ The provider's text.
720
+
721
+ #### Scenario: Provider scenario
722
+
723
+ - **WHEN** the provider runs
724
+ - **THEN** it runs
725
+ SPEC
726
+ fi
727
+ cat > "$d/openspec/changes/zz-consumer/specs/boundary/spec.md" <<'SPEC'
728
+ ## MODIFIED Requirements
729
+
730
+ ### Requirement: Proof-of-life slot
731
+
732
+ The consumer's rewritten text.
733
+
734
+ #### Scenario: Consumer scenario
735
+
736
+ - **WHEN** the consumer runs
737
+ - **THEN** it runs
738
+ SPEC
739
+ }
740
+ s="$(mk_scratch dep-ok)" || return 2
741
+ rm -rf "$s/openspec"; mk_dep_tree "$s" 1
742
+ run_flip pending-dependency-resolved "$s"
743
+ if [ "$FLIP_RC" -eq 0 ] && grep -q '^pending-dependency .*zz-provider.*zz-consumer' "$log"; then
744
+ clean "pending-dependency-resolved" "$(grep -m1 '^pending-dependency' "$log")"
745
+ else
746
+ flipbad "pending-dependency-resolved" "要绿并点名提供方与归档顺序,实得 exit $FLIP_RC:$(tail -2 "$log" | tr '\n' ' ')"
747
+ fi
748
+ s="$(mk_scratch dep-missing)" || return 2
749
+ rm -rf "$s/openspec"; mk_dep_tree "$s" 0
750
+ run_flip pending-dependency-missing "$s"
751
+ expect_red "pending-dependency-missing" '^spec-refs: .*Proof-of-life slot.*在基线里不存在'
752
+
753
+ # ⑬ 退场点名(P200):一个未归档 change 的 MODIFIED 退场基线里的一条账本引用 → 逐条点名、**不判
754
+ # 失败**(retired 行不是 undeclared)。夹具自足(最小树,不拷真 openspec):结论不随真仓库里
755
+ # 哪些 change 还没归档而变 —— 旧断言钉在「真树里恰好有未归档的退场引用」这种临时状态上,
756
+ # 真树一归档(retired 4 → 0)就必红。影子 `--break=noretired` 关掉点名 → 本用例必须 BAD。
757
+ mk_retire_tree() { # <根>:基线里一条账本引用被未归档 change 的 MODIFIED 退场
758
+ local d="$1"
759
+ mkdir -p "$d/openspec/specs/boundary" "$d/openspec/changes/zz-retire/specs/boundary"
760
+ cat > "$d/openspec/specs/boundary/spec.md" <<'SPEC'
761
+ # boundary Specification
762
+
763
+ ## Purpose
764
+
765
+ Fixture baseline for the walk's retired-reference case.
766
+
767
+ Id families: `P<n>` a task brief.
768
+
769
+ ## Requirements
770
+
771
+ ### Requirement: Retire slot
772
+
773
+ The baseline text keeps a ledger citation: `docs/team/reports/P200-fixture.md`.
774
+
775
+ #### Scenario: Retire scenario
776
+
777
+ - **WHEN** the fixture runs
778
+ - **THEN** it runs
779
+ SPEC
780
+ cat > "$d/openspec/changes/zz-retire/specs/boundary/spec.md" <<'SPEC'
781
+ ## MODIFIED Requirements
782
+
783
+ ### Requirement: Retire slot
784
+
785
+ The rewritten text drops the citation.
786
+
787
+ #### Scenario: Rewritten scenario
788
+
789
+ - **WHEN** the rewrite runs
790
+ - **THEN** it runs
791
+ SPEC
792
+ }
793
+ s="$(mk_scratch retired-naming)" || return 2
794
+ rm -rf "$s/openspec"; mk_retire_tree "$s"
795
+ run_flip retired-naming "$s"
796
+ if [ "$FLIP_RC" -eq 0 ] && grep -qaE '^retired .*docs/team/reports/P200-fixture\.md by zz-retire$' "$log"; then
797
+ clean "retired-naming" "$(grep -m1 '^retired ' "$log")"
798
+ else
799
+ flipbad "retired-naming" "要绿且逐条点名(retired … by zz-retire),实得 exit $FLIP_RC:$(tail -2 "$log" | tr '\n' ' ')"
800
+ fi
801
+
802
+ # ⑭ 反向守卫:真树的 openspec/specs/** 与 docs/team/inbox/** 逐字节未动
803
+ local after_specs after_inbox
804
+ after_specs="$(tree_hash "$ROOT/openspec/specs")"
805
+ after_inbox="$(tree_hash "$ROOT/docs/team/inbox")"
806
+ if [ "$before_specs" = "$after_specs" ] && [ "$before_inbox" = "$after_inbox" ]; then
807
+ printf 'guard %-32s openspec/specs + docs/team/inbox unchanged (%s)\n' "reverse-guard" "${after_specs:0:12}"
808
+ ok_n=$((ok_n + 1))
809
+ else
810
+ printf 'BAD %-32s 真树被动过:specs %s→%s inbox %s→%s\n' "reverse-guard" \
811
+ "${before_specs:0:12}" "${after_specs:0:12}" "${before_inbox:0:12}" "${after_inbox:0:12}"
812
+ bad_n=$((bad_n + 1))
813
+ fi
814
+
815
+ printf 'spec-refs: --flips %s(%d 用例如预期,%d 个不符;break=%s)\n' \
816
+ "$([ "$bad_n" -eq 0 ] && printf 'OK' || printf 'FAIL')" "$ok_n" "$bad_n" "${BREAK:-none}"
817
+ [ "$bad_n" -eq 0 ] || return 1
818
+ return 0
819
+ }
820
+
821
+ case "$MODE" in
822
+ check) walk "$ROOT" "$TABLE" check ;;
823
+ blocks) walk "$ROOT" "$TABLE" blocks "$CAPARG" ;;
824
+ list) list_declared ;;
825
+ flips) flips ;;
826
+ esac