teamsmith 0.2.0 → 0.3.1

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 (163) hide show
  1. package/README.md +4 -4
  2. package/package.json +1 -1
  3. package/skills/teamsmith/CHANGELOG.md +69 -0
  4. package/skills/teamsmith/SKILL.md +20 -13
  5. package/skills/teamsmith/extension/team-inbox-watch.ts +5 -3
  6. package/skills/teamsmith/extension/team-notify.ts +37 -6
  7. package/skills/teamsmith/extension/team.ts +438 -0
  8. package/skills/teamsmith/references/config.md +39 -6
  9. package/skills/teamsmith/references/migration.md +197 -47
  10. package/skills/teamsmith/references/protocol.md +201 -30
  11. package/skills/teamsmith/references/troubleshooting.md +130 -10
  12. package/skills/teamsmith/references/workflows.md +36 -2
  13. package/skills/teamsmith/scripts/lib/cmd-agents.sh +318 -23
  14. package/skills/teamsmith/scripts/lib/cmd-bootstrap.sh +2 -2
  15. package/skills/teamsmith/scripts/lib/cmd-config.sh +349 -67
  16. package/skills/teamsmith/scripts/lib/cmd-draft.sh +4 -4
  17. package/skills/teamsmith/scripts/lib/cmd-help.sh +677 -0
  18. package/skills/teamsmith/scripts/lib/cmd-init.sh +157 -5
  19. package/skills/teamsmith/scripts/lib/cmd-interrupt.sh +461 -0
  20. package/skills/teamsmith/scripts/lib/cmd-meeting.sh +2 -25
  21. package/skills/teamsmith/scripts/lib/cmd-project.sh +30 -103
  22. package/skills/teamsmith/scripts/lib/cmd-reap.sh +277 -0
  23. package/skills/teamsmith/scripts/lib/cmd-reply.sh +3 -3
  24. package/skills/teamsmith/scripts/lib/cmd-review.sh +182 -32
  25. package/skills/teamsmith/scripts/lib/cmd-status.sh +234 -21
  26. package/skills/teamsmith/scripts/lib/cmd-update.sh +36 -7
  27. package/skills/teamsmith/scripts/lib/cmd-watch.sh +564 -46
  28. package/skills/teamsmith/scripts/lib/common.sh +1004 -245
  29. package/skills/teamsmith/scripts/lib/gate-lock.sh +582 -0
  30. package/skills/teamsmith/scripts/panel/draft-send.sh +11 -1
  31. package/skills/teamsmith/scripts/panel/panel.js +211 -211
  32. package/skills/teamsmith/scripts/panel/src/App.tsx +58 -21
  33. package/skills/teamsmith/scripts/panel/src/clipboard.ts +12 -5
  34. package/skills/teamsmith/scripts/panel/src/compose.ts +6 -3
  35. package/skills/teamsmith/scripts/panel/src/data.ts +51 -5
  36. package/skills/teamsmith/scripts/panel/src/layout.ts +209 -69
  37. package/skills/teamsmith/scripts/panel/src/main.tsx +37 -8
  38. package/skills/teamsmith/scripts/panel/src/strings/en.ts +39 -29
  39. package/skills/teamsmith/scripts/panel/src/strings/zh.ts +41 -31
  40. package/skills/teamsmith/scripts/panel/src/types.ts +28 -3
  41. package/skills/teamsmith/scripts/replies.mjs +4 -1
  42. package/skills/teamsmith/scripts/shim/killall +10 -0
  43. package/skills/teamsmith/scripts/shim/pgrep +10 -0
  44. package/skills/teamsmith/scripts/shim/pidof +10 -0
  45. package/skills/teamsmith/scripts/shim/pkill +10 -0
  46. package/skills/teamsmith/scripts/shim/signal-gate +108 -31
  47. package/skills/teamsmith/scripts/shim/tmux +18 -0
  48. package/skills/teamsmith/scripts/strings/commands.tsv +86 -0
  49. package/skills/teamsmith/scripts/strings/help.en.tsv +503 -0
  50. package/skills/teamsmith/scripts/strings/help.zh.tsv +489 -0
  51. package/skills/teamsmith/scripts/team +19 -6
  52. package/skills/teamsmith/templates/AGENTS.section.md.tmpl +8 -1
  53. package/skills/teamsmith/templates/config.sh.tmpl +20 -1
  54. package/skills/teamsmith/templates/pm-prompt.md.tmpl +21 -0
  55. package/skills/teamsmith/tests/README.md +31 -0
  56. package/skills/teamsmith/tests/bg-ledger-harness.mjs +464 -40
  57. package/skills/teamsmith/tests/bg-ledger.sh +168 -10
  58. package/skills/teamsmith/tests/checkout-shape-probe.sh +83 -54
  59. package/skills/teamsmith/tests/config-cli.sh +184 -32
  60. package/skills/teamsmith/tests/fixtures/delivery-truth-real/scenario.sh +7 -1
  61. package/skills/teamsmith/tests/fixtures/p287/lint-good.sh.in +3 -0
  62. package/skills/teamsmith/tests/fixtures/p287/lint-hardcoded.sh.in +3 -0
  63. package/skills/teamsmith/tests/fixtures/p287/lint-no-ns.sh.in +3 -0
  64. package/skills/teamsmith/tests/fixtures/p55/flip-p49.sh +18 -6
  65. package/skills/teamsmith/tests/flip-m33.sh +5 -3
  66. package/skills/teamsmith/tests/flip-p158.sh +2 -1
  67. package/skills/teamsmith/tests/flip-p308-skip-not-pass.sh +151 -0
  68. package/skills/teamsmith/tests/flip-p310-lock.sh +210 -0
  69. package/skills/teamsmith/tests/flip-p310-socket.sh +125 -0
  70. package/skills/teamsmith/tests/gate-guard.sh +52 -79
  71. package/skills/teamsmith/tests/help-surface.sh +1664 -0
  72. package/skills/teamsmith/tests/install-shape.sh +359 -22
  73. package/skills/teamsmith/tests/key-replies-pty.sh +9 -10
  74. package/skills/teamsmith/tests/key-replies-ui.sh +2 -8
  75. package/skills/teamsmith/tests/key-replies.sh +281 -28
  76. package/skills/teamsmith/tests/ledger-ban.sh +5 -4
  77. package/skills/teamsmith/tests/ledger-claim.sh +2 -1
  78. package/skills/teamsmith/tests/lib/bounded-spin.sh +90 -0
  79. package/skills/teamsmith/tests/lib/model-policy.sh +4 -0
  80. package/skills/teamsmith/tests/lib/model-routing-flips.py +51 -0
  81. package/skills/teamsmith/tests/lib/pty-wait.sh +21 -111
  82. package/skills/teamsmith/tests/lib/reply-fixture.sh +4 -9
  83. package/skills/teamsmith/tests/lib/section-guard.sh +316 -164
  84. package/skills/teamsmith/tests/lib/tmp-root.sh +139 -27
  85. package/skills/teamsmith/tests/lib/tmux-cap.sh +10 -1
  86. package/skills/teamsmith/tests/lib/tmux-iso.sh +31 -13
  87. package/skills/teamsmith/tests/loop-inventory.tsv +83 -77
  88. package/skills/teamsmith/tests/panel-b2.sh +430 -352
  89. package/skills/teamsmith/tests/panel-b3-stub.sh +112 -4
  90. package/skills/teamsmith/tests/panel-b3.sh +314 -62
  91. package/skills/teamsmith/tests/panel-choices.sh +11 -6
  92. package/skills/teamsmith/tests/panel-cpu.sh +43 -130
  93. package/skills/teamsmith/tests/panel-p21.sh +251 -265
  94. package/skills/teamsmith/tests/panel-snapshots.sh +41 -225
  95. package/skills/teamsmith/tests/panel-strings.mjs +33 -18
  96. package/skills/teamsmith/tests/perf.sh +187 -396
  97. package/skills/teamsmith/tests/routes.sh +440 -117
  98. package/skills/teamsmith/tests/seat-interrupt.sh +1012 -0
  99. package/skills/teamsmith/tests/seat-visibility.sh +675 -0
  100. package/skills/teamsmith/tests/section-guard.sh +13 -102
  101. package/skills/teamsmith/tests/section-needs-audit.sh +24 -7
  102. package/skills/teamsmith/tests/section-paths.tsv +32 -18
  103. package/skills/teamsmith/tests/section-select.sh +371 -19
  104. package/skills/teamsmith/tests/signal-gate.sh +249 -27
  105. package/skills/teamsmith/tests/signal-lint-legacy.txt +4 -1
  106. package/skills/teamsmith/tests/slash-team-e2e.sh +323 -0
  107. package/skills/teamsmith/tests/slash-team.sh +617 -0
  108. package/skills/teamsmith/tests/slice-coverage.sh +206 -0
  109. package/skills/teamsmith/tests/smoke.sh +3595 -788
  110. package/skills/teamsmith/tests/task-model-routing.sh +530 -15
  111. package/skills/teamsmith/tests/team-bg-stop.sh +18 -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/tests/workflow-entry.sh +207 -0
  117. package/skills/teamsmith-init/SKILL.md +12 -12
  118. package/skills/teamsmith-init/references/bootstrap.md +3 -3
  119. package/skills/teamsmith/tests/flip-glue.sh +0 -162
  120. package/skills/teamsmith/tests/flip-m12-26c.sh +0 -129
  121. package/skills/teamsmith/tests/flip-m12.sh +0 -185
  122. package/skills/teamsmith/tests/flip-m16.sh +0 -166
  123. package/skills/teamsmith/tests/flip-m17.sh +0 -204
  124. package/skills/teamsmith/tests/flip-m34.sh +0 -96
  125. package/skills/teamsmith/tests/flip-m37.sh +0 -185
  126. package/skills/teamsmith/tests/flip-m4.3.sh +0 -375
  127. package/skills/teamsmith/tests/flip-m43.sh +0 -128
  128. package/skills/teamsmith/tests/flip-m45.sh +0 -244
  129. package/skills/teamsmith/tests/flip-m46.sh +0 -238
  130. package/skills/teamsmith/tests/flip-m48.sh +0 -304
  131. package/skills/teamsmith/tests/flip-m6.2.sh +0 -205
  132. package/skills/teamsmith/tests/flip-m6.3.sh +0 -283
  133. package/skills/teamsmith/tests/flip-m6.4.sh +0 -267
  134. package/skills/teamsmith/tests/flip-m6.5.sh +0 -113
  135. package/skills/teamsmith/tests/flip-m7.1.sh +0 -135
  136. package/skills/teamsmith/tests/flip-m7.2.sh +0 -321
  137. package/skills/teamsmith/tests/flip-p144.sh +0 -112
  138. package/skills/teamsmith/tests/flip-p156.sh +0 -133
  139. package/skills/teamsmith/tests/flip-p160.sh +0 -182
  140. package/skills/teamsmith/tests/flip-p171.sh +0 -249
  141. package/skills/teamsmith/tests/flip-p18.1.sh +0 -95
  142. package/skills/teamsmith/tests/flip-p20.sh +0 -286
  143. package/skills/teamsmith/tests/flip-p201.sh +0 -249
  144. package/skills/teamsmith/tests/flip-p210.sh +0 -239
  145. package/skills/teamsmith/tests/flip-p211.sh +0 -152
  146. package/skills/teamsmith/tests/flip-p217.sh +0 -288
  147. package/skills/teamsmith/tests/flip-p22.sh +0 -123
  148. package/skills/teamsmith/tests/flip-p23.sh +0 -239
  149. package/skills/teamsmith/tests/flip-p25.sh +0 -165
  150. package/skills/teamsmith/tests/flip-p71.sh +0 -184
  151. package/skills/teamsmith/tests/flip-p72.sh +0 -202
  152. package/skills/teamsmith/tests/load-experiment.sh +0 -290
  153. package/skills/teamsmith/tests/panel-cpu-premise.sh +0 -173
  154. package/skills/teamsmith/tests/panel-flip-m54.sh +0 -207
  155. package/skills/teamsmith/tests/panel-flip-p123.sh +0 -278
  156. package/skills/teamsmith/tests/panel-flip-p125.sh +0 -318
  157. package/skills/teamsmith/tests/panel-flip-p128.sh +0 -259
  158. package/skills/teamsmith/tests/panel-keyprobe-stub.sh +0 -41
  159. package/skills/teamsmith/tests/panel-keyprobe.sh +0 -82
  160. package/skills/teamsmith/tests/panel-knobs.sh +0 -83
  161. package/skills/teamsmith/tests/section-budgets.tsv +0 -154
  162. package/skills/teamsmith/tests/team-bg-flip.sh +0 -239
  163. package/skills/teamsmith/tests/team-inbox-watch-flip.sh +0 -191
