teamsmith 0.2.0 → 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 (117) hide show
  1. package/README.md +4 -4
  2. package/package.json +1 -1
  3. package/skills/teamsmith/CHANGELOG.md +28 -0
  4. package/skills/teamsmith/SKILL.md +14 -7
  5. package/skills/teamsmith/extension/team-notify.ts +37 -6
  6. package/skills/teamsmith/extension/team.ts +438 -0
  7. package/skills/teamsmith/references/config.md +39 -6
  8. package/skills/teamsmith/references/migration.md +197 -47
  9. package/skills/teamsmith/references/protocol.md +176 -19
  10. package/skills/teamsmith/references/troubleshooting.md +124 -7
  11. package/skills/teamsmith/references/workflows.md +36 -2
  12. package/skills/teamsmith/scripts/lib/cmd-agents.sh +318 -23
  13. package/skills/teamsmith/scripts/lib/cmd-bootstrap.sh +2 -2
  14. package/skills/teamsmith/scripts/lib/cmd-config.sh +349 -67
  15. package/skills/teamsmith/scripts/lib/cmd-draft.sh +4 -4
  16. package/skills/teamsmith/scripts/lib/cmd-help.sh +677 -0
  17. package/skills/teamsmith/scripts/lib/cmd-init.sh +157 -5
  18. package/skills/teamsmith/scripts/lib/cmd-interrupt.sh +461 -0
  19. package/skills/teamsmith/scripts/lib/cmd-meeting.sh +2 -25
  20. package/skills/teamsmith/scripts/lib/cmd-project.sh +30 -103
  21. package/skills/teamsmith/scripts/lib/cmd-reap.sh +277 -0
  22. package/skills/teamsmith/scripts/lib/cmd-reply.sh +3 -3
  23. package/skills/teamsmith/scripts/lib/cmd-review.sh +175 -30
  24. package/skills/teamsmith/scripts/lib/cmd-status.sh +226 -17
  25. package/skills/teamsmith/scripts/lib/cmd-update.sh +36 -7
  26. package/skills/teamsmith/scripts/lib/cmd-watch.sh +564 -46
  27. package/skills/teamsmith/scripts/lib/common.sh +773 -48
  28. package/skills/teamsmith/scripts/lib/gate-lock.sh +544 -0
  29. package/skills/teamsmith/scripts/panel/draft-send.sh +11 -1
  30. package/skills/teamsmith/scripts/panel/panel.js +207 -207
  31. package/skills/teamsmith/scripts/panel/src/App.tsx +58 -21
  32. package/skills/teamsmith/scripts/panel/src/clipboard.ts +11 -5
  33. package/skills/teamsmith/scripts/panel/src/compose.ts +6 -3
  34. package/skills/teamsmith/scripts/panel/src/data.ts +11 -3
  35. package/skills/teamsmith/scripts/panel/src/layout.ts +209 -69
  36. package/skills/teamsmith/scripts/panel/src/main.tsx +33 -8
  37. package/skills/teamsmith/scripts/panel/src/strings/en.ts +39 -29
  38. package/skills/teamsmith/scripts/panel/src/strings/zh.ts +41 -31
  39. package/skills/teamsmith/scripts/panel/src/types.ts +28 -3
  40. package/skills/teamsmith/scripts/replies.mjs +4 -1
  41. package/skills/teamsmith/scripts/shim/tmux +18 -0
  42. package/skills/teamsmith/scripts/strings/commands.tsv +86 -0
  43. package/skills/teamsmith/scripts/strings/help.en.tsv +503 -0
  44. package/skills/teamsmith/scripts/strings/help.zh.tsv +489 -0
  45. package/skills/teamsmith/scripts/team +19 -6
  46. package/skills/teamsmith/templates/AGENTS.section.md.tmpl +8 -1
  47. package/skills/teamsmith/templates/config.sh.tmpl +20 -1
  48. package/skills/teamsmith/templates/pm-prompt.md.tmpl +21 -0
  49. package/skills/teamsmith/tests/bg-ledger-harness.mjs +464 -40
  50. package/skills/teamsmith/tests/bg-ledger.sh +168 -10
  51. package/skills/teamsmith/tests/checkout-shape-probe.sh +3 -1
  52. package/skills/teamsmith/tests/config-cli.sh +160 -14
  53. package/skills/teamsmith/tests/fixtures/delivery-truth-real/scenario.sh +7 -1
  54. package/skills/teamsmith/tests/fixtures/p287/lint-good.sh.in +3 -0
  55. package/skills/teamsmith/tests/fixtures/p287/lint-hardcoded.sh.in +3 -0
  56. package/skills/teamsmith/tests/fixtures/p287/lint-no-ns.sh.in +3 -0
  57. package/skills/teamsmith/tests/fixtures/p55/flip-p49.sh +18 -6
  58. package/skills/teamsmith/tests/flip-m33.sh +5 -3
  59. package/skills/teamsmith/tests/flip-m37.sh +4 -1
  60. package/skills/teamsmith/tests/flip-m4.3.sh +4 -1
  61. package/skills/teamsmith/tests/flip-m6.3.sh +4 -1
  62. package/skills/teamsmith/tests/flip-m6.5.sh +4 -1
  63. package/skills/teamsmith/tests/flip-m7.2.sh +4 -1
  64. package/skills/teamsmith/tests/flip-p158.sh +2 -1
  65. package/skills/teamsmith/tests/flip-p210.sh +4 -1
  66. package/skills/teamsmith/tests/flip-p217.sh +4 -1
  67. package/skills/teamsmith/tests/flip-p287-lock.sh +342 -0
  68. package/skills/teamsmith/tests/flip-p287-name.sh +279 -0
  69. package/skills/teamsmith/tests/flip-p304-read-side.sh +98 -0
  70. package/skills/teamsmith/tests/flip-p308-skip-not-pass.sh +151 -0
  71. package/skills/teamsmith/tests/flip-p309-p162-deep.sh +122 -0
  72. package/skills/teamsmith/tests/flip-p310-lock.sh +216 -0
  73. package/skills/teamsmith/tests/flip-p310-socket.sh +125 -0
  74. package/skills/teamsmith/tests/help-surface.sh +1664 -0
  75. package/skills/teamsmith/tests/install-shape.sh +279 -20
  76. package/skills/teamsmith/tests/key-replies-pty.sh +9 -10
  77. package/skills/teamsmith/tests/key-replies-ui.sh +0 -7
  78. package/skills/teamsmith/tests/key-replies.sh +240 -1
  79. package/skills/teamsmith/tests/ledger-ban.sh +5 -4
  80. package/skills/teamsmith/tests/ledger-claim.sh +2 -1
  81. package/skills/teamsmith/tests/lib/bounded-spin.sh +90 -0
  82. package/skills/teamsmith/tests/lib/model-policy.sh +4 -0
  83. package/skills/teamsmith/tests/lib/model-routing-flips.py +51 -0
  84. package/skills/teamsmith/tests/lib/reply-fixture.sh +4 -9
  85. package/skills/teamsmith/tests/lib/section-guard.sh +258 -13
  86. package/skills/teamsmith/tests/lib/tmp-root.sh +139 -27
  87. package/skills/teamsmith/tests/lib/tmux-cap.sh +10 -1
  88. package/skills/teamsmith/tests/lib/tmux-iso.sh +31 -13
  89. package/skills/teamsmith/tests/loop-inventory.tsv +59 -47
  90. package/skills/teamsmith/tests/panel-b2.sh +106 -3
  91. package/skills/teamsmith/tests/panel-b3-stub.sh +112 -4
  92. package/skills/teamsmith/tests/panel-b3.sh +175 -19
  93. package/skills/teamsmith/tests/panel-choices.sh +11 -6
  94. package/skills/teamsmith/tests/panel-cpu.sh +2 -1
  95. package/skills/teamsmith/tests/panel-knobs.sh +7 -1
  96. package/skills/teamsmith/tests/panel-p21.sh +156 -73
  97. package/skills/teamsmith/tests/panel-snapshots.sh +41 -225
  98. package/skills/teamsmith/tests/panel-strings.mjs +33 -18
  99. package/skills/teamsmith/tests/perf.sh +121 -30
  100. package/skills/teamsmith/tests/routes.sh +327 -93
  101. package/skills/teamsmith/tests/seat-interrupt.sh +1002 -0
  102. package/skills/teamsmith/tests/seat-visibility.sh +675 -0
  103. package/skills/teamsmith/tests/section-budgets.tsv +20 -10
  104. package/skills/teamsmith/tests/section-guard.sh +6 -1
  105. package/skills/teamsmith/tests/section-needs-audit.sh +24 -7
  106. package/skills/teamsmith/tests/section-paths.tsv +19 -13
  107. package/skills/teamsmith/tests/section-select.sh +8 -1
  108. package/skills/teamsmith/tests/slash-team-e2e.sh +323 -0
  109. package/skills/teamsmith/tests/slash-team.sh +617 -0
  110. package/skills/teamsmith/tests/smoke.sh +2972 -440
  111. package/skills/teamsmith/tests/task-model-routing.sh +507 -10
  112. package/skills/teamsmith/tests/tmp-hygiene.sh +782 -45
  113. package/skills/teamsmith/tests/tmux-identity-leak.sh +773 -0
  114. package/skills/teamsmith/tests/tmux-lint-legacy.txt +12 -18
  115. package/skills/teamsmith/tests/upgrade-recipe.sh +537 -0
  116. package/skills/teamsmith-init/SKILL.md +12 -12
  117. package/skills/teamsmith-init/references/bootstrap.md +3 -3
