mandrel 2.88.0 → 2.89.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 (29) hide show
  1. package/.agents/docs/agentrc-reference.json +3 -1
  2. package/.agents/docs/configuration.md +2 -0
  3. package/.agents/schemas/agentrc.schema.json +13 -0
  4. package/.agents/scripts/coverage-capture.js +32 -49
  5. package/.agents/scripts/lib/audit-baselines/gate-surface.js +24 -11
  6. package/.agents/scripts/lib/audit-baselines/read.js +4 -2
  7. package/.agents/scripts/lib/audit-baselines/surface-entry.js +95 -11
  8. package/.agents/scripts/lib/baselines/crap-preview-scan.js +3 -1
  9. package/.agents/scripts/lib/baselines/crap-updater-cli.js +2 -0
  10. package/.agents/scripts/lib/baselines/kinds/crap.js +37 -16
  11. package/.agents/scripts/lib/baselines/preview-gates.js +1 -0
  12. package/.agents/scripts/lib/close-validation/gates.js +40 -9
  13. package/.agents/scripts/lib/config/gates/crap.schema.js +15 -0
  14. package/.agents/scripts/lib/config/quality.js +49 -1
  15. package/.agents/scripts/lib/coverage-capture-affected.js +2 -1
  16. package/.agents/scripts/lib/coverage-capture-fullscope.js +2 -2
  17. package/.agents/scripts/lib/coverage-capture-incremental.js +1 -1
  18. package/.agents/scripts/lib/coverage-capture-passes.js +143 -0
  19. package/.agents/scripts/lib/coverage-capture-usage.js +1 -1
  20. package/.agents/scripts/lib/coverage-capture.js +30 -15
  21. package/.agents/scripts/lib/crap-utils.js +6 -2
  22. package/.agents/scripts/lib/generated/agentrc-validator.js +1 -1
  23. package/.agents/scripts/lib/maintainability-utils.js +6 -1
  24. package/.agents/scripts/lib/orchestration/check-baselines/phases/evaluate.js +1 -1
  25. package/.agents/scripts/lib/orchestration/verify-credit.js +4 -1
  26. package/.agents/scripts/update-crap-baseline.js +2 -2
  27. package/.agents/workflows/audit-baselines.md +7 -2
  28. package/docs/CHANGELOG.md +13 -0
  29. package/package.json +1 -1
@@ -49,6 +49,21 @@ export const CRAP_GATE = {
49
49
  default: [...CRAP_GATE_DEFAULTS.ignoreGlobs],
50
50
  },
51
51
  incrementalCoverage: INCREMENTAL_COVERAGE_SCHEMA,
52
+ coveragePath: {
53
+ type: ['string', 'null'],
54
+ minLength: 1,
55
+ description:
56
+ "Repo-relative Istanbul `coverage-final.json` the CRAP gate joins against (Story #5717). `null` or unset falls back to `delivery.quality.gates.coverage.coveragePath`. When it differs from that path the CRAP gate has its OWN coverage source: `coverage-capture.js` refreshes it full-scope via `captureScript`, and the handoff seat, close preview and `update-crap-baseline.js` read it. It must live in a different directory from the coverage gate's artifact (the capture stamp is per directory); a same-directory path is refused at config resolution.",
57
+ default: null,
58
+ },
59
+ captureScript: {
60
+ type: 'string',
61
+ minLength: 1,
62
+ pattern: '^[A-Za-z0-9@:._/-]+$',
63
+ description:
64
+ 'npm script name (run as `npm run <script>`, never a shell command) that writes the CRAP coverage artifact. Close registers the coverage-capture gate, and the capture refusal and credited-run probe key, on this script.',
65
+ default: CRAP_GATE_DEFAULTS.captureScript,
66
+ },
52
67
  },
53
68
  additionalProperties: false,
54
69
  };
@@ -58,6 +58,8 @@ export const CRAP_GATE_DEFAULTS = Object.freeze({
58
58
  // Healthy runs resolve ~98%; a coordinate mismatch resolves 4–6%.
59
59
  minMethodResolutionRate: 0.75,
60
60
  incrementalCoverage: DEFAULT_INCREMENTAL_COVERAGE,
61
+ // npm script that writes the CRAP coverage artifact (never a shell command).
62
+ captureScript: 'test:coverage',
61
63
  });
