@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
@@ -0,0 +1,100 @@
1
+ #!/bin/bash
2
+ # GENERATED FILE — DO NOT EDIT.
3
+ # Source of truth: src/npm-package/lib/deploy-manifest.js
4
+ # Regenerate: node scripts/generate-deploy-manifest.cjs
5
+ #
6
+ # 部署文件清单的 shell 投影(REQ-DEPLOY-001,#789 根因②)。install.sh
7
+ # 无法 require JS,只能 source 本投影。JS 模块是唯一事实源;手改本文件
8
+ # 会让 install.sh 与 doctor/Node 消费方(直接读 JS 模块)不一致——
9
+ # 正是 #495/#500 的双投影漂移教训。
10
+ #
11
+ # 每个 DEPLOY_* 常量被 sourcing 父进程(install.sh)消费,非本文件。
12
+ # A linter checking this file alone cannot see that consumer, so each
13
+ # declaration carries a scoped directive (#548). Generated, not hand-added.
14
+
15
+ # 随 hook 部署到 hooks 目标目录的脚本(平铺,无子目录)。
16
+ # Consumed by the sourcing parent, not here.
17
+ # shellcheck disable=SC2034
18
+ DEPLOY_HOOK_FILES=(
19
+ 'pre-commit'
20
+ 'pre-push'
21
+ 'post-commit'
22
+ 'post-merge'
23
+ 'hook-module-resolution.sh'
24
+ )
25
+
26
+ # 门禁脚本全集(10 个 gate-*.sh + sprint-gate.sh)。部署目标依链路而异:
27
+ # 全局 copyAdapters → adapters/;项目内 install.sh → .git/hooks/。
28
+ # Consumed by the sourcing parent, not here.
29
+ # shellcheck disable=SC2034
30
+ DEPLOY_GATE_FILES=(
31
+ 'gate-3.sh'
32
+ 'gate-4.sh'
33
+ 'gate-6-layers.sh'
34
+ 'gate-7.sh'
35
+ 'gate-8.sh'
36
+ 'gate-10.sh'
37
+ 'gate-12-file-hygiene.sh'
38
+ 'gate-13-compiler-strictness.sh'
39
+ 'gate-registry.sh'
40
+ 'sprint-gate.sh'
41
+ )
42
+
43
+ # 适配器基础设施与 hooks/lib(单独字段,部署目标不同)。
44
+ # Consumed by the sourcing parent, not here.
45
+ # shellcheck disable=SC2034
46
+ DEPLOY_ADAPTER_COMMON='adapter-common.sh'
47
+ DEPLOY_HOOKS_LIB='lib'
48
+
49
+ # 非 hook 模块全集(lib/* 逐个列出,加 adapter-common.sh、deploy-manifest.sh、
50
+ # install.sh)——REQ-DEPLOY-002。
51
+ #
52
+ # 这里刻意逐文件列出,而不是给一个 `lib` 目录名让安装器自己 glob。目录名形态
53
+ # 让「清单是否完整」无法通过读清单来回答:装到用户磁盘上的文件可能没有任何
54
+ # 清单条目,于是「都在清单里」永远为真。逐文件列出使清单成为真正的安装计划。
55
+ # Consumed by the sourcing parent, not here.
56
+ # shellcheck disable=SC2034
57
+ DEPLOY_MODULE_FILES=(
58
+ 'adapter-common.sh'
59
+ 'deploy-manifest.sh'
60
+ 'install.sh'
61
+ 'gate-driver.sh'
62
+ 'gate-entries/gate-0-entry.sh'
63
+ 'gate-entries/gate-1-entry.sh'
64
+ 'gate-entries/gate-2-entry.sh'
65
+ 'gate-entries/gate-3-entry.sh'
66
+ 'gate-entries/gate-4-entry.sh'
67
+ 'gate-entries/gate-5-entry.sh'
68
+ 'gate-entries/gate-6-entry.sh'
69
+ 'gate-entries/gate-7-entry.sh'
70
+ 'gate-entries/gate-8-entry.sh'
71
+ 'gate-entries/gate-9-entry.sh'
72
+ 'gate-entries/gate-10-entry.sh'
73
+ 'gate-entries/gate-11-entry.sh'
74
+ 'gate-entries/gate-12-entry.sh'
75
+ 'gate-entries/gate-12b-entry.sh'
76
+ 'gate-entries/gate-12c-entry.sh'
77
+ 'gate-entries/gate-13-entry.sh'
78
+ 'gate-entries/subdir-restore.sh'
79
+ 'lib/Measure-PowerShellComplexity.ps1'
80
+ 'lib/assertion-quality.sh'
81
+ 'lib/audit-sanitize.sh'
82
+ 'lib/ccn-ratchet.sh'
83
+ 'lib/coverage-total.sh'
84
+ 'lib/diff-coverage.sh'
85
+ 'lib/docs-only.sh'
86
+ 'lib/gate-5c-alignment.sh'
87
+ 'lib/java-test-selection.sh'
88
+ 'lib/jscpd-run.sh'
89
+ 'lib/layer-baseline.cjs'
90
+ 'lib/native-path.sh'
91
+ 'lib/now-ms.sh'
92
+ 'lib/prepush-score.py'
93
+ 'lib/python-incremental-typecheck.sh'
94
+ 'lib/python-test-selection.sh'
95
+ 'lib/sprint-gate-report.sh'
96
+ 'lib/test-failure.sh'
97
+ 'lib/toolchain-path.sh'
98
+ 'lib/typecheck.sh'
99
+ 'lib/validate-code-walkthrough.cjs'
100
+ )
package/hooks/gate-10.sh CHANGED
@@ -40,10 +40,11 @@ GATE_10_SCANNED_FILES=0
40
40
  # GATE_10_SEMGREP_PATH records what actually happened, so a reader of the report
