teamsmith 0.1.2 → 0.3.0

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 (158) hide show
  1. package/README.md +13 -7
  2. package/package.json +1 -1
  3. package/skills/teamsmith/CHANGELOG.md +67 -0
  4. package/skills/teamsmith/SKILL.md +26 -8
  5. package/skills/teamsmith/extension/team-notify.ts +37 -6
  6. package/skills/teamsmith/extension/team.ts +438 -0
  7. package/skills/teamsmith/references/agent-adapters.md +7 -7
  8. package/skills/teamsmith/references/config.md +65 -18
  9. package/skills/teamsmith/references/migration.md +220 -34
  10. package/skills/teamsmith/references/protocol.md +250 -25
  11. package/skills/teamsmith/references/troubleshooting.md +124 -7
  12. package/skills/teamsmith/references/workflows.md +66 -20
  13. package/skills/teamsmith/scripts/lib/cmd-agents.sh +525 -90
  14. package/skills/teamsmith/scripts/lib/cmd-bootstrap.sh +71 -10
  15. package/skills/teamsmith/scripts/lib/cmd-config.sh +562 -128
  16. package/skills/teamsmith/scripts/lib/cmd-death.sh +2 -15
  17. package/skills/teamsmith/scripts/lib/cmd-docs.sh +6 -3
  18. package/skills/teamsmith/scripts/lib/cmd-draft.sh +4 -4
  19. package/skills/teamsmith/scripts/lib/cmd-help.sh +677 -0
  20. package/skills/teamsmith/scripts/lib/cmd-init.sh +157 -5
  21. package/skills/teamsmith/scripts/lib/cmd-interrupt.sh +461 -0
  22. package/skills/teamsmith/scripts/lib/cmd-meeting.sh +2 -25
  23. package/skills/teamsmith/scripts/lib/cmd-project.sh +149 -105
  24. package/skills/teamsmith/scripts/lib/cmd-reap.sh +277 -0
  25. package/skills/teamsmith/scripts/lib/cmd-reply.sh +96 -0
  26. package/skills/teamsmith/scripts/lib/cmd-review.sh +188 -40
  27. package/skills/teamsmith/scripts/lib/cmd-status.sh +268 -30
  28. package/skills/teamsmith/scripts/lib/cmd-update.sh +36 -7
  29. package/skills/teamsmith/scripts/lib/cmd-watch.sh +634 -54
  30. package/skills/teamsmith/scripts/lib/common.sh +1560 -109
  31. package/skills/teamsmith/scripts/lib/gate-lock.sh +544 -0
  32. package/skills/teamsmith/scripts/monitor.mjs +34 -3
  33. package/skills/teamsmith/scripts/panel/README.md +22 -11
  34. package/skills/teamsmith/scripts/panel/draft-send.sh +11 -1
  35. package/skills/teamsmith/scripts/panel/panel.js +226 -224
  36. package/skills/teamsmith/scripts/panel/src/App.tsx +478 -184
  37. package/skills/teamsmith/scripts/panel/src/clipboard.ts +11 -5
  38. package/skills/teamsmith/scripts/panel/src/compose.ts +6 -3
  39. package/skills/teamsmith/scripts/panel/src/data.ts +95 -7
  40. package/skills/teamsmith/scripts/panel/src/layout.ts +703 -152
  41. package/skills/teamsmith/scripts/panel/src/main.tsx +129 -19
  42. package/skills/teamsmith/scripts/panel/src/settings.ts +1 -1
  43. package/skills/teamsmith/scripts/panel/src/strings/en.ts +76 -31
  44. package/skills/teamsmith/scripts/panel/src/strings/zh.ts +78 -33
  45. package/skills/teamsmith/scripts/panel/src/types.ts +204 -17
  46. package/skills/teamsmith/scripts/replies.mjs +896 -0
  47. package/skills/teamsmith/scripts/shim/tmux +18 -0
  48. package/skills/teamsmith/scripts/strings/commands.tsv +86 -0
  49. package/skills/teamsmith/scripts/strings/help.en.tsv +503 -0
  50. package/skills/teamsmith/scripts/strings/help.zh.tsv +489 -0
  51. package/skills/teamsmith/scripts/team +20 -6
  52. package/skills/teamsmith/templates/AGENTS.section.md.tmpl +23 -3
  53. package/skills/teamsmith/templates/BOARD.md.tmpl +2 -2
  54. package/skills/teamsmith/templates/OWNERSHIP.md.tmpl +7 -3
  55. package/skills/teamsmith/templates/PROTOCOL.md.tmpl +17 -0
  56. package/skills/teamsmith/templates/config.sh.tmpl +29 -5
  57. package/skills/teamsmith/templates/memory-seed.md.tmpl +3 -0
  58. package/skills/teamsmith/templates/pm-prompt.md.tmpl +41 -1
  59. package/skills/teamsmith/templates/report.md.tmpl +12 -0
  60. package/skills/teamsmith/templates/task.md.tmpl +4 -0
  61. package/skills/teamsmith/tests/bg-ledger-harness.mjs +464 -40
  62. package/skills/teamsmith/tests/bg-ledger.sh +168 -10
  63. package/skills/teamsmith/tests/checkout-shape-probe.sh +3 -1
  64. package/skills/teamsmith/tests/config-cli.sh +320 -300
  65. package/skills/teamsmith/tests/death-cause.sh +5 -2
  66. package/skills/teamsmith/tests/fixtures/delivery-truth-real/scenario.sh +7 -1
  67. package/skills/teamsmith/tests/fixtures/p287/lint-good.sh.in +3 -0
  68. package/skills/teamsmith/tests/fixtures/p287/lint-hardcoded.sh.in +3 -0
  69. package/skills/teamsmith/tests/fixtures/p287/lint-no-ns.sh.in +3 -0
  70. package/skills/teamsmith/tests/fixtures/p55/flip-p49.sh +18 -6
  71. package/skills/teamsmith/tests/flip-m33.sh +5 -3
  72. package/skills/teamsmith/tests/flip-m37.sh +4 -1
  73. package/skills/teamsmith/tests/flip-m4.3.sh +4 -1
  74. package/skills/teamsmith/tests/flip-m6.3.sh +4 -1
  75. package/skills/teamsmith/tests/flip-m6.5.sh +4 -1
  76. package/skills/teamsmith/tests/flip-m7.2.sh +4 -1
  77. package/skills/teamsmith/tests/flip-p158.sh +2 -1
  78. package/skills/teamsmith/tests/flip-p210.sh +4 -1
  79. package/skills/teamsmith/tests/flip-p217.sh +4 -1
  80. package/skills/teamsmith/tests/flip-p287-lock.sh +342 -0
  81. package/skills/teamsmith/tests/flip-p287-name.sh +279 -0
  82. package/skills/teamsmith/tests/flip-p304-read-side.sh +98 -0
  83. package/skills/teamsmith/tests/flip-p308-skip-not-pass.sh +151 -0
  84. package/skills/teamsmith/tests/flip-p309-p162-deep.sh +122 -0
  85. package/skills/teamsmith/tests/flip-p310-lock.sh +216 -0
  86. package/skills/teamsmith/tests/flip-p310-socket.sh +125 -0
  87. package/skills/teamsmith/tests/help-surface.sh +1664 -0
  88. package/skills/teamsmith/tests/install-shape.sh +283 -22
  89. package/skills/teamsmith/tests/key-replies-pty.sh +374 -0
  90. package/skills/teamsmith/tests/key-replies-ui.sh +325 -0
  91. package/skills/teamsmith/tests/key-replies.sh +1314 -0
  92. package/skills/teamsmith/tests/ledger-ban.sh +9 -8
  93. package/skills/teamsmith/tests/ledger-claim.sh +248 -0
  94. package/skills/teamsmith/tests/lib/bounded-spin.sh +90 -0
  95. package/skills/teamsmith/tests/lib/model-policy.sh +30 -0
  96. package/skills/teamsmith/tests/lib/model-routing-boundary.sh +420 -0
  97. package/skills/teamsmith/tests/lib/model-routing-expansion.sh +155 -0
  98. package/skills/teamsmith/tests/lib/model-routing-flips.py +187 -0
  99. package/skills/teamsmith/tests/lib/model-routing-roles.sh +250 -0
  100. package/skills/teamsmith/tests/lib/proc-io.sh +103 -0
  101. package/skills/teamsmith/tests/lib/reply-fixture.sh +367 -0
  102. package/skills/teamsmith/tests/lib/section-guard.sh +258 -13
  103. package/skills/teamsmith/tests/lib/tmp-root.sh +139 -27
  104. package/skills/teamsmith/tests/lib/tmux-cap.sh +10 -1
  105. package/skills/teamsmith/tests/lib/tmux-iso.sh +39 -13
  106. package/skills/teamsmith/tests/loop-inventory.tsv +59 -47
  107. package/skills/teamsmith/tests/panel-b2.sh +107 -4
  108. package/skills/teamsmith/tests/panel-b3-stub.sh +139 -4
  109. package/skills/teamsmith/tests/panel-b3.sh +189 -29
  110. package/skills/teamsmith/tests/panel-choices.sh +81 -63
  111. package/skills/teamsmith/tests/panel-cpu.sh +3 -2
  112. package/skills/teamsmith/tests/panel-knobs.sh +7 -1
  113. package/skills/teamsmith/tests/panel-p21.sh +445 -241
  114. package/skills/teamsmith/tests/panel-snapshots.sh +41 -225
  115. package/skills/teamsmith/tests/panel-strings.mjs +33 -18
  116. package/skills/teamsmith/tests/perf.sh +122 -31
  117. package/skills/teamsmith/tests/pm-box-real.sh +1 -1
  118. package/skills/teamsmith/tests/pulse-nudge-key.sh +1 -1
  119. package/skills/teamsmith/tests/routes.sh +364 -109
  120. package/skills/teamsmith/tests/seat-interrupt.sh +1002 -0
  121. package/skills/teamsmith/tests/seat-visibility.sh +675 -0
  122. package/skills/teamsmith/tests/section-budgets.tsv +26 -12
  123. package/skills/teamsmith/tests/section-guard.sh +6 -1
  124. package/skills/teamsmith/tests/section-needs-audit.sh +24 -7
  125. package/skills/teamsmith/tests/section-paths.tsv +42 -26
  126. package/skills/teamsmith/tests/section-select.sh +90 -7
  127. package/skills/teamsmith/tests/slash-team-e2e.sh +323 -0
  128. package/skills/teamsmith/tests/slash-team.sh +617 -0
  129. package/skills/teamsmith/tests/smoke.sh +3762 -628
  130. package/skills/teamsmith/tests/snapshots/zh-dark-120-detail.txt +2 -2
  131. package/skills/teamsmith/tests/snapshots/zh-dark-120-p4.txt +2 -2
  132. package/skills/teamsmith/tests/snapshots/zh-dark-120.txt +9 -9
  133. package/skills/teamsmith/tests/snapshots/zh-dark-160-p4.txt +2 -2
  134. package/skills/teamsmith/tests/snapshots/zh-dark-160.txt +9 -9
  135. package/skills/teamsmith/tests/snapshots/zh-dark-59-detail.txt +1 -1
  136. package/skills/teamsmith/tests/snapshots/zh-dark-59-p4.txt +1 -1
  137. package/skills/teamsmith/tests/snapshots/zh-dark-59.txt +5 -5
  138. package/skills/teamsmith/tests/snapshots/zh-dark-99-p4.txt +2 -2
  139. package/skills/teamsmith/tests/snapshots/zh-dark-99.txt +5 -5
  140. package/skills/teamsmith/tests/snapshots/zh-light-120-detail.txt +2 -2
  141. package/skills/teamsmith/tests/snapshots/zh-light-120-p4.txt +2 -2
  142. package/skills/teamsmith/tests/snapshots/zh-light-120.txt +9 -9
  143. package/skills/teamsmith/tests/snapshots/zh-light-160-p4.txt +2 -2
  144. package/skills/teamsmith/tests/snapshots/zh-light-160.txt +9 -9
  145. package/skills/teamsmith/tests/snapshots/zh-light-59-detail.txt +1 -1
  146. package/skills/teamsmith/tests/snapshots/zh-light-59-p4.txt +1 -1
  147. package/skills/teamsmith/tests/snapshots/zh-light-59.txt +5 -5
  148. package/skills/teamsmith/tests/snapshots/zh-light-99-p4.txt +2 -2
  149. package/skills/teamsmith/tests/snapshots/zh-light-99.txt +5 -5
  150. package/skills/teamsmith/tests/task-header-model.sh +2 -1
  151. package/skills/teamsmith/tests/task-model-routing.sh +1511 -0
  152. package/skills/teamsmith/tests/tmp-hygiene.sh +782 -45
  153. package/skills/teamsmith/tests/tmux-identity-leak.sh +773 -0
  154. package/skills/teamsmith/tests/tmux-lint-legacy.txt +12 -18
  155. package/skills/teamsmith/tests/upgrade-recipe.sh +537 -0
  156. package/skills/teamsmith-init/SKILL.md +43 -43
  157. package/skills/teamsmith-init/references/bootstrap.md +8 -6
  158. package/skills/teamsmith-init/templates/bootstrap-prompt.md.tmpl +9 -5
