@boyingliu01/xp-gate 0.38.76 → 0.38.78

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 (111) hide show
  1. package/lib/__tests__/gate2-verdict.test.js +58 -1
  2. package/lib/__tests__/mirror-sync.test.js +387 -5
  3. package/lib/gate-runner.js +88 -9
  4. package/lib/mattpocock-license-verdict.json +1 -1
  5. package/lib/mirror-sync.js +202 -15
  6. package/package.json +1 -1
  7. package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
  8. package/plugins/claude-code/skills/delphi-review/AGENTS.md +1 -1
  9. package/plugins/claude-code/skills/sprint-flow/AGENTS.md +1 -1
  10. package/plugins/claude-code/skills/sprint-flow/references/phase-2-design.md +62 -2
  11. package/plugins/claude-code/skills/test-specification-alignment/AGENTS.md +1 -1
  12. package/plugins/dsh/package.json +1 -1
  13. package/plugins/dsh/skills/delphi-review/AGENTS.md +1 -1
  14. package/plugins/dsh/skills/sprint-flow/AGENTS.md +1 -1
  15. package/plugins/dsh/skills/sprint-flow/references/phase-2-design.md +62 -2
  16. package/plugins/dsh/skills/test-specification-alignment/AGENTS.md +1 -1
  17. package/plugins/opencode/package.json +1 -1
  18. package/plugins/opencode/skills/delphi-review/AGENTS.md +1 -1
  19. package/plugins/opencode/skills/sprint-flow/AGENTS.md +1 -1
  20. package/plugins/opencode/skills/sprint-flow/references/phase-2-design.md +62 -2
  21. package/plugins/opencode/skills/test-specification-alignment/AGENTS.md +1 -1
  22. package/plugins/qoder/plugin.json +1 -1
  23. package/plugins/qoder/skills/delphi-review/AGENTS.md +1 -1
  24. package/plugins/qoder/skills/sprint-flow/AGENTS.md +1 -1
  25. package/plugins/qoder/skills/sprint-flow/references/phase-2-design.md +62 -2
  26. package/plugins/qoder/skills/test-specification-alignment/AGENTS.md +1 -1
  27. package/scripts/generate-mirrors.cjs +10 -0
  28. package/skills/delphi-review/AGENTS.md +1 -1
  29. package/skills/sprint-flow/AGENTS.md +1 -1
  30. package/skills/sprint-flow/references/phase-2-design.md +62 -2
  31. package/skills/test-specification-alignment/AGENTS.md +1 -1
  32. package/mock-policy/__tests__/config.test.ts +0 -292
  33. package/mock-policy/__tests__/gate-m3.test.ts +0 -379
  34. package/mock-policy/__tests__/integration.test.ts +0 -600
  35. package/mock-policy/__tests__/mock-decision-engine.test.ts +0 -231
  36. package/mock-policy/__tests__/schema.test.ts +0 -239
  37. package/mock-policy/__tests__/scope-scanner.test.ts +0 -541
  38. package/mutation/__tests__/detect-ai-test.test.ts +0 -152
  39. package/mutation/__tests__/gate-m-survivors.test.ts +0 -167
  40. package/mutation/__tests__/gate-m.test.ts +0 -246
  41. package/mutation/__tests__/go-mutant-runner.test.ts +0 -288
  42. package/mutation/__tests__/gremlins-runner.test.ts +0 -167
  43. package/mutation/__tests__/init-baseline.test.ts +0 -347
  44. package/mutation/__tests__/mutation-mirror.test.ts +0 -49
  45. package/mutation/__tests__/mutmut-runner.test.ts +0 -1049
  46. package/mutation/__tests__/pitest-gradle-availability.test.ts +0 -74
  47. package/mutation/__tests__/pitest-runner.test.ts +0 -574
  48. package/mutation/__tests__/runners-index.test.ts +0 -162
  49. package/mutation/__tests__/stryker-runner.test.ts +0 -455
  50. package/mutation/__tests__/test-filter-parity.test.ts +0 -40
  51. package/mutation/__tests__/update-baseline.test.ts +0 -395
  52. package/principles/__tests__/adapter-metrics.test.ts +0 -292
  53. package/principles/__tests__/analyze-read-failure.test.ts +0 -70
  54. package/principles/__tests__/analyzer.test.ts +0 -367
  55. package/principles/__tests__/baseline-entry-total-warnings.test.ts +0 -76
  56. package/principles/__tests__/baseline-prune-timeout.test.ts +0 -71
  57. package/principles/__tests__/baseline-prune.test.ts +0 -399
  58. package/principles/__tests__/baseline-storage.test.ts +0 -188
  59. package/principles/__tests__/baseline.test.ts +0 -330
  60. package/principles/__tests__/boy-scout-init-baseline.test.ts +0 -167
  61. package/principles/__tests__/boy-scout-integration.test.ts +0 -355
  62. package/principles/__tests__/boy-scout.test.ts +0 -893
  63. package/principles/__tests__/cli-files-separators.test.ts +0 -69
  64. package/principles/__tests__/config-arch-group.test.ts +0 -106
  65. package/principles/__tests__/config-enforcement.test.ts +0 -470
  66. package/principles/__tests__/config-git-timeout.test.ts +0 -66
  67. package/principles/__tests__/config-merge-warnings.test.ts +0 -148
  68. package/principles/__tests__/config.test.ts +0 -78
  69. package/principles/__tests__/direct-execution.test.ts +0 -187
  70. package/principles/__tests__/index.test.ts +0 -94
  71. package/principles/__tests__/issue-587-java-member-count.test.ts +0 -279
  72. package/principles/__tests__/issue-588-lsp-java-false-positives.test.ts +0 -213
  73. package/principles/__tests__/lint-baseline.test.ts +0 -314
  74. package/principles/__tests__/many-exports-reexport.test.ts +0 -40
  75. package/principles/__tests__/reporter.test.ts +0 -660
  76. package/principles/__tests__/types.test.ts +0 -103
  77. package/principles/__tests__/warnings-baseline-budgets.test.ts +0 -74
  78. package/principles/adapters/__tests__/base.test.ts +0 -95
  79. package/principles/adapters/__tests__/cpp-code-block.test.ts +0 -123
  80. package/principles/adapters/__tests__/cpp-scanner-equivalence.test.ts +0 -67
  81. package/principles/adapters/__tests__/cpp.test.ts +0 -290
  82. package/principles/adapters/__tests__/dart.test.ts +0 -84
  83. package/principles/adapters/__tests__/go.test.ts +0 -68
  84. package/principles/adapters/__tests__/java.test.ts +0 -385
  85. package/principles/adapters/__tests__/kotlin.test.ts +0 -76
  86. package/principles/adapters/__tests__/objectivec.test.ts +0 -453
  87. package/principles/adapters/__tests__/python.test.ts +0 -84
  88. package/principles/adapters/__tests__/swift.test.ts +0 -68
  89. package/principles/adapters/__tests__/typescript-block-comment-lines.test.ts +0 -53
  90. package/principles/adapters/__tests__/typescript-masking.test.ts +0 -96
  91. package/principles/adapters/__tests__/typescript.test.ts +0 -116
  92. package/principles/rules/__tests__/clean-code/code-duplication.test.ts +0 -85
  93. package/principles/rules/__tests__/clean-code/deep-nesting.test.ts +0 -73
  94. package/principles/rules/__tests__/clean-code/god-class.test.ts +0 -109
  95. package/principles/rules/__tests__/clean-code/large-file.test.ts +0 -73
  96. package/principles/rules/__tests__/clean-code/long-function.test.ts +0 -81
  97. package/principles/rules/__tests__/clean-code/magic-numbers.test.ts +0 -68
  98. package/principles/rules/__tests__/clean-code/many-exports.test.ts +0 -123
  99. package/principles/rules/__tests__/clean-code/missing-error-handling.test.ts +0 -171
  100. package/principles/rules/__tests__/clean-code/too-many-params.test.ts +0 -58
  101. package/principles/rules/__tests__/clean-code/unused-imports.test.ts +0 -107
  102. package/principles/rules/__tests__/index.test.ts +0 -49
  103. package/principles/rules/__tests__/solid/dip.test.ts +0 -119
  104. package/principles/rules/__tests__/solid/isp.test.ts +0 -58
  105. package/principles/rules/__tests__/solid/issue-588-lsp-base-contract.test.ts +0 -252
  106. package/principles/rules/__tests__/solid/lsp.test.ts +0 -186
  107. package/principles/rules/__tests__/solid/ocp.test.ts +0 -67
  108. package/principles/rules/__tests__/solid/srp.test.ts +0 -87
  109. package/principles/rules/arch/__tests__/dependency.test.ts +0 -180
  110. package/principles/rules/arch/__tests__/naming.test.ts +0 -133
  111. package/principles/rules/arch/__tests__/shared.test.ts +0 -113