@@ -3,6 +3,15 @@
3
3
  #
4
4
  # 设计要点(来自 CEP 的教训):agent 的自述不算证据,PM 必须在**独立 worktree** 上
5
5
  # checkout 该分支、跑门禁、看 diff,并把结论写成 <docs>/reviews/<ID>.md。
6
+ #
7
+ # P281(change: gate-lock-hygiene):门禁共享锁的记录格式与读写是 `lib/gate-lock.sh` 里的**唯一**
8
+ # 实现(`tests/smoke.sh` 共用同一份)。它不叫 `cmd-*.sh`(不是一条命令),所以不在 `scripts/team`
9
+ # 的 `lib/cmd-*.sh` 装载循环里 —— 这里显式装。
10
+ REVIEW_LIB_DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd || printf '%s' "$SELF_DIR/lib")"
11
+ if [ -z "${GATE_LOCK_LIB:-}" ]; then
12
+ # shellcheck source=lib/gate-lock.sh
13
+ . "$REVIEW_LIB_DIR/gate-lock.sh"
14
+ fi
6
15
 
7
16
  # 解析分支:--branch > 任务在跑的 agent 的当前分支 > 唯一的 task/<ID>-* 分支
8
17
  team__resolve_branch() { # <ID> → _R;rc:0=唯一命中 1=找不到 2=歧义。M50 进程内变体(永不 team_die)
@@ -369,7 +378,8 @@ team_review_postmerge_merge3() { # <base> <branch> <path> → stdout 三路合
369
378
  # 两边都动过、blob 历史又都判不出来时,用 git 自己的合并引擎当裁判(不用自己的行集合启发式:
370
379
  # 结果 == main = main 已含分支的改动;== 分支 = 分支更新;冲突/第三个结果 = 不确定。
371
380
  local base="$1" branch="$2" path="$3" root="$TEAM_MAIN_ROOT" d rc=0
372
- d="$(mktemp -d "${TMPDIR:-/tmp}/p91-merge3.XXXXXX")" || return 1
381
+ team_tmp_root_prepare >/dev/null 2>&1 || true
382
+ d="$(mktemp -d "$(team_tmp_root)/teamsmith-p91-merge3-$(team_project_ns).XXXXXX")" || return 1
373
383
  git -C "$root" show "$TEAM_PROTECTED_BRANCH:$path" > "$d/main" 2>/dev/null || : > "$d/main"
374
384
  git -C "$root" show "$base:$path" > "$d/base" 2>/dev/null || : > "$d/base"
375
385
  git -C "$root" show "$branch:$path" > "$d/branch" 2>/dev/null || : > "$d/branch"
@@ -1024,6 +1034,7 @@ $(printf '%s\n' "$ignored_list" | head -5)
1024
1034
  # G1(P26):门禁锁的排队阶段与记账。queue_state: none(门禁没跑)/ held(祖先持有)/ queued(本
1025
1035
  # 次自己排队)/ noqueue(没有 flock,降级)/ cap(排队超上限,门禁没跑)。
1026
1036
  local queue_state="none" queue_marker="" queued_elapsed="" queue_holder="" used_lock="" lock_cap=""
1037
+ local lock_note=""
1027
1038
  if [ "$no_gates" = "1" ]; then
1028
1039
  verdict="SKIPPED"; gates_marker="none"
1029
1040
  gates_out="(--no-gates:PM 选择人工看 diff;**没有跑过任何门禁**,本记录不是 PASS 证据)"
@@ -1059,20 +1070,49 @@ $(printf '%s\n' "$ignored_list" | head -5)
1059
1070
  # (smoke.sh 的 M23 互斥)—— 那一轮排了 930s、跑 870s,硬超时在 28-i 段掐断的是**运行**,
1060
1071
  # 记录却写成 TIMEOUT,而同一个 HEAD 在机器安静后是 PASS 的。硬超时只该量运行:排队是 review
1061
1072
  # 自己的、有上限、可记账的阶段。
1062
- # held —— 环境里已经有 SMOKE_LOCK_WRAPPED=1(祖先持有锁):子树已被串行化,不再二次排队
1063
- # (否则每一层 team review 都会和自己的祖先死锁)。
1073
+ # held —— **证明得出**「某个祖辈正握着这把锁」:沿进程树 + starttime 逐层核对记录(P281 的
1074
+ # gate_lock_prove_ancestor_pair),子树已被串行化,不再二次排队(否则每一层 team review
1075
+ # 都会和自己的祖先死锁)。
1076
+ # P310(回归):这里以前看 `SMOKE_LOCK_WRAPPED` 这个**可继承的环境标记**,而 P288
1077
+ # (6f1ed69d,F4 的正确修法)让套件拿到锁后 unset 了它 —— 于是嵌套 review 认不出
1078
+ # 「祖辈正握着锁」,反向排到祖先前头(等满上限 / 段预算超时)。判断改成**读证明**,
1079
+ # 不把标记恢复成可继承(写权仍只认记录里活着的 pid,F4 不放宽)。
1064
1080
  # noqueue —— 没有 flock:打印降级,照跑(**不是**静默跳过;见 references/protocol.md §9b)。
1065
1081
  # 其它用具:TEAM_SMOKE_LOCK(路径,默认同 smoke)、TEAM_SMOKE_LOCK_WAIT(上限,默认 1800s)。
1066
- local lock="${TEAM_SMOKE_LOCK:-${TMPDIR:-/tmp}/teamsmith-smoke.lock}"
1082
+ # P287(change: gate-lock-external-deletion):与 smoke.sh **同一套**锁机器 ——
1083
+ # ① 取两把锁、顺序固定(发布路径 = 兼容声明 → 伴生锁 = 运行互斥;同行同目录、`.machine`、点开头);
1084
+ # ② 记录写在**伴生锁**边上(`<companion>.holder`),由**取锁的那个进程**(本 shell)写,含
1085
+ # `lock_identity=fd` 与 `lock_dev_ino=<dev:ino>`;取锁时刷两把锁的 mtime;
1086
+ # ③ 有界轮询(每轮短 `flock -n`、每轮重新解析路径、每轮重算上限)—— 我再等的那把被换掉会印一行;
1087
+ # ④ 入场检查:拿到锁后读 `<companion>.runs/*`,活的 `top` 登记记着另一个身份 ⇒ 互斥已断 ⇒
1088
+ # **不跑门禁**、判定 FAIL(`gates_marker=mutexbroken`,与 `queuecap` 同形:门禁没有运行)。
1089
+ # ⑤ 门禁跑完后复核身份:运行期间互斥被破坏 ⇒ 这次判定不可信 ⇒ FAIL(同样点名)。
1090
+ # 上限算式、marker 语义(P66)、超限与降级的文案/退出码**一字不改**。
1091
+ # P309:锁的发布路径**不搬**(P287 的跨版本串行)—— 只从它的唯一实现处读。
1092
+ local lock; lock="$(gate_lock_default_path)"
1093
+ local lock_comp; lock_comp="$(gate_lock_companion_path "$lock")"
1094
+ local lock_dev_ino="" lock_cap_hit=0 lock_broken="" gate_rc=0
1067
1095
  local queue_started gate_end marker_epoch
1096
+ # P281:排队上限不再是「一律 W」——它跟着**持有者的心跳**(change: gate-waiter-patience · P338):
1097
+ # 心跳还在动(任一活登记公布的心跳文件 < W 秒前更新)→ 4×W(硬顶);
1098
+ # 心跳停了 / 没有可用心跳(旧记录、`heartbeat=-`、文件不在)→ W(与今天一字不改)。
1099
+ # 每轮**现算**(不缓存排队时的读数):心跳停了,上限当轮就落回 W。
1100
+ # `expected`/`bound` 仍写在记录里、仍被点名,但**不再决定耐心**(P316 之后没有生产者)。
1101
+ local lock_wait lock_pat lock_silent
1068
1102
  lock_cap="${TEAM_SMOKE_LOCK_WAIT:-1800}"
1069
1103
  case "$lock_cap" in ''|*[!0-9]*) lock_cap=1800 ;; esac
1104
+ lock_wait="$lock_cap"
1070
1105
  queue_started="$(date +%s)"
1071
- queue_marker="$(mktemp "${TMPDIR:-/tmp}/teamsmith-review-queue.XXXXXX" 2>/dev/null || true)"
1072
- queue_holder="$(cat "$lock.holder" 2>/dev/null || printf '持有者未知')"
1073
- if [ "${SMOKE_LOCK_WRAPPED:-0}" = "1" ]; then
1106
+ local lock_record_written=0
1107
+ team_tmp_root_prepare >/dev/null 2>&1 || true
1108
+ queue_marker="$(mktemp "$(team_tmp_root)/teamsmith-review-queue.$(team_project_ns).XXXXXX" 2>/dev/null || true)"
1109
+ queue_holder="$(gate_lock_describe_pair "$lock")"
1110
+ # P310:祖先持锁的判据 = **进程树 + starttime 的证明**(不是环境里一个可继承的标记)
1111
+ local lock_ancestor=""
1112
+ lock_ancestor="$(gate_lock_prove_ancestor_pair "$lock" 2>/dev/null || true)"
1113
+ if [ -n "$lock_ancestor" ]; then
1074
1114
  queue_state="held"
1075
- team_info "门禁锁:已由祖先持有(SMOKE_LOCK_WRAPPED=1)→ 本次不排队"
1115
+ team_info "门禁锁:已由祖辈 pid=${lock_ancestor} 持有(进程树 + starttime 证明)→ 本次不排队"
1076
1116
  elif ! command -v flock >/dev/null 2>&1; then
1077
1117
  queue_state="noqueue"
1078
1118
  team_warn "本机没有 flock → 门禁排队未启用(并发两套门禁时可能互相干扰;见 references/protocol.md §9b)"
@@ -1081,36 +1121,119 @@ $(printf '%s\n' "$ignored_list" | head -5)
1081
1121
  team_warn "锁文件 $lock 建不了 → 门禁排队未启用(并发两套门禁时可能互相干扰)"
1082
1122
  else
1083
1123
  queue_state="queued"
1084
- # `flock -n` 返回 1 = 锁被别人持有(expected,不是错误)——CLI 是 `set -euo pipefail`,
1085
- # 裸跑一条会返回 1 的命令会当场把整个 review 打断(实测:改完第一版 review 在零秒里静默退出)。
1086
- local lock_probe=0
1087
- flock -n "$lock" true 2>/dev/null || lock_probe=$?
1088
- if [ "$lock_probe" -eq 1 ]; then
1089
- team_info "门禁锁正被持有(${queue_holder});排队,最多等 ${lock_cap}s(TEAM_SMOKE_LOCK_WAIT 可调)"
1090
- elif [ "$lock_probe" -gt 1 ]; then
1091
- team_warn "flock 探锁失败(rc=${lock_probe})→ 仍按排队路径走(拿不到锁时会报排队超限)"
1124
+ # 有界轮询取**两把**锁(P287)。marker 文件在排队**之前**就存在、但**只在真的拿到锁时才写入**
1125
+ # (与 flock 包装器同形):没写 = 这套没跑起来(排队被上限掐掉 / 取锁起不来)。
1126
+ local lock_probe=0 lock_reported=0 lock_waited_path="" lock_waited_id="" lock_probe_id=""
1127
+ local lock_role=""
1128
+ while :; do
1129
+ lock_probe=0
1130
+ gate_lock_acquire_pair "$lock" || lock_probe=$?
1131
+ [ "$lock_probe" = "0" ] && break
1132
+ if [ "$lock_probe" = "2" ]; then
1133
+ queue_state="noqueue"
1134
+ team_warn "锁文件 $lock 建不了 → 门禁排队未启用(并发两套门禁时可能互相干扰)"
1135
+ break
1136
+ fi
1137
+ # 我上一轮等的那把还在吗?换过就印一行(两个身份),再按路径重新解析(P287)
1138
+ if [ -n "$lock_waited_path" ]; then
1139
+ lock_probe_id="$(gate_lock_path_identity "$lock_waited_path" 2>/dev/null || true)"
1140
+ if [ "$lock_probe_id" != "$lock_waited_id" ]; then
1141
+ team_warn "我等的那把锁($lock_waited_path)已经变了 —— 旧身份 ${lock_waited_id:-(已不在)},现在 ${lock_probe_id:-(已不在)};按路径重新解析后继续等"
1142
+ fi
1143
+ lock_waited_id="$lock_probe_id"
1144
+ fi
1145
+ # 耐心跟着持有者的心跳(change: gate-waiter-patience · P338):心跳还在动 → 4W,停了 / 没有
1146
+ # 可用心跳(旧记录、`heartbeat=-`、文件不在)→ W(与今天一字不改)。**每轮现算**,不缓存。
1147
+ IFS=$'\t' read -r lock_cap lock_pat lock_silent _ \
1148
+ < <(gate_lock_patience_pair "$lock" "$lock_wait")
1149
+ case "$lock_cap" in ''|*[!0-9]*) lock_cap="$lock_wait" ;; esac
1150
+ lock_note="$(gate_lock_patience_note "$lock_pat" "$lock_silent")"
1151
+ if [ "$lock_reported" = "0" ]; then
1152
+ lock_reported=1
1153
+ lock_role="$([ "$lock_probe" = "1" ] && printf '发布路径(兼容声明)' || printf '伴生锁(运行互斥)')"
1154
+ queue_holder="$(gate_lock_describe_pair "$lock")"
1155
+ team_info "门禁锁正被持有(${queue_holder});排队,最多等 ${lock_cap}s(${lock_note};下限 TEAM_SMOKE_LOCK_WAIT=${lock_wait}s,上限 4×=$(( lock_wait * 4 ))s;TEAM_SMOKE_LOCK_WAIT 可调)"
1156
+ fi
1157
+ if [ "$lock_probe" = "1" ]; then lock_waited_path="$lock"; else lock_waited_path="$lock_comp"; fi
1158
+ [ -n "$lock_waited_id" ] || lock_waited_id="$(gate_lock_path_identity "$lock_waited_path" 2>/dev/null || true)"
1159
+ if [ "$(( $(date +%s) - queue_started ))" -ge "$lock_cap" ]; then
1160
+ lock_cap_hit=1; gate_rc=1
1161
+ break
1162
+ fi
1163
+ sleep 1
1164
+ done
1165
+ if [ "$lock_cap_hit" = "0" ] && [ "$queue_state" = "queued" ]; then
1166
+ export SMOKE_LOCK_WRAPPED=1
1167
+ export TEAM_SMOKE_LOCK="$lock"
1168
+ used_lock="$lock"
1169
+ # 记录由**取锁的进程**(本 shell)写:字段与 smoke.sh 同形 + 身份(P287 §3.1)
1170
+ touch "$lock" "$lock_comp" 2>/dev/null || true # P255 §1.6:按 age 的清理器不该删活锁
1171
+ IFS=$'\t' read -r lock_identity lock_dev_ino < <(gate_lock_claim_identity "$lock_comp")
1172
+ export GATE_KIND=top GATE_CMD="team review $id" GATE_NOLOCK=0 GATE_ANCESTOR=-
1173
+ export GATE_AGENT="$(gate_lock_seat_of_dir "${TEAM_CWD:-$PWD}")" GATE_WT="$(gate_lock_worktree_of_dir "${TEAM_CWD:-$PWD}")"
1174
+ export GATE_STARTED="$(date +%s)" GATE_HB=-
1175
+ IFS=$'\t' read -r GATE_EXP GATE_BND GATE_SECS \
1176
+ < <(gate_lock_expected "${TEAM_SKILL_DIR:-$SELF_DIR/..}/tests/smoke.sh" "${TEAM_SKILL_DIR:-$SELF_DIR/..}/tests/section-budgets.tsv")
1177
+ export GATE_EXP GATE_BND GATE_SECS
1178
+ if gate_lock_holder_claimable "$lock_comp" "$$"; then
1179
+ # `write_holder` 会写 <lock>.holder —— 记录**绝不**写进锁文件本身(锁文件的内容永远不被写:
1180
+ # 它是 flock 的对象、身份由 inode 认;写它 = 换 inode = 自己把自己的互斥判成「已替换」)
1181
+ gate_lock_write_holder "$lock_comp" "$$" top "team review $id" "$GATE_AGENT" "$GATE_WT" \
1182
+ "$GATE_STARTED" "$GATE_EXP" "$GATE_BND" "$GATE_SECS" 0 - - "$lock_identity" "$lock_dev_ino" \
1183
+ && lock_record_written=1
1184
+ else
1185
+ team_warn "拿到锁了,但 $(gate_lock_holder_file "$lock_comp") 里的持有记录是**另一个活着的进程**($(gate_lock_describe_one "$(gate_lock_holder_file "$lock_comp")"))→ 不覆写它"
1186
+ fi
1187
+ # marker:真的拿到锁、接下来跑门禁(P66 语义;`<epoch> <pid>` 与 smoke.sh 同形)
1188
+ printf '%s %s\n' "$(date +%s)" "$$" > "$queue_marker" 2>/dev/null || true
1189
+ # 入场检查(P287 §4.2):活的 top 登记记着另一个身份 ⇒ 互斥已断 ⇒ 不跑门禁
1190
+ local entry_chain entry_msg
1191
+ entry_chain="$(gate_lock_ancestry_chain "$$" | tr '\n' ' ')"
1192
+ entry_msg="$(gate_lock_entry_conflict "$lock_comp" "$lock_dev_ino" "$$" "$entry_chain")"
1193
+ if [ -n "$entry_msg" ]; then
1194
+ lock_broken="入场检查:$entry_msg"
1195
+ team_err "入场检查:$entry_msg → 本次**不跑门禁**(互斥已经断了;这不是对代码的判定)"
1196
+ : > "$queue_marker" 2>/dev/null || true # marker 清空 = 这套没有跑起来(与排队超限同形)
1197
+ gate_rc=2
1198
+ fi
1092
1199
  fi
