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,528 @@
1
+ #!/usr/bin/env bash
2
+ # tmp-root.sh — 夹具临时根的**唯一创建者**(change: test-tmp-hygiene · 实现:P53)
3
+ #
4
+ # 用法(夹具里):
5
+ # . "$SELF_DIR/lib/tmp-root.sh"
6
+ # TMP="$(tmp_root_create <kind>)" # → ${TMPDIR:-/tmp}/teamsmith-<kind>.XXXXXX
7
+ # cleanup() { tmp_root_reap_all; ... } # 正常退出 / INT / TERM 都回收;TEAM_TMP_KEEP=1 才留
8
+ # trap cleanup EXIT
9
+ #
10
+ # 为什么要有它(design.md §D1–D3、§D8):
11
+ # ① 根一律建在**解析后的**临时根 `${TMPDIR:-/tmp}`(写死 `/tmp` 的模板会被 --lint 判红);
12
+ # 名字进一个 owned 家族 `teamsmith-<kind>.XXXXXX`(sweep 只认这个前缀,别的前缀不碰);
13
+ # ② 根里写 owner 标记 `.teamsmith-tmp`(kind / 创建 pid / 该进程启动时间 / boot id / run id),
14
+ # 于是 killed run 的残留**可归因**,且「owner 已死」判定对 pid 复用安全;
15
+ # ③ 每次创建追加一条 run 台账(`$TEAM_TMP_LEDGER`,默认临时根下的点开头文件;台账头从 P122 起带
16
+ # `repo=<主仓库>` 仓库身份)—— 门禁据此断言「本轮创建的根一个都不许留下」,而不是去猜文件系统,
17
+ # tmp-hygiene 也据此把带 owner 标记的 `teamsmith-*` 归因到仓库;
18
+ # ④ 对出界进程(anchor / sleep)**spawn 时** `tmp_root_track_pid <pid>` 记账,cleanup **只对记录的
19
+ # pid** 发信号(D37):禁止按名字 / 命令行匹配 —— `--self-test` 的 `nokill` 反转钉住这一条。
20
+ #
21
+ # 关键实现事实(踩过的坑,改动前先读):
22
+ # - 夹具的惯用写法是 `TMP="$(tmp_root_create ...)"`:命令替换是**子 shell**,所以状态不能只放在
23
+ # 内存数组里 —— 根的归属走台账(pid + starttime),回收时按台账找自己的根;
24
+ # - `$$` 在子 shell 里仍是**主 shell 的 pid**(bash 语义),台账因此记的是主 shell;
25
+ # - run id 必须**跨子 shell 稳定**(同一 shell 的每次命令替换都一样),否则台账路径会漂 ——
26
+ # 所以默认 id 用 `p$$-<进程启动 jiffies>`,不用 `$RANDOM`;
27
+ # - INT/TERM 陷阱在 **source 时**装(create 在子 shell 里装不到主 shell 上);EXIT 兜底只在
28
+ # 调用者**还没有** EXIT trap 时装,夹具自己 `trap cleanup EXIT` 后由 cleanup 调 tmp_root_reap_all。
29
+ #
30
+ # 契约(细节见 references/protocol.md、design.md):
31
+ # - 正常退出与 INT/TERM 都回收;`TEAM_TMP_KEEP=1` 保留并**打印路径**;
32
+ # - 台账 / 标记在根之外或根之内自洽;nested run 各自记账,互不收对方的根;
33
+ # - 本文件**不改 shell 选项**、不 export 任何东西;只创建 `${TMPDIR:-/tmp}` 下的根。
34
+ #
35
+ # 自检:`bash tests/lib/tmp-root.sh --self-test [--break=<stage>]`
36
+ # `--break=` 把某条规则变回「有缺陷的旧行为」,守门断言必须因此变红(翻转证据,复验用)。
37
+
38
+ TMP_ROOT_LIB=1
39
+ TMP_ROOT_FAMILY_PREFIX="teamsmith-"
40
+
41
+ _tmp_root_err() { printf 'tmp-root: %s\n' "$*" >&2; }
42
+
43
+ # ---------------------------------------------------------------- 基础事实
44
+ tmp_root_base() { printf '%s\n' "${TMPDIR:-/tmp}"; }
45
+
46
+ # run id:优先 TEAM_TMP_RUN_ID;嵌套夹具会清掉 TEAM_*(身份清理),所以门禁同时给非 TEAM_ 的
47
+ # SMOKE_TMP_RUN_ID(它不属于身份,清不掉的传播位)。都没有 → 本进程自己算一个(跨子 shell 稳定)。
48
+ tmp_root_run_id() {
49
+ if [ -z "${TMP_ROOT_RUN_ID:-}" ]; then
50
+ local id="${TEAM_TMP_RUN_ID:-${SMOKE_TMP_RUN_ID:-}}"
51
+ if [ -z "$id" ]; then
52
+ local st; st="$(_tmp_root_proc_start $$ 2>/dev/null || true)"
53
+ id="p$$-${st:-0}"
54
+ fi
55
+ TMP_ROOT_RUN_ID="$(printf '%s' "$id" | tr -c 'A-Za-z0-9._-' '_')"
56
+ fi
57
+ printf '%s\n' "$TMP_ROOT_RUN_ID"
58
+ }
59
+
60
+ tmp_root_ledger() {
61
+ printf '%s\n' "${TEAM_TMP_LEDGER:-$(tmp_root_base)/.teamsmith-tmp-ledger.$(tmp_root_run_id)}"
62
+ }
63
+
64
+ tmp_root_pidfile() {
65
+ printf '%s\n' "${TMP_ROOT_PIDFILE:-$(tmp_root_base)/.teamsmith-tmp-pids.$(tmp_root_run_id).$$}"
66
+ }
67
+
68
+ # /proc/<pid>/stat 的 starttime(boot 后的 jiffies):pid 复用的安全判据。读不到 → 非 0。
69
+ _tmp_root_proc_start() {
70
+ local stat
71
+ stat="$(LC_ALL=C cat "/proc/$1/stat" 2>/dev/null)" || return 1
72
+ stat="${stat##*\) }"
73
+ # shellcheck disable=SC2086
74
+ set -- $stat
75
+ [ -n "${20:-}" ] || return 1
76
+ printf '%s\n' "$20"
77
+ }
78
+
79
+ # 0 = pid 活着且(给了 start 时)就是记下的那个进程;僵尸(Z)算死。
80
+ _tmp_root_proc_alive() {
81
+ local pid="${1:-}" want="${2:-}" st state
82
+ case "$pid" in ''|*[!0-9]*) return 1 ;; esac
83
+ [ -r "/proc/$pid/stat" ] || return 1
84
+ st="$(_tmp_root_proc_start "$pid")" || return 1
85
+ [ -n "$want" ] && [ "$want" != "unknown" ] && [ "$st" != "$want" ] && return 1
86
+ state="$(awk '{sub(/^.*\) /,""); print $1}' "/proc/$pid/stat" 2>/dev/null || true)"
87
+ [ "$state" = "Z" ] && return 1
88
+ return 0
89
+ }
90
+
91
+ # 路径是不是「解析后的临时根下的 owned 家族」——--lint/自检用;`--break=notmpdir` 模拟写死 /tmp 混过关。
92
+ tmp_root_path_ok() {
93
+ if [ "${TMP_ROOT_BREAK:-}" = "notmpdir" ]; then return 0; fi
94
+ case "${1:-}" in
95
+ "$(tmp_root_base)"/"$TMP_ROOT_FAMILY_PREFIX"*) return 0 ;;
96
+ *) return 1 ;;
97
+ esac
98
+ }
99
+
100
+ # ---------------------------------------------------------------- 创建
101
+ # 仓库身份(P122):写进 run 台账头 —— 「归属」不只靠名字前缀,台账里可证到仓库
102
+ _tmp_root_repo_id() {
103
+ local lib g
104
+ lib="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd || true)"
105
+ [ -n "$lib" ] || { printf 'unknown\n'; return 0; }
106
+ g="$(git -C "$lib" rev-parse --path-format=absolute --git-common-dir 2>/dev/null || true)"
107
+ if [ -n "$g" ]; then dirname "$g"; else printf 'unknown\n'; fi
108
+ }
109
+ _tmp_root_ledger_append() { # <path> <pid> <start> <kind> <created>
110
+ local f; f="$(tmp_root_ledger)"
111
+ if [ ! -f "$f" ]; then
112
+ printf '# teamsmith temp ledger run=%s owner=%s started=%s repo=%s\n' \
113
+ "$(tmp_root_run_id)" "$$" "$(date +%s 2>/dev/null || printf 0)" "$(_tmp_root_repo_id)" >"$f" 2>/dev/null || return 1
114
+ fi
115
+ printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$1" "$2" "$3" "$4" "$5" "$(tmp_root_run_id)" >>"$f" 2>/dev/null || return 1
116
+ return 0
117
+ }
118
+
119
+ # INT/TERM 的回收路径(实测过的 bash 语义,改动前先读):
120
+ # - INT:非交互 shell 默认**不会**因 SIGINT 而死(实测:前台 sleep 里收 INT,shell 活着、EXIT trap 不跑),
121
+ # 所以必须装 INT 陷阱;它在当前前台命令结束/`wait` 被打断时执行 → 回收。
122
+ # - TERM:**不装陷阱**。未捕获的致命 TERM 会立即杀掉 shell 并跑 EXIT trap(实测 ~100ms),
123
+ # 装了陷阱反而被 bash 「等前台命令结束再跑」的规则推迟 —— 长前台命令(真进程夹具)里回收会晚到。
124
+ # EXIT trap 只在调用者**还没有** EXIT trap 时装 —— 夹具自己的 `trap cleanup EXIT` 之后由 cleanup
125
+ # 调 tmp_root_reap_all(构造上两种路径都收)。
126
+ _tmp_root_install_traps() {
127
+ [ "${TMP_ROOT_TRAPS:-0}" = "1" ] && return 0
128
+ TMP_ROOT_TRAPS=1
129
+ trap 'tmp_root_on_signal INT' INT 2>/dev/null || true
130
+ if [ -z "$(trap -p EXIT 2>/dev/null || true)" ]; then
131
+ trap 'tmp_root_reap_all' EXIT
132
+ fi
133
+ return 0
134
+ }
135
+
136
+ # tmp_root_create <kind> → 打印根路径(stdout 只有这一行)。失败:2 非法 kind / 3 建不出来。
137
+ tmp_root_create() {
138
+ local kind="${1:-fixture}"
139
+ case "$kind" in
140
+ ''|*[!A-Za-z0-9._-]*) _tmp_root_err "非法 kind [${kind}](只允许 A-Za-z0-9._-)"; return 2 ;;
141
+ esac
142
+ local base; base="$(tmp_root_base)"
143
+ if [ ! -d "$base" ]; then
144
+ _tmp_root_err "临时根 $base 不存在(TMPDIR 指向不存在的目录?)"
145
+ return 3
146
+ fi
147
+ local root
148
+ root="$(mktemp -d "$base/$TMP_ROOT_FAMILY_PREFIX$kind.XXXXXX" 2>/dev/null)" || {
149
+ _tmp_root_err "在 $base 下建不出根(空间/inode/权限?)"; return 3; }
150
+ local pid=$$ start boot created run
151
+ start="$(_tmp_root_proc_start "$pid" 2>/dev/null || true)"
152
+ boot="$(cat /proc/sys/kernel/random/boot_id 2>/dev/null || printf 'unknown')"
153
+ created="$(date +%s 2>/dev/null || printf 0)"
154
+ run="$(tmp_root_run_id)"
155
+ if ! { printf 'kind=%s\npid=%s\nstart=%s\nboot=%s\nrun=%s\ncreated=%s\n' \
156
+ "$kind" "$pid" "${start:-unknown}" "$boot" "$run" "$created" >"$root/.teamsmith-tmp"; }; then
157
+ _tmp_root_err "写不了 owner 标记 $root/.teamsmith-tmp"
158
+ rm -rf -- "$root" 2>/dev/null || true
159
+ return 3
160
+ fi
161
+ _tmp_root_ledger_append "$root" "$pid" "${start:-unknown}" "$kind" "$created" || true
162
+ printf '%s\n' "$root"
163
+ return 0
164
+ }
165
+
166
+ # ---------------------------------------------------------------- 出界进程记账(D37)
167
+ # tmp_root_track_pid <pid> [label]:spawn 之后立刻调用;cleanup 只对这些 pid 发信号。
168
+ tmp_root_track_pid() {
169
+ local pid="${1:-}" label="${2:-}" start pf
170
+ case "$pid" in ''|*[!0-9]*) _tmp_root_err "track: 非法 pid [${pid}]"; return 2 ;; esac
171
+ [ "$pid" -gt 1 ] || return 2
172
+ start="$(_tmp_root_proc_start "$pid" 2>/dev/null || true)"
173
+ pf="$(tmp_root_pidfile)"
174
+ printf '%s\t%s\t%s\n' "$pid" "${start:-unknown}" "$label" >>"$pf" 2>/dev/null || return 3
175
+ return 0
176
+ }
177
+
178
+ # tmp_root_detach <path>:把根从「本进程退出时回收」的清单里摘出来 —— 供「返回一个路径给调用方、
179
+ # 生命周期归调用方」的 fixture CLI 用(如 load-experiment 的 private-root)。根留在台账里(--status
180
+ # 仍可归因、老了也能被 --sweep 回收),但本进程的 EXIT/INT 不再删它。
181
+ tmp_root_detach() {
182
+ local p="${1:-}"
183
+ [ -d "$p" ] || return 2
184
+ [ -f "$p/.teamsmith-tmp" ] || return 2
185
+ printf 'detached=1\n' >>"$p/.teamsmith-tmp" 2>/dev/null || return 3
186
+ return 0
187
+ }
188
+
189
+ # ---------------------------------------------------------------- 回收
190
+ _tmp_root_signal_tracked() {
191
+ local pf; pf="$(tmp_root_pidfile)"
192
+ [ -f "$pf" ] || return 0
193
+ local grace="${TEAM_TMP_KILL_GRACE:-5}"
194
+ case "$grace" in ''|*[!0-9]*) grace=5 ;; esac
195
+ [ "$grace" -gt 60 ] && grace=60
196
+
197
+ local pid start label
198
+ # ① TERM(只对记录过且 starttime 对得上的 pid)
199
+ while IFS=$'\t' read -r pid start label; do
200
+ [ -n "$pid" ] || continue
201
+ _tmp_root_proc_alive "$pid" "$start" || continue
202
+ kill -TERM "$pid" 2>/dev/null || true
203
+ # --break=nokill:把「按命令行匹配」这个旧缺陷形状放回来(自检时由夹具显式给出受害 pid,
204
+ # 绝不做真正的 pkill/模式匹配 —— 我们只打自己 spawn 的进程)。
205
+ if [ "${TMP_ROOT_BREAK:-}" = "nokill" ] && [ -n "${TEAM_TMP_BREAK_VICTIM:-}" ]; then
206
+ kill -TERM "$TEAM_TMP_BREAK_VICTIM" 2>/dev/null || true
207
+ fi
208
+ done <"$pf"
209
+
210
+ # ② 宽限期内等它自己走;不走 → KILL
211
+ local i alive
212
+ i=0
213
+ while [ "$i" -lt $((grace * 5)) ]; do
214
+ alive=0
215
+ while IFS=$'\t' read -r pid start label; do
216
+ [ -n "$pid" ] || continue
217
+ _tmp_root_proc_alive "$pid" "$start" && alive=1
218
+ done <"$pf"
219
+ [ "$alive" = "0" ] && break
220
+ sleep 0.2
221
+ i=$((i + 1))
222
+ done
223
+ while IFS=$'\t' read -r pid start label; do
224
+ [ -n "$pid" ] || continue
225
+ _tmp_root_proc_alive "$pid" "$start" || continue
226
+ kill -KILL "$pid" 2>/dev/null || true
227
+ done <"$pf"
228
+ return 0
229
+ }
230
+
231
+ _tmp_root_remove_owned() {
232
+ local f; f="$(tmp_root_ledger)"
233
+ [ -f "$f" ] || return 0
234
+ local my_start; my_start="$(_tmp_root_proc_start $$ 2>/dev/null || true)"
235
+ local keep="${TEAM_TMP_KEEP:-0}"
236
+ local path pid start kind created run
237
+ while IFS=$'\t' read -r path pid start kind created run; do
238
+ case "$path" in ''|'#'*) continue ;; esac
239
+ [ "$pid" = "$$" ] || continue # 别的进程(nested run)的根由它自己收
240
+ if [ -n "$my_start" ] && [ "$start" != "unknown" ] && [ "$start" != "$my_start" ]; then
241
+ continue # pid 复用:不是我的根
242
+ fi
243
+ [ -d "$path" ] || continue
244
+ # 调用方拥有生命周期的根(tmp_root_detach):不替它收
245
+ grep -q '^detached=1$' "$path/.teamsmith-tmp" 2>/dev/null && continue
246
+ if [ "$keep" = "1" ]; then
247
+ # --break=nokeeep:模拟「静默保留」(旧的死旋钮行为)——打印这条被守门断言钉住
248
+ [ "${TMP_ROOT_BREAK:-}" = "nokeeep" ] || printf '保留临时根:%s(TEAM_TMP_KEEP=1)\n' "$path"
249
+ continue
250
+ fi
251
+ # 夹具模式红侧(夹具泄漏断言用):故意不回收,让门禁必须报红(只在 TEAM_SMOKE_FIXTURE=1 下生效)
252
+ if [ "${TEAM_SMOKE_FIXTURE:-0}" = "1" ] && [ "${TEAM_TMP_HYGIENE_FLIP:-}" = "leak" ]; then
253
+ printf 'FLIP(leak):故意不回收 %s\n' "$path"
254
+ continue
255
+ fi
256
+ # --break=noreap:自检的翻转开关(看门断言必须因此变红)
257
+ [ "${TMP_ROOT_BREAK:-}" = "noreap" ] && continue
258
+ rm -rf -- "$path" 2>/dev/null || _tmp_root_err "删不掉 $path"
259
+ done <"$f"
260
+ return 0
261
+ }
262
+
263
+ # owner 进程退出时收走台账(nested run 不碰别人的台账)
264
+ _tmp_root_drop_ledger_if_owner() {
265
+ local f; f="$(tmp_root_ledger)"
266
+ [ -f "$f" ] || return 0
267
+ local owner
268
+ owner="$(sed -n '1s/.* owner=\([0-9]*\).*/\1/p' "$f" 2>/dev/null | head -1)"
269
+ [ "$owner" = "$$" ] || return 0
270
+ [ "${TEAM_TMP_KEEP:-0}" = "1" ] && { printf '保留 run 台账:%s\n' "$f"; return 0; }
271
+ rm -f -- "$f" 2>/dev/null || true
272
+ return 0
273
+ }
274
+
275
+ # 正常退出 / INT 都走这里(TERM 走未捕获致命信号 → EXIT trap 的路径,见 _tmp_root_install_traps):
276
+ # 发信号(只对记录的 pid)→ 收根 → 收台账。
277
+ tmp_root_reap_all() {
278
+ [ "${BASHPID:-$$}" = "$$" ] || return 0 # 子 shell 的 EXIT 不替主 shell 收(项目既有纪律)
279
+ _tmp_root_signal_tracked
280
+ _tmp_root_remove_owned
281
+ rm -f -- "$(tmp_root_pidfile)" 2>/dev/null || true
282
+ _tmp_root_drop_ledger_if_owner
283
+ return 0
284
+ }
285
+
286
+ tmp_root_on_signal() { # <INT|TERM>
287
+ [ "${TMP_ROOT_IN_SIGNAL:-0}" = "1" ] && return 0
288
+ TMP_ROOT_IN_SIGNAL=1
289
+ tmp_root_reap_all
290
+ case "${1:-TERM}" in
291
+ INT) exit 130 ;;
292
+ *) exit 143 ;;
293
+ esac
294
+ }
295
+
296
+ # 门禁用:列出台账里**还活着**的根(path<TAB>pid<TAB>kind<TAB>size_KB<TAB>files)
297
+ tmp_root_ledger_survivors() {
298
+ local f; f="$(tmp_root_ledger)"
299
+ [ -f "$f" ] || return 0
300
+ local path pid start kind created run kb files
301
+ while IFS=$'\t' read -r path pid start kind created run; do
302
+ case "$path" in ''|'#'*) continue ;; esac
303
+ [ -d "$path" ] || continue
304
+ kb="$(du -sk "$path" 2>/dev/null | awk '{print $1}' || printf 0)"
305
+ files="$(find "$path" 2>/dev/null | wc -l | tr -d ' ' || printf 0)"
306
+ printf '%s\t%s\t%s\t%s\t%s\n' "$path" "$pid" "${kind:-?}" "${kb:-0}" "${files:-0}"
307
+ done <"$f"
308
+ return 0
309
+ }
310
+
311
+ # ---------------------------------------------------------------- 自检
312
+ # `--break=<stage>`:把某条规则变回有缺陷的旧行为,守门断言必须变红(翻转)。
313
+ _tmp_root_selftest() {
314
+ local break_stage="" a
315
+ for a in "$@"; do
316
+ case "$a" in
317
+ --break=*) break_stage="${a#--break=}" ;;
318
+ -h|--help)
319
+ printf '用法:bash %s --self-test [--break=nokill|nokeeep|notmpdir|noreap]\n' "${BASH_SOURCE[0]}"
320
+ return 0 ;;
321
+ *) _tmp_root_err "不认识的自检参数 [$a]"; return 2 ;;
322
+ esac
323
+ done
324
+ TMP_ROOT_BREAK="$break_stage"; export TMP_ROOT_BREAK
325
+ local PASS=0 FAIL=0
326
+ st_ok() { printf ' \033[32m✓\033[0m %s\n' "$1"; PASS=$((PASS + 1)); }
327
+ st_bad() { printf ' \033[31m✗\033[0m %s\n' "$1"; FAIL=$((FAIL + 1)); }
328
+
329
+ local self_path
330
+ self_path="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
331
+ local st_dir
332
+ st_dir="$(mktemp -d "$(tmp_root_base)/teamsmith-libselftest.XXXXXX" 2>/dev/null)" || {
333
+ _tmp_root_err "自检建不出临时目录"; return 3; }
334
+ trap "rm -rf -- '$st_dir'" EXIT # 路径要内嵌:trap 在函数返回后执行,local 变量那时已经不在了
335
+
336
+ printf '\n\033[1m== tmp-root.sh 自检 ==\033[0m(break=%s)\n' "${break_stage:-none}"
337
+
338
+ # ① 创建:路径、家族名、标记、台账
339
+ export TMPDIR="$st_dir"
340
+ local r; r="$(tmp_root_create selftest)"
341
+ case "$r" in "$st_dir"/teamsmith-selftest.*) st_ok "根建在 \${TMPDIR} 下并且名字在 owned 家族($r)" ;;
342
+ *) st_bad "根的路径/名字不对([$r],期望 $st_dir/teamsmith-selftest.*)" ;; esac
343
+ if [ -f "$r/.teamsmith-tmp" ] && grep -q "^kind=selftest$" "$r/.teamsmith-tmp" \
344
+ && grep -q "^pid=$$$" "$r/.teamsmith-tmp" && grep -q '^run=' "$r/.teamsmith-tmp"; then
345
+ st_ok "owner 标记写全(kind / pid / start / boot / run / created)"
346
+ else
347
+ st_bad "owner 标记缺字段:$(tr '\n' ' ' <"$r/.teamsmith-tmp" 2>/dev/null)"
348
+ fi
349
+ if [ -f "$(tmp_root_ledger)" ] && grep -qF "$r $$" "$(tmp_root_ledger)"; then
350
+ st_ok "run 台账里有这一条($(tmp_root_ledger))"
351
+ else
352
+ st_bad "run 台账里没有这条根"
353
+ fi
354
+ if head -1 "$(tmp_root_ledger)" 2>/dev/null | grep -Eq ' repo=[^ ]'; then
355
+ st_ok "run 台账头记了仓库身份($(head -1 "$(tmp_root_ledger)" | sed -n 's/.* repo=//p'))"
356
+ else
357
+ st_bad "run 台账头没有 repo= 仓库身份"
358
+ fi
359
+
360
+ # ② notmpdir:写死 /tmp 的路径不许被当成合规根
361
+ if tmp_root_path_ok /tmp/teamsmith-whatever.1234; then
362
+ st_bad "写死 /tmp 的路径被当成合规根(notmpdir 规则失效)"
363
+ else
364
+ st_ok "写死 /tmp 的路径被拒(tmp_root_path_ok 非 0)"
365
+ fi
366
+ if tmp_root_path_ok "$st_dir/teamsmith-x.1234"; then
367
+ st_ok "\${TMPDIR} 下的家族路径被接受"
368
+ else
369
+ st_bad "\${TMPDIR} 下的家族路径被误拒"
370
+ fi
371
+
372
+ # ③ 正常回收:子进程创建 → 退出 → 根消失
373
+ local child_root
374
+ child_root="$( env TMPDIR="$st_dir" bash -c '
375
+ . "'"$self_path"'"
376
+ tmp_root_create reaptest' )"
377
+ if [ -n "$child_root" ] && [ ! -e "$child_root" ]; then
378
+ st_ok "子进程正常退出后根已回收($child_root 不在了)"
379
+ else
380
+ st_bad "子进程退出后根还在:[$child_root]"
381
+ fi
382
+
383
+ # ③b detach:生命周期归调用方的根不能被创建者的 trap 收掉
384
+ local det_root
385
+ det_root="$( env TMPDIR="$st_dir" bash -c '
386
+ . "'"$self_path"'"
387
+ r="$(tmp_root_create detachtest)"
388
+ tmp_root_detach "$r"
389
+ printf "%s" "$r"' )"
390
+ if [ -n "$det_root" ] && [ -d "$det_root" ] && grep -q '^detached=1$' "$det_root/.teamsmith-tmp" 2>/dev/null; then
391
+ st_ok "detach 过的根活过了创建者退出($det_root)"
392
+ else
393
+ st_bad "detach 的根被收了或没标记([$det_root])"
394
+ fi
395
+ rm -rf -- "$det_root" 2>/dev/null || true
396
+
397
+ # ④ nokeeep:TEAM_TMP_KEEP=1 必须**打印**路径(静默保留是缺陷)
398
+ local keep_out keep_root
399
+ keep_out="$( env TMPDIR="$st_dir" TEAM_TMP_KEEP=1 bash -c '
400
+ . "'"$self_path"'"
401
+ r="$(tmp_root_create keeptest)"
402
+ printf "ROOT=%s\n" "$r"
403
+ ' 2>&1 )"
404
+ keep_root="$(printf '%s\n' "$keep_out" | sed -n 's/^ROOT=//p' | head -1)"
405
+ case "$keep_out" in
406
+ *"保留临时根:$keep_root"*) st_ok "TEAM_TMP_KEEP=1 打印了保留路径" ;;
407
+ *) st_bad "TEAM_TMP_KEEP=1 没有打印保留路径(输出:$(printf '%s' "$keep_out" | tr '\n' '|'))" ;;
408
+ esac
409
+ if [ -n "$keep_root" ] && [ -d "$keep_root" ]; then
410
+ st_ok "TEAM_TMP_KEEP=1 真的把根留下了($keep_root)"
411
+ else
412
+ st_bad "TEAM_TMP_KEEP=1 的根不在([$keep_root])"
413
+ fi
414
+ rm -rf -- "$keep_root" 2>/dev/null || true
415
+
416
+ # ⑤ nokill:只对记录的 pid 发信号 —— 同 argv 的无关 sleep 必须活着
417
+ local arg="teamsmith-nokill-$$" tracked_pid="" victim_pid=""
418
+ # 两个 argv 完全一样的 sleep(pid 不同):一个被 track,一个是「调用者的」
419
+ sleep 300 & tracked_pid=$!
420
+ sleep 300 & victim_pid=$!
421
+ sleep 0.2
422
+ # 回收必须在**独立进程**里跑(子 shell 的 EXIT 按项目纪律不替主 shell 收,BASHPID != $$ → no-op)
423
+ env TMPDIR="$st_dir" TEAM_TMP_BREAK_VICTIM="$victim_pid" TRACKED_PID="$tracked_pid" bash -c '
424
+ . "'"$self_path"'"
425
+ _r="$(tmp_root_create nokilltest)"
426
+ tmp_root_track_pid "$TRACKED_PID" anchor
427
+ tmp_root_reap_all
428
+ printf "ROOT=%s\n" "$_r"
429
+ ' >"$st_dir/nokill.out" 2>&1
430
+ sleep 0.3
431
+ if _tmp_root_proc_alive "$tracked_pid"; then
432
+ st_bad "记录过的 anchor(pid $tracked_pid)没被回收"
433
+ else
434
+ st_ok "记录过的 anchor 被回收(只对它发了信号)"
435
+ fi
436
+ if _tmp_root_proc_alive "$victim_pid"; then
437
+ st_ok "同 argv 的无关 sleep 活着(cleanup 没有按命令行匹配)"
438
+ else
439
+ st_bad "无关的 sleep 被信号命中(cleanup 做了命令行匹配 —— D37 违规)"
440
+ fi
441
+ kill -KILL "$tracked_pid" "$victim_pid" 2>/dev/null || true
442
+ wait "$tracked_pid" "$victim_pid" 2>/dev/null || true
443
+
444
+ # ⑥ term:TERM 中途到达 → 根消失 + 记录的 anchor 不再存在
445
+ local term_out="$st_dir/term.out" term_pid="" term_root="" term_anchor=""
446
+ ( export TMPDIR="$st_dir"; exec bash -c '
447
+ . "'"$self_path"'"
448
+ r="$(tmp_root_create termtest)"
449
+ sleep 300 & a=$!
450
+ tmp_root_track_pid "$a" anchor
451
+ printf "ROOT=%s\nANCHOR=%s\n" "$r" "$a"
452
+ wait' ) >"$term_out" 2>&1 &
453
+ term_pid=$!
454
+ local _i=0
455
+ while [ "$_i" -lt 50 ]; do
456
+ grep -q '^ANCHOR=' "$term_out" 2>/dev/null && break
457
+ sleep 0.1; _i=$((_i + 1))
458
+ done
459
+ term_root="$(sed -n 's/^ROOT=//p' "$term_out" | head -1)"
460
+ term_anchor="$(sed -n 's/^ANCHOR=//p' "$term_out" | head -1)"
461
+ if [ -z "$term_root" ] || [ -z "$term_anchor" ]; then
462
+ st_bad "TERM 用例的子进程没报出根/锚点 pid($(tr '\n' '|' <"$term_out"))"
463
+ else
464
+ kill -TERM "$term_pid" 2>/dev/null || true
465
+ _i=0
466
+ while [ "$_i" -lt 100 ] && kill -0 "$term_pid" 2>/dev/null; do sleep 0.1; _i=$((_i + 1)); done
467
+ wait "$term_pid" 2>/dev/null || true
468
+ if [ ! -e "$term_root" ]; then
469
+ st_ok "TERM 后根在宽限期内消失($term_root)"
470
+ else
471
+ st_bad "TERM 后根还在:$term_root"
472
+ fi
473
+ if _tmp_root_proc_alive "$term_anchor"; then
474
+ st_bad "TERM 后记录的 anchor(pid $term_anchor)还活着"
475
+ else
476
+ st_ok "TERM 后记录的 anchor 不再存在(pid $term_anchor)"
477
+ fi
478
+ fi
479
+ kill -KILL "$term_pid" "$term_anchor" 2>/dev/null || true
480
+ rm -rf -- "$term_root" 2>/dev/null || true
481
+
482
+ # ⑥b 前台长命令里收到 TERM:默认动作杀掉 shell → EXIT trap 回收(**不能**装 TERM 陷阱,否则
483
+ # bash 要等前台命令结束才跑陷阱,回收会晚到)。这条钉住「长命令里也必须及时回收」。
484
+ local fg_out="$st_dir/termfg.out" fg_pid="" fg_root=""
485
+ ( export TMPDIR="$st_dir"; exec bash -c '
486
+ . "'"$self_path"'"
487
+ r="$(tmp_root_create termfg)"
488
+ printf "ROOT=%s\n" "$r"
489
+ sleep 30' ) >"$fg_out" 2>&1 &
490
+ fg_pid=$!
491
+ _i=0
492
+ while [ "$_i" -lt 50 ]; do
493
+ grep -q '^ROOT=' "$fg_out" 2>/dev/null && break
494
+ sleep 0.1; _i=$((_i + 1))
495
+ done
496
+ fg_root="$(sed -n 's/^ROOT=//p' "$fg_out" | head -1)"
497
+ if [ -z "$fg_root" ]; then
498
+ st_bad "前台长命令 TERM 用例没报出根($(tr '\n' '|' <"$fg_out" 2>/dev/null))"
499
+ else
500
+ kill -TERM "$fg_pid" 2>/dev/null || true
501
+ _i=0
502
+ while [ "$_i" -lt 25 ] && kill -0 "$fg_pid" 2>/dev/null; do sleep 0.1; _i=$((_i + 1)); done
503
+ if ! kill -0 "$fg_pid" 2>/dev/null && [ ! -e "$fg_root" ]; then
504
+ st_ok "前台长命令里 TERM:shell 立即退出且根已回收($((_i * 100))ms 内)"
505
+ else
506
+ st_bad "前台长命令里 TERM 没有及时回收(alive=$(kill -0 "$fg_pid" 2>/dev/null && echo yes || echo no) root=$([ -e "$fg_root" ] && echo 在 || echo 没了))"
507
+ fi
508
+ fi
509
+ kill -KILL "$fg_pid" 2>/dev/null || true
510
+ rm -rf -- "$fg_root" 2>/dev/null || true
511
+
512
+ printf '\n\033[1m== 结果 ==\033[0m ✓ %d ✗ %d\n' "$PASS" "$FAIL"
513
+ [ "$FAIL" -eq 0 ] && { printf '\033[32mtmp-root.sh 自检全绿\033[0m\n'; return 0; }
514
+ printf '\033[31mtmp-root.sh 自检有失败项(break=%s)\033[0m\n' "${break_stage:-none}"
515
+ return 1
516
+ }
517
+
518
+ # ---------------------------------------------------------------- 入口
519
+ if [ "${BASH_SOURCE[0]}" = "$0" ]; then
520
+ case "${1:-}" in
521
+ --self-test) shift; _tmp_root_selftest "$@" ;;
522
+ -h|--help|"") printf '用法:bash %s --self-test [--break=<stage>]\n' "${BASH_SOURCE[0]}" ;;
523
+ *) printf 'tmp-root.sh:只支持 --self-test(被夹具 source 用)\n' >&2; exit 2 ;;
524
+ esac
525
+ else
526
+ # source 时:装 INT/TERM(EXIT 只在调用者还没有 EXIT trap 时装兜底)
527
+ _tmp_root_install_traps
528
+ fi
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env bash
2
+ # teamsmith · tmux 能力探测(共享库;smoke 前导段 source)
3
+ #
4
+ # 为什么单独放进 lib/:选段运行(`smoke.sh --select <key>`)只保留前导段与选中的段,
5
+ # 段体内定义的 helper 在别的段的副本里**不存在** —— 实测:`--select 55` 里
6
+ # `tmux_has_bracket_paste_format` 报 command not found,条件判断把整段静默跳过(假绿)。
7
+ # 放前导段 source 的库里,全量与选段两种跑法都拿得到同一份实现(不再各处复制)。
8
+ #
9
+ # 隔离:本文件不在 smoke.sh 里,拿不到它的「文件级私有 socket」判定(M28 lint 按文件看状态),
10
+ # 所以这里**自建一个私有 socket 目录**问一句 —— `env -u TMUX -u TMUX_PANE TMUX_TMPDIR=<新目录> tmux …`
11
+ # 是 lint 认可的形态,而且探针不依赖调用者的 server(调用者有没有 tmux 会话都一样结论)。
12
+ # 只定义函数,不自动执行:没有 tmux 的机器 source 它也不会失败。
13
+ # shellcheck shell=bash
14
+
15
+ # P162:破坏性调用(kill-server)动手前先证明私有 socket 生效(不成立 → 拒绝执行)
16
+ # shellcheck source=tmux-iso.sh
17
+ . "$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)/tmux-iso.sh"
18
+
19
+ # M47:`#{bracket_paste_flag}` 是 tmux **3.7 起**才有的格式(同族的旧 tmux 上产品保守地走「多行落文件
20
+ # + 一行指针」,那不是缺陷,是无从探测)。
21
+ tmux_has_bracket_paste_format() {
22
+ local s="bpf-$$" d v rc=1
23
+ d="$(mktemp -d "${TMPDIR:-/tmp}/teamsmith-tmux-cap.XXXXXX" 2>/dev/null)" || return 1
24
+ if env -u TMUX -u TMUX_PANE TMUX_TMPDIR="$d" tmux new-session -d -s "$s" -x 80 -y 24 'sleep 5' 2>/dev/null; then
25
+ v="$(env -u TMUX -u TMUX_PANE TMUX_TMPDIR="$d" tmux display-message -p -t "$s" '#{bracket_paste_flag}' 2>/dev/null)"
26
+ [ -n "$v" ] && rc=0
27
+ fi
28
+ # 自己的私有 server:只杀自己刚起的那个(私有目录 + env -u TMUX,构造上打不到调用者的 server)。
29
+ # P162:动手前先证明隔离生效;不成立就拒绝执行(被 source 的库,绝不能 exit)。
30
+ if tmux_iso_guard_soft "tmux-cap" "能力探测的私有 server 收尾" --tmpdir "$d"; then
31
+ env -u TMUX -u TMUX_PANE TMUX_TMPDIR="$d" tmux kill-server 2>/dev/null || true
32
+ fi
33
+ rm -rf "$d"
34
+ return $rc
35
+ }
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env bash
2
+ # teamsmith · P180 · 「调用形态 → 前置」探针(子进程夹具)
3
+ #
4
+ # 在**子进程**里装上与门禁**同一份**壳函数(lib/tmux-iso.sh 的 tmux_iso_install_suite_guards),
5
+ # 然后执行一段给定的调用形态,用**记录桩**量出两件事:
6
+ # ① 这条形态有没有硬停(exit 2 = 前置不成立,调用绝不执行);
7
+ # ② 记录桩收到了什么 argv(收到 = 这条调用真的被执行了)。
8
+ # 这就是 P180 要求的红侧口径:「每一种绕过形态 → 必须硬停 + 记录桩为空」;反向的「正常形态照常
9
+ # 执行」也由同一个记录桩量(收到 = 放行了)。
10
+ #
11
+ # 记录桩(本脚本生成,放在私有桩目录里当 PATH 里的 `tmux`):
12
+ # · **只记「要动手」的调用**(写进 <记录桩文件>)—— 红侧要求它为空,就是「没有一条调用被放行」;
13
+ # · 一份只读动词白名单(证明要看 tmux 自己解析出的 socket:ls / display-message …)另记到
14
+ # <记录桩文件>.ro 并交给 `--real` 指的真 tmux —— 那是前置自己的观察,不算被保护的调用;
15
+ # · 白名单之外**一律只记录、绝不动手** —— 影子/破损形态下也不许真杀。
16
+ # 破坏性调用**永远不会**被桩执行:本探针构造上打不到任何真 server。
17
+ #
18
+ # 用法:
19
+ # tmux-iso-call-probe.sh <lib> <记录桩文件> <桩目录> <形态> \
20
+ # --real <真 tmux 路径> [--tmpdir <本轮声称的 TMUX_TMPDIR> | --no-tmpdir] \
21
+ # [--own-root R] [--caller-tmpdir C] [--label L]
22
+ # 形态:一段 shell 代码(在壳函数装好之后 eval)——
23
+ # 'tmux -S /tmp/tmux-$(id -u)/default kill-server'
24
+ # 'command tmux -L default kill-server'
25
+ # '\tmux kill-session -t x' # 反斜杠引号:bash 对函数照查(不是绕过)
26
+ # 'true; tmux -f /dev/null kill-server'
27
+ #
28
+ # 退出码:0 = 没硬停(调用执行了,或本来就不是破坏性调用)|2 = 硬停(前置不成立)|9 = 驱动自身问题
29
+ # shellcheck shell=bash
30
+ set -uo pipefail
31
+
32
+ LIB="${1:-}"; RECORD="${2:-}"; STUBDIR="${3:-}"; FORM="${4:-}"; shift 4 2>/dev/null || true
33
+ REAL=""; DIR=""; OWN=""; CALLER=""; LABEL="P180 探针"; NO_TMPDIR=0
34
+ while [ $# -gt 0 ]; do
35
+ case "$1" in
36
+ --real) [ $# -ge 2 ] || exit 9; REAL="$2"; shift 2 ;;
37
+ --tmpdir) [ $# -ge 2 ] || exit 9; DIR="$2"; shift 2 ;;
38
+ --no-tmpdir) NO_TMPDIR=1; shift ;;
39
+ --own-root) [ $# -ge 2 ] || exit 9; OWN="$2"; shift 2 ;;
40
+ --caller-tmpdir) [ $# -ge 2 ] || exit 9; CALLER="$2"; shift 2 ;;
41
+ --label) [ $# -ge 2 ] || exit 9; LABEL="$2"; shift 2 ;;
42
+ *) exit 9 ;;
43
+ esac
44
+ done
45
+ [ -f "$LIB" ] || exit 9
46
+ [ -n "$RECORD" ] && [ -n "$STUBDIR" ] && [ -n "$FORM" ] && [ -n "$REAL" ] || exit 9
47
+
48
+ # ── 记录桩:写在桩目录里,PATH 最前 ─────────────────────────────────────────────────────────
49
+ mkdir -p "$STUBDIR" || exit 9
50
+ : > "$RECORD"; : > "$RECORD.ro"
51
+ cat > "$STUBDIR/tmux" <<EOF
52
+ #!/usr/bin/env bash
53
+ # P180 探针的记录桩(生成物,不是仓库脚本):只记**要动手**的调用;只读白名单另记 <记录>.ro。
54
+ case "\${1:-}" in
55
+ ls|list-sessions|list-windows|list-panes|display-message|has-session|show-options|show-messages|show-environment)
56
+ printf '%s\n' "\$*" >> "$RECORD.ro" # 前置观察 socket 用的只读调用:留证,不算「动手」
57
+ # 只读也算「打到 server 上」:目标不可用(会静默回退共享默认 socket)时一律不交真身(#1794)
58
+ case "\${TMUX_TMPDIR:-}" in
59
+ ''|/tmp|/tmp/) exit 0 ;;
60
+ esac
61
+ [ -d "\${TMUX_TMPDIR:-}" ] || exit 0
62
+ exec "$REAL" "\$@" ;;
63
+ esac
64
+ printf '%s\n' "\$*" >> "$RECORD" # 其余都记(影子/破损形态下这些就是「真的执行了」的证据)
65
+ exit 0
66
+ EOF
67
+ chmod +x "$STUBDIR/tmux" || exit 9
68
+ PATH="$STUBDIR:$PATH"; export PATH
69
+ STUB="$STUBDIR/tmux"
70
+
71
+ # ── 与门禁同一份壳(lib 里装):先剥掉调用者的 tmux 身份,只留本轮声称的 TMUX_TMPDIR ──────────
72
+ unset TMUX TMUX_PANE 2>/dev/null || true
73
+ # shellcheck source=tmux-iso.sh
74
+ . "$LIB" || exit 9
75
+ tmux_iso_skip() { printf 'SKIP(前置不成立:%s) %s\n' "$2" "$1"; }
76
+ if [ "$NO_TMPDIR" = "1" ]; then unset TMUX_TMPDIR 2>/dev/null || true; else TMUX_TMPDIR="$DIR"; export TMUX_TMPDIR; fi
77
+ TMP="$OWN"; SMOKE_SEC_OPEN_ID="$LABEL"; SMOKE_CALLER_TMUX_TMPDIR="$CALLER"; export TMP SMOKE_SEC_OPEN_ID SMOKE_CALLER_TMUX_TMPDIR
78
+ tmux_iso_install_suite_guards "$STUB"
79
+
80
+ # ── 执行形态(硬停时 exit 2 就从这里直接离开本进程)────────────────────────────────────────
81
+ eval "$FORM"