yadflow 4.6.0 → 4.8.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 (136) hide show
  1. package/CHANGELOG.md +259 -0
  2. package/README.md +46 -4
  3. package/bin/commands.mjs +9 -2
  4. package/bin/yad.mjs +309 -30
  5. package/cli/agent-guard.mjs +511 -0
  6. package/cli/agent-guide.mjs +83 -0
  7. package/cli/agent-rules.mjs +381 -0
  8. package/cli/argv-flags.mjs +7 -0
  9. package/cli/artifact-status.mjs +88 -20
  10. package/cli/build.mjs +1372 -0
  11. package/cli/checkpoint.mjs +2 -2
  12. package/cli/ci-check.mjs +492 -0
  13. package/cli/commit.mjs +1 -0
  14. package/cli/dial.mjs +21 -1
  15. package/cli/docs.mjs +7 -2
  16. package/cli/doctor.mjs +176 -33
  17. package/cli/epic-state.mjs +360 -12
  18. package/cli/errors.mjs +3 -0
  19. package/cli/find-product.mjs +36 -2
  20. package/cli/fold.mjs +9 -5
  21. package/cli/gate-local.mjs +9 -1
  22. package/cli/gate.mjs +364 -22
  23. package/cli/guide.mjs +549 -0
  24. package/cli/history.mjs +22 -2
  25. package/cli/hook.mjs +35 -4
  26. package/cli/index-command.mjs +2 -2
  27. package/cli/ledger.mjs +60 -0
  28. package/cli/lib.mjs +46 -0
  29. package/cli/lifecycle/defaults/2.json +124 -0
  30. package/cli/lifecycle/lifecycle.schema.json +5 -2
  31. package/cli/lifecycle.mjs +16 -3
  32. package/cli/manifest.mjs +75 -0
  33. package/cli/marked-block.mjs +48 -0
  34. package/cli/members.mjs +19 -5
  35. package/cli/migrate.mjs +17 -19
  36. package/cli/next.mjs +35 -12
  37. package/cli/openpr.mjs +81 -5
  38. package/cli/plan.mjs +326 -22
  39. package/cli/platform.mjs +21 -0
  40. package/cli/pr-list.mjs +5 -3
  41. package/cli/product-index.mjs +6 -3
  42. package/cli/product-token.mjs +186 -0
  43. package/cli/productcommit.mjs +33 -0
  44. package/cli/protection.mjs +50 -0
  45. package/cli/recommend.mjs +3 -1
  46. package/cli/reconcile.mjs +187 -26
  47. package/cli/repo-publish.mjs +2 -2
  48. package/cli/report.mjs +62 -14
  49. package/cli/run-checks.mjs +586 -0
  50. package/cli/setup.mjs +244 -25
  51. package/cli/ship-drift.mjs +49 -18
  52. package/cli/signing.mjs +377 -0
  53. package/cli/standup.mjs +1 -1
  54. package/cli/status.mjs +282 -0
  55. package/cli/test-agents-file.mjs +993 -0
  56. package/cli/test-build.mjs +1559 -0
  57. package/cli/test-ci-check.mjs +616 -0
  58. package/cli/test-guide.mjs +1267 -0
  59. package/cli/test-node.mjs +129 -0
  60. package/cli/test-run-checks.mjs +603 -0
  61. package/cli/test-setup-agent.mjs +352 -0
  62. package/cli/test-signing.mjs +524 -0
  63. package/cli/test-worktree.mjs +520 -0
  64. package/cli/thread.mjs +4 -0
  65. package/cli/tidy.mjs +2 -2
  66. package/cli/toolbox.mjs +37 -6
  67. package/cli/update-commit.mjs +313 -11
  68. package/cli/workspace.mjs +38 -10
  69. package/cli/worktree-record.mjs +53 -0
  70. package/cli/worktree.mjs +379 -0
  71. package/package.json +6 -4
  72. package/skills/sdlc/config.yaml +5 -5
  73. package/skills/sdlc/module-help.csv +2 -1
  74. package/skills/yad-backfill/templates/checks/backfill-check.sh +9 -9
  75. package/skills/yad-change/SKILL.md +5 -1
  76. package/skills/yad-checks/SKILL.md +61 -12
  77. package/skills/yad-checks/references/check-gates.md +78 -14
  78. package/skills/yad-checks/templates/checks/build-test-lint.sh +22 -3
  79. package/skills/yad-checks/templates/checks/commit-message.sh +8 -2
  80. package/skills/yad-checks/templates/checks/contract-check.sh +61 -33
  81. package/skills/yad-checks/templates/checks/epic-open.sh +87 -14
  82. package/skills/yad-checks/templates/checks/ledger-guard.sh +3 -3
  83. package/skills/yad-checks/templates/checks/lineage-check.sh +88 -15
  84. package/skills/yad-checks/templates/checks/member-check.mjs +206 -35
  85. package/skills/yad-checks/templates/checks/package-manager.sh +42 -5
  86. package/skills/yad-checks/templates/checks/product-checkout.sh +161 -13
  87. package/skills/yad-checks/templates/checks/reconcile-debt-check.sh +94 -15
  88. package/skills/yad-checks/templates/checks/risk-map-check.sh +2 -2
  89. package/skills/yad-checks/templates/checks/spec-link.sh +63 -4
  90. package/skills/yad-checks/templates/checks/verified-commits.sh +23 -5
  91. package/skills/yad-checks/templates/github/yad-checks.yml +9 -9
  92. package/skills/yad-checks/templates/gitlab/.gitlab-ci.yml +4 -1
  93. package/skills/yad-checks/templates/gitlab/yad-checks.gitlab-ci.yml +5 -5
  94. package/skills/yad-checks/templates/gitlab/yad-product-checks.gitlab-ci.yml +16 -3
  95. package/skills/yad-checks/templates/gitlab/yad-update-guard.gitlab-ci.yml +5 -1
  96. package/skills/yad-checks/templates/gitlab/yad-verified-commits.gitlab-ci.yml +5 -1
  97. package/skills/yad-checks/templates/hooks/agent-guard.mjs +116 -0
  98. package/skills/yad-commit/SKILL.md +8 -5
  99. package/skills/yad-connect-repos/SKILL.md +5 -3
  100. package/skills/yad-connect-repos/references/code-context.md +2 -1
  101. package/skills/yad-connect-repos/references/repos-registry.md +2 -1
  102. package/skills/yad-docs/templates/app/package-lock.json +395 -106
  103. package/skills/yad-docs/templates/app/package.json +2 -2
  104. package/skills/yad-docs-overview/references/pipeline-model.md +2 -2
  105. package/skills/yad-engineer-review/SKILL.md +55 -25
  106. package/skills/yad-engineer-review/references/ship-and-record.md +49 -12
  107. package/skills/yad-epic/SKILL.md +2 -1
  108. package/skills/yad-epic/references/state-schema.md +76 -17
  109. package/skills/yad-guide/SKILL.md +192 -0
  110. package/skills/yad-implement/SKILL.md +75 -13
  111. package/skills/yad-implement/references/implement-conventions.md +8 -1
  112. package/skills/yad-implement/templates/.gitmessage +3 -0
  113. package/skills/yad-open-pr/SKILL.md +21 -0
  114. package/skills/yad-pair-review/SKILL.md +14 -5
  115. package/skills/yad-pr-template/SKILL.md +6 -2
  116. package/skills/yad-pr-template/templates/checks/pr-template.sh +85 -4
  117. package/skills/yad-pr-template/templates/checks/pr-title.sh +1 -1
  118. package/skills/yad-pr-template/templates/github/pull_request_template.md +1 -1
  119. package/skills/yad-pr-template/templates/gitlab/merge_request_templates/Default.md +1 -1
  120. package/skills/yad-product-bridge/SKILL.md +44 -12
  121. package/skills/yad-product-bridge/references/bridge.md +11 -5
  122. package/skills/yad-product-bridge/templates/github/yad-gate-sync.yml +2 -2
  123. package/skills/yad-product-bridge/templates/gitlab/yad-gate-sync.gitlab-ci.yml +9 -4
  124. package/skills/yad-reconcile/SKILL.md +9 -3
  125. package/skills/yad-report/SKILL.md +15 -0
  126. package/skills/yad-review-companion/SKILL.md +5 -3
  127. package/skills/yad-review-gate/SKILL.md +16 -8
  128. package/skills/yad-review-gate/references/gating.md +26 -1
  129. package/skills/yad-run/SKILL.md +61 -33
  130. package/skills/yad-run/references/run-loop.md +44 -22
  131. package/skills/yad-spec/SKILL.md +137 -16
  132. package/skills/yad-spec/references/spec-handoff.md +50 -3
  133. package/skills/yad-status/SKILL.md +7 -0
  134. package/skills/yad-stories/references/story-schema.md +1 -1
  135. package/skills/yad-sync-repos/SKILL.md +2 -1
  136. package/skills/yad-ui/SKILL.md +2 -2