62
64
 
63
65
  export const COVERAGE_GATE_DEFAULTS = Object.freeze({
@@ -96,6 +98,8 @@ const CRAP_GATE_KEYS = new Set([
96
98
  'ignoreGlobs',
97
99
  'minMethodResolutionRate',
98
100
  'incrementalCoverage',
101
+ 'coveragePath',
102
+ 'captureScript',
99
103
  ]);
100
104
 
101
105
  const COVERAGE_GATE_KEYS = new Set([
@@ -176,6 +180,48 @@ function resolveIncrementalCoverage(user, defaults) {
176
180
  };
177
181
  }
178
182
 
183
+ function nonEmptyString(value) {
184
+ return typeof value === 'string' && value.trim().length > 0
185
+ ? value.trim()
186
+ : null;
187
+ }
188
+
189
+ /** Directory part of a repo-relative path, separator-agnostic. */
190
+ function artifactDir(relPath) {
191
+ const parts = relPath.replace(/\\/g, '/').split('/').filter(Boolean);
192
+ parts.pop();
193
+ return parts.filter((p) => p !== '.').join('/');
194
+ }
195
+
196
+ /**
197
+ * The CRAP gate's coverage source. A user `coveragePath` that
198
+ * differs from the coverage gate's is an OWN source; one in the same
199
+ * directory would share the per-directory capture stamp and cross-credit
200
+ * freshness, so it is refused here rather than silently falling back.
201
+ *
202
+ * @param {{ coveragePath?: unknown, captureScript?: unknown }} userCrap
203
+ * @param {string} coverageGatePath
204
+ * @returns {{ coveragePath: string, captureScript: string, ownCoverageSource: boolean }}
205
+ */
206
+ function resolveCrapCoverageSource(userCrap, coverageGatePath) {
207
+ const userPath = nonEmptyString(userCrap?.coveragePath);
208
+ const captureScript =
209
+ nonEmptyString(userCrap?.captureScript) ?? CRAP_GATE_DEFAULTS.captureScript;
210
+ const coveragePath = userPath ?? coverageGatePath;
211
+ const ownCoverageSource =
212
+ userPath !== null &&
213
+ userPath.replace(/\\/g, '/') !== coverageGatePath.replace(/\\/g, '/');
214
+ if (
215
+ ownCoverageSource &&
216
+ artifactDir(userPath) === artifactDir(coverageGatePath)
217
+ ) {
218
+ throw new Error(
219
+ `[config] delivery.quality.gates.crap.coveragePath '${userPath}' sits in the same directory as delivery.quality.gates.coverage.coveragePath '${coverageGatePath}'. The capture stamp is per directory, so an own CRAP coverage source must live in its own directory.`,
220
+ );
221
+ }
222
+ return { coveragePath, captureScript, ownCoverageSource };
223
+ }
224
+
179
225
  /**
180
226
  * @param {object | undefined} userCrap
181
227
  * @param {{ coveragePath: string }} coverageGate
@@ -195,6 +241,8 @@ export function resolveMaintainabilityCrap(userCrap, coverageGate) {
195
241
  targetDirs: [...defaults.targetDirs],
196
242
  newMethodCeiling: defaults.newMethodCeiling,
197
243
  coveragePath: coverage.coveragePath,
244
+ captureScript: defaults.captureScript,
245
+ ownCoverageSource: false,
198
246
  tolerance: toleranceScalar(
199
247
  defaults.tolerance,
200
248
  DEFAULT_CRAP_TOLERANCE.value,
@@ -213,7 +261,7 @@ export function resolveMaintainabilityCrap(userCrap, coverageGate) {
213
261
  enabled: userCrap.enabled ?? defaults.enabled,
214
262
  targetDirs: resolveListValue(defaults.targetDirs, userCrap.targetDirs),
215
263
  newMethodCeiling: userCrap.newMethodCeiling ?? defaults.newMethodCeiling,
216
- coveragePath: coverage.coveragePath,
264
+ ...resolveCrapCoverageSource(userCrap, coverage.coveragePath),
217
265
  tolerance: toleranceScalar(
218
266
  userCrap.tolerance,
219
267
  toleranceScalar(defaults.tolerance, DEFAULT_CRAP_TOLERANCE.value),
@@ -185,7 +185,8 @@ function writeMerged({
185
185
  */
186
186
  async function captureAndMerge(opts) {
187
187
  const run = await runScoped(opts);
188
- if (run.code !== 0) return reportCaptureFailure(run.code, opts.logger);
188
+ if (run.code !== 0)
189
+ return reportCaptureFailure(run.code, opts.logger, AFFECTED_CAPTURE_SCRIPT);
189
190
  if (!writeMerged(opts)) return 1;
190
191
  stampCapturedTree({
191
192
  ...opts,
@@ -96,7 +96,7 @@ export async function runFullScopeCapture({
96
96
  }
97
97
 
98
98
  logger.info(
99
- `[coverage-capture] Coverage at ${crap.coveragePath} is ${describeFreshness(freshness, crap.targetDirs)}; running npm run test:coverage… ${detail}`,
99
+ `[coverage-capture] Coverage at ${crap.coveragePath} is ${describeFreshness(freshness, crap.targetDirs)}; running npm run ${crap.captureScript ?? 'test:coverage'}… ${detail}`,
100
100
  );
101
101
  const tree = snapshotCapturedTree({
102
102
  cwd: args.cwd,
@@ -112,7 +112,7 @@ export async function runFullScopeCapture({
112
112
  // After queueing behind another suite, which may have stamped this tree.
113
113
  recheckFresh: () => probe().fresh === true,
114
114
  });
115
- if (code !== 0) return reportCaptureFailure(code, logger);
115
+ if (code !== 0) return reportCaptureFailure(code, logger, crap.captureScript);
116
116
 
117
117
  stampCapturedTree({
118
118
  ...tree,
@@ -113,7 +113,7 @@ export async function tryIncrementalCapture({
113
113
  log: (m) => logger.info(m),
114
114
  recheckFresh: () => probe().fresh === true,
115
115
  });
116
- if (code !== 0) return reportCaptureFailure(code, logger);
116
+ if (code !== 0) return reportCaptureFailure(code, logger, crap.captureScript);
117
117
 
118
118
  stampCapturedTree({
119
119
  ...tree,
@@ -0,0 +1,143 @@
1
+ /**
2
+ * The ordered capture passes for `coverage-capture.js` and the capture-script
3
+ * refusal. A shared source is one pass: the existing pipeline, scoped modes
4
+ * included. An own CRAP source (`crap.ownCoverageSource`) first refreshes the
5
+ * coverage gate's artifact exactly as before, then gives the CRAP artifact
6
+ * its own full-scope pass via `crap.captureScript`, stamped in its own
7
+ * directory. Scoped capture modes never apply to the own source.
8
+ */
9
+ import { runFullScopeCapture } from './coverage-capture-fullscope.js';
10
+
11
+ const COVERAGE_GATE_SCRIPT = 'test:coverage';
12
+ const COVERAGE_PATH_KEY = 'delivery.quality.gates.coverage.coveragePath';
13
+ const CRAP_PATH_KEY = 'delivery.quality.gates.crap.coveragePath';
14
+ const CRAP_SCRIPT_KEY = 'delivery.quality.gates.crap.captureScript';
15
+
16
+ /**
17
+ * The exit code when no capture may run: the CRAP gate is off (0), or its
18
+ * capture script is missing from `package.json` (1, naming the fix).
19
+ *
20
+ * @param {{ crap: object, scripts: object, hasNpmScriptImpl: Function, logger: object }} opts
21
+ * @returns {number|null}
22
+ */
23
+ export function captureRefusal({ crap, scripts, hasNpmScriptImpl, logger }) {
24
+ if (crap.enabled === false) {
25
+ logger.info('[coverage-capture] CRAP gate disabled — skipping capture.');
26
+ return 0;
27
+ }
28
+ const script = crap.captureScript ?? COVERAGE_GATE_SCRIPT;
29
+ if (hasNpmScriptImpl(scripts, script)) return null;
30
+ // Name the fix rather than propagate npm's opaque "Missing script" exit.
31
+ logger.error(
32
+ `[coverage-capture] ✖ No "${script}" script in package.json (the CRAP capture script, set by ${CRAP_SCRIPT_KEY}). ` +
33
+ `Add one (e.g. "${script}": "node --test --experimental-test-coverage"), ` +
34
+ `point ${CRAP_SCRIPT_KEY} at an existing script, ` +
35
+ 'or disable the CRAP gate via delivery.quality.gates.crap.enabled=false.',
36
+ );
37
+ return 1;
38
+ }
39
+
40
+ /**
41
+ * @param {{ crap: object, coverage: object, scripts: object, hasNpmScriptImpl: Function, logger: object }} opts
42
+ * @returns {Array<{ crap: object, configKey: string, own: boolean }>}
43
+ */
44
+ function planPasses({ crap, coverage, scripts, hasNpmScriptImpl, logger }) {
45
+ if (crap.ownCoverageSource !== true) {
46
+ return [{ crap, configKey: COVERAGE_PATH_KEY, own: false }];
47
+ }
48
+ const ownPass = { crap, configKey: CRAP_PATH_KEY, own: true };
49
+ if (!hasNpmScriptImpl(scripts, COVERAGE_GATE_SCRIPT)) {
50
+ logger.info(
51
+ `[coverage-capture] No "${COVERAGE_GATE_SCRIPT}" script — skipping the coverage gate's capture; only the CRAP source at ${crap.coveragePath} is refreshed.`,
52
+ );
53
+ return [ownPass];
54
+ }
55
+ const gateCrap = {
56
+ ...crap,
57
+ coveragePath: coverage.coveragePath,
58
+ captureScript: COVERAGE_GATE_SCRIPT,
59
+ };
60
+ return [
61
+ { crap: gateCrap, configKey: COVERAGE_PATH_KEY, own: false },
62
+ ownPass,
63
+ ];
64
+ }
65
+
66
+ /**
67
+ * A capture runner that defaults `script` to the pass's capture script; an
68
+ * explicit `script` (the affected scope's) still wins.
69
+ *
70
+ * @param {Function} capture
71
+ * @param {string|undefined} script
72
+ * @returns {Function}
73
+ */
74
+ function withDefaultScript(capture, script) {
75
+ return script ? (opts) => capture({ script, ...opts }) : capture;
76
+ }
77
+
78
+ /**
79
+ * Run each planned pass; the first non-zero pass wins and no later pass runs.
80
+ *
81
+ * @param {{
82
+ * crap: object,
83
+ * coverage: object,
84
+ * args: object,
85
+ * scripts: object,
86
+ * hasNpmScriptImpl: Function,
87
+ * makeCapture: (source: { coveragePath: string, script?: string, configKey: string }) => Function,
88
+ * runShared: (opts: object) => Promise<number>,
89
+ * deps: object,
90
+ * }} opts
91
+ * @returns {Promise<number>}
92
+ */
93
+ export async function runCapturePasses({
94
+ crap,
95
+ coverage,
96
+ args,
97
+ scripts,
98
+ hasNpmScriptImpl,
99
+ makeCapture,
100
+ runShared,
101
+ deps,
102
+ }) {
103
+ const { logger } = deps;
104
+ const passes = planPasses({
105
+ crap,
106
+ coverage,
107
+ scripts,
108
+ hasNpmScriptImpl,
109
+ logger,
110
+ });
111
+ for (const pass of passes) {
112
+ const capture = withDefaultScript(
113
+ makeCapture({
114
+ coveragePath: pass.crap.coveragePath,
115
+ script: pass.crap.captureScript,
116
+ configKey: pass.configKey,
117
+ }),
118
+ pass.crap.captureScript,
119
+ );
120
+ const code = pass.own
121
+ ? await runOwnSource({ crap: pass.crap, coverage, args, capture, deps })
122
+ : await runShared({ crap: pass.crap, coverage, args, capture, deps });
123
+ if (code !== 0) return code;
124
+ }
125
+ return 0;
126
+ }
127
+
128
+ /**
129
+ * @param {{ crap: object, coverage: object, args: object, capture: Function, deps: object }} opts
130
+ * @returns {Promise<number>}
131
+ */
132
+ function runOwnSource({ crap, coverage, args, capture, deps }) {
133
+ deps.logger.info(
134
+ `[coverage-capture] CRAP gate has its own coverage source (${CRAP_PATH_KEY}=${crap.coveragePath}, npm run ${crap.captureScript}).`,
135
+ );
136
+ return runFullScopeCapture({
137
+ ...deps,
138
+ crap,
139
+ coverage,
140
+ args,
141
+ runCaptureImpl: capture,
142
+ });
143
+ }
@@ -14,7 +14,7 @@ const COVERAGE_CAPTURE_USAGE = {
14
14
  invocation:
15
15
  'node .agents/scripts/coverage-capture.js [--skip-when-no-crap-files] [--require-credited] [--ref <git-ref>] [--cwd <path>]',
16
16
  summary:
17
- 'Ensure coverage/coverage-final.json is present and fresh before the CRAP gate fires, spawning `npm run test:coverage` only when it is stale (`npm run test:coverage:affected`, with the base ref in MANDREL_COVERAGE_BASE_REF, under delivery.quality.gates.coverage.captureScope="affected"). Writes a content-digest capture stamp that close-validation reads to skip a redundant re-run.',
17
+ 'Ensure coverage/coverage-final.json is present and fresh before the CRAP gate fires, spawning `npm run test:coverage` only when it is stale (`npm run test:coverage:affected`, with the base ref in MANDREL_COVERAGE_BASE_REF, under delivery.quality.gates.coverage.captureScope="affected"). Writes a content-digest capture stamp that close-validation reads to skip a redundant re-run. When delivery.quality.gates.crap.coveragePath names its own artifact, that artifact is then refreshed full-scope with `npm run <delivery.quality.gates.crap.captureScript>` and stamped in its own directory.',
18
18
  flags: [
19
19
  [
20
20
  '--skip-when-no-crap-files',
@@ -486,11 +486,15 @@ export function creditedCapture(runCaptureFn, { requireCredited, logger }) {
486
486
  * @param {string} abs Absolute artifact path that was looked for.
487
487
  * @returns {string}
488
488
  */
489
- function missingCoverageArtifactMessage(abs) {
489
+ function missingCoverageArtifactMessage(
490
+ abs,
491
+ script = 'test:coverage',
492
+ configKey = 'delivery.quality.gates.coverage.coveragePath',
493
+ ) {
490
494
  return (
491
- `coverage artifact not found at ${abs}. \`npm run test:coverage\` must ` +
495
+ `coverage artifact not found at ${abs}. \`npm run ${script}\` must ` +
492
496
  'write Istanbul per-file JSON (the `json` reporter → coverage-final.json) ' +
493
- 'at delivery.quality.gates.coverage.coveragePath; a `json-summary` ' +
497
+ `at ${configKey}; a \`json-summary\` ` +
494
498
  '(coverage-summary.json) alone cannot feed the coverage or CRAP baselines.'
495
499
  );
496
500
  }
@@ -513,17 +517,20 @@ export class MissingCoverageArtifactError extends Error {
513
517
  /**
514
518
  * Wrap a capture runner so a green run that left no artifact at
515
519
  * `coveragePath` exits 1: a broken capture contract, not a capture. Every
516
- * capture path already returns before stamping on a non-zero code.
520
+ * capture path already returns before stamping on a non-zero code. `script`
521
+ * and `configKey` name the capture source in the refusal (an
522
+ * own CRAP source runs `crap.captureScript` into `crap.coveragePath`).
517
523
  *
518
524
  * @param {(opts: object) => Promise<number>} run
519
525
  * @param {{ cwd: string, coveragePath: string,
520
526
  * existsSync?: (p: string) => boolean,
527
+ * script?: string, configKey?: string,
521
528
  * logger: { error: Function } }} opts
522
529
  * @returns {(opts: object) => Promise<number>}
523
530
  */
524
531
  export function requireCaptureArtifact(
525
532
  run,
526
- { cwd, coveragePath, existsSync = fs.existsSync, logger },
533
+ { cwd, coveragePath, existsSync = fs.existsSync, script, configKey, logger },
527
534
  ) {
528
535
  return async (captureOpts) => {
529
536
  const code = await run(captureOpts);
@@ -531,7 +538,7 @@ export function requireCaptureArtifact(
531
538
  const abs = path.resolve(cwd, coveragePath);
532
539
  if (existsSync(abs)) return 0;
533
540
  logger.error(
534
- `[coverage-capture] ✖ test:coverage passed but ${missingCoverageArtifactMessage(abs)}`,
541
+ `[coverage-capture] ✖ ${script ?? 'test:coverage'} passed but ${missingCoverageArtifactMessage(abs, script, configKey)}`,
535
542
  );
536
543
  return 1;
537
544
  };
@@ -687,14 +694,20 @@ function lockWaitExpiredMessage(code) {
687
694
  return `[coverage-capture] ⏸ the full-suite lock wait expired with another suite still running, so this capture was deferred — no suite ran. Exiting ${code}; re-run it once that suite finishes.`;
688
695
  }
689
696
 
690
- /** @param {number} code */
691
- function timeoutMessage(code) {
692
- return `[coverage-capture] ⏱ npm run test:coverage timed out and was killed (exit ${code}) — no test verdict exists. This is usually host contention, not a failing test; re-run once the host is quieter.`;
697
+ /**
698
+ * @param {number} code
699
+ * @param {string} script
700
+ */
701
+ function timeoutMessage(code, script) {
702
+ return `[coverage-capture] ⏱ npm run ${script} timed out and was killed (exit ${code}) — no test verdict exists. This is usually host contention, not a failing test; re-run once the host is quieter.`;
693
703
  }
694
704
 
695
- /** @param {number} code */
696
- function failingSuiteMessage(code) {
697
- return `[coverage-capture] ✖ npm run test:coverage exited ${code}. Fix failing tests or coverage-threshold breaches before re-running the CRAP gate.`;
705
+ /**
706
+ * @param {number} code
707
+ * @param {string} script
708
+ */
709
+ function failingSuiteMessage(code, script) {
710
+ return `[coverage-capture] ✖ npm run ${script} exited ${code}. Fix failing tests or coverage-threshold breaches before re-running the CRAP gate.`;
698
711
  }
699
712
 
700
713
  /**
@@ -712,15 +725,17 @@ const FAILING_SUITE_REPORT = Object.freeze(['error', failingSuiteMessage]);
712
725
 
713
726
  /**
714
727
  * Report a non-zero capture; an expired lock wait or a timeout is not a
715
- * failing suite, so neither prints the failing-tests line.
728
+ * failing suite, so neither prints the failing-tests line. `script` is the
729
+ * one the pass ran (an own CRAP source runs `crap.captureScript`).
716
730
  *
717
731
  * @param {number} code
718
732
  * @param {{ info: Function, error: Function }} logger
733
+ * @param {string} [script]
719
734
  * @returns {number}
720
735
  */
721
- export function reportCaptureFailure(code, logger) {
736
+ export function reportCaptureFailure(code, logger, script = 'test:coverage') {
722
737
  const [level, message] =
723
738
  NON_FAILURE_CAPTURE_EXITS[code] ?? FAILING_SUITE_REPORT;
724
- logger[level](message(code));
739
+ logger[level](message(code, script));
725
740
  return code;
726
741
  }
@@ -164,7 +164,11 @@ const MIN_RESOLUTION_SAMPLE = 25;
164
164
  * @param {number} floor
165
165
  * @returns {string|null}
166
166
  */
167
- export function checkResolutionFloor(resolution, floor) {
167
+ export function checkResolutionFloor(
168
+ resolution,
169
+ floor,
170
+ captureScript = 'test:coverage',
171
+ ) {
168
172
  if (!resolution) return null;
169
173
  const { joinableMethods = 0, resolvedMethods = 0, rate = 1 } = resolution;
170
174
  if (joinableMethods < MIN_RESOLUTION_SAMPLE) return null;
@@ -181,7 +185,7 @@ export function checkResolutionFloor(resolution, floor) {
181
185
  'unresolved methods are absent and coincidental line collisions are ' +
182
186
  'mis-attributed.\n' +
183
187
  (worst ? ` Worst unresolved files:\n${worst}\n` : '') +
184
- " Regenerate coverage ('npm run test:coverage') and re-run; if the " +
188
+ ` Regenerate coverage ('npm run ${captureScript}') and re-run; if the ` +
185
189
  'rate stays low the coverage artifact and the scanned tree disagree.'
186
190
  );
187
191
  }