mandrel 2.114.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.
Files changed (28) hide show
  1. package/.agents/docs/agentrc-reference.json +1 -0
  2. package/.agents/docs/configuration.md +10 -4
  3. package/.agents/schemas/agentrc.schema.json +7 -1
  4. package/.agents/scripts/acceptance-eval.js +2 -2
  5. package/.agents/scripts/ceremony-derive.js +15 -2
  6. package/.agents/scripts/lib/baselines/crap-preview-scan.js +15 -8
  7. package/.agents/scripts/lib/baselines/crap-preview-touched.js +140 -0
  8. package/.agents/scripts/lib/baselines/crap-prune.js +86 -0
  9. package/.agents/scripts/lib/baselines/kinds/crap.js +12 -4
  10. package/.agents/scripts/lib/baselines/seat-missing.js +1 -1
  11. package/.agents/scripts/lib/config/gates/coverage.schema.js +1 -1
  12. package/.agents/scripts/lib/config/gates/crap.schema.js +7 -0
  13. package/.agents/scripts/lib/config/quality.js +3 -0
  14. package/.agents/scripts/lib/coverage-capture-delta.js +26 -3
  15. package/.agents/scripts/lib/coverage-capture-own-fix.js +35 -19
  16. package/.agents/scripts/lib/coverage-capture.js +62 -14
  17. package/.agents/scripts/lib/crap-engine.js +3 -5
  18. package/.agents/scripts/lib/crap-utils.js +12 -2
  19. package/.agents/scripts/lib/generated/agentrc-validator.js +2 -2
  20. package/.agents/scripts/lib/git-branch-lifecycle.js +2 -1
  21. package/.agents/scripts/lib/orchestration/handoff-gates.js +25 -8
  22. package/.agents/scripts/lib/orchestration/story-handoff.js +131 -41
  23. package/.agents/scripts/single-story-init.js +43 -5
  24. package/.agents/scripts/story-handoff.js +6 -2
  25. package/.agents/workflows/helpers/deliver-digest.md +3 -3
  26. package/.agents/workflows/helpers/deliver-reference.md +13 -4
  27. package/docs/CHANGELOG.md +13 -0
  28. package/package.json +1 -1
@@ -6,8 +6,14 @@
6
6
  * eligible, and every uncertainty refuses.
7
7
  */
8
8
 
9
+ import fs from 'node:fs';
10
+ import path from 'node:path';
9
11
  import picomatch from 'picomatch';
10
- import { filterSuiteInputPaths } from './coverage-capture.js';
12
+ import {
13
+ filterSuiteInputPaths,
14
+ supersededArtifactPath,
15
+ supersededStampPath,
16
+ } from './coverage-capture.js';
11
17
 
12
18
  const TEST_FILE_RE = /\.(test|spec)\.[cm]?[jt]sx?$/;
13
19
  const TEST_DIR_RE = /(^|\/)(tests?|__tests__|spec|e2e)\//;
@@ -119,34 +125,21 @@ function judgeOwnChanges(ctx, own) {
119
125
  return ctx.inputs(paths).filter((f) => ctx.delta.has(f));
120
126
  }
121
127
 
