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
@@ -23,10 +23,6 @@ team_worktree_add() { # <agent> [--create] [--no-install]
23
23
  else
24
24
  printf ' git -C %s worktree add -b %s %s %s\n' "$TEAM_MAIN_ROOT" "$branch" "$wt" "$TEAM_PROTECTED_BRANCH"
25
25
  fi
26
- team_state_set "$agent" model "$(team_agent_model "$agent")"
27
- team_state_set "$agent" model_src config
28
- team_state_set "$agent" window "$agent"
29
- team_state_set "$agent" worktree "$wt"
30
26
  return 0
31
27
  fi
32
28
  mkdir -p "$(dirname "$wt")"
@@ -52,10 +48,6 @@ team_worktree_add() { # <agent> [--create] [--no-install]
52
48
  ( cd "$wt" && eval "$TEAM_INSTALL_CMD" ) || team_warn "install 失败,先手动装依赖再派单"
53
49
  fi
54
50
 
55
- team_state_set "$agent" model "$(team_agent_model "$agent")"
56
- team_state_set "$agent" model_src config
57
- team_state_set "$agent" window "$agent"
58
- team_state_set "$agent" worktree "$wt"
59
51
  return 0
60
52
  }
61
53
 
@@ -70,9 +62,10 @@ team_pi_args() { # <model> → 打印已转义的 pi 参数
70
62
  piargs+=(-e "$TEAM_SKILL_DIR/extension/team-inbox-watch.ts")
71
63
  [ -d "$TEAM_SKILL_DIR" ] && piargs+=(--skill "$TEAM_SKILL_DIR")
72
64
  if [ -n "$TEAM_EXTRA_PI_ARGS" ]; then
73
- # 允许项目追加参数(空格分隔,不支持带空格的值)
74
- # shellcheck disable=SC2206
75
- piargs+=($TEAM_EXTRA_PI_ARGS)
65
+ # Match the ownership guard's literal grammar; never re-split/glob the
66
+ # original text, which would turn quote characters into argv data.
67
+ team_literal_argv "$TEAM_EXTRA_PI_ARGS" || return 1
68
+ piargs+=("${TEAM_LITERAL_TOKENS[@]}")
76
69
  fi
77
70
  printf '%q ' "${piargs[@]}"
78
71
  }
@@ -133,7 +126,7 @@ team_build_prompt() { # <agent> <ID> <taskfile-abs> <worktree> <model> [<session
133
126
  # 其它 adapter 不提:工具不存在,教了只会让 worker 白找。
134
127
  case "${TEAM_AGENT_CMD:-}" in
135
128
  ''|*'{bg_ext}'*)
136
- bg_block="$(printf '\n**Long jobs (a gate, a build)**: start them with `team_bg_run` (it returns a job id immediately and keeps\nworking while you go on), then harvest with `team_bg_wait <id>` (the result comes back inline). **Harvest every job\nbefore your turn ends** -- an unharvested job wakes you once when it finishes, and that costs a turn.\n')" ;;
129
+ bg_block="$(printf '\n**Long jobs (a gate, a build)**: start them with `team_bg_run` (it returns a job id immediately and keeps\nworking while you go on), then **end your turn** -- the completion wake-up is the normal harvest point: when the\njob finishes you are woken once (several jobs finishing together arrive as one message) and harvest it with\n`team_bg_wait <id>` (the exit code and the log tail come back inline). **In-turn waiting is the bounded\nexception**: use it only when all three hold -- the job is expected to finish within the inline-wait budget (about 2 minutes),\nits result blocks your immediate next step, and no other task work can advance meanwhile.\n')" ;;
137
130
  esac
138
131
 
139
132
  cat <<PROMPT
@@ -162,11 +155,15 @@ another project's help (cross-repo dependency, shared library change), write \`B
162
155
  needs to do what -- **cross-project communication is the PM's job via \`$cli meeting\` (peer exchange: interfaces,
163
156
  advice, problem reports); workers do not attend**. Only decisions that need the user's call go through the user.
164
157
 
165
- **Never stop mid-task to ask for confirmation**: end the turn in exactly two situations --
166
- (a) every acceptance command in the brief has been run + the report is written + the branch is pushed + the PR/MR is
158
+ **Never stop mid-task to ask for confirmation**: end the turn in exactly three situations --
159
+ (a) **delivered**: every acceptance command in the brief has been run + the report is written + the branch is pushed + the PR/MR is
167
160
  open (or, in local mode, the branch state is settled);