41
41
  # can tell a fast run from a degraded one without timing it.
42
42
  #
43
- # The daemon does not exist yet (that is E2/E3). Until it does, EVERY value of
44
- # XP_GATE_SASTD resolves to "direct", which is what makes shipping E1 a no-op:
45
- # the switch, the degrade flag and the tests land first so the fast path is added
46
- # onto a base that is already proven to behave exactly as today.
43
+ # The daemon exists but is NOT the default. It is opt-in via XP_GATE_SASTD=on,
44
+ # and `auto` resolves to direct on purpose -- see the `auto` case below, which
45
+ # records the measurement that made the default a direct scan. Everything here is
46
+ # a switch plus a report field; with the default settings the gate behaves exactly
47
+ # as it did before the daemon was written.
47
48
  # ---------------------------------------------------------------------------
48
49
  # Consumed by the sourcing parent (pre-commit report JSON), not here.
49
50
  # shellcheck disable=SC2034
@@ -62,28 +63,24 @@ case "${XP_GATE_SASTD:-direct}" in
62
63
  GATE_10_SEMGREP_PATH="direct"
63
64
  ;;
64
65
  auto)
65
- # "Use the daemon if one is available, otherwise just scan." Absence of a
66
- # daemon is the normal state for most users, not a fault, so `auto` does NOT
67
- # set the degraded flag -- marking it would make every ordinary run look
68
- # degraded the moment E2 lands and train readers to ignore the flag.
69
- # E2 MUST add a probe INSIDE this branch rather than flipping the value:
70
- # a daemon that is ABSENT keeps degraded=0 (normal), but a daemon that is
71
- # PRESENT BUT UNUSABLE must set degraded=1 -- otherwise `auto` silently
72
- # swallows the same failure `on` reports. Get that distinction right here;
73
- # it is the whole reason the two branches are separate.
66
+ # `auto` deliberately does NOT select the daemon. It was meant to mean "use one
67
+ # if available", but measurement killed that default: with Gate 10's real
68
+ # ruleset (p/security-audit, 225 rules) the daemon saves ~20% in steady state
69
+ # (6.6-6.9s -> 5.2-5.4s) and is SLOWER end to end (10.5s -> 14.6s), because each
70
+ # gate run is a fresh process that pays the daemon's own startup without
71
+ # amortising it over a second call. Making that the automatic choice would tax
72
+ # every user for a benefit most of them never see.
73
+ #
74
+ # `auto` therefore behaves as "off" but says so through the `path` field, and it
75
+ # never sets degraded -- a plain direct run is not a degraded run.
74
76
  GATE_10_SEMGREP_PATH="direct"
