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.
- package/README.md +4 -4
- package/package.json +1 -1
- package/skills/teamsmith/CHANGELOG.md +69 -0
- package/skills/teamsmith/SKILL.md +20 -13
- package/skills/teamsmith/extension/team-inbox-watch.ts +5 -3
- 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 +201 -30
- package/skills/teamsmith/references/troubleshooting.md +130 -10
- 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 +182 -32
- package/skills/teamsmith/scripts/lib/cmd-status.sh +234 -21
- 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 +1004 -245
- package/skills/teamsmith/scripts/lib/gate-lock.sh +582 -0
- package/skills/teamsmith/scripts/panel/draft-send.sh +11 -1
- package/skills/teamsmith/scripts/panel/panel.js +211 -211
- package/skills/teamsmith/scripts/panel/src/App.tsx +58 -21
- package/skills/teamsmith/scripts/panel/src/clipboard.ts +12 -5
- package/skills/teamsmith/scripts/panel/src/compose.ts +6 -3
- package/skills/teamsmith/scripts/panel/src/data.ts +51 -5
- package/skills/teamsmith/scripts/panel/src/layout.ts +209 -69
- package/skills/teamsmith/scripts/panel/src/main.tsx +37 -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/killall +10 -0
- package/skills/teamsmith/scripts/shim/pgrep +10 -0
- package/skills/teamsmith/scripts/shim/pidof +10 -0
- package/skills/teamsmith/scripts/shim/pkill +10 -0
- package/skills/teamsmith/scripts/shim/signal-gate +108 -31
- 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/README.md +31 -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 +83 -54
- package/skills/teamsmith/tests/config-cli.sh +184 -32
- 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-p158.sh +2 -1
- package/skills/teamsmith/tests/flip-p308-skip-not-pass.sh +151 -0
- package/skills/teamsmith/tests/flip-p310-lock.sh +210 -0
- package/skills/teamsmith/tests/flip-p310-socket.sh +125 -0
- package/skills/teamsmith/tests/gate-guard.sh +52 -79
- package/skills/teamsmith/tests/help-surface.sh +1664 -0
- package/skills/teamsmith/tests/install-shape.sh +359 -22
- package/skills/teamsmith/tests/key-replies-pty.sh +9 -10
- package/skills/teamsmith/tests/key-replies-ui.sh +2 -8
- package/skills/teamsmith/tests/key-replies.sh +281 -28
- 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/pty-wait.sh +21 -111
- package/skills/teamsmith/tests/lib/reply-fixture.sh +4 -9
- package/skills/teamsmith/tests/lib/section-guard.sh +316 -164
- 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 +83 -77
- package/skills/teamsmith/tests/panel-b2.sh +430 -352
- package/skills/teamsmith/tests/panel-b3-stub.sh +112 -4
- package/skills/teamsmith/tests/panel-b3.sh +314 -62
- package/skills/teamsmith/tests/panel-choices.sh +11 -6
- package/skills/teamsmith/tests/panel-cpu.sh +43 -130
- package/skills/teamsmith/tests/panel-p21.sh +251 -265
- package/skills/teamsmith/tests/panel-snapshots.sh +41 -225
- package/skills/teamsmith/tests/panel-strings.mjs +33 -18
- package/skills/teamsmith/tests/perf.sh +187 -396
- package/skills/teamsmith/tests/routes.sh +440 -117
- package/skills/teamsmith/tests/seat-interrupt.sh +1012 -0
- package/skills/teamsmith/tests/seat-visibility.sh +675 -0
- package/skills/teamsmith/tests/section-guard.sh +13 -102
- package/skills/teamsmith/tests/section-needs-audit.sh +24 -7
- package/skills/teamsmith/tests/section-paths.tsv +32 -18
- package/skills/teamsmith/tests/section-select.sh +371 -19
- package/skills/teamsmith/tests/signal-gate.sh +249 -27
- package/skills/teamsmith/tests/signal-lint-legacy.txt +4 -1
- package/skills/teamsmith/tests/slash-team-e2e.sh +323 -0
- package/skills/teamsmith/tests/slash-team.sh +617 -0
- package/skills/teamsmith/tests/slice-coverage.sh +206 -0
- package/skills/teamsmith/tests/smoke.sh +3595 -788
- package/skills/teamsmith/tests/task-model-routing.sh +530 -15
- package/skills/teamsmith/tests/team-bg-stop.sh +18 -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/tests/workflow-entry.sh +207 -0
- package/skills/teamsmith-init/SKILL.md +12 -12
- package/skills/teamsmith-init/references/bootstrap.md +3 -3
- package/skills/teamsmith/tests/flip-glue.sh +0 -162
- package/skills/teamsmith/tests/flip-m12-26c.sh +0 -129
- package/skills/teamsmith/tests/flip-m12.sh +0 -185
- package/skills/teamsmith/tests/flip-m16.sh +0 -166
- package/skills/teamsmith/tests/flip-m17.sh +0 -204
- package/skills/teamsmith/tests/flip-m34.sh +0 -96
- package/skills/teamsmith/tests/flip-m37.sh +0 -185
- package/skills/teamsmith/tests/flip-m4.3.sh +0 -375
- package/skills/teamsmith/tests/flip-m43.sh +0 -128
- package/skills/teamsmith/tests/flip-m45.sh +0 -244
- package/skills/teamsmith/tests/flip-m46.sh +0 -238
- package/skills/teamsmith/tests/flip-m48.sh +0 -304
- package/skills/teamsmith/tests/flip-m6.2.sh +0 -205
- package/skills/teamsmith/tests/flip-m6.3.sh +0 -283
- package/skills/teamsmith/tests/flip-m6.4.sh +0 -267
- package/skills/teamsmith/tests/flip-m6.5.sh +0 -113
- package/skills/teamsmith/tests/flip-m7.1.sh +0 -135
- package/skills/teamsmith/tests/flip-m7.2.sh +0 -321
- package/skills/teamsmith/tests/flip-p144.sh +0 -112
- package/skills/teamsmith/tests/flip-p156.sh +0 -133
- package/skills/teamsmith/tests/flip-p160.sh +0 -182
- package/skills/teamsmith/tests/flip-p171.sh +0 -249
- package/skills/teamsmith/tests/flip-p18.1.sh +0 -95
- package/skills/teamsmith/tests/flip-p20.sh +0 -286
- package/skills/teamsmith/tests/flip-p201.sh +0 -249
- package/skills/teamsmith/tests/flip-p210.sh +0 -239
- package/skills/teamsmith/tests/flip-p211.sh +0 -152
- package/skills/teamsmith/tests/flip-p217.sh +0 -288
- package/skills/teamsmith/tests/flip-p22.sh +0 -123
- package/skills/teamsmith/tests/flip-p23.sh +0 -239
- package/skills/teamsmith/tests/flip-p25.sh +0 -165
- package/skills/teamsmith/tests/flip-p71.sh +0 -184
- package/skills/teamsmith/tests/flip-p72.sh +0 -202
- package/skills/teamsmith/tests/load-experiment.sh +0 -290
- package/skills/teamsmith/tests/panel-cpu-premise.sh +0 -173
- package/skills/teamsmith/tests/panel-flip-m54.sh +0 -207
- package/skills/teamsmith/tests/panel-flip-p123.sh +0 -278
- package/skills/teamsmith/tests/panel-flip-p125.sh +0 -318
- package/skills/teamsmith/tests/panel-flip-p128.sh +0 -259
- package/skills/teamsmith/tests/panel-keyprobe-stub.sh +0 -41
- package/skills/teamsmith/tests/panel-keyprobe.sh +0 -82
- package/skills/teamsmith/tests/panel-knobs.sh +0 -83
- package/skills/teamsmith/tests/section-budgets.tsv +0 -154
- package/skills/teamsmith/tests/team-bg-flip.sh +0 -239
- package/skills/teamsmith/tests/team-inbox-watch-flip.sh +0 -191
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
#
|
|
4
4
|
# bash skills/teamsmith/tests/tmp-hygiene.sh --status # 只读清单(永远 exit 0)
|
|
5
5
|
# bash skills/teamsmith/tests/tmp-hygiene.sh --lint [--dir D] # 静态检查:有 finding → exit 1
|
|
6
|
+
# bash skills/teamsmith/tests/tmp-hygiene.sh --docs-check [--dir D] # 文档面一致性(P314)→ exit 1
|
|
6
7
|
# bash skills/teamsmith/tests/tmp-hygiene.sh --sweep [--dry-run] [--age N] [--tmux-sockets]
|
|
7
8
|
# bash skills/teamsmith/tests/tmp-hygiene.sh --self-test [--break=<stage>]
|
|
8
9
|
#
|
|
@@ -31,24 +32,69 @@
|
|
|
31
32
|
# 与**陈旧 socket**(非 default、`ss -xl` 无监听、年龄下限)的计数;`--sweep` 默认只报不动,
|
|
32
33
|
# `--tmux-sockets` 才清;**default 永不触碰**(socket 与 server 都是)。
|
|
33
34
|
# - 台账里还活着的根(门禁的泄漏断言用)由 tests/lib/tmp-root.sh 写;本脚本不碰它。
|
|
35
|
+
# - **已删除但仍被持有**的文件(P313):`--status` 报合计字节 + 持有进程数(只报总量,别家的 pid 不
|
|
36
|
+
# 点名),并把「可归属本项目」(删除目标的路径带本项目 ns)里**父进程已不在**的那部分单独报出来。
|
|
37
|
+
# `--sweep` 只对**可归属 + 父进程已不在**的持有者给出「建议结束」的计划 —— 父链还在的持有者是某个
|
|
38
|
+
# 正在跑的夹具在用,建议结束它等于建议打断别人的运行(2026-10-18 交付时现场就有一个:ppid=1、
|
|
39
|
+
# 已跑 50 分钟、持着 2 个已删 fd);仍要求显式 pid —— 本脚本绝不发信号。
|
|
40
|
+
# 现场:78 个孤儿心跳进程持有 11.98 GB 已删除文件。
|
|
41
|
+
# - **命名空间与锁对**(P287):活运行**为自己**直接落在临时根上的东西,要么点开头(台账 / pid /
|
|
42
|
+
# 诊断),要么带本项目的 ns(`<slug>-<hash8>`);根是 `teamsmith-<kind>-<ns>.XXXXXX`。`--sweep` 的
|
|
43
|
+
# 候选谓词不动(根仍是家族目录 + 台账归因),`--status` 多一列 ns,并把**锁对**按角色报出来
|
|
44
|
+
# (发布路径 = 兼容声明 / 伴生锁 = 运行互斥 / `<companion>.runs/` = 运行注册)。`--lint` 的规则
|
|
45
|
+
# 从「`mktemp -d` 根模板在前缀家族里」扩成「直接落在解析后临时根上的每一个模板:点开头或带 ns;
|
|
46
|
+
# 根还要在家族里」,作用域从 tests/** 扩到整棵 skill 树(夹具 **与** 产品自己的临时模板)。
|
|
34
47
|
#
|
|
35
48
|
# 内部旋钮(夹具自检/门禁红侧用,生产路径不设):
|
|
36
49
|
# TEAM_TMP_SWEEP_AGE 默认 30(分钟);`--age` 覆盖
|
|
37
50
|
# TEAM_TMP_HYGIENE_REPO 自检用:指定「主仓库」(默认本脚本所在仓库)——worktree/记录/归属判定的基准
|
|
38
51
|
# TEAM_TMP_HYGIENE_TMUX_DIR 自检用:指定要扫的 tmux socket 目录(默认按 $TMUX/TMUX_TMPDIR 解析)
|
|
39
52
|
# TEAM_TMP_HYGIENE_TMUX_FAKE_SERVERS 自检用:用夹具文件(pid<TAB>socket)代替 /proc 找孤儿私有 server
|
|
53
|
+
# TEAM_TMP_HYGIENE_HELD_ROOTS 自检用:限制「已删除但仍被持有」扫描的根(默认 = 解析出的临时根 +
|
|
54
|
+
# 旧默认根;P309 之前所有运行都写在旧默认根,P313 的泄漏就在 /tmp 里)
|
|
40
55
|
# TEAM_TMP_HYGIENE_FLIP 只在 TEAM_SMOKE_FIXTURE=1 时生效:noproc/occupied/orphan/foreign/lock/
|
|
41
|
-
# worktree/record/lint/reviewproof/ledgerrepo/blockskip/tmuxtouch
|
|
56
|
+
# worktree/record/lint/reviewproof/ledgerrepo/blockskip/tmuxtouch/held/heldpath
|
|
42
57
|
# (把对应规则变回有缺陷的旧行为,供翻转)
|
|
43
58
|
set -uo pipefail
|
|
44
59
|
|
|
45
60
|
SELF_DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
46
61
|
SKILL_DIR="$(cd -P "$SELF_DIR/.." && pwd)"
|
|
47
|
-
|
|
62
|
+
|
|
63
|
+
# P287:ns(项目命名空间)与伴生锁路径的**唯一实现**在 scripts/lib/ 下那两处 —— 本脚本只调它们。
|
|
64
|
+
if ! declare -F team_tmp_root >/dev/null 2>&1 && [ -f "$SKILL_DIR/scripts/lib/common.sh" ]; then
|
|
65
|
+
# shellcheck source=../scripts/lib/common.sh
|
|
66
|
+
. "$SKILL_DIR/scripts/lib/common.sh"
|
|
67
|
+
fi
|
|
68
|
+
|
|
69
|
+
# P309:基准 = 产品的**唯一**解析器(TMPDIR → TEAM_TMP_ROOT → 磁盘默认 /var/tmp/teamsmith-<ns>)。
|
|
70
|
+
# 清扫器不自己算这个根:两处各算 = 两个答案 = 清扫器与门禁对不上基准(--lint 的第二条规则钉住它)。
|
|
71
|
+
# 自检红侧 `TEAM_TMP_HYGIENE_BASE_SELF=1` 把「自己算基准」这个缺陷形状放回来(断言必须因此变红)。
|
|
72
|
+
if [ "${TEAM_TMP_HYGIENE_BASE_SELF:-0}" = "1" ]; then
|
|
73
|
+
BASE="${TMPDIR:-/tmp}"; BASE_SRC="self"
|
|
74
|
+
else
|
|
75
|
+
BASE="$(team_tmp_root)" || { printf '✗ 临时根解析失败(--status 需要可用的解析器)\n' >&2; exit 3; }
|
|
76
|
+
BASE_SRC="$(team_tmp_root_source 2>/dev/null || printf 'default')"
|
|
77
|
+
fi
|
|
48
78
|
# BASE 归一化成绝对路径(worktree 列表、/proc 链接都是绝对路径;相对 TMPDIR 会让比对全错)
|
|
49
79
|
BASE_ABS="$(cd "$BASE" 2>/dev/null && pwd || printf '%s' "$BASE")"
|
|
50
80
|
[ -n "$BASE_ABS" ] && BASE="$BASE_ABS"
|
|
51
81
|
FAMILY_RE='^(teamsmith-|review-)'
|
|
82
|
+
_hy_base_note() { # 一行「它是怎么解析出来的」(含 TMPDIR 盖过项目钉的值时的分歧)
|
|
83
|
+
if [ "$BASE_SRC" = "self" ]; then printf 'source=self(自算:缺陷形状)'; return 0; fi
|
|
84
|
+
team_tmp_root_note 2>/dev/null || printf 'source=%s' "$BASE_SRC"
|
|
85
|
+
}
|
|
86
|
+
if [ -z "${GATE_LOCK_LIB:-}" ] && [ -f "$SKILL_DIR/scripts/lib/gate-lock.sh" ]; then
|
|
87
|
+
# shellcheck source=../scripts/lib/gate-lock.sh
|
|
88
|
+
. "$SKILL_DIR/scripts/lib/gate-lock.sh"
|
|
89
|
+
fi
|
|
90
|
+
_hy_ns() { team_project_ns 2>/dev/null || printf 'nosig-00000000'; }
|
|
91
|
+
_hy_companion() { # <发布路径> → 伴生锁(机器锁)路径;库不在时按公式退一步
|
|
92
|
+
if declare -F gate_lock_companion_path >/dev/null 2>&1; then
|
|
93
|
+
gate_lock_companion_path "$1"
|
|
94
|
+
else
|
|
95
|
+
printf '%s/.%s.machine\n' "$(dirname "$1")" "$(basename "$1")"
|
|
96
|
+
fi
|
|
97
|
+
}
|
|
52
98
|
|
|
53
99
|
FLIP=""
|
|
54
100
|
[ "${TEAM_SMOKE_FIXTURE:-0}" = "1" ] && FLIP="${TEAM_TMP_HYGIENE_FLIP:-}"
|
|
@@ -60,14 +106,22 @@ bad() { printf ' \033[31m✗\033[0m %s\n' "$1"; FAIL=$((FAIL + 1)); }
|
|
|
60
106
|
_usage() {
|
|
61
107
|
cat <<'EOF'
|
|
62
108
|
用法:bash tests/tmp-hygiene.sh <verb> [选项]
|
|
63
|
-
--status 只读清单(根 / 归属未证·别家(逐条点名) / 不是根 / 占用 / 余量 /
|
|
64
|
-
|
|
109
|
+
--status 只读清单(根 / 归属未证·别家(逐条点名) / 不是根 / 占用 / 余量 / 工具身份 /
|
|
110
|
+
已删除但仍被持有的文件(P313:合计字节 + 进程数,只报总量));永远 exit 0
|
|
111
|
+
--lint [--dir D] 静态检查 mktemp 模板(默认整棵 skill 树):直接落在解析后临时根上的模板
|
|
112
|
+
必须点开头或带本项目的 ns,根还要在 owned 家族里;有 finding → exit 1
|
|
113
|
+
--docs-check [--dir D] 文档面一致性(默认整棵 skill 树):默认根 / 解析来源 / 回滚口令必须写清,
|
|
114
|
+
老行为的稳定 token(${TMPDIR:-/tmp}、旧根名、tmpfs 旧读数)不许残留;
|
|
115
|
+
有残留或缺 token → exit 1
|
|
65
116
|
--sweep [--dry-run] [--age N] [--tmux-sockets]
|
|
66
117
|
回收陈旧、无占用的根(默认 30 分钟 / TEAM_TMP_SWEEP_AGE);
|
|
118
|
+
已删除但仍被持有的文件:对**可归属本项目、且父进程已不在**的持有者
|
|
119
|
+
只给「建议结束」的计划(活着的运行在用它们 → 不建议;仍要显式 pid
|
|
120
|
+
—— 本脚本绝不发信号);
|
|
67
121
|
别家 / 归属不明的目录逐条点名、永不碰;
|
|
68
122
|
--tmux-sockets 连 tmux 侧残留一起清(默认只报不动;default 永不触碰)
|
|
69
123
|
--self-test [--break=…] 红侧自检(noproc/occupied/orphan/foreign/lock/worktree/record/lint/
|
|
70
|
-
reviewproof/ledgerrepo/blockskip/tmuxtouch)
|
|
124
|
+
reviewproof/ledgerrepo/blockskip/tmuxtouch/held)
|
|
71
125
|
exit: 0 做了事(有跳过也照常回收其余;逐条列名) · 1 lint 有 finding · 3 一个都没做(全被挡下 / 别家 / 不明)
|
|
72
126
|
EOF
|
|
73
127
|
}
|
|
@@ -181,6 +235,218 @@ _occupants_of() { # <root> → "pid(what), pid(what)";非空即占用;"无
|
|
|
181
235
|
printf '%s\n' "$out"
|
|
182
236
|
}
|
|
183
237
|
|
|
238
|
+
# ---------------------------------------------------------------- 已删除但仍被持有(P313)
|
|
239
|
+
# 「根」的扫描只看得到还在磁盘上的东西:被删掉的日志 / 临时文件只要还有进程开着 fd,块就归还不了 ——
|
|
240
|
+
# `df` 与 `du` 的差额正是这里。现场(2026-10-18):78 个孤儿心跳进程合计持有 **11.98 GB** 已删除文件,
|
|
241
|
+
# 而它们写入的目标(夹具的临时根)早就被清理了。
|
|
242
|
+
# 口径:只统计**删除目标的路径在临时根下**、且读得到的 fd(同用户);字节按 inode 去重(同一个文件被
|
|
243
|
+
# 多个 fd 打开只算一次),进程按 pid 去重。归属只按路径:删除目标所在的路径里,**某一层目录名**是本
|
|
244
|
+
# 项目的运行根(`teamsmith-*-<ns>.*`,P287)才算「可归属本项目」—— 别家的持有者只进合计,**不点名
|
|
245
|
+
# pid**。`--sweep` 对可归属者只给「建议结束」的计划,**计划行点名被持有的目标路径**(扫描时就有,不拿
|
|
246
|
+
# argv 的前 140 字节顶替 —— argv 里常常一个路径字符都没有),绝不自己发信号(动手要显式 pid)。
|
|
247
|
+
_HO_OK=0
|
|
248
|
+
_HO_PROCS=0; _HO_FILES=0; _HO_BYTES=0; _HO_OWN_PROCS=0; _HO_OWN_BYTES=0
|
|
249
|
+
_HO_ORPH_PROCS=0; _HO_ORPH_BYTES=0
|
|
250
|
+
_HO_PIDS=(); _HO_PID_BYTES=(); _HO_PID_FILES=(); _HO_PID_OWN=(); _HO_PID_ARGV=(); _HO_PID_ORPH=()
|
|
251
|
+
_HO_PID_PATHS=() # 每个 pid 持有的已删目标路径(换行分隔,按 inode 去重;计划行点名它)
|
|
252
|
+
# 父进程还在吗?僵尸按「不在」算(与 lib/bounded-spin.sh 同一口径)。
|
|
253
|
+
# 「父进程已不在」是这个泄漏的**签名**:被打断的运行把心跳孤儿留在 init 名下(ppid=1),
|
|
254
|
+
# 而它的写目标早已被清理。活着的运行(父链还在)不该被建议结束 —— 那正是夹具在用的东西。
|
|
255
|
+
_ho_parent_alive() { # <ppid>
|
|
256
|
+
local pid="${1:-}" st
|
|
257
|
+
case "$pid" in ''|0|*[!0-9]*) return 1 ;; esac
|
|
258
|
+
[ -d /proc/1 ] || { kill -0 "$pid" 2>/dev/null; return; }
|
|
259
|
+
[ -r "/proc/$pid/stat" ] || return 1
|
|
260
|
+
st="$(awk '{ sub(/^.*\) /, ""); print $1 }' "/proc/$pid/stat" 2>/dev/null || true)"
|
|
261
|
+
[ "$st" = "Z" ] && return 1
|
|
262
|
+
return 0
|
|
263
|
+
}
|
|
264
|
+
_ho_human_bytes() { # <bytes> → 人话(1024 进制,与 human_kb 同口径)
|
|
265
|
+
awk -v b="${1:-0}" 'BEGIN {
|
|
266
|
+
if (b >= 1073741824) printf "%.1f GB", b / 1073741824
|
|
267
|
+
else if (b >= 1048576) printf "%.1f MB", b / 1048576
|
|
268
|
+
else if (b >= 1024) printf "%.1f KB", b / 1024
|
|
269
|
+
else printf "%d B", b
|
|
270
|
+
}'
|
|
271
|
+
}
|
|
272
|
+
_HO_ROOTS=()
|
|
273
|
+
_ho_roots() { # 扫哪些根:解析出的临时根 + $TMPDIR + 旧默认根 /tmp(P313 的泄漏就在 /tmp)
|
|
274
|
+
if [ -n "${TEAM_TMP_HYGIENE_HELD_ROOTS:-}" ]; then
|
|
275
|
+
local r
|
|
276
|
+
for r in $TEAM_TMP_HYGIENE_HELD_ROOTS; do [ -n "$r" ] && printf '%s\n' "$r"; done
|
|
277
|
+
return 0
|
|
278
|
+
fi
|
|
279
|
+
local r legacy
|
|
280
|
+
{
|
|
281
|
+
# 解析出的根本身,以及它的**基**(P309 默认 /var/tmp):验证者的副本(/var/tmp/<run>/teamsmith-…)、
|
|
282
|
+
# 旧默认根 /tmp、显式 TMPDIR —— 泄漏可能在任何一个里,而 df 与 du 的差额是全盘的事。
|
|
283
|
+
printf '%s\n' "$BASE"
|
|
284
|
+
printf '%s\n' "$(dirname "$BASE")"
|
|
285
|
+
for r in "${TMPDIR:-}" /tmp; do
|
|
286
|
+
[ -n "$r" ] || continue
|
|
287
|
+
legacy="$(cd "$r" 2>/dev/null && pwd || printf '%s' "$r")"
|
|
288
|
+
[ "$legacy" = "$BASE" ] && continue
|
|
289
|
+
printf '%s\n' "$legacy"
|
|
290
|
+
done
|
|
291
|
+
} | awk '!seen[$0]++' | awk 'length($0) > 1'
|
|
292
|
+
return 0
|
|
293
|
+
}
|
|
294
|
+
_ho_path_ours() { # <删除目标的路径> → 0 = 路径里**任何一层**是本项目的运行根(根名带本项目 ns)
|
|
295
|
+
local p="$1" root rest comp
|
|
296
|
+
for root in "${_HO_ROOTS[@]}"; do
|
|
297
|
+
[ "${p#"$root"/}" != "$p" ] || continue
|
|
298
|
+
rest="${p#"$root"/}"
|
|
299
|
+
# 逐层看:规范根下第一层就是运行根;而在基(/var/tmp、/tmp)下,运行根可能被套在验证者的副本目录
|
|
300
|
+
# 里(/var/tmp/<副本>/teamsmith-smoke-<ns>.XXXX),所以不能只看第一层。
|
|
301
|
+
while [ -n "$rest" ]; do
|
|
302
|
+
comp="${rest%%/*}"
|
|
303
|
+
case "$comp" in teamsmith-*-"$(_hy_ns)".*) return 0 ;; esac
|
|
304
|
+
[ "$comp" = "$rest" ] && break
|
|
305
|
+
rest="${rest#*/}"
|
|
306
|
+
done
|
|
307
|
+
done
|
|
308
|
+
return 1
|
|
309
|
+
}
|
|
310
|
+
_scan_held_deleted() { # 0 = 扫过(读数可信);1 = 读不到(≠ 没有)
|
|
311
|
+
_HO_OK=0
|
|
312
|
+
_HO_PROCS=0; _HO_FILES=0; _HO_BYTES=0; _HO_OWN_PROCS=0; _HO_OWN_BYTES=0
|
|
313
|
+
_HO_ORPH_PROCS=0; _HO_ORPH_BYTES=0
|
|
314
|
+
_HO_PIDS=(); _HO_PID_BYTES=(); _HO_PID_FILES=(); _HO_PID_OWN=(); _HO_PID_ARGV=(); _HO_PID_ORPH=()
|
|
315
|
+
_HO_PID_PATHS=()
|
|
316
|
+
[ -d /proc/1 ] || return 1
|
|
317
|
+
if [ "$FLIP" = "held" ]; then _HO_OK=1; return 0; fi # 红侧:旧形状 —— 这条读数根本不存在
|
|
318
|
+
local pid id f link di size dilist pbytes pfiles owns argv root hit ppid orphan
|
|
319
|
+
local -A fbytes=() fpath=()
|
|
320
|
+
_HO_ROOTS=()
|
|
321
|
+
while IFS= read -r root; do [ -n "$root" ] && _HO_ROOTS+=("$root"); done < <(_ho_roots)
|
|
322
|
+
[ "${#_HO_ROOTS[@]}" -gt 0 ] || return 1
|
|
323
|
+
for pid in /proc/[0-9]*; do
|
|
324
|
+
id="${pid#/proc/}"
|
|
325
|
+
dilist=""
|
|
326
|
+
for f in "$pid"/fd/*; do
|
|
327
|
+
link="$(readlink "$f" 2>/dev/null || true)"
|
|
328
|
+
case "$link" in *' (deleted)') ;; *) continue ;; esac
|
|
329
|
+
link="${link% (deleted)}"
|
|
330
|
+
hit=0
|
|
331
|
+
for root in "${_HO_ROOTS[@]}"; do
|
|
332
|
+
[ "${link#"$root"/}" != "$link" ] && { hit=1; break; }
|
|
333
|
+
done
|
|
334
|
+
[ "$hit" = "1" ] || continue
|
|
335
|
+
di="$(stat -c '%d:%i' -L "$f" 2>/dev/null || true)"
|
|
336
|
+
[ -n "$di" ] || continue
|
|
337
|
+
if [ -z "${fbytes[$di]+x}" ]; then
|
|
338
|
+
size="$(stat -c %s -L "$f" 2>/dev/null || printf 0)"
|
|
339
|
+
case "$size" in ''|*[!0-9]*) size=0 ;; esac
|
|
340
|
+
fbytes[$di]="$size"; fpath[$di]="$link"
|
|
341
|
+
fi
|
|
342
|
+
case " $dilist " in *" $di "*) ;; *) dilist="$dilist $di" ;; esac
|
|
343
|
+
done
|
|
344
|
+
[ -n "$dilist" ] || continue
|
|
345
|
+
pbytes=0; pfiles=0; owns=0; ppaths=""
|
|
346
|
+
for di in $dilist; do
|
|
347
|
+
pbytes=$((pbytes + ${fbytes[$di]:-0})); pfiles=$((pfiles + 1))
|
|
348
|
+
# 目标路径(扫描时已在 fpath[] 里):计划行要印它,所以按 pid 记下来 —— 不靠 argv 反推。
|
|
349
|
+
if [ -n "$ppaths" ]; then ppaths="$ppaths"$'\n'"${fpath[$di]:-}"; else ppaths="${fpath[$di]:-}"; fi
|
|
350
|
+
if _ho_path_ours "${fpath[$di]:-}"; then owns=1; fi
|
|
351
|
+
done
|
|
352
|
+
_HO_PIDS+=("$id"); _HO_PID_BYTES+=("$pbytes"); _HO_PID_FILES+=("$pfiles")
|
|
353
|
+
_HO_PID_PATHS+=("$ppaths")
|
|
354
|
+
argv="$(tr '\0' ' ' < "/proc/$id/cmdline" 2>/dev/null | cut -c1-140 || true)"
|
|
355
|
+
_HO_PID_ARGV+=("$argv")
|
|
356
|
+
ppid="$(awk '{ sub(/^.*\) /, ""); print $2 }' "/proc/$id/stat" 2>/dev/null || true)"
|
|
357
|
+
orphan=0
|
|
358
|
+
_ho_parent_alive "${ppid:-}" || orphan=1
|
|
359
|
+
[ "${ppid:-}" = "1" ] && orphan=1
|
|
360
|
+
_HO_PID_ORPH+=("$orphan")
|
|
361
|
+
if [ "$owns" = "1" ]; then
|
|
362
|
+
_HO_PID_OWN+=("1")
|
|
363
|
+
_HO_OWN_PROCS=$((_HO_OWN_PROCS + 1)); _HO_OWN_BYTES=$((_HO_OWN_BYTES + pbytes))
|
|
364
|
+
if [ "$orphan" = "1" ]; then
|
|
365
|
+
_HO_ORPH_PROCS=$((_HO_ORPH_PROCS + 1)); _HO_ORPH_BYTES=$((_HO_ORPH_BYTES + pbytes))
|
|
366
|
+
fi
|
|
367
|
+
else
|
|
368
|
+
_HO_PID_OWN+=("0")
|
|
369
|
+
fi
|
|
370
|
+
done
|
|
371
|
+
if [ "${#fbytes[@]}" -gt 0 ]; then
|
|
372
|
+
for di in "${!fbytes[@]}"; do
|
|
373
|
+
_HO_BYTES=$((_HO_BYTES + fbytes[$di])); _HO_FILES=$((_HO_FILES + 1))
|
|
374
|
+
done
|
|
375
|
+
fi
|
|
376
|
+
_HO_PROCS="${#_HO_PIDS[@]}"
|
|
377
|
+
_HO_OK=1
|
|
378
|
+
return 0
|
|
379
|
+
}
|
|
380
|
+
_ho_status_block() { # --status 的一段:只报总量(别家的 pid 不点名)
|
|
381
|
+
printf '\n已删除但仍被持有(P313;df 与 du 的差额就在这里):\n'
|
|
382
|
+
if ! _scan_held_deleted; then
|
|
383
|
+
printf ' 合计:无法判定(/proc 不可用 —— 读不到不等于没有)\n'
|
|
384
|
+
return 0
|
|
385
|
+
fi
|
|
386
|
+
printf ' 合计:%s 个进程 · %s(%s B)· %s 个已删文件\n' \
|
|
387
|
+
"$_HO_PROCS" "$(_ho_human_bytes "$_HO_BYTES")" "$_HO_BYTES" "$_HO_FILES"
|
|
388
|
+
if [ "${#_HO_ROOTS[@]}" -gt 1 ]; then
|
|
389
|
+
printf ' 扫描的根:%s(旧默认根也算 —— P309 之前的运行都写在那里)\n' "$(printf '%s · ' "${_HO_ROOTS[@]}" | sed 's/ · $//')"
|
|
390
|
+
fi
|
|
391
|
+
if [ "$_HO_PROCS" -gt 0 ]; then
|
|
392
|
+
printf ' 可归属本项目(删除目标所在路径里有一层是本项目的运行根 teamsmith-*-%s.*):%s 个进程 · %s(%s B)\n' \
|
|
393
|
+
"$(_hy_ns)" "$_HO_OWN_PROCS" "$(_ho_human_bytes "$_HO_OWN_BYTES")" "$_HO_OWN_BYTES"
|
|
394
|
+
printf ' 其中父进程已不在(被打断的运行留下的孤儿):%s 个进程 · %s(%s B)\n' \
|
|
395
|
+
"$_HO_ORPH_PROCS" "$(_ho_human_bytes "$_HO_ORPH_BYTES")" "$_HO_ORPH_BYTES"
|
|
396
|
+
printf ' 只报总量(别家的 pid 不点名);结束它们要显式 pid —— --sweep 只给计划,绝不动手\n'
|
|
397
|
+
else
|
|
398
|
+
printf ' 没有这种持有(0 就是 0,不是「读不到」)\n'
|
|
399
|
+
fi
|
|
400
|
+
return 0
|
|
401
|
+
}
|
|
402
|
+
_ho_sweep_block() { # --sweep 的一段:可归属者只给「建议结束」的计划,绝不发信号
|
|
403
|
+
printf '\n已删除但仍被持有(P313):'
|
|
404
|
+
if ! _scan_held_deleted; then
|
|
405
|
+
printf '无法判定(/proc 不可用)—— 不碰\n'
|
|
406
|
+
return 0
|
|
407
|
+
fi
|
|
408
|
+
printf '合计 %s 个进程 · %s(%s B)· %s 个已删文件\n' \
|
|
409
|
+
"$_HO_PROCS" "$(_ho_human_bytes "$_HO_BYTES")" "$_HO_BYTES" "$_HO_FILES"
|
|
410
|
+
if [ "$_HO_PROCS" -eq 0 ]; then
|
|
411
|
+
printf ' 没有这种持有。\n'
|
|
412
|
+
return 0
|
|
413
|
+
fi
|
|
414
|
+
if [ "$_HO_OWN_PROCS" -eq 0 ]; then
|
|
415
|
+
printf ' 可归属本项目的持有者:0 个(别家的只进合计、不点名 pid;要它们停下请找各自的运行)\n'
|
|
416
|
+
return 0
|
|
417
|
+
fi
|
|
418
|
+
if [ "$_HO_ORPH_PROCS" -eq 0 ]; then
|
|
419
|
+
printf ' 可归属本项目的 %s 个持有者都还有活着的父进程(= 还在跑的运行在用它们)→ 不建议结束\n' \
|
|
420
|
+
"$_HO_OWN_PROCS"
|
|
421
|
+
return 0
|
|
422
|
+
fi
|
|
423
|
+
printf ' 建议结束(只给计划:本脚本绝不发信号;要动手请显式给 pid):\n'
|
|
424
|
+
local i nl=$'\n' first ln
|
|
425
|
+
for ((i = 0; i < ${#_HO_PIDS[@]}; i++)); do
|
|
426
|
+
[ "${_HO_PID_OWN[$i]}" = "1" ] || continue
|
|
427
|
+
[ "${_HO_PID_ORPH[$i]}" = "1" ] || continue
|
|
428
|
+
if [ "$FLIP" = "heldpath" ]; then # 红侧:旧形状 —— 计划行只有 argv(常没有路径,还会被 140 字节截断)
|
|
429
|
+
printf ' [suggest-kill] pid=%s · %s(%s B)· %s 个已删文件 · 父进程已不在 · %s\n' \
|
|
430
|
+
"${_HO_PIDS[$i]}" "$(_ho_human_bytes "${_HO_PID_BYTES[$i]}")" "${_HO_PID_BYTES[$i]}" \
|
|
431
|
+
"${_HO_PID_FILES[$i]}" "${_HO_PID_ARGV[$i]:-(argv 读不到)}"
|
|
432
|
+
continue
|
|
433
|
+
fi
|
|
434
|
+
# 计划行的第一栏就是**被持有的目标路径**(扫描时读到的 fpath[],按 inode 去重):argv 里没有路径
|
|
435
|
+
# 的持有者(`exec sleep` + fd)与长路径都要点全,不截断。argv 另起一行,只作形状识别用。
|
|
436
|
+
first="${_HO_PID_PATHS[$i]%%"$nl"*}"
|
|
437
|
+
printf ' [suggest-kill] pid=%s · %s(%s B)· %s 个已删文件 · 父进程已不在 · 目标:%s\n' \
|
|
438
|
+
"${_HO_PIDS[$i]}" "$(_ho_human_bytes "${_HO_PID_BYTES[$i]}")" "${_HO_PID_BYTES[$i]}" \
|
|
439
|
+
"${_HO_PID_FILES[$i]}" "${first:-(路径读不到)}"
|
|
440
|
+
while IFS= read -r ln; do
|
|
441
|
+
[ -n "$ln" ] || continue
|
|
442
|
+
[ "$ln" = "$first" ] && continue
|
|
443
|
+
printf ' 目标:%s\n' "$ln"
|
|
444
|
+
done <<<"${_HO_PID_PATHS[$i]}"
|
|
445
|
+
printf ' argv:%s\n' "${_HO_PID_ARGV[$i]:-(argv 读不到)}"
|
|
446
|
+
done
|
|
447
|
+
return 0
|
|
448
|
+
}
|
|
449
|
+
|
|
184
450
|
# ---------------------------------------------------------------- 仓库 / worktree / 记录
|
|
185
451
|
_repo_root() { printf '%s\n' "${TEAM_TMP_HYGIENE_REPO:-$(git -C "$SELF_DIR" rev-parse --show-toplevel 2>/dev/null || true)}"; }
|
|
186
452
|
_main_root() { # 主工作树(记录与 worktree 注册的基准)
|
|
@@ -306,6 +572,7 @@ _root_ledger_repo_verdict() {
|
|
|
306
572
|
line="$(head -1 "$ledger" 2>/dev/null || true)"
|
|
307
573
|
[ "${line##* repo=}" != "$line" ] || { printf 'unknown\t旧台账没有 repo= 字段\n'; return 0; }
|
|
308
574
|
repo="${line##* repo=}"
|
|
575
|
+
repo="${repo%% *}" # 表头行 `repo=` 之后还可能有别的键(P287 的 ns=)
|
|
309
576
|
{ [ -n "$repo" ] && [ "$repo" != "unknown" ]; } || { printf 'unknown\t台账没有可比的仓库身份\n'; return 0; }
|
|
310
577
|
mine="$(_main_root 2>/dev/null || true)"
|
|
311
578
|
if [ -n "$mine" ] && [ "$repo" = "$mine" ]; then
|
|
@@ -462,7 +729,11 @@ _candidate_dirs() {
|
|
|
462
729
|
|
|
463
730
|
# ---------------------------------------------------------------- --status
|
|
464
731
|
cmd_status() {
|
|
465
|
-
|
|
732
|
+
local base_note missing_note
|
|
733
|
+
base_note="$(_hy_base_note)"
|
|
734
|
+
missing_note="$(team_tmp_root_ancestor_note "$BASE" 2>/dev/null || true)"
|
|
735
|
+
printf '\033[1m临时根:%s(%s)\033[0m\n' "$BASE" "$base_note"
|
|
736
|
+
[ -n "$missing_note" ] && printf ' %s\n' "$missing_note"; :
|
|
466
737
|
printf ' 工具:%s\n' "$(_tool_identity)"
|
|
467
738
|
printf ' 归属基准仓库:%s\n' "$(_main_root 2>/dev/null || printf '?')"
|
|
468
739
|
if [ -d "$BASE" ]; then
|
|
@@ -478,6 +749,26 @@ cmd_status() {
|
|
|
478
749
|
else
|
|
479
750
|
printf ' \033[33m!\033[0m %s 不存在\n' "$BASE"
|
|
480
751
|
fi
|
|
752
|
+
_ho_status_block
|
|
753
|
+
|
|
754
|
+
# P309:默认值搬到磁盘后,旧默认根里的残留不会自己搬家。这里只**数**一下并给**一条**命令:
|
|
755
|
+
# 一次 `--sweep` 只作用于一个基准,所以回收旧根 = 同一条命令把旧基准显式选出来(候选谓词、
|
|
756
|
+
# 归属证明、占用证明、年龄判据一个都不变)。只读:不删、不改任何东西。
|
|
757
|
+
local legacy_base legacy_n=0 le lb
|
|
758
|
+
legacy_base="${TMPDIR:-/tmp}"
|
|
759
|
+
legacy_base="$(cd "$legacy_base" 2>/dev/null && pwd || printf '%s' "$legacy_base")"
|
|
760
|
+
if [ "$BASE" != "$legacy_base" ] && [ -d "$legacy_base" ]; then
|
|
761
|
+
for le in "$legacy_base"/*/; do
|
|
762
|
+
[ -d "$le" ] || continue
|
|
763
|
+
lb="${le%/}"; lb="${lb##*/}"
|
|
764
|
+
_family_name "$lb" || continue
|
|
765
|
+
legacy_n=$((legacy_n + 1))
|
|
766
|
+
done
|
|
767
|
+
printf '\n旧默认根(%s)下的家族目录:%s 个(只读计数,不是本次的基准)\n' "$legacy_base" "$legacy_n"
|
|
768
|
+
printf ' 回收它们(同一条命令,把旧基准显式选出来):\n'
|
|
769
|
+
printf ' TMPDIR=%s bash %s --sweep --dry-run # 先看清单\n' "$legacy_base" "$SELF_DIR/$(basename "${BASH_SOURCE[0]}")"
|
|
770
|
+
printf ' TMPDIR=%s bash %s --sweep\n' "$legacy_base" "$SELF_DIR/$(basename "${BASH_SOURCE[0]}")"
|
|
771
|
+
fi
|
|
481
772
|
|
|
482
773
|
local mech="none"
|
|
483
774
|
if [ "$FLIP" != "noproc" ]; then
|
|
@@ -487,6 +778,9 @@ cmd_status() {
|
|
|
487
778
|
|
|
488
779
|
local roots=0 reclaimable=0 occupied=0 unowned=0
|
|
489
780
|
local e b kb files age occ
|
|
781
|
+
# P287:历史平铺根(名字里没有本项目的 ns)—— P287 之前创建的根一律长这样;只标出来(一行细节),
|
|
782
|
+
# 不改候选判据,也不新增清扫规则。ns 只问一次(它要问 git),不是每个根一次。
|
|
783
|
+
local hy_self_ns; hy_self_ns="$(_hy_ns)"
|
|
490
784
|
printf '\n根(%s 下的 owned 家族;只有这些是 --sweep 候选):\n' "$BASE"
|
|
491
785
|
for e in "$BASE"/*/; do
|
|
492
786
|
[ -d "$e" ] || continue
|
|
@@ -500,16 +794,21 @@ cmd_status() {
|
|
|
500
794
|
esac
|
|
501
795
|
roots=$((roots + 1))
|
|
502
796
|
kb="$(_dir_kb "$e")"; files="$(_dir_files "$e")"; age="$(_age_minutes "$e" 2>/dev/null || printf '?')"
|
|
503
|
-
local pid start kind run owner occ_state
|
|
797
|
+
local pid start kind run owner occ_state ns legacy=""
|
|
504
798
|
pid="$(_marker_get "$e" pid || true)"; start="$(_marker_get "$e" start || true)"
|
|
505
799
|
kind="$(_marker_get "$e" kind || true)"; run="$(_marker_get "$e" run || true)"
|
|
800
|
+
ns="$(_marker_get "$e" ns || true)"
|
|
801
|
+
case "$b" in
|
|
802
|
+
teamsmith-*) case "$b" in *"-$hy_self_ns."*) ;; *) legacy=" legacy(P287 之前的平铺名,没有 ns)" ;; esac ;;
|
|
803
|
+
esac
|
|
506
804
|
if [ -n "$pid" ]; then
|
|
507
805
|
if _proc_alive "$pid" "$start"; then owner="pid=$pid 存活"; else owner="pid=$pid 已退出"; fi
|
|
508
|
-
owner="$owner${kind:+ kind=$kind}"
|
|
806
|
+
owner="$owner${kind:+ kind=$kind}${ns:+ ns=$ns}"
|
|
509
807
|
grep -q '^detached=1$' "$e/.teamsmith-tmp" 2>/dev/null && owner="$owner · detached(生命周期归调用方)"
|
|
510
808
|
else
|
|
511
809
|
owner="无 owner 标记(不是经助手创建的根)"
|
|
512
810
|
fi
|
|
811
|
+
owner="$owner$legacy"
|
|
513
812
|
if [ "$mech" = "none" ]; then occ="无法证明(既无 /proc 也无 lsof)"
|
|
514
813
|
else
|
|
515
814
|
occ="$(_occupants_of "$e")"
|
|
@@ -551,6 +850,19 @@ cmd_status() {
|
|
|
551
850
|
done
|
|
552
851
|
[ "$listed" -eq 0 ] && printf ' (没有这类条目)\n'
|
|
553
852
|
|
|
853
|
+
# P287:锁对 —— 文件永远不是 --sweep 的候选(候选只认家族**目录**),但运维在共享临时根里读到这
|
|
854
|
+
# 三样时要能一眼分清「运行互斥」「兼容声明」「运行注册」。
|
|
855
|
+
local hy_pub hy_comp hy_runs_n
|
|
856
|
+
hy_pub="${TEAM_SMOKE_LOCK:-$BASE/teamsmith-smoke.lock}"
|
|
857
|
+
hy_comp="$(_hy_companion "$hy_pub")"
|
|
858
|
+
hy_runs_n="$([ -d "$hy_comp.runs" ] && find "$hy_comp.runs" -maxdepth 1 -name '*.run' 2>/dev/null | wc -l | tr -d ' ' || printf 0)"
|
|
859
|
+
printf '\n不是根 · 锁对(P287:互斥在伴生锁上;发布路径只是兼容声明;三样都不是 --sweep 的候选):\n'
|
|
860
|
+
printf ' [machine 运行互斥] %s(%s)\n' "$hy_comp" \
|
|
861
|
+
"$([ -e "$hy_comp" ] && printf '存在' || printf '不存在(下一次取锁按需创建)')"
|
|
862
|
+
printf ' [published 兼容声明] %s(%s)\n' "$hy_pub" \
|
|
863
|
+
"$([ -e "$hy_pub" ] && printf '存在;P287 之前的写入者只取这一把' || printf '不存在(下一次取锁按需创建)')"
|
|
864
|
+
printf ' [records 运行注册] %s(%s 条)\n' "$hy_comp.runs" "${hy_runs_n:-0}"
|
|
865
|
+
|
|
554
866
|
local stale
|
|
555
867
|
stale="$(_stale_worktree_registrations | wc -l | tr -d ' ')"
|
|
556
868
|
if [ "${stale:-0}" -gt 0 ] 2>/dev/null; then
|
|
@@ -574,9 +886,20 @@ cmd_status() {
|
|
|
574
886
|
}
|
|
575
887
|
|
|
576
888
|
# ---------------------------------------------------------------- --lint
|
|
577
|
-
|
|
578
|
-
|
|
889
|
+
# P287 的规则(作用域 = 整棵 skill 树:夹具 **与** 产品自己的临时模板;跳过扫描器自己的源码与嵌套根
|
|
890
|
+
# `$TMP/…`):
|
|
891
|
+
# * 直接落在解析后临时根(`${TMPDIR:-/tmp}`)上的每一个 `mktemp` 模板:要么点开头(台账 / pid /
|
|
892
|
+
# 诊断),要么带本项目的 ns(`<slug>-<hash8>`);
|
|
893
|
+
# * `mktemp -d` 的**根**还必须在 owned 家族里(`teamsmith-<kind>-<ns>.XXXXXX`);
|
|
894
|
+
# * 写死一个别的绝对目录(不是解析后的临时根)或相对模板 → finding。
|
|
895
|
+
# finding 仍是 `<file>:<line>`、退出码 1。裸 `mktemp`(没有模板参数)没有模板可判 —— 静态闸不猜。
|
|
896
|
+
_lint_first_arg() { # <“mktemp” 之后的文本> → 第一个参数(保留引号);空 = 没有模板
|
|
897
|
+
local s="$1"
|
|
579
898
|
s="${s#"${s%%[![:space:]]*}"}" # 去前导空白
|
|
899
|
+
# 选项:本树的 mktemp 只用 -d(其余形状按「没有模板」处理,不猜)
|
|
900
|
+
case "$s" in -d) printf '\n'; return 0 ;; esac
|
|
901
|
+
case "$s" in -d*) s="${s#-d}"; s="${s#"${s%%[![:space:]]*}"}" ;; esac
|
|
902
|
+
s="${s#"${s%%[![:space:]]*}"}"
|
|
580
903
|
[ -n "$s" ] || { printf '\n'; return 0; }
|
|
581
904
|
local q="${s:0:1}" out="" i ch
|
|
582
905
|
if [ "$q" = '"' ] || [ "$q" = "'" ]; then
|
|
@@ -600,36 +923,164 @@ _lint_template() { # <“mktemp -d” 之后的文本> → 第一个参数(保
|
|
|
600
923
|
done
|
|
601
924
|
printf '%s\n' "$out"
|
|
602
925
|
}
|
|
603
|
-
|
|
604
|
-
|
|
926
|
+
# P309 · 第二条规则:**任何自己算临时根的地方都是 finding**(`${TMPDIR:-/tmp}`、`${TMPDIR}`、JS 侧的同类
|
|
927
|
+
# 回落、写死 `/tmp/…` 的 mktemp 模板)。理由:两处各算 = 两个答案 = 清扫器与门禁对不上基准。
|
|
928
|
+
# 豁免只有两处:
|
|
929
|
+
# ① 解析器自己的定义(`scripts/lib/common.sh` 里 team_tmp_root* —— 它的行里没有这两个 token);
|
|
930
|
+
# ② 冻结的门禁锁发布路径(P287:`${TMPDIR:-/tmp}/teamsmith-smoke.lock` **不许搬**,搬走会在升级窗口里
|
|
931
|
+
# 新开一条静默并发窗口;它留在 gate-lock.sh 的那一行,别处只调 gate_lock_default_path)。
|
|
932
|
+
_lint_self_computed() { # <file> <line> <logical line> → 0 = finding(已打印)/ 1 = ok
|
|
933
|
+
local f="$1" ln="$2" line="$3" tok=""
|
|
934
|
+
# 冻结的锁发布路径:门禁锁(P287)与项目锁(P309 同一条纪律,跨版本串行)。
|
|
935
|
+
case "$line" in
|
|
936
|
+
*teamsmith-lock-*|*teamsmith-smoke.lock*|*teamsmith-perf.lock*) return 1 ;;
|
|
937
|
+
esac
|
|
938
|
+
# 解析器自己(含 tmp_root_socket_dir 的「放不下就退平台默认」那一步):唯一实现,别处只能调它。
|
|
939
|
+
case "$f" in
|
|
940
|
+
*/scripts/lib/common.sh|*/tests/lib/tmp-root.sh) return 1 ;;
|
|
941
|
+
esac
|
|
942
|
+
case "$line" in
|
|
943
|
+
*'${TMPDIR:-/tmp}'*) tok='${TMPDIR:-/tmp}' ;;
|
|
944
|
+
*'${TMPDIR}'*) tok='${TMPDIR}' ;;
|
|
945
|
+
*'process.env.TMPDIR'*) tok='process.env.TMPDIR' ;;
|
|
946
|
+
*'tmpdir()'*) tok='tmpdir()' ;;
|
|
947
|
+
*) return 1 ;;
|
|
948
|
+
esac
|
|
949
|
+
printf '%s:%s: 自己算临时根(%s)—— 必须调唯一的解析器 team_tmp_root(TMPDIR → TEAM_TMP_ROOT → 平台默认)\n' \
|
|
950
|
+
"$f" "$ln" "$tok"
|
|
951
|
+
return 0
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
# P309 · 第三条规则:夹具里的 tmux socket 目录要留得下 AF_UNIX 的 107 字节。临时根搬到
|
|
955
|
+
# `/var/tmp/teamsmith-<ns>` 之后,凡是把 `TMUX_TMPDIR` 挂在 `$TMP` 下的夹具都长了 ~20 字节:
|
|
956
|
+
# `$TMP/m36/priv-tmpdir/tmux-<uid>/default` 从 83 涨到 113,私有 server 直接绑不上(31c 就是这样红的)。
|
|
957
|
+
# 这里按最长 uid(10 位)与本次的实际运行根名算,超了就点名(真跑起来才发现的坑,静态就拦掉)。
|
|
958
|
+
_lint_socket_room() { # <file> <line> <logical> → 0 = finding(已打印)/ 1 = ok
|
|
959
|
+
local f="$1" ln="$2" line="$3" frag base worst
|
|
960
|
+
case "$line" in
|
|
961
|
+
*'TMUX_TMPDIR="$TMP'*) frag="${line#*TMUX_TMPDIR=\"\$TMP}"; frag="${frag%%\"*}" ;;
|
|
962
|
+
*) return 1 ;;
|
|
963
|
+
esac
|
|
964
|
+
base="$(team_tmp_root)/teamsmith-smoke-$(_hy_ns).XXXXXX"
|
|
965
|
+
worst=$(( ${#base} + ${#frag} + 18 ))
|
|
966
|
+
[ "$worst" -le 107 ] && return 1
|
|
967
|
+
printf '%s:%s: tmux socket 目录太长(最长 %s 字节 > AF_UNIX 上限 107)—— 夹具的 socket 端点别挂在临时根下,放到本轮私有 tmux 基址(SMOKE_TMUX_TMPDIR)下\n' \
|
|
968
|
+
"$f" "$ln" "$worst"
|
|
969
|
+
return 0
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
_lint_judge() { # <file> <line> <template> <is_root> → 0 合规 / 1 finding(打印到 stdout)
|
|
973
|
+
local f="$1" ln="$2" t="$3" is_root="$4" u first ns
|
|
605
974
|
if [ -z "$t" ]; then
|
|
606
|
-
|
|
975
|
+
if [ "$is_root" = "1" ]; then
|
|
976
|
+
printf '%s:%s: 无模板(裸 mktemp -d 的名字不在 owned 家族、也没有本项目的 ns)\n' "$f" "$ln"; return 1
|
|
977
|
+
fi
|
|
978
|
+
return 0 # 裸 mktemp(文件):没有模板,静态闸不猜
|
|
607
979
|
fi
|
|
608
980
|
u="$t"
|
|
609
981
|
case "$u" in \"*\") u="${u#\"}"; u="${u%\"}" ;; \'*\') u="${u#\'}"; u="${u%\'}" ;; esac
|
|
610
|
-
if [ "$FLIP" = "lint" ]; then
|
|
611
|
-
# 红侧:把「不许写死绝对路径」这条规则关掉
|
|
612
|
-
return 0
|
|
613
|
-
fi
|
|
614
982
|
case "$u" in
|
|
615
983
|
/*)
|
|
984
|
+
# 红侧:只把「不许写死绝对路径」这一条规则关掉(ns / 家族两条照判)
|
|
985
|
+
[ "$FLIP" = "lint" ] && return 0
|
|
616
986
|
printf '%s:%s: 写死绝对路径(%s)—— 必须建在 ${TMPDIR:-/tmp} 下\n' "$f" "$ln" "$u"; return 1 ;;
|
|
617
|
-
'$
|
|
618
|
-
|
|
619
|
-
'$TMPDIR/'*)
|
|
620
|
-
|
|
987
|
+
'$(team_tmp_root)/'*) first="${u#'$(team_tmp_root)/'}"; first="${first%%/*}" ;;
|
|
988
|
+
'$(tmp_root_base)/'*) first="${u#'$(tmp_root_base)/'}"; first="${first%%/*}" ;;
|
|
989
|
+
'${TMPDIR:-/tmp}/'*) first="${u#'${TMPDIR:-/tmp}/'}"; first="${first%%/*}" ;;
|
|
990
|
+
'$TMPDIR/'*) first="${u#'$TMPDIR/'}"; first="${first%%/*}" ;;
|
|
621
991
|
''|'$'*|'${'*)
|
|
622
|
-
return 0 ;;
|
|
992
|
+
return 0 ;; # 变量模板(嵌套根 / 变量前缀):静态判不了,豁免
|
|
623
993
|
*)
|
|
624
994
|
printf '%s:%s: 相对/非临时根的模板(%s)\n' "$f" "$ln" "$u"; return 1 ;;
|
|
625
995
|
esac
|
|
996
|
+
# first = 直接落在解析后临时根上的那一段名字模板
|
|
626
997
|
case "$first" in
|
|
627
|
-
|
|
628
|
-
*) printf '%s:%s: 名字不在 owned 家族(%s)—— 必须是 teamsmith-<kind>.XXXXXX\n' "$f" "$ln" "$u"; return 1 ;;
|
|
998
|
+
.*) return 0 ;; # 点开头:天然不是别人的前缀 glob 的猎物
|
|
629
999
|
esac
|
|
1000
|
+
case "$first" in
|
|
1001
|
+
teamsmith-*|review-*) ;;
|
|
1002
|
+
*) printf '%s:%s: 名字不在 owned 家族(%s)—— 必须是 teamsmith-<kind>-<ns>.XXXXXX,或点开头\n' \
|
|
1003
|
+
"$f" "$ln" "$u"; return 1 ;;
|
|
1004
|
+
esac
|
|
1005
|
+
case "$first" in
|
|
1006
|
+
*'$'*) return 0 ;; # 参数化名字:ns 由运行时的展开给(模板里写着变量)
|
|
1007
|
+
esac
|
|
1008
|
+
ns="$(_hy_ns)"
|
|
1009
|
+
case "$first" in *"$ns"*) return 0 ;; esac
|
|
1010
|
+
if [ "$is_root" = "1" ]; then
|
|
1011
|
+
printf '%s:%s: 根模板没有带本项目的命名空间(%s)—— 必须是 teamsmith-<kind>-<ns>.XXXXXX\n' \
|
|
1012
|
+
"$f" "$ln" "$u"
|
|
1013
|
+
else
|
|
1014
|
+
printf '%s:%s: 每运行模板既不是点开头、也没有本项目的命名空间(%s)\n' "$f" "$ln" "$u"
|
|
1015
|
+
fi
|
|
1016
|
+
return 1
|
|
630
1017
|
}
|
|
1018
|
+
# ---------------------------------------------------------------- --docs-check
|
|
1019
|
+
# P314 · 文档面一致性(change: tmp-root-on-disk)。默认根从 `${TMPDIR:-/tmp}` 搬到**磁盘**上的
|
|
1020
|
+
# `/var/tmp/teamsmith-<ns>`,解析来源(`source=`)成了必须写出来的事实。文档里若还在教老行为,
|
|
1021
|
+
# 下一个照着文档操作的人就会被引回内存盘。这里只用**稳定 token**(路径形状 / 键名 / 单位),不锚整句:
|
|
1022
|
+
# 措辞可以改,事实不能退回去。
|
|
1023
|
+
# must : 这棵树里必须出现的 token(漏了 = 文档没跟着改)
|
|
1024
|
+
# mustnot : 老行为的指纹(出现 = 残留)
|
|
1025
|
+
# `${TMPDIR:-/tmp}` 在 protocol.md 里**有**合法位置(冻结的门禁锁发布路径与危险 glob 的反例),
|
|
1026
|
+
# 所以只在 troubleshooting.md / config.md 里当作残留;protocol.md 只按旧根名判。
|
|
1027
|
+
_DOC_RULES=(
|
|
1028
|
+
'references/troubleshooting.md|must|var/tmp/teamsmith-|默认根在磁盘上(不再是内存盘)'
|
|
1029
|
+
'references/troubleshooting.md|must|source=|解析来源必须写出来(谁怎么解析的)'
|
|
1030
|
+
'references/troubleshooting.md|must|TEAM_TMP_ROOT=/tmp|回滚口令要写清'
|
|
1031
|
+
'references/troubleshooting.md|mustnot|15 GB tmpfs|旧读数(tmpfs 大小)已不是现在的默认根'
|
|
1032
|
+
'references/troubleshooting.md|mustnot|${TMPDIR:-/tmp}|旧基准写法(默认根不再是它)'
|
|
1033
|
+
'references/troubleshooting.md|mustnot|teamsmith-<kind>.XXXXXX|旧根名(P287 之后带 -<ns>)'
|
|
1034
|
+
'references/migration.md|must|TEAM_TMP_ROOT|升级与回滚的入口要写清'
|
|
1035
|
+
'references/migration.md|must|var/tmp/teamsmith-|新默认根'
|
|
1036
|
+
'references/config.md|must|var/tmp/teamsmith-|新默认根'
|
|
1037
|
+
'references/config.md|mustnot|${TMPDIR:-/tmp}|旧基准写法(默认根不再是它)'
|
|
1038
|
+
'references/protocol.md|must|var/tmp/teamsmith-|新默认根'
|
|
1039
|
+
'references/protocol.md|mustnot|teamsmith-<kind>.XXXXXX|旧根名(P287 之后带 -<ns>)'
|
|
1040
|
+
)
|
|
1041
|
+
cmd_docs_check() {
|
|
1042
|
+
local root="$SKILL_DIR" rule f mode tok why path line n=0 rc=0 seen=" "
|
|
1043
|
+
while [ $# -gt 0 ]; do
|
|
1044
|
+
case "$1" in
|
|
1045
|
+
--dir) root="${2:?--dir 需要目录}"; shift 2 ;;
|
|
1046
|
+
--dir=*) root="${1#*=}"; shift ;;
|
|
1047
|
+
*) printf 'tmp-hygiene: --docs-check 不认识的参数 [%s]\n' "$1" >&2; return 2 ;;
|
|
1048
|
+
esac
|
|
1049
|
+
done
|
|
1050
|
+
for rule in "${_DOC_RULES[@]}"; do
|
|
1051
|
+
IFS='|' read -r f mode tok why <<<"$rule"
|
|
1052
|
+
path="$root/$f"
|
|
1053
|
+
n=$((n + 1))
|
|
1054
|
+
case "$seen" in *" $f "*) ;; *) seen="$seen$f " ;; esac
|
|
1055
|
+
if [ ! -f "$path" ]; then
|
|
1056
|
+
printf '\033[31m✗\033[0m %s:文件不在(%s 缺它)\n' "$f" "$root"
|
|
1057
|
+
rc=1; continue
|
|
1058
|
+
fi
|
|
1059
|
+
case "$mode" in
|
|
1060
|
+
must)
|
|
1061
|
+
LC_ALL=C grep -qF -- "$tok" "$path" 2>/dev/null && continue
|
|
1062
|
+
printf '\033[31m✗\033[0m %s:缺 token [%s] —— %s\n' "$f" "$tok" "$why"
|
|
1063
|
+
rc=1 ;;
|
|
1064
|
+
mustnot)
|
|
1065
|
+
while IFS=: read -r line _; do
|
|
1066
|
+
[ -n "$line" ] || continue
|
|
1067
|
+
printf '\033[31m✗\033[0m %s:%s:残留 token [%s] —— %s\n' "$f" "$line" "$tok" "$why"
|
|
1068
|
+
rc=1
|
|
1069
|
+
done < <(LC_ALL=C grep -nF -- "$tok" "$path" 2>/dev/null)
|
|
1070
|
+
;;
|
|
1071
|
+
esac
|
|
1072
|
+
done
|
|
1073
|
+
local nf; nf="$(printf '%s' "$seen" | wc -w | tr -d ' ')"
|
|
1074
|
+
if [ "$rc" -eq 0 ]; then
|
|
1075
|
+
printf ' \033[32m✓\033[0m 文档面与解析后的临时根一致(%s 个文件 · %s 条规则)\n' "$nf" "$n"
|
|
1076
|
+
else
|
|
1077
|
+
printf '\033[31m文档面不一致(%s 个文件 · %s 条规则)\033[0m\n' "$nf" "$n"
|
|
1078
|
+
fi
|
|
1079
|
+
return "$rc"
|
|
1080
|
+
}
|
|
1081
|
+
|
|
631
1082
|
cmd_lint() {
|
|
632
|
-
local dir="$
|
|
1083
|
+
local dir="$SKILL_DIR" # P287:默认扫整棵 skill 树(夹具 + 产品自己的临时模板)
|
|
633
1084
|
while [ $# -gt 0 ]; do
|
|
634
1085
|
case "$1" in
|
|
635
1086
|
--dir) dir="${2:-$dir}"; shift ;;
|
|
@@ -640,17 +1091,27 @@ cmd_lint() {
|
|
|
640
1091
|
local checked=0 findings=0 f ln logical
|
|
641
1092
|
local self_file="$SELF_DIR/$(basename "${BASH_SOURCE[0]}")"
|
|
642
1093
|
while IFS= read -r f; do
|
|
643
|
-
# 扫描器自己的源码里有 `mktemp
|
|
644
|
-
|
|
1094
|
+
# 扫描器自己的源码里有 `mktemp` 的**文字**(注释/格式串/自检夹具)—— 它不是产品面,跳过
|
|
1095
|
+
# (`-ef`:同一个文件的相对/绝对路径都要认出来)
|
|
1096
|
+
[ "$f" -ef "$self_file" ] && continue
|
|
645
1097
|
while IFS=$'\t' read -r ln logical; do
|
|
646
|
-
|
|
647
|
-
|
|
1098
|
+
# 整行注释里的 `mktemp` 是文字,不是模板
|
|
1099
|
+
case "${logical#"${logical%%[![:space:]]*}"}" in '#'*) continue ;; esac
|
|
1100
|
+
# P309 · 第二、三条规则先判(自己算根 / socket 目录太长);命中了就不再按模板判同一条行
|
|
1101
|
+
if _lint_self_computed "$f" "$ln" "$logical"; then findings=$((findings + 1)); continue; fi
|
|
1102
|
+
if _lint_socket_room "$f" "$ln" "$logical"; then findings=$((findings + 1)); continue; fi
|
|
1103
|
+
case "$logical" in *mktemp*) ;; *) continue ;; esac
|
|
1104
|
+
local rest="$logical" occ_word tmpl is_root
|
|
648
1105
|
while :; do
|
|
649
|
-
case "$rest" in *
|
|
650
|
-
rest="${rest#*mktemp
|
|
651
|
-
|
|
1106
|
+
case "$rest" in *mktemp*) ;; *) break ;; esac
|
|
1107
|
+
rest="${rest#*mktemp}"
|
|
1108
|
+
# 函数定义 `mktemp() { … }`(task-model-routing.sh 的桩)不是一次调用
|
|
1109
|
+
case "${rest#"${rest%%[![:space:]]*}"}" in '('*) continue ;; esac
|
|
1110
|
+
is_root=0
|
|
1111
|
+
case "${rest#"${rest%%[![:space:]]*}"}" in -d*) is_root=1 ;; esac
|
|
1112
|
+
tmpl="$(_lint_first_arg "$rest")"
|
|
652
1113
|
checked=$((checked + 1))
|
|
653
|
-
_lint_judge "$f" "$ln" "$tmpl" || findings=$((findings + 1))
|
|
1114
|
+
_lint_judge "$f" "$ln" "$tmpl" "$is_root" || findings=$((findings + 1))
|
|
654
1115
|
done
|
|
655
1116
|
done < <(awk '{
|
|
656
1117
|
buf = buf $0; if (start==0) start=NR
|
|
@@ -659,12 +1120,27 @@ cmd_lint() {
|
|
|
659
1120
|
buf=""; start=0
|
|
660
1121
|
} END { if (buf != "") print start "\t" buf }' "$f")
|
|
661
1122
|
done < <(find "$dir" -type f -name '*.sh' 2>/dev/null | sort)
|
|
662
|
-
|
|
1123
|
+
# P309 · JS 侧:产品面板源码里“自己算临时根”的同类回落(`process.env.TMPDIR` / 无参 `tmpdir()`)。
|
|
1124
|
+
# 范围 = `scripts/panel/src/**`(产品面;测试夹具的 JS 临时目录从 shell 继承 TMPDIR,不是第二份解析)。
|
|
1125
|
+
local jsf jsln jstext
|
|
1126
|
+
while IFS= read -r jsf; do
|
|
1127
|
+
[ -n "$jsf" ] || continue
|
|
1128
|
+
jsln=0
|
|
1129
|
+
while IFS= read -r jstext; do
|
|
1130
|
+
jsln=$((jsln + 1))
|
|
1131
|
+
case "${jstext#"${jstext%%[![:space:]]*}"}" in '#'*|'//'*|'*'*) continue ;; esac
|
|
1132
|
+
checked=$((checked + 1))
|
|
1133
|
+
if _lint_self_computed "$jsf" "$jsln" "$jstext"; then findings=$((findings + 1)); fi
|
|
1134
|
+
done < "$jsf"
|
|
1135
|
+
done < <(find "$dir/scripts/panel/src" -type f -name '*.ts' 2>/dev/null | sort)
|
|
1136
|
+
printf '\n检查了 %s 个 mktemp 模板与面板源码行(%s;规则:点开头或带 ns「%s」,根还要在 owned 家族里;' \
|
|
1137
|
+
"$checked" "$dir" "$(_hy_ns)"
|
|
1138
|
+
printf '自己算临时根 = finding,豁免只有解析器定义与冻结的锁发布路径)\n'
|
|
663
1139
|
if [ "$findings" -gt 0 ]; then
|
|
664
1140
|
printf '\033[31m%s 个 finding\033[0m\n' "$findings"
|
|
665
1141
|
return 1
|
|
666
1142
|
fi
|
|
667
|
-
printf '\033[32mowned 家族 / TMPDIR
|
|
1143
|
+
printf '\033[32mowned 家族 / TMPDIR / 命名空间口径全部合规\033[0m\n'
|
|
668
1144
|
return 0
|
|
669
1145
|
}
|
|
670
1146
|
|
|
@@ -794,6 +1270,8 @@ cmd_sweep() {
|
|
|
794
1270
|
"$reclaim_n" "$(human_kb "$reclaim_kb")" "$reclaim_files"
|
|
795
1271
|
fi
|
|
796
1272
|
|
|
1273
|
+
_ho_sweep_block
|
|
1274
|
+
|
|
797
1275
|
local stale
|
|
798
1276
|
stale="$(_stale_worktree_registrations | wc -l | tr -d ' ')"
|
|
799
1277
|
[ "${stale:-0}" -gt 0 ] 2>/dev/null && printf '另有 %s 个注册目录已不在的 worktree —— 修法:git -C %s worktree prune\n' "$stale" "$(_main_root 2>/dev/null || printf '?')"
|
|
@@ -900,7 +1378,8 @@ _hy_selftest() {
|
|
|
900
1378
|
esac
|
|
901
1379
|
done
|
|
902
1380
|
case "$break_stage" in
|
|
903
|
-
''|occupied|orphan|unknown|foreign|lock|worktree|record|lint|reviewproof|ledgerrepo|blockskip|tmuxtouch
|
|
1381
|
+
''|occupied|orphan|unknown|foreign|lock|worktree|record|lint|reviewproof|ledgerrepo|blockskip|tmuxtouch|\
|
|
1382
|
+
held|heldpath|inline-root|fixed-floor|missing-root) ;;
|
|
904
1383
|
*) printf 'tmp-hygiene: 不认识的 break 阶段 [%s]\n' "$break_stage" >&2; return 2 ;;
|
|
905
1384
|
esac
|
|
906
1385
|
# 每个子进程带**自己**的 flip:一个 break 只该让它的那一段变红(不污染别的段)
|
|
@@ -916,6 +1395,7 @@ _hy_selftest() {
|
|
|
916
1395
|
}
|
|
917
1396
|
|
|
918
1397
|
local D
|
|
1398
|
+
team_tmp_root_prepare >/dev/null 2>&1 || true # 默认腿按需建(0700);显式根只校验
|
|
919
1399
|
D="$(mktemp -d "$BASE/teamsmith-hygiene-selftest.XXXXXX")" || { printf '自检建不出临时目录\n' >&2; return 3; }
|
|
920
1400
|
trap "rm -rf -- '$D'" EXIT
|
|
921
1401
|
# 自检永远不会扫真实的 tmux socket 目录(否则断言会依赖机器状态、清扫也碰不到真东西)
|
|
@@ -943,6 +1423,32 @@ _hy_selftest() {
|
|
|
943
1423
|
assert_out "$o_status" "$live" "status 列出存活的根"
|
|
944
1424
|
assert_out "$o_status" "$live_holder(cwd)" "status 把存活根的占用者(pid)报出来"
|
|
945
1425
|
assert_out "$o_status" "$D/teamsmith-inotify-probe.A1.js" "status 列出家族里的文件(不是根)"
|
|
1426
|
+
# P287:根的 ns 列 + 锁对按角色报出
|
|
1427
|
+
printf 'kind=selftest\npid=999999\nstart=0\nrun=t\nns=%s\n' "$(_hy_ns)" >"$stale/.teamsmith-tmp"
|
|
1428
|
+
out="$(TMPDIR="$D" _hy "" --status 2>&1)"
|
|
1429
|
+
assert_out "$out" "ns=$(_hy_ns)" "status 在根的归属行里报出 ns"
|
|
1430
|
+
printf 'kind=selftest\npid=999999\nstart=0\nrun=t\n' >"$stale/.teamsmith-tmp"
|
|
1431
|
+
assert_out "$out" "[machine 运行互斥]" "status 按角色报出伴生锁(运行互斥)"
|
|
1432
|
+
assert_out "$out" "[published 兼容声明]" "status 按角色报出发布路径(兼容声明)"
|
|
1433
|
+
assert_out "$out" "[records 运行注册]" "status 按角色报出运行注册目录"
|
|
1434
|
+
# P287:历史平铺根(名字里没有 ns)标 legacy;带 ns 的根不标(一行细节,不改候选判据)
|
|
1435
|
+
# 注意(第一版没牙,被变异测试抓到):fixture 目录名里**不能**出现 legacy 这个词,断言也不能只 grep
|
|
1436
|
+
# `legacy` —— 否则命中的是**路径本身**,把标注去掉后断言仍然绿。所以目录叫 oldflat,断言只认标注文本。
|
|
1437
|
+
local lgc="$D/teamsmith-oldflat.L1" curn="$D/teamsmith-selftest-$(_hy_ns).L2"
|
|
1438
|
+
local lg_label='legacy(P287 之前的平铺名,没有 ns)'
|
|
1439
|
+
mkdir -p "$lgc" "$curn"; touch -d '2 hours ago' "$lgc" "$curn"
|
|
1440
|
+
local o_legacy; o_legacy="$(TMPDIR="$D" _hy "" --status 2>&1)"
|
|
1441
|
+
if printf '%s\n' "$o_legacy" | grep -A1 -F " $lgc" | grep -qF "$lg_label"; then
|
|
1442
|
+
ok "P287 status:历史平铺根标 legacy"
|
|
1443
|
+
else
|
|
1444
|
+
bad "P287 status:历史平铺根没有 legacy 标注($(printf '%s' "$o_legacy" | grep -A1 -F "$lgc" | tr '\n' '|'))"
|
|
1445
|
+
fi
|
|
1446
|
+
if printf '%s\n' "$o_legacy" | grep -A1 -F " $curn" | grep -qF "$lg_label"; then
|
|
1447
|
+
bad "P287 status:带 ns 的根也被标了 legacy(不许)"
|
|
1448
|
+
else
|
|
1449
|
+
ok "P287 status:带 ns 的根不标 legacy"
|
|
1450
|
+
fi
|
|
1451
|
+
rm -rf -- "$lgc" "$curn"
|
|
946
1452
|
assert_out "$o_status" "$D/.teamsmith-smoke-diag.A1.log" "status 列出本项目的点开头诊断(不是根)"
|
|
947
1453
|
case "$o_status" in *"$D/.foreign-dot.hm"*) bad "status 列了别家的点文件(不是我们的家族,不该列)" ;; *) ok "status 不列别家的点文件(.foreign-dot.hm 不在输出里)" ;; esac
|
|
948
1454
|
|
|
@@ -987,26 +1493,37 @@ _hy_selftest() {
|
|
|
987
1493
|
|| bad "外来名字被动了(候选规则失效)"
|
|
988
1494
|
rm -rf -- "$fc" "$fm" "$fdot"
|
|
989
1495
|
|
|
990
|
-
# ── ⑥ lock
|
|
1496
|
+
# ── ⑥ lock:**锁对三样**(发布路径 + 伴生锁 + 运行注册)与两边的 .holder 只列不删;
|
|
1497
|
+
# 同名的陈旧平铺根照收(tick 5.2 的夹具:三样 + 一个陈旧平铺根,收完只剩根被回收)。
|
|
991
1498
|
local lockd="$D/teamsmith-smoke.G8"; mkdir -p "$lockd"; touch -d '2 hours ago' "$lockd"
|
|
992
1499
|
printf '' >"$D/teamsmith-smoke.lock"; printf 'holder' >"$D/teamsmith-smoke.lock.holder"
|
|
1500
|
+
printf '' >"$D/.teamsmith-smoke.lock.machine"
|
|
1501
|
+
printf 'holder' >"$D/.teamsmith-smoke.lock.machine.holder"
|
|
1502
|
+
mkdir -p "$D/.teamsmith-smoke.lock.machine.runs"
|
|
1503
|
+
printf 'x' >"$D/.teamsmith-smoke.lock.machine.runs/1.run"
|
|
993
1504
|
out="$(TMPDIR="$D" _hy "$(_hyflip lock)" --sweep --age 0 2>&1)"
|
|
994
|
-
if [ -f "$D/teamsmith-smoke.lock" ] && [ -f "$D/teamsmith-smoke.lock.holder" ]
|
|
995
|
-
|
|
1505
|
+
if [ -f "$D/teamsmith-smoke.lock" ] && [ -f "$D/teamsmith-smoke.lock.holder" ] \
|
|
1506
|
+
&& [ -f "$D/.teamsmith-smoke.lock.machine" ] && [ -f "$D/.teamsmith-smoke.lock.machine.holder" ] \
|
|
1507
|
+
&& [ -f "$D/.teamsmith-smoke.lock.machine.runs/1.run" ] && [ ! -d "$lockd" ]; then
|
|
1508
|
+
ok "锁对三样(发布路径 / 伴生锁 / 运行注册)与两个 .holder 全存活,只有陈旧平铺根被回收"
|
|
996
1509
|
else
|
|
997
|
-
bad "锁保护失效(
|
|
1510
|
+
bad "锁保护失效(pub=$([ -f "$D/teamsmith-smoke.lock" ] && echo 在 || echo 没了) comp=$([ -f "$D/.teamsmith-smoke.lock.machine" ] && echo 在 || echo 没了) runs=$([ -f "$D/.teamsmith-smoke.lock.machine.runs/1.run" ] && echo 在 || echo 没了) dir=$([ -d "$lockd" ] && echo 在 || echo 没了))"
|
|
998
1511
|
fi
|
|
999
|
-
rm -f "$D/teamsmith-smoke.lock" "$D/teamsmith-smoke.lock.holder"
|
|
1512
|
+
rm -f "$D/teamsmith-smoke.lock" "$D/teamsmith-smoke.lock.holder" \
|
|
1513
|
+
"$D/.teamsmith-smoke.lock.machine" "$D/.teamsmith-smoke.lock.machine.holder"
|
|
1514
|
+
rm -rf "$D/.teamsmith-smoke.lock.machine.runs"
|
|
1000
1515
|
|
|
1001
|
-
# ── ⑦ lint:绿侧干净 /
|
|
1516
|
+
# ── ⑦ lint:绿侧干净 / 三条红侧各点名 file:line(写死 /tmp、丢 ns、既非点开头也非 ns)
|
|
1002
1517
|
local ld="$D/lint-src"; mkdir -p "$ld"
|
|
1003
1518
|
cat >"$ld/ok.sh" <<'EOS'
|
|
1004
1519
|
#!/usr/bin/env bash
|
|
1005
|
-
TMP="$(mktemp -d "$
|
|
1520
|
+
TMP="$(mktemp -d "$(team_tmp_root)/teamsmith-ok-$(team_project_ns).XXXXXX")"
|
|
1521
|
+
FILE="$(mktemp "$(team_tmp_root)/teamsmith-probe.$(team_project_ns).XXXXXX")"
|
|
1006
1522
|
NEST="$(mktemp -d "$TMP/login-home.XXXXXX")"
|
|
1523
|
+
DOT="$(mktemp "$(team_tmp_root)/.teamsmith-dot.XXXXXX")"
|
|
1007
1524
|
EOS
|
|
1008
1525
|
out="$(TMPDIR="$D" _hy "$(_hyflip lint)" --lint --dir "$ld" 2>&1)"; rc=$?
|
|
1009
|
-
[ "$rc" = 0 ] && ok "lint
|
|
1526
|
+
[ "$rc" = 0 ] && ok "lint 对合规模板(根带 ns / 文件带 ns / 嵌套 / 点开头)退出 0" || bad "lint 对合规模板退出 $rc($(printf '%s' "$out" | head -2 | tr '\n' '|'))"
|
|
1010
1527
|
cat >"$ld/bad.sh" <<'EOS'
|
|
1011
1528
|
#!/usr/bin/env bash
|
|
1012
1529
|
TMP="$(mktemp -d /tmp/whatever.XXXXXX)"
|
|
@@ -1014,11 +1531,135 @@ EOS
|
|
|
1014
1531
|
out="$(TMPDIR="$D" _hy "$(_hyflip lint)" --lint --dir "$ld" 2>&1)"; rc=$?
|
|
1015
1532
|
[ "$rc" = 1 ] && ok "lint 对写死 /tmp 退出 1" || bad "lint 对写死 /tmp 退出 $rc(期望 1)"
|
|
1016
1533
|
assert_out "$out" "$ld/bad.sh:2" "lint finding 点名 file:line"
|
|
1017
|
-
#
|
|
1534
|
+
# P287 红侧 ①:保留家族前缀但丢 ns(根)
|
|
1535
|
+
cat >"$ld/nons.sh" <<'EOS'
|
|
1536
|
+
#!/usr/bin/env bash
|
|
1537
|
+
TMP="$(mktemp -d "$(team_tmp_root)/teamsmith-nons.XXXXXX")"
|
|
1538
|
+
EOS
|
|
1539
|
+
out="$(TMPDIR="$D" _hy "$(_hyflip lint)" --lint --dir "$ld" 2>&1)"; rc=$?
|
|
1540
|
+
[ "$rc" = 1 ] && ok "lint 对「家族前缀但丢 ns」的根模板退出 1" || bad "lint 对丢 ns 的根模板退出 $rc(期望 1)"
|
|
1541
|
+
assert_out "$out" "$ld/nons.sh:2" "丢 ns 的根 finding 点名 file:line"
|
|
1542
|
+
assert_out "$out" "命名空间" "丢 ns 的 finding 说明是命名空间问题"
|
|
1543
|
+
# P287 红侧 ②:每运行模板既不是点开头、也没有 ns
|
|
1544
|
+
cat >"$ld/flat.sh" <<'EOS'
|
|
1545
|
+
#!/usr/bin/env bash
|
|
1546
|
+
TMP="$(mktemp "$(team_tmp_root)/teamsmith-flat.XXXXXX")"
|
|
1547
|
+
EOS
|
|
1548
|
+
out="$(TMPDIR="$D" _hy "$(_hyflip lint)" --lint --dir "$ld" 2>&1)"; rc=$?
|
|
1549
|
+
[ "$rc" = 1 ] && ok "lint 对「既非点开头也无 ns」的每运行模板退出 1" || bad "lint 对平铺每运行模板退出 $rc(期望 1)"
|
|
1550
|
+
assert_out "$out" "$ld/flat.sh:2" "平铺每运行模板 finding 点名 file:line"
|
|
1551
|
+
# 红侧:break=lint 只关掉「不许写死绝对路径」这一条(ns 两条照判)
|
|
1018
1552
|
rm -f "$ld/bad.sh"
|
|
1553
|
+
out="$(TMPDIR="$D" _hy lint --lint --dir "$ld" 2>&1)"; rc=$?
|
|
1554
|
+
[ "$rc" = 1 ] && ok "break=lint 只关写死 /tmp 那条:ns 两条仍然红(rc=1)" || bad "break=lint 把 ns 规则也关掉了(rc=$rc,期望 1)"
|
|
1555
|
+
rm -f "$ld/nons.sh" "$ld/flat.sh"
|
|
1019
1556
|
out="$(TMPDIR="$D" _hy "$(_hyflip lint)" --lint --dir "$ld" 2>&1)"; rc=$?
|
|
1020
1557
|
[ "$rc" = 0 ] && ok "删掉违规模板后 lint 回到 0" || bad "lint 没有回到 0(rc=$rc)"
|
|
1021
1558
|
|
|
1559
|
+
# ── ⑦b P309 · 第二条规则:自己算临时根 = finding(`${TMPDIR:-/tmp}` / `${TMPDIR}` / JS 侧同类回落)
|
|
1560
|
+
local scd="$D/selfcomputed"; mkdir -p "$scd/scripts/lib" "$scd/scripts/panel/src"
|
|
1561
|
+
cat >"$scd/scripts/lib/selfbase.sh" <<'EOS'
|
|
1562
|
+
#!/usr/bin/env bash
|
|
1563
|
+
TMP="$(mktemp -d "${TMPDIR:-/tmp}/teamsmith-selfbase-$(team_project_ns).XXXXXX")"
|
|
1564
|
+
EOS
|
|
1565
|
+
cat >"$scd/scripts/panel/src/clip.ts" <<'EOS'
|
|
1566
|
+
const dir = process.env.TMPDIR || tmpdir()
|
|
1567
|
+
EOS
|
|
1568
|
+
out="$(TMPDIR="$D" _hy "" --lint --dir "$scd" 2>&1)"; rc=$?
|
|
1569
|
+
[ "$rc" = 1 ] && ok "P309 lint:自己算临时根 → 退出 1" || bad "P309 lint:自己算临时根没有判红(rc=$rc)"
|
|
1570
|
+
assert_out "$out" "selfbase.sh:2: 自己算临时根" "P309 lint finding 点名 file:line(shell 侧)"
|
|
1571
|
+
assert_out "$out" "clip.ts:1: 自己算临时根" "P309 lint finding 点名 file:line(JS 侧)"
|
|
1572
|
+
rm -rf -- "$scd"
|
|
1573
|
+
|
|
1574
|
+
# ── ⑦c P309 · 解析:一处实现、来源打印、分歧不静默、只读面按祖先判
|
|
1575
|
+
local r1 r2 src_out
|
|
1576
|
+
r1="$(TMPDIR="$D" bash -c '. "'"$SKILL_DIR"'/scripts/lib/common.sh"; team_tmp_root')"
|
|
1577
|
+
[ "$r1" = "$D" ] && ok "P309 解析:TMPDIR 赢(root=\$TMPDIR)" || bad "P309 解析:TMPDIR 没赢([$r1])"
|
|
1578
|
+
r2="$(env -u TMPDIR -u TEAM_TMP_ROOT bash -c '. "'"$SKILL_DIR"'/scripts/lib/common.sh"; team_tmp_root')"
|
|
1579
|
+
case "$r2" in
|
|
1580
|
+
/var/tmp/teamsmith-*) ok "P309 解析:两块都不设 → 磁盘默认($r2)" ;;
|
|
1581
|
+
*) bad "P309 解析:默认腿不在磁盘上([$r2])" ;;
|
|
1582
|
+
esac
|
|
1583
|
+
r2="$(env -u TMPDIR TEAM_TMP_ROOT="$D" bash -c '. "'"$SKILL_DIR"'/scripts/lib/common.sh"; team_tmp_root')"
|
|
1584
|
+
local r2src
|
|
1585
|
+
r2src="$(env -u TMPDIR TEAM_TMP_ROOT="$D" bash -c '. "'"$SKILL_DIR"'/scripts/lib/common.sh"; team_tmp_root_source')"
|
|
1586
|
+
[ "$r2" = "$D" ] && [ "$r2src" = "config" ] \
|
|
1587
|
+
&& ok "P309 解析:TMPDIR 不设时契约键生效(source=config)" \
|
|
1588
|
+
|| bad "P309 解析:契约键没生效([$r2] source=[$r2src])"
|
|
1589
|
+
src_out="$(env TEAM_TMP_ROOT="$D/other" TMPDIR="$D" bash -c '. "'"$SKILL_DIR"'/scripts/lib/common.sh"; team_tmp_root_note')"
|
|
1590
|
+
assert_out "$src_out" "source=tmpdir" "P309 分歧行说 TMPDIR 赢了"
|
|
1591
|
+
assert_out "$src_out" "TEAM_TMP_ROOT=$D/other 未生效" "P309 分歧行点名没生效的契约值(不静默)"
|
|
1592
|
+
# 默认根不存在时:只读面按最近的存在祖先判,且**不创建**任何东西
|
|
1593
|
+
local ghost="$D/ghost-root/nested"
|
|
1594
|
+
mkdir -p "$D/ghost-root"
|
|
1595
|
+
src_out="$(TMPDIR="$ghost" _hy "" --status 2>&1)"; rc=$?
|
|
1596
|
+
assert_out "$src_out" "$ghost" "P309 只读面点名解析出的根(即使它还不存在)"
|
|
1597
|
+
assert_out "$src_out" "尚未创建,按 $D/ghost-root 的文件系统判" "P309 只读面按最近的存在祖先判"
|
|
1598
|
+
[ -e "$ghost" ] && bad "P309 只读面把不存在的根创建出来了" || ok "P309 只读面没有创建任何东西"
|
|
1599
|
+
# 动态红侧①:清扫器自己算基准(break=inline-root)
|
|
1600
|
+
if [ "$break_stage" = "inline-root" ]; then
|
|
1601
|
+
out="$(env -u TEAM_TMP_ROOT TEAM_TMP_HYGIENE_BASE_SELF=1 bash "$me" --status 2>&1 | head -1)"
|
|
1602
|
+
assert_out "$out" "/var/tmp/teamsmith-" "P309 红侧①:清扫器自己算基准时必须点名磁盘默认(这条在 break=inline-root 下应红)"
|
|
1603
|
+
else
|
|
1604
|
+
out="$(env -u TEAM_TMP_ROOT TMPDIR= bash "$me" --status 2>&1 | head -1)"
|
|
1605
|
+
local defroot
|
|
1606
|
+
defroot="$(env -u TMPDIR -u TEAM_TMP_ROOT bash -c '. "'"$SKILL_DIR"'/scripts/lib/common.sh"; team_tmp_root')"
|
|
1607
|
+
assert_out "$out" "$defroot" "P309 status 表头点名解析出的根"
|
|
1608
|
+
fi
|
|
1609
|
+
|
|
1610
|
+
# ── ⑦d P309 · 地板绑选定根:选定的根很空、旧默认 /tmp 很满时不许拒绝
|
|
1611
|
+
local ftem="$D/floor.table"
|
|
1612
|
+
: >"$ftem"
|
|
1613
|
+
printf '%s\t15245736\t5242880\t3811434\t2978499\n' "$BASE" >>"$ftem"
|
|
1614
|
+
[ "$BASE" = "/tmp" ] || printf '/tmp\t15245736\t122880\t3811434\t40000\n' >>"$ftem"
|
|
1615
|
+
local fout frc=0 judged="$BASE"
|
|
1616
|
+
# 红侧②(break=fixed-floor):把地板**指回固定 /tmp**(缺陷形状)—— 下面的守门断言必须因此变红
|
|
1617
|
+
[ "${break_stage}" = "fixed-floor" ] && judged="/tmp"
|
|
1618
|
+
[ "${break_stage}" = "inline-root" ] && judged="/tmp"
|
|
1619
|
+
fout="$(env -u TEAM_TMP_ROOT TMPDIR="$BASE" TEAM_DISK_STATS_FILE="$ftem" \
|
|
1620
|
+
bash -c '. "'"$SKILL_DIR"'/scripts/lib/common.sh"; team_disk_guard "$1"' _ "$judged" 2>&1)" || frc=$?
|
|
1621
|
+
if [ "$frc" = 0 ]; then ok "P309 地板:选定的根很空 → 放行(不看 /tmp)"
|
|
1622
|
+
else bad "P309 地板:判的是 $judged 而不是选定的根($fout)"; fi
|
|
1623
|
+
case "$fout" in
|
|
1624
|
+
*"/tmp "*) bad "P309 地板:拒绝/读数里出现了 /tmp(判的不是选定的根)" ;;
|
|
1625
|
+
*) ok "P309 地板:读数只提选定的根" ;;
|
|
1626
|
+
esac
|
|
1627
|
+
# 同一份夹具指向 /tmp 时**必须**拒绝并点名它 —— 证明上面那条放行真的看了「选定的根」这一行
|
|
1628
|
+
frc=0
|
|
1629
|
+
fout="$(env -u TEAM_TMP_ROOT TEAM_DISK_STATS_FILE="$ftem" \
|
|
1630
|
+
bash -c '. "'"$SKILL_DIR"'/scripts/lib/common.sh"; team_disk_guard /tmp' 2>&1)" || frc=$?
|
|
1631
|
+
[ "$frc" = 1 ] && assert_out "$fout" "/tmp 磁盘不足" "P309 红侧②对照:同一份夹具指向 /tmp 就拒绝并点名它" \
|
|
1632
|
+
|| bad "P309 红侧②对照:指向 /tmp 却没拒绝(rc=$frc)"
|
|
1633
|
+
|
|
1634
|
+
# ── ⑦e P309 · 创建者:显式根不可用 = 具名失败 + 修法,绝不回落 /tmp
|
|
1635
|
+
local crc=0 cout before_tmp after_tmp
|
|
1636
|
+
before_tmp="$(ls -A /tmp 2>/dev/null | wc -l | tr -d ' ')"
|
|
1637
|
+
local crbreak=""; [ "$break_stage" = "missing-root" ] && crbreak="fallback"
|
|
1638
|
+
cout="$(env TMPDIR= TEAM_TMP_ROOT="$D/does-not-exist" TMP_ROOT_BREAK="$crbreak" \
|
|
1639
|
+
bash -c '. "'"$SKILL_DIR"'/tests/lib/tmp-root.sh"; tmp_root_create selftest' 2>&1)" || crc=$?
|
|
1640
|
+
after_tmp="$(ls -A /tmp 2>/dev/null | wc -l | tr -d ' ')"
|
|
1641
|
+
if [ "$break_stage" = "missing-root" ]; then
|
|
1642
|
+
# 翻转预期:回落形状下创建会“成功”,守门断言因此红
|
|
1643
|
+
[ "$crc" = 0 ] && bad "P309 红侧③:break=missing-root 下创建者静默回落了(守门断言应当红 —— 翻转预期)"
|
|
1644
|
+
else
|
|
1645
|
+
[ "$crc" = 3 ] && ok "P309 创建者:显式根不存在 → 退出 3" || bad "P309 创建者:显式根不存在退出 $crc(期望 3)"
|
|
1646
|
+
assert_out "$cout" "$D/does-not-exist" "P309 创建者:失败点名那个路径"
|
|
1647
|
+
assert_out "$cout" "修法:" "P309 创建者:失败给修法"
|
|
1648
|
+
[ "$before_tmp" = "$after_tmp" ] && ok "P309 创建者:失败后 /tmp 下没有新条目" || bad "P309 创建者:失败时在 /tmp 下留下了东西"
|
|
1649
|
+
fi
|
|
1650
|
+
# 不是目录 / 不可写:两种形状都具名失败
|
|
1651
|
+
: >"$D/afile"
|
|
1652
|
+
crc=0; cout="$(env TMPDIR= TEAM_TMP_ROOT="$D/afile" bash -c '. "'"$SKILL_DIR"'/tests/lib/tmp-root.sh"; tmp_root_create selftest' 2>&1)" || crc=$?
|
|
1653
|
+
[ "$crc" = 3 ] && assert_out "$cout" "不是目录" "P309 创建者:不是目录 → 具名失败" || bad "P309 创建者:不是目录退出 $crc(期望 3)"
|
|
1654
|
+
mkdir -p "$D/ro"; chmod 500 "$D/ro"
|
|
1655
|
+
if [ ! -w "$D/ro" ]; then
|
|
1656
|
+
crc=0; cout="$(env TMPDIR= TEAM_TMP_ROOT="$D/ro" bash -c '. "'"$SKILL_DIR"'/tests/lib/tmp-root.sh"; tmp_root_create selftest' 2>&1)" || crc=$?
|
|
1657
|
+
[ "$crc" = 3 ] && assert_out "$cout" "不可写" "P309 创建者:不可写 → 具名失败" || bad "P309 创建者:不可写退出 $crc(期望 3)"
|
|
1658
|
+
else
|
|
1659
|
+
printf ' \033[2m(skip)\033[0m P309 创建者:root 绕过权限位(不可写形状跑不出来)\n'
|
|
1660
|
+
fi
|
|
1661
|
+
chmod 700 "$D/ro" 2>/dev/null || true
|
|
1662
|
+
|
|
1022
1663
|
# ── ⑧ worktree / record:注册的 worktree 只打印命令;没有已提交记录的检出被拒
|
|
1023
1664
|
local repo="$D/repo" wt="$D/review-T1.1"
|
|
1024
1665
|
mkdir -p "$repo"
|
|
@@ -1172,6 +1813,101 @@ PY
|
|
|
1172
1813
|
[ -d "$froot" ] && ok "P122 台账 repo= 指向别家 → sweep 不碰" || bad "P122 台账别家的根被删了"
|
|
1173
1814
|
rm -rf -- "$froot" "$D/.teamsmith-tmp-ledger.$frun"
|
|
1174
1815
|
|
|
1816
|
+
# ── ⑭ P313:已删除但仍被持有的文件(df 与 du 的差额)
|
|
1817
|
+
# 两个方向都要:有人持有已删文件 → 读数必须 >0(归属本项目的那一个进 --sweep 的计划);
|
|
1818
|
+
# 没人持有 → 读数必须恰好 0(「0 就是 0,不是读不到」)。红侧 break=held:这条读数回到
|
|
1819
|
+
# 旧形状(不存在)→ 正方向断言必须红,而 0 方向仍绿。红侧 break=heldpath:计划行退回
|
|
1820
|
+
# 「只印 argv」的旧形状 → 「计划行点名目标路径」那条断言必须红(V58 F1)。
|
|
1821
|
+
local hd="$D/p313-held" hpid_own hpid_for hpid_live hwait hout hn np nb nf
|
|
1822
|
+
hd="$D/p313-held"; mkdir -p "$hd" "$hd/teamsmith-heldforeign.Z9"
|
|
1823
|
+
# ① 的目标文件套在一串长目录名下:计划行要印**完整**路径(V58 实测 argv 里 171 字符的路径被
|
|
1824
|
+
# 截在 140 字节),而持有者自己的 argv(`exec sleep 60`)里一个路径字符都没有。
|
|
1825
|
+
local hown="$hd/teamsmith-held-$(_hy_ns).H1/run-tmp-a-very-long-dir-name-for-the-v58-path-clause/fixture-stuck-1.log"
|
|
1826
|
+
mkdir -p "$(dirname "$hown")" "$hd/teamsmith-held-$(_hy_ns).H1"
|
|
1827
|
+
printf '%02048d\n' 0 >"$hown"
|
|
1828
|
+
printf '%01024d\n' 0 >"$hd/teamsmith-heldforeign.Z9/x.log"
|
|
1829
|
+
printf '%03072d\n' 0 >"$hd/teamsmith-held-$(_hy_ns).H1/live.log"
|
|
1830
|
+
# 三个持有者,形状就是现场那三种:
|
|
1831
|
+
# ① 孤儿(= 被打断的运行留下的):中间短命 shell 起心跳后立刻退 → 心跳被 init 收养,父进程已不在;
|
|
1832
|
+
# ② 活着的(= 某个正在跑的夹具在用):父链就是本脚本 → **不许**进建议;
|
|
1833
|
+
# ③ 别家的(根名不带本项目 ns):只进合计。
|
|
1834
|
+
# 等它们的外壳退场,好让 ① 真的变成孤儿(ppid=1),再开始断言。
|
|
1835
|
+
( hwait=""; bash -c 'exec 3<"$1"; rm -f "$1"; exec sleep 60' _ "$hown" &
|
|
1836
|
+
hwait=$!; printf '%s\n' "$hwait" >"$hd/own.pid" ) &
|
|
1837
|
+
( bash -c 'exec 3<"$1"; rm -f "$1"; exec sleep 60' _ "$hd/teamsmith-heldforeign.Z9/x.log" &
|
|
1838
|
+
printf '%s\n' "$!" >"$hd/for.pid" ) &
|
|
1839
|
+
( exec 3<"$hd/teamsmith-held-$(_hy_ns).H1/live.log"; rm -f "$hd/teamsmith-held-$(_hy_ns).H1/live.log"; exec sleep 60 ) &
|
|
1840
|
+
hpid_live=$!
|
|
1841
|
+
local _w=0
|
|
1842
|
+
while [ "$_w" -lt 40 ]; do
|
|
1843
|
+
[ -s "$hd/own.pid" ] && [ -s "$hd/for.pid" ] && break
|
|
1844
|
+
sleep 0.05; _w=$((_w + 1))
|
|
1845
|
+
done
|
|
1846
|
+
hpid_own="$(cat "$hd/own.pid" 2>/dev/null || true)"
|
|
1847
|
+
hpid_for="$(cat "$hd/for.pid" 2>/dev/null || true)"
|
|
1848
|
+
_w=0
|
|
1849
|
+
while [ "$_w" -lt 40 ]; do
|
|
1850
|
+
[ "$(awk '{ sub(/^.*\) /, ""); print $2 }' "/proc/${hpid_own:-0}/stat" 2>/dev/null || true)" = "1" ] && break
|
|
1851
|
+
sleep 0.05; _w=$((_w + 1))
|
|
1852
|
+
done
|
|
1853
|
+
sleep 0.2
|
|
1854
|
+
# 旧默认根也在扫描面里(P309 之前的运行都写在 /tmp —— P313 的泄漏就在那里)。这一条只读根清单,
|
|
1855
|
+
# 不往 /tmp 里写任何东西;机器上真实的持有者会让计数变化,但不会让这条断言失准。
|
|
1856
|
+
hout="$(TMPDIR="$hd" _hy "" --status 2>&1)"
|
|
1857
|
+
case "$hout" in
|
|
1858
|
+
# 夹具的根在 run 临时根里,所以「它的基」是 run 根(真实运行时是 /var/tmp —— 验证者的副本就套在那里)。
|
|
1859
|
+
# 断言三件:解析出的根在最前、基在中间、旧默认根 /tmp 在最后。
|
|
1860
|
+
*"扫描的根:$hd · "*" · /tmp"*)
|
|
1861
|
+
ok "P313 status:扫描面 = 解析出的根 + 它的基(/var/tmp 那一层,验证者副本在那里)+ 旧默认根(/tmp)" ;;
|
|
1862
|
+
*) bad "P313 status:扫描面没有点名基与旧默认根($(printf '%s' "$hout" | grep -F '扫描的根' | head -1))" ;;
|
|
1863
|
+
esac
|
|
1864
|
+
# 下面的计数断言把扫描的根限定在夹具里(机器上的 /tmp 不能影响两个方向的判定)
|
|
1865
|
+
hout="$(TMPDIR="$hd" TEAM_TMP_HYGIENE_HELD_ROOTS="$hd" _hy "$(_hyflip held)" --status 2>&1)"
|
|
1866
|
+
hn="$(printf '%s\n' "$hout" | sed -n 's/^ 合计:\([0-9][0-9]*\) 个进程 · .*(\([0-9][0-9]*\) B)· \([0-9][0-9]*\) 个已删文件$/\1 \2 \3/p' | head -1)"
|
|
1867
|
+
np=""; nb=""; nf=""
|
|
1868
|
+
[ -n "$hn" ] && read -r np nb nf <<<"$hn"
|
|
1869
|
+
if [ "${np:-0}" -ge 3 ] 2>/dev/null && [ "${nb:-0}" -ge 6144 ] 2>/dev/null; then
|
|
1870
|
+
ok "P313 status:有人持有已删文件时读数 >0(${np} 个进程 · ${nb} B · ${nf} 个文件)"
|
|
1871
|
+
else
|
|
1872
|
+
bad "P313 status:持有已删文件时读数是 [$hn](期望 ≥3 个进程 / ≥6144 B)—— 这一项就是 df 与 du 的差额"
|
|
1873
|
+
fi
|
|
1874
|
+
assert_out "$hout" "可归属本项目(删除目标所在路径里有一层是本项目的运行根" "P313 status:把「可归属本项目」的持有者数单独报出来"
|
|
1875
|
+
assert_out "$hout" "其中父进程已不在(被打断的运行留下的孤儿):1 个进程" "P313 status:把「父进程已不在」的那部分单独报出来(孤儿才是泄漏)"
|
|
1876
|
+
case "$hout" in
|
|
1877
|
+
*"[suggest-kill]"*) bad "P313 status:点了建议结束的 pid(status 只报总量)" ;;
|
|
1878
|
+
*) ok "P313 status:只报总量,不点名 pid(别家的 pid 不进这一面)" ;;
|
|
1879
|
+
esac
|
|
1880
|
+
hout="$(TMPDIR="$hd" TEAM_TMP_HYGIENE_HELD_ROOTS="$hd" _hy "$(_hyflip held heldpath)" --sweep --age 0 2>&1)"
|
|
1881
|
+
assert_out "$hout" "[suggest-kill] pid=$hpid_own" "P313 sweep:可归属本项目 + 父进程已不在的持有者进「建议结束」的计划(点名 pid)"
|
|
1882
|
+
# F1 / V58:「计划行要点名目标路径」—— 这个持有者的 argv 里没有路径(`exec sleep` + fd),
|
|
1883
|
+
# 路径只能来自扫描时的 fpath[];长路径要完整印。
|
|
1884
|
+
case "$(tr '\0' ' ' <"/proc/$hpid_own/cmdline" 2>/dev/null || true)" in
|
|
1885
|
+
*"$hown"*) bad "P313 sweep:夹具失效 —— 持有者的 argv 里带了路径(红侧测不到「argv 里没有路径」)" ;;
|
|
1886
|
+
*) ok "P313 sweep:红侧持有者的 argv 里没有路径(exec sleep + fd 持有已删文件)" ;;
|
|
1887
|
+
esac
|
|
1888
|
+
assert_out "$hout" "目标:$hown" "P313 sweep:计划行点名被持有的目标路径(长路径完整印,不从 argv 顶替)"
|
|
1889
|
+
case "$hout" in
|
|
1890
|
+
*"[suggest-kill] pid=$hpid_for"*) bad "P313 sweep:点了别家的持有者 pid(只许合计里计数)" ;;
|
|
1891
|
+
*) ok "P313 sweep:别家的持有者只进合计,不点名 pid" ;;
|
|
1892
|
+
esac
|
|
1893
|
+
case "$hout" in
|
|
1894
|
+
*"pid=$hpid_live"*) bad "P313 sweep:把一个父链还在的持有者列为建议结束(那是别人正在跑的夹具)" ;;
|
|
1895
|
+
*) ok "P313 sweep:父链还在的持有者不进建议(活着的运行在用它们)" ;;
|
|
1896
|
+
esac
|
|
1897
|
+
kill -0 "$hpid_own" 2>/dev/null && kill -0 "$hpid_for" 2>/dev/null && kill -0 "$hpid_live" 2>/dev/null \
|
|
1898
|
+
&& ok "P313 sweep:计划就是计划(三个持有者都还活着,脚本没发信号)" \
|
|
1899
|
+
|| bad "P313 sweep:sweep 对持有者动了手(只许给计划)"
|
|
1900
|
+
kill -KILL "$hpid_own" "$hpid_for" "$hpid_live" 2>/dev/null || true
|
|
1901
|
+
wait "$hpid_own" "$hpid_for" "$hpid_live" 2>/dev/null || true
|
|
1902
|
+
hout="$(TMPDIR="$hd" TEAM_TMP_HYGIENE_HELD_ROOTS="$hd" _hy "" --status 2>&1)"
|
|
1903
|
+
hn="$(printf '%s\n' "$hout" | sed -n 's/^ 合计:\([0-9][0-9]*\) 个进程 · .*(\([0-9][0-9]*\) B)· \([0-9][0-9]*\) 个已删文件$/\1 \2 \3/p' | head -1)"
|
|
1904
|
+
if [ "$hn" = "0 0 0" ]; then
|
|
1905
|
+
ok "P313 status:没有持有时读数恰好 0(0 个进程 · 0 B · 0 个文件)"
|
|
1906
|
+
else
|
|
1907
|
+
bad "P313 status:没有持有时读数是 [$hn](期望「0 0 0」)"
|
|
1908
|
+
fi
|
|
1909
|
+
rm -rf -- "$hd"
|
|
1910
|
+
|
|
1175
1911
|
printf '\n\033[1m== 结果 ==\033[0m ✓ %d ✗ %d\n' "$PASS" "$FAIL"
|
|
1176
1912
|
[ "$FAIL" -eq 0 ] && { printf '\033[32mtmp-hygiene.sh 自检全绿\033[0m\n'; return 0; }
|
|
1177
1913
|
printf '\033[31mtmp-hygiene.sh 自检有失败项(break=%s)\033[0m\n' "${break_stage:-none}"
|
|
@@ -1183,6 +1919,7 @@ assert_out() { case "$1" in *"$2"*) ok "$3" ;; *) bad "$3(输出里找不到 [
|
|
|
1183
1919
|
case "${1:-}" in
|
|
1184
1920
|
--status) shift; cmd_status "$@" ;;
|
|
1185
1921
|
--lint) shift; cmd_lint "$@" ;;
|
|
1922
|
+
--docs-check) shift; cmd_docs_check "$@" ;;
|
|
1186
1923
|
--sweep) shift; cmd_sweep "$@" ;;
|
|
1187
1924
|
--self-test) shift; _hy_selftest "$@" ;;
|
|
1188
1925
|
-h|--help|"") _usage ;;
|