teamsmith 0.0.0-stage → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +158 -2
  3. package/bin/team.mjs +59 -0
  4. package/install.sh +94 -0
  5. package/package.json +37 -4
  6. package/skills/teamsmith/CHANGELOG.md +763 -0
  7. package/skills/teamsmith/SKILL.md +312 -0
  8. package/skills/teamsmith/extension/team-bg.ts +435 -0
  9. package/skills/teamsmith/extension/team-inbox-watch.ts +1412 -0
  10. package/skills/teamsmith/extension/team-notify.ts +663 -0
  11. package/skills/teamsmith/references/agent-adapters.md +643 -0
  12. package/skills/teamsmith/references/config.md +477 -0
  13. package/skills/teamsmith/references/meeting.md +124 -0
  14. package/skills/teamsmith/references/memory.md +133 -0
  15. package/skills/teamsmith/references/migration.md +227 -0
  16. package/skills/teamsmith/references/openspec.md +222 -0
  17. package/skills/teamsmith/references/philosophy.md +95 -0
  18. package/skills/teamsmith/references/protocol.md +712 -0
  19. package/skills/teamsmith/references/troubleshooting.md +1384 -0
  20. package/skills/teamsmith/references/workflows.md +448 -0
  21. package/skills/teamsmith/scripts/lib/cmd-agents.sh +1709 -0
  22. package/skills/teamsmith/scripts/lib/cmd-bg.sh +284 -0
  23. package/skills/teamsmith/scripts/lib/cmd-bootstrap.sh +311 -0
  24. package/skills/teamsmith/scripts/lib/cmd-config.sh +1156 -0
  25. package/skills/teamsmith/scripts/lib/cmd-death.sh +562 -0
  26. package/skills/teamsmith/scripts/lib/cmd-docs.sh +149 -0
  27. package/skills/teamsmith/scripts/lib/cmd-draft.sh +124 -0
  28. package/skills/teamsmith/scripts/lib/cmd-init.sh +212 -0
  29. package/skills/teamsmith/scripts/lib/cmd-meeting.sh +904 -0
  30. package/skills/teamsmith/scripts/lib/cmd-outbox.sh +196 -0
  31. package/skills/teamsmith/scripts/lib/cmd-project.sh +642 -0
  32. package/skills/teamsmith/scripts/lib/cmd-review.sh +1393 -0
  33. package/skills/teamsmith/scripts/lib/cmd-status.sh +1399 -0
  34. package/skills/teamsmith/scripts/lib/cmd-update.sh +191 -0
  35. package/skills/teamsmith/scripts/lib/cmd-watch.sh +1681 -0
  36. package/skills/teamsmith/scripts/lib/common.sh +4964 -0
  37. package/skills/teamsmith/scripts/lib/draft-entry.sh +51 -0
  38. package/skills/teamsmith/scripts/lib/outbox.sh +2042 -0
  39. package/skills/teamsmith/scripts/lib/tmux-argv.sh +165 -0
  40. package/skills/teamsmith/scripts/monitor.mjs +616 -0
  41. package/skills/teamsmith/scripts/panel/README.md +192 -0
  42. package/skills/teamsmith/scripts/panel/build.sh +47 -0
  43. package/skills/teamsmith/scripts/panel/bun.lock +109 -0
  44. package/skills/teamsmith/scripts/panel/draft-send.sh +68 -0
  45. package/skills/teamsmith/scripts/panel/package.json +24 -0
  46. package/skills/teamsmith/scripts/panel/panel.js +388 -0
  47. package/skills/teamsmith/scripts/panel/src/App.tsx +2150 -0
  48. package/skills/teamsmith/scripts/panel/src/clipboard.ts +154 -0
  49. package/skills/teamsmith/scripts/panel/src/compose.ts +691 -0
  50. package/skills/teamsmith/scripts/panel/src/data.ts +555 -0
  51. package/skills/teamsmith/scripts/panel/src/format.ts +50 -0
  52. package/skills/teamsmith/scripts/panel/src/layout.ts +2562 -0
  53. package/skills/teamsmith/scripts/panel/src/main.tsx +894 -0
  54. package/skills/teamsmith/scripts/panel/src/markdown.ts +340 -0
  55. package/skills/teamsmith/scripts/panel/src/sanitize.ts +42 -0
  56. package/skills/teamsmith/scripts/panel/src/settings.ts +154 -0
  57. package/skills/teamsmith/scripts/panel/src/strings/en.ts +416 -0
  58. package/skills/teamsmith/scripts/panel/src/strings/index.ts +65 -0
  59. package/skills/teamsmith/scripts/panel/src/strings/types.ts +9 -0
  60. package/skills/teamsmith/scripts/panel/src/strings/zh.ts +414 -0
  61. package/skills/teamsmith/scripts/panel/src/theme.ts +116 -0
  62. package/skills/teamsmith/scripts/panel/src/types.ts +652 -0
  63. package/skills/teamsmith/scripts/panel/src/width.ts +105 -0
  64. package/skills/teamsmith/scripts/panel/tsconfig.json +16 -0
  65. package/skills/teamsmith/scripts/shim/signal-gate +156 -0
  66. package/skills/teamsmith/scripts/shim/tmux +403 -0
  67. package/skills/teamsmith/scripts/team +143 -0
  68. package/skills/teamsmith/templates/AGENTS.section.md.tmpl +147 -0
  69. package/skills/teamsmith/templates/BOARD.md.tmpl +18 -0
  70. package/skills/teamsmith/templates/DECISIONS.md.tmpl +22 -0
  71. package/skills/teamsmith/templates/OWNERSHIP.md.tmpl +25 -0
  72. package/skills/teamsmith/templates/PROTOCOL.md.tmpl +84 -0
  73. package/skills/teamsmith/templates/ROADMAP.md.tmpl +29 -0
  74. package/skills/teamsmith/templates/config.sh.tmpl +106 -0
  75. package/skills/teamsmith/templates/memory-seed.md.tmpl +46 -0
  76. package/skills/teamsmith/templates/pm-prompt.md.tmpl +110 -0
  77. package/skills/teamsmith/templates/report.md.tmpl +36 -0
  78. package/skills/teamsmith/templates/task.md.tmpl +65 -0
  79. package/skills/teamsmith/templates/threads-README.md +17 -0
  80. package/skills/teamsmith/tests/bg-ledger-harness.mjs +682 -0
  81. package/skills/teamsmith/tests/bg-ledger.sh +307 -0
  82. package/skills/teamsmith/tests/checkout-shape-probe.sh +800 -0
  83. package/skills/teamsmith/tests/config-cli.sh +1562 -0
  84. package/skills/teamsmith/tests/container-tmux.sh +493 -0
  85. package/skills/teamsmith/tests/coverage-inventory.sh +173 -0
  86. package/skills/teamsmith/tests/death-cause.sh +564 -0
  87. package/skills/teamsmith/tests/delivery-truth.sh +1231 -0
  88. package/skills/teamsmith/tests/fake-tui.py +450 -0
  89. package/skills/teamsmith/tests/fixtures/delivery-truth-real/README.md +60 -0
  90. package/skills/teamsmith/tests/fixtures/delivery-truth-real/judge-second.py +139 -0
  91. package/skills/teamsmith/tests/fixtures/delivery-truth-real/mock-server.py +29 -0
  92. package/skills/teamsmith/tests/fixtures/delivery-truth-real/observe.ts +20 -0
  93. package/skills/teamsmith/tests/fixtures/delivery-truth-real/run-case.sh +79 -0
  94. package/skills/teamsmith/tests/fixtures/delivery-truth-real/scenario.sh +175 -0
  95. package/skills/teamsmith/tests/fixtures/p55/flip-p49.sh +229 -0
  96. package/skills/teamsmith/tests/fixtures/p71-incident/README.md +22 -0
  97. package/skills/teamsmith/tests/fixtures/p71-incident/dev2.seen +8 -0
  98. package/skills/teamsmith/tests/fixtures/p71-incident/dev2.wake +9 -0
  99. package/skills/teamsmith/tests/fixtures/p71-incident/pm-nudges.wake +3 -0
  100. package/skills/teamsmith/tests/fixtures/signal-lint-cases.txt +40 -0
  101. package/skills/teamsmith/tests/flip-glue.sh +162 -0
  102. package/skills/teamsmith/tests/flip-m12-26c.sh +129 -0
  103. package/skills/teamsmith/tests/flip-m12.sh +185 -0
  104. package/skills/teamsmith/tests/flip-m16.sh +166 -0
  105. package/skills/teamsmith/tests/flip-m17.sh +204 -0
  106. package/skills/teamsmith/tests/flip-m33.sh +284 -0
  107. package/skills/teamsmith/tests/flip-m34.sh +96 -0
  108. package/skills/teamsmith/tests/flip-m37.sh +185 -0
  109. package/skills/teamsmith/tests/flip-m4.3.sh +375 -0
  110. package/skills/teamsmith/tests/flip-m43.sh +128 -0
  111. package/skills/teamsmith/tests/flip-m44.sh +262 -0
  112. package/skills/teamsmith/tests/flip-m45.sh +244 -0
  113. package/skills/teamsmith/tests/flip-m46.sh +238 -0
  114. package/skills/teamsmith/tests/flip-m48.sh +304 -0
  115. package/skills/teamsmith/tests/flip-m6.2.sh +205 -0
  116. package/skills/teamsmith/tests/flip-m6.3.sh +283 -0
  117. package/skills/teamsmith/tests/flip-m6.4.sh +267 -0
  118. package/skills/teamsmith/tests/flip-m6.5.sh +113 -0
  119. package/skills/teamsmith/tests/flip-m7.1.sh +135 -0
  120. package/skills/teamsmith/tests/flip-m7.2.sh +321 -0
  121. package/skills/teamsmith/tests/flip-p144.sh +112 -0
  122. package/skills/teamsmith/tests/flip-p156.sh +133 -0
  123. package/skills/teamsmith/tests/flip-p158.sh +301 -0
  124. package/skills/teamsmith/tests/flip-p160.sh +182 -0
  125. package/skills/teamsmith/tests/flip-p171.sh +249 -0
  126. package/skills/teamsmith/tests/flip-p18.1.sh +95 -0
  127. package/skills/teamsmith/tests/flip-p20.sh +286 -0
  128. package/skills/teamsmith/tests/flip-p201.sh +249 -0
  129. package/skills/teamsmith/tests/flip-p210.sh +239 -0
  130. package/skills/teamsmith/tests/flip-p211.sh +152 -0
  131. package/skills/teamsmith/tests/flip-p217.sh +288 -0
  132. package/skills/teamsmith/tests/flip-p22.sh +123 -0
  133. package/skills/teamsmith/tests/flip-p23.sh +239 -0
  134. package/skills/teamsmith/tests/flip-p25.sh +165 -0
  135. package/skills/teamsmith/tests/flip-p71.sh +184 -0
  136. package/skills/teamsmith/tests/flip-p72.sh +202 -0
  137. package/skills/teamsmith/tests/frames/README.md +176 -0
  138. package/skills/teamsmith/tests/frames/p78-conversation-rule-below-box.txt +6 -0
  139. package/skills/teamsmith/tests/frames/p78-cursor-mid-draft.txt +6 -0
  140. package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor-line.txt +6 -0
  141. package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor.txt +6 -0
  142. package/skills/teamsmith/tests/frames/p78-draft-rule-blank-region.txt +6 -0
  143. package/skills/teamsmith/tests/frames/p78-draft-rule-only.txt +5 -0
  144. package/skills/teamsmith/tests/frames/p78-spinner-row-below-cursor.txt +6 -0
  145. package/skills/teamsmith/tests/frames/p78-wider-rule-below-cursor.txt +6 -0
  146. package/skills/teamsmith/tests/frames/p86-f1-mixed-width-disjoint-box.txt +8 -0
  147. package/skills/teamsmith/tests/frames/p86-f1-narrower-width-disjoint-box.txt +8 -0
  148. package/skills/teamsmith/tests/frames/p86-f1-spinner-top-disjoint-box.txt +8 -0
  149. package/skills/teamsmith/tests/frames/pi-0.85.1-update-banner.txt +30 -0
  150. package/skills/teamsmith/tests/frames/pi-0.87.0-draft-half-sentence.txt +30 -0
  151. package/skills/teamsmith/tests/frames/pi-0.87.0-empty-box.txt +30 -0
  152. package/skills/teamsmith/tests/frames/pi-0.87.0-one-line-draft.txt +30 -0
  153. package/skills/teamsmith/tests/frames/pi-0.87.0-project-trust-prompt.txt +30 -0
  154. package/skills/teamsmith/tests/frames/pi-0.99.2-empty-editor.txt +32 -0
  155. package/skills/teamsmith/tests/frames/pi-0.99.2-human-draft.txt +32 -0
  156. package/skills/teamsmith/tests/frames/pi-1.0.0-git-error-in-box.txt +32 -0
  157. package/skills/teamsmith/tests/gate-guard.sh +112 -0
  158. package/skills/teamsmith/tests/guard-matrix.sh +233 -0
  159. package/skills/teamsmith/tests/install-shape.sh +731 -0
  160. package/skills/teamsmith/tests/ledger-ban.sh +612 -0
  161. package/skills/teamsmith/tests/lib/box-judge.sh +68 -0
  162. package/skills/teamsmith/tests/lib/checkout-shape.sh +129 -0
  163. package/skills/teamsmith/tests/lib/loop-scan.awk +114 -0
  164. package/skills/teamsmith/tests/lib/pty-wait.sh +676 -0
  165. package/skills/teamsmith/tests/lib/section-guard.sh +855 -0
  166. package/skills/teamsmith/tests/lib/shell-lex.pl +261 -0
  167. package/skills/teamsmith/tests/lib/tmp-root.sh +528 -0
  168. package/skills/teamsmith/tests/lib/tmux-cap.sh +35 -0
  169. package/skills/teamsmith/tests/lib/tmux-iso-call-probe.sh +81 -0
  170. package/skills/teamsmith/tests/lib/tmux-iso-probe.sh +46 -0
  171. package/skills/teamsmith/tests/lib/tmux-iso.sh +381 -0
  172. package/skills/teamsmith/tests/load-experiment.sh +290 -0
  173. package/skills/teamsmith/tests/loop-inventory.tsv +84 -0
  174. package/skills/teamsmith/tests/panel-b2-slow-stub.sh +19 -0
  175. package/skills/teamsmith/tests/panel-b2.sh +1183 -0
  176. package/skills/teamsmith/tests/panel-b3-pty-mouse.py +240 -0
  177. package/skills/teamsmith/tests/panel-b3-pty-tmux-mouse.py +138 -0
  178. package/skills/teamsmith/tests/panel-b3-stub.sh +279 -0
  179. package/skills/teamsmith/tests/panel-b3.sh +1301 -0
  180. package/skills/teamsmith/tests/panel-choices.sh +572 -0
  181. package/skills/teamsmith/tests/panel-clipboard-model.ts +187 -0
  182. package/skills/teamsmith/tests/panel-compose-model.ts +290 -0
  183. package/skills/teamsmith/tests/panel-contrast.mjs +81 -0
  184. package/skills/teamsmith/tests/panel-cpu-premise.sh +173 -0
  185. package/skills/teamsmith/tests/panel-cpu.sh +391 -0
  186. package/skills/teamsmith/tests/panel-flip-m54.sh +207 -0
  187. package/skills/teamsmith/tests/panel-flip-p123.sh +278 -0
  188. package/skills/teamsmith/tests/panel-flip-p125.sh +318 -0
  189. package/skills/teamsmith/tests/panel-flip-p128.sh +259 -0
  190. package/skills/teamsmith/tests/panel-keyprobe-pty.py +99 -0
  191. package/skills/teamsmith/tests/panel-keyprobe-stub.sh +41 -0
  192. package/skills/teamsmith/tests/panel-keyprobe.sh +82 -0
  193. package/skills/teamsmith/tests/panel-keyprobe.tsx +116 -0
  194. package/skills/teamsmith/tests/panel-knobs.sh +83 -0
  195. package/skills/teamsmith/tests/panel-p21.sh +2181 -0
  196. package/skills/teamsmith/tests/panel-snapshots.sh +802 -0
  197. package/skills/teamsmith/tests/panel-strings.mjs +195 -0
  198. package/skills/teamsmith/tests/perf.sh +740 -0
  199. package/skills/teamsmith/tests/pm-box-real.sh +350 -0
  200. package/skills/teamsmith/tests/pulse-nudge-key.sh +563 -0
  201. package/skills/teamsmith/tests/routes.sh +1652 -0
  202. package/skills/teamsmith/tests/section-budgets.tsv +150 -0
  203. package/skills/teamsmith/tests/section-guard.sh +231 -0
  204. package/skills/teamsmith/tests/section-needs-audit.sh +94 -0
  205. package/skills/teamsmith/tests/section-paths.tsv +142 -0
  206. package/skills/teamsmith/tests/section-select.sh +655 -0
  207. package/skills/teamsmith/tests/signal-gate.sh +366 -0
  208. package/skills/teamsmith/tests/signal-lint-legacy.txt +14 -0
  209. package/skills/teamsmith/tests/signal-lint.pl +416 -0
  210. package/skills/teamsmith/tests/skill-load.mjs +65 -0
  211. package/skills/teamsmith/tests/smoke.sh +19711 -0
  212. package/skills/teamsmith/tests/snapshots/zh-dark-120-detail.txt +32 -0
  213. package/skills/teamsmith/tests/snapshots/zh-dark-120-p4.txt +32 -0
  214. package/skills/teamsmith/tests/snapshots/zh-dark-120.txt +32 -0
  215. package/skills/teamsmith/tests/snapshots/zh-dark-160-p4.txt +32 -0
  216. package/skills/teamsmith/tests/snapshots/zh-dark-160.txt +32 -0
  217. package/skills/teamsmith/tests/snapshots/zh-dark-59-detail.txt +32 -0
  218. package/skills/teamsmith/tests/snapshots/zh-dark-59-p4.txt +32 -0
  219. package/skills/teamsmith/tests/snapshots/zh-dark-59.txt +32 -0
  220. package/skills/teamsmith/tests/snapshots/zh-dark-99-p4.txt +32 -0
  221. package/skills/teamsmith/tests/snapshots/zh-dark-99.txt +32 -0
  222. package/skills/teamsmith/tests/snapshots/zh-light-120-detail.txt +32 -0
  223. package/skills/teamsmith/tests/snapshots/zh-light-120-p4.txt +32 -0
  224. package/skills/teamsmith/tests/snapshots/zh-light-120.txt +32 -0
  225. package/skills/teamsmith/tests/snapshots/zh-light-160-p4.txt +32 -0
  226. package/skills/teamsmith/tests/snapshots/zh-light-160.txt +32 -0
  227. package/skills/teamsmith/tests/snapshots/zh-light-59-detail.txt +32 -0
  228. package/skills/teamsmith/tests/snapshots/zh-light-59-p4.txt +32 -0
  229. package/skills/teamsmith/tests/snapshots/zh-light-59.txt +32 -0
  230. package/skills/teamsmith/tests/snapshots/zh-light-99-p4.txt +32 -0
  231. package/skills/teamsmith/tests/snapshots/zh-light-99.txt +32 -0
  232. package/skills/teamsmith/tests/spec-ledger-refs.tsv +78 -0
  233. package/skills/teamsmith/tests/spec-refs.sh +826 -0
  234. package/skills/teamsmith/tests/task-header-model.sh +267 -0
  235. package/skills/teamsmith/tests/team-bg-flip.sh +239 -0
  236. package/skills/teamsmith/tests/team-bg-harness.mjs +392 -0
  237. package/skills/teamsmith/tests/team-bg-stop.sh +434 -0
  238. package/skills/teamsmith/tests/team-inbox-watch-flip.sh +191 -0
  239. package/skills/teamsmith/tests/team-inbox-watch-harness.mjs +1559 -0
  240. package/skills/teamsmith/tests/tmp-hygiene.sh +1190 -0
  241. package/skills/teamsmith/tests/tmux-lint-legacy.txt +31 -0
  242. package/skills/teamsmith/tests/tmux-lint.pl +604 -0
  243. package/skills/teamsmith-init/SKILL.md +100 -0
  244. package/skills/teamsmith-init/references/bootstrap.md +74 -0
  245. package/skills/teamsmith-init/templates/bootstrap-prompt.md.tmpl +46 -0