75
77
  ;;
76
78
  on|1|true)
77
- # The user asserted a daemon SHOULD be here. If it is not, that is a
78
- # degradation worth reporting: their configuration is not being honoured.
79
+ # Opt-in only. The user asserted a daemon SHOULD be here; whether one actually
80
+ # answers can only be known after the probe, so the degrade flag is set there.
81
+ # Setting it at parse time would report a failure before anything has failed.
79
82
  # This is the branch that must never become "skip the scan and call it a pass".
80
- GATE_10_SEMGREP_PATH="direct"
81
- GATE_10_SASTD_DEGRADED=1
82
- # stderr, not stdout: this file is SOURCED by pre-commit, so stdout belongs to
83
- # the parent's report stream. A notice written there would land inside output
84
- # that another consumer may parse. stderr still reaches the developer's
85
- # terminal, which is the only audience a notice needs.
86
- echo " ℹ️ sastd requested but not available — using direct semgrep (degraded)" >&2
83
+ GATE_10_SEMGREP_PATH="daemon"
87
84
  ;;
88
85
  *)
89
86
  # An unrecognised value is a configuration mistake, not a reason to scan
@@ -251,8 +248,85 @@ EOF
251
248
  fi
252
249
  SEMGREP_TIMEOUT_S="${XP_GATE_SEMGREP_TIMEOUT:-180}"
253
250
  SEMGREP_TIMED_OUT=false