1093
- # 用 flock --close 包住「写 marker + 写持有者 + 跑门禁」整段:--close 让门禁的子孙拿不到锁 fd
1094
- # (M23 的漏锁教训:夹具留下的后台进程继承 fd,脚本退出后锁还挂着)。marker 是「真的拿到锁」
1095
- # 的唯一证据 —— 没有它就只有两种情况:排队被上限掐掉,或 flock 起不来。
1096
- export SMOKE_LOCK_WRAPPED=1
1097
- export TEAM_SMOKE_LOCK="$lock"
1098
- used_lock="$lock"
1200
+ # P281:持有者记录由**真的拿到锁的那个进程**写(就是下面那个 bash -c,拿到锁之后、跑门禁之前),
1201
+ # 字段与 smoke.sh 同形。行格式只在 gate-lock.sh 里定义一次:包装器 . 上那个库、调同一个写函数
1202
+ # (`start=` 是**它自己**的 starttime,所以必须在包装器里算,不能在 review 进程里预算)。
1203
+ export GATE_KIND=top GATE_CMD="team review $id" GATE_NOLOCK=0 GATE_ANCESTOR=-
1204
+ export GATE_AGENT="$(gate_lock_seat_of_dir "${TEAM_CWD:-$PWD}")" GATE_WT="$(gate_lock_worktree_of_dir "${TEAM_CWD:-$PWD}")"
1205
+ export GATE_STARTED="$(date +%s)" GATE_HB=-
1206
+ IFS=$'\t' read -r GATE_EXP GATE_BND GATE_SECS \
1207
+ < <(gate_lock_expected "${TEAM_SKILL_DIR:-$SELF_DIR/..}/tests/smoke.sh" "${TEAM_SKILL_DIR:-$SELF_DIR/..}/tests/section-budgets.tsv")
1208
+ export GATE_EXP GATE_BND GATE_SECS
1099
1209
  fi
