bmad-plus 0.20.0 → 0.21.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.
@@ -14,7 +14,7 @@ const path = require('node:path');
14
14
  const crypto = require('node:crypto');
15
15
  const { spawnSync } = require('node:child_process');
16
16
  const { matchesAny } = require('./glob');
17
- const { redactFields } = require('./redact');
17
+ const { redact, redactFields } = require('./redact');
18
18
  const rules = require('./review-rules');
19
19
 
20
20
  const SCOPE_SCHEMA = 'bmad-plus/review-scope/1';
@@ -39,6 +39,17 @@ const DISPOSITIONS = ['confirmed', 'refuted', 'unresolved'];
39
39
  const CONFIDENCE = ['high', 'medium', 'low'];
40
40
  const OUTCOMES = ['completed', 'failed', 'waived'];
41
41
 
42
+ /**
43
+ * Why a review session ended. Only `completed` lets the gate reach a verdict; every other
44
+ * reason keeps it incomplete, whatever the coverage says.
45
+ */
46
+ const STOP_REASONS = ['completed', 'budget', 'time-limit', 'failure-streak', 'interrupted'];
47
+ const ATTEMPT_OUTCOMES = ['completed', 'failed'];
48
+ /** Consecutive failed attempts after which a unit is abandoned rather than retried again. */
49
+ const STRIKES = 3;
50
+ const RUN_KEYS = ['stop', 'detail', 'passes', 'attempts', 'tokens', 'durationMs'];
51
+ const ATTEMPT_KEYS = ['unit', 'group', 'outcome', 'reason'];
52
+
42
53
  /** Files whose content must never reach a review packet, whatever an include pattern says. */
43
54
  const SECRET_PATTERNS = [
44
55
  '**/.env',
@@ -195,6 +206,14 @@ function planUnits(selected, limits = UNIT_LIMITS) {
195
206
  }));
196
207
  }
197
208
 
