yadflow 4.5.2 → 4.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/CHANGELOG.md +206 -0
  2. package/README.md +4 -1
  3. package/bin/commands.mjs +5 -1
  4. package/bin/yad.mjs +174 -18
  5. package/cli/artifact-status.mjs +88 -20
  6. package/cli/build.mjs +1372 -0
  7. package/cli/checkpoint.mjs +2 -2
  8. package/cli/commit.mjs +1 -0
  9. package/cli/dial.mjs +21 -1
  10. package/cli/docs.mjs +6 -1
  11. package/cli/doctor.mjs +133 -26
  12. package/cli/epic-state.mjs +396 -123
  13. package/cli/errors.mjs +2 -0
  14. package/cli/find-product.mjs +36 -2
  15. package/cli/fold.mjs +9 -5
  16. package/cli/gate-local.mjs +9 -1
  17. package/cli/gate.mjs +442 -33
  18. package/cli/history.mjs +22 -2
  19. package/cli/hook.mjs +3 -1
  20. package/cli/index-command.mjs +2 -2
  21. package/cli/ledger.mjs +60 -0
  22. package/cli/lib.mjs +12 -1
  23. package/cli/lifecycle/defaults/1.json +96 -0
  24. package/cli/lifecycle/lifecycle.schema.json +121 -0
  25. package/cli/lifecycle.mjs +695 -0
  26. package/cli/next.mjs +34 -11
  27. package/cli/openpr.mjs +81 -5
  28. package/cli/platform.mjs +21 -0
  29. package/cli/pr-list.mjs +49 -3
  30. package/cli/product-index.mjs +6 -3
  31. package/cli/product-token.mjs +186 -0
  32. package/cli/productcommit.mjs +33 -0
  33. package/cli/recommend.mjs +3 -1
  34. package/cli/repo-publish.mjs +2 -2
  35. package/cli/run-checks.mjs +586 -0
  36. package/cli/ship-drift.mjs +384 -0
  37. package/cli/standup.mjs +1 -1
  38. package/cli/status.mjs +282 -0
  39. package/cli/test-build.mjs +1559 -0
  40. package/cli/test-run-checks.mjs +603 -0
  41. package/cli/test-worktree.mjs +520 -0
  42. package/cli/thread.mjs +50 -3
  43. package/cli/tidy.mjs +2 -2
  44. package/cli/toolbox.mjs +36 -5
  45. package/cli/workspace.mjs +4 -1
  46. package/cli/worktree-record.mjs +53 -0
  47. package/cli/worktree.mjs +379 -0
  48. package/package.json +4 -3
  49. package/skills/sdlc/config.yaml +28 -20
  50. package/skills/sdlc/module-help.csv +1 -1
  51. package/skills/yad-backfill/references/backfill.md +8 -1
  52. package/skills/yad-backfill/templates/checks/backfill-check.sh +54 -6
  53. package/skills/yad-change/SKILL.md +3 -0
  54. package/skills/yad-checks/SKILL.md +47 -5
  55. package/skills/yad-checks/references/check-gates.md +124 -12
  56. package/skills/yad-checks/templates/checks/build-test-lint.sh +22 -3
  57. package/skills/yad-checks/templates/checks/commit-message.sh +8 -2
  58. package/skills/yad-checks/templates/checks/contract-check.sh +354 -48
  59. package/skills/yad-checks/templates/checks/epic-open.sh +87 -14
  60. package/skills/yad-checks/templates/checks/ledger-guard.sh +3 -3
  61. package/skills/yad-checks/templates/checks/lineage-check.sh +88 -15
  62. package/skills/yad-checks/templates/checks/member-check.mjs +206 -35
  63. package/skills/yad-checks/templates/checks/package-manager.sh +41 -4
  64. package/skills/yad-checks/templates/checks/product-checkout.sh +161 -13
  65. package/skills/yad-checks/templates/checks/reconcile-debt-check.sh +94 -15
  66. package/skills/yad-checks/templates/checks/risk-map-check.sh +2 -2
  67. package/skills/yad-checks/templates/checks/spec-link.sh +63 -4
  68. package/skills/yad-checks/templates/checks/verified-commits.sh +23 -5
  69. package/skills/yad-checks/templates/github/yad-checks.yml +8 -8
  70. package/skills/yad-checks/templates/gitlab/.gitlab-ci.yml +3 -0
  71. package/skills/yad-checks/templates/gitlab/yad-checks.gitlab-ci.yml +4 -4
  72. package/skills/yad-checks/templates/gitlab/yad-product-checks.gitlab-ci.yml +16 -3
  73. package/skills/yad-checks/templates/gitlab/yad-update-guard.gitlab-ci.yml +5 -1
  74. package/skills/yad-checks/templates/gitlab/yad-verified-commits.gitlab-ci.yml +5 -1
  75. package/skills/yad-commit/SKILL.md +4 -2
  76. package/skills/yad-docs/templates/app/package-lock.json +395 -106
  77. package/skills/yad-docs/templates/app/package.json +2 -2
  78. package/skills/yad-engineer-review/SKILL.md +45 -20
  79. package/skills/yad-engineer-review/references/ship-and-record.md +42 -8
  80. package/skills/yad-epic/references/state-schema.md +83 -20
  81. package/skills/yad-implement/SKILL.md +75 -13
  82. package/skills/yad-implement/references/implement-conventions.md +8 -1
  83. package/skills/yad-implement/templates/.gitmessage +3 -0
  84. package/skills/yad-open-pr/SKILL.md +21 -0
  85. package/skills/yad-pr-template/SKILL.md +6 -2
  86. package/skills/yad-pr-template/templates/checks/pr-template.sh +85 -4
  87. package/skills/yad-pr-template/templates/checks/pr-title.sh +1 -1
  88. package/skills/yad-pr-template/templates/github/pull_request_template.md +1 -1
  89. package/skills/yad-pr-template/templates/gitlab/merge_request_templates/Default.md +1 -1
  90. package/skills/yad-product-bridge/SKILL.md +14 -1
  91. package/skills/yad-product-bridge/references/bridge.md +29 -5
  92. package/skills/yad-product-bridge/templates/gitlab/yad-gate-sync.gitlab-ci.yml +9 -4
  93. package/skills/yad-reconcile/SKILL.md +30 -0
  94. package/skills/yad-review-gate/SKILL.md +8 -2
  95. package/skills/yad-review-gate/references/gating.md +26 -1
  96. package/skills/yad-run/SKILL.md +52 -28
  97. package/skills/yad-run/references/run-loop.md +42 -21
  98. package/skills/yad-spec/SKILL.md +142 -17
  99. package/skills/yad-spec/references/spec-handoff.md +68 -5
  100. package/skills/yad-status/SKILL.md +7 -0
  101. package/skills/yad-stories/references/story-schema.md +1 -1
package/cli/next.mjs CHANGED
@@ -11,15 +11,16 @@
11
11
  // yad next --all every active epic's next action at once
12
12
  // yad next [<epic>] --json the same answer as an action object, for an agent or CI
13
13
  import path from 'node:path';
14
- import { c, log, ok, info, warn, hand, fail, readJSON, exists, emitJSON } from './lib.mjs';
14
+ import { c, log, ok, info, warn, hand, fail, readJSON, exists, emitJSON, forTerminal } from './lib.mjs';
15
15
  import { PROJECT_FILES, isVerifiedLedger, productConfigPath, stepAdvance } from './manifest.mjs';
16
16
  import { printTeamHint, soloTeamHint } from './people.mjs';
17
+ import { productGit, resolveDefaultBranch } from './productcommit.mjs';
17
18
  import { OWNER_STEPS, liveOwner, readState as loadLedgerState } from './owners.mjs';
