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,1156 @@
1
+ #!/usr/bin/env bash
2
+ # teamsmith · 项目契约(.pi/team/config.sh)的读写面:team config list|set|log|set-agent-model
3
+ #
4
+ # 这一层拥有三件事,别的层不许再写一遍:
5
+ # * **schema**:每个键的 class(apply/restart/refuse)、kind(值域校验)、form(plain/export)、
6
+ # 默认值与 danger 规则。表就是真相,`team config list --json` 与面板的类徽章都读它;
7
+ # 完整性(模板键 / references/config.md 的键 ↔ schema)是测试,不是承诺(tests/config-cli.sh)。
8
+ # * **写路径**:值校验 → danger → 指纹 CAS → 加固 writer(cmd-bootstrap.sh 的
9
+ # team_config_set_in_file,唯一底层写入口)→ 原子 mv;每次尝试一行审计。
10
+ # * **模型席位**:TEAM_AGENT_MODELS / TEAM_PM_MODEL 按席位读写(set-agent-model),来源三态与
11
+ # team_agent_model_src 同口径,读侧给面板 models 块。
12
+ #
13
+ # 退出码是机器契约(面板/测试都按它分支,不解析人话):
14
+ # 0 写入成功(--dry-run 也是 0 = 校验通过)/ 3 指纹冲突(什么都没写)/
15
+ # 4 值不合法(什么都没写)/ 5 键不可改(未知键或 refuse 类)/ 6 写失败(原字节未动)/
16
+ # 7 危险值,需要 --allow-danger(什么都没写)。
17
+ #
18
+ # 契约的路径:显式 TEAM_CONFIG_FILE > 目录推导(team_load_config 已经算好 TEAM_CONFIG)。
19
+ # 没有契约 = 项目还没 init → 报错点名 `team init`,绝不悄悄造一个。
20
+
21
+ # ---------------------------------------------------------------- schema(唯一真相)
22
+ # 行格式:KEY|class|kind|spec|form|default|danger|route|suggest|group
23
+ # group:**第 10 列**,功能域 token(封闭词表,^[a-z][a-z0-9-]*$,每行必填)。它是 schema 数据,
24
+ # 不是从 `# ----` 分节注释解析出来的(注释是散文,不是语法):`team config list --json`
25
+ # 把 token 逐字放进记录,视图分组的标签从 zh/en 表的 group_<token> 查。词表封闭由
26
+ # 「schema 行用的 token ⇄ 标签」双向相等这条门禁保证(tests/panel-strings.mjs)。
27
+ # 无 token 的行是畸形行:config-cli.sh 的 groups 走查点名该键判红(视图侧同时可见降级到「未分组」)。
28
+ # kind:bool / int / seconds / mb / pct / enum / text / list / path / cmd / tpl / model /
29
+ # pairlist / pattern / winlist / bytes
30
+ # spec:int/seconds/mb/pct/bytes 是「min,max」(空 = 无边);enum 是逗号分隔的值域;
31
+ # path 是 file|dir|exec|any 加「,opt」(空值有意义);tpl 是 launch|notify|pm
32
+ # danger:'-' 或一行理由(命中即需要 --allow-danger)
33
+ # route:自由文本说明列 —— refuse 类的用法指引,以及 apply 类(frozen 接缝)的备注(面板把它原样展示)
34
+ # suggest:**可选的第 9 列**,数值类键的建议值(逗号分隔的整值;仍是 schema 数据,不是选项表);
35
+ # 没有建议的行保持 8 列(读者两形状都认)。读侧(team_config_choices)与写入校验的一致性
36
+ # 由 tests/panel-choices.sh 的走查夹具钉住:读给出的每个值都必须被 team config set 接受。
37
+ team_config_schema() {
38
+ cat <<'EOF'
39
+ # ---- 身份与账本布局(refuse:控制台不得改身份、不得搬走它正在读的账本)----
40
+ TEAM_PROJECT|refuse|text||plain||-|身份:手改 .pi/team/config.sh(team init 不改它);team init --force 会重渲染整份契约||identity
41
+ TEAM_SESSION|refuse|text||plain||-|身份:手改 .pi/team/config.sh(team init 不改它);team init --force 会重渲染整份契约||identity
42
+ TEAM_PM_WINDOW|refuse|text||plain|pm|-|身份:手改 .pi/team/config.sh(team init 不改它);team init --force 会重渲染整份契约||identity
43
+ TEAM_AGENTS|refuse|list||plain||-|名册:team add-agent <a> --register / team teardown --agent <a> --register(都走审计写入器)||identity
44
+ TEAM_DOCS_DIR|refuse|path|dir,opt|plain|docs/team|-|账本布局:手改 .pi/team/config.sh||identity
45
+ TEAM_WORKTREES_DIR|refuse|text||plain|.worktrees|-|账本布局:手改 .pi/team/config.sh||identity
46
+ TEAM_STATE_DIR|refuse|path|dir,opt|plain|.pi/team/state|-|账本布局:手改 .pi/team/config.sh||identity
47
+ TEAM_MEETINGS_DIR|refuse|path|dir,opt|plain|~/.pi/team/meetings|-|账本布局:手改 .pi/team/config.sh||identity
48
+ TEAM_SPEC_DIR|refuse|path|dir,opt|plain|openspec|-|账本布局:手改 .pi/team/config.sh||identity
49
+ TEAM_ROOT|refuse|path|dir,opt|plain||-|环境定位:用 --root 或手改 .pi/team/config.sh||identity
50
+ TEAM_MAIN_ROOT|refuse|path|dir,opt|plain||-|环境定位:用 --root 或手改 .pi/team/config.sh||identity
51
+ TEAM_CONFIG_FILE|refuse|path|file,opt|plain||-|环境定位:用 --config 或手改 .pi/team/config.sh||identity
52
+ # ---- 分支与 forge(refuse:它们定义「已落地」与凭据位置)----
53
+ TEAM_BRANCH_MODE|refuse|enum|task,agent|plain|task|-|分支语义(定义「已落地」):手改 .pi/team/config.sh||branch
54
+ TEAM_TASK_BRANCH_PREFIX|refuse|text||plain|task|-|分支语义:手改 .pi/team/config.sh||branch
55
+ TEAM_AGENT_BRANCH_PREFIX|refuse|text||plain|agent|-|分支语义:手改 .pi/team/config.sh||branch
56
+ TEAM_PROTECTED_BRANCH|refuse|text||plain|main|-|分支语义:手改 .pi/team/config.sh||branch
57
+ TEAM_REMOTE|refuse|text||plain|origin|-|分支语义:手改 .pi/team/config.sh||branch
58
+ TEAM_VCS|refuse|enum|local,github,gitlab,other|plain|local|-|分支语义/forge:手改 .pi/team/config.sh||branch
59
+ TEAM_TOKEN_FILE|refuse|path|file,opt|plain|.gh-pat|-|凭据位置:手改 .pi/team/config.sh||branch
60
+ TEAM_GITLAB_HOST|refuse|text||plain||-|凭据位置:手改 .pi/team/config.sh||branch
61
+ TEAM_GITLAB_PROJECT|refuse|text||plain||-|凭据位置:手改 .pi/team/config.sh||branch
62
+ TEAM_GITLAB_TOKEN_FILE|refuse|path|file,opt|plain||-|凭据位置:手改 .pi/team/config.sh||branch
63
+ TEAM_MERGE_PREFER_THEIRS|apply|text||plain||-|||branch
64
+ # ---- 权限与依赖策略(refuse:控制台不得给自己扩权)----
65
+ TEAM_CONFIRM_WRITES|refuse|bool||plain|1|-|权限守卫:控制台不给自己扩权;手改 .pi/team/config.sh||policy
66
+ TEAM_ALLOW_FOREIGN_IDENTITY|refuse|bool||plain|0|-|权限守卫:手改 .pi/team/config.sh||policy
67
+ TEAM_ALLOW_FOREIGN_SESSION|refuse|bool||plain|0|-|权限守卫:手改 .pi/team/config.sh||policy
68
+ TEAM_GUARD_FOREIGN_TARGET|refuse|bool||plain|1|-|权限守卫:手改 .pi/team/config.sh||policy
69
+ TEAM_REPLACE_FOREIGN_PM|refuse|bool||plain|0|-|权限守卫:手改 .pi/team/config.sh||policy
70
+ TEAM_ALLOW_DESTRUCTIVE_TMUX|refuse|bool||plain|0|-|权限守卫(M67 退役):不再授权任何操作(判定按目标);保留为不接受写入的只读墓碑,手改 .pi/team/config.sh||policy
71
+ TEAM_ASSUME_YES|refuse|bool||plain|0|-|权限守卫:手改 .pi/team/config.sh||policy
72
+ TEAM_REQUIRE_JS|refuse|bool||plain|1|-|依赖策略:手改 .pi/team/config.sh||policy
73
+ TEAM_REQUIRE_OPENSPEC|refuse|bool||plain|1|-|依赖策略:手改 .pi/team/config.sh||policy
74
+ TEAM_REQUIRE_MAGIC_CONTEXT|refuse|bool||plain|1|-|依赖策略:手改 .pi/team/config.sh||policy
75
+ TEAM_PI_AGENT_DIR|refuse|path|dir,opt|plain||-|机器路径:手改 .pi/team/config.sh||policy
76
+ TEAM_PI_SETTINGS_FILE|refuse|path|file,opt|plain|$HOME/.pi/agent/settings.json|-|机器路径:手改 .pi/team/config.sh||policy
77
+ TEAM_MEMINFO_FILE|refuse|path|file,opt|plain||-|机器路径:手改 .pi/team/config.sh||policy
78
+ TEAM_DISK_STATS_FILE|refuse|path|file,opt|plain||-|测试旋钮:磁盘读数夹具(TEAM_MEMINFO_FILE 同族);只在夹具里用||policy
79
+ TEAM_SMOKE_FAST|refuse|bool||plain|0|-|测试旋钮:环境变量或手改 .pi/team/config.sh||policy
80
+ TEAM_INBOX_WATCH_FORCE_FAIL|refuse|text||plain||-|测试旋钮(M53):强制 watcher 注册失败路径,记录标 forced=1;只在夹具里用||policy
81
+ TEAM_IW_REQUIRE_WATCH|refuse|bool||plain|0|-|测试旋钮(M53):inbox-watch 门禁严格模式——不可用的前提判红而不是可见 SKIP||policy
82
+ # ---- 名册、模型解析与适配器(apply:下一个读契约的进程就生效)----
83
+ TEAM_MODEL_LIMITS|apply|pattern||plain|kimi-coding/k3=2 openai-codex/*=1|-|||roster
84
+ TEAM_MODEL_WINDOWS|apply|winlist||plain||-|||roster
85
+ TEAM_SESSION_WARN_TOKENS|apply|int|0,|plain|200000|-||100000,200000,400000|roster
86
+ TEAM_EXTRA_PI_ARGS|apply|cmd||plain||-|||roster
87
+ TEAM_AGENT_CMD|apply|tpl|launch|plain||-|内部接缝(frozen):为将来非 Pi 适配预留,不承诺兼容;不在 init 问卷里问||roster
88
+ TEAM_AGENT_NOTIFY_CMD|apply|tpl|notify|plain||-|内部接缝(frozen):为将来非 Pi 适配预留,不承诺兼容;不在 init 问卷里问||roster
89
+ TEAM_AGENT_LOG_GLOB|apply|text||plain||-|内部接缝(frozen):为将来非 Pi 适配预留,不承诺兼容;不在 init 问卷里问||roster
90
+ TEAM_PI_BIN|apply|path|exec|plain|pi|-|||roster
91
+ TEAM_AGENT_BIN|apply|path|exec,opt|plain||-|内部接缝(frozen):为将来非 Pi 适配预留,不承诺兼容;不在 init 问卷里问||roster
92
+ TEAM_OPENSPEC_BIN|apply|path|exec|plain|openspec|-|||roster
93
+ TEAM_JS_BIN|apply|path|exec,opt|plain||-|||roster
94
+ # ---- 按席位模型(restart:运行中的席位保住旧模型,下一次 dispatch/resume 才换)----
95
+ TEAM_DEFAULT_MODEL|restart|model|req|plain|deepseek/deepseek-flash|-|模型:下一个 spawn 生效(dispatch/resume/team up)||seat-model
96
+ TEAM_AGENT_MODELS|restart|pairlist||plain||-|按席位模型:team config set-agent-model <seat> <model>||seat-model
97
+ TEAM_PM_MODEL|restart|model|opt|plain||-|PM 席位模型:team up 重建 PM 后生效||seat-model
98
+ # ---- 工作流与门禁(apply)----
99
+ TEAM_GATES|apply|cmd||plain||-|||workflow
100
+ TEAM_INSTALL_CMD|apply|cmd||plain||-|||workflow
101
+ TEAM_TASK_BRANCH_RESET|apply|bool||plain|1|-|||workflow
102
+ TEAM_DISPATCH_VERIFY_SEC|apply|seconds|0,|plain|8|-|||workflow
103
+ TEAM_DISPATCH_ALIVE_SEC|apply|seconds|0,|plain|1|-|||workflow
104
+ TEAM_SQUASH_LOOKBACK|apply|int|0,|plain|200|0 = 「已 squash 合并」的回看数为零|||workflow
105
+ TEAM_REVIEW_TIMEOUT|apply|seconds|0,|plain|1800|低于 60 秒 = 门禁会被超时掐死||600,1800,3600|workflow
106
+ TEAM_REVIEW_TIMEOUT_GRACE|apply|seconds|0,|plain|2|-|||workflow
107
+ TEAM_REVIEW_ALLOW_DIRTY|apply|bool||plain|0|1 = 复验放行脏工作树|||workflow
108
+ TEAM_REVIEW_ALLOW_IGNORED|apply|bool||plain|0|1 = 复验放行被忽略产物|||workflow
109
+ TEAM_REVIEW_ALLOW_UNRESOLVED_BRANCH|apply|bool||plain|0|1 = 给解析不到的分支盖章|||workflow
110
+ TEAM_REVIEW_ANY_DIR|apply|bool||plain|0|1 = 跳过 checkout HEAD == 分支 tip 守卫|||workflow
111
+ TEAM_BOARD_DONE_FORCE|apply|bool||plain|0|1 = 绕过 done 的证据闸门|||workflow
112
+ TEAM_BOARD_DONE_REASON|apply|text||plain||-|||workflow
113
+ # ---- 容量与投递通知(apply)----
114
+ TEAM_MIN_FREE_SWAP_MB|apply|mb|0,|plain|1024|0 = 磁盘 swap 底线关闭||512,1024,2048|delivery
115
+ TEAM_MIN_TOTAL_MB|apply|mb|0,|plain|512|0 = RAM+swap 绝对底线关闭||256,512,1024|delivery
116
+ TEAM_MIN_AVAIL_MB|apply|mb|0,|plain|1024|0 = MemAvailable 底线关闭||512,1024,2048|delivery
117
+ TEAM_TMP_MIN_FREE_MB|apply|mb|0,|plain|1024|0 = 临时根可用空间底线关闭||512,1024,2048|delivery
118
+ TEAM_TMP_MIN_FREE_INODES|apply|int|0,|plain|100000|0 = 临时根 inode 底线关闭||50000,100000,200000|delivery
119
+ TEAM_WARN_AVAIL_MB|apply|mb|0,|plain|2048|0 = 内存只警告的水位关闭||1024,2048,4096|delivery
120
+ TEAM_ZRAM_WARN_PCT|apply|pct|0,100|plain|85|-||70,80,85,90|delivery
121
+ TEAM_AGENT_MEM_MB|apply|mb|1,|plain|6144|-||2048,4096,6144|delivery
122
+ TEAM_NOTIFY_TMUX|apply|bool||plain|1|-|||delivery
123
+ TEAM_NOTIFY_DEDUP_SEC|apply|seconds|0,|plain|20|-|||delivery
124
+ TEAM_INBOX_MAX_CHARS|apply|int|1,|plain|150|-||100,150,300|delivery
125
+ TEAM_NOTIFY_LOG|apply|path|file,opt|plain|/tmp/teamsmith-notify.log|-|||delivery
126
+ TEAM_DEFER_TTL|apply|seconds|0,|plain|300|0 = 投递队列入队即过期||60,300,900|delivery
127
+ TEAM_OUTBOX_MAX|apply|int|0,|plain|200|0 = 队列不设上限(无界)||50,200,1000|delivery
128
+ TEAM_PANEL_DETAIL_CAP|apply|bytes|1024,|plain|131072|-||65536,131072,262144|delivery
129
+ # ---- 巡检与面板(restart:运行中的 pulse/面板拿着启动时的值)----
130
+ TEAM_PULSE_INTERVAL|restart|seconds|60,|plain|900|低于 60 秒 = 巡检转成忙等||300,900,1800,3600|panel
131
+ TEAM_MONITOR_REFRESH|restart|seconds|1,|plain|3|-||2,3,5|panel
132
+ TEAM_MONITOR_EVENTS|restart|int|1,|plain|4|-||2,4,8|panel
133
+ TEAM_MONITOR_UI|restart|enum|auto,tui,text|plain|auto|-|||panel
134
+ TEAM_MONITOR_ACTIVITY|restart|bool||export|1|-|||panel
135
+ TEAM_AGENT_LOG_TAIL_BYTES|restart|bytes|0,1048576|export||-|||panel
136
+ TEAM_PULSE_WINDOW|restart|text||plain|pulse|改的是运行中后端的窗口名:team pulse down(旧名)→ 改 → team pulse up|||panel
137
+ # ---- 巡检策略(apply:一拍一个 team watch --once 子进程)----
138
+ TEAM_PULSE_NUDGE_GAP|apply|seconds|0,|plain|900|-||300,900,1800|patrol
139
+ TEAM_PULSE_PENDING_BOARD|apply|bool||plain|0|-|||patrol
140
+ TEAM_PULSE_REBUILD_TMUX|apply|bool||plain|0|-|||patrol
141
+ TEAM_PULSE_MAX_RESTARTS|apply|int|0,|plain|5|0 = PM 崩溃后不再自动拉起|||patrol
142
+ # ---- PM 生命周期(restart:运行中的 PM 拿着旧参数,team up 用新的)----
143
+ TEAM_PM_SESSION_ID|restart|text||plain||-|||pm-lifecycle
144
+ TEAM_PM_CMD|restart|tpl|pm|plain||-|||pm-lifecycle
145
+ TEAM_PM_BIN|restart|path|exec,opt|plain||-|||pm-lifecycle
146
+ TEAM_PM_EXTRA_PI_ARGS|restart|cmd||plain||-|||pm-lifecycle
147
+ TEAM_PM_RESUME_ARGS|restart|cmd||plain||-|||pm-lifecycle
148
+ TEAM_PM_START_WAIT|apply|seconds|0,|plain|6|-|||pm-lifecycle
149
+ # ---- 活着的会话(restart:扩展在运行中的进程里读环境)----
150
+ TEAM_INBOX_WATCH_MAX_BYTES|restart|bytes|1024,|export|131072|-|||session
151
+ TEAM_INBOX_WATCH_PREVIEW|restart|int|16,|export|160|-|||session
152
+ TEAM_INBOX_WATCH_REPLAY_MAX|restart|int|1,|export|20|-|||session
153
+ TEAM_INBOX_WATCH_SEEN_MAX|restart|int|32,|export|512|-|||session
154
+ TEAM_INBOX_WATCH_STALE|restart|seconds|1,|export|300|-|||session
155
+ TEAM_INBOX_WATCH_STALE_SEC|restart|seconds|1,|export|900|-|||session
156
+ TEAM_INBOX_WATCH_POLL_MS|restart|int|100,|export|5000|-|||session
157
+ TEAM_INBOX_WATCH_HEARTBEAT_MS|restart|int|100,|export|5000|-|||session
158
+ TEAM_INBOX_WATCH_TARGET|restart|text||export||-|||session
159
+ TEAM_BG_LOG_MAX_BYTES|restart|bytes|1024,|export|524288|-|||session
160
+ # ---- 信号纪律(P159 · refuse:闸门接缝与宽限由手改契约控制)----
161
+ TEAM_SIGNAL_CALLS_LOG|refuse|path|file,opt|plain||-|P159 信号闸门:每次调用一行(拒绝另存 <log>.forensics,熬得过轮转);窗口启动时由闸门前缀钉值,要改就手改 .pi/team/config.sh||policy
162
+ TEAM_SIGNAL_REAL|refuse|path|exec,opt|plain||-|P159 信号闸门的真身路径(夹具钉桩用;拒绝不依赖它能否解析);手改 .pi/team/config.sh||policy
163
+ TEAM_BG_STOP_GRACE|refuse|seconds|0,|plain|5|-|P159 team bg stop 的宽限:组 TERM 之后等这么多秒再 KILL(0 = 不等待,立刻升级);手改 .pi/team/config.sh||policy
164
+ # ---- 跨项目会议(apply)----
165
+ TEAM_MEETING_TTL_HOURS|apply|int|1,|plain|72|-||24,72,168|meeting
166
+ TEAM_MEETING_MAX_TURNS|apply|int|1,|plain|20|-||5,20,50|meeting
167
+ TEAM_MEETING_KNOCK|apply|bool||plain|0|-|||meeting
168
+ TEAM_MEETING_ALLOW_USER_ID|apply|text||plain||非空 = 放开谁能敲门(扩大权限)|||meeting
169
+ EOF
170
+ }
171
+
172
+ # 契约文件定位:没有就点名 team init(绝不悄悄造一个)。
173
+ team_config_contract_path() {
174
+ local p="${TEAM_CONFIG:-}"
175
+ if [ -z "$p" ] && [ -n "${TEAM_CONFIG_FILE:-}" ]; then p="$TEAM_CONFIG_FILE"; fi
176
+ if [ -z "$p" ] && [ -n "${TEAM_MAIN_ROOT:-}" ] && [ -f "$TEAM_MAIN_ROOT/.pi/team/config.sh" ]; then
177
+ p="$TEAM_MAIN_ROOT/.pi/team/config.sh"
178
+ fi
179
+ if [ -z "$p" ] || [ ! -f "$p" ]; then
180
+ team_err "找不到项目契约 .pi/team/config.sh($p)"
181
+ team_dim " 这个项目还没初始化:先跑 $TEAM_CLI init(或 $TEAM_CLI bootstrap)" >&2
182
+ return 1
183
+ fi
184
+ printf '%s\n' "$p"
185
+ }
186
+
187
+ team_config_field() { # <row> <n:1..10> → 字段(第 9 列 suggest / 第 10 列 group 无默认;畸形行返回空串)
188
+ local row="$1" n="$2"
189
+ local -a f=()
190
+ IFS='|' read -r -a f <<< "$row"
191
+ printf '%s\n' "${f[$((n-1))]:-}"
192
+ }
193
+
194
+ team_config_row() { # <KEY> → schema 行(找不到返回 1)
195
+ local want="$1" row
196
+ while IFS= read -r row; do
197
+ case "$row" in \#*|'') continue ;; esac
198
+ [ "${row%%|*}" = "$want" ] && { printf '%s\n' "$row"; return 0; }
199
+ done < <(team_config_schema)
200
+ return 1
201
+ }
202
+
203
+ team_config_choices_set() { # <row> [known models: 每行一个] → CFG_CHOICES_JSON(读路径无子 shell)
204
+ # 唯一来源是 schema 行(加 models 块的 known):bool 的两个规范值、enum 的 constraints、数值类的
205
+ # suggest 列、模型类的 known。面板读这个对象画选择器,因此它绝不能是第二张选项表。
206
+ # empty = 写入者是否接受空值(与 team_config_validate_value 的 '' 判定同口径;走查夹具比对两者)。
207
+ # note = 该键域的解释(今天只有 path 类用它携带存在性检查的类型 file|dir|exec|any),无话可说时为空。
208
+ local row="${1-}" known="${2-}"
209
+ local -a f=()
210
+ IFS='|' read -r -a f <<< "$row"
211
+ local kind="${f[2]:-}" spec="${f[3]:-}" suggest="${f[8]:-}"
212
+ local src="none" min="" max="" empty="false" note="" val_list=""
213
+ case "$kind" in
214
+ bool)
215
+ src="schema"; val_list=$'1\n0' ;;
216
+ enum)
217
+ src="schema"; val_list="${spec//,/$'\n'}" ;;
218
+ int|seconds|mb|bytes|pct)
219
+ src="schema"
220
+ IFS=, read -r min max _ <<< "$spec"
221
+ val_list="${suggest//,/$'\n'}" ;;
222
+ model|pairlist|winlist|pattern)
223
+ src="known"; val_list="$known" ;;
224
+ path)
225
+ local typ="${spec%%,*}"; note="${typ:-any}" ;;
226
+ esac
227
+ case "$kind" in
228
+ bool|int|seconds|mb|bytes|pct) empty="false" ;;
229
+ enum) case ",$spec," in *",,"*) empty="true" ;; esac ;;
230
+ path) case "$spec" in *,opt) empty="true" ;; esac ;;
231
+ model) [ "$spec" = "opt" ] && empty="true" ;;
232
+ pairlist|pattern|winlist|tpl|list|cmd|text) empty="true" ;;
233
+ esac
234
+ local values_json="" v first=1
235
+ while IFS= read -r v; do
236
+ [ -n "$v" ] || continue
237
+ [ "$first" = "1" ] || values_json="$values_json,"
238
+ first=0
239
+ team_config_json_escape_set "$v"
240
+ values_json="$values_json\"$CFG_ESC\""
241
+ done <<< "$val_list"
242
+ team_config_json_escape_set "$min"; local min_json="$CFG_ESC"
243
+ team_config_json_escape_set "$max"; local max_json="$CFG_ESC"
244
+ team_config_json_escape_set "$note"; local note_json="$CFG_ESC"
245
+ CFG_CHOICES_JSON="{\"source\":\"$src\",\"values\":[$values_json],\"min\":\"$min_json\",\"max\":\"$max_json\",\"empty\":$empty,\"note\":\"$note_json\"}"
246
+ }
247
+
248
+ team_config_choices() { # <row> [known models] → 打印对象(兼容包装;读热路径用 _set,省一次 fork)
249
+ team_config_choices_set "${1-}" "${2-}"
250
+ printf '%s\n' "$CFG_CHOICES_JSON"
251
+ }
252
+ team_config_key_form() { # <KEY> → plain|export(未知键 = plain)
253
+ local row; row="$(team_config_row "$1" 2>/dev/null || true)"
254
+ [ -n "$row" ] || { printf 'plain\n'; return 0; }
255
+ local form; form="$(team_config_field "$row" 5)"
256
+ printf '%s\n' "${form:-plain}"
257
+ }
258
+
259
+ # notify 扩展用扁平 KEY="value" 读取器解析的八个键:值里出现 ' 就没有转义可用 → 拒写(设计 §4)。
260
+ TEAM_CONFIG_FLAT_KEYS=" TEAM_SESSION TEAM_PM_WINDOW TEAM_WORKTREES_DIR TEAM_DOCS_DIR TEAM_NOTIFY_TMUX TEAM_NOTIFY_DEDUP_SEC TEAM_INBOX_MAX_CHARS TEAM_NOTIFY_LOG "
261
+ team_config_flat_key() { # <KEY> → 0 = 扁平读取器的键
262
+ case "$TEAM_CONFIG_FLAT_KEYS" in *" $1 "*) return 0 ;; esac
263
+ return 1
264
+ }
265
+
266
+ # ---------------------------------------------------------------- 契约文件的小工具
267
+ team_config_trim() { team_trim "${1-}"; }
268
+
269
+ team_config_file_line() { # <file> <KEY> → 该键在文件里的整行(无 → 返回 1)
270
+ local f="$1" k="$2"
271
+ grep -E "^[[:space:]]*(export[[:space:]]+)?$k=" "$f" 2>/dev/null | head -1
272
+ }
273
+
274
+ team_config_file_value() { # <file> <KEY> → 文件里的字面值(去引号/行内注释;无 → 返回 1)
275
+ local f="$1" k="$2" line rest comment
276
+ line="$(team_config_file_line "$f" "$k")" || return 1
277
+ [ -n "$line" ] || return 1
278
+ rest="${line#*=}"
279
+ comment="$(printf '%s' "$line" | team_config_inline_comment_of_line)"
280
+ [ -n "$comment" ] && rest="${rest%"$comment"}"
281
+ rest="$(team_config_trim "$rest")"
282
+ case "$rest" in
283
+ \"*\") rest="${rest#\"}"; rest="${rest%\"}" ;;
284
+ \'*\') rest="${rest#\'}"; rest="${rest%\'}" ;;
285
+ esac
286
+ printf '%s\n' "$rest"
287
+ }
288
+
289
+ team_config_file_comment() { # <file> <KEY> → 行内注释(含 #;无 → 空)
290
+ local f="$1" k="$2" line
291
+ line="$(team_config_file_line "$f" "$k")" || return 0
292
+ [ -n "$line" ] || return 0
293
+ printf '%s' "$line" | team_config_inline_comment_of_line
294
+ }
295
+
296
+ team_config_fingerprint() { # <file> → sha256
297
+ local out
298
+ out="$(sha256sum "$1" 2>/dev/null | awk '{print $1}')" || true
299
+ if [ -z "$out" ]; then out="$(shasum -a 256 "$1" 2>/dev/null | awk '{print $1}')" || true; fi
300
+ printf '%s\n' "$out"
301
+ }
302
+
303
+ team_config_mtime() { # <file> → UTC ISO-8601
304
+ date -u -r "$1" +%Y-%m-%dT%H:%M:%SZ 2>/dev/null \
305
+ || stat -c %y "$1" 2>/dev/null | cut -c1-19 | tr ' ' 'T' \
306
+ || printf '\n'
307
+ }
308
+
309
+ team_config_json_escape() { # JSON 字符串转义(含控制字符)
310
+ team_config_json_escape_set "${1-}"
311
+ printf '%s' "$CFG_ESC"
312
+ }
313
+
314
+ # 读路径的无子 shell 版本(M65/Q3):`team config list --json` 每个键、每个字段一次 `$(...)`
315
+ # 就是一次 fork —— 108 键的契约里命令替换是扫描之外的主要残余成本。结果落在 CFG_ESC。
316
+ team_config_json_escape_set() { # <text> → CFG_ESC
317
+ local v="${1-}"
318
+ v="${v//\\/\\\\}"
319
+ v="${v//\"/\\\"}"
320
+ v="${v//$'\t'/\\t}"
321
+ v="${v//$'\r'/\\r}"
322
+ v="${v//$'\n'/\\n}"
323
+ CFG_ESC="$v"
324
+ }
325
+
326
+ # ---------------------------------------------------------------- 值的校验(唯一校验器)
327
+ # team_config_validate_value <KEY> <value> → 0 合法;非 0 时把「接受域」打进 stdout(调用方转 exit 4)
328
+ team_config_validate_value() {
329
+ local key="$1" val="${2-}" row class kind spec form
330
+ row="$(team_config_row "$key" 2>/dev/null || true)"
331
+ if [ -z "$row" ]; then
332
+ printf '不是已知的项目设置键(references/config.md 是清单);自定义键请手改 .pi/team/config.sh\n'
333
+ return 1
334
+ fi
335
+ class="$(team_config_field "$row" 2)"; kind="$(team_config_field "$row" 3)"
336
+ spec="$(team_config_field "$row" 4)"; form="$(team_config_field "$row" 5)"
337
+ if [ "$class" = "refuse" ]; then
338
+ printf '只读键(%s)\n' "$(team_config_field "$row" 8)"
339
+ return 1
340
+ fi
341
+ case "$val" in
342
+ *$'\n'*) printf '值不能含换行(契约是单行 KEY=value)\n'; return 1 ;;
343
+ *'#'*) printf '值不能含 #(notify 扩展的扁平读取器会从这里截断)\n'; return 1 ;;
344
+ esac
345
+ if team_config_flat_key "$key" && case "$val" in *"'"*) true ;; *) false ;; esac; then
346
+ printf '%s 由 notify 扩展的扁平读取器解析,值里不能有单引号(那个读取器没有转义)\n' "$key"
347
+ return 1
348
+ fi
349
+ local min max n
350
+ case "$kind" in
351
+ bool)
352
+ case "$val" in
353
+ 0|1|true|false|yes|no|on|off) return 0 ;;
354
+ *) printf '只接受 0/1(也认 true/false/yes/no/on/off)\n'; return 1 ;;
355
+ esac ;;
356
+ int|seconds|mb|bytes|pct)
357
+ case "$val" in
358
+ ''|*[!0-9]*) printf '只接受非负整数\n'; return 1 ;;
359
+ esac
360
+ IFS=, read -r min max <<< "$spec"
361
+ if [ -n "$min" ] && [ "$val" -lt "$min" ]; then
362
+ printf '超出范围:最小 %s%s\n' "$min" "$([ -n "$max" ] && printf -- ',最大 %s' "$max")"; return 1
363
+ fi
364
+ if [ -n "$max" ] && [ "$val" -gt "$max" ]; then
365
+ printf '超出范围:最小 %s,最大 %s\n' "${min:-0}" "$max"; return 1
366
+ fi
367
+ return 0 ;;
368
+ enum)
369
+ case ",$spec," in *",$val,"*) return 0 ;; esac
370
+ printf '只接受:%s\n' "$(printf '%s' "$spec" | tr ',' '|')"; return 1 ;;
371
+ path)
372
+ local typ="${spec%%,*}" opt=0
373
+ case "$spec" in *,opt) opt=1 ;; esac
374
+ [ -n "$val" ] && return 0
375
+ if [ "$opt" = "1" ]; then return 0; fi
376
+ printf '路径不能为空\n'; return 1 ;;
377
+ model)
378
+ local optm=0; [ "$spec" = "opt" ] && optm=1
379
+ [ -z "$val" ] && { [ "$optm" = "1" ] && return 0; printf '必须形如 provider/model\n'; return 1; }
380
+ local mwhy
381
+ if mwhy="$(team_config_model_violation "$val")"; then return 0; fi
382
+ printf '%s\n' "$mwhy"; return 1 ;;
383
+ pairlist)
384
+ local tok seat model r mwhy; r=0
385
+ for tok in $val; do
386
+ case "$tok" in
387
+ *=*) seat="${tok%%=*}"; model="${tok#*=}" ;;
388
+ *) printf '只接受 seat=provider/model 空格分隔(%s 没有 =)\n' "$tok"; return 1 ;;
389
+ esac
390
+ if ! team_config_seat_known "$seat"; then
391
+ printf '席位 %s 不在名册(%s)也不叫 pm\n' "$seat" "$(team_config_roster_text)"; return 1
392
+ fi
393
+ local mwhy
394
+ if ! mwhy="$(team_config_model_violation "$model")"; then
395
+ printf '席位 %s 的模型:%s\n' "$seat" "$mwhy"; return 1
396
+ fi
397
+ done
398
+ return 0 ;;
399
+ pattern)
400
+ local t p num
401
+ for t in $val; do
402
+ p="${t%%=*}"
403
+ if [ "$p" = "$t" ] || [ -z "$p" ]; then printf '只接受 pattern=N 空格分隔(%s 没有 =)\n' "$t"; return 1; fi
404
+ num="${t#*=}"
405
+ case "$num" in ''|*[!0-9]*) printf '只接受 pattern=N(N 为非负整数,%s)\n' "$t"; return 1 ;; esac
406
+ done
407
+ return 0 ;;
408
+ winlist)
409
+ local t p num
410
+ for t in $val; do
411
+ p="${t%%=*}"
412
+ if [ "$p" = "$t" ] || [ -z "$p" ]; then printf '只接受 model=N 空格分隔(%s 没有 =)\n' "$t"; return 1; fi
413
+ num="${t#*=}"
414
+ case "$num" in ''|*[!0-9]*) printf '只接受 model=N(N 为正整数,%s)\n' "$t"; return 1 ;; esac
415
+ done
416
+ return 0 ;;
417
+ tpl)
418
+ local kind_name="$2"
419
+ case "$spec" in launch|notify|pm) kind_name="$spec" ;; esac
420
+ [ -z "$val" ] && return 0
421
+ if [ -z "$(team_trim "$val")" ]; then
422
+ printf '只有空白(配了等于没配):要么留空(走内置 Pi),要么写一条真正的命令\n'; return 1
423
+ fi
424
+ case "$val" in *$'\n'*) printf '模板必须是**一条**命令行(第二行会被窗口 shell 当新命令执行)\n'; return 1 ;; esac
425
+ local bad; bad="$(team_agent_bogus_tokens "$kind_name" "$val" 2>/dev/null || true)"
426
+ if [ -n "$bad" ]; then
427
+ printf '有未知占位符:%s(支持:%s)\n' "$(printf '%s' "$bad" | tr '\n' ' ')" "$(team_agent_support_list "$kind_name")"
428
+ return 1
429
+ fi
430
+ return 0 ;;
431
+ list)
432
+ # 名册(唯一 list 类键)的席位名规则:token 会成为 .worktrees/ 目录名、tmux 窗口名、state 文件名
433
+ # 与分支名 —— 形状不是装饰。读(team config list --json 的 warning)与写共用这一个判定,
434
+ # 不许长第二份(P99/R1)。合法时返回 0;不合法时把原因打进 stdout(调用方转 exit 4)。
435
+ local lwhy
436
+ if lwhy="$(team_config_list_violation "$val")"; then return 0; fi
437
+ printf '%s\n' "$lwhy"; return 1 ;;
438
+ cmd|text)
439
+ return 0 ;;
440
+ *)
441
+ printf '内部错误:未知 kind %s\n' "$kind"; return 1 ;;
442
+ esac
443
+ }
444
+
445
+ # 值的规范化(写进文件的形式):bool → 1/0,其余原样。
446
+ team_config_canonical_value() { # <KEY> <value> → 规范值
447
+ local key="$1" val="${2-}" row kind
448
+ row="$(team_config_row "$key" 2>/dev/null || true)"
449
+ kind="$(team_config_field "$row" 3)"
450
+ if [ "$kind" = "bool" ]; then
451
+ case "$val" in
452
+ 1|true|yes|on) printf '1\n'; return 0 ;;
453
+ 0|false|no|off) printf '0\n'; return 0 ;;
454
+ esac
455
+ fi
456
+ printf '%s\n' "$val"
457
+ }
458
+
459
+ # 模型值形状的**一处判定**(change: model-id-shape R1):provider = 第一个 `/` 之前(非空、不含 `:`),
460
+ # model = 之后全部(先剥第一个 `:` 起的思考档后缀再判:body 非空、每段非空),整体不含空白。
461
+ # 合法 → 返回 0、无输出;不合法 → 返回 1、stdout 是**点名段落**(provider/model)的原因。
462
+ # model kind、pairlist token、set-agent-model 与 add-agent --model 全走这里,不许长第二份。
463
+ team_config_model_violation() { # <model> → 0 合法;1 时 stdout 是原因
464
+ local val="${1-}" prov body
465
+ case "$val" in
466
+ */*) ;;
467
+ *) printf 'provider 缺失:需要 provider/model 形状(没有 /)\n'; return 1 ;;
468
+ esac
469
+ prov="${val%%/*}"; body="${val#*/}"
470
+ case "$val" in *[[:space:]]*)
471
+ case "$prov" in *[[:space:]]*) printf 'provider 含空白(空格/制表符/换行)\n'; return 1 ;; esac
472
+ printf 'model 含空白(空格/制表符/换行)\n'; return 1 ;; esac
473
+ [ -n "$prov" ] || { printf 'provider 为空(/ 前没有内容)\n'; return 1; }
474
+ case "$prov" in *:*) printf 'provider 不能含 :(:思考档 后缀只属于 model 段)\n'; return 1 ;; esac
475
+ body="${body%%:*}" # 后缀先剥再判形状;调用方写盘的值仍保留后缀
476
+ [ -n "$body" ] || { printf 'model 为空(/ 后没有内容,或 :思考档 后缀前为空)\n'; return 1; }
477
+ case "/$body/" in
478
+ *//*) printf 'model 有空段(/ 分隔的每一段都必须非空)\n'; return 1 ;;
479
+ esac
480
+ return 0
481
+ }
482
+
483
+ # 布尔包装(面板与旧调用方问「行不行」):判定本体在 team_config_model_violation。
484
+ team_config_model_shape_ok() { # <model> → 0 合法 / 1 不合法
485
+ team_config_model_violation "${1-}" >/dev/null 2>&1
486
+ }
487
+
488
+ # 名册值规则(唯一一份):空格分隔的席位名,每个匹配 [A-Za-z0-9][A-Za-z0-9._-]*、只出现一次、
489
+ # 且不是 pm(PM 席位不是名册成员)。合法 → 返回 0;不合法 → 打印原因(点名 token 与接受形状)返回 1。
490
+ team_config_list_violation() { # <value> → 0 合法;1 时 stdout 是原因
491
+ local val="${1-}" tok shape='[A-Za-z0-9][A-Za-z0-9._-]*' seen=" "
492
+ for tok in $val; do
493
+ if [ "$tok" = "pm" ]; then
494
+ printf '席位名 pm 非法:pm 是 PM 席位,不是名册成员(接受 %s)\n' "$shape"; return 1
495
+ fi
496
+ case "$tok" in
497
+ [!A-Za-z0-9]*) printf '席位名 %s 非法:接受 %s(首字符必须是字母或数字)\n' "$tok" "$shape"; return 1 ;;
498
+ esac
499
+ case "${tok#?}" in
500
+ *[!A-Za-z0-9._-]*) printf '席位名 %s 非法:接受 %s\n' "$tok" "$shape"; return 1 ;;
501
+ esac
502
+ case "$seen" in *" $tok "*) printf '席位名 %s 重复出现:名册里每个席位只能出现一次\n' "$tok"; return 1 ;; esac
503
+ seen="$seen$tok "
504
+ done
505
+ return 0
506
+ }
507
+
508
+ # 席位名(**单个** token)的规则:名册值规则管的是**结果值**,而写入器把席位名当一个 token 追加 ——
509
+ # `api 1` 加进去会变成两个各自合法的 token,值规则不会响(P105/F1 实测:名册被写脏 + 一行 result=ok,
510
+ # 命令随后才在 worktree 步骤以「未知 agent」失败)。所以 add 路径在任何写入之前先在这一层拒:空、
511
+ # 含空白(空格/制表符/换行)、以及单 token 上跑列表规则的一切(形状、pm、斜杠)—— 一条规则,读写同源。
512
+ # 合法 → 0;不合法 → stdout 是原因,返回 1。remove 路径不查:移掉手改出来的非法 token 是合法的。
513
+ team_config_seat_violation() { # <seat> → 0 合法;1 时 stdout 是原因
514
+ local seat="${1-}" why shape='[A-Za-z0-9][A-Za-z0-9._-]*'
515
+ case "$seat" in
516
+ '') printf '席位名不能为空\n'; return 1 ;;
517
+ *[[:space:]]*)
518
+ printf '席位名 %s 非法:席位名是一个 token,不能含空白(空格/制表符);接受 %s\n' "$seat" "$shape"; return 1 ;;
519
+ esac
520
+ if ! why="$(team_config_list_violation "$seat")"; then printf '%s\n' "$why"; return 1; fi
521
+ return 0
522
+ }
523
+
524
+ # danger:合法的值也会关掉已发布的守卫(设计 §5)。命中 → stdout 一行理由,返回 1。
525
+ team_config_danger_reason() { # <KEY> <value>
526
+ local key="$1" val="${2-}"
527
+ case "$key" in
528
+ TEAM_MIN_FREE_SWAP_MB|TEAM_MIN_TOTAL_MB|TEAM_MIN_AVAIL_MB|TEAM_WARN_AVAIL_MB|TEAM_TMP_MIN_FREE_MB|TEAM_TMP_MIN_FREE_INODES)
529
+ [ "$val" = "0" ] && { printf '容量底线归零(守卫失效)\n'; return 1; } ;;
530
+ TEAM_SQUASH_LOOKBACK)
531
+ [ "$val" = "0" ] && { printf '「已 squash 合并」判定关闭\n'; return 1; } ;;
532
+ TEAM_REVIEW_TIMEOUT)
533
+ case "$val" in ''|*[!0-9]*) ;; *) [ "$val" -lt 60 ] && { printf '门禁超时低于 60 秒(正常门禁会被掐死)\n'; return 1; } ;; esac ;;
534
+ TEAM_PULSE_INTERVAL)
535
+ case "$val" in ''|*[!0-9]*) ;; *) [ "$val" -lt 60 ] && { printf '巡检周期低于 60 秒(转成忙等)\n'; return 1; } ;; esac ;;
536
+ TEAM_PULSE_MAX_RESTARTS)
537
+ [ "$val" = "0" ] && { printf 'PM 崩溃后不再自动拉起\n'; return 1; } ;;
538
+ TEAM_DEFER_TTL|TEAM_OUTBOX_MAX)
539
+ [ "$val" = "0" ] && { printf '投递队列的守卫关闭\n'; return 1; } ;;
540
+ TEAM_REVIEW_ALLOW_DIRTY|TEAM_REVIEW_ALLOW_IGNORED|TEAM_REVIEW_ALLOW_UNRESOLVED_BRANCH|TEAM_REVIEW_ANY_DIR)
541
+ [ "$val" = "1" ] && { printf '复验守卫的旁路被打开\n'; return 1; } ;;
542
+ TEAM_BOARD_DONE_FORCE)
543
+ [ "$val" = "1" ] && { printf 'done 的证据闸门被绕过\n'; return 1; } ;;
544
+ TEAM_MEETING_ALLOW_USER_ID)
545
+ [ -n "$val" ] && { printf '放开了一个扩权入口(谁能敲门)\n'; return 1; } ;;
546
+ TEAM_PULSE_WINDOW)
547
+ local old; old="$(team_config_file_value "$(team_config_contract_path 2>/dev/null || true)" "$key" 2>/dev/null || true)"
548
+ [ -z "$old" ] && old="$(team_config_field "$(team_config_row "$key")" 6)"
549
+ if [ "$val" != "$old" ] && [ -n "${TEAM_SESSION:-}" ] && team_have_cmd tmux \
550
+ && tmux has-session -t "$TEAM_SESSION" 2>/dev/null; then
551
+ printf '运行中的 pulse 后端还在旧窗口 %s:先 team pulse down 再改,然后 team pulse up\n' "$old"
552
+ return 1
553
+ fi ;;
554
+ esac
555
+ return 0
556
+ }
557
+
558
+ # ---------------------------------------------------------------- 审计(state/config.log)
559
+ team_config_audit_file() { printf '%s\n' "${TEAM_STATE_DIR:-.pi/team/state}/config.log"; }
560
+
561
+ team_config_audit_quote() { # 单引号形态,保证一行
562
+ local v="${1-}"
563
+ v="$(printf '%s' "$v" | sed -e "s/'/'\\\\''/g")"
564
+ printf '%s' "$v"
565
+ }
566
+
567
+ team_config_audit_write() { # <result> <actor> <key> <old> <new> [expected] [actual]
568
+ local result="$1" actor="$2" key="$3" old="$4" new="$5" expected="${6:-}" actual="${7:-}"
569
+ local f; f="$(team_config_audit_file)"
570
+ mkdir -p "$(dirname "$f")" 2>/dev/null || true
571
+ local line
572
+ line="$(date -u +%Y-%m-%dT%H:%M:%SZ) result=$result actor=$(team_config_audit_quote "$actor") key=$key old='$(team_config_audit_quote "$old")' new='$(team_config_audit_quote "$new")'"
573
+ if [ -n "$expected" ] || [ -n "$actual" ]; then
574
+ line="$line expected=${expected:-} actual=${actual:-}"
575
+ fi
576
+ if ! printf '%s\n' "$line" >> "$f" 2>/dev/null; then
577
+ team_warn "审计写不进 $f:本次写入结果照旧报告(缺审计行不等于写失败)"
578
+ return 1
579
+ fi
580
+ return 0
581
+ }
582
+
583
+ # 最多读最后 16 KiB,打印最新 N 行(时间顺序:旧的在前)。
584
+ team_config_audit_tail() { # [N=10]
585
+ local n="${1:-10}" f; f="$(team_config_audit_file)"
586
+ [ -f "$f" ] || return 0
587
+ tail -c 16384 "$f" 2>/dev/null | tail -n "$n" 2>/dev/null
588
+ }
589
+
590
+ # ---------------------------------------------------------------- 席位(models 块)
591
+ team_config_roster_text() { team_agents | tr '\n' ' ' | sed -e 's/[[:space:]]*$//'; }
592
+
593
+ team_config_seat_known() { # <seat> → 0 = 名册席位或 pm
594
+ local seat="$1"
595
+ [ "$seat" = "pm" ] && return 0
596
+ team_agent_known "$seat"
597
+ }
598
+
599
+ # 已记录但名册不再承认的席位(typo dev4=… 不得静默失效)
600
+ team_config_pairlist_unknown_seats() { # <value> → 每行一个未知席位
601
+ local tok seat
602
+ for tok in ${1-}; do
603
+ case "$tok" in
604
+ *=*) seat="${tok%%=*}" ;;
605
+ *) seat="$tok" ;;
606
+ esac
607
+ team_config_seat_known "$seat" || printf '%s\n' "$seat"
608
+ done
609
+ }
610
+
611
+ # 席位行的字段安全拆读:`IFS=$'\t' read` 会把**前导空字段**当分隔空白吃掉 —— 空模型(`dev=` 且回退
612
+ # 也为空,或 pm 席位无模型)时 read 会把 source 标签读成 model、把 override 读成 source,最后吐出一个
613
+ # 无值字段(`"override":}`)让整个文档失解。这里按字节切,空字段原样保留。
614
+ team_config_seat_split() { # <model<TAB>source<TAB>override> <var_model> <var_src> <var_override>
615
+ local line="$1" rest
616
+ case "$line" in *$'\t'*) rest="${line#*$'\t'}" ;; *) rest="" ;; esac
617
+ printf -v "$4" '%s' "${rest#*$'\t'}"
618
+ printf -v "$3" '%s' "${rest%%$'\t'*}"
619
+ printf -v "$2" '%s' "${line%%$'\t'*}"
620
+ }
621
+
622
+ # 席位显示口径与 team ps 同源:有记录用记录,没记录用配置解析;来源 = team_agent_model_src。
623
+ team_config_seat_state() { # <seat> → model<TAB>source<TAB>override
624
+ local seat="$1" model src override="false"
625
+ if [ "$seat" = "pm" ]; then
626
+ local pmv; pmv="$(team_config_file_value "$(team_config_contract_path 2>/dev/null || true)" TEAM_PM_MODEL 2>/dev/null || true)"
627
+ [ -n "$pmv" ] && override="true"
628
+ # P58/F1:模型与 PM 的启动解析**同源**(team_pm_model_resolve == team_pm_pi_args 用的那一行):
629
+ # 空 TEAM_DEFAULT_MODEL 时启动回退到 schema 默认,席位行也必须回退 —— 否则它会在空默认角落说 "",
630
+ # 与「实际会用的模型」分叉(P54 的 F1)。
631
+ model="$(team_pm_model_resolve)"
632
+ src="$(team_agent_model_src pm 2>/dev/null || printf '配置')"
633
+ printf '%s\t%s\t%s\n' "${model:-}" "$src" "$override"
634
+ return 0
635
+ fi
636
+ if [[ " ${TEAM_AGENT_MODELS:-} " == *" $seat="* ]]; then override="true"; fi
637
+ model="$(team_state_get "$seat" model "$(team_agent_model "$seat")")"
638
+ src="$(team_agent_model_src "$seat")"
639
+ printf '%s\t%s\t%s\n' "$model" "$src" "$override"
640
+ }
641
+
642
+ team_config_seat_source_token() { # <中文标签> → config|explicit|record
643
+ case "$1" in
644
+ 显式) printf 'explicit\n' ;;
645
+ 历史记录) printf 'record\n' ;;
646
+ *) printf 'config\n' ;;
647
+ esac
648
+ }
649
+
650
+ # ---------------------------------------------------------------- list
651
+ # ---------------------------------------------------------------- 读路径:契约的一次性扫描(M65/D11 · M2)
652
+ # `team config list --json` 的成本原来全在 fan-out:每个已知键两次 `grep|head`+`awk`(值 + 注释),
653
+ # 未知键的扫描再**每文件行** spawn 一个 `sed`。108 键的契约实测 ~600 个子进程、~3.5 s,而面板每次"
654
+ # 进视图/开行/选项"都同步调它。这里一次 awk 过契约,得到「赋值行的出现顺序」+「每个键第一次出现的
655
+ # 值/行内注释」;下面的两个循环只在 bash 里查表。语义逐字对齐 team_config_file_line/_value/_comment:
656
+ # 同名取第一行、引号内的 # 不是注释(双引号里的反斜杠转义也照旧)、值去首尾空白与一层引号、
657
+ # 注释含 # 前的空白并原样保留。
658
+ CFG_SCAN_ORDER=""
659
+ declare -gA CFG_SCAN_VALUE=()
660
+ declare -gA CFG_SCAN_COMMENT=()
661
+ team_config_scan() { # <file> → 填 CFG_SCAN_*(一次 awk;值/注释取首次出现,顺序含重复行)
662
+ local f="$1" k v c
663
+ CFG_SCAN_ORDER=""
664
+ CFG_SCAN_VALUE=()
665
+ CFG_SCAN_COMMENT=()
666
+ while IFS=$'\x1f' read -r k v c; do
667
+ [ -n "$k" ] || continue
668
+ CFG_SCAN_ORDER="$CFG_SCAN_ORDER$k"$'\n'
669
+ if [ -z "${CFG_SCAN_VALUE[$k]+x}" ]; then
670
+ CFG_SCAN_VALUE["$k"]="$v"
671
+ CFG_SCAN_COMMENT["$k"]="$c"
672
+ fi
673
+ done < <(awk '
674
+ /^[[:space:]]*(export[[:space:]]+)?[A-Za-z_][A-Za-z0-9_]*=/ {
675
+ line = $0
676
+ p = index(line, "=")
677
+ head = substr(line, 1, p - 1)
678
+ gsub(/^[[:space:]]*/, "", head)
679
+ sub(/^export[[:space:]]+/, "", head)
680
+ gsub(/[[:space:]]+$/, "", head)
681
+ s = substr(line, p + 1)
682
+ n = length(s); q = 0; i = 1; comment = ""
683
+ while (i <= n) {
684
+ ch = substr(s, i, 1)
685
+ if (q == 0) {
686
+ if (ch == "\"") q = 2
687
+ else if (ch == sprintf("%c", 39)) q = 1
688
+ else if (ch == "#") {
689
+ g = i; while (g > 1 && (substr(s, g-1, 1) == " " || substr(s, g-1, 1) == "\t")) g--
690
+ comment = substr(s, g); s = substr(s, 1, g - 1); break
691
+ }
692
+ } else if (q == 1) { if (ch == sprintf("%c", 39)) q = 0 }
693
+ else if (q == 2) { if (ch == "\\") i++; else if (ch == "\"") q = 0 }
694
+ i++
695
+ }
696
+ gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
697
+ if (s ~ /^".*"$/ || s ~ /^\047.*\047$/) s = substr(s, 2, length(s) - 2)
698
+ printf "%s%c%s%c%s\n", head, 31, s, 31, comment
699
+ }' "$f")
700
+ }
701
+
702
+ # P30/R1:每条记录带 `group`——schema 行的第 10 列逐字(畸形行也照实输出空串,绝不造默认组);
703
+ # 文件里 schema 不认识的键 group 为空串。字段是加法的:既有字段名/类型/顺序与 human 表、其它机器出口都不动。
704
+ team_config_list_json() {
705
+ local path; path="$(team_config_contract_path)" || return 1
706
+ local fingerprint mtime
707
+ fingerprint="$(team_config_fingerprint "$path")"
708
+ mtime="$(team_config_mtime "$path")"
709
+
710
+ # M65/D11(M2 的修法):一次扫描 + 纯 bash 查表与拼 JSON。命令替换 `$(...)` 也是一次 fork,
711
+ # 所以读路径用 *_set 写变量的孪生函数(json escape / choices),键行只用一次 `read -a` 拆。
712
+ local out="" first=1 row key class kind form def
713
+ local value comment set warning
714
+ team_config_scan "$path"
715
+ # models 块的 known 先算:它也是模型类键的 choices 词表(choices 必须随键记录一起输出,所以
716
+ # 不能等键循环结束)。token 与顺序和原来逐字一致;每个席位的**显示**模型也在表里(R2)。
717
+ local default_model known_models="" known_list="" seen=" " firstk=1
718
+ default_model="$(team_config_file_value "$path" TEAM_DEFAULT_MODEL 2>/dev/null || true)"
719
+ [ -n "$default_model" ] || default_model="$(team_config_field "$(team_config_row TEAM_DEFAULT_MODEL)" 6)"
720
+ local tok
721
+ for tok in $default_model ${TEAM_AGENT_MODELS:-}; do
722
+ case "$tok" in
723
+ *=*) tok="${tok#*=}" ;;
724
+ esac
725
+ [ -n "$tok" ] || continue
726
+ case "$seen" in *" $tok "*) continue ;; esac
727
+ seen="$seen$tok "
728
+ known_models="$known_models$tok"$'\n'
729
+ [ "$firstk" = "1" ] || known_list="$known_list,"
730
+ firstk=0
731
+ team_config_json_escape_set "$tok"; known_list="$known_list\"$CFG_ESC\""
732
+ done
733
+ local a state_model
734
+ for a in $(team_agents); do
735
+ state_model="$(team_state_get "$a" model '' 2>/dev/null || true)"
736
+ [ -n "$state_model" ] || continue
737
+ case "$seen" in *" $state_model "*) continue ;; esac
738
+ seen="$seen$state_model "
739
+ known_models="$known_models$state_model"$'\n'
740
+ [ "$firstk" = "1" ] || known_list="$known_list,"
741
+ firstk=0
742
+ team_config_json_escape_set "$state_model"; known_list="$known_list\"$CFG_ESC\""
743
+ done
744
+ # R2:每个席位的显示模型都在词汇表里。名册席位由上面的解析/记录两支覆盖;pm 席位没有 state
745
+ # 记录(team_pm_start 不写),它的显示模型走 seats 块同一支 team_config_seat_state
746
+ # (TEAM_PM_MODEL > TEAM_DEFAULT_MODEL)—— 视图的选项与 seats 块不会各说各话。
747
+ local seat_model _src _override
748
+ for a in $(team_agents) pm; do
749
+ team_config_seat_split "$(team_config_seat_state "$a")" seat_model _src _override
750
+ [ -n "$seat_model" ] || continue
751
+ case "$seen" in *" $seat_model "*) continue ;; esac
752
+ seen="$seen$seat_model "
753
+ known_models="$known_models$seat_model"$'\n'
754
+ [ "$firstk" = "1" ] || known_list="$known_list,"
755
+ firstk=0
756
+ team_config_json_escape_set "$seat_model"; known_list="$known_list\"$CFG_ESC\""
757
+ done
758
+
759
+ local schema_keys="|"
760
+ local -a f=()
761
+ while IFS= read -r row; do
762
+ case "$row" in \#*|'') continue ;; esac
763
+ f=()
764
+ IFS='|' read -r -a f <<< "$row"
765
+ key="${f[0]:-}"; class="${f[1]:-}"; kind="${f[2]:-}"; form="${f[4]:-}"; def="${f[5]:-}"
766
+ schema_keys="$schema_keys$key|"
767
+ value=""; set="false"; comment=""; warning=""
768
+ if [ -n "${CFG_SCAN_VALUE[$key]+x}" ]; then
769
+ value="${CFG_SCAN_VALUE[$key]}"
770
+ comment="${CFG_SCAN_COMMENT[$key]}"
771
+ set="true"
772
+ fi
773
+ if [ "$key" = "TEAM_AGENT_MODELS" ] && [ -n "$value" ]; then
774
+ local unknown; unknown="$(team_config_pairlist_unknown_seats "$value" | tr '\n' ' ')"
775
+ [ -n "$unknown" ] && warning="未知席位(名册没有,静默不生效):${unknown% }"
776
+ fi
777
+ # P99/R1:名册的读侧 warning 与写侧的值规则是同一份判定(team_config_list_violation)——
778
+ # 手改出一个非法 token 时,读给出警告、写入器拒绝的是同一条规则。
779
+ if [ "$key" = "TEAM_AGENTS" ] && [ -n "$value" ]; then
780
+ local rwhy
781
+ if ! rwhy="$(team_config_list_violation "$value")"; then warning="$rwhy"; fi
782
+ fi
783
+ team_config_choices_set "$row" "$known_models"
784
+ team_config_json_escape_set "${f[9]:-}"; local e_group="$CFG_ESC"
785
+ team_config_json_escape_set "$key"; local e_key="$CFG_ESC"
786
+ team_config_json_escape_set "$value"; local e_value="$CFG_ESC"
787
+ team_config_json_escape_set "$def"; local e_def="$CFG_ESC"
788
+ team_config_json_escape_set "$comment"; local e_comment="$CFG_ESC"
789
+ team_config_json_escape_set "$warning"; local e_warning="$CFG_ESC"
790
+ team_config_json_escape_set "${f[7]:-}"; local e_route="$CFG_ESC"
791
+ [ "$first" = "1" ] || out="$out,"
792
+ first=0
793
+ out="$out{\"name\":\"$e_key\",\"class\":\"$class\",\"kind\":\"$kind\",\"form\":\"$form\",\"value\":\"$e_value\",\"default\":\"$e_def\",\"set\":$set,\"comment\":\"$e_comment\",\"warning\":\"$e_warning\",\"route\":\"$e_route\",\"choices\":$CFG_CHOICES_JSON,\"group\":\"$e_group\",\"known\":true}"
794
+ done < <(team_config_schema)
795
+
796
+ # 文件里 schema 不认识的键:照实列出(面板只读展示「不是已知项目设置」),文件顺序、重复行各一条
797
+ local k v c
798
+ while IFS= read -r k; do
799
+ [ -n "$k" ] || continue
800
+ case "$schema_keys" in *"|$k|"*) continue ;; esac
801
+ v="${CFG_SCAN_VALUE[$k]-}"
802
+ c="${CFG_SCAN_COMMENT[$k]-}"
803
+ team_config_json_escape_set "$k"; local u_key="$CFG_ESC"
804
+ team_config_json_escape_set "$v"; local u_val="$CFG_ESC"
805
+ team_config_json_escape_set "$c"; local u_com="$CFG_ESC"
806
+ team_config_choices_set ""
807
+ out="$out,{\"name\":\"$u_key\",\"class\":\"refuse\",\"kind\":\"text\",\"form\":\"plain\",\"value\":\"$u_val\",\"default\":\"\",\"set\":true,\"comment\":\"$u_com\",\"warning\":\"不是已知的项目设置(见 references/config.md);手改 .pi/team/config.sh\",\"choices\":$CFG_CHOICES_JSON,\"group\":\"\",\"known\":false}"
808
+ done <<< "$CFG_SCAN_ORDER"
809
+
810
+ local seats="" firsts=1
811
+ for a in $(team_agents) pm; do
812
+ team_config_seat_split "$(team_config_seat_state "$a")" model src override
813
+ case "$override" in true) ;; *) override="false" ;; esac # override 永远是 JSON 布尔值(P47/R5)
814
+ # 空模型也要出这一行(seats 覆盖每个名册席位 + pm):空模型序列化成 "",不是无值字段也不是丢行。
815
+ team_config_json_escape_set "$a"; local s_agent="$CFG_ESC"
816
+ team_config_json_escape_set "$model"; local s_model="$CFG_ESC"
817
+ [ "$firsts" = "1" ] || seats="$seats,"
818
+ firsts=0
819
+ seats="$seats{\"agent\":\"$s_agent\",\"model\":\"$s_model\",\"source\":\"$(team_config_seat_source_token "$src")\",\"override\":$override}"
820
+ done
821
+
822
+ local audit="" firsta=1 l
823
+ while IFS= read -r l; do
824
+ [ -n "$l" ] || continue
825
+ [ "$firsta" = "1" ] || audit="$audit,"
826
+ firsta=0
827
+ team_config_json_escape_set "$l"; audit="$audit\"$CFG_ESC\""
828
+ done < <(team_config_audit_tail 10)
829
+
830
+ team_config_json_escape_set "$path"; local e_path="$CFG_ESC"
831
+ team_config_json_escape_set "$default_model"; local e_default="$CFG_ESC"
832
+ printf '{"path":"%s","fingerprint":"%s","mtime":"%s","keys":[%s],"models":{"default":"%s","known":[%s],"seats":[%s]},"audit":[%s]}\n' \
833
+ "$e_path" "$fingerprint" "$mtime" "$out" \
834
+ "$e_default" "$known_list" "$seats" "$audit"
835
+ }
836
+
837
+ team_cmd_config_list() {
838
+ local json=0
839
+ while [ $# -gt 0 ]; do
840
+ case "$1" in
841
+ --json) json=1; shift ;;
842
+ -*) team_usage_die "config list: 未知参数 $1" ;;
843
+ *) team_usage_die "config list: 多余参数 $1" ;;
844
+ esac
845
+ done
846
+ local path; path="$(team_config_contract_path)" || return 1
847
+ if [ "$json" = "1" ]; then team_config_list_json; return $?; fi
848
+
849
+ local fingerprint mtime row key class kind def value set
850
+ fingerprint="$(team_config_fingerprint "$path")"
851
+ mtime="$(team_config_mtime "$path")"
852
+ printf '契约 %s\n' "$path"
853
+ printf '指纹 %s\n' "$fingerprint"
854
+ printf 'mtime %s\n\n' "$mtime"
855
+ printf '%-42s %-8s %-10s %s\n' KEY CLASS KIND VALUE
856
+ while IFS= read -r row; do
857
+ case "$row" in \#*|'') continue ;; esac
858
+ key="$(team_config_field "$row" 1)"; class="$(team_config_field "$row" 2)"
859
+ kind="$(team_config_field "$row" 3)"; def="$(team_config_field "$row" 6)"
860
+ if value="$(team_config_file_value "$path" "$key")"; then set=1; else value="$def"; set=0; fi
861
+ printf '%-42s %-8s %-10s %s%s\n' "$key" "$class" "$kind" "$value" "$([ "$set" = "0" ] && printf ' (unset · default)')"
862
+ done < <(team_config_schema)
863
+ printf '\n审计 %s\n' "$(team_config_audit_file)"
864
+ team_config_audit_tail 5 | sed 's/^/ /'
865
+ }
866
+
867
+ team_cmd_config_log() {
868
+ local n=10
869
+ case "${1:-}" in
870
+ ''|--) ;;
871
+ *[!0-9]*) team_usage_die "config log: N 必须是数字(默认 10)" ;;
872
+ *) n="$1"; shift ;;
873
+ esac
874
+ [ $# -eq 0 ] || team_usage_die "config log: 多余参数 $1"
875
+ team_config_audit_tail "$n"
876
+ }
877
+
878
+ # ---------------------------------------------------------------- set
879
+ TEAM_CONFIG_EXIT_CONFLICT=3
880
+ TEAM_CONFIG_EXIT_INVALID=4
881
+ TEAM_CONFIG_EXIT_REFUSE=5
882
+ TEAM_CONFIG_EXIT_WRITE=6
883
+ TEAM_CONFIG_EXIT_DANGER=7
884
+
885
+ # 共用的写路径:<KEY> <value> <actor> <dry> <fp> <allow_danger>
886
+ # 返回值就是机器契约的退出码。
887
+ team_config_write_checked() { # <KEY> <value> <actor> <dry> <fp> <allow_danger>
888
+ local key="$1" val="$2" actor="$3" dry="$4" fp="$5" allow_danger="$6"
889
+ local path; path="$(team_config_contract_path)" || return 1
890
+ local row; row="$(team_config_row "$key" 2>/dev/null || true)"
891
+ local old; old="$(team_config_file_value "$path" "$key" 2>/dev/null || true)"
892
+
893
+ if [ -z "$row" ]; then
894
+ team_err "$key 不是已知的项目设置键"
895
+ team_dim " 已知键:$TEAM_CLI config list;自定义键请手改 $path" >&2
896
+ [ "$dry" = "1" ] || team_config_audit_write refused "$actor" "$key" "$old" "$val" || true
897
+ return "$TEAM_CONFIG_EXIT_REFUSE"
898
+ fi
899
+ local class kind; class="$(team_config_field "$row" 2)"; kind="$(team_config_field "$row" 3)"
900
+ if [ "$class" = "refuse" ]; then
901
+ team_err "$key 是只读键,控制台不改它"
902
+ team_dim " $(team_config_field "$row" 8)" >&2
903
+ [ "$dry" = "1" ] || team_config_audit_write refused "$actor" "$key" "$old" "$val" || true
904
+ return "$TEAM_CONFIG_EXIT_REFUSE"
905
+ fi
906
+
907
+ local canonical; canonical="$(team_config_canonical_value "$key" "$val")"
908
+ local reason
909
+ if ! reason="$(team_config_validate_value "$key" "$canonical")"; then
910
+ team_err "$key=$val 不合法:$reason"
911
+ [ "$dry" = "1" ] || team_config_audit_write invalid "$actor" "$key" "$old" "$val" || true
912
+ return "$TEAM_CONFIG_EXIT_INVALID"
913
+ fi
914
+
915
+ local danger=""
916
+ if ! danger="$(team_config_danger_reason "$key" "$canonical")"; then
917
+ if [ "$allow_danger" != "1" ]; then
918
+ team_err "$key=$val 是危险值:$danger(确认要写就加 --allow-danger)"
919
+ [ "$dry" = "1" ] || team_config_audit_write danger-refused "$actor" "$key" "$old" "$val" || true
920
+ return "$TEAM_CONFIG_EXIT_DANGER"
921
+ fi
922
+ fi
923
+
924
+ if [ -n "$fp" ]; then
925
+ local actual; actual="$(team_config_fingerprint "$path")"
926
+ if [ "$actual" != "$fp" ]; then
927
+ team_err "$key:文件在读取之后变过(指纹不符)—— 什么都没写"
928
+ team_dim " expected=$fp actual=$actual;重读 $TEAM_CLI config list 后再试" >&2
929
+ [ "$dry" = "1" ] || team_config_audit_write conflict "$actor" "$key" "$old" "$val" "$fp" "$actual" || true
930
+ return "$TEAM_CONFIG_EXIT_CONFLICT"
931
+ fi
932
+ fi
933
+
934
+ if [ "$dry" = "1" ]; then
935
+ printf 'ok: %s=%s(dry-run,未写契约、未写审计)\n' "$key" "$canonical"
936
+ return 0
937
+ fi
938
+
939
+ if ! team_config_set_in_file "$path" "$key" "$canonical" 2>/tmp/.team-config-writer.$$; then
940
+ local werr; werr="$(cat /tmp/.team-config-writer.$$ 2>/dev/null || true)"; rm -f /tmp/.team-config-writer.$$
941
+ team_err "$key 写入失败(原文件未动):$werr"
942
+ team_config_audit_write write-error "$actor" "$key" "$old" "$canonical" || true
943
+ return "$TEAM_CONFIG_EXIT_WRITE"
944
+ fi
945
+ rm -f /tmp/.team-config-writer.$$
946
+ team_config_audit_write ok "$actor" "$key" "$old" "$canonical" || true
947
+ return 0
948
+ }
949
+
950
+ # 名册的授权写入器(P99/R1):它是 TEAM_AGENTS 唯一的读-改-写入口,team config set 到不了这里
951
+ # (类检查在前)。退出码与 team config set 同族:0(写成;含「已在名册」的可见 no-op,不写不审计)/
952
+ # 3 指纹冲突 / 4 值不合法 / 5 remove 的席位不在名册 / 6 写失败。
953
+ # 席位名合法性只对**结果值**判:移掉一个手改进来的非法 token 是合法的(结果干净),
954
+ # 加进去才非法 —— 一条规则,读写同一份(team_config_list_violation)。
955
+ team_config_write_roster() { # <add|remove> <seat> [<fingerprint>] [<dry>] → 详见上面的退出码
956
+ local op="$1" seat="$2" fp="${3:-}" dry="${4:-0}"
957
+ local path; path="$(team_config_contract_path)" || return 1
958
+ local old; old="$(team_config_file_value "$path" TEAM_AGENTS 2>/dev/null || true)"
959
+ local old_fp; old_fp="$(team_config_fingerprint "$path")"
960
+
961
+ local present=0 t
962
+ # present 按 token 精确匹配:`case " $old " in *" $seat "*)` 会在 seat 含空白时**跨 token** 命中
963
+ # —— 名册里的 api + 1 两个 token 会让 `api 1` 冒充「已在名册」(P105/F1 的另一半)。
964
+ for t in $old; do [ "$t" = "$seat" ] && { present=1; break; }; done
965
+ local new="" tok
966
+ case "$op" in
967
+ add)
968
+ # 席位名自己先是一个合法 token(P105/F1):任何写入之前拒,名册字节不动、不写 ok 审计。
969
+ local seat_why
970
+ if ! seat_why="$(team_config_seat_violation "$seat")"; then
971
+ team_err "TEAM_AGENTS:$seat_why"
972
+ return "$TEAM_CONFIG_EXIT_INVALID"
973
+ fi
974
+ if [ "$present" = "1" ]; then
975
+ # 「已在名册」不豁免值规则(P99 spec 的第三条):手改出来的重复/非法 token 不能因为
976
+ # 一次 no-op 就继续冒充健康状态 —— `dev api api` 上再加 api 也是 exit 4 并点名重复 token。
977
+ # 合法值才是真 no-op(重复跑 --register 补齐半成品仍按 D2 的幂等承诺返回 0)。
978
+ local pwhy
979
+ if ! pwhy="$(team_config_list_violation "$old")"; then
980
+ team_err "TEAM_AGENTS=$old 不合法:$pwhy"
981
+ [ "$dry" = "1" ] || team_config_audit_write invalid cli TEAM_AGENTS "$old" "$old" || true
982
+ return "$TEAM_CONFIG_EXIT_INVALID"
983
+ fi
984
+ team_dim "席位 $seat 已在名册里(--register 不重复写)"
985
+ return 0
986
+ fi
987
+ new="${old:+$old }$seat" ;;
988
+ remove)
989
+ if [ "$present" != "1" ]; then
990
+ team_err "席位 $seat 不在名册里:名册是($(team_config_roster_text))"
991
+ [ "$dry" = "1" ] || team_config_audit_write refused cli TEAM_AGENTS "$old" "$seat" || true
992
+ return "$TEAM_CONFIG_EXIT_REFUSE"
993
+ fi
994
+ for tok in $old; do [ "$tok" = "$seat" ] || new="${new:+$new }$tok"; done ;;
995
+ *)
996
+ team_err "内部错误:未知的名册操作 $op"
997
+ return 1 ;;
998
+ esac
999
+
1000
+ local why
1001
+ if ! why="$(team_config_list_violation "$new")"; then
1002
+ team_err "TEAM_AGENTS=$new 不合法:$why"
1003
+ [ "$dry" = "1" ] || team_config_audit_write invalid cli TEAM_AGENTS "$old" "$new" || true
1004
+ return "$TEAM_CONFIG_EXIT_INVALID"
1005
+ fi
1006
+
1007
+ # CAS:调用方的 --fingerprint 与它读到的字节比;写前再核一次读到的指纹(读-改-写的窗口)。
1008
+ if [ -n "$fp" ] && [ "$fp" != "$old_fp" ]; then
1009
+ team_err "TEAM_AGENTS:指纹不符(expected=$fp actual=$old_fp)—— 什么都没写"
1010
+ [ "$dry" = "1" ] || team_config_audit_write conflict cli TEAM_AGENTS "$old" "$new" "$fp" "$old_fp" || true
1011
+ return "$TEAM_CONFIG_EXIT_CONFLICT"
1012
+ fi
1013
+ local now_fp; now_fp="$(team_config_fingerprint "$path")"
1014
+ if [ "$now_fp" != "$old_fp" ]; then
1015
+ team_err "TEAM_AGENTS:文件在读取之后变过(指纹不符)—— 什么都没写"
1016
+ team_dim " expected=$old_fp actual=$now_fp;重读 $TEAM_CLI config list 后再试" >&2
1017
+ [ "$dry" = "1" ] || team_config_audit_write conflict cli TEAM_AGENTS "$old" "$new" "$old_fp" "$now_fp" || true
1018
+ return "$TEAM_CONFIG_EXIT_CONFLICT"
1019
+ fi
1020
+
1021
+ if [ "$dry" = "1" ]; then
1022
+ printf 'ok: TEAM_AGENTS=%s(dry-run,未写契约、未写审计)\n' "$new"
1023
+ return 0
1024
+ fi
1025
+
1026
+ if ! team_config_set_in_file "$path" TEAM_AGENTS "$new" 2>/tmp/.team-config-roster.$$; then
1027
+ local werr; werr="$(cat /tmp/.team-config-roster.$$ 2>/dev/null || true)"; rm -f /tmp/.team-config-roster.$$
1028
+ team_err "TEAM_AGENTS 写入失败(原文件未动):$werr"
1029
+ team_config_audit_write write-error cli TEAM_AGENTS "$old" "$new" || true
1030
+ return "$TEAM_CONFIG_EXIT_WRITE"
1031
+ fi
1032
+ rm -f /tmp/.team-config-roster.$$
1033
+ # 同进程后续步骤(worktree add / state / roster 文本)读的就是这个变量:写盘成功后同步刷新。
1034
+ TEAM_AGENTS="$new"
1035
+ team_config_audit_write ok cli TEAM_AGENTS "$old" "$new" || true
1036
+ return 0
1037
+ }
1038
+
1039
+ team_cmd_config_set() {
1040
+ local key="" val="" have_val=0 dry=0 fp="" actor="${TEAM_ACTOR:-cli}" allow_danger=0
1041
+ while [ $# -gt 0 ]; do
1042
+ case "$1" in
1043
+ --dry-run) dry=1; shift ;;
1044
+ --allow-danger) allow_danger=1; shift ;;
1045
+ --fingerprint) fp="${2:?--fingerprint 需要 sha256}"; shift 2 ;;
1046
+ --fingerprint=*) fp="${1#*=}"; shift ;;
1047
+ --actor) actor="${2:?--actor 需要名字}"; shift 2 ;;
1048
+ --actor=*) actor="${1#*=}"; shift ;;
1049
+ --yes|-y) shift ;;
1050
+ -*) team_usage_die "config set: 未知参数 $1" ;;
1051
+ *) if [ -z "$key" ]; then key="$1"; elif [ "$have_val" = "0" ]; then val="$1"; have_val=1; else team_usage_die "config set: 多余参数 $1"; fi; shift ;;
1052
+ esac
1053
+ done
1054
+ [ -n "$key" ] && [ "$have_val" = "1" ] || team_usage_die "config set: 用法 team config set <KEY> <VALUE> [--dry-run] [--fingerprint <sha256>] [--actor <name>] [--allow-danger]"
1055
+ local rc=0
1056
+ team_config_write_checked "$key" "$val" "$actor" "$dry" "$fp" "$allow_danger" || rc=$?
1057
+ return $rc
1058
+ }
1059
+
1060
+ # ---------------------------------------------------------------- set-agent-model
1061
+ # 值变换(parser/serializer 只在这里):upsert/remove 一个 seat=model token。
1062
+ team_config_pairlist_upsert() { # <value> <seat> <model|-> → 新值
1063
+ local val="${1-}" seat="$2" model="$3" out="" tok found=0
1064
+ for tok in $val; do
1065
+ case "$tok" in
1066
+ "$seat"=*) found=1; [ -n "$model" ] && out="${out:+$out }$seat=$model" ;;
1067
+ *) out="${out:+$out }$tok" ;;
1068
+ esac
1069
+ done
1070
+ if [ "$found" = "0" ] && [ -n "$model" ]; then out="${out:+$out }$seat=$model"; fi
1071
+ printf '%s\n' "$out"
1072
+ }
1073
+
1074
+ team_cmd_config_set_agent_model() {
1075
+ local seat="" model="" dry=0 fp="" actor="${TEAM_ACTOR:-cli}" allow_danger=0
1076
+ while [ $# -gt 0 ]; do
1077
+ case "$1" in
1078
+ --dry-run) dry=1; shift ;;
1079
+ --allow-danger) allow_danger=1; shift ;;
1080
+ --fingerprint) fp="${2:?--fingerprint 需要 sha256}"; shift 2 ;;
1081
+ --fingerprint=*) fp="${1#*=}"; shift ;;
1082
+ --actor) actor="${2:?--actor 需要名字}"; shift 2 ;;
1083
+ --actor=*) actor="${1#*=}"; shift ;;
1084
+ --yes|-y) shift ;;
1085
+ -) if [ -z "$seat" ]; then seat="$1"; elif [ -z "$model" ]; then model="$1"; else team_usage_die "config set-agent-model: 多余参数 $1"; fi; shift ;;
1086
+ -*) team_usage_die "config set-agent-model: 未知参数 $1" ;;
1087
+ *) if [ -z "$seat" ]; then seat="$1"; elif [ -z "$model" ]; then model="$1"; else team_usage_die "config set-agent-model: 多余参数 $1"; fi; shift ;;
1088
+ esac
1089
+ done
1090
+ [ -n "$seat" ] && [ -n "$model" ] || team_usage_die "config set-agent-model: 用法 team config set-agent-model <seat> <model|->"
1091
+
1092
+ local path; path="$(team_config_contract_path)" || return 1
1093
+ local audit_key="TEAM_AGENT_MODELS"
1094
+ [ "$seat" = "pm" ] && audit_key="TEAM_PM_MODEL"
1095
+ if ! team_config_seat_known "$seat"; then
1096
+ team_err "未知席位 $seat:名册是($(team_config_roster_text))加 pm"
1097
+ team_dim " 名册的键是 TEAM_AGENTS(只读):$TEAM_CLI add-agent $seat --register(或 team teardown --agent $seat --register)" >&2
1098
+ [ "$dry" = "1" ] || team_config_audit_write refused "$actor" "$audit_key" "$seat" "$model" || true
1099
+ return "$TEAM_CONFIG_EXIT_REFUSE"
1100
+ fi
1101
+ if [ "$model" != "-" ] && ! team_config_model_shape_ok "$model"; then
1102
+ team_err "模型必须是 provider/model 形状($(team_config_model_violation "$model")):$model"
1103
+ [ "$dry" = "1" ] || team_config_audit_write invalid "$actor" "$audit_key" "$seat" "$model" || true
1104
+ return "$TEAM_CONFIG_EXIT_INVALID"
1105
+ fi
1106
+ local remove=0; [ "$model" = "-" ] && remove=1
1107
+
1108
+ if [ "$seat" = "pm" ]; then
1109
+ # pm 席位就是 TEAM_PM_MODEL 一行;'-' 清空 = 回到 TEAM_DEFAULT_MODEL
1110
+ local newval="$model"
1111
+ [ "$remove" = "1" ] && newval=""
1112
+ local rc=0
1113
+ team_config_write_checked TEAM_PM_MODEL "$newval" "$actor" "$dry" "$fp" "$allow_danger" || rc=$?
1114
+ # 同进程刷新(与名册写入器的 TEAM_AGENTS="$new" 同一纪律,P105/F2):写盘后的后续步骤
1115
+ # 必须读到这个新值,不能留一个「写盘前的旧值」冒充配置解析。
1116
+ if [ "$rc" -eq 0 ] && [ "$dry" != "1" ]; then TEAM_PM_MODEL="$newval"; fi
1117
+ return $rc
1118
+ fi
1119
+
1120
+ # 名册席位:解析并重排 TEAM_AGENT_MODELS(调用方永不自己拼 pair list)
1121
+ local old; old="$(team_config_file_value "$path" TEAM_AGENT_MODELS 2>/dev/null || true)"
1122
+ local newval; newval="$(team_config_pairlist_upsert "$old" "$seat" "$([ "$remove" = "1" ] && printf '' || printf '%s' "$model")")"
1123
+ local rc=0
1124
+ team_config_write_checked TEAM_AGENT_MODELS "$newval" "$actor" "$dry" "$fp" "$allow_danger" || rc=$?
1125
+ # 同进程刷新(P105/F2):add-agent --model 写完后走同一个进程的 worktree/state 记录步骤,
1126
+ # 不刷新的话 state 记的是写盘前的旧模型,而配置已经有 override —— 席位行自相矛盾。
1127
+ if [ "$rc" -eq 0 ] && [ "$dry" != "1" ]; then TEAM_AGENT_MODELS="$newval"; fi
1128
+ return $rc
1129
+ }
1130
+
1131
+ # ---------------------------------------------------------------- 子命令分发
1132
+ team_cmd_config() {
1133
+ local sub="${1:-}"
1134
+ [ $# -gt 0 ] && shift
1135
+ case "$sub" in
1136
+ list) team_cmd_config_list "$@" ;;
1137
+ set) team_cmd_config_set "$@" ;;
1138
+ log) team_cmd_config_log "$@" ;;
1139
+ set-agent-model) team_cmd_config_set_agent_model "$@" ;;
1140
+ ''|help|-h|--help)
1141
+ cat <<EOF
1142
+ 用法: $TEAM_CLI config <list|set|log|set-agent-model> …
1143
+
1144
+ $TEAM_CLI config list [--json] 列出契约里每个键(class/kind/当前值/默认值)与
1145
+ models 块(按席位模型 + 来源三态)
1146
+ $TEAM_CLI config set <KEY> <VALUE> [--dry-run] 唯一写入口:校验 → 指纹 CAS → 原子写 → 审计
1147
+ [--fingerprint <sha256>] [--actor <name>] [--allow-danger]
1148
+ $TEAM_CLI config set-agent-model <seat> <model|-> 按席位改模型(- 移除覆盖,回到 TEAM_DEFAULT_MODEL)
1149
+ $TEAM_CLI config log [N] 审计尾部(默认 10 行)
1150
+
1151
+ 退出码: 0 写入/校验通过 | 3 指纹冲突 | 4 值不合法 | 5 键只读 | 6 写失败 | 7 危险值需 --allow-danger
1152
+ EOF
1153
+ ;;
1154
+ *) team_usage_die "config: 未知子命令 $sub(list|set|log|set-agent-model)" ;;
1155
+ esac
1156
+ }