254
- if command -v timeout >/dev/null 2>&1; then
255
- SEMGREP_STDOUT_FILE=$(mktemp)
251
+ SEMGREP_STDOUT_FILE=$(mktemp)
252
+ SEMGREP_EXIT=0
253
+ SEMGREP_OUTPUT=""
254
+
255
+ # Daemon path (E3). One long-lived semgrep process, so its ~2.5s import tax is
256
+ # paid once per machine instead of once per gate run: direct 3.8-4.0s vs daemon
257
+ # hot 2.1-2.3s measured on this repo.
258
+ #
259
+ # A SWITCHED path, never a replacement: every failure below falls through to
260
+ # the direct scan. The daemon may make this gate SLOWER; it must never make it
261
+ # greener (#518/#559).
262
+ if [ "$GATE_10_SEMGREP_PATH" = "daemon" ] && command -v node >/dev/null 2>&1; then
263
+ # The protocol carries exactly ONE config. Counting rather than overwriting
264
+ # matters: the previous loop kept only the last --config, so a repo with two
265
+ # would have been scanned with one and reported on as if fully checked.
266
+ rules_for_daemon=""
267
+ config_count=0
268
+ for _arg in "${SEMGREP_RULES[@]}"; do
269
+ case "$_arg" in
270
+ --config=*)
271
+ rules_for_daemon="${_arg#--config=}"
272
+ config_count=$((config_count + 1))
273
+ ;;
274
+ esac
275
+ done
276
+ # Anything the protocol cannot express exactly is left to the direct path
277
+ # rather than approximated:
278
+ # - more than one config (nothing would report the dropped one);
279
+ # - p/csharp (Gate 10 adds it for C# repos);
280
+ # - "auto" (semgrep's own rule selection, not a spec we can pass through);
281
+ # - anything with a comma, which is semgrep's multi-config shorthand;
282
+ # - a remote URL, which the daemon would resolve against its own cwd and
283
+ # registry cache instead of the one the CLI would have used.
284
+ if [ "$config_count" -ne 1 ]; then
285
+ rules_for_daemon=""
286
+ fi
287
+ case "$rules_for_daemon" in
288
+ *p/csharp*|*","*|*"://"*|auto|"") rules_for_daemon="" ;;
289
+ esac
290
+ if [ -n "$rules_for_daemon" ]; then
291
+ daemon_roots=$(printf '%s\n' "${SEMGREP_ARGS[@]}" | grep -v '^--include=' || true)
292
+ # ARGV TRAP: under `node -e` there is NO script path in argv, so the first
293
+ # user argument is argv[1]. Indexing it as argv[2] passes undefined as the
294
+ # config, semgrep raises a python TypeError, and the gate "degrades" for a
295
+ # reason unrelated to the daemon -- which is how this looked like a flaky
296
+ # integration instead of an off-by-one.
297
+ # shellcheck disable=SC2086 # one argv entry per scanning root: word splitting is intended
298
+ if DAEMON_JSON=$(XP_GATE_SASTD=on node -e 'const NL = String.fromCharCode(10);
299
+ const { requestScan } = require(process.argv[1]);
300
+ requestScan({ config: process.argv[2], roots: process.argv.slice(3) }).then((r) => {
301
+ if (!r.ok) { process.stderr.write("SASTD_UNAVAILABLE: " + r.error + NL); process.exit(3); }
302
+ process.stdout.write(JSON.stringify(r.payload));
303
+ }).catch((e) => { process.stderr.write("SASTD_UNAVAILABLE: " + e.message + NL); process.exit(3); });' "$(cd "$GATE_DIR/../src/npm-package/lib" 2>/dev/null && pwd -W)/sastd-client.js" "$rules_for_daemon" $daemon_roots 2>"$SEMGREP_STDOUT_FILE.stderr"); then
304
+ printf '%s' "$DAEMON_JSON" > "$SEMGREP_STDOUT_FILE"
305
+ SEMGREP_OUTPUT=$(cat "$SEMGREP_STDOUT_FILE.stderr" 2>/dev/null || true)
306
+ rm -f "$SEMGREP_STDOUT_FILE.stderr"
307
+ else
308
+ SEMGREP_EXIT=$?
309
+ rm -f "$SEMGREP_STDOUT_FILE.stderr"
310
+ : > "$SEMGREP_STDOUT_FILE"
311
+ case "${XP_GATE_SASTD:-direct}" in
312
+ on|1|true)
313
+ # Consumed by the sourcing parent (pre-commit report JSON), not here.
314
+ # shellcheck disable=SC2034
315
+ GATE_10_SASTD_DEGRADED=1
316
+ echo " ⚠️ sastd requested but unavailable — using direct semgrep (degraded)" >&2
317
+ ;;
318
+ *)
319
+ echo " ℹ️ sastd unavailable — using direct semgrep" >&2
320
+ ;;
321
+ esac
322
+ GATE_10_SEMGREP_PATH="direct"
323
+ fi
324
+ else
325
+ GATE_10_SEMGREP_PATH="direct"
326
+ fi
327
+ fi
328
+
329
+ if [ "$GATE_10_SEMGREP_PATH" = "direct" ] && command -v timeout >/dev/null 2>&1; then
256
330
  SEMGREP_OUTPUT=$(timeout -k 10 "$SEMGREP_TIMEOUT_S" \
257
331
  "$SEMGREP_CMD" scan "${SEMGREP_RULES[@]}" --json --disable-version-check \
258
332
  "${SEMGREP_ARGS[@]}" 2>&1 >"$SEMGREP_STDOUT_FILE")
@@ -266,13 +340,12 @@ EOF
266
340
  case "$SEMGREP_EXIT" in
267
341
  124|137) SEMGREP_TIMED_OUT=true ;;
268
342
  esac
269
- else
343
+ elif [ "$GATE_10_SEMGREP_PATH" = "direct" ]; then
270
344
  # No `timeout` on this platform (stock macOS). Say so: an unbounded run
271
345
  # nobody mentioned is exactly how #559 stayed invisible for a whole
272
346
  # release. The scan still runs -- refusing to scan would be a worse
273
347
  # trade -- but the unbounded window is now stated.
274
348
  echo " ⚠️ 'timeout' command not available — semgrep runs UNBOUNDED (install coreutils or set up gtimeout)"