@@ -91,7 +91,7 @@ team_watch_reexec_if_stale() { # <启动指纹> <子命令> [子命令参数…]
91
91
  # 窗口里的巡检进程是什么时候起来的(watchdog-status 用它报「代码快照过旧」)。
92
92
  team_watch_process_start_epoch() { # <tmux 目标> → 进程启动的 epoch 秒(判不出 → 空)
93
93
  local pid et
94
- pid="$(tmux display-message -p -t "$1" '#{pane_pid}' 2>/dev/null | head -1 || true)"
94
+ pid="$(team_tmux display-message -p -t "$1" '#{pane_pid}' 2>/dev/null | head -1 || true)"
95
95
  case "${pid:-}" in ''|*[!0-9]*) return 0 ;; esac
96
96
  et="$(ps -o etimes= -p "$pid" 2>/dev/null | tr -d ' ' || true)"
97
97
  case "${et:-}" in ''|*[!0-9]*) return 0 ;; esac
@@ -123,16 +123,189 @@ team_pm_same_dir_warn() {
123
123
  team_dim " 建议:先退出它;或者在干净的新会话里跑 $TEAM_CLI up --fresh-pm(本次新开一场对话)"
124
124
  }
125
125
 
126
+
127
+ # ---------------------------------------------------------------- 启动自愈(P261 · startup-self-heal)
128
+ # 落点分工(design §1):纯决策在 cmd-config.sh(team_config_migration_*),这里只做编排:打印、备份、
129
+ # 复用同一个写入器、exec 自己、身份预告、版本提示。一个字节都不写的承诺由纯决策层保证。
130
+ team_up_migration_rows_text() { # 逐键 before → after(值可见)
131
+ local row old_key old_val new_key new_val note
132
+ while IFS='|' read -r old_key old_val new_key new_val note; do
133
+ [ -n "$old_key" ] || continue
134
+ printf ' %s %s → %s %s%s\n' "$old_key" "$old_val" "$new_key" "$new_val" "${note:+($note)}"
135
+ done <<< "${TEAM_MIGRATION_ROWS:-}"
136
+ }
137
+ team_up_migration_backup_line() { # 备份与还原命令(同一份文本给成功路径与失败路径)
138
+ local backup="$1" cfg="$2"
139
+ printf ' 备份:%s\n 还原:cp %q %q\n' "$backup" "$backup" "$cfg"
140
+ }
141
+ # 迁移可用(action=apply)时的逐键表 + 备份路径 + 为什么。
142
+ team_up_migration_preview() { # <cfg> <备份路径> → stdout 预告块
143
+ printf 'i 检测到旧形状配置(迁移 %s:形状 %s → %s,%s 引入);值一个都不改:\n' \
144
+ "${TEAM_MIGRATION_ROW:-m1}" "${TEAM_MIGRATION_FROM:-1}" "${TEAM_MIGRATION_TO:-2}" "${TEAM_MIGRATION_INTRODUCED:-0.2.0}"
145
+ team_up_migration_rows_text
146
+ team_up_migration_backup_line "$2" "$1"
147
+ [ -z "$TEAM_MIGRATION_NOTES" ] || printf ' 推导说明:%s\n' "$TEAM_MIGRATION_NOTES"
148
+ printf ' 为什么:0.2.0 起派单按任务类别选模型(brief 的 category: 行);这两个退休键不再被读取。\n'
149
+ }
150
+ # 迁移决策与编排(design §1 的 1/1a/1b/1c)。0 = 继续;1 = 拒绝(已打印);10 = 已在 print 下预告了迁移
151
+ team_up_migration_step() { # <print|no-migrate|real> <argv…>
152
+ local mode="$1"; shift
153
+ local cfg; cfg="$(team_config_contract_path)" || return 1
154
+ if ! team_config_migration_plan "$cfg"; then
155
+ team_err "启动迁移拒绝(${TEAM_MIGRATION_SHAPE:-unknown}):${TEAM_MIGRATION_WHY}"
156
+ team_dim " 一个字节都没写(不写配置、不建备份、不写审计)" >&2
157
+ if [ -n "${TEAM_MIGRATION_PARTIAL_MAP:-}" ]; then
158
+ printf ' 其余已推导出的对可以先手工提交(非法那一对用占位符):\n %s config upgrade-models \\\n --task-models '"'"'%s'"'"' \\\n --pm-model '"'"'<独立模型>'"'"' --dry-run\n' \
159
+ "$TEAM_CLI" "$TEAM_MIGRATION_PARTIAL_MAP"
160
+ fi
161
+ team_dim " 手工路线:$TEAM_CLI config upgrade-models --task-models '<完整映射>' --pm-model '<独立模型>' --dry-run" >&2
162
+ return 1
163
+ fi
164
+ [ "$TEAM_MIGRATION_ACTION" = 'apply' ] || return 0
165
+ local ts; ts="$(date -u +%Y%m%dT%H%M%SZ)"
166
+ local backup="$TEAM_MIGRATION_BACKUP_HINT"
167
+ [ -n "$backup" ] || backup="$(team_config_backup_dir)/$(team_config_backup_name "$cfg" "$ts")"
168
+ if [ "$mode" = 'print' ]; then
169
+ team_up_migration_preview "$cfg" "$backup"
170
+ return 10
171
+ fi
172
+ if [ "$mode" = 'no-migrate' ]; then
173
+ team_err "--no-migrate:不写盘,也不启动(旧形状配置还在)"
174
+ printf ' 修法(建议·按你现在的配置推导,类别名可以改):\n %s config upgrade-models \\\n --task-models '"'"'%s'"'"' \\\n --pm-model '"'"'%s'"'"' --dry-run\n' \
175
+ "$TEAM_CLI" "$TEAM_MIGRATION_MAP" "$TEAM_MIGRATION_PM"
176
+ [ -z "$TEAM_MIGRATION_NOTES" ] || printf ' (推导说明:%s)\n' "$TEAM_MIGRATION_NOTES"
177
+ team_dim " 手工事务路线(去掉 --no-migrate 就是自动):$TEAM_CLI config upgrade-models --task-models '<完整映射>' --pm-model '<独立模型>' --yes" >&2
178
+ return 1
179
+ fi
180
+ # 真实迁移:计划 → 备份 → 同一个写入器(actor=up-migration,附形状号)→ 结果/还原 → exec 自己
181
+ team_up_migration_preview "$cfg" "$backup"
182
+ mkdir -p "$TEAM_STATE_DIR" 2>/dev/null || true
183
+ local fp; fp="$(team_config_fingerprint "$cfg")"
184
+ if ! backup="$(team_config_backup_write "$cfg" "$ts")"; then
185
+ team_err "迁移中止:备份写不出来($backup)—— 配置一个字节都没动"
186
+ return 1
187
+ fi
188
+ local rc=0
189
+ team_with_lock "config:$cfg" team_config_locked_call team_config_upgrade_transaction \
190
+ "$cfg" "$TEAM_MIGRATION_MAP" "$TEAM_MIGRATION_PM" 0 "$fp" 'up-migration' \
191
+ "TEAM_CONFIG_VERSION=$TEAM_CONFIG_SHAPE_CURRENT" || rc=$?
192
+ if [ "$rc" != 0 ]; then
193
+ team_err "迁移写入失败(rc=$rc):配置保持原样(或已由事务回滚)"
194
+ team_up_migration_backup_line "$backup" "$cfg" >&2
195
+ return 1
196
+ fi
197
+ team_up_migration_backup_line "$backup" "$cfg"
198
+ team_ok "迁移完成(审计已写):重新执行 up 读取新配置"
199
+ export TEAM_MIGRATION_BACKUP="$backup"
200
+ exec bash "$TEAM_SKILL_DIR/scripts/team" up "$@"
201
+ }
202
+ # 迁移之后仍失败:信息里带上本次迁移的备份与还原命令(design §5;--print 不设置该变量)。
203
+ team_up_backup_note() {
204
+ [ -n "${TEAM_MIGRATION_BACKUP:-}" ] || return 0
205
+ team_dim " 本次迁移的还原命令:cp $(printf '%q' "$TEAM_MIGRATION_BACKUP") $(printf '%q' "$TEAM_CONFIG")" >&2
206
+ }
207
+ # --print 的身份预告(design §7):不写盘;缺记录 → 打印将要登记的那一行;不一致 → 两边值 + 拒绝。
208
+ team_pm_identity_preview() { # → 0 = 一致/将登记;1 = 不一致(已打印两边值)
209
+ local want_model want_resume
210
+ want_model="$(team_pm_model_resolve)"; want_resume="$(team_pm_resume_key)"
211
+ if [ ! -f "$TEAM_STATE_DIR/pm-launch.env" ]; then
212
+ printf 'i 身份预告:此前没有启动记录;这次启动会登记 model=%s resume_key=%s mode=%s(要换 PM 模型必须 --fresh-pm)\n' \
213
+ "$want_model" "$want_resume" "$(case "$(team_pm_continuity)" in continued:*) printf continued ;; *) printf fresh ;; esac)"
214
+ return 0
215
+ fi
216
+ if ! team_pm_metadata_load; then
217
+ team_err "身份预告:已有记录不可读/不属于本项目本窗口/是旧版本(${TEAM_IDENTITY_DIAGNOSTIC:-unreadable})→ 启动时会拒绝"
218
+ return 1
219
+ fi
220
+ if [ "$TEAM_PM_SELECTED_MODEL" != "$want_model" ] || [ "$TEAM_PM_RECORDED_RESUME_KEY" != "$want_resume" ]; then
221
+ team_err "身份预告:recorded model=$TEAM_PM_SELECTED_MODEL(suffix 显式)≠ 现在 model=$want_model;recorded resume_key=$TEAM_PM_RECORDED_RESUME_KEY ≠ 现在 resume_key=$want_resume → 启动时会拒绝(--fresh-pm 或 --adopt-pm-identity --yes)"
222
+ return 1
223
+ fi
224
+ printf 'i 身份预告:记录与当前解析一致(model=%s resume_key=%s)\n' "$want_model" "$want_resume"
225
+ return 0
226
+ }
227
+
228
+ # 版本提示(design §8):只在 team up 真实运行时触发;有界 ≤2s、缓存 24h、除「本地落后」外一律静默、不改 rc。
229
+ team_version_newer() { # <local> <published> → 0 = published 的数字段严格更大
230
+ local -a ls ps; local i a b
231
+ IFS=. read -r -a ls <<< "${1%%[-+]*}"
232
+ IFS=. read -r -a ps <<< "${2%%[-+]*}"
233
+ for i in 0 1 2; do
234
+ a="${ls[$i]:-0}"; b="${ps[$i]:-0}"
235
+ case "$a$b" in *[!0-9]*) return 1 ;; esac
236
+ [ "$b" -gt "$a" ] && return 0
237
+ [ "$b" -lt "$a" ] && return 1
238
+ done
239
+ return 1
240
+ }
241
+ team_update_lookup() { # → stdout npm 上的最新版本(严格 ^[0-9]+.[0-9]+.[0-9]+$);查不到 → 空
242
+ command -v npm >/dev/null 2>&1 || return 0
243
+ local tmp rc=0 pid i=0 out
244
+ team_tmp_root_prepare >/dev/null 2>&1 || true
245
+ team_tmp_root_prepare >/dev/null 2>&1 || true
246
+ tmp="$(mktemp "$(team_tmp_root)/teamsmith-npm.$(team_project_ns).XXXXXX" 2>/dev/null)" || return 0
247
+ if command -v timeout >/dev/null 2>&1; then
248
+ timeout 2 npm view teamsmith version --registry https://registry.npmjs.org >"$tmp" 2>/dev/null || rc=$?
249
+ else
250
+ npm view teamsmith version --registry https://registry.npmjs.org >"$tmp" 2>/dev/null &
251
+ pid=$!
252
+ while [ "$i" -lt 20 ]; do kill -0 "$pid" 2>/dev/null || break; sleep 0.1; i=$((i+1)); done
253
+ if kill -0 "$pid" 2>/dev/null; then kill -TERM "$pid" 2>/dev/null || true; rc=124; fi
254
+ wait "$pid" 2>/dev/null || { [ "$rc" != 0 ] || rc=1; }
255
+ fi
256
+ out="$(tr -d '[:space:]' < "$tmp" 2>/dev/null || true)"
257
+ rm -f "$tmp"
258
+ [ "$rc" = 0 ] || return 0
259
+ case "$out" in
260
+ [0-9]*.[0-9]*.[0-9]*) case "$out" in *[!0-9.]*) return 0 ;; esac; printf '%s\n' "$out" ;;
261
+ *) return 0 ;;
262
+ esac
263
+ }
264
+ team_update_cache_write() { # <epoch> <latest|-> <local>(原子写;失败也不影响启动)
265
+ local f="$TEAM_STATE_DIR/update-check.env" tmp
266
+ mkdir -p "$TEAM_STATE_DIR" 2>/dev/null || return 0
267
+ tmp="$f.tmp.$$"
268
+ if printf 'CHECKED_AT=%s\nLATEST=%s\nLOCAL=%s\n' "$1" "$2" "$3" > "$tmp" 2>/dev/null; then
269
+ mv -f "$tmp" "$f" 2>/dev/null || rm -f "$tmp"
270
+ else
271
+ rm -f "$tmp"
272
+ fi
273
+ return 0
274
+ }
275
+ team_update_check_notice() { # 真实 up 的版本提示:打一行或什么都不打;任何失败都不改 rc
276
+ [ "${TEAM_UPDATE_CHECK:-1}" = "0" ] && return 0
277
+ local cache="$TEAM_STATE_DIR/update-check.env" now checked='' latest='' local_v="${TEAM_VERSION:-0.0.0}"
278
+ now="$(date +%s)"
279
+ if [ -f "$cache" ]; then
280
+ checked="$(sed -n 's/^CHECKED_AT=//p' "$cache" 2>/dev/null | head -1)"
281
+ latest="$(sed -n 's/^LATEST=//p' "$cache" 2>/dev/null | head -1)"
282
+ fi
283
+ case "$checked" in ''|*[!0-9]*) checked=0 ;; esac
284
+ if [ "$checked" = 0 ] || [ $((now - checked)) -ge 86400 ] || [ -z "$latest" ]; then
285
+ latest="$(team_update_lookup)"
286
+ [ -n "$latest" ] || latest='-'
287
+ team_update_cache_write "$now" "$latest" "$local_v"
288
+ fi
289
+ [ "$latest" = '-' ] && return 0
290
+ team_version_newer "$local_v" "$latest" || return 0
291
+ printf 'i 本地 teamsmith %s,npm 上已有 %s(工具从不自己安装):npm i -g teamsmith@%s --registry https://registry.npmjs.org && bash "$(npm root -g)/teamsmith/install.sh"\n' \
292
+ "$local_v" "$latest" "$latest"
293
+ return 0
294
+ }
295
+
126
296
  # ---------------------------------------------------------------- team up
