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,1652 @@
1
+ #!/usr/bin/env bash
2
+ # routes.sh — route sincerity (P99 / roster-writer-and-route-truth, R3).
3
+ #
4
+ # The CLI prints routes (team help's usage lines; the contract schema's notes) that promise a behaviour.
5
+ # This walk judges the printed surface against the real one:
6
+ #
7
+ # Walk A every command-block line of `team help` → command path + every --flag it prints → probe
8
+ # (`<path> <flag>` in a fixture). A parser that answers with the tool's unknown-parameter /
9
+ # unknown-subcommand refusal is RED, naming path+flag. A line that cannot be attributed to a
10
+ # command and its flags is RED, naming the line — never skipped.
11
+ # Control non-vacuity: every path that prints at least one flag must REFUSE an unknown flag
12
+ # (`--frobnicate-probe`); a command that swallows unknown arguments reads as "accepted"
13
+ # otherwise, so it is RED naming the command.
14
+ # Walk B every schema note that names a `team <cmd>`: the command must resolve, and the key must have
15
+ # a declared promise probe that makes the sentence's promise really happen in the fixture
16
+ # (probes assert the EFFECT, never the exit code). The set of keys needing a probe is derived
17
+ # from the schema, so a note added without one is RED naming the key.
18
+ #
19
+ # flips ten mutations that MUST redden their guard (a field defect, a sibling flag, a swallowing
20
+ # parser, an unattributable line, a fake schema route, an unprobed note, a broken promise, a
21
+ # nested run that does not reap, an unclaimed route, a mutated repair), plus a control arm on
22
+ # the last tree that must stay green, plus two red sides of the landing check itself. They are
23
+ # the walk's own proof that it can fail.
24
+ # Every mutation must PROVE IT LANDED (target text 1→0 / new text 0→1, and the copy must
25
+ # differ from the pristine file byte-wise) BEFORE its verdict is judged: a silent no-op patch is
26
+ # reported as `变异没落地(夹具缺陷)`, never as `没有兑现` (M21). And every verdict reads the
27
+ # nested log as a FILE — never `printf … | grep -q`, which under `set -o pipefail` can read a
28
+ # matched log as unmatched (grep -q exits first, printf takes SIGPIPE, the pipeline turns 141).
29
+ #
30
+ # Usage / knobs:
31
+ # bash skills/teamsmith/tests/routes.sh [walk] [control] [promises] [refusals] [flips]
32
+ # TEAM_ROUTES_TREE=<tree> tree under test (default: this checkout; flips use scratch copies)
33
+ # TEAM_ROUTES_KEEP=1 keep the fixture root and print it
34
+ # TEAM_ROUTES_TIMEOUT=<s> per-probe timeout, default 15 (containment, never a verdict: a probe
35
+ # that times out is recorded as accepted with a visible note)
36
+ # TEAM_ROUTES_FAST=1 (or TEAM_SMOKE_FAST=1) run walk/control/promises, SKIP the flips visibly
37
+ # TEAM_TMP_KEEP=1 inherited from the gate's `--keep`: the flips' nested runs keep their
38
+ # temp roots on purpose — the tail prints them and does NOT call it a leak
39
+ # Exit: 0 every selected section green; 1 at least one finding; 3 setup failure.
40
+ #
41
+ # Containment (the walk must not touch the caller): one owned temp root via tests/lib/tmp-root.sh
42
+ # (`${TMPDIR:-/tmp}/teamsmith-routes.XXXXXX`), every run in a fresh git repo under it, a recording
43
+ # `tmux` shim first on PATH that answers session queries as absent and NEVER calls the real tmux,
44
+ # TEAM_MEETINGS_DIR/TEAM_PI_AGENT_DIR inside the fixture, stdin from /dev/null, a hard timeout per probe
45
+ # and an EXIT trap that reaps every directory (unless TEAM_TMP_KEEP=1 asks to keep it, as the CI gate's
46
+ # `--keep` does). Nothing here reads or writes the caller's project.
47
+ set -uo pipefail
48
+
49
+ # ── knobs + identity isolation (must happen first): never inherit the caller's team identity ────────
50
+ _tree_arg="${TEAM_ROUTES_TREE:-}"
51
+ _keep_arg="${TEAM_ROUTES_KEEP:-0}"
52
+ _timeout_arg="${TEAM_ROUTES_TIMEOUT:-}"
53
+ _fast_arg="${TEAM_ROUTES_FAST:-}"
54
+ _smoke_fast_arg="${TEAM_SMOKE_FAST:-}"
55
+ _tmpkeep="${TEAM_TMP_KEEP:-}"
56
+ while IFS='=' read -r _v _; do
57
+ case "$_v" in TEAM_*) unset "$_v" 2>/dev/null || true ;; esac
58
+ done < <(env)
59
+ unset _v 2>/dev/null || true
60
+
61
+ here="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
62
+ tree="${_tree_arg:-$(cd -P "$here/../../.." && pwd)}"
63
+ skill="$tree/skills/teamsmith"
64
+ team="$skill/scripts/team"
65
+ perf_sh="$skill/tests/perf.sh"
66
+ timeout_sec="${_timeout_arg:-15}"
67
+ case "$timeout_sec" in ''|*[!0-9]*) timeout_sec=15 ;; esac
68
+ keep="$_keep_arg"
69
+ case "${_fast_arg:-${_smoke_fast_arg:-0}}" in
70
+ 1|y|Y|yes|YES|true|TRUE|on|ON) FAST=1 ;;
71
+ *) FAST=0 ;;
72
+ esac
73
+ [ -n "$_tmpkeep" ] && export TEAM_TMP_KEEP="$_tmpkeep"
74
+
75
+ # 嵌套 run 与父 run 共享同一份 run 台账:SMOKE_TMP_RUN_ID 是非 TEAM_ 的传播位(嵌套 run 的身份清理
76
+ # 会清掉 TEAM_*,清不掉它);没有它时自己起一个 —— 收尾的「嵌套根都回收了」据此能看见**每一个**
77
+ # 嵌套根(find -newer 只看得到最后一个:任何后续 scratch 树的建删都会刷新 $tmp 的 mtime)。
78
+ export SMOKE_TMP_RUN_ID="${SMOKE_TMP_RUN_ID:-routes-$$-$(date +%s 2>/dev/null || printf 0)}"
79
+
80
+ # shellcheck source=tests/lib/tmp-root.sh
81
+ . "$here/lib/tmp-root.sh"
82
+ tmp="$(tmp_root_create routes)" || { printf 'routes: 临时根建不起来\n' >&2; exit 3; }
83
+ PASS=0
84
+ FIND=0
85
+ SKIP=0
86
+
87
+ cleanup() {
88
+ [ "${BASHPID:-$$}" = "$$" ] || return 0 # subshell-guarded (P50 hygiene)
89
+ if [ "$keep" = "1" ]; then printf '\n保留夹具目录:%s\n' "$tmp"
90
+ else tmp_root_reap_all; fi
91
+ }
92
+ trap cleanup EXIT
93
+ section() { printf '\n\033[1m== %s ==\033[0m\n' "$1"; }
94
+ ok() { printf ' \033[32m✓\033[0m %s\n' "$1"; PASS=$((PASS + 1)); }
95
+ finding() { printf ' \033[31m✗\033[0m %s\n' "$1"; FIND=$((FIND + 1)); }
96
+ skip() { printf ' \033[33mSKIP\033[0m %s\n' "$1"; SKIP=$((SKIP + 1)); }
97
+
98
+ [ -f "$team" ] || { printf 'routes: 没有 CLI:%s\n' "$team" >&2; exit 3; }
99
+ [ -f "$perf_sh" ] || { printf 'routes: 没有 tests/perf.sh:%s\n' "$perf_sh" >&2; exit 3; }
100
+ TIMEOUT_BIN=""
101
+ command -v timeout >/dev/null 2>&1 && TIMEOUT_BIN=timeout
102
+
103
+ SECTIONS=("$@")
104
+ # 段集是**显式**的:给了段名就只跑给的段;不带段名 = 全部四段(FAST 下 flips 按下面的 skip 可见跳过)。
105
+ # 旧写法给任何不含 flips 的子集追加 flips —— flips 的每一臂都在 scratch 树上再跑一遍
106
+ # `routes.sh walk`,那个嵌套 run 又被追加 flips,于是自己生自己:实测进程链每 ~40 秒长一层、
107
+ # 跑 12 分钟不返回(08:08 → 08:20,32 个进程),只能人工杀。子集的边界由调用者给。
108
+ [ "${#SECTIONS[@]}" -gt 0 ] || SECTIONS=(walk control promises refusals flips)
109
+ [ "${#SECTIONS[@]}" -gt 0 ] || SECTIONS=(walk control promises refusals)
110
+ want() { local s; for s in "${SECTIONS[@]}"; do [ "$s" = "$1" ] && return 0; done; return 1; }
111
+
112
+ # ── containment: the recording tmux shim (never calls the real tmux) ────────────────────────────────
113
+ # P140 扩展:除了记账,它还替窗口 harness 签证 —— 窗口注册表(new-window/kill-window/list-windows)
114
+ # 让“窗口存在”这件事自洽;TEAM_ROUTES_FAKE_LAUNCH=1 时,respawn-pane 把 harness 命令里的
115
+ # (nonce, marker) 解析出来写下启动/退出证据(假窗口但真证据,不碰真 server)。
116
+ SHIM="$tmp/shim"
117
+ TMUX_LOG="$tmp/tmux-calls.log"
118
+ WINREG="$tmp/windows.reg"
119
+ mkdir -p "$SHIM"
120
+ cat > "$SHIM/tmux" <<'SHIM'
121
+ #!/usr/bin/env bash
122
+ # Recording tmux: session queries answer "absent", windows live in a registry file, everything is
123
+ # recorded and exits 0. It never invokes the real tmux, so no call this process makes can reach a
124
+ # live server.
125
+ printf '%s\n' "$*" >> "${TEAM_ROUTES_TMUX_LOG:-/dev/null}" 2>/dev/null || true
126
+ win="${TEAM_ROUTES_WINDOWS:-/dev/null}"
127
+ sub="" t="" name=""
128
+ while [ $# -gt 0 ]; do
129
+ case "$1" in
130
+ -L|-S|-f|-T|-c) shift 2 ;;
131
+ -*) shift ;;
132
+ *) sub="$1"; shift; break ;;
133
+ esac
134
+ done
135
+ rest=("$@")
136
+ _i=0
137
+ while [ "$_i" -lt ${#rest[@]} ]; do
138
+ case "${rest[$_i]}" in
139
+ -t) t="${rest[$((_i+1))]:-}"; _i=$((_i+2)) ;;
140
+ -n) name="${rest[$((_i+1))]:-}"; _i=$((_i+2)) ;;
141
+ *) _i=$((_i+1)) ;;
142
+ esac
143
+ done
144
+ reg() { [ -n "$1" ] || return 0; grep -qxF "$1" "$win" 2>/dev/null || printf '%s\n' "$1" >> "$win"; }
145
+ unreg() { [ -n "$1" ] || return 0; [ -f "$win" ] || return 0; grep -vxF "$1" "$win" > "$win.tmp" 2>/dev/null || true; mv "$win.tmp" "$win"; }
146
+ case "$sub" in
147
+ has-session) exit 1 ;;
148
+ list-windows)
149
+ [ -f "$win" ] || exit 0
150
+ if [ -n "$t" ]; then awk -F: -v s="$t" '$1==s{print $2}' "$win"; else cat "$win"; fi
151
+ exit 0 ;;
152
+ new-window) reg "${t:-?}:$name"; exit 0 ;;
153
+ kill-window) unreg "${t:-?}"; exit 0 ;;
154
+ respawn-pane)
155
+ # P140:TEAM_ROUTES_FAKE_LAUNCH=1 时替窗口 harness 写下本轮 (nonce, pid) 启动证据与 (nonce, 0)
156
+ # 退出证据 —— 内容从 harness 命令行里解析,不是猜的;默认关着(只有最终拒绝路线真拉起时用)。
157
+ if [ "${TEAM_ROUTES_FAKE_LAUNCH:-0}" = "1" ]; then
158
+ inner="" prev=""
159
+ for a in "${rest[@]}"; do [ "$prev" = "-lc" ] && inner="$a"; prev="$a"; done
160
+ if [ -n "$inner" ]; then
161
+ marker="$(printf '%s' "$inner" | grep -oE "/[^ \"']*dispatch-[A-Za-z0-9_.-]+\.spawn" | head -1)"
162
+ exitf="$(printf '%s' "$inner" | grep -oE "/[^ \"']*dispatch-[A-Za-z0-9_.-]+\.exit" | head -1)"
163
+ nonce="$(printf '%s' "$inner" | sed -n 's/.*printf "%s %s\\n" \([^ ][^ ]*\) \$\$ >.*/\1/p' | head -1)"
164
+ [ -n "$marker" ] && [ -n "$nonce" ] && printf '%s %s\n' "$nonce" 4242 > "$marker"
165
+ [ -n "$exitf" ] && [ -n "$nonce" ] && printf '%s %s\n' "$nonce" 0 > "$exitf"
166
+ fi
167
+ fi
168
+ exit 0 ;;
169
+ list-panes|list-sessions|display-message|set-window-option|capture-pane) exit 0 ;;
170
+ show-options) printf 'on\n'; exit 0 ;;
171
+ esac
172
+ exit 0
173
+ SHIM
174
+ chmod +x "$SHIM/tmux"
175
+ # `team` 解析器(Walk C 要**粘贴路线本身**并执行:路线里的 `team …` 必须落在被测的那棵树上)
176
+ cat > "$SHIM/team" <<EOF
177
+ #!/usr/bin/env bash
178
+ exec bash $(printf '%q' "$team") "\$@"
179
+ EOF
180
+ chmod +x "$SHIM/team"
181
+ : > "$TMUX_LOG"
182
+ : > "$WINREG"
183
+
184
+ # ── fixtures ───────────────────────────────────────────────────────────────────────────────────────
185
+ # new_fixture <name> → a fresh git repo under $tmp, `team init`-ed with a clean identity.
186
+ # The tree under test provides the CLI, so a scratch TEAM_ROUTES_TREE flips the tool too.
187
+ new_fixture() {
188
+ local name="$1"
189
+ local d="$tmp/$name"
190
+ rm -rf "$d"; mkdir -p "$d"
191
+ ( cd "$d" && git init -q -b main && git config user.email routes@teamsmith && git config user.name routes \
192
+ && printf '{"name":"%s","scripts":{"verify":"true"}}\n' "$name" > package.json \
193
+ && git add -A && git commit -qm init ) >/dev/null 2>&1
194
+ if ! ( cd "$d" && env PATH="$SHIM:$PATH" TEAM_PI_BIN=/bin/true TEAM_ROUTES_TMUX_LOG="$TMUX_LOG" \
195
+ TEAM_MEETINGS_DIR="$d/.meetings" TEAM_PI_AGENT_DIR="$d/.pi-agent" \
196
+ bash "$team" init --no-skills --session "routes-$name" --agents "dev verify" --vcs local \
197
+ --gates true --docs docs/team ) >"$tmp/$name-init.log" 2>&1; then
198
+ printf 'routes: 夹具 %s 的 team init 失败:\n' "$name" >&2
199
+ tail -5 "$tmp/$name-init.log" >&2
200
+ return 1
201
+ fi
202
+ printf '%s\n' "$d"
203
+ }
204
+
205
+ # mr_run <fixture> <team args...> → stdout+stderr in MR_OUT, exit code in MR_RC (timeout contained)
206
+ mr_run() {
207
+ local d="$1"; shift
208
+ local out rc
209
+ out="$( cd "$d" && env -u TMUX -u TMUX_PANE \
210
+ PATH="$SHIM:$PATH" TEAM_PI_BIN=/bin/true TEAM_ROUTES_TMUX_LOG="$TMUX_LOG" \
211
+ TEAM_MEETINGS_DIR="$d/.meetings" TEAM_PI_AGENT_DIR="$d/.pi-agent" \
212
+ ${TIMEOUT_BIN:+"$TIMEOUT_BIN" "$timeout_sec"} bash "$team" "$@" </dev/null 2>&1 )"
213
+ rc=$?
214
+ MR_OUT="$out"; MR_RC="$rc"
215
+ }
216
+
217
+ first_line() { printf '%s' "$1" | grep -av '^[[:space:]]*$' | head -1 | cut -c1-90; }
218
+ sha_file() { sha256sum "$1" 2>/dev/null | awk '{print $1}'; }
219
+
220
+ # ── the CLI's command table (derived from the source, not a hand-kept list) ─────────────────────────
221
+ # tops: the dispatcher's own case labels in scripts/team (+ the pre-dispatch help/version).
222
+ # subs: `parent sub` pairs from every `case "$sub" in` block, attributed to its enclosing team_cmd_<x>.
223
+ mr_known_tables() {
224
+ local src="$skill/scripts/team" f
225
+ MRI_TOPS="help"
226
+ MRI_TOPS="$MRI_TOPS $(awk '
227
+ /^case "\$CMD" in/ { inb=1; next }
228
+ inb && /^esac/ { inb=0 }
229
+ inb && /^[[:space:]]+[A-Za-z0-9_'"'"'|.-]+\)/ {
230
+ line=$0; sub(/^[[:space:]]+/,"",line); sub(/\).*/,"",line)
231
+ n=split(line,a,"|"); for (i=1;i<=n;i++){ gsub(/[^a-z0-9-]/,"",a[i]); if(a[i]!="") printf "%s ", a[i] }
232
+ }' "$src")"
233
+ MRI_SUBS=""
234
+ for f in "$skill"/scripts/lib/cmd-*.sh; do
235
+ [ -f "$f" ] || continue
236
+ MRI_SUBS="$MRI_SUBS $(awk '
237
+ /^[a-z_]+\(\) *\{/ { fn=$0; sub(/\(\).*/,"",fn); sub(/^team_cmd_/,"",fn) }
238
+ /case "\$sub" in/ { insub=1; depth=0; next }
239
+ insub && /case .* in/ { depth++; next }
240
+ insub && /^[[:space:]]*esac/ { if (depth>0) { depth--; next } insub=0; next }
241
+ insub && /^[[:space:]]+[A-Za-z0-9_'"'"'|.-]+\)/ {
242
+ line=$0; sub(/^[[:space:]]+/,"",line); sub(/\).*/,"",line)
243
+ n=split(line,a,"|"); for (i=1;i<=n;i++){ gsub(/[^a-z0-9-]/,"",a[i]); if(a[i]!="" && fn!="") printf "%s:%s;", fn, a[i] }
244
+ }' "$f")"
245
+ done
246
+ if [ "${ROUTES_DEBUG_TABLES:-0}" = "1" ]; then
247
+ printf 'tops=[%s]\nsubs=[%s]\n' "$MRI_TOPS" "$MRI_SUBS" > "$tmp/debug-tables.txt"
248
+ fi
249
+ }
250
+
251
+ # ── Walk A: parse `team help` (awk), then probe every claim ─────────────────────────────────────────
252
+ mr_write_parser() {
253
+ cat > "$tmp/walk-a.awk" <<'AWK'
254
+ function fail(ln, msg) { printf "FAIL\t%d\t%s\n", ln, msg }
255
+ function tokenize(s, arr, offs, rest, tok, n, pos) {
256
+ n=0; rest=s; pos=1
257
+ while (match(rest, /\[[^]]*\]|[^ \t]+/)) {
258
+ tok=substr(rest, RSTART, RLENGTH)
259
+ n++; arr[n]=tok; offs[n]=pos+RSTART-1
260
+ pos += RSTART-1+RLENGTH; rest=substr(rest, RSTART+RLENGTH)
261
+ }
262
+ return n
263
+ }
264
+ function isph(t) {
265
+ if (t ~ /^<.*>$/) return 1
266
+ if (t ~ /^".*"$/ || t ~ /^'.*'$/) return 1
267
+ if (t=="..." || t=="…" || t=="ID" || t=="N" || t=="X") return 1
268
+ return 0
269
+ }
270
+ function addverb(w) { if (!(w in VERBW)) { VERBW[w]=1; NV++; VERBW_LIST[NV]=w } }
271
+ function lastwordpos(s, w, re, off, best) {
272
+ re = "(^|[ \t])" w "([^ \t]|$)"
273
+ off=0; best=0
274
+ while (off < length(s) && match(substr(s, off+1), re)) {
275
+ best = off + RSTART
276
+ off = best + RLENGTH - 2
277
+ if (RLENGTH <= 0) break
278
+ }
279
+ return best
280
+ }
281
+ function emit(ln, path, flag, key) {
282
+ key = path "\t" flag
283
+ if (key in EMITTED) return
284
+ EMITTED[key]=1
285
+ printf "CLAIM\t%d\t%s\t%s\n", ln, path, flag
286
+ }
287
+ function one_fragment(frag, ln, nt, toks, offs, i, t, core, j, k, na, alts, ok, usage_end, rest, base, fl, fpos, bind, best, w, p, nnp, newp, isbr) {
288
+ nt = tokenize(frag, toks, offs)
289
+ # `version / help` (and only that shape): a plain ` / ` splits into fragments when BOTH sides
290
+ # start with a command; `perf … (交互首帧 / 帧装配 / …)` is description text and must not split.
291
+ if (nt >= 1) {
292
+ # handled by the caller (expand_slash)
293
+ }
294
+ t = toks[1]; gsub(/^\[|\]$/, "", t)
295
+ if (!(t in TOP)) { fail(ln, "这一行无法归属到命令(首 token「" t "」不是 CLI 的命令):" frag); return }
296
+ NP=1; PATHS[1]=t; NV=0; delete VERBW; delete VERBW_LIST; delete EMITTED
297
+ usage_end = offs[1]+length(toks[1])-1
298
+ for (i=2; i<=nt; i++) {
299
+ t=toks[i]
300
+ isbr = (t ~ /^\[.*\]$/)
301
+ core=t; if (isbr) core=substr(t,2,length(t)-2)
302
+ if (isph(core) || core ~ /^-/) { usage_end = offs[i]+length(t)-1; continue }
303
+ if (index(core, "|") > 0) {
304
+ na=split(core, alts, "|"); ok=1
305
+ for (j=1;j<=NP;j++) for (kk=1;kk<=na;kk++) if (!(PATHS[j] " " alts[kk] in SUB)) ok=0
306
+ if (ok) {
307
+ nnp=0
308
+ for (j=1;j<=NP;j++) for (kk=1;kk<=na;kk++) { nnp++; newp[nnp]=PATHS[j] " " alts[kk] }
309
+ NP=nnp; for (j=1;j<=nnp;j++) PATHS[j]=newp[j]
310
+ for (kk=1;kk<=na;kk++) addverb(alts[kk])
311
+ usage_end = offs[i]+length(t)-1
312
+ continue
313
+ }
314
+ # 未解析的组:可选的([...])当参数——如 standby [on|off|status];不带的则是一句「这个命令有这些子命令」
315
+ # 的假活,必须红(不能静默降级成只有主命令)。
316
+ if (isbr) break
317
+ fail(ln, "这一行的命令组无法解析(" core "):" frag); return
318
+ }
319
+ ok=1
320
+ for (j=1;j<=NP;j++) if (!(PATHS[j] " " core in SUB)) ok=0
321
+ if (ok) {
322
+ for (j=1;j<=NP;j++) PATHS[j]=PATHS[j] " " core
323
+ addverb(core)
324
+ usage_end = offs[i]+length(t)-1
325
+ continue
326
+ }
327
+ break # 参数/描述文本:命令部分到此为止
328
+ }
329
+ # every --flag on the line, judged at its own offset
330
+ rest=frag; base=0
331
+ while (match(rest, /--[a-z0-9-]+/)) {
332
+ fl=substr(rest, RSTART, RLENGTH)
333
+ fpos=base+RSTART
334
+ base=base+RSTART+RLENGTH-1
335
+ rest=substr(rest, RSTART+RLENGTH)
336
+ if (fpos <= usage_end) {
337
+ for (j=1;j<=NP;j++) emit(ln, PATHS[j], fl)
338
+ continue
339
+ }
340
+ # description flag: the nearest preceding verb token of this fragment's own group, else the
341
+ # fragment's command(s) — `board …(add [--allow-dup] …)` binds to `board add`, `perf …;--host …`
342
+ # to `perf`.
343
+ best=0
344
+ for (kk=1;kk<=NV;kk++) { p=lastwordpos(substr(frag, 1, fpos-1), VERBW_LIST[kk]); if (p>best) best=p }
345
+ bind=0
346
+ if (best > 0) {
347
+ for (kk=1;kk<=NV;kk++) {
348
+ w=VERBW_LIST[kk]
349
+ if (lastwordpos(substr(frag, 1, fpos-1), w) != best) continue
350
+ for (j=1;j<=NP;j++) if (PATHS[j] ~ ("(^| )" w "$")) { emit(ln, PATHS[j], fl); bind=1 }
351
+ }
352
+ }
353
+ if (bind == 0) {
354
+ for (j=1;j<=NP;j++) emit(ln, PATHS[j], fl)
355
+ }
356
+ }
357
+ }
358
+ function expand_slash(frag, ln, np, parts, i, first, ok) {
359
+ np = split(frag, parts, / \/ /)
360
+ if (np > 1) {
361
+ ok=1
362
+ for (i=1;i<=np;i++) { first=parts[i]; sub(/^[ \t]+/,"",first); sub(/[ \t].*/,"",first); if (!(first in TOP)) ok=0 }
363
+ if (ok) { for (i=1;i<=np;i++) one_fragment(parts[i], ln); return }
364
+ }
365
+ one_fragment(frag, ln)
366
+ }
367
+ BEGIN {
368
+ n=split(tops, ta, " "); for (i=1;i<=n;i++) { gsub(/^[ \t]+|[ \t]+$/, "", ta[i]); if (ta[i]!="") TOP[ta[i]]=1 }
369
+ m=split(subs, sa, ";")
370
+ for (i=1;i<=m;i++) {
371
+ if (sa[i]=="") continue
372
+ if (split(sa[i], kv, ":") < 2) continue
373
+ p=kv[1]; gsub(/^[ \t]+|[ \t]+$/, "", p)
374
+ c=split(kv[2], sb, ",")
375
+ for (j=1;j<=c;j++) { gsub(/^[ \t]+|[ \t]+$/, "", sb[j]); if (sb[j]!="") SUB[p " " sb[j]]=1 }
376
+ }
377
+ inblock=0; lastindent=-1
378
+ }
379
+ {
380
+ line=$0
381
+ if (line ~ /^[ \t]*$/) next
382
+ if (line ~ /^[ \t]*──/) { inblock=1; lastindent=-1; next }
383
+ if (!inblock) next
384
+ m=match(line, /[^ \t]/); ind=m-1; rest=substr(line, m)
385
+ first=rest; sub(/[ \t].*/,"",first)
386
+ isentry = (first in TOP)
387
+ if (ind == 0) {
388
+ # 列 0 的行:命令块的正文一律缩进(对齐的续行更深)。
389
+ # · 打旗标的 → 无法归属(修复前的 board 形状:列 0 + [--allow-dup])→ 红;
390
+ # · 首 token 是命令的 → 未缩进的条目(形状约定是 2 空格)→ 红;
391
+ # · 其余(help 末尾的 配置:/协议: 页脚,不打印旗标)→ 块到此为止,忽略。
392
+ if (line ~ /--[a-z0-9-]+/) { fail(NR, "命令块里列 0 的行带着旗标(无法归属到任何条目):" rest) }
393
+ else if (isentry) { fail(NR, "命令块里的命令条目没有缩进(列 0):" rest) }
394
+ inblock=0; next
395
+ }
396
+ if (lastindent >= 0 && ind > lastindent) { next } # 缩进更深的行是上一条目的续行(哪怕它首 token 像个命令)
397
+ if (!isentry) {
398
+ fail(NR, "这一行既不是命令条目,也没有比上一条目缩进更深(无法归属):" rest)
399
+ next
400
+ }
401
+ lastindent=ind
402
+ printf "ENTRY\t%d\n", NR
403
+ # 全角分隔符在 bash 侧先换成 \x1f —— awk(mawk)按字节看字符类,直接写 [|/] 会在
404
+ # 任何中文 UTF-8 字节上乱切(实测把一行劈成单字碎片)。
405
+ nf = split($0, frags, "\037")
406
+ for (i=1;i<=nf;i++) expand_slash(frags[i], NR)
407
+ }
408
+ AWK
409
+ }
410
+
411
+ # mr_probe_mode <path> → `run`(默认)或 `source:<file>:<reason>`。
412
+ # `source` 只用于「这条命令只是 exec 另一个程序」的路径:跑它来验证一个旗标会真的把那个程序跑起来
413
+ # (perf = 性能套件)—— 那时读它自己的参数表。声明必须带文件与理由。
414
+ mr_probe_mode() {
415
+ case "$1" in
416
+ perf) printf 'source:%s:%s\n' "$perf_sh" 'perf 只 exec tests/perf.sh;--host 是它的布尔模式,跑了就是跑整套性能测量' ;;
417
+ *) printf 'run\n' ;;
418
+ esac
419
+ }
420
+
421
+ # mr_probe_claim <fixture> <path> <flag> → 0 accepted / 1 refused(被解析器明确拒绝)
422
+ # Refused = the tool's unknown-parameter or unknown-subcommand refusal that names this flag.
423
+ mr_probe_claim() {
424
+ local d="$1" path="$2" flag="$3" mode file reason
425
+ mode="$(mr_probe_mode "$path")"
426
+ case "$mode" in
427
+ source:*)
428
+ file="${mode#source:}"; reason="${file#*:}"; file="${file%%:*}"
429
+ if [ -f "$file" ] && grep -qE "(^|[[:space:]|])${flag//-/\\-}\\)" "$file"; then
430
+ MR_OUT="source 模式($reason):$file 的参数表里有一整条 $flag 分支"
431
+ MR_RC=0
432
+ return 0
433
+ fi
434
+ MR_OUT="source 模式($reason):$file 的参数表里找不到 $flag 分支"
435
+ MR_RC=1
436
+ return 1 ;;
437
+ esac
438
+ mr_run "$d" $path "$flag"
439
+ case "$MR_OUT" in
440
+ *未知参数*|*未知子命令*) case "$MR_OUT" in *"$flag"*) return 1 ;; esac ;;
441
+ esac
442
+ return 0
443
+ }
444
+
445
+ mr_walk_a() {
446
+ section "walk · team help 的每条用法行 ↔ 解析器(打印的每个 --flag 都要被自己的解析器接受)"
447
+ local help_out
448
+ help_out="$( cd "$tmp" && env PATH="$SHIM:$PATH" bash "$team" help 2>&1 )"
449
+ if ! printf '%s\n' "$help_out" | grep -q '用法:'; then
450
+ finding "team help 没有打印用法头部(不可解析)"
451
+ return 1
452
+ fi
453
+ mr_known_tables
454
+ mr_write_parser
455
+ printf '%s\n' "$help_out" > "$tmp/help.txt"
456
+ # 全角 |/ → \x1f(bash 的字面替换按字节序列匹配,安全;awk 的字符类不行)。
457
+ local prepped="" hline
458
+ while IFS= read -r hline; do
459
+ hline="${hline//|/$'\x1f'}"
460
+ prepped="$prepped${hline////$'\x1f'}"$'\n'
461
+ done <<< "$help_out"
462
+ printf '%s' "$prepped" | awk -v tops="$MRI_TOPS" -v subs="$MRI_SUBS" -f "$tmp/walk-a.awk" > "$tmp/walk-a.tsv" 2>"$tmp/walk-a.err"
463
+ if [ -s "$tmp/walk-a.err" ]; then
464
+ finding "Walk A 解析器报错:$(head -2 "$tmp/walk-a.err" | tr '\n' ' ')"
465
+ return 1
466
+ fi
467
+
468
+ local n_entry n_claim
469
+ n_entry="$(grep -ac '^ENTRY' "$tmp/walk-a.tsv" || true)"
470
+ n_claim="$(grep -ac '^CLAIM' "$tmp/walk-a.tsv" || true)"
471
+ if [ "${n_entry:-0}" -lt 5 ] || [ "${n_claim:-0}" -lt 10 ]; then
472
+ finding "Walk A 解析出的条目/断言太少(entry=$n_entry claim=$n_claim)—— 解析器或 help 形状变了"
473
+ grep -a '^FAIL' "$tmp/walk-a.tsv" | head -5 | sed 's/^/ /'
474
+ return 1
475
+ fi
476
+ local ln line
477
+ while IFS=$'\t' read -r _ ln; do
478
+ line="$(sed -n "${ln}p" "$tmp/help.txt" | sed 's/^[[:space:]]*//' | cut -c1-60)"
479
+ ok "用法行 L$ln:$line"
480
+ done < <(grep -a '^ENTRY' "$tmp/walk-a.tsv")
481
+ local failed=0
482
+ while IFS=$'\t' read -r _ ln rest; do
483
+ finding "L$ln 无法归属(解析器拒绝静默跳过):$rest"
484
+ failed=1
485
+ done < <(grep -a '^FAIL' "$tmp/walk-a.tsv")
486
+ [ "$failed" = "1" ] && return 1
487
+
488
+ local fixture
489
+ fixture="$(new_fixture walk-a)" || return 1
490
+ local path flag note
491
+ while IFS=$'\t' read -r _ ln path flag; do
492
+ if mr_probe_claim "$fixture" "$path" "$flag"; then
493
+ note="$(first_line "$MR_OUT")"
494
+ ok "断言 L$ln:$path $flag → rc=$MR_RC ${note:+| $note}"
495
+ else
496
+ finding "断言 L$ln:$path $flag 被自己的解析器拒绝(原文:$(first_line "$MR_OUT"))"
497
+ failed=1
498
+ fi
499
+ done < <(grep -a '^CLAIM' "$tmp/walk-a.tsv")
500
+ [ "$failed" = "1" ] && return 1
501
+ return 0
502
+ }
503
+
504
+ # ── control: an unknown flag must be refused by every flag-printing path ────────────────────────────
505
+ mr_control() {
506
+ section "control · 非空洞臂:打印旗标的每条路径都必须拒绝未知参数(--frobnicate-probe)"
507
+ [ -f "$tmp/walk-a.tsv" ] || {
508
+ local help_out prepped hline
509
+ help_out="$( cd "$tmp" && env PATH="$SHIM:$PATH" bash "$team" help 2>&1 )"
510
+ mr_known_tables
511
+ mr_write_parser
512
+ printf '%s\n' "$help_out" > "$tmp/help.txt"
513
+ prepped=""
514
+ while IFS= read -r hline; do
515
+ hline="${hline//|/$'\x1f'}"
516
+ prepped="$prepped${hline////$'\x1f'}"$'\n'
517
+ done <<< "$help_out"
518
+ printf '%s' "$prepped" | awk -v tops="$MRI_TOPS" -v subs="$MRI_SUBS" -f "$tmp/walk-a.awk" > "$tmp/walk-a.tsv" 2>/dev/null
519
+ }
520
+ local fixture; fixture="$(new_fixture control)" || return 1
521
+ local p n=0 bad=0
522
+ while IFS= read -r p; do
523
+ [ -n "$p" ] || continue
524
+ n=$((n + 1))
525
+ mr_run "$fixture" $p --frobnicate-probe
526
+ case "$MR_OUT" in
527
+ *未知参数*|*未知子命令*) case "$MR_OUT" in *--frobnicate-probe*)
528
+ ok "控制:$p --frobnicate-probe 被拒(解析器的未知参数消息,rc=$MR_RC)"; continue ;;
529
+ esac ;;
530
+ esac
531
+ # 用法行拒绝也算拒(team_usage_die 的签名行)—— reload 就是这种形状(设计 §1.2 实测)。
532
+ if [ "$MR_RC" != "0" ] && printf '%s' "$MR_OUT" | tail -1 | grep -qE '^run: .* help$'; then
533
+ ok "控制:$p --frobnicate-probe 被拒(用法行,rc=$MR_RC)"
534
+ continue
535
+ fi
536
+ finding "控制:$p 吞掉了未知参数 --frobnicate-probe(rc=$MR_RC)—— 「打印出来就被接受」不可信"
537
+ bad=1
538
+ done < <(awk -F'\t' '$1=="CLAIM" {print $3}' "$tmp/walk-a.tsv" | sort -u)
539
+ [ "$n" -gt 0 ] || { finding "控制臂没有扫到任何路径(上游解析空转)"; return 1; }
540
+ [ "$bad" = "1" ] && return 1
541
+ return 0
542
+ }
543
+
544
+ # ── Walk B: the schema notes' promises ─────────────────────────────────────────────────────────────
545
+ # Probe declaration table: key → probe id. The set of keys needing a probe is derived from the schema
546
+ # notes themselves; a note that names a team command without an entry here is a finding naming the key.
547
+ mr_probe_for() { # <KEY> → probe id(空 = 没有声明)
548
+ case "$1" in
549
+ TEAM_PROJECT|TEAM_SESSION|TEAM_PM_WINDOW) printf 'identity\n' ;;
550
+ TEAM_AGENTS) printf 'roster\n' ;;
551
+ TEAM_AGENT_MODELS) printf 'seat-model\n' ;;
552
+ TEAM_PM_MODEL) printf 'pm-model\n' ;;
553
+ TEAM_PULSE_WINDOW) printf 'pulse-window\n' ;;
554
+ TEAM_DEFAULT_MODEL) printf 'default-model\n' ;;
555
+ TEAM_BG_STOP_GRACE) printf 'bg-stop-grace\n' ;;
556
+ *) printf '\n' ;;
557
+ esac
558
+ }
559
+
560
+ mr_schema_rows() { # → KEY|note(每行一条;note 是 schema 第 8 列)
561
+ env PATH="$SHIM:$PATH" bash -c '
562
+ set -uo pipefail
563
+ . "$1"
564
+ team_config_schema' _ "$skill/scripts/lib/cmd-config.sh" 2>/dev/null \
565
+ | awk -F'|' '!/^#/ && NF>=8 { txt=$7; if (txt=="-") txt=""; if ($8!="") txt = (txt=="" ? $8 : txt " " $8); print $1 "\t" txt }'
566
+ }
567
+
568
+ mr_walk_b() {
569
+ section "promises · schema 注释点名的命令 ↔ 夹具里真的会发生(探针断言效果,不看退出码)"
570
+ local rows="$tmp/schema-notes.tsv"
571
+ mr_schema_rows > "$rows"
572
+ if [ "$(wc -l < "$rows")" -lt 50 ]; then
573
+ finding "schema 读不出足够的行($(wc -l < "$rows"))—— cmd-config.sh 的 schema 形状变了"
574
+ return 1
575
+ fi
576
+ mr_known_tables
577
+ local top_re=" $MRI_TOPS "
578
+ local bad=0 key note rest cmd probe
579
+ while IFS=$'\t' read -r key note; do
580
+ [ -n "$key" ] || continue
581
+ rest="$note"
582
+ local seen_cmds=""
583
+ while [[ "$rest" =~ team[[:space:]]+([a-z][a-z0-9-]*) ]]; do
584
+ cmd="${BASH_REMATCH[1]}"
585
+ rest="${rest#*"${BASH_REMATCH[0]}"}"
586
+ case "$seen_cmds" in *" $cmd "*) continue ;; esac
587
+ seen_cmds="$seen_cmds$cmd "
588
+ case "$top_re" in
589
+ *" $cmd "*) ;;
590
+ *) finding "promise:$key 的注释点名了 CLI 没有的命令「team $cmd」"; bad=1 ;;
591
+ esac
592
+ done
593
+ [ -n "$seen_cmds" ] || continue
594
+ probe="$(mr_probe_for "$key")"
595
+ if [ -z "$probe" ]; then
596
+ finding "promise:$key 的注释点名了 team 命令(${seen_cmds% })但没有声明 promise 探针(schema 就是覆盖来源)"
597
+ bad=1
598
+ continue
599
+ fi
600
+ case "$probe" in
601
+ identity) mr_promise_identity "$key" || bad=1 ;;
602
+ roster) mr_promise_roster "$key" || bad=1 ;;
603
+ seat-model) mr_promise_seat_model "$key" || bad=1 ;;
604
+ pm-model) mr_promise_pm_model "$key" || bad=1 ;;
605
+ pulse-window) mr_promise_pulse_window "$key" || bad=1 ;;
606
+ default-model) mr_promise_default_model "$key" || bad=1 ;;
607
+ bg-stop-grace) mr_promise_bg_stop_grace "$key" || bad=1 ;;
608
+ *) finding "promise:$key 的探针 id「$probe」没有实现(覆盖表与实现脱节)"; bad=1 ;;
609
+ esac
610
+ done < "$rows"
611
+ [ "$bad" = "1" ] && return 1
612
+ return 0
613
+ }
614
+
615
+ # -- the promise probes -----------------------------------------------------------------------------
616
+ # P159:宽限旋钮的探针。作业是夹具自己 spawn 的**新会话组头**(pgid == pid)且 **trap "" TERM**
617
+ # (SIG_IGN 会被 exec 继承,组里那个 sleep 也一样)——于是只有 KILL 能收掉它,"先 TERM、等宽限、
618
+ # 再 KILL" 这条承诺真的会发生。两次跑只差 config.sh 里手改的那个值:0 必须**不等待**(< 3s),
619
+ # 2 必须**等够**(≥ 2s)。判的是效果(进程死了 + 账本 signal=TERM,KILL + 用时差),不看退出码。
620
+ mr_bg_ignore_term_job() { # <fixture> <id> → 起作业、写记录;MR_BG_PID/MR_BG_PGID/MR_BG_START
621
+ local d="$1" id="$2" st rest
622
+ setsid bash -c 'trap "" TERM; sleep 300 & echo $! > "$1"; wait' _ "$d/$id-child.pid" >/dev/null 2>&1 &
623
+ MR_BG_PID=$!
624
+ disown "$MR_BG_PID" 2>/dev/null || true # 收掉作业时不要打「Killed」噪声(判定只看进程与账本)
625
+ sleep 0.3
626
+ st="$(cat "/proc/$MR_BG_PID/stat" 2>/dev/null)" || return 1
627
+ rest="${st##*)}"
628
+ # shellcheck disable=SC2086
629
+ set -- $rest
630
+ MR_BG_PGID="$3"; MR_BG_START="${20}"
631
+ mkdir -p "$d/.pi/team/state/bg"
632
+ printf 'id=%s\npid=%s\npgid=%s\nstart=%s\ncwd=%s\nlog=%s\ncmd=sleep 300\n' \
633
+ "$id" "$MR_BG_PID" "$MR_BG_PGID" "$MR_BG_START" "$d" "$d/.pi/team/state/bg/$id.log" \
634
+ > "$d/.pi/team/state/bg/$id.job"
635
+ return 0
636
+ }
637
+
638
+ mr_promise_bg_stop_grace() { # <KEY>
639
+ local key="$1" d t0 t1 spent
640
+ d="$(new_fixture promise-bg-stop-grace)" || return 1
641
+ # ① 宽限 0:TERM 之后**不等待**,直接 KILL
642
+ printf 'TEAM_BG_STOP_GRACE=0\n' >> "$d/.pi/team/config.sh"
643
+ mr_bg_ignore_term_job "$d" grace0 || { finding "promise($key):作业夹具起不来"; return 1; }
644
+ t0="$(date +%s)"; mr_run "$d" bg stop grace0; t1="$(date +%s)"; spent=$((t1 - t0))
645
+ if kill -0 "$MR_BG_PID" 2>/dev/null; then
646
+ kill -KILL "$MR_BG_PID" 2>/dev/null || true
647
+ finding "promise($key):宽限 0 时忽略 TERM 的作业没有被 KILL 收掉(rc=$MR_RC,$(first_line "$MR_OUT"))"
648
+ return 1
649
+ fi
650
+ if [ "$spent" -ge 3 ]; then
651
+ finding "promise($key):宽限 0 却等了 ${spent}s(旋钮没生效)"
652
+ return 1
653
+ fi
654
+ if ! grep -q 'stop id=grace0 signal=TERM,KILL result=stopped' "$d/.pi/team/state/bg.log" 2>/dev/null; then
655
+ finding "promise($key):账本没有记下 TERM→KILL 的升级($(first_line "$(cat "$d/.pi/team/state/bg.log" 2>/dev/null)"))"
656
+ return 1
657
+ fi
658
+ ok "promise($key):宽限 0 → 忽略 TERM 的作业被立刻 KILL(账本 signal=TERM,KILL,用时 ${spent}s)"
659
+ # ② 宽限 2:同样的作业要等够 2s 才升级 —— 旋钮真的改变等待
660
+ sed -i 's/^TEAM_BG_STOP_GRACE=0$/TEAM_BG_STOP_GRACE=2/' "$d/.pi/team/config.sh"
661
+ mr_bg_ignore_term_job "$d" grace2 || { finding "promise($key):第二个作业夹具起不来"; return 1; }
662
+ t0="$(date +%s)"; mr_run "$d" bg stop grace2; t1="$(date +%s)"; spent=$((t1 - t0))
663
+ if kill -0 "$MR_BG_PID" 2>/dev/null; then
664
+ kill -KILL "$MR_BG_PID" 2>/dev/null || true
665
+ finding "promise($key):宽限 2 时作业没有被 KILL 收掉(rc=$MR_RC,$(first_line "$MR_OUT"))"
666
+ return 1
667
+ fi
668
+ if [ "$spent" -lt 2 ]; then
669
+ finding "promise($key):宽限 2 却只等了 ${spent}s(旋钮没生效)"
670
+ return 1
671
+ fi
672
+ ok "promise($key):宽限 2 → 先 TERM、等 ${spent}s 后 KILL(旋钮改变等待)"
673
+ return 0
674
+ }
675
+
676
+ # -- the other promise probes -----------------------------------------------------------------------
677
+ mr_promise_identity() { # <KEY>(三个身份键共享一条承诺:手改有效;team init 是 skip;--force 重渲染并回模板值)
678
+ local key="$1" d base
679
+ d="$(new_fixture promise-identity)" || return 1
680
+ base="$(basename "$d")"
681
+ printf 'TEAM_PROJECT="handedited"\n' >> "$d/.pi/team/config.sh"
682
+ sed -i 's/^TEAM_GATES=.*/TEAM_GATES="bash my-gate.sh"/' "$d/.pi/team/config.sh"
683
+ mr_run "$d" paths
684
+ case "$MR_OUT" in
685
+ *'"project": "handedited"'*|*'"project":"handedited"'*)
686
+ ok "promise($key):手改 TEAM_PROJECT 后 team paths 报 handedited" ;;
687
+ *) finding "promise($key):手改 TEAM_PROJECT 后 team paths 没变($(first_line "$MR_OUT"))"; return 1 ;;
688
+ esac
689
+ local sha0 sha1 sha2
690
+ sha0="$(sha_file "$d/.pi/team/config.sh")"
691
+ mr_run "$d" init
692
+ sha1="$(sha_file "$d/.pi/team/config.sh")"
693
+ if [ "$sha0" = "$sha1" ]; then ok "promise($key):裸 team init 打印 skip,契约逐字节不变"
694
+ else finding "promise($key):裸 team init 改了契约(sha $sha0 → $sha1)—— 注释把它当路线就是在骗人"; fi
695
+ mr_run "$d" init --force --yes
696
+ sha2="$(sha_file "$d/.pi/team/config.sh")"
697
+ local now_proj now_gates
698
+ now_proj="$(grep -E '^TEAM_PROJECT=' "$d/.pi/team/config.sh" | head -1)"
699
+ now_gates="$(grep -E '^TEAM_GATES=' "$d/.pi/team/config.sh" | head -1)"
700
+ if [ "$sha2" != "$sha1" ] && [ "$now_proj" = "TEAM_PROJECT=\"$base\"" ] && [ "$now_gates" != 'TEAM_GATES="bash my-gate.sh"' ]; then
701
+ ok "promise($key):team init --force 重渲染(sha 变了;TEAM_PROJECT 回模板值,TEAM_GATES 回检测值)"
702
+ else
703
+ finding "promise($key):team init --force 没有兑现「重渲染并回模板值」(sha0=$sha1?proj=[$now_proj] gates=[$now_gates])"
704
+ return 1
705
+ fi
706
+ return 0
707
+ }
708
+
709
+ mr_promise_roster() { # <KEY>
710
+ local key="$1"
711
+ local d a
712
+ d="$(new_fixture promise-roster)" || return 1
713
+ a="$d/.pi/team/state/config.log"
714
+ mr_run "$d" add-agent routescheck --register --no-install
715
+ if [ "$MR_RC" != "0" ] || ! grep -q '^TEAM_AGENTS=.*routescheck' "$d/.pi/team/config.sh"; then
716
+ finding "promise($key):team add-agent routescheck --register 没有改 TEAM_AGENTS(rc=$MR_RC;$(first_line "$MR_OUT"))"
717
+ return 1
718
+ fi
719
+ if tail -1 "$a" 2>/dev/null | grep -q 'result=ok actor=cli key=TEAM_AGENTS'; then
720
+ ok "promise($key):注册改了名册行并落一行 result=ok actor=cli(TEAM_AGENTS)"
721
+ else
722
+ finding "promise($key):注册改了名册行但没有审计行($(tail -1 "$a" 2>/dev/null))"
723
+ return 1
724
+ fi
725
+ mr_run "$d" teardown --agent routescheck --register
726
+ if [ "$MR_RC" = "0" ] && ! grep -q 'routescheck' "$d/.pi/team/config.sh"; then
727
+ ok "promise($key):teardown --register 从名册移除该席位(同一条写入器)"
728
+ else
729
+ finding "promise($key):teardown --register 没有移除席位(rc=$MR_RC;$(first_line "$MR_OUT"))"
730
+ return 1
731
+ fi
732
+ return 0
733
+ }
734
+
735
+ mr_promise_seat_model() { # <KEY>
736
+ local key="$1" d
737
+ d="$(new_fixture promise-seat-model)" || return 1
738
+ mr_run "$d" config set-agent-model dev vendor/m9
739
+ if [ "$MR_RC" != "0" ] || ! grep -q "^TEAM_AGENT_MODELS='dev=vendor/m9'" "$d/.pi/team/config.sh"; then
740
+ finding "promise($key):set-agent-model 没写进 TEAM_AGENT_MODELS(rc=$MR_RC;$(first_line "$MR_OUT"))"
741
+ return 1
742
+ fi
743
+ if ! tail -1 "$d/.pi/team/state/config.log" | grep -q 'result=ok .*key=TEAM_AGENT_MODELS'; then
744
+ finding "promise($key):set-agent-model 没有审计行"
745
+ return 1
746
+ fi
747
+ mr_run "$d" config list --json
748
+ if printf '%s' "$MR_OUT" | grep -q '"agent":"dev","model":"vendor/m9".*"override":true'; then
749
+ ok "promise($key):席位行写入、审计,读侧的 dev 席位带着 vendor/m9 与 override:true"
750
+ else
751
+ finding "promise($key):读侧的 dev 席位没有带 vendor/m9/override:true"
752
+ return 1
753
+ fi
754
+ return 0
755
+ }
756
+
757
+ mr_promise_pm_model() { # <KEY>
758
+ local key="$1" d
759
+ d="$(new_fixture promise-pm-model)" || return 1
760
+ mr_run "$d" config set-agent-model pm vendor/pm9
761
+ [ "$MR_RC" = "0" ] || { finding "promise($key):set-agent-model pm 失败($(first_line "$MR_OUT"))"; return 1; }
762
+ mr_run "$d" up --print
763
+ if printf '%s' "$MR_OUT" | grep -q -- '--provider vendor --model pm9'; then
764
+ ok "promise($key):team up --print 渲染出的命令带着 --provider vendor --model pm9"
765
+ else
766
+ finding "promise($key):team up --print 的命令没带 --provider vendor --model pm9"
767
+ return 1
768
+ fi
769
+ return 0
770
+ }
771
+
772
+ mr_promise_pulse_window() { # <KEY>
773
+ local key="$1" d
774
+ d="$(new_fixture promise-pulse-window)" || return 1
775
+ mr_run "$d" config set TEAM_PULSE_WINDOW pulse9 --yes
776
+ [ "$MR_RC" = "0" ] || { finding "promise($key):设置 TEAM_PULSE_WINDOW 失败($(first_line "$MR_OUT"))"; return 1; }
777
+ : > "$TMUX_LOG"
778
+ mr_run "$d" pulse up
779
+ if grep -qE 'new-window .*-n pulse9( |$)' "$TMUX_LOG"; then
780
+ ok "promise($key):pulse up 记下的 new-window 用新窗口名 pulse9(rc=$MR_RC,不看退出码)"
781
+ else
782
+ finding "promise($key):pulse up 没有按新窗口名建窗(记录:$(grep -c . "$TMUX_LOG" 2>/dev/null || echo 0) 条 tmux 调用)"
783
+ return 1
784
+ fi
785
+ return 0
786
+ }
787
+
788
+ mr_promise_default_model() { # <KEY>
789
+ local key="$1" d br
790
+ d="$(new_fixture promise-default-model)" || return 1
791
+ mkdir -p "$d/docs/team/tasks" "$d/openspec/specs/routes"
792
+ cat > "$d/docs/team/tasks/T1-anchor.md" <<'BRIEF'
793
+ # T1 · fixture
794
+
795
+ ```
796
+ task: T1
797
+ agent: dev
798
+ change: -
799
+ specs: routes#The fixture requirement
800
+ phase: apply
801
+ deps: -
802
+ status: todo
803
+ ```
804
+
805
+ body
806
+ BRIEF
807
+ printf '### Requirement: The fixture requirement\n\nfixture\n' > "$d/openspec/specs/routes/spec.md"
808
+ br="$( cd "$d" && env -u TMUX -u TMUX_PANE PATH="$SHIM:$PATH" bash -c '
809
+ . "$0/skills/teamsmith/scripts/lib/common.sh"
810
+ for f in "$0"/skills/teamsmith/scripts/lib/cmd-*.sh; do . "$f" 2>/dev/null || true; done
811
+ team_load_config >/dev/null 2>&1
812
+ TEAM_DOCS_ABS="${TEAM_DOCS_ABS:-$TEAM_MAIN_ROOT/$TEAM_DOCS_DIR}"
813
+ team_branch_for_agent "$1" "$2"' "$tree" dev T1 )"
814
+ [ -n "$br" ] || { finding "promise($key):夹具算不出任务分支"; return 1; }
815
+ if ! git -C "$d" worktree add -b "$br" "$d/.worktrees/dev" main >/dev/null 2>&1; then
816
+ finding "promise($key):夹具建不出 worktree"; return 1
817
+ fi
818
+ rm -f "$d/.pi/team/state/dev.env"
819
+ mr_run "$d" config set TEAM_DEFAULT_MODEL vendor/dm9 --yes
820
+ [ "$MR_RC" = "0" ] || { finding "promise($key):设置 TEAM_DEFAULT_MODEL 失败"; return 1; }
821
+ mr_run "$d" dispatch dev T1 docs/team/tasks/T1-anchor.md --print
822
+ if printf '%s' "$MR_OUT" | grep -q -- '--provider vendor --model dm9'; then
823
+ ok "promise($key):dispatch --print 渲染出的启动命令带着 --provider vendor --model dm9"
824
+ else
825
+ finding "promise($key):dispatch --print 没带 --provider vendor --model dm9(rc=$MR_RC;$(first_line "$MR_OUT"))"
826
+ return 1
827
+ fi
828
+ return 0
829
+ }
830
+
831
+ # ── Walk C(P140 · dispatch-friction)· 拒绝路线:每条打印出来的 修法/改行 都真的能粘 ──────────────
832
+ #
833
+ # 两个判定:
834
+ # C1 目录对账:cmd-agents.sh 里每一条 `修法:`/`改行:` 发射必须被下面的 family 表认领(认领片段);
835
+ # 没人认领 = finding —— 新守卫的路线不能绕过走查("印了一条没人走过"就是缺陷)。
836
+ # C2 逐族走查:每个 family 在一个全新夹具里跑一次拒绝命令 → 期望的阻塞项 + 路线;把**打印出来的**
837
+ # 路线原样粘回去(`改行:` 写进任务书头,`修法:` 当命令跑);重跑要求阻塞项消失(clear)
838
+ # 或按覆盖契约兑现(force:警告 + 真拉起时恰好多一行审计)。
839
+ #
840
+ # 夹具纪律:一律 new_fixture(私有 git 仓库 + 私有身份)、PATH 最前是录制 shim(假窗口但真启动证据)、
841
+ # stdin /dev/null、每条命令有硬超时;没有一条命令能碰到调用者的项目或真 tmux。
842
+ RC_CLAIMS=(
843
+ 'dirty-stash|修法:git -C %q stash push -u -m %q'
844
+ 'branch-create|修法:git -C %q switch -c %q'
845
+ 'branch-switch|修法:git -C %q switch %q'
846
+ 'worktree-add|修法:git -C %q worktree add -b %q'
847
+ 'briefs|修法:git -C %q mv %q %q'
848
+ 'stack-resume|修法:$TEAM_CLI resume --agent $agent'
849
+ 'force|修法:$TEAM_CLI dispatch $agent $id $brief --force'
850
+ 'branch-decl|修法:$TEAM_CLI dispatch $agent $id $brief --branch $derived'
851
+ 'change-line|改行:change: '
852
+ 'deltas-line|改行:deltas: '
853
+ 'branch-line|改行:branch: '
854
+ 'config-agent-bin|修法:$TEAM_CLI config set TEAM_AGENT_BIN'
855
+ 'config-pi-bin|修法:$TEAM_CLI config set TEAM_PI_BIN'
856
+ 'session-fresh|修法:$TEAM_CLI dispatch $agent $id $taskfile --fresh'
857
+ 'session-overflow|修法:$TEAM_CLI dispatch $agent $id $taskfile --allow-overflow'
858
+ 'capacity-override|修法:$(team_dispatch_mem_fix_cmd'
859
+ )
860
+ RC_EXTRA=()
861
+ RC_PI_BIN=""
862
+ RC_OMIT_PI_BIN=0
863
+ RC_FAKE_LAUNCH=0
864
+
865
+ # rc_team <dir> <team 参数…> → 组合输出;rc = 被测 CLI 的 rc
866
+ rc_team() {
867
+ local d="$1"; shift
868
+ local -a e=(env -u TMUX -u TMUX_PANE "PATH=$SHIM:$PATH" "TEAM_ROUTES_WINDOWS=$WINREG"
869
+ "TEAM_ROUTES_TMUX_LOG=$TMUX_LOG" "TEAM_MEETINGS_DIR=$d/.meetings" "TEAM_PI_AGENT_DIR=$d/.pi-agent")
870
+ [ "$RC_OMIT_PI_BIN" = "1" ] || e+=("TEAM_PI_BIN=${RC_PI_BIN:-/bin/true}")
871
+ [ "$RC_FAKE_LAUNCH" = "1" ] && e+=("TEAM_ROUTES_FAKE_LAUNCH=1" "TEAM_DISPATCH_VERIFY_SEC=2" "TEAM_DISPATCH_ALIVE_SEC=0")
872
+ [ "${#RC_EXTRA[@]}" -gt 0 ] && e+=("${RC_EXTRA[@]}")
873
+ ( cd "$d" && ${TIMEOUT_BIN:+"$TIMEOUT_BIN" "$timeout_sec"} "${e[@]}" bash "$team" "$@" </dev/null 2>&1 )
874
+ }
875
+
876
+ # rc_sh <dir> <命令字符串> → 把打印出来的路线原样当命令跑(fake launch 开着)
877
+ rc_sh() {
878
+ local d="$1" cmd="$2"
879
+ local -a e=(env -u TMUX -u TMUX_PANE "PATH=$SHIM:$PATH" "TEAM_ROUTES_WINDOWS=$WINREG"
880
+ "TEAM_ROUTES_TMUX_LOG=$TMUX_LOG" "TEAM_MEETINGS_DIR=$d/.meetings" "TEAM_PI_AGENT_DIR=$d/.pi-agent"
881
+ "TEAM_ROUTES_FAKE_LAUNCH=1" "TEAM_DISPATCH_VERIFY_SEC=2" "TEAM_DISPATCH_ALIVE_SEC=0")
882
+ [ "$RC_OMIT_PI_BIN" = "1" ] || e+=("TEAM_PI_BIN=${RC_PI_BIN:-/bin/true}")
883
+ [ "${#RC_EXTRA[@]}" -gt 0 ] && e+=("${RC_EXTRA[@]}")
884
+ ( cd "$d" && ${TIMEOUT_BIN:+"$TIMEOUT_BIN" "$timeout_sec"} "${e[@]}" bash -c "$cmd" </dev/null 2>&1 )
885
+ }
886
+
887
+ # rc_canon <dir> <ID> [agent] → 被测 CLI 给这个任务推导出的分支名
888
+ rc_canon() {
889
+ local d="$1" id="$2" agent="${3:-dev}"
890
+ ( cd "$d" && env -u TEAM_ROOT -u TEAM_MAIN_ROOT -u TEAM_SKILL_DIR -u TEAM_PROJECT -u TEAM_SESSION \
891
+ bash -c '
892
+ . "'"$skill"'/scripts/lib/common.sh"
893
+ for _f in "'"$skill"'"/scripts/lib/cmd-*.sh; do . "$_f" 2>/dev/null || true; done
894
+ team_load_config >/dev/null 2>&1
895
+ team_branch_for_agent "$1" "$2"' _ "$agent" "$id" )
896
+ }
897
+
898
+ # rc_brief <dir> <ID> <change> <anchor> <deltas> [phase] [agent] [branch-line] → 打印任务书路径
899
+ rc_brief() {
900
+ local d="$1" id="$2" change="$3" anchor="$4" deltas="$5" phase="${6:-apply}" agent="${7:-dev}" br="${8:-}" f
901
+ f="$d/docs/team/tasks/$id-fixture.md"
902
+ mkdir -p "$d/docs/team/tasks"
903
+ {
904
+ printf '# %s · fixture\n\n```\n' "$id"
905
+ printf 'task: %s\nagent: %s\nissue: -\n' "$id" "$agent"
906
+ [ -n "$br" ] && printf 'branch: %s\n' "$br"
907
+ printf 'change: %s\nspecs: -\nanchor: %s\nphase: %s\n' "$change" "$anchor" "$phase"
908
+ printf 'deltas: %s\ndeps: -\nstatus: todo\nbudget: -\n```\n\nbody\n' "$deltas"
909
+ } > "$f"
910
+ printf '%s\n' "$f"
911
+ }
912
+
913
+ rc_board() { # <dir> <ID> <title> [status]
914
+ local d="$1" id="$2" title="$3" st="${4:-todo}"
915
+ rc_team "$d" board add "$id" "$title" dev - - >/dev/null 2>&1 || true
916
+ [ "$st" = "todo" ] || rc_team "$d" board set "$id" "$st" >/dev/null 2>&1 || true
917
+ }
918
+
919
+ rc_wt() { # <dir> <agent> <branch>
920
+ local d="$1" a="$2" br="$3"
921
+ git -C "$d" worktree add -q -b "$br" "$d/.worktrees/$a" main >/dev/null 2>&1 \
922
+ || git -C "$d" worktree add -q "$d/.worktrees/$a" "$br" >/dev/null 2>&1 || true
923
+ }
924
+
925
+ rc_route() { # <输出> <片段> → 提取打印出来的路线(**原样**:去标记与首尾空白,不剥尾注)
926
+ local out="$1" frag="$2" line txt
927
+ line="$(printf '%s\n' "$out" | grep -aF -- "$frag" | head -1)"
928
+ [ -n "$line" ] || return 1
929
+ case "$line" in
930
+ *修法:*) txt="${line#*修法:}" ;;
931
+ *改行:*) txt="${line#*改行:}" ;;
932
+ *) return 1 ;;
933
+ esac
934
+ # P151(F1/F2):不再在「(」处截断 —— 打印出来的**整行**就是整条命令(命令与说明分行)。
935
+ # 这一行就是原样粘贴回归的判据:任何还留在路线行上的说明文字都会让下面的粘贴失败。
936
+ printf '%s\n' "$(printf '%s' "$txt" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
937
+ }
938
+
939
+ rc_apply_line() { # <任务书> <字段> <确切行>:把该字段的现有行换成这条
940
+ local f="$1" field="$2" line="$3" tmp="$1.p140tmp"
941
+ awk -v field="$field" -v line="$line" '
942
+ index($0, field ":") == 1 { if (!done) { print line; done=1 } ; next }
943
+ { print }
944
+ ' "$f" > "$tmp" && mv "$tmp" "$f"
945
+ }
946
+
947
+ rc_audit_lines() { local f="$1/.pi/team/state/watchdog.log"; if [ -f "$f" ]; then wc -l < "$f" | tr -d ' '; else printf '0'; fi; }
948
+
949
+ # rc_case <family> <期望阻塞项正则> <路线片段> <apply:line:<字段>|apply:cmd> <rerun:same|route> <effect:clear|proceed|force|resume> [<重跑额外断言片段>]
950
+ rc_case() {
951
+ local fam="$1" expect="$2" frag="$3" apply="$4" rerun="$5" effect="$6" extra="${7:-}"
952
+ local d="$RC_DIR" b="$RC_BRIEF" out rc route audit0 audit1 out2="" rc2=0
953
+ apply="${apply#apply:}" # 调用点写 apply:cmd / apply:line:<字段>,这里归一到 cmd / line:<字段>
954
+ out="$(rc_team "$d" "${RC_CMD[@]}")"; rc=$?
955
+ if [ "$rc" -eq 0 ]; then finding "C2/$fam:拒绝命令没有拒绝(rc=0)"; return 1; fi
956
+ if ! printf '%s' "$out" | grep -aqE -- "$expect"; then
957
+ finding "C2/$fam:拒绝输出里没有期望的阻塞项($expect):$(first_line "$out")"; return 1
958
+ fi
959
+ route="$(rc_route "$out" "$frag")" || { finding "C2/$fam:拒绝输出里没有路线(片段 $frag)"; return 1; }
960
+ audit0="$(rc_audit_lines "$d")"
961
+ case "$apply" in
962
+ line:*)
963
+ rc_apply_line "$b" "${apply#line:}" "$route"
964
+ out2="$(rc_team "$d" "${RC_CMD[@]}")"; rc2=$?
965
+ ;;
966
+ cmd)
967
+ if [ "$rerun" = "route" ]; then
968
+ out2="$(rc_sh "$d" "$route")"; rc2=$?
969
+ else
970
+ rc_sh "$d" "$route" >/dev/null 2>&1 || true
971
+ out2="$(rc_team "$d" "${RC_CMD[@]}")"; rc2=$?
972
+ fi
973
+ ;;
974
+ esac
975
+ case "$effect" in
976
+ clear)
977
+ if printf '%s' "$out2" | grep -aqE -- "$expect"; then
978
+ finding "C2/$fam:粘了路线后阻塞项还在($expect)"
979
+ elif [ "$rc2" != "0" ]; then
980
+ finding "C2/$fam:路线清了阻塞项但重跑仍失败(rc=$rc2):$(first_line "$out2")"
981
+ else
982
+ ok "C2/$fam:$([ "$apply" = "cmd" ] && echo '修法' || echo '改行')路线让阻塞项消失($(printf '%s' "$route" | cut -c1-72))"
983
+ fi ;;
984
+ proceed)
985
+ if [ "$rc2" != "0" ]; then
986
+ finding "C2/$fam:路线没有兑现(rc=$rc2):$(first_line "$out2")"
987
+ elif [ -n "$extra" ] && ! printf '%s' "$out2" | grep -aqF -- "$extra"; then
988
+ finding "C2/$fam:重跑缺少期望的说明($extra)"
989
+ else
990
+ ok "C2/$fam:打印出来的路线真的能跑(rc=0${extra:+;$extra})"
991
+ fi ;;
992
+ force)
993
+ audit1="$(rc_audit_lines "$d")"
994
+ if [ "$rc2" != "0" ]; then
995
+ finding "C2/$fam:--force 路线没有兑现(rc=$rc2):$(first_line "$out2")"
996
+ elif ! printf '%s' "$out2" | grep -aqF '显式覆盖'; then
997
+ finding "C2/$fam:重跑没有打印覆盖警告(不许静默接管)"
998
+ elif [ "$((audit1 - audit0))" != "1" ]; then
999
+ finding "C2/$fam:覆盖后应恰好多一行审计($audit0 → $audit1)"
1000
+ else
1001
+ ok "C2/$fam:覆盖路线兑现(警告 + 恰好多一行审计 $audit0 → $audit1)"
1002
+ fi ;;
1003
+ resume)
1004
+ if [ "$rc2" != "0" ] || ! printf '%s' "$out2" | grep -aqF -- "$extra"; then
1005
+ finding "C2/$fam:继续旧任务的路线没有兑现(rc=$rc2):$(first_line "$out2")"
1006
+ else
1007
+ ok "C2/$fam:路线让旧任务继续($extra)"
1008
+ fi ;;
1009
+ esac
1010
+ }
1011
+
1012
+ mr_refusals_catalog() {
1013
+ local f="$skill/scripts/lib/cmd-agents.sh" line hit claim n=0 claimed=0
1014
+ [ -f "$f" ] || { finding "C1:找不到派单文案目录:$f"; return 0; }
1015
+ while IFS= read -r line; do
1016
+ case "$line" in
1017
+ *修法:*|*改行:*) ;;
1018
+ *) continue ;;
1019
+ esac
1020
+ [[ "$line" =~ ^[[:space:]]*# ]] && continue # 注释(含缩进注释)里提到标记不算发射
1021
+ n=$((n + 1))
1022
+ claim=""
1023
+ for hit in "${RC_CLAIMS[@]}"; do
1024
+ case "$line" in *"${hit#*|}"*) claim="${hit%%|*}"; break ;; esac
1025
+ done
1026
+ if [ -z "$claim" ]; then
1027
+ finding "C1:没人认领的路线发射(family 表里没有条目):$(printf '%s' "$line" | sed 's/^[[:space:]]*//' | cut -c1-110)"
1028
+ else
1029
+ claimed=$((claimed + 1))
1030
+ fi
1031
+ done < "$f"
1032
+ if [ "$n" -eq 0 ]; then
1033
+ finding "C1:没有扫到任何路线发射($f 里一条都没有?)"
1034
+ elif [ "$n" -eq "$claimed" ]; then
1035
+ ok "C1:$n 条路线发射全部被 family 表认领($(printf '%s\n' "${RC_CLAIMS[@]}" | cut -d'|' -f1 | sort -u | wc -l | tr -d ' ') 个 family)"
1036
+ fi
1037
+ }
1038
+
1039
+ mr_refusals() {
1040
+ section "refusals · 拒绝路线:每条打印的 修法/改行 都被粘回去并且真的生效(P140)"
1041
+ mr_refusals_catalog
1042
+ local d b br wt
1043
+
1044
+ # ① 脏工作树(换任务)→ 修法:git -C … stash push
1045
+ d="$(new_fixture refuse-dirty)"; RC_DIR="$d"
1046
+ rc_board "$d" OLD "OLD fixture" dropped
1047
+ rc_board "$d" P1 "P1 fixture"
1048
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1049
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1050
+ printf 'wip from OLD\n' > "$d/.worktrees/dev/wip.txt"
1051
+ mkdir -p "$d/.pi/team/state"
1052
+ printf 'task=OLD\nworktree=%s\ntaskfile=%s\nbranch=%s\n' "$d/.worktrees/dev" "$d/docs/team/tasks/OLD-fixture.md" "$br" > "$d/.pi/team/state/dev.env"
1053
+ rc_brief "$d" OLD - "none (infra) — fixture" - >/dev/null
1054
+ RC_CMD=(dispatch dev P1 "$b" --print)
1055
+ rc_case dirty-stash '有 1 个未提交改动.*属于上一个任务 OLD' '修法:git -C' 'apply:cmd' same clear
1056
+ # ② 工作树停在别的任务的分支上 → 修法:git -C … switch(清掉)
1057
+ d="$(new_fixture refuse-branch-switch)"; RC_DIR="$d"
1058
+ rc_board "$d" P1 "P1 fixture"
1059
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1060
+ rc_wt "$d" dev task/P9-other
1061
+ RC_CMD=(dispatch dev P1 "$b" --print)
1062
+ rc_case branch-switch '停在不属于本任务(P1)的分支上' ' switch ' 'apply:cmd' same clear
1063
+
1064
+ # ③ 工作树 detached → 修法:git -C … switch -c(建好并切过去)
1065
+ d="$(new_fixture refuse-branch-create)"; RC_DIR="$d"
1066
+ rc_board "$d" P1 "P1 fixture"
1067
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1068
+ git -C "$d" worktree add -q --detach "$d/.worktrees/dev" main >/dev/null 2>&1 || true
1069
+ RC_CMD=(dispatch dev P1 "$b" --print)
1070
+ rc_case branch-create 'detached HEAD' ' switch -c ' 'apply:cmd' same clear
1071
+
1072
+ # ④ 工作树不存在 → 修法:git -C … worktree add -b(PM 该跑的那条命令)
1073
+ d="$(new_fixture refuse-worktree-add)"; RC_DIR="$d"
1074
+ rc_board "$d" P1 "P1 fixture"
1075
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1076
+ RC_CMD=(dispatch dev P1 "$b" --print)
1077
+ rc_case worktree-add 'worktree 不存在' ' worktree add -b ' 'apply:cmd' same clear
1078
+
1079
+ # ⑤ 同一个 ID 两份任务书 → 修法:git mv 把过期那份改名出 glob
1080
+ d="$(new_fixture refuse-briefs)"; RC_DIR="$d"
1081
+ rc_board "$d" P1 "P1 fixture"
1082
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1083
+ rc_brief "$d" P1-stale - "none (infra) — fixture" - >/dev/null
1084
+ # 路线是 git mv:真实项目里任务书受版本控制(未跟踪的文件 git mv 会拒)——夹具照现实提交
1085
+ git -C "$d" add -A >/dev/null 2>&1 && git -C "$d" -c user.email=routes@teamsmith -c user.name=routes commit -qm briefs >/dev/null 2>&1 || true
1086
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1087
+ RC_CMD=(dispatch dev P1 "$b" --print)
1088
+ rc_case briefs '有多份任务书' ' mv ' 'apply:cmd' same clear
1089
+
1090
+ # ⑥ change: 行尾随文字 → 改行:change: <第一个合法 id>
1091
+ d="$(new_fixture refuse-change-line)"; RC_DIR="$d"
1092
+ rc_board "$d" P1 "P1 fixture"
1093
+ b="$(rc_brief "$d" P1 'panel(说明)' - -)"; RC_BRIEF="$b"
1094
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1095
+ RC_CMD=(dispatch dev P1 "$b" --print)
1096
+ rc_case change-line 'change: 行不合法' '改行:change: ' 'apply:line:change' same clear
1097
+
1098
+ # ⑦ change: 两行 → 改行:change: alpha(把该字段的行全部换成这一条)
1099
+ # R2 的"第一行就带示例与原因"在同一夹具里再纬一次(两行的原因点名)
1100
+ d="$(new_fixture refuse-change-line-two)"; RC_DIR="$d"
1101
+ rc_board "$d" P1 "P1 fixture"
1102
+ b="$(rc_brief "$d" P1 alpha - -)"; RC_BRIEF="$b"
1103
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1104
+ printf 'change: beta\n' >> "$b"
1105
+ out="$(rc_team "$d" dispatch dev P1 "$b" --print)" || true
1106
+ case "$(printf '%s\n' "$out" | head -1)" in
1107
+ *合法示例*change:*) ok "C2/change-line:第二行 change: 时首行带具体原因与合法示例" ;;
1108
+ *) finding "C2/change-line:两行 change: 的首行没有示例:$(printf '%s\n' "$out" | head -1 | cut -c1-160)" ;;
1109
+ esac
1110
+ RC_CMD=(dispatch dev P1 "$b" --print)
1111
+ rc_case change-line 'change: 有 2 行' '改行:change: ' 'apply:line:change' same clear
1112
+
1113
+ # ⑧ deltas: 三种畸形 → 改行:deltas: <建议>;逗号列表本身被接受(控制组)
1114
+ d="$(new_fixture refuse-deltas-line)"; RC_DIR="$d"
1115
+ rc_board "$d" P1 "P1 fixture"
1116
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1117
+ b="$(rc_brief "$d" P1 alpha - 'panel · verification')"; RC_BRIEF="$b"
1118
+ RC_CMD=(dispatch dev P1 "$b" --print)
1119
+ rc_case deltas-line 'deltas: 行不合法' '改行:deltas: ' 'apply:line:deltas' same clear
1120
+ for pair in 'panel,|尾部空项' 'panel verification|空白不是分隔符'; do
1121
+ val="${pair%%|*}"; why="${pair#*|}"
1122
+ b="$(rc_brief "$d" P1 alpha - "$val")"; RC_BRIEF="$b"
1123
+ out="$(rc_team "$d" dispatch dev P1 "$b" --print)" || true
1124
+ case "$(printf '%s\n' "$out" | head -1)" in
1125
+ *合法示例*deltas:*) ok "C2/deltas-line[$val]:首行带具体原因($why)与合法示例" ;;
1126
+ *) finding "C2/deltas-line[$val]:首行没有示例:$(printf '%s\n' "$out" | head -1 | cut -c1-160)" ;;
1127
+ esac
1128
+ RC_CMD=(dispatch dev P1 "$b" --print)
1129
+ rc_case "deltas-line[$val]" 'deltas: 行不合法' '改行:deltas: ' 'apply:line:deltas' same clear
1130
+ done
1131
+ b="$(rc_brief "$d" P1 alpha - 'panel, verification')"; RC_BRIEF="$b"
1132
+ RC_CMD=(dispatch dev P1 "$b" --print)
1133
+ if rc_team "$d" "${RC_CMD[@]}" >/dev/null 2>&1; then ok "C2/deltas-line:示例形状 \`panel, verification\` 真的被接受(控制组)"
1134
+ else finding "C2/deltas-line:示例形状却没被接受"; fi
1135
+
1136
+ # ⑨ 任务书 branch: 行指到别的任务 → 改行:branch: <本任务分支>
1137
+ d="$(new_fixture refuse-branch-line)"; RC_DIR="$d"
1138
+ rc_board "$d" P1 "P1 fixture"
1139
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" - apply dev task/P9-other)"; RC_BRIEF="$b"
1140
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1141
+ RC_CMD=(dispatch dev P1 "$b" --print)
1142
+ rc_case branch-line '分支声明不属于本任务' '改行:branch: ' 'apply:line:branch' same clear
1143
+
1144
+ # ⑩ --branch 指到别的任务 → 修法:用本任务的 --branch 重派(路线本身就是重跑命令)
1145
+ d="$(new_fixture refuse-branch-decl)"; RC_DIR="$d"
1146
+ rc_board "$d" P1 "P1 fixture"
1147
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1148
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1149
+ RC_CMD=(dispatch dev P1 "$b" --branch task/P9-other --print)
1150
+ rc_case branch-decl '分支声明不属于本任务' '修法:team dispatch' 'apply:cmd' route proceed
1151
+ # ⑪ 叠任务 → 修法一 resume(让旧任务继续)/ 修法二 --force(覆盖 + 一行审计)
1152
+ d="$(new_fixture refuse-stack)"; RC_DIR="$d"
1153
+ rc_board "$d" OLD "OLD fixture" wip
1154
+ rc_board "$d" P1 "P1 fixture"
1155
+ rc_brief "$d" OLD - "none (infra) — fixture" - >/dev/null
1156
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1157
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1158
+ mkdir -p "$d/.pi/team/state"
1159
+ printf 'task=OLD\nworktree=%s\ntaskfile=%s\nbranch=%s\n' "$d/.worktrees/dev" "$d/docs/team/tasks/OLD-fixture.md" "$br" > "$d/.pi/team/state/dev.env"
1160
+ RC_CMD=(dispatch dev P1 "$b" --print)
1161
+ rc_case stack-force '还有一个没结束的任务' '修法:team dispatch' 'apply:cmd' route force
1162
+
1163
+ # ⑪b 叠任务 · resume 路线:先收尾旧任务(honored:旧任务的窗口真的被拉起,不是 cleared)
1164
+ # 夹具与 ⑪ 不同:工作树停在 OLD 自己的分支上(resume OLD 要被放行)
1165
+ d="$(new_fixture refuse-stack-resume)"; RC_DIR="$d"
1166
+ rc_board "$d" OLD "OLD fixture" wip
1167
+ rc_board "$d" P1 "P1 fixture"
1168
+ rc_brief "$d" OLD - "none (infra) — fixture" - >/dev/null
1169
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1170
+ br_old="$(rc_canon "$d" OLD)"; rc_wt "$d" dev "$br_old"
1171
+ mkdir -p "$d/.pi/team/state"
1172
+ printf 'task=OLD\nworktree=%s\ntaskfile=%s\nbranch=%s\n' "$d/.worktrees/dev" "$d/docs/team/tasks/OLD-fixture.md" "$br_old" > "$d/.pi/team/state/dev.env"
1173
+ RC_CMD=(dispatch dev P1 "$b" --print)
1174
+ out="$(rc_team "$d" "${RC_CMD[@]}")" || true
1175
+ route="$(rc_route "$out" '修法:team resume') || true"
1176
+ if [ -z "$route" ]; then
1177
+ finding "C2/stack-resume:叠任务拒绝里没有 resume 路线"
1178
+ else
1179
+ out2="$(rc_sh "$d" "$route")"; rc2=$?
1180
+ if [ "$rc2" != "0" ] || ! printf '%s' "$out2" | grep -aqF 'dispatched OLD'; then
1181
+ finding "C2/stack-resume:resume 路线没有把旧任务交回座位(rc=$rc2):$(first_line "$out2")"
1182
+ else
1183
+ ok "C2/stack-resume:resume 路线真的让旧任务继续(dispatched OLD,rc=0)"
1184
+ fi
1185
+ fi
1186
+
1187
+ # ⑫ 没有 change 也没有锚 → 修法:--force(覆盖 + 一行审计)
1188
+ d="$(new_fixture refuse-anchor)"; RC_DIR="$d"
1189
+ rc_board "$d" P1 "P1 fixture"
1190
+ b="$(rc_brief "$d" P1 - - -)"; RC_BRIEF="$b"
1191
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1192
+ RC_CMD=(dispatch dev P1 "$b" --print)
1193
+ rc_case anchor-force '必须声明它的锚' '修法:team dispatch' 'apply:cmd' route force
1194
+
1195
+ # ⑬ delta 单写者冲突 → 修法:--force
1196
+ d="$(new_fixture refuse-delta-writer)"; RC_DIR="$d"
1197
+ rc_board "$d" SIB "SIB fixture" wip
1198
+ rc_board "$d" P1 "P1 fixture"
1199
+ rc_brief "$d" SIB alpha - panel >/dev/null
1200
+ b="$(rc_brief "$d" P1 alpha - panel)"; RC_BRIEF="$b"
1201
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1202
+ RC_CMD=(dispatch dev P1 "$b" --print)
1203
+ rc_case delta-writer-force '单写者规则' '修法:team dispatch' 'apply:cmd' route force
1204
+
1205
+ # ⑭ verify 席位 = apply 作者 → 修法:--force
1206
+ d="$(new_fixture refuse-verify-seat)"; RC_DIR="$d"
1207
+ rc_board "$d" AP "AP fixture" wip
1208
+ rc_board "$d" V1 "V1 fixture"
1209
+ rc_brief "$d" AP alpha - panel apply dev >/dev/null
1210
+ b="$(rc_brief "$d" V1 alpha - - verify dev)"; RC_BRIEF="$b"
1211
+ br="$(rc_canon "$d" V1)"; rc_wt "$d" dev "$br"
1212
+ RC_CMD=(dispatch dev V1 "$b" --print)
1213
+ rc_case verify-seat-force 'verification 不独立' '修法:team dispatch' 'apply:cmd' route force
1214
+
1215
+ # ⑮ TEAM_PI_BIN 解析不到 → 修法:team config set TEAM_PI_BIN "$(command -v pi)"
1216
+ d="$(new_fixture refuse-config-pi)"; RC_DIR="$d"
1217
+ rc_board "$d" P1 "P1 fixture"
1218
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1219
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1220
+ sed -i 's|^TEAM_PI_BIN=.*|TEAM_PI_BIN="/nonexistent/pi-fixture"|' "$d/.pi/team/config.sh"
1221
+ printf '#!/usr/bin/env bash\nexit 0\n' > "$SHIM/pi"; chmod +x "$SHIM/pi"
1222
+ RC_OMIT_PI_BIN=1
1223
+ RC_CMD=(dispatch dev P1 "$b" --print)
1224
+ rc_case config-pi-bin '找不到 pi 可执行文件' '修法:team config set TEAM_PI_BIN' 'apply:cmd' same clear
1225
+ RC_OMIT_PI_BIN=0
1226
+
1227
+ # ⑯ 自定义 adapter 的可执行文件解析不到 → 修法:team config set TEAM_AGENT_BIN
1228
+ d="$(new_fixture refuse-config-agent)"; RC_DIR="$d"
1229
+ rc_board "$d" P1 "P1 fixture"
1230
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1231
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1232
+ sed -i 's|^TEAM_AGENT_CMD=.*|TEAM_AGENT_CMD="fakeagent --run {prompt_file}"|' "$d/.pi/team/config.sh"
1233
+ sed -i 's|^TEAM_AGENT_BIN=.*|TEAM_AGENT_BIN="/nonexistent/fakeagent"|' "$d/.pi/team/config.sh"
1234
+ printf '#!/usr/bin/env bash\nexit 0\n' > "$SHIM/fakeagent"; chmod +x "$SHIM/fakeagent"
1235
+ RC_CMD=(dispatch dev P1 "$b" --print)
1236
+ rc_case config-agent-bin '找不到 agent 可执行文件' '修法:team config set TEAM_AGENT_BIN' 'apply:cmd' same clear
1237
+
1238
+ # ⑰ 会话超过窗口 → 修法:--fresh / --allow-overflow
1239
+ d="$(new_fixture refuse-session)"; RC_DIR="$d"
1240
+ rc_board "$d" P1 "P1 fixture"
1241
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1242
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1243
+ printf 'TEAM_MODEL_WINDOWS="deepseek/deepseek-flash=8"\n' >> "$d/.pi/team/config.sh"
1244
+ enc="$(printf '%s' "$d/.worktrees/dev" | sed -e 's|^/||' -e 's|[/\\:]|-|g')"
1245
+ mkdir -p "$d/.pi-agent/sessions/--$enc--"
1246
+ printf '%s\n' "$(head -c 400 /dev/zero | tr '\0' 'x')" > "$d/.pi-agent/sessions/--$enc--/x_routes-refuse-session-dev.jsonl"
1247
+ RC_CMD=(dispatch dev P1 "$b" --print)
1248
+ rc_case session-fresh '拒绝复用这个会话' '修法:team dispatch' 'apply:cmd' route proceed
1249
+ out="$(rc_team "$d" "${RC_CMD[@]}")" || true
1250
+ route="$(printf '%s\n' "$out" | grep -aF '修法:team dispatch' | grep -aF -- '--allow-overflow' | head -1 | sed 's/.*修法://; s/(.*//' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' || true)"
1251
+ if [ -z "$route" ]; then
1252
+ finding "C2/session-overflow:拒绝输出里没有 --allow-overflow 路线"
1253
+ else
1254
+ out2="$(rc_sh "$d" "$route")"; rc2=$?
1255
+ if [ "$rc2" != "0" ] || ! printf '%s' "$out2" | grep -aqF '显式放行'; then
1256
+ finding "C2/session-overflow:路线没有兑现(rc=$rc2)"
1257
+ else
1258
+ ok "C2/session-overflow:路线按自己的契约兑现(显式放行警告,rc=0)"
1259
+ fi
1260
+ fi
1261
+
1262
+ # ⑱ 容量底线拒绝 → 修法:显式降低底线重派(覆盖路线:真的放行);「等一个席位」是建议行,不是路线
1263
+ d="$(new_fixture refuse-capacity)"; RC_DIR="$d"
1264
+ rc_board "$d" P1 "P1 fixture"
1265
+ b="$(rc_brief "$d" P1 - "none (infra) — fixture" -)"; RC_BRIEF="$b"
1266
+ br="$(rc_canon "$d" P1)"; rc_wt "$d" dev "$br"
1267
+ printf 'MemTotal: 32768000 kB\nMemFree: 1024000 kB\nMemAvailable: 102400 kB\nSwapTotal: 16777216 kB\nSwapFree: 8388608 kB\n' > "$d/meminfo-low"
1268
+ printf 'Filename\t\t\t\tType\t\tSize\t\tUsed\t\tPriority\n/var/swapfile file\t\t67108860\t1048576\t\t-1\n' > "$d/swaps-plenty"
1269
+ RC_EXTRA=("TEAM_MEMINFO_FILE=$d/meminfo-low" "TEAM_SWAPFILE_PATH=$d/swaps-plenty" "TEAM_MIN_AVAIL_MB=1024")
1270
+ RC_CMD=(dispatch dev P1 "$b" --print)
1271
+ rc_case capacity-override 'MemAvailable 只剩' '修法:TEAM_MIN_AVAIL_MB=0' 'apply:cmd' route proceed "dispatched P1"
1272
+ RC_EXTRA=()
1273
+ }
1274
+
1275
+ # ── flips: scratch trees that MUST redden the walk ──────────────────────────────────────────────────
1276
+ mr_scratch_tree() { # <name> → 打印 scratch 树(skills/teamsmith + skills/teamsmith-init 的最小副本)
1277
+ local name="$1"
1278
+ local s="$tmp/$name"
1279
+ rm -rf "$s"; mkdir -p "$s/skills/teamsmith"
1280
+ cp -a "$skill/scripts" "$skill/references" "$skill/templates" "$s/skills/teamsmith/" 2>/dev/null || true
1281
+ # perf.sh 是 source 模式的声明文件(walk 要读它),scratch 树也得有。
1282
+ mkdir -p "$s/skills/teamsmith/tests"
1283
+ cp -a "$skill/tests/perf.sh" "$s/skills/teamsmith/tests/perf.sh" 2>/dev/null || true
1284
+ printf '%s\n' "$s"
1285
+ }
1286
+
1287
+ # mr_flip_run <scratch> <section...> → 在 scratch 树上跑 walk 子集;日志落在 $tmp/flip-run.log
1288
+ # (路径记进 MR_FLIP_LOGF:判据读**文件**,不再把日志搬进变量再走管道 —— 见 mr_flip_log_has)
1289
+ mr_flip_run() {
1290
+ local scratch="$1"; shift
1291
+ MR_FLIP_LOGF="$tmp/flip-run.log"
1292
+ # TEAM_ROUTES_NESTED:flips 在 scratch 树上跑 walk —— 嵌套 run 永不跑 flips(结构上的不递归守卫,
1293
+ # 与段集规则双保险:谁给嵌套 run 传 flips 都只会打印一行 skip)。
1294
+ env -u TMUX -u TMUX_PANE TEAM_ROUTES_NESTED=1 TEAM_ROUTES_TREE="$scratch" bash "$here/routes.sh" "$@" >"$MR_FLIP_LOGF" 2>&1
1295
+ MR_FLIP_RC=$?
1296
+ # TEAM_TMP_KEEP=1 时嵌套 run 的助手会保留自己的根并**打印路径**——收起来给收尾的可见说明用
1297
+ # ($tmp/flip-run.log 每次覆盖,只有这里攒得下每一个保留的根)。
1298
+ grep -a '^保留临时根:' "$MR_FLIP_LOGF" 2>/dev/null >>"$tmp/kept-roots.log" || true
1299
+ }
1300
+
1301
+ # mr_flip_log_has <literal…> → 0 = 本次嵌套 run 的日志里逐字都在(**直接读文件**,不走管道)
1302
+ # P202:旧写法 `printf '%s' "$MR_FLIP_LOG" | grep -q …` 在 set -o pipefail 下会假红 —— grep -q 一命中
1303
+ # 就退出,仍在写的 printf 收 SIGPIPE(141),整条管道变非 0,判据于是把「红且点名」读成「没有兑现」
1304
+ # (现场:同一段日志既被当证据贴出来、又被判成没兑现)。实测:170KB 日志、命中点在中间 → 3/3 误判。
1305
+ mr_flip_log_has() {
1306
+ local p
1307
+ for p in "$@"; do grep -qF -- "$p" "$MR_FLIP_LOGF" || return 1; done
1308
+ return 0
1309
+ }
1310
+
1311
+ # mr_flip_evidence → 「没有兑现」时贴的现场:优先前两条**真 finding**(红色 ✗;正文里那个 ✗ 是
1312
+ # 用法行的标记,不是 finding);一条都没有时贴日志尾巴(例如嵌套 run 起不来时只有一句 setup 失败)。
1313
+ mr_flip_evidence() {
1314
+ local ev
1315
+ ev="$(grep -aF -e $'\033[31m✗' "$MR_FLIP_LOGF" 2>/dev/null | head -2 | tr '\n' ' ')"
1316
+ [ -n "$ev" ] || ev="$(grep -a . "$MR_FLIP_LOGF" 2>/dev/null | tail -2 | tr '\n' ' ')"
1317
+ printf '%s' "$ev"
1318
+ }
1319
+
1320
+ # mr_flip_cnt <file> <literal> → 含该文本的行数(文件不在/无命中都是 0)
1321
+ mr_flip_cnt() { grep -cF -- "$2" "$1" 2>/dev/null || true; }
1322
+
1323
+ # mr_flip_landed <label> <scratch> <rel-path> <gone|-> <present|-> → 0 = 变异落地;1 = 没落地
1324
+ # 纯判据(不发 finding,理由写在 MR_FLIP_WHY):红侧要的就是「没落地」这个判定本身;
1325
+ # 翻转本身用 mr_flip_require。
1326
+ # 变异**前**的状态取自真树(副本是它的 cp -a),**后**取自副本:
1327
+ # a) 副本必须与真树逐字节不同 —— 「补丁静默 no-op」在这里就断了;
1328
+ # b) <gone>(≠ -):真树 ≥1 → 副本 0(目标文本由 1 变 0;模式陈旧当场露馅);
1329
+ # c) <present>(≠ -):真树 0 → 副本 ≥1(新文本由 0 变 1;只删不插、只插不删都过不去)。
1330
+ # 任一不成立 → MR_FLIP_WHY 写成原因并返回 1 —— 这是**夹具缺陷**,不是「没有兑现」;外层的嵌套 run
1331
+ # 由调用者跳过(既然没改到东西,判它红不红没有信息)。
1332
+ mr_flip_landed() {
1333
+ local label="$1" scratch="$2" rel="$3" gone="$4" present="$5"
1334
+ local real="$skill/$rel" copy="$scratch/skills/teamsmith/$rel" g n p
1335
+ MR_FLIP_WHY=""
1336
+ if [ ! -f "$copy" ]; then
1337
+ MR_FLIP_WHY="$rel 在副本里不存在(cp 没落或路径变了)"
1338
+ elif cmp -s "$real" "$copy"; then
1339
+ MR_FLIP_WHY="$rel 与真树逐字节相同 —— 变异一个字都没改"
1340
+ elif [ "$gone" != "-" ]; then
1341
+ g="$(mr_flip_cnt "$real" "$gone")"; n="$(mr_flip_cnt "$copy" "$gone")"
1342
+ if [ "${g:-0}" -lt 1 ]; then MR_FLIP_WHY="目标文本在真树里就找不到(模式已陈旧):gone 0→${n:-0}"
1343
+ elif [ "${n:-0}" -ne 0 ]; then MR_FLIP_WHY="目标文本还在:gone ${g:-0}→${n:-0}(替换没匹配上)"; fi
1344
+ fi
1345
+ if [ -z "$MR_FLIP_WHY" ] && [ "$present" != "-" ]; then
1346
+ g="$(mr_flip_cnt "$real" "$present")"; p="$(mr_flip_cnt "$copy" "$present")"
1347
+ if [ "${g:-0}" -ne 0 ]; then MR_FLIP_WHY="新文本在真树里本来就有(判据不成立):present ${g:-0}→${p:-0}"
1348
+ elif [ "${p:-0}" -lt 1 ]; then MR_FLIP_WHY="新文本没出现:present 0→${p:-0}"; fi
1349
+ fi
1350
+ [ -z "$MR_FLIP_WHY" ] && return 0
1351
+ return 1
1352
+ }
1353
+
1354
+ # mr_flip_require <label> <scratch> <rel-path> <gone|-> <present|-> → 落地则 0;没落地则发 finding 并返回 1
1355
+ mr_flip_require() {
1356
+ mr_flip_landed "$1" "$2" "$3" "$4" "$5" && return 0
1357
+ finding "$1:变异没落地(夹具缺陷)—— $MR_FLIP_WHY"
1358
+ return 1
1359
+ }
1360
+
1361
+ # 嵌套 run 的根:本轮台账里 kind=routes 且 pid 不是本进程的存活根,加上文件系统里新于本夹具根的
1362
+ # owned 家族目录(兜住「不用助手、裸 mkdir」的形状)。台账是主来源(父/子共享 run 台账,每个嵌套
1363
+ # 根都在册,与 mtime 无关);find 是次要来源(原始形状,保留不放松)。
1364
+ # <base> 可换:翻转⑧在私有 TMPDIR 里造「不回收」形状,不让刻意的残留落进共享临时目录。
1365
+ mr_nested_leftovers() { # [<base>]
1366
+ local base="${1:-${TMPDIR:-/tmp}}"
1367
+ {
1368
+ tmp_root_ledger_survivors 2>/dev/null | awk -F'\t' -v me="$$" -v base="$base" \
1369
+ '$2 != me && $3 == "routes" && index($1, base "/") == 1 { print $1 }'
1370
+ find "$base" -maxdepth 1 -name 'teamsmith-routes.*' -newer "$tmp" 2>/dev/null | grep -v "^$tmp\$" || true
1371
+ } | sed '/^$/d' | sort -u
1372
+ }
1373
+
1374
+ mr_flips() {
1375
+ section "flips · 把树改坏 → walk 必须红并点名(翻转证据)"
1376
+ local s log out ev
1377
+ # 判据卫生(P202):现场那条「没有兑现」贴出来的证据必须是真的 finding。旧写法 `grep '✗'` 会把
1378
+ # ✓ 断言正文里那个用法行标记(`| ✗ change status <id> [--json]`)当 finding 贴出去 —— 读者于是
1379
+ # 分不清「守卫没红」和「变异没落地」(P202 的现场证词就是这种形状)。这条把「只贴真 finding」
1380
+ # 钉住:把 mr_flip_evidence 改回宽 grep 就会红。
1381
+ MR_FLIP_LOGF="$tmp/flip-evidence-probe.log"
1382
+ printf ' \033[32m✓\033[0m 断言 L31:change status --json → rc=2 | ✗ change status <id> [--json]\n \033[31m✗\033[0m 断言 L38:add-agent --fresh 被自己的解析器拒绝\n' > "$MR_FLIP_LOGF"
1383
+ ev="$(mr_flip_evidence)"
1384
+ if [ "${ev#*add-agent --fresh}" != "$ev" ] && [ "${ev#*change status <id>}" = "$ev" ]; then
1385
+ ok "判据卫生:没有兑现时的现场只贴真 finding(正文里的用法行标记不冒充 finding)"
1386
+ else
1387
+ finding "判据卫生:证据里混进了正文的用法行标记($ev)"
1388
+ fi
1389
+ # ① 场地缺陷:help 打印 add-agent --model,解析器拒绝它
1390
+ s="$(mr_scratch_tree flip-model)"
1391
+ python3 - "$s/skills/teamsmith/scripts/lib/cmd-agents.sh" <<'PY'
1392
+ import sys
1393
+ p = sys.argv[1]
1394
+ s = open(p, encoding='utf-8').read()
1395
+ s = s.replace('--model) model="${2:?--model 需要 <provider/model> 或 -}"; has_model=1; shift 2 ;;',
1396
+ '--model) team_usage_die "add-agent: 未知参数 --model" ;;', 1)
1397
+ s = s.replace('--model=*) model="${1#*=}"; has_model=1; shift ;;', '', 1)
1398
+ open(p, 'w', encoding='utf-8').write(s)
1399
+ PY
1400
+ # 落地证明(P202):①改了两处,两处都要真的落到副本里
1401
+ if mr_flip_require "翻转①" "$s" scripts/lib/cmd-agents.sh \
1402
+ '--model) model="${2:?--model 需要 <provider/model> 或 -}"; has_model=1; shift 2 ;;' \
1403
+ '--model) team_usage_die "add-agent: 未知参数 --model" ;;' \
1404
+ && mr_flip_require "翻转①" "$s" scripts/lib/cmd-agents.sh '--model=*) model="${1#*=}"; has_model=1; shift ;;' -; then
1405
+ mr_flip_run "$s" walk
1406
+ if [ "$MR_FLIP_RC" != "0" ] && mr_flip_log_has 'add-agent --model'; then
1407
+ ok "翻转①:help 打印 --model 而解析器拒绝 → walk 红并点名 add-agent --model"
1408
+ else
1409
+ finding "翻转①没有兑现(rc=$MR_FLIP_RC):$(mr_flip_evidence)"
1410
+ fi
1411
+ fi
1412
+ # ② 兄弟旗标:--fresh 是 dispatch 的,add-agent 不接受
1413
+ s="$(mr_scratch_tree flip-sibling)"
1414
+ sed -i 's/add-agent <a> \[--register\]/add-agent <a> [--fresh]/' "$s/skills/teamsmith/scripts/lib/cmd-project.sh"
1415
+ if mr_flip_require "翻转②" "$s" scripts/lib/cmd-project.sh 'add-agent <a> [--register]' 'add-agent <a> [--fresh]'; then
1416
+ mr_flip_run "$s" walk
1417
+ if [ "$MR_FLIP_RC" != "0" ] && mr_flip_log_has 'add-agent --fresh'; then
1418
+ ok "翻转②:add-agent 行印上兄弟命令的 --fresh → walk 红并点名 add-agent --fresh"
1419
+ else
1420
+ finding "翻转②没有兑现(rc=$MR_FLIP_RC):$(mr_flip_evidence)"
1421
+ fi
1422
+ fi
1423
+ # 落地判据自己的红侧(P202:判据不能是橡皮章)。两条都只做文件级检查,不跑嵌套 run:
1424
+ # a) 陈旧模式:sed 的搜索端在文件里根本不存在 → 谁都没改 → 必须报「变异没落地」;
1425
+ # b) 只插不删:新文本出现了、旧文本还在 → 判据的另一侧必须红(证明 gone 一侧承重)。
1426
+ s="$(mr_scratch_tree flip-stale-pattern)"
1427
+ sed -i 's/add-agent <a> \[--frosh\]/add-agent <a> [--fresh]/' "$s/skills/teamsmith/scripts/lib/cmd-project.sh"
1428
+ if mr_flip_landed "落地判据红侧 a" "$s" scripts/lib/cmd-project.sh 'add-agent <a> [--register]' 'add-agent <a> [--fresh]'; then
1429
+ finding "落地判据红侧 a:陈旧 sed 什么也没改,判据却放行了(判据是橡皮章)"
1430
+ else
1431
+ ok "落地判据红侧 a:陈旧 sed(搜索端不存在)→ 判据报「变异没落地」:$MR_FLIP_WHY"
1432
+ fi
1433
+ s="$(mr_scratch_tree flip-half-landed)"
1434
+ sed -i 's/^ add-agent <a> \[--register\]/ add-agent <a> [--register] [--fresh]/' "$s/skills/teamsmith/scripts/lib/cmd-project.sh"
1435
+ if mr_flip_landed "落地判据红侧 b" "$s" scripts/lib/cmd-project.sh 'add-agent <a> [--register]' 'add-agent <a> [--fresh]'; then
1436
+ finding "落地判据红侧 b:旧文本还在(只插不删),判据却放行了"
1437
+ else
1438
+ ok "落地判据红侧 b:只插不删 → 判据报「变异没落地」:$MR_FLIP_WHY"
1439
+ fi
1440
+ # ③ 吞旗标:ps 不再拒绝未知参数(今天 version/meeting list 是实测的两种形状,已在 D6 修掉)
1441
+ s="$(mr_scratch_tree flip-swallow)"
1442
+ python3 - "$s/skills/teamsmith/scripts/lib/cmd-status.sh" <<'PY'
1443
+ import sys, re
1444
+ p = sys.argv[1]
1445
+ s = open(p, encoding='utf-8').read()
1446
+ i = s.index('team_cmd_ps() {')
1447
+ j = s.index('while [ $# -gt 0 ]; do', i)
1448
+ k = s.index('case "$1" in', j)
1449
+ ins = s.index('\n', k) + 1
1450
+ s = s[:ins] + ' -*) shift ;;\n' + s[ins:]
1451
+ open(p, 'w', encoding='utf-8').write(s)
1452
+ PY
1453
+ # ps 在 help 里不打印旗标 —— 控制臂按设计只覆盖「打印旗标的路径」;这里把 ps 的用法行改印一个旗标,
1454
+ # 让这段翻转的能量落在「吞旗标必须红」上(形状与今天实测的 version/meeting list 同类)。
1455
+ sed -i 's/^ ps 容量:/ ps [--frobnicate-probe] 容量:/' "$s/skills/teamsmith/scripts/lib/cmd-project.sh"
1456
+ if mr_flip_require "翻转③" "$s" scripts/lib/cmd-status.sh - '-*) shift ;;' \
1457
+ && mr_flip_require "翻转③" "$s" scripts/lib/cmd-project.sh ' ps 容量:' ' ps [--frobnicate-probe] 容量:'; then
1458
+ mr_flip_run "$s" walk control
1459
+ if [ "$MR_FLIP_RC" != "0" ] && mr_flip_log_has '控制:ps'; then
1460
+ ok "翻转③:ps 吞掉未知旗标 → 控制臂红并点名 ps"
1461
+ else
1462
+ finding "翻转③没有兑现(rc=$MR_FLIP_RC):$(mr_flip_evidence)"
1463
+ fi
1464
+ fi
1465
+ # ④ 无法归属的行:命令块里列 0 的行(修复前的 board 形状)
1466
+ s="$(mr_scratch_tree flip-unattributable)"
1467
+ sed -i 's/^ board add|assign|set|row|ls/board add|assign|set|row|ls/' "$s/skills/teamsmith/scripts/lib/cmd-project.sh"
1468
+ # 落地证明只查 gone 一侧:这步只动行首两个空格,去空格后的行**仍包含**原来那段文本,所以「0→1」
1469
+ # 用子串表达不出来;而「带两个空格的形式由 1 变 0」+ cmp 的字节差就是确证(只有 sed 能改出这个形状)。
1470
+ if mr_flip_require "翻转④" "$s" scripts/lib/cmd-project.sh ' board add|assign|set|row|ls' -; then
1471
+ mr_flip_run "$s" walk
1472
+ if [ "$MR_FLIP_RC" != "0" ] && mr_flip_log_has '列 0 的行'; then
1473
+ ok "翻转④:命令块里的列 0 行 → walk 红并点名该行(修复前的 board 形状)"
1474
+ else
1475
+ finding "翻转④没有兑现(rc=$MR_FLIP_RC):$(mr_flip_evidence)"
1476
+ fi
1477
+ fi
1478
+ # ⑤ schema 注释点名不存在的命令
1479
+ s="$(mr_scratch_tree flip-schema-route)"
1480
+ sed -i 's/TEAM_GATES|apply|cmd||plain||-|||workflow/TEAM_GATES|apply|cmd||plain||-|team frob off||workflow/' "$s/skills/teamsmith/scripts/lib/cmd-config.sh"
1481
+ if mr_flip_require "翻转⑤" "$s" scripts/lib/cmd-config.sh 'TEAM_GATES|apply|cmd||plain||-|||workflow' 'TEAM_GATES|apply|cmd||plain||-|team frob off||workflow'; then
1482
+ mr_flip_run "$s" promises
1483
+ if [ "$MR_FLIP_RC" != "0" ] && mr_flip_log_has 'team frob' 'TEAM_GATES'; then
1484
+ ok "翻转⑤:TEAM_GATES 的注释点名 team frob → walk 红并点名 TEAM_GATES / team frob"
1485
+ else
1486
+ finding "翻转⑤没有兑现(rc=$MR_FLIP_RC):$(mr_flip_evidence)"
1487
+ fi
1488
+ fi
1489
+ # ⑥ schema 注释点名命令但没有 promise 探针
1490
+ s="$(mr_scratch_tree flip-unprobed)"
1491
+ python3 - "$s/skills/teamsmith/scripts/lib/cmd-config.sh" <<'PY'
1492
+ import sys
1493
+ p = sys.argv[1]
1494
+ s = open(p, encoding='utf-8').read()
1495
+ anchor = "EOF\n}\n"
1496
+ i = s.index(anchor)
1497
+ row = "TEAM_ROUTES_UNPROBED|apply|text||plain||-|维护:team config set-agent-model <seat> <model>||workflow\n"
1498
+ s = s[:i] + row + s[i:]
1499
+ open(p, 'w', encoding='utf-8').write(s)
1500
+ PY
1501
+ if mr_flip_require "翻转⑥" "$s" scripts/lib/cmd-config.sh - 'TEAM_ROUTES_UNPROBED|apply|text||plain||-|维护:team config set-agent-model <seat> <model>||workflow'; then
1502
+ mr_flip_run "$s" promises
1503
+ if [ "$MR_FLIP_RC" != "0" ] && mr_flip_log_has 'TEAM_ROUTES_UNPROBED' && ! mr_flip_log_has 'promise:TEAM_GATES'; then
1504
+ ok "翻转⑥:新键的注释点名命令却没有探针 → walk 红并只点名该键"
1505
+ else
1506
+ finding "翻转⑥没有兑现(rc=$MR_FLIP_RC):$(mr_flip_evidence)"
1507
+ fi
1508
+ fi
1509
+ # ⑦ 承诺被破坏:注册命令 rc=0 但名册没变
1510
+ s="$(mr_scratch_tree flip-broken-promise)"
1511
+ python3 - "$s/skills/teamsmith/scripts/lib/cmd-config.sh" <<'PY'
1512
+ import sys
1513
+ p = sys.argv[1]
1514
+ s = open(p, encoding='utf-8').read()
1515
+ a = "\n local why\n if ! why=\"$(team_config_list_violation \"$new\")\"; then"
1516
+ b = "\n if [ \"$op\" = \"add\" ]; then return 0; fi # flip: 注册命令报成功但不写\n local why\n if ! why=\"$(team_config_list_violation \"$new\")\"; then"
1517
+ assert a in s, 'anchor not found'
1518
+ s = s.replace(a, b, 1)
1519
+ open(p, 'w', encoding='utf-8').write(s)
1520
+ PY
1521
+ if mr_flip_require "翻转⑦" "$s" scripts/lib/cmd-config.sh - '# flip: 注册命令报成功但不写'; then
1522
+ mr_flip_run "$s" promises
1523
+ if [ "$MR_FLIP_RC" != "0" ] && mr_flip_log_has 'promise(TEAM_AGENTS)'; then
1524
+ ok "翻转⑦:注册报成功而名册没变 → promise 探针红并点名 TEAM_AGENTS(断言的是效果)"
1525
+ else
1526
+ finding "翻转⑦没有兑现(rc=$MR_FLIP_RC):$(mr_flip_evidence)"
1527
+ fi
1528
+ fi
1529
+ # ⑧ 收尾断言本身必须能被证伪:让嵌套 run 的回收失效(TMP_ROOT_BREAK=noreap,tmp-root.sh 自带的
1530
+ # 自检旋钮)跑一次 walk —— 收尾扫描必须看得见那个残留(默认 KEEP 未设时,这就是那条断言的红侧)。
1531
+ # 刻意的残留落在私有 TMPDIR 里,随本夹具的根一起被回收,不进共享临时目录。
1532
+ s="$(mr_scratch_tree flip-tail-leak)"
1533
+ local lkdir="$tmp/flip-tail-leak-tmp"
1534
+ mkdir -p "$lkdir"
1535
+ # 变异(这个旋钮)的落地证明:helper 里**有**这个分支(在真树里数得着),且这次 run 真的没回收 ——
1536
+ # 私有 TMPDIR 里直接看得见一个 teamsmith-routes.* 根。它与收尾扫描互为对照(后者走台账+find):
1537
+ # 根在、扫描看不见 = 扫描瞎了;根不在 = 旋钮没生效。
1538
+ s="$(mr_scratch_tree flip-tail-leak)"
1539
+ local lkdir="$tmp/flip-tail-leak-tmp"
1540
+ mkdir -p "$lkdir"
1541
+ local knob left_fs
1542
+ knob="$(mr_flip_cnt "$skill/tests/lib/tmp-root.sh" '[ "${TMP_ROOT_BREAK:-}" = "noreap" ] && continue')"
1543
+ env -u TMUX -u TMUX_PANE TEAM_ROUTES_NESTED=1 TEAM_ROUTES_TREE="$s" TMPDIR="$lkdir" TMP_ROOT_BREAK=noreap \
1544
+ bash "$here/routes.sh" walk >"$tmp/flip-tail-leak.log" 2>&1 || true
1545
+ grep -a '^保留临时根:' "$tmp/flip-tail-leak.log" 2>/dev/null >>"$tmp/kept-roots.log" || true
1546
+ left_fs="$(find "$lkdir" -maxdepth 1 -name 'teamsmith-routes.*' -type d 2>/dev/null | head -1)"
1547
+ if [ "${knob:-0}" -lt 1 ] || [ -z "$left_fs" ]; then
1548
+ finding "翻转⑧:变异没落地(夹具缺陷)—— helper 里的 noreap 分支 ${knob:-0} 处;私有 TMPDIR 里没留下根(旋钮没生效)"
1549
+ elif [ -n "$(mr_nested_leftovers "$lkdir")" ]; then
1550
+ ok "翻转⑧:嵌套 run 不回收 → 收尾扫描看得见残留(默认 KEEP 未设时判红)"
1551
+ else
1552
+ finding "翻转⑧没有兑现:嵌套 run 不回收(根还在:$left_fs),收尾扫描却没看见残留"
1553
+ fi
1554
+ # ⑨ P140:印了一条 family 表里没有条目的路线 → C1 目录对账必须红并点名它(沉默的路线不可接受)
1555
+ s="$(mr_scratch_tree flip-unclaimed-route)"
1556
+ python3 - "$s/skills/teamsmith/scripts/lib/cmd-agents.sh" <<'PY'
1557
+ import sys
1558
+ p = sys.argv[1]
1559
+ s = open(p, encoding='utf-8').read()
1560
+ anchor = 'team_df_err " 规则 1 没有 --force 逃生门'
1561
+ assert anchor in s, 'anchor not found'
1562
+ s = s.replace(anchor, 'team_df_err " 修法:team frob on(没有走查条目的路线)"\n ' + anchor, 1)
1563
+ open(p, 'w', encoding='utf-8').write(s)
1564
+ PY
1565
+ if mr_flip_require "翻转⑨" "$s" scripts/lib/cmd-agents.sh - '修法:team frob on(没有走查条目的路线)'; then
1566
+ mr_flip_run "$s" refusals
1567
+ if [ "$MR_FLIP_RC" != "0" ] && mr_flip_log_has '没人认领' 'team frob'; then
1568
+ ok "翻转⑨:印了一条 family 表没有的路线 → C1 目录对账红并点名它"
1569
+ else
1570
+ finding "翻转⑨没有兑现(rc=$MR_FLIP_RC):$(mr_flip_evidence)"
1571
+ fi
1572
+ fi
1573
+ # ⑩ P140:打印的修法修错了目标(switch 到别的任务)→ 逐族走查必须红并点名 family;Walk A/B 同树照旧绿
1574
+ s="$(mr_scratch_tree flip-mutated-fix)"
1575
+ python3 - "$s/skills/teamsmith/scripts/lib/cmd-agents.sh" <<'PY'
1576
+ import sys
1577
+ p = sys.argv[1]
1578
+ s = open(p, encoding='utf-8').read()
1579
+ # 只改**别的任务分支**那一支的两条路线(switch / switch -c),别动 detached 支的同形路线
1580
+ anchor = 'team_df_dim " (拒绝的理由:复验/交付记录会以工作树的分支为证据,不能张冠李戴)"'
1581
+ assert anchor in s, 'reason anchor not found'
1582
+ a = s.index(anchor)
1583
+ start = s.rindex(' if git -C "$wt" show-ref --verify --quiet "refs/heads/$want"; then', 0, a)
1584
+ block = s[start:a]
1585
+ assert block.count('"$want"') >= 2, 'expected both route variants in the block'
1586
+ s = s[:start] + block.replace('"$want"', '"task/T9-wrong"') + s[a:]
1587
+ open(p, 'w', encoding='utf-8').write(s)
1588
+ PY
1589
+ if mr_flip_require "翻转⑩" "$s" scripts/lib/cmd-agents.sh - 'task/T9-wrong'; then
1590
+ mr_flip_run "$s" refusals
1591
+ if [ "$MR_FLIP_RC" != "0" ] && mr_flip_log_has 'C2/branch-switch' '路线'; then
1592
+ ok "翻转⑩:打印的 switch 目标修错(task/T9-wrong)→ 逐族走查红并点名 branch-switch"
1593
+ else
1594
+ finding "翻转⑩没有兑现(rc=$MR_FLIP_RC):$(mr_flip_evidence)"
1595
+ fi
1596
+ mr_flip_run "$s" walk control
1597
+ if [ "$MR_FLIP_RC" = "0" ]; then
1598
+ ok "翻转⑩对照:同一棵修坏的树上 Walk A/控制臂照旧绿(只红拒绝路线那一族)"
1599
+ else
1600
+ finding "翻转⑩对照:修坏的树上 Walk A/控制臂不该变红(rc=$MR_FLIP_RC)"
1601
+ fi
1602
+ fi
1603
+ # 收尾:flips 自己的 scratch 树与嵌套 run 的根都不留下。
1604
+ # TEAM_TMP_KEEP=1(门禁的 --keep 让子进程都继承它)是**刻意的保留**,不是泄漏 —— 与 smoke §40
1605
+ # 同口径:打一条可见说明,并把嵌套 run 打印的保留路径逐个列出(静默保留仍是缺陷)。
1606
+ # 本夹具自己的根也会被保留:助手在 EXIT 时打印它的路径。
1607
+ if [ "${TEAM_TMP_KEEP:-0}" = "1" ]; then
1608
+ ok "翻转收尾:本轮声明保留(TEAM_TMP_KEEP=1),不判泄漏"
1609
+ printf ' \033[2m·\033[0m 保留本夹具的根:%s(助手在 EXIT 时也会打印)\n' "$tmp"
1610
+ if [ -s "$tmp/kept-roots.log" ]; then
1611
+ sort -u "$tmp/kept-roots.log" | while IFS= read -r line; do
1612
+ printf ' \033[2m·\033[0m %s\n' "$line"
1613
+ done
1614
+ fi
1615
+ else
1616
+ local leftovers n
1617
+ leftovers="$(mr_nested_leftovers)"
1618
+ n="$(printf '%s\n' "$leftovers" | grep -c . || true)"
1619
+ if [ "${n:-0}" = "0" ]; then
1620
+ ok "翻转收尾:嵌套 run 的临时根都回收了"
1621
+ else
1622
+ finding "翻转收尾:留下 $n 个嵌套临时根:$(printf '%s' "$leftovers" | tr '\n' ' ')"
1623
+ fi
1624
+ fi
1625
+ }
1626
+
1627
+ # ── 结果 ────────────────────────────────────────────────────────────────────────────────────────────
1628
+ if want walk; then
1629
+ if mr_walk_a; then :; fi
1630
+ fi
1631
+ if want control; then
1632
+ if mr_control; then :; fi
1633
+ fi
1634
+ if want promises; then
1635
+ if mr_walk_b; then :; fi
1636
+ fi
1637
+ if want refusals; then
1638
+ if mr_refusals; then :; fi
1639
+ fi
1640
+ if want flips; then
1641
+ if [ "${TEAM_ROUTES_NESTED:-0}" = "1" ]; then
1642
+ skip "flips(嵌套 run:flips 自己在 scratch 树上跑 walk,不再往下递归)"
1643
+ elif [ "$FAST" = "1" ]; then
1644
+ skip "flips(FAST 模式:翻转要重跑 walk N 次,独立运行时跑;完整门禁会跑)"
1645
+ else
1646
+ mr_flips
1647
+ fi
1648
+ fi
1649
+
1650
+ printf '\n\033[1m== 结果 ==\033[0m ✓ %d ✗ %d SKIP %d\n' "$PASS" "$FIND" "$SKIP"
1651
+ [ "$FIND" -eq 0 ] && exit 0
1652
+ exit 1