@bluenyang/reins 0.3.3 → 0.3.4-rc.1

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,6 +1,8 @@
1
1
  import { execFileSync, spawnSync } from 'node:child_process';
2
2
  import { existsSync, readFileSync, readdirSync } from 'node:fs';
3
3
  import { join } from 'node:path';
4
+ import { ciRuns } from './detect-ci.js';
5
+ import { forwards, ownProjectFile, workspaceGlobs, workspacePackages, } from './detect-workspaces.js';
4
6
  export function generation(root) {
5
7
  const agents = join(root, 'AGENTS.md');
6
8
  const hasAgents = existsSync(agents);
@@ -66,7 +68,7 @@ const MUTATING = /--fix\b|--write\b|\bfmt\b(?!:check)|(^|\s)format(\s|$)/;
66
68
  const tidy = (s) => s.trim().replace(/\s+/g, ' ');
67
69
  const unredirect = (s) => s.replace(/\d*>&\d+/g, '').replace(/\d*>\s*\S+/g, '');
68
70
  /**
69
- * Quoted text that holds more than one word or a shell operator becomes one opaque word, so text inside
71
+ * Quoted text that is not one plain word (letters, digits, `_`, `:`, `.`, `-`) becomes one opaque word, so text inside
70
72
  * quotes is never split into commands (023 A-11); a quoted single word stays for `unquote`. null when a
71
73
  * quote is left open or a backslash appears: the body is then unclear and nothing in it counts.
72
74
  */
@@ -134,59 +136,296 @@ function calls(bundle, name, scripts) {
134
136
  const own = masked === null || masked.includes(OPAQUE) ? '' : tidy(unredirect(masked));
135
137
  return pieces(bundle).some((piece) => runsByName(piece, name) || (own !== '' && piece === own));
136
138
  }
139
+ /** The reasons a candidate carries (024 S-9); tests assert these texts. */
140
+ export const REASON = {
141
+ ci: (file) => `also in CI (${file})`,
142
+ runs: (by, piece) => `${by} already runs it (${piece})`,
143
+ calls: (bundle) => `${bundle} already calls it`,
144
+ dependsOn: (target) => `Makefile ${target} depends on it`,
145
+ holds: (by) => `the body of ${by} holds it`,
146
+ mutates: 'rewrites files',
147
+ neverEnds: 'never ends',
148
+ install: 'install step',
149
+ platform: 'platform packaging',
150
+ outside: (file) => `outside the workspace (${file})`,
151
+ unclear: (why) => `unclear (${why})`,
152
+ };
153
+ const KEYWORDS = new Set(['check', 'lint', 'test', 'typecheck', 'build', 'e2e', 'verify', 'ci']);
154
+ const NEVER_ENDS = new Set(['dev', 'start', 'watch', 'serve', 'preview']);
155
+ const INSTALL_STEPS = new Set(['prepare', 'preinstall', 'install', 'postinstall']);
156
+ const PLATFORM = new Set([
157
+ 'win',
158
+ 'windows',
159
+ 'mac',
160
+ 'macos',
161
+ 'darwin',
162
+ 'linux',
163
+ 'dmg',
164
+ 'exe',
165
+ 'msi',
166
+ 'appimage',
167
+ 'deb',
168
+ 'rpm',
169
+ 'snap',
170
+ 'pkg',
171
+ ]);
172
+ const words = (name) => name.split(/[:_-]/);
173
+ const bodyWords = (body) => body.split(/\s+/);
174
+ /** Whether a script name is a gate candidate by its name (024 S-4): a keyword as one of its words. */
175
+ export function keywordName(name) {
176
+ return name === 'type-check' || words(name).some((w) => KEYWORDS.has(w));
177
+ }
178
+ /** Why a script is never a gate (024 S-4), or null. A check-named script that rewrites files is only passed over. */
179
+ export function exclusion(name, body) {
180
+ if (INSTALL_STEPS.has(name))
181
+ return REASON.install;
182
+ if (words(name).some((w) => NEVER_ENDS.has(w)) ||
183
+ bodyWords(body).some((w) => NEVER_ENDS.has(w) || w === '--watch'))
184
+ return REASON.neverEnds;
185
+ if (words(name).some((w) => PLATFORM.has(w.toLowerCase())))
186
+ return REASON.platform;
187
+ if (MUTATING.test(body))
188
+ return REASON.mutates;
189
+ return null;
190
+ }
191
+ /** A `pre<x>`/`post<x>` script of another script `x`: an npm lifecycle hook, not a gate. */
192
+ const lifecycle = (name, scripts) => {
193
+ const m = /^(?:pre|post)(.+)$/.exec(name);
194
+ return !!m && m[1] in scripts;
195
+ };
196
+ /** A gate name from a script name: `:` cannot stand in a gates.yaml key the gate script reads (024 A-3). */
197
+ const gateName = (s) => s.replace(/:/g, '-');
198
+ const readScripts = (p) => {
199
+ try {
200
+ const s = JSON.parse(readFileSync(p, 'utf8'))
201
+ .scripts;
202
+ return Object.fromEntries(Object.entries(s ?? {}).filter((e) => typeof e[1] === 'string'));
203
+ }
204
+ catch {
205
+ return {};
206
+ }
207
+ };
208
+ /** The Makefile's targets with their prerequisites and recipe lines. */
209
+ function makeTargets(text) {
210
+ const out = new Map();
211
+ let cur = null;
212
+ for (const line of text.split(/\r?\n/)) {
213
+ const m = /^([A-Za-z0-9_.-]+)\s*:(?!=)\s*(.*)$/.exec(line);
214
+ if (m && !m[1].startsWith('.')) {
215
+ cur = { deps: m[2].replace(/#.*/, '').trim().split(/\s+/).filter(Boolean), recipe: [] };
216
+ out.set(m[1], cur);
217
+ }
218
+ else if (line.startsWith('\t') && cur)
219
+ cur.recipe.push(line.trim());
220
+ else if (line.trim() && !line.startsWith('#'))
221
+ cur = null;
222
+ }
223
+ return out;
224
+ }
225
+ const MAKE_TARGETS = ['check', 'lint', 'test', 'build', 'vet', 'verify'];
226
+ /** The pieces of a body split into words, quoted words unquoted (for the workspace forms). */
227
+ const pieceWords = (piece) => piece.split(' ').map(unquote);
137
228
  /**
138
- * The PM's discovery step 2–3 (design §5): read the project's scripts, drop mutating ones,
139
- * prefer the aggregate `check` the hooks and CI already call.
229
+ * Every gate candidate of the repository (024 S-4–S-10): the root package.json (or the root Makefile),
230
+ * package.json scripts, Makefile targets and go.mod below the root to depth 3, and the one-line `run:`
231
+ * commands of pull-request workflows, each with its status and reasons.
140
232
  */
141
- export function discoverGates(root) {
142
- const res = { gates: [], excluded: [], proposals: subdirProposals(root) };
233
+ export function detectCandidates(root) {
234
+ const out = [];
143
235
  const run = runner(root);
144
- const pkgPath = join(root, 'package.json');
145
- if (run && run !== 'make' && existsSync(pkgPath)) {
146
- const scripts = JSON.parse(readFileSync(pkgPath, 'utf8')).scripts ??
147
- {};
236
+ const pkgs = workspacePackages(root);
237
+ const covered = (c, why) => {
238
+ if (c.status === 'excluded')
239
+ return;
240
+ if (c.status !== 'covered')
241
+ c.status = 'covered';
242
+ if (!c.reasons.includes(why))
243
+ c.reasons.push(why);
244
+ };
245
+ // the root
246
+ const rootScripts = run && run !== 'make' && existsSync(join(root, 'package.json'))
247
+ ? readScripts(join(root, 'package.json'))
248
+ : {};
249
+ if (run && run !== 'make' && existsSync(join(root, 'package.json'))) {
148
250
  const cmd = (s) => (run === 'npm' ? `npm run ${s}` : `${run} ${s}`);
149
- for (const [name, body] of Object.entries(scripts))
150
- if (MUTATING.test(body) && (!/check/.test(name) || name === 'typecheck'))
151
- res.excluded.push({ name, cmd: body, why: 'mutates files (--fix/--write) — not a gate' });
152
- const pick = (names) => names.find((n) => n in scripts && !MUTATING.test(scripts[n] ?? ''));
153
- const check = pick(['check', 'verify', 'ci', 'lint:check']) ?? null;
154
- if (check)
155
- res.gates.push({ name: 'check', cmd: cmd(check), from: `package.json#scripts.${check}` });
156
- const bundle = check ? (scripts[check] ?? '') : null;
157
- for (const n of ['lint', 'typecheck']) {
158
- const found = pick([n]);
159
- if (found && !calls(bundle, n, scripts))
160
- res.gates.push({ name: n, cmd: cmd(n), from: `package.json#scripts.${n}` });
161
- }
162
- const test = check && /\btest\b/.test(scripts[check] ?? '') ? null : pick(['test', 'test:unit']);
163
- if (test)
164
- res.gates.push({ name: 'test', cmd: cmd(test), from: `package.json#scripts.${test}` });
165
- const build = pick(['build']);
166
- if (build)
167
- res.gates.push({ name: 'build', cmd: cmd(build), from: 'package.json#scripts.build' });
168
- const e2e = pick(['test:e2e', 'e2e']);
169
- if (e2e)
170
- res.gates.push({
171
- name: 'e2e',
172
- cmd: cmd(e2e),
173
- from: `package.json#scripts.${e2e}`,
174
- optional: true,
251
+ const bundle = ['check', 'verify', 'ci'].find((n) => n in rootScripts && !MUTATING.test(rootScripts[n] ?? '')) ?? null;
252
+ const has = (n) => n in rootScripts && !exclusion(n, rootScripts[n] ?? '');
253
+ const test = has('test') ? 'test' : has('test:unit') ? 'test:unit' : null;
254
+ const e2e = has('test:e2e') ? 'test:e2e' : has('e2e') ? 'e2e' : null;
255
+ const picks = new Set([
256
+ bundle,
257
+ 'lint',
258
+ 'typecheck',
259
+ test,
260
+ 'build',
261
+ e2e,
262
+ 'lint:check',
263
+ 'format:check',
264
+ 'type-check',
265
+ ].filter((n) => !!n));
266
+ for (const [name, body] of Object.entries(rootScripts)) {
267
+ if (!/^[\w:.-]+$/.test(name) || lifecycle(name, rootScripts))
268
+ continue;
269
+ const why = exclusion(name, body);
270
+ if (why) {
271
+ if (why === REASON.mutates && /check/.test(name) && name !== 'typecheck')
272
+ continue;
273
+ out.push({
274
+ name,
275
+ cmd: body,
276
+ from: `package.json#scripts.${name}`,
277
+ dir: '',
278
+ script: name,
279
+ source: 'npm',
280
+ status: 'excluded',
281
+ reasons: [why],
282
+ body,
283
+ });
284
+ continue;
285
+ }
286
+ if (!keywordName(name))
287
+ continue;
288
+ const g = name === bundle ? 'check' : name === test ? 'test' : name === e2e ? 'e2e' : gateName(name);
289
+ out.push({
290
+ name: g,
291
+ cmd: cmd(name),
292
+ from: `package.json#scripts.${name}`,
293
+ dir: '',
294
+ script: name,
295
+ source: 'npm',
296
+ status: picks.has(name) ? 'pick' : 'offer',
297
+ reasons: [],
298
+ body,
299
+ ...(name === e2e ? { optional: true } : {}),
175
300
  });
301
+ }
302
+ // what the bundle already calls (023 S-2, now for every candidate)
303
+ if (bundle)
304
+ for (const c of out)
305
+ if (c.dir === '' &&
306
+ c.script !== bundle &&
307
+ calls(rootScripts[bundle] ?? '', c.script, rootScripts))
308
+ covered(c, REASON.calls(bundle));
176
309
  }
177
310
  else if (run === 'make') {
178
- const mk = readFileSync(join(root, 'Makefile'), 'utf8');
179
- for (const t of ['check', 'test', 'build'])
180
- if (new RegExp(`^${t}:`, 'm').test(mk))
181
- res.gates.push({ name: t, cmd: `make ${t}`, from: `Makefile:${t}` });
311
+ const t = makeTargets(readFileSync(join(root, 'Makefile'), 'utf8'));
312
+ for (const name of MAKE_TARGETS)
313
+ if (t.has(name))
314
+ out.push({
315
+ name,
316
+ cmd: `make ${name}`,
317
+ from: `Makefile:${name}`,
318
+ dir: '',
319
+ script: name,
320
+ source: 'make',
321
+ status: ['check', 'test', 'build'].includes(name) ? 'pick' : 'offer',
322
+ reasons: [],
323
+ });
324
+ }
325
+ // below the root (spec 019 S-5): package.json, Makefile, go.mod
326
+ out.push(...subdirCandidates(root, run));
327
+ // a body that holds another candidate's body in the same directory (S-6)
328
+ for (const c of out) {
329
+ if (c.status === 'excluded' || !c.body)
330
+ continue;
331
+ const own = c.body.trim().replace(/\s+/g, ' ');
332
+ for (const by of out)
333
+ if (by !== c &&
334
+ by.dir === c.dir &&
335
+ by.body &&
336
+ by.status !== 'excluded' &&
337
+ by.script !== c.script) {
338
+ const ps = pieces(by.body);
339
+ if (ps.length > 1 && ps.includes(own))
340
+ covered(c, REASON.holds(by.script));
341
+ }
182
342
  }
183
- return res;
343
+ // Makefiles: a target another one depends on or calls; go test/vet a recipe already runs (S-6)
344
+ const dirs = new Set(out.filter((c) => c.source === 'make').map((c) => c.dir));
345
+ for (const d of dirs) {
346
+ const t = makeTargets(readFileSync(join(root, d, 'Makefile'), 'utf8'));
347
+ for (const c of out.filter((x) => x.dir === d && x.source === 'make'))
348
+ for (const [target, { deps, recipe }] of t)
349
+ if (target !== c.script &&
350
+ MAKE_TARGETS.includes(target) &&
351
+ (deps.includes(c.script) ||
352
+ recipe.some((r) => new RegExp(`(?:\\$\\(MAKE\\)|\\bmake)\\s+${c.script}\\b`).test(r))))
353
+ covered(c, REASON.dependsOn(target));
354
+ const recipes = [...t.values()].flatMap((x) => x.recipe);
355
+ for (const c of out.filter((x) => x.dir === d && x.source === 'go'))
356
+ if (recipes.some((r) => new RegExp(`\\bgo ${c.script}\\b`).test(r)))
357
+ covered(c, REASON.holds(`Makefile ${c.script}`));
358
+ }
359
+ // workspace packages a root script already runs (S-7)
360
+ if (pkgs.length)
361
+ for (const [name, body] of Object.entries(rootScripts)) {
362
+ if (exclusion(name, body))
363
+ continue;
364
+ for (const piece of pieces(body)) {
365
+ const f = forwards(pieceWords(piece), pkgs);
366
+ if (!f || 'unclear' in f)
367
+ continue;
368
+ for (const c of out)
369
+ if (c.source === 'npm' &&
370
+ f.pkgs.some((p) => p.dir === c.dir) &&
371
+ f.scripts.includes(c.script))
372
+ covered(c, REASON.runs(name, piece));
373
+ }
374
+ }
375
+ // a directory with a project file of its own is outside the root workspace
376
+ if (pkgs.length || workspaceGlobs(root))
377
+ for (const c of out) {
378
+ const own = ownProjectFile(root, c.dir);
379
+ if (own && c.source === 'npm' && !c.reasons.includes(REASON.outside(own)))
380
+ c.reasons.push(REASON.outside(own));
381
+ }
382
+ // pull-request workflows (S-8)
383
+ const skip = (cmd) => exclusion('', cmd) !== null;
384
+ for (const r of ciRuns(root, compound, skip)) {
385
+ const same = out.find((c) => c.status !== 'excluded' && c.cmd === r.cmd) ??
386
+ out.find((c) => c.status !== 'excluded' &&
387
+ c.dir === r.dir &&
388
+ c.body !== undefined &&
389
+ c.body.trim().replace(/\s+/g, ' ') === r.raw);
390
+ if (same) {
391
+ if (!same.reasons.includes(REASON.ci(r.file)))
392
+ same.reasons.push(REASON.ci(r.file));
393
+ continue;
394
+ }
395
+ const slug = r.cmd
396
+ .split(' ')
397
+ .filter((w) => !w.startsWith('-') && !w.startsWith('.') && w !== r.dir)
398
+ .map((w) => w.replace(/[^A-Za-z0-9]+/g, '-').replace(/^-|-$/g, ''))
399
+ .filter(Boolean)
400
+ .join('-');
401
+ out.push({
402
+ name: `ci-${slug}`,
403
+ cmd: r.cmd,
404
+ from: r.file,
405
+ dir: r.dir,
406
+ script: r.raw,
407
+ source: 'ci',
408
+ status: 'offer',
409
+ reasons: [REASON.ci(r.file)],
410
+ });
411
+ }
412
+ // one name each: a later candidate with a taken name gets -2, -3 …
413
+ const taken = new Set();
414
+ for (const c of out) {
415
+ if (c.status === 'excluded')
416
+ continue;
417
+ let n = c.name;
418
+ for (let i = 2; taken.has(n); i++)
419
+ n = `${c.name}-${i}`;
420
+ c.name = n;
421
+ taken.add(n);
422
+ }
423
+ return out;
184
424
  }
185
425
  const SKIP_DIRS = new Set(['node_modules', '.git', 'dist', 'build', 'vendor', '.loctx', '.agents']);
186
426
  const MAX_DEPTH = 3;
187
427
  const SAFE_DIR = /^[\w@.+-]+(\/[\w@.+-]+)*$/;
188
428
  const ROOT_GATE_NAMES = ['check', 'lint', 'typecheck', 'test', 'build', 'e2e'];
189
- const NODE_SCRIPTS = ['check', 'verify', 'ci', 'typecheck', 'lint', 'test', 'build'];
190
429
  function dirRunner(root, dir, rootRunner) {
191
430
  if (existsSync(join(root, dir, 'pnpm-lock.yaml')))
192
431
  return 'pnpm';
@@ -198,12 +437,11 @@ function dirRunner(root, dir, rootRunner) {
198
437
  }
199
438
  const SOURCE_ORDER = ['npm', 'make', 'go'];
200
439
  /**
201
- * Gate candidates below the root (spec S-5): package.json scripts, Makefile targets and go.mod,
202
- * to depth 3. One command each, using the tool's own directory option. The caller chooses.
440
+ * Candidates below the root (019 S-5, 024 S-4): package.json scripts named by the pattern, Makefile
441
+ * targets and go.mod, to depth 3, one command each with the tool's own directory option.
203
442
  */
204
- export function subdirProposals(root) {
443
+ function subdirCandidates(root, rootRunner) {
205
444
  const cands = [];
206
- const rootRunner = runner(root);
207
445
  const walk = (dir, depth) => {
208
446
  let entries;
209
447
  try {
@@ -220,62 +458,65 @@ export function subdirProposals(root) {
220
458
  continue;
221
459
  const pkg = join(root, rel, 'package.json');
222
460
  if (existsSync(pkg)) {
223
- let scripts = {};
224
- try {
225
- scripts =
226
- JSON.parse(readFileSync(pkg, 'utf8'))
227
- .scripts ?? {};
228
- }
229
- catch {
230
- // unreadable package.json: no proposals from it
231
- }
461
+ const scripts = readScripts(pkg);
232
462
  const run = dirRunner(root, rel, rootRunner);
233
- for (const sc of NODE_SCRIPTS)
234
- if (sc in scripts && !MUTATING.test(scripts[sc] ?? '') && /^[\w:.-]+$/.test(sc)) {
235
- const cmd = run === 'npm'
236
- ? `npm --prefix ${rel} run ${sc}`
237
- : run === 'yarn'
238
- ? `yarn --cwd ${rel} ${sc}`
239
- : `pnpm --dir ${rel} ${sc}`;
463
+ for (const [sc, body] of Object.entries(scripts))
464
+ if (/^[\w:.-]+$/.test(sc) &&
465
+ keywordName(sc) &&
466
+ !lifecycle(sc, scripts) &&
467
+ !exclusion(sc, body))
240
468
  cands.push({
241
- rel,
242
- source: 'npm',
243
- script: sc,
244
- cmd,
469
+ name: '',
470
+ cmd: run === 'npm'
471
+ ? `npm --prefix ${rel} run ${sc}`
472
+ : run === 'yarn'
473
+ ? `yarn --cwd ${rel} ${sc}`
474
+ : `pnpm --dir ${rel} ${sc}`,
245
475
  from: `${rel}/package.json#scripts.${sc}`,
476
+ dir: rel,
477
+ script: sc,
478
+ source: 'npm',
479
+ status: 'offer',
480
+ reasons: [],
481
+ body,
246
482
  });
247
- }
248
483
  }
249
484
  const mk = join(root, rel, 'Makefile');
250
485
  if (existsSync(mk)) {
251
- const text = readFileSync(mk, 'utf8');
252
- for (const t of ['check', 'test', 'build'])
253
- if (new RegExp(`^${t}:`, 'm').test(text))
486
+ const t = makeTargets(readFileSync(mk, 'utf8'));
487
+ for (const name of MAKE_TARGETS)
488
+ if (t.has(name))
254
489
  cands.push({
255
- rel,
490
+ name: '',
491
+ cmd: `make -C ${rel} ${name}`,
492
+ from: `${rel}/Makefile:${name}`,
493
+ dir: rel,
494
+ script: name,
256
495
  source: 'make',
257
- script: t,
258
- cmd: `make -C ${rel} ${t}`,
259
- from: `${rel}/Makefile:${t}`,
496
+ status: 'offer',
497
+ reasons: [],
260
498
  });
261
499
  }
262
500
  if (existsSync(join(root, rel, 'go.mod')))
263
501
  for (const t of ['test', 'vet'])
264
502
  cands.push({
265
- rel,
266
- source: 'go',
267
- script: t,
503
+ name: '',
268
504
  cmd: `go -C ${rel} ${t} ./...`,
269
505
  from: `${rel}/go.mod`,
506
+ dir: rel,
507
+ script: t,
508
+ source: 'go',
509
+ status: 'offer',
510
+ reasons: [],
270
511
  });
271
512
  if (depth < MAX_DEPTH)
272
513
  walk(rel, depth + 1);
273
514
  }
274
515
  };
275
516
  walk('', 1);
276
- // Every candidate is kept. Names are settled in a fixed order (path, source, script): the first
277
- // keeps `<dir-slug>-<script>`, a later one takes a source suffix, then -2, -3 … until free.
278
- cands.sort((a, b) => (a.rel < b.rel ? -1 : a.rel > b.rel ? 1 : 0) ||
517
+ // Names are settled in a fixed order (path, source, script): the first keeps `<dir-slug>-<script>`,
518
+ // a later one takes a source suffix, then -2, -3 … until free.
519
+ cands.sort((a, b) => (a.dir < b.dir ? -1 : a.dir > b.dir ? 1 : 0) ||
279
520
  SOURCE_ORDER.indexOf(a.source) - SOURCE_ORDER.indexOf(b.source) ||
280
521
  (a.script < b.script ? -1 : a.script > b.script ? 1 : 0));
281
522
  const taken = new Set(ROOT_GATE_NAMES);
@@ -283,17 +524,62 @@ export function subdirProposals(root) {
283
524
  for (const c of cands) {
284
525
  if (compound(c.cmd))
285
526
  continue;
286
- const base = `${c.rel.replace(/\//g, '-')}-${c.script}`;
527
+ const base = `${c.dir.replace(/\//g, '-')}-${gateName(c.script)}`;
287
528
  let name = base;
288
529
  if (taken.has(name))
289
530
  name = `${base}-${c.source}`;
290
531
  for (let i = 2; taken.has(name); i++)
291
532
  name = `${base}-${c.source}-${i}`;
292
533
  taken.add(name);
293
- out.push({ name, cmd: c.cmd, from: c.from });
534
+ out.push({ ...c, name });
294
535
  }
295
536
  return out;
296
537
  }
538
+ /** The order of the gates install writes: check, lint, typecheck, test, build, e2e (023), the 024 ones beside their kin. */
539
+ const PICK_ORDER = [
540
+ 'check',
541
+ 'lint',
542
+ 'lint-check',
543
+ 'format-check',
544
+ 'typecheck',
545
+ 'type-check',
546
+ 'test',
547
+ 'build',
548
+ 'e2e',
549
+ ];
550
+ const pickRank = (c) => {
551
+ const i = PICK_ORDER.indexOf(c.name);
552
+ return i < 0 ? PICK_ORDER.length : i;
553
+ };
554
+ /**
555
+ * The discovery of installation (design §5, 024 S-10): the candidates chosen by default become `gates`,
556
+ * the excluded ones are listed, and the ones only offered are `proposals`.
557
+ */
558
+ export function discoverGates(root) {
559
+ const candidates = detectCandidates(root);
560
+ return {
561
+ gates: candidates
562
+ .filter((c) => c.status === 'pick')
563
+ .toSorted((a, b) => pickRank(a) - pickRank(b))
564
+ .map((c) => ({
565
+ name: c.name,
566
+ cmd: c.cmd,
567
+ from: c.from,
568
+ ...(c.optional ? { optional: true } : {}),
569
+ })),
570
+ excluded: candidates
571
+ .filter((c) => c.status === 'excluded')
572
+ .map((c) => ({ name: c.script, cmd: c.cmd, why: c.reasons[0] ?? '' })),
573
+ proposals: candidates
574
+ .filter((c) => c.status === 'offer')
575
+ .map((c) => ({ name: c.name, cmd: c.cmd, from: c.from })),
576
+ candidates,
577
+ };
578
+ }
579
+ /** The candidates offered and not chosen by default (`config gates --add`). */
580
+ export function subdirProposals(root) {
581
+ return discoverGates(root).proposals;
582
+ }
297
583
  /** Vendor of a model ID by prefix, in order: gpt, gemini, cursor-, else claude (spec S-2). */
298
584
  export function vendorOf(model) {
299
585
  if (model.startsWith('gpt'))