18
- import { bindingsForEpic, bindingsForProfile, dedupeConsecutive, epicIds, isGateStep, killSwitchOn, loadAutomation, epicRel, epicRoot, loadLedger, loadSkillBindings, stepSkills, nextAction, preconditionsMet, isValidEpicId, epicLineage, typeNoun, phaseOf, stepPhase, profileSteps, lifecycleProfile, PHASES, PRODUCT_DONE, PRODUCT_EPICS, STEPS } from './epic-state.mjs';
19
+ import { bindingsForEpic, doneStepUnapproved, reviewAsItStands, bindingsForProfile, dedupeConsecutive, epicIds, isGateStep, killSwitchOn, loadAutomation, epicRel, epicRoot, loadLedger, loadSkillBindings, stepSkills, nextAction, preconditionsMet, isValidEpicId, epicLineage, typeNoun, phaseOf, stepPhase, profileSteps, lifecycleProfile, PHASES, PRODUCT_DONE, PRODUCT_EPICS, STEPS } from './epic-state.mjs';
19
20
 
20
21
  // Is solo mode on? Persisted in hub.json by setup (Phase C/D); default false. Read defensively so a
21
22
  // missing/old hub.json never breaks the driver.
22
- const soloOf = (productConfig) => !!(productConfig && (productConfig.solo === true || productConfig.review_gate?.solo === true));
23
+ export const soloOf = (productConfig) => !!(productConfig && (productConfig.solo === true || productConfig.review_gate?.solo === true));
23
24
  function isSolo(root) {
24
25
  return soloOf(readJSON(productConfigPath(root), null));
25
26
  }