1100
- local gate_rc=0 gate_started grace deadline_min
1101
- local QUEUE_WRAP='marker="$1"; holder="$2"; id="$3"; shift 3; date +%s > "$marker"; printf "%s pid=%s cmd=team review %s\n" "$(date -Is)" "$$" "$id" > "$holder" 2>/dev/null || true; exec "$@"'
1210
+ local gate_started grace deadline_min
1102
1211
  local -a gate_cmd=()
1103
- if [ "$queue_state" = "queued" ]; then
1104
- gate_cmd=(flock --close -w "$lock_cap" "$lock" bash -c "$QUEUE_WRAP" _ "$queue_marker" "$lock.holder" "$id")
1105
- fi
1212
+ # P287:锁由本 shell 自己握着(fd 8/9)—— 门禁**及其子孙**用 `8>&- 9>&-` 把这两个 fd 关掉,
1213
+ # 等价于原来 `flock --close` 的那条保证(M23 的漏锁教训:夹具留下的后台进程继承 fd,脚本退出后
1214
+ # 锁还挂着)。入场检查已经拒绝时(lock_broken 非空)不跑门禁。
1106
1215
  gate_cmd+=(${runner[@]+"${runner[@]}"} env ${gate_env[@]+"${gate_env[@]}"} bash -c "$TEAM_GATES")