122
- /** A weakened test is how a row outside the delta goes stale high. */
123
- function judgeOwnTests(ctx, files) {
124
- const tests = files.filter(isTestPath);
125
- if (tests.length === 0) return;
126
- const out = must(ctx, [
127
- 'diff',
128
- '--numstat',
129
- ctx.commit,
130
- 'HEAD',
131
- '--',
132
- ...tests,
133
- ]);
134
- for (const [added, deleted, file] of lines(out).map((l) => l.split('\t'))) {
135
- if (added === '-' || Number(deleted) > Number(added)) {
136
- refuse(`own part removes lines from test file ${file}`);
137
- }
138
- }
128
+ /** Only the test graph knows which sources a changed test reaches. */
129
+ function refuseOwnTest(files) {
130
+ const test = files.find(isTestPath);
131
+ if (test) refuse(`own part changes test file ${test}`);
139
132
  }
140
133
 
141
134
  function judge(ctx, { base, own }) {
142
135
  judgeBase(ctx, base);
143
136
  const ownFiles = judgeOwnChanges(ctx, own);
137
+ refuseOwnTest(ownFiles);
144
138
  if (ownFiles.length > ctx.max) {
145
139
  refuse(
146
140
  `own part has ${ownFiles.length} suite-input file(s), over ownFixDeltaMaxFiles ${ctx.max}`,
147
141
  );
148
142
  }
149
- judgeOwnTests(ctx, ownFiles);
150
143
  const baseFiles = ctx.inputs(base).filter((f) => ctx.delta.has(f));
151
144
  return { own: ownFiles.length, base: baseFiles.length };
152
145
  }
@@ -230,3 +223,26 @@ export function judgeOwnFix(opts) {
230
223
  reportOwnFix(p, crap, logger, opts.formatFreshnessDetail);
231
224
  return { ...plan, rule, stamp, ownFix: own, report };
232
225
  }
226
+
227
+ function readJson(file) {
228
+ try {
229
+ return JSON.parse(fs.readFileSync(file, 'utf8'));
230
+ } catch {
231
+ return null;
232
+ }
233
+ }
234
+
235
+ /** A copy still held aside means a dead capture left the artifact unusable. */
236
+ function readSupersededStamp(cwd, coveragePath) {
237
+ if (fs.existsSync(supersededArtifactPath(cwd, coveragePath))) return null;
238
+ return readJson(supersededStampPath(cwd, coveragePath));
239
+ }
240
+
241
+ /** The last green stamp and restored artifact a red run left, when usable. */
242
+ export function supersededSeed({ freshness, coverage, args, crap, changed }) {
243
+ if (freshness.reason !== 'unstamped' || !changed) return null;
244
+ if (!(coverage?.ownFixDeltaMaxFiles >= 1)) return null;
245
+ const stamp = readSupersededStamp(args.cwd, crap.coveragePath);
246
+ const prior = readJson(path.resolve(args.cwd, crap.coveragePath));
247
+ return stamp && prior ? { stamp, prior } : null;
248
+ }
@@ -675,13 +675,61 @@ export function anyChangedUnderTargets(changedFiles, targetDirs) {
675
675
  return filterFilesUnderTargets(changedFiles, targetDirs).length > 0;
676
676
  }
677
677
 
678
- /**
679
- * @param {{ cwd: string, coveragePath?: string }} opts No `coveragePath`,
680
- * no stamp to drop.
681
- */
682
- function dropCaptureStamp({ cwd, coveragePath }) {
683
- if (!coveragePath) return;
684
- fs.rmSync(captureStampPath(cwd, coveragePath), { force: true });
678
+ /** The last green capture's stamp, while a later run has not gone green. */
679
+ export function supersededStampPath(cwd, coveragePath) {
680
+ return `${captureStampPath(cwd, coveragePath)}.superseded`;
681
+ }
682
+
683
+ /** The artifact held aside for the duration of a capture. */
684
+ export function supersededArtifactPath(cwd, coveragePath) {
685
+ return `${path.resolve(cwd, coveragePath)}.superseded`;
686
+ }
687
+
688
+ function heldPaths({ cwd, coveragePath }) {
689
+ return {
690
+ stamp: captureStampPath(cwd, coveragePath),
691
+ held: supersededStampPath(cwd, coveragePath),
692
+ artifact: path.resolve(cwd, coveragePath),
693
+ copy: supersededArtifactPath(cwd, coveragePath),
694
+ };
695
+ }
696
+
697
+ function retireLiveStamp({ stamp, held, copy }) {
698
+ if (!fs.existsSync(stamp)) return;
699
+ fs.renameSync(stamp, held);
700
+ fs.rmSync(copy, { force: true });
701
+ }
702
+
703
+ /** A copy that outlived its capture is the good one; never overwrite it. */
704
+ function copyArtifactAside({ held, artifact, copy }) {
705
+ if (!fs.existsSync(held) || fs.existsSync(copy)) return;
706
+ if (fs.existsSync(artifact)) fs.copyFileSync(artifact, copy);
707
+ }
708
+
709
+ function discardHeld({ held, copy }) {
710
+ fs.rmSync(held, { force: true });
711
+ fs.rmSync(copy, { force: true });
712
+ }
713
+
714
+ /** Green discards what was held; anything else puts the artifact back. */
715
+ function settleHeld(paths, code) {
716
+ if (code === 0 || !fs.existsSync(paths.copy)) return discardHeld(paths);
717
+ fs.renameSync(paths.copy, paths.artifact);
718
+ }
719
+
720
+ /** Run with the last green stamp and artifact held aside. */
721
+ async function holdingLastGreen(opts, run) {
722
+ if (!opts.coveragePath) return run();
723
+ const paths = heldPaths(opts);
724
+ retireLiveStamp(paths);
725
+ copyArtifactAside(paths);
726
+ let code;
727
+ try {
728
+ code = await run();
729
+ return code;
730
+ } finally {
731
+ settleHeld(paths, code);
732
+ }
685
733
  }
686
734
 
687
735
  /** GNU `timeout(1)` code, so callers tell a hang (124) from failing tests. */
@@ -707,9 +755,9 @@ export const COVERAGE_HOST_RESOURCES_EXIT_CODE = 77;
707
755
  * worker. Takes no positional file args: node's runner would execute a
708
756
  * forwarded source file as a test instead of filtering the suite. Scope a
709
757
  * run through `env` instead. Given `coveragePath`, the capture stamp is
710
- * removed before the spawn: the suite is about to overwrite the artifact, so
711
- * a red, killed or crashed run must leave no stamp vouching for it — only
712
- * `stampCapturedTree` after a green run restores one.
758
+ * moved aside before the spawn: a red, killed or crashed run must leave no
759
+ * stamp vouching for the tree — only `stampCapturedTree` after a green run
760
+ * writes one — and gets the prior artifact back.
713
761
  *
714
762
  * @param {{
715
763
  * cwd: string,
@@ -724,14 +772,13 @@ export const COVERAGE_HOST_RESOURCES_EXIT_CODE = 77;
724
772
  * }} opts
725
773
  * @returns {Promise<number>}
726
774
  */
727
- export function runCapture(opts = {}) {
775
+ export async function runCapture(opts = {}) {
728
776
  const { script = COVERAGE_CAPTURE_DEFAULTS.captureScript, log = () => {} } =
729
777
  opts;
730
- dropCaptureStamp(opts);
731
778
  const [cmd, ...args] = packageScriptArgv(opts.cwd ?? process.cwd(), script);
732
779
  const command = [cmd, ...args].join(' ');
733
780
  log(`[coverage-capture] ▶ ${command}`);
734
- return runSupervisedSuite({
781
+ const supervised = {
735
782
  ...opts,
736
783
  cmd,
737
784
  args,
@@ -739,7 +786,8 @@ export function runCapture(opts = {}) {
739
786
  log(
740
787
  `[coverage-capture] ⏱ ${command} exceeded ${opts.timeoutMs}ms — killed its process group. Returning exit ${COVERAGE_TIMEOUT_EXIT_CODE}.`,
741
788
  ),
742
- });
789
+ };
790
+ return holdingLastGreen(opts, () => runSupervisedSuite(supervised));
743
791
  }
744
792
 
745
793
  /** @param {number} code */
@@ -86,18 +86,16 @@ export function methodRowsFromReport(report, coverageForFile, mapLine = null) {
86
86
  mapLine,
87
87
  );
88
88
  const cyclomatic = m?.cyclomatic ?? 0;
89
+ const endLine = resolveEndLine(m?.lineEnd, mapLine);
89
90
  const coverage =
90
91
  coverageForFile && coordinateSystem === COORDINATE_ORIGINAL
91
- ? coverageForMethodInEntry(
92
- coverageForFile,
93
- startLine,
94
- resolveEndLine(m?.lineEnd, mapLine),
95
- )
92
+ ? coverageForMethodInEntry(coverageForFile, startLine, endLine)
96
93
  : null;
97
94
  const crap = coverage === null ? null : crapFormula(cyclomatic, coverage);
98
95
  rows.push({
99
96
  ...identities[i],
100
97
  startLine,
98
+ ...(endLine === undefined ? {} : { endLine }),
101
99
  cyclomatic,
102
100
  coverage,
103
101
  crap,
@@ -270,6 +270,7 @@ function projectScanRow(relPath, mr) {
270
270
  // `method` may be a derived anonymous identity.
271
271
  anonymous: mr.anonymous === true,
272
272
  startLine: mr.startLine,
273
+ ...(mr.endLine === undefined ? {} : { endLine: mr.endLine }),
273
274
  cyclomatic: mr.cyclomatic,
274
275
  coverage: mr.coverage,
275
276
  crap: mr.crap,
@@ -416,6 +417,15 @@ function compareScanRows(a, b) {
416
417
  return 0;
417
418
  }
418
419
 
420
+ /** A file's coverage entry; one with no ranges has no coordinates to join. */
421
+ function joinableCoverageEntry(coverage, relPath) {
422
+ const entry = findCoverageEntry(coverage, relPath);
423
+ const hasRanges =
424
+ Object.keys(entry?.fnMap ?? {}).length > 0 ||
425
+ Object.keys(entry?.statementMap ?? {}).length > 0;
426
+ return hasRanges ? entry : null;
427
+ }
428
+
419
429
  /** In-process scorer; the reference the worker output must match exactly. */
420
430
  function scoreFileSerial(
421
431
  {
@@ -428,7 +438,7 @@ function scoreFileSerial(
428
438
  },
429
439
  coverage,
430
440
  ) {
431
- const entry = findCoverageEntry(coverage, relPath);
441
+ const entry = joinableCoverageEntry(coverage, relPath);
432
442
  if (
433
443
  shouldSkipFileForNoCoverage(requireCoverage, entry, touched, baselineByKey)
434
444
  ) {
@@ -474,7 +484,7 @@ async function scoreFilesViaPool(queue, coverage) {
474
484
  // Send each worker only its file's entry, not the whole coverage map.
475
485
  const enrichedQueue = queue.map((item) => ({
476
486
  ...item,
477
- coverageEntry: findCoverageEntry(coverage, item.relPath),
487
+ coverageEntry: joinableCoverageEntry(coverage, item.relPath),
478
488
  }));
479
489
  const results = await runOnPool(CRAP_WORKER_URL, enrichedQueue, {
480
490
  workerData: {},