168
- (b) you are hard-blocked (missing dependency, missing permission, someone else's bug): notify the PM with
169
- \`$cli notify $agent "<one line>"\`, state \`BLOCKED:\` clearly in the report, then end the turn.
161
+ (b) **hard-blocked** (missing dependency, missing permission, someone else's bug): notify the PM with
162
+ \`$cli notify pm "<one line>"\`, state \`BLOCKED:\` clearly in the report, then end the turn;
163
+ (c) **suspended on background jobs**: jobs this session started are still running and every step that does not depend
164
+ on their results is done. A suspension is neither a delivery nor a blockage -- do not write the report's final
165
+ verdict and do not notify \`BLOCKED:\` for it; end the turn with its **last line** naming every job you are
166
+ waiting on (\`waiting on: <id>[, <id>…]\`), so the PM can see the ending was deliberate.
170
167
  **Never work around a boundary on your own.**
171
168
 
172
169
  Delivery process:
@@ -183,7 +180,8 @@ Delivery process:
183
180
 
184
181
  Task: **$id**${issue:+ (issue #$issue)}. The brief \`$taskfile\` is the PM's read-only file -- do not modify it.${bg_block}${notify_block}
185
182
  If this is a resumed run: start with \`git status\` / \`git log --oneline -5\` to see how far you got, and continue
186
- from that point instead of starting over.
183
+ from that point instead of starting over. A previous session's wake-ups died with its process, so jobs it left
184
+ running will never wake you: check \`state/bg.log\` and \`team bg list\` before you await or re-run their results.
187
185
  PROMPT
188
186
  }
189
187
 
@@ -193,8 +191,7 @@ team_cmd_add_agent() {
193
191
  while [ $# -gt 0 ]; do
194
192
  case "$1" in
195
193
  --register) register=1; shift ;;
196
- --model) model="${2:?--model 需要 <provider/model> 或 -}"; has_model=1; shift 2 ;;
197
- --model=*) model="${1#*=}"; has_model=1; shift ;;
194
+ --model|--model=*) team_err 'add-agent --model retired;使用 task category 和 config set-task-model'; return 2 ;;
198
195
  --fingerprint) fp="${2:?--fingerprint 需要 sha256}"; shift 2 ;;
199
196
  --fingerprint=*) fp="${1#*=}"; shift ;;
200
197
  --no-install) extra+=(--no-install); shift ;;
@@ -502,7 +499,7 @@ team_agent_pane_tail() { # <agent> [<重试次数>] → stdout
502
499
  team_agent_window_exists "$agent" || return 0
503
500
  t="$TEAM_SESSION:$agent"
504
501
  while :; do
505
- out="$(tmux capture-pane -p -t "$t" -S -200 2>/dev/null || true)"
502
+ out="$(team_tmux capture-pane -p -t "$t" -S -200 2>/dev/null || true)"
506
503
  team_text_has_content "$out" && break
507
504
  [ "$i" -ge "$tries" ] && break
508
505
  i=$((i + 1)); sleep 0.1
@@ -563,7 +560,7 @@ team_dispatch_launch_diag() { # <agent> <ID> <渲染出的命令> <解析到的
563
560
  team_pane_dead_fields() { # <session:window> → "dead|status|signal|time";目标不在/读不到 → 非 0、无输出
564
561
  local t="${1:-}" out
565
562
  [ -n "$t" ] || return 1
566
- out="$(tmux list-panes -t "$t" -F '#{pane_dead}|#{pane_dead_status}|#{pane_dead_signal}|#{pane_dead_time}' 2>/dev/null | head -1)"
563
+ out="$(team_tmux list-panes -t "$t" -F '#{pane_dead}|#{pane_dead_status}|#{pane_dead_signal}|#{pane_dead_time}' 2>/dev/null | head -1)"
567
564
  [ -n "$out" ] || return 1
568
565
  printf '%s\n' "$out"
569
566
  }
@@ -618,7 +615,7 @@ team_agent_pane_evidence() { # <agent> → 死 pane 的退出证据("signal=9"
618
615
  team_agent_corpse_scene() { # <session:window> → 最后 N 行画面(抓不到 → 空)
619
616
  local t="${1:-}"
620
617
  [ -n "$t" ] || return 1
621
- tmux capture-pane -p -S - -t "$t" 2>/dev/null \
618
+ team_tmux capture-pane -p -S - -t "$t" 2>/dev/null \
622
619
  | sed -e 's/[[:space:]]*$//' -e '/^Pane is dead (/d' \
623
620
  | awk '{ if (NF) last=NR; line[NR]=$0 } END { for (i=1; i<=last; i++) print line[i] }' \
624
621
  | tail -n "$(team_agent_scene_lines)"
@@ -1019,8 +1016,7 @@ team_dispatch_mem_fix_cmd() { # <agent> <ID> <任务书> <守卫输出> → 覆
1019
1016
  printf '%s %s dispatch %s %s %s\n' "${knobs# }" "$TEAM_CLI" "$agent" "$id" "$(printf '%q' "$taskfile")"
1020
1017
  }
1021
1018
 
1022
- team_dispatch_judge_mem() { # <agent> <ID> <任务书>(team_df_run_lib 守卫:透传 team_mem_guard 的判定)
1023
- local agent="$1" id="$2" taskfile="$3" out rc=0
1019
+ team_dispatch_judge_mem() { # <agent> <ID> <任务书>(team_df_run_lib 守卫:透传 team_mem_guard 的判定) local agent="$1" id="$2" taskfile="$3" out rc=0
1024
1020
  out="$(team_mem_guard 2>&1)" || rc=$?
1025
1021
  [ -n "$out" ] && printf '%s\n' "$out"
1026
1022
  [ "$rc" -eq 0 ] && return 0
@@ -1032,6 +1028,33 @@ team_dispatch_judge_mem() { # <agent> <ID> <任务书>(team_df_run_lib 守卫
1032
1028
  return 1
1033
1029
  }
1034
1030
 
1031
+ # P309 · 临时根:显式选定的根不可用 = **阻塞项**(今天这种情况会走「读不到 → 不判」的沉默分支)。
1032
+ # 默认腿未创建**不是**拒绝(读侧按最近祖先判);显式根(TMPDIR / 契约的 TEAM_TMP_ROOT)不存在 /
1033
+ # 不是目录 / 不可写 → 点名路径、来源、原因与修法,且**绝不**换用另一个根。
1034
+ team_dispatch_judge_tmp_root() { # → 0 = 可用或默认腿;1 = 阻塞项(输出已打印)
1035
+ local raw src why
1036
+ src="$(team_tmp_root_source 2>/dev/null || printf 'default')"
1037
+ if ! raw="$(team_tmp_root 2>/dev/null)"; then
1038
+ # 解析器自己已经打印了具名失败(相对路径 / 空串)。这里只给一句话的方向,**不打印一条
1039
+ # 看起来能粘贴的路线**(那条路线得把值改成什么,只有操作者知道;夹具的 C1 会拒收未兑现的路线)。
1040
+ printf ' 方向:给它一个绝对路径(TEAM_TMP_ROOT=/var/tmp/… 或 TMPDIR=/abs/dir),或清掉它走平台默认\n'
1041
+ return 1
1042
+ fi
1043
+ [ "$src" = "default" ] && return 0 # 默认腿:未创建不是拒绝(地板按祖先判)
1044
+ team_tmp_root_usable "$raw" && return 0
1045
+ why="不存在"
1046
+ if [ -e "$raw" ]; then
1047
+ if [ ! -d "$raw" ]; then why="不是目录";
1048
+ elif [ ! -w "$raw" ]; then why="不可写"; fi
1049
+ fi
1050
+ printf '临时根不可用:%s(source=%s)%s —— 不可用,且不得换用别的根\n' "$raw" "$src" "$why"
1051
+ # 路线行 = 一整条**能直接跑**的命令(P151:命令与说明分行);说明留到下一行。
1052
+ printf ' 修法:mkdir -p %q\n' "$raw"
1053
+ printf ' 建好之后确认它可写;或者把 %s 指到另一个已存在的可写目录(references/config.md 的 TEAM_TMP_ROOT)\n' \
1054
+ "$([ "$src" = "tmpdir" ] && printf 'TMPDIR' || printf 'TEAM_TMP_ROOT')"
1055
+ return 1
1056
+ }
1057
+
1035
1058
  # ---------------------------------------------------------------- P140(dispatch-friction · R6)· 席位上一轮预提示
1036
1059
  # 两条腿各自独立、都可缺(判不出来一律沉默,不猜):
1037
1060
  # · 死因腿:agent-death-reason 的读取器给出 quota/balance —— 点名分类、来源、时间与原文;
@@ -1052,8 +1075,10 @@ team_dispatch_judge_seat_hint() { # <agent> <worktree>
1052
1075
  fi ;;
1053
1076
  esac
1054
1077
  fi
1055
- sid="$(team_state_get "$agent" sid '')"
1056
- rec_bytes="$(team_state_get "$agent" sid_bytes '')"
1078
+ # Even zero-output hints must use the same complete trusted identity as size,
1079
+ # activity and death reads; never stitch partial fields onto the current cwd.
1080
+ team_assignment_load "$agent" || return 0
1081
+ sid="${TEAM_ASSIGN[sid]}"; rec_bytes="${TEAM_ASSIGN[sid_bytes]}"; wt="${TEAM_ASSIGN[worktree]}"
1057
1082
  if [ -n "$sid" ] && [ -n "$rec_bytes" ]; then
1058
1083
  f="$(team_session_file "$sid" "$wt" 2>/dev/null || true)"
1059
1084
  if [ -n "$f" ] && [ -f "$f" ]; then
@@ -1068,7 +1093,136 @@ team_dispatch_judge_seat_hint() { # <agent> <worktree>
1068
1093
  return 0
1069
1094
  }
1070
1095
 
1071
- team_cmd_dispatch() {
1096
+ # Public front door: immutable preflight, then seat/admission serialization.
1097
+ team_cmd_dispatch() (
1098
+ local -a original=("$@") positional=()
1099
+ local seat='' printonly=0 arg cfg_fp state_fp task_fp output rc=0
1100
+ while [ $# -gt 0 ]; do
1101
+ case "$1" in
1102
+ --model|--branch) [ $# -ge 2 ] || team_usage_die "dispatch: $1 needs a value"; shift 2 ;;
1103
+ --print) printonly=1; shift ;;
1104
+ --*) shift ;; *) positional+=("$1"); [ -n "$seat" ] || seat="$1"; shift ;;
1105
+ esac
1106
+ done
1107
+ cfg_fp="$(team_config_fingerprint "$TEAM_CONFIG")"
1108
+ [ "${TEAM_CONFIG_LOADED_FP:-$cfg_fp}" = "$cfg_fp" ] || { team_err 'dispatch: config changed after load;重新读取后重试,未写/未启动'; return 1; }
1109
+ if [ "$printonly" = 1 ]; then team_dispatch_execute "${original[@]}"; return $?; fi
1110
+ state_fp="$(team_launch_state_fingerprint "$seat")"
1111
+ task_fp="$(team_launch_task_fingerprint "$seat" "${positional[1]:-}")"
1112
+ output="$( _TEAM_DISPATCH_PREFLIGHT_ONLY=1 team_dispatch_execute "${original[@]}" 2>&1)" || rc=$?
1113
+ [ "$rc" = 0 ] || { printf '%s\n' "$output"; return "$rc"; }
1114
+ local _TEAM_DISPATCH_CONFIG_FP="$cfg_fp" _TEAM_DISPATCH_STATE_FP="$state_fp" _TEAM_DISPATCH_TASK_FP="$task_fp" _TEAM_DISPATCH_TASK_ID="${positional[1]:-}"
1115
+ team_with_lock "seat:$seat" team_with_lock worker-admission team_with_lock "config:$TEAM_CONFIG" team_dispatch_execute "${original[@]}"
1116
+ )
1117
+ team_launch_state_fingerprint() {
1118
+ local f
1119
+ for f in "$TEAM_STATE_DIR/$1.env" "$TEAM_STATE_DIR/$1.pending"; do
1120
+ printf '%s\n' "$f"; [ ! -f "$f" ] || sha256sum "$f"
1121
+ done | sha256sum | cut -d' ' -f1
1122
+ }
1123
+ team_launch_task_fingerprint() {
1124
+ local f wt; wt="$(team_agent_worktree "$1")"
1125
+ {
1126
+ while IFS= read -r f; do [ -z "$f" ] || { printf '%s\n' "$f"; sha256sum "$f"; }; done < <(team_task_briefs "$2")
1127
+ git -C "$wt" symbolic-ref -q HEAD 2>/dev/null || true
1128
+ git -C "$wt" rev-parse HEAD 2>/dev/null || true
1129
+ } | sha256sum | cut -d' ' -f1
1130
+ }
1131
+ team_dispatch_final_snapshot() {
1132
+ [ "$(team_config_fingerprint "$TEAM_CONFIG")" = "$_TEAM_DISPATCH_CONFIG_FP" ] &&
1133
+ [ "$(team_launch_state_fingerprint "$1")" = "$_TEAM_DISPATCH_STATE_FP" ] &&
1134
+ [ "$(team_launch_task_fingerprint "$1" "$_TEAM_DISPATCH_TASK_ID")" = "$_TEAM_DISPATCH_TASK_FP" ] || {
1135
+ team_err 'dispatch: policy/assignment/brief/branch changed during preflight;重新预览后重试,未写/未启动'; return 1;
1136
+ }
1137
+ }
1138
+ team_dispatch_select_identity() {
1139
+ local agent="$1" id="$2" taskfile="$3" wt="$4" branch="$5" override="$6" fresh="$7"
1140
+ local declarations category oldtask complete=0 matching=0 oldmodel='' oldsid='' origin=''
1141
+ declarations="$(grep -E '^category:' "$taskfile" || true)"
1142
+ [ "$(printf '%s\n' "$declarations" | grep -c .)" = 1 ] || { team_err 'brief 必须唯一 category: header;显式 override 不豁免'; return 1; }
1143
+ category="$(team_trim "${declarations#category:}")"
1144
+ team_category_valid "$category" || { team_err "category 不合法:$category"; return 1; }
1145
+ team_model_policy_guard || return 1
1146
+ team_identity_extra_guard "${TEAM_EXTRA_PI_ARGS:-}" || return 1
1147
+ if team_assignment_load "$agent"; then
1148
+ complete=1; oldtask="${TEAM_ASSIGN[task]}"; oldmodel="${TEAM_ASSIGN[model]}"; oldsid="${TEAM_ASSIGN[sid]}"; origin="${TEAM_ASSIGN[selected_via]}"
1149
+ if [ "$oldtask" = "$id" ] && [ "${TEAM_ASSIGN[taskfile]}" = "$taskfile" ] && [ "${TEAM_ASSIGN[category]}" = "$category" ] &&
1150
+ [ "${TEAM_ASSIGN[branch]}" = "$branch" ] && [ "${TEAM_ASSIGN[worktree]}" = "$wt" ]; then matching=1; fi
1151
+ # P241/B3:只有**自称是 assignment 记录**的文件才在这里被拒(legacy/损坏/半条记录)。
1152
+ # 一份只有无关记账键的文件(旧实现由 `team inbox --ack` 写出来的 `inbox_lines=N`)不是记录,
1153
+ # 按「没有记录」继续派单;否则该席位会被无故要求 --fresh。
1154
+ elif [ "$fresh" != 1 ] && team_assignment_record_present "$agent"; then
1155
+ team_err "$agent: $TEAM_ASSIGN_DIAGNOSTIC;保留磁盘交接,dispatch $agent $id $taskfile --fresh"; return 1
1156
+ fi
1157
+ if [ -f "$TEAM_STATE_DIR/$agent.pending" ]; then
1158
+ local pending_nonce; pending_nonce="$(awk -F= '$1=="launch_nonce"{print substr($0,14)}' "$TEAM_STATE_DIR/$agent.pending")"
1159
+ if [ "$complete" != 1 ] || [ "$pending_nonce" != "${TEAM_ASSIGN[launch_nonce]}" ]; then
1160
+ [ "$fresh" = 1 ] || { team_err 'interrupted pending launch;保存诊断并 --fresh 接管'; return 1; }
1161
+ else team_warn 'committed nonce matches pending;保留完整身份,检查 BOARD 清理故障'; fi
1162
+ fi
1163
+ if [ "$complete" = 1 ] && [ "$oldtask" = "$id" ] && [ "$matching" != 1 ] && [ "$fresh" != 1 ]; then
1164
+ team_err '同任务 brief/category/branch/worktree 身份变化;必须 --fresh'; return 1
1165
+ fi
1166
+ if [ "$matching" = 1 ]; then
1167
+ if [ -n "$override" ] && [ "$override" != "$oldmodel" ] && [ "$fresh" != 1 ]; then
1168
+ team_err '同任务不同完整 model(包括 thinking suffix)必须 --fresh'; return 1
1169
+ fi
1170
+ if [ -z "$override" ] || [ "$override" = "$oldmodel" ]; then
1171
+ TEAM_SELECTED_MODEL="$oldmodel"; TEAM_SELECTED_SID="$oldsid"; TEAM_SELECTED_ORIGIN="$origin"; TEAM_LAUNCH_VIA=assignment
1172
+ if [ -n "$override" ]; then TEAM_SELECTED_ORIGIN=explicit; TEAM_LAUNCH_VIA=explicit; fi
1173
+ [ "$fresh" != 1 ] || TEAM_SELECTED_SID="$(team_unique_sid "$agent" "$wt")" || return 1
1174
+ TEAM_SELECTED_CATEGORY="$category"; TEAM_SELECTED_MODE=continued; [ "$fresh" != 1 ] || TEAM_SELECTED_MODE=fresh
1175
+ return 0
1176
+ fi
1177
+ fi
1178
+ local policy_model; policy_model="$(team_task_policy_model "$category")" || { team_err "category 未映射:$category;config set-task-model $category <model>(override 仍须映射)"; return 1; }
1179
+ TEAM_SELECTED_MODEL="${override:-$policy_model}"; team_config_model_shape_ok "$TEAM_SELECTED_MODEL" || { team_err 'override model 不合法'; return 1; }
1180
+ TEAM_SELECTED_SID="$(team_unique_sid "$agent" "$wt")" || return 1
1181
+ TEAM_SELECTED_ORIGIN=policy; TEAM_LAUNCH_VIA=policy
1182
+ [ -z "$override" ] || { TEAM_SELECTED_ORIGIN=explicit; TEAM_LAUNCH_VIA=explicit; }
1183
+ TEAM_SELECTED_CATEGORY="$category"; TEAM_SELECTED_MODE=fresh
1184
+ }
1185
+ team_dispatch_identity_refusal() { team_err "$1"; return 1; }
1186
+ team_dispatch_prepare_record() {
1187
+ local seat="$1" task="$2" brief="$3" wt="$4" branch="$5" model="$6" origin="$7" via="$8" category="$9"
1188
+ shift 9
1189
+ local sid="$1" nonce="$2" bytes="$3" mode="$4" delivered="$5" field
1190
+ TEAM_ASSIGN_TMP="$TEAM_STATE_DIR/.assignment-$seat-$nonce"
1191
+ {
1192
+ printf 'assignment_version=1\ntask=%s\ntaskfile=%s\ncategory=%s\nbranch=%s\nworktree=%s\nwindow=%s\nseat=%s\nmodel=%s\nselected_via=%s\nlaunch_via=%s\nsid=%s\nlaunch_nonce=%s\nstarted=%s\nsid_bytes=%s\nmode=%s\nidentity_via=%s\n' \
1193
+ "$task" "$brief" "$category" "$branch" "$wt" "$seat" "$seat" "$model" "$origin" "$via" "$sid" "$nonce" "$(team_timestamp)" "$bytes" "$mode" "$delivered"
1194
+ } > "$TEAM_ASSIGN_TMP" || return 1
1195
+ if ! team_assignment_load "$seat" "$TEAM_ASSIGN_TMP"; then
1196
+ team_err "planned assignment rejected: $TEAM_ASSIGN_DIAGNOSTIC"; rm -f "$TEAM_ASSIGN_TMP"; return 1
1197
+ fi
1198
+ if [ -f "$TEAM_STATE_DIR/$seat.pending" ]; then
1199
+ local prior_nonce; prior_nonce="$(awk -F= '$1=="launch_nonce"{print substr($0,14)}' "$TEAM_STATE_DIR/$seat.pending")"
1200
+ [[ "$prior_nonce" =~ ^[A-Za-z0-9._-]+$ ]] || prior_nonce="unknown-$(sha256sum "$TEAM_STATE_DIR/$seat.pending" | cut -c1-16)"
1201
+ cp "$TEAM_STATE_DIR/$seat.pending" "$TEAM_STATE_DIR/dispatch-$seat.interrupted-$prior_nonce" || { rm -f "$TEAM_ASSIGN_TMP"; return 1; }
1202
+ fi
1203
+ cp "$TEAM_ASSIGN_TMP" "$TEAM_STATE_DIR/$seat.pending" || { rm -f "$TEAM_ASSIGN_TMP"; return 1; }
1204
+ }
1205
+ team_dispatch_attempt_fail() {
1206
+ local seat="$1" nonce="$2" reason="$3"
1207
+ printf '%s nonce=%s failed=%s\n' "$(team_timestamp)" "$nonce" "$reason" >> "$TEAM_STATE_DIR/dispatch-$seat.attempts.log"
1208
+ [ ! -f "$TEAM_STATE_DIR/$seat.pending" ] || mv "$TEAM_STATE_DIR/$seat.pending" "$TEAM_STATE_DIR/dispatch-$seat.failed-$nonce"
1209
+ rm -f "${TEAM_ASSIGN_TMP:-/nonexistent}"
1210
+ team_tmux_kill_window "$TEAM_SESSION:$seat" >/dev/null 2>&1 || true
1211
+ }
1212
+ # P261(吸收 P253):退休键的迁移建议是一个**先出现**的块 —— 退休键没迁完,后面的派单前提(模型/身份)
1213
+ # 都无从谈。它在报告里占第一个位置(P140 的一次拒绝:谁先判谁先印),并把 TEAM_MODEL_POLICY_REPORTED
1214
+ # 置位 —— 身份步骤的 team_model_policy_guard 据此不再重印同一段文字(仍然拒绝,判据不变)。
1215
+ team_dispatch_judge_model_policy() {
1216
+ local issues
1217
+ TEAM_MODEL_POLICY_REPORTED=0
1218
+ issues="$(team_model_policy_issues)"
1219
+ [ -n "$issues" ] || return 0
1220
+ team_df_lines_err "$issues"
1221
+ TEAM_MODEL_POLICY_REPORTED=1
1222
+ return 1
1223
+ }
1224
+
1225
+ team_dispatch_execute() {
1072
1226
  team_require_cmd tmux "agent 在 tmux 窗口里跑,PM 需要能旁观与追问"
1073
1227
  local agent="" id="" taskfile="" model="" branch_flag="" fresh=0 printonly=0 overflow=0 force=0
1074
1228
  while [ $# -gt 0 ]; do
@@ -1100,7 +1254,7 @@ team_cmd_dispatch() {
1100
1254
  # 任务书路径:先按 cwd 解析,再按主工作树解析
1101
1255
  [ -f "$taskfile" ] || taskfile="$TEAM_MAIN_ROOT/$taskfile"
1102
1256
  [ -f "$taskfile" ] || team_die "找不到任务书:$taskfile"
1103
- taskfile="$(cd "$(dirname "$taskfile")" && pwd)/$(basename "$taskfile")"
1257
+ taskfile="$(readlink -f "$taskfile")"
1104
1258
  # M6.3 F15:任务书必须在项目主工作树里。提示词命令 worker「work only inside <project>」,
1105
1259
  # 却把 /tmp/... 称为 "repo-relative path"(自相矛盾);而且项目外的文件不归项目管。
1106
1260
  case "$taskfile" in
@@ -1123,6 +1277,10 @@ team_cmd_dispatch() {
1123
1277
  TEAM_DISPATCH_AUDIT_LINES=""
1124
1278
  TEAM_DISPATCH_STACK_PREV=""
1125
1279
 
1280
+ # ⓪ P261(吸收 P253):旧配置(退休键)是**本次派单的前提** —— 它的迁移建议先判、先印,占据这份
1281
+ # 拒绝的第一段;同一件事不再在下面的身份步骤里重印(team_model_policy_guard 认这个标记,仍然拒绝)。
1282
+ team_df_run team_dispatch_judge_model_policy
1283
+
1126
1284
  # ① 任务书歧义
1127
1285
  team_df_run team_dispatch_judge_briefs "$id" "$briefs" "$nbrief" "$taskfile"
1128
1286
 
@@ -1166,26 +1324,51 @@ team_cmd_dispatch() {
1166
1324
  # M14:解析顺序 = --model 显式参数 > 配置(TEAM_AGENT_MODELS 的 per-agent > TEAM_DEFAULT_MODEL)。
1167
1325
  # 名册 state 里的 model 只是「上次用了什么」的展示记录(roster/ps 标注来源用),不再当默认来源 ——
1168
1326
  # 旧行为(state 优先)让配置改了也不生效:名册里的 deepseek 旧记录压过了新配的 k3-256k。
1169
- local model_src="config"
1170
- if [ -z "$model" ]; then model="$(team_agent_model "$agent")"; else model_src="explicit"; fi
1171
- local provider="${model%%/*}"
1172
- local sid="$TEAM_SESSION-$agent"
1173
- [ "$fresh" = "1" ] && sid="$sid-$(date +%s)"
1327
+ local identity_ok=1 identity_output=''
1328
+ local TEAM_SELECTED_MODEL='' TEAM_SELECTED_SID='' TEAM_SELECTED_ORIGIN='' TEAM_LAUNCH_VIA='' TEAM_SELECTED_CATEGORY='' TEAM_SELECTED_MODE=''
1329
+ # Execute in this shell so the accepted identity is not lost through a subshell.
1330
+ if ! identity_output="$(team_dispatch_select_identity "$agent" "$id" "$taskfile" "$wt" "${TEAM_CHECKED_BRANCH:-$br_name}" "$model" "$fresh" 2>&1)"; then
1331
+ # P261:空输出不冒充一条阻塞项(迁移块已在最前面报过时,身份步骤只静默地拒绝)。
1332
+ identity_ok=0
1333
+ if [ -n "$identity_output" ]; then team_df_run_lib team_dispatch_identity_refusal "$identity_output"; fi
1334
+ else
1335
+ team_dispatch_select_identity "$agent" "$id" "$taskfile" "$wt" "${TEAM_CHECKED_BRANCH:-$br_name}" "$model" "$fresh" >/dev/null || return 1
1336
+ model="$TEAM_SELECTED_MODEL"
1337
+ fi
1338
+ local provider="${model%%/*}" sid="$TEAM_SELECTED_SID"
1339
+ local model_src="$TEAM_SELECTED_ORIGIN"
1340
+ # P228/R6(OBS1):本次启动的模板**实际交付**哪些身份字段(发布不得多于它)。内建 Pi = 三个全交;
1341
+ # 自定义模板 = 模板里已绑定的 selector(角色守卫在预检时已证过);opaque = 空。发布路径与记录都用它。
1342
+ local identity_via
1343
+ if ! identity_via="$(team_launch_identity_fields worker "${TEAM_AGENT_CMD:-}" "${TEAM_SELECTED_MODE:-fresh}")"; then
1344
+ team_df_run_lib team_dispatch_identity_refusal '无法判定模板交付的身份字段(模板不合法)'
1345
+ identity_ok=0; identity_via=''
1346
+ fi
1174
1347
 
1175
1348
  team_df_run_lib team_dispatch_judge_mem "$agent" "$id" "$taskfile"
1176
- # P144:磁盘/inode 腿 —— 判的是这个 worker 真正要写的两个文件系统(临时根 + 它的工作树,
1177
- # 不是共享的 worktrees 根),在**任何开窗动作之前**(--print 也在内),并且是 P140 的启动前
1178
- # 那一趟里的一个 finding(拒绝时和别的阻塞项一起交底,不抢自己的单独出口)。
1179
- local disk_tmp="${TMPDIR:-/tmp}"
1180
- team_df_run_lib team_disk_guard "$disk_tmp" "$wt"
1181
- # 读数是一条「说明」(rc=0 的 finding):放行时在启动前那一趟里打印;被拒时由拒绝本身带读数。
1182
- if [ "$_TEAM_DF_RUN_RC" -eq 0 ]; then team_df_run_lib team_capacity_line "$disk_tmp" "$wt"; fi
1183
- team_df_run_lib team_model_guard "$model"
1349
+ # P309:临时根的**可用性**先判(显式选定的根不可用 = 阻塞项);地板再判它的字节/inode。
1350
+ team_df_run_lib team_dispatch_judge_tmp_root
1351
+ local tmp_ok="$_TEAM_DF_RUN_RC"
1352
+ # P144:磁盘/inode 腿 —— 判的是解析出的临时根 + 这个 worker 的工作树(不是共享的 worktrees 根),
1353
+ # 在**任何开窗动作之前**(--print 也在内)。P309:不在这里再算一遍默认值。
1354
+ local disk_tmp=""
1355
+ disk_tmp="$(team_tmp_root 2>/dev/null || true)"
1356
+ # 夹具红侧(只在 TEAM_SMOKE_FIXTURE=1 下生效):把地板指回固定的 /tmp —— 守门断言必须因此变红。
1357
+ if [ "${TEAM_SMOKE_FIXTURE:-0}" = "1" ] && [ "${TEAM_DISPATCH_FLOOR_BREAK:-}" = "fixed" ]; then disk_tmp="/tmp"; fi
1358
+ if [ "$tmp_ok" = "0" ] && [ -n "$disk_tmp" ]; then
1359
+ team_df_run_lib team_disk_guard "$disk_tmp" "$wt"
1360
+ # 读数是一条「说明」(rc=0 的 finding):放行时在启动前那一趟里打印;被拒时由拒绝本身带读数。
1361
+ if [ "$_TEAM_DF_RUN_RC" -eq 0 ]; then team_df_run_lib team_capacity_line "$disk_tmp" "$wt"; fi
1362
+ else
1363
+ team_df_say " 临时根不可用(见上面那个阻塞项)→ 磁盘余量没有可判的根,本次不判"
1364
+ fi
1365
+ [ "$identity_ok" != 1 ] || team_df_run_lib team_model_guard "$model" "$agent"
1184
1366
  # M4.3 A:会话规模 vs 模型窗口(默认拒绝,--fresh / --allow-overflow 是出路)
1185
- team_df_run_lib team_guard_resume_session "$agent" "$model" "$sid" "$wt" "$fresh" "$overflow" "$id" "$taskfile"
1367
+ [ "$identity_ok" != 1 ] || team_df_run_lib team_guard_resume_session "$agent" "$model" "$sid" "$wt" "$([ "$TEAM_SELECTED_MODE" = fresh ] && printf 1 || printf 0)" "$overflow" "$id" "$taskfile"
1186
1368
  # agent adapter:空配置 = 内置 Pi(老路径,报错文案也不变);die 被捕获成阻塞项,不带走其他判定
1187
1369
  team_df_run_lib team_agent_check_launch
1188
1370
  if [ "$_TEAM_DF_RUN_RC" -eq 0 ]; then
1371
+ team_df_run_lib team_template_identity_guard launch "${TEAM_AGENT_CMD:-}" "${TEAM_SELECTED_MODE:-fresh}"
1189
1372
  team_df_run team_dispatch_judge_agent_bin "$agent"
1190
1373
  else
1191
1374
  team_df_say " adapter 配置不合法 → agent 可执行文件判不了(见上面那一项)"
@@ -1199,6 +1382,12 @@ team_cmd_dispatch() {
1199
1382
  return 1
1200
1383
  fi
1201
1384
 
1385
+ [ "${_TEAM_DISPATCH_PREFLIGHT_ONLY:-0}" != 1 ] || return 0
1386
+ # P309:写盘前把解析出的根准备好(默认腿按需 mkdir 0700;显式根只校验,绝不代建)。--print 不创建。
1387
+ team_tmp_root_prepare >/dev/null || return 1
1388
+ if [ "$printonly" != 1 ]; then
1389
+ team_dispatch_final_snapshot "$agent" || return 1
1390
+ fi
1202
1391
  # 报告通过:这时才允许写 state / 打计划 / 开窗
1203
1392
  # 必需依赖体检(D10):缺依赖**不拒绝**派单(worker 照样能干活),但 PM 必须知道证据/spec 层是缺的。
1204
1393
  # 一行、每次派单只说一次。
@@ -1219,9 +1408,10 @@ team_cmd_dispatch() {
1219
1408
  prompt="$(team_build_prompt "$agent" "$id" "$taskfile" "$wt" "$model" "$sid" "$task_branch" "$task_branch_src")"
1220
1409
  # 提示词落盘:模板里的 {prompt_file} 用它,排查“到底派了什么”也看它(state/ 已 gitignore)
1221
1410
  prompt_file="$(team_agent_prompt_file "$agent" "$id")"
1222
- mkdir -p "$TEAM_STATE_DIR"
1223
- printf '%s\n' "$prompt" > "$prompt_file"
1224
- agent_cmd="$(team_agent_launch_cmd "$agent" "$sid" "$wt" "$prompt_file" "$model")"
1411
+ if [ "$printonly" != 1 ]; then
1412
+ mkdir -p "$TEAM_STATE_DIR"; printf '%s\n' "$prompt" > "$prompt_file"
1413
+ fi
1414
+ agent_cmd="$(team_agent_launch_cmd "$agent" "$sid" "$wt" "$prompt_file" "$model")" || return 1
1225
1415
 
1226
1416
  # notify 模板坏掉时不阻塞派单,但必须说清楚(否则 worker 回合结束没人知道);
1227
1417
  # 提示词那侧会用同一个判断把通知段换成「写进报告」(F8)。
@@ -1233,6 +1423,15 @@ team_cmd_dispatch() {
1233
1423
  fi
1234
1424
 
1235
1425
  if [ "$printonly" = "1" ]; then
1426
+ # P228/R6(OBS1):只发布模板真的交付了的身份字段(未交付的不写成已启动身份);全交付时与旧行逐字节相同。
1427
+ local id_line="identity: category=$TEAM_SELECTED_CATEGORY"
1428
+ team_identity_field_in model "$identity_via" && id_line="$id_line model=$model"
1429
+ id_line="$id_line selected_via=$model_src launch_via=$TEAM_LAUNCH_VIA mode=$TEAM_SELECTED_MODE"
1430
+ team_identity_field_in session_id "$identity_via" && id_line="$id_line sid=$sid"
1431
+ printf '%s\n' "$id_line"
1432
+ local id_undelivered; id_undelivered="$(team_identity_undelivered_text "$identity_via")"
1433
+ [ -z "$id_undelivered" ] || printf 'identity-delivery: 未交付 %s(模板没有绑定这些身份占位符:不当作已启动身份发布)\n' "$id_undelivered"
1434
+ [ "$TEAM_SELECTED_MODE" != fresh ] || printf 'fresh preview SID is a non-reserved candidate; actual launch allocates a new SID\n'
1236
1435
  printf '=== agent 命令(adapter: %s)===\n' "$(team_agent_adapter_label)"
1237
1436
  # M40:身份环境单独一行(真窗口里它在 cd 之前执行;这里不许破 `^cd ` 的行契约)
1238
1437
  printf '身份环境(启动前先执行):%s\n' "$(team_identity_env_prefix "$wt")"
@@ -1263,6 +1462,9 @@ team_cmd_dispatch() {
1263
1462
  mkdir -p "$TEAM_STATE_DIR"
1264
1463
  while [ "$attempt" -lt 2 ] && [ -z "$pid" ]; do
1265
1464
  attempt=$((attempt + 1))
1465
+ nonce="$(date +%s)-$BASHPID-$RANDOM-$RANDOM-$attempt"
1466
+ team_dispatch_prepare_record "$agent" "$id" "$taskfile" "$wt" "$task_branch" "$model" "$model_src" "$TEAM_LAUNCH_VIA" "$TEAM_SELECTED_CATEGORY" "$sid" "$nonce" "$sid_bytes_at_launch" "$TEAM_SELECTED_MODE" "$identity_via" || return 1
1467
+ printf '%s nonce=%s sid=%s attempt=%s\n' "$(team_timestamp)" "$nonce" "$sid" "$attempt" >> "$TEAM_STATE_DIR/dispatch-$agent.attempts.log"
1266
1468
  if team_agent_window_exists "$agent"; then
1267
1469
  if [ "$attempt" = "1" ]; then
1268
1470
  # P55(遗体复用留证):窗口还在但 pane 已死 = 上一轮死在里面 —— 先把现场落盘再替换
@@ -1278,7 +1480,6 @@ team_cmd_dispatch() {
1278
1480
  sleep 0.5
1279
1481
  fi
1280
1482
  # 每轮换 nonce:证据必须来自这一轮的启动(上一轮留在盘上的不算数)
1281
- nonce="$(date +%s)-$$-$RANDOM-$attempt"
1282
1483
  rm -f "$marker" "$exitfile" "$tailfile"
1283
1484
  # 命令里写死绝对路径 + 短暂等待(窗口 shell 可能刚起、PATH/rc 还没就绪);
1284
1485
  # 拿到可执行文件后先写下 (nonce, pid),再跑 agent —— 这一行就是「harness 真的执行了」的证据。
@@ -1291,12 +1492,12 @@ team_cmd_dispatch() {
1291
1492
  # P55(remain-on-exit 的时机是红线):占位命令先持窗 → 设选项并读回 → 才把 pane 交给 harness。
1292
1493
  # 顺序不能反:harness 先跑时它若秒退,窗口在设选项之前就没了(探针 P1/P3 = 2026-09-22 事故形状)。
1293
1494
  # respawn-pane 用 argv 形式(不经 shell 解析):harness 原样保留「bash -lc $inner $prompt-as-$0」。
1294
- tmux new-window -t "$TEAM_SESSION" -n "$agent" -d 'sleep 30' >/dev/null 2>&1 || true
1495
+ team_tmux new-window -t "$TEAM_SESSION" -n "$agent" -d 'sleep 30' >/dev/null 2>&1 || true
1295
1496
  if team_agent_window_exists "$agent"; then
1296
- tmux set-window-option -t "$TEAM_SESSION:$agent" remain-on-exit on >/dev/null 2>&1 || true
1297
- local roe; roe="$(tmux show-options -w -v -t "$TEAM_SESSION:$agent" remain-on-exit 2>/dev/null || true)"
1497
+ team_tmux set-window-option -t "$TEAM_SESSION:$agent" remain-on-exit on >/dev/null 2>&1 || true
1498
+ local roe; roe="$(team_tmux show-options -w -v -t "$TEAM_SESSION:$agent" remain-on-exit 2>/dev/null || true)"
1298
1499
  if [ "$roe" = "on" ]; then
1299
- tmux respawn-pane -k -t "$TEAM_SESSION:$agent" bash -lc "$inner" "$prompt" >/dev/null 2>&1 || true
1500
+ team_tmux respawn-pane -k -t "$TEAM_SESSION:$agent" bash -lc "$inner" "$prompt" >/dev/null 2>&1 || true
1300
1501
  else
1301
1502
  # 读回失败 = 这一轮的启动作废:窗口留给下一轮的重用分支杀(没有下一轮则由失败路径杀),
1302
1503
  # 绝不 return —— 否则「重试一次 + 失败诊断」的既有契约会被这条新守卫短路。
@@ -1329,6 +1530,7 @@ team_cmd_dispatch() {
1329
1530
  team_dim " 排查:tmux 里手动跑一次 —— cd $wt 然后跑 dispatch --print 打出的那条命令;看 agent 自己的报错"
1330
1531
  team_dim " 常见原因:可执行文件/模型/provider 不可用;旧会话楔死(换新会话:--fresh);内存/磁盘不足"
1331
1532
  team_dim " 重派:$TEAM_CLI dispatch $agent $id $taskfile --fresh"
1533
+ team_dispatch_attempt_fail "$agent" "$nonce" 'no nonce proof'
1332
1534
  return 1
1333
1535
  fi
1334
1536
 
@@ -1346,22 +1548,15 @@ team_cmd_dispatch() {
1346
1548
  team_dim " 手工复现:cd $(printf '%q' "$wt") && $agent_cmd"
1347
1549
  team_dim " 常见原因:首词不在窗口(登录 bash)的 PATH 里(现在会被解析成绝对路径,除非 TEAM_AGENT_BIN 指向别的名字);CLI 参数/认证被拒"
1348
1550
  team_dim " 重派:$TEAM_CLI dispatch $agent $id $taskfile"
1551
+ team_dispatch_attempt_fail "$agent" "$nonce" "custom exit=$exit_code"
1349
1552
  return 1
1350
1553
  fi
1351
1554
 
1352
- team_state_set "$agent" model "$model"
1353
- team_state_set "$agent" model_src "$model_src"
1354
- team_state_set "$agent" window "$agent"
1355
- team_state_set "$agent" worktree "$wt"
1356
- team_state_set "$agent" task "$id"
1357
- team_state_set "$agent" taskfile "$taskfile"
1358
- [ -n "$task_branch" ] && team_state_set "$agent" branch "$task_branch"
1359
- team_state_set "$agent" started "$(team_timestamp)"
1360
- # P140(R6)/ P151(F5):记下这一轮的会话 id 与**启动时**(worker 能跑之前)的会话文件字节数
1361
- # —— 下一轮据此判「一个字节没长」。只在派单真的成功之后才落盘。
1362
- team_state_set "$agent" sid "$sid"
1363
- team_state_set "$agent" sid_bytes "$sid_bytes_at_launch"
1364
- team_board_set "$id" wip 2>/dev/null || true
1555
+ if ! mv -f "$TEAM_ASSIGN_TMP" "$TEAM_STATE_DIR/$agent.env"; then
1556
+ team_dispatch_attempt_fail "$agent" "$nonce" 'publication failed'; return 1
1557
+ fi
1558
+ if team_board_set "$id" wip; then rm -f "$TEAM_STATE_DIR/$agent.pending"
1559
+ else team_warn 'assignment 已完整提交;BOARD 更新失败,pending nonce 保留诊断,不撤销启动'; fi
1365
1560
 
1366
1561
  # M9.3:`--force` 接管了另一个没结束的任务 —— 审计里留一条(输出里已经写明,这里落盘)
1367
1562
  if [ -n "${TEAM_DISPATCH_STACK_PREV:-}" ]; then
@@ -1374,7 +1569,7 @@ team_cmd_dispatch() {
1374
1569
  done <<< "$TEAM_DISPATCH_AUDIT_LINES"
1375
1570
  TEAM_DISPATCH_AUDIT_LINES=""
1376
1571
  fi
1377
- team_ok "dispatched $id → $TEAM_SESSION:$agent(含启动校验:proof=spawn pid=$pid;provider=$provider model=${model#*/} session=$sid)"
1572
+ team_ok "dispatched $id → $TEAM_SESSION:$agent(含启动校验:proof=spawn pid=$pid;$(team_dispatch_identity_claim "$identity_via" "$provider" "${model#*/}" "$sid"))"
1378
1573
  # 观察结论也要说出来(不是只报“成功”):内建 Pi 路径下,agent 是「已经退出」还是「还在跑」,
1379
1574
  # PM 都要拿到**观察到的事实**(退出码来自窗口 harness 写下的事件证据,不是猜的;也不再假设
1380
1575
  # 「窗口一定已经回到 shell」——那正是 M7.5 里被环境 churn 打脸的那句话)。
@@ -1395,7 +1590,7 @@ team_cmd_dispatch() {
1395
1590
 
1396
1591
  team_pane_snapshot() { # <target> → pane 内容指纹(用于确认投递真的落到 TUI)
1397
1592
  team_tmux_target_required "capture-pane" "${1:-}" || return 1
1398
- tmux capture-pane -p -t "$1" 2>/dev/null | tail -c 400 | cksum | tr -d ' \n'
1593
+ team_tmux capture-pane -p -t "$1" 2>/dev/null | tail -c 400 | cksum | tr -d ' \n'
1399
1594
  }
1400
1595
 
1401
1596
  # agent 没在跑时:消息仍然要落到收件箱(durable),但绝不打字(空提示符会当命令执行)
@@ -1505,11 +1700,250 @@ team_cmd_say() {
1505
1700
  }
1506
1701
 
1507
1702
  team_inbox_append() { # <agent> <tag> <msg> [<sender>]
1508
- local dir; dir="$(team_inbox_dir)"
1509
- mkdir -p "$dir"
1703
+ local line
1510
1704
  # P82:第 4 参可选 = **发送者**(notify 传解析出来的发送者,收件人只当文件名);
1511
1705
  # say/draft/pulse 不传,沿用「owner 标签」(它们另有 --from/标签语义,本次不动)。
1512
- printf -- '- %s [%s] agent:%s · %s\n' "$(team_timestamp)" "$2" "${4:-$1}" "$3" >> "$dir/$1.md"
1706
+ line="$(printf '%s [%s] agent:%s · %s' "$(team_timestamp)" "$2" "${4:-$1}" "$3")"
1707
+ # turn-notice-merge:与扩展的合并路径共用**同一把锁**(同一收件人)。手工/队列/pulse 行的 tag 不是
1708
+ # auto 类 → 不参与合并、不进明细,但它的追加仍然落在临界区里(不会与合并的截尾重写互相丢写)。
1709
+ team_inbox_write_line "$1" "$line" >/dev/null
1710
+ }
1711
+
1712
+ # ---------------------------------------------------------------- 收件箱写入口(turn-notice-merge · 写入面)
1713
+ # 合同在 openspec/specs/notify-and-inbox(ADDED「raw events stay in a bounded detail file」+ MODIFIED 的
1714
+ # 合并键/合并行/叫醒段)。这里的实现要点:
1715
+ # ① 合并在**写入侧**、只认收件箱的**尾行**(截尾重写),锚校验不过一律追加(fail-safe);
1716
+ # ② 每收件人一把锁(flock;没有 flock 时降级为无锁,行为与历史一致);
1717
+ # ③ **先明细、后合并判定**:state/notify.log 一行一事件,所以汇总行的 `×N` 永远等于该 run 的
1718
+ # 明细记录数;明细写失败 → 本事件不合并、行带 `detail=unavailable`、日志点名,该行也不做锚;
1719
+ # ④ 只有 [auto*] 类行参与合并与明细;[manual]/[draft]/[nudge]/pulse 行原样追加。
1720
+
1721
+ # 行 → 合并键(seat|class|task|awaiting)。非 [auto*] 类 / 解析不成立 → rc 1(调用方按「不可合并」处理)。
1722
+ # awaiting 是**集合**(去重、排序;缺字段与空集是两个不同的值);task 取 task= 戳里的 ID,否则取
1723
+ # agent 后的第一个 <ID> token;branch/tip/uncommitted 不进键(它们只更新显示值)。
1724
+ team_notify_line_key() {
1725
+ local body="$1"
1726
+ case "$body" in '- '*) body="${body#- }" ;; esac
1727
+ local iso rest tag class seat fields aw nrm t
1728
+ iso="${body%% *}"
1729
+ case "$iso" in ''|*[!0-9TZ:.-]*) return 1 ;; esac
1730
+ rest="${body#"$iso" }"
1731
+ [ "$rest" != "$body" ] || return 1
1732
+ # ⚠ 前缀(未提交告警)不是键的一部分:跳过它直到 tag 的 '['
1733
+ case "$rest" in
1734
+ '['*) ;;
1735
+ '⚠'*) rest="[${rest#*\[}" ;;
1736
+ *) return 1 ;;
1737
+ esac
1738
+ rest="${rest#\[}"
1739
+ tag="${rest%%\]*}"
1740
+ rest="${rest#"$tag"\]}"
1741
+ case "$rest" in ' '*) rest="${rest# }" ;; *) return 1 ;; esac
1742
+ case "$tag" in
1743
+ auto) class=delivered ;;
1744
+ 'auto·suspended') class=suspended ;;
1745
+ 'auto·interrupted') class=interrupted ;;
1746
+ *) return 1 ;;
1747
+ esac
1748
+ case "$rest" in agent:*) ;; *) return 1 ;; esac
1749
+ rest="${rest#agent:}"
1750
+ seat="${rest%% *}"
1751
+ [ -n "$seat" ] || return 1
1752
+ rest="${rest#"$seat"}"
1753
+ case "$rest" in ' '*) rest="${rest# }" ;; *) return 1 ;; esac
1754
+ fields="${body%% :: *}"
1755
+ nrm='-'
1756
+ case "$fields" in
1757
+ *awaiting=*)
1758
+ aw="${fields#*awaiting=}"; aw="${aw%% *}"
1759
+ nrm="A:$(printf '%s' "$aw" | tr ',' '\n' | sed 's/^[[:space:]]*//; s/[[:space:]]*$//' | grep -v '^$' | LC_ALL=C sort -u | paste -sd, -)" ;;
1760
+ esac
1761
+ if [[ "$fields" =~ task=([A-Za-z0-9._-]+) ]]; then
1762
+ t="${BASH_REMATCH[1]}"
1763
+ else
1764
+ case "$rest" in '· '*) rest="${rest#· }" ;; esac
1765
+ t="${rest%% · *}"
1766
+ t="${t%% *}"
1767
+ fi
1768
+ [ -n "$t" ] || t='?'
1769
+ printf '%s|%s|%s|%s\n' "$seat" "$class" "$t" "$nrm"
1770
+ }
1771
+
1772
+ # 尾行能不能当锚:带 detail=unavailable 的行不是锚(否则下一次成功的事件会把它抹掉,缺口就不可见了)。
1773
+ team_notify_line_anchor_ok() {
1774
+ case "$1" in *'detail=unavailable'*) return 1 ;; esac
1775
+ return 0
1776
+ }
1777
+
1778
+ team_notify_line_iso() { local body="${1#- }"; printf '%s\n' "${body%% *}"; }
1779
+
1780
+ team_notify_line_iso_last() { # <行> → 字段区里 last=<ISO> 的值(没有 → 空)
1781
+ local body="${1#- }" fields v
1782
+ fields="${body%% :: *}"
1783
+ case "$fields" in
1784
+ *'last='*) v="${fields#*last=}"; v="${v%% *}" ;;
1785
+ *) v='' ;;
1786
+ esac
1787
+ printf '%s\n' "$v"
1788
+ }
1789
+
1790
+ # 行已汇总的事件数(汇总行带 `×N`;单事件行 = 1)。只读字段区(` :: ` 之前)。
1791
+ team_notify_line_events() {
1792
+ local body="${1#- }" head n
1793
+ head="${body%% :: *}"
1794
+ case "$head" in
1795
+ *'×'*)
1796
+ n="${head#*×}"; n="${n%% *}"
1797
+ case "$n" in ''|*[!0-9]*) n=1 ;; esac ;;
1798
+ *) n=1 ;;
1799
+ esac
1800
+ printf '%s\n' "$n"
1801
+ }
1802
+
1803
+ # 汇总行 = **最后一条事件的行** + 首事件时间戳 + 字段区 `×N first= last=`(branch/tip/uncommitted 自动是最后一条的值)。
1804
+ team_notify_line_merge() { # <新事件行> <首时间戳> <n> <末时间戳>
1805
+ local c="$1" first="$2" n="$3" last="$4" head tail rest
1806
+ case "$c" in
1807
+ *' :: '*) head="${c%% :: *}"; tail="${c#* :: }" ;;
1808
+ *) head="$c"; tail='' ;;
1809
+ esac
1810
+ rest="${head#* }"
1811
+ head="${first} ${rest} · ×${n} first=${first} last=${last}"
1812
+ if [ "${c%% :: *}" != "$c" ]; then printf '%s :: %s\n' "$head" "$tail"; else printf '%s\n' "$head"; fi
1813
+ }
1814
+
1815
+ team_notify_line_mark_detail() { # <行> · 字段区带 detail=unavailable(明细写失败的事件,绝不参与合并)
1816
+ local c="$1"
1817
+ case "$c" in
1818
+ *' :: '*) printf '%s · detail=unavailable :: %s\n' "${c%% :: *}" "${c#* :: }" ;;
1819
+ *) printf '%s · detail=unavailable\n' "$c" ;;
1820
+ esac
1821
+ }
1822
+
1823
+ team_notify_log_line() { # <文本>:写进扩展日志(TEAM_NOTIFY_LOG)
1824
+ local f="${TEAM_NOTIFY_LOG:-/tmp/teamsmith-notify.log}"
1825
+ printf '%s %s\n' "$(team_timestamp)" "$1" >> "$f" 2>/dev/null || true
1826
+ }
1827
+
1828
+ team_notify_detail_max() {
1829
+ local n="${TEAM_NOTIFY_DETAIL_MAX:-2048}"
1830
+ case "$n" in ''|*[!0-9]*) n=2048 ;; esac
1831
+ [ "$n" -gt 0 ] || n=2048
1832
+ printf '%s\n' "$n"
1833
+ }
1834
+
1835
+ team_notify_detail_events() { # <明细文件> → 事件行数(轮转头不算)
1836
+ local n=''
1837
+ if [ -f "$1" ]; then n="$(grep -c '^- ' "$1" 2>/dev/null)" || true; fi
1838
+ case "$n" in ''|*[!0-9]*) n=0 ;; esac
1839
+ printf '%s\n' "$n"
1840
+ }
1841
+
1842
+ # 先明细:一行一事件(内容 = 该事件单发形状的收件箱行)。越界 → 轮转一代到 <file>.1 + 新代轮转头。
1843
+ team_notify_detail_append() { # <事件行> <明细文件> → 0 = 已落盘;1 = 失败(调用方写 detail=unavailable)
1844
+ local content="$1" file="$2" dir max count
1845
+ dir="$(dirname "$file")"
1846
+ mkdir -p "$dir" || return 1
1847
+ max="$(team_notify_detail_max)"
1848
+ count="$(team_notify_detail_events "$file")"
1849
+ if [ "$count" -ge "$max" ]; then
1850
+ mv -f "$file" "$file.1" || return 1
1851
+ printf '%s\n' "# rotation $(team_timestamp) moved=${count} path=${file}.1" > "$file" || return 1
1852
+ team_notify_log_line "notify detail rotated: moved=${count} to=${file}.1"
1853
+ fi
1854
+ printf -- '- %s\n' "$content" >> "$file" 2>/dev/null || return 1
1855
+ return 0
1856
+ }
1857
+
1858
+ # 截掉尾行、写回汇总行(同一目录的临时文件 + mv,读者不会看到半行)。
1859
+ team_inbox_rewrite_last() { # <收件箱文件> <新尾行> → 0 = 已重写;1 = 没动
1860
+ local file="$1" line="$2" dir tmp
1861
+ dir="$(dirname "$file")"
1862
+ tmp="$(mktemp "$dir/.inbox-merge.XXXXXX")" || return 1
1863
+ if ! sed '$d' "$file" > "$tmp" || ! printf -- '- %s\n' "$line" >> "$tmp"; then
1864
+ rm -f "$tmp"
1865
+ return 1
1866
+ fi
1867
+ [ -f "$file" ] && chmod --reference="$file" "$tmp" 2>/dev/null || true
1868
+ if ! mv -f "$tmp" "$file" 2>/dev/null; then rm -f "$tmp"; return 1; fi
1869
+ return 0
1870
+ }
1871
+
1872
+ # 临界区(调用方持锁):先明细 → 尾行锚判定 → 截尾重写或追加 → 打印写入结果。
1873
+ team_inbox_write_locked() { # <recipient> <行> <文件> <明细> → state=new|extended first=<ISO> key=<键>
1874
+ local recipient="$1" content="$2" file="$3" detail="$4"
1875
+ local key='' detail_failed=0 newcontent="$content" state=new first='' tail_line merged n
1876
+ key="$(team_notify_line_key "$content" || true)"
1877
+ if [ -n "$key" ]; then
1878
+ if ! team_notify_detail_append "$content" "$detail"; then
1879
+ detail_failed=1
1880
+ newcontent="$(team_notify_line_mark_detail "$content")"
1881
+ team_notify_log_line "notify detail write failed: $detail(本事件不合并,行带 detail=unavailable)"
1882
+ fi
1883
+ fi
1884
+ first="$(team_notify_line_iso "$newcontent")"
1885
+ if [ -n "$key" ] && [ "$detail_failed" = 0 ] && [ -s "$file" ]; then
1886
+ tail_line="$(tail -n 1 "$file")"
1887
+ if [ -n "$tail_line" ] && team_notify_line_anchor_ok "$tail_line" \
1888
+ && [ "$(team_notify_line_key "$tail_line" 2>/dev/null || true)" = "$key" ]; then
1889
+ n=$(( $(team_notify_line_events "$tail_line") + 1 ))
1890
+ merged="$(team_notify_line_merge "$newcontent" "$(team_notify_line_iso "$tail_line")" "$n" "$first")"
1891
+ if team_inbox_rewrite_last "$file" "$merged"; then
1892
+ state=extended
1893
+ first="$(team_notify_line_iso "$tail_line")"
1894
+ fi
1895
+ fi
1896
+ fi
1897
+ if [ "$state" = new ]; then
1898
+ printf -- '- %s\n' "$newcontent" >> "$file" || return 1
1899
+ fi
1900
+ # 写入结果协议(单行一段,扩展用 ^first=/^key= 解析):state=new|extended
1901
+ printf 'state=%s\nfirst=%s\nkey=%s\n' "$state" "$first" "${key:--}"
1902
+ return 0
1903
+ }
1904
+
1905
+ # 每收件人一把锁的写入口(team_inbox_append 与扩展的 __inbox-write 共用)。
1906
+ team_inbox_write_line() { # <recipient> <行> → 打印写入结果(同 team_inbox_write_locked)
1907
+ local recipient="$1" content="$2"
1908
+ local dir file detail lockfd rc=0
1909
+ dir="$(team_inbox_dir)"; file="$dir/$recipient.md"
1910
+ local state; state="$(team_state_dir)"
1911
+ detail="$state/notify.log"
1912
+ mkdir -p "$dir" || return 1
1913
+ if command -v flock >/dev/null 2>&1; then
1914
+ # exec 加**不带命令**的重定向会永久改变当前 shell:`exec {fd}>&- 2>/dev/null` 会把 stderr 永久接到
1915
+ # /dev/null(P251 实测:say 的 team_warn 从此消失)。这里只关 fd,不带任何 stderr 重定向。
1916
+ if ! exec {lockfd}>"$state/inbox-$recipient.lock"; then return 1; fi
1917
+ if flock -w 10 "$lockfd"; then
1918
+ team_inbox_write_locked "$recipient" "$content" "$file" "$detail" || rc=$?
1919
+ else
1920
+ team_notify_log_line "inbox lock timeout: $state/inbox-$recipient.lock"
1921
+ rc=1
1922
+ fi
1923
+ exec {lockfd}>&-
1924
+ return $rc
1925
+ fi
1926
+ team_inbox_write_locked "$recipient" "$content" "$file" "$detail"
1927
+ }
1928
+
1929
+ # 内部命令(扩展消费写入结果;不进 help):--agent <席位> --from-file <单行文件>
1930
+ team_cmd_inbox_write() {
1931
+ local agent='' src=''
1932
+ while [ $# -gt 0 ]; do
1933
+ case "$1" in
1934
+ --agent) agent="${2:?--agent 需要值}"; shift 2 ;;
1935
+ --agent=*) agent="${1#*=}"; shift ;;
1936
+ --from-file) src="${2:?--from-file 需要文件}"; shift 2 ;;
1937
+ --from-file=*) src="${1#*=}"; shift ;;
1938
+ -*) team_usage_die "__inbox-write: 未知参数 $1" ;;
1939
+ *) team_usage_die "__inbox-write: 多余参数 $1" ;;
1940
+ esac
1941
+ done
1942
+ [ -n "$agent" ] || team_usage_die "__inbox-write: 需要 --agent <席位>"
1943
+ if [ -z "$src" ] || [ ! -f "$src" ]; then team_usage_die "__inbox-write: 需要 --from-file <行文件>"; fi
1944
+ local content; content="$(head -n 1 "$src")"
1945
+ if [ -z "$content" ]; then printf 'state=failed\nfirst=-\nkey=-\n'; return 1; fi
1946
+ team_inbox_write_line "$agent" "$content"
1513
1947
  }
1514
1948
 
1515
1949
  # notify 的去重键:内容 + 长度(同一份通知在 TEAM_NOTIFY_DEDUP_SEC 内只入队/投递一次)。
@@ -1656,6 +2090,27 @@ team_cmd_notify() {
1656
2090
  return "$knock_rc"
1657
2091
  }
1658
2092
 
2093
+ team_teardown_seat_locked() {
2094
+ local a="$1" purge="$2" force="$3" register="$4" fp="$5" w wt
2095
+ if [ "$register" = 1 ]; then team_config_write_roster remove "$a" "$fp" 0 || return $?; fi
2096
+ w="$(team_state_get "$a" window "$a")"
2097
+ if team_tmux_has_window "$TEAM_SESSION" "$w"; then
2098
+ team_tmux kill-window -t "$TEAM_SESSION:$w" 2>/dev/null || return 1
2099
+ team_ok "kill window $TEAM_SESSION:$w"
2100
+ fi
2101
+ team_state_clear "$a"
2102
+ rm -f "$TEAM_STATE_DIR/$a.pending" "$TEAM_STATE_DIR"/.assignment-"$a"-*
2103
+ if [ "$purge" = 1 ]; then
2104
+ wt="$(team_agent_worktree "$a")"
2105
+ if [ -d "$wt" ]; then
2106
+ if [ "$force" != 1 ] && [ -n "$(git -C "$wt" status --porcelain 2>/dev/null)" ]; then
2107
+ team_warn "worktree 有未提交改动,保留:$wt(要强删:--force)"; return 0
2108
+ fi
2109
+ team_git worktree remove --force "$wt" 2>/dev/null && team_ok "remove worktree $wt" || team_warn "worktree 删除失败:$wt"
2110
+ team_scan_invalidate git
2111
+ fi
2112
+ fi
2113
+ }
1659
2114
  team_cmd_teardown() {
1660
2115
  local agent="" all=0 purge=0 force=0 register=0 fp=""
1661
2116
  while [ $# -gt 0 ]; do
@@ -1680,29 +2135,9 @@ team_cmd_teardown() {
1680
2135
  fi
1681
2136
  # P99/R2:--register 先缩名册(审计写入器),再走今天的窗口/state/worktree 清理;
1682
2137
  # 名册里没有这个席位就 exit 5、什么都不动。不带旗标时名册逐字节不变(今天的行为是默认)。
1683
- if [ "$register" = "1" ]; then
1684
- local rrc=0
1685
- team_config_write_roster remove "$agent" "$fp" 0 || rrc=$?
1686
- [ "$rrc" -eq 0 ] || return "$rrc"
1687
- fi
1688
- local a w wt
2138
+ local a
1689
2139
  for a in "${targets[@]}"; do
1690
- w="$(team_state_get "$a" window "$a")"
1691
- if team_tmux_has_window "$TEAM_SESSION" "$w"; then
1692
- tmux kill-window -t "$TEAM_SESSION:$w" 2>/dev/null && team_ok "kill window $TEAM_SESSION:$w"
1693
- fi
1694
- team_state_clear "$a"
1695
- if [ "$purge" = "1" ]; then
1696
- wt="$(team_agent_worktree "$a")"
1697
- if [ -d "$wt" ]; then
1698
- if [ "$force" != "1" ] && [ -n "$(git -C "$wt" status --porcelain 2>/dev/null)" ]; then
1699
- team_warn "worktree 有未提交改动,保留:$wt(要强删:--force)"
1700
- continue
1701
- fi
1702
- team_git worktree remove --force "$wt" 2>/dev/null && team_ok "remove worktree $wt" || team_warn "worktree 删除失败:$wt"
1703
- team_scan_invalidate git # M50
1704
- fi
1705
- fi
2140
+ team_with_lock "seat:$a" team_with_lock worker-admission team_teardown_seat_locked "$a" "$purge" "$force" "$register" "$fp" || return $?
1706
2141
  done
1707
2142
  [ "$purge" != "1" ] && team_dim "worktree 仍保留(加 --purge 删除);分支保留,需要时 git branch -d"
1708
2143
  return 0