1107
1216
  # M25-②:门禁的 stdin 必须是 /dev/null,**不能**是调用者的 tty。实测(tests/smoke.sh 6i-b):
1108
1217
  # 后台进程组里的子进程一读 tty 就吃 SIGTTIN 被**停住**(STAT=TN,0% CPU,看着像挂死;
1109
1218
  # 登录 profile 里的 host-spawn 就会碰 tty)。复验常在后台窗口里跑,门禁不能依赖 tty。
1110
- ( cd "$revdir" && "${gate_cmd[@]}" ) > "$log" 2>&1 < /dev/null || gate_rc=$?
1219
+ if [ -n "$lock_broken" ] || [ "$lock_cap_hit" = "1" ]; then
1220
+ # 入场检查拒绝(互斥已断)或排队撞上限:**门禁不跑**(判定与记账在各分支里)
1221
+ : > "$log"
1222
+ [ -n "$lock_broken" ] && queue_state="broken"
1223
+ else
1224
+ ( cd "$revdir" && "${gate_cmd[@]}" 8>&- 9>&- ) > "$log" 2>&1 < /dev/null || gate_rc=$?
1225
+ fi
1226
+ # P287:收尾只删**自己**写的持有者记录(锁文件永不 unlink;别人的活记录绝不碰)——
1227
+ # 留下一条陈旧记录会把后来者的排队上限与点名带偏(34②/34⑦ 实测过)。
1228
+ if [ "$lock_record_written" = "1" ] && gate_lock_holder_claimable "$lock_comp" "$$"; then
1229
+ gate_lock_remove_holder "$lock_comp"
1230
+ rmdir "$lock_comp.runs" 2>/dev/null || true
1231
+ fi
1111
1232
  gate_end="$(date +%s)"
