@dev-loops/core 1.0.0-rc.3 → 1.0.0-rc.5

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.
@@ -1,9 +1,12 @@
1
1
  import { readFile } from "node:fs/promises";
2
+ import { normalizeSeverity } from "../loop/gate-fanin.mjs";
2
3
  import { execFileSync } from "node:child_process";
3
4
  import path from "node:path";
4
5
  import { parse as parseYaml } from "yaml";
5
6
  import { fileURLToPath } from "node:url";
6
7
  import { z } from "zod";
8
+ import { classifyFile } from "../analysis/diff-analyzer.mjs";
9
+ import { isDevLoopConfigSourcePath } from "../loop/gate-carry-forward.mjs";
7
10
 
8
11
  // ============================================================================
9
12
  // Sub-schemas
@@ -118,12 +121,24 @@ const RefinementConfig = z.strictObject({
118
121
  roles: z.array(z.string().trim().min(1)).describe("Review lenses the refinement fan-out dispatches.").optional(),
119
122
  });
120
123
 
124
+ // Per-angle surface scope: how much of the gate-context bundle an angle
125
+ // actually needs. "full" (default) is today's omniscient briefing;
126
+ // "changed-files" drops the adjacent-code bundle AND the invariant prefix's
127
+ // "Changed files + adjacent-code summary" section (the diff itself still
128
+ // carries every changed file); "docs-only" narrows further to doc-file
129
+ // hunks only. Resolution (resolveGateAngleScope) fails open to "full" for an
130
+ // unknown/missing value — a narrow scope is an opt-in cost saving, never a
131
+ // silently-enforced information cut.
132
+ export const GATE_ANGLE_SCOPES = Object.freeze(["full", "changed-files", "docs-only"]);
133
+
121
134
  // One review angle: a bare string is sugar for `{ name }`. An object may also
122
135
  // set `mandatory` (always runs, survives dynamic pruning — was
123
136
  // gates.<gate>.mandatoryAngles), `enabled: false` (drops it from the resolved
124
- // list — was gates.<gate>.excludeAngles, D3), and `persona`/`prompt`/`model`/
137
+ // list — was gates.<gate>.excludeAngles, D3), `persona`/`prompt`/`model`/
125
138
  // `tier` (was the top-level `personas` map + angle-keyed
126
- // `models.roles`/`models.roleTiers`, D4: model > tier > built-in precedence).
139
+ // `models.roles`/`models.roleTiers`, D4: model > tier > built-in precedence),
140
+ // and `scope` (AC3: the surface briefing variant this angle needs — see
141
+ // GATE_ANGLE_SCOPES).
127
142
  // This is the ONE identity for a gate-review angle (was five separate places
128
143
  // — see the config-schema RFC). `mergeConfigLayers` merges these arrays BY
129
144
  // `name` across config layers (D3), so a later layer can add or disable a
@@ -144,11 +159,51 @@ const GateAngleEntry = z.preprocess(
144
159
  prompt: z.string().min(1).optional().describe("Short focused instruction for the reviewer agent — what to look for and how to judge this angle."),
145
160
  model: z.string().trim().min(1).optional().describe("Concrete model override for this angle (highest precedence)."),
146
161
  tier: z.string().trim().min(1).optional().describe("Model tier alias for this angle (used when `model` is absent)."),
162
+ scope: z.enum(GATE_ANGLE_SCOPES).optional().describe("Surface scope this angle needs: full (default), changed-files (diff without the adjacent-code bundle or its changed-files/adjacent-file summary section), or docs-only (doc-file hunks only). Unknown/omitted resolves to full."),
147
163
  }),
148
164
  );
149
165
 