127
297
  # 人来跑的工具:把 PM 恢复起来。
128
298
  # agent 归 PM 管,所以默认不动 agent;要顺手把停了的 agent 也续起来就加 --agents。
129
299
  team_cmd_up() {
130
- local with_agents=0 show_prompt=0 pm_fail=0 pm_fresh=0
300
+ local with_agents=0 show_prompt=0 pm_fail=0 pm_fresh=0 no_migrate=0 adopt_pm=0 register=0
301
+ local -a up_argv=("$@")
131
302
  while [ $# -gt 0 ]; do
132
303
  case "$1" in
133
304
  --agents) with_agents=1; shift ;;
134
305
  --fresh-pm) pm_fresh=1; shift ;;
135
306
  --print) show_prompt=1; shift ;;
307
+ --no-migrate) no_migrate=1; shift ;;
308
+ --adopt-pm-identity) adopt_pm=1; shift ;;
136
309
  -*) team_usage_die "up: 未知参数 $1" ;;
137
310
  *) team_usage_die "up: 多余参数 $1" ;;
138
311
  esac
@@ -141,13 +314,40 @@ team_cmd_up() {
141
314
  # 导出给同一次启动里的 team_pm_pi_args / team_pm_launch_cmd / team_pm_continuity。
142
315
  [ "$pm_fresh" = "1" ] && export TEAM_PM_FRESH_LAUNCH=1
143
316
 
317
+ # P261 ①(design §1):迁移决策在所有其它步骤**之前**(身份守卫之前、建场地之前、拉起 PM 之前)。
318
+ local up_mode=real mig=0
319
+ [ "$show_prompt" = "1" ] && up_mode=print
320
+ [ "$no_migrate" = "1" ] && up_mode=no-migrate
321
+ # 建议命令里打印的是**将要使用的**备份路径(真跑时才创建);plan 已就绪时先算出来。
322
+ TEAM_MIGRATION_BACKUP_HINT=''
323
+ team_up_migration_step "$up_mode" "${up_argv[@]}" || mig=$?
324
+ if [ "$mig" = 1 ]; then
325
+ team_up_backup_note
326
+ return 1
327
+ fi
328
+
144
329
  if [ "$show_prompt" = "1" ]; then
330
+ if [ "$mig" = 10 ]; then
331
+ # 旧配置的 --print:只预告(逐键表 + 备份路径 + 身份预告),不打印基于**未迁移**值的启动命令,
332
+ # 也不为预览开 guard 投影通道(design §6 裁定点 7);迁移可用时退 0。
333
+ team_pm_identity_preview || true
334
+ printf '\n(迁移后重跑 %s up --print 才有完整启动预览;迁移可用时 --print 退 0)\n' "$TEAM_CLI"
335
+ printf '(本可检查 npm 上的新版本:--print 下不联网、不写缓存)\n'
336
+ return 0
337
+ fi
338
+ if ! team_pm_identity_guard; then
339
+ # --print 不写盘(design §6/§7):adopt 的写入只发生在真实启动路径上。
340
+ [ "$adopt_pm" != 1 ] || team_dim " --print 不写盘:--adopt-pm-identity 的写入只发生在真实启动时(去掉 --print 重跑)" >&2
341
+ team_up_backup_note; return 1
342
+ fi
343
+ team_pm_identity_preview || true
145
344
  team_pm_prompt
146
345
  # P36(A):--print 是「看清这一次启动」的地方 —— 会话判定(fresh:/continued:/lost:)与渲染出的
147
346
  # 完整命令都要看得见、能断言(有没有 -c 一眼可见)。不写任何状态、不动 tmux。
148
347
  printf '\n—— 本次启动 ——\n会话判定:%s\n完整命令:%s\n' \
149
348
  "$(team_pm_continuity)" \
150
349
  "$(team_pm_launch_cmd "$(team_pm_prompt_file)" "$(team_pm_spawn_file)")"
350
+ printf '(本可检查 npm 上的新版本:--print 下不联网、不写缓存)\n'
151
351
  return 0
152
352
  fi
153
353
 
@@ -156,11 +356,28 @@ team_cmd_up() {
156
356
  team_assert_own_session "team up" || team_die "team up 拒绝在「不属于本项目的 session」上动手(见上)"
157
357
  team_hdr "teamsmith up · $TEAM_PROJECT(只负责 PM)"
158
358
 
359
+ # Running/starting PM is never restarted by a policy edit. Otherwise guard BEFORE windows.
360
+ case "$(team_pm_state)" in
361
+ running:*|starting:*)
362
+ # P261:adopt 声明的是**这次续跑**的身份,而运行中的 PM 的身份就是记录里那一个(政策改变不重启它)。
363
+ # 这时写记录会把它改成「没有在跑的那个模型」,下一次续跑就接着一份对不上的会话 —— 拒绝,并给两条路。
364
+ if [ "$adopt_pm" = "1" ]; then
365
+ team_err "--adopt-pm-identity:PM 正在运行/正在起($(team_pm_state)),它的身份是记录里那一个;这次不写记录"
366
+ team_dim " 要换续跑身份:等 PM 停了再 $TEAM_CLI up --adopt-pm-identity [--yes];要换 PM 模型:$TEAM_CLI up --fresh-pm" >&2
367
+ return 1
368
+ fi ;;
369
+ *)
370
+ if [ "$adopt_pm" = "1" ]; then
371
+ team_pm_identity_adopt "${TEAM_ASSUME_YES:-0}" || { team_up_backup_note; return 1; }
372
+ fi
373
+ team_pm_identity_guard || { team_up_backup_note; return 1; }
374
+ register=0; [ "${TEAM_PM_IDENTITY_REGISTER:-0}" = 1 ] && register=1 ;;
375
+ esac
159
376
  # 1) tmux 场地:人跑 up 就是明确要求“把工地建起来”,所以这里允许建 session/窗口
160
377
  if ! team_tmux_has_session "$TEAM_SESSION"; then
161
378
  team_warn "tmux session '$TEAM_SESSION' 不存在:重建"
162
379
  team_tmux_ensure_session
163
- tmux set-option -t "$TEAM_SESSION" destroy-unattached off >/dev/null 2>&1 || true
380
+ team_tmux set-option -t "$TEAM_SESSION" destroy-unattached off >/dev/null 2>&1 || true
164
381
  fi
165
382
  if ! team_pm_window_exists; then
166
383
  team_tmux_new_window "$TEAM_SESSION" "$TEAM_PM_WINDOW" || true
@@ -179,7 +396,7 @@ team_cmd_up() {
179
396
  idle:*) team_warn "PM 没在跑(空提示符):启动 $(team_pm_cli_name)"
