teamsmith 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/package.json +1 -1
- package/skills/teamsmith/CHANGELOG.md +28 -0
- package/skills/teamsmith/SKILL.md +14 -7
- package/skills/teamsmith/extension/team-notify.ts +37 -6
- package/skills/teamsmith/extension/team.ts +438 -0
- package/skills/teamsmith/references/config.md +39 -6
- package/skills/teamsmith/references/migration.md +197 -47
- package/skills/teamsmith/references/protocol.md +176 -19
- package/skills/teamsmith/references/troubleshooting.md +124 -7
- package/skills/teamsmith/references/workflows.md +36 -2
- package/skills/teamsmith/scripts/lib/cmd-agents.sh +318 -23
- package/skills/teamsmith/scripts/lib/cmd-bootstrap.sh +2 -2
- package/skills/teamsmith/scripts/lib/cmd-config.sh +349 -67
- package/skills/teamsmith/scripts/lib/cmd-draft.sh +4 -4
- package/skills/teamsmith/scripts/lib/cmd-help.sh +677 -0
- package/skills/teamsmith/scripts/lib/cmd-init.sh +157 -5
- package/skills/teamsmith/scripts/lib/cmd-interrupt.sh +461 -0
- package/skills/teamsmith/scripts/lib/cmd-meeting.sh +2 -25
- package/skills/teamsmith/scripts/lib/cmd-project.sh +30 -103
- package/skills/teamsmith/scripts/lib/cmd-reap.sh +277 -0
- package/skills/teamsmith/scripts/lib/cmd-reply.sh +3 -3
- package/skills/teamsmith/scripts/lib/cmd-review.sh +175 -30
- package/skills/teamsmith/scripts/lib/cmd-status.sh +226 -17
- package/skills/teamsmith/scripts/lib/cmd-update.sh +36 -7
- package/skills/teamsmith/scripts/lib/cmd-watch.sh +564 -46
- package/skills/teamsmith/scripts/lib/common.sh +773 -48
- package/skills/teamsmith/scripts/lib/gate-lock.sh +544 -0
- package/skills/teamsmith/scripts/panel/draft-send.sh +11 -1
- package/skills/teamsmith/scripts/panel/panel.js +207 -207
- package/skills/teamsmith/scripts/panel/src/App.tsx +58 -21
- package/skills/teamsmith/scripts/panel/src/clipboard.ts +11 -5
- package/skills/teamsmith/scripts/panel/src/compose.ts +6 -3
- package/skills/teamsmith/scripts/panel/src/data.ts +11 -3
- package/skills/teamsmith/scripts/panel/src/layout.ts +209 -69
- package/skills/teamsmith/scripts/panel/src/main.tsx +33 -8
- package/skills/teamsmith/scripts/panel/src/strings/en.ts +39 -29
- package/skills/teamsmith/scripts/panel/src/strings/zh.ts +41 -31
- package/skills/teamsmith/scripts/panel/src/types.ts +28 -3
- package/skills/teamsmith/scripts/replies.mjs +4 -1
- package/skills/teamsmith/scripts/shim/tmux +18 -0
- package/skills/teamsmith/scripts/strings/commands.tsv +86 -0
- package/skills/teamsmith/scripts/strings/help.en.tsv +503 -0
- package/skills/teamsmith/scripts/strings/help.zh.tsv +489 -0
- package/skills/teamsmith/scripts/team +19 -6
- package/skills/teamsmith/templates/AGENTS.section.md.tmpl +8 -1
- package/skills/teamsmith/templates/config.sh.tmpl +20 -1
- package/skills/teamsmith/templates/pm-prompt.md.tmpl +21 -0
- package/skills/teamsmith/tests/bg-ledger-harness.mjs +464 -40
- package/skills/teamsmith/tests/bg-ledger.sh +168 -10
- package/skills/teamsmith/tests/checkout-shape-probe.sh +3 -1
- package/skills/teamsmith/tests/config-cli.sh +160 -14
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/scenario.sh +7 -1
- package/skills/teamsmith/tests/fixtures/p287/lint-good.sh.in +3 -0
- package/skills/teamsmith/tests/fixtures/p287/lint-hardcoded.sh.in +3 -0
- package/skills/teamsmith/tests/fixtures/p287/lint-no-ns.sh.in +3 -0
- package/skills/teamsmith/tests/fixtures/p55/flip-p49.sh +18 -6
- package/skills/teamsmith/tests/flip-m33.sh +5 -3
- package/skills/teamsmith/tests/flip-m37.sh +4 -1
- package/skills/teamsmith/tests/flip-m4.3.sh +4 -1
- package/skills/teamsmith/tests/flip-m6.3.sh +4 -1
- package/skills/teamsmith/tests/flip-m6.5.sh +4 -1
- package/skills/teamsmith/tests/flip-m7.2.sh +4 -1
- package/skills/teamsmith/tests/flip-p158.sh +2 -1
- package/skills/teamsmith/tests/flip-p210.sh +4 -1
- package/skills/teamsmith/tests/flip-p217.sh +4 -1
- package/skills/teamsmith/tests/flip-p287-lock.sh +342 -0
- package/skills/teamsmith/tests/flip-p287-name.sh +279 -0
- package/skills/teamsmith/tests/flip-p304-read-side.sh +98 -0
- package/skills/teamsmith/tests/flip-p308-skip-not-pass.sh +151 -0
- package/skills/teamsmith/tests/flip-p309-p162-deep.sh +122 -0
- package/skills/teamsmith/tests/flip-p310-lock.sh +216 -0
- package/skills/teamsmith/tests/flip-p310-socket.sh +125 -0
- package/skills/teamsmith/tests/help-surface.sh +1664 -0
- package/skills/teamsmith/tests/install-shape.sh +279 -20
- package/skills/teamsmith/tests/key-replies-pty.sh +9 -10
- package/skills/teamsmith/tests/key-replies-ui.sh +0 -7
- package/skills/teamsmith/tests/key-replies.sh +240 -1
- package/skills/teamsmith/tests/ledger-ban.sh +5 -4
- package/skills/teamsmith/tests/ledger-claim.sh +2 -1
- package/skills/teamsmith/tests/lib/bounded-spin.sh +90 -0
- package/skills/teamsmith/tests/lib/model-policy.sh +4 -0
- package/skills/teamsmith/tests/lib/model-routing-flips.py +51 -0
- package/skills/teamsmith/tests/lib/reply-fixture.sh +4 -9
- package/skills/teamsmith/tests/lib/section-guard.sh +258 -13
- package/skills/teamsmith/tests/lib/tmp-root.sh +139 -27
- package/skills/teamsmith/tests/lib/tmux-cap.sh +10 -1
- package/skills/teamsmith/tests/lib/tmux-iso.sh +31 -13
- package/skills/teamsmith/tests/loop-inventory.tsv +59 -47
- package/skills/teamsmith/tests/panel-b2.sh +106 -3
- package/skills/teamsmith/tests/panel-b3-stub.sh +112 -4
- package/skills/teamsmith/tests/panel-b3.sh +175 -19
- package/skills/teamsmith/tests/panel-choices.sh +11 -6
- package/skills/teamsmith/tests/panel-cpu.sh +2 -1
- package/skills/teamsmith/tests/panel-knobs.sh +7 -1
- package/skills/teamsmith/tests/panel-p21.sh +156 -73
- package/skills/teamsmith/tests/panel-snapshots.sh +41 -225
- package/skills/teamsmith/tests/panel-strings.mjs +33 -18
- package/skills/teamsmith/tests/perf.sh +121 -30
- package/skills/teamsmith/tests/routes.sh +327 -93
- package/skills/teamsmith/tests/seat-interrupt.sh +1002 -0
- package/skills/teamsmith/tests/seat-visibility.sh +675 -0
- package/skills/teamsmith/tests/section-budgets.tsv +20 -10
- package/skills/teamsmith/tests/section-guard.sh +6 -1
- package/skills/teamsmith/tests/section-needs-audit.sh +24 -7
- package/skills/teamsmith/tests/section-paths.tsv +19 -13
- package/skills/teamsmith/tests/section-select.sh +8 -1
- package/skills/teamsmith/tests/slash-team-e2e.sh +323 -0
- package/skills/teamsmith/tests/slash-team.sh +617 -0
- package/skills/teamsmith/tests/smoke.sh +2972 -440
- package/skills/teamsmith/tests/task-model-routing.sh +507 -10
- package/skills/teamsmith/tests/tmp-hygiene.sh +782 -45
- package/skills/teamsmith/tests/tmux-identity-leak.sh +773 -0
- package/skills/teamsmith/tests/tmux-lint-legacy.txt +12 -18
- package/skills/teamsmith/tests/upgrade-recipe.sh +537 -0
- package/skills/teamsmith-init/SKILL.md +12 -12
- package/skills/teamsmith-init/references/bootstrap.md +3 -3
|
@@ -0,0 +1,544 @@
|
|
|
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
|
+
# (运行时目录,名册拼写优先,**不取窗口名**);解析不出来就写 `?`——如实说不知道。
|
|
27
|
+
#
|
|
28
|
+
# `/proc` 只用于 pid 复用安全与祖辈证明:没有 `/proc` → 证明不成立 → fail closed(一律按顶层)。
|
|
29
|
+
|
|
30
|
+
GATE_LOCK_LIB=1
|
|
31
|
+
|
|
32
|
+
# ---------------------------------------------------------------- 时间 / 进程
|
|
33
|
+
gate_lock_now() { date +%s; }
|
|
34
|
+
|
|
35
|
+
gate_lock_proc_start() { # <pid> → /proc/<pid>/stat 的 starttime(pid 复用安全的判据)
|
|
36
|
+
local st; st="$(LC_ALL=C cat "/proc/$1/stat" 2>/dev/null)" || return 1
|
|
37
|
+
st="${st##*\) }"; set -- $st
|
|
38
|
+
[ -n "${20:-}" ] || return 1
|
|
39
|
+
printf '%s\n' "${20}"
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
gate_lock_ppid() { # <pid> → ppid
|
|
43
|
+
local st; st="$(LC_ALL=C cat "/proc/$1/stat" 2>/dev/null)" || return 1
|
|
44
|
+
st="${st##*\) }"; set -- $st
|
|
45
|
+
[ -n "${2:-}" ] || return 1
|
|
46
|
+
printf '%s\n' "$2"
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
gate_lock_proc_zombie() { # <pid> → 0 = 存在且状态是 Z(僵尸:已经跑完了,只等被 wait 收尸)
|
|
50
|
+
# 僵尸不持有任何东西。不认它会把「一次 SIGKILL 留下的僵尸记录」当成活着的持有者,
|
|
51
|
+
# 于是后来的**合法**持有者会被 gate_lock_holder_claimable 挡在门外(记录永远写不上去)。
|
|
52
|
+
local raw
|
|
53
|
+
raw=""; { IFS= read -r raw < "/proc/$1/stat"; } 2>/dev/null || raw=""
|
|
54
|
+
raw="${raw##*\) }"; case "${raw%% *}" in Z) return 0 ;; esac
|
|
55
|
+
return 1
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
gate_lock_pid_alive() { # <pid> [<starttime>] → 0 = 活着(非僵尸)且(给了 starttime 时)还是那一次
|
|
59
|
+
local pid="${1:-}" want="${2:-}" st
|
|
60
|
+
case "$pid" in ''|*[!0-9]*) return 1 ;; esac
|
|
61
|
+
[ "$pid" -gt 1 ] || return 1
|
|
62
|
+
kill -0 "$pid" 2>/dev/null || return 1
|
|
63
|
+
gate_lock_proc_zombie "$pid" && return 1
|
|
64
|
+
if [ -n "$want" ]; then
|
|
65
|
+
st="$(gate_lock_proc_start "$pid" 2>/dev/null || true)"
|
|
66
|
+
[ "$st" = "$want" ] || return 1
|
|
67
|
+
fi
|
|
68
|
+
return 0
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
gate_lock_proc_tree_available() { [ -r /proc/self/stat ]; }
|
|
72
|
+
|
|
73
|
+
gate_lock_default_path() { # 默认锁路径(与 smoke.sh / cmd-review.sh 改动前的口径一致)
|
|
74
|
+
printf '%s\n' "${TEAM_SMOKE_LOCK:-${TMPDIR:-/tmp}/teamsmith-smoke.lock}"
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
# ---------------------------------------------------------------- 伴生锁与身份(P287 · D1–D3)
|
|
78
|
+
# 两把锁、一个顺序(合同文本在 openspec/specs/verification:*The gate's lock is identified, and losing
|
|
79
|
+
# it stops the run loudly*):
|
|
80
|
+
# ① **发布路径** `<pub>`(默认 `${TMPDIR:-/tmp}/teamsmith-smoke.lock`)—— **兼容声明**:P287 之前的
|
|
81
|
+
# 写入者只认这一把。先取它、后取伴生锁;文件不存在就建(旧写入者也是这么建的),**永不 unlink**。
|
|
82
|
+
# ② **伴生锁** `<pub 同目录>/.<pub basename>.machine` —— **运行互斥**:本变更之后互斥**实际落在**它
|
|
83
|
+
# 上面。点开头,于是别人的 `rm -rf /tmp/teamsmith-smoke.*` 前缀 glob 够不着它(D184 的事故形状)。
|
|
84
|
+
# 身份:持锁进程**自己的**描述符身份(`stat -L /proc/<持有者>/fd/<n>` 的 `dev:ino`)——只有持有那个 fd
|
|
85
|
+
# 的进程(或读得到它 /proc 的同 uid 进程)读得到;读不到时退回「解析路径」的身份,并**如实写**是哪一种
|
|
86
|
+
# (`lock_identity=fd|path`)。design §4 D3:首选「取锁的那个进程自己握着 fd」,取锁瞬间的替换窗口
|
|
87
|
+
# (flock 开文件与 stat 之间)因此关掉。
|
|
88
|
+
gate_lock_companion_path() { # <发布路径> → 伴生锁路径(同行同目录、basename + `.machine`、点开头)
|
|
89
|
+
local pub="$1" dir base
|
|
90
|
+
dir="$(dirname "$pub")"; base="$(basename "$pub")"
|
|
91
|
+
printf '%s/.%s.machine\n' "$dir" "$base"
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
GATE_LOCK_PUB_FD=8 # 发布路径(兼容声明)的 fd
|
|
95
|
+
GATE_LOCK_FD=9 # 伴生锁(运行互斥)的 fd
|
|
96
|
+
|
|
97
|
+
# <路径> → dev:ino(读不到 → 非 0)。GNU stat 优先,BSD/macOS 兜底(仓里既有写法)。
|
|
98
|
+
gate_lock_path_identity() {
|
|
99
|
+
stat -c '%d:%i' "$1" 2>/dev/null || stat -f '%d:%i' "$1" 2>/dev/null
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
# <pid> <路径> → "<fd>\t<link>":该进程第一个指向 <路径>(或 "<路径> (deleted)")的描述符
|
|
103
|
+
gate_lock_fd_of_pid() {
|
|
104
|
+
local pid="${1:-}" p="$2" f l
|
|
105
|
+
[ -n "$pid" ] || return 1
|
|
106
|
+
[ -d "/proc/$pid/fd" ] || return 1
|
|
107
|
+
for f in "/proc/$pid"/fd/*; do
|
|
108
|
+
[ -L "$f" ] || [ -e "$f" ] || continue
|
|
109
|
+
l="$(readlink "$f" 2>/dev/null || true)"
|
|
110
|
+
case "$l" in "$p"|"$p (deleted)") printf '%s\t%s\n' "${f##*/}" "$l"; return 0 ;; esac
|
|
111
|
+
done
|
|
112
|
+
return 1
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
# <pid> <路径> <期望 dev:ino> → 0 = 那个进程确实握着一个指向该路径、且身份等于期望值的描述符。
|
|
116
|
+
# 判据是**它的 /proc 里真有那个 fd**(不是「它自称」——环境变量里的 pid/身份单独不作数)。
|
|
117
|
+
gate_lock_pid_holds_path() {
|
|
118
|
+
local pid="${1:-}" p="$2" want="${3:-}" l fd now
|
|
119
|
+
[ -n "$pid" ] && [ -n "$want" ] || return 1
|
|
120
|
+
l="$(gate_lock_fd_of_pid "$pid" "$p" 2>/dev/null)" || return 1
|
|
121
|
+
fd="${l%%$'\t'*}"
|
|
122
|
+
now="$(gate_lock_fd_identity "$pid" "$fd")"
|
|
123
|
+
# 结尾必须显式 return(不能以 `&&` 链收尾:set -e 的调用方会静默退出;§0c 静态检查会红)
|
|
124
|
+
if [ -n "$now" ] && [ "$now" = "$want" ]; then return 0; fi
|
|
125
|
+
return 1
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
# <pid> <fd> → 那个描述符指向的 inode 的 dev:ino(文件被 unlink 也读得到)
|
|
129
|
+
gate_lock_fd_identity() {
|
|
130
|
+
stat -Lc '%d:%i' "/proc/${1:-}/fd/${2:-}" 2>/dev/null || printf -- '-'
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
# 取两把锁(**在本进程里**开 fd、加锁;顺序固定:发布路径 → 伴生锁)。GATE_LOCK_COMPANION 设好。
|
|
134
|
+
# 0 = 两把都拿到(fd 8 = 发布路径,fd 9 = 伴生锁)
|
|
135
|
+
# 1 = 发布路径被别人持有(旧的写入者,或另一个本版本运行)
|
|
136
|
+
# 3 = 伴生锁被别人持有
|
|
137
|
+
# 2 = 建不出来 / 没有 flock
|
|
138
|
+
# 为什么不用 `flock --close` 包一层:本进程自己握着 fd,才能从**持有者**的描述符读到真身份;而跑套件
|
|
139
|
+
# 的是本进程的**子进程**,用 `8>&- 9>&-` 把这两个 fd 关掉 —— 子孙(夹具留下的后台进程)不会继承锁
|
|
140
|
+
# fd,运行退出即释放(M23 的「漏锁」教训在这里是构造性关掉的)。
|
|
141
|
+
gate_lock_acquire_pair() {
|
|
142
|
+
local pub="$1" comp
|
|
143
|
+
comp="$(gate_lock_companion_path "$pub")"
|
|
144
|
+
# 注意:`exec … 2>/dev/null` 会把**本 shell 的 stderr** 永久改成 /dev/null(exec 的重定向作用于
|
|
145
|
+
# shell 本身)—— 实测过一次:静默吃掉后面所有的诊断与点名行。要静默就用**花括号组**(临时重定向)。
|
|
146
|
+
mkdir -p "$(dirname "$pub")" 2>/dev/null || return 2
|
|
147
|
+
: >>"$pub" 2>/dev/null || true
|
|
148
|
+
{ exec 8>>"$pub"; } 2>/dev/null || return 2
|
|
149
|
+
if ! flock -n 8 2>/dev/null; then { exec 8>&-; } 2>/dev/null || true; return 1; fi
|
|
150
|
+
mkdir -p "$(dirname "$comp")" 2>/dev/null || { { exec 8>&-; } 2>/dev/null || true; return 2; }
|
|
151
|
+
{ exec 9>>"$comp"; } 2>/dev/null || { { exec 8>&-; } 2>/dev/null || true; return 2; }
|
|
152
|
+
if ! flock -n 9 2>/dev/null; then
|
|
153
|
+
{ exec 9>&-; } 2>/dev/null || true; { exec 8>&-; } 2>/dev/null || true; return 3
|
|
154
|
+
fi
|
|
155
|
+
GATE_LOCK_COMPANION="$comp"
|
|
156
|
+
return 0
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
# 上面那把锁的**身份**:打印 "<fd|path>\t<dev:ino>"。读得到持有者(本进程)的描述符就用 `fd`+那个
|
|
160
|
+
# inode;读不到(没有 /proc / 描述符已经不在)就用解析路径的身份。两个值都读不到 → "-\t-"。
|
|
161
|
+
gate_lock_claim_identity() { # <伴生锁> [<pid>]
|
|
162
|
+
local comp="$1" pid="${2:-$$}" ids
|
|
163
|
+
ids="$(gate_lock_fd_identity "$pid" "$GATE_LOCK_FD")"
|
|
164
|
+
if [ -n "$ids" ] && [ "$ids" != "-" ] && gate_lock_fd_of_pid "$pid" "$comp" >/dev/null 2>&1; then
|
|
165
|
+
printf 'fd\t%s\n' "$ids"; return 0
|
|
166
|
+
fi
|
|
167
|
+
ids="$(gate_lock_path_identity "$comp" 2>/dev/null || true)"
|
|
168
|
+
printf 'path\t%s\n' "${ids:--}"
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
# 守卫探针:<路径> <身份种类> <dev:ino> <持有者 pid> → "ok" | "gone<TAB>为什么" | "replaced<TAB>期望<TAB>实测"
|
|
172
|
+
# 判定(design §4/§5 + 合同的两条文案):
|
|
173
|
+
# * 先从**持有它的那个进程**读描述符(那才是「我持有的那个文件」);描述符被 unlink 时读出来的
|
|
174
|
+
# dev:ino 仍是那个孤儿 inode,readlink 带 ` (deleted)`;
|
|
175
|
+
# * 再看路径现在是谁:
|
|
176
|
+
# 路径存在且 == 持有者的 inode → ok(互斥完好)
|
|
177
|
+
# 路径存在但 != 持有者的 inode → replaced(**删了又建**与 **mv 覆盖**都落在这条;
|
|
178
|
+
# 期望 = 我持有的,实测 = 路径上的)
|
|
179
|
+
# 路径不存在且描述符已 deleted → gone(被外部删除,没有重建)
|
|
180
|
+
# 读不到描述符(身份是 path / 持有者已退)→ 只看路径:不存在 → gone;inode 不同 → replaced
|
|
181
|
+
gate_lock_snapshot() {
|
|
182
|
+
local p="$1" ident="${2:-fd}" want="${3:-}" hpid="${4:-}" l fd now path_ino= path_ok=0
|
|
183
|
+
if [ -e "$p" ] || [ -L "$p" ]; then
|
|
184
|
+
path_ok=1; path_ino="$(gate_lock_path_identity "$p" 2>/dev/null || true)"
|
|
185
|
+
fi
|
|
186
|
+
if [ -n "$hpid" ] && [ "$ident" = "fd" ]; then
|
|
187
|
+
if l="$(gate_lock_fd_of_pid "$hpid" "$p" 2>/dev/null)"; then
|
|
188
|
+
fd="${l%%$'\t'*}"; now="$(gate_lock_fd_identity "$hpid" "$fd")"
|
|
189
|
+
if [ "$path_ok" = "1" ] && [ -n "$path_ino" ] && [ "$path_ino" = "$now" ]; then return 0; fi
|
|
190
|
+
if [ "$path_ok" = "1" ]; then
|
|
191
|
+
printf 'replaced\t%s\t%s\n' "${now:--}" "${path_ino:--}"; return 0
|
|
192
|
+
fi
|
|
193
|
+
case "${l#*$'\t'}" in
|
|
194
|
+
"$p (deleted)") printf 'gone\t%s\n' "持有者(pid=$hpid)握着的 inode 已被外部 unlink,路径上没有重建"; return 0 ;;
|
|
195
|
+
esac
|
|
196
|
+
printf 'gone\t%s\n' "锁文件已不在(持有者 pid=$hpid 的描述符也没了)"; return 0
|
|
197
|
+
fi
|
|
198
|
+
if ! kill -0 "$hpid" 2>/dev/null; then
|
|
199
|
+
printf 'gone\t%s\n' "持有者进程 pid=$hpid 已退出(锁随之释放)"; return 0
|
|
200
|
+
fi
|
|
201
|
+
fi
|
|
202
|
+
if [ "$path_ok" != "1" ]; then printf 'gone\t%s\n' "路径上的文件已不在"; return 0; fi
|
|
203
|
+
if [ -n "$want" ] && [ "$path_ino" = "$want" ]; then return 0; fi
|
|
204
|
+
printf 'replaced\t%s\t%s\n' "${want:--}" "${path_ino:--}"
|
|
205
|
+
return 0
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
# 入场检查(design §6):拿到锁之后、跑套件之前 —— 读伴生锁边上的**活登记**,若有一条 top 登记记着
|
|
209
|
+
# **另一个身份**,说明互斥已经断了(旧持有者还活着,但路径上已经是另一个 inode)。打印一行;空 = 干净。
|
|
210
|
+
# <comp> <我的 dev:ino> <我自己的 pid> <我的祖先 pid 名单(空格分隔)>
|
|
211
|
+
gate_lock_entry_conflict() {
|
|
212
|
+
local comp="$1" mine="$2" me="${3:-$$}" chain=" ${4:-} " f pid st got who
|
|
213
|
+
for f in "$(gate_lock_holder_file "$comp")" "$(gate_lock_runs_dir "$comp")"/*.run; do
|
|
214
|
+
[ -f "$f" ] || continue
|
|
215
|
+
gate_lock_record_live "$f" || continue
|
|
216
|
+
pid="$(gate_lock_record_get "$f" pid)"
|
|
217
|
+
[ -n "$pid" ] || continue
|
|
218
|
+
[ "$pid" = "$me" ] && continue
|
|
219
|
+
case "$chain" in *" $pid "*) continue ;; esac # 我的祖辈(同一套运行的持有者)
|
|
220
|
+
case "$(gate_lock_record_get "$f" kind)" in top|'') ;; *) continue ;; esac
|
|
221
|
+
got="$(gate_lock_record_get "$f" lock_dev_ino)"
|
|
222
|
+
case "$got" in ''|'-') continue ;; esac # 没记身份的老记录不参与判定
|
|
223
|
+
[ "$got" = "$mine" ] && continue
|
|
224
|
+
who="$(gate_lock_describe_one "$f")"
|
|
225
|
+
printf '另一个活着的运行登记着**另一个身份**(%s):它的 dev:ino=%s,路径上现在是 %s —— 互斥已断\n' \
|
|
226
|
+
"$who" "$got" "${mine:--}"
|
|
227
|
+
return 1
|
|
228
|
+
done
|
|
229
|
+
return 0
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
# 伙伴记录:读锁记录时优先**伴生锁**边上那份(本版本写的),退回这条路径自己的(P287 之前的写入者
|
|
233
|
+
# 写在 <published>.holder)。返回实际用的路径;空 = 两边都没有可用记录。
|
|
234
|
+
gate_lock_record_source() { # <发布路径> → 打印 "<记录路径>\t<current|legacy|->"
|
|
235
|
+
local pub="$1" comp ch ph comp_any=0 pub_any=0
|
|
236
|
+
comp="$(gate_lock_companion_path "$pub")"
|
|
237
|
+
ch="$(gate_lock_holder_file "$comp")"; ph="$(gate_lock_holder_file "$pub")"
|
|
238
|
+
if [ -s "$ch" ] || compgen -G "$(gate_lock_runs_dir "$comp")/*.run" >/dev/null 2>&1; then comp_any=1; fi
|
|
239
|
+
if [ -s "$ph" ] || compgen -G "$(gate_lock_runs_dir "$pub")/*.run" >/dev/null 2>&1; then pub_any=1; fi
|
|
240
|
+
# ① **活着的**持有者记录优先(本版本写在伴生锁边上;P287 之前的写入者写在发布路径边上)——
|
|
241
|
+
# 不然一条陈旧记录会遮住正在挡着你的那个持有者(实测:34② 的上限与 34⑦ 的点名都被带偏)。
|
|
242
|
+
if [ -s "$ch" ] && gate_lock_record_live "$ch"; then printf '%s\tcurrent\n' "$comp"; return 0; fi
|
|
243
|
+
if [ -s "$ph" ] && gate_lock_record_live "$ph"; then printf '%s\tlegacy\n' "$pub"; return 0; fi
|
|
244
|
+
# ② 没有活记录:谁有东西就报谁(陈旧记录仍会被打印成「陈旧记录」并忽略),发布路径那条优先
|
|
245
|
+
# —— 它是旧写入者(与 P281 时代的夹具)写的地方。
|
|
246
|
+
if [ "$pub_any" = "1" ]; then printf '%s\tlegacy\n' "$pub"; return 0; fi
|
|
247
|
+
if [ "$comp_any" = "1" ]; then printf '%s\tcurrent\n' "$comp"; return 0; fi
|
|
248
|
+
printf '\t-\n'
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
# ---------------------------------------------------------------- marker(「我真的是持有者」的证据)
|
|
252
|
+
# `flock` 的包装进程在**拿到锁之后**写一行 `<epoch> <pid>`:pid 就是被 exec 起来的那次运行的 pid。
|
|
253
|
+
# 这是「本进程就是 flock 的持有者」的唯一证据 —— 环境标记(SMOKE_LOCK_WRAPPED)会被子孙继承,
|
|
254
|
+
# 所以它只配当线索,不配当证明(EXP1:带标记的嵌套运行覆写并删掉了真持有者的记录)。
|
|
255
|
+
gate_lock_marker_write() { # <marker> <pid>
|
|
256
|
+
printf '%s %s\n' "$(gate_lock_now)" "$2" > "$1" 2>/dev/null || true
|
|
257
|
+
}
|
|
258
|
+
gate_lock_marker_pid() { # <marker> → 记的 pid(旧形状只有 epoch / 读不到 → 空)
|
|
259
|
+
local line; line="$(head -1 "$1" 2>/dev/null || true)"
|
|
260
|
+
case "$line" in *' '*) printf '%s\n' "${line##* }" ;; *) printf '\n' ;; esac
|
|
261
|
+
}
|
|
262
|
+
gate_lock_marker_epoch() { # <marker> → 记的 epoch(读不到 → 空)
|
|
263
|
+
local line; line="$(head -1 "$1" 2>/dev/null || true)"
|
|
264
|
+
printf '%s\n' "${line%% *}"
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
# ---------------------------------------------------------------- 记录:写
|
|
268
|
+
gate_lock_record_write() { # <file> <pid> <kind> <cmd> <agent> <worktree> <started> <expected> <bound> <sections> <no_lock> <ancestor> <heartbeat> [<identity> <dev:ino>]
|
|
269
|
+
local f="$1" pid="$2" kind="$3" cmd="$4" agent="$5" wt="$6" started="$7" \
|
|
270
|
+
expected="$8" bound="$9" sections="${10}" no_lock="${11}" ancestor="${12}" hb="${13}" \
|
|
271
|
+
ident="${14:--}" dino="${15:--}"
|
|
272
|
+
local st tmp; st="$(gate_lock_proc_start "$pid" 2>/dev/null || true)"; [ -n "$st" ] || st="-"
|
|
273
|
+
case "$ident" in ''|fd|path) ;; *) ident="-" ;; esac
|
|
274
|
+
case "$dino" in ''|*[!0-9:]*) dino="-" ;; esac
|
|
275
|
+
mkdir -p "$(dirname "$f")" 2>/dev/null || true
|
|
276
|
+
tmp="$f.$$.tmp"
|
|
277
|
+
# 原子写:读的人要么看到旧的一条完整的,要么看到新的一条完整的,不会看到半行
|
|
278
|
+
# P287:末两个字段 = 我持有的那把锁的身份(`lock_identity=fd|path` + `lock_dev_ino=<dev:ino>`);
|
|
279
|
+
# 不持有锁的运行(嵌套 / no_lock)**如实写 `-`**(合同:记不下来就不许编)。
|
|
280
|
+
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' \
|
|
281
|
+
"$(date -Is)" "$pid" "$st" "$cmd" "$kind" "$agent" "$wt" "$started" \
|
|
282
|
+
"$expected" "$bound" "$sections" "$no_lock" "$ancestor" "$hb" "$ident" "$dino" > "$tmp" 2>/dev/null; then
|
|
283
|
+
mv -f "$tmp" "$f" 2>/dev/null || rm -f "$tmp" 2>/dev/null || true
|
|
284
|
+
else
|
|
285
|
+
rm -f "$tmp" 2>/dev/null || true
|
|
286
|
+
fi
|
|
287
|
+
return 0
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
gate_lock_holder_file() { printf '%s\n' "$1.holder"; }
|
|
291
|
+
gate_lock_runs_dir() { printf '%s\n' "$1.runs"; }
|
|
292
|
+
gate_lock_run_file() { printf '%s\n' "$1.runs/$2.run"; }
|
|
293
|
+
|
|
294
|
+
gate_lock_write_holder() { # <lock> <pid> …(字段同 gate_lock_record_write)
|
|
295
|
+
local lock="$1"; shift
|
|
296
|
+
gate_lock_record_write "$(gate_lock_holder_file "$lock")" "$@"
|
|
297
|
+
}
|
|
298
|
+
gate_lock_register_run() { # <lock> <pid> …(字段同 gate_lock_record_write)
|
|
299
|
+
local lock="$1"; shift
|
|
300
|
+
mkdir -p "$(gate_lock_runs_dir "$lock")" 2>/dev/null || true
|
|
301
|
+
gate_lock_record_write "$(gate_lock_run_file "$lock" "$1")" "$@"
|
|
302
|
+
}
|
|
303
|
+
gate_lock_remove_holder() { rm -f "$(gate_lock_holder_file "$1")" 2>/dev/null || true; }
|
|
304
|
+
gate_lock_remove_run() { rm -f "$(gate_lock_run_file "$1" "$2")" 2>/dev/null || true; }
|
|
305
|
+
|
|
306
|
+
# 持有者记录的写权(返修轮次 1 · F4):记录里**活着的 pid** 是唯一不可继承的证明。
|
|
307
|
+
# `SMOKE_LOCK_MARKER` 只是一条环境里的路径,一个继承了它的后代可以改写那个文件、把自己的 pid 填进去
|
|
308
|
+
# ——「自述是持有者」于是变成自证。这里在**写之前**再问一句:记录现在是谁的?
|
|
309
|
+
# 没有记录 / 空 / 缺 pid(旧形状)→ 可以写(不猜,不把「读不到」当拒绝);
|
|
310
|
+
# 记录就是我(重入) → 可以写;
|
|
311
|
+
# 记录是别人但已不活(pid 死了 / 僵尸 / pid 复用后 starttime 不符)→ 陈旧,可以接管;
|
|
312
|
+
# 记录是另一个**活着的**进程 → 不许写、也不许删(真持有者的记录只由它自己删)。
|
|
313
|
+
gate_lock_holder_claimable() { # <lock> <pid> → 0 = 可以写/删 <lock>.holder
|
|
314
|
+
local lock="$1" pid="${2:-$$}" f rp
|
|
315
|
+
f="$(gate_lock_holder_file "$lock")"
|
|
316
|
+
[ -s "$f" ] || return 0
|
|
317
|
+
rp="$(gate_lock_record_get "$f" pid)"
|
|
318
|
+
[ -n "$rp" ] || return 0
|
|
319
|
+
[ "$rp" = "$pid" ] && return 0
|
|
320
|
+
gate_lock_record_live "$f" && return 1
|
|
321
|
+
return 0
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
# ---------------------------------------------------------------- 记录:读
|
|
325
|
+
gate_lock_record_get() { # <file> <key> → 值(缺 → 空)
|
|
326
|
+
local f="$1" k="$2" line v
|
|
327
|
+
line="$(head -1 "$f" 2>/dev/null || true)"
|
|
328
|
+
[ -n "$line" ] || return 0
|
|
329
|
+
line="$(printf '%s' "$line" | tr '\t' ' ')"
|
|
330
|
+
for v in $line; do
|
|
331
|
+
case "$v" in "$k="*) printf '%s\n' "${v#"$k="}"; return 0 ;; esac
|
|
332
|
+
done
|
|
333
|
+
return 0
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
# 祖辈的 section-guard 心跳(返修轮次 1 · F1):沿着**已证明的祖辈 pid** 去读它记录里公布的
|
|
337
|
+
# `heartbeat=`。证明成功时手里就有那个 pid,所以这正是「读记录」而不是「读环境里的一个变量」——
|
|
338
|
+
# 套件自己的夹具会清掉环境变量,那时今日的实现会**无声**退回自己的表预算(V43 的 F1)。
|
|
339
|
+
# 只认**活着的那一条**(pid 相符且 gate_lock_record_live),读不到 → 非 0(调用方按自己的表预算走,
|
|
340
|
+
# 并且必须把这次退回打出来)。
|
|
341
|
+
gate_lock_heartbeat_of_pid() { # <lock> <pid> → 该 pid 记录里公布的 heartbeat 路径(无 → 非 0)
|
|
342
|
+
local lock="$1" pid="${2:-}" f rp hb
|
|
343
|
+
[ -n "$pid" ] || return 1
|
|
344
|
+
for f in "$(gate_lock_holder_file "$lock")" "$(gate_lock_runs_dir "$lock")"/*.run; do
|
|
345
|
+
[ -f "$f" ] || continue
|
|
346
|
+
rp="$(gate_lock_record_get "$f" pid)"
|
|
347
|
+
[ "$rp" = "$pid" ] || continue
|
|
348
|
+
gate_lock_record_live "$f" || continue
|
|
349
|
+
hb="$(gate_lock_record_get "$f" heartbeat)"
|
|
350
|
+
case "$hb" in ''|'-') return 1 ;; esac
|
|
351
|
+
printf '%s\n' "$hb"; return 0
|
|
352
|
+
done
|
|
353
|
+
return 1
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
gate_lock_record_live() { # <file> → 0 = pid 活着且 starttime 相符(没记 start 时只看活着)
|
|
357
|
+
local f="$1" pid st
|
|
358
|
+
pid="$(gate_lock_record_get "$f" pid)"; st="$(gate_lock_record_get "$f" start)"
|
|
359
|
+
[ -n "$pid" ] || return 1
|
|
360
|
+
case "$st" in ''|'-') st="" ;; esac
|
|
361
|
+
gate_lock_pid_alive "$pid" "$st"
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
gate_lock_describe_one() { # <file> → 一个内联片段(缺的字段打 `?`,不编)
|
|
365
|
+
local f="$1" kind pid cmd agent wt started expected bound age="?" now nolock=""
|
|
366
|
+
kind="$(gate_lock_record_get "$f" kind)"; pid="$(gate_lock_record_get "$f" pid)"
|
|
367
|
+
cmd="$(gate_lock_record_get "$f" cmd)"; agent="$(gate_lock_record_get "$f" agent)"
|
|
368
|
+
wt="$(gate_lock_record_get "$f" worktree)"; started="$(gate_lock_record_get "$f" started)"
|
|
369
|
+
expected="$(gate_lock_record_get "$f" expected)"; bound="$(gate_lock_record_get "$f" bound)"
|
|
370
|
+
case "$started" in ''|'-'|*[!0-9]*) ;; *)
|
|
371
|
+
now="$(gate_lock_now)"; age=$(( now - started )); [ "$age" -lt 0 ] && age=0 ;;
|
|
372
|
+
esac
|
|
373
|
+
# 残余窗口(TEAM_SMOKE_NO_LOCK / 没有 flock / FAST)在记录里是 no_lock=1,等待者要能看见它 ——
|
|
374
|
+
# 只在**它是 1** 时打出来(0 的运行不加噪音,行里的字段照旧可逐项断言)。
|
|
375
|
+
[ "$(gate_lock_record_get "$f" no_lock)" = "1" ] && nolock=" no_lock=1"
|
|
376
|
+
# P287:P287 之前的写入者只写 `<ISO> pid=… cmd=…`(没有 kind/席位/上限/身份)—— 有多少字段打多少,
|
|
377
|
+
# 缺的写 `?`,并**标出来**是旧版本的记录(绝不印「持有者未知」)。
|
|
378
|
+
local ver=""
|
|
379
|
+
[ -z "$kind" ] && ver=" version=legacy" # 旧写入者的记录没有 kind —— 有多少字段打多少,并标出来
|
|
380
|
+
printf 'kind=%s pid=%s cmd=%s agent=%s worktree=%s 已跑 %ss expected=%s bound=%s%s%s' \
|
|
381
|
+
"${kind:-?}" "${pid:-?}" "${cmd:-?}" "${agent:-?}" "${wt:-?}" "$age" "${expected:-?}" "${bound:-?}" "$nolock" "$ver"
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
# <发布路径> → 一行:优先伴生锁边上的记录(本版本);没有就退回发布路径自己的(旧写入者,标 legacy)
|
|
385
|
+
gate_lock_describe_pair() {
|
|
386
|
+
local pub="$1" src kind
|
|
387
|
+
IFS=$'\t' read -r src kind < <(gate_lock_record_source "$pub")
|
|
388
|
+
if [ -z "$src" ]; then printf '持有者未知\n'; return 0; fi
|
|
389
|
+
gate_lock_describe "$src"
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
# <发布路径> <wait> → gate_lock_cap 的同一行,但记录优先读伴生锁边上的那份(旧写入者的退回发布路径)
|
|
393
|
+
gate_lock_cap_pair() {
|
|
394
|
+
local pub="$1" src kind
|
|
395
|
+
IFS=$'\t' read -r src kind < <(gate_lock_record_source "$pub")
|
|
396
|
+
[ -n "$src" ] || src="$pub"
|
|
397
|
+
gate_lock_cap "$src" "$2"
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
gate_lock_describe() { # <lock> → 一行:活着的记录逐个列出;陈旧条目标 `stale` 并忽略
|
|
401
|
+
local lock="$1" f pid seen=" " live="" stale="" n=0
|
|
402
|
+
for f in "$(gate_lock_holder_file "$lock")" "$(gate_lock_runs_dir "$lock")"/*.run; do
|
|
403
|
+
[ -f "$f" ] || continue
|
|
404
|
+
pid="$(gate_lock_record_get "$f" pid)"; [ -n "$pid" ] || pid="?"
|
|
405
|
+
case "$seen" in *" $pid "*) continue ;; esac
|
|
406
|
+
seen="$seen$pid "
|
|
407
|
+
if gate_lock_record_live "$f"; then
|
|
408
|
+
live="${live:+$live;}$(gate_lock_describe_one "$f")"; n=$((n + 1))
|
|
409
|
+
else
|
|
410
|
+
stale="${stale:+$stale;}$(gate_lock_describe_one "$f")"
|
|
411
|
+
fi
|
|
412
|
+
done
|
|
413
|
+
if [ "$n" -eq 0 ]; then
|
|
414
|
+
printf '持有者未知%s\n' "${stale:+(陈旧记录:$stale)}"
|
|
415
|
+
else
|
|
416
|
+
printf '%s%s\n' "$live" "${stale:+(陈旧记录:$stale)}"
|
|
417
|
+
fi
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
# ---------------------------------------------------------------- 祖辈证明(嵌套判定的唯一依据)
|
|
421
|
+
gate_lock_ancestry_chain() { # [<起点 pid>] → 祖先 pid,从近到远(不含起点;没有 /proc → 空)
|
|
422
|
+
# 起点默认是**本脚本进程**(`$$`):这个函数按惯例在 `$( )` 里跑,`BASHPID` 会是那个子 shell
|
|
423
|
+
# 的 pid,于是「自己的父进程」会被当成「祖辈」——实测过,是真阳性的假证(P281 自测抓到)。
|
|
424
|
+
local pid="${1:-$$}" i=0 pp
|
|
425
|
+
while [ "$i" -lt 64 ]; do
|
|
426
|
+
pp="$(gate_lock_ppid "$pid" 2>/dev/null || true)"
|
|
427
|
+
case "$pp" in ''|0|1) break ;; esac
|
|
428
|
+
printf '%s\n' "$pp"
|
|
429
|
+
pid="$pp"; i=$((i + 1))
|
|
430
|
+
done
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
gate_lock_prove_ancestor() { # <lock> → 最近的「记录里活着且 starttime 相符」的祖辈 pid;证不出 → 空
|
|
434
|
+
local lock="$1" p st f rp rs
|
|
435
|
+
for p in $(gate_lock_ancestry_chain "$$"); do
|
|
436
|
+
st="$(gate_lock_proc_start "$p" 2>/dev/null || true)"
|
|
437
|
+
for f in "$(gate_lock_holder_file "$lock")" "$(gate_lock_runs_dir "$lock")"/*.run; do
|
|
438
|
+
[ -f "$f" ] || continue
|
|
439
|
+
rp="$(gate_lock_record_get "$f" pid)"; rs="$(gate_lock_record_get "$f" start)"
|
|
440
|
+
[ -n "$rp" ] && [ "$rp" = "$p" ] || continue
|
|
441
|
+
case "$rs" in ''|'-') ;; *) [ "$rs" = "$st" ] || continue ;; esac
|
|
442
|
+
printf '%s\n' "$p"; return 0
|
|
443
|
+
done
|
|
444
|
+
done
|
|
445
|
+
return 1
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
# <发布路径>:祖辈证明与心跳都按**记录实际所在的那一侧**读(伴生锁优先,退回发布路径)
|
|
449
|
+
gate_lock_prove_ancestor_pair() {
|
|
450
|
+
local pub="$1" src kind
|
|
451
|
+
IFS=$'\t' read -r src kind < <(gate_lock_record_source "$pub")
|
|
452
|
+
[ -n "$src" ] || return 1
|
|
453
|
+
gate_lock_prove_ancestor "$src"
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
gate_lock_heartbeat_of_pid_pair() { # <发布路径> <祖辈 pid>
|
|
457
|
+
local pub="$1" src kind
|
|
458
|
+
IFS=$'\t' read -r src kind < <(gate_lock_record_source "$pub")
|
|
459
|
+
[ -n "$src" ] || return 1
|
|
460
|
+
gate_lock_heartbeat_of_pid "$src" "$2"
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
# ---------------------------------------------------------------- 上限算式(D3)
|
|
464
|
+
# cap = clamp(remaining + 60, W, 4W),remaining = expected − (now − started)
|
|
465
|
+
# 记录里没有 expected(旧写入者 / 读不到)→ 退回今天的 W(一字不改)。
|
|
466
|
+
gate_lock_cap() { # <lock> <wait> → "cap<TAB>remaining<TAB>expected<TAB>started"(未知的写 `-`)
|
|
467
|
+
local lock="$1" wait="$2" expected started remaining cap now
|
|
468
|
+
expected="$(gate_lock_record_get "$(gate_lock_holder_file "$lock")" expected)"
|
|
469
|
+
started="$(gate_lock_record_get "$(gate_lock_holder_file "$lock")" started)"
|
|
470
|
+
case "$expected" in ''|'-'|*[!0-9]*) printf '%s\t-\t-\t-\n' "$wait"; return 0 ;; esac
|
|
471
|
+
case "$started" in ''|'-'|*[!0-9]*) printf '%s\t-\t%s\t-\n' "$wait" "$expected"; return 0 ;; esac
|
|
472
|
+
now="$(gate_lock_now)"; remaining=$(( expected - (now - started) ))
|
|
473
|
+
cap=$(( remaining + 60 ))
|
|
474
|
+
[ "$cap" -lt "$wait" ] && cap="$wait"
|
|
475
|
+
[ "$cap" -gt "$(( wait * 4 ))" ] && cap="$(( wait * 4 ))"
|
|
476
|
+
printf '%s\t%s\t%s\t%s\n' "$cap" "$remaining" "$expected" "$started"
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
# ---------------------------------------------------------------- 席位 / 工作树(team notify 同一条规则)
|
|
480
|
+
# 运行时目录 = cwd 的 git toplevel;主检出 → `pm`;`<main>/<worktrees>/<名字>` 之下 → 那个一级目录名
|
|
481
|
+
# (名册里真有这个名字时用名册的拼写)。**绝不读窗口名**(tmux-identity-scope 的教训)。
|
|
482
|
+
gate_lock_worktree_of_dir() { # <dir> → 绝对路径(解析不出 → `?`)
|
|
483
|
+
local top
|
|
484
|
+
top="$(cd "$1" 2>/dev/null && git rev-parse --path-format=absolute --show-toplevel 2>/dev/null || true)"
|
|
485
|
+
[ -n "$top" ] || { printf '?\n'; return 0; }
|
|
486
|
+
printf '%s\n' "$top"
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
gate_lock_seat_of_dir() { # <dir> [<roster>] [<worktrees 相对/绝对>] [<main root>] → 席位(`?` = 解析不出)
|
|
490
|
+
local dir="$1" roster="${2:-${TEAM_AGENTS:-}}" wtr="${3:-${TEAM_WORKTREES_DIR:-.worktrees}}" main="${4:-}"
|
|
491
|
+
local top common n a
|
|
492
|
+
top="$(cd "$dir" 2>/dev/null && git rev-parse --path-format=absolute --show-toplevel 2>/dev/null || true)"
|
|
493
|
+
[ -n "$top" ] || { printf '?\n'; return 0; }
|
|
494
|
+
if [ -z "$main" ]; then
|
|
495
|
+
common="$(cd "$dir" 2>/dev/null && git rev-parse --path-format=absolute --git-common-dir 2>/dev/null || true)"
|
|
496
|
+
[ -n "$common" ] && main="$(dirname "$common")"
|
|
497
|
+
fi
|
|
498
|
+
[ -n "$main" ] || { printf '?\n'; return 0; }
|
|
499
|
+
top="$(cd "$top" 2>/dev/null && pwd -P || printf '%s' "$top")"
|
|
500
|
+
main="$(cd "$main" 2>/dev/null && pwd -P || printf '%s' "$main")"
|
|
501
|
+
case "$wtr" in /*) ;; *) wtr="$main/$wtr" ;; esac
|
|
502
|
+
[ "$top" = "$main" ] && { printf 'pm\n'; return 0; }
|
|
503
|
+
case "$top" in
|
|
504
|
+
"$wtr"/*) n="${top#"$wtr"/}"; n="${n%%/*}" ;;
|
|
505
|
+
*) printf '?\n'; return 0 ;;
|
|
506
|
+
esac
|
|
507
|
+
[ -n "$n" ] || { printf '?\n'; return 0; }
|
|
508
|
+
for a in $roster; do [ "$n" = "$a" ] && { printf '%s\n' "$a"; return 0; }; done
|
|
509
|
+
printf '%s\n' "$n"
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
# ---------------------------------------------------------------- 期望用时(Σband / Σbudget / 段数)
|
|
513
|
+
# 段清单从**正在跑的那份脚本**里读(选段副本与全量同一份实现),键按 `section "…"` 的 id 与预算表
|
|
514
|
+
# 对齐;表里没登记的段用有界默认(同 section-guard 的 SG_DEFAULT_BUDGET)。
|
|
515
|
+
gate_lock_expected() { # <suite-file> <budgets-file> [<默认预算>] → "expected<TAB>bound<TAB>sections"
|
|
516
|
+
local suite="$1" budgets="$2" dflt="${3:-900}" line ids="" n=0
|
|
517
|
+
[ -r "$suite" ] || { printf '0\t0\t0\n'; return 0; }
|
|
518
|
+
while IFS= read -r line; do
|
|
519
|
+
case "$line" in
|
|
520
|
+
*'section "'*)
|
|
521
|
+
if [[ $line =~ ^[[:space:]]*section[[:space:]]+\"([^\"]+)\" ]]; then
|
|
522
|
+
ids="${ids}${ids:+$'\n'}${BASH_REMATCH[1]}"; n=$((n + 1))
|
|
523
|
+
fi ;;
|
|
524
|
+
esac
|
|
525
|
+
done < "$suite"
|
|
526
|
+
# 一次 awk 读完(旧写法每段起一个 awk:129 段 ≈ 129 个进程,嵌套运行里肉眼可见地慢)
|
|
527
|
+
# 注意:这里不能用 `NR==FNR` 的「第一份输入」技巧 —— 第一份是 stdin(`-`),awk 不保证 FNR 会归零。
|
|
528
|
+
[ -r "$budgets" ] || { printf '0\t0\t%s\n' "$n"; return 0; }
|
|
529
|
+
awk -F'\t' -v dflt="$dflt" -v n="$n" -v wantstr="$ids" '
|
|
530
|
+
BEGIN { nw = split(wantstr, W, "\n"); for (i = 1; i <= nw; i++) want[W[i]] = 1 }
|
|
531
|
+
/^[[:space:]]*#/ { next }
|
|
532
|
+
($1 in want) {
|
|
533
|
+
b = $2; u = $3
|
|
534
|
+
if (b ~ /^[0-9.]+$/) bands += b
|
|
535
|
+
if (u ~ /^[0-9]+$/) bud += u; else bud += dflt
|
|
536
|
+
seen[$1] = 1
|
|
537
|
+
next
|
|
538
|
+
}
|
|
539
|
+
END {
|
|
540
|
+
for (k in want) if (!(k in seen)) bud += dflt
|
|
541
|
+
printf "%d\t%d\t%d\n", bands + 0.5, bud, n
|
|
542
|
+
}
|
|
543
|
+
' "$budgets" || printf '0\t0\t%s\n' "$n"
|
|
544
|
+
}
|
|
@@ -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
|
-
|
|
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
|