mandrel 2.113.0 → 2.115.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.
- package/.agents/docs/agentrc-reference.json +2 -0
- package/.agents/docs/configuration.md +11 -4
- package/.agents/schemas/agentrc.schema.json +13 -1
- package/.agents/scripts/acceptance-eval.js +2 -2
- package/.agents/scripts/ceremony-derive.js +15 -2
- package/.agents/scripts/lib/baselines/crap-preview-scan.js +15 -8
- package/.agents/scripts/lib/baselines/crap-preview-touched.js +140 -0
- package/.agents/scripts/lib/baselines/crap-prune.js +86 -0
- package/.agents/scripts/lib/baselines/duplication-scan-child.js +19 -0
- package/.agents/scripts/lib/baselines/duplication-scan-fork.js +76 -0
- package/.agents/scripts/lib/baselines/kinds/crap.js +12 -4
- package/.agents/scripts/lib/baselines/refresh-service.js +14 -15
- package/.agents/scripts/lib/baselines/seat-missing.js +1 -1
- package/.agents/scripts/lib/config/gates/coverage.schema.js +1 -1
- package/.agents/scripts/lib/config/gates/crap.schema.js +7 -0
- package/.agents/scripts/lib/config/gates/duplication.schema.js +7 -0
- package/.agents/scripts/lib/config/quality.js +3 -0
- package/.agents/scripts/lib/coverage-capture-delta.js +26 -3
- package/.agents/scripts/lib/coverage-capture-own-fix.js +35 -19
- package/.agents/scripts/lib/coverage-capture.js +62 -14
- package/.agents/scripts/lib/crap-engine.js +3 -5
- package/.agents/scripts/lib/crap-utils.js +12 -2
- package/.agents/scripts/lib/generated/agentrc-validator.js +2 -2
- package/.agents/scripts/lib/git-branch-lifecycle.js +2 -1
- package/.agents/scripts/lib/orchestration/handoff-gates.js +25 -8
- package/.agents/scripts/lib/orchestration/story-handoff.js +131 -41
- package/.agents/scripts/single-story-init.js +43 -5
- package/.agents/scripts/story-handoff.js +6 -2
- package/.agents/workflows/helpers/deliver-digest.md +3 -3
- package/.agents/workflows/helpers/deliver-reference.md +13 -4
- package/docs/CHANGELOG.md +20 -0
- package/package.json +1 -1
|
@@ -156,6 +156,7 @@
|
|
|
156
156
|
"skipWhenUnchanged": true,
|
|
157
157
|
"baselineJoin": false
|
|
158
158
|
},
|
|
159
|
+
"previewScope": "file",
|
|
159
160
|
"coveragePath": null
|
|
160
161
|
},
|
|
161
162
|
"maintainability": {
|
|
@@ -212,6 +213,7 @@
|
|
|
212
213
|
},
|
|
213
214
|
"targetDirs": ["src"],
|
|
214
215
|
"additionalFormats": [],
|
|
216
|
+
"scanHeapMb": 4096,
|
|
215
217
|
"ignoreGlobs": []
|
|
216
218
|
}
|
|
217
219
|
},
|
|
@@ -187,7 +187,7 @@ Everything `/mandrel-deliver` and `single-story-close` consume: worktree isolati
|
|
|
187
187
|
| `quality.gates.coverage.ciRatchetCheck` | No | `string` \| `null` | `null` | Name of the consumer's CI check (check-run or status context) that runs `coverage-ratchet.js` on a deferred PR. Required by `suiteOfRecord: ci`; when set, confirm-merge holds `agent::done` on a PR carrying the `Coverage-deferred:` key line until this check is green on the PR head. Default `null`. |
|
|
188
188
|
| `quality.gates.coverage.nonInputGlobs` | No | `array<string>` | `[]` | Repo-relative picomatch globs naming paths no test reads. A matching path is dropped from both sides of the coverage delta-refresh overlap check and from the re-measured set — a delta of only matching, baseline and coverage-directory paths restamps without a run — and from the overlap check of close's `test` delta re-credit (its incoming count and emptiness test are unchanged). It never changes the suite-input filter, the tree digest or stamp freshness. Default `[]`: nothing is built in, so Markdown counts as an input unless declared here. |
|
|
189
189
|
| `quality.gates.coverage.captureHandlesConfigChanges` | No | `boolean` | `false` | Declares that the affected capture script, given `MANDREL_COVERAGE_BASE_REF`, widens its own selection when a coverage-determining config file (`package.json`, a lockfile, `tsconfig*.json`, a test-runner config) changes. When `true`, such a file in the coverage delta no longer refuses the delta refresh. Close's `test` delta re-credit refuses on a config file under either value. Default `false`. |
|
|
190
|
-
| `quality.gates.coverage.ownFixDeltaMaxFiles` | No | `integer` | `0` | Opt-in own-fix delta refresh for `captureScope: affected`. `0` (default) keeps it off. `N >= 1` lets a delta that today's rule refuses for a Story overlap or a config file be re-measured as a delta when it splits, along HEAD's first-parent chain, into base-sync merges that pass today's rule and the Story's own non-merge commits touching at most `N` suite-input files, with no config file, no deleted or renamed suite input and no
|
|
190
|
+
| `quality.gates.coverage.ownFixDeltaMaxFiles` | No | `integer` | `0` | Opt-in own-fix delta refresh for `captureScope: affected`. `0` (default) keeps it off. `N >= 1` lets a delta that today's rule refuses for a Story overlap or a config file be re-measured as a delta when it splits, along HEAD's first-parent chain, into base-sync merges that pass today's rule and the Story's own non-merge commits touching at most `N` suite-input files, with no config file, no deleted or renamed suite input and no test file: a changed test moves the rows of sources mandrel cannot name, so it refuses the split. A red capture keeps the last green stamp for this split alone. Rows outside the delta can go stale in either direction; validate against full captures before raising it. `10` is a reasonable start. |
|
|
191
191
|
| `quality.gates.crap` | No | `object` | — | CRAP (Change Risk Anti-Pattern) ratchet — per-method cyclomatic complexity joined against per-method coverage. |
|
|
192
192
|
| `quality.gates.crap.enabled` | No | `boolean` | `true` | When false, the checker exits 0 with a skip line and the gate is reported as `skipped`, never omitted. |
|
|
193
193
|
| `quality.gates.crap.baselinePath` | No | `string` | `"baselines/crap.json"` | Repo-root-relative path to the gate's committed baseline artifact. |
|
|
@@ -205,6 +205,7 @@ Everything `/mandrel-deliver` and `single-story-close` consume: worktree isolati
|
|
|
205
205
|
| `quality.gates.crap.incrementalCoverage.skipWhenUnchanged` | No | `boolean` | `true` | Skip the capture entirely when no changed file under `crap.targetDirs` versus `baseRef` was touched. The only measured saving, and gate-semantics-neutral. Defaults to true. |
|
|
206
206
|
| `quality.gates.crap.incrementalCoverage.baselineJoin` | No | `boolean` | `false` | Let the CRAP join resolve a method in a file the diff did not touch from its committed baseline row instead of requiring fresh coverage for it. A gate loosening, not a saving — defaults to false. |
|
|
207
207
|
| `quality.gates.crap.incrementalCoverage.baseRef` | No | `string` | — | Default git ref the changed-file set is computed against, for a caller that passes no `--ref`. A `--ref` the caller named wins over this value, so a caller that anchors another gate on the same ref — `.husky/pre-push`, which passes `--ref origin/main` and then previews `--changed-since origin/main` — resolves ONE scope for both steps (Story #5365). Omitted and unpassed, the default is `origin/<project.baseBranch>` (`origin/main` by default), so a local base branch that trails the remote cannot widen the set; a clone where that remote ref does not resolve falls back to the local base branch with a warning. |
|
|
208
|
+
| `quality.gates.crap.previewScope` | No | `"file"` \| `"touched-methods"` | `"file"` | What the CRAP preview judges in a changed file. `file` compares every method to its row. `touched-methods` compares only methods whose line range intersects a hunk of the diff against the base: other regressions print as informational rows and pass. New methods always face `newMethodCeiling`, and a method whose hunks cannot be read is judged. |
|
|
208
209
|
| `quality.gates.crap.coveragePath` | No | `string` \| `null` | `null` | 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. Its capture stamp sits beside it, so it may share a directory with the coverage gate's artifact. |
|
|
209
210
|
| `quality.gates.crap.captureScript` | No | `string` | — | `package.json` script (never a shell command) that writes the CRAP gate's OWN coverage artifact, spawned as `<pm> run <script>`. Valid only beside an own `coveragePath`: without one the CRAP gate reads the coverage gate's artifact, so this key is refused and `delivery.quality.gates.coverage.captureScript` runs. Unset beside an own `coveragePath`, it is the coverage gate's `captureScript`. |
|
|
210
211
|
| `quality.gates.maintainability` | No | `object` | — | Maintainability-index ratchet. Scores per file as the average over its methods, so deleting a small high-MI method can legitimately lower a file’s score. |
|
|
@@ -244,6 +245,7 @@ Everything `/mandrel-deliver` and `single-story-close` consume: worktree isolati
|
|
|
244
245
|
| `quality.gates.duplication.components` | No | `object<map>` | — | Component-keyed membership map — the membership half of the component mapping whose threshold half is `floors`: component name to the glob list whose rows roll up under it (matched against the row key: the file path, or the bundle name for bundle-size). `check-baselines` reports one rollup per component that matches a row and holds it to `floors.<component>`, falling back to `floors["*"]`. Defaults to `{ "*": ["**"] }` at the resolver layer. |
|
|
245
246
|
| `quality.gates.duplication.targetDirs` | No | `string[]` or `{ append?, prepend? }` | `["src"]` | Directories whose scorable sources (`.js`, `.mjs`, `.cjs`, `.jsx`, `.ts`, `.tsx`, `.mts`, `.cts`), plus the files of any `additionalFormats`, the duplication (DRY) gate scans for copy-paste clones. Mandrel ships a `src/`-centric default; projects whose executable code lives elsewhere (e.g. this repo's `.agents/scripts/`) override here. The framework default is intentionally not auto-discovered, so an override is the explicit, auditable signal (Story #3664). |
|
|
246
247
|
| `quality.gates.duplication.additionalFormats` | No | `array<string>` | `[]` | Extra jscpd format names (e.g. `python`, `go`) the duplication gate scans on top of the scorable formats it always reads. Extend-only: the scorable formats cannot be removed, and naming one here is a no-op. An unknown format name fails the scan rather than measuring nothing. The CRAP, MI, coverage and native-review gates still score only the scorable list (Story #5849). |
|
|
248
|
+
| `quality.gates.duplication.scanHeapMb` | No | `integer` | `4096` | V8 old-space limit, in MB, of the child process that runs the whole-tree duplication scan. The need grows with the scanned tree. It is a ceiling, not an allocation, and it reaches only that child. |
|
|
247
249
|
| `quality.gates.duplication.ignoreGlobs` | No | `array<string>` | `[]` | Minimatch glob patterns matched against the canonicalised repo-relative path of each discovered file. Files matching any pattern are excluded from duplication discovery before scanning. Orthogonal to `components` (grouping). Absent or empty preserves the existing behaviour (Story #3664). This is the lever for scanning fewer files. |
|
|
248
250
|
| `quality.autoRefresh` | No | `object` | — | Baseline-attribution auto-refresh: when a gate can prove a regression is a legitimate consequence of the diff, it rewrites the baseline instead of blocking. The single-row CRAP jump cap (5) and the rescore scope (`diff`) are fixed. |
|
|
249
251
|
| `quality.autoRefresh.enabled` | No | `boolean` | `true` | Master switch for the auto-refresh path. When false, every baseline refresh is a deliberate operator action. |
|
|
@@ -550,12 +552,17 @@ only when **every** guard holds:
|
|
|
550
552
|
- the own part holds no coverage-determining config file;
|
|
551
553
|
- the own part touches at most `N` suite-input paths in the delta;
|
|
552
554
|
- no own commit deletes or renames a suite-input path;
|
|
553
|
-
-
|
|
554
|
-
`__tests__`, `spec` or `e2e` directory)
|
|
555
|
-
|
|
555
|
+
- the own part changes no test file (`*.test.*` / `*.spec.*`, or under a
|
|
556
|
+
`tests`, `test`, `__tests__`, `spec` or `e2e` directory): only the test
|
|
557
|
+
graph knows which sources a test reaches;
|
|
556
558
|
- no own commit is reachable from `origin/<baseBranch>` (that would be a
|
|
557
559
|
fast-forwarded base commit).
|
|
558
560
|
|
|
561
|
+
A red, killed or crashed capture restores the prior artifact and keeps the
|
|
562
|
+
last green stamp as a `.superseded` sibling. It never reads as fresh; it only
|
|
563
|
+
seeds this split, under the same guards, so the next own fix need not re-pay
|
|
564
|
+
the whole slice.
|
|
565
|
+
|
|
559
566
|
An eligible plan logs `needs an own-fix delta refresh — re-measuring <k>
|
|
560
567
|
file(s) changed since <sha> (<n> own file(s), <m> base file(s))` with
|
|
561
568
|
verdict `own-fix-delta-refresh`. A refusal logs the ordinary `delta refresh
|
|
@@ -788,7 +788,7 @@
|
|
|
788
788
|
"type": "integer",
|
|
789
789
|
"minimum": 0,
|
|
790
790
|
"maximum": 50,
|
|
791
|
-
"description": "Opt-in own-fix delta refresh for `captureScope: affected`. `0` (default) keeps it off. `N >= 1` lets a delta that today's rule refuses for a Story overlap or a config file be re-measured as a delta when it splits, along HEAD's first-parent chain, into base-sync merges that pass today's rule and the Story's own non-merge commits touching at most `N` suite-input files, with no config file, no deleted or renamed suite input and no
|
|
791
|
+
"description": "Opt-in own-fix delta refresh for `captureScope: affected`. `0` (default) keeps it off. `N >= 1` lets a delta that today's rule refuses for a Story overlap or a config file be re-measured as a delta when it splits, along HEAD's first-parent chain, into base-sync merges that pass today's rule and the Story's own non-merge commits touching at most `N` suite-input files, with no config file, no deleted or renamed suite input and no test file: a changed test moves the rows of sources mandrel cannot name, so it refuses the split. A red capture keeps the last green stamp for this split alone. Rows outside the delta can go stale in either direction; validate against full captures before raising it. `10` is a reasonable start.",
|
|
792
792
|
"default": 0
|
|
793
793
|
}
|
|
794
794
|
},
|
|
@@ -940,6 +940,12 @@
|
|
|
940
940
|
},
|
|
941
941
|
"additionalProperties": false
|
|
942
942
|
},
|
|
943
|
+
"previewScope": {
|
|
944
|
+
"type": "string",
|
|
945
|
+
"enum": ["file", "touched-methods"],
|
|
946
|
+
"description": "What the CRAP preview judges in a changed file. `file` compares every method to its row. `touched-methods` compares only methods whose line range intersects a hunk of the diff against the base: other regressions print as informational rows and pass. New methods always face `newMethodCeiling`, and a method whose hunks cannot be read is judged.",
|
|
947
|
+
"default": "file"
|
|
948
|
+
},
|
|
943
949
|
"coveragePath": {
|
|
944
950
|
"type": ["string", "null"],
|
|
945
951
|
"minLength": 1,
|
|
@@ -1335,6 +1341,12 @@
|
|
|
1335
1341
|
"description": "Extra jscpd format names (e.g. `python`, `go`) the duplication gate scans on top of the scorable formats it always reads. Extend-only: the scorable formats cannot be removed, and naming one here is a no-op. An unknown format name fails the scan rather than measuring nothing. The CRAP, MI, coverage and native-review gates still score only the scorable list (Story #5849).",
|
|
1336
1342
|
"default": []
|
|
1337
1343
|
},
|
|
1344
|
+
"scanHeapMb": {
|
|
1345
|
+
"type": "integer",
|
|
1346
|
+
"minimum": 256,
|
|
1347
|
+
"description": "V8 old-space limit, in MB, of the child process that runs the whole-tree duplication scan. The need grows with the scanned tree. It is a ceiling, not an allocation, and it reaches only that child.",
|
|
1348
|
+
"default": 4096
|
|
1349
|
+
},
|
|
1338
1350
|
"ignoreGlobs": {
|
|
1339
1351
|
"type": "array",
|
|
1340
1352
|
"items": {
|
|
@@ -22,7 +22,7 @@ import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
|
22
22
|
import path from 'node:path';
|
|
23
23
|
import { fileURLToPath } from 'node:url';
|
|
24
24
|
import { parseArgs } from 'node:util';
|
|
25
|
-
import { deriveCeremony } from './ceremony-derive.js';
|
|
25
|
+
import { deriveCeremony, resolveDefaultBaseRef } from './ceremony-derive.js';
|
|
26
26
|
import { runAsCli } from './lib/cli-utils.js';
|
|
27
27
|
import { getDeliveryRouting } from './lib/config/delivery-routing.js';
|
|
28
28
|
import { resolvedTempRoot } from './lib/config/temp-paths.js';
|
|
@@ -261,7 +261,7 @@ export async function initVerdictSkeleton({ storyId, cwd, config }, deps = {}) {
|
|
|
261
261
|
}
|
|
262
262
|
const ceremony = deriveCeremonyImpl({
|
|
263
263
|
storyId,
|
|
264
|
-
baseRef: config
|
|
264
|
+
baseRef: resolveDefaultBaseRef({ config, cwd }),
|
|
265
265
|
cwd,
|
|
266
266
|
ceremonyProfile: getDeliveryRouting(config).ceremonyProfile,
|
|
267
267
|
});
|
|
@@ -19,6 +19,7 @@ import { getDeliveryRouting } from './lib/config/delivery-routing.js';
|
|
|
19
19
|
import { resolveConfig } from './lib/config-resolver.js';
|
|
20
20
|
import { resolveCeremonyForRisk } from './lib/orchestration/ceremony-routing.js';
|
|
21
21
|
import { computeChangeSet } from './lib/orchestration/change-set.js';
|
|
22
|
+
import { resolveSharedBaseRef } from './lib/orchestration/review-base-ref.js';
|
|
22
23
|
import { deriveChangeLevel } from './lib/orchestration/review-depth.js';
|
|
23
24
|
|
|
24
25
|
const USAGE = {
|
|
@@ -30,7 +31,7 @@ const USAGE = {
|
|
|
30
31
|
['--story <id>', 'Story issue number; the head ref is story-<id>.'],
|
|
31
32
|
[
|
|
32
33
|
'--base <ref>',
|
|
33
|
-
'Base ref for the three-dot diff (default: project.baseBranch
|
|
34
|
+
'Base ref for the three-dot diff (default: origin/<project.baseBranch>, else project.baseBranch).',
|
|
34
35
|
],
|
|
35
36
|
[
|
|
36
37
|
'--cwd <path>',
|
|
@@ -39,6 +40,18 @@ const USAGE = {
|
|
|
39
40
|
],
|
|
40
41
|
};
|
|
41
42
|
|
|
43
|
+
/**
|
|
44
|
+
* The default change-set base: the remote ref handoff and close diff against,
|
|
45
|
+
* so a stale local base cannot score others' commits as the Story's.
|
|
46
|
+
*
|
|
47
|
+
* @param {{ config: object, cwd: string }} args
|
|
48
|
+
* @returns {string}
|
|
49
|
+
*/
|
|
50
|
+
export function resolveDefaultBaseRef({ config, cwd }) {
|
|
51
|
+
const baseBranch = config?.project?.baseBranch ?? 'main';
|
|
52
|
+
return resolveSharedBaseRef({ baseBranch, cwd }).ref ?? baseBranch;
|
|
53
|
+
}
|
|
54
|
+
|
|
42
55
|
/**
|
|
43
56
|
* @param {string[]} argv
|
|
44
57
|
* @returns {{ storyId: number|null, base: string|null, cwd: string|null }}
|
|
@@ -137,7 +150,7 @@ export async function runCeremonyDeriveCli(
|
|
|
137
150
|
const envelope = deriveCeremony(
|
|
138
151
|
{
|
|
139
152
|
storyId,
|
|
140
|
-
baseRef: base ?? config
|
|
153
|
+
baseRef: base ?? resolveDefaultBaseRef({ config, cwd: workCwd }),
|
|
141
154
|
cwd: workCwd,
|
|
142
155
|
ceremonyProfile: getDeliveryRouting(config).ceremonyProfile,
|
|
143
156
|
},
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import path from 'node:path';
|
|
7
|
-
import { COVERAGE_CAPTURE_DEFAULTS } from '../config/gates/coverage.schema.js';
|
|
8
7
|
import { loadCoverage } from '../coverage-utils.js';
|
|
9
8
|
import {
|
|
10
9
|
KERNEL_VERSION,
|
|
@@ -13,6 +12,12 @@ import {
|
|
|
13
12
|
} from '../crap-utils.js';
|
|
14
13
|
import { CYCLOMATIC_CEILING } from '../cyclomatic-ceiling.js';
|
|
15
14
|
import { resolveCrapPreviewIncremental } from './crap-preview-incremental.js';
|
|
15
|
+
import {
|
|
16
|
+
outsideDiffDiagnostics,
|
|
17
|
+
previewCoveragePath,
|
|
18
|
+
previewNotRun,
|
|
19
|
+
resolveIsJudged,
|
|
20
|
+
} from './crap-preview-touched.js';
|
|
16
21
|
import { resolveCrapEnvOverrides } from './env-overrides.js';
|
|
17
22
|
import {
|
|
18
23
|
buildCrapReport,
|
|
@@ -57,12 +62,6 @@ export function listCyclomaticAdvisories(rows) {
|
|
|
57
62
|
}));
|
|
58
63
|
}
|
|
59
64
|
|
|
60
|
-
function loadPreviewCoverage(crap, cwd) {
|
|
61
|
-
const coveragePath =
|
|
62
|
-
crap.coveragePath ?? COVERAGE_CAPTURE_DEFAULTS.coveragePath;
|
|
63
|
-
return loadCoverage(path.resolve(cwd, coveragePath));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
65
|
/**
|
|
67
66
|
* @param {{
|
|
68
67
|
* crap: object,
|
|
@@ -72,6 +71,7 @@ function loadPreviewCoverage(crap, cwd) {
|
|
|
72
71
|
* diffRef: string|null,
|
|
73
72
|
* baseline: { rows: object[] },
|
|
74
73
|
* baseBranch?: string,
|
|
74
|
+
* git?: Function,
|
|
75
75
|
* }} opts
|
|
76
76
|
* @returns {Promise<{ exitCode: number, envelope: object }>}
|
|
77
77
|
*/
|
|
@@ -83,19 +83,22 @@ export async function computeCrapPreviewScan({
|
|
|
83
83
|
diffRef,
|
|
84
84
|
baseline,
|
|
85
85
|
baseBranch,
|
|
86
|
+
git,
|
|
86
87
|
}) {
|
|
87
88
|
const targetDirs = Array.isArray(crap.targetDirs) ? crap.targetDirs : [];
|
|
88
89
|
const crapIgnoreGlobs = Array.isArray(crap.ignoreGlobs)
|
|
89
90
|
? crap.ignoreGlobs
|
|
90
91
|
: [];
|
|
91
92
|
const requireCoverage = crap.requireCoverage !== false;
|
|
92
|
-
const coverage = loadPreviewCoverage(crap, cwd);
|
|
93
93
|
// The configured tolerance keeps the preview aligned with the authoritative
|
|
94
94
|
// gate: deltas at or under it are demoted, not failed.
|
|
95
95
|
const { newMethodCeiling, tolerance } = resolveCrapEnvOverrides(
|
|
96
96
|
crap,
|
|
97
97
|
process.env,
|
|
98
98
|
);
|
|
99
|
+
const notRun = previewNotRun({ crap, targetDirs, cwd, scope, diffRef });
|
|
100
|
+
if (notRun) return notRun;
|
|
101
|
+
const coverage = loadCoverage(path.resolve(cwd, previewCoveragePath(crap)));
|
|
99
102
|
const incremental = resolveCrapPreviewIncremental({
|
|
100
103
|
crap,
|
|
101
104
|
diffRef,
|
|
@@ -118,6 +121,7 @@ export async function computeCrapPreviewScan({
|
|
|
118
121
|
baselineRows,
|
|
119
122
|
newMethodCeiling,
|
|
120
123
|
tolerance,
|
|
124
|
+
isJudged: resolveIsJudged({ crap, diffRef, baseBranch, cwd, git }),
|
|
121
125
|
});
|
|
122
126
|
const envelope = buildCrapReport({
|
|
123
127
|
compareResult: result,
|
|
@@ -128,6 +132,9 @@ export async function computeCrapPreviewScan({
|
|
|
128
132
|
scopeInfo: { scope, diffRef },
|
|
129
133
|
});
|
|
130
134
|
envelope.cyclomaticAdvisories = listCyclomaticAdvisories(scan.rows);
|
|
135
|
+
if (result.informational.length > 0) {
|
|
136
|
+
envelope.diagnostics = outsideDiffDiagnostics(result.informational);
|
|
137
|
+
}
|
|
131
138
|
const exitCode = hasCrapRegressions(result) ? 1 : 0;
|
|
132
139
|
return { exitCode, envelope };
|
|
133
140
|
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `crap.previewScope: touched-methods`: which regressions the preview judges,
|
|
3
|
+
* from the hunks of the diff against the base; and the preview's own absence
|
|
4
|
+
* when no fresh artifact exists.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { COVERAGE_CAPTURE_DEFAULTS } from '../config/gates/coverage.schema.js';
|
|
10
|
+
import { COORDINATE_ORIGINAL } from '../crap-coordinates.js';
|
|
11
|
+
import { KERNEL_VERSION, resolveEscomplexVersion } from '../crap-utils.js';
|
|
12
|
+
import { gitSpawn } from '../git-utils.js';
|
|
13
|
+
import {
|
|
14
|
+
buildCrapReport,
|
|
15
|
+
compareCrap,
|
|
16
|
+
suppressVerdicts,
|
|
17
|
+
} from './kinds/crap.js';
|
|
18
|
+
import { isCaptureFresh } from './seat-missing.js';
|
|
19
|
+
|
|
20
|
+
export function previewCoveragePath(crap) {
|
|
21
|
+
return crap.coveragePath ?? COVERAGE_CAPTURE_DEFAULTS.coveragePath;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Why the artifact cannot be joined, or `null` when it matches the tree. */
|
|
25
|
+
function coverageAbsence(crap, targetDirs, cwd) {
|
|
26
|
+
const coveragePath = previewCoveragePath(crap);
|
|
27
|
+
if (isCaptureFresh({ coveragePath, targetDirs }, { cwd })) return null;
|
|
28
|
+
return fs.existsSync(path.resolve(cwd, coveragePath))
|
|
29
|
+
? `${coveragePath} does not match the current tree`
|
|
30
|
+
: `no coverage artifact at ${coveragePath}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The verdict-free report of a preview that scored nothing. */
|
|
34
|
+
function notRunResult({ reason, scope, diffRef, newMethodCeiling }) {
|
|
35
|
+
const envelope = buildCrapReport({
|
|
36
|
+
compareResult: compareCrap({ currentRows: [], baselineRows: [] }),
|
|
37
|
+
kernelVersion: KERNEL_VERSION,
|
|
38
|
+
escomplexVersion: resolveEscomplexVersion(),
|
|
39
|
+
newMethodCeiling,
|
|
40
|
+
scopeInfo: { scope, diffRef },
|
|
41
|
+
});
|
|
42
|
+
const diagnostic = {
|
|
43
|
+
name: 'crap-coverage-not-fresh',
|
|
44
|
+
message: `CRAP half did not run: ${reason}`,
|
|
45
|
+
};
|
|
46
|
+
return { exitCode: 0, envelope: suppressVerdicts(envelope, diagnostic) };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The not-run result when no artifact matches the tree, else `null`. */
|
|
50
|
+
export function previewNotRun({ crap, targetDirs, cwd, scope, diffRef }) {
|
|
51
|
+
const reason = coverageAbsence(crap, targetDirs, cwd);
|
|
52
|
+
if (!reason) return null;
|
|
53
|
+
const { newMethodCeiling } = crap;
|
|
54
|
+
return notRunResult({ reason, scope, diffRef, newMethodCeiling });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* New-side line ranges per file of a `-U0` diff. Hunk bodies are skipped by
|
|
59
|
+
* count, so an added line reading `+++ b/x` opens no file.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} text
|
|
62
|
+
* @returns {Map<string, Array<[number, number]>>}
|
|
63
|
+
*/
|
|
64
|
+
function parseDiffHunks(text) {
|
|
65
|
+
const hunks = new Map();
|
|
66
|
+
let ranges = null;
|
|
67
|
+
let body = 0;
|
|
68
|
+
for (const line of text.split(/\r?\n/)) {
|
|
69
|
+
if (body > 0) {
|
|
70
|
+
body -= 1;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (line.startsWith('+++ ')) {
|
|
74
|
+
const file = /^\+\+\+ b\/(.+)$/.exec(line)?.[1];
|
|
75
|
+
ranges = file ? [] : null;
|
|
76
|
+
if (file) hunks.set(file, ranges);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const m = /^@@ -\d+(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
80
|
+
if (!m) continue;
|
|
81
|
+
const [removed, start, added] = [m[1] ?? 1, m[2], m[3] ?? 1].map(Number);
|
|
82
|
+
body = removed + added;
|
|
83
|
+
// A pure deletion names the line before it.
|
|
84
|
+
ranges?.push([start, start + Math.max(added - 1, 0)]);
|
|
85
|
+
}
|
|
86
|
+
return hunks;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The hunks of the diff from `ref`'s merge-base; `null` when unreadable. */
|
|
90
|
+
function readDiffHunks(ref, cwd, git) {
|
|
91
|
+
const base = git(cwd, 'merge-base', ref, 'HEAD');
|
|
92
|
+
if (base?.status !== 0) return null;
|
|
93
|
+
const diff = git(
|
|
94
|
+
cwd,
|
|
95
|
+
'diff',
|
|
96
|
+
'-U0',
|
|
97
|
+
'--no-color',
|
|
98
|
+
'--no-ext-diff',
|
|
99
|
+
'--src-prefix=a/',
|
|
100
|
+
'--dst-prefix=b/',
|
|
101
|
+
String(base.stdout).trim(),
|
|
102
|
+
);
|
|
103
|
+
return diff?.status === 0 ? parseDiffHunks(String(diff.stdout)) : null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The `compareCrap` predicate for `previewScope`. Fails closed: a row with
|
|
108
|
+
* no original-coordinate span, or a file with no readable hunks, is judged.
|
|
109
|
+
*
|
|
110
|
+
* @returns {((row: object) => boolean)|undefined}
|
|
111
|
+
*/
|
|
112
|
+
export function resolveIsJudged({
|
|
113
|
+
crap,
|
|
114
|
+
diffRef,
|
|
115
|
+
baseBranch,
|
|
116
|
+
cwd,
|
|
117
|
+
git = gitSpawn,
|
|
118
|
+
}) {
|
|
119
|
+
if (crap.previewScope !== 'touched-methods') return undefined;
|
|
120
|
+
const hunks = readDiffHunks(diffRef ?? `origin/${baseBranch}`, cwd, git);
|
|
121
|
+
if (!hunks) return undefined;
|
|
122
|
+
return (row) => {
|
|
123
|
+
const ranges = hunks.get(row.file);
|
|
124
|
+
if (
|
|
125
|
+
!ranges ||
|
|
126
|
+
row.coordinateSystem !== COORDINATE_ORIGINAL ||
|
|
127
|
+
!Number.isFinite(row.endLine)
|
|
128
|
+
) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
return ranges.some(([s, e]) => s <= row.endLine && e >= row.startLine);
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function outsideDiffDiagnostics(informational) {
|
|
136
|
+
return informational.map((v) => ({
|
|
137
|
+
name: 'crap-regression-outside-diff',
|
|
138
|
+
message: `informational: ${v.file}::${v.method} crap ${v.crap.toFixed(2)} > baseline ${v.baseline.toFixed(2)}, outside this diff's hunks`,
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stale-row prune for the CRAP baseline: statically derived, so it runs
|
|
3
|
+
* before any coverage exists.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import nodeFs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { execFileCaptureAsync } from '../child-exec.js';
|
|
9
|
+
import { calculateCrapForSource, UNSCORABLE } from '../crap-engine.js';
|
|
10
|
+
import { methodStem } from '../crap-method-identity.js';
|
|
11
|
+
import { prepareSourceForScoring } from '../transpile.js';
|
|
12
|
+
import { assertEnvelope } from './envelope.js';
|
|
13
|
+
import { canonicalizeBaselinePath } from './path-canon.js';
|
|
14
|
+
import { readPriorState } from './refresh-service.js';
|
|
15
|
+
import { assertCrapPriorCurrent } from './seat-missing.js';
|
|
16
|
+
import { writeFile as writeEnvelopeFile } from './writer.js';
|
|
17
|
+
|
|
18
|
+
/** The branch's changed files, both sides of a rename. */
|
|
19
|
+
async function changedFiles({ baseRef, headRef, cwd }) {
|
|
20
|
+
const { stdout } = await execFileCaptureAsync(
|
|
21
|
+
'git',
|
|
22
|
+
['diff', '--name-only', '--no-renames', `${baseRef}...${headRef}`],
|
|
23
|
+
{ cwd },
|
|
24
|
+
);
|
|
25
|
+
return stdout.split(/\r?\n/).filter((line) => line.trim().length > 0);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The method stems a source file yields now: empty for a deleted file,
|
|
30
|
+
* `null` when it cannot be scored, so its rows are kept.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} abs
|
|
33
|
+
* @param {typeof nodeFs} fs
|
|
34
|
+
* @returns {Set<string>|null}
|
|
35
|
+
*/
|
|
36
|
+
function currentMethodStems(abs, fs) {
|
|
37
|
+
if (!fs.existsSync(abs)) return new Set();
|
|
38
|
+
const prepared = prepareSourceForScoring(abs);
|
|
39
|
+
if (prepared.error) return null;
|
|
40
|
+
const rows = calculateCrapForSource(prepared.code, null, prepared.mapLine);
|
|
41
|
+
if (rows === UNSCORABLE) return null;
|
|
42
|
+
return new Set(rows.map((row) => methodStem(row.method)));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Prior rows minus each change-set file's rows whose stem no longer exists. */
|
|
46
|
+
function withoutStaleRows(rows, changed, stemsOf) {
|
|
47
|
+
const stems = new Map();
|
|
48
|
+
return rows.filter((row) => {
|
|
49
|
+
if (!changed.has(row.path)) return true;
|
|
50
|
+
if (!stems.has(row.path)) stems.set(row.path, stemsOf(row.path));
|
|
51
|
+
return stems.get(row.path)?.has(methodStem(row.method)) ?? true;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Drop the CRAP rows of change-set files whose method no longer exists.
|
|
57
|
+
* Siblings collapse by stem, so a surviving namesake keeps the row.
|
|
58
|
+
*
|
|
59
|
+
* @param {{writePath: string, baseRef: string, headRef?: string, cwd?: string,
|
|
60
|
+
* gitDiff?: Function, fs?: typeof nodeFs}} opts
|
|
61
|
+
* @returns {Promise<{pruned: number}>}
|
|
62
|
+
*/
|
|
63
|
+
export async function pruneStaleCrapRows({
|
|
64
|
+
writePath,
|
|
65
|
+
baseRef,
|
|
66
|
+
headRef = 'HEAD',
|
|
67
|
+
cwd = process.cwd(),
|
|
68
|
+
gitDiff = changedFiles,
|
|
69
|
+
fs = nodeFs,
|
|
70
|
+
}) {
|
|
71
|
+
const { envelope, ahead } = readPriorState(writePath, fs, 'crap');
|
|
72
|
+
if (ahead || !envelope) return { pruned: 0 };
|
|
73
|
+
assertCrapPriorCurrent('crap', envelope);
|
|
74
|
+
const changed = new Set(
|
|
75
|
+
(await gitDiff({ baseRef, headRef, cwd })).map(canonicalizeBaselinePath),
|
|
76
|
+
);
|
|
77
|
+
const rows = withoutStaleRows(envelope.rows, changed, (relPath) =>
|
|
78
|
+
currentMethodStems(path.resolve(cwd, relPath), fs),
|
|
79
|
+
);
|
|
80
|
+
const pruned = envelope.rows.length - rows.length;
|
|
81
|
+
if (pruned === 0) return { pruned };
|
|
82
|
+
const next = { ...envelope, rows };
|
|
83
|
+
assertEnvelope(next);
|
|
84
|
+
writeEnvelopeFile(writePath, next, { fsImpl: fs });
|
|
85
|
+
return { pruned };
|
|
86
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Forked entry for the whole-tree duplication scan. Disconnecting after the
|
|
3
|
+
* reply lets the process end by itself, so a coverage run still flushes.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { resolveDetectClones, scanDuplication } from './duplication-scanner.js';
|
|
7
|
+
|
|
8
|
+
process.once('message', async (params) => {
|
|
9
|
+
let reply;
|
|
10
|
+
try {
|
|
11
|
+
const detect = resolveDetectClones();
|
|
12
|
+
reply = { rows: await scanDuplication({ ...params, detect }) };
|
|
13
|
+
} catch (err) {
|
|
14
|
+
reply = {
|
|
15
|
+
error: { message: err?.message ?? String(err), code: err?.code },
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
process.send(reply, () => process.disconnect());
|
|
19
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs the duplication scan in a child with its own heap: the scan needs
|
|
3
|
+
* gigabytes on a large tree, and a flag on the fork reaches nobody else.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { fork } from 'node:child_process';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import { scanDuplication } from './duplication-scanner.js';
|
|
9
|
+
|
|
10
|
+
const DEFAULT_SCAN_HEAP_MB = 4096;
|
|
11
|
+
const STDERR_TAIL_CHARS = 2000;
|
|
12
|
+
const HEAP_FLAG = /^--max[-_]old[-_]space[-_]size=/;
|
|
13
|
+
const CHILD_ENTRY = fileURLToPath(
|
|
14
|
+
new URL('./duplication-scan-child.js', import.meta.url),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
function isHeapDeath(code, signal, stderr) {
|
|
18
|
+
return (
|
|
19
|
+
signal === 'SIGABRT' || code === 134 || /heap out of memory/i.test(stderr)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function childDeathError(code, signal, stderr, scanHeapMb) {
|
|
24
|
+
const how = signal ? `signal ${signal}` : `exit code ${code}`;
|
|
25
|
+
const hint = isHeapDeath(code, signal, stderr)
|
|
26
|
+
? ` It ran out of heap at ${scanHeapMb} MB; raise delivery.quality.gates.duplication.scanHeapMb.`
|
|
27
|
+
: '';
|
|
28
|
+
return new Error(
|
|
29
|
+
`[Duplication] scan child died with ${how} before reporting.${hint} ${stderr.trim()}`.trim(),
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function replyError({ message, code }) {
|
|
34
|
+
const err = new Error(message);
|
|
35
|
+
if (code !== undefined) err.code = code;
|
|
36
|
+
return err;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {{ targetDirs: string[], cwd: string, ignoreGlobs?: string[], additionalFormats?: string[] }} scan
|
|
41
|
+
* @param {{ detect?: (opts: object) => Promise<object[]>, scanHeapMb?: number, forkImpl?: typeof fork }} [opts]
|
|
42
|
+
* an injected `detect` scans in-process and forks nothing
|
|
43
|
+
* @returns {Promise<object[]>} the scan's rows
|
|
44
|
+
*/
|
|
45
|
+
export function runDuplicationScan(
|
|
46
|
+
scan,
|
|
47
|
+
{ detect, scanHeapMb = DEFAULT_SCAN_HEAP_MB, forkImpl = fork } = {},
|
|
48
|
+
) {
|
|
49
|
+
if (detect) return scanDuplication({ ...scan, detect });
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const child = forkImpl(CHILD_ENTRY, [], {
|
|
52
|
+
execArgv: [
|
|
53
|
+
...process.execArgv.filter((arg) => !HEAP_FLAG.test(arg)),
|
|
54
|
+
`--max-old-space-size=${scanHeapMb}`,
|
|
55
|
+
],
|
|
56
|
+
silent: true,
|
|
57
|
+
});
|
|
58
|
+
let reply;
|
|
59
|
+
let stderr = '';
|
|
60
|
+
child.stdout?.resume();
|
|
61
|
+
child.stderr?.on('data', (chunk) => {
|
|
62
|
+
stderr = (stderr + chunk).slice(-STDERR_TAIL_CHARS);
|
|
63
|
+
});
|
|
64
|
+
child.on('message', (message) => {
|
|
65
|
+
reply = message;
|
|
66
|
+
});
|
|
67
|
+
child.once('error', reject);
|
|
68
|
+
child.once('close', (code, signal) => {
|
|
69
|
+
if (reply?.error) reject(replyError(reply.error));
|
|
70
|
+
else if (reply) resolve(reply.rows);
|
|
71
|
+
else reject(childDeathError(code, signal, stderr, scanHeapMb));
|
|
72
|
+
});
|
|
73
|
+
// A failed send surfaces as the child's `close`.
|
|
74
|
+
child.send(scan, () => {});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
@@ -264,19 +264,23 @@ export function checkCrapRegression(row, baseline, tolerance, kind) {
|
|
|
264
264
|
* (`drifted`) instead of reporting them new and removed. Unpaired current
|
|
265
265
|
* rows face the new-method ceiling; unpaired baseline rows are `removed`.
|
|
266
266
|
* Unscorable rows (null crap/coverage) are bucketed and excluded from
|
|
267
|
-
* `comparable` so they cannot dilute a ratio.
|
|
267
|
+
* `comparable` so they cannot dilute a ratio. A regression whose row
|
|
268
|
+
* `isJudged` rejects lands in `informational` and fails nothing.
|
|
268
269
|
*/
|
|
269
270
|
export function compareCrap({
|
|
270
271
|
currentRows,
|
|
271
272
|
baselineRows,
|
|
272
273
|
newMethodCeiling,
|
|
273
274
|
tolerance,
|
|
275
|
+
isJudged = () => true,
|
|
274
276
|
}) {
|
|
275
277
|
const rows = currentRows ?? [];
|
|
276
278
|
const state = {
|
|
277
279
|
uncoveredFiles: deriveUncoveredFiles(currentRows),
|
|
278
280
|
newMethodCeiling,
|
|
279
281
|
tolerance: Number.isFinite(tolerance) ? tolerance : 0,
|
|
282
|
+
isJudged,
|
|
283
|
+
informational: [],
|
|
280
284
|
claimed: new Set(),
|
|
281
285
|
verdicts: new Array(rows.length).fill(null),
|
|
282
286
|
regressions: 0,
|
|
@@ -314,6 +318,7 @@ export function compareCrap({
|
|
|
314
318
|
unscorable: unscorableRows.length,
|
|
315
319
|
removed: removedRows.length,
|
|
316
320
|
violations: state.verdicts.filter((v) => v !== null),
|
|
321
|
+
informational: state.informational,
|
|
317
322
|
unscorableRows,
|
|
318
323
|
removedRows,
|
|
319
324
|
};
|
|
@@ -322,10 +327,13 @@ export function compareCrap({
|
|
|
322
327
|
/** Score one (current, baseline) pair into the verdict slot `idx`. */
|
|
323
328
|
function scorePair(state, idx, row, baseline, kind) {
|
|
324
329
|
const v = checkCrapRegression(row, baseline, state.tolerance, kind);
|
|
325
|
-
if (v)
|
|
326
|
-
|
|
327
|
-
state.
|
|
330
|
+
if (!v) return;
|
|
331
|
+
if (!state.isJudged(row)) {
|
|
332
|
+
state.informational.push(v);
|
|
333
|
+
return;
|
|
328
334
|
}
|
|
335
|
+
state.regressions += 1;
|
|
336
|
+
state.verdicts[idx] = v;
|
|
329
337
|
}
|
|
330
338
|
|
|
331
339
|
/** A current row with no baseline row: the new-method ceiling. */
|