@boyingliu01/xp-gate 0.37.3 → 0.38.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/adapter-common.sh +12 -2
  2. package/adapters/csharp.sh +73 -2
  3. package/adapters/dart.sh +13 -0
  4. package/adapters/go.sh +5 -0
  5. package/adapters/iac.sh +46 -3
  6. package/adapters/java.sh +6 -0
  7. package/adapters/kotlin.sh +6 -0
  8. package/adapters/shell.sh +119 -34
  9. package/adapters/swift.sh +6 -0
  10. package/adapters/typescript.sh +98 -34
  11. package/bin/xp-gate.js +48 -2
  12. package/gate-10.sh +99 -26
  13. package/gate-12-file-hygiene.sh +13 -1
  14. package/gate-registry.sh +197 -0
  15. package/gates/python-health.ts +67 -29
  16. package/hooks/adapter-common.sh +12 -2
  17. package/hooks/deploy-manifest.sh +100 -0
  18. package/hooks/gate-10.sh +99 -26
  19. package/hooks/gate-12-file-hygiene.sh +13 -1
  20. package/hooks/gate-driver.sh +120 -0
  21. package/hooks/gate-entries/gate-0-entry.sh +825 -0
  22. package/hooks/gate-entries/gate-1-entry.sh +767 -0
  23. package/hooks/gate-entries/gate-10-entry.sh +52 -0
  24. package/hooks/gate-entries/gate-11-entry.sh +120 -0
  25. package/hooks/gate-entries/gate-12-entry.sh +44 -0
  26. package/hooks/gate-entries/gate-12b-entry.sh +178 -0
  27. package/hooks/gate-entries/gate-12c-entry.sh +80 -0
  28. package/hooks/gate-entries/gate-13-entry.sh +584 -0
  29. package/hooks/gate-entries/gate-2-entry.sh +199 -0
  30. package/hooks/gate-entries/gate-3-entry.sh +53 -0
  31. package/hooks/gate-entries/gate-4-entry.sh +46 -0
  32. package/hooks/gate-entries/gate-5-entry.sh +1729 -0
  33. package/hooks/gate-entries/gate-6-entry.sh +658 -0
  34. package/hooks/gate-entries/gate-7-entry.sh +41 -0
  35. package/hooks/gate-entries/gate-8-entry.sh +33 -0
  36. package/hooks/gate-entries/gate-9-entry.sh +307 -0
  37. package/hooks/gate-entries/subdir-restore.sh +27 -0
  38. package/hooks/gate-registry.sh +51 -15
  39. package/hooks/hook-module-resolution.sh +73 -0
  40. package/hooks/install.sh +135 -0
  41. package/hooks/lib/now-ms.sh +55 -19
  42. package/hooks/lib/toolchain-path.sh +50 -6
  43. package/hooks/pre-commit +256 -5091
  44. package/hooks/pre-push +212 -25
  45. package/lib/__tests__/arch-config-template.test.js +190 -0
  46. package/lib/__tests__/baseline-shell-scope.test.js +60 -0
  47. package/lib/__tests__/crap-cli.test.js +14 -3
  48. package/lib/__tests__/deploy-manifest-check-wired.test.js +138 -0
  49. package/lib/__tests__/deploy-manifest-completeness.test.js +151 -0
  50. package/lib/__tests__/deploy-manifest-consumers-adapter-uninstall.test.js +40 -0
  51. package/lib/__tests__/deploy-manifest-consumers-no-private-lists.test.js +74 -0
  52. package/lib/__tests__/deploy-manifest-projection.test.js +66 -0
  53. package/lib/__tests__/deploy-manifest-shell-parity.test.js +108 -0
  54. package/lib/__tests__/deploy-manifest.test.js +97 -0
  55. package/lib/__tests__/doctor.test.js +51 -7
  56. package/lib/__tests__/gate-coverage.test.js +79 -12
  57. package/lib/__tests__/gate-numbering.test.js +28 -1
  58. package/lib/__tests__/install-sh-deploy-manifest.test.js +80 -0
  59. package/lib/__tests__/mirror-sync.test.js +45 -3
  60. package/lib/__tests__/npm-package.test.js +10 -7
  61. package/lib/__tests__/sastd-client.test.js +514 -0
  62. package/lib/__tests__/shared-utils-copyHooks-manifest.test.js +69 -0
  63. package/lib/__tests__/sprint-status-rework.test.js +1 -0
  64. package/lib/arch-config-template.js +87 -0
  65. package/lib/baseline.js +54 -2
  66. package/lib/deploy-manifest.js +174 -0
  67. package/lib/doctor.js +46 -14
  68. package/lib/gate-capability.js +291 -0
  69. package/lib/gate-coverage.js +194 -74
  70. package/lib/gate-identity.js +349 -0
  71. package/lib/gate-runner.js +11 -1
  72. package/lib/hook-module-resolution.js +27 -0
  73. package/lib/init.js +72 -5
  74. package/lib/mattpocock-license-verdict.json +1 -1
  75. package/lib/sastd-client.js +572 -0
  76. package/lib/shared-utils.js +31 -9
  77. package/lib/uninstall.js +10 -7
  78. package/lib/update-hooks.js +27 -18
  79. package/mock-policy/AGENTS.md +1 -1
  80. package/mutation/AGENTS.md +1 -1
  81. package/package.json +3 -1
  82. package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
  83. package/plugins/claude-code/skills/delphi-review/AGENTS.md +1 -1
  84. package/plugins/claude-code/skills/sprint-flow/AGENTS.md +1 -1
  85. package/plugins/claude-code/skills/test-specification-alignment/AGENTS.md +1 -1
  86. package/plugins/dsh/package.json +1 -1
  87. package/plugins/dsh/skills/delphi-review/AGENTS.md +1 -1
  88. package/plugins/dsh/skills/sprint-flow/AGENTS.md +1 -1
  89. package/plugins/dsh/skills/test-specification-alignment/AGENTS.md +1 -1
  90. package/plugins/opencode/package.json +1 -1
  91. package/plugins/opencode/skills/delphi-review/AGENTS.md +1 -1
  92. package/plugins/opencode/skills/sprint-flow/AGENTS.md +1 -1
  93. package/plugins/opencode/skills/test-specification-alignment/AGENTS.md +1 -1
  94. package/plugins/qoder/plugin.json +1 -1
  95. package/plugins/qoder/skills/delphi-review/AGENTS.md +1 -1
  96. package/plugins/qoder/skills/sprint-flow/AGENTS.md +1 -1
  97. package/plugins/qoder/skills/test-specification-alignment/AGENTS.md +1 -1
  98. package/principles/AGENTS.md +1 -1
  99. package/principles/__tests__/analyzer.test.ts +2 -1
  100. package/principles/__tests__/config-enforcement.test.ts +1 -0
  101. package/principles/__tests__/types.test.ts +1 -0
  102. package/principles/adapters/base.ts +14 -1
  103. package/principles/adapters/duplication.ts +134 -0
  104. package/principles/rules/__tests__/clean-code/large-file.test.ts +2 -1
  105. package/principles/rules/clean-code/code-duplication.ts +75 -9
  106. package/principles/types.ts +39 -0
  107. package/scripts/__tests__/check-mattpocock-license.test.js +36 -3
  108. package/scripts/__tests__/sync-package-content.test.js +12 -2
  109. package/scripts/generate-mirrors.cjs +125 -0
  110. package/scripts/sync-package-content.js +64 -20
  111. package/skills/delphi-review/AGENTS.md +1 -1
  112. package/skills/sprint-flow/AGENTS.md +1 -1
  113. package/skills/test-specification-alignment/AGENTS.md +1 -1
  114. package/adapters/gate-9.sh +0 -245
  115. package/gate-9.sh +0 -245
  116. package/hooks/gate-9.sh +0 -245