180
397
  team_pm_same_dir_warn
181
398
  if team_pm_start; then
182
- team_ok "PM 已启动(cli=$(team_pm_cli_name),proof=$(team_pm_proof || echo '?'),model=${TEAM_PM_MODEL:-$TEAM_DEFAULT_MODEL})"
399
+ team_ok "PM 已启动(cli=$(team_pm_cli_name),proof=$(team_pm_proof || echo '?'),$(team_pm_identity_claim))"
183
400
  else
184
401
  pm_fail=1
185
402
  team_err "PM 启动失败:请手动到 $TEAM_SESSION:$TEAM_PM_WINDOW 里跑 $(team_pm_bin_path);诊断见 $(team_pm_launch_failed_log)"
@@ -191,7 +408,7 @@ team_cmd_up() {
191
408
  team_warn "PM 窗口里有非 PM 进程(${pm_state#unknown:},cwd=$_ucwd):不算存活"
192
409
  team_pm_same_dir_warn
193
410
  if team_pm_start; then
194
- team_ok "PM 已启动(替换了非 PM 进程;cli=$(team_pm_cli_name),proof=$(team_pm_proof || echo '?'),model=${TEAM_PM_MODEL:-$TEAM_DEFAULT_MODEL})"
411
+ team_ok "PM 已启动(替换了非 PM 进程;cli=$(team_pm_cli_name),proof=$(team_pm_proof || echo '?'),$(team_pm_identity_claim))"
195
412
  else
196
413
  pm_fail=1
197
414
  team_err "PM 启动失败:请手动到 $TEAM_SESSION:$TEAM_PM_WINDOW 里跑 $(team_pm_bin_path);诊断见 $(team_pm_launch_failed_log)"
@@ -222,13 +439,29 @@ team_cmd_up() {
222
439
  team_dim " agent 不归 pulse/up 管:需要续跑时跑 $TEAM_CLI resume [--dry-run]"
223
440
  fi
224
441
 
442
+ # P261 ②(design §7):缺记录时登记本次启动身份并说明(写入由成功启动后的 publish 完成);
443
+ # --adopt-pm-identity 的声明行由 team_pm_identity_adopt 打印(那一刻就写完了)。
444
+ if [ "$register" = 1 ] && [ "$pm_fail" != 1 ]; then
445
+ if team_pm_metadata_load 2>/dev/null; then
446
+ printf 'i 此前没有启动记录;已登记本次启动身份:model=%s resume_key=%s mode=%s(要换 PM 模型必须 --fresh-pm)\n' \
447
+ "$TEAM_PM_SELECTED_MODEL" "$TEAM_PM_RECORDED_RESUME_KEY" "$TEAM_PM_RECORDED_MODE"
448
+ else
449
+ printf 'i 此前没有启动记录;本次启动的身份登记未确认(记录读不回来)\n'
450
+ fi
451
+ fi
452
+
225
453
  if team_in_standby; then
226
454
  team_warn "注意:当前 standby on(原因:$(team_standby_reason || echo -))。pulse 不会自动叫醒 PM;处理好后跑 $TEAM_CLI standby off"
227
455
  fi
228
456
 
229
457
  team_info ""
230
458
  team_capacity_line
459
+ # P276:孤儿席位进程读数(启动决策**之后**的一行;只读、不发信号)
460
+ team_orphan_up_line
231
461
  team_dim " 旁观:tmux attach -t $TEAM_SESSION | 待办:$TEAM_CLI digest"
462
+ # P261 ③(design §8):版本提示在启动决策**之后**,任何失败都不改 rc、不启动/停止任何东西。
463
+ team_update_check_notice || true
464
+ team_up_backup_note
232
465
  # 状态就是承诺:PM 没起来就不许退 0(否则脚本里的 `team up && …` 会把失败当成功)
233
466
  [ "$pm_fail" = "1" ] && return 1
234
467
  return 0
@@ -237,7 +470,7 @@ team_cmd_up() {
237
470
  # ---------------------------------------------------------------- team resume(PM 的工具)
238
471
  # 注意:watchdog 不调用这个。agent 的启停由 PM 决定(PM 开场会 --dry-run 看一眼)。
239
472
  team_cmd_resume() {
240
- local only="" all=1 dry=0 quiet=0
473
+ local only="" all=1 dry=0 quiet=0 failed=0
241
474
  while [ $# -gt 0 ]; do
242
475
  case "$1" in
243
476
  --agent) only="${2:?}"; all=0; shift 2 ;;
@@ -245,7 +478,7 @@ team_cmd_resume() {
245
478
  --dry-run) dry=1; shift ;;
246
479
  --quiet) quiet=1; shift ;;
247
480
  -*) team_usage_die "resume: 未知参数 $1" ;;
248
- # 位置参数也当 agent 名(erp 反馈:team resume dev 应该能用)
481
+ # 位置参数也当 agent 名(外部项目反馈:team resume dev 应该能用)
249
482
  *) if [ -z "$only" ]; then only="$1"; all=0; shift; else team_usage_die "resume: 多余参数 $1"; fi ;;
250
483
  esac
251
484
  done
@@ -257,18 +490,30 @@ team_cmd_resume() {
257
490
  for a in $(team_agents); do
258
491
  [ -n "$only" ] && [ "$a" != "$only" ] && continue
259
492
  task="$(team_state_get "$a" task '')"
260
- [ -n "$task" ] || continue
493
+ if [ -z "$task" ]; then
494
+ if [ -n "$only" ]; then
495
+ team_err "$a: no recorded assignment to resume;保留磁盘交接后显式 dispatch <task> <brief> --fresh"
496
+ failed=1
497
+ fi
498
+ continue
499
+ fi
261
500
  if team_agent_live "$a"; then live=$((live + 1)); continue; fi
262
501
  taskfile="$(team_state_get "$a" taskfile '')"
263
502
  if [ ! -f "$taskfile" ]; then
264
- team_warn "$a:任务 $task 的任务书不见了($taskfile)→ 写 thread,跳过"
503
+ team_warn "$a:任务 $task 的任务书不见了($taskfile)→ 跳过"
504
+ failed=1
505
+ [ "$dry" != 1 ] || continue
265
506
  team_cmd_thread "$a" --re "$task" "pulse 想续跑 $task,但任务书 $taskfile 不存在。请 PM 重新生成或改用 --task 指定。" >/dev/null 2>&1 || true
266
507
  team_inbox_append "$a" blocked "pulse: 任务书丢失,无法续跑 $task"
267
508
  continue
268
509
  fi
269
510
  n=$((n + 1))
270
511
  if [ "$dry" = "1" ]; then
512
+ # P240:dry-run 是 PM「一眼看清哪些席位有活可续」的清单入口(workflows.md:the PM looks for itself)。
513
+ # P228 的合同要求 dry-run 解析与真派单同一套预检/计划(退非 0、不写盘),那条走下面的 --print;
514
+ # 但清单行本身是既有输出契约(--select 11c / 6k⑥ / M9.3-③ 都钉它),两个都要在:先列清单,再看计划。
271
515
  printf ' %s 可续跑:%s → %s\n' "$a" "$task" "$taskfile"
516
+ team_cmd_dispatch "$a" "$task" "$taskfile" --print || failed=1
272
517
  else
273
518
  team_info " 续跑 $a · $task"
274
519
  # M9.3:resume / `up --agents` 与 dispatch 共用同一扇门,而它们派的**永远是这个 agent 自己
@@ -278,6 +523,7 @@ team_cmd_resume() {
278
523
  team_wlog "resume agent=$a task=$task"
279
524
  else
280
525
  # 常见两类:容量/并发排队(正常),或工作树状态不允许(需要 PM 处理)
526
+ failed=1
281
527
  team_warn "$a 续跑没成功:看上面一行原因(容量/并发属正常排队;工作树脏/分支不对要 PM 处理)"
282
528
  fi
283
529
  fi
@@ -285,7 +531,7 @@ team_cmd_resume() {
285
531
  if [ "$n" -eq 0 ]; then
286
532
  [ "$quiet" != "1" ] && team_dim " 没有需要续跑的 agent(在跑 $live 个)"
287
533
  fi
288
- return 0
534
+ return "$failed"
289
535
  }
290
536
 
291
537
  # ---------------------------------------------------------------- team watch
@@ -396,6 +642,10 @@ team_watch_once() {
396
642
  return 0
397
643
  fi
398
644
 
645
+ if ! team_pm_identity_guard; then
646
+ team_wlog "PM continuity blocked;人工 $TEAM_CLI up --fresh-pm;不计 attempt/restart,不建窗"
647
+ return 0
648
+ fi
399
649
  # ③c 有待办但 PM 没在跑 → 把它拉起来(除非 tmux 场地不在且不允许重建)
400
650
  # idle(空提示符)与 unknown(本项目里的非 PM 占用者)都算「没有 PM」;foreign 不碰。
401
651
  case "$st" in
@@ -410,7 +660,7 @@ team_watch_once() {
410
660
  if ! team_tmux_has_session "$TEAM_SESSION"; then
411
661
  team_wlog "tmux session 丢失,重建(TEAM_PULSE_REBUILD_TMUX=1)"
412
662
  team_tmux_ensure_session
413
- tmux set-option -t "$TEAM_SESSION" destroy-unattached off >/dev/null 2>&1 || true
663
+ team_tmux set-option -t "$TEAM_SESSION" destroy-unattached off >/dev/null 2>&1 || true
414
664
  fi
415
665
  if ! team_pm_window_exists; then
416
666
  team_wlog "PM 窗口丢失,重建(TEAM_PULSE_REBUILD_TMUX=1)"
@@ -597,7 +847,8 @@ team_panel_capacity_json() {
597
847
  fi
598
848
  [ -n "${ram:-}" ] || ram=0
599
849
  local p t a it iff id seen=" " entries="" avail_json ino_json readable
600
- for p in "${TMPDIR:-/tmp}" "${TEAM_MAIN_ROOT:-$PWD}/${TEAM_WORKTREES_DIR:-.worktrees}"; do
850
+ local temp_root; temp_root="$(team_tmp_root 2>/dev/null || true)"
851
+ for p in "$temp_root" "${TEAM_MAIN_ROOT:-$PWD}/${TEAM_WORKTREES_DIR:-.worktrees}"; do
601
852
  [ -n "$p" ] || continue
602
853
  team__disk_stats "$p"; t="$_D_T"; a="$_D_A"; it="$_D_IT"; iff="$_D_IFF"; id="$_D_ID"
603
854
  if [ -n "$id" ]; then
@@ -611,9 +862,11 @@ team_panel_capacity_json() {
611
862
  fi
612
863
  entries="$entries${entries:+, }{\"path\": $(team_panel_json_str "$p"), \"avail_mb\": $avail_json, \"free_inodes\": $ino_json, \"readable\": $readable}"
613
864
  done
614
- printf '{"ram_avail_mb": %s, "swap_free_mb": %s, "agents": %s, "disk": [%s], "spark": %s}\n' \
865
+ printf '{"ram_avail_mb": %s, "swap_free_mb": %s, "agents": %s, "temp_root": %s, "temp_root_source": %s, "disk": [%s], "spark": %s}\n' \
615
866
  "$(team_panel_num "$ram")" "$(team_panel_num "$diskfree")" \
616
- "$(team_panel_num "$(team_agent_capacity)")" "$entries" "$spark"
867
+ "$(team_panel_num "$(team_agent_capacity)")" \
868
+ "$(team_panel_json_str "${temp_root:-}")" "$(team_panel_json_str "$(team_tmp_root_source 2>/dev/null || printf 'default')")" \
869
+ "$entries" "$spark"
617
870
  [ "$degraded" = "0" ]
618
871
  }
619
872
 
@@ -823,7 +1076,7 @@ team_panel_activity_json() { # <1|0> <events>
823
1076
  # 本 session 里没有任何窗口 → 不翻别人的会话(也不给 monitor.mjs 传空 --only,那等于「全都要」)
824
1077
  [ -n "$only" ] || { printf '[]'; return 0; }
825
1078
  [ -n "${TEAM_AGENT_LOG_GLOB:-}" ] && g=(--log-glob "$TEAM_AGENT_LOG_GLOB")
826
- out="$("$runner" "$js" --root "$TEAM_MAIN_ROOT" --only "$only" --events "$events" \
1079
+ out="$("$runner" "$js" --root "$TEAM_MAIN_ROOT" --state-dir "$TEAM_STATE_DIR" --only "$only" --events "$events" \
827
1080
  ${g[@]+"${g[@]}"} --json 2>/dev/null || true)"
828
1081
  case "${out:-}" in
829
1082
  \[*) printf '%s' "$out" ;;
@@ -989,6 +1242,11 @@ team_panel_change_tasks_json() { # <change id> → "tasks": [...], "tasks_more":
989
1242
  team_panel_changes_json() {
990
1243
  local spec_dir dir bin listed="" src="dir" out="[" first=1 id done total age phase n_changes=0
991
1244
  spec_dir="$(team_spec_dir_abs)"; dir="$spec_dir/changes"
1245
+ # F3:`openspec`(或它到项目根之间的段)或 `openspec/changes` 是软链时,本块不列任何条目 ——
1246
+ # 只读预览的合同是「只读 openspec 目录内、不得读穿」,列表跟详情一样按同一条判据拒绝。
1247
+ if team_panel_detail_symlink_hit "$spec_dir" "$dir" >/dev/null; then
1248
+ printf '{"available": false, "source": "none", "changes": [], "count": 0}'; return 0
1249
+ fi
992
1250
  bin="$(team_openspec_bin_path 2>/dev/null || true)"
993
1251
  if [ -n "$bin" ] && command -v "$bin" >/dev/null 2>&1 && [ -d "$TEAM_MAIN_ROOT" ]; then
994
1252
  listed="$(cd "$TEAM_MAIN_ROOT" && "$bin" list 2>/dev/null | sed -n 's/^[[:space:]]\{2,\}\([^[:space:]]*\)[[:space:]]\{1,\}\([0-9][0-9]*\)\/\([0-9][0-9]*\).*/\1\t\2\t\3/p' || true)"
@@ -1028,6 +1286,10 @@ team_panel_changes_json() {
1028
1286
  team_panel_specs_json() {
1029
1287
  local spec_dir bin listed="" src="dir" out="[" first=1 name reqs count=0 total=0 line
1030
1288
  spec_dir="$(team_spec_dir_abs)"
1289
+ # F3:同 changes —— `openspec`(含到项目根之间的段)或 `openspec/specs` 是软链时不列。
1290
+ if team_panel_detail_symlink_hit "$spec_dir" "$spec_dir/specs" >/dev/null; then
1291
+ printf '{"available": false, "count": 0, "requirements": 0, "specs": []}'; return 0
1292
+ fi
1031
1293
  bin="$(team_openspec_bin_path 2>/dev/null || true)"
1032
1294
  if [ -n "$bin" ] && command -v "$bin" >/dev/null 2>&1 && [ -d "$TEAM_MAIN_ROOT" ]; then
1033
1295
  listed="$(cd "$TEAM_MAIN_ROOT" && "$bin" list --specs 2>/dev/null | sed -n 's/^[[:space:]]\{2,\}\([^[:space:]]*\)[[:space:]]\{1,\}requirements[[:space:]]\{1,\}\([0-9][0-9]*\).*/\1\t\2/p' || true)"
@@ -1062,6 +1324,10 @@ team_panel_specs_json() {
1062
1324
 
1063
1325
  team_panel_decisions_json() { # DECISIONS.md:标题数 + 最新三条(新→旧)
1064
1326
  local f="$TEAM_DOCS_ABS/DECISIONS.md" out="[" first=1 line count=0
1327
+ # F3:`docs`(或它到项目根之间的段)是软链时,条目不列(详情面会点名那一段)。
1328
+ if team_panel_detail_symlink_hit "$TEAM_DOCS_ABS" "$f" >/dev/null; then
1329
+ printf '{"count": 0, "recent": []}'; return 0
1330
+ fi
1065
1331
  [ -f "$f" ] || { printf '{"count": 0, "recent": []}'; return 0; }
1066
1332
  count="$(grep -c '^## ' "$f" 2>/dev/null || true)"
1067
1333
  while IFS= read -r line; do
@@ -1161,57 +1427,275 @@ team_panel_settings_json() {
1161
1427
  # `--file` 只服务**发现集里的**路径:任何别的路径都非 0 且不打印内容——面板不会变成任意文件读取器。
1162
1428
  # 正文上限 128 KiB(超出截断并带 truncated 标记),UTF-8 边界用 iconv -c 收尾。
1163
1429
 
1164
- # 发现记录:每行 `<相对路径>\t<tab 名>\t<字节数>`,顺序 = 任务书 → 报告 → 复验。
1165
- team_panel_detail_records() { # <id>
1430
+ # 发现记录:每行 `<相对路径>\t<tab 名>\t<字节数>\t<软链 0|1>`,顺序 = 树上/账本上的呈现序。
1431
+ # source 缺省 entry(既有语义逐字不变);spec/change/decision 三类见各自函数。
1432
+ # 软链的路径照旧出现在记录里但带 `1` 标记:json 把它排除出发现集(`--file` 选不到它)并在 reason 里点名。
1433
+ team_panel_detail_rel() { # <绝对路径> → 相对 main root(不在 root 下时原样)
1434
+ local f="$1"
1435
+ case "$f" in "$TEAM_MAIN_ROOT"/*) printf '%s' "${f#"$TEAM_MAIN_ROOT"/}" ;; *) printf '%s' "$f" ;; esac
1436
+ }
1437
+
1438
+ # F1 返修:目录段软链与叶软链一视同仁 —— 只挡叶节点时,`changes/<id>` / `specs/<cap>` 这一层是指向
1439
+ # 根外(读穿 openspec 根)或根内另一个 change / `archive/`(越权服务)的软链,都会被当成正常路径。
1440
+ # F3 返修:锚点从**源根**提到**项目根** —— `openspec` 本身、`docs` 本身也在被读路径上,必须一起判;
1441
+ # 只从源根往下走,项目根到源根之间的段是软链时四类预览照样读树外正文。
1442
+ # 沿**项目根 → 候选路径**逐段判定:命中软链就打印**那一段**(相对 main root)并退 0;整条路径没有软链退 1。
1443
+ # 包含关系仍按调用方给的允许根判(`p` 必须在 `root` 之下);项目根自己(`p == TEAM_MAIN_ROOT`)不判 ——
1444
+ # 项目根本身是软链属于本机布局,不在本合同的拒绝面里(路径前缀由 `read_nofollow` 的物理路径比对处理)。
1445
+ team_panel_detail_symlink_hit() { # <允许根 abs> <候选路径 abs>
1446
+ local root="$1" p="$2" rel cur c
1447
+ case "$root" in "$TEAM_MAIN_ROOT"|"$TEAM_MAIN_ROOT"/*) ;; *) return 1 ;; esac
1448
+ case "$p" in "$root"|"$root"/*) ;; *) return 1 ;; esac
1449
+ rel="${p#"$TEAM_MAIN_ROOT"/}"
1450
+ [ "$rel" != "$p" ] || return 1
1451
+ cur="$TEAM_MAIN_ROOT"
1452
+ local IFS=/
1453
+ for c in $rel; do
1454
+ [ -n "$c" ] || continue
1455
+ cur="$cur/$c"
1456
+ if [ -L "$cur" ]; then team_panel_detail_rel "$cur"; return 0; fi
1457
+ done
1458
+ return 1
1459
+ }
1460
+
1461
+ team_panel_detail_source_root() { # <source> → 该 source 的允许根(entry/decision 不走软链目录判定)
1462
+ case "$1" in
1463
+ spec) printf '%s/specs' "$(team_spec_dir_abs)" ;;
1464
+ change) printf '%s/changes' "$(team_spec_dir_abs)" ;;
1465
+ esac
1466
+ }
1467
+
1468
+ # `设备:inode` 读数:GNU(-c)优先、BSD/macOS(-f)兜底(仓里既有写法,如 common.sh 的 `stat -c … || stat -f …`)。
1469
+ # follow = -L(跟到目标,软链给的是目标身份);nofollow = 不跟(lstat,软链给的是**链接自己**的 inode)。
1470
+ team_panel_detail_stat_id() { # <follow|nofollow> <路径>
1471
+ case "$1" in
1472
+ follow) stat -Lc '%d:%i' -- "$2" 2>/dev/null || stat -Lf '%d:%i' -- "$2" 2>/dev/null ;;
1473
+ nofollow) stat -c '%d:%i' -- "$2" 2>/dev/null || stat -f '%d:%i' -- "$2" 2>/dev/null ;;
1474
+ esac
1475
+ }
1476
+
1477
+ # F2 返修:把「判」与「读」合成原子 —— 一次 open,之后只从**同一个 fd** 读。
1478
+ # 两轮动过的三种窗口都在这一个函数里关掉:
1479
+ # ① fd 打开到的目标(`stat -L`)必须等于 ② 路径此刻的不跟随身份(`stat`)—— 叶节点在 open 前/后被换成
1480
+ # 软链、或被换成另一个文件,两者必不相同(第一轮承诺的「发现之后、读之前被换也不读」在这里才成立);
1481
+ # ③ 路径的每一段(含允许根本身)此刻都不是软链 —— 中间目录在 open 之后被换成软链也会命中(① 对
1482
+ # 中间段无能为力:lstat 会跟完前缀,两侧拿到的都是树外那个目标)。
1483
+ # 正文只从 ① 里那个 fd 读,所以「判据作用的对象」与「读出的内容」是同一个 inode。
1484
+ # rc=0 读到(可能空正文 —— 与今天 `head -c` 打不开同形);rc=3 被换/命中软链(调用者按「软链拒绝」点名);
1485
+ # rc=4 既没有 /proc/self/fd 也没有 /dev/fd:拿不到 fd 身份就不读(宁可拒,也不拿可能被换过的路径去读)。
1486
+ # `<cap>` 写 `-` = 读到 EOF(决策源要拿到整篇才能切片)。
1487
+ team_panel_detail_read_nofollow() { # <允许根|-> <abs> <cap>
1488
+ local root="$1" abs="$2" cap="$3" id_fd="" id_path="" p
1489
+ { exec 9<"$abs"; } 2>/dev/null || return 0
1490
+ for p in /proc/self/fd/9 /dev/fd/9; do
1491
+ id_fd="$(team_panel_detail_stat_id follow "$p")"
1492
+ [ -n "$id_fd" ] && break
1493
+ done
1494
+ if [ -z "$id_fd" ]; then exec 9<&-; return 4; fi
1495
+ id_path="$(team_panel_detail_stat_id nofollow "$abs")"
1496
+ if [ -z "$id_path" ] || [ "$id_fd" != "$id_path" ]; then exec 9<&-; return 3; fi
1497
+ # F3/F4 返修:判据作用于**读的那个对象** —— 句柄自己解析出的路径(open 那一刻固定)必须逐字节等于
1498
+ # 「锚点真实路径 + 字面相对路径」。锚点默认是**项目根**,所以 `openspec`/`docs` 本身、中间目录、叶节点
1499
+ # 在 open 时是不是软链,在这一条里一次性可见(F2 的「读前再判一次」是按路径名走的,中间段换回真目录
1500
+ # 就放行)。允许根不在项目树内(例如绝对路径的 TEAM_DOCS_DIR)时退回允许根做锚点:那类布局没有更外层
1501
+ # 的可信锚点,仍按叶/前缀的物理路径比对。拿不到 fd 路径或锚点就拒绝(fail-closed)。
1502
+ local fd_real root_real rel_real anchor="$TEAM_MAIN_ROOT"
1503
+ case "$root" in
1504
+ -|'') anchor="" ;;
1505
+ "$TEAM_MAIN_ROOT"|"$TEAM_MAIN_ROOT"/*) ;;
1506
+ *) anchor="$root" ;;
1507
+ esac
1508
+ # 判据无条件生效(没有「某条件不满足就跳过」的旁路):候选路径**必须**落在锚点之下,且句柄解析出的
1509
+ # 路径**必须逐字节等于**「锚点真实路径 + 字面相对路径」。现有调用图里 abs 全部由允许根与发现集拼出,
1510
+ # 前缀不匹配这条分支不可达;把它写成拒绝(而不是跳过)是为了让未来的调用方出错时也 fail-closed。
1511
+ if [ -n "$anchor" ]; then
1512
+ case "$abs" in "$anchor"/*) rel_real="${abs#"$anchor"/}" ;; *) exec 9<&-; return 3 ;; esac
1513
+ fd_real="$(readlink /proc/self/fd/9 2>/dev/null || readlink /dev/fd/9 2>/dev/null)"
1514
+ root_real="$(cd "$anchor" 2>/dev/null && pwd -P)"
1515
+ if [ -z "$fd_real" ] || [ -z "$root_real" ] || [ "$fd_real" != "$root_real/$rel_real" ]; then
1516
+ exec 9<&-; return 3
1517
+ fi
1518
+ fi
1519
+ if [ -n "$root" ] && [ "$root" != - ] && team_panel_detail_symlink_hit "$root" "$abs" >/dev/null; then
1520
+ exec 9<&-; return 3
1521
+ fi
1522
+ if [ "$cap" = - ]; then
1523
+ iconv -c -f UTF-8 -t UTF-8 <&9 2>/dev/null || true
1524
+ else
1525
+ head -c "$cap" <&9 2>/dev/null | iconv -c -f UTF-8 -t UTF-8 2>/dev/null || true
1526
+ fi
1527
+ exec 9<&-
1528
+ return 0
1529
+ }
1530
+
1531
+ team_panel_detail_records_entry() { # <id>
1166
1532
  local LC_ALL=C
1167
- local id="$1" f name suffix tab size rel first
1533
+ local id="$1" f name suffix tab size rel first=1 refused=0 hit
1534
+ # F3:`docs`(或它到项目根之间的段)是软链时,整条发现集拒绝并点名那一段(与 spec/change 同形)。
1535
+ if hit="$(team_panel_detail_symlink_hit "$TEAM_DOCS_ABS" "$TEAM_DOCS_ABS")"; then
1536
+ printf '%s\t%s\t%s\t%s\n' "$hit" "brief" "0" "1"
1537
+ return 0
1538
+ fi
1168
1539
  [ -d "$TEAM_DOCS_ABS" ] || return 0
1169
- first=1
1170
1540
  for f in "$TEAM_DOCS_ABS/tasks/$id"-*.md; do
1171
1541
  [ -f "$f" ] || continue
1172
1542
  name="${f##*/}"; suffix="${name#"$id"-}"; suffix="${suffix%.md}"
1173
1543
  tab="brief"; [ "$first" = 1 ] || tab="brief:$suffix"; first=0
1174
1544
  size="$(wc -c < "$f" 2>/dev/null | tr -d ' ')"
1175
- rel="${f#"$TEAM_MAIN_ROOT"/}"
1176
- printf '%s\t%s\t%s\n' "$rel" "$tab" "${size:-0}"
1545
+ rel="$(team_panel_detail_rel "$f")"
1546
+ refused=0; [ -L "$f" ] && refused=1
1547
+ printf '%s\t%s\t%s\t%s\n' "$rel" "$tab" "${size:-0}" "$refused"
1177
1548
  done
1178
1549
  for f in "$TEAM_DOCS_ABS/reports/$id"-*.md; do
1179
1550
  [ -f "$f" ] || continue
1180
1551
  name="${f##*/}"; suffix="${name#"$id"-}"; suffix="${suffix%.md}"
1181
1552
  size="$(wc -c < "$f" 2>/dev/null | tr -d ' ')"
1182
- rel="${f#"$TEAM_MAIN_ROOT"/}"
1183
- printf '%s\t%s\t%s\n' "$rel" "report:$suffix" "${size:-0}"
1553
+ rel="$(team_panel_detail_rel "$f")"
1554
+ refused=0; [ -L "$f" ] && refused=1
1555
+ printf '%s\t%s\t%s\t%s\n' "$rel" "report:$suffix" "${size:-0}" "$refused"
1184
1556
  done
1185
1557
  f="$TEAM_DOCS_ABS/reviews/$id.md"
1186
1558
  if [ -f "$f" ]; then
1187
1559
  size="$(wc -c < "$f" 2>/dev/null | tr -d ' ')"
1188
- rel="${f#"$TEAM_MAIN_ROOT"/}"
1189
- printf '%s\t%s\t%s\n' "$rel" "review" "${size:-0}"
1560
+ rel="$(team_panel_detail_rel "$f")"
1561
+ refused=0; [ -L "$f" ] && refused=1
1562
+ printf '%s\t%s\t%s\t%s\n' "$rel" "review" "${size:-0}" "$refused"
1190
1563
  fi
1191
1564
  for f in "$TEAM_DOCS_ABS/reviews/$id"-*.md; do
1192
1565
  [ -f "$f" ] || continue
1193
1566
  name="${f##*/}"; suffix="${name#"$id"-}"; suffix="${suffix%.md}"
1194
1567
  size="$(wc -c < "$f" 2>/dev/null | tr -d ' ')"
1195
- rel="${f#"$TEAM_MAIN_ROOT"/}"
1196
- printf '%s\t%s\t%s\n' "$rel" "review:$suffix" "${size:-0}"
1568
+ rel="$(team_panel_detail_rel "$f")"
1569
+ refused=0; [ -L "$f" ] && refused=1
1570
+ printf '%s\t%s\t%s\t%s\n' "$rel" "review:$suffix" "${size:-0}" "$refused"
1197
1571
  done
1198
1572
  return 0
1199
1573
  }
1200
1574
 
1575
+ # source spec:`<spec_dir>/specs/<capability>/spec.md`(唯一的那个文件,tab `spec`)。
1576
+ team_panel_detail_records_spec() { # <capability>
1577
+ local id="$1" f size rel refused=0 root hit
1578
+ root="$(team_spec_dir_abs)/specs"
1579
+ f="$root/$id/spec.md"
1580
+ # 目录段软链先判(`-f` 会跟随它):目标在不在都拒绝并点名,不拿软链之外的正文充数。
1581
+ if hit="$(team_panel_detail_symlink_hit "$root" "$f")"; then
1582
+ printf '%s\t%s\t%s\t%s\n' "$hit" "spec" "0" "1"
1583
+ return 0
1584
+ fi
1585
+ [ -f "$f" ] || return 0
1586
+ size="$(wc -c < "$f" 2>/dev/null | tr -d ' ')"
1587
+ rel="$(team_panel_detail_rel "$f")"
1588
+ [ -L "$f" ] && refused=1
1589
+ printf '%s\t%s\t%s\t%s\n' "$rel" "spec" "${size:-0}" "$refused"
1590
+ }
1591
+
1592
+ # source change:该 change 自己的 proposal/design/tasks(按此序,缺的不补;archive 到不了)。
1593
+ team_panel_detail_records_change() { # <change id>
1594
+ local id="$1" dir f name size rel refused=0 root hit
1595
+ root="$(team_spec_dir_abs)/changes"
1596
+ dir="$root/$id"
1597
+ # 同上:`changes/<id>` 是软链(指根外、指别的 change、指 archive/)时整条拒绝并点名那一段。
1598
+ if hit="$(team_panel_detail_symlink_hit "$root" "$dir")"; then
1599
+ printf '%s\t%s\t%s\t%s\n' "$hit" "change" "0" "1"
1600
+ return 0
1601
+ fi
1602
+ [ -d "$dir" ] || return 0
1603
+ for name in proposal design tasks; do
1604
+ f="$dir/$name.md"
1605
+ [ -f "$f" ] || continue
1606
+ size="$(wc -c < "$f" 2>/dev/null | tr -d ' ')"
1607
+ rel="$(team_panel_detail_rel "$f")"
1608
+ refused=0; [ -L "$f" ] && refused=1
1609
+ printf '%s\t%s\t%s\t%s\n' "$rel" "$name" "${size:-0}" "$refused"
1610
+ done
1611
+ }
1612
+
1613
+ # 决策切片:条目标题 = `##`/`###` 且 id 后跟非数字;正文到「同层或更浅的下一个标题」为止,
1614
+ # 同 id 的更深层补记(`### D7 补记`)留在正文里。查无此号 → 无输出(由 json 给空表 + reason)。
1615
+ team_panel_detail_decision_slice() { # <D编号> [文件](缺省账本;`-` = 从 stdin 读)
1616
+ local id="$1" f="${2:-$TEAM_DOCS_ABS/DECISIONS.md}"
1617
+ [ "$f" = - ] || [ -f "$f" ] || return 0
1618
+ awk -v want="$id" '
1619
+ { lines[NR] = $0 }
1620
+ END {
1621
+ start = 0; level = 0
1622
+ for (i = 1; i <= NR; i++) {
1623
+ lv = 0
1624
+ if (lines[i] ~ /^### /) lv = 3; else if (lines[i] ~ /^## /) lv = 2
1625
+ if (lv == 0) continue
1626
+ h = lines[i]; sub(/^#+[ \t]+/, "", h)
1627
+ if (h ~ ("^" want "([^0-9]|$)")) { start = i; level = lv; break }
1628
+ }
1629
+ if (start == 0) { exit }
1630
+ end = NR
1631
+ for (i = start + 1; i <= NR; i++) {
1632
+ lv = 0
1633
+ if (lines[i] ~ /^### /) lv = 3; else if (lines[i] ~ /^## /) lv = 2
1634
+ if (lv > 0 && lv <= level) { end = i - 1; break }
1635
+ }
1636
+ for (i = start; i <= end; i++) print lines[i]
1637
+ }' "$f"
1638
+ }
1639
+
1640
+ # source decision:只服务所请求条目的切片(不提供整文件出口)。
1641
+ team_panel_detail_records_decision() { # <D编号>
1642
+ local id="$1" f="$TEAM_DOCS_ABS/DECISIONS.md" slice size rel refused=0 hit
1643
+ # F3:`docs`(或它到项目根之间的段)是软链时,账本条目整条拒绝并点名那一段(正文更是一个字节不读)。
1644
+ if hit="$(team_panel_detail_symlink_hit "$TEAM_DOCS_ABS" "$f")"; then
1645
+ printf '%s\t%s\t%s\t%s\n' "$hit" "$id" "0" "1"
1646
+ return 0
1647
+ fi
1648
+ [ -f "$f" ] || return 0
1649
+ slice="$(team_panel_detail_decision_slice "$id")"
1650
+ [ -n "$slice" ] || return 0
1651
+ size="$(printf '%s\n' "$slice" | wc -c | tr -d ' ')"
1652
+ rel="$(team_panel_detail_rel "$f")"
1653
+ [ -L "$f" ] && refused=1
1654
+ printf '%s\t%s\t%s\t%s\n' "$rel" "$id" "${size:-0}" "$refused"
1655
+ }
1656
+
1657
+ team_panel_detail_records() { # <id> [source]
1658
+ case "${2:-entry}" in
1659
+ entry) team_panel_detail_records_entry "$1" ;;
1660
+ spec) team_panel_detail_records_spec "$1" ;;
1661
+ change) team_panel_detail_records_change "$1" ;;
1662
+ decision) team_panel_detail_records_decision "$1" ;;
1663
+ esac
1664
+ return 0
1665
+ }
1666
+
1201
1667
  # 详情块:文件清单 + 被服务文件的正文(默认第一份;`--file` 指定时必须已在发现集里)。
1202
- team_panel_detail_json() { # <id> [file]
1203
- local id="${1:-}" want="${2:-}" cap="${TEAM_PANEL_DETAIL_CAP:-131072}"
1668
+ # `--source`(第三参)决定发现集;缺省 entry 与今天逐字一致。目标缺失不是错误:空文件表 + reason。
1669
+ team_panel_detail_json() { # <id> [file] [source]
1670
+ local id="${1:-}" want="${2:-}" source="${3:-entry}" cap="${TEAM_PANEL_DETAIL_CAP:-131072}"
1204
1671
  [ -n "$id" ] || { printf 'team: __panel-data --block detail 需要 --id <ID>\n' >&2; return 2; }
1205
- # id 直接进 glob:只接受字母数字与 . _ -(挡掉通配符与路径穿越;`board` 的 id 就长这样)。
1206
- case "$id" in
1207
- *[!A-Za-z0-9._-]*) printf 'team: __panel-data --block detail: 非法的 --id %s\n' "$id" >&2; return 2 ;;
1672
+ case "$source" in
1673
+ ''|entry|spec|change|decision) source="${source:-entry}" ;;
1674
+ *) printf 'team: __panel-data --block detail: 未知的 --source %s\n' "$source" >&2; return 2 ;;
1208
1675
  esac
1676
+ # id 直接进路径/glob:decision 只接受 `D<数字>`,其余 source 只接受字母数字与 . _ -,
1677
+ # 且明确拒绝 `.`/`..`(前者是 glob 通配符与穿越字符,后者会从 specs/<id>/ 爬上一层)。
1678
+ if [ "$source" = decision ]; then
1679
+ case "$id" in
1680
+ D[0-9]*) ;;
1681
+ *) printf 'team: __panel-data --block detail: 非法的 --id %s(decision 只接受 D<数字>)\n' "$id" >&2; return 2 ;;
1682
+ esac
1683
+ case "${id#D}" in
1684
+ ''|*[!0-9]*) printf 'team: __panel-data --block detail: 非法的 --id %s(decision 只接受 D<数字>)\n' "$id" >&2; return 2 ;;
1685
+ esac
1686
+ else
1687
+ case "$id" in
1688
+ *[!A-Za-z0-9._-]*|.|..) printf 'team: __panel-data --block detail: 非法的 --id %s\n' "$id" >&2; return 2 ;;
1689
+ esac
1690
+ fi
1209
1691
  local -a paths=() tabs=() sizes=()
1210
- local path tab size
1211
- while IFS=$'\t' read -r path tab size; do
1692
+ local path tab size flag refused=""
1693
+ while IFS=$'\t' read -r path tab size flag; do
1212
1694
  [ -n "$path" ] || continue
1695
+ # 软链不进发现集(`--file` 选不到、正文永不读穿),但在 reason 里点名。
1696
+ if [ "${flag:-0}" = 1 ]; then refused="${refused:+$refused · }$path"; continue; fi
1213
1697
  paths+=("$path"); tabs+=("$tab"); sizes+=("${size:-0}")
1214
- done < <(team_panel_detail_records "$id")
1698
+ done < <(team_panel_detail_records "$id" "$source")
1215
1699
  local n="${#paths[@]}" i pick=0
1216
1700
  if [ -n "$want" ]; then
1217
1701
  local found=-1
@@ -1232,20 +1716,106 @@ team_panel_detail_json() { # <id> [file]
1232
1716
  out="$out{\"tab\": $(team_panel_json_str "${tabs[$i]}"), \"name\": $(team_panel_json_str "${paths[$i]##*/}"), \"path\": $(team_panel_json_str "${paths[$i]}"), \"size\": $(team_panel_num "${sizes[$i]}"), \"truncated\": $t}"
1233
1717
  done
1234
1718
  out="$out]"
1235
- local file="" text="" truncated=false
1719
+ local file="" text="" truncated=false abs read_hit="" read_unchecked="" read_root="-" read_rc=0 decision_rel="" decision_body=""
1236
1720
  if [ "$n" -gt 0 ]; then
1237
1721
  file="${paths[$pick]}"
1238
- text="$(head -c "$cap" "$TEAM_MAIN_ROOT/$file" 2>/dev/null | iconv -c -f UTF-8 -t UTF-8 2>/dev/null || true)"
1722
+ case "$file" in /*) abs="$file" ;; *) abs="$TEAM_MAIN_ROOT/$file" ;; esac
1723
+ # 路径级复判(发现与读取两处都判):发现之后、读之前路径被换成软链也不读 —— 这里顺手点名命中的那一段。
1724
+ # F3:entry/decision 两类也判(锚点是项目根:`docs` 本身是软链时点名 `docs`)。
1725
+ case "$source" in
1726
+ spec|change)
1727
+ if read_hit="$(team_panel_detail_symlink_hit "$(team_panel_detail_source_root "$source")" "$abs")"; then
1728
+ file=""
1729
+ fi ;;
1730
+ entry|decision)
1731
+ if read_hit="$(team_panel_detail_symlink_hit "$TEAM_DOCS_ABS" "$abs")"; then
1732
+ file=""
1733
+ fi ;;
1734
+ esac
1735
+ if [ -n "$read_hit" ]; then
1736
+ : # 命中软链:正文一个字节都不读(下面 reason 里点名)
1737
+ elif [ "$source" = decision ]; then
1738
+ # 决策源也是「发现到的路径」(合同把三类 source 的软链都点名了):先原子读账本,再从**读到的那份正文**
1739
+ # 切片 —— 账本在发现之后被换成软链,切出来的一样不是树外内容。
1740
+ decision_rel="$(team_panel_detail_rel "$TEAM_DOCS_ABS/DECISIONS.md")"
1741
+ decision_body="$(team_panel_detail_read_nofollow "$TEAM_DOCS_ABS" "$TEAM_DOCS_ABS/DECISIONS.md" -)" || read_rc=$?
1742
+ case "$read_rc" in
1743
+ 3) read_hit="$decision_rel"; file="" ;;
1744
+ 4) read_unchecked="$decision_rel"; file="" ;;
1745
+ *) text="$(printf '%s\n' "$decision_body" | team_panel_detail_decision_slice "$id" - | head -c "$cap" | iconv -c -f UTF-8 -t UTF-8 2>/dev/null || true)" ;;
1746
+ esac
1747
+ else
1748
+ # 读面原子化(F2):判据与读取作用于同一个 inode(open 一次 + fd 身份 + 逐段现查)。
1749
+ case "$source" in
1750
+ spec|change) read_root="$(team_panel_detail_source_root "$source")" ;;
1751
+ entry) read_root="$TEAM_DOCS_ABS" ;;
1752
+ esac
1753
+ text="$(team_panel_detail_read_nofollow "$read_root" "$abs" "$cap")" || read_rc=$?
1754
+ case "$read_rc" in
1755
+ 3) read_hit="$file"; file="" ;;
1756
+ 4) read_unchecked="$file"; file="" ;;
1757
+ esac
1758
+ fi
1239
1759
  [ "${sizes[$pick]}" -gt "$cap" ] && truncated=true
1240
1760
  fi
1241
- printf '{"id": %s, "count": %s, "file": %s, "text": %s, "truncated": %s, "files": %s}\n' \
1761
+ # 被拒的软链点名;目标根本不存在时给可读原因(不是错误帧,也不拿邻居的正文充数)。
1762
+ local reason=""
1763
+ if [ -n "$read_hit" ]; then
1764
+ reason="软链拒绝:$read_hit"
1765
+ elif [ -n "$read_unchecked" ]; then
1766
+ reason="读取身份无法确认(没有 /proc/self/fd 也没有 /dev/fd):$read_unchecked"
1767
+ elif [ -n "$refused" ]; then
1768
+ reason="软链拒绝:$refused"
1769
+ elif [ "$n" -eq 0 ]; then
1770
+ case "$source" in
1771
+ spec) reason="${TEAM_SPEC_DIR:-openspec}/specs/$id/spec.md 不存在" ;;
1772
+ change) reason="${TEAM_SPEC_DIR:-openspec}/changes/$id/ 下没有 proposal/design/tasks" ;;
1773
+ decision) reason="DECISIONS.md 里没有 $id" ;;
1774
+ esac
1775
+ fi
1776
+ local reason_json=""
1777
+ [ -n "$reason" ] && reason_json=", \"reason\": $(team_panel_json_str "$reason")"
1778
+ printf '{"id": %s, "count": %s, "file": %s, "text": %s, "truncated": %s, "files": %s%s}\n' \
1242
1779
  "$(team_panel_json_str "$id")" "$(team_panel_num "$n")" "$(team_panel_json_str "$file")" \
1243
- "$(team_panel_json_str_ml "$text")" "$truncated" "$out"
1780
+ "$(team_panel_json_str_ml "$text")" "$truncated" "$out" "$reason_json"
1781
+ }
1782
+
1783
+ # ---- 关键回复块(change: pulse-key-replies):三个 console-only 只读块,直接进 helper。
1784
+ # 这三块**不参与** team_scan_warm:回复读路径的 IO 边界必须在自己的 helper 里被证明,
1785
+ # 不能被既有 board/report 的全量扫描掩盖(design §4)。helper 自己不 mkdir、不写盘、不等 writer 锁。
1786
+ team_panel_reply_helper() { # <helper 子命令> [args...]
1787
+ local helper="$TEAM_SKILL_DIR/scripts/replies.mjs"
1788
+ if [ ! -f "$helper" ]; then
1789
+ printf 'team: __panel-data: 缺存储 helper(%s)\n' "$helper" >&2
1790
+ return 4
1791
+ fi
1792
+ local runner; runner="$(team_js_runner)"
1793
+ if [ -z "$runner" ]; then
1794
+ printf 'team: __panel-data: 缺 JS 运行时(node/bun/tsx)\n' >&2
1795
+ return 4
1796
+ fi
1797
+ local state_dir="${TEAM_STATE_DIR:-$TEAM_MAIN_ROOT/.pi/team/state}"
1798
+ "$runner" "$helper" "$@" --main-root "$TEAM_MAIN_ROOT" --state-dir "$state_dir"
1799
+ }
1800
+
1801
+ team_panel_reply_summary_json() { team_panel_reply_helper summary; }
1802
+
1803
+ team_panel_reply_list_json() { # [--before <no>]
1804
+ if [ -n "${1:-}" ] && [ "${1:-}" != "-" ]; then
1805
+ team_panel_reply_helper list --before "$1" --format json
1806
+ else
1807
+ team_panel_reply_helper list --format json
1808
+ fi
1809
+ }
1810
+
1811
+ team_panel_reply_body_json() { # <no>
1812
+ [ -n "${1:-}" ] || { printf 'team: __panel-data --block reply_body 需要 --no <编号>\n' >&2; return 2; }
1813
+ team_panel_reply_helper show --no "$1" --format json
1244
1814
  }
1245
1815
 
1246
1816
  # 块分发:一个块一个子进程(panel.js 用 --block 起一片),源坏时打印尽力而为的片段并返回非 0,
1247
1817
  # 由渲染层把那一块画成 `—`,其余块照常渲染。
1248
- team_panel_block() { # <块名> <activity 1|0> <events> [id] [file]
1818
+ team_panel_block() { # <块名> <activity 1|0> <events> [id] [file] [before] [no] [source]
1249
1819
  case "$1" in
1250
1820
  frame) team_panel_frame_json ;;
1251
1821
  pm) team_panel_pm_json ;;
@@ -1262,8 +1832,11 @@ team_panel_block() { # <块名> <activity 1|0> <events> [id] [file]
1262
1832
  inbox) team_panel_inbox_json ;;
1263
1833
  patrol) team_panel_patrol_json ;;
1264
1834
  health) team_panel_health_json ;;
1265
- detail) team_panel_detail_json "${4:-}" "${5:-}" ;;
1835
+ detail) team_panel_detail_json "${4:-}" "${5:-}" "${8:-}" ;;
1266
1836
  settings) team_panel_settings_json ;;
1837
+ reply_summary) team_panel_reply_summary_json ;;
1838
+ reply_list) team_panel_reply_list_json "${6:-}" ;;
1839
+ reply_body) team_panel_reply_body_json "${7:-}" ;;
1267
1840
  activity) team_panel_activity_json "${2:-1}" "${3:-4}" ;;
1268
1841
  *) team_usage_die "__panel-data: 未知块名 $1" ;;
1269
1842
  esac
@@ -1272,7 +1845,7 @@ team_panel_block() { # <块名> <activity 1|0> <events> [id] [file]
1272
1845
  # 内部命令:给 panel.js 一个 JSON({panel, activity})或一个块(--block <名>)。
1273
1846
  # 名字带 __ 前缀 = 不是给人用的命令面。
1274
1847
  team_cmd_panel_data() {
1275
- local activity=1 events="${TEAM_MONITOR_EVENTS:-4}" block="" did="" dfile=""
1848
+ local activity=1 events="${TEAM_MONITOR_EVENTS:-4}" block="" did="" dfile="" rbefore="" rno="" dsource=""
1276
1849
  while [ $# -gt 0 ]; do
1277
1850
  case "$1" in
1278
1851
  --no-activity) activity=0; shift ;;
@@ -1281,6 +1854,9 @@ team_cmd_panel_data() {
1281
1854
  --block) block="${2:?__panel-data: --block 需要块名}"; shift 2 ;;
1282
1855
  --id) did="${2:?__panel-data: --id 需要入口 id}"; shift 2 ;;
1283
1856
  --file) dfile="${2:?__panel-data: --file 需要路径}"; shift 2 ;;
1857
+ --source) dsource="${2:?__panel-data: --source 需要取值}"; shift 2 ;;
1858
+ --before) rbefore="${2:?__panel-data: --before 需要编号}"; shift 2 ;;
1859
+ --no) rno="${2:?__panel-data: --no 需要编号}"; shift 2 ;;
1284
1860
  -*) team_usage_die "__panel-data: 未知参数 $1" ;;
1285
1861
  *) team_usage_die "__panel-data: 多余参数 $1" ;;
1286
1862
  esac
@@ -1290,9 +1866,13 @@ team_cmd_panel_data() {
1290
1866
  # 进程替换起一个后台产者,末尾再收;输出键序与字节与串行完全一致。
1291
1867
  local _act_fd _act_out='[]'
1292
1868
  if [ -n "$block" ]; then
1293
- # M50:pending 块逐份迭代报告 → 全量档;其余块(pm/frame/capacity/agents/…)不碰 candidates → 宽度档
1294
- case "$block" in pending) team_scan_warm --reports ;; *) team_scan_warm ;; esac
1295
- team_panel_block "$block" "$activity" "$events" "$did" "$dfile"
1869
+ # 关键回复三块直接进只读 helper(不预热扫描);其余块照旧共享扫描缓存。
1870
+ case "$block" in
1871
+ reply_summary|reply_list|reply_body) ;;
1872
+ pending) team_scan_warm --reports ;;
1873
+ *) team_scan_warm ;;
1874
+ esac
1875
+ team_panel_block "$block" "$activity" "$events" "$did" "$dfile" "$rbefore" "$rno" "$dsource"
1296
1876
  return $?
1297
1877
  fi
1298
1878
  if [ "$activity" = "1" ]; then
@@ -1447,7 +2027,7 @@ team_pulse_shape() { # console | headless | unknown
1447
2027
  local w pane args
1448
2028
  w="$(team_pulse_backend_window)"
1449
2029
  team_tmux_has_window "$TEAM_SESSION" "$w" || { printf 'unknown'; return 0; }
1450
- pane="$(tmux list-panes -t "$TEAM_SESSION:$w" -F '#{pane_pid}' 2>/dev/null | head -1)"
2030
+ pane="$(team_tmux list-panes -t "$TEAM_SESSION:$w" -F '#{pane_pid}' 2>/dev/null | head -1)"
1451
2031
  case "${pane:-}" in ''|*[!0-9]*) printf 'unknown'; return 0 ;; esac
1452
2032
  args="$(ps -o args= -p "$pane" 2>/dev/null || true)"
1453
2033
  case "$args" in
@@ -1477,13 +2057,13 @@ team_pulse_tmux_up() {
1477
2057
  # (那样的窗口会被 pulse status 当成「在跑」,是 D19 明确要避免的假绿)
1478
2058
  team_require_js_runtime "team pulse up"
1479
2059
  team_tmux_ensure_session
1480
- tmux set-option -t "$TEAM_SESSION" destroy-unattached off >/dev/null 2>&1 || true
2060
+ team_tmux set-option -t "$TEAM_SESSION" destroy-unattached off >/dev/null 2>&1 || true
1481
2061
  local w st lw shape; w="$(team_pulse_window)"; st="$(team_pulse_window_state)"
1482
2062
  if [ "$st" = "running" ]; then
1483
2063
  # 无界面形态(q 收起过)→ 同一窗口原地恢复控制台,不开第二个窗口。
1484
2064
  shape="$(team_pulse_shape)"
1485
2065
  if [ "$shape" = "headless" ]; then
1486
- tmux respawn-window -k -t "$TEAM_SESSION:$w" -- bash -c "$(team_pulse_window_cmd)"
2066
+ team_tmux respawn-window -k -t "$TEAM_SESSION:$w" -- bash -c "$(team_pulse_window_cmd)"
1487
2067
  sleep 1.5
1488
2068
  if [ "$(team_pulse_window_state)" = "running" ] && [ "$(team_pulse_shape)" = "console" ]; then
1489
2069
  team_ok "已把无界面巡检恢复成控制台($TEAM_SESSION:$w)"
@@ -1500,8 +2080,8 @@ team_pulse_tmux_up() {
1500
2080
  team_warn "旧窗口 $TEAM_SESSION:$lw 仍在跑 → $TEAM_CLI pulse restart 换成 $TEAM_SESSION:$w(up 不开第二个巡检)"
1501
2081
  return 0
1502
2082
  fi
1503
- [ "$st" = "idle" ] && { team_warn "窗口 $w 停在空提示符:重开"; tmux kill-window -t "$TEAM_SESSION:$w" 2>/dev/null || true; }
1504
- tmux new-window -t "$TEAM_SESSION" -n "$w" -d -- bash -c "$(team_pulse_window_cmd)"
2083
+ [ "$st" = "idle" ] && { team_warn "窗口 $w 停在空提示符:重开"; team_tmux kill-window -t "$TEAM_SESSION:$w" 2>/dev/null || true; }
2084
+ team_tmux new-window -t "$TEAM_SESSION" -n "$w" -d -- bash -c "$(team_pulse_window_cmd)"
1505
2085
  sleep 1.5
1506
2086
  st="$(team_pulse_window_state)"
1507
2087
  if [ "$st" = "running" ]; then
@@ -1515,11 +2095,11 @@ team_pulse_tmux_up() {
1515
2095
  team_pulse_tmux_down() {
1516
2096
  local w found=0; w="$(team_pulse_window)"
1517
2097
  if team_tmux_has_window "$TEAM_SESSION" "$w"; then
1518
- tmux kill-window -t "$TEAM_SESSION:$w" 2>/dev/null && { team_ok "已关掉巡检窗口:$TEAM_SESSION:$w"; found=1; } || team_warn "关窗口失败"
2098
+ team_tmux kill-window -t "$TEAM_SESSION:$w" 2>/dev/null && { team_ok "已关掉巡检窗口:$TEAM_SESSION:$w"; found=1; } || team_warn "关窗口失败"
1519
2099
  fi
1520
2100
  # 别名期:结束巡检 = 两个名字都结束(旧窗口里可能是升级前的进程)
1521
2101
  if [ "$w" != "watchdog" ] && team_tmux_has_window "$TEAM_SESSION" watchdog; then
1522
- tmux kill-window -t "$TEAM_SESSION:watchdog" 2>/dev/null && { team_ok "已关掉旧名巡检窗口:$TEAM_SESSION:watchdog"; found=1; } || team_warn "关旧名窗口失败"
2102
+ team_tmux kill-window -t "$TEAM_SESSION:watchdog" 2>/dev/null && { team_ok "已关掉旧名巡检窗口:$TEAM_SESSION:watchdog"; found=1; } || team_warn "关旧名窗口失败"
1523
2103
  fi
1524
2104
  [ "$found" = "0" ] && team_dim "没有巡检窗口($TEAM_SESSION:$w)"
1525
2105
  return 0
@@ -1528,7 +2108,7 @@ team_pulse_tmux_down() {
1528
2108
  team_pulse_tmux_logs() { # 打印面板最近若干行(pane 快照;旧名窗口也是后端,迁移前照样能看)
1529
2109
  local w; w="$(team_pulse_backend_window)"
1530
2110
  team_tmux_has_window "$TEAM_SESSION" "$w" || { team_err "没有巡检窗口($TEAM_SESSION:$w);$TEAM_CLI pulse up 起一个"; return 1; }
1531
- tmux capture-pane -p -t "$TEAM_SESSION:$w" -S -60 2>/dev/null | sed '/^$/d' | tail -60
2111
+ team_tmux capture-pane -p -t "$TEAM_SESSION:$w" -S -60 2>/dev/null | sed '/^$/d' | tail -60
1532
2112
  }
1533
2113
 
1534
2114
  # `q` 收起控制台:同一个窗口原地 respawn 成无界面循环。窗口在、形态是 console 才动手。
@@ -1544,7 +2124,7 @@ team_pulse_tmux_collapse() {
1544
2124
  team_dim "巡检窗口已经是无界面形态($shape):不需要收起"
1545
2125
  return 0
1546
2126
  fi
1547
- tmux respawn-window -k -t "$TEAM_SESSION:$w" -- bash -c "$(team_pulse_window_cmd --headless)"
2127
+ team_tmux respawn-window -k -t "$TEAM_SESSION:$w" -- bash -c "$(team_pulse_window_cmd --headless)"
1548
2128
  team_ok "已收起控制台:$TEAM_SESSION:$w 改为无界面巡检(tick 继续,$TEAM_CLI pulse up 恢复)"
1549
2129
  }
1550
2130