Repository navigation
chore(ci): mutation-gate 追平上游 v2(本地增量档 / CI 全量档) #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| python |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "anchor": "07d00c124", | ||
| "files": { | ||
| ".lefthook/mutation-gate.sh": "5c1ac799a5a9a47acf7006f36f7f03c0ab257f77c9e69dbc704669d8fcc650bf", | ||
| "stryker.config.json": "73fdf767c577561a16efcfa505ca73edaab2e72d31201c5c7a2733ebcbd133ac" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,271 @@ | ||
| #!/usr/bin/env bash | ||
| # 变异测试红线(awesome-rules tools/git 分发,由 lefthook pre-push 调用) | ||
| # 用法: bash .lefthook/mutation-gate.sh # push 集含 src/main/java 生产代码时触发 | ||
| # 红线: PIT 变异分 ≥ pom <mutationThreshold>(各仓/模块自定;未配置阈值时 PIT 只报告不拦截) | ||
| # 语义: 只约束生产代码变更——纯测试/文档/脚本变更跳过(变异分门禁防"生产行为改了而测试没跟上") | ||
| # 无 pitest 配置的模块提示后跳过(接入: pom 加 pitest-maven + mutationThreshold) | ||
| # 变异测试红线(awesome-factory tools/git/gates 分发,由 lefthook pre-push 调用) | ||
| # 用法: bash .lefthook/mutation-gate.sh [--ci] | ||
| # 无参 本地增量档(lefthook pre-push 调用): java 只变异本次变更类 + 其嵌套类, | ||
| # 样本过小(< MUTATION_MIN_MUTANTS,默认 30 个变异体)时只报告不拦截 | ||
| # --ci CI 全量档: 遍历全部含 pitest-maven 的 pom,按 pom 的 targetClasses 全量变异, | ||
| # 不加 -DtargetClasses —— 吸收本地子集档的欠账(CI 无 git 推送基线也无妨) | ||
| # 两档靠调用方显式声明区分(本地 lefthook / CI 流水线),不做 CI 环境变量嗅探: | ||
| # 分发到多仓后 `CI=` 是否被导出不可控,档位必须可审计 | ||
| # 语言分派: 读仓根 .lefthook-profile 声明(gates-install.sh 安装时探测生成,人工可覆写; | ||
| # 多语言逗号分隔,含 java/ts 各走各分支,互不干扰) | ||
| # java → PIT 红线(awesome-factory 迁入; 模块判定按变更文件全路径向上最近 pom.xml—— | ||
| # 修复嵌套 Maven 模块漏检。质量档位见上方两档说明) | ||
| # ts → StrykerJS 快速档: diff 驱动只变异本次变更文件、--incremental 增量、 | ||
| # 预算封顶(默认 180s,MUTATION_GATE_BUDGET 可调, 须正整数秒); | ||
| # 仅"看门狗确证"的超时放行转提示(不绑架 push),外部终止/OOM 同状态码不放行 | ||
| # 红线: java PIT 变异分 ≥ pom <mutationThreshold>; ts 变异分 ≥ stryker.config.json | ||
| # thresholds.break(模板默认 50,仓可调) | ||
| # 语义: 只约束生产代码变更——纯测试/文档/脚本变更跳过;无 stryker 配置或无 | ||
| # vitest/jest 测试基线的仓提示后跳过(不绑架无基线仓) | ||
| # 命名消歧: 本件是 StrykerJS/PIT 源码变异测试门禁; .factory/mutations/ 是工厂验门 | ||
| # 灵敏度的缺陷注入体系——同名异物(见 gates.json description) | ||
| set -u | ||
|
|
||
| REPO=$(git rev-parse --show-toplevel) | ||
| cd "$REPO" | ||
|
|
||
| # 增量基线: @{push}(上次推送点)...HEAD,回退 @{u}(上游);均不可解析=首次推送,跳过 | ||
| # (与 coverage.sh full 模式同口径:度量"本次推送",不回退主干避免长命分支欠账失真) | ||
| COMPARE="" | ||
| for b in '@{push}' '@{u}'; do | ||
| git rev-parse --verify -q "$b" >/dev/null 2>&1 && { COMPARE="$b"; break; } | ||
| done | ||
| [ -z "$COMPARE" ] && { echo "○ [mutation] 无推送基线(首次推送), 跳过"; exit 0; } | ||
| changed=$(git diff --name-only "$COMPARE"...HEAD 2>/dev/null) | ||
| # 非阻断告警: 删除测试文件会削弱断言强度(变异分/覆盖率红线),本门禁不拦纯测试变更, | ||
| # 弱化在下次生产代码变更时由 mutationThreshold 兜底吸收 —— 删除意图请走 MR/评审自行把关 | ||
| deleted_tests=$(git diff --name-only --diff-filter=D "$COMPARE"...HEAD 2>/dev/null | grep -E 'src/test/.*\.(java|kt)$' || true) | ||
| if [ -n "$deleted_tests" ]; then | ||
| echo "⚠ [mutation] 本次推送删除了测试文件(不拦截; 变异分由下次生产变更时阈值兜底):" | ||
| printf ' - %s\n' $deleted_tests | ||
| # cd 失败即终止(fail-closed):后续 diff/文件收集全按相对路径,静默继续会整门假绿 | ||
| cd "$REPO" || { echo "✗ [mutation] 无法进入仓库根目录 ($REPO), 终止"; exit 1; } | ||
|
|
||
| # 档位: 无参=本地增量档 / --ci=CI 全量档。 | ||
| # 旧装仓的 lefthook.yml 仍会传入 {push_files}(文件清单,非 -- 开头): 兼容忽略并提示, | ||
| # 不改硬失败——否则 21 个已装仓升级脚本后每次 push 都被自己的门禁拦下。 | ||
| # 以 -- 开头的未知参数(如 --c1 手误)仍 fail-closed,不给"以为跑了全量档" | ||
| CI_MODE=0 | ||
| case "${1:-}" in | ||
| "") ;; | ||
| --ci) CI_MODE=1 ;; | ||
| --*) echo "✗ [mutation] 未知参数 '$1' (用法: mutation-gate.sh [--ci])"; exit 1 ;; | ||
| *) echo "⚠ [mutation] 已忽略参数 '$1' (lefthook {push_files} 旧装兼容; 变更集由 git diff 自算)" ;; | ||
| esac | ||
|
|
||
| # 子集样本下限: 本地档只变异变更类时样本可能仅个位数,72% 阈值在小样本上抖动大 | ||
| # (如 3/4 击杀=75% 过、2/3=67% 不过,同一质量水平),故低于下限只报告不拦截。 | ||
| # CI 全量档不受此限:全模块样本足够,按 pom 阈值硬拦。 | ||
| MIN_MUT=${MUTATION_MIN_MUTANTS:-30} | ||
| printf '%s' "$MIN_MUT" | grep -qE '^[1-9][0-9]*$' \ | ||
| || { echo "✗ [mutation] MUTATION_MIN_MUTANTS 非法(须为正整数): '$MIN_MUT'"; exit 1; } | ||
|
|
||
| if [ "$CI_MODE" = 1 ]; then | ||
| # CI 无推送基线(fresh checkout/浅克隆皆可能),且语义上不按变更集收敛 | ||
| changed="" | ||
| COMPARE="" | ||
| else | ||
| # 增量基线: @{push}(上次推送点)...HEAD,回退 @{u}(上游);均不可解析=首次推送,跳过 | ||
| # (与 coverage.sh full 模式同口径:度量"本次推送",不回退主干避免长命分支欠账失真) | ||
| COMPARE="" | ||
| for b in '@{push}' '@{u}'; do | ||
| git rev-parse --verify -q "$b" >/dev/null 2>&1 && { COMPARE="$b"; break; } | ||
| done | ||
| [ -z "$COMPARE" ] && { echo "○ [mutation] 无推送基线(首次推送), 跳过"; exit 0; } | ||
| # fail-closed: 范围算不出时 changed 会静默为空 → 语言分支误报"无生产变更"放行 | ||
| if ! changed=$(git diff --name-only "$COMPARE"...HEAD 2>&1); then | ||
| echo "✗ [mutation] 无法计算变更范围 ($COMPARE...HEAD): $changed" | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| # 触发条件: 生产 java 代码变更(src/main/java);纯测试/文档/配置变更直接跳过 | ||
| prod=$(printf '%s\n' $changed | grep -E 'src/main/java/.*\.java$' || true) | ||
| [ -z "$prod" ] && { echo "○ [mutation] 无 src/main/java 生产代码变更, 跳过"; exit 0; } | ||
| # 语言声明: 缺席即跳过(运行时只读声明,判定收口在安装时) | ||
| PROFILE="" | ||
| [ -f .lefthook-profile ] && PROFILE=$(head -1 .lefthook-profile | tr -d '[:space:]') | ||
| if [ -z "$PROFILE" ]; then | ||
| echo "○ [mutation] 无 .lefthook-profile 语言声明, 跳过 (接入: bash <awesome-factory>/tools/git/gates/gates-install.sh --repo .)" | ||
| exit 0 | ||
| fi | ||
|
|
||
| # 候选模块: 变更文件一级目录;根结构(src/main/java 直接在仓库根)映射为 "." | ||
| mods=$(printf '%s\n' $prod | awk -F/ '{print ($1=="src") ? "." : $1}' | sort -u) | ||
| want_java=0; want_ts=0 | ||
| case ",$PROFILE," in *,java,*) want_java=1 ;; esac | ||
| case ",$PROFILE," in *,ts,*) want_ts=1 ;; esac | ||
| if [ "$want_java" = 0 ] && [ "$want_ts" = 0 ]; then | ||
| echo "○ [mutation] profile($PROFILE) 不含 java/ts, 跳过" | ||
| exit 0 | ||
| fi | ||
|
|
||
| fail=0 | ||
| for m in $mods; do | ||
| pom="pom.xml"; [ "$m" != "." ] && pom="$m/pom.xml" | ||
| if [ ! -f "$pom" ] || ! grep -q 'pitest-maven' "$pom"; then | ||
| echo "○ [mutation] $m 未配置 pitest, 跳过 (接入: pom 加 pitest-maven + mutationThreshold)" | ||
| continue | ||
|
|
||
| # 递归 SIGTERM 进程树(先子后父): 看门狗超时收割 stryker 全树——防残留子进程与 | ||
| # 后续运行竞争共享增量/临时文件(macOS/Linux 均自带 pgrep) | ||
| kill_tree() { | ||
| local c | ||
| # shellcheck disable=SC2046 # pgrep 输出为纯 pid 清单, 按词展开是意图 | ||
| for c in $(pgrep -P "$1" 2>/dev/null); do kill_tree "$c"; done | ||
| kill "$1" 2>/dev/null | ||
| } | ||
|
|
||
| # ── Java 分支: PIT 红线(本地子集档 / CI 全量档) ───────────────────────────── | ||
| if [ "$want_java" = 1 ]; then | ||
| if [ "$CI_MODE" = 1 ]; then | ||
| echo "▶ [mutation] CI 全量档: 全量 PIT(pom targetClasses + mutationThreshold)" | ||
| # 全仓含 pitest 的模块(不依赖变更集: CI 无推送基线,且语义上就是吸收子集档欠账) | ||
| mods=$(find . -name pom.xml -not -path '*/target/*' -not -path '*/node_modules/*' 2>/dev/null \ | ||
| | while IFS= read -r p; do | ||
| [ -f "$p" ] || continue | ||
| # 去掉 find 前缀 "./", 与本地档的模块名口径一致(-pl x 而非 -pl ./x) | ||
| grep -q 'pitest-maven' "$p" 2>/dev/null && { d=$(dirname "$p"); printf '%s\n' "${d#./}"; } | ||
| done | sort -u) | ||
| [ -n "$mods" ] || echo "○ [mutation] CI 全量档: 无配置 pitest-maven 的模块, 跳过" | ||
| else | ||
| # 非阻断告警: 删除测试文件会削弱断言强度,本门禁不拦纯测试变更, | ||
| # 弱化在下次生产代码变更时由 mutationThreshold 兜底吸收 —— 删除意图请走 MR/评审自行把关 | ||
| deleted_tests=$(git diff --name-only --diff-filter=D "$COMPARE"...HEAD 2>/dev/null | grep -E 'src/test/.*\.(java|kt)$' || true) | ||
| if [ -n "$deleted_tests" ]; then | ||
| echo "⚠ [mutation] 本次推送删除了测试文件(不拦截; 变异分由下次生产变更时阈值兜底):" | ||
| while IFS= read -r f; do printf ' - %s\n' "$f"; done <<<"$deleted_tests" | ||
| fi | ||
|
|
||
| # 触发条件: 生产 java 代码变更(src/main/java);纯测试/文档/配置变更直接跳过 | ||
| # 清单全程 quoted 展开 + while read 按行处理——路径含空白不被拆词 | ||
| prod=$(printf '%s\n' "$changed" | grep -E 'src/main/java/.*\.java$' || true) | ||
| mods="" | ||
| if [ -n "$prod" ]; then | ||
| # 候选模块: 自变更文件全路径向上找最近 pom.xml(嵌套 Maven 模块命中子模块而非 | ||
| # 一级目录误判); 根结构(src/main/java 直接在仓库根)或全程无 pom → "." | ||
| while IFS= read -r f; do | ||
| [ -n "$f" ] || continue | ||
| dir=$(dirname "$f"); m="" | ||
| while :; do | ||
| [ -f "$dir/pom.xml" ] && { m="$dir"; break; } | ||
| [ "$dir" = "." ] && break | ||
| dir=$(dirname "$dir") | ||
| done | ||
| mods="${mods:+$mods$'\n'}${m:-.}" | ||
| done <<<"$prod" | ||
| mods=$(printf '%s\n' "$mods" | sort -u) | ||
| else | ||
| echo "○ [mutation] 无 src/main/java 生产代码变更, 跳过" | ||
| fi | ||
| fi | ||
| PL=(); [ "$m" != "." ] && PL=(-pl "$m") | ||
| echo "▶ [mutation] $m: PIT mutationCoverage (阈值见 pom mutationThreshold)" | ||
| # 单模块跑 PIT 时 minion 从本地仓库取兄弟模块 jar —— 先静默 install 依赖(跳测试)消除 classpath 漂移 | ||
| mvn -B -q ${PL[@]+"${PL[@]}"} -am install -DskipTests >/dev/null 2>&1 || true | ||
| if mvn -B ${PL[@]+"${PL[@]}"} org.pitest:pitest-maven:mutationCoverage; then | ||
| echo "✓ [mutation] $m 变异分达标" | ||
|
|
||
| while IFS= read -r m; do | ||
| [ -n "$m" ] || continue | ||
| pom="pom.xml"; [ "$m" != "." ] && pom="$m/pom.xml" | ||
| if [ ! -f "$pom" ] || ! grep -q 'pitest-maven' "$pom"; then | ||
| echo "○ [mutation] $m 未配置 pitest, 跳过 (接入: pom 加 pitest-maven + mutationThreshold)" | ||
| continue | ||
| fi | ||
| PL=(); [ "$m" != "." ] && PL=(-pl "$m") | ||
| scope=() | ||
| if [ "$CI_MODE" = 0 ]; then | ||
| # 变更类 → PIT targetClasses: 精确类名 + 嵌套类(Foo$*;lambda/内部类编进同一 class 文件) | ||
| # 不传 -DtargetTests: PIT 只跑覆盖这些类的测试,无需手填 | ||
| cls="" | ||
| while IFS= read -r f; do | ||
| [ -n "$f" ] || continue | ||
| case "$f" in | ||
| */src/main/java/*) fm=${f%%/src/main/java/*} ;; | ||
| src/main/java/*) fm=. ;; | ||
| *) continue ;; | ||
| esac | ||
| [ "$fm" = "$m" ] || continue | ||
| [ -f "$f" ] || continue # 本次已删除的文件无变异目标 | ||
| c=$(printf '%s' "$f" | sed -e 's#^.*src/main/java/##' -e 's#\.java$##' -e 's#/#.#g') | ||
| cls="${cls:+$cls,}$c,$c\$*" | ||
| done <<<"$prod" | ||
| if [ -z "$cls" ]; then | ||
| echo "○ [mutation] $m: 变更文件无可变异类(本次仅删除), 跳过" | ||
| continue | ||
| fi | ||
| ncls=$(printf '%s' "$cls" | tr ',' '\n' | grep -vc '\$') | ||
| scope=(-DtargetClasses="$cls" -DfailWhenNoMutations=false) | ||
| echo "▶ [mutation] $m: PIT mutationCoverage 子集档 ($ncls 个变更类; 阈值见 pom mutationThreshold)" | ||
| else | ||
| echo "▶ [mutation] $m: PIT mutationCoverage 全量档 (阈值见 pom mutationThreshold)" | ||
| fi | ||
| # 单模块跑 PIT 时 minion 从本地仓库取兄弟模块 jar —— 先静默 clean install 依赖(跳测试): | ||
| # clean 防增量编译陈旧 class(2026-09-06 实证: 残留产物致 lambda$new$0 假红), 变异体须基于当前源码 | ||
| mvn -B -q ${PL[@]+"${PL[@]}"} -am clean install -DskipTests >/dev/null 2>&1 || true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 中等 · 并发运行会破坏变异测试状态 当配置启用了 Java 或 TypeScript,并且同一检出目录中同时有两次推送或门禁调用运行时,两个门禁进程会操作同一检出目录:一个 请对每个检出目录中的门禁调用进行串行化,或隔离每次调用的构建产物、报告和增量缓存。 另见 面向 AI 代理的提示在 `.lefthook/mutation-gate.sh` 第 175 行:
**并发运行会破坏变异测试状态**
当配置启用了 Java 或 TypeScript,并且同一检出目录中同时有两次推送或门禁调用运行时,两个门禁进程会操作同一检出目录:一个 `mvn clean install` 会在另一个进程运行 PIT 时删除或替换构建产物,而并发的 Stryker 调用会共享增量状态。它们的结果可能基于混合输入,或者一次运行可能干扰另一次运行,因此推送可能被阻止,也可能基于不可靠的结果通过。
请对每个检出目录中的门禁调用进行串行化,或隔离每次调用的构建产物、报告和增量缓存。
另见 `.lefthook/mutation-gate.sh:246`。Original comment in English🟡 Medium · Concurrent runs corrupt mutation state When the profile enables Java or TypeScript and two pushes or gate invocations run at once in the same checkout, both gate processes operate on the same checkout: one Serialize gate invocations per checkout, or isolate each invocation’s build outputs, reports, and incremental cache. Also at Prompt for AI agentsIn `.lefthook/mutation-gate.sh` at line 175:
**Concurrent runs corrupt mutation state**
When the profile enables Java or TypeScript and two pushes or gate invocations run at once in the same checkout, both gate processes operate on the same checkout: one `mvn clean install` removes or replaces build outputs while the other runs PIT, and concurrent Stryker invocations share the incremental state. Their results can be based on mixed inputs or one run can disrupt the other, so a push can be blocked or pass on an unreliable result.
Serialize gate invocations per checkout, or isolate each invocation’s build outputs, reports, and incremental cache.
Also at `.lefthook/mutation-gate.sh:246`. |
||
| # 旧报告先删: 用"报告是否本轮生成"判定样本数, 不依赖 mtime——macOS bash 3.2 的 | ||
| # -nt 只到秒, 同秒内写完的报告会被判成陈旧; 报告缺席一律按失败处理(fail-closed) | ||
| xml="target/pit-reports/mutations.xml"; [ "$m" != "." ] && xml="$m/target/pit-reports/mutations.xml" | ||
| rm -f "$xml" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 中等 · 过期的 PIT 报告导致门禁通过 如果在本次 PIT 命令失败之前,报告无法删除,或另一次门禁调用写入了该报告,那么 如果报告删除失败,请立即报错退出;并确保计入的报告由本次调用生成,例如使用独立的报告目录,或串行运行 PIT。 另见 面向 AI 代理的提示在 `.lefthook/mutation-gate.sh` 第 179 行:
**过期的 PIT 报告导致门禁通过**
如果在本次 PIT 命令失败之前,报告无法删除,或另一次门禁调用写入了该报告,那么 `rm -f` 可能会失败却不会停止脚本;并发运行的 PIT 也可能写入共享的报告路径。如果本次 Maven 命令随后失败,脚本可能会将旧报告或其他运行生成的小型报告计作本次运行的样本,并据此豁免失败,从而放行推送。
如果报告删除失败,请立即报错退出;并确保计入的报告由本次调用生成,例如使用独立的报告目录,或串行运行 PIT。
另见 `.lefthook/mutation-gate.sh:189`。Original comment in English🟡 Medium · Stale PIT report passes gate When the report cannot be removed, or another gate invocation writes it, before this run’s PIT command fails, Fail if report removal fails, and ensure the report being counted was generated by this invocation, for example by using an isolated report directory or serializing PIT runs. Also at Prompt for AI agentsIn `.lefthook/mutation-gate.sh` at line 179:
**Stale PIT report passes gate**
When the report cannot be removed, or another gate invocation writes it, before this run’s PIT command fails, `rm -f` can fail without stopping the script, and a concurrent PIT run can also write to the shared report path. If this run’s Maven command then fails, the script can count an old or other run’s small report as this run’s sample and waive the failure, allowing the push.
Fail if report removal fails, and ensure the report being counted was generated by this invocation, for example by using an isolated report directory or serializing PIT runs.
Also at `.lefthook/mutation-gate.sh:189`. |
||
| if mvn -B ${PL[@]+"${PL[@]}"} org.pitest:pitest-maven:mutationCoverage ${scope[@]+"${scope[@]}"}; then | ||
| echo "✓ [mutation] $m 变异分达标" | ||
| else | ||
| # 子集档的小样本豁免: 只认本轮生成的报告(读不到=执行失败, 不是"样本太小") | ||
| total=0 | ||
| if [ -f "$xml" ]; then | ||
| total=$(grep -c '<mutation ' "$xml" 2>/dev/null || true) | ||
| [ -n "$total" ] || total=0 | ||
| fi | ||
| if [ "$CI_MODE" = 0 ] && [ "$total" -gt 0 ] && [ "$total" -lt "$MIN_MUT" ]; then | ||
| echo "○ [mutation] $m: 子集样本仅 $total 个变异体 (< MUTATION_MIN_MUTANTS=$MIN_MUT), 只报告不拦截" | ||
| else | ||
| echo "✗ [mutation] $m 变异分低于阈值或执行失败 (报告: target/pit-reports)" | ||
| fail=1 | ||
| fi | ||
| fi | ||
| done <<<"$mods" | ||
| fi | ||
|
|
||
| # ── TS 分支: StrykerJS 快速档(diff 驱动 + 预算封顶) ─────────────────────── | ||
| if [ "$want_ts" = 1 ]; then | ||
| if [ "$CI_MODE" = 1 ]; then | ||
| # 显式说明而非静默跳过: CI 档当前只交付 java 全量,ts 全量(--mutate 全量)尚未接 | ||
| echo "○ [mutation] ts: CI 全量档暂只覆盖 java, ts 仍走本地增量档, 本次跳过" | ||
| elif [ ! -f stryker.config.json ]; then | ||
| echo "○ [mutation] ts: 无 stryker.config.json, 跳过 (接入: gates-install.sh 落模板 + devDependencies 装 @stryker-mutator/core 与 vitest-runner)" | ||
| elif ! grep -qE '"(vitest|jest)"' package.json 2>/dev/null; then | ||
| echo "○ [mutation] ts: 无 vitest/jest 测试基线, 跳过 (先建测试基线, 门禁自动生效)" | ||
| else | ||
| echo "✗ [mutation] $m 变异分低于阈值或执行失败 (报告: target/pit-reports)" | ||
| fail=1 | ||
| # 非阻断告警: 删除 TS 测试文件(与 Java 分支同语义,阈值兜底吸收) | ||
| deleted_ts=$(git diff --name-only --diff-filter=D "$COMPARE"...HEAD 2>/dev/null | grep -E '(\.test|\.spec)\.(ts|tsx|js|mjs)$' || true) | ||
| if [ -n "$deleted_ts" ]; then | ||
| echo "⚠ [mutation] ts: 本次推送删除了测试文件(不拦截; 变异分由下次生产变更时阈值兜底):" | ||
| while IFS= read -r f; do printf ' - %s\n' "$f"; done <<<"$deleted_ts" | ||
| fi | ||
|
|
||
| # 触发条件: src/ 下生产 .ts/.tsx 变更(排除声明文件/测试/mock/脚本); 按行处理防空白路径拆词 | ||
| prod_ts=$(printf '%s\n' "$changed" | grep -E '^src/.*\.tsx?$' \ | ||
| | grep -vE '(\.d\.ts|\.test\.|\.spec\.|/mock/|/scripts/)' || true) | ||
| if [ -z "$prod_ts" ]; then | ||
| echo "○ [mutation] ts: 无 src/ 生产 TS 代码变更, 跳过" | ||
| else | ||
| # 变异目标只收工作树存续文件: 纯删除推送把已删路径喂给 stryker 会因目标缺失报错绑架 push | ||
| MUT=() | ||
| while IFS= read -r f; do | ||
| [ -n "$f" ] && [ -f "$f" ] && MUT+=(--mutate "$f") | ||
| done <<<"$prod_ts" | ||
| if [ ${#MUT[@]} -eq 0 ]; then | ||
| echo "○ [mutation] ts: 生产 TS 变更仅含已删除文件(无可变异目标), 跳过" | ||
| elif ! printf '%s' "${MUTATION_GATE_BUDGET:-180}" | grep -qE '^[1-9][0-9]*$'; then | ||
| # 预算先验: 非法值会被 sleep 拒收, 看门狗行为不可预期(早杀或永不杀)——不进该路径 | ||
| echo "✗ [mutation] ts: MUTATION_GATE_BUDGET 非法(须为正整数秒): '${MUTATION_GATE_BUDGET:-180}'" | ||
| fail=1 | ||
| else | ||
| budget="${MUTATION_GATE_BUDGET:-180}" | ||
| n=$(( ${#MUT[@]} / 2 )) | ||
| echo "▶ [mutation] ts: StrykerJS 快速档 ($n 个变更文件, 预算 ${budget}s, 阈值见 stryker.config.json thresholds.break)" | ||
| # --no-install: 本地未装 stryker 即失败(fail-closed 接入不完整),不静默联网装 | ||
| # 看门狗三防(确定性极性): | ||
| # 1) 子壳整体重定向——否则继承调用方(lefthook/测试)捕获管道写端, gate 退出后 | ||
| # 孤儿 sleep 持管道不放, 捕获方阻塞到预算耗尽才见 EOF | ||
| # 2) sleep 成功走完才写 flag 并 kill_tree 先子后父收割全树; 主壳见 flag 只 | ||
| # wait 让收割收完——真超时确定性放行 | ||
| # 3) sleep 被主壳 pkill 打杀则 && 短路, flag 永不落盘——外部终止/OOM 同状态码 | ||
| # (143/137) 确定性不放行; 放行只认"flag 在 + rc 143/137"双证 | ||
| flag=$(mktemp "${TMPDIR:-/tmp}/mgate-flag.XXXXXX"); [ -n "$flag" ] && rm -f "$flag" | ||
| npx --no-install stryker run --incremental "${MUT[@]}" & | ||
| s_pid=$! | ||
| ( sleep "$budget" && { : > "$flag"; kill_tree "$s_pid"; } ) >/dev/null 2>&1 & | ||
|
Comment on lines
+245
to
+248
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 中等 · 可绕过变异测试失败 如果同一主机上的其他租户能在共享且可写的 请保留安全创建的 面向 AI 代理的提示在 `.lefthook/mutation-gate.sh` 第 245-248 行:
**可绕过变异测试失败**
如果同一主机上的其他租户能在共享且可写的 `TMPDIR` 中看到临时文件名,并在 watchdog 打开该文件前将其替换为符号链接,那么 watchdog 的 `: > "$flag"` 会跟随该路径上的符号链接并截断其目标;后续的 `-f "$flag"` 也会跟随符号链接。因此,攻击者既可以覆盖 hook 用户有权写入的文件,也可以让无关的 Stryker 退出码 137/143 被视为超时,从而放行推送。
请保留安全创建的 `mktemp` 文件(或使用私有临时目录),并确保超时检查验证写入该受保护文件的标记。Original comment in English🟡 Medium · Mutation failures can be bypassed When a co-tenant can observe the temporary filename in a shared writable Keep the securely created Prompt for AI agentsIn `.lefthook/mutation-gate.sh` at lines 245-248:
**Mutation failures can be bypassed**
When a co-tenant can observe the temporary filename in a shared writable `TMPDIR` and replace it with a symlink before the watchdog opens it, the watchdog’s `: > "$flag"` follows a symlink at that path and truncates its target; the later `-f "$flag"` also follows symlinks, so an attacker can both overwrite a file writable by the hook user and make an unrelated Stryker exit 137/143 count as a timeout, allowing the push.
Keep the securely created `mktemp` file in place (or use a private temporary directory) and ensure timeout checks validate a marker written to that protected file. |
||
| w_pid=$! | ||
| wait "$s_pid"; rc=$? | ||
| # 看门狗组清理: 先打杀其 sleep 子进程(防孤儿 sleep 存活到预算耗尽); | ||
| # flag 已落盘说明 sleep 自然走完 → 只 wait 让 kill_tree 收割完, 不打断 | ||
| pkill -TERM -P "$w_pid" 2>/dev/null | ||
| if [ -f "$flag" ]; then wait "$w_pid" 2>/dev/null; else kill "$w_pid" 2>/dev/null; wait "$w_pid" 2>/dev/null; fi | ||
| timed=0; [ -n "$flag" ] && [ -f "$flag" ] && timed=1 | ||
| [ -n "$flag" ] && rm -f "$flag" | ||
| if [ "$timed" = 1 ] && { [ "$rc" -eq 143 ] || [ "$rc" -eq 137 ]; }; then | ||
| echo "○ [mutation] ts: 预算 ${budget}s 封顶, 放行 (增量缓存已留存下次续跑; ts 全量档未接, --ci 目前跳过 ts)" | ||
| elif [ "$rc" -eq 0 ]; then | ||
| echo "✓ [mutation] ts: 变异分达标" | ||
| else | ||
| echo "✗ [mutation] ts: 变异分低于阈值或执行失败 (报告: reports/mutation/)" | ||
| fail=1 | ||
| fi | ||
| fi | ||
| fi | ||
| fi | ||
| done | ||
| fi | ||
|
|
||
| [ $fail -ne 0 ] && echo "✗ [mutation] 变异测试红线未通过" | ||
| exit $fail | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 中等 · 跳过继承 PIT 配置的模块
当子 Maven 模块从父 POM 继承 PIT 插件,却未在自己的 POM 中重复声明该插件时,子模块会继承父级的
pitest-maven,但自身 POM 中没有该声明,因此本地分支会将该子模块标记为未配置,并跳过其变异测试。CI 扫描只记录父模块,随后选择该聚合模块而非子模块,导致子模块的生产代码变更未接受检查。请解析 Maven 的有效插件配置,并将继承配置的子模块纳入模块列表,而不是依赖对每个 POM 的字面匹配。
另见
.lefthook/mutation-gate.sh:103。面向 AI 代理的提示
Original comment in English
🟡 Medium · Inherited PIT modules are skipped
When a child Maven module inherits the PIT plugin from a parent POM without repeating the plugin declaration in its own POM, a child module inherits
pitest-mavenfrom a parent but omits it from its own POM, so the local branch labels the child unconfigured and skips its mutation checks. The CI scan records only the parent, then selects that aggregator rather than the child, leaving the child’s production changes unchecked.Resolve effective Maven plugin configuration and include inherited child modules in the module list rather than relying on literal matches in each POM.
Also at
.lefthook/mutation-gate.sh:103.Prompt for AI agents