275
- SEMGREP_STDOUT_FILE=$(mktemp)
276
349
  SEMGREP_OUTPUT=$("$SEMGREP_CMD" scan "${SEMGREP_RULES[@]}" --json --disable-version-check \
277
350
  "${SEMGREP_ARGS[@]}" 2>&1 >"$SEMGREP_STDOUT_FILE")
278
351
  SEMGREP_EXIT=$?
@@ -15,7 +15,19 @@ GATE_12_START=$(gate_start_ms)
15
15
  MAX_FILE_SIZE_BYTES="${XP_GATE_MAX_FILE_SIZE:-1048576}" # 1MB default
16
16
 
17
17
  # Get list of staged files (excluding deleted files)
18
- STAGED_FILES=$(git diff --cached --name-only --diff-filter=d 2>/dev/null)
18
+ #
19
+ # Third-party trees are excluded. In a fresh `git init` fixture the CI's
20
+ # "First Commit Gates" job does `git add -A` before committing, so every
21
+ # node_modules/ file becomes staged — and third-party packages legitimately
22
+ # CONTAIN `<<<<<<<` text as documentation (semver/README.md, various NOTICE.txt).
23
+ # Gate 12 then reported "merge conflict markers detected" for files no author
24
+ # wrote, blocking the fixture commit, on every commit for months. A hygiene check
25
+ # that reads vendored documentation as a conflict is not finding anything.
26
+ #
27
+ # This is the same class as the __pycache__ filter in check-hook-mirror.sh (#587):
28
+ # build/dependency artefacts are not project source and must not be judged as it.
29
+ STAGED_FILES=$(git diff --cached --name-only --diff-filter=d 2>/dev/null \
30
+ | grep -vE '(^|/)(node_modules|\.stryker-tmp|vendor)/' || true)
19
31
 
20
32
  if [ -z "$STAGED_FILES" ]; then
21
33
  echo " ℹ️ No staged files — skipping"