@@ -27,7 +28,7 @@ function isSolo(root) {
27
28
  const isSetUp = (root) => exists(path.join(root, PROJECT_FILES.version)) || exists(productConfigPath(root));
28
29
 
29
30
  // Every epic that has a state ledger, the Foundation included, in id order.
30
- function listEpics(root) {
31
+ export function listEpics(root) {
31
32
  return epicIds(root).filter((id) => exists(path.join(epicRoot(root, id), '.sdlc', 'state.json')));
32
33
  }
33
34
 
@@ -51,7 +52,7 @@ function listEpics(root) {
51
52
  // the printed line. `nextAction` would narrow them itself, but `actionLine` names skills the action
52
53
  // never carried (the Build fallback, the hand-off to the next epic) — a view built in two places is two
53
54
  // answers for one step.
54
- function epicAction(root, id, lin = epicLineage(root, id), bindings = loadSkillBindings(root)) {
55
+ export function epicAction(root, id, lin = epicLineage(root, id), bindings = loadSkillBindings(root)) {
55
56
  const ledger = loadLedger(epicRoot(root, id));
56
57
  const view = bindingsForEpic(bindings, ledger?.state);
57
58
  const action = {
@@ -82,7 +83,22 @@ const actionFor = (root, id, lin, bindings) => epicAction(root, id, lin, binding
82
83
  const rowFor = (root, id, bindings = loadSkillBindings(root)) => {
83
84
  const lin = epicLineage(root, id);
84
85
  const { action, view } = epicAction(root, id, lin, bindings);
85
- return { action, theme: lin.theme, owners: liveOwners(root, id), bindings: view };
86
+ return { action, theme: lin.theme, owners: liveOwners(root, id), changed: changedSinceApproved(root, id), bindings: view };
87
+ };
88
+
89
+ // The done reviews whose artifact changed after they were approved (#418). Beside the action, like the
90
+ // owners below: the action's `--json` keys are frozen. Said on every run until a fresh review passes.
91
+ const changedSinceApproved = (root, id) => {
92
+ let ledger;
93
+ try { ledger = loadLedger(epicRoot(root, id)); } catch { return []; }
94
+ const productConfig = readJSON(productConfigPath(root), null);
95
+ // Not in solo mode, where no approval is counted and the gate says nothing either.
96
+ if (soloOf(productConfig)) return [];
97
+ const steps = Array.isArray(ledger?.state?.steps) ? ledger.state.steps : [];
98
+ const found = steps.filter((s) => doneStepUnapproved(epicRoot(root, id), s, ledger.approvals));
99
+ if (!found.length) return [];
100
+ const trunk = resolveDefaultBranch(productGit(root), productConfig);
101
+ return found.map((s) => ({ step: s.id, artifact: s.artifact, fix: reviewAsItStands(id, s.artifact, trunk) }));
86
102
  };
87
103
 
88
104
  // The live step owners of one epic (E47), by step. Beside the action, never in it: `yad next --json`'s
@@ -138,7 +154,7 @@ const skillList = (a) => a?.skills || (a?.skill ? [a.skill] : []);
138
154
  // "the yad-architecture skill", or for a bound chain "the shape-it skill, then yad-stories". Closed
139
155
  // decision 7: several skills on one step are a CHAIN, not a panel — they run in the order given, each
140
156
  // seeing what the one before it produced, and the last output is the artifact.
141
- function skillPhrase(a, fallback = null) {
157
+ export function skillPhrase(a, fallback = null) {
142
158
  const ids = skillList(a);
143
159
  if (!ids.length) return `the ${c.bold(fallback || '(no skill)')} skill`;
144
160
  const [first, ...rest] = ids;
@@ -185,7 +201,7 @@ function printBuildLanes(builds, automation = null) {
185
201
  // fallback printed when no story has a build-state yet. Both are the engine naming a step's skill in
186
202
  // prose, so both must name the project's choice — otherwise `yad next` and `yad skill list` disagree
187
203
  // about the same project.
188
- function actionLine(a, { solo, bindings = null, verified = false } = {}) {
204
+ export function actionLine(a, { solo, bindings = null, verified = false } = {}) {
189
205
  const runs = (stepId, fallback) => stepSkills(stepId, bindings)[0] || fallback;
190
206
  switch (a.kind) {
191
207
  case 'new':
@@ -244,7 +260,7 @@ function actionLine(a, { solo, bindings = null, verified = false } = {}) {
244
260
  // `printAction` renders `a` and `--json` emits the SAME `a` verbatim, and that JSON is deep-equalled
245
261
  // by the golden test, which an added key breaks (see actionFor). It comes from `rowFor`, off the same
246
262
  // `epic.md` read the action's own `lineageKind` came from.
247
- function printAction(a, { solo, theme: tag = null, bindings = null, automation = null, verified = false, owners = null } = {}) {
263
+ function printAction(a, { solo, theme: tag = null, bindings = null, automation = null, verified = false, owners = null, changed = [] } = {}) {
248
264
  // Prefix the id with the type noun (Defect / Change request / Hotfix / Chore / Epic) so a glance
249
265
  // says what kind of work this is. The product level is not a feature — leave it un-prefixed.
250
266
  const noun = a.lineageKind && !PRODUCT_EPICS.includes(a.epicId) ? `${typeNoun(a.lineageKind)} ` : '';
@@ -291,6 +307,12 @@ function printAction(a, { solo, theme: tag = null, bindings = null, automation =
291
307
  // DEBT is reminded on every run until it is paid (E41). The line says what is owed and the one command
292
308
  // that starts paying it — or, once that has started, what finishes it.
293
309
  for (const d of a.debt || []) warn(debtLine(a.epicId, d, verified));
310
+ for (const x of changed) { warn(forTerminal(`${x.step} is done, but ${x.artifact} changed after it was approved`)); hand(forTerminal(x.fix)); }
311
+ // Why the chain walked back, when it did (#322). Read from a file a person can edit: plain text only.
312
+ if (a.lastReopen) {
313
+ const r = a.lastReopen;
314
+ info(c.dim(forTerminal(`reopened ${r.step ?? '?'}${r.date ? ` on ${r.date}` : ''}${r.by ? ` by ${r.by}` : ''}${r.reason ? ` — ${r.reason}` : ''} (\`yad gate reopen\`); every gate from it is required again`)));
315
+ }
294
316
  phaseLine(a);
295
317
  }
296
318
 
@@ -424,11 +446,12 @@ function generalNext(root, { all, headCount = null } = {}) {
424
446
  // The grouping theme rides this list too. This is the ONE `yad next` view that shows several epics
425
447
  // side by side, so it is where seeing which of them belong together is worth most — and leaving it
426
448
  // off would have meant bare `yad next`, the command people run by default, never showed the tag.
427
- for (const { action: a, theme: tag, bindings: view } of rows) {
449
+ for (const { action: a, theme: tag, bindings: view, changed } of rows) {
428
450
  const theme = tag ? ` ${c.dim(`#${tag}`)}` : '';
429
451
  // Debt is reminded here too (E41): the roll-up is the view people run most, and a reminder that only
430
452
  // appears once you already look at the one epic is not a reminder.
431
- const owed = a.debt?.length ? ` ${c.yellow(`· ${a.debt.length} owed as debt`)}` : '';
453
+ const owed = (a.debt?.length ? ` ${c.yellow(`· ${a.debt.length} owed as debt`)}` : '')
454
+ + (changed?.length ? ` ${c.yellow(`· ${changed.length} changed since approved`)}` : '');
432
455
  log(` ${c.cyan(`${typeNoun(a.lineageKind)} ${a.epicId}`)}${theme} ${actionLine(a, { solo, bindings: view, verified })}${owed}`);
433
456
  }
434
457
  // Painted per segment, not dim-wrapping a bold: `paint` closes with a full reset, so the nested
package/cli/openpr.mjs CHANGED
@@ -7,7 +7,7 @@ import fs from 'node:fs';
7
7
  import { c, log, ok, info, warn, hand, fail, run, exists, readJSON } from './lib.mjs';
8
8
  import { PROJECT_FILES , productConfigPath, settingsEditHint } from './manifest.mjs';
9
9
  import {
10
- detectPlatform, createPr, platformLogin, resolveBaseBranch,
10
+ detectPlatform, createPr, platformLogin, resolveBaseBranch, prNumberFromUrl,
11
11
  } from './platform.mjs';
12
12
  import { taskFromBranch } from './commit.mjs';
13
13
  import { parseReviewBranch, artifactFromBase, gateCapFor, capLimit, peopleWord, gateRuleSum, gateRuleEnforced, legacyLogins } from './epic-state.mjs';
@@ -18,6 +18,10 @@ import { personLabel } from './riskmap.mjs';
18
18
  import { gateOpen } from './gate.mjs';
19
19
  import { unfoldedPaths, authorStepOf } from './fold.mjs';
20
20
  import { registryEntryFor } from './find-product.mjs';
21
+ import { autoOpenCheck, recordAutoOpen, branchTasks, rangeTasks, groupWarnings } from './build.mjs';
22
+ import { productDriftError } from './epic-state.mjs';
23
+ import { err } from './errors.mjs';
24
+ import { recordActor } from './skip.mjs';
21
25
 
22
26
  // Resolve the target code repo: --repo <name> from the registry, else --dir, else cwd. `product` (E80) is
23
27
  // the Product found from a code repo beside it: the registry is read there, and a repo run without
@@ -91,7 +95,8 @@ export function templateBody(repoRoot, platform, { task, summary, risk, contract
91
95
  }
92
96
  // The Spec line's placeholder is the STORY dir `specs/EP-<slug>-S0N/` (no `-T0N`), so the task
93
97
  // regex below never touches it — derive the story from the task (task minus its `-T0N` suffix).
94
- const story = task ? task.replace(/-T\d+$/i, '') : null;
98
+ // A grouped PR's `task` lists its tasks (one story): the story is the first one's.
99
+ const story = task ? task.split('`')[0].replace(/-T\d+$/i, '') : null;
95
100
  // Fill the obvious fields; leave the rest of the committed template intact for the author.
96
101
  // Function replacers (not string replacements) so a `$` in interpolated free text — a commit-derived
97
102
  // summary or a repo name — is inserted verbatim, never read as a `$1`/`$&` replacement token.
@@ -239,6 +244,8 @@ export function suggestReviewers(repoRoot, baseBranch, platform, { remote } = {}
239
244
 
240
245
  export async function runOpenPr(root, opts = {}) {
241
246
  log(c.bold('\nyad open-pr'));
247
+ const via = opts.via == null ? 'human' : opts.via;
248
+ if (!['auto', 'human'].includes(via)) throw err('YAD-STATE-009', `--via must be auto or human (got '${via}')`, '--via auto opens it on the standing approval (`yad dial open-pr`)');
242
249
  const { repoRoot, meta } = resolveRepo(root, opts);
243
250
  if (!exists(path.join(repoRoot, '.git'))) { fail(`not a git repo: ${repoRoot}`); process.exitCode = 1; return; }
244
251
 
@@ -248,6 +255,8 @@ export async function runOpenPr(root, opts = {}) {
248
255
 
249
256
  const branch = run('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: repoRoot }).stdout;
250
257
  const stage = detectStage(root, repoRoot, branch, meta);
258
+ // Before the Product review hand-off below, which pushes: --via auto never pushes what it refuses.
259
+ if (via === 'auto' && stage !== 'code-repo') throw err('YAD-STATE-009', 'only a task PR/MR in a code repo is opened with --via auto', 'a Product review or tooling PR is opened by a person');
251
260
 
252
261
  // product-shape: this is a Shape artifact-review PR (review/EP-*/<artifact> head on the Product). The
253
262
  // artifact-review title, body, and ledger bookkeeping all live in `yad gate open` — delegate to it
@@ -275,6 +284,32 @@ export async function runOpenPr(root, opts = {}) {
275
284
  return res;
276
285
  }
277
286
 
287
+ // `--via auto` (#356): opened on the project's standing approval, without asking. Everything it needs is
288
+ // checked HERE, before the push, so a refusal leaves no pushed branch behind.
289
+ let auto = null;
290
+ if (via === 'auto') {
291
+ const productRoot = opts.product || (exists(productConfigPath(root)) ? root : null);
292
+ if (!productRoot) throw err('YAD-STATE-009', 'no Product found, so the open-pr dial and the lane cannot be read', 'run it from a repo inside the workspace, or pass --product <folder>');
293
+ if (!meta?.name) throw err('YAD-STATE-009', `${repoRoot} is not a repo the Product registers, so it has no lane`, 'register it with `yad repo add`, or ask a person to open the PR/MR');
294
+ const drift = productDriftError(productRoot);
295
+ if (drift) throw drift;
296
+ // The task the branch carries: of every reading of its name, the ones that are stories of this Product
297
+ // (`branchTasks`). Exactly one: the branch is what is pushed, so a branch carrying two tasks, or none, is
298
+ // refused, and a --task only confirms the one it carries — it never names a task the branch does not.
299
+ const carried = branchTasks(productRoot, branch);
300
+ if (carried.length > 1) throw err('YAD-STATE-009', `${branch} carries more than one task: ${carried.map((x) => `${x.story}-${x.task}`).join(', ')}`, 'open it from a branch that carries one task; a person opens this one');
301
+ const m = opts.task ? String(opts.task).toLowerCase().match(/^ep-([a-z0-9-]+)-s(\d+)-t(\d+)$/) : null;
302
+ if (opts.task && !m) throw err('YAD-STATE-009', `--task must be a task id such as EP-<slug>-S01-T01 (got '${opts.task}')`, '--task EP-<slug>-S<n>-T<n>');
303
+ if (!carried.length) throw err('YAD-STATE-009', `${branch} carries no task of this Product (expected …EP-<slug>-S<n>-T<n>…, a story that exists)`, 'open it from the task\'s branch; a person opens any other PR/MR');
304
+ const pick = carried[0];
305
+ if (m && `EP-${m[1]}-S${m[2]}-T${m[3]}` !== `${pick.story}-${pick.task}`) {
306
+ throw err('YAD-STATE-009', `--task ${opts.task} is not the task ${branch} carries (${pick.story}-${pick.task})`, 'drop --task, or switch to that task\'s branch');
307
+ }
308
+ const { story, task: taskId } = pick;
309
+ // Checked once the base is known, still before the push: a grouped PR's tasks are read from its commits.
310
+ auto = { productRoot, story, task: taskId, repo: meta.name };
311
+ }
312
+
278
313
  // The Product's default_branch, which only applies when the PR targets the Product ITSELF
279
314
  // (a product-tooling branch) — for a connected code repo the Product's trunk belongs to a different repo and
280
315
  // must never leak in. Resolved AFTER the product-shape hand-off above, which delegates its own base to
@@ -303,13 +338,45 @@ export async function runOpenPr(root, opts = {}) {
303
338
  }
304
339
  }
305
340
 
341
+ // The tasks this PR carries (#350): the `Task:` lines of its commits, as spec-link reads them. More than one
342
+ // makes it a grouped PR — several small tasks of one story — which its body lists, and whose size is warned
343
+ // about over the Product's `group_limit`.
344
+ const baseRef = `origin/${baseBranch}`;
345
+ // Fetched first: a stale origin/<base> would count tasks merged since then as this PR's.
346
+ const fetched = run('git', ['fetch', '--quiet', 'origin', `+refs/heads/${baseBranch}:refs/remotes/${baseRef}`], { cwd: repoRoot }).ok;
347
+ if (!fetched && auto) throw err('YAD-STATE-009', `could not fetch ${baseBranch} from origin, so the tasks ${branch} carries are not known`, 'check the network and your login, then run this again');
348
+ if (!fetched) warn(`could not fetch ${baseBranch} from origin — the tasks this PR carries are read against the last fetched ${baseRef}`);
349
+ const haveBase = run('git', ['rev-parse', '--verify', '--quiet', `${baseRef}^{commit}`], { cwd: repoRoot }).ok;
350
+ const commitIds = haveBase ? rangeTasks(repoRoot, `${baseRef}..HEAD`) : null;
351
+ let groupIds = null;
352
+ if (auto) {
353
+ // Every task the PR carries must be the branch's story, and each one's checks must have passed.
354
+ if (!commitIds) throw err('YAD-STATE-009', `could not read the commits of ${branch} against ${baseRef}, so the tasks it carries are not known`, `fetch it (git fetch origin ${baseBranch}), then run this again`);
355
+ const own = `${auto.story}-${auto.task}`;
356
+ const ids = [...new Set([own, ...commitIds])];
357
+ const strangers = ids.filter((t) => t.replace(/-T\d+$/, '') !== auto.story);
358
+ if (strangers.length) throw err('YAD-STATE-009', `${branch} carries tasks of another story: ${strangers.join(', ')}`, 'a grouped PR/MR holds small tasks of ONE story; a person opens this one');
359
+ const tasks = ids.map((t) => t.slice(auto.story.length + 1));
360
+ Object.assign(auto, { tasks, ...autoOpenCheck(auto.productRoot, { story: auto.story, tasks, repo: auto.repo }) });
361
+ if (ids.length > 1) groupIds = ids;
362
+ info(`opened on the standing approval: the checks of ${ids.join(', ')} passed, and the open-pr dial is auto`);
363
+ } else if (commitIds && commitIds.length > 1) groupIds = commitIds;
364
+ if (groupIds) {
365
+ info(`a grouped PR/MR: ${groupIds.join(', ')}`);
366
+ const settings = readJSON(productConfigPath(opts.product || root), {}) || {};
367
+ for (const line of groupWarnings(repoRoot, baseRef, groupIds, settings)) warn(line);
368
+ }
369
+
306
370
  // Push the branch (sets upstream) using the user's own auth. Abort on failure — creating a PR for a
307
371
  // branch that is not on the remote just fails with a more confusing error.
308
372
  info(`pushing ${branch} …`);
309
373
  const push = run('git', ['push', '-u', 'origin', branch], { cwd: repoRoot });
310
374
  if (!push.ok) { fail(`git push failed — ${push.stderr.split('\n')[0] || 'unknown'}`); process.exitCode = 1; return; }
311
375
 
312
- const task = opts.task || taskFromBranch(branch);
376
+ // Under --via auto, the task it was checked for, written the canonical way; a grouped PR's body lists
377
+ // every task it carries.
378
+ const task = auto ? `${auto.story}-${auto.task}` : opts.task || taskFromBranch(branch);
379
+ const bodyTask = groupIds ? groupIds.join('`, `') : task;
313
380
  const subject = run('git', ['log', '-1', '--format=%s'], { cwd: repoRoot }).stdout;
314
381
  const title = opts.title || subject || `task ${task || branch}`;
315
382
  // Summary = commit subject (Conventional-Commits "type:" prefix stripped) + body paragraphs,
@@ -323,7 +390,7 @@ export async function runOpenPr(root, opts = {}) {
323
390
  .trim();
324
391
  const summary = [subjectText, bodyText].filter(Boolean).join('\n\n') || undefined;
325
392
  const body = templateBody(repoRoot, platform, {
326
- task, summary, risk: opts.risk || 'low', contract: !!opts.contractChange, domains: meta?.name, stage,
393
+ task: bodyTask, summary, risk: opts.risk || 'low', contract: !!opts.contractChange, domains: meta?.name, stage,
327
394
  });
328
395
 
329
396
  // Assignee = the committer: `@me` on GitHub (buildPrArgs sends it when no assignee is named, and `gh`
@@ -338,6 +405,15 @@ export async function runOpenPr(root, opts = {}) {
338
405
  const r = creator(platform, { title, body, base: baseBranch, head: branch, assignees, cwd: repoRoot });
339
406
  if (!r.ok) { fail(`could not open PR/MR — ${r.reason || 'unknown'}`); process.exitCode = 1; return; }
340
407
  ok(`opened ${r.url}`);
408
+ if (auto) {
409
+ // The PR is open whatever happens next: a record that cannot be written is said, never a failure.
410
+ try {
411
+ recordAutoOpen(auto.file, { story: auto.story, repo: auto.repo, tasks: auto.tasks, runs: auto.runs, pr: Number(prNumberFromUrl(r.url)) || null, url: r.url, run: auto.run, by: recordActor(auto.productRoot), date: opts.today || new Date().toISOString().slice(0, 10) });
412
+ hand('recorded on the lane; commit it from the Product with `yad checkpoint --push`');
413
+ } catch (e) {
414
+ warn(`the PR/MR is open, but it could not be recorded on the lane: ${e.message}`);
415
+ }
416
+ }
341
417
  hand('no reviewers were requested — ask them on the PR itself');
342
418
  // The Product-wide active count (E71), read ONCE here and handed down: `routeCount` works inside a
343
419
  // code repo and must not go looking for a Product itself.
@@ -366,5 +442,5 @@ export async function runOpenPr(root, opts = {}) {
366
442
  if (stage === 'code-repo') for (const [say, line] of suggestReviewers(repoRoot, baseBranch, platform, { remote }).lines) say(line);
367
443
  // The --json answer (E1). The count and the reviewer suggestion are said in prose, and a warning among
368
444
  // them reaches `warnings`; nothing here is a request — no reviewer is ever asked for (E62).
369
- return { url: r.url, platform, stage, base: baseBranch, baseSource, branch, title, task: task || null };
445
+ return { url: r.url, platform, stage, base: baseBranch, baseSource, branch, title, task: task || null, tasks: groupIds || null, via };
370
446
  }
package/cli/platform.mjs CHANGED
@@ -313,6 +313,27 @@ export function findPrForBranch(platform, branch, { cwd } = {}) {
313
313
  return { ok: true, number: Number(pr.number), url: pr.url || null };
314
314
  }
315
315
 
316
+ // The highest PR/MR number the repo has given out, open or not (`yad gate reopen`, #322): numbers only grow,
317
+ // so every review opened before the reopen is at or below it. `{ ok: true, number }` (null when the repo has
318
+ // none yet), or `{ ok: false, reason }`. Never throws.
319
+ export function newestPrNumber(platform, { cwd } = {}) {
320
+ if (!platformReady(platform)) return { ok: false, reason: `${cliFor(platform) || 'platform CLI'} not available` };
321
+ if (platform === 'gitlab') {
322
+ const r = run('glab', ['api', 'projects/:id/merge_requests?state=all&order_by=created_at&sort=desc&per_page=1'], { cwd });
323
+ if (!r.ok) return { ok: false, reason: r.stderr || 'glab api merge_requests failed' };
324
+ let rows;
325
+ try { rows = JSON.parse(r.stdout); } catch { return { ok: false, reason: 'unreadable glab api response' }; }
326
+ const n = Array.isArray(rows) ? Number(rows[0]?.iid) : NaN;
327
+ return { ok: true, number: Number.isInteger(n) && n > 0 ? n : null };
328
+ }
329
+ const r = run('gh', ['pr', 'list', '--state', 'all', '--limit', '1', '--json', 'number'], { cwd });
330
+ if (!r.ok) return { ok: false, reason: r.stderr || 'gh pr list failed' };
331
+ let rows;
332
+ try { rows = JSON.parse(r.stdout); } catch { return { ok: false, reason: 'unreadable gh pr list response' }; }
333
+ const n = Array.isArray(rows) ? Number(rows[0]?.number) : NaN;
334
+ return { ok: true, number: Number.isInteger(n) && n > 0 ? n : null };
335
+ }
336
+
316
337
  // The head/source branch of a PR/MR, so a caller can confirm a number a human typed actually belongs
317
338
  // to the review it is about to bind approvals to. Returns { ok, branch }; never throws.
318
339
  export function prBranch(platform, n, { cwd } = {}) {
package/cli/pr-list.mjs CHANGED
@@ -19,7 +19,7 @@ import { api, httpStatus, loggedIn, repoPathFromGitUrl, shown, whyFailed, PAGE,
19
19
  const GH_CHECKS = { SUCCESS: 'passing', FAILURE: 'failing', ERROR: 'failing', PENDING: 'running', EXPECTED: 'running' };
20
20
 
21
21
  const GH_QUERY = `query($owner:String!,$name:String!){repository(owner:$owner,name:$name){
22
- open:pullRequests(states:OPEN,first:${PAGE},orderBy:{field:CREATED_AT,direction:DESC}){totalCount nodes{number isDraft author{__typename login} reviewDecision reviews{totalCount} latestOpinionatedReviews(first:100){nodes{state}} commits(last:1){nodes{commit{statusCheckRollup{state}}}}}}
22
+ open:pullRequests(states:OPEN,first:${PAGE},orderBy:{field:CREATED_AT,direction:DESC}){totalCount nodes{number isDraft headRefName author{__typename login} reviewDecision reviews{totalCount} latestOpinionatedReviews(first:100){nodes{state}} commits(last:1){nodes{commit{statusCheckRollup{state}}}}}}
23
23
  merged:pullRequests(states:MERGED,first:${PAGE},orderBy:{field:UPDATED_AT,direction:DESC}){nodes{number mergedAt updatedAt author{__typename login}}}}}`;
24
24
 
25
25
  // GitHub's review state, in the words the standup prints. `none` = no review of any kind yet.
@@ -64,7 +64,8 @@ export function prTarget({ platform = null, gitUrl = '' } = {}, { runner = run,
64
64
  // The PRs/MRs of one repo: `{ ok: true, open: [...], merged: [...], openPartial, mergedPartial }` or
65
65
  // `{ ok: false, kind, why }`. `sinceMs` bounds the merged list (epoch milliseconds).
66
66
  // open: { number, author, draft, checks: passing|failing|running|none|null, review: none|reviewed|approved|changes-requested|null,
67
- // status? (GitLab's own word), waits? (GitLab: a status that holds the merge) }
67
+ // status? (GitLab's own word), waits? (GitLab: a status that holds the merge),
68
+ // branch (the head branch, or null: `yad status` finds the follow-ups by it, #349) }
68
69
  // merged: { number, author, mergedAt (ISO) }
69
70
  export function listPrs(target, { sinceMs, runner = run, env = process.env, authCache = new Map() } = {}) {
70
71
  const t = target?.cli ? target : prTarget(target, { runner, env, authCache });
@@ -91,6 +92,7 @@ function listGitHub(t, sinceMs, runner) {
91
92
  number: n.number, author: author(n.author), draft: n.isDraft === true,
92
93
  checks: GH_CHECKS[n.commits?.nodes?.[0]?.commit?.statusCheckRollup?.state] || (n.commits?.nodes?.[0]?.commit?.statusCheckRollup ? null : 'none'),
93
94
  review: ghReview(n),
95
+ branch: typeof n.headRefName === 'string' ? n.headRefName : null,
94
96
  }));
95
97
  const nodes = repo.merged.nodes;
96
98
  const merged = nodes.filter((n) => Date.parse(n.mergedAt) >= sinceMs).map((n) => ({ number: n.number, author: author(n.author), mergedAt: n.mergedAt }));
@@ -117,9 +119,53 @@ function listGitLab(t, sinceMs, runner) {
117
119
  const open = o.body.map((x) => {
118
120
  const st = typeof x.detailed_merge_status === 'string' ? x.detailed_merge_status : null;
119
121
  const checks = st && Object.hasOwn(GL_CHECKS, st) ? GL_CHECKS[st] : null;
120
- return { number: x.iid, author: who(x.author), draft: x.draft === true, checks, review: null, ...(st && !checks ? { status: st } : {}), waits: !!st && GL_WAITS.includes(st) };
122
+ return { number: x.iid, author: who(x.author), draft: x.draft === true, checks, review: null, ...(st && !checks ? { status: st } : {}), waits: !!st && GL_WAITS.includes(st), branch: typeof x.source_branch === 'string' ? x.source_branch : null };
121
123
  });
122
124
  const merged = m.body.filter((x) => Date.parse(x.merged_at) >= sinceMs).map((x) => ({ number: x.iid, author: who(x.author), mergedAt: x.merged_at }));
123
125
  return { ...t, ok: true, open, merged, openPartial: o.body.length >= PAGE, mergedPartial: m.body.length >= PAGE };
124
126
  }
125
127
 
128
+
129
+ // #365 — the merged PRs/MRs of one repo, newest first, for `yad reconcile check`: which task branches
130
+ // merged. yad's task branches carry the task ID (`feat/EP-x-S01-T01-…`), and a squash merge on GitLab keeps
131
+ // only the MR title — so the branch name is the one place a squashed task still shows. Only the number,
132
+ // the source branch and the merge commit are read, and the caller prints only the task ID it finds in a
133
+ // branch name: never a title, a body, or the rest of a branch name.
134
+ // `base` keeps only those merged INTO it (the repo's default branch): a task merged into a release branch, a
135
+ // stacked parent or `develop` has not reached it (review 1).
136
+ // `{ ok: true, merged: [{ number, branch, commit }], partial }` or `{ ok: false, kind, why }`.
137
+ // What the platform says is printed and pasted (review 2): a number only when it is one, a SHA only when hex.
138
+ const hexSha = (v) => (typeof v === 'string' && /^[0-9a-f]{7,64}$/i.test(v) ? v.toLowerCase() : null);
139
+ // The day it merged, `YYYY-MM-DD` — what a retroactive ship's date is compared with (#365 review 3).
140
+ const dayOf = (v) => (typeof v === 'string' && !Number.isNaN(Date.parse(v)) ? new Date(v).toISOString().slice(0, 10) : null);
141
+ const GH_MERGED_BRANCHES = `query($owner:String!,$name:String!,$base:String){repository(owner:$owner,name:$name){
142
+ pullRequests(states:MERGED,baseRefName:$base,first:${PAGE},orderBy:{field:UPDATED_AT,direction:DESC}){nodes{number headRefName baseRefName mergedAt mergeCommit{oid}}}}}`;
143
+ export function listMergedBranches(target, { base = null, runner = run, env = process.env, authCache = new Map() } = {}) {
144
+ const t = target?.cli ? target : prTarget(target, { runner, env, authCache });
145
+ if (!t.ok) return t;
146
+ if (t.platform === 'github') {
147
+ const parts = t.repo.split('/');
148
+ const fail = (res) => ({ ...t, ok: false, kind: 'other', why: whyFailed(res, { platform: 'github', host: t.host, what: `the merged pull requests of ${shown(t.repo)}`, plural: true }) });
149
+ if (parts.length !== 2) return { ...t, ok: false, kind: 'no-url', why: `${shown(t.repo)} is not an owner/name GitHub repo path` };
150
+ const r = runner('gh', ['api', '--hostname', t.host, 'graphql', '-f', `query=${GH_MERGED_BRANCHES}`, '-f', `owner=${parts[0]}`, '-f', `name=${parts[1]}`, ...(base ? ['-f', `base=${base}`] : [])], { timeout: TIMEOUT });
151
+ if (!r.ok && httpStatus(r) == null && /\bGraphQL:/.test(`${r.stderr || ''}${r.stdout || ''}`)) {
152
+ return { ...t, ok: false, kind: 'other', why: `GitHub answered the pull-request query for ${shown(t.repo)} with an error (the repo may not exist, or your login may not see it)` };
153
+ }
154
+ if (!r.ok) return fail({ ok: false, status: httpStatus(r) });
155
+ let body;
156
+ try { body = JSON.parse(r.stdout); } catch { return fail({ unreadable: true }); }
157
+ const nodes = body?.data?.repository?.pullRequests?.nodes;
158
+ if (!Array.isArray(nodes)) return fail({ unreadable: true });
159
+ const merged = nodes.filter((n) => !base || n.baseRefName === base)
160
+ .map((n) => ({ number: Number.isInteger(n.number) ? n.number : null, branch: typeof n.headRefName === 'string' ? n.headRefName : null, commit: hexSha(n.mergeCommit?.oid), date: dayOf(n.mergedAt) }));
161
+ return { ...t, ok: true, merged, partial: nodes.length >= PAGE };
162
+ }
163
+ const m = api(runner, 'glab', t.host, `projects/${encodeURIComponent(t.repo)}/merge_requests?state=merged${base ? `&target_branch=${encodeURIComponent(base)}` : ''}&order_by=updated_at&sort=desc&per_page=${PAGE}`);
164
+ if (!m.ok) return { ...t, ok: false, kind: 'other', why: whyFailed(m, { platform: 'gitlab', host: t.host, what: `the merged merge requests of ${shown(t.repo)}`, plural: true }) };
165
+ if (!Array.isArray(m.body)) return { ...t, ok: false, kind: 'other', why: whyFailed({ unreadable: true }, { platform: 'gitlab', host: t.host, what: `the merged merge requests of ${shown(t.repo)}`, plural: true }) };
166
+ const merged = m.body.filter((x) => !base || x.target_branch === base).map((x) => ({
167
+ number: Number.isInteger(x.iid) ? x.iid : null, branch: typeof x.source_branch === 'string' ? x.source_branch : null,
168
+ commit: hexSha(x.squash_commit_sha) || hexSha(x.merge_commit_sha), date: dayOf(x.merged_at),
169
+ }));
170
+ return { ...t, ok: true, merged, partial: m.body.length >= PAGE };
171
+ }
@@ -24,7 +24,7 @@ import { createHash } from 'node:crypto';
24
24
  import { spawnSync } from 'node:child_process';
25
25
  import { isPlainObject, writeJSON, info, warn } from './lib.mjs';
26
26
  import { isVerifiedLedger } from './manifest.mjs';
27
- import { productGit, resolveDefaultBranch } from './productcommit.mjs';
27
+ import { productGit, resolveDefaultBranch, branchToCommitOn } from './productcommit.mjs';
28
28
  import {
29
29
  epicIds, epicRel, epicRoot, unlistedLedgerDirs, parseFrontmatter, lineageFrom, stepStatus, STEP_STATES,
30
30
  FOUNDATION_EPIC, FOUNDATION_TITLE, titleOf,
@@ -273,8 +273,11 @@ export function uncommittedIndexInputs(root, commits = null) {
273
273
  export function refreshIndexAfterWrite(root, productConfig, { quiet = false } = {}) {
274
274
  if (isVerifiedLedger(productConfig) || !fs.existsSync(path.join(root, '.git'))) return false;
275
275
  const git = productGit(root);
276
- const branch = git('rev-parse', '--abbrev-ref', 'HEAD').stdout;
277
- if (!branch || branch !== resolveDefaultBranch(git, productConfig)) return false;
276
+ const defaultBranch = resolveDefaultBranch(git, productConfig);
277
+ // An epic worktree counts (#115), judged by the local origin/<default>: the index is a derived file this
278
+ // writes and never pushes — the command that commits it fetches first, and refuses on its own — so no
279
+ // fetch on every write.
280
+ if (branchToCommitOn(root, git, defaultBranch, { fetch: false }) !== defaultBranch) return false;
278
281
  try {
279
282
  const changed = writeIndex(root);
280
283
  if (changed && !quiet) info(`rebuilt ${INDEX_FILE}`);
@@ -0,0 +1,186 @@
1
+ // #345 — where a connected code repo's CI gets `YAD_PRODUCT_TOKEN`, the token `checks/product-checkout.sh`
2
+ // clones the Product with before the four Product-reading gates. Without it those gates only defer, so a
3
+ // token that is missing, or set where the PR pipelines never see it, goes quiet. `yad doctor` asks the
4
+ // platform, with the user's own `gh`/`glab` (the E70 reads in cli/protection.mjs, and the same
5
+ // `YAD_PLATFORM_READ=0` off switch), and reports:
6
+ // - whether the code repo has the secret (GitHub: an Actions secret on the repo, or an organisation
7
+ // secret shared with it; GitLab: a CI/CD variable on the project, or on one of its groups);
8
+ // - on GitLab, a variable that MR pipelines never get: `protected` (only protected branches get it) or
9
+ // limited to one environment;
10
+ // - on GitLab, a token set on a GROUP — every project in that group then gets a token that reads the
11
+ // Product (the user's decision, 2026-10-19: that is the "wrong level"), with the other registered
12
+ // repos in that group named;
13
+ // - on a GitLab Product, its own access tokens that can clone it and expire within 30 days. Which of them
14
+ // is the one in a code repo's variable is never known here, and the line says so.
15
+ // NAMES ONLY. A token's value never reaches this machine from GitHub (its API lists secret names), but
16
+ // GitLab's variables API answers with each variable's value: the answer is cut down to key, `protected`
17
+ // and `environment_scope` at the call, and nothing else of it is kept or printed. The token's own expiry
18
+ // is read where the token is — in CI, by product-checkout.sh.
19
+ import { run } from './lib.mjs';
20
+ import { api, loggedIn, whyFailed, repoPathFromGitUrl, shown, PAGE, PLATFORM_NAME } from './protection.mjs';
21
+ import { cliFor, hostFromGitUrl, detectPlatform, plainHost } from './platform.mjs';
22
+
23
+ export const TOKEN_NAME = 'YAD_PRODUCT_TOKEN';
24
+ export const EXPIRY_WARN_DAYS = 30;
25
+
26
+ const enc = (p) => encodeURIComponent(p);
27
+
28
+ // The parts every platform read shares: which CLI, which host, which repo, and whether it may be asked.
29
+ function target({ platform, gitUrl }, { runner, env, authCache }) {
30
+ const rawHost = hostFromGitUrl(gitUrl || '');
31
+ const host = plainHost(rawHost);
32
+ const repo = repoPathFromGitUrl(gitUrl || '');
33
+ const plat = platform || detectPlatform(gitUrl || '') || null;
34
+ const base = { platform: plat, host, repo };
35
+ const unknown = (kind, why) => ({ ...base, known: false, kind, why });
36
+ const cli = cliFor(plat);
37
+ if (!cli) return { fail: unknown('no-platform', 'no platform yad reads (GitHub or GitLab) is set for it') };
38
+ if (env.YAD_PLATFORM_READ === '0') return { fail: unknown('off', 'platform reads are turned off (YAD_PLATFORM_READ=0)') };
39
+ if (rawHost && !host) return { fail: unknown('no-url', 'its git remote URL names a host that is not a plain host name, so yad does not ask about it') };
40
+ if (!host || !repo) return { fail: unknown('no-url', 'no git remote URL yad can read, so it cannot tell which repo to ask about') };
41
+ if (!runner(process.platform === 'win32' ? 'where' : 'which', [cli], {}).ok) return { fail: unknown('no-cli', `${cli} is not installed, so yad cannot ask ${PLATFORM_NAME[plat]}`) };
42
+ if (!loggedIn(runner, cli, host, authCache)) return { fail: unknown('no-login', `${cli} is not logged in for ${host} (or ${host} did not answer)`) };
43
+ return { base, cli, unknown };
44
+ }
45
+
46
+ // GitLab variables, cut down at the call to what may be kept: never the value.
47
+ const gitlabVars = (body) => (Array.isArray(body) ? body : [])
48
+ .filter((v) => v && v.key === TOKEN_NAME)
49
+ .map((v) => ({ protected: v.protected === true, scope: typeof v.environment_scope === 'string' ? v.environment_scope : '*' }));
50
+
51
+ // Where the code repo's CI gets the token. `{ known: false, why, kind }` when the repo's own list could not
52
+ // be read; otherwise `{ known: true, platform, project, org | groups }`, where each place is
53
+ // GitHub project: { set } org: { set: true|false|null, why }
54
+ // GitLab project: { set, vars: [{ protected, scope }] }
55
+ // groups: [{ path, set: true|false|null, vars, why }] — nearest group first
56
+ export function readTokenPlacement({ platform, gitUrl }, { runner = run, env = process.env, authCache = new Map() } = {}) {
57
+ const t = target({ platform, gitUrl }, { runner, env, authCache });
58
+ if (t.fail) return t.fail;
59
+ const { base, unknown } = t;
60
+ const { host, repo } = base;
61
+ if (base.platform === 'github') {
62
+ const what = `the Actions secrets of ${shown(repo)}`;
63
+ const r = api(runner, 'gh', host, `repos/${repo}/actions/secrets?per_page=${PAGE}`);
64
+ if (!r.ok || !Array.isArray(r.body?.secrets)) return unknown('other', r.ok ? `GitHub answered ${what} with something yad could not read` : whyFailed(r, { platform: 'github', host, what, plural: true }));
65
+ const set = r.body.secrets.some((s) => s?.name === TOKEN_NAME);
66
+ // A full page that does not hold the name proves nothing about the next one.
67
+ if (!set && r.body.secrets.length >= PAGE) return unknown('partial', `${shown(repo)} has more than ${PAGE} Actions secrets, and yad reads one page`);
68
+ if (set) return { ...base, known: true, project: { set: true } };
69
+ const o = api(runner, 'gh', host, `repos/${repo}/actions/organization-secrets?per_page=${PAGE}`);
70
+ const orgRead = o.ok && Array.isArray(o.body?.secrets);
71
+ const orgSet = orgRead ? o.body.secrets.some((s) => s?.name === TOKEN_NAME) : null;
72
+ const orgWhy = orgRead ? (!orgSet && o.body.secrets.length >= PAGE ? `it shares more than ${PAGE}, and yad reads one page` : null)
73
+ : (o.ok ? 'GitHub answered with something yad could not read' : whyFailed(o, { platform: 'github', host, what: 'the organisation secrets shared with it', plural: true }));
74
+ return { ...base, known: true, project: { set: false }, org: { set: orgWhy ? null : orgSet, why: orgWhy } };
75
+ }
76
+ const what = `the CI/CD variables of ${shown(repo)}`;
77
+ const r = api(runner, 'glab', host, `projects/${enc(repo)}/variables?per_page=${PAGE}`);
78
+ if (!r.ok || !Array.isArray(r.body)) return unknown('other', r.ok ? `GitLab answered ${what} with something yad could not read` : whyFailed(r, { platform: 'gitlab', host, what, plural: true }));
79
+ const vars = gitlabVars(r.body);
80
+ if (!vars.length && r.body.length >= PAGE) return unknown('partial', `${shown(repo)} has more than ${PAGE} CI/CD variables, and yad reads one page`);
81
+ // Its groups, nearest first: `a/b/c` is in `a/b`, which is in `a`. A group's variable reaches every
82
+ // project under it. Reading a group's variables needs a maintainer or owner there.
83
+ const parts = repo.split('/');
84
+ const groups = [];
85
+ // A project in a user's own namespace (`someone/app`) has no groups. The project's own answer says whose
86
+ // namespace it is, and anyone who can read its variables can read it — `namespaces/<user>` answers 404
87
+ // to all but that user and an admin (review 2).
88
+ const proj = api(runner, 'glab', host, `projects/${enc(repo)}`);
89
+ const userNs = proj.ok && proj.body?.namespace?.kind === 'user';
90
+ for (let i = userNs ? 0 : parts.length - 1; i >= 1; i--) {
91
+ const g = parts.slice(0, i).join('/');
92
+ const gr = api(runner, 'glab', host, `groups/${enc(g)}/variables?per_page=${PAGE}`);
93
+ if (!gr.ok || !Array.isArray(gr.body)) {
94
+ groups.push({ path: g, set: null, vars: [], why: gr.ok ? 'GitLab answered with something yad could not read' : whyFailed(gr, { platform: 'gitlab', host, what: `the CI/CD variables of the group ${shown(g)}`, plural: true }) });
95
+ continue;
96
+ }
97
+ const gv = gitlabVars(gr.body);
98
+ const full = !gv.length && gr.body.length >= PAGE;
99
+ groups.push({ path: g, set: full ? null : gv.length > 0, vars: gv, why: full ? `it has more than ${PAGE} CI/CD variables, and yad reads one page` : null });
100
+ }
101
+ return { ...base, known: true, project: { set: vars.length > 0, vars }, groups };
102
+ }
103
+
104
+ // What a set of GitLab variables misses: MR pipelines on unprotected branches never get a protected one,
105
+ // and get an environment-scoped one only in a job that names that environment (the gate jobs name none).
106
+ function reachProblem(vars) {
107
+ if (!vars.length) return null;
108
+ if (vars.some((v) => !v.protected && v.scope === '*')) return null;
109
+ if (vars.every((v) => v.protected)) return 'it is a PROTECTED variable, which only pipelines on protected branches get — so no merge request from a feature branch does';
110
+ return `it is limited to the environment ${vars.filter((v) => v.scope !== '*').map((v) => shown(v.scope)).join(', ')}, which the gate jobs do not name — so they never get it`;
111
+ }
112
+ const UNPROTECT = 'Settings → CI/CD → Variables: edit it, untick "Protect variable", keep "Mask variable", and set the environment to All (*)';
113
+
114
+ // One doctor line for one code repo: { status, message, hint }. `others(groupPath)` names the other
115
+ // registered repos in that group.
116
+ export function tokenLine(r, { name, others = () => [] } = {}) {
117
+ const who = name;
118
+ // A warn, as E70's "not known" is: doctor prints ok, warn and fail only (review 1).
119
+ if (!r.known) return { status: 'warn', message: `${who}: whether ${TOKEN_NAME} is set for its CI is not known — ${r.why}` };
120
+ const defer = 'contract-check, lineage-check, epic-open and reconcile-debt-check cannot read the Product there and only defer';
121
+ if (r.platform === 'github') {
122
+ if (r.project.set) return { status: 'ok', message: `${who}: ${TOKEN_NAME} is set as an Actions secret` };
123
+ if (r.org.set === true) return { status: 'ok', message: `${who}: ${TOKEN_NAME} comes from an organisation secret shared with it` };
124
+ const add = `add a token that can read the Product: gh secret set ${TOKEN_NAME} --repo ${shown(r.repo)}`;
125
+ if (r.org.set === null) return { status: 'warn', message: `${who}: ${TOKEN_NAME} is not an Actions secret of the repo, and the organisation secrets shared with it could not be read (${r.org.why}) — unless it is one of those, ${defer} on every PR`, hint: add };
126
+ return { status: 'warn', message: `${who}: ${TOKEN_NAME} is not set for its CI — ${defer} on every PR`, hint: add };
127
+ }
128
+ const groupSet = r.groups.filter((g) => g.set === true);
129
+ const unread = r.groups.filter((g) => g.set === null);
130
+ const groupNote = (g) => {
131
+ const also = others(g.path);
132
+ return `the group ${shown(g.path)} also sets ${TOKEN_NAME}, so every project in it gets a token that can read the Product${also.length ? ` (registered here: ${also.join(', ')})` : ''}`;
133
+ };
134
+ if (r.project.set) {
135
+ const miss = reachProblem(r.project.vars);
136
+ if (miss) return { status: 'warn', message: `${who}: ${TOKEN_NAME} is set on the project, but ${miss} and ${defer} on them`, hint: UNPROTECT };
137
+ if (groupSet.length) return { status: 'warn', message: `${who}: ${TOKEN_NAME} is set on the project, and ${groupNote(groupSet[0])}`, hint: `remove it from the group's CI/CD variables (Settings → CI/CD → Variables of ${shown(groupSet[0].path)}) unless every project there should read the Product` };
138
+ return { status: 'ok', message: `${who}: ${TOKEN_NAME} is set on the project` };
139
+ }
140
+ if (groupSet.length) {
141
+ const g = groupSet[0];
142
+ const miss = reachProblem(g.vars);
143
+ const also = others(g.path);
144
+ return {
145
+ status: 'warn',
146
+ message: `${who}: ${TOKEN_NAME} comes from the group ${shown(g.path)}, so every project in that group gets a token that can read the Product${also.length ? ` (registered here: ${also.join(', ')})` : ''}${miss ? `; and ${miss}` : ''}`,
147
+ hint: `set it on the project instead (Settings → CI/CD → Variables: masked, not protected, all environments), then remove it from the group ${shown(g.path)}`,
148
+ };
149
+ }
150
+ const add = `add a token that can read the Product as the ${TOKEN_NAME} CI/CD variable of ${shown(r.repo)} (Settings → CI/CD → Variables: masked, not protected, all environments)`;
151
+ if (unread.length) {
152
+ return { status: 'warn', message: `${who}: ${TOKEN_NAME} is not set on the project, and ${unread.length === 1 ? `the group ${shown(unread[0].path)} could` : `the groups ${unread.map((g) => shown(g.path)).join(', ')} could`} not be read (${unread[0].why}) — unless it is set there, ${defer} on every merge request`, hint: add };
153
+ }
154
+ return { status: 'warn', message: `${who}: ${TOKEN_NAME} is not set on the project or its groups — ${defer} on every merge request`, hint: add };
155
+ }
156
+
157
+ // A GitLab Product's own access tokens that can clone it and expire within EXPIRY_WARN_DAYS, or expired
158
+ // within the last EXPIRY_WARN_DAYS. `today` is
159
+ // YYYY-MM-DD (injectable for tests). `{ known: false, why }`, or `{ known: true, soon: [{ name, expires,
160
+ // days, scopes }] }`.
161
+ export function readExpiringProductTokens({ gitUrl }, { runner = run, env = process.env, authCache = new Map(), today = new Date().toISOString().slice(0, 10) } = {}) {
162
+ const t = target({ platform: 'gitlab', gitUrl }, { runner, env, authCache });
163
+ if (t.fail) return t.fail;
164
+ const { base, unknown } = t;
165
+ const what = `the access tokens of ${shown(base.repo)}`;
166
+ const r = api(runner, 'glab', base.host, `projects/${enc(base.repo)}/access_tokens?per_page=${PAGE}`);
167
+ if (!r.ok || !Array.isArray(r.body)) return unknown('other', r.ok ? `GitLab answered ${what} with something yad could not read` : whyFailed(r, { platform: 'gitlab', host: base.host, what, plural: true }));
168
+ const day = (d) => Date.parse(`${d}T00:00:00Z`) / 864e5;
169
+ const soon = [];
170
+ for (const tok of r.body) {
171
+ // An EXPIRED token is listed with `active: false`: kept, so doctor says it expired (review 1). Only a
172
+ // revoked one is gone on purpose.
173
+ if (!tok || tok.revoked === true) continue;
174
+ const scopes = Array.isArray(tok.scopes) ? tok.scopes.filter((s) => typeof s === 'string') : [];
175
+ if (!scopes.some((s) => s === 'read_repository' || s === 'api')) continue;
176
+ const exp = typeof tok.expires_at === 'string' && /^\d{4}-\d{2}-\d{2}/.test(tok.expires_at) ? tok.expires_at.slice(0, 10) : null;
177
+ if (!exp) continue;
178
+ const days = Math.round(day(exp) - day(today));
179
+ // Within 30 days either side: one that expired long ago is no news, and GitLab offers no way to remove
180
+ // an expired token from its list, so a line about it could never be cleared (review 3).
181
+ if (days <= EXPIRY_WARN_DAYS && days >= -EXPIRY_WARN_DAYS) soon.push({ id: Number.isInteger(tok.id) ? tok.id : null, name: typeof tok.name === 'string' ? tok.name : `#${tok.id}`, expires: exp, days, scopes });
182
+ }
183
+ // A full page proves nothing about the tokens past it (review 1).
184
+ if (r.body.length >= PAGE) return unknown('partial', `${shown(base.repo)} has more than ${PAGE} access tokens, and yad reads one page`);
185
+ return { ...base, known: true, soon };
186
+ }