1112
1233
  if [ -n "$queue_marker" ] && [ -s "$queue_marker" ]; then
1113
1234
  marker_epoch="$(cat "$queue_marker" 2>/dev/null)"
1235
+ # P281:marker 现在是 `<epoch> <pid>`(与 smoke.sh 同一约定)—— 只取第一个字段当时刻
1236
+ marker_epoch="${marker_epoch%% *}"
1114
1237
  case "$marker_epoch" in ''|*[!0-9]*) marker_epoch="$(date +%s)" ;; esac
1115
1238
  queued_elapsed=$(( marker_epoch - queue_started ))
1116
1239
  [ "$queued_elapsed" -lt 0 ] && queued_elapsed=0
@@ -1120,16 +1243,25 @@ $(printf '%s\n' "$ignored_list" | head -5)
1120
1243
  fi
1121
1244
  # 排队超上限:flock 没拿到锁(没有 marker)→ 门禁**没有运行**。判定 FAIL 而不是 TIMEOUT:
1122
1245
  # 「没跑」和「跑了被杀」必须能分开(记录里点名持锁者与上限)。
1123
- if [ "$queue_state" = "queued" ] && [ -z "$(cat "$queue_marker" 2>/dev/null)" ]; then
1246
+ if [ "$queue_state" = "broken" ]; then
1247
+ verdict="FAIL"
1248
+ gates_marker="mutexbroken"
1249
+ queue_holder="$(gate_lock_describe_pair "$lock")"
1250
+ gates_out="(互斥已断:**门禁没有运行**。${lock_broken}。实际持有者(读伴生锁边上的记录):${queue_holder}。这不是对代码的判定 —— 等那个运行结束后重跑。)"
1251
+ gate_elapsed=""
1252
+ used_timeout=0
1253
+ used_lock="$lock"
1254
+ elif [ "$queue_state" = "queued" ] && [ -z "$(cat "$queue_marker" 2>/dev/null)" ]; then
1124
1255
  verdict="FAIL"