@@ -0,0 +1,120 @@
1
+ #!/usr/bin/env bash
2
+ # ============================================================================
3
+ # Ordered gate driver (#798)
4
+ #
5
+ # WHY THIS FILE EXISTS
6
+ # The gate order used to BE the physical order of 16 inline blocks inside
7
+ # pre-commit. Reordering meant moving thousands of lines between blocks, and
8
+ # the first attempt at doing that moved the hook's epilogue along with the
9
+ # last gate: every gate after it silently stopped running while `bash -n`,
10
+ # a line count check and five ordering assertions all passed. Gate 5a stopped
11
+ # blocking commits and only a behavioural fingerprint caught it.
12
+ #
13
+ # Order is now data. Each gate body lives in githooks/gate-entries/ and is
14
+ # called by name from XP_GATE_ORDER below. Moving a gate is a one-line edit
15
+ # that cannot detach its body from its position.
16
+ #
17
+ # CONTRACT FOR ENTRY FILES
18
+ # gate_NN_run() returns 0 to let the commit continue and non-zero to stop it.
19
+ # It never calls `exit`: the driver owns that decision, so the EXIT trap that
20
+ # writes the quality report keeps firing on the gate's status (see
21
+ # scripts/wrap-gate-entries.cjs for why return/exit are equivalent here).
22
+ # ============================================================================
23
+
24
+ # Gate execution order. THIS LIST IS THE ORDER OF THE PRE-COMMIT CHAIN.
25
+ #
26
+ # Ordered by cost, cheapest-rejecting first (#798). A gate that rejects a
27
+ # commit should do so before the expensive ones spend their time:
28
+ #
29
+ # 1. Compile Gate 13 (compiler), Gate 9 (build integrity)
30
+ # The compiler is the cheapest and strictest analysis any
31
+ # compiled language has, and nothing downstream is meaningful
32
+ # if it fails.
33
+ # 2. StaticCheck Gate 1 (linters / third-party static analysis)
34
+ # 3. SimianCheck Gate 2 (duplicate code)
35
+ # 4. CCNCheck Gate 3 (cyclomatic complexity)
36
+ # 5. Security Gate 8 (secrets), Gate 10 (SAST), Gate 7 (IaC)
37
+ # 6. RegressiveTest Gate 5 (test run + coverage) - the most expensive gate,
38
+ # so it runs LAST among the checks.
39
+ # tail Gate 6 (architecture), Gate 4 (principles)
40
+ #
41
+ # The prelude (0, 11, 12/12b/12c) runs first because it is cheap and decides
42
+ # whether the commit is even a candidate (version consistency, sprint state,
43
+ # file hygiene/conflict markers).
44
+ XP_GATE_ORDER="0 11 12 12b 12c 13 9 1 2 3 8 10 7 5 6 4"
45
+
46
+ # Gates that expect to run inside $PROJECT_SUBDIR. The `cd` back to the original
47
+ # directory is emitted after the LAST of these in the order above; deriving it
48
+ # (instead of leaving the restore inside gate 8's block) is what makes the
49
+ # restore survive a reorder.
50
+ XP_GATE_SUBDIR_GATES="${XP_GATE_SUBDIR_GATES:-0 11 12 12b 12c 13 9 1 2 3 8}"
51
+
52
+ # Source every gate body once, before the chain runs. Sourcing defines the
53
+ # gate_NN_run functions; it does not execute them.
54
+ _xp_gate_load_entries() {
55
+ local entry_dir="$1" key fname
56
+ for key in $XP_GATE_ORDER; do
57
+ fname="$entry_dir/gate-${key}-entry.sh"
58
+ if [ ! -f "$fname" ]; then
59
+ echo "❌ FATAL: gate entry missing: $fname" >&2
60
+ echo " The order lists gate '$key' but its body file is absent." >&2
61
+ echo " Fix: reinstall the hooks (xp-gate doctor --sync-hooks)." >&2
62
+ return 1
63
+ fi
64
+ # shellcheck source=/dev/null
65
+ . "$fname" || {
66
+ echo "❌ FATAL: gate entry failed to load: $fname" >&2
67
+ return 1
68
+ }
69
+ done
70
+ return 0
71
+ }
72
+
73
+ # Run the ordered chain.
74
+ #
75
+ # A gate that returns non-zero stops the commit. The driver exits with that
76
+ # status rather than returning it: only a real `exit` fires pre-commit's EXIT
77
+ # trap, and that trap is what writes the quality report for a blocked commit.
78
+ _xp_gate_run_chain() {
79
+ local key fn rc
80
+ local last_subdir=""
81
+
82
+ # Derive which gate is the LAST one expecting $PROJECT_SUBDIR, so the restore
83
+ # is emitted at the right point regardless of how the order is edited.
84
+ for key in $XP_GATE_ORDER; do
85
+ case " $XP_GATE_SUBDIR_GATES " in
86
+ *" $key "*) last_subdir="$key" ;;
87
+ esac
88
+ done
89
+
90
+ for key in $XP_GATE_ORDER; do
91
+ fn="gate_${key}_run"
92
+
93
+ if ! declare -F "$fn" >/dev/null 2>&1; then
94
+ echo "❌ FATAL: gate '$key' was loaded but defines no $fn()" >&2
95
+ echo " The entry file exists but its function signature does not match" >&2
96
+ echo " its order key. This gate did NOT run." >&2
97
+ exit 1
98
+ fi
99
+
100
+ "$fn"
101
+ rc=$?
102
+ if [ "$rc" -ne 0 ]; then
103
+ exit "$rc"
104
+ fi
105
+
106
+ if [ "$key" = "$last_subdir" ]; then
107
+ # Resolve beside THIS file, not via $GATE_DIR: pre-commit's preamble points
108
+ # GATE_DIR at ADAPTER_DIR for adapter lookup, so it does not name the
109
+ # directory the gate bodies live in.
110
+ local _chain_dir
111
+ _chain_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
112
+ if [ -f "$_chain_dir/gate-entries/subdir-restore.sh" ]; then
113
+ # shellcheck source=/dev/null
114
+ . "$_chain_dir/gate-entries/subdir-restore.sh"
115
+ xp_gate_subdir_restore
116
+ fi
117
+ fi
118
+ done
119
+ return 0
120
+ }