teamsmith 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +158 -2
- package/bin/team.mjs +59 -0
- package/install.sh +94 -0
- package/package.json +37 -4
- package/skills/teamsmith/CHANGELOG.md +763 -0
- package/skills/teamsmith/SKILL.md +312 -0
- package/skills/teamsmith/extension/team-bg.ts +435 -0
- package/skills/teamsmith/extension/team-inbox-watch.ts +1412 -0
- package/skills/teamsmith/extension/team-notify.ts +663 -0
- package/skills/teamsmith/references/agent-adapters.md +643 -0
- package/skills/teamsmith/references/config.md +477 -0
- package/skills/teamsmith/references/meeting.md +124 -0
- package/skills/teamsmith/references/memory.md +133 -0
- package/skills/teamsmith/references/migration.md +227 -0
- package/skills/teamsmith/references/openspec.md +222 -0
- package/skills/teamsmith/references/philosophy.md +95 -0
- package/skills/teamsmith/references/protocol.md +712 -0
- package/skills/teamsmith/references/troubleshooting.md +1384 -0
- package/skills/teamsmith/references/workflows.md +448 -0
- package/skills/teamsmith/scripts/lib/cmd-agents.sh +1709 -0
- package/skills/teamsmith/scripts/lib/cmd-bg.sh +284 -0
- package/skills/teamsmith/scripts/lib/cmd-bootstrap.sh +311 -0
- package/skills/teamsmith/scripts/lib/cmd-config.sh +1156 -0
- package/skills/teamsmith/scripts/lib/cmd-death.sh +562 -0
- package/skills/teamsmith/scripts/lib/cmd-docs.sh +149 -0
- package/skills/teamsmith/scripts/lib/cmd-draft.sh +124 -0
- package/skills/teamsmith/scripts/lib/cmd-init.sh +212 -0
- package/skills/teamsmith/scripts/lib/cmd-meeting.sh +904 -0
- package/skills/teamsmith/scripts/lib/cmd-outbox.sh +196 -0
- package/skills/teamsmith/scripts/lib/cmd-project.sh +642 -0
- package/skills/teamsmith/scripts/lib/cmd-review.sh +1393 -0
- package/skills/teamsmith/scripts/lib/cmd-status.sh +1399 -0
- package/skills/teamsmith/scripts/lib/cmd-update.sh +191 -0
- package/skills/teamsmith/scripts/lib/cmd-watch.sh +1681 -0
- package/skills/teamsmith/scripts/lib/common.sh +4964 -0
- package/skills/teamsmith/scripts/lib/draft-entry.sh +51 -0
- package/skills/teamsmith/scripts/lib/outbox.sh +2042 -0
- package/skills/teamsmith/scripts/lib/tmux-argv.sh +165 -0
- package/skills/teamsmith/scripts/monitor.mjs +616 -0
- package/skills/teamsmith/scripts/panel/README.md +192 -0
- package/skills/teamsmith/scripts/panel/build.sh +47 -0
- package/skills/teamsmith/scripts/panel/bun.lock +109 -0
- package/skills/teamsmith/scripts/panel/draft-send.sh +68 -0
- package/skills/teamsmith/scripts/panel/package.json +24 -0
- package/skills/teamsmith/scripts/panel/panel.js +388 -0
- package/skills/teamsmith/scripts/panel/src/App.tsx +2150 -0
- package/skills/teamsmith/scripts/panel/src/clipboard.ts +154 -0
- package/skills/teamsmith/scripts/panel/src/compose.ts +691 -0
- package/skills/teamsmith/scripts/panel/src/data.ts +555 -0
- package/skills/teamsmith/scripts/panel/src/format.ts +50 -0
- package/skills/teamsmith/scripts/panel/src/layout.ts +2562 -0
- package/skills/teamsmith/scripts/panel/src/main.tsx +894 -0
- package/skills/teamsmith/scripts/panel/src/markdown.ts +340 -0
- package/skills/teamsmith/scripts/panel/src/sanitize.ts +42 -0
- package/skills/teamsmith/scripts/panel/src/settings.ts +154 -0
- package/skills/teamsmith/scripts/panel/src/strings/en.ts +416 -0
- package/skills/teamsmith/scripts/panel/src/strings/index.ts +65 -0
- package/skills/teamsmith/scripts/panel/src/strings/types.ts +9 -0
- package/skills/teamsmith/scripts/panel/src/strings/zh.ts +414 -0
- package/skills/teamsmith/scripts/panel/src/theme.ts +116 -0
- package/skills/teamsmith/scripts/panel/src/types.ts +652 -0
- package/skills/teamsmith/scripts/panel/src/width.ts +105 -0
- package/skills/teamsmith/scripts/panel/tsconfig.json +16 -0
- package/skills/teamsmith/scripts/shim/signal-gate +156 -0
- package/skills/teamsmith/scripts/shim/tmux +403 -0
- package/skills/teamsmith/scripts/team +143 -0
- package/skills/teamsmith/templates/AGENTS.section.md.tmpl +147 -0
- package/skills/teamsmith/templates/BOARD.md.tmpl +18 -0
- package/skills/teamsmith/templates/DECISIONS.md.tmpl +22 -0
- package/skills/teamsmith/templates/OWNERSHIP.md.tmpl +25 -0
- package/skills/teamsmith/templates/PROTOCOL.md.tmpl +84 -0
- package/skills/teamsmith/templates/ROADMAP.md.tmpl +29 -0
- package/skills/teamsmith/templates/config.sh.tmpl +106 -0
- package/skills/teamsmith/templates/memory-seed.md.tmpl +46 -0
- package/skills/teamsmith/templates/pm-prompt.md.tmpl +110 -0
- package/skills/teamsmith/templates/report.md.tmpl +36 -0
- package/skills/teamsmith/templates/task.md.tmpl +65 -0
- package/skills/teamsmith/templates/threads-README.md +17 -0
- package/skills/teamsmith/tests/bg-ledger-harness.mjs +682 -0
- package/skills/teamsmith/tests/bg-ledger.sh +307 -0
- package/skills/teamsmith/tests/checkout-shape-probe.sh +800 -0
- package/skills/teamsmith/tests/config-cli.sh +1562 -0
- package/skills/teamsmith/tests/container-tmux.sh +493 -0
- package/skills/teamsmith/tests/coverage-inventory.sh +173 -0
- package/skills/teamsmith/tests/death-cause.sh +564 -0
- package/skills/teamsmith/tests/delivery-truth.sh +1231 -0
- package/skills/teamsmith/tests/fake-tui.py +450 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/README.md +60 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/judge-second.py +139 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/mock-server.py +29 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/observe.ts +20 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/run-case.sh +79 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/scenario.sh +175 -0
- package/skills/teamsmith/tests/fixtures/p55/flip-p49.sh +229 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/README.md +22 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/dev2.seen +8 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/dev2.wake +9 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/pm-nudges.wake +3 -0
- package/skills/teamsmith/tests/fixtures/signal-lint-cases.txt +40 -0
- package/skills/teamsmith/tests/flip-glue.sh +162 -0
- package/skills/teamsmith/tests/flip-m12-26c.sh +129 -0
- package/skills/teamsmith/tests/flip-m12.sh +185 -0
- package/skills/teamsmith/tests/flip-m16.sh +166 -0
- package/skills/teamsmith/tests/flip-m17.sh +204 -0
- package/skills/teamsmith/tests/flip-m33.sh +284 -0
- package/skills/teamsmith/tests/flip-m34.sh +96 -0
- package/skills/teamsmith/tests/flip-m37.sh +185 -0
- package/skills/teamsmith/tests/flip-m4.3.sh +375 -0
- package/skills/teamsmith/tests/flip-m43.sh +128 -0
- package/skills/teamsmith/tests/flip-m44.sh +262 -0
- package/skills/teamsmith/tests/flip-m45.sh +244 -0
- package/skills/teamsmith/tests/flip-m46.sh +238 -0
- package/skills/teamsmith/tests/flip-m48.sh +304 -0
- package/skills/teamsmith/tests/flip-m6.2.sh +205 -0
- package/skills/teamsmith/tests/flip-m6.3.sh +283 -0
- package/skills/teamsmith/tests/flip-m6.4.sh +267 -0
- package/skills/teamsmith/tests/flip-m6.5.sh +113 -0
- package/skills/teamsmith/tests/flip-m7.1.sh +135 -0
- package/skills/teamsmith/tests/flip-m7.2.sh +321 -0
- package/skills/teamsmith/tests/flip-p144.sh +112 -0
- package/skills/teamsmith/tests/flip-p156.sh +133 -0
- package/skills/teamsmith/tests/flip-p158.sh +301 -0
- package/skills/teamsmith/tests/flip-p160.sh +182 -0
- package/skills/teamsmith/tests/flip-p171.sh +249 -0
- package/skills/teamsmith/tests/flip-p18.1.sh +95 -0
- package/skills/teamsmith/tests/flip-p20.sh +286 -0
- package/skills/teamsmith/tests/flip-p201.sh +249 -0
- package/skills/teamsmith/tests/flip-p210.sh +239 -0
- package/skills/teamsmith/tests/flip-p211.sh +152 -0
- package/skills/teamsmith/tests/flip-p217.sh +288 -0
- package/skills/teamsmith/tests/flip-p22.sh +123 -0
- package/skills/teamsmith/tests/flip-p23.sh +239 -0
- package/skills/teamsmith/tests/flip-p25.sh +165 -0
- package/skills/teamsmith/tests/flip-p71.sh +184 -0
- package/skills/teamsmith/tests/flip-p72.sh +202 -0
- package/skills/teamsmith/tests/frames/README.md +176 -0
- package/skills/teamsmith/tests/frames/p78-conversation-rule-below-box.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-cursor-mid-draft.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor-line.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-blank-region.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-only.txt +5 -0
- package/skills/teamsmith/tests/frames/p78-spinner-row-below-cursor.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-wider-rule-below-cursor.txt +6 -0
- package/skills/teamsmith/tests/frames/p86-f1-mixed-width-disjoint-box.txt +8 -0
- package/skills/teamsmith/tests/frames/p86-f1-narrower-width-disjoint-box.txt +8 -0
- package/skills/teamsmith/tests/frames/p86-f1-spinner-top-disjoint-box.txt +8 -0
- package/skills/teamsmith/tests/frames/pi-0.85.1-update-banner.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-draft-half-sentence.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-empty-box.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-one-line-draft.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-project-trust-prompt.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.99.2-empty-editor.txt +32 -0
- package/skills/teamsmith/tests/frames/pi-0.99.2-human-draft.txt +32 -0
- package/skills/teamsmith/tests/frames/pi-1.0.0-git-error-in-box.txt +32 -0
- package/skills/teamsmith/tests/gate-guard.sh +112 -0
- package/skills/teamsmith/tests/guard-matrix.sh +233 -0
- package/skills/teamsmith/tests/install-shape.sh +731 -0
- package/skills/teamsmith/tests/ledger-ban.sh +612 -0
- package/skills/teamsmith/tests/lib/box-judge.sh +68 -0
- package/skills/teamsmith/tests/lib/checkout-shape.sh +129 -0
- package/skills/teamsmith/tests/lib/loop-scan.awk +114 -0
- package/skills/teamsmith/tests/lib/pty-wait.sh +676 -0
- package/skills/teamsmith/tests/lib/section-guard.sh +855 -0
- package/skills/teamsmith/tests/lib/shell-lex.pl +261 -0
- package/skills/teamsmith/tests/lib/tmp-root.sh +528 -0
- package/skills/teamsmith/tests/lib/tmux-cap.sh +35 -0
- package/skills/teamsmith/tests/lib/tmux-iso-call-probe.sh +81 -0
- package/skills/teamsmith/tests/lib/tmux-iso-probe.sh +46 -0
- package/skills/teamsmith/tests/lib/tmux-iso.sh +381 -0
- package/skills/teamsmith/tests/load-experiment.sh +290 -0
- package/skills/teamsmith/tests/loop-inventory.tsv +84 -0
- package/skills/teamsmith/tests/panel-b2-slow-stub.sh +19 -0
- package/skills/teamsmith/tests/panel-b2.sh +1183 -0
- package/skills/teamsmith/tests/panel-b3-pty-mouse.py +240 -0
- package/skills/teamsmith/tests/panel-b3-pty-tmux-mouse.py +138 -0
- package/skills/teamsmith/tests/panel-b3-stub.sh +279 -0
- package/skills/teamsmith/tests/panel-b3.sh +1301 -0
- package/skills/teamsmith/tests/panel-choices.sh +572 -0
- package/skills/teamsmith/tests/panel-clipboard-model.ts +187 -0
- package/skills/teamsmith/tests/panel-compose-model.ts +290 -0
- package/skills/teamsmith/tests/panel-contrast.mjs +81 -0
- package/skills/teamsmith/tests/panel-cpu-premise.sh +173 -0
- package/skills/teamsmith/tests/panel-cpu.sh +391 -0
- package/skills/teamsmith/tests/panel-flip-m54.sh +207 -0
- package/skills/teamsmith/tests/panel-flip-p123.sh +278 -0
- package/skills/teamsmith/tests/panel-flip-p125.sh +318 -0
- package/skills/teamsmith/tests/panel-flip-p128.sh +259 -0
- package/skills/teamsmith/tests/panel-keyprobe-pty.py +99 -0
- package/skills/teamsmith/tests/panel-keyprobe-stub.sh +41 -0
- package/skills/teamsmith/tests/panel-keyprobe.sh +82 -0
- package/skills/teamsmith/tests/panel-keyprobe.tsx +116 -0
- package/skills/teamsmith/tests/panel-knobs.sh +83 -0
- package/skills/teamsmith/tests/panel-p21.sh +2181 -0
- package/skills/teamsmith/tests/panel-snapshots.sh +802 -0
- package/skills/teamsmith/tests/panel-strings.mjs +195 -0
- package/skills/teamsmith/tests/perf.sh +740 -0
- package/skills/teamsmith/tests/pm-box-real.sh +350 -0
- package/skills/teamsmith/tests/pulse-nudge-key.sh +563 -0
- package/skills/teamsmith/tests/routes.sh +1652 -0
- package/skills/teamsmith/tests/section-budgets.tsv +150 -0
- package/skills/teamsmith/tests/section-guard.sh +231 -0
- package/skills/teamsmith/tests/section-needs-audit.sh +94 -0
- package/skills/teamsmith/tests/section-paths.tsv +142 -0
- package/skills/teamsmith/tests/section-select.sh +655 -0
- package/skills/teamsmith/tests/signal-gate.sh +366 -0
- package/skills/teamsmith/tests/signal-lint-legacy.txt +14 -0
- package/skills/teamsmith/tests/signal-lint.pl +416 -0
- package/skills/teamsmith/tests/skill-load.mjs +65 -0
- package/skills/teamsmith/tests/smoke.sh +19711 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-120-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-120-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-120.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-160-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-160.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-59-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-59-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-59.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-99-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-99.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-120-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-120-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-120.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-160-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-160.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-59-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-59-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-59.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-99-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-99.txt +32 -0
- package/skills/teamsmith/tests/spec-ledger-refs.tsv +78 -0
- package/skills/teamsmith/tests/spec-refs.sh +826 -0
- package/skills/teamsmith/tests/task-header-model.sh +267 -0
- package/skills/teamsmith/tests/team-bg-flip.sh +239 -0
- package/skills/teamsmith/tests/team-bg-harness.mjs +392 -0
- package/skills/teamsmith/tests/team-bg-stop.sh +434 -0
- package/skills/teamsmith/tests/team-inbox-watch-flip.sh +191 -0
- package/skills/teamsmith/tests/team-inbox-watch-harness.mjs +1559 -0
- package/skills/teamsmith/tests/tmp-hygiene.sh +1190 -0
- package/skills/teamsmith/tests/tmux-lint-legacy.txt +31 -0
- package/skills/teamsmith/tests/tmux-lint.pl +604 -0
- package/skills/teamsmith-init/SKILL.md +100 -0
- package/skills/teamsmith-init/references/bootstrap.md +74 -0
- package/skills/teamsmith-init/templates/bootstrap-prompt.md.tmpl +46 -0
|
@@ -0,0 +1,855 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# section-guard.sh — 门禁的**每段自述 + 硬预算 + 现场**(change: gate-section-accounting · 实现:P70)
|
|
3
|
+
#
|
|
4
|
+
# 被 `tests/smoke.sh` source(也支持 `bash tests/lib/section-guard.sh --self-test` 自检)。
|
|
5
|
+
# 契约见 `openspec/changes/gate-section-accounting/specs/verification/spec.md`:
|
|
6
|
+
# * 每段开跑前打印 `== #<N> <id> == <ISO-8601> · 预算 <B>s`;结束时打印**唯一**的收口行
|
|
7
|
+
# `#<N> <id> · 用时 <S>s · <✓/✗/SKIP 增量> · ticks <T>`(stdout 与 sections.log 逐字节一致;
|
|
8
|
+
# 增量由 P98 账本经 SG_CLOSE_COUNTS 注入,没有账本时是 `-`);
|
|
9
|
+
# 每段一行进 `<run tmp>/sections.tsv`(`no id start elapsed_s ticks`)。
|
|
10
|
+
# * 每段有一个来自 `tests/section-budgets.tsv` 的硬预算(表里没有 → 有界默认值,并在开跑行里说明);
|
|
11
|
+
# 第一段超预算 → **停跑**:看门狗打印一行点名该段(`#<N>` + id + 预算 + 实际用时)、写现场、
|
|
12
|
+
# 然后才发信号;套件在自己的安全点看到 trip 标记 → exit 2。
|
|
13
|
+
# * 看门狗是**双重 fork**(不进作业表:裸 `wait` 不会被它卡住),只对进程树里的子孙发信号,
|
|
14
|
+
# **绝不** `kill -- -$pgid`(门禁的进程组里有 team review / CI / 启动它的席位)。
|
|
15
|
+
# * 超预算 = **存活检测**,不是性能判定:段内只要在预算内跑完,机器再慢也绿(D33 保持)。
|
|
16
|
+
# * 夹具旋钮只在 `TEAM_SMOKE_FIXTURE=1` 下生效,否则**打印忽略行**(环境不能悄悄拆掉边界)。
|
|
17
|
+
#
|
|
18
|
+
# 现场(trip 时):`${TMPDIR:-/tmp}/.teamsmith-smoke-scene.<pid>/`,点开头、在 run 自己的临时根**之外**
|
|
19
|
+
# —— 套件的清理(含失败时删根)带不走它,CI 的 `docker cp <容器>:/tmp/.` 原样收走(D39 通道不动)。
|
|
20
|
+
|
|
21
|
+
SG_SECTION_GUARD_LIB=1
|
|
22
|
+
|
|
23
|
+
# ---------------------------------------------------------------- 状态(全部有默认值:smoke 是 set -u)
|
|
24
|
+
SG_ENABLED=0
|
|
25
|
+
SG_ARMED=0
|
|
26
|
+
SG_TRIPPED=0
|
|
27
|
+
SG_SEC_NO=0
|
|
28
|
+
SG_SEC_ID=""
|
|
29
|
+
SG_SEC_START=""
|
|
30
|
+
SG_BUDGET=""
|
|
31
|
+
SG_BAND=""
|
|
32
|
+
SG_BUDGET_SOURCE=""
|
|
33
|
+
SG_ARMED_EPOCH=0
|
|
34
|
+
SG_ARMED_SECONDS=0
|
|
35
|
+
SG_TICKS=0
|
|
36
|
+
SG_ELAPSED=0
|
|
37
|
+
SG_OWNER_PID="$$"
|
|
38
|
+
SG_OWNER_START=""
|
|
39
|
+
SG_BASE="${TMPDIR:-/tmp}"
|
|
40
|
+
SG_RUN_TMP=""
|
|
41
|
+
SG_BUDGETS=""
|
|
42
|
+
SG_TIMING=""
|
|
43
|
+
SG_LOG=""
|
|
44
|
+
SG_HEARTBEAT=""
|
|
45
|
+
SG_MARKER=""
|
|
46
|
+
SG_SCENE=""
|
|
47
|
+
SG_STOP=""
|
|
48
|
+
SG_ACK=""
|
|
49
|
+
SG_WATCHDOG_PIDFILE=""
|
|
50
|
+
SG_POLL=1
|
|
51
|
+
SG_DESC_GRACE=6
|
|
52
|
+
SG_SUITE_GRACE=2
|
|
53
|
+
SG_PROGRESS_INTERVAL=60
|
|
54
|
+
SG_DEFAULT_BUDGET=900
|
|
55
|
+
SG_FIXTURE="${TEAM_SMOKE_FIXTURE:-0}"
|
|
56
|
+
SG_STUCK_SECTION=""
|
|
57
|
+
SG_BUDGET_OVERRIDE=""
|
|
58
|
+
SG_FAST="${TEAM_SMOKE_FAST:-0}"
|
|
59
|
+
SG_LOCK_NOTE=""
|
|
60
|
+
SG_CLOSE_COUNTS="" # P98 账本:本段收口行里的 ✓/✗/SKIP 增量(关段前注入;空 → 行里打 `-`)
|
|
61
|
+
|
|
62
|
+
_sg_now() { date +%s; }
|
|
63
|
+
|
|
64
|
+
_sg_proc_start() { # /proc/<pid>/stat 的 starttime:pid 复用安全的判据
|
|
65
|
+
local st; st="$(LC_ALL=C cat "/proc/$1/stat" 2>/dev/null)" || return 1
|
|
66
|
+
st="${st##*\) }"; set -- $st
|
|
67
|
+
[ -n "${20:-}" ] || return 1
|
|
68
|
+
printf '%s\n' "${20}"
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
_sg_pid_alive() { # <pid> [<starttime>] → 0 活着且(给了 starttime 时)还是那个进程
|
|
72
|
+
local pid="${1:-}" want="${2:-}" st
|
|
73
|
+
case "$pid" in ''|*[!0-9]*) return 1 ;; esac
|
|
74
|
+
[ "$pid" -gt 1 ] || return 1
|
|
75
|
+
kill -0 "$pid" 2>/dev/null || return 1
|
|
76
|
+
if [ -n "$want" ]; then
|
|
77
|
+
st="$(_sg_proc_start "$pid" 2>/dev/null || true)"
|
|
78
|
+
[ "$st" = "$want" ] || return 1
|
|
79
|
+
fi
|
|
80
|
+
return 0
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
# ---------------------------------------------------------------- 初始化
|
|
84
|
+
# section_guard_init <run tmp> [--poll <s>] [--progress <s>] [--suite-grace <s>]
|
|
85
|
+
# [--desc-grace <n>] [--default-budget <s>] [--budgets <file>]
|
|
86
|
+
section_guard_init() {
|
|
87
|
+
SG_RUN_TMP="${1:-}"; shift || true
|
|
88
|
+
while [ $# -gt 0 ]; do
|
|
89
|
+
case "$1" in
|
|
90
|
+
--poll) SG_POLL="${2:-1}"; shift 2 ;;
|
|
91
|
+
--progress) SG_PROGRESS_INTERVAL="${2:-60}"; shift 2 ;;
|
|
92
|
+
--suite-grace) SG_SUITE_GRACE="${2:-2}"; shift 2 ;;
|
|
93
|
+
--desc-grace) SG_DESC_GRACE="${2:-6}"; shift 2 ;;
|
|
94
|
+
--default-budget) SG_DEFAULT_BUDGET="${2:-900}"; shift 2 ;;
|
|
95
|
+
--budgets) SG_BUDGETS="${2:-}"; shift 2 ;;
|
|
96
|
+
*) printf 'section-guard: 不认识的参数 %s\n' "$1" >&2; return 2 ;;
|
|
97
|
+
esac
|
|
98
|
+
done
|
|
99
|
+
SG_BASE="${TMPDIR:-/tmp}"
|
|
100
|
+
SG_FIXTURE="${TEAM_SMOKE_FIXTURE:-0}"
|
|
101
|
+
SG_OWNER_PID="${BASHPID:-$$}"
|
|
102
|
+
SG_OWNER_START="$(_sg_proc_start "$SG_OWNER_PID" 2>/dev/null || true)"
|
|
103
|
+
SG_HEARTBEAT="$SG_BASE/.teamsmith-smoke-heartbeat.$SG_OWNER_PID"
|
|
104
|
+
SG_MARKER="$SG_BASE/.teamsmith-smoke-trip.$SG_OWNER_PID"
|
|
105
|
+
SG_SCENE="$SG_BASE/.teamsmith-smoke-scene.$SG_OWNER_PID"
|
|
106
|
+
SG_STOP="$SG_BASE/.teamsmith-smoke-guard-stop.$SG_OWNER_PID"
|
|
107
|
+
SG_ACK="$SG_BASE/.teamsmith-smoke-guard-ack.$SG_OWNER_PID"
|
|
108
|
+
SG_WATCHDOG_PIDFILE="$SG_BASE/.teamsmith-smoke-guard.pid.$SG_OWNER_PID"
|
|
109
|
+
SG_TIMING="$SG_RUN_TMP/sections.tsv"
|
|
110
|
+
SG_LOG="$SG_RUN_TMP/sections.log"
|
|
111
|
+
[ -n "$SG_BUDGETS" ] || SG_BUDGETS="${SG_SKILL_DIR:-}/tests/section-budgets.tsv"
|
|
112
|
+
_sg_parse_knobs
|
|
113
|
+
if [ ! -s "$SG_TIMING" ]; then
|
|
114
|
+
printf 'no\tid\tstart\telapsed_s\tticks\n' > "$SG_TIMING" 2>/dev/null || true
|
|
115
|
+
fi
|
|
116
|
+
: > "$SG_LOG" 2>/dev/null || true
|
|
117
|
+
return 0
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
_sg_knob_note() { # <name> <value>:非夹具路径下的忽略行(旋钮不许从环境拆掉边界)
|
|
121
|
+
printf ' \033[2m·\033[0m 注意:%s=%s 只给夹具路径用;非夹具路径忽略它\n' "$1" "$2"
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
_sg_parse_knobs() {
|
|
125
|
+
local k v
|
|
126
|
+
SG_STUCK_SECTION=""
|
|
127
|
+
SG_BUDGET_OVERRIDE=""
|
|
128
|
+
SG_POLL="${SG_POLL:-1}"
|
|
129
|
+
SG_PROGRESS_INTERVAL="${SG_PROGRESS_INTERVAL:-60}"
|
|
130
|
+
if [ "${SG_FIXTURE:-0}" = "1" ]; then
|
|
131
|
+
SG_STUCK_SECTION="${TEAM_SMOKE_STUCK_SECTION:-}"
|
|
132
|
+
SG_BUDGET_OVERRIDE="${TEAM_SMOKE_SECTION_BUDGET:-}"
|
|
133
|
+
[ -n "${TEAM_SMOKE_PROGRESS_INTERVAL:-}" ] && SG_PROGRESS_INTERVAL="$TEAM_SMOKE_PROGRESS_INTERVAL"
|
|
134
|
+
[ -n "${TEAM_SMOKE_GUARD_POLL:-}" ] && SG_POLL="$TEAM_SMOKE_GUARD_POLL"
|
|
135
|
+
return 0
|
|
136
|
+
fi
|
|
137
|
+
for k in TEAM_SMOKE_STUCK_SECTION TEAM_SMOKE_SECTION_BUDGET TEAM_SMOKE_PROGRESS_INTERVAL TEAM_SMOKE_GUARD_POLL SMOKE_PROGRESS_INTERVAL; do
|
|
138
|
+
case "$k" in
|
|
139
|
+
TEAM_SMOKE_STUCK_SECTION) v="${TEAM_SMOKE_STUCK_SECTION:-}" ;;
|
|
140
|
+
TEAM_SMOKE_SECTION_BUDGET) v="${TEAM_SMOKE_SECTION_BUDGET:-}" ;;
|
|
141
|
+
TEAM_SMOKE_PROGRESS_INTERVAL) v="${TEAM_SMOKE_PROGRESS_INTERVAL:-}" ;;
|
|
142
|
+
TEAM_SMOKE_GUARD_POLL) v="${TEAM_SMOKE_GUARD_POLL:-}" ;;
|
|
143
|
+
SMOKE_PROGRESS_INTERVAL) v="${SMOKE_PROGRESS_INTERVAL:-}" ;;
|
|
144
|
+
esac
|
|
145
|
+
[ -n "$v" ] && _sg_knob_note "$k" "$v"
|
|
146
|
+
done
|
|
147
|
+
SG_PROGRESS_INTERVAL=60
|
|
148
|
+
SG_POLL=1
|
|
149
|
+
return 0
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
# ---------------------------------------------------------------- 预算表
|
|
153
|
+
# section_guard_budget_for <id> → 设置 SG_BUDGET / SG_BAND / SG_BUDGET_SOURCE
|
|
154
|
+
section_guard_budget_for() {
|
|
155
|
+
local id="$1" row=""
|
|
156
|
+
if [ -n "$SG_BUDGETS" ] && [ -r "$SG_BUDGETS" ]; then
|
|
157
|
+
row="$(awk -F'\t' -v id="$id" '
|
|
158
|
+
/^[[:space:]]*#/ { next }
|
|
159
|
+
$1 == id { print $2 "\t" $3; found = 1; exit }
|
|
160
|
+
END { if (!found) exit 1 }
|
|
161
|
+
' "$SG_BUDGETS" 2>/dev/null || true)"
|
|
162
|
+
fi
|
|
163
|
+
# 行必须在、band 可以是 "-"(没测过),budget 必须是正整数
|
|
164
|
+
case "$row" in
|
|
165
|
+
*$'\t'*) SG_BAND="${row%% *}"; SG_BUDGET="${row##* }" ;;
|
|
166
|
+
*) SG_BAND=""; SG_BUDGET="" ;;
|
|
167
|
+
esac
|
|
168
|
+
case "${SG_BUDGET:-x}" in ''|*[!0-9]*) row="" ;; esac
|
|
169
|
+
if [ -n "$row" ]; then
|
|
170
|
+
SG_BUDGET_SOURCE="table"
|
|
171
|
+
else
|
|
172
|
+
SG_BAND=""
|
|
173
|
+
SG_BUDGET="$SG_DEFAULT_BUDGET"
|
|
174
|
+
SG_BUDGET_SOURCE="default"
|
|
175
|
+
fi
|
|
176
|
+
# 夹具的预算覆盖:只作用于被点名的 stuck 段;没点名就作用于每段(自检/夹具用)
|
|
177
|
+
if [ "${SG_FIXTURE:-0}" = "1" ] && [ -n "$SG_BUDGET_OVERRIDE" ]; then
|
|
178
|
+
case "${SG_BUDGET_OVERRIDE:-x}" in *[!0-9]*) ;; *)
|
|
179
|
+
if [ -z "$SG_STUCK_SECTION" ] || _sg_id_matches "$SG_STUCK_SECTION" "$id"; then
|
|
180
|
+
SG_BUDGET="$SG_BUDGET_OVERRIDE"
|
|
181
|
+
SG_BUDGET_SOURCE="fixture"
|
|
182
|
+
fi
|
|
183
|
+
;;
|
|
184
|
+
esac
|
|
185
|
+
fi
|
|
186
|
+
return 0
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
_sg_id_matches() { # <knob> <id>:完整 id 或「前缀 + 空格」都算
|
|
190
|
+
local knob="$1" id="$2"
|
|
191
|
+
case "$id" in
|
|
192
|
+
"$knob"|"$knob "*) return 0 ;;
|
|
193
|
+
*) return 1 ;;
|
|
194
|
+
esac
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
# ---------------------------------------------------------------- 心跳 / 安全点
|
|
198
|
+
section_guard_write_heartbeat() {
|
|
199
|
+
[ "$SG_ENABLED" = "1" ] || return 0
|
|
200
|
+
if [ -z "$SG_SEC_ID" ]; then
|
|
201
|
+
printf 'disarm\n' > "$SG_HEARTBEAT" 2>/dev/null || true
|
|
202
|
+
return 0
|
|
203
|
+
fi
|
|
204
|
+
SG_ELAPSED=$(( SECONDS - SG_ARMED_SECONDS ))
|
|
205
|
+
printf '%s|%s|%s|%s|%s|%s\n' \
|
|
206
|
+
"$SG_SEC_NO" "$SG_ARMED_EPOCH" "$SG_BUDGET" "$SG_ELAPSED" "$SG_TICKS" "$SG_SEC_ID" \
|
|
207
|
+
> "$SG_HEARTBEAT" 2>/dev/null || true
|
|
208
|
+
return 0
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
section_guard_progress_tick() {
|
|
212
|
+
[ "$SG_ENABLED" = "1" ] || return 0
|
|
213
|
+
[ "$SG_ARMED" = "1" ] || return 0
|
|
214
|
+
SG_TICKS=$((SG_TICKS + 1))
|
|
215
|
+
section_guard_write_heartbeat
|
|
216
|
+
return 0
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
# 安全点:trip 标记在 → 停跑(看门狗已经打印过点名行;这里只退出,不重复点名)
|
|
220
|
+
_sg_check_trip() {
|
|
221
|
+
[ "$SG_ENABLED" = "1" ] || return 0
|
|
222
|
+
if [ -e "$SG_MARKER" ]; then
|
|
223
|
+
SG_TRIPPED=1
|
|
224
|
+
_sg_exit_trip
|
|
225
|
+
fi
|
|
226
|
+
return 0
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
# ok()/bad()/skip/等待轮都调用它:先看 trip,再把 ticks/elapsed 写回心跳(纯内建写入)
|
|
230
|
+
section_guard_check() {
|
|
231
|
+
[ "$SG_ENABLED" = "1" ] || return 0
|
|
232
|
+
_sg_check_trip
|
|
233
|
+
[ "$SG_ARMED" = "1" ] || return 0
|
|
234
|
+
section_guard_progress_tick
|
|
235
|
+
return 0
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
_sg_exit_trip() {
|
|
239
|
+
trap - TERM 2>/dev/null || true
|
|
240
|
+
if [ "${BASHPID:-$$}" = "$$" ]; then
|
|
241
|
+
# 主 shell:先收看门狗,别让它在 cleanup 之后继续打;EXIT trap 照常收临时根
|
|
242
|
+
section_guard_stop_watchdog
|
|
243
|
+
fi
|
|
244
|
+
exit 2
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
section_guard_on_term() { # 看门狗对我们的 TERM:trip → exit 2;否则恢复默认动作重新自杀
|
|
248
|
+
if [ -e "$SG_MARKER" ]; then
|
|
249
|
+
_sg_exit_trip
|
|
250
|
+
fi
|
|
251
|
+
trap - TERM 2>/dev/null || true
|
|
252
|
+
kill -TERM "${BASHPID:-$$}" 2>/dev/null || true
|
|
253
|
+
exit 143
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
# ---------------------------------------------------------------- 段落开闭
|
|
257
|
+
_sg_start_line() {
|
|
258
|
+
local src=""
|
|
259
|
+
case "$SG_BUDGET_SOURCE" in
|
|
260
|
+
default) src="(默认:预算表没有这段)" ;;
|
|
261
|
+
fixture) src="(夹具覆盖)" ;;
|
|
262
|
+
esac
|
|
263
|
+
printf '== #%s %s == %s · 预算 %ss%s' "$SG_SEC_NO" "$SG_SEC_ID" "$SG_SEC_START" "$SG_BUDGET" "$src"
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
# _sg_band_over <elapsed> <band> → 0 当且仅当 band 是数字且 elapsed 超过它(**记录**,不是判定)。
|
|
267
|
+
# band 是小数也必须比 —— 表的 110/112 行是 `0.02 … 44.00`(P108 F2:旧的 `case ''|*[!0-9]*`
|
|
268
|
+
# 整数守卫把带小数点的带全落进空分支,规范承诺的「超过实测带打一行警告」对它们永不可达)。
|
|
269
|
+
# 空 / `-` = 表里没有实测带 → 不比较;非数字(表坏了)由 `section-guard.sh --budget-check` 点名。
|
|
270
|
+
_sg_band_over() {
|
|
271
|
+
local elapsed="$1" band="${2:-}"
|
|
272
|
+
case "$band" in
|
|
273
|
+
''|-) return 1 ;; # 没有实测带(表里写「-」)→ 不比较
|
|
274
|
+
*[!0-9.]*) return 1 ;; # 非数字(表坏了,由 --budget-check 点名)→ 不比较
|
|
275
|
+
esac
|
|
276
|
+
awk -v a="$elapsed" -v b="$band" 'BEGIN { exit !(a + 0 > b + 0) }'
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
_sg_close_current() {
|
|
280
|
+
[ "$SG_ARMED" = "1" ] || return 0
|
|
281
|
+
local elapsed now counts
|
|
282
|
+
now="$(_sg_now)"; elapsed=$(( now - SG_ARMED_EPOCH ))
|
|
283
|
+
[ "$elapsed" -lt 0 ] && elapsed=0
|
|
284
|
+
SG_ELAPSED="$elapsed" # P98 账本读回:收口行与 sections.tsv 用同一个数,不再各量一次
|
|
285
|
+
counts="${SG_CLOSE_COUNTS:--}"; SG_CLOSE_COUNTS=""
|
|
286
|
+
# 唯一收口行(gate-section-accounting × gate-runtime-budget 的合并形态):P70 的用时/ticks 与
|
|
287
|
+
# P98 账本的增量同形同一行。stdout 与 sections.log 逐字节一致;行不带门禁红标、不缩进。
|
|
288
|
+
printf '\033[2m#%s\033[0m %s · 用时 %ss · %s · ticks %s\n' \
|
|
289
|
+
"$SG_SEC_NO" "$SG_SEC_ID" "$elapsed" "$counts" "$SG_TICKS"
|
|
290
|
+
printf '#%s %s · 用时 %ss · %s · ticks %s\n' \
|
|
291
|
+
"$SG_SEC_NO" "$SG_SEC_ID" "$elapsed" "$counts" "$SG_TICKS" >> "$SG_LOG" 2>/dev/null || true
|
|
292
|
+
printf '%s\t%s\t%s\t%s\t%s\n' "$SG_SEC_NO" "$SG_SEC_ID" "$SG_SEC_START" "$elapsed" "$SG_TICKS" >> "$SG_TIMING" 2>/dev/null || true
|
|
293
|
+
if _sg_band_over "$elapsed" "${SG_BAND:-}"; then
|
|
294
|
+
printf ' \033[33m⚠\033[0m #%s 用时 %ss,超过实测带 %ss(这是记录,不是判定)\n' \
|
|
295
|
+
"$SG_SEC_NO" "$elapsed" "$SG_BAND"
|
|
296
|
+
fi
|
|
297
|
+
SG_ARMED=0
|
|
298
|
+
SG_SEC_ID=""
|
|
299
|
+
printf 'disarm\n' > "$SG_HEARTBEAT" 2>/dev/null || true
|
|
300
|
+
return 0
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
section_guard_close() { # 公共收口入口(P98 账本用):调用方先给 SG_CLOSE_COUNTS 注入本段增量
|
|
304
|
+
_sg_close_current
|
|
305
|
+
SG_CLOSE_COUNTS="" # 就算没武装(无收口行)也清掉:不让一次注入粘到下一段
|
|
306
|
+
return 0
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
section_guard_begin() { # <id>:收上一段 → 开这一段(含夹具注入)
|
|
310
|
+
local id="$1" line
|
|
311
|
+
[ "$SG_ENABLED" = "1" ] || return 0
|
|
312
|
+
_sg_check_trip
|
|
313
|
+
[ "$SG_ARMED" = "1" ] && _sg_close_current
|
|
314
|
+
SG_SEC_NO=$((SG_SEC_NO + 1))
|
|
315
|
+
SG_SEC_ID="$id"
|
|
316
|
+
SG_SEC_START="$(date -Is)"
|
|
317
|
+
SG_ARMED_EPOCH="$(_sg_now)"
|
|
318
|
+
SG_ARMED_SECONDS="$SECONDS"
|
|
319
|
+
SG_TICKS=0
|
|
320
|
+
section_guard_budget_for "$id"
|
|
321
|
+
SG_ARMED=1
|
|
322
|
+
section_guard_write_heartbeat
|
|
323
|
+
line="$(_sg_start_line)"
|
|
324
|
+
printf '\n\033[1m%s\033[0m\n' "$line"
|
|
325
|
+
printf '%s\n' "$line" >> "$SG_LOG" 2>/dev/null || true
|
|
326
|
+
# 夹具注入:被点名的段落永不返回(预算到点必须点名 + 停跑 + 留现场)
|
|
327
|
+
if [ "${SG_FIXTURE:-0}" = "1" ] && [ -n "$SG_STUCK_SECTION" ] && _sg_id_matches "$SG_STUCK_SECTION" "$id"; then
|
|
328
|
+
printf ' \033[35m[fixture]\033[0m 注入永不返回的段落(%s,预算 %ss)\n' "$id" "$SG_BUDGET"
|
|
329
|
+
local stuck_log="$SG_RUN_TMP/fixture-stuck-$SG_SEC_NO.log"
|
|
330
|
+
printf 'fixture 注入:段落 #%s(%s)永不返回(预算 %ss)\n' "$SG_SEC_NO" "$id" "$SG_BUDGET" > "$stuck_log" 2>/dev/null || true
|
|
331
|
+
bash -c 'log="$1"; trap "" TERM; while :; do date +%s >> "$log"; sleep 0.2; done' _ "$stuck_log" &
|
|
332
|
+
local child=$!
|
|
333
|
+
command -v tmp_root_track_pid >/dev/null 2>&1 && tmp_root_track_pid "$child" "section-guard-stuck" || true
|
|
334
|
+
wait "$child" 2>/dev/null || true
|
|
335
|
+
_sg_check_trip
|
|
336
|
+
printf ' \033[35m[fixture]\033[0m 注入的段落返回了(trip 标记没出现?)\n'
|
|
337
|
+
fi
|
|
338
|
+
return 0
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
section_guard_finish() { # 结果行之前:收最后一段 + 关看门狗
|
|
342
|
+
[ "$SG_ENABLED" = "1" ] || return 0
|
|
343
|
+
[ "$SG_ARMED" = "1" ] && _sg_close_current
|
|
344
|
+
section_guard_stop_watchdog
|
|
345
|
+
SG_ENABLED=0
|
|
346
|
+
return 0
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
# ---------------------------------------------------------------- 看门狗
|
|
350
|
+
section_guard_arm() {
|
|
351
|
+
SG_ENABLED=1
|
|
352
|
+
SG_ARMED=0
|
|
353
|
+
SG_TRIPPED=0
|
|
354
|
+
SG_SEC_NO=0
|
|
355
|
+
printf 'disarm\n' > "$SG_HEARTBEAT" 2>/dev/null || true
|
|
356
|
+
rm -f "$SG_MARKER" "$SG_STOP" "$SG_ACK" "$SG_WATCHDOG_PIDFILE" 2>/dev/null || true
|
|
357
|
+
( section_guard_watchdog & )
|
|
358
|
+
# 起 TERM 陷阱:trip 的 TERM → exit 2;别的 TERM → 恢复默认动作(见 section_guard_on_term)
|
|
359
|
+
trap 'section_guard_on_term' TERM
|
|
360
|
+
local i=0
|
|
361
|
+
for i in $(seq 1 50); do
|
|
362
|
+
[ -s "$SG_WATCHDOG_PIDFILE" ] && break
|
|
363
|
+
sleep 0.02
|
|
364
|
+
done
|
|
365
|
+
if [ ! -s "$SG_WATCHDOG_PIDFILE" ]; then
|
|
366
|
+
printf ' \033[31m✗\033[0m section-guard:看门狗没起来(%s)—— 本套**没有**每段硬预算\n' "$SG_WATCHDOG_PIDFILE"
|
|
367
|
+
fi
|
|
368
|
+
return 0
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
section_guard_stop_watchdog() {
|
|
372
|
+
: > "$SG_STOP" 2>/dev/null || true
|
|
373
|
+
local i wpid wstart
|
|
374
|
+
for i in $(seq 1 25); do [ -e "$SG_ACK" ] && break; sleep 0.02; done
|
|
375
|
+
wpid="$(awk -F'\t' 'NR==1{print $1}' "$SG_WATCHDOG_PIDFILE" 2>/dev/null || true)"
|
|
376
|
+
wstart="$(awk -F'\t' 'NR==1{print $2}' "$SG_WATCHDOG_PIDFILE" 2>/dev/null || true)"
|
|
377
|
+
if [ ! -e "$SG_ACK" ] && [ -n "$wpid" ]; then
|
|
378
|
+
# 没握手(比如看门狗卡在写现场):按 pid + starttime 有界地收,绝不按名字/进程组
|
|
379
|
+
_sg_pid_alive "$wpid" "${wstart:-}" && kill -TERM "$wpid" 2>/dev/null || true
|
|
380
|
+
for i in $(seq 1 25); do
|
|
381
|
+
_sg_pid_alive "$wpid" "${wstart:-}" || break
|
|
382
|
+
sleep 0.02
|
|
383
|
+
done
|
|
384
|
+
_sg_pid_alive "$wpid" "${wstart:-}" && kill -KILL "$wpid" 2>/dev/null || true
|
|
385
|
+
fi
|
|
386
|
+
rm -f "$SG_STOP" "$SG_ACK" "$SG_WATCHDOG_PIDFILE" 2>/dev/null || true
|
|
387
|
+
return 0
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
# 收尾(cleanup 里调用):干净跑把哨兵的兄弟文件收掉;trip 过 → 现场留着(那是证据)
|
|
391
|
+
section_guard_sweep() {
|
|
392
|
+
rm -f "$SG_STOP" "$SG_ACK" "$SG_WATCHDOG_PIDFILE" 2>/dev/null || true
|
|
393
|
+
[ "$SG_TRIPPED" = "1" ] && return 0
|
|
394
|
+
rm -f "$SG_HEARTBEAT" "$SG_MARKER" 2>/dev/null || true
|
|
395
|
+
return 0
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
_sg_descendants() { # <pid> <exclude> → 叶子优先
|
|
399
|
+
local pid="$1" exclude="$2" f c
|
|
400
|
+
for f in /proc/"$pid"/task/*/children; do
|
|
401
|
+
[ -r "$f" ] || continue
|
|
402
|
+
for c in $(cat "$f" 2>/dev/null); do
|
|
403
|
+
[ -n "$c" ] || continue
|
|
404
|
+
[ "$c" = "$exclude" ] && continue
|
|
405
|
+
_sg_descendants "$c" "$exclude"
|
|
406
|
+
printf '%s\n' "$c"
|
|
407
|
+
done
|
|
408
|
+
done
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
section_guard_watchdog() {
|
|
412
|
+
local me="$BASHPID"
|
|
413
|
+
printf '%s\t%s\n' "$me" "$(_sg_proc_start "$me" 2>/dev/null || true)" > "$SG_WATCHDOG_PIDFILE" 2>/dev/null || true
|
|
414
|
+
local line no armed budget elapsed ticks id now last_seen=0 last_progress=0
|
|
415
|
+
while :; do
|
|
416
|
+
[ -e "$SG_STOP" ] && break
|
|
417
|
+
_sg_pid_alive "$SG_OWNER_PID" "$SG_OWNER_START" || break
|
|
418
|
+
line="$(cat "$SG_HEARTBEAT" 2>/dev/null || true)"
|
|
419
|
+
case "$line" in ''|disarm*) sleep "$SG_POLL"; continue ;; esac
|
|
420
|
+
IFS='|' read -r no armed budget elapsed ticks id <<<"$line"
|
|
421
|
+
case "${no:-x}${armed:-x}${budget:-x}" in *[!0-9]*) sleep "$SG_POLL"; continue ;; esac
|
|
422
|
+
now="$(_sg_now)"
|
|
423
|
+
case "${elapsed:-0}" in ''|*[!0-9]*) elapsed=0 ;; esac
|
|
424
|
+
[ "${elapsed}" -gt "$last_seen" ] && last_seen="$elapsed"
|
|
425
|
+
case "${SG_PROGRESS_INTERVAL:-60}" in ''|*[!0-9]*) SG_PROGRESS_INTERVAL=60 ;; esac
|
|
426
|
+
if [ "$SG_PROGRESS_INTERVAL" -gt 0 ] \
|
|
427
|
+
&& [ "$(( now - armed ))" -ge "$SG_PROGRESS_INTERVAL" ] \
|
|
428
|
+
&& [ "$(( now - last_progress ))" -ge "$SG_PROGRESS_INTERVAL" ]; then
|
|
429
|
+
_sg_progress_line "$no" "$id" "$(( now - armed ))" "$budget"
|
|
430
|
+
last_progress=$now
|
|
431
|
+
fi
|
|
432
|
+
if [ "$(( now - armed ))" -ge "$budget" ]; then
|
|
433
|
+
_sg_trip "$no" "$id" "$armed" "$budget" "$ticks" "$last_seen" "$now"
|
|
434
|
+
break
|
|
435
|
+
fi
|
|
436
|
+
sleep "$SG_POLL"
|
|
437
|
+
done
|
|
438
|
+
rm -f "$SG_WATCHDOG_PIDFILE" 2>/dev/null || true
|
|
439
|
+
: > "$SG_ACK" 2>/dev/null || true
|
|
440
|
+
exit 0
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
_sg_progress_line() {
|
|
444
|
+
local no="$1" id="$2" elapsed="$3" budget="$4" slow=""
|
|
445
|
+
slow="$(awk -F'\t' 'NR > 1 && $4 ~ /^[0-9]+$/ { printf "%s\t#%s %s\n", $4, $1, $2 }' "$SG_TIMING" 2>/dev/null \
|
|
446
|
+
| sort -rn 2>/dev/null | head -3 | cut -f2 | tr '\n' ' ' || true)"
|
|
447
|
+
printf ' \033[2m… 段落 #%s(%s)已运行 %ss(预算 %ss)%s\033[0m\n' \
|
|
448
|
+
"$no" "$id" "$elapsed" "$budget" "${slow:+;最慢:$slow}"
|
|
449
|
+
printf '… 段落 #%s(%s)已运行 %ss(预算 %ss)%s\n' \
|
|
450
|
+
"$no" "$id" "$elapsed" "$budget" "${slow:+;最慢:$slow}" >> "$SG_LOG" 2>/dev/null || true
|
|
451
|
+
return 0
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
_sg_trip() { # <no> <id> <armed> <budget> <ticks> <last_seen> <now>
|
|
455
|
+
local no="$1" id="$2" armed="$3" budget="$4" ticks="$5" last_seen="$6" now="$7"
|
|
456
|
+
local elapsed="$(( now - armed ))" i=0
|
|
457
|
+
printf '\n \033[31m✗\033[0m 段落 #%s(%s)超时:预算 %ss,实际 %ss,最后进度 %ss → 停跑(exit 2),现场 %s\n' \
|
|
458
|
+
"$no" "$id" "$budget" "$elapsed" "$last_seen" "$SG_SCENE"
|
|
459
|
+
: > "$SG_MARKER" 2>/dev/null || true
|
|
460
|
+
_sg_scene_write "$no" "$id" "$budget" "$elapsed" "$last_seen" "$ticks" "$armed"
|
|
461
|
+
_sg_stop_children "$no"
|
|
462
|
+
# 给套件一个**有界**的机会自己走到安全点退出(exit 2);它不走才 TERM/KILL
|
|
463
|
+
while [ "$i" -lt $(( SG_SUITE_GRACE * 5 )) ]; do
|
|
464
|
+
_sg_pid_alive "$SG_OWNER_PID" "$SG_OWNER_START" || return 0
|
|
465
|
+
sleep 0.2; i=$((i + 1))
|
|
466
|
+
done
|
|
467
|
+
kill -TERM "$SG_OWNER_PID" 2>/dev/null || true
|
|
468
|
+
i=0
|
|
469
|
+
while [ "$i" -lt 25 ]; do
|
|
470
|
+
_sg_pid_alive "$SG_OWNER_PID" "$SG_OWNER_START" || return 0
|
|
471
|
+
sleep 0.2; i=$((i + 1))
|
|
472
|
+
done
|
|
473
|
+
kill -KILL "$SG_OWNER_PID" 2>/dev/null || true
|
|
474
|
+
return 0
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
_sg_stop_children() { # <no>:只停这个套件进程树里的子孙(不含看门狗),TERM → 有界宽限 → KILL
|
|
478
|
+
local no="$1" p grace=0 _sg_p
|
|
479
|
+
while :; do
|
|
480
|
+
p="$(_sg_descendants "$SG_OWNER_PID" "$BASHPID" | tac)"
|
|
481
|
+
[ -n "$p" ] || break
|
|
482
|
+
[ "$grace" -ge "$SG_DESC_GRACE" ] && break
|
|
483
|
+
# 信号只发给 _sg_descendants 从 /proc 里**读出来并记下的 pid**(P159:xargs 的 stdin 是选择,禁止)
|
|
484
|
+
for _sg_p in $p; do kill -TERM "$_sg_p" 2>/dev/null || true; done
|
|
485
|
+
sleep 0.2; grace=$((grace + 1))
|
|
486
|
+
done
|
|
487
|
+
if [ "$grace" -ge "$SG_DESC_GRACE" ]; then
|
|
488
|
+
p="$(_sg_descendants "$SG_OWNER_PID" "$BASHPID" | tac)"
|
|
489
|
+
if [ -n "$p" ]; then
|
|
490
|
+
for _sg_p in $p; do kill -KILL "$_sg_p" 2>/dev/null || true; done
|
|
491
|
+
# 只声明确实知道的:grace 用尽 + 已发 KILL。谁真的扛过了 TERM 是**现场判定**的事
|
|
492
|
+
# (P108 F1:套件退出路径持续产出短命子进程时,「子孙忽略 TERM」是与现场矛盾的归因)。
|
|
493
|
+
printf 'escalation: 段落 #%s 的 grace 用尽(仍有子孙存活)→ 已对它们发 KILL(归因以现场为准)\n' "$no" >> "$SG_SCENE/summary.txt" 2>/dev/null || true
|
|
494
|
+
printf ' \033[31m✗\033[0m 看门狗:段落 #%s 的 grace 用尽(仍有子孙存活)→ 已发 KILL(归因以现场为准;现场 %s)\n' "$no" "$SG_SCENE"
|
|
495
|
+
fi
|
|
496
|
+
fi
|
|
497
|
+
return 0
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
_sg_recent_files() { # <root> <min_epoch> <max>:窗口内动过的文件,**按 mtime 从新到旧**(有界)
|
|
501
|
+
# 不按 find 的目录顺序:正在被写的那份(挂住段落的夹具日志)必须排在最前,否则 12 个名额会被
|
|
502
|
+
# 窗口里更早、目录顺序靠前的文件占满 —— 2026-09-28 容器实测:0f 现场收进 .git/hooks/*.sample,
|
|
503
|
+
# 挂住的夹具日志一条都没有(宿主 tmpfs 的目录顺序恰好是新文件在前,所以只在容器里现形)。
|
|
504
|
+
find "$1" -type f -newermt "@$2" -printf '%T@\t%p\n' 2>/dev/null \
|
|
505
|
+
| sort -rn | head -n "$3" | cut -f2-
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
_sg_scene_write() { # <no> <id> <budget> <elapsed> <last_seen> <ticks> <armed>
|
|
509
|
+
local no="$1" id="$2" budget="$3" elapsed="$4" last_seen="$5" ticks="$6" armed="$7"
|
|
510
|
+
mkdir -p "$SG_SCENE/logs" 2>/dev/null || return 0
|
|
511
|
+
{
|
|
512
|
+
printf 'section: #%s\nid: %s\nbudget: %ss\nelapsed: %ss\nlast progress: %ss\nticks: %s\n' \
|
|
513
|
+
"$no" "$id" "$budget" "$elapsed" "$last_seen" "$ticks"
|
|
514
|
+
printf 'watchdog pid: %s\n' "${BASHPID:-?}"
|
|
515
|
+
printf 'suite pid: %s\n' "$SG_OWNER_PID"
|
|
516
|
+
printf 'mode: FAST=%s fixture=%s\n' "${SG_FAST:-0}" "${SG_FIXTURE:-0}"
|
|
517
|
+
printf 'knobs: stuck=%s budget_override=%s progress=%s poll=%s\n' \
|
|
518
|
+
"${SG_STUCK_SECTION:-}" "${SG_BUDGET_OVERRIDE:-}" "$SG_PROGRESS_INTERVAL" "$SG_POLL"
|
|
519
|
+
printf 'lock: %s\n' "$([ -n "$SG_LOCK_NOTE" ] && printf '%s holder=%s' "$SG_LOCK_NOTE" "$(cat "$SG_LOCK_NOTE.holder" 2>/dev/null || true)" || printf '(无)')"
|
|
520
|
+
printf 'machine: nproc=%s loadavg=%s run_tmp=%s\n' \
|
|
521
|
+
"$(nproc 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || printf '?')" \
|
|
522
|
+
"$(cut -d' ' -f1-3 /proc/loadavg 2>/dev/null || printf '?')" "$SG_RUN_TMP"
|
|
523
|
+
printf -- '-- 子孙进程树(叶子优先)--\n'
|
|
524
|
+
local p; p="$(_sg_descendants "$SG_OWNER_PID" "$BASHPID" | paste -sd, - 2>/dev/null || true)"
|
|
525
|
+
if [ -n "$p" ]; then
|
|
526
|
+
ps -o pid,ppid,pgid,stat,etime,args -p "$p" 2>/dev/null || true
|
|
527
|
+
else
|
|
528
|
+
printf '(无子孙)\n'
|
|
529
|
+
fi
|
|
530
|
+
printf -- '-- 进程表快照(有界 tail)--\n'
|
|
531
|
+
ps -eo pid,ppid,pgid,stat,etime,args 2>/dev/null | tail -60
|
|
532
|
+
} > "$SG_SCENE/summary.txt" 2>&1 || true
|
|
533
|
+
{
|
|
534
|
+
printf -- '-- 本段窗口内动过的文件($TMP 下,有界 tail;按 mtime 从新到旧)--\n'
|
|
535
|
+
local n=0 f
|
|
536
|
+
while IFS= read -r f; do
|
|
537
|
+
[ -f "$f" ] || continue
|
|
538
|
+
n=$((n + 1))
|
|
539
|
+
[ "$n" -gt 12 ] && break
|
|
540
|
+
printf '== %s ==\n' "${f#$SG_RUN_TMP/}"
|
|
541
|
+
tail -c 4096 "$f" 2>/dev/null | tail -20 || true
|
|
542
|
+
done < <(_sg_recent_files "$SG_RUN_TMP" "$((armed - 1))" 12)
|
|
543
|
+
[ "$n" -eq 0 ] && printf '(本段窗口内没有 $TMP 下的新文件)\n'
|
|
544
|
+
} > "$SG_SCENE/logs/tails.txt" 2>&1 || true
|
|
545
|
+
if command -v tmux >/dev/null 2>&1; then
|
|
546
|
+
{
|
|
547
|
+
printf -- '-- 本 run 私有 tmux server 的 pane 尾屏(有界)--\n'
|
|
548
|
+
local pane
|
|
549
|
+
while IFS= read -r pane; do
|
|
550
|
+
[ -n "$pane" ] || continue
|
|
551
|
+
printf '== %s ==\n' "$pane"
|
|
552
|
+
tmux capture-pane -p -t "$pane" 2>/dev/null | tail -15 || true
|
|
553
|
+
done < <(tmux list-panes -a -F '#{session_name}:#{window_name}.#{pane_index}' 2>/dev/null | head -8)
|
|
554
|
+
} > "$SG_SCENE/panes.txt" 2>&1 || true
|
|
555
|
+
fi
|
|
556
|
+
cp "$SG_TIMING" "$SG_SCENE/sections.tsv.partial" 2>/dev/null || true
|
|
557
|
+
return 0
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
# ---------------------------------------------------------------- 有界等待助手
|
|
561
|
+
# section_guard_wait <name> <cap> <interval> <state> <reader|-> <predicate...>
|
|
562
|
+
# 每一轮刷新心跳/ticks;到顶打印一行归因(名字、n/cap、等什么、最后一次读数)并返回非 0。
|
|
563
|
+
section_guard_wait() {
|
|
564
|
+
local name="$1" cap="$2" interval="$3" state="$4" reader="$5"; shift 5
|
|
565
|
+
case "${cap:-x}" in ''|*[!0-9]*) printf 'section_guard_wait: cap 必须是正整数(收到 [%s])\n' "${cap:-}" >&2; return 2 ;; esac
|
|
566
|
+
[ "$cap" -gt 0 ] || { printf 'section_guard_wait: cap 必须是正整数(收到 %s)\n' "$cap" >&2; return 2; }
|
|
567
|
+
local round=0 reading="(无读数)"
|
|
568
|
+
while [ "$round" -lt "$cap" ]; do
|
|
569
|
+
round=$((round + 1))
|
|
570
|
+
section_guard_check
|
|
571
|
+
if [ "$reader" != "-" ] && [ -n "$reader" ] && command -v "$reader" >/dev/null 2>&1; then
|
|
572
|
+
reading="$("$reader" "$round" 2>/dev/null || true)"
|
|
573
|
+
[ -n "$reading" ] || reading="(读数命令没有输出)"
|
|
574
|
+
fi
|
|
575
|
+
if "$@"; then
|
|
576
|
+
return 0
|
|
577
|
+
fi
|
|
578
|
+
sleep "$interval"
|
|
579
|
+
done
|
|
580
|
+
printf ' \033[33m等待到顶\033[0m %s:%s/%s 轮;在等「%s」;最后一次读数:%s\n' \
|
|
581
|
+
"$name" "$round" "$cap" "$state" "$reading"
|
|
582
|
+
return 1
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
# ---------------------------------------------------------------- 自检(形状 + 现场顺序 + 红侧翻转)
|
|
586
|
+
_sg_selftest_case() { # <name> <shape> <want:0|2|!0> <want_scene:0|1>
|
|
587
|
+
local name="$1" shape="$2" want="$3" want_scene="$4"
|
|
588
|
+
local dir="$ST_ROOT/$name"; mkdir -p "$dir/run-tmp"
|
|
589
|
+
local rc=0
|
|
590
|
+
(
|
|
591
|
+
export TMPDIR="$dir"
|
|
592
|
+
section_guard_init "$dir/run-tmp" --poll 0.2 --progress 300 --suite-grace 1 --desc-grace 2 --default-budget 2
|
|
593
|
+
section_guard_arm
|
|
594
|
+
section_guard_begin "selftest-$name"
|
|
595
|
+
wait
|
|
596
|
+
printf 'BARE_WAIT_RETURNED\n'
|
|
597
|
+
case "$shape" in
|
|
598
|
+
stuck) eval 'sleep infinity' ;;
|
|
599
|
+
stubborn) bash -c 'trap "" TERM; while :; do sleep 0.2; done' ;;
|
|
600
|
+
spin) while :; do :; done ;;
|
|
601
|
+
deadspin) trap '' TERM; while :; do :; done ;;
|
|
602
|
+
clean) SG_BAND=0; SG_ARMED_EPOCH="$(( $(date +%s) - 5 ))" ;;
|
|
603
|
+
decimal) SG_BAND=0.19; SG_ARMED_EPOCH="$(( $(date +%s) - 5 ))" ;;
|
|
604
|
+
esac
|
|
605
|
+
section_guard_check
|
|
606
|
+
section_guard_finish
|
|
607
|
+
printf 'SECTION_DONE\n'
|
|
608
|
+
exit 0
|
|
609
|
+
) >"$dir/out.log" 2>&1
|
|
610
|
+
rc=$?
|
|
611
|
+
case "$want" in
|
|
612
|
+
0) [ "$rc" = "0" ] && st_ok "$name:套件 exit 0" || st_bad "$name:套件 exit $rc(期望 0)" ;;
|
|
613
|
+
2) [ "$rc" = "2" ] && st_ok "$name:套件 exit 2(trip 路径)" || st_bad "$name:套件 exit $rc(期望 2)" ;;
|
|
614
|
+
!0) [ "$rc" != "0" ] && st_ok "$name:套件 exit $rc(非零,KILL 路径)" || st_bad "$name:套件 exit 0" ;;
|
|
615
|
+
esac
|
|
616
|
+
local scene
|
|
617
|
+
scene="$(ls -d "$dir"/.teamsmith-smoke-scene.* 2>/dev/null | head -1 || true)"
|
|
618
|
+
if [ "$want_scene" = "1" ]; then
|
|
619
|
+
if [ -n "$scene" ] && [ -s "$scene/summary.txt" ]; then
|
|
620
|
+
st_ok "$name:现场存在且有摘要(${scene#$ST_ROOT/})"
|
|
621
|
+
grep -qF "selftest-$name" "$scene/summary.txt" && st_ok "$name:现场点名该段" || st_bad "$name:现场没点名该段"
|
|
622
|
+
grep -qF '超时' "$dir/out.log" && st_ok "$name:stdout 有一行点名超时" || st_bad "$name:stdout 没有点名超时"
|
|
623
|
+
grep -qF 'SECTION_DONE' "$dir/out.log" && st_bad "$name:套件居然跑完了整段" || st_ok "$name:套件没有跑完整段"
|
|
624
|
+
else
|
|
625
|
+
st_bad "$name:没有现场(期望有)"
|
|
626
|
+
fi
|
|
627
|
+
else
|
|
628
|
+
[ -z "$scene" ] && st_ok "$name:干净路径没有现场" || st_bad "$name:干净路径留下了现场"
|
|
629
|
+
fi
|
|
630
|
+
# 现场要活过 run 自己的临时根(trip 路径:run-tmp 被删,现场还在)
|
|
631
|
+
if [ "$want_scene" = "1" ] && [ -n "$scene" ]; then
|
|
632
|
+
rm -rf -- "$dir/run-tmp" 2>/dev/null || true
|
|
633
|
+
[ -d "$scene" ] && [ -s "$scene/summary.txt" ] \
|
|
634
|
+
&& st_ok "$name:现场活过了 run 临时根被删(清理带不走)" \
|
|
635
|
+
|| st_bad "$name:run 临时根删掉后现场也丢了"
|
|
636
|
+
fi
|
|
637
|
+
grep -qF 'BARE_WAIT_RETURNED' "$dir/out.log" && st_ok "$name:裸 wait 没被看门狗卡住" || st_bad "$name:裸 wait 没返回"
|
|
638
|
+
if [ "$shape" = "clean" ]; then
|
|
639
|
+
grep -qF '超过实测带' "$dir/out.log" && st_ok "$name:整数 band 超过时打一行警告(记录,不是判定)" \
|
|
640
|
+
|| st_bad "$name:超过实测带没有警告行"
|
|
641
|
+
fi
|
|
642
|
+
if [ "$shape" = "decimal" ]; then
|
|
643
|
+
grep -qF '超过实测带 0.19s' "$dir/out.log" \
|
|
644
|
+
&& st_ok "$name:小数 band(0.19)超过时也打警告(P108 F2)" \
|
|
645
|
+
|| st_bad "$name:小数 band 超过时没有警告行(规范承诺对 110/112 行不可达)"
|
|
646
|
+
grep -qF '这是记录,不是判定' "$dir/out.log" \
|
|
647
|
+
&& st_ok "$name:小数 band 的警告仍是记录、不是判定(D33)" \
|
|
648
|
+
|| st_bad "$name:小数 band 的警告丢了「记录,不是判定」"
|
|
649
|
+
fi
|
|
650
|
+
local i pidfile
|
|
651
|
+
for i in $(seq 1 25); do
|
|
652
|
+
pidfile="$(ls "$dir"/.teamsmith-smoke-guard.pid.* 2>/dev/null | head -1 || true)"
|
|
653
|
+
[ -z "$pidfile" ] && break
|
|
654
|
+
sleep 0.1
|
|
655
|
+
done
|
|
656
|
+
[ -z "$pidfile" ] && st_ok "$name:看门狗随套件退场(pidfile 已收)" || { st_bad "$name:看门狗 pidfile 还在:$pidfile"; rm -f "$pidfile"; }
|
|
657
|
+
return 0
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
# F1 夹具(P110):`--desc-grace 2` + 一个**响应 TERM**的子进程。套件的退出路径会持续产出短命
|
|
661
|
+
# 子进程(P108 的现场形状),所以 grace 用尽时仍有子孙存活 —— 但没有任何证据能说它们「忽略了
|
|
662
|
+
# TERM」。归因行只许说「grace 用尽 → 已发 KILL」;旧措辞「子孙忽略 TERM」是与现场矛盾的断言。
|
|
663
|
+
_sg_selftest_f1() {
|
|
664
|
+
local dir="$ST_ROOT/f1-escalation"
|
|
665
|
+
local scene="$dir/scene"
|
|
666
|
+
mkdir -p "$scene"
|
|
667
|
+
local out="$dir/stop.log"
|
|
668
|
+
# 假套件(= `_sg_stop_children` 看到的 $SG_OWNER_PID 树):每个后台 `sleep 1` 都是响应 TERM 的
|
|
669
|
+
# 普通子进程,新的不断出生 —— grace 用尽时总有子孙在,但没有一个 pid 能证明自己忽略了 TERM。
|
|
670
|
+
# 收尾只动我们自己 spawn 的 pid(D37:TERM 陷阱让假套件带走自己的 jobs;绝不按名字杀)。
|
|
671
|
+
bash -c '
|
|
672
|
+
trap "kill \$(jobs -p) 2>/dev/null; exit 0" TERM
|
|
673
|
+
for i in $(seq 1 400); do sleep 1 & sleep 0.05; done
|
|
674
|
+
wait
|
|
675
|
+
' 2>"$dir/suite.err" &
|
|
676
|
+
local suite=$!
|
|
677
|
+
# 等第一个孩子出生:否则 `_sg_stop_children` 的第一轮可能看到空树直接退出(夹具抖动)。
|
|
678
|
+
local i=0
|
|
679
|
+
while [ "$i" -lt 40 ]; do
|
|
680
|
+
[ -n "$(_sg_descendants "$suite" "$BASHPID")" ] && break
|
|
681
|
+
sleep 0.05; i=$((i + 1))
|
|
682
|
+
done
|
|
683
|
+
SG_OWNER_PID="$suite"
|
|
684
|
+
SG_SCENE="$scene"
|
|
685
|
+
SG_DESC_GRACE=2
|
|
686
|
+
_sg_stop_children 99 >"$out" 2>&1 || true
|
|
687
|
+
# 现场仍然要留(KILL 照发、现场照写;改的只是归因)
|
|
688
|
+
grep -qF 'escalation:' "$scene/summary.txt" 2>/dev/null \
|
|
689
|
+
&& st_ok "F1 夹具:grace 用尽时现场记录了 KILL 升级" \
|
|
690
|
+
|| st_bad "F1 夹具:现场没有记录 escalation(KILL 现场不许被删)"
|
|
691
|
+
grep -qF '已发 KILL' "$out" \
|
|
692
|
+
&& st_ok "F1 夹具:stdout 有一行 KILL 升级(只改归因,不删行)" \
|
|
693
|
+
|| st_bad "F1 夹具:stdout 的 KILL 升级行不见了"
|
|
694
|
+
# 归因诚实:两个现场都不得出现旧措辞的因果断言(是否忽略 TERM 未验证)
|
|
695
|
+
if grep -qF '忽略 TERM' "$out" "$scene/summary.txt" 2>/dev/null; then
|
|
696
|
+
st_bad "F1 夹具:归因仍声称「子孙忽略 TERM」(现场里没有这个证据)"
|
|
697
|
+
else
|
|
698
|
+
st_ok "F1 夹具:归因只说「grace 用尽」,没有「忽略 TERM」的断言"
|
|
699
|
+
fi
|
|
700
|
+
grep -qF 'grace 用尽' "$out" \
|
|
701
|
+
&& st_ok "F1 夹具:归因行点名真正的原因(grace 用尽)" \
|
|
702
|
+
|| st_bad "F1 夹具:归因行没有说明是 grace 用尽"
|
|
703
|
+
# 收尾:只对我们 spawn 的假套件发信号;它的 TERM 陷阱带走自己的 jobs
|
|
704
|
+
kill -TERM "$suite" 2>/dev/null || true
|
|
705
|
+
local j
|
|
706
|
+
for j in $(seq 1 25); do
|
|
707
|
+
kill -0 "$suite" 2>/dev/null || break
|
|
708
|
+
sleep 0.05
|
|
709
|
+
done
|
|
710
|
+
kill -KILL "$suite" 2>/dev/null || true
|
|
711
|
+
wait "$suite" 2>/dev/null || true
|
|
712
|
+
return 0
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
# 红侧翻转:把实现变异回旧形状(f2 = 整数守卫;f1 = 「子孙忽略 TERM」措辞)。
|
|
716
|
+
# 这是「破坏实现 → 守卫测试必须红」的前半:变异后的副本由 _sg_selftest_flips 跑。
|
|
717
|
+
_sg_selftest_mutate() { # <src> <dst> <f2|f1> → 0 = 变异落地
|
|
718
|
+
local src="$1" dst="$2" kind="$3"
|
|
719
|
+
case "$kind" in
|
|
720
|
+
f2) awk '
|
|
721
|
+
/^_sg_band_over\(\) \{/ { inband = 1 }
|
|
722
|
+
inband && /^ case "\$band" in/ {
|
|
723
|
+
print " case \"$band\" in # P110_F2 红侧:旧整数守卫(小数落空分支)"
|
|
724
|
+
print " \047\047|*[!0-9]*) return 1 ;;"
|
|
725
|
+
print " esac"
|
|
726
|
+
skipping = 1; inband = 0; next
|
|
727
|
+
}
|
|
728
|
+
skipping { if ($0 == " esac") skipping = 0; next }
|
|
729
|
+
{ print }
|
|
730
|
+
' "$src" > "$dst" ;;
|
|
731
|
+
f1) sed 's/的 grace 用尽(仍有子孙存活)→ 已/的子孙忽略 TERM → 已/g' "$src" > "$dst" ;;
|
|
732
|
+
*) return 1 ;;
|
|
733
|
+
esac
|
|
734
|
+
# 变异必须真的改变文件(拿标记 grep 会被变异代码自身的存在骗过),且副本仍然可解析。
|
|
735
|
+
[ -s "$dst" ] || { rm -f "$dst"; return 1; }
|
|
736
|
+
cmp -s "$src" "$dst" && { rm -f "$dst"; return 1; }
|
|
737
|
+
bash -n "$dst" 2>/dev/null || { rm -f "$dst"; return 1; }
|
|
738
|
+
return 0
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
# 两个红侧翻转(只在完整自检里跑;`--self-test <形状>` 只跑形状,避免递归):
|
|
742
|
+
# F2:小数值 band 的警告 —— 变异回整数守卫 → decimal 夹具必须红;
|
|
743
|
+
# F1:grace 用尽的归因 —— 变异回「子孙忽略 TERM」→ f1 夹具必须红。
|
|
744
|
+
# 变异副本跑在自己的私有 TMPDIR 下,跑完连根删掉(失败也不留现场)。
|
|
745
|
+
_sg_selftest_flips() {
|
|
746
|
+
local dir="$ST_ROOT/flips" self mut rc out
|
|
747
|
+
self="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
|
|
748
|
+
mkdir -p "$dir/f2-tmp" "$dir/f1-tmp"
|
|
749
|
+
# --- F2 ---
|
|
750
|
+
mut="$dir/f2-legacy-guard.sh"; out="$dir/f2-mutated.log"
|
|
751
|
+
if _sg_selftest_mutate "$self" "$mut" f2; then
|
|
752
|
+
TMPDIR="$dir/f2-tmp" bash "$mut" --self-test decimal >"$out" 2>&1; rc=$?
|
|
753
|
+
[ "$rc" -ne 0 ] && st_ok "F2 红侧:还原成整数守卫 → decimal 夹具红(rc=$rc)" \
|
|
754
|
+
|| st_bad "F2 红侧:还原成整数守卫后 decimal 夹具仍绿(守卫没被证到)"
|
|
755
|
+
grep -qF '超过实测带' "$out" && st_bad "F2 红侧:整数守卫下居然打了「超过实测带」行" \
|
|
756
|
+
|| st_ok "F2 红侧:整数守卫下没有「超过实测带」警告行(规范承诺回到不可达)"
|
|
757
|
+
grep -qF '小数 band 超过时没有警告行' "$out" \
|
|
758
|
+
&& st_ok "F2 红侧:红的是那条夹具断言本身(不是别的原因)" \
|
|
759
|
+
|| st_bad "F2 红侧:红的原因不是「小数 band 没有警告行」"
|
|
760
|
+
else
|
|
761
|
+
st_bad "F2 红侧:变异没落到 _sg_band_over(夹具失效)"
|
|
762
|
+
fi
|
|
763
|
+
# --- F1 ---
|
|
764
|
+
mut="$dir/f1-legacy-wording.sh"; out="$dir/f1-mutated.log"
|
|
765
|
+
if _sg_selftest_mutate "$self" "$mut" f1; then
|
|
766
|
+
TMPDIR="$dir/f1-tmp" bash "$mut" --self-test f1 >"$out" 2>&1; rc=$?
|
|
767
|
+
[ "$rc" -ne 0 ] && st_ok "F1 红侧:还原成「子孙忽略 TERM」措辞 → f1 夹具红(rc=$rc)" \
|
|
768
|
+
|| st_bad "F1 红侧:还原措辞后 f1 夹具仍绿(归因守卫没被证到)"
|
|
769
|
+
grep -qF '归因仍声称' "$out" \
|
|
770
|
+
&& st_ok "F1 红侧:红的是那条夹具断言本身(不是别的原因)" \
|
|
771
|
+
|| st_bad "F1 红侧:红的原因不是「归因仍声称忽略 TERM」"
|
|
772
|
+
else
|
|
773
|
+
st_bad "F1 红侧:变异没落到归因措辞(夹具失效)"
|
|
774
|
+
fi
|
|
775
|
+
rm -rf -- "$dir" 2>/dev/null || true
|
|
776
|
+
return 0
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
_sg_selftest() {
|
|
780
|
+
local arg shape shapes want_all=1
|
|
781
|
+
local only=()
|
|
782
|
+
for arg in "$@"; do
|
|
783
|
+
case "$arg" in
|
|
784
|
+
--help|-h) printf '用法:bash %s --self-test [形状…]\n 形状:clean stuck stubborn spin deadspin decimal f1(不带形状 = 全部 + 红侧翻转)\n' "${BASH_SOURCE[0]}"; return 0 ;;
|
|
785
|
+
-*) printf 'section-guard: 自检不认识的参数 %s\n' "$arg" >&2; return 2 ;;
|
|
786
|
+
*) only+=("$arg"); want_all=0 ;;
|
|
787
|
+
esac
|
|
788
|
+
done
|
|
789
|
+
local PASS=0 FAIL=0
|
|
790
|
+
st_ok() { printf ' \033[32m✓\033[0m %s\n' "$1"; PASS=$((PASS + 1)); }
|
|
791
|
+
st_bad() { printf ' \033[31m✗\033[0m %s\n' "$1"; FAIL=$((FAIL + 1)); }
|
|
792
|
+
local ST_ROOT
|
|
793
|
+
ST_ROOT="$(mktemp -d "${TMPDIR:-/tmp}/teamsmith-sg-selftest.XXXXXX" 2>/dev/null)" || {
|
|
794
|
+
printf 'section-guard 自检:建不出临时根\n' >&2; return 3; }
|
|
795
|
+
local pgid_before pgid_after
|
|
796
|
+
pgid_before="$(ps -o pgid= -p "$$" 2>/dev/null | tr -d ' ')"
|
|
797
|
+
printf '\n\033[1m== section-guard.sh 自检(形状 + 现场顺序 + 进程组 + 红侧翻转)==\033[0m\n'
|
|
798
|
+
SG_FIXTURE=1
|
|
799
|
+
TEAM_SMOKE_FIXTURE=1
|
|
800
|
+
if [ "$want_all" = "1" ]; then
|
|
801
|
+
shapes="clean stuck stubborn spin deadspin decimal"
|
|
802
|
+
else
|
|
803
|
+
shapes="${only[*]}"
|
|
804
|
+
fi
|
|
805
|
+
for shape in $shapes; do
|
|
806
|
+
case "$shape" in
|
|
807
|
+
clean) _sg_selftest_case clean clean 0 0 ;;
|
|
808
|
+
stuck) _sg_selftest_case stuck stuck 2 1 ;;
|
|
809
|
+
stubborn) _sg_selftest_case stubborn stubborn 2 1 ;;
|
|
810
|
+
spin) _sg_selftest_case spin spin 2 1 ;;
|
|
811
|
+
deadspin) _sg_selftest_case deadspin deadspin '!0' 1 ;;
|
|
812
|
+
decimal) _sg_selftest_case decimal decimal 0 0 ;;
|
|
813
|
+
f1) _sg_selftest_f1 ;;
|
|
814
|
+
*) st_bad "自检:不认识的形状「$shape」(可用:clean stuck stubborn spin deadspin decimal f1)"; break ;;
|
|
815
|
+
esac
|
|
816
|
+
done
|
|
817
|
+
# 现场尾巴的收集顺序 = mtime 降序,不是 find 的目录顺序(容器实测:目录顺序把挂住的夹具日志
|
|
818
|
+
# 挤出了 12 个名额)。夹具反向:先建 live 文件,再建 15 个「目录顺序更靠前但 mtime 更旧」的。
|
|
819
|
+
local cd="$ST_ROOT/recent"; mkdir -p "$cd"
|
|
820
|
+
: > "$cd/live.txt"
|
|
821
|
+
local i
|
|
822
|
+
for i in $(seq 1 15); do : > "$cd/old-$i.txt"; done
|
|
823
|
+
touch -d '2020-01-01 00:00:00' "$cd"/old-*.txt
|
|
824
|
+
local recent_first recent_n
|
|
825
|
+
recent_first="$(_sg_recent_files "$cd" 0 12 | head -1)"
|
|
826
|
+
recent_n="$(_sg_recent_files "$cd" 0 12 | wc -l | tr -d ' ')"
|
|
827
|
+
[ "$(basename "$recent_first")" = "live.txt" ] \
|
|
828
|
+
&& st_ok "现场尾巴:按 mtime 从新到旧(正在被写的那份在最前)" \
|
|
829
|
+
|| st_bad "现场尾巴:最新被写的文件不在最前(第一行 ${recent_first:-空})"
|
|
830
|
+
[ "$recent_n" = "12" ] && st_ok "现场尾巴:名额有界(16 个候选只收 12 个)" \
|
|
831
|
+
|| st_bad "现场尾巴:条数 $recent_n ≠ 12"
|
|
832
|
+
pgid_after="$(ps -o pgid= -p "$$" 2>/dev/null | tr -d ' ')"
|
|
833
|
+
[ "$pgid_before" = "$pgid_after" ] && st_ok "调用者的进程组没变(没有进程组信号)" \
|
|
834
|
+
|| st_bad "调用者的进程组变了($pgid_before → $pgid_after)"
|
|
835
|
+
if [ "$want_all" = "1" ]; then
|
|
836
|
+
_sg_selftest_f1
|
|
837
|
+
_sg_selftest_flips
|
|
838
|
+
fi
|
|
839
|
+
printf '\n\033[1m== 结果 ==\033[0m ✓ %d ✗ %d\n' "$PASS" "$FAIL"
|
|
840
|
+
if [ "$FAIL" -eq 0 ]; then
|
|
841
|
+
rm -rf -- "$ST_ROOT" 2>/dev/null || true
|
|
842
|
+
printf '\033[32msection-guard 自检全绿\033[0m\n'
|
|
843
|
+
return 0
|
|
844
|
+
fi
|
|
845
|
+
printf '\033[31msection-guard 自检有失败项(现场保留 %s)\033[0m\n' "$ST_ROOT"
|
|
846
|
+
return 1
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
|
|
850
|
+
case "${1:-}" in
|
|
851
|
+
--self-test) shift; _sg_selftest "$@" ;;
|
|
852
|
+
--help|-h|"") printf '用法:bash %s --self-test(被 smoke.sh source 用)\n' "${BASH_SOURCE[0]}" ;;
|
|
853
|
+
*) printf 'section-guard.sh:只支持 --self-test(被 smoke.sh source 用)\n' >&2; exit 2 ;;
|
|
854
|
+
esac
|
|
855
|
+
fi
|