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
@@ -0,0 +1,582 @@
1
+ #!/usr/bin/env bash
2
+ # gate-lock.sh — 门禁共享锁的**记录格式**与它的唯一读写实现(change: gate-lock-hygiene · 实现:P281)
3
+ #
4
+ # 被 `tests/smoke.sh` 与 `scripts/lib/cmd-review.sh` source(两者共用同一份实现;格式常量只在这里
5
+ # 定义一次)。契约文本在 `openspec/specs/verification/spec.md`:
6
+ # * The gate lock's holder record names its kind, its owner, its elapsed time and its bound
7
+ # * A nested run does not queue on the machine lock, and its work stays inside the section that spawned it
8
+ # * The hard timeout covers the gate run, not the queue(排队上限跟随持有者心跳)
9
+ #
10
+ # 两个位置、一个写者规则:
11
+ # `<lock>.holder` 只有**真的持有 flock 的那个进程**写它、也只有它删它(收尾时)。
12
+ # `<lock>.runs/<pid>.run` **每个**参与的运行登记自己的一条(顶层与嵌套),退出时删自己那条。
13
+ # 为什么不是单文件追加:多个运行同时写同一个文件会互相覆写/删除(EXP1 的病);每运行一条文件天然
14
+ # 无竞争,等待者 glob 一次读全。
15
+ #
16
+ # 记录格式(两处同形,制表符分隔;**值里不许有空白**,解析按空白切):
17
+ # <ISO-8601> pid=<pid> start=<进程 starttime> cmd=<命令名> kind=<top|nested>
18
+ # agent=<席位> worktree=<绝对路径> started=<epoch> expected=<Σband_s> bound=<Σbudget_s>
19
+ # sections=<段数> no_lock=<0|1> ancestor=<pid|-> heartbeat=<section-guard 心跳路径|->
20
+ # lock_identity=<fd|path|-> lock_dev_ino=<dev:ino|-> (P287:持有者记下自己握的那把锁的身份)
21
+ # 旧形状(`<ISO> pid=<pid> cmd=<...>`,空格分隔)照旧可读:缺的字段按「不知道」处理(打印 `?`,
22
+ # 不编)。`start` 是进程 starttime —— pid 复用安全的判据(`pid` 活着且 starttime 仍是那一次才算它)。
23
+ #
24
+ # 读取:glob + pid/starttime 过滤。已死但没清掉的条目打印 `stale` 并**忽略**(等待者不去删别人的
25
+ # 文件:一处误判就可能删掉别人正在跑的东西)。席位/工作树按 `team notify` 发送者的同一条规则
26
+ # (运行时目录,名册拼写优先,**不取窗口名**;推导本体是 common.sh 的 team_seat_from_paths,只此
27
+ # 一份 —— 本库只供给 git 上下文);解析不出来就写 `?`——如实说不知道(记录必须写成功)。
28
+ #
29
+ # `/proc` 只用于 pid 复用安全与祖辈证明:没有 `/proc` → 证明不成立 → fail closed(一律按顶层)。
30
+
31
+ GATE_LOCK_LIB=1
32
+
33
+ # ---------------------------------------------------------------- 时间 / 进程
34
+ gate_lock_now() { date +%s; }
35
+
36
+ gate_lock_proc_start() { # <pid> → /proc/<pid>/stat 的 starttime(pid 复用安全的判据)
37
+ local st; st="$(LC_ALL=C cat "/proc/$1/stat" 2>/dev/null)" || return 1
38
+ st="${st##*\) }"; set -- $st
39
+ [ -n "${20:-}" ] || return 1
40
+ printf '%s\n' "${20}"
41
+ }
42
+
43
+ gate_lock_ppid() { # <pid> → ppid
44
+ local st; st="$(LC_ALL=C cat "/proc/$1/stat" 2>/dev/null)" || return 1
45
+ st="${st##*\) }"; set -- $st
46
+ [ -n "${2:-}" ] || return 1
47
+ printf '%s\n' "$2"
48
+ }
49
+
50
+ gate_lock_proc_zombie() { # <pid> → 0 = 存在且状态是 Z(僵尸:已经跑完了,只等被 wait 收尸)
51
+ # 僵尸不持有任何东西。不认它会把「一次 SIGKILL 留下的僵尸记录」当成活着的持有者,
52
+ # 于是后来的**合法**持有者会被 gate_lock_holder_claimable 挡在门外(记录永远写不上去)。
53
+ local raw
54
+ raw=""; { IFS= read -r raw < "/proc/$1/stat"; } 2>/dev/null || raw=""
55
+ raw="${raw##*\) }"; case "${raw%% *}" in Z) return 0 ;; esac
56
+ return 1
57
+ }
58
+
59
+ gate_lock_pid_alive() { # <pid> [<starttime>] → 0 = 活着(非僵尸)且(给了 starttime 时)还是那一次
60
+ local pid="${1:-}" want="${2:-}" st
61
+ case "$pid" in ''|*[!0-9]*) return 1 ;; esac
62
+ [ "$pid" -gt 1 ] || return 1
63
+ kill -0 "$pid" 2>/dev/null || return 1
64
+ gate_lock_proc_zombie "$pid" && return 1
65
+ if [ -n "$want" ]; then
66
+ st="$(gate_lock_proc_start "$pid" 2>/dev/null || true)"
67
+ [ "$st" = "$want" ] || return 1
68
+ fi
69
+ return 0
70
+ }
71
+
72
+ gate_lock_proc_tree_available() { [ -r /proc/self/stat ]; }
73
+
74
+ gate_lock_default_path() { # 默认锁路径(与 smoke.sh / cmd-review.sh 改动前的口径一致)
75
+ printf '%s\n' "${TEAM_SMOKE_LOCK:-${TMPDIR:-/tmp}/teamsmith-smoke.lock}"
76
+ }
77
+
78
+ # ---------------------------------------------------------------- 伴生锁与身份(P287 · D1–D3)
79
+ # 两把锁、一个顺序(合同文本在 openspec/specs/verification:*The gate's lock is identified, and losing
80
+ # it stops the run loudly*):
81
+ # ① **发布路径** `<pub>`(默认 `${TMPDIR:-/tmp}/teamsmith-smoke.lock`)—— **兼容声明**:P287 之前的
82
+ # 写入者只认这一把。先取它、后取伴生锁;文件不存在就建(旧写入者也是这么建的),**永不 unlink**。
83
+ # ② **伴生锁** `<pub 同目录>/.<pub basename>.machine` —— **运行互斥**:本变更之后互斥**实际落在**它
84
+ # 上面。点开头,于是别人的 `rm -rf /tmp/teamsmith-smoke.*` 前缀 glob 够不着它(D184 的事故形状)。
85
+ # 身份:持锁进程**自己的**描述符身份(`stat -L /proc/<持有者>/fd/<n>` 的 `dev:ino`)——只有持有那个 fd
86
+ # 的进程(或读得到它 /proc 的同 uid 进程)读得到;读不到时退回「解析路径」的身份,并**如实写**是哪一种
87
+ # (`lock_identity=fd|path`)。design §4 D3:首选「取锁的那个进程自己握着 fd」,取锁瞬间的替换窗口
88
+ # (flock 开文件与 stat 之间)因此关掉。
89
+ gate_lock_companion_path() { # <发布路径> → 伴生锁路径(同行同目录、basename + `.machine`、点开头)
90
+ local pub="$1" dir base
91
+ dir="$(dirname "$pub")"; base="$(basename "$pub")"
92
+ printf '%s/.%s.machine\n' "$dir" "$base"
93
+ }
94
+
95
+ GATE_LOCK_PUB_FD=8 # 发布路径(兼容声明)的 fd
96
+ GATE_LOCK_FD=9 # 伴生锁(运行互斥)的 fd
97
+
98
+ # <路径> → dev:ino(读不到 → 非 0)。GNU stat 优先,BSD/macOS 兜底(仓里既有写法)。
99
+ gate_lock_path_identity() {
100
+ stat -c '%d:%i' "$1" 2>/dev/null || stat -f '%d:%i' "$1" 2>/dev/null
101
+ }
102
+
103
+ # <路径> → mtime 的 epoch 秒(读不到 → 非 0)。同一个两平台写法:心跳是 mtime,内容不必解析。
104
+ gate_lock_path_mtime() {
105
+ stat -c '%Y' "$1" 2>/dev/null || stat -f '%m' "$1" 2>/dev/null
106
+ }
107
+
108
+ # <pid> <路径> → "<fd>\t<link>":该进程第一个指向 <路径>(或 "<路径> (deleted)")的描述符
109
+ gate_lock_fd_of_pid() {
110
+ local pid="${1:-}" p="$2" f l
111
+ [ -n "$pid" ] || return 1
112
+ [ -d "/proc/$pid/fd" ] || return 1
113
+ for f in "/proc/$pid"/fd/*; do
114
+ [ -L "$f" ] || [ -e "$f" ] || continue
115
+ l="$(readlink "$f" 2>/dev/null || true)"
116
+ case "$l" in "$p"|"$p (deleted)") printf '%s\t%s\n' "${f##*/}" "$l"; return 0 ;; esac
117
+ done
118
+ return 1
119
+ }
120
+
121
+ # <pid> <路径> <期望 dev:ino> → 0 = 那个进程确实握着一个指向该路径、且身份等于期望值的描述符。
122
+ # 判据是**它的 /proc 里真有那个 fd**(不是「它自称」——环境变量里的 pid/身份单独不作数)。
123
+ gate_lock_pid_holds_path() {
124
+ local pid="${1:-}" p="$2" want="${3:-}" l fd now
125
+ [ -n "$pid" ] && [ -n "$want" ] || return 1
126
+ l="$(gate_lock_fd_of_pid "$pid" "$p" 2>/dev/null)" || return 1
127
+ fd="${l%%$'\t'*}"
128
+ now="$(gate_lock_fd_identity "$pid" "$fd")"
129
+ # 结尾必须显式 return(不能以 `&&` 链收尾:set -e 的调用方会静默退出;§0c 静态检查会红)
130
+ if [ -n "$now" ] && [ "$now" = "$want" ]; then return 0; fi
131
+ return 1
132
+ }
133
+
134
+ # <pid> <fd> → 那个描述符指向的 inode 的 dev:ino(文件被 unlink 也读得到)
135
+ gate_lock_fd_identity() {
136
+ stat -Lc '%d:%i' "/proc/${1:-}/fd/${2:-}" 2>/dev/null || printf -- '-'
137
+ }
138
+
139
+ # 取两把锁(**在本进程里**开 fd、加锁;顺序固定:发布路径 → 伴生锁)。GATE_LOCK_COMPANION 设好。
140
+ # 0 = 两把都拿到(fd 8 = 发布路径,fd 9 = 伴生锁)
141
+ # 1 = 发布路径被别人持有(旧的写入者,或另一个本版本运行)
142
+ # 3 = 伴生锁被别人持有
143
+ # 2 = 建不出来 / 没有 flock
144
+ # 为什么不用 `flock --close` 包一层:本进程自己握着 fd,才能从**持有者**的描述符读到真身份;而跑套件
145
+ # 的是本进程的**子进程**,用 `8>&- 9>&-` 把这两个 fd 关掉 —— 子孙(夹具留下的后台进程)不会继承锁
146
+ # fd,运行退出即释放(M23 的「漏锁」教训在这里是构造性关掉的)。
147
+ gate_lock_acquire_pair() {
148
+ local pub="$1" comp
149
+ comp="$(gate_lock_companion_path "$pub")"
150
+ # 注意:`exec … 2>/dev/null` 会把**本 shell 的 stderr** 永久改成 /dev/null(exec 的重定向作用于
151
+ # shell 本身)—— 实测过一次:静默吃掉后面所有的诊断与点名行。要静默就用**花括号组**(临时重定向)。
152
+ mkdir -p "$(dirname "$pub")" 2>/dev/null || return 2
153
+ : >>"$pub" 2>/dev/null || true
154
+ { exec 8>>"$pub"; } 2>/dev/null || return 2
155
+ if ! flock -n 8 2>/dev/null; then { exec 8>&-; } 2>/dev/null || true; return 1; fi
156
+ mkdir -p "$(dirname "$comp")" 2>/dev/null || { { exec 8>&-; } 2>/dev/null || true; return 2; }
157
+ { exec 9>>"$comp"; } 2>/dev/null || { { exec 8>&-; } 2>/dev/null || true; return 2; }
158
+ if ! flock -n 9 2>/dev/null; then
159
+ { exec 9>&-; } 2>/dev/null || true; { exec 8>&-; } 2>/dev/null || true; return 3
160
+ fi
161
+ GATE_LOCK_COMPANION="$comp"
162
+ return 0
163
+ }
164
+
165
+ # 上面那把锁的**身份**:打印 "<fd|path>\t<dev:ino>"。读得到持有者(本进程)的描述符就用 `fd`+那个
166
+ # inode;读不到(没有 /proc / 描述符已经不在)就用解析路径的身份。两个值都读不到 → "-\t-"。
167
+ gate_lock_claim_identity() { # <伴生锁> [<pid>]
168
+ local comp="$1" pid="${2:-$$}" ids
169
+ ids="$(gate_lock_fd_identity "$pid" "$GATE_LOCK_FD")"
170
+ if [ -n "$ids" ] && [ "$ids" != "-" ] && gate_lock_fd_of_pid "$pid" "$comp" >/dev/null 2>&1; then
171
+ printf 'fd\t%s\n' "$ids"; return 0
172
+ fi
173
+ ids="$(gate_lock_path_identity "$comp" 2>/dev/null || true)"
174
+ printf 'path\t%s\n' "${ids:--}"
175
+ }
176
+
177
+ # 守卫探针:<路径> <身份种类> <dev:ino> <持有者 pid> → "ok" | "gone<TAB>为什么" | "replaced<TAB>期望<TAB>实测"
178
+ # 判定(design §4/§5 + 合同的两条文案):
179
+ # * 先从**持有它的那个进程**读描述符(那才是「我持有的那个文件」);描述符被 unlink 时读出来的
180
+ # dev:ino 仍是那个孤儿 inode,readlink 带 ` (deleted)`;
181
+ # * 再看路径现在是谁:
182
+ # 路径存在且 == 持有者的 inode → ok(互斥完好)
183
+ # 路径存在但 != 持有者的 inode → replaced(**删了又建**与 **mv 覆盖**都落在这条;
184
+ # 期望 = 我持有的,实测 = 路径上的)
185
+ # 路径不存在且描述符已 deleted → gone(被外部删除,没有重建)
186
+ # 读不到描述符(身份是 path / 持有者已退)→ 只看路径:不存在 → gone;inode 不同 → replaced
187
+ gate_lock_snapshot() {
188
+ local p="$1" ident="${2:-fd}" want="${3:-}" hpid="${4:-}" l fd now path_ino= path_ok=0
189
+ if [ -e "$p" ] || [ -L "$p" ]; then
190
+ path_ok=1; path_ino="$(gate_lock_path_identity "$p" 2>/dev/null || true)"
191
+ fi
192
+ if [ -n "$hpid" ] && [ "$ident" = "fd" ]; then
193
+ if l="$(gate_lock_fd_of_pid "$hpid" "$p" 2>/dev/null)"; then
194
+ fd="${l%%$'\t'*}"; now="$(gate_lock_fd_identity "$hpid" "$fd")"
195
+ if [ "$path_ok" = "1" ] && [ -n "$path_ino" ] && [ "$path_ino" = "$now" ]; then return 0; fi
196
+ if [ "$path_ok" = "1" ]; then
197
+ printf 'replaced\t%s\t%s\n' "${now:--}" "${path_ino:--}"; return 0
198
+ fi
199
+ case "${l#*$'\t'}" in
200
+ "$p (deleted)") printf 'gone\t%s\n' "持有者(pid=$hpid)握着的 inode 已被外部 unlink,路径上没有重建"; return 0 ;;
201
+ esac
202
+ printf 'gone\t%s\n' "锁文件已不在(持有者 pid=$hpid 的描述符也没了)"; return 0
203
+ fi
204
+ if ! kill -0 "$hpid" 2>/dev/null; then
205
+ printf 'gone\t%s\n' "持有者进程 pid=$hpid 已退出(锁随之释放)"; return 0
206
+ fi
207
+ fi
208
+ if [ "$path_ok" != "1" ]; then printf 'gone\t%s\n' "路径上的文件已不在"; return 0; fi
209
+ if [ -n "$want" ] && [ "$path_ino" = "$want" ]; then return 0; fi
210
+ printf 'replaced\t%s\t%s\n' "${want:--}" "${path_ino:--}"
211
+ return 0
212
+ }
213
+
214
+ # 入场检查(design §6):拿到锁之后、跑套件之前 —— 读伴生锁边上的**活登记**,若有一条 top 登记记着
215
+ # **另一个身份**,说明互斥已经断了(旧持有者还活着,但路径上已经是另一个 inode)。打印一行;空 = 干净。
216
+ # <comp> <我的 dev:ino> <我自己的 pid> <我的祖先 pid 名单(空格分隔)>
217
+ gate_lock_entry_conflict() {
218
+ local comp="$1" mine="$2" me="${3:-$$}" chain=" ${4:-} " f pid st got who
219
+ for f in "$(gate_lock_holder_file "$comp")" "$(gate_lock_runs_dir "$comp")"/*.run; do
220
+ [ -f "$f" ] || continue
221
+ gate_lock_record_live "$f" || continue
222
+ pid="$(gate_lock_record_get "$f" pid)"
223
+ [ -n "$pid" ] || continue
224
+ [ "$pid" = "$me" ] && continue
225
+ case "$chain" in *" $pid "*) continue ;; esac # 我的祖辈(同一套运行的持有者)
226
+ case "$(gate_lock_record_get "$f" kind)" in top|'') ;; *) continue ;; esac
227
+ got="$(gate_lock_record_get "$f" lock_dev_ino)"
228
+ case "$got" in ''|'-') continue ;; esac # 没记身份的老记录不参与判定
229
+ [ "$got" = "$mine" ] && continue
230
+ who="$(gate_lock_describe_one "$f")"
231
+ printf '另一个活着的运行登记着**另一个身份**(%s):它的 dev:ino=%s,路径上现在是 %s —— 互斥已断\n' \
232
+ "$who" "$got" "${mine:--}"
233
+ return 1
234
+ done
235
+ return 0
236
+ }
237
+
238
+ # 伙伴记录:读锁记录时优先**伴生锁**边上那份(本版本写的),退回这条路径自己的(P287 之前的写入者
239
+ # 写在 <published>.holder)。返回实际用的路径;空 = 两边都没有可用记录。
240
+ gate_lock_record_source() { # <发布路径> → 打印 "<记录路径>\t<current|legacy|->"
241
+ local pub="$1" comp ch ph comp_any=0 pub_any=0
242
+ comp="$(gate_lock_companion_path "$pub")"
243
+ ch="$(gate_lock_holder_file "$comp")"; ph="$(gate_lock_holder_file "$pub")"
244
+ if [ -s "$ch" ] || compgen -G "$(gate_lock_runs_dir "$comp")/*.run" >/dev/null 2>&1; then comp_any=1; fi
245
+ if [ -s "$ph" ] || compgen -G "$(gate_lock_runs_dir "$pub")/*.run" >/dev/null 2>&1; then pub_any=1; fi
246
+ # ① **活着的**持有者记录优先(本版本写在伴生锁边上;P287 之前的写入者写在发布路径边上)——
247
+ # 不然一条陈旧记录会遮住正在挡着你的那个持有者(实测:34② 的上限与 34⑦ 的点名都被带偏)。
248
+ if [ -s "$ch" ] && gate_lock_record_live "$ch"; then printf '%s\tcurrent\n' "$comp"; return 0; fi
249
+ if [ -s "$ph" ] && gate_lock_record_live "$ph"; then printf '%s\tlegacy\n' "$pub"; return 0; fi
250
+ # ② 没有活记录:谁有东西就报谁(陈旧记录仍会被打印成「陈旧记录」并忽略),发布路径那条优先
251
+ # —— 它是旧写入者(与 P281 时代的夹具)写的地方。
252
+ if [ "$pub_any" = "1" ]; then printf '%s\tlegacy\n' "$pub"; return 0; fi
253
+ if [ "$comp_any" = "1" ]; then printf '%s\tcurrent\n' "$comp"; return 0; fi
254
+ printf '\t-\n'
255
+ }
256
+
257
+ # ---------------------------------------------------------------- marker(「我真的是持有者」的证据)
258
+ # `flock` 的包装进程在**拿到锁之后**写一行 `<epoch> <pid>`:pid 就是被 exec 起来的那次运行的 pid。
259
+ # 这是「本进程就是 flock 的持有者」的唯一证据 —— 环境标记(SMOKE_LOCK_WRAPPED)会被子孙继承,
260
+ # 所以它只配当线索,不配当证明(EXP1:带标记的嵌套运行覆写并删掉了真持有者的记录)。
261
+ gate_lock_marker_write() { # <marker> <pid>
262
+ printf '%s %s\n' "$(gate_lock_now)" "$2" > "$1" 2>/dev/null || true
263
+ }
264
+ gate_lock_marker_pid() { # <marker> → 记的 pid(旧形状只有 epoch / 读不到 → 空)
265
+ local line; line="$(head -1 "$1" 2>/dev/null || true)"
266
+ case "$line" in *' '*) printf '%s\n' "${line##* }" ;; *) printf '\n' ;; esac
267
+ }
268
+ gate_lock_marker_epoch() { # <marker> → 记的 epoch(读不到 → 空)
269
+ local line; line="$(head -1 "$1" 2>/dev/null || true)"
270
+ printf '%s\n' "${line%% *}"
271
+ }
272
+
273
+ # ---------------------------------------------------------------- 记录:写
274
+ gate_lock_record_write() { # <file> <pid> <kind> <cmd> <agent> <worktree> <started> <expected> <bound> <sections> <no_lock> <ancestor> <heartbeat> [<identity> <dev:ino>]
275
+ local f="$1" pid="$2" kind="$3" cmd="$4" agent="$5" wt="$6" started="$7" \
276
+ expected="$8" bound="$9" sections="${10}" no_lock="${11}" ancestor="${12}" hb="${13}" \
277
+ ident="${14:--}" dino="${15:--}"
278
+ local st tmp; st="$(gate_lock_proc_start "$pid" 2>/dev/null || true)"; [ -n "$st" ] || st="-"
279
+ case "$ident" in ''|fd|path) ;; *) ident="-" ;; esac
280
+ case "$dino" in ''|*[!0-9:]*) dino="-" ;; esac
281
+ mkdir -p "$(dirname "$f")" 2>/dev/null || true
282
+ tmp="$f.$$.tmp"
283
+ # 原子写:读的人要么看到旧的一条完整的,要么看到新的一条完整的,不会看到半行
284
+ # P287:末两个字段 = 我持有的那把锁的身份(`lock_identity=fd|path` + `lock_dev_ino=<dev:ino>`);
285
+ # 不持有锁的运行(嵌套 / no_lock)**如实写 `-`**(合同:记不下来就不许编)。
286
+ if printf '%s\tpid=%s\tstart=%s\tcmd=%s\tkind=%s\tagent=%s\tworktree=%s\tstarted=%s\texpected=%s\tbound=%s\tsections=%s\tno_lock=%s\tancestor=%s\theartbeat=%s\tlock_identity=%s\tlock_dev_ino=%s\n' \
287
+ "$(date -Is)" "$pid" "$st" "$cmd" "$kind" "$agent" "$wt" "$started" \
288
+ "$expected" "$bound" "$sections" "$no_lock" "$ancestor" "$hb" "$ident" "$dino" > "$tmp" 2>/dev/null; then
289
+ mv -f "$tmp" "$f" 2>/dev/null || rm -f "$tmp" 2>/dev/null || true
290
+ else
291
+ rm -f "$tmp" 2>/dev/null || true
292
+ fi
293
+ return 0
294
+ }
295
+
296
+ gate_lock_holder_file() { printf '%s\n' "$1.holder"; }
297
+ gate_lock_runs_dir() { printf '%s\n' "$1.runs"; }
298
+ gate_lock_run_file() { printf '%s\n' "$1.runs/$2.run"; }
299
+
300
+ gate_lock_write_holder() { # <lock> <pid> …(字段同 gate_lock_record_write)
301
+ local lock="$1"; shift
302
+ gate_lock_record_write "$(gate_lock_holder_file "$lock")" "$@"
303
+ }
304
+ gate_lock_register_run() { # <lock> <pid> …(字段同 gate_lock_record_write)
305
+ local lock="$1"; shift
306
+ mkdir -p "$(gate_lock_runs_dir "$lock")" 2>/dev/null || true
307
+ gate_lock_record_write "$(gate_lock_run_file "$lock" "$1")" "$@"
308
+ }
309
+ gate_lock_remove_holder() { rm -f "$(gate_lock_holder_file "$1")" 2>/dev/null || true; }
310
+ gate_lock_remove_run() { rm -f "$(gate_lock_run_file "$1" "$2")" 2>/dev/null || true; }
311
+
312
+ # 持有者记录的写权(返修轮次 1 · F4):记录里**活着的 pid** 是唯一不可继承的证明。
313
+ # `SMOKE_LOCK_MARKER` 只是一条环境里的路径,一个继承了它的后代可以改写那个文件、把自己的 pid 填进去
314
+ # ——「自述是持有者」于是变成自证。这里在**写之前**再问一句:记录现在是谁的?
315
+ # 没有记录 / 空 / 缺 pid(旧形状)→ 可以写(不猜,不把「读不到」当拒绝);
316
+ # 记录就是我(重入) → 可以写;
317
+ # 记录是别人但已不活(pid 死了 / 僵尸 / pid 复用后 starttime 不符)→ 陈旧,可以接管;
318
+ # 记录是另一个**活着的**进程 → 不许写、也不许删(真持有者的记录只由它自己删)。
319
+ gate_lock_holder_claimable() { # <lock> <pid> → 0 = 可以写/删 <lock>.holder
320
+ local lock="$1" pid="${2:-$$}" f rp
321
+ f="$(gate_lock_holder_file "$lock")"
322
+ [ -s "$f" ] || return 0
323
+ rp="$(gate_lock_record_get "$f" pid)"
324
+ [ -n "$rp" ] || return 0
325
+ [ "$rp" = "$pid" ] && return 0
326
+ gate_lock_record_live "$f" && return 1
327
+ return 0
328
+ }
329
+
330
+ # ---------------------------------------------------------------- 记录:读
331
+ gate_lock_record_get() { # <file> <key> → 值(缺 → 空)
332
+ local f="$1" k="$2" line v
333
+ line="$(head -1 "$f" 2>/dev/null || true)"
334
+ [ -n "$line" ] || return 0
335
+ line="$(printf '%s' "$line" | tr '\t' ' ')"
336
+ for v in $line; do
337
+ case "$v" in "$k="*) printf '%s\n' "${v#"$k="}"; return 0 ;; esac
338
+ done
339
+ return 0
340
+ }
341
+
342
+ # 祖辈的 section-guard 心跳(返修轮次 1 · F1):沿着**已证明的祖辈 pid** 去读它记录里公布的
343
+ # `heartbeat=`。证明成功时手里就有那个 pid,所以这正是「读记录」而不是「读环境里的一个变量」——
344
+ # 套件自己的夹具会清掉环境变量,那时今日的实现会**无声**退回自己的表预算(V43 的 F1)。
345
+ # 只认**活着的那一条**(pid 相符且 gate_lock_record_live),读不到 → 非 0(调用方按自己的表预算走,
346
+ # 并且必须把这次退回打出来)。
347
+ gate_lock_heartbeat_of_pid() { # <lock> <pid> → 该 pid 记录里公布的 heartbeat 路径(无 → 非 0)
348
+ local lock="$1" pid="${2:-}" f rp hb
349
+ [ -n "$pid" ] || return 1
350
+ for f in "$(gate_lock_holder_file "$lock")" "$(gate_lock_runs_dir "$lock")"/*.run; do
351
+ [ -f "$f" ] || continue
352
+ rp="$(gate_lock_record_get "$f" pid)"
353
+ [ "$rp" = "$pid" ] || continue
354
+ gate_lock_record_live "$f" || continue
355
+ hb="$(gate_lock_record_get "$f" heartbeat)"
356
+ case "$hb" in ''|'-') return 1 ;; esac
357
+ printf '%s\n' "$hb"; return 0
358
+ done
359
+ return 1
360
+ }
361
+
362
+ gate_lock_record_live() { # <file> → 0 = pid 活着且 starttime 相符(没记 start 时只看活着)
363
+ local f="$1" pid st
364
+ pid="$(gate_lock_record_get "$f" pid)"; st="$(gate_lock_record_get "$f" start)"
365
+ [ -n "$pid" ] || return 1
366
+ case "$st" in ''|'-') st="" ;; esac
367
+ gate_lock_pid_alive "$pid" "$st"
368
+ }
369
+
370
+ gate_lock_describe_one() { # <file> → 一个内联片段(缺的字段打 `?`,不编)
371
+ local f="$1" kind pid cmd agent wt started expected bound age="?" now nolock=""
372
+ kind="$(gate_lock_record_get "$f" kind)"; pid="$(gate_lock_record_get "$f" pid)"
373
+ cmd="$(gate_lock_record_get "$f" cmd)"; agent="$(gate_lock_record_get "$f" agent)"
374
+ wt="$(gate_lock_record_get "$f" worktree)"; started="$(gate_lock_record_get "$f" started)"
375
+ expected="$(gate_lock_record_get "$f" expected)"; bound="$(gate_lock_record_get "$f" bound)"
376
+ case "$started" in ''|'-'|*[!0-9]*) ;; *)
377
+ now="$(gate_lock_now)"; age=$(( now - started )); [ "$age" -lt 0 ] && age=0 ;;
378
+ esac
379
+ # 残余窗口(TEAM_SMOKE_NO_LOCK / 没有 flock / FAST)在记录里是 no_lock=1,等待者要能看见它 ——
380
+ # 只在**它是 1** 时打出来(0 的运行不加噪音,行里的字段照旧可逐项断言)。
381
+ [ "$(gate_lock_record_get "$f" no_lock)" = "1" ] && nolock=" no_lock=1"
382
+ # P287:P287 之前的写入者只写 `<ISO> pid=… cmd=…`(没有 kind/席位/上限/身份)—— 有多少字段打多少,
383
+ # 缺的写 `?`,并**标出来**是旧版本的记录(绝不印「持有者未知」)。
384
+ local ver=""
385
+ [ -z "$kind" ] && ver=" version=legacy" # 旧写入者的记录没有 kind —— 有多少字段打多少,并标出来
386
+ printf 'kind=%s pid=%s cmd=%s agent=%s worktree=%s 已跑 %ss expected=%s bound=%s%s%s' \
387
+ "${kind:-?}" "${pid:-?}" "${cmd:-?}" "${agent:-?}" "${wt:-?}" "$age" "${expected:-?}" "${bound:-?}" "$nolock" "$ver"
388
+ }
389
+
390
+ # <发布路径> → 一行:优先伴生锁边上的记录(本版本);没有就退回发布路径自己的(旧写入者,标 legacy)
391
+ gate_lock_describe_pair() {
392
+ local pub="$1" src kind
393
+ IFS=$'\t' read -r src kind < <(gate_lock_record_source "$pub")
394
+ if [ -z "$src" ]; then printf '持有者未知\n'; return 0; fi
395
+ gate_lock_describe "$src"
396
+ }
397
+
398
+ # <发布路径> <wait> → gate_lock_patience 的同一行,但记录优先读伴生锁边上的那份(旧写入者的退回发布路径)
399
+ gate_lock_patience_pair() {
400
+ local pub="$1" src kind
401
+ IFS=$'\t' read -r src kind < <(gate_lock_record_source "$pub")
402
+ [ -n "$src" ] || src="$pub"
403
+ gate_lock_patience "$src" "$2"
404
+ }
405
+
406
+ gate_lock_describe() { # <lock> → 一行:活着的记录逐个列出;陈旧条目标 `stale` 并忽略
407
+ local lock="$1" f pid seen=" " live="" stale="" n=0
408
+ for f in "$(gate_lock_holder_file "$lock")" "$(gate_lock_runs_dir "$lock")"/*.run; do
409
+ [ -f "$f" ] || continue
410
+ pid="$(gate_lock_record_get "$f" pid)"; [ -n "$pid" ] || pid="?"
411
+ case "$seen" in *" $pid "*) continue ;; esac
412
+ seen="$seen$pid "
413
+ if gate_lock_record_live "$f"; then
414
+ live="${live:+$live;}$(gate_lock_describe_one "$f")"; n=$((n + 1))
415
+ else
416
+ stale="${stale:+$stale;}$(gate_lock_describe_one "$f")"
417
+ fi
418
+ done
419
+ if [ "$n" -eq 0 ]; then
420
+ printf '持有者未知%s\n' "${stale:+(陈旧记录:$stale)}"
421
+ else
422
+ printf '%s%s\n' "$live" "${stale:+(陈旧记录:$stale)}"
423
+ fi
424
+ }
425
+
426
+ # ---------------------------------------------------------------- 祖辈证明(嵌套判定的唯一依据)
427
+ gate_lock_ancestry_chain() { # [<起点 pid>] → 祖先 pid,从近到远(不含起点;没有 /proc → 空)
428
+ # 起点默认是**本脚本进程**(`$$`):这个函数按惯例在 `$( )` 里跑,`BASHPID` 会是那个子 shell
429
+ # 的 pid,于是「自己的父进程」会被当成「祖辈」——实测过,是真阳性的假证(P281 自测抓到)。
430
+ local pid="${1:-$$}" i=0 pp
431
+ while [ "$i" -lt 64 ]; do
432
+ pp="$(gate_lock_ppid "$pid" 2>/dev/null || true)"
433
+ case "$pp" in ''|0|1) break ;; esac
434
+ printf '%s\n' "$pp"
435
+ pid="$pp"; i=$((i + 1))
436
+ done
437
+ }
438
+
439
+ gate_lock_prove_ancestor() { # <lock> → 最近的「记录里活着且 starttime 相符」的祖辈 pid;证不出 → 空
440
+ local lock="$1" p st f rp rs
441
+ for p in $(gate_lock_ancestry_chain "$$"); do
442
+ st="$(gate_lock_proc_start "$p" 2>/dev/null || true)"
443
+ for f in "$(gate_lock_holder_file "$lock")" "$(gate_lock_runs_dir "$lock")"/*.run; do
444
+ [ -f "$f" ] || continue
445
+ rp="$(gate_lock_record_get "$f" pid)"; rs="$(gate_lock_record_get "$f" start)"
446
+ [ -n "$rp" ] && [ "$rp" = "$p" ] || continue
447
+ case "$rs" in ''|'-') ;; *) [ "$rs" = "$st" ] || continue ;; esac
448
+ printf '%s\n' "$p"; return 0
449
+ done
450
+ done
451
+ return 1
452
+ }
453
+
454
+ # <发布路径>:祖辈证明与心跳都按**记录实际所在的那一侧**读(伴生锁优先,退回发布路径)
455
+ gate_lock_prove_ancestor_pair() {
456
+ local pub="$1" src kind
457
+ IFS=$'\t' read -r src kind < <(gate_lock_record_source "$pub")
458
+ [ -n "$src" ] || return 1
459
+ gate_lock_prove_ancestor "$src"
460
+ }
461
+
462
+ gate_lock_heartbeat_of_pid_pair() { # <发布路径> <祖辈 pid>
463
+ local pub="$1" src kind
464
+ IFS=$'\t' read -r src kind < <(gate_lock_record_source "$pub")
465
+ [ -n "$src" ] || return 1
466
+ gate_lock_heartbeat_of_pid "$src" "$2"
467
+ }
468
+
469
+ # ---------------------------------------------------------------- 等待者的耐心(跟随心跳,不是预算投影)
470
+ # change: gate-waiter-patience(P338)。上限每轮**按现场重算**,判据只有等待者自己的已等时长:
471
+ # live 本锁某条活登记公布的心跳文件 < W 秒前还在动 → 上限 4 × W(硬顶,再久也不等)
472
+ # stalled 公布了,但已经 >= W 秒没动(边界归这一侧) → 上限 W(持有者按卡死处理)
473
+ # none 没有活登记公布心跳 / 公布的是 `-` / 路径上的文件不在 → 上限 W(旧记录:与今天一字不改)
474
+ # 心跳取**本锁所有活登记里最新的那一条**(`<lock>.holder` 与 `<lock>.runs/*.run`):顶层 smoke.sh 的
475
+ # 持有者记录公布的是取锁包装器的路径(那个文件没人写),真正在动的是子套件自己那条登记;`team review`
476
+ # 的持有者记录直接写 `heartbeat=-`,它跑的门禁只在子登记里公布心跳。只读 `.holder` 两种真实形状都
477
+ # 读不到,等于没改(design §3)。
478
+ # 读取**不校验**登记与当前持有者的祖先关系:一条遗留但仍活着的登记还能把上限顶到 4W —— 本 change
479
+ # 明写接受这条有界放宽(默认值下最坏 2 小时),不在实现里补认证。
480
+ gate_lock_heartbeat_newest() { # <lock> → "path<TAB>mtime"(没有可用心跳 → 非 0,无输出)
481
+ local lock="$1" f hb mt best_path="" best_mt=""
482
+ for f in "$(gate_lock_holder_file "$lock")" "$(gate_lock_runs_dir "$lock")"/*.run; do
483
+ [ -f "$f" ] || continue
484
+ gate_lock_record_live "$f" || continue
485
+ hb="$(gate_lock_record_get "$f" heartbeat)"
486
+ case "$hb" in ''|'-') continue ;; esac
487
+ mt="$(gate_lock_path_mtime "$hb")" || continue
488
+ [ -n "$mt" ] || continue
489
+ case "$mt" in *[!0-9]*) continue ;; esac
490
+ if [ -z "$best_mt" ] || [ "$mt" -gt "$best_mt" ]; then best_path="$hb"; best_mt="$mt"; fi
491
+ done
492
+ [ -n "$best_mt" ] || return 1
493
+ printf '%s\t%s\n' "$best_path" "$best_mt"
494
+ }
495
+
496
+ gate_lock_patience() { # <lock> <wait> → "ceiling<TAB>state<TAB>silent_for<TAB>heartbeat"
497
+ local lock="$1" wait="$2" newest hb mt now silent
498
+ # 唯一的时钟来源是既有的 gate_lock_now(测试可以在子 shell 里局部覆写它把时刻钉住)
499
+ newest="$(gate_lock_heartbeat_newest "$lock" 2>/dev/null || true)"
500
+ if [ -z "$newest" ]; then printf '%s\tnone\t-\t-\n' "$wait"; return 0; fi
501
+ hb="${newest%%$'\t'*}"; mt="${newest#*$'\t'}"
502
+ now="$(gate_lock_now)"
503
+ silent=$(( now - mt )); [ "$silent" -lt 0 ] && silent=0
504
+ if [ "$silent" -ge "$wait" ]; then
505
+ printf '%s\tstalled\t%s\t%s\n' "$wait" "$silent" "$hb"
506
+ else
507
+ printf '%s\tlive\t%s\t%s\n' "$(( wait * 4 ))" "$silent" "$hb"
508
+ fi
509
+ }
510
+
511
+ # <state> <silent_for> → 一句话说明上限来自哪里(排队行与失败行共用同一份措辞,只有这一处)
512
+ gate_lock_patience_note() {
513
+ case "$1" in
514
+ live) printf '心跳仍在动(%ss 前更新)\n' "$2" ;;
515
+ stalled) printf '心跳已停 %ss\n' "$2" ;;
516
+ *) printf '没有可用心跳\n' ;;
517
+ esac
518
+ }
519
+
520
+ # ---------------------------------------------------------------- 席位 / 工作树(team notify 同一条规则)
521
+ # 运行时目录 = cwd 的 git toplevel;主检出 → `pm`;`<main>/<worktrees>/<名字>` 之下 → 那个一级目录名
522
+ # (名册里真有这个名字时用名册的拼写)。**绝不读窗口名**(tmux-identity-scope 的教训)。
523
+ gate_lock_worktree_of_dir() { # <dir> → 绝对路径(解析不出 → `?`)
524
+ local top
525
+ top="$(cd "$1" 2>/dev/null && git rev-parse --path-format=absolute --show-toplevel 2>/dev/null || true)"
526
+ [ -n "$top" ] || { printf '?\n'; return 0; }
527
+ printf '%s\n' "$top"
528
+ }
529
+
530
+ gate_lock_seat_of_dir() { # <dir> [<名册>] [<worktrees 相对/绝对>] [<main root>] → 席位(`?` = 解析不出)
531
+ # 推导本体只有一份:common.sh 的 team_seat_from_paths(与 `team notify` 的发送者同一个函数)。
532
+ # 本函数只负责给它上下文(dir → git toplevel、主检出),并把「解析不出」收成 `?` —— 记录必须写
533
+ # 成功,它是诊断证据(P329)。
534
+ local dir="$1" roster="${2:-${TEAM_AGENTS:-}}" wtr="${3:-${TEAM_WORKTREES_DIR:-.worktrees}}" main="${4:-}"
535
+ local top common n
536
+ top="$(cd "$dir" 2>/dev/null && git rev-parse --path-format=absolute --show-toplevel 2>/dev/null || true)"
537
+ [ -n "$top" ] || { printf '?\n'; return 0; }
538
+ if [ -z "$main" ]; then
539
+ common="$(cd "$dir" 2>/dev/null && git rev-parse --path-format=absolute --git-common-dir 2>/dev/null || true)"
540
+ [ -n "$common" ] && main="$(dirname "$common")"
541
+ fi
542
+ [ -n "$main" ] || { printf '?\n'; return 0; }
543
+ top="$(cd "$top" 2>/dev/null && pwd -P || printf '%s' "$top")"
544
+ main="$(cd "$main" 2>/dev/null && pwd -P || printf '%s' "$main")"
545
+ n="$(team_seat_from_paths "$top" "$main" "$wtr" "$roster")"
546
+ [ -n "$n" ] || { printf '?\n'; return 0; }
547
+ printf '%s\n' "$n"
548
+ }
549
+
550
+ # ---------------------------------------------------------------- 期望用时(Σband / Σbudget / 段数)
551
+ # 段清单从**正在跑的那份脚本**里读(选段副本与全量同一份实现),键按 `section "…"` 的 id 与预算表
552
+ # 对齐;表里没登记的段用有界默认(同 section-guard 的 SG_DEFAULT_BUDGET)。
553
+ gate_lock_expected() { # <suite-file> <budgets-file> [<默认预算>] → "expected<TAB>bound<TAB>sections"
554
+ local suite="$1" budgets="$2" dflt="${3:-900}" line ids="" n=0
555
+ [ -r "$suite" ] || { printf '0\t0\t0\n'; return 0; }
556
+ while IFS= read -r line; do
557
+ case "$line" in
558
+ *'section "'*)
559
+ if [[ $line =~ ^[[:space:]]*section[[:space:]]+\"([^\"]+)\" ]]; then
560
+ ids="${ids}${ids:+$'\n'}${BASH_REMATCH[1]}"; n=$((n + 1))
561
+ fi ;;
562
+ esac
563
+ done < "$suite"
564
+ # 一次 awk 读完(旧写法每段起一个 awk:129 段 ≈ 129 个进程,嵌套运行里肉眼可见地慢)
565
+ # 注意:这里不能用 `NR==FNR` 的「第一份输入」技巧 —— 第一份是 stdin(`-`),awk 不保证 FNR 会归零。
566
+ [ -r "$budgets" ] || { printf '0\t0\t%s\n' "$n"; return 0; }
567
+ awk -F'\t' -v dflt="$dflt" -v n="$n" -v wantstr="$ids" '
568
+ BEGIN { nw = split(wantstr, W, "\n"); for (i = 1; i <= nw; i++) want[W[i]] = 1 }
569
+ /^[[:space:]]*#/ { next }
570
+ ($1 in want) {
571
+ b = $2; u = $3
572
+ if (b ~ /^[0-9.]+$/) bands += b
573
+ if (u ~ /^[0-9]+$/) bud += u; else bud += dflt
574
+ seen[$1] = 1
575
+ next
576
+ }
577
+ END {
578
+ for (k in want) if (!(k in seen)) bud += dflt
579
+ printf "%d\t%d\t%d\n", bands + 0.5, bud, n
580
+ }
581
+ ' "$budgets" || printf '0\t0\t%s\n' "$n"
582
+ }
@@ -22,6 +22,9 @@ set -uo pipefail
22
22
 
23
23
  self="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
24
24
  skill="$(cd -P "$self/../.." && pwd)"
25
+ # P309: the temp root comes from the one resolver (no second `${TMPDIR:-/tmp}` here)
26
+ # shellcheck source=../lib/common.sh
27
+ . "$skill/scripts/lib/common.sh"
25
28
  file="${1:-}"
26
29
  if [ -n "$file" ]; then shift || true; fi
27
30
 
@@ -33,7 +36,14 @@ if [ -z "$file" ] || [ ! -f "$file" ]; then
33
36
  exit 2
34
37
  fi
35
38
 
36
- log="$(mktemp "${TMPDIR:-/tmp}/panel-draft-send.XXXXXX")"
39
+ # P309: the resolved root is only *ours* to create on the default leg; an explicit root that is
40
+ # unusable fails loudly instead of sending through some other directory.
41
+ if ! team_tmp_root_prepare >/dev/null 2>&1; then
42
+ printf 'draft-send: 临时根不可用(%s)\n' "$(team_tmp_root 2>/dev/null || printf '?')" >&2
43
+ emit 2
44
+ exit 2
45
+ fi
46
+ log="$(mktemp "$(team_tmp_root)/.teamsmith-panel-draft-send.XXXXXX")"
37
47
  trap 'rm -f "$log"' EXIT
38
48
 
39
49
  TEAM_SKILL_DIR="${TEAM_SKILL_DIR:-$skill}"; export TEAM_SKILL_DIR