@@ -14,7 +14,7 @@ const fs = require('node:fs');
14
14
  const path = require('node:path');
15
15
  const os = require('node:os');
16
16
 
17
- const { buildJscpdArgs } = require('../gate-runner');
17
+ const { buildJscpdArgs, classifyJscpdRun } = require('../gate-runner');
18
18
 
19
19
  let tmpDir;
20
20
 
@@ -124,3 +124,60 @@ describe('#853 Gate 2 must be able to fail', () => {
124
124
  expect(args).not.toContain('--config');
125
125
  });
126
126
  });
127
+
128
+ /**
129
+ * @test REQ-GATE2-NO-FILES the CLI must not report "clean" when jscpd analysed zero files
130
+ * @intent `xp-gate check 2` captures jscpd's stream and classifies it BEFORE deciding the
131
+ * verdict. The shell hook already does this (`classify_jscpd_run` in
132
+ * githooks/lib/jscpd-run.sh, pinned by gate-2-no-files.test.bats); the CLI path
133
+ * had no equivalent, so the same defect survived there.
134
+ * @covers AC-855-01 AC-855-02 AC-855-03 AC-855-04
135
+ *
136
+ * #855 measured on 0.38.76: a two-file fixture jscpd refuses to parse (below its
137
+ * min-tokens floor) produced `Files analyzed: 0`, jscpd's own
138
+ * `Warning: jscpd analyzed no files`, and `exit 0` from `xp-gate check 2` with
139
+ * "No duplicates found." A scan that read nothing was indistinguishable from a scan
140
+ * that read the change set and found it clean -- the exact failure family both
141
+ * AGENTS.md files name.
142
+ */
143
+ describe('#855 Gate 2 must not call an empty scan clean', () => {
144
+ it('classifies an "analyzed no files" warning as no_files, not clean', () => {
145
+ const out = [
146
+ 'No duplicates found.',
147
+ '| Format | Files analyzed | Total lines | Total tokens | Clones found |',
148
+ 'Found 0 clones.',
149
+ 'time: 8.279ms',
150
+ 'Warning: jscpd analyzed no files: check the paths and the --format, --ignore and --pattern filters',
151
+ ].join('\n');
152
+ expect(classifyJscpdRun(0, out)).toBe('no_files');
153
+ });
154
+
155
+ it('keeps a real scan with zero clones as clean', () => {
156
+ // Anti-vacuity: if every "Found 0 clones" became no_files the gate would skip
157
+ // every project, trading a silent pass for a permanent skip.
158
+ const out = ['| Total: | 12 | 340 | 2100 | 0 |', 'Found 0 clones.'].join('\n');
159
+ expect(classifyJscpdRun(0, out)).toBe('clean');
160
+ });
161
+
162
+ it('keeps a real scan with clones as duplicates', () => {
163
+ const out = ['| Total: | 12 | 340 | 2100 | 3 |', 'Found 3 clones.'].join('\n');
164
+ expect(classifyJscpdRun(0, out)).toBe('duplicates');
165
+ });
166
+
167
+ it('lets a non-zero exit win over the no-files warning', () => {
168
+ // A failed run is the more specific fact; calling it no_files would hide it.
169
+ expect(classifyJscpdRun(2, 'Warning: jscpd analyzed no files: check the paths')).toBe('error');
170
+ });
171
+
172
+ it('treats a report with no clones line and no files line as no_files', () => {
173
+ // Measured shape: an --ignore that excludes everything prints one timing line.
174
+ expect(classifyJscpdRun(0, 'time: 0.156ms')).toBe('no_files');
175
+ });
176
+
177
+ it('does not turn an unfamiliar but non-empty report into a skip', () => {
178
+ // The pre-existing tolerance, kept deliberately: a future jscpd format must not
179
+ // disable the gate for everyone whose output we do not recognise.
180
+ const out = 'some future jscpd format\nformat v9\nclones: none detected\nfiles scanned: 14';
181
+ expect(classifyJscpdRun(0, out)).toBe('clean');
182
+ });
183
+ });
@@ -28,8 +28,42 @@ const {
28
28
  syncMirrors,
29
29
  handleSyncMirrors,
30
30
  SYNC_EXCLUDES,
31
+ KNOWN_PAIRS,
32
+ HAND_LISTED_PAIRS,
33
+ assertPairsResolved,
31
34
  } = require('../mirror-sync');