1125
1256
  gates_marker="queuecap"
1126
1257
  queued_elapsed=$(( gate_end - queue_started ))
1127
1258
  [ "$queued_elapsed" -lt 0 ] && queued_elapsed=0
1128
- gates_out="(门禁锁排队超过上限 ${lock_cap}s:**门禁没有运行**。持锁者:${queue_holder}。这不是对代码的判定 —— 等持锁者结束,或抬 TEAM_SMOKE_LOCK_WAIT 后重跑。)"
1259
+ queue_holder="$(gate_lock_describe_pair "$lock")" # 撞上限时**重读**:等的这段时间持有者可能换了
1260
+ gates_out="(门禁锁排队超过上限 ${lock_cap}s:**门禁没有运行**。持锁者:${queue_holder}。${lock_note}这不是对代码的判定 —— 等持锁者结束,或抬 TEAM_SMOKE_LOCK_WAIT 后重跑。)"
1129
1261
  gate_elapsed=""
1130
1262
  used_timeout=0
1131
1263
  used_lock="$lock"
1132
- team_err "门禁锁排队超过上限 ${lock_cap}s → 门禁没有运行(持锁者:${queue_holder})"
1264
+ team_err "门禁锁排队超过上限 ${lock_cap}s → 门禁没有运行(持锁者:${queue_holder};${lock_note})"
1133
1265
  fi
