@boyingliu01/xp-gate 0.37.3 → 0.38.0

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 +452 -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 +534 -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,658 @@
1
+ #!/usr/bin/env bash
2
+ # ============================================================================
3
+ # gate-6 entry — extracted from pre-commit by scripts/extract-gates.cjs
4
+ #
5
+ # WHY THIS FILE EXISTS (#798)
6
+ # The gate order used to BE the physical order of blocks in a 6341-line
7
+ # hook, so changing the order meant moving thousands of lines. An attempt at
8
+ # that also moved the hook epilogue, after which every gate past Gate 13
9
+ # silently stopped running while `bash -n`, a line count and five ordering
10
+ # tests all passed. Gate 5a stopped blocking and only a behavioural test
11
+ # noticed. With one file per gate, order is a list of names in
12
+ # pre-commit's GATE_ORDER and cannot detach a body from its position.
13
+ #
14
+ # THE BODY BELOW IS A BYTE-EXACT SLICE of the original block. It is wrapped
15
+ # in a function so the gate keeps its own variable scope, and is called from
16
+ # pre-commit at the point the driver reaches it. Edit the gate here from now
17
+ # on - there is no copy left in the hook.
18
+ # ============================================================================
19
+
20
+ # ============================================================================
21
+ # GATE 6: Architecture & Tech Debt (Combines Old Gate 8 - Boy Scout and Gate 9 - Architecture)
22
+ # ============================================================================
23
+ # Gate body. `return` is used instead of `exit` so the driver decides to
24
+ # stop: see scripts/wrap-gate-entries.cjs for why the two are equivalent
25
+ # here, and why sub-shell exits are deliberately left as `exit`.
26
+ gate_6_run() {
27
+ 2>&1 echo ""
28
+ 2>&1 echo "→ Gate 6: Architecture & tech debt..."
29
+ GATE_6_START=$(gate_start_ms)
30
+
31
+ # First Part: Architecture Validation (previously Gate 9)
32
+ 2>&1 echo " └─ Architecture validation:"
33
+
34
+ if [ "$PROJECT_LANG" = "documentation-only" ]; then
35
+ echo " ✅ Skipped (documentation project)."
36
+
37
+ elif has_project_lang "powershell" 2>/dev/null || [ "$PROJECT_LANG" = "powershell" ]; then
38
+ echo " ℹ️ No PowerShell architecture tooling available"
39
+ echo " ⏭️ SKIPPED - Architecture validation (no tool for PowerShell)"
40
+
41
+ else
42
+ run_archlint() (
43
+ cd "$PROJECT_ROOT" || exit 1
44
+ if [ "$ARCHLINT_KIND" = "npx" ]; then
45
+ npx @archlinter/cli "$@"
46
+ elif [ "${ARCHLINT_NODE:-0}" = "1" ]; then
47
+ node "$ARCHLINT_BIN" "$@"
48
+ else
49
+ "$ARCHLINT_BIN" "$@"
50
+ fi
51
+ )
52
+
53
+ # Check if architecture config exists (use $PROJECT_ROOT — hook may have cd'd to a subdir via ARCH-03)
54
+ if [ -f "$PROJECT_ROOT/architecture.yaml" ] || [ -f "$PROJECT_ROOT/.architecturerc" ] || [ -f "$PROJECT_ROOT/.archlint.yaml" ]; then
55
+ # #686: an `architecture.yaml` is NOT evidence that layer boundaries are
56
+ # checked. Gate 6 only ever tested for the file's existence; archlint is
57
+ # invoked without --config, and even with a config it cannot enforce layers:
58
+ # @archlinter/cli 0.16.0 lists a `layer_violation` detector but produces no
59
+ # results for it under any configuration (-A, -d layer_violation, enabled:
60
+ # true, globs, exact paths — all exit 0 with zero layer findings). See
61
+ # docs/plans/2026-11-02-arch-01-gate6-layers-not-enforced-design.md §2.2.
62
+ #
63
+ # So a repo that declares layers gets a generic-smell check and a green
64
+ # verdict, which reads as "layers are clean". It is not: nothing looked.
65
+ # This block records that fact as a per-language reason so the quality
66
+ # report cannot score the dimension as covered, and emits a marker the
67
+ # terminal output can be asserted against.
68
+ #
69
+ # Deliberately NOT a capability probe: archlint silently tolerates unknown
70
+ # config keys, so "it did not complain" is a tautological guard — it would
71
+ # return "supported" no matter what. File presence is the only honest input.
72
+ GATE_6_LAYERS_UNCHECKED=0
73
+ if [ -f "$PROJECT_ROOT/architecture.yaml" ] || [ -f "$PROJECT_ROOT/.architecturerc" ]; then
74
+ # The declaration is now read by a checker of our own, so the warning
75
+ # below is no longer "nothing looked at this" — it is "archlint did not".
76
+ # The verdict on layer boundaries comes from gate-6-layers.sh; this flag
77
+ # only records that archlint's generic-smell pass measured no layers.
78
+ GATE_6_LAYERS_UNCHECKED=1
79
+ fi
80
+
81
+ # Layer-boundary enforcement (#686 / #691 / #728).
82
+ # This runs in addition to archlint, not instead of it: archlint checks
83
+ # generic smells (cycles, god modules, dead code) and its layer_violation
84
+ # detector is a measured no-op, so the dependency direction has to be
85
+ # checked directly. The checker always builds the whole-repo import graph —
86
+ # a layer violation is an edge between two directories, so an incremental
87
+ # per-directory scan structurally cannot see one.
88
+ LAYER_CHECKER=""
89
+ if [ -f "$GATE_DIR/gate-6-layers.sh" ]; then
90
+ LAYER_CHECKER="$GATE_DIR/gate-6-layers.sh"
91
+ elif [ -f "$(git rev-parse --show-toplevel 2>/dev/null)/githooks/gate-6-layers.sh" ]; then
92
+ LAYER_CHECKER="$(git rev-parse --show-toplevel)/githooks/gate-6-layers.sh"
93
+ fi
94
+ if [ -n "$LAYER_CHECKER" ]; then
95
+ LAYER_OUT=$(cd "$PROJECT_ROOT" && bash "$LAYER_CHECKER" 2>&1)
96
+ LAYER_RC=$?
97
+ printf '%s\n' "$LAYER_OUT" | sed 's/^/ /'
98
+ case "$LAYER_RC" in
99
+ 0)
100
+ GATE_6_LAYER_STATUS="PASS"
101
+ ;;
102
+ 1)
103
+ GATE_6_LAYER_STATUS="FAIL"
104
+ GATE_6_STATUS="FAIL"
105
+ ;;
106
+ 2)
107
+ # Config present but unusable: fail closed. Reporting PASS here is the
108
+ # exact defect #686 documented, one level deeper.
109
+ GATE_6_LAYER_STATUS="BLOCK"
110
+ GATE_6_STATUS="FAIL"
111
+ ;;
112
+ 3)
113
+ # WARN (no layer config, or only baselined violations).
114
+ GATE_6_LAYER_STATUS="WARN"
115
+ ;;
116
+ *)
117
+ # Any other exit means the checker itself did not complete: 126/127
118
+ # (not executable / not found), 137 (killed), or a bash error. Mapping
119
+ # those to WARN would let a crashed checker pass as "not checked",
120
+ # which is #686's defect one level deeper — the gate would look like it
121
+ # covered layers while nothing ran. Fail closed.
122
+ GATE_6_LAYER_STATUS="BLOCK"
123
+ GATE_6_STATUS="FAIL"
124
+ GATE_6_LAYER_REASON="layer_checker_crashed_rc_${LAYER_RC}"
125
+ GATE_6_LAYER_CRASHED=1
126
+ ;;
127
+ esac
128
+ GATE_6_LAYER_REASON=${GATE_6_LAYER_REASON:-$(printf '%s\n' "$LAYER_OUT" | sed -n 's/^ *reason: *//p' | head -1)}
129
+ else
130
+ # The checker is missing, so the dimension is unmeasured — say so rather
131
+ # than letting the gate look like it covered layers.
132
+ GATE_6_LAYER_STATUS="WARN"
133
+ GATE_6_LAYER_REASON="layer_checker_missing"
134
+ echo " ⚠️ [WARN:LAYER_CHECKER_MISSING] gate-6-layers.sh not found — layer boundaries NOT checked."
135
+ fi
136
+ if [ "${GATE_6_LAYER_CRASHED:-0}" = "1" ]; then
137
+ echo " ❌ [BLOCK:LAYER_CHECKER_CRASHED] gate-6-layers.sh exited $LAYER_RC without a verdict."
138
+ echo " A checker that did not finish has not checked anything — fail closed."
139
+ fi
140
+ # #537: per-language reason bookkeeping. Gate 3/4 are single-language gates
141
+ # and get away with one scalar reason; Gate 6 loops, so a scalar would lose
142
+ # information ("TS passed, PS has no detector" cannot be one string).
143
+ # Bash 4 associative array, with a scalar aggregate for the legacy field.
144
+ GATE_6_REASON=""
145
+ GATE_6_LANG_REASONS=""
146
+ if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
147
+ declare -A GATE_6_REASON_BY_LANG=()
148
+ fi
149
+ # Different architecture tools per language
150
+ for CURRENT_LANG in $PROJECT_LANGS; do
151
+ case "$CURRENT_LANG" in
152
+ "typescript")
153
+ # Prefer node on the package's own entry over the .bin shim: the shim's
154
+ # exec bit and extension vary by platform and package manager (npm writes
155
+ # archlint, archlint.cmd and archlint.ps1 on Windows), so `-x .bin/...`
156
+ # is true here while the no-extension shim is what Git Bash would run.
157
+ # `node <entry>` sidesteps all of that and also avoids npx's ~1.6 s
158
+ # package resolution (measured: 1909 ms via npx vs 319 ms direct).
159
+ if [ -f "$PROJECT_ROOT/node_modules/@archlinter/cli/bin/archlint.js" ] && command -v node >/dev/null 2>&1; then
160
+ ARCHLINT_KIND="binary"
161
+ ARCHLINT_BIN="$PROJECT_ROOT/node_modules/@archlinter/cli/bin/archlint.js"
162
+ ARCHLINT_NODE=1
163
+ elif [ -x "$PROJECT_ROOT/node_modules/.bin/archlint" ]; then
164
+ ARCHLINT_KIND="binary"
165
+ ARCHLINT_BIN="$PROJECT_ROOT/node_modules/.bin/archlint"
166
+ elif command -v archlint >/dev/null 2>&1; then
167
+ ARCHLINT_KIND="binary"
168
+ ARCHLINT_BIN="$(command -v archlint)"
169
+ elif probe_archlint_available "$PROJECT_ROOT"; then
170
+ # The three branches above are what this helper checks locally; it only
171
+ # reaches for npx when the probe is explicitly enabled
172
+ # (XP_GATE_NPX_PROBE=1). The old form always did, so on a machine with
173
+ # no archlint it downloaded the package, answered --version, and ran a
174
+ # tool the project never depended on -- see
175
+ # docs/incidents/2026-11-09-command-could-not-run-*.
176
+ ARCHLINT_KIND="npx"
177
+ else
178
+ echo " ❌ BLOCKED - archlint not installed"
179
+ echo " Install with: npm install --save-dev @archlinter/cli"
180
+ return 1
181
+ fi
182
+ echo " Running archlint for TypeScript..."
183
+ # #640: archlint costs ~8s and scans the whole tree, but an architecture
184
+ # violation cannot be introduced by a commit that touches no source file.
185
+ # Gating on "did any source change" removes the single largest fixed cost
186
+ # from docs/config-only commits without weakening the check at all.
187
+ if ! printf '%s\n' "${XP_STAGED_ACM:-}" | grep -qE '\.(ts|tsx|js|jsx|mjs|cjs|py|go|java|kt|kts|rs|cpp|cc|cxx|c|h|hpp|swift|dart|cs|csx|fs|vb)$'; then
188
+ echo " ⏭️ SKIPPED - Architecture validation (no source files changed)"
189
+ ARCHLINT_OUTPUT=""
190
+ ARCHLINT_EXIT=0
191
+ elif [ -f "$PROJECT_ROOT/.architecture-baseline.json" ]; then
192
+ ARCHLINT_OUTPUT=$(run_archlint diff .architecture-baseline.json --fail-on high 2>&1)
193
+ ARCHLINT_EXIT=$?
194
+ else
195
+ ARCHLINT_OUTPUT=$(run_archlint scan . -f table --quiet 2>&1)
196
+ ARCHLINT_EXIT=$?
197
+ fi
198
+ echo "$ARCHLINT_OUTPUT" | tail -30
199
+ if [ "$ARCHLINT_EXIT" -ne 0 ]; then
200
+ echo ""
201
+ echo "❌ BLOCKED - Architecture violations detected"
202
+ if [ -f "$PROJECT_ROOT/.architecture-baseline.json" ]; then
203
+ echo "New architecture violations found (baseline ratchet mode)."
204
+ echo "Fix the new violations above, or update the baseline with: npx @archlinter/cli snapshot -o .architecture-baseline.json"
205
+ else
206
+ echo "Fix the architecture violations above before committing."
207
+ echo "Or generate a baseline for gradual adoption: npx @archlinter/cli snapshot -o .architecture-baseline.json"
208
+ fi
209
+ return 1
210
+ fi
211
+ if [ -f "$PROJECT_ROOT/.architecture-baseline.json" ]; then
212
+ echo " ✅ TypeScript architecture validation completed (baseline ratchet mode)."
213
+ else
214
+ echo " ✅ TypeScript architecture validation completed (zero-tolerance mode). no baseline found."
215
+ fi
216
+ # #537: archlint actually ran here, so there is no skip reason. The
217
+ # matrix reads an absent reason + PASS status as `covered`.
218
+ ;;
219
+ "python")
220
+ # detect any import-linter carrier (incl. pyproject.toml [tool.importlinter])
221
+ if [ -f "$PROJECT_ROOT/.import-linter.yml" ] || [ -f "$PROJECT_ROOT/import_linter_config.yml" ] || [ -f "$PROJECT_ROOT/.importlinter" ] || grep -q '^\[tool\.importlinter\]' "$PROJECT_ROOT/pyproject.toml" 2>/dev/null; then
222
+ if require_tool "lint-imports" "Gate 6" "pip install import-linter"; then
223
+ echo " Running import-linter for Python..."
224
+ # run at PROJECT_ROOT (hook may have cd'd); block on exit code
225
+ LINT_IMPORTS_OUTPUT=$(cd "$PROJECT_ROOT" && lint-imports 2>&1)
226
+ LINT_IMPORTS_EXIT=$?
227
+ echo "$LINT_IMPORTS_OUTPUT" | head -30
228
+ if [ "$LINT_IMPORTS_EXIT" -ne 0 ]; then
229
+ echo ""
230
+ echo "❌ BLOCKED - Python architecture contract violations (import-linter)"
231
+ echo "Fix the broken contracts above before committing."
232
+ return 1
233
+ fi
234
+ echo " ✅ Python architecture validation completed."
235
+ else
236
+ echo " ❌ BLOCKED - import-linter not installed"
237
+ echo " Install with: pip install import-linter"
238
+ return 1
239
+ fi
240
+ else
241
+ echo " ℹ️ No import-linter config found (.import-linter.yml/.importlinter/[tool.importlinter]) - skipping"
242
+ echo " ⏭️ SKIPPED - Python architecture validation (no config)"
243
+ # #537: no config means Python architecture was NOT measured. Without
244
+ # this the matrix would call it `covered` simply because the branch
245
+ # recorded nothing — the exact false-confidence the matrix prevents.
246
+ if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
247
+ GATE_6_REASON_BY_LANG["$CURRENT_LANG"]="architecture_not_configured"
248
+ fi
249
+ fi
250
+ ;;
251
+ "go")
252
+ if [ -f "arch-go.yaml" ] || [ -f "arch-go.yml" ]; then
253
+ if require_tool "arch-go" "Gate 6" "go install github.com/arch-go/arch-go@latest"; then
254
+ echo " Running arch-go for Go..."
255
+ # #630: #602 replaced the vacuous Java arm with a status-based verdict,
256
+ # but the Go arm kept the identical fail-open shape: it piped arch-go
257
+ # output into `head` and then printed "Go architecture validation
258
+ # completed" unconditionally. A pipeline's status is `head`'s -- 0 for
259
+ # ANY non-empty output -- so every arch-go result, contract violations
260
+ # included, printed "completed" and the commit landed. A gate that
261
+ # cannot fail is not a gate. Capture the checker's own status and
262
+ # block on it, as the Python arm above and run_java_build_integrity do.
263
+ arch-go check 2>&1 | head -20
264
+ # The pipeline's own status is head's, so the checker's verdict has
265
+ # to come from PIPESTATUS[0] (#630). This is the same reading #602
266
+ # installed for Java via run_java_build_integrity.
267
+ _go_arch_rc="${PIPESTATUS[0]}"
268
+ if [ "$_go_arch_rc" -ne 0 ]; then
269
+ echo ""
270
+ echo "❌ BLOCKED - Go architecture contract violations (arch-go, exit $_go_arch_rc)"
271
+ echo "Fix the layering violation(s) above before committing."
272
+ GATE_6_REASON_BY_LANG["$CURRENT_LANG"]="architecture_tests_failed"
273
+ return 1
274
+ fi
275
+ echo " ✅ Go architecture validation completed."
276
+ else
277
+ echo " ❌ BLOCKED - arch-go not installed"
278
+ echo " Install with: go install github.com/arch-go/arch-go@latest"
279
+ return 1
280
+ fi
281
+ else
282
+ echo " ℹ️ No arch-go.yaml found - skipping Go architecture"
283
+ echo " ⏭️ SKIPPED - Go architecture validation (no config)"
284
+ # #537: same reasoning as the Python branch above.
285
+ if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
286
+ GATE_6_REASON_BY_LANG["$CURRENT_LANG"]="architecture_not_configured"
287
+ fi
288
+ fi
289
+ ;;
290
+ "java")
291
+ # #602: this branch used to be a guaranteed PASS no matter what happened.
292
+ # 1) The test-dir probe hardcoded `src/test/java/architecture` at the
293
+ # repo root. A multi-module Maven repo (core/ + demo/) puts the arch
294
+ # tests under <module>/src/test/java/..., so the probe missed them
295
+ # and the gate took the SKIP path -- reporting architecture as
296
+ # "not configured" for a repo that HAS such tests.
297
+ # 2) Even when it did run, `mvn ... | head -20` yields head's exit
298
+ # status (0) as the pipeline result, so the `||` fallback never
299
+ # fired and `echo "✅ ... completed"` printed unconditionally. A
300
+ # failing architecture test was reported as success. Verified:
301
+ # `false | head -20` exits 0; with `set -o pipefail` it exits 1.
302
+ # Fix: discover the test source roots instead of guessing one path, and
303
+ # judge the build by PIPESTATUS[0] (the pattern run_java_build_integrity
304
+ # already uses), so the verdict comes from the build, not from `head`.
305
+ _java_arch_roots=""
306
+ if [ -f "pom.xml" ] || [ -x "./mvnw" ]; then
307
+ # Any */src/test/java dir (module-relative root), including the repo
308
+ # root itself, which find prints as ./src/test/java -> root ".".
309
+ while IFS= read -r _root; do
310
+ [ -n "$_root" ] && _java_arch_roots="${_java_arch_roots} ${_root}"
311
+ done <<JAVAARCHROOTS
312
+ $(find . -path ./target -prune -o -type d -path '*/src/test/java' -print 2>/dev/null \
313
+ | sed 's|^\./||; s|/src/test/java$||; s|^src/test/java$|.|')
314
+ JAVAARCHROOTS
315
+ elif [ -f "build.gradle" ] || [ -f "build.gradle.kts" ]; then
316
+ _java_arch_roots="."
317
+ fi
318
+
319
+ # Only the roots that actually contain an *Architecture* test count.
320
+ _java_arch_hit=""
321
+ for _r in $_java_arch_roots; do
322
+ _probe_dir="$_r/src/test/java"
323
+ [ "$_r" = "." ] && _probe_dir="src/test/java"
324
+ if find "$_probe_dir" -name '*Architecture*' -type f 2>/dev/null | head -1 | grep -q .; then
325
+ _java_arch_hit="${_java_arch_hit} ${_r}"
326
+ fi
327
+ done
328
+
329
+ if [ -n "$_java_arch_hit" ]; then
330
+ echo " Running Java architecture tests (roots:$_java_arch_hit)..."
331
+ _java_arch_rc=0
332
+ if [ -f "pom.xml" ] || [ -x "./mvnw" ]; then
333
+ _mvn_bin="mvn"
334
+ [ -x "./mvnw" ] && _mvn_bin="./mvnw"
335
+ # -pl <module> -am runs only the module that holds the tests (plus
336
+ # what it needs); quoting the whole root list into one -Dtest works
337
+ # for Surefire because it accepts comma-separated patterns.
338
+ for _r in $_java_arch_hit; do
339
+ if [ "$_r" = "." ]; then
340
+ "$_mvn_bin" -q test -Dtest='*Architecture*' 2>&1 | tail -20
341
+ else
342
+ "$_mvn_bin" -q -pl "$_r" -am test -Dtest='*Architecture*' 2>&1 | tail -20
343
+ fi
344
+ _rc="${PIPESTATUS[0]}"
345
+ [ "$_rc" -ne 0 ] && _java_arch_rc="$_rc"
346
+ done
347
+ elif [ -x "./gradlew" ]; then
348
+ ./gradlew test --tests '*Architecture*' 2>&1 | tail -20
349
+ _java_arch_rc="${PIPESTATUS[0]}"
350
+ fi
351
+
352
+ if [ "$_java_arch_rc" -ne 0 ]; then
353
+ echo ""
354
+ echo "❌ BLOCKED - Java architecture test(s) failed (exit $_java_arch_rc)"
355
+ echo "Fix the layering violation(s) above before committing."
356
+ GATE_6_REASON_BY_LANG["$CURRENT_LANG"]="architecture_tests_failed"
357
+ return 1
358
+ fi
359
+ echo " ✅ Java architecture validation completed."
360
+ else
361
+ echo " ℹ️ No *Architecture* test sources found under */src/test/java - skipping"
362
+ echo " ⏭️ SKIPPED - Architecture validation (no tests)"
363
+ # #537: no test files means Java architecture was NOT measured.
364
+ if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
365
+ GATE_6_REASON_BY_LANG["$CURRENT_LANG"]="architecture_not_configured"
366
+ fi
367
+ fi
368
+ ;;
369
+ "csharp")
370
+ # #717: C# had no arm at all, so it fell through to the `*)` default
371
+ # below and was reported as "not configured for csharp" -- a language
372
+ # the gate silently could not measure, with no way to configure it
373
+ # either, because the config file was never looked for.
374
+ #
375
+ # Architecture for .NET is normally expressed as *tests* (NetArchTest,
376
+ # ArchUnitNET, or a plain xUnit project asserting layering). There is no
377
+ # universal CLI, so this arm keys off a config the project opts into,
378
+ # exactly like the Go arm's arch-go.yaml.
379
+ if [ -f "arch-csharp.yaml" ] || [ -f ".xp-gate/arch-csharp.yaml" ]; then
380
+ _cs_arch_cfg="arch-csharp.yaml"
381
+ [ -f "$_cs_arch_cfg" ] || _cs_arch_cfg=".xp-gate/arch-csharp.yaml"
382
+ # Find an architecture test project: a .csproj whose name or contents
383
+ # mention architecture tests. Guessing one fixed path is the #602
384
+ # defect, which reported "not configured" for repos that had them.
385
+ _cs_arch_proj=""
386
+ while IFS= read -r _p; do
387
+ [ -n "$_p" ] || continue
388
+ if grep -qiE "architecture|archunit|netarchtest" "$_p" 2>/dev/null; then
389
+ _cs_arch_proj="$_p"
390
+ break
391
+ fi
392
+ done <<CSARCHPROJ
393
+ $(git ls-files '*.csproj' 2>/dev/null)
394
+ CSARCHPROJ
395
+
396
+ if [ -z "$_cs_arch_proj" ]; then
397
+ echo " ℹ️ No architecture test project found - skipping C# architecture"
398
+ echo " ⏭️ SKIPPED - C# architecture validation (no arch test project)"
399
+ if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
400
+ GATE_6_REASON_BY_LANG["$CURRENT_LANG"]="architecture_not_configured"
401
+ fi
402
+ elif ! command -v dotnet >/dev/null 2>&1; then
403
+ echo " ⏭️ SKIPPED - C# architecture validation (dotnet SDK not installed)"
404
+ if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
405
+ GATE_6_REASON_BY_LANG["$CURRENT_LANG"]="tool_missing"
406
+ fi
407
+ else
408
+ echo " Running C# architecture tests ($_cs_arch_proj)..."
409
+ # Same reading as the Go/Java arms (#630/#602): the checker's own
410
+ # status decides the verdict. Piping into `tail` and printing
411
+ # "completed" unconditionally is the fail-open shape those issues
412
+ # removed; PIPESTATUS[0] is what keeps this arm from repeating it.
413
+ dotnet test "$_cs_arch_proj" --nologo 2>&1 | tail -20
414
+ _cs_arch_rc="${PIPESTATUS[0]}"
415
+ if [ "$_cs_arch_rc" -ne 0 ]; then
416
+ echo ""
417
+ echo "❌ BLOCKED - C# architecture violations ($_cs_arch_proj, exit $_cs_arch_rc)"
418
+ echo "Fix the layering violation(s) above before committing."
419
+ if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
420
+ GATE_6_REASON_BY_LANG["$CURRENT_LANG"]="architecture_tests_failed"
421
+ fi
422
+ return 1
423
+ fi
424
+ echo " ✅ C# architecture validation completed."
425
+ fi
426
+ else
427
+ echo " ℹ️ No arch-csharp.yaml found - skipping C# architecture"
428
+ echo " ⏭️ SKIPPED - C# architecture validation (no config)"
429
+ if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
430
+ GATE_6_REASON_BY_LANG["$CURRENT_LANG"]="architecture_not_configured"
431
+ fi
432
+ fi
433
+ ;;
434
+ *)
435
+ echo " ⏭️ SKIPPED - Architecture validation (not configured for $CURRENT_LANG)"
436
+ # #537: archlint ships no detector for this language. Say so in a way
437
+ # the quality report can read, instead of leaving the dimension silent.
438
+ if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
439
+ GATE_6_REASON_BY_LANG["$CURRENT_LANG"]="no_archlint_detector"
440
+ fi
441
+ ;;
442
+ esac
443
+ done # end for CURRENT_LANG (architecture)
444
+
445
+ # #537: fold the per-language reasons into the legacy scalar. Homogeneous
446
+ # reasons survive as-is; a genuine mix becomes the `mixed` sentinel, which
447
+ # classify_coverage_reason() maps to `unknown` (fail closed) rather than
448
+ # pretending one language's verdict covers all of them.
449
+ #
450
+ # `declare -A` inside a function is FUNCTION-LOCAL in bash, and the report
451
+ # is written by generate_quality_report() — a different function. So the
452
+ # assoc array is flattened into this global string, which the report parses.
453
+ # (Found by running the hook: the matrix reported tool_missing for
454
+ # typescript because the lookup saw an empty array.)
455
+ GATE_6_LANG_REASONS=""
456
+ _g6_seen=""
457
+ _g6_mixed=0
458
+ # #686: a language that archlint actually measured and passed still carries a
459
+ # reason when the repo declared layer boundaries, because the layer dimension
460
+ # itself was never measured. Only languages WITHOUT a reason get this stamp —
461
+ # an existing reason (tool missing, tests failed, ...) is more specific and
462
+ # must not be overwritten. The `-z` test is what keeps this honest: stamping
463
+ # unconditionally would erase the real reason and make every language look
464
+ # alike in the report.
465
+ if [ "${GATE_6_LAYERS_UNCHECKED:-0}" = "1" ] && [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
466
+ for _g6_lang in $PROJECT_LANGS; do
467
+ if [ -z "${GATE_6_REASON_BY_LANG[$_g6_lang]:-}" ]; then
468
+ GATE_6_REASON_BY_LANG["$_g6_lang"]="layers_config_not_enforced"
469
+ fi
470
+ done
471
+ fi
472
+ for _g6_lang in $PROJECT_LANGS; do
473
+ _g6_r=""
474
+ if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
475
+ _g6_r="${GATE_6_REASON_BY_LANG[$_g6_lang]:-}"
476
+ fi
477
+ if [ -n "$_g6_r" ]; then
478
+ GATE_6_LANG_REASONS="${GATE_6_LANG_REASONS:+$GATE_6_LANG_REASONS,}${_g6_lang}=${_g6_r}"
479
+ if [ -z "$_g6_seen" ]; then
480
+ _g6_seen="$_g6_r"
481
+ elif [ "$_g6_seen" != "$_g6_r" ]; then
482
+ _g6_mixed=1
483
+ fi
484
+ fi
485
+ done
486
+ if [ "$_g6_mixed" = "1" ]; then
487
+ GATE_6_REASON="mixed"
488
+ else
489
+ GATE_6_REASON="$_g6_seen"
490
+ fi
491
+
492
+ # #602 (status): a language that was really measured and passed leaves NO skip
493
+ # reason behind (see the #537 comment in the typescript arm: "archlint
494
+ # actually ran here, so there is no skip reason"). The verdict was never
495
+ # upgraded to match, so Gate 6 reported SKIP even when every language in
496
+ # the project had been validated and passed -- a measured dimension
497
+ # rendered as unmeasured. That is the mirror image of the bug #537 fixed,
498
+ # and the report could not tell the two apart.
499
+ #
500
+ # Upgrade to PASS only when the measurement actually happened for EVERY
501
+ # language: no language may carry a skip reason. A single skipped language
502
+ # keeps the gate non-PASS, so "some languages measured" cannot be scored
503
+ # as a full success.
504
+ if [ -z "$GATE_6_LANG_REASONS" ] && [ -n "$PROJECT_LANGS" ]; then
505
+ GATE_6_STATUS="PASS"
506
+ fi
507
+ else
508
+ # Architecture config is REQUIRED — BLOCK if missing
509
+ if [ -f "$PROJECT_ROOT/.architecture-skip" ]; then
510
+ echo " ⚠️ Architecture config missing but .architecture-skip present — allowed to skip"
511
+ echo " ⏭️ SKIPPED - Architecture validation (.architecture-skip exemption)"
512
+ # #537: an explicit exemption is "not applicable", not "tool missing".
513
+ GATE_6_REASON="architecture_skip_exemption"
514
+ else
515
+ echo ""
516
+ echo "❌ ARCHITECTURE CONFIG MISSING - COMMIT BLOCKED"
517
+ echo " Required configuration file (.archlint.yaml / architecture.yaml) is NOT found."
518
+ echo " Architecture Quality Gate requires explicit architecture constraints."
519
+ echo ""
520
+ echo " Quick fix: create .archlint.yaml with: npx archlint init --no-interactive"
521
+ echo " Or write one by hand — this is archlint's own config format, and the"
522
+ echo " only one it actually reads:"
523
+ echo " rules:"
524
+ echo " cyclic_dependency:"
525
+ echo " severity: high"
526
+ echo " god_module:"
527
+ echo " severity: high"
528
+ echo ""
529
+ # #686: this used to recommend a top-level `layers:` block. That format is
530
+ # silently ignored by @archlinter/cli 0.16.0 (verified against a fixture
531
+ # with a textbook domain->infra violation: the layer_violation detector is
532
+ # listed by `archlint detectors list` but produces no results under any
533
+ # configuration, including -A and an explicit -d layer_violation). The hint
534
+ # therefore taught users to write a file that bought them nothing while
535
+ # Gate 6 reported green — the worst possible combination.
536
+ echo " NOTE: layer-boundary rules are NOT enforced by archlint 0.16.0."
537
+ echo " Do not write a top-level 'layers:' block expecting it to be checked;"
538
+ echo " Gate 6 will tell you when that changes. See #686 / #690 / #692."
539
+ echo ""
540
+ echo " After creating the config, retry the commit."
541
+ echo " (To skip with warning, create .architecture-skip file)"
542
+ return 1
543
+ fi
544
+ fi
545
+ fi
546
+
547
+ # Second Part: Boy Scout Rule (previously Gate 8) - Unified Enforcement
548
+ 2>&1 echo " └─ Boy Scout Rule enforcement:"
549
+
550
+ if [ "$PROJECT_LANG" = "documentation-only" ]; then
551
+ echo " ✅ Skipped (documentation project)."
552
+
553
+ else
554
+ # Check for Boy Scout script in installed modules first, then project src/
555
+ BOY_SCOUT_SCRIPT=""
556
+ if [ -f ".xp-gate/modules/principles/boy-scout.ts" ]; then
557
+ BOY_SCOUT_SCRIPT=".xp-gate/modules/principles/boy-scout.ts"
558
+ elif [ -f "src/principles/boy-scout.ts" ]; then
559
+ BOY_SCOUT_SCRIPT="src/principles/boy-scout.ts"
560
+ fi
561
+
562
+ if [ -n "$BOY_SCOUT_SCRIPT" ]; then
563
+ echo " Checking Boy Scout Rule compliance..."
564
+
565
+ # Separate new files and modified files.
566
+ #
567
+ # src/npm-package/** is a byte-identical MIRROR of canonical sources
568
+ # (plugins/**, src/principles/**, githooks/**) produced by
569
+ # sync-package-content.js at publish time. Checking it here double-counts
570
+ # every warning: the mirror path enters the baseline instead of the source
571
+ # path, so a violation gets recorded against a path nobody edits and the
572
+ # real source path stays unbudgeted. Exclude it and let the mirroring be
573
+ # governed by Mirror Parity in CI, which compares content rather than
574
+ # re-linting it. (See the same exclusion in adapters/powershell.sh.)
575
+ MIRROR_EXCLUDE="$MIRROR_PATH_FILTER"
576
+ # filter_project_paths also removes dependencies and build output (#649):
577
+ # a staged node_modules/ or dist/ file is not budgetable project code, and
578
+ # entering it in the Boy Scout baseline would budget a path nobody edits.
579
+ NEW_FILES=$(git diff --cached --name-only --diff-filter=A | filter_project_paths | grep -v "$MIRROR_EXCLUDE" | grep -v "$DASHBOARD_PATH_FILTER" | tr '\n' ' ')
580
+ MODIFIED_FILES=$(git diff --cached --name-only --diff-filter=M | filter_project_paths | grep -v "$MIRROR_EXCLUDE" | grep -v "$DASHBOARD_PATH_FILTER" | tr '\n' ' ')
581
+
582
+ if [ -z "$NEW_FILES" ] && [ -z "$MODIFIED_FILES" ]; then
583
+ echo " ✅ No new or modified files to check."
584
+ else
585
+ # Run Boy Scout Rule enforcement
586
+ if command -v npx &> /dev/null || [ -f "${PROJECT_ROOT:-$(pwd)}/node_modules/tsx/dist/cli.mjs" ]; then
587
+ # Skip if no files to check
588
+ if [ -z "$(echo "$NEW_FILES" | tr -d ' ')" ] && [ -z "$(echo "$MODIFIED_FILES" | tr -d ' ')" ]; then
589
+ echo " ✅ No new or modified source files for Boy Scout check."
590
+ else
591
+ BOY_SCOUT_OUTPUT=$(run_tsx "$BOY_SCOUT_SCRIPT" \
592
+ --new-files "$(echo "$NEW_FILES" | tr ' ' ',')" \
593
+ --modified-files "$(echo "$MODIFIED_FILES" | tr ' ' ',')" \
594
+ --baseline ".warnings-baseline.json" 2>&1)
595
+
596
+ BOY_SCOUT_EXIT=$?
597
+
598
+ if [ "$BOY_SCOUT_EXIT" -ne 0 ]; then
599
+ echo "$BOY_SCOUT_OUTPUT"
600
+ echo ""
601
+ echo "❌ BLOCKED - Boy Scout Rule violation"
602
+ echo "Requirements:"
603
+ echo " - NEW files: must have zero warnings"
604
+ echo " - MODIFIED files: cannot increase warnings from baseline"
605
+ echo " - Files with ≤5 warnings: must clear to zero"
606
+ return 1
607
+ fi
608
+
609
+ # Check output for violations
610
+ VIOLATION_COUNT=$(echo "$BOY_SCOUT_OUTPUT" | grep -c '"enforcement": "BLOCK"' 2>/dev/null || true)
611
+ VIOLATION_COUNT=${VIOLATION_COUNT:-0}
612
+ if [ "$VIOLATION_COUNT" -gt 0 ]; then
613
+ echo "$BOY_SCOUT_OUTPUT"
614
+ echo ""
615
+ echo "❌ BLOCKED - Boy Scout Rule violations detected ($VIOLATION_COUNT)"
616
+ return 1
617
+ fi
618
+
619
+ # REQ-4 (#452): the auto-init rewrites a committed file, which used to
620
+ # surface as an unexplained dirty ` M .warnings-baseline.json` after
621
+ # every commit that touched a first-time-modified file. Stage it so the
622
+ # budget update travels with the commit that caused it; when the
623
+ # baseline is unchanged (the common case) this is a no-op.
624
+ if [ -n "$(git status --porcelain -- .warnings-baseline.json 2>/dev/null)" ]; then
625
+ git add .warnings-baseline.json 2>/dev/null || true
626
+ echo " ℹ️ .warnings-baseline.json auto-updated and staged with this commit."
627
+ fi
628
+
629
+ echo " ✅ PASSED - Boy Scout Rule compliance."
630
+ fi
631
+ else
632
+ echo " ℹ️ npx/tsx not available - skipping Boy Scout Rule"
633
+ echo " ⏭️ SKIPPED - Boy Scout Rule (Node.js/tsx not available)"
634
+ fi
635
+ fi
636
+ else
637
+ echo " ℹ️ Boy scout rule not available in project - skipping"
638
+ echo " ⏭️ SKIPPED - Boy Scout Rule (not available in project)"
639
+ fi
640
+ fi
641
+ # A PASS here must mean 'the checks ran and found nothing'. The two
642
+ # branches above print 'SKIPPED - not configured/available'; stamping PASS
643
+ # after them turned 'nothing was checked' into a scored success.
644
+ if [ -n "${GATE_6_STATUS:-}" ]; then
645
+ : # a branch that really ran already decided the verdict
646
+ else
647
+ GATE_6_STATUS="SKIP"
648
+ fi
649
+ record_gate_audit "gate-6" "architecture-boy-scout" "$GATE_6_STATUS" "${BS_BLOCKED:-0}" "$GATE_6_START"
650
+
651
+ # ============================================================================
652
+ return 0
653
+ }
654
+
655
+
656
+
657
+ # Called by pre-commit's ordered driver.
658
+