@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,825 @@
1
+ #!/usr/bin/env bash
2
+ # ============================================================================
3
+ # gate-0 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 0: Version Consistency Check (Protected Branches)
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_0_run() {
27
+ GATE_0_START=$(gate_start_ms)
28
+ GATE_0_STATUS="PASS"
29
+
30
+ CURRENT_BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --abbrev-ref HEAD 2>/dev/null) || CURRENT_BRANCH=""
31
+ CURRENT_BRANCH=$(printf '%s' "$CURRENT_BRANCH" | tr -d '\r\n'); [ -n "$CURRENT_BRANCH" ] || CURRENT_BRANCH="HEAD"
32
+ PROTECTED_BRANCHES="master develop trunk mainline"
33
+ # #711: same shape as the branch/commit captures above — `git rev-parse` can
34
+ # print a partial line before failing, so `|| echo "."` can yield "<something>\n."
35
+ # and every later `cd "$ROOT_DIR"` / path join would silently use a two-line value.
36
+ ROOT_DIR=$(git rev-parse --show-toplevel 2>/dev/null) || ROOT_DIR=""
37
+ ROOT_DIR=$(printf '%s' "$ROOT_DIR" | tr -d '\r\n'); [ -n "$ROOT_DIR" ] || ROOT_DIR="."
38
+
39
+ # Optional: project-level .protected-branches config file
40
+ if [ -f "$ROOT_DIR/.protected-branches" ]; then
41
+ PROTECTED_BRANCHES="$PROTECTED_BRANCHES $(cat "$ROOT_DIR/.protected-branches" | tr '\n' ' ')"
42
+ fi
43
+
44
+ is_protected=false
45
+ for branch in $PROTECTED_BRANCHES; do
46
+ if [ "$CURRENT_BRANCH" = "$branch" ]; then
47
+ is_protected=true
48
+ break
49
+ fi
50
+ done
51
+
52
+ # A2 guard: narrowing the gate set is a local iteration tool, never a way to
53
+ # commit onto a protected branch with fewer checks. Same reasoning as
54
+ # SKIP_GATE_5A_BLOCK's main/master refusal further down, and refused outright
55
+ # rather than silently ignored so a scripted `XP_GATE_ONLY_GATE=1 git commit` on
56
+ # main cannot quietly appear to have run the full chain.
57
+ if gate_selection_active && [ "$is_protected" = true ]; then
58
+ echo ""
59
+ echo "❌ BLOCKED - XP_GATE_ONLY_GATE=${XP_GATE_ONLY_GATE} is not allowed on protected branch '$CURRENT_BRANCH'."
60
+ echo " Run the full gate chain here: unset XP_GATE_ONLY_GATE."
61
+ return 1
62
+ fi
63
+
64
+ if gate_selection_active; then
65
+ echo ""
66
+ echo "⚠️ PARTIAL GATE RUN — XP_GATE_ONLY_GATE=${XP_GATE_ONLY_GATE} (branch: $CURRENT_BRANCH)"
67
+ echo " Gates not named are recorded as SKIP/not_selected, never as PASS."
68
+ echo " This commit has NOT been checked against the gates it skips."
69
+ fi
70
+
71
+ has_version_change=false
72
+ has_changelog_change=false
73
+
74
+ # #640: a fork costs ~90ms on Windows (measured: 100 `git rev-parse` = 9456ms),
75
+ # and this exact query is re-run by later gates. Cache it once; consumers below
76
+ # read $XP_STAGED_ACM. Subprocess COUNT, not scan duration, is the bottleneck.
77
+ #
78
+ # Computed HERE, once, and outside the protected-branch test below. It used to
79
+ # live inside that test, which meant the variable was only ever assigned when
80
+ # committing to master/main/develop/trunk/mainline. On a feature branch -- where
81
+ # essentially all work happens, and where CI fixtures commit -- it stayed EMPTY,
82
+ # and every #640 consumer that asks "did this commit touch a source file?" then
83
+ # matched nothing:
84
+ # - Gate 6 skipped archlint on every feature-branch commit that DID change
85
+ # source ("no source files changed"), silently dropping the architecture
86
+ # check;
87
+ # - Gate 0's own hint fired as though nothing were staged.
88
+ # A cache that is only warm on some branches is not a cache, it is a branch-wide
89
+ # behaviour difference. Assign it before anything can branch.
90
+ STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)
91
+ XP_STAGED_ACM="$STAGED_FILES"
92
+ export XP_STAGED_ACM
93
+
94
+ if [ "$is_protected" = "true" ]; then
95
+ # Env var bypass: SKIP_VERSION_CHECK=1 skips Gate 0 entirely
96
+ # Useful for: SKIP_VERSION_CHECK=1 git commit -m "chore: ..."
97
+ if ! gate_enabled 0; then
98
+ echo "⏭️ SKIPPED - Gate 0: Version consistency (not selected by XP_GATE_ONLY_GATE)"
99
+ GATE_0_STATUS="SKIP"
100
+ GATE_0_DETAIL="not_selected"
101
+ elif [ "${SKIP_VERSION_CHECK:-}" = "1" ]; then
102
+ echo "⏭️ SKIPPED - Gate 0: Version consistency (SKIP_VERSION_CHECK=1 env var)"
103
+ else
104
+ for file in $STAGED_FILES; do
105
+ case "$file" in
106
+ VERSION) has_version_change=true ;;
107
+ CHANGELOG.md) has_changelog_change=true ;;
108
+ esac
109
+ done
110
+
111
+ # If either VERSION or CHANGELOG.md is staged, pass
112
+ if [ "$has_version_change" = "true" ] || [ "$has_changelog_change" = "true" ]; then
113
+ # Auto-run sync-version when VERSION is staged (issue #206 follow-up).
114
+ # Propagates VERSION → 4 package.json + refreshes AGENTS.md headers,
115
+ # then stages only files sync-version is known to touch (NOT runtime
116
+ # journals or unrelated edits) so the commit captures it atomically.
117
+ # Prefer Node.js (.cjs) for cross-platform; fall back to bash (.sh).
118
+ SYNC_VERSION_CMD=""
119
+ if [ -f "$ROOT_DIR/scripts/sync-version.cjs" ] && command -v node >/dev/null 2>&1; then
120
+ SYNC_VERSION_CMD="node $ROOT_DIR/scripts/sync-version.cjs"
121
+ elif [ -f "$ROOT_DIR/scripts/sync-version.sh" ]; then
122
+ SYNC_VERSION_CMD="bash $ROOT_DIR/scripts/sync-version.sh"
123
+ fi
124
+ if [ "$has_version_change" = "true" ] && [ -n "$SYNC_VERSION_CMD" ]; then
125
+ echo "🔄 Gate 0: VERSION staged — running sync-version to fan out to package.json + AGENTS.md headers..."
126
+ if $SYNC_VERSION_CMD > /tmp/xp-gate-sync-version.log 2>&1; then
127
+ SYNC_STAGED=0
128
+ # Stage the 4 known package.json targets if modified
129
+ for pkg in package.json src/npm-package/package.json \
130
+ plugins/claude-code/.claude-plugin/plugin.json \
131
+ plugins/opencode/package.json \
132
+ src/npm-package/plugins/claude-code/.claude-plugin/plugin.json \
133
+ src/npm-package/plugins/opencode/package.json; do
134
+ if [ -f "$ROOT_DIR/$pkg" ] && ! git diff --quiet -- "$ROOT_DIR/$pkg" 2>/dev/null; then
135
+ git add "$ROOT_DIR/$pkg"
136
+ SYNC_STAGED=$((SYNC_STAGED + 1))
137
+ fi
138
+ done
139
+ # Stage AGENTS.md files with header-only modifications (header refresh is the only thing sync-version touches in AGENTS.md)
140
+ while IFS= read -r -d '' agents_file; do
141
+ if ! git diff --quiet -- "$agents_file" 2>/dev/null; then
142
+ git add "$agents_file"
143
+ SYNC_STAGED=$((SYNC_STAGED + 1))
144
+ fi
145
+ done < <(find "$ROOT_DIR" -name 'AGENTS.md' -not -path '*/node_modules/*' -not -path '*/.git/*' -print0)
146
+ echo " auto-staged $SYNC_STAGED file(s) from sync-version (package.json × ≤6 + AGENTS.md headers)"
147
+ else
148
+ echo "⚠️ sync-version failed; see /tmp/xp-gate-sync-version.log — continuing without auto-sync"
149
+ fi
150
+ fi
151
+ echo "✅ PASSED - Gate 0: Version Consistency Check (VERSION/CHANGELOG staged)"
152
+ else
153
+ # Check for doc-only changes (no source code files)
154
+ has_source=false
155
+ for file in $STAGED_FILES; do
156
+ case "$file" in
157
+ *.ts|*.js|*.tsx|*.jsx|*.py|*.go|*.java|*.rs|*.c|*.cpp|*.h|*.css|*.scss)
158
+ has_source=true
159
+ ;;
160
+ esac
161
+ done
162
+
163
+ # Check for build-tooling-only changes (adapters, scripts, hooks)
164
+ has_real_source=false
165
+ for file in $STAGED_FILES; do
166
+ case "$file" in
167
+ src/npm-package/adapters/*|src/npm-package/scripts/*|src/npm-package/hooks/*|scripts/*|githooks/adapters/*)
168
+ continue ;;
169
+ *.ts|*.js|*.tsx|*.jsx|*.py|*.go|*.java|*.rs|*.c|*.cpp|*.h|*.css|*.scss)
170
+ has_real_source=true
171
+ ;;
172
+ esac
173
+ done
174
+
175
+ # Doc-only / build-tooling-only changes need no VERSION bump.
176
+ #
177
+ # #624 defect 2: the split used to test ONLY `has_source`, which is true for
178
+ # any .py/.ts/... path INCLUDING `scripts/*`. So a commit that touched
179
+ # nothing but build tooling still fell into the version-check branch and was
180
+ # blocked. `has_real_source` is exactly "production source that needs a bump"
181
+ # — it was computed a few lines up but never consulted here. It must decide.
182
+ if [ "$has_source" = "false" ]; then
183
+ echo "✅ PASSED - Gate 0: Version Consistency Check (doc-only changes)"
184
+ elif [ "$has_real_source" = "false" ]; then
185
+ echo "✅ PASSED - Gate 0: Version Consistency Check (build-tooling-only changes)"
186
+ else
187
+ # Production source on a protected branch without VERSION/CHANGELOG.
188
+ #
189
+ # #624 defect 1: this branch used to offer a `[skip-version-check]` message
190
+ # prefix as a bypass, read from `.git/COMMIT_EDITMSG`. That cannot work:
191
+ # git runs pre-commit BEFORE it builds the message, so the file does not
192
+ # exist yet (`-m`, `-F` and the interactive editor path all measured on
193
+ # git 2.54), and the hook gets no arguments. What the old code read was the
194
+ # PREVIOUS commit's message, so the prefix never matched — the documented
195
+ # recipe silently could not clear the gate, and --no-verify was the only
196
+ # way through. Rather than keep a bypass whose input this hook structurally
197
+ # cannot see, the escape hatch is the one that actually works and is
198
+ # already documented in CHANGELOG: the SKIP_VERSION_CHECK=1 environment
199
+ # variable (handled earlier in this Gate 0 block).
200
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
201
+ VERSION CONSISTENCY CHECK (Gate 0)
202
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
203
+ Branch: $CURRENT_BRANCH
204
+ ERROR: Committing production source to a protected branch without a
205
+ VERSION/CHANGELOG update.
206
+
207
+ This bypasses the ship workflow and causes version drift.
208
+ Before committing:
209
+ 1. Update VERSION file with new version
210
+ 2. Add entry to CHANGELOG.md
211
+ 3. Run: node scripts/sync-version.cjs
212
+
213
+ Build-tooling-only changes (adapters/, scripts/, hooks/) need NO version bump
214
+ and pass this gate automatically — no marker required.
215
+
216
+ If you genuinely must land production source without a version bump, the
217
+ supported escape hatch is the env var (a message prefix cannot work here
218
+ because pre-commit runs before git builds the message):
219
+ SKIP_VERSION_CHECK=1 git commit ...
220
+
221
+ Staged files: $(echo "$STAGED_FILES" | tr '\n' ', ' | sed 's/,$//')
222
+ "
223
+ GATE_0_STATUS="BLOCK"
224
+ record_gate_audit "gate-0" "version-consistency" "$GATE_0_STATUS" "1" "$GATE_0_START"
225
+ return 1
226
+ fi
227
+ fi
228
+ fi # closes SKIP_VERSION_CHECK if/else
229
+ else
230
+ echo "✅ PASSED - Gate 0: Version Consistency Check (non-protected branch: $CURRENT_BRANCH)"
231
+ fi
232
+
233
+ record_gate_audit "gate-0" "version-consistency" "$GATE_0_STATUS" "0" "$GATE_0_START"
234
+
235
+ echo ""
236
+
237
+ # Get list of changed files.
238
+ #
239
+ # Byte-identical mirrors (src/npm-package/**) are governed by CI Mirror Parity,
240
+ # not by re-linting. Leaving them in this scan set double-counted every
241
+ # lint/complexity signal and made jscpd report the source-vs-mirror pair as a
242
+ # 100% whole-file clone, which then tripped Gate 6's "<=5 warnings must clear
243
+ # to zero" rule on ANY edit to a mirrored file -- a warning that is
244
+ # structurally impossible to clear. (Gate 6's MIRROR_EXCLUDE and #494's test
245
+ # selection already exclude mirrors for the same reason.)
246
+ #
247
+ # IMPORTANT: the exclusion applies to the LINT SCAN CORPUS only. The early
248
+ # exit below must key on the RAW staged list -- Gate 8 (secret scanning) is
249
+ # unconditional and fail-closed (#499), and gating it behind a path-filtered
250
+ # list would let a mirror-only commit land with zero secret scanning and no
251
+ # CI backstop.
252
+ # Single source of truth for the mirror-path filter (#510): every consumer
253
+ # that must ignore the byte-identical npm-package mirrors (lint corpus here,
254
+ # Boy Scout's new/modified split further down) reads this one regex, so the
255
+ # two sites cannot drift apart.
256
+ # dashboard/ is a standalone browser-only visualization page (document/Chart.js
257
+ # globals, static HTML output). It is not part of the gate engine, never runs
258
+ # in CI or hooks, and its chart-config object literals trip structural rules
259
+ # (deep-nesting/long-function) that carry no signal for this codebase. Same
260
+ # scope rationale as the mirror exclusion (#507 Delphi round-2 follow-up).
261
+ MIRROR_PATH_FILTER='^src/npm-package/'
262
+ DASHBOARD_PATH_FILTER='^dashboard/'
263
+ RAW_CHANGED_FILES="${XP_STAGED_ACM:-$(git diff --cached --name-only --diff-filter=ACM)}"
264
+
265
+ # Dependencies and build output are not project code (#649).
266
+ #
267
+ # `git diff --cached` knows nothing about .gitignore: anything that reaches the
268
+ # index is listed. A project that vendors its dependencies, a CI fixture that
269
+ # commits after `npm install`, an accidental `git add -A`, or a monorepo's
270
+ # nested `packages/*/node_modules` therefore hands every downstream scanner a
271
+ # list containing third-party source, and each scanner judges it as the
272
+ # project's own.
273
+ #
274
+ # This has already cost two outages, one language branch at a time: #595 (Gate 1
275
+ # PowerShell arm blocked every commit once Playwright's node_modules was
276
+ # scanned for Write-Host) and #648 (Gate 1 JS/TS arm blocked on the bare
277
+ # `debugger;` in node_modules/typescript/dist/internal/utils.js). Both were
278
+ # fixed at the single site that happened to fail, so the next branch re-broke
279
+ # the same way. The filter belongs HERE, at the one place the list is built, so
280
+ # a scanner added tomorrow is safe by default instead of by memory.
281
+ #
282
+ # The exclusion itself is the shared build-output list from jscpd-run.sh
283
+ # (jscpd_is_build_output), so the directory names have one definition across the
284
+ # gates. Deliberately NOT the full Gate 2 ignore set: that one also drops
285
+ # *.yaml/*.yml/*.txt/*.log because repetition there is data shape rather than a
286
+ # refactoring target, and using it here would make a commit that only adds
287
+ # infra/deployment.yaml look like "No code files changed" -- skipping the IaC
288
+ # and lint gates that exist to scan exactly that file. The XP_GATE_JSCPD_DEFAULT_IGNORE=0
289
+ # escape hatch still applies.
290
+ #
291
+ # RAW_CHANGED_FILES stays unfiltered on purpose: it is the only truthful answer
292
+ # to "did the user stage anything at all", and a future "you accidentally
293
+ # committed dependencies" warning needs to see what this filter removed.
294
+ CHANGED_FILES=$(printf '%s\n' "$RAW_CHANGED_FILES" \
295
+ | grep -v "$MIRROR_PATH_FILTER" | grep -v "$DASHBOARD_PATH_FILTER" \
296
+ | {
297
+ while IFS= read -r _cf; do
298
+ [ -n "$_cf" ] || continue
299
+ jscpd_is_build_output "$_cf" || printf '%s\n' "$_cf"
300
+ done
301
+ } || true)
302
+
303
+ # REQ-F2: one commit-level docs-only verdict, computed once and reused by the
304
+ # heavy gates (2/5/9/12). Light gates never consult it — a secret in a README
305
+ # is still a secret.
306
+ DOCS_ONLY_COMMIT=false
307
+ if printf '%s\n' "$CHANGED_FILES" | detect_docs_only_commit | grep -q '^true$'; then
308
+ DOCS_ONLY_COMMIT=true
309
+ fi
310
+
311
+ if [ -z "$RAW_CHANGED_FILES" ]; then
312
+ echo "No files changed. Skipping gates."
313
+ return 0
314
+ fi
315
+
316
+ # Gate 5 test selection (#494): vitest.config.ts excludes the bundled mirror
317
+ # subtrees and the skills/plugins source trees, so a path under one of them can
318
+ # never be collected. When such a path is the only target, vitest exits 1 with
319
+ # "No test files found" and Gate 5 reports a failing test run -- a commit that
320
+ # touched nothing but a byte-identical mirror cannot land. Map each mirror path
321
+ # to the canonical twin it copies, drop what has no twin, collapse duplicates.
322
+ select_root_suite_targets() {
323
+ # Deliberately not `s|...|...|`: the alternation below contains `|`, which sed
324
+ # would read as the end of its pattern ("unknown option to `s'").
325
+ printf '%s\n' "$1" \
326
+ | sed -E 's#^src/npm-package/(principles|mutation|mock-policy|build-integrity)/#src/\1/#' \
327
+ | grep -vE '^(src/_wip/|plugins/|skills/|src/npm-package/(plugins|skills|principles|mutation|mock-policy|build-integrity)/)' \
328
+ | sed '/^$/d' \
329
+ | sort -u
330
+ }
331
+
332
+ any_changed_files_match() {
333
+ local pattern
334
+
335
+ for pattern in "$@"; do
336
+ # Here-string, not `printf | grep -q`. Under the `set -o pipefail` above,
337
+ # `grep -q` exits the moment it matches, the writer takes SIGPIPE, and the
338
+ # pipeline reports 141 -- so the `if` reads "no match" even though grep DID
339
+ # match. Whether that happens depends on whether the list fits in the pipe
340
+ # buffer, which is why it showed up as a size-dependent, flaky SKIP.
341
+ #
342
+ # It was not theoretical: in the Cross-Platform CI first-commit fixture the
343
+ # staged list is long enough to trip it, and Gate 1 announced "No code files
344
+ # changed, skipping lint" for a commit that had staged 19 shell scripts --
345
+ # while Gate 2, reading the same variable, happily scanned them and reported
346
+ # duplicates. The hook log carried the fingerprint the whole time:
347
+ # `line 847: printf: Broken pipe`.
348
+ #
349
+ # A here-string has no writer process to be killed, so the match cannot be
350
+ # lost. Same reason `run_tsx` and the coverage probes avoid `| head`.
351
+ if grep -qE -- "$pattern" <<< "$CHANGED_FILES"; then
352
+ return 0
353
+ fi
354
+ done
355
+ return 1
356
+ }
357
+
358
+ # ============================================================================
359
+ # Multi-language detection (v0.15.0+): detect all languages from changed file
360
+ # extensions, with fallback to manifest-based detection.
361
+ # Result: PROJECT_LANGS (space-separated string), PROJECT_LANG (first language for
362
+ # compatibility), PROJECT_SUBDIR (derived from changed file paths in monorepos;
363
+ # v0.15.1+; ARCH-03 fix), PROJECT_SUBDIRS (per-language subdir override, empty = root).
364
+ # ============================================================================
365
+
366
+ # Language override resolution (project-level > git config > env var)
367
+ # Priority: .xp-gate-lang file > git config xp-gate.lang > XP_GATE_LANG env
368
+ _RESOLVED_LANG_OVERRIDE=""
369
+ if [ -f ".xp-gate-lang" ]; then
370
+ _RESOLVED_LANG_OVERRIDE=$(sed -n '1p' .xp-gate-lang 2>/dev/null | tr -d '[:space:]')
371
+ elif git config xp-gate.lang >/dev/null 2>&1; then
372
+ _RESOLVED_LANG_OVERRIDE=$(git config xp-gate.lang 2>/dev/null)
373
+ elif [ -n "${XP_GATE_LANG:-}" ]; then
374
+ _RESOLVED_LANG_OVERRIDE="$XP_GATE_LANG"
375
+ echo "⚠️ XP_GATE_LANG env var is deprecated for multi-project use."
376
+ echo " Prefer: echo '$XP_GATE_LANG' > .xp-gate-lang OR git config xp-gate.lang $XP_GATE_LANG"
377
+ fi
378
+
379
+ if [ -n "$_RESOLVED_LANG_OVERRIDE" ]; then
380
+ PROJECT_LANGS="$_RESOLVED_LANG_OVERRIDE"
381
+ PROJECT_LANG="$_RESOLVED_LANG_OVERRIDE"
382
+ echo ""
383
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
384
+ echo " 🔧 LANGUAGE OVERRIDE: $PROJECT_LANG"
385
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
386
+ echo ""
387
+ elif [ -f ".xp-gate-config.json" ]; then
388
+ # ---- Read from project config file (v0.15.2+) ----
389
+ # .xp-gate-config.json is generated by `xp-gate init` or `xp-gate detect-languages`
390
+ # It contains detected languages and their tool status.
391
+ _CONFIG_LANGS=$(node -e "try{const c=JSON.parse(require('fs').readFileSync('.xp-gate-config.json','utf8'));console.log(Object.keys(c.languages||{}).join(' '));}catch(e){console.log('');}" 2>/dev/null || echo "")
392
+ if [ -n "$_CONFIG_LANGS" ]; then
393
+ PROJECT_LANGS="$_CONFIG_LANGS"
394
+ PROJECT_LANG=$(echo "$_CONFIG_LANGS" | awk '{print $1}')
395
+ echo ""
396
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
397
+ echo " 📋 LANGUAGES FROM .xp-gate-config.json"
398
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
399
+ echo ""
400
+ echo "Detected languages: $PROJECT_LANGS"
401
+ echo ""
402
+ else
403
+ # Config file exists but no languages detected — fall through to extension detection
404
+ _CONFIG_LANGS=""
405
+ fi
406
+ fi
407
+
408
+ # If no languages from override or config, detect from changed files
409
+ if [ -z "$PROJECT_LANGS" ] && [ -z "$_CONFIG_LANGS" ]; then
410
+ # ---- Extension → language mapping ----
411
+ # Extracts unique languages from changed file extensions
412
+ PROJECT_LANGS=""
413
+ _detect_ext_lang() {
414
+ for ext in "$@"; do
415
+ if echo "$CHANGED_FILES" | grep -qE "\.${ext}$" 2>/dev/null; then
416
+ echo "1"
417
+ return 0
418
+ fi
419
+ done
420
+ echo "0"
421
+ return 1
422
+ }
423
+
424
+ # Check changed files for each language by extension
425
+ if [ "$(_detect_ext_lang "ts" "tsx" "js" "jsx" "mjs")" = "1" ]; then
426
+ PROJECT_LANGS="$PROJECT_LANGS typescript"
427
+ fi
428
+ if [ "$(_detect_ext_lang "py")" = "1" ]; then
429
+ PROJECT_LANGS="$PROJECT_LANGS python"
430
+ fi
431
+ if [ "$(_detect_ext_lang "go")" = "1" ]; then
432
+ PROJECT_LANGS="$PROJECT_LANGS go"
433
+ fi
434
+ if [ "$(_detect_ext_lang "java")" = "1" ]; then
435
+ PROJECT_LANGS="$PROJECT_LANGS java"
436
+ fi
437
+ if [ "$(_detect_ext_lang "kt" "kts")" = "1" ]; then
438
+ PROJECT_LANGS="$PROJECT_LANGS kotlin"
439
+ fi
440
+ if [ "$(_detect_ext_lang "dart")" = "1" ]; then
441
+ # Check for Flutter: if pubspec.yaml exists and contains flutter
442
+ if [ -f "pubspec.yaml" ] && grep -q "flutter:" pubspec.yaml 2>/dev/null; then
443
+ PROJECT_LANGS="$PROJECT_LANGS flutter"
444
+ else
445
+ PROJECT_LANGS="$PROJECT_LANGS dart"
446
+ fi
447
+ fi
448
+ if [ "$(_detect_ext_lang "swift")" = "1" ]; then
449
+ PROJECT_LANGS="$PROJECT_LANGS swift"
450
+ fi
451
+ if [ "$(_detect_ext_lang "m" "mm")" = "1" ]; then
452
+ PROJECT_LANGS="$PROJECT_LANGS objectivec"
453
+ fi
454
+ if [ "$(_detect_ext_lang "cpp" "cxx" "cc" "c" "hpp" "h")" = "1" ]; then
455
+ PROJECT_LANGS="$PROJECT_LANGS cpp"
456
+ fi
457
+ if [ "$(_detect_ext_lang "sh")" = "1" ]; then
458
+ PROJECT_LANGS="$PROJECT_LANGS shell"
459
+ fi
460
+ if [ "$(_detect_ext_lang "ps1" "psm1")" = "1" ]; then
461
+ PROJECT_LANGS="$PROJECT_LANGS powershell"
462
+ fi
463
+ # #717: without this arm a staged .cs file never produced a `csharp` entry in
464
+ # PROJECT_LANGS, so the per-language loop at the adapter-routing site had no
465
+ # turn in which to resolve adapters/csharp.sh -- the adapter was unreachable
466
+ # no matter how correct it was. Detection on extensions (not manifests) is why
467
+ # one arm is enough: a repo staging .cs always lands here.
468
+ if [ "$(_detect_ext_lang "cs" "csx")" = "1" ]; then
469
+ PROJECT_LANGS="$PROJECT_LANGS csharp"
470
+ fi
471
+ # IaC: .tf, .tfvars, Dockerfile, docker-compose*, .yaml/.yml in kube context
472
+ if echo "$CHANGED_FILES" | grep -qE '(\.tf$|\.tfvars$|Dockerfile|docker-compose)' 2>/dev/null; then
473
+ PROJECT_LANGS="$PROJECT_LANGS iac"
474
+ fi
475
+
476
+ # Trim leading space
477
+ PROJECT_LANGS="${PROJECT_LANGS# }"
478
+
479
+ # ---- Fallback: manifest-based detection when no extension matches ----
480
+ if [ -z "$PROJECT_LANGS" ]; then
481
+ if [ -f "package.json" ]; then
482
+ HAS_RN=$(grep -q "react-native" package.json 2>/dev/null && echo "yes" || echo "no")
483
+ HAS_IOS=$([ -d "ios" ] && echo "yes" || echo "no")
484
+ HAS_ANDROID=$([ -d "android" ] && echo "yes" || echo "no")
485
+ if [ "$HAS_RN" = "yes" ] || { [ "$HAS_IOS" = "yes" ] && [ "$HAS_ANDROID" = "yes" ]; }; then
486
+ PROJECT_LANGS="react-native"
487
+ else
488
+ PROJECT_LANGS="typescript"
489
+ fi
490
+ elif [ -f "pyproject.toml" ] || [ -f "setup.py" ] || [ -f "requirements.txt" ]; then
491
+ PROJECT_LANGS="python"
492
+ elif [ -f "go.mod" ]; then
493
+ PROJECT_LANGS="go"
494
+ elif [ -f "pubspec.yaml" ]; then
495
+ if grep -q "flutter:" pubspec.yaml 2>/dev/null || [ -f ".metadata" ]; then
496
+ PROJECT_LANGS="flutter"
497
+ else
498
+ PROJECT_LANGS="dart"
499
+ fi
500
+ elif [ -f "pom.xml" ] || [ -f "build.gradle" ] || [ -f "build.gradle.kts" ]; then
501
+ KOTLIN_FILES=$(find . -name "*.kt" -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | wc -l)
502
+ if [ "$KOTLIN_FILES" -gt 0 ]; then
503
+ PROJECT_LANGS="kotlin"
504
+ else
505
+ PROJECT_LANGS="java"
506
+ fi
507
+ elif [ -f "CMakeLists.txt" ] || [ -f "Makefile" ]; then
508
+ CPP_FILES=$(find . -maxdepth 2 \( -name "*.cpp" -o -name "*.cxx" -o -name "*.cc" \) -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | head -1)
509
+ OBJC_FILES=$(find . -maxdepth 2 \( -name "*.m" -o -name "*.mm" \) -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | head -1)
510
+ if [ -n "$CPP_FILES" ]; then
511
+ PROJECT_LANGS="cpp"
512
+ elif [ -n "$OBJC_FILES" ]; then
513
+ PROJECT_LANGS="objectivec"
514
+ fi
515
+ elif [ -n "$(find . -maxdepth 2 -name "*.ps1" -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | head -1)" ]; then
516
+ PROJECT_LANGS="powershell"
517
+ fi
518
+ fi
519
+
520
+ # ---- Derive PROJECT_SUBDIR from changed file paths (v0.15.1+; ARCH-03 fix) ----
521
+ # Extension-based detection sets PROJECT_LANGS but leaves PROJECT_SUBDIR empty.
522
+ # In a monorepo where config files (tsconfig.json, pyproject.toml, ...) live in
523
+ # subdirectories (e.g. packages/frontend/), we must derive the subdirectory from
524
+ # the actual changed files so that Gates 1-9 run in the correct context.
525
+ PROJECT_SUBDIR=""
526
+ if [ -n "$PROJECT_LANGS" ]; then
527
+ # Walk up from each changed file to find the nearest project marker.
528
+ _detect_subdir() {
529
+ local fdir
530
+ fdir=$(dirname "$1")
531
+ while [ "$fdir" != "." ] && [ "$fdir" != "/" ]; do
532
+ # Skip node_modules, .git, and other non-project directories
533
+ case "$fdir" in
534
+ */node_modules/*) fdir="${fdir%/*}" ; continue ;;
535
+ */.git/*) fdir="${fdir%/*}" ; continue ;;
536
+ esac
537
+ if [ -f "$fdir/tsconfig.json" ] || [ -f "$fdir/package.json" ] || \
538
+ [ -f "$fdir/pyproject.toml" ] || [ -f "$fdir/setup.py" ] || \
539
+ [ -f "$fdir/go.mod" ] || [ -f "$fdir/pubspec.yaml" ] || \
540
+ [ -f "$fdir/pom.xml" ] || [ -f "$fdir/build.gradle" ] || \
541
+ [ -f "$fdir/build.gradle.kts" ] || [ -f "$fdir/CMakeLists.txt" ]; then
542
+ echo "$fdir"
543
+ return 0
544
+ fi
545
+ case "$fdir" in
546
+ */*) fdir="${fdir%/*}" ;;
547
+ *) break ;;
548
+ esac
549
+ done
550
+ return 1
551
+ }
552
+ # #527: two ecosystems whose MULTI-module layouts normally carry a root
553
+ # marker, so a root marker alone cannot prove "single project":
554
+ # - Gradle: root build.gradle(.kts) + settings.gradle(.kts) with include
555
+ # - CMake: root CMakeLists.txt + a nested */CMakeLists.txt
556
+ # Both probes are bounded (two file reads for Gradle; one maxdepth-7 find
557
+ # for CMake — MC-05, no new unbounded scan) and only run once a root marker
558
+ # is present. A false positive keeps derivation ON — the #515 default —
559
+ # so the failure direction is never a false BLOCK.
560
+ _gradle_multi_module_root() {
561
+ local _sg
562
+ for _sg in settings.gradle settings.gradle.kts; do
563
+ # include ':app' (Groovy) / include(":app") (Kotlin DSL). The char
564
+ # class anchors on the token after `include`, so `includeBuild` and
565
+ # comment prose cannot false-positive.
566
+ if [ -f "$_sg" ] && grep -qE "include[[:space:]]*['\"(]" "$_sg" 2>/dev/null; then
567
+ return 0
568
+ fi
569
+ done
570
+ return 1
571
+ }
572
+ _cmake_multi_module_root() {
573
+ [ -f "CMakeLists.txt" ] || return 1
574
+ # A nested CMakeLists.txt (depth 2..7 below the root) means the root file
575
+ # is a wiring file, not the whole project.
576
+ # -prune, not -not -path: filters the output AND stops traversal, so
577
+ # .git/node_modules are never descended into — the old form walked the
578
+ # whole tree on every commit of a repo that has both a root
579
+ # CMakeLists.txt and a big node_modules (#532 walkthrough FC-04).
580
+ # -mindepth is deliberately absent (a global -mindepth suppresses the
581
+ # depth-1 prune action); the root file is excluded by the awk NF>2
582
+ # filter instead (./CMakeLists.txt has no directory component).
583
+ [ -n "$(find . -maxdepth 7 \( -path '*/.git' -o -path '*/node_modules' \) -prune -o -name "CMakeLists.txt" -print 2>/dev/null | awk -F/ 'NF>2' | head -1)" ]
584
+ }
585
+ # #515: when the repo ROOT itself declares the project, a nested marker that
586
+ # is merely a module file (skill_cert/cli/setup.py incident) must not hijack
587
+ # PROJECT_SUBDIR — Gates 1-9 would run from the wrong cwd and false-BLOCK on
588
+ # root-relative paths. The root-marker set below MUST stay aligned with
589
+ # _detect_subdir's marker set above (10 markers): every ecosystem recognized
590
+ # for subproject derivation is also protected at the root. The JS-monorepo
591
+ # check runs FIRST (#515 R2 delphi walkthrough): a root package.json with a
592
+ # "workspaces" key declares a JS monorepo (often together with a shared
593
+ # tsconfig.json), not a single project, so derivation keeps running there.
594
+ # #527 (resolved): Gradle multi-module and CMake subdirectory layouts are
595
+ # disambiguated by the predicates above; the short-circuit now applies to
596
+ # genuinely single-project roots. pnpm/Lerna workspaces remain a known
597
+ # limitation (no probe yet). Migration: repos that depended on the old
598
+ # short-circuit set XP_GATE_ROOT_SHORTCIRCUIT=1 (or delete the root
599
+ # marker); see githooks/__tests__/issue-527-subdir-multimodule.test.bats.
600
+ _skip_subdir_derive=0
601
+ if [ -f package.json ] && grep -q '"workspaces":' package.json 2>/dev/null; then
602
+ _skip_subdir_derive=0 # JS monorepo root: subproject derivation stays on
603
+ elif [ -f package.json ] || [ -f tsconfig.json ] || [ -f pyproject.toml ] || \
604
+ [ -f setup.py ] || [ -f go.mod ] || [ -f pubspec.yaml ] || \
605
+ [ -f pom.xml ] || [ -f build.gradle ] || \
606
+ [ -f build.gradle.kts ] || [ -f CMakeLists.txt ]; then
607
+ # #527: before short-circuiting, ask whether the root marker declares
608
+ # MODULES (Gradle settings include / nested CMakeLists) rather than one
609
+ # project. The override restores the pre-#527 behavior explicitly.
610
+ if [ "${XP_GATE_ROOT_SHORTCIRCUIT:-0}" = "1" ]; then
611
+ _skip_subdir_derive=1
612
+ elif _gradle_multi_module_root || _cmake_multi_module_root; then
613
+ _skip_subdir_derive=0 # root marker declares modules — keep deriving
614
+ else
615
+ _skip_subdir_derive=1
616
+ fi
617
+ fi
618
+ _subdir_set=""
619
+ if [ "$_skip_subdir_derive" -eq 0 ]; then
620
+ while IFS= read -r _changed; do
621
+ [ -z "$_changed" ] && continue
622
+ _d=$(_detect_subdir "$_changed" 2>/dev/null) || true
623
+ if [ -n "$_d" ] && [ "$_d" != "." ]; then
624
+ case " $_subdir_set " in
625
+ *" $_d "*) ;; # already seen
626
+ *) _subdir_set="${_subdir_set} $_d" ;;
627
+ esac
628
+ fi
629
+ done <<CHANGEDFILELIST
630
+ $CHANGED_FILES
631
+ CHANGEDFILELIST
632
+ _subdir_count=$(echo "$_subdir_set" | awk '{ n=0; for(i=1;i<=NF;i++) n++; print n }')
633
+ if [ "$_subdir_count" = "1" ]; then
634
+ PROJECT_SUBDIR=$(echo "$_subdir_set" | awk '{print $1}')
635
+ echo ""
636
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
637
+ echo " 📁 MULTILANGUAGE SUBDIR DETECTED FROM CHANGED FILES"
638
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
639
+ echo ""
640
+ echo "Language: $PROJECT_LANGS"
641
+ echo "Location: $PROJECT_SUBDIR/"
642
+ echo ""
643
+ fi
644
+ fi
645
+ fi
646
+
647
+ # ---- Subdirectory fallback (for single-language projects in subdirectories) ----
648
+ # Only used when extension-based detection found nothing
649
+ if [ -z "$PROJECT_LANGS" ]; then
650
+ # Search for TypeScript/JavaScript projects
651
+ TSCONFIG_SUB=$(find . -maxdepth 2 -name "tsconfig.json" -not -path "./.git/*" -not -path "*/node_modules/*" -not -path "./node_modules/*" 2>/dev/null | head -1)
652
+ if [ -n "$TSCONFIG_SUB" ]; then
653
+ PROJECT_SUBDIR=$(dirname "$TSCONFIG_SUB")
654
+ if [ -f "$PROJECT_SUBDIR/package.json" ] && grep -q "react-native" "$PROJECT_SUBDIR/package.json" 2>/dev/null; then
655
+ PROJECT_LANGS="react-native"
656
+ else
657
+ PROJECT_LANGS="typescript"
658
+ fi
659
+ fi
660
+
661
+ # Search for Python projects
662
+ if [ -z "$PROJECT_LANGS" ]; then
663
+ PYPROJECT_SUB=$(find . -maxdepth 2 -name "pyproject.toml" -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | head -1)
664
+ if [ -n "$PYPROJECT_SUB" ]; then
665
+ PROJECT_LANGS="python"
666
+ PROJECT_SUBDIR=$(dirname "$PYPROJECT_SUB")
667
+ fi
668
+ fi
669
+
670
+ # Search for Go projects
671
+ if [ -z "$PROJECT_LANGS" ]; then
672
+ GOMOD_SUB=$(find . -maxdepth 2 -name "go.mod" -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | head -1)
673
+ if [ -n "$GOMOD_SUB" ]; then
674
+ PROJECT_LANGS="go"
675
+ PROJECT_SUBDIR=$(dirname "$GOMOD_SUB")
676
+ fi
677
+ fi
678
+
679
+ # Search for Flutter/Dart projects
680
+ if [ -z "$PROJECT_LANGS" ]; then
681
+ PUBSPEC_SUB=$(find . -maxdepth 2 -name "pubspec.yaml" -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | head -1)
682
+ if [ -n "$PUBSPEC_SUB" ]; then
683
+ if grep -q "flutter:" "$PUBSPEC_SUB" 2>/dev/null || [ -f "$(dirname "$PUBSPEC_SUB")/.metadata" ]; then
684
+ PROJECT_LANGS="flutter"
685
+ else
686
+ PROJECT_LANGS="dart"
687
+ fi
688
+ PROJECT_SUBDIR=$(dirname "$PUBSPEC_SUB")
689
+ fi
690
+ fi
691
+
692
+ # Search for Java/Kotlin projects
693
+ if [ -z "$PROJECT_LANGS" ]; then
694
+ POM_SUB=$(find . -maxdepth 2 -name "pom.xml" -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | head -1)
695
+ GRADLE_SUB=$(find . -maxdepth 2 -name "build.gradle" -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | head -1)
696
+ if [ -n "$POM_SUB" ] || [ -n "$GRADLE_SUB" ]; then
697
+ MANIFEST_FILE="${POM_SUB:-$GRADLE_SUB}"
698
+ PROJECT_SUBDIR=$(dirname "$MANIFEST_FILE")
699
+ KOTLIN_COUNT=$(find "$PROJECT_SUBDIR" -name "*.kt" -not -path "*/.git/*" 2>/dev/null | wc -l)
700
+ if [ "$KOTLIN_COUNT" -gt 0 ]; then
701
+ PROJECT_LANGS="kotlin"
702
+ else
703
+ PROJECT_LANGS="java"
704
+ fi
705
+ fi
706
+ fi
707
+
708
+ # Search for C++ projects
709
+ if [ -z "$PROJECT_LANGS" ]; then
710
+ CMAKE_SUB=$(find . -maxdepth 2 -name "CMakeLists.txt" -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | head -1)
711
+ if [ -n "$CMAKE_SUB" ]; then
712
+ PROJECT_LANGS="cpp"
713
+ PROJECT_SUBDIR=$(dirname "$CMAKE_SUB")
714
+ fi
715
+ fi
716
+
717
+ # Search for React Native projects (ios/ + android/ directories)
718
+ if [ -z "$PROJECT_LANGS" ]; then
719
+ RN_SUB=$(find . -maxdepth 2 -type d -name "ios" -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | head -1)
720
+ if [ -n "$RN_SUB" ]; then
721
+ PROJECT_SUBDIR=$(dirname "$RN_SUB")
722
+ if [ -d "$PROJECT_SUBDIR/android" ] || [ -f "$PROJECT_SUBDIR/package.json" ]; then
723
+ PROJECT_LANGS="react-native"
724
+ fi
725
+ fi
726
+ fi
727
+
728
+ # Search for PowerShell projects
729
+ if [ -z "$PROJECT_LANGS" ]; then
730
+ PS_SUB=$(find . -maxdepth 2 -name "*.ps1" -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | head -1)
731
+ if [ -n "$PS_SUB" ]; then
732
+ PROJECT_LANGS="powershell"
733
+ PROJECT_SUBDIR=$(dirname "$PS_SUB")
734
+ fi
735
+ fi
736
+
737
+ # Report detected subdirectory project
738
+ if [ -n "$PROJECT_SUBDIR" ] && [ -n "$PROJECT_LANGS" ]; then
739
+ echo ""
740
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
741
+ echo " 📁 SUBDIRECTORY PROJECT DETECTED"
742
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
743
+ echo ""
744
+ echo "Language: $PROJECT_LANGS"
745
+ echo "Location: $PROJECT_SUBDIR/"
746
+ echo ""
747
+ fi
748
+ fi
749
+
750
+ # Set PROJECT_LANG for backward compatibility (first language in list)
751
+ PROJECT_LANG=$(echo "$PROJECT_LANGS" | awk '{print $1}')
752
+
753
+ # ---- Documentation-only fallback ----
754
+ if [ -z "$PROJECT_LANGS" ]; then
755
+ CODE_FILES=$(find . -type f \( -name "*.py" -o -name "*.js" -o -name "*.ts" -o -name "*.tsx" -o -name "*.java" -o -name "*.go" -o -name "*.rs" -o -name "*.cpp" -o -name "*.c" -o -name "*.swift" -o -name "*.kt" -o -name "*.sh" -o -name "*.dart" -o -name "*.ps1" \) -not -path "./.git/*" -not -path "*/node_modules/*" 2>/dev/null | wc -l)
756
+
757
+ if [ "$CODE_FILES" -eq 0 ]; then
758
+ PROJECT_LANGS="documentation-only"
759
+ PROJECT_LANG="documentation-only"
760
+ echo ""
761
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
762
+ echo " 📚 DOCUMENTATION-ONLY PROJECT DETECTED"
763
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
764
+ echo ""
765
+ echo "This project contains no source code files."
766
+ echo "Skipping static analysis and test gates."
767
+ echo ""
768
+ echo "Proceeding with documentation-only checks..."
769
+ fi
770
+ fi
771
+
772
+ # ---- Multi-language project banner ----
773
+ _lang_count=$(echo "$PROJECT_LANGS" | wc -w)
774
+ if [ "$_lang_count" -gt 1 ]; then
775
+ echo ""
776
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
777
+ echo " 🌐 MULTI-LANGUAGE PROJECT DETECTED"
778
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
779
+ echo ""
780
+ echo "Detected $_lang_count languages from changed files:"
781
+ for _l in $PROJECT_LANGS; do
782
+ echo " • $_l"
783
+ done
784
+ echo ""
785
+ fi
786
+ fi # close language override if/else
787
+
788
+ # Helper: check if a language is in PROJECT_LANGS
789
+ has_project_lang() {
790
+ for lang in $PROJECT_LANGS; do
791
+ if [ "$lang" = "$1" ]; then return 0; fi
792
+ done
793
+ return 1
794
+ }
795
+ # Switch to subdirectory if detected (for single-language subdirectory projects).
796
+ # Multi-language projects stay at root — each adapter handles its own path.
797
+ #
798
+ # IN_PROJECT_SUBDIR records whether this cd actually happened. Git reports
799
+ # staged paths relative to the repo root; after cd "$PROJECT_SUBDIR" the [ -f ]
800
+ # probes below need subdirectory-relative paths (hence the PROJECT_SUBDIR
801
+ # prefix strips in Gates 5a/5b/5c), but when the hook stays at the root
802
+ # (multi-language projects) stripping the prefix makes every path-based check
803
+ # resolve against the wrong directory — false BLOCKs in Gate 5a, silent no-ops
804
+ # in 5b/5c. Only strip when we really are inside the subdirectory.
805
+ ORIGINAL_DIR=""
806
+ IN_PROJECT_SUBDIR=0
807
+ _lang_count=$(echo "$PROJECT_LANGS" | wc -w)
808
+ if [ -n "$PROJECT_SUBDIR" ] && [ "$PROJECT_SUBDIR" != "." ] && [ "$PROJECT_LANG" != "documentation-only" ] && [ "$_lang_count" -le 1 ]; then
809
+ ORIGINAL_DIR=$(pwd)
810
+ # #392-#10: guard cd so a missing subdir cannot run gates in the wrong cwd.
811
+ if cd "$PROJECT_SUBDIR" 2>/dev/null; then
812
+ IN_PROJECT_SUBDIR=1
813
+ echo "Working in: $PROJECT_SUBDIR/"
814
+ echo ""
815
+ else
816
+ echo "WARN: could not enter $PROJECT_SUBDIR — continuing at repo root" >&2
817
+ fi
818
+ fi
819
+ return 0
820
+ }
821
+
822
+
823
+
824
+ # Called by pre-commit's ordered driver.
825
+