209
+ function contentDigest(projectDir, file) {
210
+ try {
211
+ return sha256(fs.readFileSync(path.join(projectDir, file)));
212
+ } catch {
213
+ return null;
214
+ }
215
+ }
216
+
198
217
  /**
199
218
  * The review scope: what changed between base and head (or the working tree), which files
200
219
  * are selected, and why every other changed file is not. Secrets are excluded before any
@@ -234,12 +253,24 @@ function buildScope(projectDir, options = {}) {
234
253
  }
235
254
  }
236
255
  changed.sort((a, b) => a.path.localeCompare(b.path));
256
+ // The review's own evidence is not part of the change under review: writing findings must
257
+ // never alter the scope they answer.
258
+ const evidenceDir = path
259
+ .relative(projectDir, path.resolve(projectDir, options.outputDir || DEFAULT_DIR))
260
+ .split(path.sep)
261
+ .join('/');
262
+ const ownEvidence = (file) =>
263
+ evidenceDir !== '' &&
264
+ !evidenceDir.startsWith('..') &&
265
+ !path.isAbsolute(evidenceDir) &&
266
+ (file === evidenceDir || file.startsWith(`${evidenceDir}/`));
267
+ const candidates = changed.filter((entry) => !ownEvidence(entry.path));
237
268
 
238
269
  const include = options.include || [];
239
270
  const exclude = options.exclude || [];
240
271
  const selected = [];
241
272
  const excluded = [];
242
- for (const entry of changed) {
273
+ for (const entry of candidates) {
243
274
  const file = entry.path;
244
275
  let reason = null;
245
276
  if (matchesAny(file, SECRET_PATTERNS)) reason = 'secret';
@@ -262,10 +293,16 @@ function buildScope(projectDir, options = {}) {
262
293
  const effort = options.effort || 'medium';
263
294
  if (!EFFORTS[effort]) throw new Error(`effort must be one of ${Object.keys(EFFORTS).join('|')}`);
264
295
  const ruleset = options.ruleset || rules.loadRuleset(projectDir);
265
- for (const item of selected) item.rules = rules.rulesFor(ruleset, item.path);
296
+ for (const item of selected) {
297
+ item.rules = rules.rulesFor(ruleset, item.path);
298
+ // A commit fixes the content under review; a working tree does not, so its bytes are sealed.
299
+ if (options.workspace) item.sha256 = contentDigest(projectDir, item.path);
300
+ }
266
301
  const identity = {
267
302
  base,
268
303
  head: head || 'WORKTREE',
304
+ // The ref as given, so a continuation can tell whether it still names the reviewed commit.
305
+ headRef: options.workspace ? null : options.head || 'HEAD',
269
306
  mergeBase,
270
307
  workspace: Boolean(options.workspace),
271
308
  include,
@@ -273,10 +310,11 @@ function buildScope(projectDir, options = {}) {
273
310
  effort,
274
311
  rulesSha256: ruleset.sha256,
275
312
  };
276
- const units = planUnits(selected, options.unitLimits).map((unit) => ({
277
- ...unit,
278
- rules: [...new Set(unit.paths.flatMap((p) => selected.find((s) => s.path === p).rules))],
279
- }));
313
+ const units = planUnits(selected, options.unitLimits).map((unit) => {
314
+ const ids = [...new Set(unit.paths.flatMap((p) => selected.find((s) => s.path === p).rules))];
315
+ // Rule groups let parallel reviewers split one unit by rule family.
316
+ return { ...unit, rules: ids, groups: rules.groupsOf(ruleset, ids) };
317
+ });
280
318
  const lines = selected.reduce((n, item) => n + item.added + item.removed, 0);
281
319
  const plan = reviewPlan(effort, lines, units.length);
282
320
  const body = { identity, selected, excluded, units, plan };
@@ -285,7 +323,7 @@ function buildScope(projectDir, options = {}) {
285
323
  id: options.id,
286
324
  ...body,
287
325
  totals: {
288
- changed: changed.length,
326
+ changed: candidates.length,
289
327
  selected: selected.length,
290
328
  excluded: excluded.length,
291
329
  lines,
@@ -442,6 +480,148 @@ const REDACTED_FIELDS = [
442
480
 
443
481
  // ── Coverage and gate ─────────────────────────────────────────────────────────
444
482
 
483
+ const isCount = (value) => Number.isInteger(value) && value >= 0;
484
+ const isRecord = (value) => Boolean(value) && typeof value === 'object' && !Array.isArray(value);
485
+
486
+ /** What an attempt worked on: a whole unit (`u2`) or one rule group of it (`u2/data`). */
487
+ const attemptKey = (attempt) => (attempt.group ? `${attempt.unit}/${attempt.group}` : attempt.unit);
488
+ const unitOfKey = (key) => key.split('/')[0];
489
+
490
+ /**
491
+ * The three-strikes rule over an ordered attempt log: work whose attempts failed three
492
+ * times in a row is exhausted, and any later attempt of it is a violation. A whole unit
493
+ * and each of its groups overlap: exhausting one blocks the other.
494
+ */
495
+ function strikeLedger(attempts) {
496
+ const streak = new Map();
497
+ const exhausted = new Set();
498
+ const violations = [];
499
+ const blocked = (attempt) =>
500
+ exhausted.has(attempt.unit) ||
501
+ (attempt.group
502
+ ? exhausted.has(attemptKey(attempt))
503
+ : [...exhausted].some((key) => unitOfKey(key) === attempt.unit));
504
+ for (const [index, attempt] of attempts.entries()) {
505
+ const key = attemptKey(attempt);
506
+ if (blocked(attempt)) {
507
+ violations.push({ index, unit: key });
508
+ continue;
509
+ }
510
+ const failures = attempt.outcome === 'failed' ? (streak.get(key) || 0) + 1 : 0;
511
+ streak.set(key, failures);
512
+ if (failures >= STRIKES) exhausted.add(key);
513
+ }
514
+ return { exhausted, violations, units: new Set([...exhausted].map(unitOfKey)) };
515
+ }
516
+
517
+ const unitIndex = (scope) =>
518
+ new Map(scope.units.flatMap((unit) => unit.paths.map((file) => [file, unit.id])));
519
+
520
+ /** Units whose review completed: in one attempt, or in one completed attempt per rule group. */
521
+ function completedUnits(scope, attempts) {
522
+ const done = new Set(attempts.filter((a) => a.outcome === 'completed').map(attemptKey));
523
+ return new Set(
524
+ scope.units
525
+ .filter(
526
+ (unit) =>
527
+ done.has(unit.id) ||
528
+ (Array.isArray(unit.groups) &&
529
+ unit.groups.length > 0 &&
530
+ unit.groups.every((group) => done.has(`${unit.id}/${group.id}`)))
531
+ )
532
+ .map((unit) => unit.id)
533
+ );
534
+ }
535
+
536
+ /**
537
+ * The run record of a review: why it stopped, what it observed (passes run, units
538
+ * attempted, and tokens or duration only when the host reports them — never estimated),
539
+ * and the ordered log of unit attempts. When the log is not empty it is the whole story:
540
+ * a file is completed only if an attempt of its unit completed.
541
+ */
542
+ function validateRun(run, scope, items) {
543
+ if (!isRecord(run)) return ['run must be an object'];
544
+ const errors = [];
545
+ for (const key of Object.keys(run))
546
+ if (!RUN_KEYS.includes(key)) errors.push(`run: unknown key "${key}"`);
547
+ if (!STOP_REASONS.includes(run.stop))
548
+ errors.push(`run: stop "${run.stop}" is not one of ${STOP_REASONS.join('|')}`);
549
+ if (run.detail !== undefined && typeof run.detail !== 'string')
550
+ errors.push('run: detail is text');
551
+ else if (STOP_REASONS.includes(run.stop) && run.stop !== 'completed' && !run.detail?.trim())
552
+ errors.push(`run: a review stopped by ${run.stop} says why in detail`);
553
+ if (!isCount(run.passes)) errors.push('run: passes is the number of passes actually run');
554
+ else if (run.stop === 'completed' && run.passes < 1)
555
+ errors.push('run: a completed review ran at least one pass');
556
+ for (const key of ['tokens', 'durationMs'])
557
+ if (run[key] !== undefined && !isCount(run[key]))
558
+ errors.push(`run: ${key} is a non-negative integer reported by the host, or absent`);
559
+ if (!Array.isArray(run.attempts)) {
560
+ errors.push('run: attempts lists every unit attempt, in order (empty when none was logged)');
561
+ return errors;
562
+ }
563
+ const units = new Map(scope.units.map((unit) => [unit.id, unit]));
564
+ for (const [index, attempt] of run.attempts.entries()) {
565
+ const at = `run: attempt ${index + 1}`;
566
+ if (!isRecord(attempt)) {
567
+ errors.push(`${at}: must be an object`);
568
+ continue;
569
+ }
570
+ for (const key of Object.keys(attempt))
571
+ if (!ATTEMPT_KEYS.includes(key)) errors.push(`${at}: unknown key "${key}"`);
572
+ const unit = units.get(attempt.unit);
573
+ if (!unit) errors.push(`${at}: unit "${attempt.unit}" is not in the scope`);
574
+ else if (
575
+ attempt.group !== undefined &&
576
+ !(unit.groups || []).some((group) => group.id === attempt.group)
577
+ )
578
+ errors.push(`${at}: group "${attempt.group}" is not a rule group of unit ${unit.id}`);
579
+ if (!ATTEMPT_OUTCOMES.includes(attempt.outcome))
580
+ errors.push(
581
+ `${at}: outcome "${attempt.outcome}" is not one of ${ATTEMPT_OUTCOMES.join('|')}`
582
+ );
583
+ if (attempt.reason !== undefined && typeof attempt.reason !== 'string')
584
+ errors.push(`${at}: reason is text`);
585
+ else if (attempt.outcome === 'failed' && !attempt.reason?.trim())
586
+ errors.push(`${at}: a failed attempt needs a reason`);
587
+ }
588
+ if (errors.length) return errors;
589
+
590
+ const { exhausted, violations } = strikeLedger(run.attempts);
591
+ for (const { index, unit } of violations)
592
+ errors.push(
593
+ `run: attempt ${index + 1} retries unit ${unit} after ${STRIKES} consecutive failures — it is abandoned in this review`
594
+ );
595
+ if (run.stop === 'failure-streak' && !exhausted.size)
596
+ errors.push(`run: failure-streak needs a unit with ${STRIKES} consecutive failed attempts`);
597
+ if (run.attempts.length) {
598
+ const unitOf = unitIndex(scope);
599
+ const done = completedUnits(scope, run.attempts);
600
+ for (const item of items)
601
+ if (item.outcome === 'completed' && unitOf.has(item.path) && !done.has(unitOf.get(item.path)))
602
+ errors.push(
603
+ `${item.path}: completed in coverage, but unit ${unitOf.get(item.path)} was not completed — as a whole or in every rule group`
604
+ );
605
+ }
606
+ return errors;
607
+ }
608
+
609
+ /** What the review actually consumed, as reported; null when no run was recorded. */
610
+ function observedUsage(scope, run) {
611
+ if (!isRecord(run)) return null;
612
+ const attempts = Array.isArray(run.attempts) ? run.attempts.filter(isRecord) : [];
613
+ return {
614
+ passes: isCount(run.passes) ? run.passes : null,
615
+ plannedPasses: scope.plan ? scope.plan.passes : null,
616
+ units: scope.units.length,
617
+ unitsAttempted: new Set(attempts.map((a) => a.unit)).size,
618
+ attempts: attempts.length,
619
+ failedAttempts: attempts.filter((a) => a.outcome === 'failed').length,
620
+ ...(isCount(run.tokens) ? { tokens: run.tokens } : {}),
621
+ ...(isCount(run.durationMs) ? { durationMs: run.durationMs } : {}),
622
+ };
623
+ }
624
+
445
625
  function validateCoverage(doc, scope) {
446
626
  const errors = [];
447
627
  if (!doc || doc.schema !== COVERAGE_SCHEMA) errors.push(`schema must be "${COVERAGE_SCHEMA}"`);
@@ -461,12 +641,13 @@ function validateCoverage(doc, scope) {
461
641
  )
462
642
  errors.push(`${item.path}: a ${item.outcome} file needs a reason`);
463
643
  }