@@ -1,107 +1,8 @@
1
1
  #!/usr/bin/env bash
2
2
  # teamsmith · init / doctor / help
3
3
 
4
- team_help() {
5
- cat <<'EOF'
6
- teamsmith — 用 Pi Agent 组建一个可复用的多 Agent 团队(PM 编排 + worker 并行)
7
-
8
- 用法: team <command> [args...] [--yes]
9
-
10
- ── 第一次使用 ─────────────────────────────────────────────
11
- bootstrap [--agents "dev verify"] [--task-models '<map>'] [--pm-model '<model>'] [--print]
12
- **推荐**:一条命令把项目初始化到可派单状态:写配置 + 文档骨架 + AGENTS 段落 + 把 skill 装进
13
- 项目 .pi/skills/ → 建 agent worktree → 起巡检窗口 → 打印下一步清单;幂等,可反复跑
14
- --task-models/--pm-model 是新鲜项目的必填输入(没有默认模型可猜);已有合法策略重入可省
15
- (退休键的旧项目在任何写入之前拒绝,指向 config upgrade-models;--print 只诊断不写)
16
- init 只做配置/文档骨架 + 把 skill 装进项目 .pi/skills/(bootstrap 的其中一步)
17
- --task-models '<category=provider/model …>' --pm-model '<provider/model>'(新鲜项目必填)
18
- --copy 复制一份(默认软链,跟随包更新);--no-skills 跳过装 skill;--force 覆盖冲突条目
19
- doctor 环境自检(git/tmux/pi/门禁/forge/容量/容器)
20
-
21
- ── 观察 ───────────────────────────────────────────────────
22
- roster 名册:agent、窗口是否在跑、分支、脏文件、领先提交数
23
- status [ID] roster + 任务/report 状态;给 ID 时只显示该任务
24
- ps 容量:RAM/swap/还能再跑几个 agent + 模型并发占用(派单前看)
25
- bg list|stop <id> 后台作业车道:list 看本项目记录到的作业(pid/启动指纹/命令);stop 只按记录里的
26
- (pid, 启动时间指纹) 停那一个作业(组内子孙一起;身份对不上就拒)
27
- digest 给 PM 的待办摘要:收件箱未处理项 + 待复验报告 + 任务状态
28
- inbox [agent] 打印收件箱(agent 在回合结束时自动追加)
29
- paths 打印当前解析出的路径/ session(JSON),排障用
30
- config list|set|log|set-task-model|upgrade-models
31
- 项目契约(.pi/team/config.sh)的读写面:list 列每个键的效果类(apply/restart/refuse)
32
- 与默认值;set 是**唯一写入口**(值校验 + sha256 指纹 CAS + 原子写 + 审计);
33
- log 看审计尾部;set-task-model 按类别改模型;upgrade-models 显式四键升级
34
-
35
- ── 文档契约(PM 维护) ─────────────────────────────────────
36
- task ID --title ... --category <category> [--agent a] [--deps ...] [--issue N]
37
- 生成任务书 <docs>/tasks/ID-slug.md 并在 BOARD.md 建行
38
- board add|assign|set|row|ls BOARD.md 行管理(add [--allow-dup] / assign ID agent / set ID 状态 / row ID / ls)
39
- change status <id> [--json]
40
- change 的 readiness 视图(只读):任务/阶段/agent/看板/证据、delta 写者、阻塞项;
41
- ready(至少一个任务且全部结束)→ 退出码 0,否则 1
42
- thread <agent> ["msg"] 追加 / 读取往来记录(append-only)
43
- report ID <agent> [--force] 生成报告骨架
44
-
45
- ── 派单与协作 ─────────────────────────────────────────────
46
- add-agent <a> [--register] [--create] [--no-install] [--print] 建长期 worktree(分支 agent/<a>);
47
- 名册:--register 走审计写入器(不带旗标的未知席位会被拒,并给出两条真路线)
48
- dispatch <a> <ID> <task-file> [--model m] [--branch <name>] [--fresh] [--allow-overflow] [--print]
49
- 首次按类别/override 分配新 SID,完整同任务恢复保留 model/SID;换模型必须 --fresh;--branch 声明任务分支
50
- say <a> "<一句话>" [--now] 往 agent 窗口发消息;目标输入框里有草稿就**延后投递**(state/outbox/
51
- + 返回 queued);--now = 故意跳过守卫粘字(记入 outbox/forced.log)
52
- notify <a> "<一句话>" [--from <发送者>] agent → PM 一句话(发送者按运行时目录解析;写收件箱 + 唤醒 PM 窗口)
53
- draft [pm] 人的草稿入口:开/复用 draft 窗口跑 $EDITOR,保存退出后自动入队(不抢焦点)
54
- draft send [<文件>] [--now] 无头形式:把草稿文件整段投递(守卫路径)
55
- reply post --from <自称> --title <标题> --stdin 关键回复:显式发布一份要保留的正文(只从 stdin
56
- 取原文;随后照常把同一份答复发给人;保存失败不阻止答复)。reply list/show 只读
57
- outbox [list] 延后队列:待投递条目(活动 + held,编号供 drop)
58
- outbox flush [--now] 立刻排水(--now = 跳过守卫直投,留审计)
59
- outbox drop <n|all> 人显式丢弃队列条目
60
-
61
- ── 定时巡检(pulse 由 PM 配置和维护) ────────────────────
62
- pulse up|down|restart|status|logs [--print]
63
- 巡检(同 session 的 pulse 窗口跑 monitor + 定时巡检;只有一个后端,无容器依赖)
64
- watchdog …/watchdog-status/install-watchdog/uninstall-watchdog
65
- pulse 的旧名(别名期保留到 v2.0.0;先印一行弃用提示,再转交 pulse)
66
- monitor [--once] [--print|--json] [--width N] [--height N] [--interval N] [--events K] [--no-pulse]
67
- 状态面板(pulse 窗口跑的就是它):默认 TUI(Ink),管道/--print 出纯文本,--json 出机读对象;
68
- 非 TTY 自动走纯文本;按周期顺带跑巡检(--no-pulse = 只看不巡)。需要 node/bun/tsx(D19)
69
- watch [--once] [--interval N] [--ui] 手动/前台巡检(--ui = monitor)
70
- standby [on|off|status] [--reason "..."] PM 主动停工:on 之后 pulse 不再叫醒(人处理完 off)
71
-
72
- ── 跨项目会议(PM 对 PM 的 peer 交流,不是指令通道) ─────────
73
- meeting open <slug> --with <项目>[:<session>] --topic "…" [--ttl 72] [--yes]
74
- meeting say <slug> --intent <info|question|report|proposal|request> "…" [--knock]
75
- meeting read <slug> [--since N] [--peek] | meeting inbox | meeting list [--all]
76
- meeting propose <slug> "…" | meeting agree <slug> <A1> [--note "…"] | meeting close <slug>
77
- up [--agents] [--print] [--fresh-pm]
78
- 恢复 PM:建 tmux 场地 + 把 PM 拉起来(默认 pi -c 续上本目录上一个会话)
79
- --fresh-pm = 本次启动新开一场对话(不 -c;旧会话文件原样留在历史里,作为开工记录);
80
- 它最优先:这一次启动里 TEAM_PM_SESSION_ID / TEAM_PM_RESUME_ARGS 都不生效,且只影响
81
- 这一次启动、不写进配置(守护/巡检按配置拉起 PM 时照旧续跑;--print 会打出判定与命令)
82
- resume [--agent a] [--all] [--dry-run] PM 的工具:把停了但没交活的 agent 续跑
83
-
84
- ── 复验 / 合并 / 收尾 ─────────────────────────────────────
85
- review ID --dir <独立checkout> [--no-gates] [--strong] [--allow-unresolved-branch]
86
- 独立 detached worktree 上 checkout 分支 → 跑门禁 → 写
87
- <docs>/reviews/ID.md(PM 复验证据,不接受 agent 自述)
88
- 脏树 / 被忽略产物 / --branch 解析不到 → 默认拒绝(覆盖开关 TEAM_REVIEW_ALLOW_*)
89
- close ID 收尾:更新 BOARD、关窗口、保留 worktree(git 由 PM 做)
90
- teardown [--agent a] [--all] [--purge] [--force] [--register] 关窗口 / 删 worktree(--purge 才删 worktree);
91
- --register = 先从名册移除该席位(同样走审计写入器)
92
-
93
- smoke 在临时仓库里端到端自测这套工具(不碰当前项目)
94
- perf 性能判定(交互首帧 / 帧装配 / 稳态窗格 CPU):默认在参考镜像里跑;--host = 宿主(非参考环境)
95
- ── 版本与更新(skill 更新怎么拿到) ─────────────────────────
96
- mark-loaded [--version X] 记录本会话加载的 skill 版本(PM 开局跑一次)
97
- version [--check] 看磁盘版本 / SKILL.md 版本 / CHANGELOG / 本会话加载版本;--check 给结论
98
- changelog [--since X] 看 skill 变更史
99
- reload [--done] 请求重载(在 Pi 里 /reload 或 /teamsmith-reload 立即生效)
100
- version / help
101
-
102
- 配置:项目根 .pi/team/config.sh(见 references/config.md)。
103
- 协议:<docs>/PROTOCOL.md 或 SKILL.md 里的一页速览。
104
- EOF
4
+ team_help() { # 兼容入口:`team help` 的三级路由在 scripts/lib/cmd-help.sh(唯一渲染器)
5
+ team_help_route 0 help "$@"
105
6
  }
106
7
 
107
8
  # ---------------------------------------------------------------- init
@@ -261,7 +162,7 @@ team_cmd_init() {
261
162
  if [ -n "$legacy_keys" ]; then
262
163
  team_err "init: $cfg 还在用退休的模型键($(printf '%s' "$legacy_keys" | tr '\n' ' '))—— 任何写入之前停住"
263
164
  team_dim " 项目模型策略是破坏式升级:init / init --force / bootstrap 都不是迁移路径"
264
- team_dim " 升级(先预览):$TEAM_CLI config upgrade-models --task-models '<category=provider/model …>' --pm-model '<provider/model>' --dry-run"
165
+ team_legacy_model_route_lines "$cfg"
265
166
  return 1
266
167
  fi
267
168
  fi
@@ -329,7 +230,8 @@ team_cmd_init() {
329
230
  "PROJECT=$project" "SESSION=$session" "PM_WINDOW=$pmwin" \
330
231
  "DOCS_DIR=$docs" "AGENTS=$agents" "GATES=$(team_escape_dq "$gates")" "VCS=$vcs" \
331
232
  "SKILL_DIR=$TEAM_SKILL_DIR" \
332
- "DETECTED_VCS=$(team_detect_vcs)" "TODAY=$(date +%F)" > "$cfg"; then
233
+ "DETECTED_VCS=$(team_detect_vcs)" "TODAY=$(date +%F)" \
234
+ "CONFIG_VERSION=$TEAM_CONFIG_SHAPE_CURRENT" > "$cfg"; then
333
235
  team_err "init: 渲染配置失败:$tmpl"
334
236
  rm -f "$cfg"
335
237
  return 1
@@ -500,6 +402,21 @@ team_cmd_doctor() {
500
402
  check "tmux 闸门退役键"; printf '%s\n' "SKIP:没有 tmux,无法探测 server 全局环境"
501
403
  fi
502
404
 
405
+ # P304 · 本项目身份在运行中 server 全局环境里的残留(**只读**报告,绝不代改):残留会让同一 server 上
406
+ # **别的项目**新开的窗口继承本项目的身份(用户 2026-10-15 报的形状:server 死了重开,在别的项目里
407
+ # 被自己的身份守卫拒掉)。本 change 落地后产品路径不再新增它;这份只读行是「人自己决定清不清」的
408
+ # 出口(清它 = 改共享 server 的全局环境,属用户的管辖)。判据与读侧归因同源。
409
+ if team_have_cmd tmux; then
410
+ local id_residue="" id_vars="" id_route=""
411
+ id_residue="$(team_identity_server_residue_vars)"
412
+ if [ -n "$id_residue" ]; then
413
+ id_vars="$(printf '%s\n' "$id_residue" | awk -F= 'NF { printf "%s%s", (NR > 1 ? " " : ""), $1 }')"
414
+ id_route="$(team_identity_residue_route $id_vars)"
415
+ check "tmux 身份残留"
416
+ warn "运行中的 tmux server 全局环境里还有本项目的身份残留($(printf '%s' "$id_residue" | tr '\n' ' '))——它会让同一 server 上别的项目新开的窗口继承本项目的身份;只报告不代改,清掉(改共享 server,时机由你定):$id_route"
417
+ fi
418
+ fi
419
+
503
420
  check "pi"; if [ -n "${TEAM_AGENT_CMD:-}" ]; then
504
421
  pass "本项目用自定义 agent adapter:不需要 pi"
505
422
  elif team_have_cmd pi; then
@@ -738,6 +655,16 @@ team_cmd_doctor() {
738
655
  esac
739
656
  fi
740
657
 
658
+ # P270:项目本地的 `/team` 扩展(同一条 warn-don't-fail 线,同一个「没装就不刷行」的口径)。
659
+ local ext_row; ext_row="$(team_project_extension_install_row)"
660
+ if [ -n "$ext_row" ]; then
661
+ check "/team 扩展安装"
662
+ case "$ext_row" in
663
+ pass\ *) pass "${ext_row#pass }" ;;
664
+ warn\ *) warn "${ext_row#warn }" ;;
665
+ esac
666
+ fi
667
+
741
668
  check "陈旧 state"; local stale=0 a w
742
669
  for a in $(team_agents); do
743
670
  w="$(team_state_get "$a" window "$a")"
@@ -0,0 +1,277 @@
1
+ # teamsmith · team reap(P276 · change: orphan-visibility · delta: boundary)
2
+ #
3
+ # 合同:openspec/changes/orphan-visibility/specs/boundary/spec.md 的
4
+ # `Orphan seat processes of this project are reaped by pid, with an audit and no pattern`。
5
+ #
6
+ # 为什么它是一个**独立命令**而不是 `ps`/`status` 的副作用:读命令一个信号都不发。清理是破坏性动作,
7
+ # 只能由人在 argv 上显式授权(`--yes`),并且在每个目标**发信号之前**再验一遍——从普查到发信号之间
8
+ # 有 TOCTOU 窗口,一个刚被回收的 pid 可能是别人的进程、一个刚被重派的席位可能已经有活 pane。
9
+ # 既有纪律(`boundary#Signals go to a recorded pid, never to a name or a pattern`)要的正是「对象是
10
+ # 证明过的那个」,所以复验是**判据的一部分**,不是防御性装饰。
11
+ #
12
+ # 三条硬边界:
13
+ # ① **只按 pid 选**:没有 pkill/killall/名字/模式匹配;argv 里给的永远是显式 pid(或 `--orphans`
14
+ # 这一条由普查列出的计划),发信号永远 `kill <pid>`。
15
+ # ② `--yes` 是**唯一**授权:不看 TEAM_CONFIRM_WRITES、不看 TEAM_ASSUME_YES、不看任何继承值——
16
+ # 环境变量与配置键都不能让一个信号默认发出(合同明文)。
17
+ # ③ 跨项目零点名:别的项目的 pid/项目名/席位名/路径一个都不打印(这份读数会被抄进公开账本)。
18
+
19
+ # 审计日志的行格式:时间戳 / pid / 席位 / 信号 / 结果。**每个信号恰好一行**(SIGTERM 一行;有界等待后
20
+ # 仍活着才再发 SIGKILL,那也是一行)。计划态**不写**这个文件。
21
+ team_reap_log_path() { printf '%s\n' "${TEAM_STATE_DIR:-}/reap.log"; }
22
+
23
+ # 有界等待的秒数(SIGTERM 之后等这么久,然后再 SIGKILL)。它是个**有界**上界,不是睡眠:进程一退出
24
+ # 就立刻返回。合同只要求「有界」,没给它一个契约键,所以留在实现里(写进命令页)。
25
+ team_reap_grace() { printf '%s\n' "${TEAM_REAP_GRACE_SEC:-5}"; }
26
+
27
+ team_reap_log_open() { # 授权后打开审计日志(创建 state/ 与 reap.log;计划态绝不调它)
28
+ local d f
29
+ d="$(team_state_dir)"
30
+ f="$d/reap.log"
31
+ : >> "$f" 2>/dev/null || { team_err "reap: 审计日志写不了($f)—— 没有授权就没有动作"; return 1; }
32
+ return 0
33
+ }
34
+
35
+ team_reap_log_line() { # <pid> <seat> <signal> <outcome>
36
+ local f; f="$(team_reap_log_path)"
37
+ printf '%s pid=%s seat=%s signal=%s outcome=%s\n' \
38
+ "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${1:-?}" "${2:--}" "${3:-?}" "${4:-?}" >> "$f"
39
+ }
40
+
41
+ # 调用者自己或它的祖先(/proc/<pid>/stat 第 4 字段是 ppid)。**自己与祖先**永远不许杀:那是自杀/杀
42
+ # 起它的那个壳,与「孤儿」无关。走 /proc,不依赖 ps 的格式。
43
+ team_reap_is_self_or_ancestor() { # <pid>
44
+ local p="${1:-}" cur="$$" depth=0
45
+ case "$p" in ''|*[!0-9]*) return 1 ;; esac
46
+ [ "$p" = "$$" ] && return 0
47
+ while [ "$depth" -lt 64 ]; do
48
+ cur="$(awk '{print $4}' "/proc/$cur/stat" 2>/dev/null || true)"
49
+ case "$cur" in ''|*[!0-9]*) break ;; esac
50
+ [ "$cur" = "0" ] && break
51
+ [ "$cur" = "$p" ] && return 0
52
+ depth=$((depth + 1))
53
+ done
54
+ return 1
55
+ }
56
+
57
+ # 一个 pid 的 argv 快照(真 /proc 用一次 ps;夹具根读 cmdline)。形状判据自己也会读,但复验要的是
58
+ # **同一次**读到的快照——两次读之间有 execve 的窗口(M39 的既有教训)。
59
+ team_reap_argv_of() { # <pid> → argv 快照
60
+ local pid="${1:-}" root out
61
+ [ -n "$pid" ] || return 1
62
+ root="$(team_orphan_proc_root)"
63
+ if [ "$root" = "/proc" ]; then
64
+ out="$(ps -ww -o args= -p "$pid" 2>/dev/null | head -1)"
65
+ [ -n "$out" ] || return 1
66
+ printf '%s\n' "$out"
67
+ return 0
68
+ fi
69
+ team_orphan_read_cmdline "$pid"
70
+ }
71
+
72
+ team_reap_usage() {
73
+ printf '用法:team reap --orphans [--yes] | team reap <pid> [--yes]\n'
74
+ printf ' 计划态(没有 --yes):打印目标与将发的信号,一个信号都不发,退 0。\n'
75
+ printf ' 执行态(--yes):每个目标发信号前**再验一遍**(仍是本项目席位 / 仍没有活 pane / 启动时刻\n'
76
+ printf ' 与计划时一致),先 SIGTERM,有界等待后仍活着才 SIGKILL;每个信号一行 state/reap.log。\n'
77
+ printf ' --yes 是唯一授权:配置键、TEAM_ASSUME_YES、继承的环境变量都不能让它默认开。\n'
78
+ }
79
+
80
+ # 目标的第一段判定:这个 pid 现在是什么(own/foreign/anon/orphan/live/unjudged)。印出记录或非 0。
81
+ team_reap_target_record() { # <pid> → 一条普查记录
82
+ local pid="${1:-}" args
83
+ args="$(team_reap_argv_of "$pid" 2>/dev/null || true)"
84
+ [ -n "$args" ] || return 1
85
+ team_orphan_record_for "$pid" "$args"
86
+ }
87
+
88
+ # 复验:发信号**之前**,用与普查同一个判据再读一次;任何一条不成立都拒发并说明原因(写进 REAP_REASON)。
89
+ team_reap_verify() { # <pid> <计划时的 start> → 0 = 复验通过
90
+ local pid="${1:-}" planned="${2:-}" rec kind seat task start rss judge
91
+ REAP_REASON=""
92
+ rec="$(team_reap_target_record "$pid" 2>/dev/null || true)"
93
+ if [ -z "$rec" ]; then
94
+ REAP_REASON="它不再是席位形状的进程(命令行不再命中本项目配置的 agent/PM 可执行文件,或身份/命令行读不到)"
95
+ return 1
96
+ fi
97
+ IFS=$'\t' read -r _ kind seat task start rss judge <<< "$rec"
98
+ case "$kind" in
99
+ foreign) REAP_REASON="它的身份属于**别的项目**(跨项目零点名:这里不打印它的项目名)"; return 1 ;;
100
+ anon) REAP_REASON="它没有 teamsmith 身份(可能是人工起的同形进程)"; return 1 ;;
101
+ own) ;;
102
+ *) REAP_REASON="它的归属判不出来($kind)"; return 1 ;;
103
+ esac
104
+ case "$judge" in
105
+ live) REAP_REASON="它的 pane 又活了——现在不是孤儿了"; return 1 ;;
106
+ unjudged) REAP_REASON="它自己那个 tmux server 查询不到——判不了就不猜(未判定)"; return 1 ;;
107
+ orphan) ;;
108
+ *) REAP_REASON="它的孤儿判据给不出结论($judge)"; return 1 ;;
109
+ esac
110
+ if [ "$start" != "$planned" ]; then
111
+ REAP_REASON="进程启动时刻与计划时不一致(计划=$planned 现在=$start:pid 可能已被内核回收)"
112
+ return 1
113
+ fi
114
+ REAP_SEAT="$seat"
115
+ return 0
116
+ }
117
+
118
+ # 发信号:SIGTERM → 有界等待 → 仍活着再 SIGKILL。每个信号一行审计。
119
+ team_reap_signal() { # <pid> <seat> → 0 = 目标已退出
120
+ local pid="${1:-}" seat="${2:--}" grace ticks i alive=0
121
+ grace="$(team_reap_grace)"
122
+ case "$grace" in ''|*[!0-9]*) grace=5 ;; esac
123
+ if ! kill -TERM "$pid" 2>/dev/null; then
124
+ team_reap_log_line "$pid" "$seat" SIGTERM "failed"
125
+ team_err "reap: pid $pid(席位=$seat)SIGTERM 发不出去(进程刚好消失了?)"
126
+ return 1
127
+ fi
128
+ ticks=$(( grace * 10 ))
129
+ i=0
130
+ while [ "$i" -lt "$ticks" ]; do
131
+ kill -0 "$pid" 2>/dev/null || break
132
+ sleep 0.1
133
+ i=$((i + 1))
134
+ done
135
+ if ! kill -0 "$pid" 2>/dev/null; then
136
+ team_reap_log_line "$pid" "$seat" SIGTERM gone
137
+ team_ok "reap: pid $pid(席位=$seat)SIGTERM → 已退出"
138
+ return 0
139
+ fi
140
+ team_reap_log_line "$pid" "$seat" SIGTERM "alive→SIGKILL"
141
+ if ! kill -KILL "$pid" 2>/dev/null; then
142
+ team_reap_log_line "$pid" "$seat" SIGKILL failed
143
+ team_err "reap: pid $pid(席位=$seat)SIGKILL 发不出去"
144
+ return 1
145
+ fi
146
+ i=0
147
+ while [ "$i" -lt 100 ]; do
148
+ kill -0 "$pid" 2>/dev/null || break
149
+ sleep 0.1
150
+ i=$((i + 1))
151
+ done
152
+ if kill -0 "$pid" 2>/dev/null; then
153
+ team_reap_log_line "$pid" "$seat" SIGKILL "still-alive"
154
+ team_err "reap: pid $pid(席位=$seat)SIGKILL 之后仍然活着(不可中断/D 状态?)"
155
+ return 1
156
+ fi
157
+ team_reap_log_line "$pid" "$seat" SIGKILL gone
158
+ team_ok "reap: pid $pid(席位=$seat)SIGTERM 之后仍活着 → SIGKILL → 已退出"
159
+ return 0
160
+ }
161
+
162
+ team_cmd_reap() {
163
+ # `--yes` 是**唯一**授权:只看这一次 argv 上有没有它(scripts/team 的全局解析器把 TEAM_YES_ARGV 落成 1)。
164
+ # 显式不看 TEAM_CONFIRM_WRITES、不看继承来的 TEAM_ASSUME_YES——它们都不是「人这次说了要动手」的证据。
165
+ local yes=0 orphans=0 pid="" a
166
+ [ "${TEAM_YES_ARGV:-0}" = "1" ] && yes=1
167
+ while [ $# -gt 0 ]; do
168
+ case "$1" in
169
+ --yes) yes=1; shift ;;
170
+ --orphans) orphans=1; shift ;;
171
+ -h|--help) team_reap_usage; return 0 ;;
172
+ -*) team_usage_die "reap: 未知参数 $1" ;;
173
+ *)
174
+ case "$1" in *[!0-9]*) team_usage_die "reap: 目标只能是显式 pid 或 --orphans(收到 '$1')——本命令不接受名字或模式" ;; esac
175
+ [ -z "$pid" ] || team_usage_die "reap: 多余参数 $1"
176
+ pid="$1"; shift ;;
177
+ esac
178
+ done
179
+ if [ "$orphans" = 1 ] && [ -n "$pid" ]; then team_usage_die "reap: --orphans 与 <pid> 不同用"; fi
180
+ if [ "$orphans" != 1 ] && [ -z "$pid" ]; then
181
+ team_usage_die "reap: 需要 --orphans 或一个 pid(team reap --orphans [--yes] | team reap <pid> [--yes])"
182
+ fi
183
+ if [ "$orphans" = 1 ] && [ "${TEAM_ORPHAN_SCAN:-1}" != "1" ]; then
184
+ team_err "reap: 普查被关闭(TEAM_ORPHAN_SCAN=0)——列不出孤儿;用显式 pid(team reap <pid>)"
185
+ return 2
186
+ fi
187
+
188
+ team_orphan_ctx_init
189
+ local -a tpids=() tseats=() tmems=() tstarts=()
190
+
191
+ if [ -n "$pid" ]; then
192
+ # ── 单个 pid:先把它读成一条记录(拒绝集合在这里就落地) ──
193
+ local rec="" kind seat task start rss judge
194
+ rec="$(team_reap_target_record "$pid" 2>/dev/null || true)"
195
+ if [ -z "$rec" ]; then
196
+ team_err "reap: pid $pid 不是席位形状的进程(命令行不命中本项目配置的 agent/PM 可执行文件,或它的身份/命令行读不到)——一个信号都不发"
197
+ return 2
198
+ fi
199
+ IFS=$'\t' read -r _ kind seat task start rss judge <<< "$rec"
200
+ case "$kind" in
201
+ foreign) team_err "reap: pid $pid 属于**别的项目**的席位——拒发(跨项目零点名:它的项目名/席位名/路径都不打印)"; return 2 ;;
202
+ anon) team_err "reap: pid $pid 没有 teamsmith 身份——拒发(它可能不是任何项目的席位)"; return 2 ;;
203
+ esac
204
+ if team_reap_is_self_or_ancestor "$pid"; then
205
+ team_err "reap: pid $pid 是调用者自己或它的祖先——拒发(那不是孤儿,是起你的那个壳)"
206
+ return 2
207
+ fi
208
+ if [ "$judge" != "orphan" ]; then
209
+ case "$judge" in
210
+ live)
211
+ # F2(V52 返修轮次 1):这句拒绝必须**点名另一条入口** —— protocol §4a 的合约是「每个拒绝都
212
+ # 点名另一个入口」(interrupt 侧早已指向 `team reap --orphans`),而 reap 侧此前全文件
213
+ # `grep -c interrupt` = 0:一个活着的席位上被拒,用户拿着一个正确的拒绝却不知道该换哪条命令。
214
+ # 席位名读得到就给具体的(`team interrupt dev --reason …`),读不到(`-`)给占位。
215
+ local who="$seat"; [ "$who" != "-" ] || who="<seat>"
216
+ team_err "reap: pid $pid 不是孤儿(它的 pane 还活着)——它在跑一个回合,要结束它走 team interrupt $who --reason <一句话>;一个信号都不发" ;;
217
+ unjudged) team_err "reap: pid $pid 判不了(它自己那个 tmux server 查询不到)——不猜、不发" ;;
218
+ *) team_err "reap: pid $pid 的孤儿判据给不出结论($judge)——不发" ;;
219
+ esac
220
+ return 2
221
+ fi
222
+ tpids+=("$pid"); tseats+=("$seat"); tmems+=("$rss"); tstarts+=("$start")
223
+ else
224
+ # ── --orphans:普查列出的**本项目**孤儿就是计划 ──
225
+ while IFS=$'\t' read -r p kind seat task start rss judge; do
226
+ [ -n "$p" ] || continue
227
+ [ "$kind" = "own" ] || continue
228
+ [ "$judge" = "orphan" ] || continue
229
+ if team_reap_is_self_or_ancestor "$p"; then
230
+ team_warn "reap: 跳过 pid $p——它是调用者自己或它的祖先(不是孤儿)"
231
+ continue
232
+ fi
233
+ tpids+=("$p"); tseats+=("$seat"); tmems+=("$rss"); tstarts+=("$start")
234
+ done < <(team_orphan_census)
235
+ fi
236
+
237
+ local n=${#tpids[@]} i
238
+ local grace; grace="$(team_reap_grace)"
239
+ case "$grace" in ''|*[!0-9]*) grace=5 ;; esac
240
+
241
+ if [ "$n" -eq 0 ]; then
242
+ if [ "$yes" = 1 ]; then
243
+ team_reap_log_open || return 1
244
+ team_info "reap:没有本项目孤儿席位进程(一个信号都没发;审计日志 $(team_reap_log_path) 已就绪)"
245
+ else
246
+ team_info "reap:没有本项目孤儿席位进程(计划为空;一个信号都不会发)"
247
+ fi
248
+ return 0
249
+ fi
250
+
251
+ if [ "$yes" != 1 ]; then
252
+ printf 'reap 计划(%s 个目标;**未授权**——一个信号都不会发;加 --yes 执行):\n' "$n"
253
+ for i in "${!tpids[@]}"; do
254
+ printf ' %s 席位=%s 内存=%s 启动=%s → 先 SIGTERM,%ss 后仍活着再 SIGKILL\n' \
255
+ "${tpids[$i]}" "${tseats[$i]}" "$(team_orphan_rss_text "${tmems[$i]}")" \
256
+ "$(team_orphan_ts "${tstarts[$i]}")" "$grace"
257
+ done
258
+ printf ' 审计:%s(执行时每个信号一行)\n' "$(team_reap_log_path)"
259
+ return 0
260
+ fi
261
+
262
+ # ── 执行态:先开审计日志(授权之后才写盘),然后逐个「复验 → 发信号」 ──
263
+ team_reap_log_open || return 1
264
+ printf 'reap 执行(--yes;%s 个目标;每个目标发信号前**再验一遍**):\n' "$n"
265
+ local rc=0 seat2
266
+ for i in "${!tpids[@]}"; do
267
+ if ! team_reap_verify "${tpids[$i]}" "${tstarts[$i]}"; then
268
+ team_err "reap: pid ${tpids[$i]} 复验不过,拒发:$REAP_REASON"
269
+ rc=1
270
+ continue
271
+ fi
272
+ seat2="${REAP_SEAT:-${tseats[$i]}}"
273
+ team_reap_signal "${tpids[$i]}" "$seat2" || rc=1
274
+ done
275
+ [ "$rc" = 0 ] || team_err "reap: 有目标没有收干净(见上面的逐条结论)"
276
+ return "$rc"
277
+ }
@@ -13,13 +13,13 @@ team_reply_usage() {
13
13
  cat <<'EOF'
14
14
  用法:
15
15
  team reply post --from <label> --title <title> --stdin
16
- 把 stdin 的原文发布为一份关键回复。--from 是自称的平坦标识符(1–48 个 [A-Za-z0-9._-]),
17
- 不是身份认证;--title 单行、最多 160 个码点、不含终端控制/双向控制;正文 1–131072 字节的合法
16
+ 把 stdin 的原文发布为一份关键回复。--from 是调用方填的标签(1–48 个 [A-Za-z0-9._-]);
17
+ 工具不核对它是谁;--title 单行、最多 160 个码点、不含终端控制/双向控制;正文 1–131072 字节的合法
18
18
  UTF-8、不含 NUL。成功只打印 body-free 回执与编号;保存失败不阻止你照常发送答复。
19
19
  退出码:0 已持久化 / 2 输入 / 4 存储不可用 / 5 写锁竞争(≤2s) / 6 已可见但持久性未确认。
20
20
  team reply list [--before <no>] [--json]
21
21
  最新在前,每页最多 50 个编号槽位;`--before` 是排他游标(正整数)。文本模式每行
22
- `#no <时间> <自称发布者> <标题>`,坏槽位显示为不可用行。空存储返回 0。
22
+ `#no <时间> <发布者标签> <标题>`,坏槽位显示为不可用行。空存储返回 0。
23
23
  team reply show <no> [--json|--raw]
24
24
  默认打印安全显示文本(终端控制已剥除,正文以原文保存);`--json` 给机读结构
25
25
  (display_text/bytes/sha256,标明是显示表示);`--raw` 只在非 TTY stdout 上导出**原始字节**,