@@ -0,0 +1,562 @@
1
+ #!/usr/bin/env bash
2
+ # teamsmith · 席位死因(change: agent-death-reason · apply)
3
+ #
4
+ # 一个只读读取器回答「这个座位的**当前这次启动**为什么死了」,闭集:
5
+ # quota | balance | rate_limit | window | auth | normal | unknown
6
+ # 硬约束(design D1–D9,逐条对应 openspec/changes/agent-death-reason/design.md):
7
+ # · 只读:读取路径不写 .pi/team/state/ 的任何文件(内容与 mtime 都不动;指纹由夹具钉住);
8
+ # · 缺证据 = unknown,绝不编造;没有「错误形状」的散文不算分类;
9
+ # · 只认当前这次启动的证据(started / 启动 nonce 守卫),重启不继承旧因;
10
+ # · 判定只读有界尾部(TEAM_DEATH_SCAN_LINES,默认 40;会话尾 64 KiB);
11
+ # · 唯一的新写入者是巡检(state/deaths.log + knock,见 team_watch_deaths_step);
12
+ # · 本文件不碰 M6.5 的存活判据(running/dead/foreign/unknown 的规则一字不改)。
13
+ #
14
+ # 分类事实与红/绿证据:docs/team/reports/P113-dev3.md;夹具:tests/death-cause.sh。
15
+
16
+ # ================================================================ 分类核心(纯逻辑)
17
+
18
+ team_death_scan_lines() { # → 有界尾行数(TEAM_DEATH_SCAN_LINES;默认 40,非数字 → 40)
19
+ local n="${TEAM_DEATH_SCAN_LINES:-40}"
20
+ case "$n" in ''|*[!0-9]*) n=40 ;; esac
21
+ [ "$n" -ge 1 ] || n=40
22
+ printf '%s\n' "$n"
23
+ }
24
+
25
+ team_death_sanitize_line() { # <行> → 单行、去控制字符、≤300 字符(截断加 …)
26
+ local v="${1:-}"
27
+ v="$(printf '%s' "$v" | LC_ALL=C tr -d '\000-\010\013-\037\177' | tr '\t' ' ')"
28
+ if [ "${#v}" -gt 300 ]; then
29
+ printf '%s…\n' "${v:0:300}"
30
+ else
31
+ printf '%s\n' "$v"
32
+ fi
33
+ }
34
+
35
+ # 形状(frame)+ 措辞。两者都要:只有形状不足以分类,只有词(散文)也不算 —— design D3。
36
+ # 形状(大小写不敏感):① 行里出现 `Error:`/`error:`;② HTTP 状态(401|402|403|429|5xx)挨着
37
+ # 供应商错误码(*_error);③ JSON 错误对象({"error… / "error": … "message");④ 供应商 *_error;
38
+ # ⑤ window 额外接受 Pi 自己的无帧措辞(Context full / context overflow / context length exceeded)。
39
+ # 命中形状后按固定优先级取**一个**分类:balance > quota > rate_limit > window > auth。
40
+ team_death_classify_line() { # <行> → 分类 | 空
41
+ local line="${1:-}" l
42
+ [ -n "$line" ] || return 0
43
+ l="$(printf '%s' "$line" | LC_ALL=C tr 'A-Z' 'a-z')"
44
+ local framed=0
45
+ case "$l" in
46
+ error:*|*"error:"*) framed=1 ;;
47
+ esac
48
+ case "$l" in
49
+ *'{"error'*|*'"error"'*message*|*'"message"'*'"error"'*) framed=1 ;;
50
+ esac
51
+ case "$l" in
52
+ *_error*) framed=1 ;;
53
+ esac
54
+ case "$l" in
55
+ *401*|*402*|*403*|*429*|*5[0-9][0-9]*)
56
+ case "$l" in *_error*) framed=1 ;; esac ;;
57
+ esac
58
+ case "$l" in
59
+ *"context full"*|*"context overflow"*|*"context length exceeded"*) framed=1 ;;
60
+ esac
61
+ [ "$framed" = "1" ] || return 0
62
+ case "$l" in
63
+ *"insufficient balance"*|*"insufficient_balance"*|*"balance insufficient"*|*"余额不足"*|*"欠费"*|*"payment required"*)
64
+ printf 'balance\n'; return 0 ;;
65
+ *"usage limit"*|*"quota"*|*"insufficient_quota"*|*"exceeded your current quota"*|*"额度"*|*"用量上限"*|*"weekly limit"*|*"5-hour limit"*)
66
+ printf 'quota\n'; return 0 ;;
67
+ *"rate limit"*|*"rate_limit"*|*"too many requests"*|*429*)
68
+ printf 'rate_limit\n'; return 0 ;;
69
+ *"context window"*|*"context length"*|*"context full"*|*"too many tokens"*|*"maximum context"*)
70
+ printf 'window\n'; return 0 ;;
71
+ *401*|*"unauthorized"*|*"authentication"*|*"invalid api key"*|*"permission_error"*|*"forbidden"*|*"permission denied"*)
72
+ printf 'auth\n'; return 0 ;;
73
+ esac
74
+ return 0
75
+ }
76
+
77
+ team_death_scene_trim() { # stdin 场景文本 → 去尾空行后最后 N 行(N=TEAM_DEATH_SCAN_LINES)
78
+ local n; n="$(team_death_scan_lines)"
79
+ awk -v n="$n" '
80
+ { line[NR] = $0 }
81
+ NF { last = NR }
82
+ END {
83
+ if (n < 1 || last == 0) exit 0
84
+ start = last - n + 1; if (start < 1) start = 1
85
+ for (i = start; i <= last; i++) print line[i]
86
+ }
87
+ '
88
+ }
89
+
90
+ # 一条文本(现场 / 尾屏)→ 全局 `TD_SCAN_CAT` / `TD_SCAN_RAW` / `TD_SCAN_LAST`。同一段里
91
+ # **最后一条**命中分类的行赢(前面可能有真实报错、后面可能有无关错误;design D3)。
92
+ team_death_scan_text() { # <文本> → 见上(不经 $(…):全局在当前 shell 里读,空字段不会被 IFS 吞掉)
93
+ local text="${1:-}" line cat
94
+ TD_SCAN_CAT=""; TD_SCAN_RAW=""; TD_SCAN_LAST=""
95
+ while IFS= read -r line; do
96
+ line="$(team_death_sanitize_line "$line")"
97
+ [ -n "$line" ] || continue
98
+ TD_SCAN_LAST="$line"
99
+ cat="$(team_death_classify_line "$line")"
100
+ if [ -n "$cat" ]; then TD_SCAN_CAT="$cat"; TD_SCAN_RAW="$line"; fi
101
+ done < <(printf '%s\n' "$text" | sed -e 's/[[:space:]]*$//' -e '/^Pane is dead (/d' | team_death_scene_trim)
102
+ }
103
+
104
+ # ================================================================ 工具
105
+
106
+ team_death_epoch() { # <时间文本> → epoch | 空(解析不了)
107
+ local s="${1:-}" e
108
+ [ -n "$s" ] || return 0
109
+ e="$(date -d "$s" +%s 2>/dev/null || true)"
110
+ case "$e" in ''|*[!0-9]*) return 0 ;; esac
111
+ printf '%s\n' "$e"
112
+ }
113
+
114
+ team_death_epoch_current() { # <候选 epoch> <started epoch> → 0=属于当前启动
115
+ local t="${1:-}" s="${2:-}"
116
+ case "$t" in ''|*[!0-9]*) return 1 ;; esac
117
+ case "$s" in ''|*[!0-9]*) return 0 ;; esac
118
+ [ "$t" -ge "$s" ]
119
+ }
120
+
121
+ team_death_time_current() { # <时间文本> <started epoch> → 0=属于当前启动(ISO 文本回退到字典序)
122
+ local txt="${1:-}" s_epoch="${2:-}" e
123
+ [ -n "$txt" ] && [ "$txt" != "-" ] || return 1
124
+ e="$(team_death_epoch "$txt")"
125
+ if [ -n "$e" ]; then team_death_epoch_current "$e" "$s_epoch"; return $?; fi
126
+ case "$s_epoch" in ''|*[!0-9]*) return 0 ;; esac
127
+ case "$txt" in
128
+ [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T*) return 1 ;;
129
+ esac
130
+ return 0
131
+ }
132
+
133
+ team_death_file_mtime() { # <文件> → mtime epoch | 空
134
+ local f="${1:-}"
135
+ [ -f "$f" ] || return 0
136
+ stat -c %Y "$f" 2>/dev/null || stat -f %m "$f" 2>/dev/null || true
137
+ }
138
+
139
+ team_death_identity() { # <seat> <分类> <锚> → 身份哈希
140
+ team_hash "${1:-}|${2:-}|${3:-}"
141
+ }
142
+
143
+ # ================================================================ 会话侧读取器
144
+
145
+ # 该席位工作树里**最新**的 pi 会话 JSONL。`team_session_file` 只认 `*_<sid>.jsonl`,漏掉
146
+ # `--fresh` 的 `*_<sid>-<epoch>.jsonl`;这里补上(design D2/D9:不复用共享函数,免得静默改变
147
+ # roster/ps 的会话大小语义)。
148
+ team_agent_session_latest() { # <agent> → 文件路径 | 非 0
149
+ local a="${1:-}" wt d sid f best="" bestm=-1 m
150
+ [ -n "$a" ] || return 1
151
+ wt="$(team_agent_worktree "$a")"
152
+ [ -d "$wt" ] || return 1
153
+ d="$(team_pi_session_dir "$wt")"
154
+ [ -d "$d" ] || return 1
155
+ sid="$TEAM_SESSION-$a"
156
+ for f in "$d"/*_"$sid".jsonl "$d"/*_"$sid"-*.jsonl; do
157
+ [ -f "$f" ] || continue
158
+ m="$(team_death_file_mtime "$f")"
159
+ case "$m" in ''|*[!0-9]*) continue ;; esac
160
+ if [ "$m" -gt "$bestm" ]; then best="$f"; bestm="$m"; fi
161
+ done
162
+ [ -n "$best" ] || return 1
163
+ printf '%s\n' "$best"
164
+ }
165
+
166
+ # 有界尾部(默认 64 KiB)里**最后**一条 assistant + stopReason=error 的 errorMessage。
167
+ # 层级/类型不对、或用 JSON 解析器才能取出的(消息里有转义引号)→ 空:绝不猜一个残缺的行出来。
168
+ team_session_error_line() { # <文件> → "原文⇥时间" | 非 0
169
+ local f="${1:-}" chunk line last_msg="" last_ts="" msg ts
170
+ [ -f "$f" ] || return 1
171
+ chunk="$(tail -c "${TEAM_DEATH_SESSION_TAIL:-65536}" "$f" 2>/dev/null || true)"
172
+ [ -n "$chunk" ] || return 1
173
+ while IFS= read -r line; do
174
+ case "$line" in *'"stopReason":"error"'*) ;; *) continue ;; esac
175
+ case "$line" in *'"role":"assistant"'*) ;; *) continue ;; esac
176
+ msg="$(printf '%s' "$line" | sed -n 's/.*"errorMessage":"\([^"]*\)".*/\1/p')"
177
+ [ -n "$msg" ] || continue
178
+ ts="$(printf '%s' "$line" | grep -o '"timestamp":"[^"]*"' 2>/dev/null | head -1 | sed -e 's/^"timestamp":"//' -e 's/"$//' || true)"
179
+ last_msg="$msg"; last_ts="${ts:--}"
180
+ done < <(printf '%s\n' "$chunk")
181
+ [ -n "$last_msg" ] || return 1
182
+ printf '%s\t%s\n' "$last_msg" "$last_ts"
183
+ }
184
+
185
+ # ================================================================ 唯一读取器
186
+
187
+ # 异常死亡的证据(全局:TEAM_DEATH_*;调用方在**当前 shell** 读,不经 $(…))。
188
+ # 返回 0 = 这是一次异常死亡(分类已填;source=none 表示没有任何可读来源);
189
+ # 返回 1 = 不是异常死亡(运行中 / 无登记任务 / 干净退出)或根本判不出死亡。
190
+ # TEAM_DEATH_CAT 分类(闭集;异常时至少是 unknown)
191
+ # TEAM_DEATH_SOURCE pane | session | recorded | none
192
+ # TEAM_DEATH_TIME 记录时间(人读文本;未知 → 空)
193
+ # TEAM_DEATH_RAW 原始证据行(可能为空)
194
+ # TEAM_DEATH_ANCHOR 身份锚(nonce:<n> | pane:<epoch> | pid:<id> | line:<hash> | 记录的锚)
195
+ # TEAM_DEATH_READABLE 1 = 至少读到一个来源(决定巡检是否 knock)
196
+ team_death_evidence() { # <agent> → 见上
197
+ TEAM_DEATH_CAT=""; TEAM_DEATH_SOURCE=""; TEAM_DEATH_TIME=""; TEAM_DEATH_RAW=""
198
+ TEAM_DEATH_ANCHOR=""; TEAM_DEATH_READABLE="0"
199
+ local a="${1:-}" task w started s_epoch nonce anchor_nonce
200
+ [ -n "$a" ] || return 1
201
+ task="$(team_state_get "$a" task '')"
202
+ [ -n "$task" ] || return 1
203
+ team_agent_live "$a" && return 1
204
+ started="$(team_state_get "$a" started '')"
205
+ s_epoch="$(team_death_epoch "$started")"
206
+ nonce=""
207
+ if [ -r "$(team_dispatch_spawn_file "$a")" ]; then
208
+ nonce="$(head -1 "$(team_dispatch_spawn_file "$a")" 2>/dev/null | awk '{print $1}' || true)"
209
+ fi
210
+ anchor_nonce=""; [ -n "$nonce" ] && anchor_nonce="nonce:$nonce"
211
+
212
+ # ---- pane 侧(按现有现场读取器的**同一** recency 顺序:活遗体 → 留证 → 尾屏 → 退出证据)
213
+ local p_cat="" p_raw="" p_time="" p_anchor="" p_readable=0 p_normal=0 p_unknown_raw=""
214
+ w="$(team_state_get "$a" window "$a")"
215
+ # ① 活遗体:窗口在、pane 死(pane_dead=1 是 tmux census 铁证)
216
+ if [ -n "$w" ] && [ -n "$TEAM_SESSION" ] && team_tmux_has_window "$TEAM_SESSION" "$w"; then
217
+ local f dead status signal dtime
218
+ f="$(team_pane_dead_fields "$TEAM_SESSION:$w" 2>/dev/null || true)"
219
+ if [ "${f%%|*}" = "1" ]; then
220
+ IFS='|' read -r dead status signal dtime <<< "$f"
221
+ local dnum=""
222
+ case "$dtime" in ''|*[!0-9]*) ;; *) dnum="$dtime" ;; esac
223
+ if team_death_epoch_current "$dnum" "$s_epoch"; then
224
+ local scene c raw last pid
225
+ scene="$(tmux capture-pane -p -S - -t "$TEAM_SESSION:$w" 2>/dev/null || true)"
226
+ team_death_scan_text "$scene"
227
+ c="$TD_SCAN_CAT"; raw="$TD_SCAN_RAW"; last="$TD_SCAN_LAST"
228
+ p_time="$(team_pane_dead_time_text "$dtime")"; [ -n "$p_time" ] || p_time="-"
229
+ pid="$(tmux list-panes -t "$TEAM_SESSION:$w" -F '#{pane_id}' 2>/dev/null | head -1 || true)"
230
+ p_anchor="$anchor_nonce"
231
+ if [ -z "$p_anchor" ]; then
232
+ if [ -n "$dnum" ]; then p_anchor="pane:$dnum"; elif [ -n "$pid" ]; then p_anchor="pid:$pid"; fi
233
+ fi
234
+ if [ -n "$c" ]; then
235
+ p_cat="$c"; p_raw="$raw"; p_readable=1
236
+ elif [ "$status" = "0" ]; then
237
+ p_normal=1
238
+ else
239
+ p_readable=1
240
+ p_unknown_raw="${last:-$(team_pane_evidence_text "$status" "$signal")}"
241
+ [ -n "$p_unknown_raw" ] || p_unknown_raw="pane 已死:没有可读的最后一行"
242
+ fi
243
+ [ -n "$p_anchor" ] || p_anchor="line:$(team_hash "${p_unknown_raw:-${raw:-dead}}")"
244
+ fi
245
+ fi
246
+ fi
247
+ # ② 留证文件:dispatch 替换遗体前抓的现场(重启后旧文件必须被 started 守卫挡住)
248
+ if [ -z "$p_cat" ] && [ "$p_normal" != "1" ]; then
249
+ local cf
250
+ cf="$(team_agent_pane_dead_file "$a")"
251
+ if [ -r "$cf" ]; then
252
+ local ctime c_epoch
253
+ ctime="$(sed -n 's/^dead_time: //p' "$cf" | head -1)"
254
+ [ -n "$ctime" ] || ctime="$(sed -n 's/^captured: //p' "$cf" | head -1)"
255
+ c_epoch="$(team_death_epoch "$ctime")"
256
+ [ -n "$c_epoch" ] || c_epoch="$(team_death_file_mtime "$cf")"
257
+ if team_death_epoch_current "$c_epoch" "$s_epoch"; then
258
+ local scene c raw last
259
+ scene="$(sed -n '/^--- scene ---/,$p' "$cf" | tail -n +2)"
260
+ team_death_scan_text "$scene"
261
+ c="$TD_SCAN_CAT"; raw="$TD_SCAN_RAW"; last="$TD_SCAN_LAST"
262
+ if [ -n "$c" ]; then
263
+ p_cat="$c"; p_raw="$raw"; p_time="${ctime:-}"; p_readable=1
264
+ p_anchor="$anchor_nonce"
265
+ [ -n "$p_anchor" ] || p_anchor="line:$(team_hash "$raw")"
266
+ elif [ -n "$last" ] || [ -n "$scene" ]; then
267
+ p_readable=1
268
+ [ -n "$p_unknown_raw" ] || p_unknown_raw="$last"
269
+ p_time="${ctime:-}"
270
+ fi
271
+ fi
272
+ fi
273
+ fi
274
+ # ③ harness 尾屏:agent 退出那一刻自抓的最后 N 行
275
+ if [ -z "$p_cat" ] && [ "$p_normal" != "1" ]; then
276
+ local tf tm
277
+ tf="$TEAM_STATE_DIR/dispatch-$a-tail.txt"
278
+ if [ -r "$tf" ]; then
279
+ tm="$(team_death_file_mtime "$tf")"
280
+ if team_death_epoch_current "$tm" "$s_epoch"; then
281
+ local scene c raw last
282
+ scene="$(team_status_tail_scene "$tf" "$(team_death_scan_lines)")"
283
+ team_death_scan_text "$scene"
284
+ c="$TD_SCAN_CAT"; raw="$TD_SCAN_RAW"; last="$TD_SCAN_LAST"
285
+ if [ -n "$c" ]; then
286
+ p_cat="$c"; p_raw="$raw"; p_readable=1
287
+ p_time="$(date -d "@$tm" '+%Y-%m-%d %H:%M:%S %Z' 2>/dev/null || printf '%s' '-')"
288
+ p_anchor="$anchor_nonce"
289
+ [ -n "$p_anchor" ] || p_anchor="line:$(team_hash "$raw")"
290
+ elif [ -n "$last" ]; then
291
+ p_readable=1
292
+ [ -n "$p_unknown_raw" ] || p_unknown_raw="$last"
293
+ [ -n "$p_time" ] || p_time="$(date -d "@$tm" '+%Y-%m-%d %H:%M:%S %Z' 2>/dev/null || printf '%s' '-')"
294
+ fi
295
+ fi
296
+ fi
297
+ fi
298
+ # ④ 当前启动的退出证据(nonce 精确匹配):status 0 = **正证据**(干净退出,见 D4);
299
+ # 非零/信号退出在没有框架时才拿自己的原文当兜底 → unknown。
300
+ # 注意:可读但没分类的尾屏/现场**不许**压掉 status 0(现场门禁的 12b-e 就是这样被套出来的:
301
+ # fake agent 把 prompt 原文行的最后一句留在尾屏、进程 exit 0,旧写法把它当成了一次异常死亡)。
302
+ if [ -n "$nonce" ] && [ -r "$(team_dispatch_exit_file "$a")" ]; then
303
+ local en ec
304
+ en="$(head -1 "$(team_dispatch_exit_file "$a")" 2>/dev/null | awk '{print $1}' || true)"
305
+ ec="$(head -1 "$(team_dispatch_exit_file "$a")" 2>/dev/null | awk '{print $2}' || true)"
306
+ if [ -n "$en" ] && [ "$en" = "$nonce" ]; then
307
+ case "$ec" in ''|*[!0-9]*) ec="?" ;; esac
308
+ if [ -z "$p_cat" ]; then
309
+ if [ "$ec" = "0" ]; then
310
+ p_normal=1
311
+ elif [ -z "$p_unknown_raw" ]; then
312
+ p_readable=1; p_unknown_raw="exit status=$ec"
313
+ fi
314
+ [ -n "$p_time" ] || p_time="$(date -d "@$(team_death_file_mtime "$(team_dispatch_exit_file "$a")")" '+%Y-%m-%d %H:%M:%S %Z' 2>/dev/null || printf '%s' '-')"
315
+ fi
316
+ fi
317
+ fi
318
+ [ -n "$p_anchor" ] || p_anchor="$anchor_nonce"
319
+
320
+ # ---- session 侧(只读有界尾;torn/不可解析 → 没有会话证据)
321
+ local s_cat="" s_raw="" s_time="" s_readable=0
322
+ local sf
323
+ sf="$(team_agent_session_latest "$a" 2>/dev/null || true)"
324
+ if [ -n "$sf" ]; then
325
+ local er err raw ts
326
+ er="$(team_session_error_line "$sf" 2>/dev/null || true)"
327
+ if [ -n "$er" ]; then
328
+ IFS=$'\t' read -r raw ts <<< "$er"
329
+ if team_death_time_current "$ts" "$s_epoch"; then
330
+ local c
331
+ c="$(team_death_classify_line "$raw")"
332
+ if [ -n "$c" ]; then s_cat="$c"; else s_cat="unknown"; fi
333
+ s_raw="$(team_death_sanitize_line "$raw")"
334
+ s_time="$ts"
335
+ s_readable=1
336
+ fi
337
+ fi
338
+ fi
339
+
340
+ # ---- 合并(D2):非 unknown 赢 unknown;同类别取更新的记录时间;时间不可比 → session 赢
341
+ local cat="" source="" time="" raw="" anchor=""
342
+ if [ -n "$p_cat" ] && [ "$p_cat" != "unknown" ]; then
343
+ cat="$p_cat"; source="pane"; time="$p_time"; raw="$p_raw"; anchor="$p_anchor"
344
+ if [ -n "$s_cat" ] && [ "$s_cat" != "unknown" ]; then
345
+ if team_death_evidence_newer "$p_time" "$s_time"; then
346
+ cat="$s_cat"; source="session"; time="$s_time"; raw="$s_raw"; anchor="$anchor_nonce"
347
+ [ -n "$anchor" ] || anchor="$p_anchor"
348
+ fi
349
+ fi
350
+ elif [ -n "$s_cat" ] && [ "$s_cat" != "unknown" ]; then
351
+ cat="$s_cat"; source="session"; time="$s_time"; raw="$s_raw"; anchor="$anchor_nonce"
352
+ [ -n "$anchor" ] || anchor="line:$(team_hash "$s_raw")"
353
+ elif [ -n "$p_cat" ]; then # pane 侧 unknown(可读但没有分类)
354
+ cat="unknown"; source="pane"; time="$p_time"; raw="${p_raw:-$p_unknown_raw}"; anchor="$p_anchor"
355
+ if [ "$s_readable" = "1" ]; then
356
+ if team_death_evidence_newer "$p_time" "$s_time"; then
357
+ source="session"; time="$s_time"; raw="$s_raw"; anchor="$anchor_nonce"
358
+ [ -n "$anchor" ] || anchor="$p_anchor"
359
+ fi
360
+ fi
361
+ elif [ "$p_readable" = "1" ]; then # 可读但没分类:unknown + 现场原文
362
+ cat="unknown"; source="pane"; time="$p_time"
363
+ raw="$p_unknown_raw"; anchor="$p_anchor"
364
+ if [ "$s_readable" = "1" ]; then
365
+ if team_death_evidence_newer "$p_time" "$s_time"; then
366
+ source="session"; time="$s_time"; raw="$s_raw"; anchor="$anchor_nonce"
367
+ [ -n "$anchor" ] || anchor="$p_anchor"
368
+ fi
369
+ fi
370
+ elif [ "$s_readable" = "1" ]; then # 只有 session 侧可读且有 errorMessage
371
+ cat="unknown"; source="session"; time="$s_time"; raw="$s_raw"; anchor="$anchor_nonce"
372
+ [ -n "$anchor" ] || anchor="line:$(team_hash "$s_raw")"
373
+ fi
374
+
375
+ # 干净退出是**正证据**(pane_dead_status=0 / 当前启动的 .exit=0):只有在没有任何分类过的因、
376
+ # 且会话侧**没有** error 记录时,这次启动才不是异常死亡。
377
+ # · pane 侧「可读但没有帧」的散文(fake agent 回显的 prompt、装订文本)**不算**死亡证据 ——
378
+ # 现场门禁的 12b-e 就是这样被套出来的(干净退出被报成 unknown 死亡);
379
+ # · 会话侧的 `stopReason=error` 是**显式错误记录**(本身就是一种帧):即使措辞不认识也不许
380
+ # 被 exit 0 抹成 normal —— 否则「供应商换了措辞」这类死亡又会变回用户看不见的原因。
381
+ if [ "$p_normal" = "1" ] && [ -z "$p_cat" ] && [ -z "$s_cat" ]; then
382
+ return 1
383
+ fi
384
+
385
+ # ---- 没有可读来源:unknown(报但不装懂);记录能让现场消失后继续供因
386
+ if [ -z "$cat" ]; then
387
+ if team_death_record_current "$a"; then
388
+ TEAM_DEATH_CAT="$TD_REC_CAT"; TEAM_DEATH_SOURCE="recorded"; TEAM_DEATH_TIME="$TD_REC_TIME"
389
+ TEAM_DEATH_RAW="$TD_REC_RAW"; TEAM_DEATH_ANCHOR="$TD_REC_ANCHOR"; TEAM_DEATH_READABLE="1"
390
+ return 0
391
+ fi
392
+ TEAM_DEATH_CAT="unknown"; TEAM_DEATH_SOURCE="none"
393
+ return 0
394
+ fi
395
+
396
+ # ---- 读到未知但现场已经没了:记录回退优先(source=recorded)
397
+ if [ "$cat" = "unknown" ]; then
398
+ if team_death_record_current "$a"; then
399
+ TEAM_DEATH_CAT="$TD_REC_CAT"; TEAM_DEATH_SOURCE="recorded"; TEAM_DEATH_TIME="$TD_REC_TIME"
400
+ TEAM_DEATH_RAW="$TD_REC_RAW"; TEAM_DEATH_ANCHOR="$TD_REC_ANCHOR"; TEAM_DEATH_READABLE="1"
401
+ return 0
402
+ fi
403
+ fi
404
+
405
+ TEAM_DEATH_CAT="$cat"; TEAM_DEATH_SOURCE="$source"; TEAM_DEATH_TIME="$time"
406
+ TEAM_DEATH_RAW="$raw"; TEAM_DEATH_ANCHOR="$anchor"; TEAM_DEATH_READABLE="1"
407
+ return 0
408
+ }
409
+
410
+ # 记录时间 b 是否比 a 更新(epoch 优先;判不出来 → b 更新,即 session 侧赢)
411
+ team_death_evidence_newer() { # <a 时间> <b 时间> → 0 = b 更新(a 是现任、b 是挑战者)
412
+ local a="${1:-}" b="${2:-}"
413
+ local ae be
414
+ ae="$(team_death_epoch "$a")"; be="$(team_death_epoch "$b")"
415
+ if [ -n "$ae" ] && [ -n "$be" ]; then [ "$be" -gt "$ae" ]; return $?; fi
416
+ if [ -n "$ae" ] && [ -z "$be" ]; then return 1; fi
417
+ return 0
418
+ }
419
+
420
+ # 表面用四段式(空洞:没有异常死亡);只读、无副作用。时间未知用 `-` 占位
421
+ # (字段永不为空 → 读取方用 IFS=tab 不会碰到连续分隔符被吞的坑;原文是最后一段,允许为空)。
422
+ team_seat_death_fields() { # <agent> → "分类⇥来源⇥时间⇥原文" | 空
423
+ team_death_evidence "${1:-}" || return 0
424
+ printf '%s\t%s\t%s\t%s\n' "$TEAM_DEATH_CAT" "$TEAM_DEATH_SOURCE" "${TEAM_DEATH_TIME:--}" "$TEAM_DEATH_RAW"
425
+ }
426
+
427
+ # 状态面的人读一行(无异常死亡 → 空)。unknown 不装懂:明说没有可读来源。
428
+ # P94 的旋钮 TEAM_AGENT_SCENE_LINES=0 = 按配置不打印画面内容:死因行保留分类与来源,原文一并省去
429
+ # (原文就是现场内容;“不打印画面”的承诺不能在一个新面上被漏掉)。
430
+ team_seat_death_text() { # <agent> → 行 | 空
431
+ local a="${1:-}" fields cat source time raw note raw_display
432
+ fields="$(team_seat_death_fields "$a")"
433
+ [ -n "$fields" ] || return 0
434
+ IFS=$'\t' read -r cat source time raw <<< "$fields"
435
+ case "$source" in
436
+ none)
437
+ printf ' 原因:unknown(来源:无 · 没有可读的现场或会话证据)\n'
438
+ return 0 ;;
439
+ recorded) note="(来自 state/deaths.log 记录)" ;;
440
+ *) note="" ;;
441
+ esac
442
+ raw_display="${raw:--}"
443
+ if [ "$(team_agent_scene_lines)" = "0" ]; then
444
+ raw_display="(按 TEAM_AGENT_SCENE_LINES=0:不打印原文)"
445
+ fi
446
+ if [ -n "$time" ] && [ "$time" != "-" ]; then
447
+ printf ' 原因:%s(来源:%s · %s)%s· 原文:%s\n' "$cat" "$source" "$time" "$note" "$raw_display"
448
+ else
449
+ printf ' 原因:%s(来源:%s)%s· 原文:%s\n' "$cat" "$source" "$note" "$raw_display"
450
+ fi
451
+ }
452
+
453
+ # 巡检/收件箱用的 knock 正文(design D7)。
454
+ team_death_knock_text() { # <agent> → 单行正文
455
+ local a="${1:-}" fields cat source time raw task stamp
456
+ fields="$(team_seat_death_fields "$a")"
457
+ [ -n "$fields" ] || return 0
458
+ IFS=$'\t' read -r cat source time raw <<< "$fields"
459
+ task="$(team_state_get "$a" task '-')"
460
+ if [ -n "$time" ] && [ "$time" != "-" ]; then stamp="$source · $time"; else stamp="$source"; fi
461
+ if [ "$cat" = "unknown" ]; then
462
+ printf '[pulse] 席位 %s 死了:unknown(死因无法判定 · 来源:%s)· 原文:%s | 现场:%s status %s\n' \
463
+ "$a" "$stamp" "${raw:--}" "$TEAM_CLI" "$task"
464
+ else
465
+ printf '[pulse] 席位 %s 死了:%s(来源:%s)· 原文:%s | 现场:%s status %s\n' \
466
+ "$a" "$cat" "$stamp" "${raw:--}" "$TEAM_CLI" "$task"
467
+ fi
468
+ }
469
+
470
+ # 待办摘要里的停跑席位明细:"(dev=quota, dev2=unknown)";一个都读不到 → 空(调用方退回裸计数)。
471
+ team_stopped_deaths_text() { # → "(a=cat, b=cat)" | 空
472
+ local a task fields cat out="" n=0
473
+ for a in $(team_agents); do
474
+ task="$(team_state_get "$a" task '')"
475
+ [ -n "$task" ] || continue
476
+ team_agent_live "$a" && continue
477
+ fields="$(team_seat_death_fields "$a")"
478
+ [ -n "$fields" ] || continue
479
+ IFS=$'\t' read -r cat _ _ _ <<< "$fields"
480
+ [ -n "$cat" ] || continue
481
+ out="${out}${out:+, }$a=$cat"
482
+ n=$((n + 1))
483
+ done
484
+ [ "$n" -gt 0 ] || return 0
485
+ printf '(%s)\n' "$out"
486
+ }
487
+
488
+ # ================================================================ 记录(唯一写入者是巡检)
489
+
490
+ team_death_record_file() { printf '%s\n' "$TEAM_STATE_DIR/deaths.log"; }
491
+
492
+ team_death_record_seen() { # <身份> → 0=已在记录里
493
+ local id="${1:-}" f
494
+ [ -n "$id" ] || return 1
495
+ f="$(team_death_record_file)"
496
+ [ -r "$f" ] || return 1
497
+ awk -F'\t' -v id="$id" '$6 == id { found = 1 } END { exit !found }' "$f"
498
+ }
499
+
500
+ team_death_record_add() { # <seat> <分类> <来源> <锚> <身份> <原文>
501
+ local f
502
+ mkdir -p "$TEAM_STATE_DIR"
503
+ f="$(team_death_record_file)"
504
+ printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$(team_timestamp)" "${1:-}" "${2:-}" "${3:-}" "${4:-}" "${5:-}" "${6:-}" >> "$f"
505
+ if [ "$(wc -l < "$f" 2>/dev/null || echo 0)" -gt 500 ]; then
506
+ tail -n 500 "$f" > "$f.tmp" 2>/dev/null && mv "$f.tmp" "$f"
507
+ fi
508
+ return 0
509
+ }
510
+
511
+ # 该席位**当前启动**最新的一条记录(全局 TD_REC_*)。锚等于当前 nonce,或记录时间 ≥ started。
512
+ team_death_record_current() { # <agent> → 0=找到
513
+ TD_REC_CAT=""; TD_REC_SOURCE=""; TD_REC_TIME=""; TD_REC_RAW=""; TD_REC_ANCHOR=""
514
+ local a="${1:-}" f nonce s_epoch ts seat cat source anchor identity raw e found=0
515
+ [ -n "$a" ] || return 1
516
+ f="$(team_death_record_file)"
517
+ [ -r "$f" ] || return 1
518
+ nonce=""
519
+ if [ -r "$(team_dispatch_spawn_file "$a")" ]; then
520
+ nonce="$(head -1 "$(team_dispatch_spawn_file "$a")" 2>/dev/null | awk '{print $1}' || true)"
521
+ fi
522
+ s_epoch="$(team_death_epoch "$(team_state_get "$a" started '')")"
523
+ while IFS=$'\t' read -r ts seat cat source anchor identity raw; do
524
+ [ "$seat" = "$a" ] || continue
525
+ if [ -n "$nonce" ] && [ "$anchor" = "nonce:$nonce" ]; then
526
+ found=1
527
+ else
528
+ e="$(team_death_epoch "$ts")"
529
+ team_death_epoch_current "$e" "$s_epoch" || continue
530
+ found=1
531
+ fi
532
+ TD_REC_CAT="$cat"; TD_REC_SOURCE="$source"; TD_REC_TIME="$ts"; TD_REC_RAW="$raw"; TD_REC_ANCHOR="$anchor"
533
+ done < "$f"
534
+ [ "$found" = "1" ] || return 1
535
+ return 0
536
+ }
537
+
538
+ # ================================================================ 巡检一拍(唯一写入者)
539
+
540
+ # 每拍:停跑席位(task= 非空且没在跑)→ 分类 → 未记录的异常死亡**先落记录再 knock**。
541
+ # standby 时**不写记录也不 knock**(顺延而不是丢:之后第一拍恰好一条)。
542
+ # 返回 0(永远不阻断巡逻;内部的读失败降级为「没有死因」)。
543
+ team_watch_deaths_step() {
544
+ local a identity payload
545
+ for a in $(team_agents); do
546
+ team_death_evidence "$a" || continue
547
+ [ "$TEAM_DEATH_READABLE" = "1" ] || continue
548
+ case "$TEAM_DEATH_CAT" in ''|normal) continue ;; esac
549
+ identity="$(team_death_identity "$a" "$TEAM_DEATH_CAT" "$TEAM_DEATH_ANCHOR")"
550
+ team_death_record_seen "$identity" && continue
551
+ team_in_standby && continue
552
+ team_death_record_add "$a" "$TEAM_DEATH_CAT" "$TEAM_DEATH_SOURCE" "$TEAM_DEATH_ANCHOR" "$identity" "$TEAM_DEATH_RAW"
553
+ payload="$(team_death_knock_text "$a")"
554
+ [ -n "$payload" ] || continue
555
+ # 记录先落盘、knock 后入队(D6 的 at-most-once 方向):中间崩掉就少一个 knock(不重复),
556
+ # 而这次死亡在 watchdog.log 与 digest [1] 里都看得见 —— 不是静默丢失。
557
+ command -v team_wlog >/dev/null 2>&1 && team_wlog "席位死因:$a 死了:$TEAM_DEATH_CAT(来源:$TEAM_DEATH_SOURCE)→ 已记入 deaths.log,敲醒入队(dedup death:$identity)"
558
+ team_outbox_enqueue --kind knock --target "$(team_pm_target)" --from pulse \
559
+ --dedup "death:$identity" --payload "$payload" >/dev/null 2>&1 || true
560
+ done
561
+ return 0
562
+ }