32
35
 
36
+ /**
37
+ * Directories that are mirrors of a canonical tree rather than this package's own
38
+ * source.
39
+ *
40
+ * `bin/`, `lib/` and `scripts/` are the package's own code, not copies.
41
+ *
42
+ * `hooks/`, `plugins/` and `gates/` are supervised ELSEWHERE and are deliberately
43
+ * omitted here rather than being reported as unsupervised:
44
+ * - `hooks/` -> HOOK_MIRROR_FILES in scripts/check-hook-mirror.sh (a curated
45
+ * file list, not the whole tree; same reason KNOWN_PAIRS omits it)
46
+ * - `plugins/`-> syncPlugins in sync-package-content.js
47
+ * - `gates/` -> listed in files[] but the directory does not exist at all, so
48
+ * there is nothing to compare. Left alone: removing a stale
49
+ * files[] entry is a packaging change, not a mirror one.
50
+ * Including them would make this case report three permanent failures that no
51
+ * mirror work could ever fix.
52
+ */
53
+ const MIRROR_DIR_CANDIDATES = [
54
+ 'adapters',
55
+ 'skills',
56
+ 'principles',
57
+ 'mutation',
58
+ 'mock-policy',
59
+ 'build-integrity',
60
+ ];
61
+
62
+ /** Package-relative mirror dirs supervised by an explicit pair, not a generator. */
63
+ const HAND_LISTED_MIRRORS = HAND_LISTED_PAIRS.map((pair) =>
64
+ pair.mirror.replace(/^src\/npm-package\//, '')
65
+ );
66
+
33
67
  let TMP_REPO;
34
68
 
35
69
  /**
@@ -99,6 +133,21 @@ function trackAll(root) {
99
133
  execFileSync('git', ['add', '-A', '-f'], { cwd: root });
100
134
  }
101
135
 
136
+ /**
137
+ * Stage the canonical tree and `.gitignore`, leaving the mirror untracked.
138
+ *
139
+ * `trackAll` cannot be used for the generated-mirror cases: its `-f` overrides
140
+ * `.gitignore`, so `mirror/` gets staged and the "the mirror is untracked" premise
141
+ * disappears. Verified in a scratch repo: `git add -A -f` lists `mirror/a.sh` in
142
+ * `git ls-files` even with `mirror/` ignored.
143
+ *
144
+ * @param {string} root
145
+ * @param {string} canonical directory to track
146
+ */
147
+ function trackCanonicalOnly(root, canonical) {
148
+ execFileSync('git', ['add', '--', '.gitignore', canonical], { cwd: root });
149
+ }
150
+
102
151
  describe('diffTrees — AC-713-01 untracked artifacts are not drift', () => {
103
152
  it('ignores an untracked build artifact in the canonical tree', () => {
104
153
  write(TMP_REPO, 'canonical/a.sh', 'echo a\n');
@@ -150,23 +199,140 @@ describe('diffTrees — AC-713-01 untracked artifacts are not drift', () => {
150
199
 
151
200
  describe('diffTrees — per-pair excludes', () => {
152
201
  it('honours a pair-specific exclude, matching the guard script it mirrors', () => {
153
- // check-hook-mirror.sh:98 compares src/principles with `-x __tests__`.
154
- // A sync that copied tests into the package would ship test code and make
155
- // the guard fail on the very next run.
202
+ // The mechanism under test is the per-pair `exclude` option. It used to be
203
+ // demonstrated with `__tests__`, but that is now a GLOBAL rule (MIRROR_EXCLUDES)
204
+ // because the generator never copies test code for any pair -- keeping the old
205
+ // example would assert that an undeclared `__tests__` still reports drift,
206
+ // which is the bug this file now pins. `fixtures` stands in for a directory
207
+ // only one pair wants to skip.
156
208
  write(TMP_REPO, 'canonical/lib.ts', 'export const a = 1;\n');
157
- write(TMP_REPO, 'canonical/__tests__/lib.test.ts', 'test\n');
209
+ write(TMP_REPO, 'canonical/fixtures/data.json', '{}\n');
158
210
  write(TMP_REPO, 'mirror/lib.ts', 'export const a = 1;\n');
159
211
  trackAll(TMP_REPO);
160
212
 
161
213
  const withoutExclude = diffTrees(TMP_REPO, 'canonical', 'mirror');
162
214
  expect(withoutExclude.inSync).toBe(false);
163
215
 
164
- const withExclude = diffTrees(TMP_REPO, 'canonical', 'mirror', { exclude: ['__tests__'] });
216
+ const withExclude = diffTrees(TMP_REPO, 'canonical', 'mirror', { exclude: ['fixtures'] });
165
217
  expect(withExclude.inSync).toBe(true);
166
218
  expect(withExclude.missing).toEqual([]);
167
219
  });
220
+
221
+ it('does not let a pair-specific exclude re-enable the global ones', () => {
222
+ // The lists compose. `exclude: ['fixtures']` must not silently drop
223
+ // `__tests__` back into the comparison, or every caller that names one
224
+ // directory reintroduces the drift this section exists to prevent.
225
+ write(TMP_REPO, 'canonical/lib.ts', 'export const a = 1;\n');
226
+ write(TMP_REPO, 'canonical/__tests__/lib.test.ts', 'test\n');
227
+ write(TMP_REPO, 'mirror/lib.ts', 'export const a = 1;\n');
228
+ trackAll(TMP_REPO);
229
+
230
+ const diff = diffTrees(TMP_REPO, 'canonical', 'mirror', { exclude: ['fixtures'] });
231
+ expect(diff.inSync).toBe(true);
232
+ });
168
233
  });
169
234
 
235
+ describe('diffTrees — P1-E a generated mirror is untracked, not missing', () => {
236
+ it('does not report drift when the mirror tree is gitignored and byte-identical', () => {
237
+ // P1-E step 2 took four mirror trees out of version control: prepack
238
+ // regenerates them from canonical, so tracking them IS the duplication the
239
+ // decision set out to remove.
240
+ //
241
+ // Measured after that landed: `xp-gate sync-mirrors` reported DRIFT for
242
+ // adapters, hooks/lib, build-integrity AND skills -- the four trees the
243
+ // generator had just written correctly. Both sides were compared on their
244
+ // tracked file set, and a gitignored tree has none, so every canonical file
245
+ // read as "missing from the mirror".
246
+ write(TMP_REPO, 'canonical/a.sh', 'echo a\n');
247
+ write(TMP_REPO, 'canonical/sub/b.sh', 'echo b\n');
248
+ write(TMP_REPO, 'mirror/a.sh', 'echo a\n');
249
+ write(TMP_REPO, 'mirror/sub/b.sh', 'echo b\n');
250
+ write(TMP_REPO, '.gitignore', 'mirror/\n');
251
+ trackCanonicalOnly(TMP_REPO, 'canonical');
252
+
253
+ // Premise asserted before the verdict: if the fixture stages the mirror, the
254
+ // case passes vacuously and stops covering what it names.
255
+ const tracked = execFileSync('git', ['ls-files', '--', 'mirror'], {
256
+ cwd: TMP_REPO,
257
+ encoding: 'utf8',
258
+ }).trim();
259
+ expect(tracked).toBe('');
260
+
261
+ const diff = diffTrees(TMP_REPO, 'canonical', 'mirror');
262
+ expect(diff.missing).toEqual([]);
263
+ expect(diff.differing).toEqual([]);
264
+ expect(diff.extra).toEqual([]);
265
+ expect(diff.inSync).toBe(true);
266
+ });
267
+
268
+ it('still reports a byte difference inside an untracked mirror tree', () => {
269
+ // Control. Relaxing to "the mirror is untracked, stop looking" would make the
270
+ // case above pass and destroy the check.
271
+ write(TMP_REPO, 'canonical/a.sh', 'echo a\n');
272
+ write(TMP_REPO, 'mirror/a.sh', 'echo DRIFTED\n');
273
+ write(TMP_REPO, '.gitignore', 'mirror/\n');
274
+ trackCanonicalOnly(TMP_REPO, 'canonical');
275
+
276
+ const diff = diffTrees(TMP_REPO, 'canonical', 'mirror');
277
+ expect(diff.inSync).toBe(false);
278
+ expect(diff.differing).toContain('a.sh');
279
+ });
280
+
281
+ it('still reports a genuinely absent file inside an untracked mirror tree', () => {
282
+ write(TMP_REPO, 'canonical/a.sh', 'echo a\n');
283
+ write(TMP_REPO, 'canonical/b.sh', 'echo b\n');
284
+ write(TMP_REPO, 'mirror/a.sh', 'echo a\n');
285
+ write(TMP_REPO, '.gitignore', 'mirror/\n');
286
+ trackCanonicalOnly(TMP_REPO, 'canonical');
287
+
288
+ const diff = diffTrees(TMP_REPO, 'canonical', 'mirror');
289
+ expect(diff.inSync).toBe(false);
290
+ expect(diff.missing).toContain('b.sh');
291
+ });
292
+
293
+ it('reports an orphan in an untracked mirror tree, not just missing files', () => {
294
+ // `extra` is the verdict a disk-walk fix is most likely to drop: on the tracked
295
+ // path it came from the mirror's own set, which is now empty.
296
+ write(TMP_REPO, 'canonical/a.sh', 'echo a\n');
297
+ write(TMP_REPO, 'mirror/a.sh', 'echo a\n');
298
+ write(TMP_REPO, 'mirror/gone.sh', 'echo orphan\n');
299
+ write(TMP_REPO, '.gitignore', 'mirror/\n');
300
+ trackCanonicalOnly(TMP_REPO, 'canonical');
301
+
302
+ const diff = diffTrees(TMP_REPO, 'canonical', 'mirror');
303
+ expect(diff.extra).toContain('gone.sh');
304
+ expect(diff.inSync).toBe(false);
305
+ });
306
+
307
+ it('keeps an untracked artifact in an untracked mirror out of the verdict', () => {
308
+ // Both relaxations at once, which is the real repo's shape: the mirror is
309
+ // gitignored AND holds build noise. The disk fallback must not start reporting
310
+ // cache files as orphans -- that is the #587/#588 false failure reintroduced.
311
+ write(TMP_REPO, 'canonical/a.sh', 'echo a\n');
312
+ write(TMP_REPO, 'mirror/a.sh', 'echo a\n');
313
+ write(TMP_REPO, 'mirror/__pycache__/a.pyc', 'binary');
314
+ write(TMP_REPO, '.gitignore', 'mirror/\n');
315
+ trackCanonicalOnly(TMP_REPO, 'canonical');
316
+
317
+ const diff = diffTrees(TMP_REPO, 'canonical', 'mirror');
318
+ expect(diff.extra).toEqual([]);
319
+ expect(diff.inSync).toBe(true);
320
+ });
321
+
322
+ it('still respects a pair-specific exclude in an untracked mirror tree', () => {
323
+ write(TMP_REPO, 'canonical/lib.ts', 'export const a = 1;\n');
324
+ write(TMP_REPO, 'mirror/lib.ts', 'export const a = 1;\n');
325
+ write(TMP_REPO, 'mirror/__tests__/lib.test.ts', 'test\n');
326
+ write(TMP_REPO, '.gitignore', 'mirror/\n');
327
+ trackCanonicalOnly(TMP_REPO, 'canonical');
328
+
329
+ const diff = diffTrees(TMP_REPO, 'canonical', 'mirror', { exclude: ['__tests__'] });
330
+ expect(diff.inSync).toBe(true);
331
+ expect(diff.extra).toEqual([]);
332
+ });
333
+ });
334
+
335
+
170
336
  describe('collectMirrorPairs — AC-713-02 pair discovery', () => {
171
337
  it('discovers a pair only when both sides exist', () => {
172
338
  write(TMP_REPO, 'githooks/lib/a.sh', 'echo a\n');
@@ -281,6 +447,32 @@ describe('handleSyncMirrors — CLI contract', () => {
281
447
  expect(captured.join('')).toMatch(/NOT MEASURABLE/i);
282
448
  });
283
449
 
450
+ it('names the generator when a checkout cannot resolve any pairs', () => {
451
+ // The caller has two branches on `assertPairsResolved`'s return: a bare
452
+ // "is this the repo?" when it is null, and the generator-specific reason when it
453
+ // is not. Only the first was ever tested, so review could not tell whether the
454
+ // second was reachable. This pins it.
455
+ //
456
+ // `broken` is non-null only when the generator is present AND the pair list is
457
+ // empty, so the stubbed generator reproduces exactly that shape: present on
458
+ // disk, exporting nothing usable.
459
+ const fakeCheckout = fs.mkdtempSync(path.join(os.tmpdir(), 'xp-nopairs-'));
460
+ fs.mkdirSync(path.join(fakeCheckout, 'src', 'npm-package', 'scripts'), { recursive: true });
461
+ fs.writeFileSync(
462
+ path.join(fakeCheckout, 'src', 'npm-package', 'scripts', 'generate-mirrors.cjs'),
463
+ 'module.exports = {};\n'
464
+ );
465
+
466
+ // Here KNOWN_PAIRS is still non-empty (it belongs to the module, not the root),
467
+ // so this repo is measured normally -- proving the fixture does not accidentally
468
+ // reach the branch under test. The branch itself is covered directly in the
469
+ // `assertPairsResolved` suite by passing an empty pair list.
470
+ expect(fs.existsSync(path.join(fakeCheckout, 'src', 'npm-package', 'scripts', 'generate-mirrors.cjs'))).toBe(true);
471
+ const code = handleSyncMirrors([], { root: fakeCheckout });
472
+ expect(code).toBe(1);
473
+ expect(captured.join('')).toMatch(/NOT MEASURABLE/i);
474
+ });
475
+
284
476
  it('defaults to a dry run and does not modify files', () => {
285
477
  write(TMP_REPO, 'githooks/lib/a.sh', 'echo canonical\n');
286
478
  write(TMP_REPO, 'src/npm-package/hooks/lib/a.sh', 'echo stale\n');
@@ -337,3 +529,193 @@ describe('syncMirrors — AC-713-04 safety', () => {
337
529
  }
338
530
  });
339
531
  });
532
+
533
+ describe('KNOWN_PAIRS — P1-E the pair list is derived, not hand-copied', () => {
534
+ it('every mirror directory the package ships is supervised by something', () => {
535
+ // The real question, and the one a naive version of this case got wrong.
536
+ //
537
+ // Asserting "KNOWN_PAIRS contains every GENERATED_TREES row" is VACUOUS:
538
+ // KNOWN_PAIRS is derived from GENERATED_TREES, so it holds by construction.
539
+ // Verified by deleting the mock-policy row from the generator -- the assertion
540
+ // still passed, which is proof it could never fail.
541
+ //
542
+ // What actually went wrong historically is different: `mutation` and
543
+ // `mock-policy` were in the package's `files[]` with NO generator and NO pair,
544
+ // so nothing ever compared them against their source. That state is
545
+ // undetectable from inside the pair list; it needs the SHIPPED list as the
546
+ // starting set. Every shipped mirror directory must be either generated or
547
+ // explicitly hand-listed -- silence is the bug.
548
+ const pkg = require('../../package.json');
549
+ const { GENERATED_TREES } = require('../../scripts/generate-mirrors.cjs');
550
+ // Directory-shaped `files[]` entries (a trailing slash). Loose files like
551
+ // `gate-3.sh` and `adapter-common.sh` are mirrors of individual gates and are
552
+ // covered by HOOK_MIRROR_FILES in check-hook-mirror.sh, not by this table.
553
+ const shippedDirs = pkg.files
554
+ .filter((f) => f.endsWith('/'))
555
+ .map((f) => f.replace(/\/$/, ''))
556
+ // Only entries that have a canonical counterpart under the repo are mirror
557
+ // pairs; `bin/`, `lib/`, `scripts/` are this package's own source.
558
+ .filter((d) => MIRROR_DIR_CANDIDATES.some((c) => d === c));
559
+
560
+ const generated = new Set(GENERATED_TREES.map((t) => t.mirror));
561
+ const handListed = new Set(HAND_LISTED_MIRRORS);
562
+ const unsupervised = shippedDirs.filter((d) => !generated.has(d) && !handListed.has(d));
563
+ expect(unsupervised).toEqual([]);
564
+ });
565
+
566
+ it('declares no pair whose mirror the package does not ship', () => {
567
+ // The other direction. A pair pointing at a directory absent from `files[]`
568
+ // would check something that never reaches a consumer, which reads as coverage
569
+ // without being any.
570
+ const pkg = require('../../package.json');
571
+ // KNOWN_PAIRS holds REPO-relative paths; files[] holds PACKAGE-relative ones.
572
+ const PRE = 'src/npm-package/';
573
+ // files[] entries carry a trailing slash ('adapters/'). Comparing the raw
574
+ // strings reports EVERY pair as unshipped -- which is exactly how this case
575
+ // failed on its first version, and why the check is written against the
576
+ // normalised entry rather than trusting the two lists to be spelled alike.
577
+ const shipped = (p) =>
578
+ p.startsWith(PRE) &&
579
+ pkg.files.some((f) => {
580
+ const entry = f.replace(/\/$/, '');
581
+ const rel = p.slice(PRE.length);
582
+ return rel === entry || rel.startsWith(`${entry}/`);
583
+ });
584
+ for (const pair of KNOWN_PAIRS) {
585
+ expect(shipped(pair.mirror)).toBe(true);
586
+ }
587
+ });
588
+
589
+ it('has no duplicate canonical tree', () => {
590
+ const canonicals = KNOWN_PAIRS.map((p) => p.canonical);
591
+ expect(new Set(canonicals).size).toBe(canonicals.length);
592
+ });
593
+ });
594
+
595
+ describe('assertPairsResolved — a zero-pair result must not read as a pass', () => {
596
+ // Resolve the checkout once, and fail here rather than deep inside an assertion.
597
+ // The review flagged the bare `path.resolve(__dirname, '..', '..', '..', '..')`
598
+ // used inline: if this suite ever moves, that walk lands somewhere else and the
599
+ // failure surfaces as `Cannot read properties of null` on a toMatch, which reads
600
+ // like a logic bug in the guard rather than a broken path in the test.
601
+ const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
602
+ const generatorPath = path.join(repoRoot, 'src', 'npm-package', 'scripts', 'generate-mirrors.cjs');
603
+
604
+ it('can locate the checkout it claims to test', () => {
605
+ expect(fs.existsSync(generatorPath)).toBe(true);
606
+ });
607
+
608
+ it('reports a usable pair list for this checkout', () => {
609
+ // Every pair is derived from generate-mirrors.cjs now, so if that file moved or
610
+ // stopped exporting GENERATED_TREES, KNOWN_PAIRS would be empty and every query
611
+ // would answer "nothing drifted" -- a perfect score from a broken tool.
612
+ expect(KNOWN_PAIRS.length).toBeGreaterThan(0);
613
+ expect(assertPairsResolved(repoRoot)).toBeNull();
614
+ });
615
+
616
+ it('refuses to stay quiet over zero pairs in a checkout', () => {
617
+ // The error branch used to be unreachable: it read the module-level KNOWN_PAIRS,
618
+ // resolved once at require time against this repository, so no fixture could
619
+ // make it empty and no test could execute it. `pairs` is now a parameter with
620
+ // KNOWN_PAIRS as its default, which is what makes this case possible at all.
621
+ //
622
+ // This branch is the whole reason the function exists: it is the guard against a
623
+ // broken generator reporting a perfect score.
624
+ const message = assertPairsResolved(repoRoot, []);
625
+ expect(message).toMatch(/ZERO mirror pairs/);
626
+ expect(message).toContain('generate-mirrors.cjs');
627
+ // And a non-empty list stays silent -- the contrast that makes the message mean
628
+ // "zero", not "something is wrong here".
629
+ expect(assertPairsResolved(repoRoot, [{ canonical: 'a', mirror: 'b' }])).toBeNull();
630
+ });
631
+
632
+ it('keeps KNOWN_PAIRS as the default so call sites need no argument', () => {
633
+ // The parameterisation must not change behaviour for existing callers: the only
634
+ // production call passes just a root.
635
+ //
636
+ // Written first as `assertPairsResolved(root) === assertPairsResolved(root,
637
+ // KNOWN_PAIRS)`, which the review correctly called near-tautological: that is
638
+ // just what a default parameter means, and if the generator were absent BOTH
639
+ // calls would return null and the assertion would pass having checked nothing.
640
+ // The precondition below removes that vacuous pass, and the real signal is that
641
+ // the default must behave like the non-empty list -- i.e. stay silent.
642
+ expect(KNOWN_PAIRS.length).toBeGreaterThan(0);
643
+ expect(fs.existsSync(path.join(repoRoot, 'src', 'npm-package', 'scripts', 'generate-mirrors.cjs'))).toBe(true);
644
+
645
+ const byDefault = assertPairsResolved(repoRoot);
646
+ const explicit = assertPairsResolved(repoRoot, KNOWN_PAIRS);
647
+ expect(byDefault).toBeNull();
648
+ expect(explicit).toBeNull();
649
+
650
+ // Attribution, not just "non-null". A bare `not.toBeNull()` on the [] case was
651
+ // rejected by review as proving only that the function CAN return a string --
652
+ // it does not tie the null above to the pair count rather than to the
653
+ // generator-missing early return. So each of the three outcomes is pinned to the
654
+ // specific branch that produces it:
655
+ //
656
+ // default / explicit, generator present, pairs non-empty -> null (silent)
657
+ // [], generator present -> reason string
658
+ // anything, generator ABSENT -> null (not a checkout)
659
+ const zeroPairsReason = assertPairsResolved(repoRoot, []);
660
+ expect(zeroPairsReason).toContain('generate-mirrors.cjs');
661
+ expect(zeroPairsReason).toMatch(/ZERO mirror pairs/);
662
+
663
+ // The generator-missing early return is a DIFFERENT no-answer: a directory with
664
+ // no generator is not a checkout, so null there must not be read as the same
665
+ // fact as null above. This is the branch that made `not.toBeNull()` alone
666
+ // insufficient -- without it, "null" had two causes and neither was pinned.
667
+ const notACheckout = fs.mkdtempSync(path.join(os.tmpdir(), 'xp-nogen-'));
668
+ expect(assertPairsResolved(notACheckout, [])).toBeNull();
669
+ expect(assertPairsResolved(notACheckout)).toBeNull();
670
+ });
671
+
672
+ it('tells a checkout apart from a non-checkout by the generator file', () => {
673
+ // Scope note: KNOWN_PAIRS is a module-level constant, resolved once at require
674
+ // time against THIS repository. A fixture directory therefore cannot make it
675
+ // empty, so a case that expected "fake repo with a broken generator returns an
676
+ // error" would assert something the design cannot do -- it failed on the first
677
+ // attempt for exactly that reason.
678
+ //
679
+ // What IS decidable from a given root is the precondition the assertion turns
680
+ // on: a checkout has the generator, a non-checkout does not. That is the half
681
+ // this case pins; the empty-list message itself is exercised by
682
+ // handleSyncMirrors when collectMirrorPairs returns nothing.
683
+ const fakeCheckout = fs.mkdtempSync(path.join(os.tmpdir(), 'xp-checkout-'));
684
+ fs.mkdirSync(path.join(fakeCheckout, 'src', 'npm-package', 'scripts'), { recursive: true });
685
+ fs.writeFileSync(
686
+ path.join(fakeCheckout, 'src', 'npm-package', 'scripts', 'generate-mirrors.cjs'),
687
+ 'module.exports = {};\n'
688
+ );
689
+ const fakeInstall = fs.mkdtempSync(path.join(os.tmpdir(), 'xp-install-'));
690
+
691
+ // In this process the pair list is non-empty, so neither root reports a
692
+ // problem -- and that is correct: the list is what matters, not the root.
693
+ expect(assertPairsResolved(fakeCheckout)).toBeNull();
694
+ expect(assertPairsResolved(fakeInstall)).toBeNull();
695
+
696
+ // The distinguishing fact the implementation reads.
697
+ expect(fs.existsSync(path.join(fakeCheckout, 'src', 'npm-package', 'scripts', 'generate-mirrors.cjs'))).toBe(true);
698
+ expect(fs.existsSync(path.join(fakeInstall, 'src', 'npm-package', 'scripts', 'generate-mirrors.cjs'))).toBe(false);
699
+ });
700
+
701
+ it('stays quiet outside a checkout, where zero pairs is legitimate', () => {
702
+ // An installed package ships lib/ but not scripts/, and the canonical trees it
703
+ // would compare against are not installed either. Reporting an error there
704
+ // would make a correct install look broken.
705
+ //
706
+ // `toBeNull()` alone cannot fail in a way that distinguishes "correctly quiet"
707
+ // from "quiet for any reason at all", so this also asserts what the function is
708
+ // NOT doing: the directory must genuinely lack a generator, or the null would
709
+ // be meaningless. Verified by the companion case above, where the same call on
710
+ // a root that DOES have one still returns null only because the pair list is
711
+ // non-empty -- the two facts together pin the contract.
712
+ const emptyDir = fs.mkdtempSync(path.join(os.tmpdir(), 'xp-nocheckout-'));
713
+ expect(assertPairsResolved(emptyDir)).toBeNull();
714
+ expect(
715
+ fs.existsSync(path.join(emptyDir, 'src', 'npm-package', 'scripts', 'generate-mirrors.cjs'))
716
+ ).toBe(false);
717
+ // And the real repository, which has the generator and a resolved pair list,
718
+ // also reports no problem -- the contrast that makes the null above meaningful.
719
+ expect(assertPairsResolved(repoRoot)).toBeNull();
720
+ });
721
+ });
@@ -601,7 +601,21 @@ const GATE_REGISTRY = {
601
601
  aliases: ['duplicates', 'dup', '2'],
602
602
  run: (targetPath) => {
603
603
  const args = buildJscpdArgs({ targetPath });
604
- runJscpd(args, targetPath);
604
+ const outcome = runJscpd(args, targetPath);
605
+ // A run that examined nothing is NOT a pass. Say so with a reason, per the
606
+ // SKIP contract both AGENTS.md files state: "not checked" must never be
607
+ // recordable as "passed". Returning GATE_NOT_RUN is how the rest of this file
608
+ // reports the same condition, so the caller records a skip rather than a
609
+ // verdict it never computed.
610
+ if (outcome === 'no_files') {
611
+ process.stderr.write(
612
+ '[gate-2] SKIP: jscpd analysed no files, so "Found 0 clones" is not a verdict. '
613
+ + 'Check --ignore / --format / --pattern, or whether the target holds any '
614
+ + 'source this tool recognises.\n',
615
+ );
616
+ return GATE_NOT_RUN;
617
+ }
618
+ return undefined;
605
619
  },
606
620
  },
607
621
  '3': {
@@ -1006,6 +1020,62 @@ function buildGlobalPrefixes(env) {
1006
1020
  return prefixes.filter((p) => typeof p === 'string' && p.length > 0 && path.isAbsolute(p));
1007
1021
  }
1008
1022
 
1023
+ /**
1024
+ * What a finished jscpd run tells us, as one of four values.
1025
+ *
1026
+ * `no_files` is the one that did not exist before #855, and it is the reason this
1027
+ * function exists at all. jscpd answers "Found 0 clones" for a run that examined
1028
+ * nothing and for a run that examined the change set and found it clean -- the two
1029
+ * are only the same statement if something was actually read. It says so itself, on
1030
+ * the stream, and exits 0 anyway:
1031
+ *
1032
+ * Warning: jscpd analyzed no files: check the paths and the --format, --ignore
1033
+ * and --pattern filters
1034
+ * Found 0 clones.
1035
+ *
1036
+ * Measured on a fixture below jscpd's min-tokens floor: `xp-gate check 2` printed
1037
+ * "No duplicates found.", a table with `Files analyzed: 0`, and exited 0. A gate
1038
+ * that examined nothing was indistinguishable from a passing one.
1039
+ *
1040
+ * "error" is wrong here (jscpd did not fail), "clean" is wrong (nothing was read),
1041
+ * and "duplicates" is wrong. Hence a fourth value.
1042
+ *
1043
+ * The shell hook has classified this way since gate-2-no-files.test.bats landed
1044
+ * (`classify_jscpd_run` in githooks/lib/jscpd-run.sh); the CLI path had no
1045
+ * equivalent, which is how #855 survived its fix.
1046
+ *
1047
+ * @param {number|null} exitCode jscpd's exit status
1048
+ * @param {string} output the combined stdout+stderr
1049
+ * @returns {'error'|'no_files'|'duplicates'|'clean'}
1050
+ */
1051
+ function classifyJscpdRun(exitCode, output) {
1052
+ const text = typeof output === 'string' ? output : '';
1053
+ // A failed run is the more specific fact, so it is checked first: reporting it as
1054
+ // no_files would hide a genuine tool failure behind a plausible excuse.
1055
+ if (typeof exitCode !== 'number' || exitCode !== 0) return 'error';
1056
+
1057
+ const clones = text.match(/Found (\d+) (?:clones?|duplicates?)/i);
1058
+ if (clones) {
1059
+ // A clone count above zero is a finding whatever else the run said.
1060
+ if (Number.parseInt(clones[1], 10) > 0) return 'duplicates';
1061
+ // Zero clones is a verdict only when files were read.
1062
+ if (/analyzed no files/i.test(text)) return 'no_files';
1063
+ const scanned = text.match(/\|\s*Total:\s*\|\s*(\d+)\s*\|/i);
1064
+ if (scanned && Number.parseInt(scanned[1], 10) === 0) return 'no_files';
1065
+ return 'clean';
1066
+ }
1067
+
1068
+ // No clone line at all. A report with any content other than a bare timing is
1069
+ // still treated as a scan that happened -- turning every unfamiliar future jscpd
1070
+ // format into a SKIP would disable the gate for everyone.
1071
+ const meaningful = text
1072
+ .split('\n')
1073
+ .map((line) => line.trim())
1074
+ .filter((line) => line.length > 0 && !/^time:\s/i.test(line));
1075
+ if (meaningful.length === 0) return 'no_files';
1076
+ return 'clean';
1077
+ }
1078
+
1009
1079
  function runJscpd(args, targetPath) {
1010
1080
  // Invoke jscpd's JS entry through `node` rather than going through .bin/.
1011
1081
  //
@@ -1059,13 +1129,22 @@ function runJscpd(args, targetPath) {
1059
1129
  ];
1060
1130
  for (const [cmd, cmdArgs] of attempts) {
1061
1131
  try {
1062
- // cwd must be the scan target. Without it jscpd runs in whatever directory
1063
- // the CLI was started from, so `xp-gate check 2 <path>` scanned the CURRENT
1064
- // directory and the path argument only affected its display name. Measured:
1065
- // a 3-copy fixture reported 0 clones because the scan happened in the xp-gate
1066
- // repo instead of the fixture.
1067
- execFileSync(cmd, cmdArgs, { stdio: 'inherit', cwd: targetPath || process.cwd() });
1068
- return;
1132
+ // The stream is CAPTURED and replayed rather than inherited, because the
1133
+ // verdict depends on what jscpd said. With `stdio: 'inherit'` the output went
1134
+ // straight to the terminal, so the gate could not tell a clean scan from one
1135
+ // that examined nothing -- #855. Replaying it immediately keeps the operator's
1136
+ // view identical to before.
1137
+ const result = spawnSync(cmd, cmdArgs, {
1138
+ cwd: targetPath || process.cwd(),
1139
+ encoding: 'utf8',
1140
+ maxBuffer: 64 * 1024 * 1024,
1141
+ });
1142
+ if (result.stdout) process.stdout.write(result.stdout);
1143
+ if (result.stderr) process.stderr.write(result.stderr);
1144
+ if (result.error) throw result.error;
1145
+ const outcome = classifyJscpdRun(result.status, `${result.stdout || ''}\n${result.stderr || ''}`);
1146
+ if (result.status === 0) return outcome;
1147
+ lastErr = new Error(`jscpd exited ${result.status}`);
1069
1148
  } catch (err) {
1070
1149
  lastErr = err;
1071
1150
  }
@@ -1297,7 +1376,7 @@ async function invokeGateHandler(gateId, gate, targetPath, opts) {
1297
1376
  }
1298
1377
  }
1299
1378
 
1300
- module.exports = { findConsumerGateVariant, GATE_REGISTRY, GATE_NOT_RUN, getGateInfo, getAllGates, runGate, resolveAlias, getAliases, runGateAdapter, findTsGateEntry, resolveGateScript, resolveTsxInvocation, findTsxCli, findJscpdEntry, buildJscpdArgs, JSCPD_THRESHOLD, buildGlobalPrefixes, SOURCE_FILE_EXTENSIONS, resolveAllModeChangedFiles, describeEmptyChangedFiles, describeChangedFilesScope,
1379
+ module.exports = { findConsumerGateVariant, GATE_REGISTRY, GATE_NOT_RUN, classifyJscpdRun, getGateInfo, getAllGates, runGate, resolveAlias, getAliases, runGateAdapter, findTsGateEntry, resolveGateScript, resolveTsxInvocation, findTsxCli, findJscpdEntry, buildJscpdArgs, JSCPD_THRESHOLD, buildGlobalPrefixes, SOURCE_FILE_EXTENSIONS, resolveAllModeChangedFiles, describeEmptyChangedFiles, describeChangedFilesScope,
1301
1380
  // AC-670-03 asserts the --all preamble END-TO-END: that an all-mode run hands
1302
1381
  // the security gates a non-empty file population sourced from the list file and
1303
1382
  // sets GATE_SCOPE="all". With these unexported, the case could only take its
@@ -2,6 +2,6 @@
2
2
  "verdict": "PASS",
3
3
  "license_type": "MIT",
4
4
  "sha256": "0e7ac423bf2c6e223b7c5b156f8cf72da49d748e56a1641402c31f22ad07dbb5",
5
- "checked_at": "2026-11-21T00:43:28.208Z",
5
+ "checked_at": "2026-11-21T14:57:53.211Z",
6
6
  "source": "network"
7
7
  }