package/adapter-common.sh CHANGED
@@ -225,7 +225,15 @@ require_tool() {
225
225
  return 0
226
226
  fi
227
227
 
228
- if command -v npx >/dev/null 2>&1 && npx --no-install "$tool_name" --version >/dev/null 2>&1; then
228
+ # Project-local node_modules/.bin. This replaces an `npx --no-install <tool>
229
+ # --version` probe: `--no-install` does prevent the package from being
230
+ # INSTALLED, but it still asks the registry every time (measured: 697 ms on a
231
+ # cache miss, 105 ms revalidating on a hit), inside a pre-commit hook. And once
232
+ # any earlier misuse populates the npx cache the same probe returns rc=0 for a
233
+ # tool the project never depended on, so the answer can be corrupted, not just
234
+ # slow. Checking the directory the tool would actually live in is both cheaper
235
+ # and honest. See docs/incidents/2026-11-09-command-could-not-run-*.
236
+ if [ -x "node_modules/.bin/$tool_name" ]; then
229
237
  return 0
230
238
  fi
231
239
 
@@ -289,7 +297,9 @@ detect_mutation_testable() {
289
297
  if [ -f "package.json" ] && grep -qE '"@stryker-mutator[^"]*"' package.json 2>/dev/null; then
290
298
  return 0
291
299
  fi
292
- if command -v npx >/dev/null 2>&1 && npx --no-install stryker --version >/dev/null 2>&1; then
300
+ # Project-local install. Replaces `npx --no-install stryker --version`, which
301
+ # still queried the registry on every call (see require_tool above).
302
+ if [ -x "node_modules/.bin/stryker" ]; then
293
303
  return 0
294
304
  fi
295
305
  # Legacy config files (backwards compatibility — deprecated)
@@ -29,6 +29,53 @@
29
29
  # Project discovery
30
30
  # ---------------------------------------------------------------------------
31
31
 
32
+ # Print the workspace argument `dotnet build` / `dotnet format` should be given.
33
+ #
34
+ # Both commands accept a directory, a .sln or a .csproj — but a DIRECTORY is
35
+ # ambiguous when a solution and a project file sit at the same level:
36
+ #
37
+ # `dotnet format <dir>` → Unhandled exception: System.IO.FileNotFoundException:
38
+ # 已在 '<dir>' 中同时找到 MSBuild 项目文件和解决方案文件
39
+ # `dotnet build <dir>` → MSBUILD : error MSB1050: "<dir>" 文件夹中包含多个
40
+ # 项目或解决方案文件
41
+ #
42
+ # Neither is a source verdict, but the callers cannot tell them apart from a real
43
+ # finding -- format's abort exits 1 like "1 change required", and build's abort
44
+ # exits 1 like "compilation failed". Measured on this host with a root .sln and a
45
+ # root .csproj: directory form rc=1 for both, rc=0 for both when the file is
46
+ # named. Naming the file removes the ambiguity the tools refuse to guess about.
47
+ #
48
+ # WHICH file, though, is not a matter of preference:
49
+ #
50
+ # * The .sln only governs what it REFERENCES. `dotnet new sln` produces a valid
51
+ # solution that references nothing, and `dotnet format <that .sln>` then
52
+ # checks zero files and exits 0 — a silently empty gate. Measured: violated
53
+ # sources, sln with no Project(...) entries -> rc=0 and no findings.
54
+ # * The same violated sources with the project added to the sln -> the
55
+ # WHITESPACE errors are reported.
56
+ #
57
+ # So a solution is used only when it actually contains a Project entry; otherwise
58
+ # the single .csproj is named; otherwise the directory is returned and the tool
59
+ # decides (correct when no ambiguity exists).
60
+ _workspace_arg() {
61
+ local root="$1"
62
+ local sln csproj count
63
+ sln=$(find "$root" -maxdepth 1 -name "*.sln" -type f 2>/dev/null | sed -n '1p; 1q')
64
+ if [ -n "$sln" ] && grep -qE '^Project\(' "$sln" 2>/dev/null; then
65
+ printf '%s\n' "$sln"
66
+ return 0
67
+ fi
68
+ # No usable solution at this root. A single .csproj is unambiguous; several are
69
+ # not, and the tools want the directory in that case (they walk the tree).
70
+ count=$(find "$root" -maxdepth 1 -name "*.csproj" -type f 2>/dev/null | wc -l | tr -d ' ')
71
+ if [ "$count" = "1" ]; then
72
+ csproj=$(find "$root" -maxdepth 1 -name "*.csproj" -type f 2>/dev/null | sed -n '1p; 1q')
73
+ printf '%s\n' "$csproj"
74
+ return 0
75
+ fi
76
+ printf '%s\n' "$root"
77
+ }
78
+
32
79
  # Print the directory that owns the build, or nothing.
33
80
  #
34
81
  # A .sln at the root is the usual shape; a bare .csproj is the single-project
@@ -99,7 +146,14 @@ run_static_analysis() {
99
146
  # --- build (recorded, not authoritative) -------------------------------
100
147
  # `--nologo` keeps the banner out; `-warnaserror` is Gate 13's job, not Gate
101
148
  # 1's, so the build here only needs to answer "does this compile".
102
- build_out=$("$dotnet" build "$dotnet_root" --nologo -v quiet 2>&1)
149
+ #
150
+ # Same ambiguity as `format` below, with a different symptom: `dotnet build`
151
+ # on a directory holding both markers fails with MSB1050 ("the folder contains
152
+ # more than one project or solution file"). That is a build that could not be
153
+ # ATTEMPTED, not a build that failed, and it would report an otherwise clean
154
+ # project as BUILD FAILED.
155
+ build_target=$(_workspace_arg "$dotnet_root")
156
+ build_out=$("$dotnet" build "$build_target" --nologo -v quiet 2>&1)
103
157
  build_rc=$?
104
158
  if [ "$build_rc" -ne 0 ]; then
105
159
  echo "BUILD FAILED (dotnet build exited $build_rc):"
@@ -112,7 +166,13 @@ run_static_analysis() {
112
166
  # `dotnet format --verify-no-changes` is the closest C# equivalent of
113
167
  # `eslint --max-warnings 0`: formatting + the configured analyzers, exit 0
114
168
  # only when everything is clean.
115
- format_out=$("$dotnet" format "$dotnet_root" --verify-no-changes --verbosity quiet 2>&1)
169
+ #
170
+ # The workspace argument is the FILE when the root carries both a .sln and a
171
+ # .csproj — see _workspace_arg. Handing it the bare directory there
172
+ # makes the tool abort with an ambiguity exception and exit 1, which is
173
+ # indistinguishable from "1 change required".
174
+ format_target=$(_workspace_arg "$dotnet_root")
175
+ format_out=$("$dotnet" format "$format_target" --verify-no-changes --verbosity quiet 2>&1)
116
176
  format_rc=$?
117
177
 
118
178
  # (#717) A command that could not RUN is not a command that found problems.
@@ -225,6 +285,17 @@ _csharp_newest_cobertura() {
225
285
  | sort -rn | head -1 | cut -f2-
226
286
  }
227
287
 
288
+ # #798 / #794: run_coverage() below is `dotnet test --collect:...`, i.e. it runs
289
+ # and validates the SAME suite run_tests() runs. #798 measured one commit at
290
+ # 28.5s for run_tests + 30.0s for run_coverage with identical results (319
291
+ # passing both times) — the first pass contributed no verdict at all.
292
+ #
293
+ # Declaring it here is what lets Gate 5 run the suite once. The decision lives in
294
+ # the adapter because the adapter is what knows its toolchain; a hook-level
295
+ # `if [ "$CURRENT_LANG" = ... ]` had already been written twice (#794) and would
296
+ # need writing again for every new language.
297
+ coverage_runs_tests() { return 0; }
298
+
228
299
  run_coverage() {
229
300
  local dotnet_root dotnet
230
301
  dotnet_root=$(_detect_dotnet_root) || { echo "No .NET project marker found"; return 0; }
package/adapters/dart.sh CHANGED
@@ -30,6 +30,19 @@ run_tests() {
30
30
  fi
31
31
  }
32
32
 
33
+ # #798 / #794: does run_coverage() also execute AND validate the test suite?
34
+ #
35
+ # Yes here: `dart test --coverage=coverage` runs every test exactly as
36
+ # run_tests() does and reports failures the same way, so the coverage pass
37
+ # carries all the information a separate test pass would. Declaring this lets
38
+ # Gate 5 run the suite ONCE instead of twice (#794 measured 28.5s + 30.0s for
39
+ # two near-identical runs on a 319-test project).
40
+ #
41
+ # This declaration is what removes the need for a language special case in the
42
+ # hook. An adapter that does NOT declare stays on the safe double-run default:
43
+ # slower, but never silently dropping test verification.
44
+ coverage_runs_tests() { return 0; }
45
+
33
46
  run_coverage() {
34
47
  if command -v dart &>/dev/null; then
35
48
  dart test --coverage=coverage 2>&1 | tail -10
package/adapters/go.sh CHANGED
@@ -58,6 +58,11 @@ run_tests() {
58
58
  return $?
59
59
  }
60
60
 
61
+ # #798 / #794: run_coverage() below also executes and validates every test
62
+ # (`go test`), so a separate run_tests() pass adds no verdict -- only time.
63
+ # Undeclared adapters keep the safe double-run default.
64
+ coverage_runs_tests() { return 0; }
65
+
61
66
  run_coverage() {
62
67
  require_tool go "Go SDK" || return 1
63
68
 
package/adapters/iac.sh CHANGED
@@ -76,6 +76,11 @@ run_static_analysis() {
76
76
  echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
77
77
 
78
78
  local exit_code=0
79
+ # #803: counts scanner/file pairs that actually ran, independent of their
80
+ # verdict. `exit_code == 0` answers "did anything fail?"; it cannot answer "did
81
+ # anything run?", and using it for both made a clean scan indistinguishable from
82
+ # a missing toolchain.
83
+ local iac_scans_completed=0
79
84
 
80
85
  # Try checkov first (recommended - supports multiple platforms)
81
86
  # Resolve to an executable that actually RUNS, not merely one that exists. On
@@ -99,7 +104,18 @@ run_static_analysis() {
99
104
  for _checkov_candidate in checkov checkov.exe checkov.cmd; do
100
105
  command -v "$_checkov_candidate" >/dev/null 2>&1 || continue
101
106
  if command -v timeout >/dev/null 2>&1; then
102
- _checkov_probe="timeout 20 $_checkov_candidate --version"
107
+ # #803: the budget must exceed the cost of the thing being timed. It was 20 s,
108
+ # and `checkov --version` measures 22.4 s on Windows with a cold pip cache
109
+ # (python startup + imports, not network). So the probe timed out, checkov was
110
+ # screened out as unusable, and the gate spent the commit pretending the tool
111
+ # was not installed — while checkov 3.3.30 was on PATH and passing the very
112
+ # file being committed.
113
+ #
114
+ # The probe exists to catch a shim that does not answer (#596), which is a
115
+ # hang, not a slow start. A tool that answers in 25 s is not a broken shim, and
116
+ # a false "not installed" is the more expensive error here: it silences a
117
+ # scanner that works. Generous, still bounded.
118
+ _checkov_probe="timeout 60 $_checkov_candidate --version"
103
119
  else
104
120
  _checkov_probe="$_checkov_candidate --version"
105
121
  fi
@@ -139,6 +155,10 @@ run_static_analysis() {
139
155
  record_checkov_run() {
140
156
  local status=$1 file=$2
141
157
  checkov_scan_count=$((checkov_scan_count + 1))
158
+ # #803: a checkov run counts as a completed scan even when it reports a
159
+ # failure — the point of this counter is "a scanner looked", which is what
160
+ # distinguishes a clean scan from a missing toolchain.
161
+ iac_scans_completed=$((iac_scans_completed + 1))
142
162
  if [ "$status" -ne 0 ] && [ "$status" -ne 1 ]; then
143
163
  CHECKOV_CRASHED=1
144
164
  echo " ⚠️ checkov exited $status on $file — the scanner failed, this is not a finding"
@@ -242,6 +262,7 @@ run_static_analysis() {
242
262
  for file in $docker_files; do
243
263
  if [ -f "$file" ]; then
244
264
  hadolint "$file" 2>&1 | tail -20
265
+ iac_scans_completed=$((iac_scans_completed + 1))
245
266
  local hadolint_exit=${PIPESTATUS[0]}
246
267
  if [ $hadolint_exit -ne 0 ]; then
247
268
  exit_code=$hadolint_exit
@@ -257,6 +278,7 @@ run_static_analysis() {
257
278
  for file in $k8s_files; do
258
279
  if [ -f "$file" ]; then
259
280
  kube-score score "$file" 2>&1 | tail -20
281
+ iac_scans_completed=$((iac_scans_completed + 1))
260
282
  local kube_exit=${PIPESTATUS[0]}
261
283
  if [ $kube_exit -ne 0 ]; then
262
284
  exit_code=$kube_exit
@@ -272,6 +294,7 @@ run_static_analysis() {
272
294
  for file in $tf_files; do
273
295
  if [ -f "$file" ]; then
274
296
  tflint --chdir "$(dirname "$file")" 2>&1 | tail -20
297
+ iac_scans_completed=$((iac_scans_completed + 1))
275
298
  local tflint_exit=${PIPESTATUS[0]}
276
299
  if [ $tflint_exit -ne 0 ]; then
277
300
  exit_code=$tflint_exit
@@ -281,7 +304,22 @@ run_static_analysis() {
281
304
  fi
282
305
 
283
306
  # If no tools available but IaC files detected, warn user
284
- if [ $exit_code -eq 0 ]; then
307
+ #
308
+ # #803: this branch used to test `[ $exit_code -eq 0 ]`, which conflates "every
309
+ # applicable scanner ran and found nothing" with "no scanner ran at all". They
310
+ # are opposite statements about the same commit, and only one of them is a
311
+ # toolchain problem.
312
+ #
313
+ # The conflation is not theoretical. On a host where `checkov --version` takes
314
+ # longer than the probe's own `timeout 20` (measured 22.4 s on Windows with a
315
+ # cold pip cache), checkov is screened out as unusable and the gate falls back
316
+ # to hadolint / kube-score / tflint -- none of which inspects a GitHub Actions
317
+ # workflow. All three iterate zero files, exit_code stays 0, and a workflow that
318
+ # checkov passes with 116 checks reported PASSED was reported as "no scanning
319
+ # tools available" and BLOCKED, with an install hint for a tool already working.
320
+ #
321
+ # So track "did any scanner actually run" separately, and say which scanners ran.
322
+ if [ "$iac_scans_completed" -eq 0 ]; then
285
323
  echo "⚠ IaC files detected but no scanning tools available."
286
324
  echo " Install checkov (recommended) or individual tools:"
287
325
  echo " - checkov: pip install checkov"
@@ -292,7 +330,12 @@ run_static_analysis() {
292
330
  echo " Per QUALITY-GATES-CODE-OF-CONDUCT.md: tool unavailable = BLOCK, not SKIP"
293
331
  return 1
294
332
  fi
295
-
333
+
334
+ # Scanners ran and none of them found anything. That is a PASS, and it must not
335
+ # be reported as a missing toolchain: a gate that blocks a clean scan teaches
336
+ # its reader that the gate is broken, and the next honest commit looks like the
337
+ # one that needed a bypass.
338
+ echo "IaC scan completed: $iac_scans_completed scanner file(s) checked, no findings."
296
339
  return $exit_code
297
340
  }
298
341
 
package/adapters/java.sh CHANGED
@@ -696,6 +696,12 @@ _run_gradle_tests() {
696
696
  return "${PIPESTATUS[0]}"
697
697
  }
698
698
 
699
+ # #798 / #794: run_coverage() below drives jacoco via the build tool's `test`
700
+ # phase, so it executes and validates the same suite run_tests() does. Declaring
701
+ # that here lets Gate 5 run the suite once; undeclared adapters keep the safe
702
+ # double-run default rather than risk skipping test verification.
703
+ coverage_runs_tests() { return 0; }
704
+
699
705
  run_coverage() {
700
706
  local build_system
701
707
  build_system=$(_detect_java_build)
@@ -105,6 +105,12 @@ run_tests() {
105
105
  fi
106
106
  }
107
107
 
108
+ # #798 / #794: run_coverage() below re-runs the test phase itself (`mvn test` /
109
+ # `gradle jacocoTestReport` both execute the suite), so a separate run_tests()
110
+ # pass adds no verdict -- only time. Undeclared adapters keep the double-run
111
+ # default, which is slower but never skips test verification.
112
+ coverage_runs_tests() { return 0; }
113
+
108
114
  run_coverage() {
109
115
  local build_system
110
116
  build_system=$(_detect_kotlin_build)
package/adapters/shell.sh CHANGED
@@ -1,55 +1,140 @@
1
1
  #!/usr/bin/env bash
2
2
 
3
- # Shell adapter for quality gates
3
+ # Shell adapter for quality gates.
4
+ #
5
+ # Scope rule: a file is a shell script when it is named *.sh / *.bash, OR when
6
+ # its first line carries a bash/sh shebang. The shebang arm is not decoration --
7
+ # `pre-commit` and `pre-push` have no extension and are the two largest shell
8
+ # programs in this repository, so an extension-only filter has never been able
9
+ # to see them.
10
+ #
11
+ # The previous implementation handed shellcheck `./*.sh ./**/**/*.sh`.
12
+ # With globstar off (the default) `**` degrades to `*`, so the second pattern
13
+ # matched paths of exactly one directory component and nothing else: measured
14
+ # here, 47 of 173 tracked .sh files were linted and 126 were dropped without a
15
+ # word, while `./*.sh` matched nothing at all because the repo root holds no
16
+ # scripts. The gate reported PASS on the quarter it happened to glob.
4
17
 
5
- run_static_analysis() {
6
- if command -v shellcheck >/dev/null 2>&1; then
7
- echo "Running Shell static analysis..."
8
- shellcheck ./*.sh
9
- return $?
10
- else
11
- echo "shellcheck not available, skipping Shell linting"
18
+ # Emit the absolute path of every shell script in this project, one per line.
19
+ #
20
+ # Files come from `git ls-files -co --exclude-standard` so that build output and
21
+ # dependencies (node_modules, dist, coverage) are excluded by the project's own
22
+ # .gitignore rather than by a hardcoded ignore list that drifts. Inside a git
23
+ # repository every shell-script candidate is checked against the shebang rule;
24
+ # outside one, the tracked-file list is unavailable and the adapter falls back
25
+ # to a bounded find so a non-git consumer still gets a real file list instead of
26
+ # an empty one that reads like a clean project.
27
+ #
28
+ # Skipped deliberately: githooks/__tests__/*.bats carry .bats, not .sh, and are
29
+ # suites rather than shipped code -- `run_tests` covers them.
30
+ _shell_script_files() {
31
+ local root
32
+ root="$(pwd)"
33
+
34
+ if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
35
+ while IFS= read -r rel; do
36
+ [ -n "$rel" ] || continue
37
+ [ -f "$rel" ] || continue
38
+ case "$rel" in
39
+ *.sh|*.bash) echo "$root/$rel"; continue ;;
40
+ esac
41
+ # No shell extension: accept only a shell shebang.
42
+ local first
43
+ first="$(sed -n '1p' "$rel" 2>/dev/null || true)"
44
+ case "$first" in
45
+ '#!'*bash*|'#!'*'/sh'|'#!'*' sh'|'#!'*'env sh'*) echo "$root/$rel" ;;
46
+ esac
47
+ done < <(git ls-files -co --exclude-standard '*.sh' '*.bash' \
48
+ '**/pre-commit' 'pre-commit' \
49
+ '**/pre-push' 'pre-push' \
50
+ '**/post-commit' 'post-commit' \
51
+ '**/post-merge' 'post-merge' \
52
+ '**/post-checkout' 'post-checkout' \
53
+ '**/commit-msg' 'commit-msg' \
54
+ '**/prepare-commit-msg' 'prepare-commit-msg' 2>/dev/null)
12
55
  return 0
13
56
  fi
57
+
58
+ # Not a git work tree: bounded search, still shebang-filtered.
59
+ while IFS= read -r f; do
60
+ [ -n "$f" ] || continue
61
+ echo "$f"
62
+ done < <(
63
+ find . -type f \( -name '*.sh' -o -name '*.bash' \) \
64
+ -not -path './node_modules/*' -not -path './.git/*' \
65
+ -not -path './dist/*' -not -path './coverage/*' 2>/dev/null | sed "s|^\./|$root/|"
66
+ )
14
67
  }
15
68
 
16
- run_lint() {
17
- if command -v shellcheck >/dev/null 2>&1; then
18
- echo "Running Shell linting..."
19
- shellcheck ./*.sh ./**/**/*.sh
20
- return $?
21
- else
69
+ run_static_analysis() {
70
+ if ! command -v shellcheck >/dev/null 2>&1; then
22
71
  echo "shellcheck not available, skipping Shell linting"
23
72
  return 0
24
73
  fi
74
+
75
+ local files=()
76
+ while IFS= read -r f; do
77
+ [ -n "$f" ] && files+=("$f")
78
+ done < <(_shell_script_files)
79
+
80
+ # No input is a distinguishable outcome, not a pass that checked something.
81
+ # Probing shellcheck second keeps the cheap question ("is there anything
82
+ # here?") in front of the expensive one.
83
+ if [ "${#files[@]}" -eq 0 ]; then
84
+ echo "No shell scripts detected."
85
+ return 0
86
+ fi
87
+
88
+ echo "Running Shell static analysis on ${#files[@]} script(s)..."
89
+ # Each path is passed as its own argument (Bash 3.2 compatible: no mapfile),
90
+ # so a path containing spaces cannot be split into two bogus arguments.
91
+ shellcheck "${files[@]}"
92
+ return $?
93
+ }
94
+
95
+ run_lint() {
96
+ # Static analysis and lint are the same check for shell -- shellcheck is the
97
+ # only analyser. Keeping both entry points working means the host hook may
98
+ # call either, but the file list must not be assembled twice.
99
+ run_static_analysis
100
+ }
101
+
102
+ # Locate the project's shell test suite. Bats is the ecosystem's shell test
103
+ # runner and this repo already carries its suites, so discovery is by *.bats.
104
+ _shell_test_files() {
105
+ find . -type f -name '*.bats' \
106
+ -not -path './node_modules/*' -not -path './.git/*' \
107
+ -not -path './dist/*' 2>/dev/null | sort
25
108
  }
26
109
 
27
110
  run_tests() {
28
- # Look for and run shell test files
29
- if [ -d "tests" ] && [ -n "$(find tests -name "*.sh" -type f | sed -n '1p; 1q')" ]; then
30
- echo "Running Shell tests..."
31
- for test_file in tests/*.sh; do
32
- if [ -x "$test_file" ]; then
33
- "$test_file"
34
- local exit_code=$?
35
- if [ "$exit_code" -ne 0 ]; then
36
- return $exit_code
37
- fi
38
- fi
39
- done
40
- return 0
41
- elif command -v shunit2 >/dev/null 2>&1; then
42
- echo "Checking for shunit2 tests..."
43
- # Skip - shunit2 requires properly formatted tests, just run any script if available
111
+ local suites
112
+ suites="$(_shell_test_files)"
113
+
114
+ if [ -z "$suites" ]; then
115
+ # Distinguished from a run that executed tests: the previous version
116
+ # printed a skip line and returned 0, which the gate reported as a pass.
117
+ echo "No shell test suite detected (*.bats)."
44
118
  return 0
45
- else
46
- echo "No shell tests detected or shunit2 available"
119
+ fi
120
+
121
+ if ! command -v bats >/dev/null 2>&1; then
122
+ echo "bats not available, skipping shell test suite (install: npm i -D bats)"
47
123
  return 0
48
124
  fi
125
+
126
+ local count
127
+ count="$(printf '%s\n' "$suites" | wc -l | tr -d ' ')"
128
+ echo "Running Shell tests (${count} .bats suite(s))..."
129
+
130
+ # shellcheck disable=SC2086 # word-splitting is the point: one arg per suite
131
+ bats $suites
132
+ return $?
49
133
  }
50
134
 
51
135
  run_coverage() {
52
- # Shell does not typically have standardized coverage tools
136
+ # Shell has no standard coverage tool. Reporting that is honest; inventing a
137
+ # number would let a coverage gate pass on nothing.
53
138
  echo "Shell coverage not available"
54
139
  return 0
55
- }
140
+ }
package/adapters/swift.sh CHANGED
@@ -33,6 +33,12 @@ run_tests() {
33
33
  fi
34
34
  }
35
35
 
36
+ # #798 / #794: does run_coverage() also execute AND validate the test suite?
37
+ # Yes: `swift test --enable-code-coverage` runs the same suite and reports the
38
+ # same failures, so a separate run_tests() pass adds no verdict -- only time.
39
+ # Undeclared adapters keep the safe double-run default.
40
+ coverage_runs_tests() { return 0; }
41
+
36
42
  run_coverage() {
37
43
  if command -v swift &>/dev/null && [ -f "Package.swift" ]; then
38
44
  swift test --enable-code-coverage 2>&1 | tail -10
@@ -2,6 +2,48 @@
2
2
 
3
3
  # TypeScript adapter for quality gates
4
4
 
5
+ # Print the path of a LOCALLY installed Node tool, or nothing.
6
+ #
7
+ # `npx <tool> --version` must never be used to answer "is this tool installed?".
8
+ # When the package is absent, npx DOWNLOADS it from the registry and runs it, so
9
+ # the probe succeeds no matter what the project actually depends on. Measured on
10
+ # a repo with no vitest at all:
11
+ #
12
+ # $ npx vitest --version
13
+ # vitest/5.1.0 win32-x64 node-v24.22.1 # rc=0
14
+ # $ ls node_modules/vitest
15
+ # (absent)
16
+ #
17
+ # The gate then ran a vitest that the project never declared, failed on a missing
18
+ # @vitest/coverage-v8, and reported that as "tests failed". Answering a question
19
+ # about the local environment must not install packages to do it -- the same rule
20
+ # AGENTS.md states for XP_GATE_NPX_PROBE, which src/gates/common-process.ts
21
+ # already honours on the TypeScript side.
22
+ #
23
+ # Only the project's own node_modules/.bin is consulted, which is what a real
24
+ # install populates. Nothing outside the project can satisfy this check.
25
+ _local_bin() {
26
+ local tool="$1"
27
+ if [ -x "node_modules/.bin/$tool" ]; then
28
+ printf '%s\n' "node_modules/.bin/$tool"
29
+ return 0
30
+ fi
31
+ return 1
32
+ }
33
+
34
+ # Run a locally installed Node tool with the given arguments.
35
+ #
36
+ # Uses the resolved binary directly rather than `npx <tool>`, for the same reason:
37
+ # npx would fetch the tool when the local binary is missing, so a typo'd tool name
38
+ # would silently test with a downloaded copy instead of failing.
39
+ _run_local_tool() {
40
+ local tool="$1"
41
+ shift
42
+ local bin
43
+ bin=$(_local_bin "$tool") || return 127
44
+ run_without_git_context "$bin" "$@"
45
+ }
46
+
5
47
  run_static_analysis() {
6
48
  # The type-check decision is shared with pre-commit's Gate 1 (#436): a project's
7
49
  # own checker (typecheck script, or GATE_TS_TYPECHECK_CMD) is used when declared,
@@ -84,63 +126,85 @@ run_lint() {
84
126
  }
85
127
 
86
128
  run_tests() {
87
- if command -v npx >/dev/null 2>&1; then
88
- echo "Running TypeScript tests..."
89
- if run_without_git_context npx vitest --version >/dev/null 2>&1; then
90
- run_without_git_context npx vitest run
91
- elif run_without_git_context npx jest --version >/dev/null 2>&1; then
92
- run_without_git_context npx jest --passWithNoTests
93
- else
94
- echo "No test runner available (vitest or jest required)"
95
- return 1
96
- fi
97
- return $?
129
+ echo "Running TypeScript tests..."
130
+ if _local_bin vitest >/dev/null; then
131
+ _run_local_tool vitest run
132
+ elif _local_bin jest >/dev/null; then
133
+ _run_local_tool jest --passWithNoTests
98
134
  else
99
- echo "npx not available, skipping TypeScript tests"
135
+ # SKIP, not BLOCK: no runner is installed, so no test was expected to run.
136
+ # Returning non-zero here is what turned "this project does not use
137
+ # vitest/jest" into "TESTS FAILED" for every downstream repo with .js files.
138
+ echo "⏭️ SKIP - No test runner installed locally (node_modules/.bin has no vitest or jest)"
139
+ echo " Install the runner this project uses, or declare its test command."
100
140
  return 0
101
141
  fi
102
142
  }
103
143
 
104
144
  run_coverage() {
105
- if command -v npx >/dev/null 2>&1; then
106
- echo "Running TypeScript coverage..."
107
- if run_without_git_context npx vitest --version >/dev/null 2>&1; then
108
- run_without_git_context npx vitest run --coverage
109
- elif run_without_git_context npx jest --version >/dev/null 2>&1; then
110
- run_without_git_context npx jest --coverage
111
- else
112
- echo "No test runner available for coverage"
113
- return 1
114
- fi
115
- return $?
145
+ echo "Running TypeScript coverage..."
146
+ local runner bin out_file rc
147
+ if _local_bin vitest >/dev/null; then
148
+ runner=vitest
149
+ bin=$(_local_bin vitest)
150
+ elif _local_bin jest >/dev/null; then
151
+ runner=jest
152
+ bin=$(_local_bin jest)
116
153
  else
117
- echo "npx not available, skipping TypeScript coverage"
154
+ echo "⏭️ SKIP - No test runner installed locally (node_modules/.bin has no vitest or jest)"
118
155
  return 0
119
156
  fi
157
+
158
+ out_file=$(mktemp)
159
+ if [ "$runner" = "vitest" ]; then
160
+ run_without_git_context "$bin" run --coverage 2>&1 | tee "$out_file"
161
+ else
162
+ run_without_git_context "$bin" --coverage 2>&1 | tee "$out_file"
163
+ fi
164
+ rc=${PIPESTATUS[0]}
165
+
166
+ if [ "$rc" -ne 0 ]; then
167
+ # A runner that could not RUN is not a runner that found failing tests. The
168
+ # common real case is a missing optional peer such as
169
+ # `@vitest/coverage-v8`; reporting that as "tests failed" sends the reader
170
+ # looking for a broken test that does not exist.
171
+ if grep -qiE "MISSING DEPENDENCY|cannot find (dependency|module)" "$out_file"; then
172
+ echo "⏭️ SKIP - $runner could not run (missing dependency), not a test failure"
173
+ echo " Install the missing package, or drop --coverage from this gate."
174
+ rm -f "$out_file"
175
+ return 0
176
+ fi
177
+ rm -f "$out_file"
178
+ return "$rc"
179
+ fi
180
+ rm -f "$out_file"
181
+ return 0
120
182
  }
121
183
 
122
184
  # ── Biome linting (if installed) ──
123
185
 
124
186
  run_biome_lint() {
125
- # Only run if biome is available
126
- if ! command -v npx >/dev/null 2>&1; then
127
- return 0
128
- fi
129
- if ! npx biome --version >/dev/null 2>&1; then
130
- return 0 # biome not installed → SKIP (same as other tools)
131
- fi
187
+ # Detection must not go through npx: `npx biome --version` downloads biome from
188
+ # the registry when the project does not have it, so the probe would always say
189
+ # "installed" and then lint with a package the project never depended on.
190
+ local BIOME_BIN
191
+ BIOME_BIN=$(_local_bin biome) || return 0 # not installed → SKIP (same as other tools)
132
192
 
133
193
  echo "Running Biome linting..."
134
194
  local BIOME_EXIT=0
135
- npx biome lint . 2>&1 || BIOME_EXIT=$?
195
+ run_without_git_context "$BIOME_BIN" lint . 2>&1 || BIOME_EXIT=$?
136
196
 
137
197
  # Check for known conflict: useLiteralKeys vs TypeScript TS4111
138
198
  # When biome reports useLiteralKeys on Record<string,unknown> usage,
139
199
  # it conflicts with TypeScript strict index-signature access (TS4111).
140
200
  # We only warn — we do NOT block — because both rules are correct in their domain.
141
- if npx biome lint . 2>&1 | grep -q "useLiteralKeys"; then
201
+ if run_without_git_context "$BIOME_BIN" lint . 2>&1 | grep -q "useLiteralKeys"; then
142
202
  local TSC_CHECK=0
143
- npx tsc --noEmit 2>&1 | grep -q "TS4111" || TSC_CHECK=$?
203
+ if [ -x "node_modules/.bin/tsc" ]; then
204
+ run_without_git_context node_modules/.bin/tsc --noEmit 2>&1 | grep -q "TS4111" || TSC_CHECK=$?
205
+ else
206
+ TSC_CHECK=1 # no local tsc → cannot confirm the conflict; stay quiet
207
+ fi
144
208
  if [ $TSC_CHECK -eq 0 ]; then
145
209
  echo ""
146
210
  echo "ℹ️ NOTE: Biome useLiteralKeys conflicts with TypeScript TS4111"