1134
1266
  grace="${TEAM_REVIEW_TIMEOUT_GRACE:-2}"
1135
1267
  case "$grace" in ''|*[!0-9]*) grace=2 ;; esac
@@ -1160,7 +1292,22 @@ $(printf '%s\n' "$ignored_list" | head -5)
1160
1292
  esac
1161
1293
  fi
1162
1294
  fi
1295
+ # P287 §3.2 的等价物(review 侧):门禁跑完后复核「我还握着入场时那个 inode 吗」——
1296
+ # 运行期间锁被外部删/换 ⇒ 这次判定不可信(与哨兵同一判据、同一措辞);正常时一行不加。
1297
+ if [ "$queue_state" = "queued" ] && [ -n "$lock_dev_ino" ]; then
1298
+ local lock_now
1299
+ lock_now="$(gate_lock_snapshot "$lock_comp" "$lock_identity" "$lock_dev_ino" "$$")"
1300
+ case "$lock_now" in
1301
+ ok|'') ;; # 完好;或读不出来(/proc 不在等)—— 不误报
1302
+ *) lock_broken="$(printf '%s' "$lock_now" | cut -s -f1) 现场:$(printf '%s' "$lock_now" | cut -s -f2-)"
1303
+ team_err "门禁运行期间互斥被破坏($(printf '%s' "$lock_now" | tr '\t' ' '))→ 这次判定不可信,判 FAIL"
1304
+ verdict="FAIL"; gates_marker="mutexbroken" ;;
1305
+ esac
1306
+ fi
1163
1307
  gates_out="$(tail -25 "$log")"
1308
+ if [ -n "$lock_broken" ] && [ "$queue_state" = "queued" ]; then
1309
+ gates_out="(互斥在运行期间被破坏,这次判定不可信;判 FAIL。现场:${lock_broken})$(printf '\n')${gates_out}"
1310
+ fi
1164
1311
  team_ok "门禁输出:${log#"$TEAM_MAIN_ROOT"/}(${verdict},${gate_elapsed}s)"
1165
1312
  fi
1166
1313
 
@@ -1197,6 +1344,9 @@ $(printf '%s\n' "$ignored_list" | head -5)
1197
1344
  [ "$unresolved_override" = "1" ] && head_flags="$head_flags · branch-unresolved (override)"
1198
1345
  [ "$gates_marker" = "none" ] && head_flags="$head_flags · gates: none"
1199
1346
  [ "$gates_marker" = "queuecap" ] && head_flags="$head_flags · gate queue cap exceeded(门禁没跑)"
1347
+ # P287:互斥被破坏(入场检查拒绝 → 门禁没跑;或跑完复核发现锁没了 → 判定不可信)。抬头里点名,
1348
+ # 与 `queuecap` 同形 —— 这条也必须是机器可读的。
1349
+ [ "$gates_marker" = "mutexbroken" ] && head_flags="$head_flags · gate mutex broken(互斥被破坏:判定不可信)"
1200
1350
  # 被信号终止(不是超时):把信号名写进抬头,别拿「超时」顶替「被杀」(M6.5 / F15)
1201
1351
  [ -n "$gate_signal" ] && head_flags="$head_flags · gate killed: $gate_signal"
1202
1352
  {
@@ -1219,7 +1369,7 @@ $(printf '%s\n' "$ignored_list" | head -5)
1219
1369
  else
1220
1370
  printf -- '- 门禁锁:`%s`;本次排队 %ss(上限 %ss,持有者:%s)\n' "${used_lock:-$lock}" "$q_acc" "$lock_cap" "${queue_holder:-未知}"
1221
1371
  fi ;;
1222
- held) printf -- '- 门禁锁:`%s`;**已由祖先持有**(SMOKE_LOCK_WRAPPED=1)→ 本次不排队(queued=0s;避免与自己祖先死锁)\n' "${used_lock:-$lock}" ;;
1372
+ held) printf -- '- 门禁锁:`%s`;**已由祖先持有**(进程树 + starttime 证明:祖辈 pid=%s)→ 本次不排队(queued=0s;避免与自己祖先死锁)\n' "${used_lock:-$lock}" "${lock_ancestor:--}" ;;
1223
1373
  noqueue) printf -- '- 门禁锁:**排队未启用**(本机无 flock 或锁文件建不了)→ 并发两套门禁时可能互相干扰\n' ;;
1224
1374
  *) ;;
1225
1375
  esac