166
+ // Diff-class kinds a tier's `match` can name — exactly classifyFile()'s
167
+ // output range (../analysis/diff-analyzer.mjs), so a tier config can never
168
+ // name a kind the classifier could not produce.
169
+ const GateTierMatchKind = z.enum(["code", "docs", "config", "test", "ci", "unknown"]);
170
+
171
+ // A tier's match conditions: EVERY changed file's kind must be in `kinds`
172
+ // (when set) AND the change must stay within `maxFiles`/`maxLines` (when
173
+ // set). At least one condition is required — a bare `{}` would match every
174
+ // diff unconditionally, which is never the intent of an explicit tier entry.
175
+ const GateTierMatch = z
176
+ .strictObject({
177
+ kinds: z.array(GateTierMatchKind).min(1).describe("Changed-file kinds this tier matches; every changed file's classifyFile() kind must be in this set.").optional(),
178
+ maxFiles: z.number().int().min(1).describe("Match only when the change touches at most this many files.").optional(),
179
+ maxLines: z.number().int().min(1).describe("Match only when the change stays within this many changed lines.").optional(),
180
+ })
181
+ .superRefine((match, ctx) => {
182
+ if (match.kinds === undefined && match.maxFiles === undefined && match.maxLines === undefined) {
183
+ ctx.addIssue({
184
+ code: z.ZodIssueCode.custom,
185
+ message: "match must set at least one of kinds, maxFiles, maxLines",
186
+ });
187
+ }
188
+ });
189
+
190
+ // One diff-class angle tier: a fixed angle set applied instead of dynamic
191
+ // subtractive/additive reduction when `match` holds. See resolveGateTier.
192
+ const GateTier = z.strictObject({
193
+ name: z.string().trim().min(1).describe("Tier name; surfaces as the tier:<name> resolution reason."),
194
+ match: GateTierMatch.describe("Diff-class conditions that select this tier."),
195
+ angles: z.array(z.string().trim().min(1)).min(1).describe("Angle set this tier resolves to when matched; unioned with the gate's mandatory angles."),
196
+ });
197
+
150
198
  const GateDynamicConfig = z.strictObject({
151
- subtractive: z.boolean().default(false).describe("Enable diff-driven dynamic angle PRUNING for this gate (was gates.<gate>.dynamicAngles)."),
199
+ // Diff-driven dynamic angle selection is ON by default (#1579): a fresh
200
+ // install narrows the angle pool to what the diff-classifier recommends.
201
+ // mandatory:true angles stay a hard always-run floor; fallbackToAll fires
202
+ // when classification is ambiguous, degrading to the full static pool. Set
203
+ // subtractive:false to restore the full static angle pool (the gate:full label
204
+ // only forces per-angle dispatch of the still-pruned set, not the full pool —
205
+ // combine both for the original full static fan-out).
206
+ subtractive: z.boolean().default(true).describe("Enable diff-driven dynamic angle PRUNING for this gate (ON by default; set false to restore the full static angle pool). Was gates.<gate>.dynamicAngles."),
152
207
  // Additive counterpart to the subtractive path (#1048): when true, the
153
208
  // context-builder may also ADD catalog angles — from resolveAnglePool()
154
209
  // (gates.anglePool, or else the union of the persona registry and this
@@ -169,13 +224,96 @@ const GateConfig = z.strictObject({
169
224
  dynamic: GateDynamicConfig.optional().describe("Diff-driven dynamic angle selection policy for this gate."),
170
225
  required: z.boolean().default(true).describe("Whether this gate must run."),
171
226
  requireCi: z.boolean().default(true).describe("Per-gate CI prerequisite (default true): the gate requires green CI on the current head; false opts this gate out of the CI precondition entirely, including a real failure."),
227
+ // Defect severities only (high/medium/low, plus their pre-rename spellings)
228
+ // — "question"/"nit" are non-defect categories that never block a clean
229
+ // verdict by severity: a question's own answered/never-deferred contract
230
+ // and a nit's immediate-defer disposition already decide its fate, so
231
+ // admitting either here would let a config block on a severity the
232
+ // disposition pass simultaneously auto-resolves.
172
233
  blockCleanOnFindingSeverities: z
173
- .array(z.enum(["must-fix", "worth-fixing-now", "defer"]))
234
+ .array(z.enum(["high", "medium", "low", "must-fix", "worth-fixing-now", "nice-to-have", "defer"]))
174
235
  .min(1)
175
- .default(["must-fix"])
176
- .describe("Finding severities that block a clean gate verdict."),
236
+ .default(["high"])
237
+ .describe("Defect finding severities that block a clean gate verdict (high/medium/low only — \"question\"/\"nit\" are non-defect categories and never block by severity). \"must-fix\" is the deprecated legacy spelling of \"high\", \"worth-fixing-now\" of \"medium\", and \"nice-to-have\"/\"defer\" of \"low\"; consumers normalize them."),
238
+ // Per-gate medium fix window (#1581): an open medium finding stays in the
239
+ // in-gate fix loop through this many rounds of THIS gate's chain and is
240
+ // deferred (replied-to + resolved) from the next round on. Defaults to 3
241
+ // (the built-in MEDIUM_FIX_WINDOW fallback in
242
+ // scripts/github/_gate-finding-surface.mjs). high is exempt: it never
243
+ // defers and forces per-gate continuation until the gate round cap escalates.
244
+ // No schema-level `.default()`: resolveGateConfig applies the built-in
245
+ // fallback (3) only after checking BOTH this key and the deprecated
246
+ // `worthFixingNowFixWindow` alias. A schema-level default would fill this
247
+ // key on every config LAYER independently (each layer is parsed through
248
+ // this schema on its own before merging), permanently shadowing a layer
249
+ // that sets only the deprecated alias.
250
+ mediumFixWindow: z.number().int().nonnegative().optional().describe("Per-gate medium fix window: an open medium finding stays in the in-gate fix loop through this many rounds of this gate's chain before deferral. high is exempt (never defers). Default 3."),
251
+ // Deprecated alias for `mediumFixWindow` (pre-rename key); accepted on read
252
+ // and normalized in resolveGateConfig so an unmigrated config still behaves
253
+ // identically. `mediumFixWindow` wins when both are set.
254
+ worthFixingNowFixWindow: z.number().int().nonnegative().optional().describe("Deprecated alias for mediumFixWindow (pre-rename key name); mediumFixWindow wins when both are set."),
255
+ // Ordered, first-match-wins diff-class angle tiers (see resolveGateTier).
256
+ // Absent/empty = tiers never apply, so a gate that never sets this key keeps
257
+ // today's dynamic-subtractive/additive/full-pool resolution unchanged.
258
+ tiers: z.array(GateTier).min(1).describe("Ordered, first-match-wins diff-class angle tiers for this gate. When the first-matching tier's angle set is inside the gate's angle pool, it replaces dynamic angle reduction for that diff class.").optional(),
259
+ });
260
+
261
+ // One named group of angles dispatched together onto a single reviewer under
262
+ // grouped fan-out (AC6). `name` is recorded as the shared reviewer's
263
+ // provenance `group` (see resolveFanoutGroups / fanoutReviewerPairingError).
264
+ const FanoutGroup = z.strictObject({
265
+ name: z.string().trim().min(1).describe("Group name; recorded as the shared reviewer's provenance `group` when this group dispatches."),
266
+ angles: z.array(z.string().trim().min(1)).min(1).describe("Angle names batched onto one reviewer when this group resolves."),
177
267
  });
178
268
 
269
+ // Angle-dispatch fan-out policy (AC6 + #1601 two-knob dispatch bounds). The
270
+ // grouped default batches related angles from a static table onto one
271
+ // reviewer per group, cutting the fixed per-reviewer briefing cost when
272
+ // several angles read the same surface; `per-angle` keeps the original
273
+ // one-reviewer-per-angle fan-out (bypasses configured groups). `gate:full` no
274
+ // longer restores per-angle dispatch (ADR 0047 superseded by 0048): it forces
275
+ // the full angle set upstream (resolveGateTier) and dispatches GROUPED here.
276
+ // Two orthogonal bounds (issue #1601):
277
+ // maxAnglesPerGroup (N, default 3, min 1) — after configured-groups
278
+ // matching, leftover ungrouped angles auto-chunk into dispatch units of
279
+ // ≤N instead of singletons. mode: per-angle bypasses the table entirely
280
+ // maxConcurrent (M, default 4, min 1) — the conductor dispatches at most M
281
+ // dispatch units per wave (scheduleFanoutWaves via scheduleParallelWaves).
282
+ // An angle resolved for a round but not named in any configured group joins
283
+ // the auto-chunked leftover pool — `groups` need only list the angles worth
284
+ // batching explicitly.
285
+ const FanoutConfig = z.strictObject({
286
+ mode: z.enum(["grouped", "per-angle"]).default("grouped").describe("Angle dispatch mode: grouped batches related angles onto one reviewer each (default); per-angle bypasses the configured-groups table and emits one singleton unit per angle (the original full-scrutiny shape). per-angle is equivalent to maxAnglesPerGroup: 1 in dispatch unit size ONLY when no configured multi-angle group matches a resolved angle; otherwise per-angle bypasses configured groups while maxAnglesPerGroup: 1 honors them (matched first, never split)."),
287
+ groups: z.array(FanoutGroup).optional().describe("Static named angle groups consulted in grouped mode. An angle absent from every group joins the auto-chunked leftover pool (chunked into units of ≤maxAnglesPerGroup)."),
288
+ maxAnglesPerGroup: z.number().int().min(1).default(3).describe("Max angles per auto-chunked dispatch unit for leftover ungrouped angles (default 3, min 1). Configured groups are matched first and never split by this knob; mode: per-angle bypasses the table entirely (one singleton per angle)."),
289
+ maxConcurrent: z.number().int().min(1).default(4).describe("Max dispatch units (groups) the conductor dispatches concurrently per wave (default 4, min 1). The wave plan is emitted by write-gate-context.mjs via scheduleFanoutWaves (scheduleParallelWaves)."),
290
+ });
291
+
292
+ /**
293
+ * Two `gates.fanout.groups` entries sharing one `name` would resolve to two
294
+ * dispatch units with the same reviewer-sentinel scope (resolveFanoutGroups
295
+ * keys the scope by group name) — reject at config-validation time rather
296
+ * than let it degrade silently at dispatch time. Applied via `.superRefine`
297
+ * where `FanoutConfig` is used (zod v4 rejects `.partial()` on a schema that
298
+ * already carries a refinement), not on `FanoutConfig` itself.
299
+ * @param {{ groups?: Array<{ name: string }> }} val
300
+ * @param {import("zod").RefinementCtx} ctx
301
+ */
302
+ function rejectDuplicateFanoutGroupNames(val, ctx) {
303
+ if (!Array.isArray(val.groups)) return;
304
+ const seen = new Set();
305
+ for (const [index, group] of val.groups.entries()) {
306
+ if (seen.has(group.name)) {
307
+ ctx.addIssue({
308
+ code: z.ZodIssueCode.custom,
309
+ path: ["groups", index, "name"],
310
+ message: `duplicate gates.fanout.groups name "${group.name}"`,
311
+ });
312
+ }
313
+ seen.add(group.name);
314
+ }
315
+ }
316
+
179
317
  const GatesConfig = z.strictObject({
180
318
  draft: GateConfig.optional(),
181
319
  // `requireCi` is honored on both gates: default true keeps CI a precondition,
@@ -206,16 +344,22 @@ const GatesConfig = z.strictObject({
206
344
  // is active. Default false (opt-in): closing this loophole is additive and
207
345
  // does not change behavior for existing ledgers that carry no provenance.
208
346
  requireFanoutProvenance: z.boolean().default(false),
209
- // Cap on how many scoped `review` reviewers the gate fan-out spawns in
210
- // parallel. When the resolved angle set exceeds this cap, the overflow runs
211
- // in sequential batches and the degradation is recorded in the gate evidence.
212
- maxFanoutReviewers: z.number().int().min(1).max(64).default(8),
213
- // Post the consolidated gate fan-out findings as a visible, marker-tagged PR
214
- // comment so they are auditable and Copilot/humans are aware of them. Default
215
- // true (opt-out). The disposition ledger is written regardless; this flag only
216
- // suppresses the PR comment when explicitly false. See
347
+ // SUPERSEDED by gates.fanout.maxConcurrent (#1601, ADR 0048): the conductor
348
+ // now dispatches wave-by-wave at most M dispatch units per wave via
349
+ // scheduleFanoutWaves (the wave plan emitted by write-gate-context.mjs), so
350
+ // maxFanoutReviewers no longer governs fan-out dispatch. Kept for back-compat
351
+ // (zero non-test callers in the dispatch path); a consumer setting it gets
352
+ // no dispatch effect. See gates.fanout.maxConcurrent for the active cap.
353
+ maxFanoutReviewers: z.number().int().min(1).max(64).default(8).describe("SUPERSEDED by gates.fanout.maxConcurrent (#1601, ADR 0048): no longer governs fan-out dispatch — the conductor dispatches wave-by-wave at most gates.fanout.maxConcurrent (M) dispatch units per wave via scheduleFanoutWaves (the wave plan emitted by write-gate-context.mjs). Kept for back-compat; setting it has no dispatch effect."),
354
+ // #1462 GATE-EXEC-PRIME is MANDATORY (not a flag): every gate fan-out primes the
355
+ // byte-identical briefing prefix before the reviewers read it — see
217
356
  // skills/docs/gate-review-sub-loop-contract.md.
218
- postFindingsComments: z.boolean().default(true),
357
+ // Post the consolidated gate fan-out findings as a SECOND visible,
358
+ // marker-tagged PR comment. Default false (opt-in): the round's verdict
359
+ // review already carries every finding (GATE-COMMENT-SINGLE-SURFACE), so this
360
+ // comment renders each finding's text a second time. The disposition ledger
361
+ // is written regardless. See skills/docs/gate-review-sub-loop-contract.md.
362
+ postFindingsComments: z.boolean().default(false),
219
363
  // Explicit global lens catalog override for additive angle selection
220
364
  // (gates.<gate>.dynamic.additive, #1048). GLOBAL, not per-gate (D1): one
221
365
  // repo-wide catalog for additive selection. When absent, resolveAnglePool()
@@ -229,6 +373,9 @@ const GatesConfig = z.strictObject({
229
373
  // (reject); set false to warn instead of fail. See resolveRejectForeignAngles
230
374
  // / skills/docs/gate-review-sub-loop-contract.md.
231
375
  rejectForeignAngles: z.boolean().default(true),
376
+ // Grouped vs per-angle fan-out dispatch policy + static grouping table
377
+ // (AC6). GLOBAL, not per-gate — see resolveFanoutGroups.
378
+ fanout: FanoutConfig.superRefine(rejectDuplicateFanoutGroupNames).optional(),
232
379
  });
233
380
 
234
381
  const AutonomyConfig = z.strictObject({
@@ -277,7 +424,7 @@ const WorkflowConfig = z.strictObject({
277
424
  // it here would also mean renaming a shipped artifact contract, not just a
278
425
  // config key. Out of scope for this config-shape RFC; revisit as its own
279
426
  // change against skills/docs/gate-review-comment-contract.md + the envelope schema.
280
- requireRetrospective: z.boolean().describe("Require a retrospective checkpoint before a loop completes."),
427
+ requireRetrospective: z.boolean().describe("Require a retrospective checkpoint for the previous qualifying async completion before the next dev-loop start/resume."),
281
428
  requireDraftFirst: z.boolean().describe("Open pull requests as drafts and promote via the draft gate."),
282
429
  devModeDefault: z.boolean().describe("Default new loops to dev mode."),
283
430
  // No default here and absent from BUILT_IN_DEFAULTS — unset means "keep
@@ -586,10 +733,11 @@ const FileGatesConfig = z.strictObject({
586
733
  spike: GateConfig.partial().describe("Relaxed spike gate profile; applies only to spike-mode work.").optional(),
587
734
  requireFanoutEvidence: z.boolean().describe("Require fan-out/fan-in review evidence on gate verdicts; inline single-agent verdicts are rejected except under the strict light-mode exception (under-threshold scope, no gate:full label, recorded inline reason).").optional(),
588
735
  requireFanoutProvenance: z.boolean().describe("Additionally require recorded, internally-consistent fan-out provenance (distinct reviewer count + per-angle dispatch).").optional(),
589
- maxFanoutReviewers: z.number().int().min(1).max(64).describe("Cap on parallel gate fan-out reviewers; overflow runs in sequential batches.").optional(),
590
- postFindingsComments: z.boolean().describe("Post consolidated gate findings as a marker-tagged PR comment (default true).").optional(),
736
+ maxFanoutReviewers: z.number().int().min(1).max(64).describe("SUPERSEDED by gates.fanout.maxConcurrent (#1601, ADR 0048): no longer governs fan-out dispatch — the conductor dispatches wave-by-wave at most gates.fanout.maxConcurrent (M) dispatch units per wave via scheduleFanoutWaves (the wave plan emitted by write-gate-context.mjs). Kept for back-compat; setting it has no dispatch effect.").optional(),
737
+ postFindingsComments: z.boolean().describe("Also post consolidated gate findings as a second marker-tagged PR comment, duplicating the verdict review's own findings (default false).").optional(),
591
738
  anglePool: z.array(z.string().trim().min(1)).describe("Explicit global lens catalog for additive angle selection (global, not per-gate).").optional(),
592
739
  rejectForeignAngles: z.boolean().describe("Reject fan-out provenance naming angles outside the gate's configured pool (default true).").optional(),
740
+ fanout: FanoutConfig.partial().superRefine(rejectDuplicateFanoutGroupNames).describe("Grouped vs per-angle fan-out dispatch policy + static grouping table (global, not per-gate).").optional(),
593
741
  });
594
742
 
595
743
  // ============================================================================
@@ -759,10 +907,13 @@ const DEFAULT_REVIEWER_PERSONA = "default-reviewer";
759
907
  /**
760
908
  * Normalize one raw `gates.<gate>.angles[]` entry (string sugar or object,
761
909
  * possibly hand-built and never zod-validated — e.g. a test config object) to
762
- * `{ name, mandatory?, enabled?, persona?, prompt?, model?, tier? }`. Returns
763
- * null for a malformed/empty entry so callers can filter it out.
910
+ * `{ name, mandatory?, enabled?, persona?, prompt?, model?, tier?, scope? }`.
911
+ * Returns null for a malformed/empty entry so callers can filter it out. An
912
+ * invalid `scope` (not one of GATE_ANGLE_SCOPES) is dropped rather than
913
+ * kept verbatim — resolveGateAngleScope's fail-open default only ever needs
914
+ * to handle an ABSENT field, never a foreign value.
764
915
  * @param {unknown} a
765
- * @returns {{name: string, mandatory?: boolean, enabled?: boolean, persona?: string, prompt?: string, model?: string, tier?: string}|null}
916
+ * @returns {{name: string, mandatory?: boolean, enabled?: boolean, persona?: string, prompt?: string, model?: string, tier?: string, scope?: string}|null}
766
917
  */
767
918
  function normalizeAngleEntry(a) {
768
919
  if (typeof a === "string") {
@@ -779,6 +930,7 @@ function normalizeAngleEntry(a) {
779
930
  if (typeof a.prompt === "string" && a.prompt.length > 0) entry.prompt = a.prompt;
780
931
  if (typeof a.model === "string" && a.model.trim().length > 0) entry.model = a.model.trim();
781
932
  if (typeof a.tier === "string" && a.tier.trim().length > 0) entry.tier = a.tier.trim();
933
+ if (typeof a.scope === "string" && GATE_ANGLE_SCOPES.includes(a.scope.trim())) entry.scope = a.scope.trim();
782
934
  return entry;
783
935
  }
784
936
  return null;
@@ -788,7 +940,7 @@ function normalizeAngleEntry(a) {
788
940
  * Normalize a raw `gates.<gate>.angles` array into full entry objects,
789
941
  * dropping malformed entries.
790
942
  * @param {unknown} raw
791
- * @returns {Array<{name: string, mandatory?: boolean, enabled?: boolean, persona?: string, prompt?: string, model?: string, tier?: string}>}
943
+ * @returns {Array<{name: string, mandatory?: boolean, enabled?: boolean, persona?: string, prompt?: string, model?: string, tier?: string, scope?: string}>}
792
944
  */
793
945
  function normalizeAngleEntries(raw) {
794
946
  if (!Array.isArray(raw)) return [];
@@ -834,6 +986,27 @@ function findAngleEntry(config, name) {
834
986
  return null;
835
987
  }
836
988
 
989
+ /**
990
+ * Resolve a gate angle's declared surface scope (AC3, #1572): "full"
991
+ * (default), "changed-files", or "docs-only" — see GATE_ANGLE_SCOPES. Unlike
992
+ * {@link findAngleEntry} (which searches every gate in a fixed priority
993
+ * order because persona/prompt resolution has no gate context), this looks up
994
+ * the entry within the ONE named gate — an angle's scope is meaningful only
995
+ * for the specific gate pass building its briefing. Fails open to "full" for
996
+ * an angle with no configured entry, a disabled entry, or an
997
+ * unknown/malformed `scope` value: a narrow scope is an opt-in cost saving,
998
+ * never a silently-enforced information cut.
999
+ * @param {DevLoopConfig} config
1000
+ * @param {"draft"|"preApproval"|"spike"} gate
1001
+ * @param {string} name
1002
+ * @returns {"full"|"changed-files"|"docs-only"}
1003
+ */
1004
+ export function resolveGateAngleScope(config, gate, name) {
1005
+ const entries = normalizeAngleEntries(config?.gates?.[gate]?.angles);
1006
+ const found = entries.find((e) => e.name === name && e.enabled !== false);
1007
+ return found?.scope ?? "full";
1008
+ }
1009
+
837
1010
  /**
838
1011
  * Resolve a tier alias to its per-harness concrete model, or `null`
839
1012
  * (`inherit`/unmapped/absent → no override). Deep-merges the alias mapping so
@@ -1233,6 +1406,20 @@ async function applyLayer(merged, basePaths, layer, warnings, errors, options =
1233
1406
  data = { ...data, strategy: "tracker-first" };
1234
1407
  }
1235
1408
 
1409
+ // Removed `gates.primeSharedPrefix` (#1462): GATE-EXEC-PRIME cache priming is
1410
+ // now mandatory, not a knob. The schema is strictObject, so a stale key would
1411
+ // otherwise drop the WHOLE gates layer as invalid. Strip it before validation
1412
+ // with a deprecation warning — old configs keep loading; priming happens
1413
+ // unconditionally regardless of the removed value.
1414
+ if (data?.gates && Object.prototype.hasOwnProperty.call(data.gates, "primeSharedPrefix")) {
1415
+ warnings.push(
1416
+ `gates.primeSharedPrefix is removed (#1462): cache priming is now mandatory, not configurable. ` +
1417
+ `Remove it from ${path.basename(filePath)}; the key is ignored.`
1418
+ );
1419
+ const { primeSharedPrefix: _removed, ...gatesRest } = data.gates;
1420
+ data = { ...data, gates: gatesRest };
1421
+ }
1422
+
1236
1423
  // Validate the file's structure before merging. Pre-existing behavior
1237
1424
  // (unrelated to the #1404 angle-entry redesign): a schema violation ANYWHERE
1238
1425
  // in this layer's file drops the WHOLE layer (errors is populated, `merged`
@@ -1244,6 +1431,35 @@ async function applyLayer(merged, basePaths, layer, warnings, errors, options =
1244
1431
  // is an existing, separate concern.
1245
1432
  const validation = FileConfigSchema.safeParse(data);
1246
1433
  if (!validation.success) {
1434
+ // Surface a visible WARNING (not just the structured error) so the
1435
+ // whole-layer drop is never silent (#1578): many consumers destructure
1436
+ // only `config` (or `config` + `warnings`) and never read `errors`, so a
1437
+ // schema-rejected layer would vanish without a trace. Naming the
1438
+ // offending keys here lets a stale raw-key config (e.g.
1439
+ // gates.<gate>.mandatoryAngles/excludeAngles) point at the canonical
1440
+ // angle-entry migration path.
1441
+ const offendingKeys = validation.error.issues
1442
+ .flatMap((i) => {
1443
+ if (i.code === "unrecognized_keys" && Array.isArray(i.keys) && i.keys.length) {
1444
+ const prefix = i.path.length ? `${i.path.join(".")}.` : "";
1445
+ return i.keys.map((k) => `${prefix}${k}`);
1446
+ }
1447
+ return i.path.length ? [i.path.join(".")] : [];
1448
+ });
1449
+ // Gate the raw-key migration hint: only append it when the offending
1450
+ // keys actually include the pre-redesign mandatoryAngles/excludeAngles
1451
+ // names, so an unrelated schema failure (e.g. a type error) does not get
1452
+ // misleading raw-key migration guidance. (#1578)
1453
+ const hasRawGateKey = offendingKeys.some((k) => /mandatoryAngles|excludeAngles/.test(k));
1454
+ const migrationHint = hasRawGateKey
1455
+ ? ` Migrate raw gates.<gate>.mandatoryAngles/excludeAngles to the canonical angle-entry shape ` +
1456
+ `(gates.<gate>.angles with { name, mandatory: true } / { name, enabled: false }).`
1457
+ : ` Fix or remove the offending key(s) to restore this config layer.`;
1458
+ warnings.push(
1459
+ `${path.basename(filePath)}: config layer rejected by schema — the whole layer was dropped, so this layer's overrides are not applied (previously merged layers remain in effect). ` +
1460
+ `Offending key(s): ${offendingKeys.length ? offendingKeys.join(", ") : "(unknown)"}.` +
1461
+ migrationHint
1462
+ );
1247
1463
  errors.push({
1248
1464
  path: filePath,
1249
1465
  message: `${path.basename(filePath)}: Schema validation failed: ${validation.error.issues.map(i => `${i.path.join(".")}: ${i.message}`).join("; ")}`,
@@ -1608,7 +1824,7 @@ export function resolveRefinement(config) {
1608
1824
  *
1609
1825
  * @param {DevLoopConfig} config
1610
1826
  * @param {"draft"|"preApproval"|"spike"} gate
1611
- * @returns {{ angles: string[]|null, excludeAngles: string[], mandatoryAngles: string[], required: boolean, requireCi: boolean, blockCleanOnFindingSeverities: string[], dynamicAngles: boolean, additiveAngles: boolean }}
1827
+ * @returns {{ angles: string[]|null, excludeAngles: string[], mandatoryAngles: string[], required: boolean, requireCi: boolean, blockCleanOnFindingSeverities: string[], dynamicAngles: boolean, additiveAngles: boolean, mediumFixWindow: number, tiers: Array<{name: string, match: object, angles: string[]}> }}
1612
1828
  */
1613
1829
  export function resolveGateConfig(config, gate) {
1614
1830
  const gateConfig = config?.gates?.[gate];
@@ -1623,11 +1839,19 @@ export function resolveGateConfig(config, gate) {
1623
1839
  mandatoryAngles: entries.filter((e) => e.enabled !== false && e.mandatory === true).map((e) => e.name),
1624
1840
  required: gateConfig?.required ?? true,
1625
1841
  requireCi: gateConfig?.requireCi ?? true,
1626
- dynamicAngles: gateConfig?.dynamic?.subtractive ?? false,
1842
+ dynamicAngles: gateConfig?.dynamic?.subtractive ?? true,
1627
1843
  additiveAngles: gateConfig?.dynamic?.additive ?? false,
1844
+ // Normalized + deduped at the resolve boundary so every consumer (envelope,
1845
+ // verdict poster, fan-in, viewer) sees canonical spellings only; a
1846
+ // half-migrated ["must-fix","low","defer"] collapses to two entries.
1628
1847
  blockCleanOnFindingSeverities: gateConfig?.blockCleanOnFindingSeverities && Array.isArray(gateConfig.blockCleanOnFindingSeverities)
1629
- ? [...gateConfig.blockCleanOnFindingSeverities]
1630
- : ["must-fix"],
1848
+ ? [...new Set(gateConfig.blockCleanOnFindingSeverities.map((s) => normalizeSeverity(s)))]
1849
+ : ["high"],
1850
+ // `mediumFixWindow` wins; `worthFixingNowFixWindow` is the deprecated
1851
+ // pre-rename key, still honored so an unmigrated config keeps its
1852
+ // configured window rather than silently reverting to the default.
1853
+ mediumFixWindow: gateConfig?.mediumFixWindow ?? gateConfig?.worthFixingNowFixWindow ?? 3,
1854
+ tiers: gateConfig?.tiers ?? [],
1631
1855
  };
1632
1856
  }
1633
1857
 
@@ -1650,10 +1874,11 @@ export function resolveRequireFanoutEvidence(config) {
1650
1874
  }
1651
1875
 
1652
1876
  /**
1653
- * Minimum distinct reviewer count for a fanout_fanin ledger to satisfy
1654
- * requireFanoutProvenance. A floor of 2 is the smallest count that is not a
1655
- * single agent; it raises the bar but does not prove independence (provenance
1656
- * is self-reported — see the honest caveat in
1877
+ * ABSOLUTE minimum distinct reviewer count for a fanout_fanin ledger to
1878
+ * satisfy requireFanoutProvenance; the effective read-time floor scales to
1879
+ * max(this, the ledger's fresh-angle count). A floor of 2 is the smallest
1880
+ * count that is not a single agent; it raises the bar but does not prove
1881
+ * independence (provenance is self-reported — see the honest caveat in
1657
1882
  * skills/docs/gate-review-sub-loop-contract.md).
1658
1883
  */
1659
1884
  export const FANOUT_PROVENANCE_MIN_REVIEWERS = 2;
@@ -1687,21 +1912,21 @@ export function resolveRejectForeignAngles(config) {
1687
1912
  }
1688
1913
 
1689
1914
  /**
1690
- * Resolve whether the consolidated gate fan-out findings should be posted as a
1691
- * visible, marker-tagged PR comment.
1915
+ * Resolve whether the consolidated gate fan-out findings should ALSO be posted
1916
+ * as a second visible, marker-tagged PR comment.
1692
1917
  *
1693
- * Returns true (post the comment) unless `gates.postFindingsComments` is
1694
- * explicitly set to false. Using a `!== false` test (rather than `=== true`)
1695
- * keeps the opt-out semantics robust for programmatically-built config objects
1696
- * that bypass schema defaulting. The disposition ledger is written regardless;
1697
- * this flag only suppresses the auditable PR comment. See
1698
- * skills/docs/gate-review-sub-loop-contract.md.
1918
+ * Returns false unless `gates.postFindingsComments` is explicitly set to true.
1919
+ * The round's verdict review is already the findings surface
1920
+ * (`GATE-COMMENT-SINGLE-SURFACE`), so this comment is opt-in duplication; the
1921
+ * `=== true` test keeps that opt-in semantics for programmatically-built config
1922
+ * objects that bypass schema defaulting. The disposition ledger is written
1923
+ * regardless. See skills/docs/gate-review-sub-loop-contract.md.
1699
1924
  *
1700
1925
  * @param {DevLoopConfig} config
1701
1926
  * @returns {boolean}
1702
1927
  */
1703
1928
  export function resolveGatePostFindingsComments(config) {
1704
- return config?.gates?.postFindingsComments !== false;
1929
+ return config?.gates?.postFindingsComments === true;
1705
1930
  }
1706
1931
 
1707
1932
  /**
@@ -1812,14 +2037,187 @@ export function resolveGateDispatchMode(config, gate, { scope, hasFullLabel = fa
1812
2037
  return { mode: "full_fanout", reason: "over_threshold", threshold };
1813
2038
  }
1814
2039
  if (Array.isArray(inlineFindingSeverities) && inlineFindingSeverities.length > 0) {
1815
- const blocking = new Set(resolveGateConfig(config, gate).blockCleanOnFindingSeverities);
1816
- if (inlineFindingSeverities.some((s) => blocking.has(s))) {
2040
+ // Both sides normalize legacy spellings so a "defer" finding still
2041
+ // compares against a "low" blocking entry and vice versa.
2042
+ const blocking = new Set(resolveGateConfig(config, gate).blockCleanOnFindingSeverities.map((s) => normalizeSeverity(s)));
2043
+ if (inlineFindingSeverities.some((s) => blocking.has(normalizeSeverity(s)))) {
1817
2044
  return { mode: "full_fanout", reason: "escalated", threshold };
1818
2045
  }
1819
2046
  }
1820
2047
  return { mode: "inline", reason: "under_threshold", threshold };
1821
2048
  }
1822
2049
 
2050
+ /**
2051
+ * Default auto-chunk size for ungrouped angles (issue #1601). Mirrors the
2052
+ * zod default on `gates.fanout.maxAnglesPerGroup`.
2053
+ */
2054
+ export const DEFAULT_MAX_ANGLES_PER_GROUP = 3;
2055
+
2056
+ /**
2057
+ * Default concurrent-dispatch-unit cap per wave (issue #1601). Mirrors the
2058
+ * zod default on `gates.fanout.maxConcurrent`; consumed by
2059
+ * `scheduleFanoutWaves` (@dev-loops/core/loop/gate-fanin).
2060
+ */
2061
+ export const DEFAULT_FANOUT_MAX_CONCURRENT = 4;
2062
+
2063
+ /**
2064
+ * Resolve `gates.fanout.maxAnglesPerGroup` (issue #1601, default 3, min 1).
2065
+ * The number of ungrouped angles auto-chunked into one dispatch unit.
2066
+ * Defensive, independent of zod: a non-integer or sub-1 value falls back to
2067
+ * the built-in default so a malformed raw merged config (which zod may have
2068
+ * rejected at load time while still returning it) never crashes Phase 2.
2069
+ * @param {DevLoopConfig} config
2070
+ * @returns {number}
2071
+ */
2072
+ export function resolveMaxAnglesPerGroup(config) {
2073
+ const n = config?.gates?.fanout?.maxAnglesPerGroup;
2074
+ if (typeof n !== "number" || !Number.isInteger(n) || n < 1) return DEFAULT_MAX_ANGLES_PER_GROUP;
2075
+ return n;
2076
+ }
2077
+
2078
+ /**
2079
+ * Resolve `gates.fanout.maxConcurrent` (issue #1601, default 4, min 1). The
2080
+ * max dispatch units (groups) the conductor dispatches concurrently per wave.
2081
+ * Defensive, independent of zod (same rationale as resolveMaxAnglesPerGroup).
2082
+ * @param {DevLoopConfig} config
2083
+ * @returns {number}
2084
+ */
2085
+ export function resolveFanoutMaxConcurrent(config) {
2086
+ const m = config?.gates?.fanout?.maxConcurrent;
2087
+ if (typeof m !== "number" || !Number.isInteger(m) || m < 1) return DEFAULT_FANOUT_MAX_CONCURRENT;
2088
+ return m;
2089
+ }
2090
+
2091
+ /**
2092
+ * Resolve grouped fan-out dispatch (AC6 + #1601 two-knob dispatch bounds):
2093
+ * map a round's resolved review angles onto the dispatch units it actually
2094
+ * dispatches.
2095
+ *
2096
+ * Dispatch shape precedence (first match wins):
2097
+ * 1. `gates.fanout.mode === "per-angle"` → bypasses configured groups; one
2098
+ * singleton unit per angle (the original one-reviewer-per-angle fan-out;
2099
+ * NOT equivalent to maxAnglesPerGroup: 1 when configured groups match)
2100
+ * 2. otherwise (default `grouped`) → configured `gates.fanout.groups` are
2101
+ * matched first (unchanged), then the leftover ungrouped angles are
2102
+ * auto-chunked into dispatch units of ≤ `maxAnglesPerGroup` (default 3)
2103
+ * instead of singletons.
2104
+ *
2105
+ * `gate:full` (`options.fullLabel`) NO LONGER restores per-angle dispatch
2106
+ * (ADR 0047 superseded by 0048): `gate:full` keeps forcing the full angle set
2107
+ * UPSTREAM (resolveGateTier returns `gate_full_label`, so resolveGateAnglesDynamic
2108
+ * skips diff-class tier reduction) and dispatches GROUPED here. The `fullLabel`
2109
+ * parameter is retained on the signature (callers thread it) but no longer
2110
+ * changes the dispatch shape — it is a no-op here, kept only to avoid a breaking
2111
+ * API change to the exported resolver; its angle-set effect lives upstream.
2112
+ *
2113
+ * A configured group is included only when at least one of its angles is in
2114
+ * `resolvedAngles` this round — an unmatched group is dropped, never emitted
2115
+ * empty. Configured groups are NEVER split by `maxAnglesPerGroup` (the knob
2116
+ * chunks only the leftover ungrouped pool). Each reviewer still writes ONE
2117
+ * artifact per angle at the existing per-angle paths; grouping only changes how
2118
+ * many reviewers are dispatched, not the artifact shape (see
2119
+ * skills/docs/gate-review-sub-loop-contract.md).
2120
+ *
2121
+ * Auto-chunk unit names are deterministic and stable (issue #1601): a
2122
+ * single-angle leftover chunk is named by its angle (collisions with an emitted
2123
+ * group name disambiguated to `angle:<name>`, preserving the pre-#1601
2124
+ * singleton convention); a multi-angle chunk is named `group:<a>+<b>+<c>` from
2125
+ * its deterministically-ordered members. Unit names key reviewer-sentinel
2126
+ * scopes and provenance `group`, so they must be unique — a chunk whose base
2127
+ * name still collides gets a `#2`/`#3`/… suffix.
2128
+ *
2129
+ * Defensive, independent of zod: `loadDevLoopConfig` returns the raw merged
2130
+ * config even when schema validation fails (on ANY layer, not necessarily
2131
+ * `gates.fanout` itself), so a malformed `gates.fanout.groups` entry can
2132
+ * reach here. A non-object entry, a non-array/blank `angles`, or a
2133
+ * blank/duplicate `name` is dropped (its angles fall through to the leftover
2134
+ * auto-chunk pool) rather than thrown — mirroring the sibling
2135
+ * `normalizeAngleEntries` convention: this resolver degrades to a smaller
2136
+ * grouping table, never crashes the conductor's Phase 2 planning.
2137
+ * `resolvedAngles` is deduplicated up front so a duplicated entry (e.g. a
2138
+ * hand-built `--angles` list) never mints two dispatch units sharing one name.
2139
+ *
2140
+ * @param {DevLoopConfig} config
2141
+ * @param {"draft"|"preApproval"|"spike"} gate unused today — fan-out grouping
2142
+ * is a global policy (`gates.fanout`), not per-gate; accepted for symmetry
2143
+ * with the other `resolveGate*(config, gate, ...)` resolvers.
2144
+ * @param {string[]} resolvedAngles this round's resolved angle names
2145
+ * @param {{ fullLabel?: boolean }} [options] — retained for API stability;
2146
+ * no longer changes the dispatch shape (see `gate:full` note above).
2147
+ * @returns {{ name: string, angles: string[] }[]}
2148
+ */
2149
+ export function resolveFanoutGroups(config, gate, resolvedAngles, { fullLabel = false } = {}) {
2150
+ const angles = Array.isArray(resolvedAngles)
2151
+ ? [...new Set(resolvedAngles.filter((a) => typeof a === "string" && a.trim().length > 0).map((a) => a.trim()))]
2152
+ : [];
2153
+ const perAngleGroups = () => angles.map((name) => ({ name, angles: [name] }));
2154
+ // per-angle: bypass configured groups and emit one singleton unit per
2155
+ // angle (the original one-reviewer-per-angle fan-out). gate:full no longer
2156
+ // takes this branch (ADR 0047 superseded by 0048): fullLabel is a no-op here.
2157
+ const fanout = config?.gates?.fanout ?? {};
2158
+ if (fanout.mode === "per-angle") return perAngleGroups();
2159
+ const angleSet = new Set(angles);
2160
+ const rawGroups = Array.isArray(fanout.groups) ? fanout.groups : [];
2161
+ const configuredGroups = [];
2162
+ const seenGroupNames = new Set();
2163
+ for (const group of rawGroups) {
2164
+ if (!group || typeof group !== "object" || Array.isArray(group)) continue;
2165
+ const name = typeof group.name === "string" ? group.name.trim() : "";
2166
+ if (name.length === 0 || seenGroupNames.has(name)) continue;
2167
+ const groupAngles = Array.isArray(group.angles)
2168
+ ? [...new Set(group.angles.filter((a) => typeof a === "string" && a.trim().length > 0).map((a) => a.trim()))]
2169
+ : [];
2170
+ if (groupAngles.length === 0) continue;
2171
+ seenGroupNames.add(name);
2172
+ configuredGroups.push({ name, angles: groupAngles });
2173
+ }
2174
+ const grouped = new Set();
2175
+ const result = [];
2176
+ for (const group of configuredGroups) {
2177
+ const members = group.angles.filter((a) => angleSet.has(a) && !grouped.has(a));
2178
+ if (members.length === 0) continue;
2179
+ for (const a of members) grouped.add(a);
2180
+ result.push({ name: group.name, angles: members });
2181
+ }
2182
+ // Issue #1601: leftover ungrouped angles auto-chunk into dispatch units of
2183
+ // ≤ maxAnglesPerGroup (default 3) instead of singletons. Configured groups
2184
+ // are matched first and never split by this knob (only the leftover pool is
2185
+ // chunked). Deterministic order (input order) + stable unit names.
2186
+ const usedNames = new Set(result.map((g) => g.name));
2187
+ const leftover = angles.filter((name) => !grouped.has(name));
2188
+ const maxAnglesPerGroup = resolveMaxAnglesPerGroup(config);
2189
+ for (let i = 0; i < leftover.length; i += maxAnglesPerGroup) {
2190
+ const chunk = leftover.slice(i, i + maxAnglesPerGroup);
2191
+ const unitName = stableAutoChunkUnitName(chunk, usedNames);
2192
+ usedNames.add(unitName);
2193
+ result.push({ name: unitName, angles: chunk });
2194
+ }
2195
+ return result;
2196
+ }
2197
+
2198
+ /**
2199
+ * Deterministic, stable dispatch-unit name for an auto-chunked leftover
2200
+ * unit (issue #1601). A single-angle chunk keeps the pre-#1601 singleton
2201
+ * convention (the angle name, disambiguated to `angle:<name>` on collision
2202
+ * with an emitted group name); a multi-angle chunk is named
2203
+ * `group:<a>+<b>+<c>` from its deterministically-ordered members, with a
2204
+ * `#N` suffix when even that base collides. Pure.
2205
+ * @param {string[]} chunk — non-empty, deterministically ordered
2206
+ * @param {Set<string>} usedNames — already-emitted unit names (mutated by caller)
2207
+ * @returns {string}
2208
+ */
2209
+ function stableAutoChunkUnitName(chunk, usedNames) {
2210
+ if (chunk.length === 1) {
2211
+ const name = chunk[0];
2212
+ return usedNames.has(name) ? `angle:${name}` : name;
2213
+ }
2214
+ const base = `group:${chunk.join("+")}`;
2215
+ if (!usedNames.has(base)) return base;
2216
+ let k = 2;
2217
+ while (usedNames.has(`${base}#${k}`)) k++;
2218
+ return `${base}#${k}`;
2219
+ }
2220
+
1823
2221
  /**
1824
2222
  * Resolve review angles for a specific gate from the merged dev-loop config.
1825
2223
  *
@@ -1904,14 +2302,79 @@ export function resolveGateAngleContract(config, gate) {
1904
2302
  return { mandatoryAngles, pool };
1905
2303
  }
1906
2304
 
2305
+ /**
2306
+ * Resolve the diff-class angle tier for a gate from its configured, ordered
2307
+ * `gates.<gate>.tiers` list (first-match-wins). Pure and synchronous — the
2308
+ * single source of truth for tier selection, consulted at the top of
2309
+ * `resolveGateAnglesDynamic` before any dynamic subtractive/additive
2310
+ * reduction runs.
2311
+ *
2312
+ * FAIL CLOSED at every uncertain step: the `gate:full` label, no tiers
2313
+ * configured, an unavailable/malformed scope, a changed dev-loop
2314
+ * config-source file (`isDevLoopConfigSourcePath`), or an unclassifiable
2315
+ * changed file (`classifyFile` returns "unknown") all resolve to `tier: null`
2316
+ * rather than a guess. A matched tier's angle set is additionally validated
2317
+ * against the gate's angle pool (`resolveGateAngleContract`) — ANY tier angle
2318
+ * outside a non-null pool voids the whole match (no partial intersection): a
2319
+ * typo'd tier angle is caught here, not by silently dropping reviewers at
2320
+ * gate time.
2321
+ *
2322
+ * @param {DevLoopConfig} config
2323
+ * @param {"draft"|"preApproval"|"spike"} gate
2324
+ * @param {object} facts
2325
+ * @param {string[]} [facts.changedFiles] — repo-relative changed file paths for this diff
2326
+ * @param {number} [facts.filesChanged] — count of changed files
2327
+ * @param {number} [facts.linesChanged] — count of changed lines (added + deleted)
2328
+ * @param {boolean} [facts.hasFullLabel] — `gate:full` label present on the PR
2329
+ * @returns {{ tier: string|null, angles: string[]|null, reason: string }}
2330
+ */
2331
+ export function resolveGateTier(config, gate, { changedFiles, filesChanged, linesChanged, hasFullLabel = false } = {}) {
2332
+ if (hasFullLabel) {
2333
+ return { tier: null, angles: null, reason: "gate_full_label" };
2334
+ }
2335
+ const tiers = resolveGateConfig(config, gate).tiers;
2336
+ if (tiers.length === 0) {
2337
+ return { tier: null, angles: null, reason: "no_tiers_configured" };
2338
+ }
2339
+ if (
2340
+ !Array.isArray(changedFiles) || changedFiles.length === 0 ||
2341
+ !Number.isFinite(filesChanged) || !Number.isFinite(linesChanged)
2342
+ ) {
2343
+ return { tier: null, angles: null, reason: "scope_unavailable" };
2344
+ }
2345
+ if (changedFiles.some((f) => isDevLoopConfigSourcePath(f))) {
2346
+ return { tier: null, angles: null, reason: "config_source_delta" };
2347
+ }
2348
+ const kinds = changedFiles.map((f) => classifyFile(f));
2349
+ if (kinds.some((k) => k === "unknown")) {
2350
+ return { tier: null, angles: null, reason: "unclassifiable_file" };
2351
+ }
2352
+ const matched = tiers.find((t) => {
2353
+ const match = t.match ?? {};
2354
+ if (Array.isArray(match.kinds) && !kinds.every((k) => match.kinds.includes(k))) return false;
2355
+ if (typeof match.maxFiles === "number" && filesChanged > match.maxFiles) return false;
2356
+ if (typeof match.maxLines === "number" && linesChanged > match.maxLines) return false;
2357
+ return true;
2358
+ });
2359
+ if (!matched) {
2360
+ return { tier: null, angles: null, reason: "no_tier_match" };
2361
+ }
2362
+ const { mandatoryAngles, pool } = resolveGateAngleContract(config, gate);
2363
+ if (pool !== null && matched.angles.some((a) => !pool.includes(a))) {
2364
+ return { tier: null, angles: null, reason: "angle_outside_pool" };
2365
+ }
2366
+ return { tier: matched.name, angles: [...new Set([...mandatoryAngles, ...matched.angles])], reason: "tier_match" };
2367
+ }
2368
+
1907
2369
  /**
1908
2370
  * Resolve gate angles dynamically when `dynamicAngles` is enabled in config.
1909
2371
  *
1910
2372
  * Uses diff analysis helpers (from ../analysis/*) to filter the
1911
2373
  * configured angle list down to only angles relevant to the change set.
1912
2374
  *
1913
- * When `dynamicAngles` is disabled (default), returns the full configured
1914
- * angle list (same as `resolveGateAngles`).
2375
+ * When `dynamicAngles` is disabled (opt-out via `dynamic.subtractive: false`,
2376
+ * see #1579), returns the full configured angle list (same as
2377
+ * `resolveGateAngles`); no diff also falls back to the full static pool.
1915
2378
  *
1916
2379
  * When `additiveAngles` is also enabled (default off, see #1048), catalog
1917
2380
  * angles from `resolveAnglePool()` (`gates.anglePool`, or else the union of
@@ -1919,13 +2382,55 @@ export function resolveGateAngleContract(config, gate) {
1919
2382
  * by change-category heuristics but absent from the gate's configured pool
1920
2383
  * may also be added; `excludeAngles` remains a hard ceiling on additions.
1921
2384
  *
2385
+ * Diff-class angle tiers (`gates.<gate>.tiers`, see `resolveGateTier`) are
2386
+ * consulted FIRST, ahead of any subtractive/additive reduction below: when the
2387
+ * diff's changed-file scope matches a configured tier, that tier's angle set
2388
+ * (unioned with mandatory angles) is returned directly and the
2389
+ * subtractive/additive machinery below is skipped entirely. No tier match
2390
+ * (including "no tiers configured") falls through to the existing behavior
2391
+ * unchanged.
2392
+ *
1922
2393
  * @param {import("./types.js").DevLoopConfig} config
1923
2394
  * @param {"draft"|"preApproval"} gate
1924
2395
  * @param {object} [options]
1925
2396
  * @param {{ nameStatusOutput: string, diffOutput?: string }} [options.diff]
2397
+ * @param {boolean} [options.hasFullLabel] — `gate:full` label present on the PR (bypasses tier resolution)
1926
2398
  * @returns {{ recommendedAngles: string[] | null, skippedAngles: string[], reasons: Record<string,string>, fallbackToAll: boolean, dynamicAnglesActive: boolean, addedAngles: string[], addedReasons: Record<string,string> }}
1927
2399
  */
1928
- export async function resolveGateAnglesDynamic(config, gate, { diff } = {}) {
2400
+ export async function resolveGateAnglesDynamic(config, gate, { diff, hasFullLabel = false } = {}) {
2401
+ // Tier scope facts: changedFiles/filesChanged from T0 (file-level), linesChanged
2402
+ // from T1 (hunk-level) reused for its real added+deleted line count rather than
2403
+ // T0's/analyzeDiff's own inferred-category path, which reports a fake 0 line
2404
+ // count for an unambiguous (e.g. docs-only) diff — see analyzeT1/analyzeDiff.
2405
+ let changedFiles;
2406
+ let filesChanged;
2407
+ let linesChanged;
2408
+ if (diff) {
2409
+ const { analyzeT0, analyzeT1 } = await import("../analysis/diff-analyzer.mjs");
2410
+ const t0 = analyzeT0(diff.nameStatusOutput);
2411
+ changedFiles = t0.files;
2412
+ filesChanged = changedFiles.length;
2413
+ if (diff.diffOutput) {
2414
+ const lineStats = analyzeT1(diff.diffOutput, t0).lineStats;
2415
+ linesChanged = lineStats.added + lineStats.deleted;
2416
+ }
2417
+ }
2418
+ const tierResult = resolveGateTier(config, gate, { changedFiles, filesChanged, linesChanged, hasFullLabel });
2419
+ if (tierResult.tier) {
2420
+ const configuredAngles = resolveGateAngles(config, gate) ?? [];
2421
+ const tierAngleSet = new Set(tierResult.angles);
2422
+ const skippedAngles = configuredAngles.filter((a) => !tierAngleSet.has(a));
2423
+ return {
2424
+ recommendedAngles: tierResult.angles,
2425
+ skippedAngles,
2426
+ reasons: Object.fromEntries(skippedAngles.map((a) => [a, `tier:${tierResult.tier}`])),
2427
+ fallbackToAll: false,
2428
+ dynamicAnglesActive: true,
2429
+ addedAngles: [],
2430
+ addedReasons: {},
2431
+ };
2432
+ }
2433
+
1929
2434
  const gateConfig = resolveGateConfig(config, gate);
1930
2435
  const staticAngles = resolveGateAngles(config, gate);
1931
2436
  if (staticAngles === null) {