644
+ if (doc && doc.run !== undefined) errors.push(...validateRun(doc.run, scope, doc.items || []));
464
645
  return errors;
465
646
  }
466
647
 
467
648
  /**
468
649
  * The review verdict. `incomplete` whenever a selected file was not accounted for, failed,
469
- * or the evidence is invalid; otherwise `findings` or `clean` — the latter only means no
650
+ * the review stopped before the end, or the evidence is invalid; otherwise `findings` or `clean` — the latter only means no
470
651
  * confirmed or unresolved finding within a fully covered scope.
471
652
  */
472
653
  function reviewGate({ scope, findings, coverage, anchored }) {
@@ -489,6 +670,17 @@ function reviewGate({ scope, findings, coverage, anchored }) {
489
670
  );
490
671
  if (failed.length)
491
672
  reasons.push(`${failed.length} file(s) whose review failed: ${failed.slice(0, 5).join(', ')}`);
673
+ const run = coverage && isRecord(coverage.run) ? coverage.run : null;
674
+ // A review that stopped early is never clean, even when every file happens to be listed.
675
+ if (run && STOP_REASONS.includes(run.stop) && run.stop !== 'completed')
676
+ reasons.push(
677
+ `the review stopped before the end (${run.stop}): ${typeof run.detail === 'string' ? run.detail.trim() : ''}`
678
+ );
679
+ if (run && Array.isArray(run.attempts)) {
680
+ const { exhausted } = strikeLedger(run.attempts.filter(isRecord));
681
+ for (const unit of exhausted)
682
+ reasons.push(`unit ${unit} abandoned after ${STRIKES} consecutive failed attempts`);
683
+ }
492
684
  const open = ((anchored && anchored.findings) || []).filter((f) => f.disposition !== 'refuted');
493
685
  const unlocated = open.filter((f) => f.location.status !== 'located');
494
686
  if (unlocated.length)
@@ -504,7 +696,11 @@ function reviewGate({ scope, findings, coverage, anchored }) {
504
696
  const status = reasons.length ? 'incomplete' : open.length ? 'findings' : 'clean';
505
697
  return {
506
698
  status,
507
- reasons,
699
+ // Reasons echo host-written text (a stop detail, an invalid value), and they reach the
700
+ // terminal, the CI log and the check document alike: redacted once, here.
701
+ reasons: reasons.map((reason) => redact(reason).text),
702
+ stop: run && STOP_REASONS.includes(run.stop) ? run.stop : null,
703
+ usage: observedUsage(scope, run),
508
704
  coverage: {
509
705
  selected: total,
510
706
  completed: done,
@@ -525,6 +721,255 @@ function reviewGate({ scope, findings, coverage, anchored }) {
525
721
  };
526
722
  }
527
723
 
724
+ // ── Check result for CI ───────────────────────────────────────────────────────
725
+
726
+ const CHECK_SCHEMA = 'bmad-plus/review-check/1';
727
+ const GATE_EXIT = { clean: 0, findings: 1, incomplete: 2 };
728
+ const ANNOTATION_LEVEL = { critical: 'failure', high: 'failure', medium: 'warning', low: 'notice' };
729
+ /** The GitHub check-runs API accepts at most 50 annotations per request. */
730
+ const MAX_ANNOTATIONS = 50;
731
+ const MAX_SUMMARY_ROWS = 50;
732
+
733
+ const cell = (value) =>
734
+ String(value)
735
+ .replace(/[|\\`]/g, '\\$&')
736
+ .replace(/\s+/g, ' ');
737
+
738
+ /**
739
+ * The gate verdict as a stable document a CI step consumes without parsing prose: the
740
+ * disposition and its exit code, the reasons, the counts, the scope it answers, the open
741
+ * findings with their anchored lines, and a `github` block shaped like the check-runs API
742
+ * (`conclusion` and `output` with a Markdown summary and annotations). Free text is passed
743
+ * through the redaction floor; the document carries no timestamp, so the same evidence
744
+ * always yields the same bytes.
745
+ */
746
+ function checkResult({ id, scope, verdict, anchored }) {
747
+ const rank = (severity) => SEVERITIES.indexOf(severity);
748
+ const open = ((anchored && anchored.findings) || [])
749
+ .filter((f) => f.disposition !== 'refuted')
750
+ .map((f) => ({
751
+ id: f.id,
752
+ path: f.path,
753
+ severity: f.severity,
754
+ category: f.category,
755
+ confidence: f.confidence,
756
+ disposition: f.disposition,
757
+ ...(f.rule !== undefined ? { rule: f.rule } : {}),
758
+ anchor: f.location.status,
759
+ lineStart: f.location.status === 'located' ? f.location.lineStart : null,
760
+ lineEnd: f.location.status === 'located' ? f.location.lineEnd : null,
761
+ message: f.content,
762
+ }))
763
+ .sort(
764
+ (a, b) =>
765
+ rank(a.severity) - rank(b.severity) ||
766
+ a.path.localeCompare(b.path) ||
767
+ (a.lineStart || 0) - (b.lineStart || 0) ||
768
+ String(a.id).localeCompare(String(b.id))
769
+ );
770
+ const reasons = verdict.reasons.map((reason) => redact(reason).text);
771
+ const located = open.filter((f) => f.anchor === 'located');
772
+ const annotations = located.slice(0, MAX_ANNOTATIONS).map((f) => ({
773
+ path: f.path,
774
+ start_line: f.lineStart,
775
+ end_line: f.lineEnd,
776
+ annotation_level: ANNOTATION_LEVEL[f.severity],
777
+ title: `${f.severity} ${f.category} (${f.id})`,
778
+ message: f.message,
779
+ }));
780
+ const { coverage, findings } = verdict;
781
+ const summary = [
782
+ `**${verdict.status}**: ${coverage.completed}/${coverage.selected} selected file(s) completed, ${coverage.waived} waived, ${coverage.failed} failed, ${coverage.missing} missing; ${findings.open} open finding(s), ${findings.refuted} refuted.`,
783
+ `Scope \`${scope.sha256.slice(0, 12)}\` (${scope.identity.effort || 'medium'} effort)${verdict.stop ? `, stopped: ${verdict.stop}` : ''}.`,
784
+ ...(reasons.length ? ['', ...reasons.map((reason) => `- ${cell(reason)}`)] : []),
785
+ ...(open.length
786
+ ? [
787
+ '',
788
+ '| Severity | Category | Finding | Location |',
789
+ '|---|---|---|---|',
790
+ ...open
791
+ .slice(0, MAX_SUMMARY_ROWS)
792
+ .map(
793
+ (f) =>
794
+ `| ${f.severity} | ${f.category} | ${cell(f.id)} | ${cell(f.path)}${f.lineStart ? `:${f.lineStart}` : ` (${f.anchor})`} |`
795
+ ),
796
+ ...(open.length > MAX_SUMMARY_ROWS
797
+ ? [`\n${open.length - MAX_SUMMARY_ROWS} more in the check document.`]
798
+ : []),
799
+ ]
800
+ : []),
801
+ ...(verdict.status === 'clean'
802
+ ? [
803
+ '',
804
+ 'Clean means no open finding within a fully covered scope; it does not prove the code correct.',
805
+ ]
806
+ : []),
807
+ ].join('\n');
808
+ const body = {
809
+ id,
810
+ scopeSha256: scope.sha256,
811
+ identity: {
812
+ base: scope.identity.base,
813
+ head: scope.identity.head,
814
+ mergeBase: scope.identity.mergeBase,
815
+ workspace: scope.identity.workspace,
816
+ effort: scope.identity.effort || null,
817
+ rulesSha256: scope.identity.rulesSha256 || null,
818
+ },
819
+ status: verdict.status,
820
+ exitCode: GATE_EXIT[verdict.status],
821
+ reasons,
822
+ stop: verdict.stop,
823
+ usage: verdict.usage,
824
+ coverage,
825
+ findings,
826
+ open,
827
+ github: {
828
+ name: `bmad-plus review ${id}`,
829
+ conclusion: verdict.status === 'clean' ? 'success' : 'failure',
830
+ output: {
831
+ title: `Review ${verdict.status}: ${findings.open} open finding(s), ${coverage.completed}/${coverage.selected} file(s) completed`,
832
+ summary,
833
+ annotations,
834
+ },
835
+ annotationsOmitted: located.length - annotations.length,
836
+ },
837
+ };
838
+ return { schema: CHECK_SCHEMA, ...body, sha256: sha256(JSON.stringify(body)) };
839
+ }
840
+
841
+ // ── Continuing an interrupted review ──────────────────────────────────────────
842
+
843
+ const CONTINUE_SCHEMA = 'bmad-plus/review-continue/1';
844
+ const short = (sha) => String(sha).slice(0, 12);
845
+
846
+ /**
847
+ * Why a sealed scope no longer describes the code: its head ref names another commit, the
848
+ * review rules changed, or the change itself did (for a working tree, any selected byte).
849
+ * Empty when the scope rebuilt now is exactly the scope that was sealed.
850
+ */
851
+ function scopeDrift(projectDir, scope, options = {}) {
852
+ const { identity } = scope;
853
+ // Since continuation, every scope carries headRef (null for a working tree); an older one,
854
+ // commit or working tree, lacks the key along with the per-file digests it relies on.
855
+ if (!('headRef' in identity) || (!identity.workspace && !identity.headRef))
856
+ return ['the scope was sealed without its head ref, before continuation existed'];
857
+ const drift = [];
858
+ if (!identity.workspace) {
859
+ let now = null;
860
+ try {
861
+ now = resolveCommit(projectDir, identity.headRef);
862
+ } catch {
863
+ // Reported just below: a ref that no longer resolves has moved too.
864
+ }
865
+ if (now !== identity.head)
866
+ drift.push(
867
+ now
868
+ ? `${identity.headRef} moved from ${short(identity.head)} to ${short(now)}`
869
+ : `${identity.headRef} no longer resolves to a commit`
870
+ );
871
+ }
872
+ let rebuilt;
873
+ try {
874
+ rebuilt = buildScope(projectDir, {
875
+ id: scope.id,
876
+ base: identity.base,
877
+ head: identity.workspace ? undefined : identity.headRef,
878
+ workspace: identity.workspace,
879
+ include: identity.include,
880
+ exclude: identity.exclude,
881
+ effort: identity.effort,
882
+ ruleset: options.ruleset,
883
+ outputDir: options.outputDir,
884
+ });
885
+ } catch (error) {
886
+ return [...drift, `the scope cannot be rebuilt: ${error.message}`];
887
+ }
888
+ if (rebuilt.sha256 === scope.sha256 || drift.length) return drift;
889
+ if (rebuilt.identity.rulesSha256 !== identity.rulesSha256) drift.push('the review rules changed');
890
+ // Rules are compared above; here only what each file is and how it changed.
891
+ const describe = (list) =>
892
+ new Map(list.map((item) => [item.path, JSON.stringify({ ...item, rules: undefined })]));
893
+ const sealed = describe(scope.selected);
894
+ const current = describe(rebuilt.selected);
895
+ const moved = [...new Set([...sealed.keys(), ...current.keys()])]
896
+ .filter((file) => sealed.get(file) !== current.get(file))
897
+ .sort();
898
+ if (moved.length)
899
+ drift.push(
900
+ `${moved.length} file(s) changed since the scope was sealed: ${moved.slice(0, 5).join(', ')}${moved.length > 5 ? '…' : ''}`
901
+ );
902
+ if (!drift.length) drift.push('the scope rebuilt now differs from the sealed one');
903
+ return drift;
904
+ }
905
+
906
+ /**
907
+ * What an interrupted review still owes against its unchanged scope: selected files never
908
+ * accounted for or whose review failed, grouped back into their units; files of units
909
+ * abandoned under the three-strikes rule, which this review will not retry; open findings
910
+ * to requote; and the passes the plan still asks for when the run stopped early.
911
+ */
912
+ function remainingWork({ scope, coverage, anchored, findingErrors = [] }) {
913
+ const accounted = new Map(((coverage && coverage.items) || []).map((item) => [item.path, item]));
914
+ const run = coverage && isRecord(coverage.run) ? coverage.run : null;
915
+ const attempts = run && Array.isArray(run.attempts) ? run.attempts.filter(isRecord) : [];
916
+ const { units: exhausted } = strikeLedger(attempts);
917
+ const done = new Set(attempts.filter((a) => a.outcome === 'completed').map(attemptKey));
918
+ const unitOf = unitIndex(scope);
919
+ const files = [];
920
+ const abandoned = [];
921
+ for (const item of scope.selected) {
922
+ const entry = accounted.get(item.path);
923
+ if (entry && entry.outcome !== 'failed') continue;
924
+ const record = {
925
+ path: item.path,
926
+ unit: unitOf.get(item.path),
927
+ state: entry ? 'failed' : 'missing',
928
+ };
929
+ (exhausted.has(record.unit) ? abandoned : files).push(record);
930
+ }
931
+ const pending = new Set(files.map((file) => file.path));
932
+ const units = scope.units
933
+ .map((unit) => ({
934
+ id: unit.id,
935
+ paths: unit.paths.filter((file) => pending.has(file)),
936
+ rules: unit.rules,
937
+ // Rule groups another reviewer already completed are not handed out again.
938
+ ...(unit.groups
939
+ ? { groups: unit.groups.filter((group) => !done.has(`${unit.id}/${group.id}`)) }
940
+ : {}),
941
+ }))
942
+ .filter((unit) => unit.paths.length);
943
+ const requote = ((anchored && anchored.findings) || [])
944
+ .filter((f) => f.disposition !== 'refuted' && f.location.status !== 'located')
945
+ .map((f) => ({ id: f.id, path: f.path, status: f.location.status }));
946
+ const usage = observedUsage(scope, run);
947
+ const stopped = run && run.stop !== 'completed';
948
+ const passes =
949
+ stopped && usage.passes !== null && usage.plannedPasses !== null
950
+ ? Math.max(0, usage.plannedPasses - usage.passes)
951
+ : 0;
952
+ return {
953
+ schema: CONTINUE_SCHEMA,
954
+ id: scope.id,
955
+ scopeSha256: scope.sha256,
956
+ previous: run ? { stop: STOP_REASONS.includes(run.stop) ? run.stop : null, usage } : null,
957
+ counts: {
958
+ files: files.length,
959
+ units: units.length,
960
+ abandoned: abandoned.length,
961
+ requote: requote.length,
962
+ findingErrors: findingErrors.length,
963
+ passes,
964
+ },
965
+ units,
966
+ files,
967
+ abandoned,
968
+ requote,
969
+ findingErrors,
970
+ };
971
+ }
972
+
528
973
  // ── Comparing two reviews ─────────────────────────────────────────────────────
529
974
 
530
975
  const COMPARE_SCHEMA = 'bmad-plus/review-compare/1';
@@ -602,6 +1047,7 @@ function layout(projectDir, dir = DEFAULT_DIR, id) {
602
1047
  coverage: path.join(root, 'coverage.json'),
603
1048
  checklist: path.join(root, 'checklist.md'),
604
1049
  compare: path.join(root, 'compare.json'),
1050
+ continue: path.join(root, 'continue.json'),
605
1051
  };
606
1052
  }
607
1053
 
@@ -617,6 +1063,8 @@ module.exports = {
617
1063
  DISPOSITIONS,
618
1064
  CONFIDENCE,
619
1065
  OUTCOMES,
1066
+ STOP_REASONS,
1067
+ STRIKES,
620
1068
  SECRET_PATTERNS,
621
1069
  GENERATED_PATTERNS,
622
1070
  parseNumstat,
@@ -626,7 +1074,15 @@ module.exports = {
626
1074
  locate,
627
1075
  anchorFindings,
628
1076
  validateCoverage,
1077
+ validateRun,
1078
+ strikeLedger,
629
1079
  reviewGate,
1080
+ CHECK_SCHEMA,
1081
+ GATE_EXIT,
1082
+ checkResult,
1083
+ CONTINUE_SCHEMA,
1084
+ scopeDrift,
1085
+ remainingWork,
630
1086
  reviewPlan,
631
1087
  findingKey,
632
1088
  compareReviews,
@@ -1,36 +1,45 @@
1
1
  # Built-in review checklists, applied by path. Every matching rule applies to a file.
2
2
  # A project adds, replaces (same id) or disables rules in _bmad/review-rules.yaml.
3
+ # A group is a rule family: parallel reviewers can split one unit by group.
3
4
  schema: bmad-plus/review-rules/1
4
5
  rules:
5
6
  - id: general
6
7
  title: Every change
8
+ group: code
7
9
  globs: ['**']
8
10
  doc: general.md
9
11
  - id: javascript-typescript
10
12
  title: JavaScript and TypeScript
13
+ group: code
11
14
  globs: ['**/*.{js,cjs,mjs,jsx,ts,cts,mts,tsx}', '**/*.{vue,svelte}']
12
15
  doc: javascript-typescript.md
13
16
  - id: python
14
17
  title: Python
18
+ group: code
15
19
  globs: ['**/*.{py,pyi}']
16
20
  doc: python.md
17
21
  - id: sql-and-migrations
18
22
  title: SQL, queries and migrations
23
+ group: data
19
24
  globs: ['**/*.sql', '**/{migrations,migrate,db/migrate}/**', '**/*.prisma', '**/schema.rb']
20
25
  doc: sql-and-migrations.md
21
26
  - id: shell
22
27
  title: Shell and PowerShell scripts
28
+ group: code
23
29
  globs: ['**/*.{sh,bash,zsh,ps1,psm1}', '**/{Makefile,makefile}']
24
30
  doc: shell.md
25
31
  - id: ci-workflows
26
32
  title: CI workflows
33
+ group: delivery
27
34
  globs: ['.github/workflows/**/*.{yml,yaml}', '**/.gitlab-ci.yml', '**/{bitbucket-pipelines.yml,azure-pipelines.yml}']
28
35
  doc: ci-workflows.md
29
36
  - id: containers
30
37
  title: Containers and deployment manifests
38
+ group: delivery
31
39
  globs: ['**/{Dockerfile,Containerfile}', '**/*.dockerfile', '**/{docker-,}compose*.{yml,yaml}', '**/{k8s,kubernetes,helm,charts}/**/*.{yml,yaml}']
32
40
  doc: containers.md
33
41
  - id: configuration
34
42
  title: Configuration and dependency manifests
43
+ group: delivery
35
44
  globs: ['**/*.{json,yml,yaml,toml,ini,properties}', '**/.env.example', '**/{package.json,pyproject.toml,requirements*.txt,go.mod,Cargo.toml,composer.json}']
36
45
  doc: configuration.md