@@ -0,0 +1,1559 @@
1
+ // `yad build start|step|trust|ship|done` (issue #346): one command per Build record. The spine test drives
2
+ // a story from its first lane to `shipped` through the real CLI, then asks the EXISTING readers — `yad next`,
3
+ // the epic seal, `yad doctor`, `yad tidy` — whether they agree it shipped: the user's decision was that
4
+ // `build done` makes the three "shipped" signals agree.
5
+ import './fixtures/git-quiet.mjs';
6
+ import { test } from 'node:test';
7
+ import assert from 'node:assert/strict';
8
+ import { execFileSync, spawnSync } from 'node:child_process';
9
+ import fs from 'node:fs';
10
+ import os from 'node:os';
11
+ import path from 'node:path';
12
+ import { fileURLToPath } from 'node:url';
13
+
14
+ const ROOT = fileURLToPath(new URL('..', import.meta.url));
15
+ const YAD = path.join(ROOT, 'bin/yad.mjs');
16
+ const ENV = Object.fromEntries(Object.entries(process.env).filter(([k]) =>
17
+ !/^GIT_(AUTHOR|COMMITTER)_/.test(k) && !/^(CI|GITLAB_CI|GITHUB_ACTIONS|SDLC_BASE|SDLC_HUB_CONFIG|SDLC_PRODUCT_CONFIG)$/.test(k) && !/^(CI_|GITHUB_)/.test(k)));
18
+ ENV.GIT_CONFIG_GLOBAL = '/dev/null';
19
+ const git = (cwd, ...a) => execFileSync('git', a, { cwd, stdio: 'pipe', env: ENV }).toString().trim();
20
+ const yad = (cwd, ...args) => {
21
+ const r = spawnSync(process.execPath, [YAD, ...args], { cwd, env: { ...ENV, PWD: cwd, YAD_NO_REPORT: '1', YAD_NO_UPDATE_CHECK: '1' }, encoding: 'utf8' });
22
+ return { code: r.status, out: `${r.stdout}${r.stderr}`, stdout: r.stdout };
23
+ };
24
+ const json = (r) => JSON.parse(r.stdout);
25
+
26
+ function write(dir, files) {
27
+ for (const [rel, content] of Object.entries(files)) {
28
+ const p = path.join(dir, rel);
29
+ fs.mkdirSync(path.dirname(p), { recursive: true });
30
+ fs.writeFileSync(p, content);
31
+ }
32
+ }
33
+ function initRepo(dir) {
34
+ fs.mkdirSync(dir, { recursive: true });
35
+ git(dir, 'init', '-q', '-b', 'main');
36
+ git(dir, 'config', 'user.name', 'alice');
37
+ git(dir, 'config', 'user.email', 'alice@corp.io');
38
+ }
39
+
40
+ // A Product (W/product) with one epic whose stories review passed, one story declaring api and web, and the
41
+ // api repo (W/api) on its origin with the story's tasks.md merged. No platform: ships are recorded by flag.
42
+ function workspace({ tasks = '# Tasks\n\n- [ ] T01 — create the order\n- [ ] T02 — list orders\n', spec = null } = {}) {
43
+ const W = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'yad-build-')));
44
+ fs.writeFileSync(path.join(W, '.yad-workspace.json'), JSON.stringify({ version: 1, product: 'product' }));
45
+ const P = path.join(W, 'product');
46
+ const dir = path.join(P, 'epics/EP-l');
47
+ const st = (id, type, status) => ({ id, type, artifact: id.startsWith('stories') ? 'stories/' : 'epic.md', status, risk_tags: [] });
48
+ write(P, {
49
+ '.sdlc/product.json': JSON.stringify({ name: 'p' }),
50
+ '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api' }, { name: 'web', path: '../web' }] }),
51
+ 'epics/EP-l/epic.md': '---\nid: EP-l\ntype: chore\nstatus: approved\n---\n# EP-l\n',
52
+ 'epics/EP-l/.sdlc/state.json': JSON.stringify({
53
+ epicId: 'EP-l', profile: 'chore', currentStep: 'ready-for-build',
54
+ steps: [st('epic', 'author', 'done'), st('epic-review', 'review+approve', 'done'), st('stories', 'author', 'done'), st('stories-review', 'review+approve', 'done')],
55
+ }),
56
+ 'epics/EP-l/.sdlc/approvals.json': '[]',
57
+ 'epics/EP-l/.sdlc/comments.json': '[]',
58
+ 'epics/EP-l/stories/EP-l-S01.md': '---\nid: EP-l-S01\nepic: EP-l\nstatus: approved\nrepos: [api, web]\n---\n# S01\n',
59
+ });
60
+ initRepo(P);
61
+ git(P, 'add', '-A');
62
+ git(P, 'commit', '-q', '-m', 'chore: seed');
63
+ const api = path.join(W, 'api');
64
+ initRepo(api);
65
+ // `spec` replaces the tasks.md with other spec files (a light spec, #348).
66
+ write(api, { ...(spec || { [`specs/EP-l-S01/tasks.md`]: tasks }), 'a.txt': 'a\n' });
67
+ git(api, 'add', '-A');
68
+ git(api, 'commit', '-q', '-m', 'docs: tasks');
69
+ git(W, 'init', '-q', '--bare', '-b', 'main', 'api.git');
70
+ git(api, 'remote', 'add', 'origin', path.join(W, 'api.git'));
71
+ git(api, 'push', '-q', '-u', 'origin', 'main');
72
+ git(api, 'remote', 'set-head', 'origin', 'main');
73
+ // A merged commit per task, each naming its task with a Task: trailer, on origin/main.
74
+ const sha = {};
75
+ for (const t of ['T01', 'T02']) {
76
+ write(api, { [`src/${t}.js`]: `${t}\n` });
77
+ git(api, 'add', '-A');
78
+ git(api, 'commit', '-q', '-m', `feat: ${t}\n\nTask: EP-l-S01-${t}`);
79
+ sha[t] = git(api, 'rev-parse', 'HEAD');
80
+ }
81
+ git(api, 'push', '-q', 'origin', 'main');
82
+ return { W, P, dir, api, sha };
83
+ }
84
+ const read = (p) => JSON.parse(fs.readFileSync(p, 'utf8'));
85
+ const status = (dir) => (fs.readFileSync(path.join(dir, 'stories/EP-l-S01.md'), 'utf8').match(/^status:\s*(\S+)/m) || [])[1];
86
+
87
+ test('yad build: start → step → trust → ship → done, and every reader agrees the story shipped (#346)', () => {
88
+ const { W, P, dir, sha } = workspace();
89
+ try {
90
+ let r = yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
91
+ assert.equal(r.code, 0, r.out);
92
+ assert.match(r.out, /EP-l-S01 \/ api lane started at spec/);
93
+ const file = path.join(dir, '.sdlc/build-state/EP-l-S01.json');
94
+ const lane = read(file).repos.api;
95
+ assert.deepEqual(lane.steps.map((s) => s.id), ['spec', 'tasks', 'implement', 'checks', 'engineer-review']);
96
+ assert.ok(lane.steps.every((s) => s.status === 'todo' && s.advance === 'human' && s.automation === 'human_approve'));
97
+ assert.equal(lane.steps.find((s) => s.id === 'engineer-review').locked, true);
98
+ // The web lane is set aside: the story needs no change there.
99
+ r = yad(P, 'skip', 'EP-l', 'EP-l-S01', '--repo', 'web', '--reason', 'no UI change');
100
+ assert.equal(r.code, 0, r.out);
101
+
102
+ for (const step of ['spec', 'tasks', 'implement', 'checks']) {
103
+ const signal = { spec: 'human_edited_spec=false', tasks: 'task_rescoped=false', implement: 'scope_overrun=false', checks: 'checks=pass' }[step];
104
+ r = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', step, '--verdict', 'approved-unchanged', '--signal', signal, '--json');
105
+ assert.equal(r.code, 0, r.out);
106
+ const { uid } = json(r);
107
+ assert.match(uid, /^[0-9a-f]{8}$/);
108
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', step, 'done', '--run', uid);
109
+ assert.equal(r.code, 0, r.out);
110
+ }
111
+ const closed = read(file).repos.api.steps.find((s) => s.id === 'checks').closed;
112
+ assert.deepEqual([closed.via, typeof closed.run], ['human', 'string']);
113
+ assert.equal(read(file).repos.api.currentStep, 'engineer-review');
114
+
115
+ r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', sha.T01, '--approver', 'bob');
116
+ assert.equal(r.code, 0, r.out);
117
+ assert.equal(status(dir), 'in-build', 'a first ship puts an approved story in Build');
118
+ // Not every task shipped: refused, naming what is missing.
119
+ r = yad(P, 'build', 'done', 'EP-l-S01');
120
+ assert.equal(r.code, 1, r.out);
121
+ assert.match(r.out, /no ship yet for: api\/T02/);
122
+ r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T02', '--repo', 'api', '--merge-commit', sha.T02);
123
+ assert.equal(r.code, 0, r.out);
124
+ r = yad(P, 'build', 'done', 'EP-l-S01');
125
+ assert.equal(r.code, 0, r.out);
126
+ assert.match(r.out, /EP-l-S01 is shipped — engineer-review closed in 1 lane/);
127
+ assert.match(r.out, /EP-l is now sealed: every story shipped/);
128
+
129
+ // The three signals agree.
130
+ assert.equal(status(dir), 'shipped');
131
+ assert.ok(read(file).repos.api.steps.every((s) => s.status === 'done'));
132
+ const ships = fs.readdirSync(path.join(dir, '.sdlc/build-log')).sort();
133
+ assert.deepEqual(ships, ['EP-l-S01-T01-api.json', 'EP-l-S01-T02-api.json']);
134
+ const ship = read(path.join(dir, '.sdlc/build-log/EP-l-S01-T01-api.json'));
135
+ assert.deepEqual([ship.story, ship.task, ship.repo, ship.mergeCommit, ship.engineer_review[0].approver], ['EP-l-S01', 'T01', 'api', sha.T01, 'bob']);
136
+ assert.equal(fs.readdirSync(path.join(dir, '.sdlc/trust-log')).length, 4);
137
+
138
+ // And every reader says so.
139
+ r = yad(P, 'next', 'EP-l', '--json');
140
+ assert.equal(r.code, 0, r.out);
141
+ const flat = JSON.stringify(json(r));
142
+ assert.match(flat, /Build — every story\/repo lane is shipped or skipped/, flat.slice(0, 600));
143
+ assert.match(flat, /"repo":"api"[^}]*"shipped":true|"shipped":true[^}]*"repo":"api"/, flat.slice(0, 600));
144
+ // What yad checkpoint would commit: the Build ledgers and the story's status line, nothing else.
145
+ r = yad(P, 'checkpoint', '--dry-run', '--json');
146
+ assert.equal(r.code, 0, r.out);
147
+ assert.deepEqual(json(r).files.sort(), [
148
+ 'epics/EP-l/.sdlc/build-log/EP-l-S01-T01-api.json', 'epics/EP-l/.sdlc/build-log/EP-l-S01-T02-api.json',
149
+ 'epics/EP-l/.sdlc/build-state/EP-l-S01.json', ...fs.readdirSync(path.join(dir, '.sdlc/trust-log')).map((n) => `epics/EP-l/.sdlc/trust-log/${n}`),
150
+ 'epics/EP-l/stories/EP-l-S01.md',
151
+ ].sort());
152
+ r = yad(P, 'doctor', '--json');
153
+ const lanesWarn = JSON.stringify(json(r)).match(/lane:[^"]*|step:no-record[^"]*/g) || [];
154
+ assert.deepEqual(lanesWarn, [], 'doctor finds nothing wrong with the lanes');
155
+ r = yad(P, 'tidy', 'up', '--dry-run');
156
+ assert.match(r.out, /EP-l: would fold 4 trust \+ 2 build shard\(s\)/, 'tidy sees a shipped story to fold');
157
+ // Done twice changes nothing.
158
+ r = yad(P, 'build', 'done', 'EP-l-S01');
159
+ assert.equal(r.code, 0, r.out);
160
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
161
+ });
162
+
163
+ test('yad build step: the lane is walked in order; a halt needs a reason; the review step closes with done', () => {
164
+ const { W, P, dir } = workspace();
165
+ try {
166
+ let r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'in_progress');
167
+ assert.equal(r.code, 1, r.out);
168
+ assert.match(r.out, /EP-l-S01 has no lane in api yet/);
169
+ assert.match(r.out, /yad build start EP-l-S01 --repo api/);
170
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
171
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'done');
172
+ assert.equal(r.code, 1, r.out);
173
+ assert.match(r.out, /EP-l-S01 \/ api is at spec, not implement/);
174
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'blocked');
175
+ assert.match(r.out, /a halt needs a reason/);
176
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'blocked', '--reason', 'the contract is unclear');
177
+ assert.equal(r.code, 0, r.out);
178
+ const spec = read(path.join(dir, '.sdlc/build-state/EP-l-S01.json')).repos.api.steps[0];
179
+ assert.deepEqual([spec.status, spec.record.reason], ['blocked', 'the contract is unclear']);
180
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'engineer-review', 'done');
181
+ assert.match(r.out, /engineer-review is closed by `yad build done EP-l-S01`/);
182
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'done', '--run', 'deadbeef');
183
+ assert.match(r.out, /no trust run deadbeef for EP-l-S01 \/ api \/ spec/);
184
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'done');
185
+ assert.equal(r.code, 0, r.out);
186
+ const after = read(path.join(dir, '.sdlc/build-state/EP-l-S01.json')).repos.api;
187
+ assert.equal(after.currentStep, 'tasks');
188
+ assert.equal('record' in after.steps[0], false, 'closing a halted step drops the halt record');
189
+ r = yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'nope');
190
+ assert.match(r.out, /EP-l-S01 does not declare nope/);
191
+ r = yad(P, 'build', 'start', 'EP-l-S9', '--repo', 'api');
192
+ assert.match(r.out, /no story EP-l-S9/);
193
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
194
+ });
195
+
196
+ test('yad build trust: signals are checked per step, and --uid finalizes the verdict', () => {
197
+ const { W, P, dir } = workspace();
198
+ try {
199
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
200
+ let r = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'implement', '--verdict', 'great');
201
+ assert.match(r.out, /'great' is not a verdict/);
202
+ r = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'implement', '--verdict', 'rejected', '--signal', 'checks=pass');
203
+ assert.match(r.out, /'checks' is not a implement signal/);
204
+ r = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'engineer-review', '--verdict', 'rejected');
205
+ assert.match(r.out, /'engineer-review' has no trust entry/);
206
+ r = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'implement', '--verdict', 'approved-unchanged', '--signal', 'human_edited_diff=false', '--ran-by', 'machine', '--json');
207
+ assert.equal(r.code, 0, r.out);
208
+ const { uid } = json(r);
209
+ const shard = path.join(dir, `.sdlc/trust-log/EP-l-S01-api-implement-${uid}.json`);
210
+ const entry = read(shard);
211
+ assert.deepEqual([entry.verdict, entry.ranBy, entry.signals.human_edited_diff, entry.advance, entry.automation], ['approved-unchanged', 'machine', false, 'human', 'human_approve']);
212
+ r = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'implement', '--uid', uid, '--verdict', 'approved-with-edits');
213
+ assert.equal(r.code, 0, r.out);
214
+ assert.equal(read(shard).verdict, 'approved-with-edits');
215
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
216
+ });
217
+
218
+ test('yad build ship: reads the merged PR, refuses an unmerged one or another task\'s branch', async () => {
219
+ const { buildShip } = await import('./build.mjs');
220
+ const { W, P, dir, sha } = workspace();
221
+ try {
222
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
223
+ const view = (over = {}) => ({
224
+ state: 'MERGED', mergedAt: '2026-10-18T10:00:00Z', mergeCommit: { oid: sha.T01 }, headRefName: 'feat/EP-l-S01-T01-orders',
225
+ url: 'https://github.com/o/api/pull/7', body: '## Summary\nx\n\nRisk level: medium\n<!-- yad:trailer -->\n',
226
+ statusCheckRollup: [{ name: 'spec-link', conclusion: 'SUCCESS' }, { name: 'build-test-lint', conclusion: 'SUCCESS' }, { name: 'flaky', conclusion: 'FAILURE' }], ...over,
227
+ });
228
+ let answer = view();
229
+ const runner = (cmd, args) => (cmd === 'gh' && args[0] === 'pr' ? { ok: true, stdout: JSON.stringify(answer), stderr: '' } : { ok: false, stdout: '', stderr: 'x' });
230
+ const prReader = () => ({ ok: true, reviews: [{ login: 'bob', state: 'APPROVED', body: '' }], headOid: null });
231
+ const r = await buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', pr: '7', today: '2026-10-19', runner, prReader });
232
+ assert.equal(r.changed, true);
233
+ const ship = read(path.join(dir, '.sdlc/build-log/EP-l-S01-T01-api.json'));
234
+ assert.deepEqual(
235
+ [ship.branch, ship.pr, ship.mergeCommit, ship.gates, ship.risk, ship.companion.trailer, ship.shippedAt, ship.engineer_review],
236
+ ['feat/EP-l-S01-T01-orders', 'https://github.com/o/api/pull/7', sha.T01, ['build-test-lint', 'spec-link'], 'medium', true, '2026-10-18', [{ approver: 'bob', engagement: 'none' }]],
237
+ );
238
+ answer = view({ state: 'OPEN', headRefName: 'feat/EP-l-S01-T02-x' });
239
+ await assert.rejects(() => buildShip(P, { story: 'EP-l-S01', task: 'T02', repo: 'api', pr: '8', today: '2026-10-19', runner, prReader }), /PR #8 in api is not merged/);
240
+ answer = view({ headRefName: 'feat/EP-l-S01-T03-x' });
241
+ await assert.rejects(() => buildShip(P, { story: 'EP-l-S01', task: 'T02', repo: 'api', pr: '9', today: '2026-10-19', runner, prReader }), /feat\/EP-l-S01-T03-x, which names another task than EP-l-S01-T02/);
242
+ answer = view();
243
+ await assert.rejects(() => buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', pr: '7', today: '2026-10-19', runner, prReader }), /already has a ship in api/);
244
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
245
+ });
246
+
247
+ test('parseTaskIds and planDone: task lines in their usual forms; a retro ship covers its repo; an unreadable repo refuses', async () => {
248
+ const { parseTaskIds, planDone } = await import('./build.mjs');
249
+ assert.deepEqual(parseTaskIds('# Tasks\n- [ ] T01 a\n- [x] **T02** b\n### T03 — c\n4. T04 d\nT05: e\nsee T09 in text\n- T01 again\n'), ['T01', 'T02', 'T03', 'T04', 'T05']);
250
+ const ships = [{ story: 'S', repo: 'api', task: 'T01' }, { story: 'S', repo: 'web', task: 'retro', retroactive: true }];
251
+ assert.deepEqual(planDone({ story: 'S', declared: ['api', 'web'], owed: { api: ['T01'], web: null }, ships }), { missing: [], unreadable: [], shipped: true });
252
+ assert.deepEqual(planDone({ story: 'S', declared: ['api', 'ops'], owed: { api: ['T01', 'T02'], ops: null }, ships }).missing, ['api/T02']);
253
+ assert.deepEqual(planDone({ story: 'S', declared: ['api', 'ops'], owed: { api: ['T01'], ops: null }, ships }).unreadable, ['ops']);
254
+ assert.equal(planDone({ story: 'S', declared: ['api', 'ops'], skipped: ['ops'], owed: { api: ['T01'] }, ships }).shipped, true);
255
+ });
256
+
257
+ test('yad build done: a repo the story owes with no lane is refused, so yad next and the status never disagree', () => {
258
+ const { W, P, dir, sha } = workspace({ tasks: '- T01 one\n' });
259
+ try {
260
+ // Shipped by hand in api, with no lane: done would set shipped while yad next still reads it in Build.
261
+ let r = yad(P, 'skip', 'EP-l', 'EP-l-S01', '--repo', 'web', '--reason', 'no UI change');
262
+ assert.equal(r.code, 0, r.out);
263
+ r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', sha.T01);
264
+ assert.equal(r.code, 0, r.out);
265
+ r = yad(P, 'build', 'done', 'EP-l-S01');
266
+ assert.equal(r.code, 1, r.out);
267
+ assert.match(r.out, /EP-l-S01 has no lane in api, so `yad next` would still read it in Build/);
268
+ assert.match(r.out, /yad checkpoint --retro-ship/);
269
+ assert.notEqual((fs.readFileSync(path.join(dir, 'stories/EP-l-S01.md'), 'utf8').match(/^status:\s*(\S+)/m) || [])[1], 'shipped');
270
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
271
+ });
272
+
273
+ // Drive api's lane through its four author steps (no trust runs: the closing record names none).
274
+ function walk(P) {
275
+ for (const step of ['spec', 'tasks', 'implement', 'checks']) yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', step, 'done');
276
+ }
277
+ const story = (dir) => path.join(dir, 'stories/EP-l-S01.md');
278
+ const setStory = (dir, fm, name = 'EP-l-S01.md') => {
279
+ fs.rmSync(story(dir), { force: true });
280
+ fs.writeFileSync(path.join(dir, 'stories', name), `---\n${fm}\n---\n# S01\n`);
281
+ };
282
+
283
+ test('yad build done: the status line moves from any Shape value, and a value it cannot place is refused before anything is written (#346 review 1)', () => {
284
+ const { W, P, dir, sha } = workspace({ tasks: '- T01 one\n' });
285
+ try {
286
+ // A file named other than its id is refused: yad tidy and the seal read a story by its file name.
287
+ setStory(dir, 'id: EP-l-S01\nepic: EP-l\nstatus: draft\nrepos: [api, web]', 'EP-l-S01-checkout.md');
288
+ let r = yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
289
+ assert.match(r.out, /EP-l-S01 is in stories\/EP-l-S01-checkout\.md, not stories\/EP-l-S01\.md/);
290
+ fs.rmSync(path.join(dir, 'stories/EP-l-S01-checkout.md'));
291
+ // A story nobody ran sync-status on still says draft, here quoted with a comment.
292
+ setStory(dir, 'id: EP-l-S01\nepic: EP-l\nstatus: "draft" # authored\nrepos: [api, web]');
293
+ yad(P, 'skip', 'EP-l', 'EP-l-S01', '--repo', 'web', '--reason', 'no UI change');
294
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
295
+ walk(P);
296
+ r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', sha.T01);
297
+ assert.equal(r.code, 0, r.out);
298
+ r = yad(P, 'build', 'done', 'EP-l-S01');
299
+ assert.equal(r.code, 0, r.out);
300
+ assert.match(fs.readFileSync(story(dir), 'utf8'), /^status: shipped$/m);
301
+ assert.match(r.out, /EP-l is now sealed/);
302
+
303
+ // A value no Build write can move on: refused, the lanes untouched.
304
+ const file = path.join(dir, '.sdlc/build-state/EP-l-S01.json');
305
+ const lanesBefore = fs.readFileSync(file, 'utf8');
306
+ setStory(dir, 'id: EP-l-S01\nepic: EP-l\nstatus: ready-for-build\nrepos: [api, web]');
307
+ const bs = JSON.parse(lanesBefore);
308
+ bs.repos.api.steps.find((s) => s.id === 'engineer-review').status = 'todo';
309
+ fs.writeFileSync(file, JSON.stringify(bs));
310
+ r = yad(P, 'build', 'done', 'EP-l-S01');
311
+ assert.equal(r.code, 1, r.out);
312
+ assert.match(r.out, /has status: ready-for-build, which a Build write cannot move on/);
313
+ assert.equal(JSON.parse(fs.readFileSync(file, 'utf8')).repos.api.steps.find((s) => s.id === 'engineer-review').status, 'todo');
314
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
315
+ });
316
+
317
+ test('yad build done: no repo owed, a stray lane, a blocked review step, a failed fetch — each refused (#346 review 1)', () => {
318
+ const { W, P, dir, api, sha } = workspace({ tasks: '- T01 one\n' });
319
+ try {
320
+ setStory(dir, 'id: EP-l-S01\nepic: EP-l\nstatus: approved\nrepos: []');
321
+ let r = yad(P, 'build', 'done', 'EP-l-S01');
322
+ assert.match(r.out, /owes no lane in any repo/);
323
+ setStory(dir, 'id: EP-l-S01\nepic: EP-l\nstatus: approved\nrepos: [api]');
324
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
325
+ walk(P);
326
+ yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', sha.T01);
327
+ const file = path.join(dir, '.sdlc/build-state/EP-l-S01.json');
328
+ const bs = JSON.parse(fs.readFileSync(file, 'utf8'));
329
+ fs.writeFileSync(file, JSON.stringify({ ...bs, repos: { ...bs.repos, Api: { currentStep: 'spec', steps: [{ id: 'spec', status: 'todo' }] } } }));
330
+ r = yad(P, 'build', 'done', 'EP-l-S01');
331
+ assert.match(r.out, /has a lane the story does not declare: Api/);
332
+ fs.writeFileSync(file, JSON.stringify(bs));
333
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'engineer-review', 'blocked', '--reason', 'waiting on a fix');
334
+ r = yad(P, 'build', 'done', 'EP-l-S01');
335
+ assert.match(r.out, /engineer-review is blocked in api/);
336
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'engineer-review', 'in_progress');
337
+ git(api, 'remote', 'set-url', 'origin', path.join(W, 'gone.git'));
338
+ r = yad(P, 'build', 'done', 'EP-l-S01');
339
+ assert.match(r.out, /could not fetch main in api to read the tasks it owes/);
340
+ r = yad(P, 'build', 'done', 'EP-l-S01', '--no-fetch');
341
+ assert.equal(r.code, 0, r.out);
342
+ const review = JSON.parse(fs.readFileSync(file, 'utf8')).repos.api.steps.find((s) => s.id === 'engineer-review');
343
+ assert.equal(review.status, 'done');
344
+ assert.equal('record' in review, false, 'no halt record left on a closed step');
345
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
346
+ });
347
+
348
+ test('yad build ship: a repo not checked out is never read through the Product; --pr is a number; the stories review must have passed; the branch match ignores case and prefixes (#346 review 1)', async () => {
349
+ const { buildShip } = await import('./build.mjs');
350
+ const { W, P, dir, sha } = workspace();
351
+ try {
352
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../nowhere', platform: 'github' }, { name: 'web', path: '../web' }] }) });
353
+ let r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--pr', '7');
354
+ assert.match(r.out, /api is not checked out here, so its PR cannot be read/);
355
+ r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--pr', 'https://x/pull/7', '--merge-commit', 'a'.repeat(40));
356
+ assert.match(r.out, /--pr must be the PR\/MR number/);
357
+ r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', 'not-a-sha');
358
+ assert.match(r.out, /--merge-commit must be a commit SHA/);
359
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
360
+ const answer = { state: 'MERGED', mergedAt: '2026-10-18T10:00:00Z', mergeCommit: { oid: sha.T01 }, headRefName: 'amn/feat/ep-l-s01-t01-orders', url: 'u', body: '', statusCheckRollup: [] };
361
+ const runner = (cmd, args) => (cmd === 'gh' && args[0] === 'pr' ? { ok: true, stdout: JSON.stringify(answer), stderr: '' } : { ok: false, stdout: '', stderr: 'x' });
362
+ const res = await buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', pr: '7', today: '2026-10-19', runner, prReader: () => ({ ok: true, reviews: [] }) });
363
+ assert.equal(res.changed, true, 'a lower-case branch under a folder still names the task');
364
+ const st = JSON.parse(fs.readFileSync(path.join(dir, '.sdlc/state.json'), 'utf8'));
365
+ st.steps.find((x) => x.id === 'stories-review').status = 'in_review';
366
+ fs.writeFileSync(path.join(dir, '.sdlc/state.json'), JSON.stringify(st));
367
+ r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T02', '--repo', 'api', '--merge-commit', 'a'.repeat(40));
368
+ assert.match(r.out, /the stories review has not passed/);
369
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
370
+ });
371
+
372
+ test('yad build done: a shipped status written another way is rewritten; two status lines or two files are refused; the epic must be in Build (#346 review 2)', () => {
373
+ const { W, P, dir, sha } = workspace({ tasks: '- T01 one\n' });
374
+ try {
375
+ yad(P, 'skip', 'EP-l', 'EP-l-S01', '--repo', 'web', '--reason', 'no UI change');
376
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
377
+ walk(P);
378
+ yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', sha.T01);
379
+ setStory(dir, 'id: EP-l-S01\nepic: EP-l\nstatus: approved\nstatus: approved\nrepos: [api, web]');
380
+ let r = yad(P, 'build', 'done', 'EP-l-S01');
381
+ assert.match(r.out, /which a Build write cannot move on/);
382
+ fs.writeFileSync(path.join(dir, 'stories/EP-l-S01-copy.md'), '---\nid: EP-l-S01\nstatus: approved\n---\n');
383
+ r = yad(P, 'build', 'done', 'EP-l-S01');
384
+ assert.match(r.out, /2 story files claim EP-l-S01/);
385
+ fs.rmSync(path.join(dir, 'stories/EP-l-S01-copy.md'));
386
+ setStory(dir, 'id: EP-l-S01\nepic: EP-l\nstatus: "shipped" # by hand\nrepos: [api, web]');
387
+ r = yad(P, 'build', 'done', 'EP-l-S01');
388
+ assert.equal(r.code, 0, r.out);
389
+ assert.match(fs.readFileSync(story(dir), 'utf8'), /^status: shipped$/m, 'rewritten as the seal and tidy read it');
390
+ assert.match(r.out, /EP-l is now sealed/);
391
+ // A reopened stories review takes the epic out of Build.
392
+ const st = JSON.parse(fs.readFileSync(path.join(dir, '.sdlc/state.json'), 'utf8'));
393
+ st.steps.find((x) => x.id === 'stories-review').status = 'in_review';
394
+ st.currentStep = 'stories-review';
395
+ fs.writeFileSync(path.join(dir, '.sdlc/state.json'), JSON.stringify(st));
396
+ r = yad(P, 'build', 'done', 'EP-l-S01');
397
+ assert.match(r.out, /EP-l is not in Build/);
398
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
399
+ });
400
+
401
+ test('tasksInBranch: the task a branch names, under folders and in any case, never a slug after it (#346 review 3)', async () => {
402
+ const { tasksInBranch } = await import('./build.mjs');
403
+ assert.deepEqual(tasksInBranch('feat/EP-auth-S01-T02-2fa-setup'), ['ep-auth-s01-t02']);
404
+ assert.deepEqual(tasksInBranch('amn/feat/ep-l-s01-t01-orders'), ['ep-l-s01-t01']);
405
+ assert.deepEqual(tasksInBranch('feat/EP-x-S01-T01-then-S02-T03'), ['ep-x-s01-t01']);
406
+ assert.deepEqual(tasksInBranch('feat/EP-l-S01-T012-x'), ['ep-l-s01-t012']);
407
+ assert.deepEqual(tasksInBranch('EP-l-S01-T01'), ['ep-l-s01-t01']);
408
+ assert.deepEqual(tasksInBranch('fix-typo'), []);
409
+ });
410
+
411
+ test('yad build done: a lane with no steps is half-seeded, not shipped; a clone is matched to its registered URL by path, never printed (#346 review 3)', () => {
412
+ const { W, P, dir, api, sha } = workspace({ tasks: '- T01 one\n' });
413
+ try {
414
+ yad(P, 'skip', 'EP-l', 'EP-l-S01', '--repo', 'web', '--reason', 'no UI change');
415
+ yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', sha.T01);
416
+ const file = path.join(dir, '.sdlc/build-state/EP-l-S01.json');
417
+ const bs = JSON.parse(fs.readFileSync(file, 'utf8'));
418
+ fs.writeFileSync(file, JSON.stringify({ ...bs, repos: { ...bs.repos, api: { currentStep: 'spec', steps: [] } } }));
419
+ let r = yad(P, 'build', 'done', 'EP-l-S01');
420
+ assert.match(r.out, /EP-l-S01 has no lane in api/);
421
+ // The registered URL with a port and a token, the clone over another remote form: one repo.
422
+ git(api, 'remote', 'set-url', 'origin', path.join(W, 'api.git'));
423
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', git_url: 'ssh://git@host:2222/team/api.git' }, { name: 'web', path: '../web' }] }) });
424
+ git(api, 'remote', 'set-url', 'origin', 'https://oauth2:SECRET@host/team/api/');
425
+ r = yad(P, 'build', 'done', 'EP-l-S01', '--no-fetch');
426
+ assert.doesNotMatch(r.out, /is a clone of/);
427
+ git(api, 'remote', 'set-url', 'origin', 'https://oauth2:SECRET@host/team/other.git');
428
+ r = yad(P, 'build', 'done', 'EP-l-S01', '--no-fetch');
429
+ assert.match(r.out, /\.\.\/api is a clone of team\/other, not team\/api/);
430
+ assert.doesNotMatch(r.out, /SECRET/);
431
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
432
+ });
433
+
434
+ test('yad build ship: an epic slug that reads like a task still matches its own branch (#346 review 4)', async () => {
435
+ const { tasksInBranch } = await import('./build.mjs');
436
+ assert.deepEqual(tasksInBranch('feat/EP-aws-s3-t2-backup-S01-T01-x'), ['ep-aws-s3-t2'], 'the general scan alone would refuse it');
437
+ // The exact id is matched first, so a branch whose slug after the task reads like another task passes too.
438
+ const { W, P, sha } = workspace();
439
+ try {
440
+ const { buildShip } = await import('./build.mjs');
441
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
442
+ const answer = { state: 'MERGED', mergedAt: '2026-10-18T10:00:00Z', mergeCommit: { oid: sha.T01 }, headRefName: 'feat/EP-l-S01-T01-s3-t2-x', url: 'u', body: '', statusCheckRollup: [] };
443
+ const runner = (cmd, args) => (cmd === 'gh' && args[0] === 'pr' ? { ok: true, stdout: JSON.stringify(answer), stderr: '' } : { ok: false, stdout: '', stderr: 'x' });
444
+ const r = await buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', pr: '7', today: '2026-10-20', runner, prReader: () => ({ ok: true, reviews: [] }) });
445
+ assert.equal(r.changed, true);
446
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
447
+ });
448
+
449
+ test('yad build done: a status write that fails puts the lanes back; another build-state file claiming the story is refused (#346 review 4)', { skip: (process.getuid && process.getuid() === 0) && 'root writes read-only files' }, () => {
450
+ const { W, P, dir, sha } = workspace({ tasks: '- T01 one\n' });
451
+ try {
452
+ yad(P, 'skip', 'EP-l', 'EP-l-S01', '--repo', 'web', '--reason', 'no UI change');
453
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
454
+ walk(P);
455
+ yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', sha.T01);
456
+ const file = path.join(dir, '.sdlc/build-state/EP-l-S01.json');
457
+ fs.writeFileSync(path.join(dir, '.sdlc/build-state/copy.json'), JSON.stringify({ story: 'EP-l-S01', repos: {} }));
458
+ let r = yad(P, 'build', 'done', 'EP-l-S01');
459
+ assert.match(r.out, /build-state\/copy\.json also says story: EP-l-S01/);
460
+ fs.rmSync(path.join(dir, '.sdlc/build-state/copy.json'));
461
+ const before = fs.readFileSync(file, 'utf8');
462
+ fs.chmodSync(story(dir), 0o444);
463
+ try {
464
+ r = yad(P, 'build', 'done', 'EP-l-S01');
465
+ assert.notEqual(r.code, 0, r.out);
466
+ assert.equal(fs.readFileSync(file, 'utf8'), before, 'the lanes are exactly as they were');
467
+ } finally { fs.chmodSync(story(dir), 0o644); }
468
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
469
+ });
470
+
471
+ test('yad build step --task: the next task reopens implement and checks once the lane is at the review step; each close is kept (#346)', () => {
472
+ const { W, P, dir } = workspace();
473
+ try {
474
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
475
+ for (const step of ['spec', 'tasks']) yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', step, 'done');
476
+ let r = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'implement', '--verdict', 'approved-unchanged', '--task', 'T01', '--json');
477
+ const t1 = json(r).uid;
478
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'done', '--run', t1, '--task', 'T01');
479
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'in_progress', '--task', 'T02');
480
+ assert.match(r.out, /once the lane is at engineer-review/, 'not before checks is done for T01');
481
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'checks', 'done', '--task', 'T01');
482
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'in_progress');
483
+ assert.match(r.out, /implement is already done/, 'no --task, no reopen');
484
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'blocked', '--task', 'T02', '--reason', 'scope overrun: b.js');
485
+ assert.equal(r.code, 0, r.out);
486
+ assert.match(r.out, /implement is blocked again, for T02/);
487
+ const file = path.join(dir, '.sdlc/build-state/EP-l-S01.json');
488
+ let lane = read(file).repos.api;
489
+ const impl = lane.steps.find((s) => s.id === 'implement');
490
+ assert.deepEqual([lane.currentStep, impl.status, impl.record.reason, impl.runs[0].task, impl.runs[0].run], ['implement', 'blocked', 'scope overrun: b.js', 'T01', t1]);
491
+ assert.equal(lane.steps.find((s) => s.id === 'checks').status, 'todo', 'checks is owed again for T02');
492
+ r = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'implement', '--verdict', 'approved-with-edits', '--task', 'T02', '--json');
493
+ const t2 = json(r).uid;
494
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'done', '--run', t2, '--task', 'T02');
495
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'checks', 'done', '--task', 'T02');
496
+ lane = read(file).repos.api;
497
+ assert.equal(lane.currentStep, 'engineer-review');
498
+ assert.deepEqual(lane.steps.find((s) => s.id === 'implement').closed, { by: 'alice', date: lane.steps.find((s) => s.id === 'implement').closed.date, via: 'human', run: t2, task: 'T02' });
499
+ assert.equal(read(path.join(dir, `.sdlc/trust-log/EP-l-S01-api-implement-${t2}.json`)).task, 'T02');
500
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
501
+ });
502
+
503
+ test('yad build ship: a task merged with no PR is recorded from its commit on the default branch (#346)', async () => {
504
+ const { buildShip } = await import('./build.mjs');
505
+ const { W, P, dir, api } = workspace();
506
+ try {
507
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
508
+ write(api, { 'src/x.js': 'x\n' });
509
+ git(api, 'add', '-A');
510
+ git(api, 'commit', '-q', '-m', 'feat: x\n\nTask: EP-l-S01-T01');
511
+ const sha = git(api, 'rev-parse', 'HEAD');
512
+ const runner = (cmd, args) => (cmd === 'gh' && args[0] === 'api' ? { ok: true, stdout: '[]', stderr: '' } : { ok: false, stdout: '', stderr: 'no PR is read' });
513
+ // Not pushed yet: not on the default branch.
514
+ await assert.rejects(() => buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', mergeCommit: sha, today: '2026-10-20', runner }), /is not on api's main/);
515
+ git(api, 'push', '-q', 'origin', 'main');
516
+ await assert.rejects(() => buildShip(P, { story: 'EP-l-S01', task: 'T02', repo: 'api', mergeCommit: sha, today: '2026-10-20', runner }), /names EP-l-S01-T01, not EP-l-S01-T02/);
517
+ const r = await buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', mergeCommit: sha.slice(0, 12), today: '2026-10-20', runner });
518
+ assert.equal(r.changed, true);
519
+ const ship = read(path.join(dir, '.sdlc/build-log/EP-l-S01-T01-api.json'));
520
+ assert.deepEqual([ship.pr, ship.mergeCommit, ship.engineer_review], ['none', sha, []]);
521
+ // Hand-given approvers are still refused where the platform can be read.
522
+ await assert.rejects(() => buildShip(P, { story: 'EP-l-S01', task: 'T02', repo: 'api', mergeCommit: sha, approvers: ['bob'], today: '2026-10-20', runner }), /the ship is read from PR/);
523
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
524
+ });
525
+
526
+ test('yad build ship with no PR, where the platform can be read: the commit names the task, and the platform says no PR merged it (#346 review 7)', async () => {
527
+ const { buildShip } = await import('./build.mjs');
528
+ const { W, P, dir, api } = workspace();
529
+ try {
530
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
531
+ const commit = (msg) => {
532
+ write(api, { [`src/${Math.random().toString(36).slice(2)}.js`]: 'x\n' });
533
+ git(api, 'add', '-A');
534
+ git(api, 'commit', '-q', '-m', msg);
535
+ git(api, 'push', '-q', 'origin', 'main');
536
+ return git(api, 'rev-parse', 'HEAD');
537
+ };
538
+ let pulls = '[]';
539
+ let fails = false;
540
+ const runner = (cmd, args) => (cmd === 'gh' && args[0] === 'api' && /\/pulls$/.test(args[1])
541
+ ? (fails ? { ok: false, stdout: '', stderr: 'HTTP 401' } : { ok: true, stdout: pulls, stderr: '' })
542
+ : { ok: false, stdout: '', stderr: 'x' });
543
+ const ship = (task, sha) => buildShip(P, { story: 'EP-l-S01', task, repo: 'api', mergeCommit: sha, today: '2026-10-20', runner });
544
+ await assert.rejects(() => ship('T1', commit('feat: ten\n\nTask: EP-l-S01-T10')), /names EP-l-S01-T10, not EP-l-S01-T1/);
545
+ await assert.rejects(() => ship('T03', commit('Revert "feat: three EP-l-S01-T03"')), /does not name EP-l-S01-T03/);
546
+ await assert.rejects(() => ship('T03', commit('docs: notes\n\nfollow-up to EP-l-S01-T03')), /does not name EP-l-S01-T03/);
547
+ // The platform says a PR merged it: refused, naming that PR so its approvers are kept.
548
+ const squashed = commit('feat: EP-l-S01-T03 orders (#5)');
549
+ pulls = JSON.stringify([{ number: 5, merged_at: '2026-10-19T10:00:00Z' }]);
550
+ await assert.rejects(() => ship('T03', squashed), /merged by PR\/MR #5/);
551
+ // The platform cannot answer: refused, never a guess.
552
+ fails = true;
553
+ await assert.rejects(() => ship('T03', squashed), /could not ask github whether a PR\/MR merged/);
554
+ // "(#346)" cites an issue and the platform knows no PR: recorded with no PR.
555
+ fails = false;
556
+ pulls = '[]';
557
+ const r = await ship('T04', commit('feat: EP-l-S01-T04 orders (#346)'));
558
+ assert.equal(r.changed, true);
559
+ assert.equal(read(path.join(dir, '.sdlc/build-log/EP-l-S01-T04-api.json')).pr, 'none');
560
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
561
+ });
562
+
563
+ test('yad build ship by hand, where the platform cannot be read: the person vouches, but not for another task or another PR (#346 review 7)', () => {
564
+ const { W, P, api } = workspace();
565
+ try {
566
+ const commit = (msg) => {
567
+ write(api, { [`src/${Math.random().toString(36).slice(2)}.js`]: 'x\n' });
568
+ git(api, 'add', '-A');
569
+ git(api, 'commit', '-q', '-m', msg);
570
+ git(api, 'push', '-q', 'origin', 'main');
571
+ return git(api, 'rev-parse', 'HEAD');
572
+ };
573
+ let r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T03', '--repo', 'api', '--merge-commit', commit('feat: ten\n\nTask: EP-l-S01-T10'));
574
+ assert.match(r.out, /names EP-l-S01-T10, not EP-l-S01-T03/);
575
+ r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T03', '--repo', 'api', '--pr', '7', '--merge-commit', commit('Merge pull request #9 from org/feat/x'), '--approver', 'bob');
576
+ assert.match(r.out, /merged PR\/MR #9, not #7/);
577
+ r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T03', '--repo', 'api', '--pr', '5', '--merge-commit', commit('feat: list orders (#5)'), '--approver', 'bob');
578
+ assert.equal(r.code, 0, r.out);
579
+ assert.match(r.out, /names no task — recorded as you say it ships EP-l-S01-T03/);
580
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
581
+ });
582
+
583
+ test('yad build step: done --task for another task is refused; only implement reopens; closes without --run keep their history (#346 review 5)', () => {
584
+ const { W, P, dir } = workspace();
585
+ try {
586
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
587
+ for (const step of ['spec', 'tasks']) yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', step, 'done');
588
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'done', '--task', 'T01');
589
+ let r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'done', '--task', 'T02');
590
+ assert.match(r.out, /implement was closed for T01, not T02/);
591
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'checks', 'done');
592
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'checks', 'in_progress', '--task', 'T02');
593
+ assert.match(r.out, /the next task reopens implement once the lane is at engineer-review/);
594
+ // Walked by hand, with no --run: every task's close is kept.
595
+ for (const t of ['T02', 'T03']) {
596
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'in_progress', '--task', t);
597
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'done');
598
+ assert.equal(r.code, 0, r.out);
599
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'checks', 'done');
600
+ }
601
+ const impl = read(path.join(dir, '.sdlc/build-state/EP-l-S01.json')).repos.api.steps.find((s) => s.id === 'implement');
602
+ assert.deepEqual([impl.closed.task, ...impl.runs.map((x) => x.task)], ['T03', 'T01', 'T02'], 'the task defaults to the one the step was reopened for');
603
+ // A close with no task is not "already done" for T04, and a run already used for a close is not reused.
604
+ r = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'implement', '--verdict', 'approved-unchanged', '--json');
605
+ const used = json(r).uid;
606
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'in_progress', '--task', 'T04');
607
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'done', '--run', used);
608
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'done', '--task', 'T09');
609
+ assert.match(r.out, /implement was closed for T04, not T09/);
610
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'checks', 'done');
611
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'in_progress', '--task', 'T05');
612
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'done', '--run', used);
613
+ assert.match(r.out, new RegExp(`trust run ${used} already closed implement`));
614
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
615
+ });
616
+
617
+ test('prOfCommit: certain for a GitHub or GitLab merge, only a maybe for a squash (#346 review 6)', async () => {
618
+ const { prOfCommit } = await import('./ship-drift.mjs');
619
+ assert.deepEqual(prOfCommit('Merge pull request #42 from org/feat/x'), { pr: 42, certain: true });
620
+ assert.deepEqual(prOfCommit("Merge branch 'feat/x' into 'main'", 'See merge request team/api!12'), { pr: 12, certain: true });
621
+ assert.deepEqual(prOfCommit('feat: orders (#17)'), { pr: 17, certain: false });
622
+ assert.deepEqual(prOfCommit('feat: orders #17 later'), { pr: null, certain: false });
623
+ });
624
+
625
+ test('yad build step: the same close sent again is "already done", and no second trust run is asked for (#346 review 7)', () => {
626
+ const { W, P, dir } = workspace();
627
+ try {
628
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
629
+ const r0 = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'spec', '--verdict', 'approved-unchanged', '--json');
630
+ const uid = json(r0).uid;
631
+ let r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'done', '--run', uid);
632
+ assert.equal(r.code, 0, r.out);
633
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'done', '--run', uid);
634
+ assert.equal(r.code, 0, r.out);
635
+ assert.match(r.out, /spec is already done in EP-l-S01 \/ api — nothing changed/);
636
+ assert.equal(fs.readdirSync(path.join(dir, '.sdlc/trust-log')).length, 1);
637
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'done', '--task', 'T01');
638
+ assert.match(r.out, /--task does not apply to spec/);
639
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
640
+ });
641
+
642
+ test('yad build: the PR path checks what merged; a by-hand ship keeps the commit date; --via auto obeys the kill switch and a rejected run (#346 final review)', async () => {
643
+ const { buildShip } = await import('./build.mjs');
644
+ const { W, P, api, sha } = workspace();
645
+ try {
646
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
647
+ // Merged into another branch: not on main.
648
+ git(api, 'checkout', '-q', '-b', 'release/1.x');
649
+ write(api, { 'src/rel.js': 'x\n' });
650
+ git(api, 'add', '-A');
651
+ git(api, 'commit', '-q', '-m', 'feat: release fix\n\nTask: EP-l-S01-T01');
652
+ const off = git(api, 'rev-parse', 'HEAD');
653
+ git(api, 'push', '-q', 'origin', 'release/1.x');
654
+ git(api, 'checkout', '-q', 'main');
655
+ let mergeOid = off;
656
+ let branch = 'feat/EP-l-S01-T01-x';
657
+ const runner = (cmd, args) => (cmd === 'gh' && args[0] === 'pr'
658
+ ? { ok: true, stdout: JSON.stringify({ state: 'MERGED', mergedAt: '2026-10-18T10:00:00Z', mergeCommit: { oid: mergeOid }, headRefName: branch, url: 'u', body: '', statusCheckRollup: [] }), stderr: '' }
659
+ : { ok: false, stdout: '', stderr: 'x' });
660
+ const prReader = () => ({ ok: true, reviews: [] });
661
+ await assert.rejects(() => buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', pr: '7', today: '2026-10-20', runner, prReader }), /is not on api's main/);
662
+ // A branch naming no task, a merge commit naming another: refused.
663
+ mergeOid = sha.T02;
664
+ branch = 'hotfix-login';
665
+ await assert.rejects(() => buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', pr: '8', today: '2026-10-20', runner, prReader }), /names EP-l-S01-T02, not EP-l-S01-T01/);
666
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
667
+ });
668
+
669
+ test('yad build: a by-hand ship records the commit date; --via auto needs the dial in force and a run not rejected (#346 final review)', () => {
670
+ const { W, P, dir, api, sha } = workspace();
671
+ try {
672
+ const when = git(api, 'log', '-1', '--format=%cs', sha.T01);
673
+ let r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', sha.T01, '--approver', 'bob');
674
+ assert.equal(r.code, 0, r.out);
675
+ assert.equal(read(path.join(dir, '.sdlc/build-log/EP-l-S01-T01-api.json')).shippedAt, when);
676
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
677
+ r = yad(P, 'dial', 'EP-l', 'EP-l-S01', '--repo', 'api', 'spec', '--to', 'auto');
678
+ assert.equal(r.code, 0, r.out);
679
+ r = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'spec', '--verdict', 'rejected', '--json');
680
+ const bad = json(r).uid;
681
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'done', '--via', 'auto');
682
+ assert.match(r.out, /--via auto needs --run <uid>/);
683
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'done', '--via', 'auto', '--run', bad);
684
+ assert.match(r.out, /is rejected, so spec does not advance on its own/);
685
+ r = yad(P, 'kill', '--reason', 'a bad week');
686
+ assert.equal(r.code, 0, r.out);
687
+ r = yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'spec', '--verdict', 'approved-unchanged', '--json');
688
+ const good = json(r).uid;
689
+ assert.equal(read(path.join(dir, `.sdlc/trust-log/EP-l-S01-api-spec-${good}.json`)).advance, 'human', 'the dial in force under the kill switch');
690
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'done', '--via', 'auto', '--run', good);
691
+ assert.match(r.out, /the kill switch is on/);
692
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
693
+ });
694
+
695
+ test('yad build merged: asks the platform about the task\'s PR, found by its branch; --ship records it once (#355)', async () => {
696
+ const { buildMerged } = await import('./build.mjs');
697
+ const { W, P, dir, sha } = workspace();
698
+ try {
699
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
700
+ let list = [];
701
+ const views = {};
702
+ const asked = [];
703
+ const runner = (cmd, args) => {
704
+ asked.push(args.slice(0, 2).join(' '));
705
+ if (cmd === 'gh' && args[0] === 'pr' && args[1] === 'list') return { ok: true, stdout: JSON.stringify(list), stderr: '' };
706
+ if (cmd === 'gh' && args[0] === 'pr' && args[1] === 'view' && views[args[2]]) return { ok: true, stdout: JSON.stringify(views[args[2]]), stderr: '' };
707
+ return { ok: false, stdout: '', stderr: 'x' };
708
+ };
709
+ const prReader = () => ({ ok: true, reviews: [{ login: 'bob', state: 'APPROVED', body: '' }], headOid: null });
710
+ const ask = (over = {}) => buildMerged(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', today: '2026-10-20', runner, prReader, ...over });
711
+ const view = (state, branch = 'feat/EP-l-S01-T01-orders', oid = sha.T01) => ({
712
+ state, mergedAt: state === 'MERGED' ? '2026-10-19T10:00:00Z' : null, mergeCommit: state === 'MERGED' ? { oid } : null,
713
+ headRefName: branch, url: 'https://github.com/o/api/pull/7', body: '', statusCheckRollup: [],
714
+ });
715
+ // No PR carries the task yet. Another task's PR and a branch with the id inside a longer one do not count.
716
+ list = [{ number: 3, state: 'OPEN', headRefName: 'feat/EP-l-S01-T02-x', url: 'u3' }, { number: 4, state: 'OPEN', headRefName: 'feat/EP-l-S01-T012-x', url: 'u4' }];
717
+ assert.equal((await ask()).state, 'none');
718
+ // An open PR: not merged yet, and nothing written.
719
+ list.push({ number: 7, state: 'OPEN', headRefName: 'feat/EP-l-S01-T01-orders', url: 'u7' });
720
+ views['7'] = view('OPEN');
721
+ let r = await ask({ ship: true });
722
+ assert.deepEqual([r.state, r.pr, r.changed], ['open', 7, false]);
723
+ assert.equal(fs.existsSync(path.join(dir, '.sdlc/build-log/EP-l-S01-T01-api.json')), false);
724
+ // Two live PRs for the task: refused, naming both. A closed one alongside a live one does not count.
725
+ list.push({ number: 8, state: 'OPEN', headRefName: 'fix/ep-l-s01-t01', url: 'u8' });
726
+ await assert.rejects(() => ask(), /more than one PR\/MR carries EP-l-S01-T01: #7 \(open\), #8 \(open\)/);
727
+ list[list.length - 1].state = 'CLOSED';
728
+ // Merged: the merge commit is named; without --ship nothing is written.
729
+ views['7'] = view('MERGED');
730
+ r = await ask();
731
+ assert.deepEqual([r.state, r.pr, r.mergeCommit, r.changed], ['merged', 7, sha.T01, false]);
732
+ assert.equal(fs.existsSync(path.join(dir, '.sdlc/build-log/EP-l-S01-T01-api.json')), false);
733
+ // --ship records it through `build ship`, with the PR's approvers.
734
+ r = await ask({ ship: true });
735
+ assert.deepEqual([r.state, r.changed, r.ship.engineer_review], ['merged', true, [{ approver: 'bob', engagement: 'none' }]]);
736
+ assert.equal(read(path.join(dir, '.sdlc/build-log/EP-l-S01-T01-api.json')).mergeCommit, sha.T01);
737
+ assert.equal(status(dir), 'in-build');
738
+ // Asked again: already shipped, the same answer with or without --ship, and the platform is not asked.
739
+ asked.length = 0;
740
+ r = await ask({ ship: true });
741
+ assert.deepEqual([r.state, r.changed, r.mergeCommit], ['shipped', false, sha.T01]);
742
+ assert.deepEqual(asked, []);
743
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
744
+ });
745
+
746
+ test('yad build merged: closed, a cut-short list, a --pr for another task, and no platform to ask (#355)', async () => {
747
+ const { buildMerged, findTaskPrs } = await import('./build.mjs');
748
+ const { W, P } = workspace();
749
+ try {
750
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'gitlab' }, { name: 'web', path: '../web' }] }) });
751
+ let mrs = [];
752
+ const mr = {};
753
+ const runner = (cmd, args) => {
754
+ if (cmd !== 'glab') return { ok: false, stdout: '', stderr: 'x' };
755
+ if (args[1].startsWith('projects/:id/merge_requests?')) return { ok: true, stdout: JSON.stringify(mrs), stderr: '' };
756
+ const n = (args[1].match(/merge_requests\/(\d+)$/) || [])[1];
757
+ return n && mr[n] ? { ok: true, stdout: JSON.stringify(mr[n]), stderr: '' } : { ok: false, stdout: '', stderr: 'x' };
758
+ };
759
+ const ask = (over = {}) => buildMerged(P, { story: 'EP-l-S01', task: 'T02', repo: 'api', today: '2026-10-20', runner, ...over });
760
+ // Only a closed MR: closed, naming it.
761
+ mrs = [{ iid: 5, state: 'closed', source_branch: 'feat/EP-l-S01-T02-y', web_url: 'w5' }];
762
+ assert.deepEqual([(await ask()).state, (await ask()).pr], ['closed', 5]);
763
+ // A locked MR is mid-merge: still open.
764
+ mr['6'] = { iid: 6, state: 'locked', source_branch: 'feat/EP-l-S01-T02-y', web_url: 'w6' };
765
+ assert.equal((await ask({ pr: '6' })).state, 'open');
766
+ // A full page with no match: none, and the answer says the list was cut short.
767
+ mrs = Array.from({ length: 100 }, (_, i) => ({ iid: i + 10, state: 'merged', source_branch: `feat/x-${i}` }));
768
+ const r = await ask();
769
+ assert.deepEqual([r.state, r.partial], ['none', true]);
770
+ assert.equal(findTaskPrs('gitlab', { story: 'EP-l-S01', task: 'T02' }, { runner }).partial, true);
771
+ // --pr naming another task's MR: refused. A bad number: refused.
772
+ mr['9'] = { iid: 9, state: 'merged', source_branch: 'feat/EP-l-S01-T01-x', merge_commit_sha: 'abc1234' };
773
+ await assert.rejects(() => ask({ pr: '9' }), /feat\/EP-l-S01-T01-x, which names another task than EP-l-S01-T02/);
774
+ await assert.rejects(() => ask({ pr: 'x9' }), /--pr must be the PR\/MR number/);
775
+ // The listing fails: refused, never "none".
776
+ await assert.rejects(() => ask({ runner: () => ({ ok: false, stdout: '', stderr: 'denied' }) }), /could not list the PRs\/MRs of api: denied/);
777
+ // No platform recorded: refused with the by-hand way.
778
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api' }, { name: 'web', path: '../web' }] }) });
779
+ fs.rmSync(path.join(W, 'api/.git/refs/remotes'), { recursive: true, force: true });
780
+ execFileSync('git', ['remote', 'remove', 'origin'], { cwd: path.join(W, 'api') });
781
+ await assert.rejects(() => ask(), /api has no platform to ask, so yad cannot see whether EP-l-S01-T02 merged/);
782
+ // A story the repo is not declared for: the same refusal `build ship` gives.
783
+ await assert.rejects(() => buildMerged(P, { story: 'EP-l-S01', task: 'T02', repo: 'ops', runner }), /does not declare ops/);
784
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
785
+ });
786
+
787
+ test('yad build merged through the CLI: help names it, and --json carries the state (#355)', () => {
788
+ const { W, P } = workspace();
789
+ try {
790
+ const h = yad(P, '--help');
791
+ assert.match(h.out, /yad build merged <story> --task <T> --repo <r>/);
792
+ const r = yad(P, 'build', 'merged', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--json');
793
+ // api has an origin on disk but no platform: refused with the by-hand hint, as JSON.
794
+ assert.notEqual(r.code, 0);
795
+ assert.match(r.stdout, /cannot see whether EP-l-S01-T01 merged/);
796
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
797
+ });
798
+
799
+ test('yad build merged: a PR merged off the default branch, or naming another task, is refused, never "merged" (#355 review 1)', async () => {
800
+ const { buildMerged } = await import('./build.mjs');
801
+ const { W, P, api, sha } = workspace();
802
+ try {
803
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
804
+ git(api, 'checkout', '-q', '-b', 'release/1.x');
805
+ write(api, { 'src/rel.js': 'x\n' });
806
+ git(api, 'add', '-A');
807
+ git(api, 'commit', '-q', '-m', 'feat: release fix\n\nTask: EP-l-S01-T01');
808
+ const off = git(api, 'rev-parse', 'HEAD');
809
+ git(api, 'push', '-q', 'origin', 'release/1.x');
810
+ git(api, 'checkout', '-q', 'main');
811
+ let oid = off;
812
+ const runner = (cmd, args) => (cmd === 'gh' && args[1] === 'view'
813
+ ? { ok: true, stdout: JSON.stringify({ state: 'MERGED', mergedAt: '2026-10-19T10:00:00Z', mergeCommit: oid ? { oid } : null, headRefName: 'feat/EP-l-S01-T01-x', url: 'u', body: '', statusCheckRollup: [] }), stderr: '' }
814
+ : { ok: false, stdout: '', stderr: 'x' });
815
+ const ask = () => buildMerged(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', pr: '7', today: '2026-10-20', runner });
816
+ await assert.rejects(ask, /is not on api's main/);
817
+ oid = sha.T02;
818
+ await assert.rejects(ask, /names EP-l-S01-T02, not EP-l-S01-T01/);
819
+ oid = null;
820
+ await assert.rejects(ask, /merged, but the platform names no merge commit/);
821
+ oid = sha.T01;
822
+ assert.deepEqual([(await ask()).state, (await ask()).mergeCommit], ['merged', sha.T01]);
823
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
824
+ });
825
+
826
+ test('yad build merged: a ship build ship would not write (a file another repo\'s name shares) is refused, not "merged" (#355 review 3)', async () => {
827
+ const { buildMerged } = await import('./build.mjs');
828
+ const { W, P, dir, sha } = workspace();
829
+ try {
830
+ write(P, {
831
+ '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }),
832
+ // A shard under api's file name that the ledger cannot read: build ship would refuse to write over it.
833
+ 'epics/EP-l/.sdlc/build-log/EP-l-S01-T01-api.json': '{ not json',
834
+ });
835
+ const runner = (cmd, args) => (cmd === 'gh' && args[1] === 'view'
836
+ ? { ok: true, stdout: JSON.stringify({ state: 'MERGED', mergedAt: '2026-10-19T10:00:00Z', mergeCommit: { oid: sha.T01 }, headRefName: 'feat/EP-l-S01-T01-x', url: 'u', body: '', statusCheckRollup: [] }), stderr: '' }
837
+ : { ok: false, stdout: '', stderr: 'x' });
838
+ await assert.rejects(() => buildMerged(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', pr: '7', today: '2026-10-20', runner }), /api shares a ship file name/);
839
+ assert.equal(fs.readFileSync(path.join(dir, '.sdlc/build-log/EP-l-S01-T01-api.json'), 'utf8'), '{ not json');
840
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
841
+ });
842
+
843
+ test('planAutoOpen: the checks step passed now, and each task\'s newest close names it with a run that passed and was not rejected (#356, #350)', async () => {
844
+ const { planAutoOpen } = await import('./build.mjs');
845
+ const at = { story: 'EP-l-S01', repo: 'api' };
846
+ const run = (uid, over = {}) => ({ uid, story: 'EP-l-S01', repo: 'api', step: 'checks', verdict: 'approved-unchanged', signals: { checks: 'pass' }, ...over });
847
+ const lane = (checks, extra = {}) => ({ currentStep: 'engineer-review', steps: [{ id: 'implement', status: 'done' }, { id: 'checks', status: 'done', ...checks }], ...extra });
848
+ const ask = (l, runs, task = 'T01') => planAutoOpen(l, runs, { ...at, task });
849
+ // Not closed, or closed for another task only.
850
+ assert.match(ask(lane({}), []).message, /checks is not closed/);
851
+ assert.match(ask(lane({ closed: { run: 'a', task: 'T02' } }), [run('a', { task: 'T02' })]).message, /checks has not closed for T01/);
852
+ // A close and a run naming no task count for no task: the next task's branch cannot ride on it.
853
+ assert.match(ask(lane({ closed: { run: 'a' } }), [run('a')]).message, /has not closed for T01/);
854
+ assert.match(ask(lane({ closed: { run: 'a' } }), [run('a')], 'T02').message, /has not closed for T02/);
855
+ assert.equal(ask(lane({ closed: { run: 'a' } }), [run('a', { task: 'T01' })]).ok, true, 'the run names it');
856
+ assert.match(ask(lane({ closed: { run: 'a', task: 'T01' } }), [run('a', { task: 'T02' })]).message, /run a, which is for T02/);
857
+ // A close naming a run that is not recorded, a rejected run, a run without checks=pass.
858
+ assert.match(ask(lane({ closed: { run: 'a', task: 'T01' } }), []).message, /trust run a, which is not recorded/);
859
+ assert.match(ask(lane({ closed: { run: 'a', task: 'T01' } }), [run('a', { verdict: 'rejected' })]).message, /is rejected/);
860
+ assert.match(ask(lane({ closed: { run: 'a', task: 'T01' } }), [run('a', { signals: { checks: 'fail' } })]).message, /did not record checks=pass/);
861
+ assert.match(ask(lane({ closed: { run: 'a', task: 'T01' } }), [run('a', { signals: {} })]).message, /did not record checks=pass/);
862
+ assert.equal(ask(lane({ closed: { run: 'a', task: 'T01' } }), [run('a')]).ok, true);
863
+ // The step reopened and not closed again holds every task — for the next task, or this one again with
864
+ // new work its old run never checked.
865
+ assert.match(ask(lane({ status: 'todo', task: 'T02', runs: [{ run: 'a', task: 'T01' }] }), [run('a')]).message, /checks is not closed \(it is open for T02\)/);
866
+ assert.match(ask(lane({ status: 'blocked', task: 'T01', runs: [{ run: 'a', task: 'T01' }] }), [run('a')]).message, /it is open for T01/);
867
+ // Closed again: the NEWEST close for the task counts — an earlier pass never hides a later rejected run.
868
+ const again = lane({ closed: { run: 'b', task: 'T01' }, runs: [{ run: 'a', task: 'T01' }] });
869
+ assert.match(ask(again, [run('a'), run('b', { verdict: 'rejected' })]).message, /\(b\) is rejected/);
870
+ assert.equal(ask(lane({ closed: { run: 'b', task: 'T01' }, runs: [{ run: 'a', task: 'T01' }] }), [run('a', { verdict: 'rejected' }), run('b')]).ok, true);
871
+ // A group (#350): T01's close kept in `runs`, T02's current — both count while the step is passed now.
872
+ const group = lane({ closed: { run: 'b', task: 'T02' }, runs: [{ run: 'a', task: 'T01' }] });
873
+ const both = planAutoOpen(group, [run('a'), run('b')], { ...at, tasks: ['T01', 'T02'] });
874
+ assert.deepEqual([both.ok, Object.keys(both.runs)], [true, ['T01', 'T02']]);
875
+ assert.match(planAutoOpen(group, [run('a', { signals: { checks: 'fail' } }), run('b')], { ...at, tasks: ['T01', 'T02'] }).message, /checks run for T01 \(a\) did not record checks=pass/);
876
+ assert.match(planAutoOpen(group, [run('a'), run('b')], { ...at, tasks: ['T01', 'T02', 'T03'] }).message, /checks has not closed for T03/);
877
+ assert.match(planAutoOpen(lane({ status: 'todo', task: 'T03', runs: [{ run: 'a', task: 'T01' }, { run: 'b', task: 'T02' }] }), [run('a'), run('b')], { ...at, tasks: ['T01', 'T02'] }).message, /open for T03/);
878
+ // Already opened by the dial: not a second time, for any task of the group.
879
+ assert.match(ask(lane({ closed: { run: 'a', task: 'T01' } }, { opened: [{ task: 'T01', pr: 12 }] }), [run('a')]).message, /already opened by the dial: #12/);
880
+ assert.match(planAutoOpen(group, [run('a'), run('b')], { ...at, tasks: ['T01', 'T02'] , }).ok && planAutoOpen({ ...group, opened: [{ task: 'T02', pr: 4 }] }, [run('a'), run('b')], { ...at, tasks: ['T01', 'T02'] }).message, /EP-l-S01-T02's PR\/MR was already opened by the dial: #4/);
881
+ assert.match(ask(null, []).message, /has no lane/);
882
+ });
883
+
884
+ test('yad dial open-pr: a project-wide action dial every automation.json writer keeps; doctor names a bad value (#356)', async () => {
885
+ const { automationChecks } = await import('./doctor.mjs');
886
+ const { W, P } = workspace();
887
+ try {
888
+ const file = path.join(P, '.sdlc/automation.json');
889
+ let r = yad(P, 'dial', 'open-pr', '--json');
890
+ assert.deepEqual([json(r).scope, json(r).advance, json(r).why], ['action', 'human', 'project']);
891
+ r = yad(P, 'dial', 'open-pr', '--to', 'auto');
892
+ assert.equal(r.code, 0, r.out);
893
+ assert.deepEqual(read(file).actions, { 'open-pr': 'auto' });
894
+ // A Shape dial and the kill switch rewrite the file: the action dial survives each.
895
+ yad(P, 'dial', 'architecture', '--to', 'auto');
896
+ yad(P, 'kill', '--reason', 'bad deploy');
897
+ r = yad(P, 'dial', 'open-pr', '--json');
898
+ assert.deepEqual([json(r).set, json(r).advance, json(r).why], ['auto', 'human', 'kill']);
899
+ yad(P, 'unkill');
900
+ assert.deepEqual([read(file).actions, read(file).steps], [{ 'open-pr': 'auto' }, { architecture: 'auto' }]);
901
+ assert.equal(json(yad(P, 'dial', 'open-pr', '--json')).advance, 'auto');
902
+ // Set for the project, never per lane.
903
+ r = yad(P, 'dial', 'EP-l', 'EP-l-S01', '--repo', 'api', 'open-pr', '--to', 'auto');
904
+ assert.notEqual(r.code, 0);
905
+ assert.match(r.out, /open-pr is set for the whole project, not per lane/);
906
+ // Back to human: the key is removed.
907
+ yad(P, 'dial', 'open-pr', '--to', 'human');
908
+ assert.equal(read(file).actions, undefined);
909
+ // Doctor: an unknown value or action is named.
910
+ fs.writeFileSync(file, JSON.stringify({ steps: {}, actions: { 'open-pr': 'yes', merge: 'auto' } }));
911
+ const checks = [];
912
+ automationChecks(checks, P);
913
+ const unknown = checks.find((x) => x.id === 'automation:unknown');
914
+ assert.match(unknown.message, /actions\.open-pr: "yes" is not human or auto/);
915
+ assert.match(unknown.message, /actions\.merge: not an action this release knows/);
916
+ assert.equal(json(yad(P, 'dial', 'open-pr', '--json')).advance, 'human', '"yes" is not auto');
917
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
918
+ });
919
+
920
+ test('yad open-pr --via auto: opened on the standing approval only after this task\'s checks passed; refusals push nothing; recorded on the lane (#356)', async () => {
921
+ const { runOpenPr } = await import('./openpr.mjs');
922
+ const { buildMerged } = await import('./build.mjs');
923
+ const { W, P, dir, api, sha } = workspace();
924
+ const prev = process.exitCode;
925
+ try {
926
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github', default_branch: 'main' }, { name: 'web', path: '../web' }] }) });
927
+ git(P, 'add', '-A');
928
+ git(P, 'commit', '-q', '-m', 'chore: register api on github');
929
+ const branch = 'feat/EP-l-S01-T01-orders';
930
+ git(api, 'checkout', '-q', '-b', branch);
931
+ write(api, { 'src/orders.js': 'x\n' });
932
+ git(api, 'add', '-A');
933
+ git(api, 'commit', '-q', '-m', 'feat: orders\n\nTask: EP-l-S01-T01');
934
+ const pushed = () => git(api, 'ls-remote', '--heads', 'origin', branch) !== '';
935
+ let created = 0;
936
+ const creator = () => { created += 1; return { ok: true, url: 'https://github.com/o/api/pull/12' }; };
937
+ const runner = () => ({ ok: false, stdout: '', stderr: 'no platform here' });
938
+ const open = (over = {}) => runOpenPr(api, { product: P, via: 'auto', creator, runner, today: '2026-10-20', ...over });
939
+ // The dial is human: refused, nothing pushed.
940
+ await assert.rejects(open, /the open-pr dial is human/);
941
+ yad(P, 'dial', 'open-pr', '--to', 'auto');
942
+ // No lane yet, then checks not closed for T01: refused, nothing pushed.
943
+ await assert.rejects(open, /has no lane/);
944
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
945
+ for (const step of ['spec', 'tasks']) yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', step, 'done');
946
+ const impl = json(yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'implement', '--verdict', 'approved-unchanged', '--task', 'T01', '--json')).uid;
947
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'done', '--run', impl, '--task', 'T01');
948
+ await assert.rejects(open, /checks is not closed/);
949
+ const checks = json(yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', 'checks', '--verdict', 'approved-unchanged', '--signal', 'checks=pass', '--task', 'T01', '--json')).uid;
950
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'checks', 'done', '--run', checks, '--task', 'T01');
951
+ // The kill switch holds it.
952
+ yad(P, 'kill', '--reason', 'freeze');
953
+ await assert.rejects(open, /kill switch .* holds it at human/);
954
+ yad(P, 'unkill');
955
+ // A bad --via; a branch naming no task.
956
+ await assert.rejects(() => open({ via: 'yes' }), /--via must be auto or human/);
957
+ git(api, 'checkout', '-q', '-b', 'misc');
958
+ await assert.rejects(open, /misc carries no task of this Product/);
959
+ // A --task never names a task the branch does not carry (review 5).
960
+ await assert.rejects(() => open({ task: 'EP-l-S01-T01' }), /misc carries no task of this Product/);
961
+ git(api, 'checkout', '-q', branch);
962
+ // A --task naming another task than the branch carries: refused, never believed (review 1).
963
+ await assert.rejects(() => open({ task: 'EP-l-S01-T02' }), /--task EP-l-S01-T02 is not the task feat\/EP-l-S01-T01-orders carries/);
964
+ // On the Product, a review branch is never opened with --via auto, and nothing is pushed (review 1).
965
+ git(P, 'checkout', '-q', '-b', 'review/EP-l/epic');
966
+ await assert.rejects(() => runOpenPr(P, { via: 'auto', platform: 'github', creator, runner }), /only a task PR\/MR in a code repo/);
967
+ git(P, 'checkout', '-q', 'main');
968
+ assert.equal(pushed(), false, 'no refusal pushed the branch');
969
+ assert.equal(created, 0);
970
+ // Opened: pushed, created once, and recorded on the lane with the run that earned it.
971
+ const res = await open();
972
+ assert.deepEqual([res.via, pushed(), created], ['auto', true, 1]);
973
+ const lane = read(path.join(dir, '.sdlc/build-state/EP-l-S01.json')).repos.api;
974
+ assert.deepEqual(lane.opened, [{ task: 'T01', pr: 12, url: 'https://github.com/o/api/pull/12', via: 'auto', run: checks, by: 'alice', date: '2026-10-20' }]);
975
+ // Not a second time.
976
+ await assert.rejects(open, /already opened by the dial: #12/);
977
+ // The same task spelled in another case, or under more folders, is read as the same task (review 2).
978
+ await assert.rejects(() => open({ task: 'ep-l-s01-t01' }), /already opened by the dial: #12/);
979
+ git(api, 'checkout', '-q', '-b', 'amn/feat/ep-l-s01-t01-again');
980
+ await assert.rejects(open, /already opened by the dial: #12/);
981
+ git(api, 'checkout', '-q', branch);
982
+ // Every reading of a branch name is tried; only a story of this Product counts (reviews 3 and 4).
983
+ const { branchTasks } = await import('./build.mjs');
984
+ write(P, { 'epics/EP-aws-s3-t2-backup/stories/EP-aws-s3-t2-backup-S01.md': '---\nid: EP-aws-s3-t2-backup-S01\n---\n', 'epics/EP-l/stories/EP-l-S02.md': '---\nid: EP-l-S02\n---\n' });
985
+ const ids = (b) => branchTasks(P, b).map((x) => `${x.story}-${x.task}`);
986
+ assert.deepEqual(ids('feat/EP-aws-s3-t2-backup-S01-T01'), ['EP-aws-s3-t2-backup-S01-T01'], 'a slug that reads like a task');
987
+ assert.deepEqual(ids('feat/EP-l-S01-T01-then-S02-T03'), ['EP-l-S01-T01'], 'words after the task');
988
+ assert.deepEqual(ids('feat/EP-l-S01-T01-add-s3-t2-support'), ['EP-l-S01-T01']);
989
+ assert.deepEqual(ids('amn/feat/ep-l-s01-t01-again'), ['EP-l-S01-T01'], 'any case, any folders');
990
+ assert.deepEqual(ids('feat/EP-l-S01-T01_EP-l-S02-T02'), ['EP-l-S01-T01', 'EP-l-S02-T02'], 'two real tasks');
991
+ assert.deepEqual(ids('feat/EP-zz-S01-T01'), [], 'not a story here');
992
+ git(api, 'checkout', '-q', '-b', 'feat/EP-l-S01-T01_EP-l-S02-T02');
993
+ await assert.rejects(open, /carries more than one task: EP-l-S01-T01, EP-l-S02-T02/);
994
+ await assert.rejects(() => open({ task: 'EP-l-S01-T01' }), /carries more than one task/);
995
+ git(api, 'checkout', '-q', '-b', 'feat/EP-l-S01-T01-then-S02-T03');
996
+ await assert.rejects(open, /already opened by the dial: #12/, 'read as EP-l-S01-T01');
997
+ await assert.rejects(() => open({ task: 'EP-l-S02-T03' }), /is not the task feat\/EP-l-S01-T01-then-S02-T03 carries/);
998
+ git(api, 'checkout', '-q', branch);
999
+ // `yad build merged` reads the recorded number: no search is needed (the listing would fail here).
1000
+ const view = (cmd, args) => (cmd === 'gh' && args[1] === 'view' && args[2] === '12'
1001
+ ? { ok: true, stdout: JSON.stringify({ state: 'OPEN', headRefName: branch, url: 'u12', body: '', statusCheckRollup: [] }), stderr: '' }
1002
+ : { ok: false, stdout: '', stderr: 'x' });
1003
+ const m = await buildMerged(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', today: '2026-10-20', runner: view });
1004
+ assert.deepEqual([m.state, m.pr], ['open', 12]);
1005
+ // The recorded PR closed without a merge, and a person opened #15: the search finds it (review 1).
1006
+ const after = (cmd, args) => {
1007
+ if (cmd === 'gh' && args[1] === 'view' && args[2] === '12') return { ok: true, stdout: JSON.stringify({ state: 'CLOSED', headRefName: branch, url: 'u12', body: '', statusCheckRollup: [] }), stderr: '' };
1008
+ if (cmd === 'gh' && args[1] === 'view' && args[2] === '15') return { ok: true, stdout: JSON.stringify({ state: 'OPEN', headRefName: `${branch}-2`, url: 'u15', body: '', statusCheckRollup: [] }), stderr: '' };
1009
+ if (cmd === 'gh' && args[1] === 'list') return { ok: true, stdout: JSON.stringify([{ number: 15, state: 'OPEN', headRefName: `${branch}-2`, url: 'u15' }, { number: 12, state: 'CLOSED', headRefName: branch, url: 'u12' }]), stderr: '' };
1010
+ return { ok: false, stdout: '', stderr: 'x' };
1011
+ };
1012
+ const m2 = await buildMerged(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', today: '2026-10-20', runner: after });
1013
+ assert.deepEqual([m2.state, m2.pr], ['open', 15]);
1014
+ // A human open is today's open: no dial asked, nothing recorded.
1015
+ git(api, 'checkout', '-q', '-b', 'feat/EP-l-S01-T02-list');
1016
+ const h = await runOpenPr(api, { product: P, creator, runner });
1017
+ assert.equal(h.via, 'human');
1018
+ assert.equal(read(path.join(dir, '.sdlc/build-state/EP-l-S01.json')).repos.api.opened.length, 1);
1019
+ void sha;
1020
+ } finally { process.exitCode = prev; fs.rmSync(W, { recursive: true, force: true }); }
1021
+ });
1022
+
1023
+ test('follow-ups: a fix/<story>-<task>-followup branch never competes with the task\'s PR, and its merge is added to the ship (#350)', async () => {
1024
+ const { buildShip, buildMerged, isFollowUpBranch } = await import('./build.mjs');
1025
+ const { W, P, dir, api, sha } = workspace();
1026
+ try {
1027
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
1028
+ assert.equal(isFollowUpBranch('fix/EP-l-S01-T01-followup-null-check', 'EP-l-S01', 'T01'), true);
1029
+ assert.equal(isFollowUpBranch('amn/fix/ep-l-s01-t01-followup', 'EP-l-S01', 'T01'), true);
1030
+ assert.equal(isFollowUpBranch('feat/EP-l-S01-T01-orders', 'EP-l-S01', 'T01'), false);
1031
+ assert.equal(isFollowUpBranch('fix/EP-l-S01-T012-followup', 'EP-l-S01', 'T01'), false);
1032
+ // A follow-up commit on main, naming the task.
1033
+ write(api, { 'src/T01-fix.js': 'fix\n' });
1034
+ git(api, 'add', '-A');
1035
+ git(api, 'commit', '-q', '-m', 'fix: null check\n\nTask: EP-l-S01-T01');
1036
+ const fu = git(api, 'rev-parse', 'HEAD');
1037
+ git(api, 'push', '-q', 'origin', 'main');
1038
+ const views = {
1039
+ 7: { state: 'MERGED', mergedAt: '2026-10-18T10:00:00Z', mergeCommit: { oid: sha.T01 }, headRefName: 'feat/EP-l-S01-T01-orders', url: 'https://github.com/o/api/pull/7', body: '', statusCheckRollup: [] },
1040
+ };
1041
+ const followUpPr = { state: 'OPEN', mergedAt: null, mergeCommit: null, headRefName: 'fix/EP-l-S01-T01-followup-null', url: 'https://github.com/o/api/pull/9', body: '', statusCheckRollup: [] };
1042
+ const runner = (cmd, args) => {
1043
+ if (cmd === 'gh' && args[1] === 'list') return { ok: true, stdout: JSON.stringify(Object.entries(views).map(([n, v]) => ({ number: Number(n), state: v.state, headRefName: v.headRefName, url: v.url })).reverse()), stderr: '' };
1044
+ if (cmd === 'gh' && args[1] === 'view' && views[args[2]]) return { ok: true, stdout: JSON.stringify(views[args[2]]), stderr: '' };
1045
+ return { ok: false, stdout: '', stderr: 'x' };
1046
+ };
1047
+ const prReader = () => ({ ok: true, reviews: [] });
1048
+ const base = { story: 'EP-l-S01', task: 'T01', repo: 'api', today: '2026-10-20', runner, prReader };
1049
+ // Before the ship, an open follow-up does not compete with the task's own merged PR, by search or by number;
1050
+ // and it is never recorded as the task's ship (review 2).
1051
+ views[9] = followUpPr;
1052
+ let r = await buildMerged(P, base);
1053
+ assert.deepEqual([r.state, r.pr], ['merged', 7]);
1054
+ await assert.rejects(() => buildMerged(P, { ...base, pr: '9' }), /a follow-up branch, while EP-l-S01-T01's own PR\/MR is another/);
1055
+ views[9] = { ...followUpPr, state: 'MERGED', mergeCommit: { oid: sha.T02 } };
1056
+ await assert.rejects(() => buildShip(P, { ...base, pr: '9' }), /a follow-up branch of EP-l-S01-T01/);
1057
+ // A follow-up-named PR recorded by the dial is held to the same rule (review 4).
1058
+ const bsFile = path.join(dir, '.sdlc/build-state/EP-l-S01.json');
1059
+ fs.mkdirSync(path.dirname(bsFile), { recursive: true });
1060
+ fs.writeFileSync(bsFile, JSON.stringify({ story: 'EP-l-S01', repos: { api: { currentStep: 'engineer-review', steps: [], opened: [{ task: 'T01', pr: 9, via: 'auto' }] } } }));
1061
+ await assert.rejects(() => buildMerged(P, base), /a follow-up branch, while EP-l-S01-T01's own PR\/MR is another/);
1062
+ fs.rmSync(bsFile);
1063
+ delete views[9];
1064
+ await assert.rejects(() => buildMerged(P, { ...base, followUp: true }), /has no ship in api yet, so it has no follow-up/);
1065
+ await assert.rejects(() => buildShip(P, { ...base, pr: '9', followUp: true }), /has no ship in api yet/);
1066
+ r = await buildMerged(P, { ...base, ship: true });
1067
+ assert.equal(r.changed, true);
1068
+ // Once shipped, the task's own question is answered `shipped`, follow-up or not; the dial opens no follow-up.
1069
+ views[9] = followUpPr;
1070
+ assert.equal((await buildMerged(P, base)).state, 'shipped');
1071
+ yad(P, 'dial', 'open-pr', '--to', 'auto');
1072
+ const { autoOpenCheck } = await import('./build.mjs');
1073
+ assert.throws(() => autoOpenCheck(P, { story: 'EP-l-S01', task: 'T01', repo: 'api' }), /EP-l-S01-T01 has already shipped in api/);
1074
+ // A follow-up PR is never the task's own ship, nor asked about as one.
1075
+ views[9] = { ...views[9], state: 'MERGED', mergedAt: '2026-10-20T09:00:00Z', mergeCommit: { oid: fu } };
1076
+ await assert.rejects(() => buildShip(P, { ...base, pr: '9' }), /PR #9 is from fix\/EP-l-S01-T01-followup-null, a follow-up branch of EP-l-S01-T01/);
1077
+ // As a follow-up: found by its branch, merged, and added to the ship with --ship.
1078
+ views[9] = { ...views[9], state: 'OPEN', mergeCommit: null };
1079
+ assert.deepEqual([(await buildMerged(P, { ...base, followUp: true })).state, (await buildMerged(P, { ...base, followUp: true })).pr], ['open', 9]);
1080
+ views[9] = { ...views[9], state: 'MERGED', mergeCommit: { oid: fu } };
1081
+ r = await buildMerged(P, { ...base, followUp: true });
1082
+ assert.deepEqual([r.state, r.pr, r.mergeCommit, r.changed], ['merged', 9, fu, false]);
1083
+ r = await buildMerged(P, { ...base, followUp: true, ship: true });
1084
+ assert.deepEqual([r.state, r.changed, r.entry], ['merged', true, { pr: 'https://github.com/o/api/pull/9', branch: 'fix/EP-l-S01-T01-followup-null', mergeCommit: fu, date: '2026-10-20' }]);
1085
+ const shipFile = path.join(dir, '.sdlc/build-log/EP-l-S01-T01-api.json');
1086
+ assert.deepEqual([read(shipFile).mergeCommit, read(shipFile).followUps.length], [sha.T01, 1]);
1087
+ assert.equal(status(dir), 'in-build', 'the status is left as it is');
1088
+ // Asked again: recorded, so no follow-up is left to find; by number it is `shipped`; a second record is refused.
1089
+ assert.equal((await buildMerged(P, { ...base, followUp: true })).state, 'none');
1090
+ // An older follow-up closed without a merge owes nothing: still `none`, not `closed` (review 1).
1091
+ views[8] = { ...followUpPr, state: 'CLOSED', headRefName: 'fix/EP-l-S01-T01-followup-old', url: 'u8' };
1092
+ assert.equal((await buildMerged(P, { ...base, followUp: true })).state, 'none');
1093
+ delete views[8];
1094
+ assert.equal((await buildMerged(P, { ...base, followUp: true, pr: '9' })).state, 'shipped');
1095
+ await assert.rejects(() => buildShip(P, { ...base, pr: '9', followUp: true }), /a follow-up of EP-l-S01 \/ T01 already records/);
1096
+ await assert.rejects(() => buildShip(P, { ...base, pr: '7', followUp: true }), /T01's own ship already records/);
1097
+ // Where the platform can be read, a follow-up comes from its PR; where it cannot, by hand.
1098
+ await assert.rejects(() => buildShip(P, { ...base, mergeCommit: fu, followUp: true }), /a follow-up is read from its merged PR\/MR/);
1099
+ write(api, { 'src/T01-fix2.js': 'fix2\n' });
1100
+ git(api, 'add', '-A');
1101
+ git(api, 'commit', '-q', '-m', 'fix: second\n\nTask: EP-l-S01-T01');
1102
+ const fu2 = git(api, 'rev-parse', 'HEAD');
1103
+ git(api, 'push', '-q', 'origin', 'main');
1104
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api' }, { name: 'web', path: '../web' }] }) });
1105
+ r = await buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', mergeCommit: fu2, followUp: true, today: '2026-10-20' });
1106
+ assert.equal(r.followUp.pr, 'local');
1107
+ assert.deepEqual(read(shipFile).followUps.map((x) => x.mergeCommit), [fu, fu2]);
1108
+ // The same commit in its short, upper-case form is the same follow-up (review 1).
1109
+ await assert.rejects(() => buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', mergeCommit: fu2.slice(0, 9).toUpperCase(), followUp: true, today: '2026-10-20' }), /already records merge/);
1110
+ await assert.rejects(() => buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', mergeCommit: sha.T01.slice(0, 7), followUp: true, today: '2026-10-20' }), /T01's own ship already records merge/);
1111
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1112
+ });
1113
+
1114
+ test('a task branch whose words start with "followup" is the task\'s own until it ships (#350 review 1)', async () => {
1115
+ const { buildShip, buildMerged } = await import('./build.mjs');
1116
+ const { W, P, dir, sha } = workspace();
1117
+ try {
1118
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
1119
+ const view = { state: 'MERGED', mergedAt: '2026-10-19T10:00:00Z', mergeCommit: { oid: sha.T02 }, headRefName: 'feat/EP-l-S01-T02-followup-reminders', url: 'u5', body: '', statusCheckRollup: [] };
1120
+ const runner = (cmd, args) => {
1121
+ if (cmd === 'gh' && args[1] === 'list') return { ok: true, stdout: JSON.stringify([{ number: 5, state: 'MERGED', headRefName: view.headRefName, url: 'u5' }]), stderr: '' };
1122
+ if (cmd === 'gh' && args[1] === 'view' && args[2] === '5') return { ok: true, stdout: JSON.stringify(view), stderr: '' };
1123
+ return { ok: false, stdout: '', stderr: 'x' };
1124
+ };
1125
+ const base = { story: 'EP-l-S01', task: 'T02', repo: 'api', today: '2026-10-20', runner, prReader: () => ({ ok: true, reviews: [] }) };
1126
+ assert.deepEqual([(await buildMerged(P, base)).state, (await buildMerged(P, base)).pr], ['merged', 5]);
1127
+ // Older than the page the listing reads: the PR asked about still counts (review 3).
1128
+ const older = { ...base, runner: (cmd, args) => (cmd === 'gh' && args[1] === 'list' ? { ok: true, stdout: '[]', stderr: '' } : runner(cmd, args)) };
1129
+ assert.equal((await buildMerged(P, { ...older, pr: '5' })).state, 'merged');
1130
+ const r = await buildShip(P, { ...older, pr: '5' });
1131
+ assert.equal(r.changed, true);
1132
+ assert.equal(read(path.join(dir, '.sdlc/build-log/EP-l-S01-T02-api.json')).mergeCommit, sha.T02);
1133
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1134
+ });
1135
+
1136
+ // A grouped PR's branch in `api`: two commits, T01 and T02, merged into main as PR #20 with a merge commit.
1137
+ function groupedMerge(api, { merge = true } = {}) {
1138
+ git(api, 'checkout', '-q', '-b', 'feat/EP-l-S01-T01-small-fixes');
1139
+ for (const t of ['T01', 'T02']) {
1140
+ write(api, { [`src/g-${t}.js`]: `${t}\n` });
1141
+ git(api, 'add', '-A');
1142
+ git(api, 'commit', '-q', '-m', `fix: small ${t}\n\nTask: EP-l-S01-${t}`);
1143
+ }
1144
+ git(api, 'checkout', '-q', 'main');
1145
+ if (merge) git(api, 'merge', '-q', '--no-ff', '-m', 'Merge pull request #20 from o/feat/EP-l-S01-T01-small-fixes', 'feat/EP-l-S01-T01-small-fixes');
1146
+ else {
1147
+ git(api, 'merge', '-q', '--squash', 'feat/EP-l-S01-T01-small-fixes');
1148
+ git(api, 'commit', '-q', '-m', 'fix: small fixes (#20)\n\nTask: EP-l-S01-T01');
1149
+ }
1150
+ git(api, 'push', '-q', 'origin', 'main');
1151
+ return git(api, 'rev-parse', 'HEAD');
1152
+ }
1153
+
1154
+ test('a grouped PR: several small tasks of one story in one PR, each shipped from it (#350)', async () => {
1155
+ const { buildShip, buildMerged, prTasks, trailerTask } = await import('./build.mjs');
1156
+ const { W, P, dir, api } = workspace({ tasks: '# Tasks\n\n- [ ] T01 — a\n- [ ] T02 — b\n- [ ] T03 — c\n' });
1157
+ try {
1158
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
1159
+ const m = groupedMerge(api);
1160
+ // A Task: trailer as git reads it: the last paragraph, the key in any case, the first one (review 1).
1161
+ assert.equal(trailerTask('fix: x\n\nTask: EP-l-S01-T01\nTask: EP-l-S01-T02'), 'EP-l-S01-T01');
1162
+ assert.equal(trailerTask('fix: x\r\n\r\ntask: EP-l-S01-T02 \r\n'), 'EP-l-S01-T02');
1163
+ assert.equal(trailerTask('fix: x\n\nTask: EP-l-S01-T07\nmore words\n\nTask: EP-l-S01-T04'), 'EP-l-S01-T04', 'a Task: line in a middle paragraph is not a trailer');
1164
+ assert.equal(trailerTask('fix: x\n\nTask: not-an-id'), null);
1165
+ // git's own trailer rules (review 2): prose before it, the title alone, a folded value, an empty first one.
1166
+ assert.equal(trailerTask('fix: a\n\nSome prose here.\nTask: EP-l-S01-T01'), null);
1167
+ assert.equal(trailerTask('Task: EP-l-S01-T01'), null);
1168
+ assert.equal(trailerTask('fix: a\n\nTask: EP-l-S01-T01\n---\nfoo'), null);
1169
+ assert.equal(trailerTask('fix: a\n\nTask:\n EP-l-S01-T01'), 'EP-l-S01-T01');
1170
+ assert.equal(trailerTask('fix: a\n\nTask:\nTask: EP-l-S01-T02'), 'EP-l-S01-T02');
1171
+ const view = { state: 'MERGED', mergedAt: '2026-10-20T10:00:00Z', mergeCommit: { oid: m }, headRefName: 'feat/EP-l-S01-T01-small-fixes', url: 'https://github.com/o/api/pull/20', body: '', statusCheckRollup: [] };
1172
+ const commits = { commits: [{ commit: { message: 'fix: small T01\n\nTask: EP-l-S01-T01' } }, { commit: { message: 'fix: small T02\n\nTask: EP-l-S01-T02' } }] };
1173
+ let commitsOk = true;
1174
+ const runner = (cmd, args) => {
1175
+ if (cmd === 'gh' && args[0] === 'api' && args[1].startsWith('repos/{owner}/{repo}/pulls/20/commits')) return commitsOk ? { ok: true, stdout: JSON.stringify(commits.commits), stderr: '' } : { ok: false, stdout: '', stderr: 'denied' };
1176
+ if (cmd === 'gh' && args[1] === 'view' && args[2] === '20') return { ok: true, stdout: JSON.stringify(view), stderr: '' };
1177
+ if (cmd === 'gh' && args[1] === 'list') return { ok: true, stdout: JSON.stringify([{ number: 20, state: 'MERGED', headRefName: view.headRefName, url: view.url }]), stderr: '' };
1178
+ return { ok: false, stdout: '', stderr: 'x' };
1179
+ };
1180
+ const base = { story: 'EP-l-S01', repo: 'api', today: '2026-10-20', runner, prReader: () => ({ ok: true, reviews: [] }) };
1181
+ assert.deepEqual(prTasks('github', 20, { runner }).tasks, ['EP-l-S01-T01', 'EP-l-S01-T02']);
1182
+ // T02 is not the branch's task: the search does not find it, by --pr it does.
1183
+ let r = await buildMerged(P, { ...base, task: 'T02' });
1184
+ assert.equal(r.state, 'none');
1185
+ r = await buildMerged(P, { ...base, task: 'T02', pr: '20' });
1186
+ assert.deepEqual([r.state, r.mergeCommit], ['merged', m]);
1187
+ // A task the PR does not carry is refused; commits that cannot be read are a refusal, never a guess.
1188
+ await assert.rejects(() => buildMerged(P, { ...base, task: 'T03', pr: '20' }), /PR\/MR #20's commits carry EP-l-S01-T01, EP-l-S01-T02, not EP-l-S01-T03/);
1189
+ // A branch naming no task whose commits carry other tasks: that is evidence, not a warning (review 1).
1190
+ view.headRefName = 'fix/typo';
1191
+ await assert.rejects(() => buildShip(P, { ...base, task: 'T03', pr: '20' }), /PR #20's commits carry EP-l-S01-T01, EP-l-S01-T02, not EP-l-S01-T03/);
1192
+ view.headRefName = 'feat/EP-l-S01-T01-small-fixes';
1193
+ commitsOk = false;
1194
+ await assert.rejects(() => buildShip(P, { ...base, task: 'T02', pr: '20' }), /its commits could not all be read to see whether it carries T02 too: denied/);
1195
+ commitsOk = true;
1196
+ // More than a page of commits: as unread as a failed read (review 1).
1197
+ const many = commits.commits;
1198
+ commits.commits = Array.from({ length: 100 }, () => ({ commit: { message: 'chore: x' } }));
1199
+ await assert.rejects(() => buildShip(P, { ...base, task: 'T02', pr: '20' }), /\(more than one page of them\)/);
1200
+ commits.commits = many;
1201
+ // Each task ships from the one PR, with the same merge commit.
1202
+ await buildShip(P, { ...base, task: 'T01', pr: '20' });
1203
+ await buildShip(P, { ...base, task: 'T02', pr: '20' });
1204
+ const ship = (t) => read(path.join(dir, `.sdlc/build-log/EP-l-S01-${t}-api.json`));
1205
+ assert.deepEqual([ship('T01').mergeCommit, ship('T02').mergeCommit, ship('T02').pr], [m, m, 'https://github.com/o/api/pull/20']);
1206
+ assert.equal((await buildMerged(P, { ...base, task: 'T02', pr: '20' })).state, 'shipped');
1207
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1208
+ });
1209
+
1210
+ test('a grouped PR by hand: the merge commit\'s merged range carries the tasks; a squash names only one (#350)', async () => {
1211
+ const { buildShip } = await import('./build.mjs');
1212
+ for (const merge of [true, false]) {
1213
+ const { W, P, api } = workspace();
1214
+ try {
1215
+ const m = groupedMerge(api, { merge });
1216
+ const ship = () => buildShip(P, { story: 'EP-l-S01', task: 'T02', repo: 'api', mergeCommit: m, today: '2026-10-20' });
1217
+ if (merge) assert.equal((await ship()).ship.mergeCommit, m);
1218
+ else await assert.rejects(ship, /names EP-l-S01-T01, not EP-l-S01-T02/);
1219
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1220
+ }
1221
+ });
1222
+
1223
+ test('grouped PR size: lines and files from the merge base, binary counted; warn only, over group_limit (#350)', async () => {
1224
+ const { changeSize, groupLimit, groupWarnings } = await import('./build.mjs');
1225
+ const { W, api } = workspace();
1226
+ try {
1227
+ git(api, 'checkout', '-q', '-b', 'g');
1228
+ write(api, { 'big.txt': Array.from({ length: 150 }, (_, i) => `l${i}`).join('\n') + '\n', 'two.txt': 'a\nb\n' });
1229
+ fs.writeFileSync(path.join(api, 'bin.dat'), Buffer.from([0, 1, 2, 0, 255]));
1230
+ git(api, 'add', '-A');
1231
+ git(api, 'commit', '-q', '-m', 'fix: a\n\nTask: EP-l-S01-T01');
1232
+ assert.deepEqual(changeSize(api, 'origin/main'), { lines: 152, files: 3 });
1233
+ const ids = ['EP-l-S01-T01', 'EP-l-S01-T02'];
1234
+ assert.deepEqual(groupWarnings(api, 'origin/main', ['EP-l-S01-T01'], {}), [], 'one task is not a group');
1235
+ assert.deepEqual(groupWarnings(api, 'origin/main', ids, {}), [], 'under the default limit');
1236
+ assert.match(groupWarnings(api, 'origin/main', ids, { group_limit: { lines: 100 } })[0], /changes 152 lines in 3 files — over the limit for a group \(100 lines, 10 files\)/);
1237
+ assert.match(groupWarnings(api, 'origin/main', ids, { group_limit: { files: 2 } })[0], /over the limit/);
1238
+ assert.deepEqual(groupLimit({ group_limit: { lines: -1, files: 'x' } }), { lines: 200, files: 10, bad: ['group_limit.lines', 'group_limit.files'] });
1239
+ assert.match(groupWarnings(api, 'origin/main', ['EP-l-S01-T01', 'EP-l-S02-T01'], {})[0], /tasks of more than one story/);
1240
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1241
+ });
1242
+
1243
+ test('yad open-pr on a grouped branch: the body lists every task; --via auto checks each task\'s run and records each (#350)', async () => {
1244
+ const { runOpenPr } = await import('./openpr.mjs');
1245
+ const { W, P, dir, api } = workspace({ tasks: '# Tasks\n\n- [ ] T01 — a\n- [ ] T02 — b\n' });
1246
+ const prev = process.exitCode;
1247
+ try {
1248
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github', default_branch: 'main' }, { name: 'web', path: '../web' }] }) });
1249
+ git(P, 'add', '-A');
1250
+ git(P, 'commit', '-q', '-m', 'chore: register api on github');
1251
+ groupedMerge(api, { merge: false });
1252
+ git(api, 'reset', '-q', '--hard', 'origin/main~1');
1253
+ git(api, 'push', '-q', '-f', 'origin', 'main');
1254
+ git(api, 'checkout', '-q', 'feat/EP-l-S01-T01-small-fixes');
1255
+ let seen = null;
1256
+ const creator = (_p, o) => { seen = o; return { ok: true, url: 'https://github.com/o/api/pull/21' }; };
1257
+ const runner = () => ({ ok: false, stdout: '', stderr: 'no platform here' });
1258
+ // A person opens it: the body lists both tasks; nothing is recorded.
1259
+ let res = await runOpenPr(api, { product: P, creator, runner, today: '2026-10-20' });
1260
+ assert.deepEqual(res.tasks, ['EP-l-S01-T01', 'EP-l-S01-T02']);
1261
+ assert.match(seen.body, /Story \/ task: `EP-l-S01-T01`, `EP-l-S01-T02`/);
1262
+ assert.match(seen.body, /specs\/EP-l-S01\//);
1263
+ // On the standing approval: each task's checks must have passed — T02's has not closed yet.
1264
+ yad(P, 'dial', 'open-pr', '--to', 'auto');
1265
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
1266
+ for (const step of ['spec', 'tasks']) yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', step, 'done');
1267
+ const close = (task) => {
1268
+ for (const step of ['implement', 'checks']) {
1269
+ const uid = json(yad(P, 'build', 'trust', 'EP-l-S01', '--repo', 'api', step, '--verdict', 'approved-unchanged', ...(step === 'checks' ? ['--signal', 'checks=pass'] : []), '--task', task, '--json')).uid;
1270
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', step, 'done', '--run', uid, '--task', task);
1271
+ }
1272
+ };
1273
+ close('T01');
1274
+ const open = () => runOpenPr(api, { product: P, via: 'auto', creator, runner, today: '2026-10-20' });
1275
+ await assert.rejects(open, /checks has not closed for T02/);
1276
+ yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'implement', 'in_progress', '--task', 'T02');
1277
+ close('T02');
1278
+ res = await open();
1279
+ assert.deepEqual(res.tasks, ['EP-l-S01-T01', 'EP-l-S01-T02']);
1280
+ const opened = read(path.join(dir, '.sdlc/build-state/EP-l-S01.json')).repos.api.opened;
1281
+ assert.deepEqual(opened.map((o) => [o.task, o.pr, o.via]), [['T01', 21, 'auto'], ['T02', 21, 'auto']]);
1282
+ assert.notEqual(opened[0].run, opened[1].run, 'each task names its own checks run');
1283
+ // A commit of another story on the branch: refused before the push.
1284
+ write(api, { 'src/other.js': 'o\n' });
1285
+ git(api, 'add', '-A');
1286
+ git(api, 'commit', '-q', '-m', 'fix: other\n\nTask: EP-l-S02-T01');
1287
+ await assert.rejects(open, /carries tasks of another story: EP-l-S02-T01/);
1288
+ } finally { process.exitCode = prev; fs.rmSync(W, { recursive: true, force: true }); }
1289
+ });
1290
+
1291
+ test('yad build sync: records every merged task with no ship through yad build ship, then build done; lists what it cannot (#365)', async () => {
1292
+ const { buildSync } = await import('./build.mjs');
1293
+ const { W, P, dir, sha } = workspace();
1294
+ try {
1295
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github', default_branch: 'main' }, { name: 'web', path: '../web' }] }) });
1296
+ const asked = [];
1297
+ const runner = (cmd, args) => {
1298
+ asked.push(args.join(' '));
1299
+ // T01 merged with no PR; T02's merge commit was merged by PR #9.
1300
+ if (cmd === 'gh' && args[0] === 'api' && args[1] === `repos/{owner}/{repo}/commits/${sha.T01}/pulls`) return { ok: true, stdout: '[]', stderr: '' };
1301
+ if (cmd === 'gh' && args[0] === 'api' && args[1] === `repos/{owner}/{repo}/commits/${sha.T02}/pulls`) return { ok: true, stdout: JSON.stringify([{ number: 9, merged_at: '2026-10-20T10:00:00Z' }]), stderr: '' };
1302
+ if (cmd === 'gh' && args[1] === 'view' && args[2] === '9') return { ok: true, stdout: JSON.stringify({ state: 'MERGED', mergedAt: '2026-10-20T10:00:00Z', mergeCommit: { oid: sha.T02 }, headRefName: 'feat/EP-l-S01-T02-list', url: 'https://github.com/o/api/pull/9', body: '', statusCheckRollup: [] }), stderr: '' };
1303
+ return { ok: false, stdout: '', stderr: 'x' };
1304
+ };
1305
+ const prReader = () => ({ ok: true, reviews: [{ login: 'bob', state: 'APPROVED', body: '' }] });
1306
+ // No Build lane: the story may predate tracking, so a person decides (review 1).
1307
+ let r = await buildSync(P, { today: '2026-10-21', runner, prReader });
1308
+ assert.deepEqual([r.recorded.length, r.left.length], [0, 2]);
1309
+ assert.match(r.left[0].why, /has no Build lane in api — it may predate tracking/);
1310
+ assert.match(r.left[0].fix, /--retro-ship/);
1311
+ // A skipped lane, and a build-state that does not parse, are refused by build ship with their own cause (review 2).
1312
+ const bsFile = path.join(dir, '.sdlc/build-state/EP-l-S01.json');
1313
+ write(P, { 'epics/EP-l/.sdlc/build-state/EP-l-S01.json': JSON.stringify({ story: 'EP-l-S01', repos: { api: { status: 'skipped', record: { reason: 'x' } } } }) });
1314
+ r = await buildSync(P, { today: '2026-10-21', runner, prReader });
1315
+ assert.match(r.left[0].why, /EP-l-S01 \/ api is skipped/);
1316
+ for (const bad of ['{ not json', '[]', '{"repos": []}']) {
1317
+ fs.writeFileSync(bsFile, bad);
1318
+ r = await buildSync(P, { today: '2026-10-21', runner, prReader });
1319
+ assert.doesNotMatch(r.left[0].why, /predate tracking/, bad);
1320
+ }
1321
+ fs.rmSync(bsFile);
1322
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
1323
+ // A dry run writes nothing and says what it would run — the build done too (review 1).
1324
+ r = await buildSync(P, { dryRun: true, today: '2026-10-21', runner, prReader });
1325
+ assert.deepEqual(r.planned.filter((p) => p.task).map((p) => p.task).sort(), ['T01', 'T02']);
1326
+ assert.ok(r.planned.some((p) => p.command === 'yad build done EP-l-S01'));
1327
+ assert.equal(fs.existsSync(path.join(dir, '.sdlc/build-log')), false);
1328
+ // The run: T01 from its commit (no PR merged it), T02 from PR #9, which the platform names for its commit.
1329
+ r = await buildSync(P, { today: '2026-10-21', runner, prReader });
1330
+ assert.deepEqual(r.recorded.map((x) => [x.task, x.pr]).sort(), [['T01', 'none'], ['T02', 'https://github.com/o/api/pull/9']]);
1331
+ assert.deepEqual(read(path.join(dir, '.sdlc/build-log/EP-l-S01-T02-api.json')).engineer_review, [{ approver: 'bob', engagement: 'none' }]);
1332
+ assert.equal(status(dir), 'in-build');
1333
+ // build done ran for the story, and its refusal (web is not cloned) is listed, not fatal.
1334
+ assert.equal(r.notDone[0].story, 'EP-l-S01');
1335
+ assert.equal(r.changed, true);
1336
+ // Asked again: nothing left to record.
1337
+ r = await buildSync(P, { today: '2026-10-21', runner, prReader });
1338
+ assert.deepEqual([r.recorded.length, r.left.length], [0, 0]);
1339
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1340
+ });
1341
+
1342
+ test('yad build sync: where the platform cannot be read, nothing is recorded on nobody\'s word (#365)', async () => {
1343
+ const { buildSync } = await import('./build.mjs');
1344
+ const { W, P, dir } = workspace();
1345
+ try {
1346
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', default_branch: 'main' }, { name: 'web', path: '../web' }] }) });
1347
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
1348
+ const r = await buildSync(P, { today: '2026-10-21' });
1349
+ assert.equal(r.recorded.length, 0);
1350
+ assert.deepEqual(r.left.map((x) => x.task).sort(), ['T01', 'T02']);
1351
+ assert.match(r.left[0].why, /api has no platform to read the PR\/MR that merged/);
1352
+ assert.match(r.left[0].fix, /a person records it/);
1353
+ assert.equal(fs.existsSync(path.join(dir, '.sdlc/build-log')), false);
1354
+ await assert.rejects(() => buildSync(P, { epic: 'nope' }), /'nope' is not an epic id/);
1355
+ await assert.rejects(() => buildSync(P, { epic: 'EP-typo' }), /no epic EP-typo in this Product/);
1356
+ assert.match(yad(P, 'build', 'sync', 'EP-l', 'EP-x').out, /takes one epic at most/);
1357
+ // A repo not read is unknown, never an all-clear (review 1).
1358
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../gone', default_branch: 'main' }, { name: 'web', path: '../web' }] }) });
1359
+ const u = await buildSync(P, { today: '2026-10-21' });
1360
+ assert.equal(u.recorded.length + u.left.length, 0);
1361
+ assert.ok(u.unread.some((x) => x.repo === 'api'));
1362
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1363
+ });
1364
+
1365
+ test('yad build ship from a commit more than one merged PR holds: refused, naming them, never the first (#365 review 1)', async () => {
1366
+ const { buildShip } = await import('./build.mjs');
1367
+ const { W, P, sha } = workspace();
1368
+ try {
1369
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', platform: 'github' }, { name: 'web', path: '../web' }] }) });
1370
+ const runner = (cmd, args) => (cmd === 'gh' && args[0] === 'api' && /\/pulls$/.test(args[1])
1371
+ ? { ok: true, stdout: JSON.stringify([{ number: 4, merged_at: 'x' }, { number: 8, merged_at: 'y' }]), stderr: '' }
1372
+ : { ok: false, stdout: '', stderr: 'x' });
1373
+ await assert.rejects(() => buildShip(P, { story: 'EP-l-S01', task: 'T01', repo: 'api', mergeCommit: sha.T01, today: '2026-10-21', runner }), /is in more than one merged PR\/MR: #4, #8/);
1374
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1375
+ });
1376
+
1377
+ test('yad build step spec in_progress pins Spec Kit\'s feature folder to the story, only where Spec Kit is used (#348)', () => {
1378
+ const { W, P, api } = workspace();
1379
+ const fj = path.join(api, '.specify/feature.json');
1380
+ try {
1381
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
1382
+ // No .specify/: the repo does not use Spec Kit, so nothing is written or said.
1383
+ let r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'in_progress', '--json');
1384
+ assert.equal(r.code, 0, r.out);
1385
+ assert.equal('specKit' in json(r), false);
1386
+ assert.equal(fs.existsSync(fj), false);
1387
+ assert.doesNotMatch(r.out, /Spec Kit/);
1388
+ // The previous story's folder, plus a key of Spec Kit's own: the folder moves, the key stays.
1389
+ write(api, { '.specify/feature.json': JSON.stringify({ feature_directory: 'specs/EP-l-S00', other: 1 }) });
1390
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'in_progress', '--json');
1391
+ assert.equal(r.code, 0, r.out);
1392
+ assert.deepEqual(json(r).specKit, { file: 'api/.specify/feature.json', featureDirectory: 'specs/EP-l-S01', changed: true });
1393
+ assert.deepEqual(read(fj), { feature_directory: 'specs/EP-l-S01', other: 1 });
1394
+ assert.match(r.out, /SPECIFY_FEATURE_DIRECTORY=specs\/EP-l-S01/);
1395
+ // Already pinned: no rewrite.
1396
+ const before = fs.statSync(fj).mtimeMs;
1397
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'in_progress');
1398
+ assert.equal(r.code, 0, r.out);
1399
+ assert.match(r.out, /feature folder in api is already specs\/EP-l-S01/);
1400
+ assert.equal(fs.statSync(fj).mtimeMs, before);
1401
+ // A file it cannot read is left alone, and the step is still recorded.
1402
+ fs.writeFileSync(fj, '{ not json');
1403
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'in_progress');
1404
+ assert.equal(r.code, 0, r.out);
1405
+ assert.match(r.out, /feature\.json cannot be read, so it was left alone/);
1406
+ assert.equal(fs.readFileSync(fj, 'utf8'), '{ not json');
1407
+ fs.writeFileSync(fj, '[]');
1408
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'in_progress');
1409
+ assert.match(r.out, /is not a JSON object/);
1410
+ assert.equal(fs.readFileSync(fj, 'utf8'), '[]');
1411
+ // Other moves of the step do not touch it.
1412
+ fs.rmSync(fj);
1413
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'done');
1414
+ assert.equal(r.code, 0, r.out);
1415
+ assert.equal(fs.existsSync(fj), false);
1416
+ // The tasks step pins it too (yad-spec runs both, and another story may have moved it), creating the
1417
+ // file when .specify/ has none; specify's hint is for the spec step only.
1418
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'tasks', 'in_progress', '--json');
1419
+ assert.equal(r.code, 0, r.out);
1420
+ assert.deepEqual(json(r).specKit, { file: 'api/.specify/feature.json', featureDirectory: 'specs/EP-l-S01', changed: true });
1421
+ assert.equal(fs.readFileSync(fj, 'utf8'), '{"feature_directory":"specs/EP-l-S01"}\n');
1422
+ assert.doesNotMatch(r.out, /SPECIFY_FEATURE_DIRECTORY/);
1423
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'tasks', 'in_progress', '--json');
1424
+ assert.deepEqual(json(r).specKit, { file: 'api/.specify/feature.json', featureDirectory: 'specs/EP-l-S01', changed: false });
1425
+ // Not a plain file, or a .specify that is a link: never written through.
1426
+ fs.rmSync(fj);
1427
+ fs.mkdirSync(fj);
1428
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'tasks', 'in_progress');
1429
+ assert.equal(r.code, 0, r.out);
1430
+ assert.match(r.out, /feature\.json is not a plain file, so it was left alone/);
1431
+ assert.equal(fs.statSync(fj).isDirectory(), true);
1432
+ fs.rmSync(path.join(api, '.specify'), { recursive: true });
1433
+ const elsewhere = path.join(W, 'elsewhere');
1434
+ fs.mkdirSync(elsewhere);
1435
+ fs.symlinkSync(elsewhere, path.join(api, '.specify'));
1436
+ r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'tasks', 'in_progress');
1437
+ assert.equal(r.code, 0, r.out);
1438
+ assert.match(r.out, /api\/\.specify is not a plain folder, so Spec Kit's feature folder was not pinned/);
1439
+ assert.deepEqual(fs.readdirSync(elsewhere), []);
1440
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1441
+ });
1442
+
1443
+ test('yad build step spec in_progress: a repo not checked out is said, and the step is still recorded (#348)', () => {
1444
+ const { W, P, dir } = workspace();
1445
+ try {
1446
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'web');
1447
+ const r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'web', 'spec', 'in_progress');
1448
+ assert.equal(r.code, 0, r.out);
1449
+ assert.match(r.out, /web is not checked out here, so Spec Kit's feature folder was not pinned/);
1450
+ assert.equal(read(path.join(dir, '.sdlc/build-state/EP-l-S01.json')).repos.web.steps[0].status, 'in_progress');
1451
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1452
+ });
1453
+
1454
+ test('yad build step spec in_progress: a pin that cannot run never fails the recorded step (#348 review 2)', () => {
1455
+ const { W, P, dir, api } = workspace();
1456
+ try {
1457
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
1458
+ write(api, { '.specify/memory/constitution.md': '# c\n' });
1459
+ // The folder is a clone of another repo than the one registered: the lookup throws, the pin is a warning
1460
+ // that keeps the lookup's fix, and the step stands.
1461
+ git(api, 'remote', 'set-url', 'origin', 'https://github.com/team/api.git');
1462
+ write(P, { '.sdlc/repos.json': JSON.stringify({ repos: [{ name: 'api', path: '../api', git_url: 'https://github.com/team/other.git' }, { name: 'web', path: '../web' }] }) });
1463
+ const r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', 'spec', 'in_progress', '--json');
1464
+ assert.equal(r.code, 0, r.out);
1465
+ assert.match(r.out, /Spec Kit folder not pinned: \.\.\/api is a clone of team\/api, not team\/other — fix the path of api/);
1466
+ assert.equal('specKit' in json(r), false);
1467
+ assert.equal(fs.existsSync(path.join(api, '.specify/feature.json')), false);
1468
+ assert.equal(read(path.join(dir, '.sdlc/build-state/EP-l-S01.json')).repos.api.steps[0].status, 'in_progress');
1469
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1470
+ });
1471
+
1472
+ // A light spec (#348): one spec.md with the spec, the plan and the tasks; link.md says spec-mode: light.
1473
+ const LIGHT_LINK = '---\nstory: EP-l-S01\nepic: EP-l\nrepo: api\nfeature-id: EP-l-S01\nproduct-repo: ../../../product\ncontract-lock: none\nspeckit: not-used\nspec-mode: light\ngenerated: 2026-10-25\n---\n# Spec link\n';
1474
+ const lightSpec = (tasks) => `# Spec — EP-l-S01\n\n## Acceptance criteria\n- T09 is not a task: it is above the block.\n\n## Plan\n1. T08 neither.\n\n## Tasks\n${tasks}`;
1475
+ const LIGHT_TASKS_OK = '<!-- yad:tasks -->\n## T01 — create the order\n**Files:** src/T01.js\n\n## T02 — list orders\n**Files:** src/T02.js\n<!-- /yad:tasks -->\n';
1476
+ function walkToReview(P) {
1477
+ yad(P, 'build', 'start', 'EP-l-S01', '--repo', 'api');
1478
+ yad(P, 'skip', 'EP-l', 'EP-l-S01', '--repo', 'web', '--reason', 'no UI change');
1479
+ for (const step of ['spec', 'tasks', 'implement', 'checks']) {
1480
+ const r = yad(P, 'build', 'step', 'EP-l-S01', '--repo', 'api', step, 'done');
1481
+ assert.equal(r.code, 0, r.out);
1482
+ }
1483
+ }
1484
+
1485
+ test('yad build done reads a light spec\'s tasks from the marked block of spec.md (#348)', () => {
1486
+ const { W, P, dir, sha } = workspace({ spec: { 'specs/EP-l-S01/link.md': LIGHT_LINK, 'specs/EP-l-S01/spec.md': lightSpec(LIGHT_TASKS_OK) } });
1487
+ try {
1488
+ walkToReview(P);
1489
+ let r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', sha.T01);
1490
+ assert.equal(r.code, 0, r.out);
1491
+ // T02 is owed; T08 and T09 above the block are not.
1492
+ r = yad(P, 'build', 'done', 'EP-l-S01');
1493
+ assert.equal(r.code, 1, r.out);
1494
+ assert.match(r.out, /no ship yet for: api\/T02/);
1495
+ assert.doesNotMatch(r.out, /T0[89]/);
1496
+ r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T02', '--repo', 'api', '--merge-commit', sha.T02);
1497
+ assert.equal(r.code, 0, r.out);
1498
+ r = yad(P, 'build', 'done', 'EP-l-S01');
1499
+ assert.equal(r.code, 0, r.out);
1500
+ assert.equal(status(dir), 'shipped');
1501
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1502
+ });
1503
+
1504
+ test('yad build done: a spec.md tasks block counts only for a light spec, and only when it is whole (#348)', () => {
1505
+ const cases = [
1506
+ // link.md does not say light: the missing tasks.md is missing, whatever spec.md holds.
1507
+ [{ 'specs/EP-l-S01/link.md': LIGHT_LINK.replace('spec-mode: light\n', ''), 'specs/EP-l-S01/spec.md': lightSpec(LIGHT_TASKS_OK) }, /its link\.md does not say `spec-mode: light` \(plain, on its own line\), so the tasks block in spec\.md is not read/],
1508
+ // A link.md with Windows line endings is not read as light: named, not hidden behind "no tasks.md".
1509
+ [{ 'specs/EP-l-S01/link.md': LIGHT_LINK.replace(/\n/g, '\r\n'), 'specs/EP-l-S01/spec.md': lightSpec(LIGHT_TASKS_OK) }, /its link\.md does not say `spec-mode: light`/],
1510
+ [{ 'specs/EP-l-S01/link.md': LIGHT_LINK.replace('spec-mode: light', 'spec-mode: full') }, /no specs\/EP-l-S01\/tasks\.md on origin\/main/],
1511
+ [{ 'specs/EP-l-S01/link.md': LIGHT_LINK }, /says spec-mode: light, but there is no spec\.md/],
1512
+ [{ 'specs/EP-l-S01/link.md': LIGHT_LINK, 'specs/EP-l-S01/spec.md': lightSpec('## T01 — no markers\n') }, /is a light spec without one <!-- yad:tasks --> … <!-- \/yad:tasks --> block/],
1513
+ [{ 'specs/EP-l-S01/link.md': LIGHT_LINK, 'specs/EP-l-S01/spec.md': lightSpec(LIGHT_TASKS_OK + LIGHT_TASKS_OK) }, /is a light spec without one/],
1514
+ [{ 'specs/EP-l-S01/link.md': LIGHT_LINK, 'specs/EP-l-S01/spec.md': lightSpec('<!-- /yad:tasks -->\n## T01 — x\n<!-- yad:tasks -->\n') }, /is a light spec without one/],
1515
+ [{ 'specs/EP-l-S01/link.md': LIGHT_LINK, 'specs/EP-l-S01/spec.md': lightSpec('<!-- yad:tasks -->\nnothing yet\n<!-- /yad:tasks -->\n') }, /the tasks block of specs\/EP-l-S01\/spec\.md on origin\/main names no task/],
1516
+ ];
1517
+ for (const [spec, why] of cases) {
1518
+ const { W, P, sha } = workspace({ spec });
1519
+ try {
1520
+ walkToReview(P);
1521
+ yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', sha.T01);
1522
+ const r = yad(P, 'build', 'done', 'EP-l-S01');
1523
+ assert.equal(r.code, 1, r.out);
1524
+ assert.match(r.out, why);
1525
+ assert.match(r.out, /EP-l-S01 is not shipped yet/);
1526
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1527
+ }
1528
+ });
1529
+
1530
+ test('the light-spec markers the skills write are the ones yad build done reads (#348)', async () => {
1531
+ const { LIGHT_TASKS, lightTasksBlock } = await import('./build.mjs');
1532
+ const read = (f) => fs.readFileSync(path.join(ROOT, f), 'utf8');
1533
+ for (const f of ['skills/yad-spec/SKILL.md', 'skills/yad-spec/references/spec-handoff.md', 'skills/yad-implement/SKILL.md', 'docs/CLI.md']) {
1534
+ const text = read(f);
1535
+ assert.ok(text.includes(LIGHT_TASKS.begin) && text.includes(LIGHT_TASKS.end), f);
1536
+ }
1537
+ assert.match(read('skills/yad-spec/SKILL.md'), /`spec-mode: light`/);
1538
+ assert.match(read('skills/yad-spec/references/spec-handoff.md'), /^spec-mode: full \| light$/m);
1539
+ // The example in spec-handoff.md is a block build done can read.
1540
+ const example = read('skills/yad-spec/references/spec-handoff.md').match(/```markdown\n(# Spec — [\s\S]*?)```/)[1];
1541
+ assert.match(lightTasksBlock(example), /^## T01 — /m);
1542
+ });
1543
+
1544
+ test('yad build done: a tasks.md that exists wins over a light spec\'s tasks block (#348 review 1)', () => {
1545
+ const { W, P, dir, sha } = workspace({ spec: {
1546
+ 'specs/EP-l-S01/tasks.md': '# Tasks\n\n- [ ] T01 — create the order\n',
1547
+ 'specs/EP-l-S01/link.md': LIGHT_LINK,
1548
+ 'specs/EP-l-S01/spec.md': lightSpec(LIGHT_TASKS_OK),
1549
+ } });
1550
+ try {
1551
+ walkToReview(P);
1552
+ let r = yad(P, 'build', 'ship', 'EP-l-S01', '--task', 'T01', '--repo', 'api', '--merge-commit', sha.T01);
1553
+ assert.equal(r.code, 0, r.out);
1554
+ r = yad(P, 'build', 'done', 'EP-l-S01');
1555
+ assert.equal(r.code, 0, r.out);
1556
+ assert.doesNotMatch(r.out, /T02/);
1557
+ assert.equal(status(dir), 'shipped');
1558
+ } finally { fs.rmSync(W, { recursive: true, force: true }); }
1559
+ });